feat_make_macos_sccontentsharingpicker_work_in_electron.patch 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Keeley Hammond <[email protected]>
  3. Date: Wed, 21 Aug 2024 19:00:00 -0700
  4. Subject: feat: make MacOS SCContentSharingPicker work in Electron
  5. This patch is a work in progress that contains assorted changes to make the MacOS SCContentSharingPicker upstream implementation work within Electron. If this comment is still in this patch during PR review, it is not ready for prime time
  6. This patch can be removed after our desktopCapturer is refactored.
  7. diff --git a/chrome/browser/media/webrtc/desktop_media_list_base.cc b/chrome/browser/media/webrtc/desktop_media_list_base.cc
  8. index 6599311831b638f49658e768fe35e19e9961ef1d..f49519a6cc52d6e90ff07b64e5a71010094f9c5d 100644
  9. --- a/chrome/browser/media/webrtc/desktop_media_list_base.cc
  10. +++ b/chrome/browser/media/webrtc/desktop_media_list_base.cc
  11. @@ -77,7 +77,7 @@ void DesktopMediaListBase::StartUpdating(DesktopMediaListObserver* observer) {
  12. void DesktopMediaListBase::Update(UpdateCallback callback, bool refresh_thumbnails) {
  13. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  14. DCHECK(sources_.empty());
  15. - DCHECK(!refresh_callback_);
  16. + // DCHECK(!refresh_callback_);
  17. refresh_callback_ = std::move(callback);
  18. Refresh(refresh_thumbnails);
  19. }
  20. diff --git a/chrome/browser/media/webrtc/native_desktop_media_list.cc b/chrome/browser/media/webrtc/native_desktop_media_list.cc
  21. index 95a1c18438619c19a1dd71ca3e6e23af5e0ebacb..68ece50018124992f951557e817a12aa45d65956 100644
  22. --- a/chrome/browser/media/webrtc/native_desktop_media_list.cc
  23. +++ b/chrome/browser/media/webrtc/native_desktop_media_list.cc
  24. @@ -46,6 +46,7 @@
  25. #endif
  26. #if BUILDFLAG(IS_MAC)
  27. +#include "chrome/browser/media/webrtc/thumbnail_capturer_mac.h"
  28. #include "components/remote_cocoa/browser/scoped_cg_window_id.h"
  29. #endif
  30. @@ -545,11 +546,23 @@ NativeDesktopMediaList::Worker::FormatSources(
  31. break;
  32. case DesktopMediaID::Type::TYPE_WINDOW:
  33. +#if BUILDFLAG(IS_MAC)
  34. + // If using NativeScreenCapturePickerMac,
  35. + // skipping the picker will skip the first window selection.
  36. + if (ShouldUseSCContentSharingPicker()) {
  37. + title = base::UTF8ToUTF16(sources[i].title);
  38. + } else if (sources[i].id == excluded_window_id) {
  39. + // Skip the picker dialog window.
  40. + continue;
  41. + }
  42. + title = base::UTF8ToUTF16(sources[i].title);
  43. + #else
  44. // Skip the picker dialog window.
  45. if (sources[i].id == excluded_window_id) {
  46. continue;
  47. }
  48. title = base::UTF8ToUTF16(sources[i].title);
  49. +#endif
  50. break;
  51. default:
  52. diff --git a/chrome/browser/media/webrtc/thumbnail_capturer_mac.h b/chrome/browser/media/webrtc/thumbnail_capturer_mac.h
  53. index 12a74f8f32cc00a7f3d7802865ae4b309961341d..acbcfb08ae8c44e24a04b326096289428bc6ff60 100644
  54. --- a/chrome/browser/media/webrtc/thumbnail_capturer_mac.h
  55. +++ b/chrome/browser/media/webrtc/thumbnail_capturer_mac.h
  56. @@ -8,6 +8,9 @@
  57. #include "chrome/browser/media/webrtc/desktop_media_list.h"
  58. #include "chrome/browser/media/webrtc/thumbnail_capturer.h"
  59. +// Returns true if the SCK sharing picker is available and enabled.
  60. +bool ShouldUseSCContentSharingPicker();
  61. +
  62. // Returns true if the SCK thumbnail capturer is available and enabled.
  63. bool ShouldUseThumbnailCapturerMac(DesktopMediaList::Type type);
  64. diff --git a/chrome/browser/media/webrtc/thumbnail_capturer_mac.mm b/chrome/browser/media/webrtc/thumbnail_capturer_mac.mm
  65. index 2215bf4589342fa4619fb58ec3e21ff5ef3ed3b4..3e52ce331b80cf97fd7b9bcbf7dd4311bacf07f2 100644
  66. --- a/chrome/browser/media/webrtc/thumbnail_capturer_mac.mm
  67. +++ b/chrome/browser/media/webrtc/thumbnail_capturer_mac.mm
  68. @@ -40,14 +40,14 @@
  69. // is required to avoid recurring permission dialogs.
  70. BASE_FEATURE(kUseSCContentSharingPicker,
  71. "UseSCContentSharingPicker",
  72. - base::FEATURE_DISABLED_BY_DEFAULT);
  73. + base::FEATURE_ENABLED_BY_DEFAULT);
  74. // Use the built-in MacOS screen-sharing picker (SCContentSharingPicker) on
  75. // MacOS 14 Sonoma and later. This flag will use the built-in picker for all
  76. // MacOS versions where it is supported.
  77. BASE_FEATURE(kUseSCContentSharingPickerSonoma,
  78. "UseSCContentSharingPickerSonoma",
  79. - base::FEATURE_DISABLED_BY_DEFAULT);
  80. + base::FEATURE_ENABLED_BY_DEFAULT);
  81. #endif
  82. using SampleCallback =
  83. @@ -1006,6 +1006,8 @@ void OnCapturedFrame(base::apple::ScopedCFTypeRef<CGImageRef> image,
  84. source_id);
  85. }
  86. +} // namespace
  87. +
  88. bool ShouldUseSCContentSharingPicker() {
  89. if (@available(macOS 15.0, *)) {
  90. if (base::FeatureList::IsEnabled(kUseSCContentSharingPicker)) {
  91. @@ -1020,8 +1022,6 @@ bool ShouldUseSCContentSharingPicker() {
  92. return false;
  93. }
  94. -} // namespace
  95. -
  96. bool ShouldUseThumbnailCapturerMac(DesktopMediaList::Type type) {
  97. // There was a bug in ScreenCaptureKit that was fixed in 14.4,
  98. // see b/40076027.
  99. diff --git a/content/browser/media/capture/native_screen_capture_picker_mac.mm b/content/browser/media/capture/native_screen_capture_picker_mac.mm
  100. index b5a776f37b4bb667bc1aa62a08102b67a12f5b64..36b1508f0a8bd17bec0e49bf797a64c2fcc38bc2 100644
  101. --- a/content/browser/media/capture/native_screen_capture_picker_mac.mm
  102. +++ b/content/browser/media/capture/native_screen_capture_picker_mac.mm
  103. @@ -117,8 +117,11 @@ void Open(DesktopMediaID::Type type,
  104. base::OnceCallback<void()> cancel_callback,
  105. base::OnceCallback<void()> error_callback) {
  106. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  107. + // Chrome doesn't allow both screens & windows in their picker,
  108. + // but Electron does - add a check for TYPE_NONE.
  109. CHECK(type == DesktopMediaID::Type::TYPE_SCREEN ||
  110. - type == DesktopMediaID::Type::TYPE_WINDOW);
  111. + type == DesktopMediaID::Type::TYPE_WINDOW ||
  112. + type == DesktopMediaID::Type::TYPE_NONE);
  113. if (@available(macOS 14.0, *)) {
  114. NSNumber* source_id = @(next_id_);
  115. auto picker_observer = [[PickerObserver alloc]
  116. @@ -135,20 +138,14 @@ void Open(DesktopMediaID::Type type,
  117. // TODO(https://crbug.com/360781940): Add support for changing selected
  118. // content. The problem to solve is how this should interact with stream
  119. // restart.
  120. - config.allowsChangingSelectedContent = false;
  121. + config.allowsChangingSelectedContent = true;
  122. // Limits the maximum number of screen/window capture to 5.
  123. NSNumber* max_stream_count = @5;
  124. - if (type == DesktopMediaID::Type::TYPE_SCREEN) {
  125. - config.allowedPickerModes = SCContentSharingPickerModeSingleDisplay;
  126. - picker.defaultConfiguration = config;
  127. - picker.maximumStreamCount = max_stream_count;
  128. - [picker presentPickerUsingContentStyle:SCShareableContentStyleDisplay];
  129. - } else {
  130. - config.allowedPickerModes = SCContentSharingPickerModeSingleWindow;
  131. - picker.defaultConfiguration = config;
  132. - picker.maximumStreamCount = max_stream_count;
  133. - [picker presentPickerUsingContentStyle:SCShareableContentStyleWindow];
  134. - }
  135. + // Chrome doesn't allow both screens & windows in their picker,
  136. + // but Electron does; we patch out the MediaID::Type conditional here
  137. + picker.defaultConfiguration = config;
  138. + picker.maximumStreamCount = max_stream_count;
  139. + [picker present];
  140. } else {
  141. NOTREACHED();
  142. }