Use a constant for invalid PipeWire file descriptor

We use value -1 on over all the places through our code so it might be
better to define a constant and use it instead to make the code more
understandable on first look.

Bug: webrtc:15203
Change-Id: I4fc3e561bc7a7778c43ec6cfde7acebef2af79e8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/306620
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Commit-Queue: Jan Grulich <grulja@gmail.com>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40156}
This commit is contained in:
Jan Grulich 2023-05-25 19:16:32 +02:00 committed by WebRTC LUCI CQ
parent 0f1a2c5d97
commit 9caef2a8b8
9 changed files with 23 additions and 13 deletions

View file

@ -80,9 +80,9 @@ void ScreenCastPortal::Stop() {
proxy_ = nullptr;
restore_token_ = "";
if (pw_fd_ != -1) {
if (pw_fd_ != kInvalidPipeWireFd) {
close(pw_fd_);
pw_fd_ = -1;
pw_fd_ = kInvalidPipeWireFd;
}
}
@ -456,7 +456,7 @@ void ScreenCastPortal::OnOpenPipeWireRemoteRequested(GDBusProxy* proxy,
that->pw_fd_ = g_unix_fd_list_get(outlist.get(), index, error.receive());
if (that->pw_fd_ == -1) {
if (that->pw_fd_ == kInvalidPipeWireFd) {
RTC_LOG(LS_ERROR) << "Failed to get file descriptor from the list: "
<< error->message;
that->OnPortalDone(RequestResponse::kError);

View file

@ -17,6 +17,7 @@
#include "modules/desktop_capture/desktop_capture_types.h"
#include "modules/desktop_capture/linux/wayland/screen_capture_portal_interface.h"
#include "modules/portal/pipewire_utils.h"
#include "modules/portal/portal_request_response.h"
#include "modules/portal/xdg_desktop_portal_utils.h"
#include "modules/portal/xdg_session_details.h"
@ -137,7 +138,7 @@ class RTC_EXPORT ScreenCastPortal
// A PipeWire stream ID of stream we will be connecting to
uint32_t pw_stream_node_id_ = 0;
// A file descriptor of PipeWire socket
int pw_fd_ = -1;
int pw_fd_ = kInvalidPipeWireFd;
// Restore token that can be used to restore previous session
std::string restore_token_;

View file

@ -448,7 +448,7 @@ bool SharedScreenCastStreamPrivate::StartScreenCastStream(
{
PipeWireThreadLoopLock thread_loop_lock(pw_main_loop_);
if (fd >= 0) {
if (fd != kInvalidPipeWireFd) {
pw_core_ = pw_context_connect_fd(
pw_context_, fcntl(fd, F_DUPFD_CLOEXEC, 0), nullptr, 0);
} else {
@ -978,7 +978,7 @@ SharedScreenCastStream::CreateDefault() {
}
bool SharedScreenCastStream::StartScreenCastStream(uint32_t stream_node_id) {
return private_->StartScreenCastStream(stream_node_id, -1);
return private_->StartScreenCastStream(stream_node_id, kInvalidPipeWireFd);
}
bool SharedScreenCastStream::StartScreenCastStream(

View file

@ -312,7 +312,7 @@ void TestScreenCastStreamProvider::OnStreamAddBuffer(void* data,
spa_data->type = SPA_DATA_MemFd;
spa_data->fd =
memfd_create("pipewire-test-memfd", MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (spa_data->fd == -1) {
if (spa_data->fd == kInvalidPipeWireFd) {
RTC_LOG(LS_ERROR) << "PipeWire test: Can't create memfd";
return;
}

View file

@ -15,6 +15,8 @@ struct pw_thread_loop;
namespace webrtc {
constexpr int kInvalidPipeWireFd = -1;
// Prepare PipeWire so that it is ready to be used. If it needs to be dlopen'd
// this will do so. Note that this does not guarantee a PipeWire server is
// running nor does it establish a connection to one.

View file

@ -13,6 +13,7 @@
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include "modules/portal/pipewire_utils.h"
#include "modules/portal/xdg_desktop_portal_utils.h"
namespace webrtc {
@ -31,7 +32,8 @@ class CameraPortalPrivate {
void Start();
private:
void OnPortalDone(xdg_portal::RequestResponse result, int fd = -1);
void OnPortalDone(xdg_portal::RequestResponse result,
int fd = kInvalidPipeWireFd);
static void OnProxyRequested(GObject* object,
GAsyncResult* result,
@ -216,7 +218,7 @@ void CameraPortalPrivate::OnOpenResponse(GDBusProxy* proxy,
int fd = g_unix_fd_list_get(outlist.get(), index, error.receive());
if (fd == -1) {
if (fd == kInvalidPipeWireFd) {
RTC_LOG(LS_ERROR) << "Failed to get file descriptor from the list: "
<< error->message;
that->OnPortalDone(RequestResponse::kError);

View file

@ -17,7 +17,6 @@
#include <spa/pod/parser.h>
#include "common_video/libyuv/include/webrtc_libyuv.h"
#include "modules/portal/pipewire_utils.h"
#include "modules/video_capture/device_info_impl.h"
#include "rtc_base/logging.h"
#include "rtc_base/string_encode.h"
@ -235,7 +234,7 @@ PipeWireSession::~PipeWireSession() {
void PipeWireSession::Init(VideoCaptureOptions::Callback* callback, int fd) {
callback_ = callback;
if (fd != -1) {
if (fd != kInvalidPipeWireFd) {
InitPipeWire(fd);
} else {
portal_notifier_ = std::make_unique<CameraPortalNotifier>(this);

View file

@ -20,6 +20,7 @@
#include "api/ref_counted_base.h"
#include "api/scoped_refptr.h"
#include "modules/portal/pipewire_utils.h"
#include "modules/video_capture/linux/camera_portal.h"
#include "modules/video_capture/video_capture.h"
#include "modules/video_capture/video_capture_options.h"
@ -83,7 +84,8 @@ class PipeWireSession : public rtc::RefCountedNonVirtual<PipeWireSession> {
PipeWireSession();
~PipeWireSession();
void Init(VideoCaptureOptions::Callback* callback, int fd = -1);
void Init(VideoCaptureOptions::Callback* callback,
int fd = kInvalidPipeWireFd);
const std::deque<PipeWireNode>& nodes() const { return nodes_; }

View file

@ -13,6 +13,10 @@
#include "api/scoped_refptr.h"
#include "rtc_base/system/rtc_export.h"
#if defined(WEBRTC_USE_PIPEWIRE)
#include "modules/portal/pipewire_utils.h"
#endif
namespace webrtc {
#if defined(WEBRTC_USE_PIPEWIRE)
@ -69,7 +73,7 @@ class RTC_EXPORT VideoCaptureOptions {
#endif
#if defined(WEBRTC_USE_PIPEWIRE)
bool allow_pipewire_ = false;
int pipewire_fd_ = -1;
int pipewire_fd_ = kInvalidPipeWireFd;
rtc::scoped_refptr<videocapturemodule::PipeWireSession> pipewire_session_;
#endif
};