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