author Randell Jesup <>
Wed, 18 Nov 2015 15:03:25 -0500
changeset 307280 ae32ad44ce1c048258a881dbaa7727def08c53a4
parent 307279 f62c9e49a44fb473b5c8701776d8f5ae3da22cbc
child 356084 27eeb6ce9c20ea30b3018e2ab614698f1284f78c
permissions -rw-r--r--
Bug 1198458: Rollup of changes previously applied to media/webrtc/trunk/webrtc and fixes to those rs=jesup r=froyd,jib,bwc,jesup,gcp,sotaro,pkerr,pehrsons Landing as one rolled-up patch to avoid breaking regression tests, and in keeping with previous WebRTC imports. Broken out parts that needed review are on the bug.

 *  Copyright (c) 2014 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 <windows.h>

#include "webrtc/base/scoped_ptr.h"
#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
#include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
#include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"

namespace webrtc {

class Differ;

// ScreenCapturerWinGdi captures 32bit RGB using GDI.
// ScreenCapturerWinGdi is double-buffered as required by ScreenCapturer.
class ScreenCapturerWinGdi : public ScreenCapturer {
  explicit ScreenCapturerWinGdi(const DesktopCaptureOptions& options);
  virtual ~ScreenCapturerWinGdi();

  // Overridden from ScreenCapturer:
  void Start(Callback* callback) override;
  void Capture(const DesktopRegion& region) override;
  bool GetScreenList(ScreenList* screens) override;
  bool SelectScreen(ScreenId id) override;

  typedef HRESULT (WINAPI * DwmEnableCompositionFunc)(UINT);
  typedef HRESULT (WINAPI * DwmIsCompositionEnabledFunc)(BOOL*);

  // Make sure that the device contexts match the screen configuration.
  void PrepareCaptureResources();

  // Captures the current screen contents into the current buffer. Returns true
  // if succeeded.
  bool CaptureImage();

  // Capture the current cursor shape.
  void CaptureCursor();

  Callback* callback_;
  ScreenId current_screen_id_;
  std::wstring current_device_key_;

  // A thread-safe list of invalid rectangles, and the size of the most
  // recently captured screen.
  ScreenCapturerHelper helper_;

  ScopedThreadDesktop desktop_;

  // GDI resources used for screen capture.
  HDC desktop_dc_;
  HDC memory_dc_;

  // Queue of the frames buffers.
  ScreenCaptureFrameQueue queue_;

  // Rectangle describing the bounds of the desktop device context, relative to
  // the primary display's top-left.
  DesktopRect desktop_dc_rect_;

  // Class to calculate the difference between two screen bitmaps.
  rtc::scoped_ptr<Differ> differ_;

  HMODULE dwmapi_library_;
  DwmEnableCompositionFunc composition_func_;
  DwmIsCompositionEnabledFunc composition_enabled_func_;

  bool disable_composition_;

  // Used to suppress duplicate logging of SetThreadExecutionState errors.
  bool set_thread_execution_state_failed_;


}  // namespace webrtc