blob: 7aab7f8e3c07fc4b0620d920ff7a10c5dea9c087 [file] [log] [blame]
jiayl@webrtc.org0e710702014-11-11 18:15:55 +00001/*
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"
Henrik Kjellander98f53512015-10-28 18:17:40 +010014#include "webrtc/system_wrappers/include/logging.h"
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000015
16namespace webrtc {
17
18CroppingWindowCapturer::CroppingWindowCapturer(
19 const DesktopCaptureOptions& options)
20 : options_(options),
21 callback_(NULL),
22 window_capturer_(WindowCapturer::Create(options)),
23 selected_window_(kNullWindowId),
24 excluded_window_(kNullWindowId) {
25}
26
27CroppingWindowCapturer::~CroppingWindowCapturer() {}
28
29void CroppingWindowCapturer::Start(DesktopCapturer::Callback* callback) {
30 callback_ = callback;
31 window_capturer_->Start(callback);
32}
33
sergeyucc9669c2016-02-09 15:13:26 -080034void CroppingWindowCapturer::SetSharedMemoryFactory(
35 rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) {
36 window_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
37}
38
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000039void CroppingWindowCapturer::Capture(const DesktopRegion& region) {
40 if (ShouldUseScreenCapturer()) {
41 if (!screen_capturer_.get()) {
42 screen_capturer_.reset(ScreenCapturer::Create(options_));
43 if (excluded_window_) {
44 screen_capturer_->SetExcludedWindow(excluded_window_);
45 }
46 screen_capturer_->Start(this);
47 }
48 screen_capturer_->Capture(region);
49 } else {
50 window_capturer_->Capture(region);
51 }
52}
53
54void CroppingWindowCapturer::SetExcludedWindow(WindowId window) {
55 excluded_window_ = window;
56 if (screen_capturer_.get()) {
57 screen_capturer_->SetExcludedWindow(window);
58 }
59}
60
61bool CroppingWindowCapturer::GetWindowList(WindowList* windows) {
62 return window_capturer_->GetWindowList(windows);
63}
64
65bool CroppingWindowCapturer::SelectWindow(WindowId id) {
66 if (window_capturer_->SelectWindow(id)) {
67 selected_window_ = id;
68 return true;
69 }
70 return false;
71}
72
73bool CroppingWindowCapturer::BringSelectedWindowToFront() {
74 return window_capturer_->BringSelectedWindowToFront();
75}
76
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000077void CroppingWindowCapturer::OnCaptureCompleted(DesktopFrame* frame) {
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000078 rtc::scoped_ptr<DesktopFrame> screen_frame(frame);
jiayl@webrtc.org0e710702014-11-11 18:15:55 +000079
80 if (!ShouldUseScreenCapturer()) {
81 LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes";
82 window_capturer_->Capture(DesktopRegion());
83 return;
84 }
85
86 if (!frame) {
87 LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame";
88 callback_->OnCaptureCompleted(NULL);
89 return;
90 }
91
92 DesktopRect window_rect = GetWindowRectInVirtualScreen();
93 if (window_rect.is_empty()) {
94 LOG(LS_WARNING) << "Window rect is empty";
95 callback_->OnCaptureCompleted(NULL);
96 return;
97 }
98
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000099 rtc::scoped_ptr<DesktopFrame> window_frame(
jiayl@webrtc.org0e710702014-11-11 18:15:55 +0000100 CreateCroppedDesktopFrame(screen_frame.release(), window_rect));
101 callback_->OnCaptureCompleted(window_frame.release());
102}
103
jiayl@webrtc.org90b9b082014-11-12 20:53:00 +0000104#if !defined(WEBRTC_WIN)
jiayl@webrtc.org0e710702014-11-11 18:15:55 +0000105// static
106WindowCapturer*
107CroppingWindowCapturer::Create(const DesktopCaptureOptions& options) {
108 return WindowCapturer::Create(options);
109}
110#endif
111
112} // namespace webrtc