cd486f
diff -up firefox-60.3.0/config/system-headers.mozbuild.pipewire firefox-60.3.0/config/system-headers.mozbuild
cd486f
--- firefox-60.3.0/config/system-headers.mozbuild.pipewire	2018-10-17 22:39:27.000000000 +0200
cd486f
+++ firefox-60.3.0/config/system-headers.mozbuild	2019-01-07 14:53:17.200061752 +0100
cd486f
@@ -314,6 +314,7 @@ system_headers = [
cd486f
     'Gestalt.h',
cd486f
     'getopt.h',
cd486f
     'gio/gio.h',
cd486f
+    'gio/gunixfdlist.h',
cd486f
     'glibconfig.h',
cd486f
     'glib.h',
cd486f
     'glib-object.h',
cd486f
@@ -607,6 +608,7 @@ system_headers = [
cd486f
     'Pgenerr.h',
cd486f
     'PGenErr.h',
cd486f
     'Ph.h',
cd486f
+    'pipewire/pipewire.h',
cd486f
     'pixman.h',
cd486f
     'pk11func.h',
cd486f
     'pk11pqg.h',
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_linux.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_linux.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_linux.cc.pipewire	2019-01-07 14:53:17.200061752 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_linux.cc	2019-01-07 14:53:17.200061752 +0100
cd486f
@@ -0,0 +1,39 @@
cd486f
+/*
cd486f
+ *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+#include "webrtc/modules/desktop_capture/app_capturer_x11.h"
cd486f
+#endif // defined(USE_X11)
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+// static
cd486f
+AppCapturer* AppCapturer::Create(const DesktopCaptureOptions& options) {
cd486f
+#if defined(USE_X11)
cd486f
+    return AppCapturerX11::Create(options);
cd486f
+#endif // defined(USE_X11)
cd486f
+
cd486f
+  return nullptr;
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawAppCapturer(
cd486f
+    const DesktopCaptureOptions& options) {
cd486f
+#if defined(USE_X11)
cd486f
+  return AppCapturerX11::CreateRawAppCapturer(options);
cd486f
+#endif // defined(USE_X11)
cd486f
+
cd486f
+  return nullptr;
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.cc.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.cc	2019-01-07 14:53:17.201061764 +0100
cd486f
@@ -7,9 +7,6 @@
cd486f
 *  in the file PATENTS.  All contributing project authors may
cd486f
 *  be found in the AUTHORS file in the root of the source tree.
cd486f
 */
cd486f
-#include "webrtc/modules/desktop_capture/app_capturer.h"
cd486f
-#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
cd486f
-#include "webrtc/modules/desktop_capture/x11/shared_x_util.h"
cd486f
 
cd486f
 #include <assert.h>
cd486f
 #include <string.h>
cd486f
@@ -21,80 +18,19 @@
cd486f
 
cd486f
 #include <algorithm>
cd486f
 
cd486f
+#include "webrtc/modules/desktop_capture/app_capturer_x11.h"
cd486f
+
cd486f
 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
 #include "webrtc/modules/desktop_capture/desktop_frame.h"
cd486f
 #include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/shared_x_util.h"
cd486f
 #include "webrtc/modules/desktop_capture/x11/x_error_trap.h"
cd486f
 #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
cd486f
 #include "webrtc/system_wrappers/include/logging.h"
cd486f
 
cd486f
 namespace webrtc {
cd486f
 
cd486f
-namespace {
cd486f
-
cd486f
-class ScreenCapturerProxy : DesktopCapturer::Callback {
cd486f
-public:
cd486f
-  ScreenCapturerProxy()
cd486f
-    : screen_capturer_(DesktopCapturer::CreateScreenCapturer(DesktopCaptureOptions::CreateDefault())) {
cd486f
-    screen_capturer_->SelectSource(kFullDesktopScreenId);
cd486f
-    screen_capturer_->Start(this);
cd486f
-  }
cd486f
-  void CaptureFrame() { screen_capturer_->CaptureFrame(); }
cd486f
-  std::unique_ptr<DesktopFrame> GetFrame() { return std::move(frame_); }
cd486f
-
cd486f
-   // Callback interface
cd486f
-  virtual void OnCaptureResult(DesktopCapturer::Result result,
cd486f
-                               std::unique_ptr<DesktopFrame> frame) {
cd486f
-    frame_ = std::move(frame);
cd486f
-  }
cd486f
-
cd486f
-protected:
cd486f
-  std::unique_ptr<DesktopCapturer> screen_capturer_;
cd486f
-  std::unique_ptr<DesktopFrame> frame_;
cd486f
-};
cd486f
-
cd486f
-class AppCapturerLinux : public AppCapturer {
cd486f
-public:
cd486f
-  AppCapturerLinux(const DesktopCaptureOptions& options);
cd486f
-  virtual ~AppCapturerLinux();
cd486f
-
cd486f
-  // AppCapturer interface.
cd486f
-  virtual bool GetAppList(AppList* apps) override;
cd486f
-  virtual bool SelectApp(ProcessId processId) override;
cd486f
-  virtual bool BringAppToFront() override;
cd486f
-
cd486f
-  // DesktopCapturer interface.
cd486f
-  virtual void Start(Callback* callback) override;
cd486f
-  virtual void Stop() override;
cd486f
-  virtual void CaptureFrame() override;
cd486f
-  virtual bool SelectSource(SourceId id) override
cd486f
-  {
cd486f
-    return SelectApp(static_cast<ProcessId>(id));
cd486f
-  }
cd486f
-
cd486f
-protected:
cd486f
-  Display* GetDisplay() { return x_display_->display(); }
cd486f
-  bool UpdateRegions();
cd486f
-
cd486f
-  void FillDesktopFrameRegionWithColor(DesktopFrame* pDesktopFrame,Region rgn, uint32_t color);
cd486f
-private:
cd486f
-  Callback* callback_;
cd486f
-  ProcessId selected_process_;
cd486f
-
cd486f
-  // Sample Mode
cd486f
-  ScreenCapturerProxy screen_capturer_proxy_;
cd486f
-  // Mask of foreground (non-app windows in front of selected)
cd486f
-  Region rgn_mask_;
cd486f
-  // Region of selected windows
cd486f
-  Region rgn_visual_;
cd486f
-  // Mask of background (desktop, non-app windows behind selected)
cd486f
-  Region rgn_background_;
cd486f
-
cd486f
-  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
-  RTC_DISALLOW_COPY_AND_ASSIGN(AppCapturerLinux);
cd486f
-};
cd486f
-
cd486f
-AppCapturerLinux::AppCapturerLinux(const DesktopCaptureOptions& options)
cd486f
+AppCapturerX11::AppCapturerX11(const DesktopCaptureOptions& options)
cd486f
     : callback_(NULL),
cd486f
       selected_process_(0),
cd486f
       x_display_(options.x_display()) {
cd486f
@@ -103,7 +39,7 @@ AppCapturerLinux::AppCapturerLinux(const
cd486f
   rgn_background_ = XCreateRegion();
cd486f
 }
cd486f
 
cd486f
-AppCapturerLinux::~AppCapturerLinux() {
cd486f
+AppCapturerX11::~AppCapturerX11() {
cd486f
   if (rgn_mask_) {
cd486f
     XDestroyRegion(rgn_mask_);
cd486f
   }
cd486f
@@ -116,32 +52,32 @@ AppCapturerLinux::~AppCapturerLinux() {
cd486f
 }
cd486f
 
cd486f
 // AppCapturer interface.
cd486f
-bool AppCapturerLinux::GetAppList(AppList* apps) {
cd486f
+bool AppCapturerX11::GetAppList(AppList* apps) {
cd486f
   // Implemented in DesktopDeviceInfo
cd486f
   return true;
cd486f
 }
cd486f
-bool AppCapturerLinux::SelectApp(ProcessId processId) {
cd486f
+bool AppCapturerX11::SelectApp(ProcessId processId) {
cd486f
   selected_process_ = processId;
cd486f
   return true;
cd486f
 }
cd486f
-bool AppCapturerLinux::BringAppToFront() {
cd486f
+bool AppCapturerX11::BringAppToFront() {
cd486f
   // Not implemented yet: See Bug 1036653
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
 // DesktopCapturer interface.
cd486f
-void AppCapturerLinux::Start(Callback* callback) {
cd486f
+void AppCapturerX11::Start(Callback* callback) {
cd486f
   assert(!callback_);
cd486f
   assert(callback);
cd486f
 
cd486f
   callback_ = callback;
cd486f
 }
cd486f
 
cd486f
-void AppCapturerLinux::Stop() {
cd486f
+void AppCapturerX11::Stop() {
cd486f
   callback_ = NULL;
cd486f
 }
cd486f
 
cd486f
-void AppCapturerLinux::CaptureFrame() {
cd486f
+void AppCapturerX11::CaptureFrame() {
cd486f
   XErrorTrap error_trap(GetDisplay());
cd486f
 
cd486f
   //Capture screen >> set root window as capture window
cd486f
@@ -169,7 +105,7 @@ void AppCapturerLinux::CaptureFrame() {
cd486f
   }
cd486f
 }
cd486f
 
cd486f
-void AppCapturerLinux::FillDesktopFrameRegionWithColor(DesktopFrame* pDesktopFrame, Region rgn, uint32_t color) {
cd486f
+void AppCapturerX11::FillDesktopFrameRegionWithColor(DesktopFrame* pDesktopFrame, Region rgn, uint32_t color) {
cd486f
   XErrorTrap error_trap(GetDisplay());
cd486f
 
cd486f
   if (!pDesktopFrame) {
cd486f
@@ -192,7 +128,7 @@ void AppCapturerLinux::FillDesktopFrameR
cd486f
   }
cd486f
 }
cd486f
 
cd486f
-bool AppCapturerLinux::UpdateRegions() {
cd486f
+bool AppCapturerX11::UpdateRegions() {
cd486f
   XErrorTrap error_trap(GetDisplay());
cd486f
 
cd486f
   XSubtractRegion(rgn_visual_, rgn_visual_, rgn_visual_);
cd486f
@@ -269,21 +205,19 @@ bool AppCapturerLinux::UpdateRegions() {
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-}  // namespace
cd486f
-
cd486f
 // static
cd486f
-AppCapturer* AppCapturer::Create(const DesktopCaptureOptions& options) {
cd486f
-  return new AppCapturerLinux(options);
cd486f
+AppCapturer* AppCapturerX11::Create(const DesktopCaptureOptions& options) {
cd486f
+  return new AppCapturerX11(options);
cd486f
 }
cd486f
 
cd486f
 // static
cd486f
-std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawAppCapturer(
cd486f
+std::unique_ptr<DesktopCapturer> AppCapturerX11::CreateRawAppCapturer(
cd486f
     const DesktopCaptureOptions& options) {
cd486f
 
cd486f
   if (!options.x_display())
cd486f
     return nullptr;
cd486f
 
cd486f
-  std::unique_ptr<AppCapturerLinux> capturer(new AppCapturerLinux(options));
cd486f
+  std::unique_ptr<AppCapturerX11> capturer(new AppCapturerX11(options));
cd486f
 
cd486f
   return std::unique_ptr<DesktopCapturer>(std::move(capturer));
cd486f
 }
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.h.pipewire	2019-01-07 14:53:17.201061764 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_x11.h	2019-01-07 14:53:17.201061764 +0100
cd486f
@@ -0,0 +1,98 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_APP_CAPTURER_X11_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_APP_CAPTURER_X11_H_
cd486f
+
cd486f
+#include <X11/X.h>
cd486f
+#include <X11/Xlib.h>
cd486f
+#include <memory>
cd486f
+#include <string>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/app_capturer.h"
cd486f
+#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_geometry.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
cd486f
+#include "webrtc/base/constructormagic.h"
cd486f
+#include "webrtc/base/scoped_ref_ptr.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class ScreenCapturerProxy : DesktopCapturer::Callback {
cd486f
+public:
cd486f
+  ScreenCapturerProxy()
cd486f
+    : screen_capturer_(DesktopCapturer::CreateScreenCapturer(DesktopCaptureOptions::CreateDefault())) {
cd486f
+    screen_capturer_->SelectSource(kFullDesktopScreenId);
cd486f
+    screen_capturer_->Start(this);
cd486f
+  }
cd486f
+  void CaptureFrame() { screen_capturer_->CaptureFrame(); }
cd486f
+  std::unique_ptr<DesktopFrame> GetFrame() { return std::move(frame_); }
cd486f
+
cd486f
+   // Callback interface
cd486f
+  virtual void OnCaptureResult(DesktopCapturer::Result result,
cd486f
+                               std::unique_ptr<DesktopFrame> frame) {
cd486f
+    frame_ = std::move(frame);
cd486f
+  }
cd486f
+
cd486f
+protected:
cd486f
+  std::unique_ptr<DesktopCapturer> screen_capturer_;
cd486f
+  std::unique_ptr<DesktopFrame> frame_;
cd486f
+};
cd486f
+
cd486f
+class AppCapturerX11 : public AppCapturer {
cd486f
+public:
cd486f
+  AppCapturerX11(const DesktopCaptureOptions& options);
cd486f
+  virtual ~AppCapturerX11();
cd486f
+
cd486f
+  static AppCapturer* Create(const DesktopCaptureOptions& options);
cd486f
+  static std::unique_ptr<DesktopCapturer> CreateRawAppCapturer(const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  // AppCapturer interface.
cd486f
+  virtual bool GetAppList(AppList* apps) override;
cd486f
+  virtual bool SelectApp(ProcessId processId) override;
cd486f
+  virtual bool BringAppToFront() override;
cd486f
+
cd486f
+  // DesktopCapturer interface.
cd486f
+  virtual void Start(Callback* callback) override;
cd486f
+  virtual void Stop() override;
cd486f
+  virtual void CaptureFrame() override;
cd486f
+  virtual bool SelectSource(SourceId id) override
cd486f
+  {
cd486f
+    return SelectApp(static_cast<ProcessId>(id));
cd486f
+  }
cd486f
+
cd486f
+protected:
cd486f
+  Display* GetDisplay() { return x_display_->display(); }
cd486f
+  bool UpdateRegions();
cd486f
+
cd486f
+  void FillDesktopFrameRegionWithColor(DesktopFrame* pDesktopFrame,Region rgn, uint32_t color);
cd486f
+private:
cd486f
+  Callback* callback_;
cd486f
+  ProcessId selected_process_;
cd486f
+
cd486f
+  // Sample Mode
cd486f
+  ScreenCapturerProxy screen_capturer_proxy_;
cd486f
+  // Mask of foreground (non-app windows in front of selected)
cd486f
+  Region rgn_mask_;
cd486f
+  // Region of selected windows
cd486f
+  Region rgn_visual_;
cd486f
+  // Mask of background (desktop, non-app windows behind selected)
cd486f
+  Region rgn_background_;
cd486f
+
cd486f
+  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(AppCapturerX11);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_APP_CAPTURER_X11_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.cc.pipewire	2019-01-07 14:53:17.201061764 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.cc	2019-01-07 14:53:17.201061764 +0100
cd486f
@@ -0,0 +1,849 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include <cstring>
cd486f
+#include <gio/gunixfdlist.h>
cd486f
+#include <glib-object.h>
cd486f
+
cd486f
+#include <spa/param/format-utils.h>
cd486f
+#include <spa/param/props.h>
cd486f
+#include <spa/param/video/raw-utils.h>
cd486f
+#include <spa/support/type-map.h>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/base_capturer_pipewire.h"
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+#include "webrtc/base/checks.h"
cd486f
+#include "webrtc/base/logging.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+const char kDesktopBusName[] = "org.freedesktop.portal.Desktop";
cd486f
+const char kDesktopObjectPath[] = "/org/freedesktop/portal/desktop";
cd486f
+const char kDesktopRequestObjectPath[] =
cd486f
+    "/org/freedesktop/portal/desktop/request";
cd486f
+const char kSessionInterfaceName[] = "org.freedesktop.portal.Session";
cd486f
+const char kRequestInterfaceName[] = "org.freedesktop.portal.Request";
cd486f
+const char kScreenCastInterfaceName[] = "org.freedesktop.portal.ScreenCast";
cd486f
+
cd486f
+const int kBytesPerPixelPw = 4;
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStateChanged(void* data,
cd486f
+                                            pw_remote_state old_state,
cd486f
+                                            pw_remote_state state,
cd486f
+                                            const char* error_message) {
cd486f
+  BaseCapturerPipeWire* that = static_cast<BaseCapturerPipeWire*>(data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  switch (state) {
cd486f
+    case PW_REMOTE_STATE_ERROR:
cd486f
+      LOG(LS_ERROR) << "PipeWire remote state error: " << error_message;
cd486f
+      break;
cd486f
+    case PW_REMOTE_STATE_CONNECTED:
cd486f
+      LOG(LS_INFO) << "PipeWire remote state: connected.";
cd486f
+      that->CreateReceivingStream();
cd486f
+      break;
cd486f
+    case PW_REMOTE_STATE_CONNECTING:
cd486f
+      LOG(LS_INFO) << "PipeWire remote state: connecting.";
cd486f
+      break;
cd486f
+    case PW_REMOTE_STATE_UNCONNECTED:
cd486f
+      LOG(LS_INFO) << "PipeWire remote state: unconnected.";
cd486f
+      break;
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStreamStateChanged(void* data,
cd486f
+                                                  pw_stream_state old_state,
cd486f
+                                                  pw_stream_state state,
cd486f
+                                                  const char* error_message) {
cd486f
+  BaseCapturerPipeWire* that = static_cast<BaseCapturerPipeWire*>(data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  switch (state) {
cd486f
+    case PW_STREAM_STATE_ERROR:
cd486f
+      LOG(LS_ERROR) << "PipeWire stream state error: " << error_message;
cd486f
+      break;
cd486f
+    case PW_STREAM_STATE_CONFIGURE:
cd486f
+      pw_stream_set_active(that->pw_stream_, true);
cd486f
+      break;
cd486f
+    case PW_STREAM_STATE_UNCONNECTED:
cd486f
+    case PW_STREAM_STATE_CONNECTING:
cd486f
+    case PW_STREAM_STATE_READY:
cd486f
+    case PW_STREAM_STATE_PAUSED:
cd486f
+    case PW_STREAM_STATE_STREAMING:
cd486f
+      break;
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStreamFormatChanged(
cd486f
+    void* data,
cd486f
+    const struct spa_pod* format) {
cd486f
+  BaseCapturerPipeWire* that = static_cast<BaseCapturerPipeWire*>(data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  LOG(LS_INFO) << "PipeWire stream format changed.";
cd486f
+
cd486f
+  if (!format) {
cd486f
+    pw_stream_finish_format(that->pw_stream_, /*res=*/0, /*params=*/nullptr,
cd486f
+                            /*n_params=*/0);
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  that->spa_video_format_ = new spa_video_info_raw();
cd486f
+  spa_format_video_raw_parse(format, that->spa_video_format_,
cd486f
+                             &that->pw_type_->format_video);
cd486f
+
cd486f
+  auto width = that->spa_video_format_->size.width;
cd486f
+  auto height = that->spa_video_format_->size.height;
cd486f
+  auto stride = SPA_ROUND_UP_N(width * kBytesPerPixelPw, 4);
cd486f
+  auto size = height * stride;
cd486f
+
cd486f
+  uint8_t buffer[1024] = {};
cd486f
+  auto builder = spa_pod_builder{buffer, sizeof(buffer)};
cd486f
+
cd486f
+  // Setup buffers and meta header for new format.
cd486f
+  const struct spa_pod* params[2];
cd486f
+  params[0] = reinterpret_cast<spa_pod*>(spa_pod_builder_object(
cd486f
+      &builder,
cd486f
+      // id to enumerate buffer requirements
cd486f
+      that->pw_core_type_->param.idBuffers,
cd486f
+      that->pw_core_type_->param_buffers.Buffers,
cd486f
+      // Size: specified as integer (i) and set to specified size
cd486f
+      ":", that->pw_core_type_->param_buffers.size, "i", size,
cd486f
+      // Stride: specified as integer (i) and set to specified stride
cd486f
+      ":", that->pw_core_type_->param_buffers.stride, "i", stride,
cd486f
+      // Buffers: specifies how many buffers we want to deal with, set as
cd486f
+      // integer (i) where preferred number is 8, then allowed number is defined
cd486f
+      // as range (r) from min and max values and it is undecided (u) to allow
cd486f
+      // negotiation
cd486f
+      ":", that->pw_core_type_->param_buffers.buffers, "iru", 8,
cd486f
+      SPA_POD_PROP_MIN_MAX(1, 32),
cd486f
+      // Align: memory alignment of the buffer, set as integer (i) to specified
cd486f
+      // value
cd486f
+      ":", that->pw_core_type_->param_buffers.align, "i", 16));
cd486f
+  params[1] = reinterpret_cast<spa_pod*>(spa_pod_builder_object(
cd486f
+      &builder,
cd486f
+      // id to enumerate supported metadata
cd486f
+      that->pw_core_type_->param.idMeta, that->pw_core_type_->param_meta.Meta,
cd486f
+      // Type: specified as id or enum (I)
cd486f
+      ":", that->pw_core_type_->param_meta.type, "I",
cd486f
+      that->pw_core_type_->meta.Header,
cd486f
+      // Size: size of the metadata, specified as integer (i)
cd486f
+      ":", that->pw_core_type_->param_meta.size, "i",
cd486f
+      sizeof(struct spa_meta_header)));
cd486f
+
cd486f
+  pw_stream_finish_format(that->pw_stream_, /*res=*/0, params, /*n_params=*/2);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStreamProcess(void* data) {
cd486f
+  BaseCapturerPipeWire* that = static_cast<BaseCapturerPipeWire*>(data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  pw_buffer* buf = nullptr;
cd486f
+
cd486f
+  if (!(buf = pw_stream_dequeue_buffer(that->pw_stream_))) {
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  that->HandleBuffer(buf);
cd486f
+
cd486f
+  pw_stream_queue_buffer(that->pw_stream_, buf);
cd486f
+}
cd486f
+
cd486f
+BaseCapturerPipeWire::BaseCapturerPipeWire(CaptureSourceType source_type)
cd486f
+    : capture_source_type_(source_type) {}
cd486f
+
cd486f
+BaseCapturerPipeWire::~BaseCapturerPipeWire() {
cd486f
+  if (pw_main_loop_) {
cd486f
+    pw_thread_loop_stop(pw_main_loop_);
cd486f
+  }
cd486f
+
cd486f
+  if (pw_type_) {
cd486f
+    delete pw_type_;
cd486f
+  }
cd486f
+
cd486f
+  if (spa_video_format_) {
cd486f
+    delete spa_video_format_;
cd486f
+  }
cd486f
+
cd486f
+  if (pw_stream_) {
cd486f
+    pw_stream_destroy(pw_stream_);
cd486f
+  }
cd486f
+
cd486f
+  if (pw_remote_) {
cd486f
+    pw_remote_destroy(pw_remote_);
cd486f
+  }
cd486f
+
cd486f
+  if (pw_core_) {
cd486f
+    pw_core_destroy(pw_core_);
cd486f
+  }
cd486f
+
cd486f
+  if (pw_main_loop_) {
cd486f
+    pw_thread_loop_destroy(pw_main_loop_);
cd486f
+  }
cd486f
+
cd486f
+  if (pw_loop_) {
cd486f
+    pw_loop_destroy(pw_loop_);
cd486f
+  }
cd486f
+
cd486f
+  if (current_frame_) {
cd486f
+    free(current_frame_);
cd486f
+  }
cd486f
+
cd486f
+  if (start_request_signal_id_) {
cd486f
+    g_dbus_connection_signal_unsubscribe(connection_, start_request_signal_id_);
cd486f
+  }
cd486f
+  if (sources_request_signal_id_) {
cd486f
+    g_dbus_connection_signal_unsubscribe(connection_,
cd486f
+                                         sources_request_signal_id_);
cd486f
+  }
cd486f
+  if (session_request_signal_id_) {
cd486f
+    g_dbus_connection_signal_unsubscribe(connection_,
cd486f
+                                         session_request_signal_id_);
cd486f
+  }
cd486f
+
cd486f
+  if (session_handle_) {
cd486f
+    GDBusMessage* message = g_dbus_message_new_method_call(
cd486f
+        kDesktopBusName, session_handle_, kSessionInterfaceName, "Close");
cd486f
+    if (message) {
cd486f
+      GError* error = nullptr;
cd486f
+      g_dbus_connection_send_message(connection_, message,
cd486f
+                                     G_DBUS_SEND_MESSAGE_FLAGS_NONE,
cd486f
+                                     /*out_serial=*/nullptr, &error);
cd486f
+      if (error) {
cd486f
+        LOG(LS_ERROR) << "Failed to close the session: " << error->message;
cd486f
+        g_error_free(error);
cd486f
+      }
cd486f
+      g_object_unref(message);
cd486f
+    }
cd486f
+  }
cd486f
+
cd486f
+  g_free(start_handle_);
cd486f
+  g_free(sources_handle_);
cd486f
+  g_free(session_handle_);
cd486f
+  g_free(portal_handle_);
cd486f
+
cd486f
+  if (proxy_) {
cd486f
+    g_clear_object(&proxy_);
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::InitPortals() {
cd486f
+  g_dbus_proxy_new_for_bus(
cd486f
+      G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, /*info=*/nullptr,
cd486f
+      kDesktopBusName, kDesktopObjectPath, kScreenCastInterfaceName,
cd486f
+      /*cancellable=*/nullptr,
cd486f
+      reinterpret_cast<GAsyncReadyCallback>(OnProxyRequested), this);
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::InitPipeWire() {
cd486f
+  pw_init(/*argc=*/nullptr, /*argc=*/nullptr);
cd486f
+
cd486f
+  pw_loop_ = pw_loop_new(/*properties=*/nullptr);
cd486f
+  pw_main_loop_ = pw_thread_loop_new(pw_loop_, "pipewire-main-loop");
cd486f
+
cd486f
+  pw_core_ = pw_core_new(pw_loop_, /*properties=*/nullptr);
cd486f
+  pw_core_type_ = pw_core_get_type(pw_core_);
cd486f
+  pw_remote_ = pw_remote_new(pw_core_, nullptr, /*user_data_size=*/0);
cd486f
+
cd486f
+  InitPipeWireTypes();
cd486f
+
cd486f
+  // Initialize event handlers, remote end and stream-related.
cd486f
+  pw_remote_events_.version = PW_VERSION_REMOTE_EVENTS;
cd486f
+  pw_remote_events_.state_changed = &OnStateChanged;
cd486f
+
cd486f
+  pw_stream_events_.version = PW_VERSION_STREAM_EVENTS;
cd486f
+  pw_stream_events_.state_changed = &OnStreamStateChanged;
cd486f
+  pw_stream_events_.format_changed = &OnStreamFormatChanged;
cd486f
+  pw_stream_events_.process = &OnStreamProcess;
cd486f
+
cd486f
+  pw_remote_add_listener(pw_remote_, &spa_remote_listener_, &pw_remote_events_,
cd486f
+                         this);
cd486f
+  pw_remote_connect_fd(pw_remote_, pw_fd_);
cd486f
+
cd486f
+  if (pw_thread_loop_start(pw_main_loop_) < 0) {
cd486f
+    LOG(LS_ERROR) << "Failed to start main PipeWire loop";
cd486f
+    portal_init_failed_ = true;
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::InitPipeWireTypes() {
cd486f
+  spa_type_map* map = pw_core_type_->map;
cd486f
+  pw_type_ = new PipeWireType();
cd486f
+
cd486f
+  spa_type_media_type_map(map, &pw_type_->media_type);
cd486f
+  spa_type_media_subtype_map(map, &pw_type_->media_subtype);
cd486f
+  spa_type_format_video_map(map, &pw_type_->format_video);
cd486f
+  spa_type_video_format_map(map, &pw_type_->video_format);
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::CreateReceivingStream() {
cd486f
+  spa_rectangle pwMinScreenBounds = spa_rectangle{1, 1};
cd486f
+  spa_rectangle pwScreenBounds =
cd486f
+      spa_rectangle{static_cast<uint32_t>(desktop_size_.width()),
cd486f
+                    static_cast<uint32_t>(desktop_size_.height())};
cd486f
+
cd486f
+  spa_fraction pwFrameRateMin = spa_fraction{0, 1};
cd486f
+  spa_fraction pwFrameRateMax = spa_fraction{60, 1};
cd486f
+
cd486f
+  pw_properties* reuseProps = pw_properties_new("pipewire.client.reuse", "1",
cd486f
+                                                /*end of varargs*/ nullptr);
cd486f
+  pw_stream_ = pw_stream_new(pw_remote_, "webrtc-consume-stream", reuseProps);
cd486f
+
cd486f
+  uint8_t buffer[1024] = {};
cd486f
+  const spa_pod* params[1];
cd486f
+  spa_pod_builder builder = spa_pod_builder{buffer, sizeof(buffer)};
cd486f
+  params[0] = reinterpret_cast<spa_pod*>(spa_pod_builder_object(
cd486f
+      &builder,
cd486f
+      // id to enumerate formats
cd486f
+      pw_core_type_->param.idEnumFormat, pw_core_type_->spa_format, "I",
cd486f
+      pw_type_->media_type.video, "I", pw_type_->media_subtype.raw,
cd486f
+      // Video format: specified as id or enum (I), preferred format is BGRx,
cd486f
+      // then allowed formats are enumerated (e) and the format is undecided (u)
cd486f
+      // to allow negotiation
cd486f
+      ":", pw_type_->format_video.format, "Ieu", pw_type_->video_format.BGRx,
cd486f
+      SPA_POD_PROP_ENUM(2, pw_type_->video_format.RGBx,
cd486f
+                        pw_type_->video_format.BGRx),
cd486f
+      // Video size: specified as rectangle (R), preferred size is specified as
cd486f
+      // first parameter, then allowed size is defined as range (r) from min and
cd486f
+      // max values and the format is undecided (u) to allow negotiation
cd486f
+      ":", pw_type_->format_video.size, "Rru", &pwScreenBounds, 2,
cd486f
+      &pwMinScreenBounds, &pwScreenBounds,
cd486f
+      // Frame rate: specified as fraction (F) and set to minimum frame rate
cd486f
+      // value
cd486f
+      ":", pw_type_->format_video.framerate, "F", &pwFrameRateMin,
cd486f
+      // Max frame rate: specified as fraction (F), preferred frame rate is set
cd486f
+      // to maximum value, then allowed frame rate is defined as range (r) from
cd486f
+      // min and max values and it is undecided (u) to allow negotiation
cd486f
+      ":", pw_type_->format_video.max_framerate, "Fru", &pwFrameRateMax, 2,
cd486f
+      &pwFrameRateMin, &pwFrameRateMax));
cd486f
+
cd486f
+  pw_stream_add_listener(pw_stream_, &spa_stream_listener_, &pw_stream_events_,
cd486f
+                         this);
cd486f
+  pw_stream_flags flags = static_cast<pw_stream_flags>(
cd486f
+      PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_INACTIVE |
cd486f
+      PW_STREAM_FLAG_MAP_BUFFERS);
cd486f
+  if (pw_stream_connect(pw_stream_, PW_DIRECTION_INPUT, /*port_path=*/nullptr,
cd486f
+                        flags, params,
cd486f
+                        /*n_params=*/1) != 0) {
cd486f
+    LOG(LS_ERROR) << "Could not connect receiving stream.";
cd486f
+    portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::HandleBuffer(pw_buffer* buffer) {
cd486f
+  spa_buffer* spaBuffer = buffer->buffer;
cd486f
+  void* src = nullptr;
cd486f
+
cd486f
+  if (!(src = spaBuffer->datas[0].data)) {
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  uint32_t maxSize = spaBuffer->datas[0].maxsize;
cd486f
+  int32_t srcStride = spaBuffer->datas[0].chunk->stride;
cd486f
+  if (srcStride != (desktop_size_.width() * kBytesPerPixelPw)) {
cd486f
+    LOG(LS_ERROR) << "Got buffer with stride different from screen stride: "
cd486f
+                      << srcStride
cd486f
+                      << " != " << (desktop_size_.width() * kBytesPerPixelPw);
cd486f
+    portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  if (!current_frame_) {
cd486f
+    current_frame_ = static_cast<uint8_t*>(malloc(maxSize));
cd486f
+  }
cd486f
+  RTC_DCHECK(current_frame_ != nullptr);
cd486f
+
cd486f
+  // If both sides decided to go with the RGBx format we need to convert it to
cd486f
+  // BGRx to match color format expected by WebRTC.
cd486f
+  if (spa_video_format_->format == pw_type_->video_format.RGBx) {
cd486f
+    uint8_t* tempFrame = static_cast<uint8_t*>(malloc(maxSize));
cd486f
+    std::memcpy(tempFrame, src, maxSize);
cd486f
+    ConvertRGBxToBGRx(tempFrame, maxSize);
cd486f
+    std::memcpy(current_frame_, tempFrame, maxSize);
cd486f
+    free(tempFrame);
cd486f
+  } else {
cd486f
+    std::memcpy(current_frame_, src, maxSize);
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::ConvertRGBxToBGRx(uint8_t* frame, uint32_t size) {
cd486f
+  // Change color format for KDE KWin which uses RGBx and not BGRx
cd486f
+  for (uint32_t i = 0; i < size; i += 4) {
cd486f
+    uint8_t tempR = frame[i];
cd486f
+    uint8_t tempB = frame[i + 2];
cd486f
+    frame[i] = tempB;
cd486f
+    frame[i + 2] = tempR;
cd486f
+  }
cd486f
+}
cd486f
+
cd486f
+guint BaseCapturerPipeWire::SetupRequestResponseSignal(
cd486f
+    const gchar* object_path,
cd486f
+    GDBusSignalCallback callback) {
cd486f
+  return g_dbus_connection_signal_subscribe(
cd486f
+      connection_, kDesktopBusName, kRequestInterfaceName, "Response",
cd486f
+      object_path, /*arg0=*/nullptr, G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
cd486f
+      callback, this, /*user_data_free_func=*/nullptr);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnProxyRequested(GObject* /*object*/,
cd486f
+                                              GAsyncResult* result,
cd486f
+                                              gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  GError* error = nullptr;
cd486f
+  that->proxy_ = g_dbus_proxy_new_finish(result, &error);
cd486f
+  if (!that->proxy_) {
cd486f
+    LOG(LS_ERROR) << "Failed to create a proxy for the screen cast portal: "
cd486f
+                      << error->message;
cd486f
+    g_error_free(error);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+  that->connection_ = g_dbus_proxy_get_connection(that->proxy_);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Created proxy for the screen cast portal.";
cd486f
+  that->SessionRequest();
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+gchar* BaseCapturerPipeWire::PrepareSignalHandle(GDBusConnection* connection,
cd486f
+                                                   const gchar* token) {
cd486f
+  gchar* sender = g_strdup(g_dbus_connection_get_unique_name(connection) + 1);
cd486f
+  for (int i = 0; sender[i]; i++) {
cd486f
+    if (sender[i] == '.') {
cd486f
+      sender[i] = '_';
cd486f
+    }
cd486f
+  }
cd486f
+
cd486f
+  gchar* handle = g_strconcat(kDesktopRequestObjectPath, "/", sender, "/",
cd486f
+                              token, /*end of varargs*/ nullptr);
cd486f
+  g_free(sender);
cd486f
+
cd486f
+  return handle;
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::SessionRequest() {
cd486f
+  GVariantBuilder builder;
cd486f
+  gchar* variant_string;
cd486f
+
cd486f
+  g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
cd486f
+  variant_string =
cd486f
+      g_strdup_printf("webrtc_session%d", g_random_int_range(0, G_MAXINT));
cd486f
+  g_variant_builder_add(&builder, "{sv}", "session_handle_token",
cd486f
+                        g_variant_new_string(variant_string));
cd486f
+  g_free(variant_string);
cd486f
+  variant_string = g_strdup_printf("webrtc%d", g_random_int_range(0, G_MAXINT));
cd486f
+  g_variant_builder_add(&builder, "{sv}", "handle_token",
cd486f
+                        g_variant_new_string(variant_string));
cd486f
+
cd486f
+  portal_handle_ = PrepareSignalHandle(connection_, variant_string);
cd486f
+  session_request_signal_id_ = SetupRequestResponseSignal(
cd486f
+      portal_handle_, OnSessionRequestResponseSignal);
cd486f
+  g_free(variant_string);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Screen cast session requested.";
cd486f
+  g_dbus_proxy_call(
cd486f
+      proxy_, "CreateSession", g_variant_new("(a{sv})", &builder),
cd486f
+      G_DBUS_CALL_FLAGS_NONE, /*timeout=*/-1, /*cancellable=*/nullptr,
cd486f
+      reinterpret_cast<GAsyncReadyCallback>(OnSessionRequested), this);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnSessionRequested(GDBusConnection* connection,
cd486f
+                                                GAsyncResult* result,
cd486f
+                                                gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  GError* error = nullptr;
cd486f
+  GVariant* variant = g_dbus_proxy_call_finish(that->proxy_, result, &error);
cd486f
+  if (!variant) {
cd486f
+    LOG(LS_ERROR) << "Failed to create a screen cast session: "
cd486f
+                      << error->message;
cd486f
+    g_error_free(error);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+  LOG(LS_INFO) << "Initializing the screen cast session.";
cd486f
+
cd486f
+  gchar* handle = nullptr;
cd486f
+  g_variant_get_child(variant, 0, "o", &handle);
cd486f
+  g_variant_unref(variant);
cd486f
+  if (!handle) {
cd486f
+    LOG(LS_ERROR) << "Failed to initialize the screen cast session.";
cd486f
+    if (that->session_request_signal_id_) {
cd486f
+      g_dbus_connection_signal_unsubscribe(connection,
cd486f
+                                           that->session_request_signal_id_);
cd486f
+      that->session_request_signal_id_ = 0;
cd486f
+    }
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  g_free(handle);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Subscribing to the screen cast session.";
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnSessionRequestResponseSignal(
cd486f
+    GDBusConnection* connection,
cd486f
+    const gchar* sender_name,
cd486f
+    const gchar* object_path,
cd486f
+    const gchar* interface_name,
cd486f
+    const gchar* signal_name,
cd486f
+    GVariant* parameters,
cd486f
+    gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+
cd486f
+  LOG(LS_INFO)
cd486f
+      << "Received response for the screen cast session subscription.";
cd486f
+
cd486f
+  guint32 portal_response;
cd486f
+  GVariant* response_data;
cd486f
+  g_variant_get(parameters, "(u@a{sv})", &portal_response, &response_data);
cd486f
+  g_variant_lookup(response_data, "session_handle", "s",
cd486f
+                   &that->session_handle_);
cd486f
+  g_variant_unref(response_data);
cd486f
+
cd486f
+  if (!that->session_handle_ || portal_response) {
cd486f
+    LOG(LS_ERROR)
cd486f
+        << "Failed to request the screen cast session subscription.";
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  that->SourcesRequest();
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::SourcesRequest() {
cd486f
+  GVariantBuilder builder;
cd486f
+  gchar* variant_string;
cd486f
+
cd486f
+  g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
cd486f
+  // We want to record monitor content.
cd486f
+  g_variant_builder_add(&builder, "{sv}", "types",
cd486f
+                        g_variant_new_uint32(capture_source_type_));
cd486f
+  // We don't want to allow selection of multiple sources.
cd486f
+  g_variant_builder_add(&builder, "{sv}", "multiple",
cd486f
+                        g_variant_new_boolean(false));
cd486f
+  variant_string = g_strdup_printf("webrtc%d", g_random_int_range(0, G_MAXINT));
cd486f
+  g_variant_builder_add(&builder, "{sv}", "handle_token",
cd486f
+                        g_variant_new_string(variant_string));
cd486f
+
cd486f
+  sources_handle_ = PrepareSignalHandle(connection_, variant_string);
cd486f
+  sources_request_signal_id_ = SetupRequestResponseSignal(
cd486f
+      sources_handle_, OnSourcesRequestResponseSignal);
cd486f
+  g_free(variant_string);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Requesting sources from the screen cast session.";
cd486f
+  g_dbus_proxy_call(
cd486f
+      proxy_, "SelectSources",
cd486f
+      g_variant_new("(oa{sv})", session_handle_, &builder),
cd486f
+      G_DBUS_CALL_FLAGS_NONE, /*timeout=*/-1, /*cancellable=*/nullptr,
cd486f
+      reinterpret_cast<GAsyncReadyCallback>(OnSourcesRequested), this);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnSourcesRequested(GDBusConnection* connection,
cd486f
+                                                GAsyncResult* result,
cd486f
+                                                gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  GError* error = nullptr;
cd486f
+  GVariant* variant = g_dbus_proxy_call_finish(that->proxy_, result, &error);
cd486f
+  if (!variant) {
cd486f
+    LOG(LS_ERROR) << "Failed to request the sources: " << error->message;
cd486f
+    g_error_free(error);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  LOG(LS_INFO) << "Sources requested from the screen cast session.";
cd486f
+
cd486f
+  gchar* handle = nullptr;
cd486f
+  g_variant_get_child(variant, 0, "o", &handle);
cd486f
+  g_variant_unref(variant);
cd486f
+  if (!handle) {
cd486f
+    LOG(LS_ERROR) << "Failed to initialize the screen cast session.";
cd486f
+    if (that->sources_request_signal_id_) {
cd486f
+      g_dbus_connection_signal_unsubscribe(connection,
cd486f
+                                           that->sources_request_signal_id_);
cd486f
+      that->sources_request_signal_id_ = 0;
cd486f
+    }
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  g_free(handle);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Subscribed to sources signal.";
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnSourcesRequestResponseSignal(
cd486f
+    GDBusConnection* connection,
cd486f
+    const gchar* sender_name,
cd486f
+    const gchar* object_path,
cd486f
+    const gchar* interface_name,
cd486f
+    const gchar* signal_name,
cd486f
+    GVariant* parameters,
cd486f
+    gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  guint32 portal_response;
cd486f
+  g_variant_get(parameters, "(u@a{sv})", &portal_response, nullptr);
cd486f
+  if (portal_response) {
cd486f
+    LOG(LS_ERROR)
cd486f
+        << "Failed to select sources for the screen cast session.";
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  LOG(LS_INFO) << "Received sources signal from session.";
cd486f
+  that->StartRequest();
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::StartRequest() {
cd486f
+  GVariantBuilder builder;
cd486f
+  gchar* variant_string;
cd486f
+
cd486f
+  g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
cd486f
+  variant_string = g_strdup_printf("webrtc%d", g_random_int_range(0, G_MAXINT));
cd486f
+  g_variant_builder_add(&builder, "{sv}", "handle_token",
cd486f
+                        g_variant_new_string(variant_string));
cd486f
+
cd486f
+  start_handle_ = PrepareSignalHandle(connection_, variant_string);
cd486f
+  start_request_signal_id_ =
cd486f
+      SetupRequestResponseSignal(start_handle_, OnStartRequestResponseSignal);
cd486f
+  g_free(variant_string);
cd486f
+
cd486f
+  // "Identifier for the application window", this is Wayland, so not "x11:...".
cd486f
+  const gchar parent_window[] = "";
cd486f
+
cd486f
+  LOG(LS_INFO) << "Starting the screen cast session.";
cd486f
+  g_dbus_proxy_call(
cd486f
+      proxy_, "Start",
cd486f
+      g_variant_new("(osa{sv})", session_handle_, parent_window, &builder),
cd486f
+      G_DBUS_CALL_FLAGS_NONE, /*timeout=*/-1, /*cancellable=*/nullptr,
cd486f
+      reinterpret_cast<GAsyncReadyCallback>(OnStartRequested), this);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStartRequested(GDBusConnection* connection,
cd486f
+                                              GAsyncResult* result,
cd486f
+                                              gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  GError* error = nullptr;
cd486f
+  GVariant* variant = g_dbus_proxy_call_finish(that->proxy_, result, &error);
cd486f
+  if (!variant) {
cd486f
+    LOG(LS_ERROR) << "Failed to start the screen cast session: "
cd486f
+                      << error->message;
cd486f
+    g_error_free(error);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  LOG(LS_INFO) << "Initializing the start of the screen cast session.";
cd486f
+
cd486f
+  gchar* handle = nullptr;
cd486f
+  g_variant_get_child(variant, 0, "o", &handle);
cd486f
+  g_variant_unref(variant);
cd486f
+  if (!handle) {
cd486f
+    LOG(LS_ERROR)
cd486f
+        << "Failed to initialize the start of the screen cast session.";
cd486f
+    if (that->start_request_signal_id_) {
cd486f
+      g_dbus_connection_signal_unsubscribe(connection,
cd486f
+                                           that->start_request_signal_id_);
cd486f
+      that->start_request_signal_id_ = 0;
cd486f
+    }
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  g_free(handle);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Subscribed to the start signal.";
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnStartRequestResponseSignal(
cd486f
+    GDBusConnection* connection,
cd486f
+    const gchar* sender_name,
cd486f
+    const gchar* object_path,
cd486f
+    const gchar* interface_name,
cd486f
+    const gchar* signal_name,
cd486f
+    GVariant* parameters,
cd486f
+    gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Start signal received.";
cd486f
+  guint32 portal_response;
cd486f
+  GVariant* response_data;
cd486f
+  GVariantIter* iter = nullptr;
cd486f
+  g_variant_get(parameters, "(u@a{sv})", &portal_response, &response_data);
cd486f
+  if (portal_response || !response_data) {
cd486f
+    LOG(LS_ERROR) << "Failed to start the screen cast session.";
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  // Array of PipeWire streams. See
cd486f
+  // https://github.com/flatpak/xdg-desktop-portal/blob/master/data/org.freedesktop.portal.ScreenCast.xml
cd486f
+  // documentation for <method name="Start">.
cd486f
+  if (g_variant_lookup(response_data, "streams", "a(ua{sv})", &iter)) {
cd486f
+    GVariant* variant;
cd486f
+
cd486f
+    while (g_variant_iter_next(iter, "@(ua{sv})", &variant)) {
cd486f
+      guint32 stream_id;
cd486f
+      gint32 width;
cd486f
+      gint32 height;
cd486f
+      GVariant* options;
cd486f
+
cd486f
+      g_variant_get(variant, "(u@a{sv})", &stream_id, &options);
cd486f
+      RTC_DCHECK(options != nullptr);
cd486f
+
cd486f
+      g_variant_lookup(options, "size", "(ii)", &width, &height);
cd486f
+
cd486f
+      that->desktop_size_.set(width, height);
cd486f
+
cd486f
+      g_variant_unref(options);
cd486f
+      g_variant_unref(variant);
cd486f
+    }
cd486f
+  }
cd486f
+  g_variant_iter_free(iter);
cd486f
+  g_variant_unref(response_data);
cd486f
+
cd486f
+  that->OpenPipeWireRemote();
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::OpenPipeWireRemote() {
cd486f
+  GVariantBuilder builder;
cd486f
+  g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
cd486f
+
cd486f
+  LOG(LS_INFO) << "Opening the PipeWire remote.";
cd486f
+
cd486f
+  g_dbus_proxy_call_with_unix_fd_list(
cd486f
+      proxy_, "OpenPipeWireRemote",
cd486f
+      g_variant_new("(oa{sv})", session_handle_, &builder),
cd486f
+      G_DBUS_CALL_FLAGS_NONE, /*timeout=*/-1, /*fd_list=*/nullptr,
cd486f
+      /*cancellable=*/nullptr,
cd486f
+      reinterpret_cast<GAsyncReadyCallback>(OnOpenPipeWireRemoteRequested),
cd486f
+      this);
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+void BaseCapturerPipeWire::OnOpenPipeWireRemoteRequested(
cd486f
+    GDBusConnection* connection,
cd486f
+    GAsyncResult* result,
cd486f
+    gpointer user_data) {
cd486f
+  BaseCapturerPipeWire* that =
cd486f
+      static_cast<BaseCapturerPipeWire*>(user_data);
cd486f
+  RTC_DCHECK(that);
cd486f
+
cd486f
+  GError* error = nullptr;
cd486f
+  GUnixFDList* outlist = nullptr;
cd486f
+  GVariant* variant = g_dbus_proxy_call_with_unix_fd_list_finish(
cd486f
+      that->proxy_, &outlist, result, &error);
cd486f
+  if (!variant) {
cd486f
+    LOG(LS_ERROR) << "Failed to open the PipeWire remote: "
cd486f
+                      << error->message;
cd486f
+    g_error_free(error);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  gint32 index;
cd486f
+  g_variant_get(variant, "(h)", &index);
cd486f
+
cd486f
+  if ((that->pw_fd_ = g_unix_fd_list_get(outlist, index, &error)) == -1) {
cd486f
+    LOG(LS_ERROR) << "Failed to get file descriptor from the list: "
cd486f
+                      << error->message;
cd486f
+    g_error_free(error);
cd486f
+    g_variant_unref(variant);
cd486f
+    that->portal_init_failed_ = true;
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  g_variant_unref(variant);
cd486f
+  g_object_unref(outlist);
cd486f
+
cd486f
+  that->InitPipeWire();
cd486f
+  LOG(LS_INFO) << "PipeWire remote opened.";
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::Start(Callback* callback) {
cd486f
+  RTC_DCHECK(!callback_);
cd486f
+  RTC_DCHECK(callback);
cd486f
+
cd486f
+  InitPortals();
cd486f
+
cd486f
+  callback_ = callback;
cd486f
+}
cd486f
+
cd486f
+void BaseCapturerPipeWire::CaptureFrame() {
cd486f
+  if (portal_init_failed_) {
cd486f
+    callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr);
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  if (!current_frame_) {
cd486f
+    callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
cd486f
+    return;
cd486f
+  }
cd486f
+
cd486f
+  std::unique_ptr<DesktopFrame> result(new BasicDesktopFrame(desktop_size_));
cd486f
+  result->CopyPixelsFrom(
cd486f
+      current_frame_, (desktop_size_.width() * kBytesPerPixelPw),
cd486f
+      DesktopRect::MakeWH(desktop_size_.width(), desktop_size_.height()));
cd486f
+  if (!result) {
cd486f
+    callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
cd486f
+    return;
cd486f
+  }
cd486f
+  callback_->OnCaptureResult(Result::SUCCESS, std::move(result));
cd486f
+}
cd486f
+
cd486f
+bool BaseCapturerPipeWire::GetSourceList(SourceList* sources) {
cd486f
+  RTC_DCHECK(sources->size() == 0);
cd486f
+  // List of available screens is already presented by the xdg-desktop-portal.
cd486f
+  // But we have to add an empty source as the code expects it.
cd486f
+  sources->push_back({0});
cd486f
+  return true;
cd486f
+}
cd486f
+
cd486f
+bool BaseCapturerPipeWire::SelectSource(SourceId id) {
cd486f
+  // Screen selection is handled by the xdg-desktop-portal.
cd486f
+  return true;
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.h.pipewire	2019-01-07 14:53:17.201061764 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.h	2019-01-07 14:53:17.201061764 +0100
cd486f
@@ -0,0 +1,167 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_BASE_CAPTURER_PIPEWIRE_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_BASE_CAPTURER_PIPEWIRE_H_
cd486f
+
cd486f
+#include <gio/gio.h>
cd486f
+#define typeof __typeof__
cd486f
+#include <pipewire/pipewire.h>
cd486f
+#include <spa/param/video/format-utils.h>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class PipeWireType {
cd486f
+ public:
cd486f
+  spa_type_media_type media_type;
cd486f
+  spa_type_media_subtype media_subtype;
cd486f
+  spa_type_format_video format_video;
cd486f
+  spa_type_video_format video_format;
cd486f
+};
cd486f
+
cd486f
+class BaseCapturerPipeWire : public DesktopCapturer {
cd486f
+ public:
cd486f
+  enum CaptureSourceType { Screen = 1, Window };
cd486f
+
cd486f
+  explicit BaseCapturerPipeWire(CaptureSourceType source_type);
cd486f
+  ~BaseCapturerPipeWire() override;
cd486f
+
cd486f
+  // DesktopCapturer interface.
cd486f
+  void Start(Callback* delegate) override;
cd486f
+  void Stop() override { callback_ = nullptr; }
cd486f
+  void CaptureFrame() override;
cd486f
+  bool GetSourceList(SourceList* sources) override;
cd486f
+  bool SelectSource(SourceId id) override;
cd486f
+
cd486f
+ private:
cd486f
+  // PipeWire types -->
cd486f
+  pw_core* pw_core_ = nullptr;
cd486f
+  pw_type* pw_core_type_ = nullptr;
cd486f
+  pw_stream* pw_stream_ = nullptr;
cd486f
+  pw_remote* pw_remote_ = nullptr;
cd486f
+  pw_loop* pw_loop_ = nullptr;
cd486f
+  pw_thread_loop* pw_main_loop_ = nullptr;
cd486f
+  PipeWireType* pw_type_ = nullptr;
cd486f
+
cd486f
+  spa_hook spa_stream_listener_ = {};
cd486f
+  spa_hook spa_remote_listener_ = {};
cd486f
+
cd486f
+  pw_stream_events pw_stream_events_ = {};
cd486f
+  pw_remote_events pw_remote_events_ = {};
cd486f
+
cd486f
+  spa_video_info_raw* spa_video_format_ = nullptr;
cd486f
+
cd486f
+  gint32 pw_fd_ = -1;
cd486f
+
cd486f
+  CaptureSourceType capture_source_type_ =
cd486f
+      BaseCapturerPipeWire::CaptureSourceType::Screen;
cd486f
+
cd486f
+  // <-- end of PipeWire types
cd486f
+
cd486f
+  GDBusConnection* connection_ = nullptr;
cd486f
+  GDBusProxy* proxy_ = nullptr;
cd486f
+  gchar* portal_handle_ = nullptr;
cd486f
+  gchar* session_handle_ = nullptr;
cd486f
+  gchar* sources_handle_ = nullptr;
cd486f
+  gchar* start_handle_ = nullptr;
cd486f
+  guint session_request_signal_id_ = 0;
cd486f
+  guint sources_request_signal_id_ = 0;
cd486f
+  guint start_request_signal_id_ = 0;
cd486f
+
cd486f
+  DesktopSize desktop_size_ = {};
cd486f
+  DesktopCaptureOptions options_ = {};
cd486f
+
cd486f
+  uint8_t* current_frame_ = nullptr;
cd486f
+  Callback* callback_ = nullptr;
cd486f
+
cd486f
+  bool portal_init_failed_ = false;
cd486f
+
cd486f
+  void InitPortals();
cd486f
+  void InitPipeWire();
cd486f
+  void InitPipeWireTypes();
cd486f
+
cd486f
+  void CreateReceivingStream();
cd486f
+  void HandleBuffer(pw_buffer* buffer);
cd486f
+
cd486f
+  void ConvertRGBxToBGRx(uint8_t* frame, uint32_t size);
cd486f
+
cd486f
+  static void OnStateChanged(void* data,
cd486f
+                             pw_remote_state old_state,
cd486f
+                             pw_remote_state state,
cd486f
+                             const char* error);
cd486f
+  static void OnStreamStateChanged(void* data,
cd486f
+                                   pw_stream_state old_state,
cd486f
+                                   pw_stream_state state,
cd486f
+                                   const char* error_message);
cd486f
+
cd486f
+  static void OnStreamFormatChanged(void* data, const struct spa_pod* format);
cd486f
+  static void OnStreamProcess(void* data);
cd486f
+  static void OnNewBuffer(void* data, uint32_t id);
cd486f
+
cd486f
+  guint SetupRequestResponseSignal(const gchar* object_path,
cd486f
+                                   GDBusSignalCallback callback);
cd486f
+
cd486f
+  static void OnProxyRequested(GObject* object,
cd486f
+                               GAsyncResult* result,
cd486f
+                               gpointer user_data);
cd486f
+
cd486f
+  static gchar* PrepareSignalHandle(GDBusConnection* connection,
cd486f
+                                    const gchar* token);
cd486f
+
cd486f
+  void SessionRequest();
cd486f
+  static void OnSessionRequested(GDBusConnection* connection,
cd486f
+                                 GAsyncResult* result,
cd486f
+                                 gpointer user_data);
cd486f
+  static void OnSessionRequestResponseSignal(GDBusConnection* connection,
cd486f
+                                             const gchar* sender_name,
cd486f
+                                             const gchar* object_path,
cd486f
+                                             const gchar* interface_name,
cd486f
+                                             const gchar* signal_name,
cd486f
+                                             GVariant* parameters,
cd486f
+                                             gpointer user_data);
cd486f
+
cd486f
+  void SourcesRequest();
cd486f
+  static void OnSourcesRequested(GDBusConnection* connection,
cd486f
+                                 GAsyncResult* result,
cd486f
+                                 gpointer user_data);
cd486f
+  static void OnSourcesRequestResponseSignal(GDBusConnection* connection,
cd486f
+                                             const gchar* sender_name,
cd486f
+                                             const gchar* object_path,
cd486f
+                                             const gchar* interface_name,
cd486f
+                                             const gchar* signal_name,
cd486f
+                                             GVariant* parameters,
cd486f
+                                             gpointer user_data);
cd486f
+
cd486f
+  void StartRequest();
cd486f
+  static void OnStartRequested(GDBusConnection* connection,
cd486f
+                               GAsyncResult* result,
cd486f
+                               gpointer user_data);
cd486f
+  static void OnStartRequestResponseSignal(GDBusConnection* connection,
cd486f
+                                           const gchar* sender_name,
cd486f
+                                           const gchar* object_path,
cd486f
+                                           const gchar* interface_name,
cd486f
+                                           const gchar* signal_name,
cd486f
+                                           GVariant* parameters,
cd486f
+                                           gpointer user_data);
cd486f
+
cd486f
+  void OpenPipeWireRemote();
cd486f
+  static void OnOpenPipeWireRemoteRequested(GDBusConnection* connection,
cd486f
+                                            GAsyncResult* result,
cd486f
+                                            gpointer user_data);
cd486f
+
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(BaseCapturerPipeWire);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_BASE_CAPTURER_PIPEWIRE_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_gn/moz.build.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_gn/moz.build
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_gn/moz.build.pipewire	2019-01-07 14:53:17.201061764 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_gn/moz.build	2019-01-07 14:56:57.674544724 +0100
cd486f
@@ -144,14 +144,26 @@ if CONFIG["OS_TARGET"] == "FreeBSD":
cd486f
         "/media/webrtc/trunk/webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.cc"
cd486f
     ]
cd486f
 
cd486f
+# Common Linux stuff between X11 and PipeWire
cd486f
 if CONFIG["OS_TARGET"] == "Linux":
cd486f
 
cd486f
     DEFINES["USE_NSS_CERTS"] = "1"
cd486f
-    DEFINES["USE_X11"] = "1"
cd486f
     DEFINES["WEBRTC_LINUX"] = True
cd486f
     DEFINES["WEBRTC_POSIX"] = True
cd486f
     DEFINES["_FILE_OFFSET_BITS"] = "64"
cd486f
 
cd486f
+    UNIFIED_SOURCES += [
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/app_capturer_linux.cc",
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_linux.cc",
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_linux.cc",
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_linux.cc",
cd486f
+    ]
cd486f
+
cd486f
+# X11 specific files
cd486f
+if CONFIG["OS_TARGET"] == "Linux":
cd486f
+
cd486f
+    DEFINES["USE_X11"] = "1"
cd486f
+
cd486f
     OS_LIBS += [
cd486f
         "rt",
cd486f
         "X11",
cd486f
@@ -178,6 +190,28 @@ if CONFIG["OS_TARGET"] == "Linux":
cd486f
         "/media/webrtc/trunk/webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.cc"
cd486f
     ]
cd486f
 
cd486f
+# PipeWire specific files
cd486f
+if CONFIG["OS_TARGET"] == "Linux":
cd486f
+
cd486f
+    DEFINES["WEBRTC_USE_PIPEWIRE"] = "1"
cd486f
+
cd486f
+    OS_LIBS += [
cd486f
+        "rt",
cd486f
+        "pipewire-0.2",
cd486f
+        "glib-2.0",
cd486f
+        "gio-2.0",
cd486f
+        "gobject-2.0"
cd486f
+    ]
cd486f
+
cd486f
+    CXXFLAGS += CONFIG['TK_CFLAGS']
cd486f
+
cd486f
+    UNIFIED_SOURCES += [
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/base_capturer_pipewire.cc",
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.cc",
cd486f
+        "/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.cc"
cd486f
+    ]
cd486f
+
cd486f
+
cd486f
 if CONFIG["OS_TARGET"] == "NetBSD":
cd486f
 
cd486f
     DEFINES["USE_X11"] = "1"
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_options.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_options.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_options.h.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capture_options.h	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -107,6 +107,11 @@ class DesktopCaptureOptions {
cd486f
   }
cd486f
 #endif
cd486f
 
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+  bool allow_pipewire() const { return allow_pipewire_; }
cd486f
+  void set_allow_pipewire(bool allow) { allow_pipewire_ = allow; }
cd486f
+#endif
cd486f
+
cd486f
  private:
cd486f
 #if defined(USE_X11)
cd486f
   rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
@@ -129,6 +134,9 @@ class DesktopCaptureOptions {
cd486f
 #endif
cd486f
   bool disable_effects_ = true;
cd486f
   bool detect_updated_region_ = false;
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+  bool allow_pipewire_ = true;
cd486f
+#endif
cd486f
 };
cd486f
 
cd486f
 }  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.cc.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.cc	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -66,4 +66,17 @@ std::unique_ptr<DesktopCapturer> Desktop
cd486f
   return capturer;
cd486f
 }
cd486f
 
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE) || defined(USE_X11)
cd486f
+bool DesktopCapturer::IsRunningUnderWayland() {
cd486f
+  const char* xdg_session_type = getenv("XDG_SESSION_TYPE");
cd486f
+  if (!xdg_session_type || strncmp(xdg_session_type, "wayland", 7) != 0)
cd486f
+    return false;
cd486f
+
cd486f
+  if (!(getenv("WAYLAND_DISPLAY")))
cd486f
+    return false;
cd486f
+
cd486f
+  return true;
cd486f
+}
cd486f
+#endif
cd486f
+
cd486f
 }  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.h.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/desktop_capturer.h	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -129,6 +129,10 @@ class DesktopCapturer {
cd486f
   static std::unique_ptr<DesktopCapturer> CreateAppCapturer(
cd486f
       const DesktopCaptureOptions& options);
cd486f
 
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE) || defined(USE_X11)
cd486f
+  static bool IsRunningUnderWayland();
cd486f
+#endif
cd486f
+
cd486f
  protected:
cd486f
   // CroppingWindowCapturer needs to create raw capturers without wrappers, so
cd486f
   // the following two functions are protected.
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_linux.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_linux.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_linux.cc.pipewire	2019-01-07 14:53:17.202061775 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_linux.cc	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -0,0 +1,40 @@
cd486f
+/*
cd486f
+ *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+#include "webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h"
cd486f
+#endif // defined(USE_X11)
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+// static
cd486f
+MouseCursorMonitor* MouseCursorMonitor::CreateForWindow(
cd486f
+    const DesktopCaptureOptions& options, WindowId window) {
cd486f
+#if defined(USE_X11)
cd486f
+  return MouseCursorMonitorX11::CreateForWindow(options, window);
cd486f
+#else
cd486f
+  return nullptr;
cd486f
+#endif // defined(USE_X11)
cd486f
+}
cd486f
+
cd486f
+// static
cd486f
+MouseCursorMonitor* MouseCursorMonitor::CreateForScreen(
cd486f
+    const DesktopCaptureOptions& options,
cd486f
+    ScreenId screen) {
cd486f
+#if defined(USE_X11)
cd486f
+  return MouseCursorMonitorX11::CreateForScreen(options, screen);
cd486f
+#else
cd486f
+  return nullptr;
cd486f
+#endif // defined(USE_X11)
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.cc.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.cc	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -16,6 +16,8 @@
cd486f
 #include <X11/Xlib.h>
cd486f
 #include <X11/Xutil.h>
cd486f
 
cd486f
+#include "webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h"
cd486f
+
cd486f
 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
 #include "webrtc/modules/desktop_capture/desktop_frame.h"
cd486f
 #include "webrtc/modules/desktop_capture/mouse_cursor.h"
cd486f
@@ -59,38 +61,6 @@ Window GetTopLevelWindow(Display* displa
cd486f
 
cd486f
 namespace webrtc {
cd486f
 
cd486f
-class MouseCursorMonitorX11 : public MouseCursorMonitor,
cd486f
-                              public SharedXDisplay::XEventHandler {
cd486f
- public:
cd486f
-  MouseCursorMonitorX11(const DesktopCaptureOptions& options, Window window, Window inner_window);
cd486f
-  ~MouseCursorMonitorX11() override;
cd486f
-
cd486f
-  void Start(Callback* callback, Mode mode) override;
cd486f
-  void Stop() override;
cd486f
-  void Capture() override;
cd486f
-
cd486f
- private:
cd486f
-  // SharedXDisplay::XEventHandler interface.
cd486f
-  bool HandleXEvent(const XEvent& event) override;
cd486f
-
cd486f
-  Display* display() { return x_display_->display(); }
cd486f
-
cd486f
-  // Captures current cursor shape and stores it in |cursor_shape_|.
cd486f
-  void CaptureCursor();
cd486f
-
cd486f
-  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
-  Callback* callback_;
cd486f
-  Mode mode_;
cd486f
-  Window window_;
cd486f
-  Window inner_window_;
cd486f
-
cd486f
-  bool have_xfixes_;
cd486f
-  int xfixes_event_base_;
cd486f
-  int xfixes_error_base_;
cd486f
-
cd486f
-  std::unique_ptr<MouseCursor> cursor_shape_;
cd486f
-};
cd486f
-
cd486f
 MouseCursorMonitorX11::MouseCursorMonitorX11(
cd486f
     const DesktopCaptureOptions& options,
cd486f
     Window window, Window inner_window)
cd486f
@@ -244,7 +214,7 @@ void MouseCursorMonitorX11::CaptureCurso
cd486f
 }
cd486f
 
cd486f
 // static
cd486f
-MouseCursorMonitor* MouseCursorMonitor::CreateForWindow(
cd486f
+MouseCursorMonitor* MouseCursorMonitorX11::CreateForWindow(
cd486f
     const DesktopCaptureOptions& options, WindowId window) {
cd486f
   if (!options.x_display())
cd486f
     return NULL;
cd486f
@@ -254,7 +224,7 @@ MouseCursorMonitor* MouseCursorMonitor::
cd486f
   return new MouseCursorMonitorX11(options, outer_window, window);
cd486f
 }
cd486f
 
cd486f
-MouseCursorMonitor* MouseCursorMonitor::CreateForScreen(
cd486f
+MouseCursorMonitor* MouseCursorMonitorX11::CreateForScreen(
cd486f
     const DesktopCaptureOptions& options,
cd486f
     ScreenId screen) {
cd486f
   if (!options.x_display())
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h.pipewire	2019-01-07 14:53:17.202061775 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/mouse_cursor_monitor_x11.h	2019-01-07 14:53:17.202061775 +0100
cd486f
@@ -0,0 +1,63 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_MOUSE_CURSOR_MONITOR_X11_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_MOUSE_CURSOR_MONITOR_X11_H_
cd486f
+
cd486f
+#include <X11/X.h>
cd486f
+#include <memory>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_types.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
cd486f
+#include "webrtc/modules/desktop_capture/mouse_cursor.h"
cd486f
+#include "webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
cd486f
+#include "webrtc/base/scoped_ref_ptr.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class MouseCursorMonitorX11 : public MouseCursorMonitor,
cd486f
+                              public SharedXDisplay::XEventHandler {
cd486f
+ public:
cd486f
+  MouseCursorMonitorX11(const DesktopCaptureOptions& options, Window window, Window inner_window);
cd486f
+  ~MouseCursorMonitorX11() override;
cd486f
+
cd486f
+  void Start(Callback* callback, Mode mode) override;
cd486f
+  void Stop() override;
cd486f
+  void Capture() override;
cd486f
+
cd486f
+  static MouseCursorMonitor* CreateForWindow(const DesktopCaptureOptions& options, WindowId window);
cd486f
+  static MouseCursorMonitor* CreateForScreen(const DesktopCaptureOptions& options, ScreenId screen);
cd486f
+
cd486f
+ private:
cd486f
+  // SharedXDisplay::XEventHandler interface.
cd486f
+  bool HandleXEvent(const XEvent& event) override;
cd486f
+
cd486f
+  Display* display() { return x_display_->display(); }
cd486f
+
cd486f
+  // Captures current cursor shape and stores it in |cursor_shape_|.
cd486f
+  void CaptureCursor();
cd486f
+
cd486f
+  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
+  Callback* callback_;
cd486f
+  Mode mode_;
cd486f
+  Window window_;
cd486f
+  Window inner_window_;
cd486f
+
cd486f
+  bool have_xfixes_;
cd486f
+  int xfixes_event_base_;
cd486f
+  int xfixes_error_base_;
cd486f
+
cd486f
+  std::unique_ptr<MouseCursor> cursor_shape_;
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_MOUSE_CURSOR_MONITOR_X11_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_linux.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_linux.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_linux.cc.pipewire	2019-01-07 14:53:17.203061786 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_linux.cc	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -0,0 +1,40 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+#include "webrtc/modules/desktop_capture/screen_capturer_pipewire.h"
cd486f
+#endif  // defined(WEBRTC_USE_PIPEWIRE)
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+#include "webrtc/modules/desktop_capture/screen_capturer_x11.h"
cd486f
+#endif  // defined(USE_X11)
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+// static
cd486f
+std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawScreenCapturer(
cd486f
+    const DesktopCaptureOptions& options) {
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+  if (options.allow_pipewire() && DesktopCapturer::IsRunningUnderWayland()) {
cd486f
+    return ScreenCapturerPipeWire::CreateRawScreenCapturer(options);
cd486f
+  }
cd486f
+#endif  // defined(WEBRTC_USE_PIPEWIRE)
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+  return ScreenCapturerX11::CreateRawScreenCapturer(options);
cd486f
+#endif  // defined(USE_X11)
cd486f
+
cd486f
+  return nullptr;
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.cc.pipewire	2019-01-07 14:53:17.203061786 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.cc	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -0,0 +1,31 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/screen_capturer_pipewire.h"
cd486f
+
cd486f
+#include <memory>
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+ScreenCapturerPipeWire::ScreenCapturerPipeWire()
cd486f
+    : BaseCapturerPipeWire(BaseCapturerPipeWire::CaptureSourceType::Screen) {}
cd486f
+ScreenCapturerPipeWire::~ScreenCapturerPipeWire() {}
cd486f
+
cd486f
+// static
cd486f
+std::unique_ptr<DesktopCapturer>
cd486f
+ScreenCapturerPipeWire::CreateRawScreenCapturer(
cd486f
+    const DesktopCaptureOptions& options) {
cd486f
+  std::unique_ptr<ScreenCapturerPipeWire> capturer(
cd486f
+      new ScreenCapturerPipeWire());
cd486f
+
cd486f
+  return std::move(capturer);
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.h.pipewire	2019-01-07 14:53:17.203061786 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_pipewire.h	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -0,0 +1,33 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_PIPEWIRE_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_PIPEWIRE_H_
cd486f
+
cd486f
+#include <memory>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/base_capturer_pipewire.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class ScreenCapturerPipeWire : public BaseCapturerPipeWire {
cd486f
+ public:
cd486f
+  ScreenCapturerPipeWire();
cd486f
+  ~ScreenCapturerPipeWire() override;
cd486f
+
cd486f
+  static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer(
cd486f
+      const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerPipeWire);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_PIPEWIRE_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.cc.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.cc	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -19,6 +19,8 @@
cd486f
 #include <X11/Xlib.h>
cd486f
 #include <X11/Xutil.h>
cd486f
 
cd486f
+#include "webrtc/modules/desktop_capture/screen_capturer_x11.h"
cd486f
+
cd486f
 #include "webrtc/base/checks.h"
cd486f
 #include "webrtc/base/constructormagic.h"
cd486f
 #include "webrtc/base/timeutils.h"
cd486f
@@ -32,100 +34,12 @@
cd486f
 #include "webrtc/system_wrappers/include/logging.h"
cd486f
 
cd486f
 namespace webrtc {
cd486f
-namespace {
cd486f
-
cd486f
-// A class to perform video frame capturing for Linux.
cd486f
-//
cd486f
-// If XDamage is used, this class sets DesktopFrame::updated_region() according
cd486f
-// to the areas reported by XDamage. Otherwise this class does not detect
cd486f
-// DesktopFrame::updated_region(), the field is always set to the entire frame
cd486f
-// rectangle. ScreenCapturerDifferWrapper should be used if that functionality
cd486f
-// is necessary.
cd486f
-class ScreenCapturerLinux : public DesktopCapturer,
cd486f
-                            public SharedXDisplay::XEventHandler {
cd486f
- public:
cd486f
-  ScreenCapturerLinux();
cd486f
-  ~ScreenCapturerLinux() override;
cd486f
-
cd486f
-  // TODO(ajwong): Do we really want this to be synchronous?
cd486f
-  bool Init(const DesktopCaptureOptions& options);
cd486f
-
cd486f
-  // DesktopCapturer interface.
cd486f
-  void Start(Callback* delegate) override;
cd486f
-  void Stop() override;
cd486f
-  void CaptureFrame() override;
cd486f
-  bool GetSourceList(SourceList* sources) override;
cd486f
-  bool SelectSource(SourceId id) override;
cd486f
-
cd486f
- private:
cd486f
-  Display* display() { return options_.x_display()->display(); }
cd486f
-
cd486f
-  // SharedXDisplay::XEventHandler interface.
cd486f
-  bool HandleXEvent(const XEvent& event) override;
cd486f
-
cd486f
-  void InitXDamage();
cd486f
-
cd486f
-  // Capture screen pixels to the current buffer in the queue. In the DAMAGE
cd486f
-  // case, the ScreenCapturerHelper already holds the list of invalid rectangles
cd486f
-  // from HandleXEvent(). In the non-DAMAGE case, this captures the
cd486f
-  // whole screen, then calculates some invalid rectangles that include any
cd486f
-  // differences between this and the previous capture.
cd486f
-  std::unique_ptr<DesktopFrame> CaptureScreen();
cd486f
-
cd486f
-  // Called when the screen configuration is changed.
cd486f
-  void ScreenConfigurationChanged();
cd486f
-
cd486f
-  // Synchronize the current buffer with |last_buffer_|, by copying pixels from
cd486f
-  // the area of |last_invalid_rects|.
cd486f
-  // Note this only works on the assumption that kNumBuffers == 2, as
cd486f
-  // |last_invalid_rects| holds the differences from the previous buffer and
cd486f
-  // the one prior to that (which will then be the current buffer).
cd486f
-  void SynchronizeFrame();
cd486f
-
cd486f
-  void DeinitXlib();
cd486f
-
cd486f
-  DesktopCaptureOptions options_;
cd486f
-
cd486f
-  Callback* callback_ = nullptr;
cd486f
-
cd486f
-  // X11 graphics context.
cd486f
-  GC gc_ = nullptr;
cd486f
-  Window root_window_ = BadValue;
cd486f
-
cd486f
-  // XFixes.
cd486f
-  bool has_xfixes_ = false;
cd486f
-  int xfixes_event_base_ = -1;
cd486f
-  int xfixes_error_base_ = -1;
cd486f
-
cd486f
-  // XDamage information.
cd486f
-  bool use_damage_ = false;
cd486f
-  Damage damage_handle_ = 0;
cd486f
-  int damage_event_base_ = -1;
cd486f
-  int damage_error_base_ = -1;
cd486f
-  XserverRegion damage_region_ = 0;
cd486f
-
cd486f
-  // Access to the X Server's pixel buffer.
cd486f
-  XServerPixelBuffer x_server_pixel_buffer_;
cd486f
-
cd486f
-  // A thread-safe list of invalid rectangles, and the size of the most
cd486f
-  // recently captured screen.
cd486f
-  ScreenCapturerHelper helper_;
cd486f
-
cd486f
-  // Queue of the frames buffers.
cd486f
-  ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
cd486f
-
cd486f
-  // Invalid region from the previous capture. This is used to synchronize the
cd486f
-  // current with the last buffer used.
cd486f
-  DesktopRegion last_invalid_region_;
cd486f
 
cd486f
-  RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux);
cd486f
-};
cd486f
-
cd486f
-ScreenCapturerLinux::ScreenCapturerLinux() {
cd486f
+ScreenCapturerX11::ScreenCapturerX11() {
cd486f
   helper_.SetLogGridSize(4);
cd486f
 }
cd486f
 
cd486f
-ScreenCapturerLinux::~ScreenCapturerLinux() {
cd486f
+ScreenCapturerX11::~ScreenCapturerX11() {
cd486f
   options_.x_display()->RemoveEventHandler(ConfigureNotify, this);
cd486f
   if (use_damage_) {
cd486f
     options_.x_display()->RemoveEventHandler(
cd486f
@@ -134,7 +48,7 @@ ScreenCapturerLinux::~ScreenCapturerLinu
cd486f
   DeinitXlib();
cd486f
 }
cd486f
 
cd486f
-bool ScreenCapturerLinux::Init(const DesktopCaptureOptions& options) {
cd486f
+bool ScreenCapturerX11::Init(const DesktopCaptureOptions& options) {
cd486f
   options_ = options;
cd486f
 
cd486f
   root_window_ = RootWindow(display(), DefaultScreen(display()));
cd486f
@@ -177,7 +91,7 @@ bool ScreenCapturerLinux::Init(const Des
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::InitXDamage() {
cd486f
+void ScreenCapturerX11::InitXDamage() {
cd486f
   // Our use of XDamage requires XFixes.
cd486f
   if (!has_xfixes_) {
cd486f
     return;
cd486f
@@ -218,18 +132,18 @@ void ScreenCapturerLinux::InitXDamage()
cd486f
   LOG(LS_INFO) << "Using XDamage extension.";
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::Start(Callback* callback) {
cd486f
+void ScreenCapturerX11::Start(Callback* callback) {
cd486f
   RTC_DCHECK(!callback_);
cd486f
   RTC_DCHECK(callback);
cd486f
 
cd486f
   callback_ = callback;
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::Stop() {
cd486f
+void ScreenCapturerX11::Stop() {
cd486f
   callback_ = NULL;
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::CaptureFrame() {
cd486f
+void ScreenCapturerX11::CaptureFrame() {
cd486f
   int64_t capture_start_time_nanos = rtc::TimeNanos();
cd486f
 
cd486f
   queue_.MoveToNextFrame();
cd486f
@@ -243,6 +157,7 @@ void ScreenCapturerLinux::CaptureFrame()
cd486f
   // in a good shape.
cd486f
   if (!x_server_pixel_buffer_.is_initialized()) {
cd486f
      // We failed to initialize pixel buffer.
cd486f
+     LOG(LS_ERROR) << "Pixel buffer is not initialized.";
cd486f
      callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr);
cd486f
      return;
cd486f
   }
cd486f
@@ -258,6 +173,7 @@ void ScreenCapturerLinux::CaptureFrame()
cd486f
 
cd486f
   std::unique_ptr<DesktopFrame> result = CaptureScreen();
cd486f
   if (!result) {
cd486f
+    LOG(LS_WARNING) << "Temporarily failed to capture screen.";
cd486f
     callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
cd486f
     return;
cd486f
   }
cd486f
@@ -268,19 +184,19 @@ void ScreenCapturerLinux::CaptureFrame()
cd486f
   callback_->OnCaptureResult(Result::SUCCESS, std::move(result));
cd486f
 }
cd486f
 
cd486f
-bool ScreenCapturerLinux::GetSourceList(SourceList* sources) {
cd486f
+bool ScreenCapturerX11::GetSourceList(SourceList* sources) {
cd486f
   RTC_DCHECK(sources->size() == 0);
cd486f
   // TODO(jiayl): implement screen enumeration.
cd486f
   sources->push_back({0});
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-bool ScreenCapturerLinux::SelectSource(SourceId id) {
cd486f
+bool ScreenCapturerX11::SelectSource(SourceId id) {
cd486f
   // TODO(jiayl): implement screen selection.
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-bool ScreenCapturerLinux::HandleXEvent(const XEvent& event) {
cd486f
+bool ScreenCapturerX11::HandleXEvent(const XEvent& event) {
cd486f
   if (use_damage_ && (event.type == damage_event_base_ + XDamageNotify)) {
cd486f
     const XDamageNotifyEvent* damage_event =
cd486f
         reinterpret_cast<const XDamageNotifyEvent*>(&event);
cd486f
@@ -295,7 +211,7 @@ bool ScreenCapturerLinux::HandleXEvent(c
cd486f
   return false;
cd486f
 }
cd486f
 
cd486f
-std::unique_ptr<DesktopFrame> ScreenCapturerLinux::CaptureScreen() {
cd486f
+std::unique_ptr<DesktopFrame> ScreenCapturerX11::CaptureScreen() {
cd486f
   std::unique_ptr<SharedDesktopFrame> frame = queue_.current_frame()->Share();
cd486f
   RTC_DCHECK(x_server_pixel_buffer_.window_size().equals(frame->size()));
cd486f
 
cd486f
@@ -345,14 +261,15 @@ std::unique_ptr<DesktopFrame> ScreenCapt
cd486f
     // Doing full-screen polling, or this is the first capture after a
cd486f
     // screen-resolution change.  In either case, need a full-screen capture.
cd486f
     DesktopRect screen_rect = DesktopRect::MakeSize(frame->size());
cd486f
-    x_server_pixel_buffer_.CaptureRect(screen_rect, frame.get());
cd486f
+    if (!x_server_pixel_buffer_.CaptureRect(screen_rect, frame.get()))
cd486f
+      return nullptr;
cd486f
     updated_region->SetRect(screen_rect);
cd486f
   }
cd486f
 
cd486f
   return std::move(frame);
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::ScreenConfigurationChanged() {
cd486f
+void ScreenCapturerX11::ScreenConfigurationChanged() {
cd486f
   // Make sure the frame buffers will be reallocated.
cd486f
   queue_.Reset();
cd486f
 
cd486f
@@ -363,7 +280,7 @@ void ScreenCapturerLinux::ScreenConfigur
cd486f
   }
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::SynchronizeFrame() {
cd486f
+void ScreenCapturerX11::SynchronizeFrame() {
cd486f
   // Synchronize the current buffer with the previous one since we do not
cd486f
   // capture the entire desktop. Note that encoder may be reading from the
cd486f
   // previous buffer at this time so thread access complaints are false
cd486f
@@ -383,7 +300,7 @@ void ScreenCapturerLinux::SynchronizeFra
cd486f
   }
cd486f
 }
cd486f
 
cd486f
-void ScreenCapturerLinux::DeinitXlib() {
cd486f
+void ScreenCapturerX11::DeinitXlib() {
cd486f
   if (gc_) {
cd486f
     XFreeGC(display(), gc_);
cd486f
     gc_ = nullptr;
cd486f
@@ -404,20 +321,18 @@ void ScreenCapturerLinux::DeinitXlib() {
cd486f
   }
cd486f
 }
cd486f
 
cd486f
-}  // namespace
cd486f
-
cd486f
 // static
cd486f
-std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawScreenCapturer(
cd486f
+std::unique_ptr<DesktopCapturer> ScreenCapturerX11::CreateRawScreenCapturer(
cd486f
     const DesktopCaptureOptions& options) {
cd486f
   if (!options.x_display())
cd486f
     return nullptr;
cd486f
 
cd486f
-  std::unique_ptr<ScreenCapturerLinux> capturer(new ScreenCapturerLinux());
cd486f
+  std::unique_ptr<ScreenCapturerX11> capturer(new ScreenCapturerX11());
cd486f
   if (!capturer.get()->Init(options)) {
cd486f
     return nullptr;
cd486f
   }
cd486f
 
cd486f
-  return std::unique_ptr<DesktopCapturer>(capturer.release());
cd486f
+  return std::move(capturer);
cd486f
 }
cd486f
 
cd486f
 }  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.h.pipewire	2019-01-07 14:53:17.203061786 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/screen_capturer_x11.h	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -0,0 +1,124 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_X11_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_X11_H_
cd486f
+
cd486f
+#include <X11/X.h>
cd486f
+#include <X11/Xlib.h>
cd486f
+#include <X11/extensions/Xdamage.h>
cd486f
+#include <X11/extensions/Xfixes.h>
cd486f
+#include <memory>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_frame.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_region.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
cd486f
+#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
cd486f
+#include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
cd486f
+#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
cd486f
+#include "webrtc/base/constructormagic.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+// A class to perform video frame capturing for Linux on X11.
cd486f
+//
cd486f
+// If XDamage is used, this class sets DesktopFrame::updated_region() according
cd486f
+// to the areas reported by XDamage. Otherwise this class does not detect
cd486f
+// DesktopFrame::updated_region(), the field is always set to the entire frame
cd486f
+// rectangle. ScreenCapturerDifferWrapper should be used if that functionality
cd486f
+// is necessary.
cd486f
+class ScreenCapturerX11 : public DesktopCapturer,
cd486f
+                          public SharedXDisplay::XEventHandler {
cd486f
+ public:
cd486f
+  ScreenCapturerX11();
cd486f
+  ~ScreenCapturerX11() override;
cd486f
+
cd486f
+  static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer(const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  // TODO(ajwong): Do we really want this to be synchronous?
cd486f
+  bool Init(const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  // DesktopCapturer interface.
cd486f
+  void Start(Callback* delegate) override;
cd486f
+  void Stop() override;
cd486f
+  void CaptureFrame() override;
cd486f
+  bool GetSourceList(SourceList* sources) override;
cd486f
+  bool SelectSource(SourceId id) override;
cd486f
+
cd486f
+ private:
cd486f
+  Display* display() { return options_.x_display()->display(); }
cd486f
+
cd486f
+  // SharedXDisplay::XEventHandler interface.
cd486f
+  bool HandleXEvent(const XEvent& event) override;
cd486f
+
cd486f
+  void InitXDamage();
cd486f
+
cd486f
+  // Capture screen pixels to the current buffer in the queue. In the DAMAGE
cd486f
+  // case, the ScreenCapturerHelper already holds the list of invalid rectangles
cd486f
+  // from HandleXEvent(). In the non-DAMAGE case, this captures the
cd486f
+  // whole screen, then calculates some invalid rectangles that include any
cd486f
+  // differences between this and the previous capture.
cd486f
+  std::unique_ptr<DesktopFrame> CaptureScreen();
cd486f
+
cd486f
+  // Called when the screen configuration is changed.
cd486f
+  void ScreenConfigurationChanged();
cd486f
+
cd486f
+  // Synchronize the current buffer with |last_buffer_|, by copying pixels from
cd486f
+  // the area of |last_invalid_rects|.
cd486f
+  // Note this only works on the assumption that kNumBuffers == 2, as
cd486f
+  // |last_invalid_rects| holds the differences from the previous buffer and
cd486f
+  // the one prior to that (which will then be the current buffer).
cd486f
+  void SynchronizeFrame();
cd486f
+
cd486f
+  void DeinitXlib();
cd486f
+
cd486f
+  DesktopCaptureOptions options_;
cd486f
+
cd486f
+  Callback* callback_ = nullptr;
cd486f
+
cd486f
+  // X11 graphics context.
cd486f
+  GC gc_ = nullptr;
cd486f
+  Window root_window_ = BadValue;
cd486f
+
cd486f
+  // XFixes.
cd486f
+  bool has_xfixes_ = false;
cd486f
+  int xfixes_event_base_ = -1;
cd486f
+  int xfixes_error_base_ = -1;
cd486f
+
cd486f
+  // XDamage information.
cd486f
+  bool use_damage_ = false;
cd486f
+  Damage damage_handle_ = 0;
cd486f
+  int damage_event_base_ = -1;
cd486f
+  int damage_error_base_ = -1;
cd486f
+  XserverRegion damage_region_ = 0;
cd486f
+
cd486f
+  // Access to the X Server's pixel buffer.
cd486f
+  XServerPixelBuffer x_server_pixel_buffer_;
cd486f
+
cd486f
+  // A thread-safe list of invalid rectangles, and the size of the most
cd486f
+  // recently captured screen.
cd486f
+  ScreenCapturerHelper helper_;
cd486f
+
cd486f
+  // Queue of the frames buffers.
cd486f
+  ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
cd486f
+
cd486f
+  // Invalid region from the previous capture. This is used to synchronize the
cd486f
+  // current with the last buffer used.
cd486f
+  DesktopRegion last_invalid_region_;
cd486f
+
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerX11);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURER_X11_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_linux.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_linux.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_linux.cc.pipewire	2019-01-07 14:53:17.203061786 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_linux.cc	2019-01-07 14:53:17.203061786 +0100
cd486f
@@ -0,0 +1,40 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+#include "webrtc/modules/desktop_capture/window_capturer_pipewire.h"
cd486f
+#endif  // defined(WEBRTC_USE_PIPEWIRE)
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+#include "webrtc/modules/desktop_capture/window_capturer_x11.h"
cd486f
+#endif  // defined(USE_X11)
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+// static
cd486f
+std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawWindowCapturer(
cd486f
+    const DesktopCaptureOptions& options) {
cd486f
+#if defined(WEBRTC_USE_PIPEWIRE)
cd486f
+  if (options.allow_pipewire() && DesktopCapturer::IsRunningUnderWayland()) {
cd486f
+    return WindowCapturerPipeWire::CreateRawWindowCapturer(options);
cd486f
+  }
cd486f
+#endif  // defined(WEBRTC_USE_PIPEWIRE)
cd486f
+
cd486f
+#if defined(USE_X11)
cd486f
+  return WindowCapturerX11::CreateRawWindowCapturer(options);
cd486f
+#endif  // defined(USE_X11)
cd486f
+
cd486f
+  return nullptr;
cd486f
+}
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.cc.pipewire	2019-01-07 14:53:17.204061797 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.cc	2019-01-07 14:53:17.204061797 +0100
cd486f
@@ -0,0 +1,28 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/window_capturer_pipewire.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+WindowCapturerPipeWire::WindowCapturerPipeWire()
cd486f
+    : BaseCapturerPipeWire(BaseCapturerPipeWire::CaptureSourceType::Window) {}
cd486f
+WindowCapturerPipeWire::~WindowCapturerPipeWire() {}
cd486f
+
cd486f
+// static
cd486f
+std::unique_ptr<DesktopCapturer>
cd486f
+WindowCapturerPipeWire::CreateRawWindowCapturer(
cd486f
+    const DesktopCaptureOptions& options) {
cd486f
+  std::unique_ptr<WindowCapturerPipeWire> capturer(
cd486f
+      new WindowCapturerPipeWire());
cd486f
+
cd486f
+  return std::move(capturer);
cd486f
+}
cd486f
+}  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.h.pipewire	2019-01-07 14:53:17.204061797 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_pipewire.h	2019-01-07 14:53:17.204061797 +0100
cd486f
@@ -0,0 +1,33 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_PIPEWIRE_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_PIPEWIRE_H_
cd486f
+
cd486f
+#include <memory>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/base_capturer_pipewire.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class WindowCapturerPipeWire : public BaseCapturerPipeWire {
cd486f
+ public:
cd486f
+  WindowCapturerPipeWire();
cd486f
+  ~WindowCapturerPipeWire() override;
cd486f
+
cd486f
+  static std::unique_ptr<DesktopCapturer> CreateRawWindowCapturer(
cd486f
+      const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerPipeWire);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_PIPEWIRE_H_
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.cc.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.cc
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.cc.pipewire	2018-10-17 22:39:32.000000000 +0200
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.cc	2019-01-07 14:53:17.204061797 +0100
cd486f
@@ -17,6 +17,8 @@
cd486f
 
cd486f
 #include <algorithm>
cd486f
 
cd486f
+#include "webrtc/modules/desktop_capture/window_capturer_x11.h"
cd486f
+
cd486f
 #include "webrtc/base/constructormagic.h"
cd486f
 #include "webrtc/base/scoped_ref_ptr.h"
cd486f
 #include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
@@ -30,58 +32,7 @@
cd486f
 
cd486f
 namespace webrtc {
cd486f
 
cd486f
-namespace {
cd486f
-
cd486f
-class WindowCapturerLinux : public DesktopCapturer,
cd486f
-                            public SharedXDisplay::XEventHandler {
cd486f
- public:
cd486f
-  WindowCapturerLinux(const DesktopCaptureOptions& options);
cd486f
-  ~WindowCapturerLinux() override;
cd486f
-
cd486f
-  // DesktopCapturer interface.
cd486f
-  void Start(Callback* callback) override;
cd486f
-  void Stop() override;
cd486f
-  void CaptureFrame() override;
cd486f
-  bool GetSourceList(SourceList* sources) override;
cd486f
-  bool SelectSource(SourceId id) override;
cd486f
-  bool FocusOnSelectedSource() override;
cd486f
-
cd486f
-  // SharedXDisplay::XEventHandler interface.
cd486f
-  bool HandleXEvent(const XEvent& event) override;
cd486f
-
cd486f
- private:
cd486f
-  Display* display() { return x_display_->display(); }
cd486f
-
cd486f
-  // Iterates through |window| hierarchy to find first visible window, i.e. one
cd486f
-  // that has WM_STATE property set to NormalState.
cd486f
-  // See http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.3.1 .
cd486f
-  ::Window GetApplicationWindow(::Window window);
cd486f
-
cd486f
-  // Returns true if the |window| is a desktop element.
cd486f
-  bool IsDesktopElement(::Window window);
cd486f
-
cd486f
-  // Returns window title for the specified X |window|.
cd486f
-  bool GetWindowTitle(::Window window, std::string* title);
cd486f
-
cd486f
-  // Returns the id of the owning process.
cd486f
-  int GetWindowProcessID(::Window window);
cd486f
-
cd486f
-  Callback* callback_ = nullptr;
cd486f
-
cd486f
-  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
-
cd486f
-  Atom wm_state_atom_;
cd486f
-  Atom window_type_atom_;
cd486f
-  Atom normal_window_type_atom_;
cd486f
-  bool has_composite_extension_ = false;
cd486f
-
cd486f
-  ::Window selected_window_ = 0;
cd486f
-  XServerPixelBuffer x_server_pixel_buffer_;
cd486f
-
cd486f
-  RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerLinux);
cd486f
-};
cd486f
-
cd486f
-WindowCapturerLinux::WindowCapturerLinux(const DesktopCaptureOptions& options)
cd486f
+WindowCapturerX11::WindowCapturerX11(const DesktopCaptureOptions& options)
cd486f
     : x_display_(options.x_display()) {
cd486f
   // Create Atoms so we don't need to do it every time they are used.
cd486f
   wm_state_atom_ = XInternAtom(display(), "WM_STATE", True);
cd486f
@@ -102,11 +53,11 @@ WindowCapturerLinux::WindowCapturerLinux
cd486f
   x_display_->AddEventHandler(ConfigureNotify, this);
cd486f
 }
cd486f
 
cd486f
-WindowCapturerLinux::~WindowCapturerLinux() {
cd486f
+WindowCapturerX11::~WindowCapturerX11() {
cd486f
   x_display_->RemoveEventHandler(ConfigureNotify, this);
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::GetSourceList(SourceList* sources) {
cd486f
+bool WindowCapturerX11::GetSourceList(SourceList* sources) {
cd486f
   SourceList result;
cd486f
 
cd486f
   XErrorTrap error_trap(display());
cd486f
@@ -159,7 +110,7 @@ bool WindowCapturerLinux::GetSourceList(
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::SelectSource(SourceId id) {
cd486f
+bool WindowCapturerX11::SelectSource(SourceId id) {
cd486f
   if (!x_server_pixel_buffer_.Init(display(), id))
cd486f
     return false;
cd486f
 
cd486f
@@ -180,7 +131,7 @@ bool WindowCapturerLinux::SelectSource(S
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::FocusOnSelectedSource() {
cd486f
+bool WindowCapturerX11::FocusOnSelectedSource() {
cd486f
   if (!selected_window_)
cd486f
     return false;
cd486f
 
cd486f
@@ -229,18 +180,18 @@ bool WindowCapturerLinux::FocusOnSelecte
cd486f
   return true;
cd486f
 }
cd486f
 
cd486f
-void WindowCapturerLinux::Start(Callback* callback) {
cd486f
+void WindowCapturerX11::Start(Callback* callback) {
cd486f
   assert(!callback_);
cd486f
   assert(callback);
cd486f
 
cd486f
   callback_ = callback;
cd486f
 }
cd486f
 
cd486f
-void WindowCapturerLinux::Stop() {
cd486f
+void WindowCapturerX11::Stop() {
cd486f
   callback_ = NULL;
cd486f
 }
cd486f
 
cd486f
-void WindowCapturerLinux::CaptureFrame() {
cd486f
+void WindowCapturerX11::CaptureFrame() {
cd486f
   x_display_->ProcessPendingXEvents();
cd486f
 
cd486f
   if (!x_server_pixel_buffer_.IsWindowValid()) {
cd486f
@@ -274,7 +225,7 @@ void WindowCapturerLinux::CaptureFrame()
cd486f
   callback_->OnCaptureResult(Result::SUCCESS, std::move(frame));
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::HandleXEvent(const XEvent& event) {
cd486f
+bool WindowCapturerX11::HandleXEvent(const XEvent& event) {
cd486f
   if (event.type == ConfigureNotify) {
cd486f
     XConfigureEvent xce = event.xconfigure;
cd486f
     if (!DesktopSize(xce.width, xce.height).equals(
cd486f
@@ -288,7 +239,7 @@ bool WindowCapturerLinux::HandleXEvent(c
cd486f
   return false;
cd486f
 }
cd486f
 
cd486f
-::Window WindowCapturerLinux::GetApplicationWindow(::Window window) {
cd486f
+::Window WindowCapturerX11::GetApplicationWindow(::Window window) {
cd486f
   // Get WM_STATE property of the window.
cd486f
   XWindowProperty<uint32_t> window_state(display(), window, wm_state_atom_);
cd486f
 
cd486f
@@ -326,7 +277,7 @@ bool WindowCapturerLinux::HandleXEvent(c
cd486f
   return app_window;
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::IsDesktopElement(::Window window) {
cd486f
+bool WindowCapturerX11::IsDesktopElement(::Window window) {
cd486f
   if (window == 0)
cd486f
     return false;
cd486f
 
cd486f
@@ -361,7 +312,7 @@ bool WindowCapturerLinux::IsDesktopEleme
cd486f
   return result;
cd486f
 }
cd486f
 
cd486f
-bool WindowCapturerLinux::GetWindowTitle(::Window window, std::string* title) {
cd486f
+bool WindowCapturerX11::GetWindowTitle(::Window window, std::string* title) {
cd486f
   int status;
cd486f
   bool result = false;
cd486f
   XTextProperty window_name;
cd486f
@@ -390,9 +341,7 @@ bool WindowCapturerLinux::GetWindowTitle
cd486f
   return result;
cd486f
 }
cd486f
 
cd486f
-}  // namespace
cd486f
-
cd486f
-int WindowCapturerLinux::GetWindowProcessID(::Window window) {
cd486f
+int WindowCapturerX11::GetWindowProcessID(::Window window) {
cd486f
   // Get _NET_WM_PID property of the window.
cd486f
   Atom process_atom = XInternAtom(display(), "_NET_WM_PID", True);
cd486f
   XWindowProperty<uint32_t> process_id(display(), window, process_atom);
cd486f
@@ -401,11 +350,11 @@ int WindowCapturerLinux::GetWindowProces
cd486f
 }
cd486f
 
cd486f
 // static
cd486f
-std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawWindowCapturer(
cd486f
+std::unique_ptr<DesktopCapturer> WindowCapturerX11::CreateRawWindowCapturer(
cd486f
     const DesktopCaptureOptions& options) {
cd486f
   if (!options.x_display())
cd486f
     return nullptr;
cd486f
-  return std::unique_ptr<DesktopCapturer>(new WindowCapturerLinux(options));
cd486f
+  return std::unique_ptr<DesktopCapturer>(new WindowCapturerX11(options));
cd486f
 }
cd486f
 
cd486f
 }  // namespace webrtc
cd486f
diff -up firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.h.pipewire firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.h
cd486f
--- firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.h.pipewire	2019-01-07 14:53:17.204061797 +0100
cd486f
+++ firefox-60.3.0/media/webrtc/trunk/webrtc/modules/desktop_capture/window_capturer_x11.h	2019-01-07 14:53:17.204061797 +0100
cd486f
@@ -0,0 +1,83 @@
cd486f
+/*
cd486f
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
cd486f
+ *
cd486f
+ *  Use of this source code is governed by a BSD-style license
cd486f
+ *  that can be found in the LICENSE file in the root of the source
cd486f
+ *  tree. An additional intellectual property rights grant can be found
cd486f
+ *  in the file PATENTS.  All contributing project authors may
cd486f
+ *  be found in the AUTHORS file in the root of the source tree.
cd486f
+ */
cd486f
+
cd486f
+#ifndef MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_X11_H_
cd486f
+#define MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_X11_H_
cd486f
+
cd486f
+#include <X11/X.h>
cd486f
+#include <X11/Xlib.h>
cd486f
+#include <memory>
cd486f
+#include <string>
cd486f
+
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_capturer.h"
cd486f
+#include "webrtc/modules/desktop_capture/desktop_geometry.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/shared_x_display.h"
cd486f
+#include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
cd486f
+#include "webrtc/base/constructormagic.h"
cd486f
+#include "webrtc/base/scoped_ref_ptr.h"
cd486f
+
cd486f
+namespace webrtc {
cd486f
+
cd486f
+class WindowCapturerX11 : public DesktopCapturer,
cd486f
+                          public SharedXDisplay::XEventHandler {
cd486f
+ public:
cd486f
+  explicit WindowCapturerX11(const DesktopCaptureOptions& options);
cd486f
+  ~WindowCapturerX11() override;
cd486f
+
cd486f
+  static std::unique_ptr<DesktopCapturer> CreateRawWindowCapturer(
cd486f
+      const DesktopCaptureOptions& options);
cd486f
+
cd486f
+  // DesktopCapturer interface.
cd486f
+  void Start(Callback* callback) override;
cd486f
+  void Stop() override;
cd486f
+  void CaptureFrame() override;
cd486f
+  bool GetSourceList(SourceList* sources) override;
cd486f
+  bool SelectSource(SourceId id) override;
cd486f
+  bool FocusOnSelectedSource() override;
cd486f
+
cd486f
+  // SharedXDisplay::XEventHandler interface.
cd486f
+  bool HandleXEvent(const XEvent& event) override;
cd486f
+
cd486f
+ private:
cd486f
+  Display* display() { return x_display_->display(); }
cd486f
+
cd486f
+  // Iterates through |window| hierarchy to find first visible window, i.e. one
cd486f
+  // that has WM_STATE property set to NormalState.
cd486f
+  // See http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.3.1 .
cd486f
+  ::Window GetApplicationWindow(::Window window);
cd486f
+
cd486f
+  // Returns true if the |window| is a desktop element.
cd486f
+  bool IsDesktopElement(::Window window);
cd486f
+
cd486f
+  // Returns window title for the specified X |window|.
cd486f
+  bool GetWindowTitle(::Window window, std::string* title);
cd486f
+
cd486f
+  // Returns the id of the owning process.
cd486f
+  int GetWindowProcessID(::Window window);
cd486f
+
cd486f
+  Callback* callback_ = nullptr;
cd486f
+
cd486f
+  rtc::scoped_refptr<SharedXDisplay> x_display_;
cd486f
+
cd486f
+  Atom wm_state_atom_;
cd486f
+  Atom window_type_atom_;
cd486f
+  Atom normal_window_type_atom_;
cd486f
+  bool has_composite_extension_ = false;
cd486f
+
cd486f
+  ::Window selected_window_ = 0;
cd486f
+  XServerPixelBuffer x_server_pixel_buffer_;
cd486f
+
cd486f
+  RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerX11);
cd486f
+};
cd486f
+
cd486f
+}  // namespace webrtc
cd486f
+
cd486f
+#endif  // MODULES_DESKTOP_CAPTURE_WINDOW_CAPTURER_X11_H_