cropping_window_capturer.cc revision 00b8f6b3643332cce1ee711715f7fbb824d793ca
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/interface/logging.h"
15
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
34void CroppingWindowCapturer::Capture(const DesktopRegion& region) {
35  if (ShouldUseScreenCapturer()) {
36    if (!screen_capturer_.get()) {
37      screen_capturer_.reset(ScreenCapturer::Create(options_));
38      if (excluded_window_) {
39        screen_capturer_->SetExcludedWindow(excluded_window_);
40      }
41      screen_capturer_->Start(this);
42    }
43    screen_capturer_->Capture(region);
44  } else {
45    window_capturer_->Capture(region);
46  }
47}
48
49void CroppingWindowCapturer::SetExcludedWindow(WindowId window) {
50  excluded_window_ = window;
51  if (screen_capturer_.get()) {
52    screen_capturer_->SetExcludedWindow(window);
53  }
54}
55
56bool CroppingWindowCapturer::GetWindowList(WindowList* windows) {
57  return window_capturer_->GetWindowList(windows);
58}
59
60bool CroppingWindowCapturer::SelectWindow(WindowId id) {
61  if (window_capturer_->SelectWindow(id)) {
62    selected_window_ = id;
63    return true;
64  }
65  return false;
66}
67
68bool CroppingWindowCapturer::BringSelectedWindowToFront() {
69  return window_capturer_->BringSelectedWindowToFront();
70}
71
72SharedMemory* CroppingWindowCapturer::CreateSharedMemory(size_t size) {
73  return callback_->CreateSharedMemory(size);
74}
75
76void CroppingWindowCapturer::OnCaptureCompleted(DesktopFrame* frame) {
77  rtc::scoped_ptr<DesktopFrame> screen_frame(frame);
78
79  if (!ShouldUseScreenCapturer()) {
80    LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes";
81    window_capturer_->Capture(DesktopRegion());
82    return;
83  }
84
85  if (!frame) {
86    LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame";
87    callback_->OnCaptureCompleted(NULL);
88    return;
89  }
90
91  DesktopRect window_rect = GetWindowRectInVirtualScreen();
92  if (window_rect.is_empty()) {
93    LOG(LS_WARNING) << "Window rect is empty";
94    callback_->OnCaptureCompleted(NULL);
95    return;
96  }
97
98  rtc::scoped_ptr<DesktopFrame> window_frame(
99      CreateCroppedDesktopFrame(screen_frame.release(), window_rect));
100  callback_->OnCaptureCompleted(window_frame.release());
101}
102
103#if !defined(WEBRTC_WIN)
104// static
105WindowCapturer*
106CroppingWindowCapturer::Create(const DesktopCaptureOptions& options) {
107  return WindowCapturer::Create(options);
108}
109#endif
110
111}  // namespace webrtc
112