Line data Source code
1 : /*
2 : * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include "webrtc/modules/desktop_capture/cropping_window_capturer.h"
12 :
13 : #include "webrtc/modules/desktop_capture/cropped_desktop_frame.h"
14 : #include "webrtc/system_wrappers/include/logging.h"
15 :
16 : namespace webrtc {
17 :
18 0 : CroppingWindowCapturer::CroppingWindowCapturer(
19 0 : const DesktopCaptureOptions& options)
20 : : options_(options),
21 : callback_(NULL),
22 : window_capturer_(DesktopCapturer::CreateRawWindowCapturer(options)),
23 : selected_window_(kNullWindowId),
24 0 : excluded_window_(kNullWindowId) {}
25 :
26 0 : CroppingWindowCapturer::~CroppingWindowCapturer() {}
27 :
28 0 : void CroppingWindowCapturer::Start(DesktopCapturer::Callback* callback) {
29 0 : callback_ = callback;
30 0 : window_capturer_->Start(callback);
31 0 : }
32 :
33 0 : void CroppingWindowCapturer::Stop() {
34 0 : window_capturer_->Stop();
35 0 : callback_ = NULL;
36 0 : }
37 :
38 0 : void CroppingWindowCapturer::SetSharedMemoryFactory(
39 : std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
40 0 : window_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
41 0 : }
42 :
43 0 : void CroppingWindowCapturer::CaptureFrame() {
44 0 : if (ShouldUseScreenCapturer()) {
45 0 : if (!screen_capturer_.get()) {
46 0 : screen_capturer_ = DesktopCapturer::CreateRawScreenCapturer(options_);
47 0 : if (excluded_window_) {
48 0 : screen_capturer_->SetExcludedWindow(excluded_window_);
49 : }
50 0 : screen_capturer_->Start(this);
51 : }
52 0 : screen_capturer_->CaptureFrame();
53 : } else {
54 0 : window_capturer_->CaptureFrame();
55 : }
56 0 : }
57 :
58 0 : void CroppingWindowCapturer::SetExcludedWindow(WindowId window) {
59 0 : excluded_window_ = window;
60 0 : if (screen_capturer_.get()) {
61 0 : screen_capturer_->SetExcludedWindow(window);
62 : }
63 0 : }
64 :
65 0 : bool CroppingWindowCapturer::GetSourceList(SourceList* sources) {
66 0 : return window_capturer_->GetSourceList(sources);
67 : }
68 :
69 0 : bool CroppingWindowCapturer::SelectSource(SourceId id) {
70 0 : if (window_capturer_->SelectSource(id)) {
71 0 : selected_window_ = id;
72 0 : return true;
73 : }
74 0 : return false;
75 : }
76 :
77 0 : bool CroppingWindowCapturer::FocusOnSelectedSource() {
78 0 : return window_capturer_->FocusOnSelectedSource();
79 : }
80 :
81 0 : void CroppingWindowCapturer::OnCaptureResult(
82 : DesktopCapturer::Result result,
83 : std::unique_ptr<DesktopFrame> screen_frame) {
84 0 : if (!ShouldUseScreenCapturer()) {
85 0 : LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes";
86 0 : window_capturer_->CaptureFrame();
87 0 : return;
88 : }
89 :
90 0 : if (result != Result::SUCCESS) {
91 0 : LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame";
92 0 : callback_->OnCaptureResult(result, nullptr);
93 0 : return;
94 : }
95 :
96 0 : DesktopRect window_rect = GetWindowRectInVirtualScreen();
97 0 : if (window_rect.is_empty()) {
98 0 : LOG(LS_WARNING) << "Window rect is empty";
99 0 : callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
100 0 : return;
101 : }
102 :
103 0 : callback_->OnCaptureResult(
104 : Result::SUCCESS,
105 0 : CreateCroppedDesktopFrame(std::move(screen_frame), window_rect));
106 : }
107 :
108 : #if !defined(WEBRTC_WIN)
109 : // CroppingWindowCapturer is implemented only for windows. On other platforms
110 : // the regular window capturer is used.
111 : // static
112 0 : std::unique_ptr<DesktopCapturer> CroppingWindowCapturer::CreateCapturer(
113 : const DesktopCaptureOptions& options) {
114 0 : return DesktopCapturer::CreateWindowCapturer(options);
115 : }
116 : #endif
117 :
118 : } // namespace webrtc
|