From 99c1e5934fef1dfda6ecd682e48b97abb6823887 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Wed, 17 May 2017 14:18:45 +0200 Subject: Fix WebRTC screen sharing on macOS Previously when a screen sharing request was accepted on macOS, instead of showing the captured screen, a black rectangle was displayed. This was due to passing an incorrect screen id to the screen capturer, which resulted in a failed attempt to find the main active screen. The fix is to query for all available screen ids from the operating system, and choose the first id as the id to pass along to the screen capturer. Task-number: QTBUG-55165 Change-Id: Id8e648e59755aa2820b05b990adeaa9b58fd26f0 Reviewed-by: Allan Sandfeld Jensen --- src/core/media_capture_devices_dispatcher.cpp | 32 ++++++++++++++++++++++++++- src/core/qtwebengine.gni | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/core/media_capture_devices_dispatcher.cpp b/src/core/media_capture_devices_dispatcher.cpp index b3c42aa08..65c818903 100644 --- a/src/core/media_capture_devices_dispatcher.cpp +++ b/src/core/media_capture_devices_dispatcher.cpp @@ -62,6 +62,8 @@ #include "content/public/common/media_stream_request.h" #include "media/audio/audio_device_description.h" #include "media/audio/audio_manager_base.h" +#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" +#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" #include "ui/base/l10n/l10n_util.h" #include @@ -331,7 +333,35 @@ void MediaCaptureDevicesDispatcher::handleScreenCaptureAccessRequest(content::We content::MediaStreamDevices devices; std::unique_ptr ui; if (userAccepted) { - content::DesktopMediaID screenId = content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN, 0); + // Source id patterns are different across platforms. + // On Linux, the hardcoded value "0" is used. + // On Windows, the screens are enumerated consecutively in increasing order from 0. + // On macOS the source ids are randomish numbers assigned by the OS. + webrtc::DesktopCapturer::SourceId id = 0; + +#if defined(ENABLE_WEBRTC) + // In order to provide a correct screen id, we query for the available screen ids, and + // select the first one as the main display id. + // The code is based on the file + // src/chrome/browser/extensions/api/desktop_capture/desktop_capture_base.cc. + webrtc::DesktopCaptureOptions options = + webrtc::DesktopCaptureOptions::CreateDefault(); + options.set_disable_effects(false); + std::unique_ptr screen_capturer( + webrtc::DesktopCapturer::CreateScreenCapturer(options)); + + if (screen_capturer) { + webrtc::DesktopCapturer::SourceList screens; + if (screen_capturer->GetSourceList(&screens)) { + if (screens.size() > 0) { + id = screens[0].id; + } + } + } +#endif + + content::DesktopMediaID screenId = content::DesktopMediaID( + content::DesktopMediaID::TYPE_SCREEN, id); ui = getDevicesForDesktopCapture(&devices, screenId, false/*capture_audio*/, false/*display_notification*/, getContentsUrl(webContents)); } std::map::iterator it = diff --git a/src/core/qtwebengine.gni b/src/core/qtwebengine.gni index 1c0c8a415..bb269baa2 100644 --- a/src/core/qtwebengine.gni +++ b/src/core/qtwebengine.gni @@ -25,6 +25,7 @@ deps = [ "//net:net_browser_services", "//skia", "//third_party/WebKit/public:blink", + "//third_party/webrtc/base:base", "//ui/accessibility", "//third_party/mesa:mesa_headers", ":qtwebengine_sources", -- cgit v1.2.3