remote_desktop_portal.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_HOST_LINUX_REMOTE_DESKTOP_PORTAL_H_
  5. #define REMOTING_HOST_LINUX_REMOTE_DESKTOP_PORTAL_H_
  6. #include <gio/gio.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/portal_request_response.h"
  13. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/scoped_glib.h"
  14. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/screen_capture_portal_interface.h"
  15. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/screencast_portal.h"
  16. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/xdg_desktop_portal_utils.h"
  17. #include "third_party/webrtc/modules/desktop_capture/linux/wayland/xdg_session_details.h"
  18. namespace remoting {
  19. namespace xdg_portal {
  20. // Helper class to setup an XDG remote desktop portal session. An instance of
  21. // this class is owned by the wayland capturer. The methods on this class are
  22. // called from the capturer thread.
  23. class RemoteDesktopPortal
  24. : public webrtc::xdg_portal::ScreenCapturePortalInterface,
  25. public webrtc::ScreenCastPortal::PortalNotifier {
  26. public:
  27. // |notifier| must outlive |RemoteDesktopPortal| instance and will be called
  28. // into from the capturer thread.
  29. explicit RemoteDesktopPortal(
  30. webrtc::ScreenCastPortal::PortalNotifier* notifier);
  31. RemoteDesktopPortal(const RemoteDesktopPortal&) = delete;
  32. RemoteDesktopPortal& operator=(const RemoteDesktopPortal&) = delete;
  33. ~RemoteDesktopPortal() override;
  34. // ScreenCapturePortalInterface overrides.
  35. void Start() override;
  36. void OnPortalDone(webrtc::xdg_portal::RequestResponse result) override;
  37. webrtc::xdg_portal::SessionDetails GetSessionDetails() override;
  38. void RequestSession(GDBusProxy* proxy) override;
  39. // PortalNotifier interface.
  40. void OnScreenCastRequestResult(webrtc::xdg_portal::RequestResponse result,
  41. uint32_t stream_node_id,
  42. int fd) override;
  43. void OnScreenCastSessionClosed() override;
  44. private:
  45. void Cleanup();
  46. void UnsubscribeSignalHandlers();
  47. void RequestSources();
  48. void SelectDevices();
  49. void StartRequest();
  50. uint32_t pipewire_stream_node_id();
  51. static void OnProxyRequested(GObject* object,
  52. GAsyncResult* result,
  53. gpointer user_data);
  54. static void OnSessionRequestResponseSignal(GDBusConnection* connection,
  55. const char* sender_name,
  56. const char* object_path,
  57. const char* interface_name,
  58. const char* signal_name,
  59. GVariant* parameters,
  60. gpointer user_data);
  61. static void OnScreenCastPortalProxyRequested(GObject* object,
  62. GAsyncResult* result,
  63. gpointer user_data);
  64. static void OnDevicesRequestImpl(GDBusConnection* connection,
  65. const gchar* sender_name,
  66. const gchar* object_path,
  67. const gchar* interface_name,
  68. const gchar* signal_name,
  69. GVariant* parameters,
  70. gpointer user_data);
  71. static void OnStartRequestResponseSignal(GDBusConnection* connection,
  72. const char* sender_name,
  73. const char* object_path,
  74. const char* interface_name,
  75. const char* signal_name,
  76. GVariant* parameters,
  77. gpointer user_data);
  78. static void OnStartRequested(GDBusProxy* proxy,
  79. GAsyncResult* result,
  80. gpointer user_data);
  81. static void OnSourcesRequestResponseSignal(GDBusConnection* connection,
  82. const char* sender_name,
  83. const char* object_path,
  84. const char* interface_name,
  85. const char* signal_name,
  86. GVariant* parameters,
  87. gpointer user_data);
  88. static void OnSessionClosedSignal(GDBusConnection* connection,
  89. const char* sender_name,
  90. const char* object_path,
  91. const char* interface_name,
  92. const char* signal_name,
  93. GVariant* parameters,
  94. gpointer user_data);
  95. static void OnSessionRequested(GDBusProxy* proxy,
  96. GAsyncResult* result,
  97. gpointer user_data);
  98. static void OnDevicesRequested(GDBusProxy* proxy,
  99. GAsyncResult* result,
  100. gpointer user_data);
  101. webrtc::xdg_portal::RequestResponse screencast_portal_status_
  102. GUARDED_BY_CONTEXT(sequence_checker_) =
  103. webrtc::xdg_portal::RequestResponse::kUnknown;
  104. raw_ptr<GDBusConnection> connection_ GUARDED_BY_CONTEXT(sequence_checker_) =
  105. nullptr;
  106. raw_ptr<GDBusProxy> proxy_ GUARDED_BY_CONTEXT(sequence_checker_) = nullptr;
  107. raw_ptr<GCancellable> cancellable_ GUARDED_BY_CONTEXT(sequence_checker_) =
  108. nullptr;
  109. raw_ptr<webrtc::ScreenCastPortal::PortalNotifier> notifier_
  110. GUARDED_BY_CONTEXT(sequence_checker_) = nullptr;
  111. std::string portal_handle_ GUARDED_BY_CONTEXT(sequence_checker_);
  112. std::string session_handle_ GUARDED_BY_CONTEXT(sequence_checker_);
  113. std::string start_handle_ GUARDED_BY_CONTEXT(sequence_checker_);
  114. std::string devices_handle_ GUARDED_BY_CONTEXT(sequence_checker_);
  115. guint session_request_signal_id_ GUARDED_BY_CONTEXT(sequence_checker_) = 0;
  116. guint start_request_signal_id_ GUARDED_BY_CONTEXT(sequence_checker_) = 0;
  117. guint session_closed_signal_id_ GUARDED_BY_CONTEXT(sequence_checker_) = 0;
  118. guint devices_request_signal_id_ GUARDED_BY_CONTEXT(sequence_checker_) = 0;
  119. std::unique_ptr<webrtc::ScreenCastPortal> screencast_portal_
  120. GUARDED_BY_CONTEXT(sequence_checker_);
  121. GMainContext* context_ GUARDED_BY_CONTEXT(sequence_checker_) = nullptr;
  122. SEQUENCE_CHECKER(sequence_checker_);
  123. };
  124. } // namespace xdg_portal
  125. } // namespace remoting
  126. #endif // REMOTING_HOST_LINUX_REMOTE_DESKTOP_PORTAL_H_