diff --git a/webrtc/modules/desktop_capture/BUILD.gn b/webrtc/modules/desktop_capture/BUILD.gn index 1583066dc6..5529537f5f 100644 --- a/webrtc/modules/desktop_capture/BUILD.gn +++ b/webrtc/modules/desktop_capture/BUILD.gn @@ -61,6 +61,7 @@ rtc_static_library("desktop_capture") { "desktop_and_cursor_composer.h", "desktop_capture_options.cc", "desktop_capture_options.h", + "desktop_capturer.cc", "desktop_capturer.h", "desktop_frame_win.cc", "desktop_frame_win.h", @@ -80,6 +81,7 @@ rtc_static_library("desktop_capture") { "mouse_cursor_monitor_mac.mm", "mouse_cursor_monitor_win.cc", "screen_capture_frame_queue.h", + "screen_capturer.cc", "screen_capturer.h", "screen_capturer_helper.cc", "screen_capturer_helper.h", @@ -118,6 +120,7 @@ rtc_static_library("desktop_capture") { "win/screen_capturer_win_magnifier.h", "win/window_capture_utils.cc", "win/window_capture_utils.h", + "window_capturer.cc", "window_capturer.h", "window_capturer_mac.mm", "window_capturer_win.cc", diff --git a/webrtc/modules/desktop_capture/desktop_capture.gypi b/webrtc/modules/desktop_capture/desktop_capture.gypi index 48bafdf2a5..ab1fd32f9f 100644 --- a/webrtc/modules/desktop_capture/desktop_capture.gypi +++ b/webrtc/modules/desktop_capture/desktop_capture.gypi @@ -39,6 +39,7 @@ 'desktop_and_cursor_composer.h', 'desktop_capture_options.h', 'desktop_capture_options.cc', + 'desktop_capturer.cc', 'desktop_capturer.h', 'desktop_frame_win.cc', 'desktop_frame_win.h', diff --git a/webrtc/modules/desktop_capture/desktop_capturer.cc b/webrtc/modules/desktop_capture/desktop_capturer.cc new file mode 100644 index 0000000000..a18976a626 --- /dev/null +++ b/webrtc/modules/desktop_capture/desktop_capturer.cc @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/desktop_capture/desktop_capturer.h" + +#include "webrtc/modules/desktop_capture/desktop_capture_options.h" +#include "webrtc/modules/desktop_capture/screen_capturer_differ_wrapper.h" + +namespace webrtc { + +DesktopCapturer::~DesktopCapturer() = default; + +void DesktopCapturer::SetSharedMemoryFactory( + std::unique_ptr shared_memory_factory) {} + +void DesktopCapturer::SetExcludedWindow(WindowId window) {} + +bool DesktopCapturer::GetSourceList(SourceList* sources) { + return true; +} + +bool DesktopCapturer::SelectSource(SourceId id) { + return false; +} + +bool DesktopCapturer::FocusOnSelectedSource() { + return false; +} + +} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/desktop_capturer.h b/webrtc/modules/desktop_capture/desktop_capturer.h index d758946b84..6b9c820ce9 100644 --- a/webrtc/modules/desktop_capture/desktop_capturer.h +++ b/webrtc/modules/desktop_capture/desktop_capturer.h @@ -12,8 +12,11 @@ #define WEBRTC_MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_H_ #include +#include #include +#include +#include #include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/desktop_capture_types.h" @@ -21,6 +24,7 @@ namespace webrtc { +class DesktopCaptureOptions; class DesktopFrame; // Abstract interface for screen and window capturers. @@ -53,7 +57,20 @@ class DesktopCapturer { virtual ~Callback() {} }; - virtual ~DesktopCapturer() {} + typedef intptr_t SourceId; + + struct Source { + // The unique id to represent a Source of current DesktopCapturer. + SourceId id; + + // Title of the window or screen in UTF-8 encoding, maybe empty. This field + // should not be used to identify a source. + std::string title; + }; + + typedef std::vector SourceList; + + virtual ~DesktopCapturer(); // Called at the beginning of a capturing session. |callback| must remain // valid until capturer is destroyed. @@ -65,7 +82,7 @@ class DesktopCapturer { // Shared memory is currently supported only by some DesktopCapturer // implementations. virtual void SetSharedMemoryFactory( - std::unique_ptr shared_memory_factory) {} + std::unique_ptr shared_memory_factory); // Captures next frame, and involve callback provided by Start() function. // Pending capture requests are canceled when DesktopCapturer is deleted. @@ -74,7 +91,25 @@ class DesktopCapturer { // Sets the window to be excluded from the captured image in the future // Capture calls. Used to exclude the screenshare notification window for // screen capturing. - virtual void SetExcludedWindow(WindowId window) {} + virtual void SetExcludedWindow(WindowId window); + + // TODO(zijiehe): Following functions should be pure virtual. The default + // implementations are for backward compatibility only. Remove default + // implementations once all DesktopCapturer implementations in Chromium have + // implemented these functions. + + // Gets a list of sources current capturer supports. Returns false in case of + // a failure. + virtual bool GetSourceList(SourceList* sources); + + // Selects a source to be captured. Returns false in case of a failure (e.g. + // if there is no source with the specified type and id.) + virtual bool SelectSource(SourceId id); + + // Brings the selected source to the front and sets the input focus on it. + // Returns false in case of a failure or no source has been selected or the + // implementation does not support this functionality. + virtual bool FocusOnSelectedSource(); }; } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/screen_capturer.cc b/webrtc/modules/desktop_capture/screen_capturer.cc new file mode 100644 index 0000000000..a35aa1a014 --- /dev/null +++ b/webrtc/modules/desktop_capture/screen_capturer.cc @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/desktop_capture/screen_capturer.h" + +#include +#include + +namespace webrtc { + +ScreenCapturer::~ScreenCapturer() = default; + +bool ScreenCapturer::GetScreenList(ScreenList* screens) { + SourceList sources; + if (!GetSourceList(&sources)) { + return false; + } + + for (const Source& source : sources) { + screens->push_back({source.id}); + } + return true; +} + +bool ScreenCapturer::SelectScreen(ScreenId id) { + return SelectSource(id); +} + +bool ScreenCapturer::GetSourceList(SourceList* sources) { + ScreenList screens; + if (!GetScreenList(&screens)) { + return false; + } + + for (const Screen& screen : screens) { + sources->push_back({screen.id}); + } + + return true; +} + +bool ScreenCapturer::SelectSource(SourceId id) { + return SelectScreen(id); +} + +} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/screen_capturer.h b/webrtc/modules/desktop_capture/screen_capturer.h index 5f07ba1a53..a6bbde73ee 100644 --- a/webrtc/modules/desktop_capture/screen_capturer.h +++ b/webrtc/modules/desktop_capture/screen_capturer.h @@ -21,6 +21,7 @@ namespace webrtc { class DesktopCaptureOptions; +// TODO(zijiehe): Remove this class. // Class used to capture video frames asynchronously. // // The full capture sequence is as follows: @@ -48,19 +49,25 @@ class ScreenCapturer : public DesktopCapturer { }; typedef std::vector ScreenList; - ~ScreenCapturer() override {} + ~ScreenCapturer() override; // Creates a platform-specific capturer. static ScreenCapturer* Create(const DesktopCaptureOptions& options); + // Deprecated, use GetSourceList(). // Get the list of screens (not containing kFullDesktopScreenId). Returns // false in case of a failure. - virtual bool GetScreenList(ScreenList* screens) = 0; + virtual bool GetScreenList(ScreenList* screens); + // Deprecated, use SelectSource(). // Select the screen to be captured. Returns false in case of a failure (e.g. // if there is no screen with the specified id). If this is never called, the // full desktop is captured. - virtual bool SelectScreen(ScreenId id) = 0; + virtual bool SelectScreen(ScreenId id); + + // DesktopCapturer interfaces. + bool GetSourceList(SourceList* sources) override; + bool SelectSource(SourceId id) override; }; } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/window_capturer.cc b/webrtc/modules/desktop_capture/window_capturer.cc new file mode 100644 index 0000000000..62932ccb90 --- /dev/null +++ b/webrtc/modules/desktop_capture/window_capturer.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/desktop_capture/window_capturer.h" + +#include +#include + +namespace webrtc { + +WindowCapturer::~WindowCapturer() = default; + +bool WindowCapturer::GetWindowList(WindowList* windows) { + SourceList sources; + if (!GetSourceList(&sources)) { + return false; + } + + for (const Source& source : sources) { + windows->push_back({source.id, std::move(source.title)}); + } + return true; +} + +bool WindowCapturer::SelectWindow(WindowId id) { + return SelectSource(id); +} + +bool WindowCapturer::BringSelectedWindowToFront() { + return FocusOnSelectedSource(); +} + +bool WindowCapturer::GetSourceList(SourceList* sources) { + WindowList windows; + if (!GetWindowList(&windows)) { + return false; + } + + for (const Window& window : windows) { + sources->push_back({window.id, std::move(window.title)}); + } + + return true; +} + +bool WindowCapturer::SelectSource(SourceId id) { + return SelectWindow(id); +} + +bool WindowCapturer::FocusOnSelectedSource() { + return BringSelectedWindowToFront(); +} + +} // namespace webrtc diff --git a/webrtc/modules/desktop_capture/window_capturer.h b/webrtc/modules/desktop_capture/window_capturer.h index 682778c558..4ef07aa1a2 100644 --- a/webrtc/modules/desktop_capture/window_capturer.h +++ b/webrtc/modules/desktop_capture/window_capturer.h @@ -23,10 +23,9 @@ namespace webrtc { class DesktopCaptureOptions; +// TODO(zijiehe): Remove this class. class WindowCapturer : public DesktopCapturer { public: - typedef webrtc::WindowId WindowId; - struct Window { WindowId id; @@ -36,20 +35,29 @@ class WindowCapturer : public DesktopCapturer { typedef std::vector WindowList; + // Consumers should use DesktopCapturer::CreateWindowCapturer. static WindowCapturer* Create(const DesktopCaptureOptions& options); - ~WindowCapturer() override {} + ~WindowCapturer() override; + // Deprecated, use GetSourceList(). // Get list of windows. Returns false in case of a failure. - virtual bool GetWindowList(WindowList* windows) = 0; + virtual bool GetWindowList(WindowList* windows); + // Deprecated, use SelectSource(). // Select window to be captured. Returns false in case of a failure (e.g. if // there is no window with the specified id). - virtual bool SelectWindow(WindowId id) = 0; + virtual bool SelectWindow(WindowId id); + // Deprecated, use FocusOnSelectedSource(). // Bring the selected window to the front. Returns false in case of a // failure or no window selected. - virtual bool BringSelectedWindowToFront() = 0; + virtual bool BringSelectedWindowToFront(); + + // DesktopCapturer interfaces. + bool GetSourceList(SourceList* sources) override; + bool SelectSource(SourceId id) override; + bool FocusOnSelectedSource() override; }; } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/window_capturer_win.cc b/webrtc/modules/desktop_capture/window_capturer_win.cc index 9253b86d22..c43db7e6bb 100644 --- a/webrtc/modules/desktop_capture/window_capturer_win.cc +++ b/webrtc/modules/desktop_capture/window_capturer_win.cc @@ -63,7 +63,7 @@ BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) { } WindowCapturer::Window window; - window.id = reinterpret_cast(hwnd); + window.id = reinterpret_cast(hwnd); const size_t kTitleLength = 500; WCHAR window_title[kTitleLength];