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}
diff --git a/modules/desktop_capture/linux/wayland/screencast_portal.cc b/modules/desktop_capture/linux/wayland/screencast_portal.cc
index 3d28d42..a473802 100644
--- a/modules/desktop_capture/linux/wayland/screencast_portal.cc
+++ b/modules/desktop_capture/linux/wayland/screencast_portal.cc
@@ -80,9 +80,9 @@
   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 @@
 
   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);
diff --git a/modules/desktop_capture/linux/wayland/screencast_portal.h b/modules/desktop_capture/linux/wayland/screencast_portal.h
index 3d59ce4..2bf7800 100644
--- a/modules/desktop_capture/linux/wayland/screencast_portal.h
+++ b/modules/desktop_capture/linux/wayland/screencast_portal.h
@@ -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 @@
   // 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_;
 
diff --git a/modules/desktop_capture/linux/wayland/shared_screencast_stream.cc b/modules/desktop_capture/linux/wayland/shared_screencast_stream.cc
index 17c8950..5b4e23c 100644
--- a/modules/desktop_capture/linux/wayland/shared_screencast_stream.cc
+++ b/modules/desktop_capture/linux/wayland/shared_screencast_stream.cc
@@ -448,7 +448,7 @@
   {
     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 @@
 }
 
 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(
diff --git a/modules/desktop_capture/linux/wayland/test/test_screencast_stream_provider.cc b/modules/desktop_capture/linux/wayland/test/test_screencast_stream_provider.cc
index ffba137..ee5c17e 100644
--- a/modules/desktop_capture/linux/wayland/test/test_screencast_stream_provider.cc
+++ b/modules/desktop_capture/linux/wayland/test/test_screencast_stream_provider.cc
@@ -312,7 +312,7 @@
   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;
   }
diff --git a/modules/portal/pipewire_utils.h b/modules/portal/pipewire_utils.h
index 0f5ccf3..8344a8c 100644
--- a/modules/portal/pipewire_utils.h
+++ b/modules/portal/pipewire_utils.h
@@ -15,6 +15,8 @@
 
 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.
diff --git a/modules/video_capture/linux/camera_portal.cc b/modules/video_capture/linux/camera_portal.cc
index d217dc7..85b9f20 100644
--- a/modules/video_capture/linux/camera_portal.cc
+++ b/modules/video_capture/linux/camera_portal.cc
@@ -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 @@
   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 @@
 
   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);
diff --git a/modules/video_capture/linux/pipewire_session.cc b/modules/video_capture/linux/pipewire_session.cc
index f628bfc..2adb1cd 100644
--- a/modules/video_capture/linux/pipewire_session.cc
+++ b/modules/video_capture/linux/pipewire_session.cc
@@ -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 @@
 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);
diff --git a/modules/video_capture/linux/pipewire_session.h b/modules/video_capture/linux/pipewire_session.h
index 54555ed..982b468 100644
--- a/modules/video_capture/linux/pipewire_session.h
+++ b/modules/video_capture/linux/pipewire_session.h
@@ -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 @@
   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_; }
 
diff --git a/modules/video_capture/video_capture_options.h b/modules/video_capture/video_capture_options.h
index c90e035..6f72f79 100644
--- a/modules/video_capture/video_capture_options.h
+++ b/modules/video_capture/video_capture_options.h
@@ -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 @@
 #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
 };