Modify ScreenCaptureFrameQueue into a template

BUG=

Committed: https://crrev.com/34cad48cfbd362ae0c9027365550bfe28e2e10ef
Cr-Commit-Position: refs/heads/master@{#12458}

Review URL: https://codereview.webrtc.org/1902323002

Cr-Commit-Position: refs/heads/master@{#12478}
This commit is contained in:
zijiehe
2016-04-22 16:08:39 -07:00
committed by Commit bot
parent 8053f79bd9
commit 809dcb4b3e
11 changed files with 42 additions and 70 deletions

View File

@ -60,7 +60,6 @@ source_set("desktop_capture") {
"mouse_cursor_monitor.h", "mouse_cursor_monitor.h",
"mouse_cursor_monitor_mac.mm", "mouse_cursor_monitor_mac.mm",
"mouse_cursor_monitor_win.cc", "mouse_cursor_monitor_win.cc",
"screen_capture_frame_queue.cc",
"screen_capture_frame_queue.h", "screen_capture_frame_queue.h",
"screen_capturer.h", "screen_capturer.h",
"screen_capturer_helper.cc", "screen_capturer_helper.cc",

View File

@ -56,7 +56,6 @@
"mouse_cursor_monitor_mac.mm", "mouse_cursor_monitor_mac.mm",
"mouse_cursor_monitor_win.cc", "mouse_cursor_monitor_win.cc",
"mouse_cursor_monitor_x11.cc", "mouse_cursor_monitor_x11.cc",
"screen_capture_frame_queue.cc",
"screen_capture_frame_queue.h", "screen_capture_frame_queue.h",
"screen_capturer.h", "screen_capturer.h",
"screen_capturer_helper.cc", "screen_capturer_helper.cc",

View File

@ -1,44 +0,0 @@
/*
* Copyright (c) 2013 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_capture_frame_queue.h"
#include <assert.h>
#include <algorithm>
#include "webrtc/modules/desktop_capture/desktop_frame.h"
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "webrtc/system_wrappers/include/logging.h"
#include "webrtc/typedefs.h"
namespace webrtc {
ScreenCaptureFrameQueue::ScreenCaptureFrameQueue() : current_(0) {}
ScreenCaptureFrameQueue::~ScreenCaptureFrameQueue() {}
void ScreenCaptureFrameQueue::MoveToNextFrame() {
current_ = (current_ + 1) % kQueueLength;
// Verify that the frame is not shared, i.e. that consumer has released it
// before attempting to capture again.
assert(!frames_[current_].get() || !frames_[current_]->IsShared());
}
void ScreenCaptureFrameQueue::ReplaceCurrentFrame(DesktopFrame* frame) {
frames_[current_].reset(SharedDesktopFrame::Wrap(frame));
}
void ScreenCaptureFrameQueue::Reset() {
for (int i = 0; i < kQueueLength; ++i)
frames_[i].reset();
}
} // namespace webrtc

View File

@ -13,12 +13,12 @@
#include <memory> #include <memory>
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" #include "webrtc/base/constructormagic.h"
#include "webrtc/typedefs.h" // TODO(zijiehe): These headers are not used in this file, but to avoid build
// break in remoting/host. We should add headers in each individual files.
#include "webrtc/modules/desktop_capture/desktop_frame.h" // Remove
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h" // Remove
namespace webrtc {
class DesktopFrame;
} // namespace webrtc
namespace webrtc { namespace webrtc {
@ -35,28 +35,38 @@ namespace webrtc {
// Frame consumer is expected to never hold more than kQueueLength frames // Frame consumer is expected to never hold more than kQueueLength frames
// created by this function and it should release the earliest one before trying // created by this function and it should release the earliest one before trying
// to capture a new frame (i.e. before MoveToNextFrame() is called). // to capture a new frame (i.e. before MoveToNextFrame() is called).
template <typename FrameType>
class ScreenCaptureFrameQueue { class ScreenCaptureFrameQueue {
public: public:
ScreenCaptureFrameQueue(); ScreenCaptureFrameQueue() : current_(0) {}
~ScreenCaptureFrameQueue(); ~ScreenCaptureFrameQueue() = default;
// Moves to the next frame in the queue, moving the 'current' frame to become // Moves to the next frame in the queue, moving the 'current' frame to become
// the 'previous' one. // the 'previous' one.
void MoveToNextFrame(); void MoveToNextFrame() {
current_ = (current_ + 1) % kQueueLength;
}
// Replaces the current frame with a new one allocated by the caller. The // Replaces the current frame with a new one allocated by the caller. The
// existing frame (if any) is destroyed. Takes ownership of |frame|. // existing frame (if any) is destroyed. Takes ownership of |frame|.
void ReplaceCurrentFrame(DesktopFrame* frame); void ReplaceCurrentFrame(FrameType* frame) {
frames_[current_].reset(frame);
}
// Marks all frames obsolete and resets the previous frame pointer. No // Marks all frames obsolete and resets the previous frame pointer. No
// frames are freed though as the caller can still access them. // frames are freed though as the caller can still access them.
void Reset(); void Reset() {
for (int i = 0; i < kQueueLength; i++) {
frames_[i].reset();
}
current_ = 0;
}
SharedDesktopFrame* current_frame() const { FrameType* current_frame() const {
return frames_[current_].get(); return frames_[current_].get();
} }
SharedDesktopFrame* previous_frame() const { FrameType* previous_frame() const {
return frames_[(current_ + kQueueLength - 1) % kQueueLength].get(); return frames_[(current_ + kQueueLength - 1) % kQueueLength].get();
} }
@ -65,7 +75,7 @@ class ScreenCaptureFrameQueue {
int current_; int current_;
static const int kQueueLength = 2; static const int kQueueLength = 2;
std::unique_ptr<SharedDesktopFrame> frames_[kQueueLength]; std::unique_ptr<FrameType> frames_[kQueueLength];
RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue); RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue);
}; };

View File

@ -22,6 +22,7 @@
#include <OpenGL/CGLMacro.h> #include <OpenGL/CGLMacro.h>
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h>
#include "webrtc/base/checks.h"
#include "webrtc/base/macutils.h" #include "webrtc/base/macutils.h"
#include "webrtc/modules/desktop_capture/desktop_capture_options.h" #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/desktop_frame.h"
@ -32,6 +33,7 @@
#include "webrtc/modules/desktop_capture/mac/scoped_pixel_buffer_object.h" #include "webrtc/modules/desktop_capture/mac/scoped_pixel_buffer_object.h"
#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.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/screen_capturer_helper.h"
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "webrtc/system_wrappers/include/logging.h" #include "webrtc/system_wrappers/include/logging.h"
#include "webrtc/system_wrappers/include/tick_util.h" #include "webrtc/system_wrappers/include/tick_util.h"
@ -234,7 +236,7 @@ class ScreenCapturerMac : public ScreenCapturer {
ScopedPixelBufferObject pixel_buffer_object_; ScopedPixelBufferObject pixel_buffer_object_;
// Queue of the frames buffers. // Queue of the frames buffers.
ScreenCaptureFrameQueue queue_; ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Current display configuration. // Current display configuration.
MacDesktopConfiguration desktop_config_; MacDesktopConfiguration desktop_config_;
@ -384,6 +386,7 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) {
TickTime capture_start_time = TickTime::Now(); TickTime capture_start_time = TickTime::Now();
queue_.MoveToNextFrame(); queue_.MoveToNextFrame();
RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared());
desktop_config_monitor_->Lock(); desktop_config_monitor_->Lock();
MacDesktopConfiguration new_config = MacDesktopConfiguration new_config =
@ -405,7 +408,7 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) {
// Note that we can't reallocate other buffers at this point, since the caller // Note that we can't reallocate other buffers at this point, since the caller
// may still be reading from them. // may still be reading from them.
if (!queue_.current_frame()) if (!queue_.current_frame())
queue_.ReplaceCurrentFrame(CreateFrame()); queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(CreateFrame()));
DesktopFrame* current_frame = queue_.current_frame(); DesktopFrame* current_frame = queue_.current_frame();

View File

@ -26,6 +26,7 @@
#include "webrtc/modules/desktop_capture/differ.h" #include "webrtc/modules/desktop_capture/differ.h"
#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.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/screen_capturer_helper.h"
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h" #include "webrtc/modules/desktop_capture/x11/x_server_pixel_buffer.h"
#include "webrtc/system_wrappers/include/logging.h" #include "webrtc/system_wrappers/include/logging.h"
#include "webrtc/system_wrappers/include/tick_util.h" #include "webrtc/system_wrappers/include/tick_util.h"
@ -106,7 +107,7 @@ class ScreenCapturerLinux : public ScreenCapturer,
ScreenCapturerHelper helper_; ScreenCapturerHelper helper_;
// Queue of the frames buffers. // Queue of the frames buffers.
ScreenCaptureFrameQueue queue_; ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Invalid region from the previous capture. This is used to synchronize the // Invalid region from the previous capture. This is used to synchronize the
// current with the last buffer used. // current with the last buffer used.
@ -237,6 +238,7 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) {
TickTime capture_start_time = TickTime::Now(); TickTime capture_start_time = TickTime::Now();
queue_.MoveToNextFrame(); queue_.MoveToNextFrame();
RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared());
// Process XEvents for XDamage and cursor shape tracking. // Process XEvents for XDamage and cursor shape tracking.
options_.x_display()->ProcessPendingXEvents(); options_.x_display()->ProcessPendingXEvents();
@ -256,7 +258,7 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) {
if (!queue_.current_frame()) { if (!queue_.current_frame()) {
std::unique_ptr<DesktopFrame> frame( std::unique_ptr<DesktopFrame> frame(
new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); new BasicDesktopFrame(x_server_pixel_buffer_.window_size()));
queue_.ReplaceCurrentFrame(frame.release()); queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release()));
} }
// Refresh the Differ helper used by CaptureFrame(), if needed. // Refresh the Differ helper used by CaptureFrame(), if needed.

View File

@ -48,8 +48,7 @@ class SharedDesktopFrame::Core {
SharedDesktopFrame::~SharedDesktopFrame() {} SharedDesktopFrame::~SharedDesktopFrame() {}
// static // static
SharedDesktopFrame* SharedDesktopFrame::Wrap( SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) {
DesktopFrame* desktop_frame) {
rtc::scoped_refptr<Core> core(new Core(desktop_frame)); rtc::scoped_refptr<Core> core(new Core(desktop_frame));
return new SharedDesktopFrame(core); return new SharedDesktopFrame(core);
} }

View File

@ -14,6 +14,7 @@
#include <utility> #include <utility>
#include "webrtc/base/checks.h"
#include "webrtc/modules/desktop_capture/desktop_capture_options.h" #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/desktop_frame.h"
#include "webrtc/modules/desktop_capture/desktop_frame_win.h" #include "webrtc/modules/desktop_capture/desktop_frame_win.h"
@ -82,6 +83,7 @@ void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) {
TickTime capture_start_time = TickTime::Now(); TickTime capture_start_time = TickTime::Now();
queue_.MoveToNextFrame(); queue_.MoveToNextFrame();
RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared());
// Request that the system not power-down the system, or the display hardware. // Request that the system not power-down the system, or the display hardware.
if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) {
@ -247,9 +249,9 @@ bool ScreenCapturerWinGdi::CaptureImage() {
std::unique_ptr<DesktopFrame> buffer(DesktopFrameWin::Create( std::unique_ptr<DesktopFrame> buffer(DesktopFrameWin::Create(
size, shared_memory_factory_.get(), desktop_dc_)); size, shared_memory_factory_.get(), desktop_dc_));
if (!buffer.get()) if (!buffer)
return false; return false;
queue_.ReplaceCurrentFrame(buffer.release()); queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(buffer.release()));
} }
// Select the target bitmap into the memory dc and copy the rect from desktop // Select the target bitmap into the memory dc and copy the rect from desktop

View File

@ -20,6 +20,7 @@
#include "webrtc/base/scoped_ptr.h" #include "webrtc/base/scoped_ptr.h"
#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.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/screen_capturer_helper.h"
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h" #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"
namespace webrtc { namespace webrtc {
@ -71,7 +72,7 @@ class ScreenCapturerWinGdi : public ScreenCapturer {
HDC memory_dc_; HDC memory_dc_;
// Queue of the frames buffers. // Queue of the frames buffers.
ScreenCaptureFrameQueue queue_; ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Rectangle describing the bounds of the desktop device context, relative to // Rectangle describing the bounds of the desktop device context, relative to
// the primary display's top-left. // the primary display's top-left.

View File

@ -433,7 +433,7 @@ void ScreenCapturerWinMagnifier::CreateCurrentFrameIfNecessary(
? SharedMemoryDesktopFrame::Create(size, ? SharedMemoryDesktopFrame::Create(size,
shared_memory_factory_.get()) shared_memory_factory_.get())
: std::unique_ptr<DesktopFrame>(new BasicDesktopFrame(size)); : std::unique_ptr<DesktopFrame>(new BasicDesktopFrame(size));
queue_.ReplaceCurrentFrame(frame.release()); queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release()));
} }
} }

View File

@ -22,6 +22,7 @@
#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
#include "webrtc/modules/desktop_capture/screen_capturer.h" #include "webrtc/modules/desktop_capture/screen_capturer.h"
#include "webrtc/modules/desktop_capture/screen_capturer_helper.h" #include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h" #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"
#include "webrtc/system_wrappers/include/atomic32.h" #include "webrtc/system_wrappers/include/atomic32.h"
@ -118,7 +119,7 @@ class ScreenCapturerWinMagnifier : public ScreenCapturer {
ScreenCapturerHelper helper_; ScreenCapturerHelper helper_;
// Queue of the frames buffers. // Queue of the frames buffers.
ScreenCaptureFrameQueue queue_; ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Class to calculate the difference between two screen bitmaps. // Class to calculate the difference between two screen bitmaps.
std::unique_ptr<Differ> differ_; std::unique_ptr<Differ> differ_;