From b4c7b8365d9ed11099b4c5bdcc4eeab33923cd9c Mon Sep 17 00:00:00 2001 From: sergeyu Date: Fri, 3 Jun 2016 05:56:42 -0700 Subject: [PATCH] Revert of Use std::unique_ptr<> to pass frame ownership in DesktopCapturer impls. (patchset #7 id:140001 of https://codereview.webrtc.org/1988783003/ ) Reason for revert: Broke chromium builder Original issue's description: > Use std::unique_ptr<> to pass frame ownership in DesktopCapturer impls. > > Previously raw pointers were used for owned DesktopFrame instances. > Updated all screen and window capturer implementations to use > std::unique_ptr<>. > > Also includes some other cleanups in the capturers: > - s/NULL/nullptr > - moved default initializers to class definition. > > BUG=webrtc:5950 > > Committed: https://crrev.com/4a627a8c13554d12412cabb8f751caee6e61ee32 > Cr-Commit-Position: refs/heads/master@{#13032} TBR=wez@chromium.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=webrtc:5950 Review-Url: https://codereview.webrtc.org/2030333003 Cr-Commit-Position: refs/heads/master@{#13033} --- .../desktop_capture/cropped_desktop_frame.cc | 23 ++-- .../desktop_capture/cropped_desktop_frame.h | 9 +- .../cropping_window_capturer.cc | 18 +-- .../cropping_window_capturer.h | 3 +- .../desktop_and_cursor_composer.cc | 30 +++-- .../desktop_and_cursor_composer.h | 3 +- .../desktop_and_cursor_composer_unittest.cc | 20 ++-- .../desktop_capture/desktop_capturer.h | 31 +---- .../desktop_capture/desktop_frame_win.cc | 8 +- .../desktop_capture/desktop_frame_win.h | 6 +- .../screen_capture_frame_queue.h | 4 +- .../desktop_capture/screen_capturer_mac.mm | 107 ++++++++++-------- .../screen_capturer_mac_unittest.cc | 29 ++--- .../screen_capturer_mock_objects.h | 8 +- .../screen_capturer_unittest.cc | 30 +++-- .../desktop_capture/screen_capturer_x11.cc | 82 ++++++++------ .../desktop_capture/shared_desktop_frame.cc | 55 ++++++--- .../desktop_capture/shared_desktop_frame.h | 10 +- .../win/screen_capturer_win_directx.cc | 62 +++++----- .../win/screen_capturer_win_directx.h | 4 +- .../win/screen_capturer_win_gdi.cc | 52 +++++---- .../win/screen_capturer_win_gdi.h | 14 +-- .../win/screen_capturer_win_magnifier.cc | 65 ++++++++--- .../win/screen_capturer_win_magnifier.h | 32 +++--- .../desktop_capture/window_capturer_mac.mm | 35 +++--- .../desktop_capture/window_capturer_null.cc | 12 +- .../window_capturer_unittest.cc | 5 +- .../desktop_capture/window_capturer_win.cc | 34 +++--- .../desktop_capture/window_capturer_x11.cc | 42 ++++--- 29 files changed, 447 insertions(+), 386 deletions(-) mode change 100644 => 100755 webrtc/modules/desktop_capture/window_capturer_x11.cc diff --git a/webrtc/modules/desktop_capture/cropped_desktop_frame.cc b/webrtc/modules/desktop_capture/cropped_desktop_frame.cc index b26d5b0600..733fe9b2ad 100644 --- a/webrtc/modules/desktop_capture/cropped_desktop_frame.cc +++ b/webrtc/modules/desktop_capture/cropped_desktop_frame.cc @@ -19,8 +19,7 @@ namespace webrtc { // A DesktopFrame that is a sub-rect of another DesktopFrame. class CroppedDesktopFrame : public DesktopFrame { public: - CroppedDesktopFrame(std::unique_ptr frame, - const DesktopRect& rect); + CroppedDesktopFrame(DesktopFrame* frame, const DesktopRect& rect); private: std::unique_ptr frame_; @@ -28,23 +27,23 @@ class CroppedDesktopFrame : public DesktopFrame { RTC_DISALLOW_COPY_AND_ASSIGN(CroppedDesktopFrame); }; -std::unique_ptr CreateCroppedDesktopFrame( - std::unique_ptr frame, - const DesktopRect& rect) { - if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) - return nullptr; +DesktopFrame* +CreateCroppedDesktopFrame(DesktopFrame* frame, const DesktopRect& rect) { + if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) { + delete frame; + return NULL; + } - return std::unique_ptr( - new CroppedDesktopFrame(std::move(frame), rect)); + return new CroppedDesktopFrame(frame, rect); } -CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr frame, +CroppedDesktopFrame::CroppedDesktopFrame(DesktopFrame* frame, const DesktopRect& rect) : DesktopFrame(rect.size(), frame->stride(), frame->GetFrameDataAtPos(rect.top_left()), - frame->shared_memory()) { - frame_ = std::move(frame); + frame->shared_memory()), + frame_(frame) { } } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/cropped_desktop_frame.h b/webrtc/modules/desktop_capture/cropped_desktop_frame.h index 42ae587acb..29449e27f3 100644 --- a/webrtc/modules/desktop_capture/cropped_desktop_frame.h +++ b/webrtc/modules/desktop_capture/cropped_desktop_frame.h @@ -15,11 +15,10 @@ namespace webrtc { -// Returns nullptr frame if |rect| is not contained by the bounds of |frame|. -std::unique_ptr CreateCroppedDesktopFrame( - std::unique_ptr frame, - const DesktopRect& rect); - +// Always takes ownership of |frame|. Returns NULL if |rect| is not contained +// by the bounds of |frame|. +DesktopFrame* CreateCroppedDesktopFrame(DesktopFrame* frame, + const DesktopRect& rect); } // namespace webrtc #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_CROPPED_DESKTOP_FRAME_H_ diff --git a/webrtc/modules/desktop_capture/cropping_window_capturer.cc b/webrtc/modules/desktop_capture/cropping_window_capturer.cc index 50aaeea030..cbe7d96e5d 100644 --- a/webrtc/modules/desktop_capture/cropping_window_capturer.cc +++ b/webrtc/modules/desktop_capture/cropping_window_capturer.cc @@ -74,31 +74,31 @@ bool CroppingWindowCapturer::BringSelectedWindowToFront() { return window_capturer_->BringSelectedWindowToFront(); } -void CroppingWindowCapturer::OnCaptureResult( - DesktopCapturer::Result result, - std::unique_ptr screen_frame) { +void CroppingWindowCapturer::OnCaptureCompleted(DesktopFrame* frame) { + std::unique_ptr screen_frame(frame); + if (!ShouldUseScreenCapturer()) { LOG(LS_INFO) << "Window no longer on top when ScreenCapturer finishes"; window_capturer_->Capture(DesktopRegion()); return; } - if (result != Result::SUCCESS) { + if (!frame) { LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame"; - callback_->OnCaptureResult(result, nullptr); + callback_->OnCaptureCompleted(NULL); return; } DesktopRect window_rect = GetWindowRectInVirtualScreen(); if (window_rect.is_empty()) { LOG(LS_WARNING) << "Window rect is empty"; - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } - callback_->OnCaptureResult( - Result::SUCCESS, - CreateCroppedDesktopFrame(std::move(screen_frame), window_rect)); + std::unique_ptr window_frame( + CreateCroppedDesktopFrame(screen_frame.release(), window_rect)); + callback_->OnCaptureCompleted(window_frame.release()); } #if !defined(WEBRTC_WIN) diff --git a/webrtc/modules/desktop_capture/cropping_window_capturer.h b/webrtc/modules/desktop_capture/cropping_window_capturer.h index e2db7daf10..dfeb447e44 100644 --- a/webrtc/modules/desktop_capture/cropping_window_capturer.h +++ b/webrtc/modules/desktop_capture/cropping_window_capturer.h @@ -42,8 +42,7 @@ class CroppingWindowCapturer : public WindowCapturer, // DesktopCapturer::Callback implementation, passed to |screen_capturer_| to // intercept the capture result. - void OnCaptureResult(DesktopCapturer::Result result, - std::unique_ptr frame) override; + void OnCaptureCompleted(DesktopFrame* frame) override; protected: explicit CroppingWindowCapturer(const DesktopCaptureOptions& options); diff --git a/webrtc/modules/desktop_capture/desktop_and_cursor_composer.cc b/webrtc/modules/desktop_capture/desktop_and_cursor_composer.cc index a4deda6ae9..4c6e27e561 100644 --- a/webrtc/modules/desktop_capture/desktop_and_cursor_composer.cc +++ b/webrtc/modules/desktop_capture/desktop_and_cursor_composer.cc @@ -57,7 +57,7 @@ void AlphaBlend(uint8_t* dest, int dest_stride, class DesktopFrameWithCursor : public DesktopFrame { public: // Takes ownership of |frame|. - DesktopFrameWithCursor(std::unique_ptr frame, + DesktopFrameWithCursor(DesktopFrame* frame, const MouseCursor& cursor, const DesktopVector& position); virtual ~DesktopFrameWithCursor(); @@ -71,18 +71,15 @@ class DesktopFrameWithCursor : public DesktopFrame { RTC_DISALLOW_COPY_AND_ASSIGN(DesktopFrameWithCursor); }; -DesktopFrameWithCursor::DesktopFrameWithCursor( - std::unique_ptr frame, - const MouseCursor& cursor, - const DesktopVector& position) - : DesktopFrame(frame->size(), - frame->stride(), - frame->data(), - frame->shared_memory()) { +DesktopFrameWithCursor::DesktopFrameWithCursor(DesktopFrame* frame, + const MouseCursor& cursor, + const DesktopVector& position) + : DesktopFrame(frame->size(), frame->stride(), + frame->data(), frame->shared_memory()), + original_frame_(frame) { set_dpi(frame->dpi()); set_capture_time_ms(frame->capture_time_ms()); mutable_updated_region()->Swap(frame->mutable_updated_region()); - original_frame_ = std::move(frame); DesktopVector image_pos = position.subtract(cursor.hotspot()); DesktopRect target_rect = DesktopRect::MakeSize(cursor.image()->size()); @@ -155,15 +152,14 @@ void DesktopAndCursorComposer::SetExcludedWindow(WindowId window) { desktop_capturer_->SetExcludedWindow(window); } -void DesktopAndCursorComposer::OnCaptureResult( - DesktopCapturer::Result result, - std::unique_ptr frame) { - if (frame && cursor_ && cursor_state_ == MouseCursorMonitor::INSIDE) { - frame = std::unique_ptr(new DesktopFrameWithCursor( - std::move(frame), *cursor_, cursor_position_)); +void DesktopAndCursorComposer::OnCaptureCompleted(DesktopFrame* frame) { + if (frame && cursor_.get() && cursor_state_ == MouseCursorMonitor::INSIDE) { + DesktopFrameWithCursor* frame_with_cursor = + new DesktopFrameWithCursor(frame, *cursor_, cursor_position_); + frame = frame_with_cursor; } - callback_->OnCaptureResult(result, std::move(frame)); + callback_->OnCaptureCompleted(frame); } void DesktopAndCursorComposer::OnMouseCursor(MouseCursor* cursor) { diff --git a/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h b/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h index ef6340211f..dcbe6129e6 100644 --- a/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h +++ b/webrtc/modules/desktop_capture/desktop_and_cursor_composer.h @@ -42,8 +42,7 @@ class DesktopAndCursorComposer : public DesktopCapturer, private: // DesktopCapturer::Callback interface. - void OnCaptureResult(DesktopCapturer::Result result, - std::unique_ptr frame) override; + void OnCaptureCompleted(DesktopFrame* frame) override; // MouseCursorMonitor::Callback interface. void OnMouseCursor(MouseCursor* cursor) override; diff --git a/webrtc/modules/desktop_capture/desktop_and_cursor_composer_unittest.cc b/webrtc/modules/desktop_capture/desktop_and_cursor_composer_unittest.cc index 9e89439b6f..6652a1af71 100644 --- a/webrtc/modules/desktop_capture/desktop_and_cursor_composer_unittest.cc +++ b/webrtc/modules/desktop_capture/desktop_and_cursor_composer_unittest.cc @@ -78,17 +78,15 @@ class FakeScreenCapturer : public DesktopCapturer { void Start(Callback* callback) override { callback_ = callback; } void Capture(const DesktopRegion& region) override { - callback_->OnCaptureResult( - next_frame_ ? Result::SUCCESS : Result::ERROR_TEMPORARY, - std::move(next_frame_)); + callback_->OnCaptureCompleted(next_frame_.release()); } - void SetNextFrame(std::unique_ptr next_frame) { - next_frame_ = std::move(next_frame); + void SetNextFrame(DesktopFrame* next_frame) { + next_frame_.reset(next_frame); } private: - Callback* callback_ = nullptr; + Callback* callback_; std::unique_ptr next_frame_; }; @@ -167,13 +165,11 @@ class DesktopAndCursorComposerTest : public testing::Test, DesktopAndCursorComposerTest() : fake_screen_(new FakeScreenCapturer()), fake_cursor_(new FakeMouseMonitor()), - blender_(fake_screen_, fake_cursor_) {} + blender_(fake_screen_, fake_cursor_) { + } // DesktopCapturer::Callback interface - void OnCaptureResult(DesktopCapturer::Result result, - std::unique_ptr frame) override { - frame_ = std::move(frame); - } + void OnCaptureCompleted(DesktopFrame* frame) override { frame_.reset(frame); } protected: // Owned by |blender_|. @@ -191,7 +187,7 @@ TEST_F(DesktopAndCursorComposerTest, Error) { fake_cursor_->SetHotspot(DesktopVector()); fake_cursor_->SetState(MouseCursorMonitor::INSIDE, DesktopVector()); - fake_screen_->SetNextFrame(nullptr); + fake_screen_->SetNextFrame(NULL); blender_.Capture(DesktopRegion()); diff --git a/webrtc/modules/desktop_capture/desktop_capturer.h b/webrtc/modules/desktop_capture/desktop_capturer.h index 80d910908b..ba70e01553 100644 --- a/webrtc/modules/desktop_capture/desktop_capturer.h +++ b/webrtc/modules/desktop_capture/desktop_capturer.h @@ -15,45 +15,24 @@ #include -#include "webrtc/modules/desktop_capture/desktop_frame.h" #include "webrtc/modules/desktop_capture/desktop_capture_types.h" #include "webrtc/modules/desktop_capture/shared_memory.h" namespace webrtc { class DesktopFrame; +class DesktopRegion; // Abstract interface for screen and window capturers. class DesktopCapturer { public: - enum class Result { - // The frame was captured successfully. - SUCCESS, - - // There was a temporary error. The caller should continue calling - // Capture(), in the expectation that it will eventually recover. - ERROR_TEMPORARY, - - // Capture has failed and will keep failing if the caller tries calling - // Capture() again. - ERROR_PERMANENT, - }; - // Interface that must be implemented by the DesktopCapturer consumers. class Callback { public: - // Called after a frame has been captured. |frame| is not nullptr if and - // only if |result| is SUCCESS. - virtual void OnCaptureResult(Result result, - std::unique_ptr frame) { - OnCaptureCompleted(frame.release()); - } - - // Deprecated version of the method above that uses raw pointer instead of - // std::unique_ptr<>. - // TODO(sergeyu): Remove this method and make OnCaptureResult() pure - // virtual. crbug.com/webrtc/5950 - virtual void OnCaptureCompleted(DesktopFrame* frame) { delete frame; }; + // Called after a frame has been captured. Handler must take ownership of + // |frame|. If capture has failed for any reason |frame| is set to NULL + // (e.g. the window has been closed). + virtual void OnCaptureCompleted(DesktopFrame* frame) = 0; protected: virtual ~Callback() {} diff --git a/webrtc/modules/desktop_capture/desktop_frame_win.cc b/webrtc/modules/desktop_capture/desktop_frame_win.cc index 6adce92f39..624b729203 100644 --- a/webrtc/modules/desktop_capture/desktop_frame_win.cc +++ b/webrtc/modules/desktop_capture/desktop_frame_win.cc @@ -30,7 +30,7 @@ DesktopFrameWin::~DesktopFrameWin() { } // static -std::unique_ptr DesktopFrameWin::Create( +DesktopFrameWin* DesktopFrameWin::Create( DesktopSize size, SharedMemoryFactory* shared_memory_factory, HDC hdc) { @@ -60,9 +60,9 @@ std::unique_ptr DesktopFrameWin::Create( return nullptr; } - return std::unique_ptr( - new DesktopFrameWin(size, bytes_per_row, reinterpret_cast(data), - std::move(shared_memory), bitmap)); + return new DesktopFrameWin(size, bytes_per_row, + reinterpret_cast(data), + std::move(shared_memory), bitmap); } } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/desktop_frame_win.h b/webrtc/modules/desktop_capture/desktop_frame_win.h index e9a374b2fc..3513e14ffb 100644 --- a/webrtc/modules/desktop_capture/desktop_frame_win.h +++ b/webrtc/modules/desktop_capture/desktop_frame_win.h @@ -26,9 +26,9 @@ namespace webrtc { class DesktopFrameWin : public DesktopFrame { public: virtual ~DesktopFrameWin(); - - static std::unique_ptr - Create(DesktopSize size, SharedMemoryFactory* shared_memory_factory, HDC hdc); + static DesktopFrameWin* Create(DesktopSize size, + SharedMemoryFactory* shared_memory_factory, + HDC hdc); HBITMAP bitmap() { return bitmap_; } diff --git a/webrtc/modules/desktop_capture/screen_capture_frame_queue.h b/webrtc/modules/desktop_capture/screen_capture_frame_queue.h index b7945b0921..97f3b810e9 100644 --- a/webrtc/modules/desktop_capture/screen_capture_frame_queue.h +++ b/webrtc/modules/desktop_capture/screen_capture_frame_queue.h @@ -49,8 +49,8 @@ class ScreenCaptureFrameQueue { // Replaces the current frame with a new one allocated by the caller. The // existing frame (if any) is destroyed. Takes ownership of |frame|. - void ReplaceCurrentFrame(std::unique_ptr frame) { - frames_[current_] = std::move(frame); + void ReplaceCurrentFrame(FrameType* frame) { + frames_[current_].reset(frame); } // Marks all frames obsolete and resets the previous frame pointer. No diff --git a/webrtc/modules/desktop_capture/screen_capturer_mac.mm b/webrtc/modules/desktop_capture/screen_capturer_mac.mm index b846b7571c..bf6c72950c 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_mac.mm +++ b/webrtc/modules/desktop_capture/screen_capturer_mac.mm @@ -96,15 +96,15 @@ void CopyRect(const uint8_t* src_plane, // caller should release the returned CFArrayRef. CFArrayRef CreateWindowListWithExclusion(CGWindowID window_to_exclude) { if (!window_to_exclude) - return nullptr; + return NULL; CFArrayRef all_windows = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID); if (!all_windows) - return nullptr; + return NULL; - CFMutableArrayRef returned_array = - CFArrayCreateMutable(nullptr, CFArrayGetCount(all_windows), nullptr); + CFMutableArrayRef returned_array = CFArrayCreateMutable( + NULL, CFArrayGetCount(all_windows), NULL); bool found = false; for (CFIndex i = 0; i < CFArrayGetCount(all_windows); ++i) { @@ -126,7 +126,7 @@ CFArrayRef CreateWindowListWithExclusion(CGWindowID window_to_exclude) { if (!found) { CFRelease(returned_array); - returned_array = nullptr; + returned_array = NULL; } return returned_array; } @@ -143,7 +143,7 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, ids[0] = window; CFArrayRef window_id_array = - CFArrayCreate(nullptr, reinterpret_cast(&ids), 1, nullptr); + CFArrayCreate(NULL, reinterpret_cast(&ids), 1, NULL); CFArrayRef window_array = CGWindowListCreateDescriptionFromArray(window_id_array); @@ -229,11 +229,11 @@ class ScreenCapturerMac : public ScreenCapturer { void *user_parameter); void ReleaseBuffers(); - std::unique_ptr CreateFrame(); + DesktopFrame* CreateFrame(); - Callback* callback_ = nullptr; + Callback* callback_; - CGLContextObj cgl_context_ = nullptr; + CGLContextObj cgl_context_; ScopedPixelBufferObject pixel_buffer_object_; // Queue of the frames buffers. @@ -244,13 +244,13 @@ class ScreenCapturerMac : public ScreenCapturer { // Currently selected display, or 0 if the full desktop is selected. On OS X // 10.6 and before, this is always 0. - CGDirectDisplayID current_display_ = 0; + CGDirectDisplayID current_display_; // The physical pixel bounds of the current screen. DesktopRect screen_pixel_bounds_; // The dip to physical pixel scale of the current screen. - float dip_to_pixel_scale_ = 1.0f; + float dip_to_pixel_scale_; // A thread-safe list of invalid rectangles, and the size of the most // recently captured screen. @@ -263,20 +263,20 @@ class ScreenCapturerMac : public ScreenCapturer { rtc::scoped_refptr desktop_config_monitor_; // Power management assertion to prevent the screen from sleeping. - IOPMAssertionID power_assertion_id_display_ = kIOPMNullAssertionID; + IOPMAssertionID power_assertion_id_display_; // Power management assertion to indicate that the user is active. - IOPMAssertionID power_assertion_id_user_ = kIOPMNullAssertionID; + IOPMAssertionID power_assertion_id_user_; // Dynamically link to deprecated APIs for Mac OS X 10.6 support. - void* app_services_library_ = nullptr; - CGDisplayBaseAddressFunc cg_display_base_address_ = nullptr; - CGDisplayBytesPerRowFunc cg_display_bytes_per_row_ = nullptr; - CGDisplayBitsPerPixelFunc cg_display_bits_per_pixel_ = nullptr; - void* opengl_library_ = nullptr; - CGLSetFullScreenFunc cgl_set_full_screen_ = nullptr; + void* app_services_library_; + CGDisplayBaseAddressFunc cg_display_base_address_; + CGDisplayBytesPerRowFunc cg_display_bytes_per_row_; + CGDisplayBitsPerPixelFunc cg_display_bits_per_pixel_; + void* opengl_library_; + CGLSetFullScreenFunc cgl_set_full_screen_; - CGWindowID excluded_window_ = 0; + CGWindowID excluded_window_; RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerMac); }; @@ -285,16 +285,16 @@ class ScreenCapturerMac : public ScreenCapturer { // stride. class InvertedDesktopFrame : public DesktopFrame { public: - InvertedDesktopFrame(std::unique_ptr frame) + // Takes ownership of |frame|. + InvertedDesktopFrame(DesktopFrame* frame) : DesktopFrame( - frame->size(), - -frame->stride(), + frame->size(), -frame->stride(), frame->data() + (frame->size().height() - 1) * frame->stride(), - frame->shared_memory()) { - original_frame_ = std::move(frame); - set_dpi(original_frame_->dpi()); - set_capture_time_ms(original_frame_->capture_time_ms()); - mutable_updated_region()->Swap(original_frame_->mutable_updated_region()); + frame->shared_memory()), + original_frame_(frame) { + set_dpi(frame->dpi()); + set_capture_time_ms(frame->capture_time_ms()); + mutable_updated_region()->Swap(frame->mutable_updated_region()); } virtual ~InvertedDesktopFrame() {} @@ -306,7 +306,21 @@ class InvertedDesktopFrame : public DesktopFrame { ScreenCapturerMac::ScreenCapturerMac( rtc::scoped_refptr desktop_config_monitor) - : desktop_config_monitor_(desktop_config_monitor) {} + : callback_(NULL), + cgl_context_(NULL), + current_display_(0), + dip_to_pixel_scale_(1.0f), + desktop_config_monitor_(desktop_config_monitor), + power_assertion_id_display_(kIOPMNullAssertionID), + power_assertion_id_user_(kIOPMNullAssertionID), + app_services_library_(NULL), + cg_display_base_address_(NULL), + cg_display_bytes_per_row_(NULL), + cg_display_bits_per_pixel_(NULL), + opengl_library_(NULL), + cgl_set_full_screen_(NULL), + excluded_window_(0) { +} ScreenCapturerMac::~ScreenCapturerMac() { if (power_assertion_id_display_ != kIOPMNullAssertionID) { @@ -339,7 +353,7 @@ void ScreenCapturerMac::ReleaseBuffers() { if (cgl_context_) { pixel_buffer_object_.Release(); CGLDestroyContext(cgl_context_); - cgl_context_ = nullptr; + cgl_context_ = NULL; } // The buffers might be in use by the encoder, so don't delete them here. // Instead, mark them as "needs update"; next time the buffers are used by @@ -405,7 +419,7 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) { // APIS currently crash on 10.6.8 if there is no monitor attached. if (!CgBlitPostLion(*current_frame, region)) { desktop_config_monitor_->Unlock(); - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } } else if (cgl_context_) { @@ -421,11 +435,11 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) { CgBlitPreLion(*current_frame, region); } - std::unique_ptr new_frame = queue_.current_frame()->Share(); + DesktopFrame* new_frame = queue_.current_frame()->Share(); *new_frame->mutable_updated_region() = region; if (flip) - new_frame.reset(new InvertedDesktopFrame(std::move(new_frame))); + new_frame = new InvertedDesktopFrame(new_frame); helper_.set_size_most_recent(new_frame->size()); @@ -433,9 +447,10 @@ void ScreenCapturerMac::Capture(const DesktopRegion& region_to_capture) { // and accessing display structures. desktop_config_monitor_->Unlock(); - new_frame->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) / - rtc::kNumNanosecsPerMillisec); - callback_->OnCaptureResult(Result::SUCCESS, std::move(new_frame)); + new_frame->set_capture_time_ms( + (rtc::TimeNanos() - capture_start_time_nanos) / + rtc::kNumNanosecsPerMillisec); + callback_->OnCaptureCompleted(new_frame); } void ScreenCapturerMac::SetExcludedWindow(WindowId window) { @@ -519,7 +534,7 @@ void ScreenCapturerMac::GlBlitFast(const DesktopFrame& frame, GL_UNSIGNED_BYTE, 0); GLubyte* ptr = static_cast( glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB)); - if (!ptr) { + if (ptr == NULL) { // If the buffer can't be mapped, assume that it's no longer valid and // release it. pixel_buffer_object_.Release(); @@ -627,7 +642,8 @@ bool ScreenCapturerMac::CgBlitPostLion(const DesktopFrame& frame, // TODO(wez): Get rid of this as per crbug.com/145064, or implement // crbug.com/92354. if (queue_.previous_frame()) { - memcpy(frame.data(), queue_.previous_frame()->data(), + memcpy(frame.data(), + queue_.previous_frame()->data(), frame.stride() * frame.size().height()); } @@ -676,7 +692,7 @@ bool ScreenCapturerMac::CgBlitPostLion(const DesktopFrame& frame, copy_region.Translate(-display_bounds.left(), -display_bounds.top()); DesktopRect excluded_window_bounds; - CGImageRef excluded_image = nullptr; + CGImageRef excluded_image = NULL; if (excluded_window_ && window_list) { // Get the region of the excluded window relative the primary display. excluded_window_bounds = GetExcludedWindowPixelBounds( @@ -694,7 +710,7 @@ bool ScreenCapturerMac::CgBlitPostLion(const DesktopFrame& frame, // Create an image containing a snapshot of the display. CGImageRef image = CGDisplayCreateImage(display_config.id); - if (!image) { + if (image == NULL) { if (excluded_image) CFRelease(excluded_image); continue; @@ -858,13 +874,13 @@ void ScreenCapturerMac::ScreenConfigurationChanged() { (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(mainDevice), (CGLPixelFormatAttribute)0 }; - CGLPixelFormatObj pixel_format = nullptr; + CGLPixelFormatObj pixel_format = NULL; GLint matching_pixel_format_count = 0; CGLError err = CGLChoosePixelFormat(attributes, &pixel_format, &matching_pixel_format_count); assert(err == kCGLNoError); - err = CGLCreateContext(pixel_format, nullptr, &cgl_context_); + err = CGLCreateContext(pixel_format, NULL, &cgl_context_); assert(err == kCGLNoError); CGLDestroyPixelFormat(pixel_format); (*cgl_set_full_screen_)(cgl_context_); @@ -953,12 +969,13 @@ void ScreenCapturerMac::ScreenUpdateMoveCallback( capturer->ScreenUpdateMove(delta, count, rect_array); } -std::unique_ptr ScreenCapturerMac::CreateFrame() { +DesktopFrame* ScreenCapturerMac::CreateFrame() { std::unique_ptr frame( new BasicDesktopFrame(screen_pixel_bounds_.size())); + frame->set_dpi(DesktopVector(kStandardDPI * dip_to_pixel_scale_, kStandardDPI * dip_to_pixel_scale_)); - return frame; + return frame.release(); } } // namespace @@ -966,7 +983,7 @@ std::unique_ptr ScreenCapturerMac::CreateFrame() { // static ScreenCapturer* ScreenCapturer::Create(const DesktopCaptureOptions& options) { if (!options.configuration_monitor()) - return nullptr; + return NULL; std::unique_ptr capturer( new ScreenCapturerMac(options.configuration_monitor())); diff --git a/webrtc/modules/desktop_capture/screen_capturer_mac_unittest.cc b/webrtc/modules/desktop_capture/screen_capturer_mac_unittest.cc index 47e7219996..815c7f53af 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_mac_unittest.cc +++ b/webrtc/modules/desktop_capture/screen_capturer_mac_unittest.cc @@ -32,13 +32,11 @@ namespace webrtc { class ScreenCapturerMacTest : public testing::Test { public: // Verifies that the whole screen is initially dirty. - void CaptureDoneCallback1(DesktopCapturer::Result result, - std::unique_ptr* frame); + void CaptureDoneCallback1(DesktopFrame* frame); // Verifies that a rectangle explicitly marked as dirty is propagated // correctly. - void CaptureDoneCallback2(DesktopCapturer::Result result, - std::unique_ptr* frame); + void CaptureDoneCallback2(DesktopFrame* frame); protected: void SetUp() override { @@ -51,40 +49,37 @@ class ScreenCapturerMacTest : public testing::Test { }; void ScreenCapturerMacTest::CaptureDoneCallback1( - DesktopCapturer::Result result, - std::unique_ptr* frame) { - EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS); + DesktopFrame* frame) { + std::unique_ptr owned_frame(frame); MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent( MacDesktopConfiguration::BottomLeftOrigin); // Verify that the region contains full frame. - DesktopRegion::Iterator it((*frame)->updated_region()); + DesktopRegion::Iterator it(frame->updated_region()); EXPECT_TRUE(!it.IsAtEnd() && it.rect().equals(config.pixel_bounds)); } void ScreenCapturerMacTest::CaptureDoneCallback2( - DesktopCapturer::Result result, - std::unique_ptr* frame) { - EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS); + DesktopFrame* frame) { + std::unique_ptr owned_frame(frame); MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent( MacDesktopConfiguration::BottomLeftOrigin); int width = config.pixel_bounds.width(); int height = config.pixel_bounds.height(); - EXPECT_EQ(width, (*frame)->size().width()); - EXPECT_EQ(height, (*frame)->size().height()); - EXPECT_TRUE((*frame)->data() != NULL); + EXPECT_EQ(width, frame->size().width()); + EXPECT_EQ(height, frame->size().height()); + EXPECT_TRUE(frame->data() != NULL); // Depending on the capture method, the screen may be flipped or not, so // the stride may be positive or negative. EXPECT_EQ(static_cast(sizeof(uint32_t) * width), - abs((*frame)->stride())); + abs(frame->stride())); } TEST_F(ScreenCapturerMacTest, Capture) { - EXPECT_CALL(callback_, - OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) + EXPECT_CALL(callback_, OnCaptureCompleted(_)) .Times(2) .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback1)) .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback2)); diff --git a/webrtc/modules/desktop_capture/screen_capturer_mock_objects.h b/webrtc/modules/desktop_capture/screen_capturer_mock_objects.h index 1f17e8c8be..7264249e0f 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_mock_objects.h +++ b/webrtc/modules/desktop_capture/screen_capturer_mock_objects.h @@ -36,13 +36,7 @@ class MockScreenCapturerCallback : public ScreenCapturer::Callback { MockScreenCapturerCallback() {} virtual ~MockScreenCapturerCallback() {} - MOCK_METHOD2(OnCaptureResultPtr, - void(DesktopCapturer::Result result, - std::unique_ptr* frame)); - void OnCaptureResult(DesktopCapturer::Result result, - std::unique_ptr frame) override { - OnCaptureResultPtr(result, &frame); - } + MOCK_METHOD1(OnCaptureCompleted, void(DesktopFrame*)); private: RTC_DISALLOW_COPY_AND_ASSIGN(MockScreenCapturerCallback); diff --git a/webrtc/modules/desktop_capture/screen_capturer_unittest.cc b/webrtc/modules/desktop_capture/screen_capturer_unittest.cc index 4d7799cc68..bc87ed3eba 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_unittest.cc +++ b/webrtc/modules/desktop_capture/screen_capturer_unittest.cc @@ -23,6 +23,7 @@ using ::testing::_; using ::testing::AnyNumber; using ::testing::Return; +using ::testing::SaveArg; const int kTestSharedMemoryId = 123; @@ -68,10 +69,6 @@ class FakeSharedMemoryFactory : public SharedMemoryFactory { RTC_DISALLOW_COPY_AND_ASSIGN(FakeSharedMemoryFactory); }; -ACTION_P(SaveUniquePtrArg, dest) { - *dest = std::move(*arg1); -} - TEST_F(ScreenCapturerTest, GetScreenListAndSelectScreen) { webrtc::ScreenCapturer::ScreenList screens; EXPECT_TRUE(capturer_->GetScreenList(&screens)); @@ -87,10 +84,9 @@ TEST_F(ScreenCapturerTest, StartCapturer) { TEST_F(ScreenCapturerTest, Capture) { // Assume that Start() treats the screen as invalid initially. - std::unique_ptr frame; - EXPECT_CALL(callback_, - OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) - .WillOnce(SaveUniquePtrArg(&frame)); + DesktopFrame* frame = NULL; + EXPECT_CALL(callback_, OnCaptureCompleted(_)) + .WillOnce(SaveArg<0>(&frame)); capturer_->Start(&callback_); capturer_->Capture(DesktopRegion()); @@ -109,15 +105,16 @@ TEST_F(ScreenCapturerTest, Capture) { EXPECT_TRUE(it.rect().equals(DesktopRect::MakeSize(frame->size()))); it.Advance(); EXPECT_TRUE(it.IsAtEnd()); + + delete frame; } #if defined(WEBRTC_WIN) TEST_F(ScreenCapturerTest, UseSharedBuffers) { - std::unique_ptr frame; - EXPECT_CALL(callback_, - OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) - .WillOnce(SaveUniquePtrArg(&frame)); + DesktopFrame* frame = NULL; + EXPECT_CALL(callback_, OnCaptureCompleted(_)) + .WillOnce(SaveArg<0>(&frame)); capturer_->Start(&callback_); capturer_->SetSharedMemoryFactory( @@ -127,6 +124,8 @@ TEST_F(ScreenCapturerTest, UseSharedBuffers) { ASSERT_TRUE(frame); ASSERT_TRUE(frame->shared_memory()); EXPECT_EQ(frame->shared_memory()->id(), kTestSharedMemoryId); + + delete frame; } TEST_F(ScreenCapturerTest, UseMagnifier) { @@ -134,14 +133,13 @@ TEST_F(ScreenCapturerTest, UseMagnifier) { options.set_allow_use_magnification_api(true); capturer_.reset(ScreenCapturer::Create(options)); - std::unique_ptr frame; - EXPECT_CALL(callback_, - OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _)) - .WillOnce(SaveUniquePtrArg(&frame)); + DesktopFrame* frame = NULL; + EXPECT_CALL(callback_, OnCaptureCompleted(_)).WillOnce(SaveArg<0>(&frame)); capturer_->Start(&callback_); capturer_->Capture(DesktopRegion()); ASSERT_TRUE(frame); + delete frame; } #endif // defined(WEBRTC_WIN) diff --git a/webrtc/modules/desktop_capture/screen_capturer_x11.cc b/webrtc/modules/desktop_capture/screen_capturer_x11.cc index 57c2e5fc16..5540e6820f 100644 --- a/webrtc/modules/desktop_capture/screen_capturer_x11.cc +++ b/webrtc/modules/desktop_capture/screen_capturer_x11.cc @@ -66,7 +66,7 @@ class ScreenCapturerLinux : public ScreenCapturer, // from HandleXEvent(). In the non-DAMAGE case, this captures the // whole screen, then calculates some invalid rectangles that include any // differences between this and the previous capture. - std::unique_ptr CaptureScreen(); + DesktopFrame* CaptureScreen(); // Called when the screen configuration is changed. void ScreenConfigurationChanged(); @@ -82,23 +82,23 @@ class ScreenCapturerLinux : public ScreenCapturer, DesktopCaptureOptions options_; - Callback* callback_ = nullptr; + Callback* callback_; // X11 graphics context. - GC gc_ = nullptr; - Window root_window_ = BadValue; + GC gc_; + Window root_window_; // XFixes. - bool has_xfixes_ = false; - int xfixes_event_base_ = -1; - int xfixes_error_base_ = -1; + bool has_xfixes_; + int xfixes_event_base_; + int xfixes_error_base_; // XDamage information. - bool use_damage_ = false; - Damage damage_handle_ = 0; - int damage_event_base_ = -1; - int damage_error_base_ = -1; - XserverRegion damage_region_ = 0; + bool use_damage_; + Damage damage_handle_; + int damage_event_base_; + int damage_error_base_; + XserverRegion damage_region_; // Access to the X Server's pixel buffer. XServerPixelBuffer x_server_pixel_buffer_; @@ -120,7 +120,18 @@ class ScreenCapturerLinux : public ScreenCapturer, RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerLinux); }; -ScreenCapturerLinux::ScreenCapturerLinux() { +ScreenCapturerLinux::ScreenCapturerLinux() + : callback_(NULL), + gc_(NULL), + root_window_(BadValue), + has_xfixes_(false), + xfixes_event_base_(-1), + xfixes_error_base_(-1), + use_damage_(false), + damage_handle_(0), + damage_event_base_(-1), + damage_error_base_(-1), + damage_region_(0) { helper_.SetLogGridSize(4); } @@ -238,7 +249,7 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) { // in a good shape. if (!x_server_pixel_buffer_.is_initialized()) { // We failed to initialize pixel buffer. - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -246,26 +257,29 @@ void ScreenCapturerLinux::Capture(const DesktopRegion& region) { // Note that we can't reallocate other buffers at this point, since the caller // may still be reading from them. if (!queue_.current_frame()) { - queue_.ReplaceCurrentFrame( - SharedDesktopFrame::Wrap(std::unique_ptr( - new BasicDesktopFrame(x_server_pixel_buffer_.window_size())))); + std::unique_ptr frame( + new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release())); } // Refresh the Differ helper used by CaptureFrame(), if needed. DesktopFrame* frame = queue_.current_frame(); - if (!use_damage_ && - (!differ_ || (differ_->width() != frame->size().width()) || - (differ_->height() != frame->size().height()) || - (differ_->bytes_per_row() != frame->stride()))) { + if (!use_damage_ && ( + !differ_.get() || + (differ_->width() != frame->size().width()) || + (differ_->height() != frame->size().height()) || + (differ_->bytes_per_row() != frame->stride()))) { differ_.reset(new Differ(frame->size().width(), frame->size().height(), - DesktopFrame::kBytesPerPixel, frame->stride())); + DesktopFrame::kBytesPerPixel, + frame->stride())); } - std::unique_ptr result = CaptureScreen(); + DesktopFrame* result = CaptureScreen(); last_invalid_region_ = result->updated_region(); - result->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) / - rtc::kNumNanosecsPerMillisec); - callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); + result->set_capture_time_ms( + (rtc::TimeNanos() - capture_start_time_nanos) / + rtc::kNumNanosecsPerMillisec); + callback_->OnCaptureCompleted(result); } bool ScreenCapturerLinux::GetScreenList(ScreenList* screens) { @@ -297,8 +311,8 @@ bool ScreenCapturerLinux::HandleXEvent(const XEvent& event) { return false; } -std::unique_ptr ScreenCapturerLinux::CaptureScreen() { - std::unique_ptr frame = queue_.current_frame()->Share(); +DesktopFrame* ScreenCapturerLinux::CaptureScreen() { + DesktopFrame* frame = queue_.current_frame()->Share(); assert(x_server_pixel_buffer_.window_size().equals(frame->size())); // Pass the screen size to the helper, so it can clip the invalid region if it @@ -340,18 +354,18 @@ std::unique_ptr ScreenCapturerLinux::CaptureScreen() { for (DesktopRegion::Iterator it(*updated_region); !it.IsAtEnd(); it.Advance()) { - x_server_pixel_buffer_.CaptureRect(it.rect(), frame.get()); + x_server_pixel_buffer_.CaptureRect(it.rect(), frame); } } else { // Doing full-screen polling, or this is the first capture after a // screen-resolution change. In either case, need a full-screen capture. DesktopRect screen_rect = DesktopRect::MakeSize(frame->size()); - x_server_pixel_buffer_.CaptureRect(screen_rect, frame.get()); + x_server_pixel_buffer_.CaptureRect(screen_rect, frame); if (queue_.previous_frame()) { // Full-screen polling, so calculate the invalid rects here, based on the // changed pixels between current and previous buffers. - RTC_DCHECK(differ_); + RTC_DCHECK(differ_.get() != NULL); RTC_DCHECK(queue_.previous_frame()->data()); differ_->CalcDirtyRegion(queue_.previous_frame()->data(), frame->data(), updated_region); @@ -364,7 +378,7 @@ std::unique_ptr ScreenCapturerLinux::CaptureScreen() { } } - return std::move(frame); + return frame; } void ScreenCapturerLinux::ScreenConfigurationChanged() { @@ -401,7 +415,7 @@ void ScreenCapturerLinux::SynchronizeFrame() { void ScreenCapturerLinux::DeinitXlib() { if (gc_) { XFreeGC(display(), gc_); - gc_ = nullptr; + gc_ = NULL; } x_server_pixel_buffer_.Release(); @@ -424,7 +438,7 @@ void ScreenCapturerLinux::DeinitXlib() { // static ScreenCapturer* ScreenCapturer::Create(const DesktopCaptureOptions& options) { if (!options.x_display()) - return nullptr; + return NULL; std::unique_ptr capturer(new ScreenCapturerLinux()); if (!capturer->Init(options)) diff --git a/webrtc/modules/desktop_capture/shared_desktop_frame.cc b/webrtc/modules/desktop_capture/shared_desktop_frame.cc index e069a54d02..8d10827e29 100644 --- a/webrtc/modules/desktop_capture/shared_desktop_frame.cc +++ b/webrtc/modules/desktop_capture/shared_desktop_frame.cc @@ -17,25 +17,49 @@ namespace webrtc { +class SharedDesktopFrame::Core { + public: + Core(DesktopFrame* frame) : frame_(frame) {} + + DesktopFrame* frame() { return frame_.get(); } + + bool HasOneRef() { return ref_count_.Value() == 1; } + + virtual int32_t AddRef() { + return ++ref_count_; + } + + virtual int32_t Release() { + int32_t ref_count; + ref_count = --ref_count_; + if (ref_count == 0) + delete this; + return ref_count; + } + + private: + virtual ~Core() {} + + Atomic32 ref_count_; + std::unique_ptr frame_; + + RTC_DISALLOW_COPY_AND_ASSIGN(Core); +}; + SharedDesktopFrame::~SharedDesktopFrame() {} // static -std::unique_ptr SharedDesktopFrame::Wrap( - std::unique_ptr desktop_frame) { - return std::unique_ptr( - new SharedDesktopFrame(new Core(desktop_frame.release()))); -} - SharedDesktopFrame* SharedDesktopFrame::Wrap(DesktopFrame* desktop_frame) { - return Wrap(std::unique_ptr(desktop_frame)).release(); + rtc::scoped_refptr core(new Core(desktop_frame)); + return new SharedDesktopFrame(core); } DesktopFrame* SharedDesktopFrame::GetUnderlyingFrame() { - return core_->get(); + return core_->frame(); } -std::unique_ptr SharedDesktopFrame::Share() { - std::unique_ptr result(new SharedDesktopFrame(core_)); +SharedDesktopFrame* SharedDesktopFrame::Share() { + SharedDesktopFrame* result = new SharedDesktopFrame(core_); result->set_dpi(dpi()); result->set_capture_time_ms(capture_time_ms()); *result->mutable_updated_region() = updated_region(); @@ -47,10 +71,11 @@ bool SharedDesktopFrame::IsShared() { } SharedDesktopFrame::SharedDesktopFrame(rtc::scoped_refptr core) - : DesktopFrame((*core)->size(), - (*core)->stride(), - (*core)->data(), - (*core)->shared_memory()), - core_(core) {} + : DesktopFrame(core->frame()->size(), + core->frame()->stride(), + core->frame()->data(), + core->frame()->shared_memory()), + core_(core) { +} } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/shared_desktop_frame.h b/webrtc/modules/desktop_capture/shared_desktop_frame.h index d9a521b064..4f6a2bb7c6 100644 --- a/webrtc/modules/desktop_capture/shared_desktop_frame.h +++ b/webrtc/modules/desktop_capture/shared_desktop_frame.h @@ -12,7 +12,6 @@ #define WEBRTC_MODULES_DESKTOP_CAPTURE_SHARED_DESKTOP_FRAME_H_ #include "webrtc/base/constructormagic.h" -#include "webrtc/base/refcount.h" #include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/desktop_capture/desktop_frame.h" @@ -24,25 +23,20 @@ class SharedDesktopFrame : public DesktopFrame { public: virtual ~SharedDesktopFrame(); - static std::unique_ptr Wrap( - std::unique_ptr desktop_frame); - - // Deprecated. - // TODO(sergeyu): remove this method. static SharedDesktopFrame* Wrap(DesktopFrame* desktop_frame); // Returns the underlying instance of DesktopFrame. DesktopFrame* GetUnderlyingFrame(); // Creates a clone of this object. - std::unique_ptr Share(); + SharedDesktopFrame* Share(); // Checks if the frame is currently shared. If it returns false it's // guaranteed that there are no clones of the object. bool IsShared(); private: - typedef rtc::RefCountedObject> Core; + class Core; SharedDesktopFrame(rtc::scoped_refptr core); diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc index bdada9e398..8a2e77c756 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc @@ -413,27 +413,28 @@ bool ScreenCapturerWinDirectx::DuplicateOutput() { _com_error error = g_container->output1->DuplicateOutput( static_cast(g_container->device), g_container->duplication.GetAddressOf()); - if (error.Error() != S_OK || !g_container->duplication) { + if (error.Error() == S_OK && g_container->duplication) { + memset(&g_container->duplication_desc, 0, sizeof(DXGI_OUTDUPL_DESC)); + g_container->duplication->GetDesc(&g_container->duplication_desc); + if (g_container->duplication_desc.ModeDesc.Format != + DXGI_FORMAT_B8G8R8A8_UNORM) { + g_container->duplication.Reset(); + LOG(LS_ERROR) << "IDXGIDuplicateOutput does not use RGBA (8 bit) " + "format, which is required by downstream components, " + "format is " + << g_container->duplication_desc.ModeDesc.Format; + return false; + } + return true; + } else { + // Make sure we have correct signal and duplicate the output next time. g_container->duplication.Reset(); LOG(LS_WARNING) << "Failed to duplicate output from IDXGIOutput1, error " << error.ErrorMessage() << ", with code " << error.Error(); - return false; } - memset(&g_container->duplication_desc, 0, sizeof(DXGI_OUTDUPL_DESC)); - g_container->duplication->GetDesc(&g_container->duplication_desc); - if (g_container->duplication_desc.ModeDesc.Format != - DXGI_FORMAT_B8G8R8A8_UNORM) { - g_container->duplication.Reset(); - LOG(LS_ERROR) << "IDXGIDuplicateOutput does not use RGBA (8 bit) " - "format, which is required by downstream components, " - "format is " - << g_container->duplication_desc.ModeDesc.Format; - return false; - } - - return true; + return false; } bool ScreenCapturerWinDirectx::ForceDuplicateOutput() { @@ -457,8 +458,8 @@ ScreenCapturerWinDirectx::ScreenCapturerWinDirectx( // Texture instance won't change forever. while (!surfaces_.current_frame()) { - surfaces_.ReplaceCurrentFrame(std::unique_ptr>( - new rtc::scoped_refptr(new Texture()))); + surfaces_.ReplaceCurrentFrame( + new rtc::scoped_refptr(new Texture())); surfaces_.MoveToNextFrame(); } } @@ -592,9 +593,9 @@ std::unique_ptr ScreenCapturerWinDirectx::ProcessFrame( return std::unique_ptr(); } frames_.ReplaceCurrentFrame( - SharedDesktopFrame::Wrap(std::move(new_frame))); + SharedDesktopFrame::Wrap(new_frame.release())); } - result = frames_.current_frame()->Share(); + result.reset(frames_.current_frame()->Share()); std::unique_ptr frame( new DxgiDesktopFrame(*surfaces_.current_frame())); @@ -619,8 +620,9 @@ void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) { RTC_DCHECK(callback_); if (!g_container->duplication && !DuplicateOutput()) { - // Failed to initialize desktop duplication. - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + // Receive a capture request when application is shutting down, or between + // mode change. + callback_->OnCaptureCompleted(nullptr); return; } @@ -654,7 +656,7 @@ void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) { if (ForceDuplicateOutput()) { EmitCurrentFrame(); } else { - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(nullptr); } return; } @@ -675,14 +677,14 @@ void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) { g_container->duplication->ReleaseFrame(); } if (!result) { - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(nullptr); return; } result->set_capture_time_ms( (rtc::TimeNanos() - capture_start_time_nanos) / rtc::kNumNanosecsPerMillisec); - callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); + callback_->OnCaptureCompleted(result.release()); } bool ScreenCapturerWinDirectx::GetScreenList(ScreenList* screens) { @@ -697,7 +699,7 @@ bool ScreenCapturerWinDirectx::SelectScreen(ScreenId id) { void ScreenCapturerWinDirectx::EmitCurrentFrame() { if (!surfaces_.current_frame()->get()->bits()) { // At the very begining, we have not captured any frames. - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(nullptr); return; } @@ -706,12 +708,12 @@ void ScreenCapturerWinDirectx::EmitCurrentFrame() { // queue. If there is not an existing frame (at the very begining), we can // only return a nullptr. if (frames_.current_frame()) { - std::unique_ptr frame = - frames_.current_frame()->Share(); + std::unique_ptr frame( + frames_.current_frame()->Share()); frame->mutable_updated_region()->Clear(); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame.release()); } else { - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(nullptr); } return; } @@ -720,7 +722,7 @@ void ScreenCapturerWinDirectx::EmitCurrentFrame() { // queue. std::unique_ptr frame( new DxgiDesktopFrame(*surfaces_.current_frame())); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame.release()); } } // namespace webrtc diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h index d477991e73..3d33ea6b8e 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h @@ -82,7 +82,9 @@ class ScreenCapturerWinDirectx : public ScreenCapturer { const char* stage); // Processes one frame received from AcquireNextFrame function, returns a - // nullptr if anything wrong. + // nullptr if anything wrong, and Capture function will call + // callback_->OnCaptureCompleted(nullptr), otherwise + // callback_->OnCaptureCompleted(frame) will be called. std::unique_ptr ProcessFrame( const DXGI_OUTDUPL_FRAME_INFO& frame_info, IDXGIResource* resource); diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc index 31fbf4b897..9df2e5fc9b 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.cc @@ -39,8 +39,14 @@ const wchar_t kDwmapiLibraryName[] = L"dwmapi.dll"; } // namespace -ScreenCapturerWinGdi::ScreenCapturerWinGdi( - const DesktopCaptureOptions& options) { +ScreenCapturerWinGdi::ScreenCapturerWinGdi(const DesktopCaptureOptions& options) + : callback_(NULL), + current_screen_id_(kFullDesktopScreenId), + desktop_dc_(NULL), + memory_dc_(NULL), + dwmapi_library_(NULL), + composition_func_(NULL), + set_thread_execution_state_failed_(false) { if (options.disable_effects()) { // Load dwmapi.dll dynamically since it is not available on XP. if (!dwmapi_library_) @@ -91,7 +97,7 @@ void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { PrepareCaptureResources(); if (!CaptureImage()) { - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -124,7 +130,7 @@ void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { helper_.set_size_most_recent(current_frame->size()); // Emit the current frame. - std::unique_ptr frame = queue_.current_frame()->Share(); + DesktopFrame* frame = queue_.current_frame()->Share(); frame->set_dpi(DesktopVector( GetDeviceCaps(desktop_dc_, LOGPIXELSX), GetDeviceCaps(desktop_dc_, LOGPIXELSY))); @@ -133,7 +139,7 @@ void ScreenCapturerWinGdi::Capture(const DesktopRegion& region) { frame->set_capture_time_ms( (rtc::TimeNanos() - capture_start_time_nanos) / rtc::kNumNanosecsPerMillisec); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame); } bool ScreenCapturerWinGdi::GetScreenList(ScreenList* screens) { @@ -164,16 +170,16 @@ void ScreenCapturerWinGdi::PrepareCaptureResources() { // Switch to the desktop receiving user input if different from the current // one. std::unique_ptr input_desktop(Desktop::GetInputDesktop()); - if (input_desktop && !desktop_.IsSame(*input_desktop)) { + if (input_desktop.get() != NULL && !desktop_.IsSame(*input_desktop)) { // Release GDI resources otherwise SetThreadDesktop will fail. if (desktop_dc_) { ReleaseDC(NULL, desktop_dc_); - desktop_dc_ = nullptr; + desktop_dc_ = NULL; } if (memory_dc_) { DeleteDC(memory_dc_); - memory_dc_ = nullptr; + memory_dc_ = NULL; } // If SetThreadDesktop() fails, the thread is still assigned a desktop. @@ -182,7 +188,7 @@ void ScreenCapturerWinGdi::PrepareCaptureResources() { // Re-assert our vote to disable Aero. // See crbug.com/124018 and crbug.com/129906. - if (composition_func_) { + if (composition_func_ != NULL) { (*composition_func_)(DWM_EC_DISABLECOMPOSITION); } } @@ -197,20 +203,20 @@ void ScreenCapturerWinGdi::PrepareCaptureResources() { if (!screen_rect.equals(desktop_dc_rect_)) { if (desktop_dc_) { ReleaseDC(NULL, desktop_dc_); - desktop_dc_ = nullptr; + desktop_dc_ = NULL; } if (memory_dc_) { DeleteDC(memory_dc_); - memory_dc_ = nullptr; + memory_dc_ = NULL; } desktop_dc_rect_ = DesktopRect(); } - if (!desktop_dc_) { - assert(!memory_dc_); + if (desktop_dc_ == NULL) { + assert(memory_dc_ == NULL); // Create GDI device contexts to capture from the desktop into memory. - desktop_dc_ = GetDC(nullptr); + desktop_dc_ = GetDC(NULL); if (!desktop_dc_) abort(); memory_dc_ = CreateCompatibleDC(desktop_dc_); @@ -238,14 +244,14 @@ bool ScreenCapturerWinGdi::CaptureImage() { // may still be reading from them. if (!queue_.current_frame() || !queue_.current_frame()->size().equals(screen_rect.size())) { - assert(desktop_dc_); - assert(memory_dc_); + assert(desktop_dc_ != NULL); + assert(memory_dc_ != NULL); - std::unique_ptr buffer = DesktopFrameWin::Create( - size, shared_memory_factory_.get(), desktop_dc_); + std::unique_ptr buffer(DesktopFrameWin::Create( + size, shared_memory_factory_.get(), desktop_dc_)); if (!buffer) return false; - queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(buffer))); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(buffer.release())); } // Select the target bitmap into the memory dc and copy the rect from desktop @@ -253,9 +259,11 @@ bool ScreenCapturerWinGdi::CaptureImage() { DesktopFrameWin* current = static_cast( queue_.current_frame()->GetUnderlyingFrame()); HGDIOBJ previous_object = SelectObject(memory_dc_, current->bitmap()); - if (previous_object) { - BitBlt(memory_dc_, 0, 0, screen_rect.width(), screen_rect.height(), - desktop_dc_, screen_rect.left(), screen_rect.top(), + if (previous_object != NULL) { + BitBlt(memory_dc_, + 0, 0, screen_rect.width(), screen_rect.height(), + desktop_dc_, + screen_rect.left(), screen_rect.top(), SRCCOPY | CAPTUREBLT); // Select back the previously selected object to that the device contect diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h index 3c855e4a4f..5a50580e69 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_gdi.h @@ -56,9 +56,9 @@ class ScreenCapturerWinGdi : public ScreenCapturer { // Capture the current cursor shape. void CaptureCursor(); - Callback* callback_ = nullptr; + Callback* callback_; std::unique_ptr shared_memory_factory_; - ScreenId current_screen_id_ = kFullDesktopScreenId; + ScreenId current_screen_id_; std::wstring current_device_key_; // A thread-safe list of invalid rectangles, and the size of the most @@ -68,8 +68,8 @@ class ScreenCapturerWinGdi : public ScreenCapturer { ScopedThreadDesktop desktop_; // GDI resources used for screen capture. - HDC desktop_dc_ = NULL; - HDC memory_dc_ = NULL; + HDC desktop_dc_; + HDC memory_dc_; // Queue of the frames buffers. ScreenCaptureFrameQueue queue_; @@ -81,11 +81,11 @@ class ScreenCapturerWinGdi : public ScreenCapturer { // Class to calculate the difference between two screen bitmaps. std::unique_ptr differ_; - HMODULE dwmapi_library_ = NULL; - DwmEnableCompositionFunc composition_func_ = nullptr; + HMODULE dwmapi_library_; + DwmEnableCompositionFunc composition_func_; // Used to suppress duplicate logging of SetThreadExecutionState errors. - bool set_thread_execution_state_failed_ = false; + bool set_thread_execution_state_failed_; RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinGdi); }; diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc index 7fdc8eb43f..6b4308bb7a 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc @@ -39,7 +39,23 @@ Atomic32 ScreenCapturerWinMagnifier::tls_index_(TLS_OUT_OF_INDEXES); ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier( std::unique_ptr fallback_capturer) - : fallback_capturer_(std::move(fallback_capturer)) {} + : fallback_capturer_(std::move(fallback_capturer)), + fallback_capturer_started_(false), + callback_(NULL), + current_screen_id_(kFullDesktopScreenId), + excluded_window_(NULL), + set_thread_execution_state_failed_(false), + desktop_dc_(NULL), + mag_lib_handle_(NULL), + mag_initialize_func_(NULL), + mag_uninitialize_func_(NULL), + set_window_source_func_(NULL), + set_window_filter_list_func_(NULL), + set_image_scaling_callback_func_(NULL), + host_window_(NULL), + magnifier_window_(NULL), + magnifier_initialized_(false), + magnifier_capture_succeeded_(true) {} ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() { // DestroyWindow must be called before MagUninitialize. magnifier_window_ is @@ -146,14 +162,15 @@ void ScreenCapturerWinMagnifier::Capture(const DesktopRegion& region) { helper_.set_size_most_recent(current_frame->size()); // Emit the current frame. - std::unique_ptr frame = queue_.current_frame()->Share(); + DesktopFrame* frame = queue_.current_frame()->Share(); frame->set_dpi(DesktopVector(GetDeviceCaps(desktop_dc_, LOGPIXELSX), GetDeviceCaps(desktop_dc_, LOGPIXELSY))); frame->mutable_updated_region()->Clear(); helper_.TakeInvalidRegion(frame->mutable_updated_region()); - frame->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) / - rtc::kNumNanosecsPerMillisec); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + frame->set_capture_time_ms( + (rtc::TimeNanos() - capture_start_time_nanos) / + rtc::kNumNanosecsPerMillisec); + callback_->OnCaptureCompleted(frame); } bool ScreenCapturerWinMagnifier::GetScreenList(ScreenList* screens) { @@ -187,8 +204,11 @@ bool ScreenCapturerWinMagnifier::CaptureImage(const DesktopRect& rect) { // Set the magnifier control to cover the captured rect. The content of the // magnifier control will be the captured image. - BOOL result = SetWindowPos(magnifier_window_, NULL, rect.left(), rect.top(), - rect.width(), rect.height(), 0); + BOOL result = SetWindowPos(magnifier_window_, + NULL, + rect.left(), rect.top(), + rect.width(), rect.height(), + 0); if (!result) { LOG_F(LS_WARNING) << "Failed to call SetWindowPos: " << GetLastError() << ". Rect = {" << rect.left() << ", " << rect.top() @@ -237,7 +257,7 @@ BOOL ScreenCapturerWinMagnifier::OnMagImageScalingCallback( bool ScreenCapturerWinMagnifier::InitializeMagnifier() { assert(!magnifier_initialized_); - desktop_dc_ = GetDC(nullptr); + desktop_dc_ = GetDC(NULL); mag_lib_handle_ = LoadLibrary(L"Magnification.dll"); if (!mag_lib_handle_) @@ -271,7 +291,7 @@ bool ScreenCapturerWinMagnifier::InitializeMagnifier() { return false; } - HMODULE hInstance = nullptr; + HMODULE hInstance = NULL; result = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast(&DefWindowProc), @@ -289,16 +309,22 @@ bool ScreenCapturerWinMagnifier::InitializeMagnifier() { wcex.cbSize = sizeof(WNDCLASSEX); wcex.lpfnWndProc = &DefWindowProc; wcex.hInstance = hInstance; - wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.lpszClassName = kMagnifierHostClass; // Ignore the error which may happen when the class is already registered. RegisterClassEx(&wcex); // Create the host window. - host_window_ = - CreateWindowEx(WS_EX_LAYERED, kMagnifierHostClass, kHostWindowName, 0, 0, - 0, 0, 0, nullptr, nullptr, hInstance, nullptr); + host_window_ = CreateWindowEx(WS_EX_LAYERED, + kMagnifierHostClass, + kHostWindowName, + 0, + 0, 0, 0, 0, + NULL, + NULL, + hInstance, + NULL); if (!host_window_) { mag_uninitialize_func_(); LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " @@ -307,9 +333,14 @@ bool ScreenCapturerWinMagnifier::InitializeMagnifier() { } // Create the magnifier control. - magnifier_window_ = CreateWindow(kMagnifierWindowClass, kMagnifierWindowName, - WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, - host_window_, nullptr, hInstance, nullptr); + magnifier_window_ = CreateWindow(kMagnifierWindowClass, + kMagnifierWindowName, + WS_CHILD | WS_VISIBLE, + 0, 0, 0, 0, + host_window_, + NULL, + hInstance, + NULL); if (!magnifier_window_) { mag_uninitialize_func_(); LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " @@ -402,7 +433,7 @@ void ScreenCapturerWinMagnifier::CreateCurrentFrameIfNecessary( ? SharedMemoryDesktopFrame::Create(size, shared_memory_factory_.get()) : std::unique_ptr(new BasicDesktopFrame(size)); - queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(frame))); + queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release())); } } diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h index 1a90791c03..623c8a3003 100644 --- a/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h @@ -106,12 +106,12 @@ class ScreenCapturerWinMagnifier : public ScreenCapturer { static Atomic32 tls_index_; std::unique_ptr fallback_capturer_; - bool fallback_capturer_started_ = false; - Callback* callback_ = nullptr; + bool fallback_capturer_started_; + Callback* callback_; std::unique_ptr shared_memory_factory_; - ScreenId current_screen_id_ = kFullDesktopScreenId; + ScreenId current_screen_id_; std::wstring current_device_key_; - HWND excluded_window_ = NULL; + HWND excluded_window_; // A thread-safe list of invalid rectangles, and the size of the most // recently captured screen. @@ -124,31 +124,31 @@ class ScreenCapturerWinMagnifier : public ScreenCapturer { std::unique_ptr differ_; // Used to suppress duplicate logging of SetThreadExecutionState errors. - bool set_thread_execution_state_failed_ = false; + bool set_thread_execution_state_failed_; ScopedThreadDesktop desktop_; // Used for getting the screen dpi. - HDC desktop_dc_ = NULL; + HDC desktop_dc_; - HMODULE mag_lib_handle_ = NULL; - MagInitializeFunc mag_initialize_func_ = nullptr; - MagUninitializeFunc mag_uninitialize_func_ = nullptr; - MagSetWindowSourceFunc set_window_source_func_ = nullptr; - MagSetWindowFilterListFunc set_window_filter_list_func_ = nullptr; - MagSetImageScalingCallbackFunc set_image_scaling_callback_func_ = nullptr; + HMODULE mag_lib_handle_; + MagInitializeFunc mag_initialize_func_; + MagUninitializeFunc mag_uninitialize_func_; + MagSetWindowSourceFunc set_window_source_func_; + MagSetWindowFilterListFunc set_window_filter_list_func_; + MagSetImageScalingCallbackFunc set_image_scaling_callback_func_; // The hidden window hosting the magnifier control. - HWND host_window_ = NULL; + HWND host_window_; // The magnifier control that captures the screen. - HWND magnifier_window_ = NULL; + HWND magnifier_window_; // True if the magnifier control has been successfully initialized. - bool magnifier_initialized_ = false; + bool magnifier_initialized_; // True if the last OnMagImageScalingCallback was called and handled // successfully. Reset at the beginning of each CaptureImage call. - bool magnifier_capture_succeeded_ = true; + bool magnifier_capture_succeeded_; RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier); }; diff --git a/webrtc/modules/desktop_capture/window_capturer_mac.mm b/webrtc/modules/desktop_capture/window_capturer_mac.mm index cbdf14be6f..ac5fdb6bc1 100644 --- a/webrtc/modules/desktop_capture/window_capturer_mac.mm +++ b/webrtc/modules/desktop_capture/window_capturer_mac.mm @@ -32,7 +32,7 @@ namespace { // Returns true if the window exists. bool IsWindowValid(CGWindowID id) { CFArrayRef window_id_array = - CFArrayCreate(nullptr, reinterpret_cast(&id), 1, nullptr); + CFArrayCreate(NULL, reinterpret_cast(&id), 1, NULL); CFArrayRef window_array = CGWindowListCreateDescriptionFromArray(window_id_array); bool valid = window_array && CFArrayGetCount(window_array); @@ -58,10 +58,10 @@ class WindowCapturerMac : public WindowCapturer { void Capture(const DesktopRegion& region) override; private: - Callback* callback_ = nullptr; + Callback* callback_; // The window being captured. - CGWindowID window_id_ = 0; + CGWindowID window_id_; rtc::scoped_refptr full_screen_chrome_window_detector_; @@ -69,12 +69,15 @@ class WindowCapturerMac : public WindowCapturer { RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerMac); }; -WindowCapturerMac::WindowCapturerMac( - rtc::scoped_refptr - full_screen_chrome_window_detector) - : full_screen_chrome_window_detector_(full_screen_chrome_window_detector) {} +WindowCapturerMac::WindowCapturerMac(rtc::scoped_refptr< + FullScreenChromeWindowDetector> full_screen_chrome_window_detector) + : callback_(NULL), + window_id_(0), + full_screen_chrome_window_detector_(full_screen_chrome_window_detector) { +} -WindowCapturerMac::~WindowCapturerMac() {} +WindowCapturerMac::~WindowCapturerMac() { +} bool WindowCapturerMac::GetWindowList(WindowList* windows) { // Only get on screen, non-desktop windows. @@ -139,11 +142,11 @@ bool WindowCapturerMac::BringSelectedWindowToFront() { CGWindowID ids[1]; ids[0] = window_id_; CFArrayRef window_id_array = - CFArrayCreate(nullptr, reinterpret_cast(&ids), 1, nullptr); + CFArrayCreate(NULL, reinterpret_cast(&ids), 1, NULL); CFArrayRef window_array = CGWindowListCreateDescriptionFromArray(window_id_array); - if (!window_array || 0 == CFArrayGetCount(window_array)) { + if (window_array == NULL || 0 == CFArrayGetCount(window_array)) { // Could not find the window. It might have been closed. LOG(LS_INFO) << "Window not found"; CFRelease(window_id_array); @@ -178,7 +181,7 @@ void WindowCapturerMac::Start(Callback* callback) { void WindowCapturerMac::Capture(const DesktopRegion& region) { if (!IsWindowValid(window_id_)) { - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -196,7 +199,7 @@ void WindowCapturerMac::Capture(const DesktopRegion& region) { on_screen_window, kCGWindowImageBoundsIgnoreFraming); if (!window_image) { - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -204,7 +207,7 @@ void WindowCapturerMac::Capture(const DesktopRegion& region) { if (bits_per_pixel != 32) { LOG(LS_ERROR) << "Unsupported window image depth: " << bits_per_pixel; CFRelease(window_image); - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -212,8 +215,8 @@ void WindowCapturerMac::Capture(const DesktopRegion& region) { int height = CGImageGetHeight(window_image); CGDataProviderRef provider = CGImageGetDataProvider(window_image); CFDataRef cf_data = CGDataProviderCopyData(provider); - std::unique_ptr frame( - new BasicDesktopFrame(DesktopSize(width, height))); + DesktopFrame* frame = new BasicDesktopFrame( + DesktopSize(width, height)); int src_stride = CGImageGetBytesPerRow(window_image); const uint8_t* src_data = CFDataGetBytePtr(cf_data); @@ -228,7 +231,7 @@ void WindowCapturerMac::Capture(const DesktopRegion& region) { frame->mutable_updated_region()->SetRect( DesktopRect::MakeSize(frame->size())); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame); if (full_screen_chrome_window_detector_) full_screen_chrome_window_detector_->UpdateWindowListIfNeeded(window_id_); diff --git a/webrtc/modules/desktop_capture/window_capturer_null.cc b/webrtc/modules/desktop_capture/window_capturer_null.cc index 3fe21cf718..5f32c3d71c 100755 --- a/webrtc/modules/desktop_capture/window_capturer_null.cc +++ b/webrtc/modules/desktop_capture/window_capturer_null.cc @@ -34,13 +34,17 @@ class WindowCapturerNull : public WindowCapturer { void Capture(const DesktopRegion& region) override; private: - Callback* callback_ = nullptr; + Callback* callback_; RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerNull); }; -WindowCapturerNull::WindowCapturerNull() {} -WindowCapturerNull::~WindowCapturerNull() {} +WindowCapturerNull::WindowCapturerNull() + : callback_(NULL) { +} + +WindowCapturerNull::~WindowCapturerNull() { +} bool WindowCapturerNull::GetWindowList(WindowList* windows) { // Not implemented yet. @@ -66,7 +70,7 @@ void WindowCapturerNull::Start(Callback* callback) { void WindowCapturerNull::Capture(const DesktopRegion& region) { // Not implemented yet. - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); } } // namespace diff --git a/webrtc/modules/desktop_capture/window_capturer_unittest.cc b/webrtc/modules/desktop_capture/window_capturer_unittest.cc index d2d326e39f..32b8b8f23c 100644 --- a/webrtc/modules/desktop_capture/window_capturer_unittest.cc +++ b/webrtc/modules/desktop_capture/window_capturer_unittest.cc @@ -31,10 +31,7 @@ class WindowCapturerTest : public testing::Test, void TearDown() override {} // DesktopCapturer::Callback interface - void OnCaptureResult(DesktopCapturer::Result result, - std::unique_ptr frame) override { - frame_ = std::move(frame); - } + void OnCaptureCompleted(DesktopFrame* frame) override { frame_.reset(frame); } protected: std::unique_ptr capturer_; diff --git a/webrtc/modules/desktop_capture/window_capturer_win.cc b/webrtc/modules/desktop_capture/window_capturer_win.cc index c7c312a17e..702324372b 100644 --- a/webrtc/modules/desktop_capture/window_capturer_win.cc +++ b/webrtc/modules/desktop_capture/window_capturer_win.cc @@ -95,11 +95,11 @@ class WindowCapturerWin : public WindowCapturer { void Capture(const DesktopRegion& region) override; private: - Callback* callback_ = nullptr; + Callback* callback_; - // HWND and HDC for the currently selected window or nullptr if window is not + // HWND and HDC for the currently selected window or NULL if window is not // selected. - HWND window_ = nullptr; + HWND window_; DesktopSize previous_size_; @@ -112,8 +112,13 @@ class WindowCapturerWin : public WindowCapturer { RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerWin); }; -WindowCapturerWin::WindowCapturerWin() {} -WindowCapturerWin::~WindowCapturerWin() {} +WindowCapturerWin::WindowCapturerWin() + : callback_(NULL), + window_(NULL) { +} + +WindowCapturerWin::~WindowCapturerWin() { +} bool WindowCapturerWin::GetWindowList(WindowList* windows) { WindowList result; @@ -163,13 +168,13 @@ void WindowCapturerWin::Start(Callback* callback) { void WindowCapturerWin::Capture(const DesktopRegion& region) { if (!window_) { LOG(LS_ERROR) << "Window hasn't been selected: " << GetLastError(); - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } // Stop capturing if the window has been closed. if (!IsWindow(window_)) { - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -177,13 +182,12 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { // behavior on mace. Window can be temporarily invisible during the // transition of full screen mode on/off. if (IsIconic(window_) || !IsWindowVisible(window_)) { - std::unique_ptr frame( - new BasicDesktopFrame(DesktopSize(1, 1))); + BasicDesktopFrame* frame = new BasicDesktopFrame(DesktopSize(1, 1)); memset(frame->data(), 0, frame->stride() * frame->size().height()); previous_size_ = frame->size(); window_size_map_[window_] = previous_size_; - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame); return; } @@ -191,22 +195,22 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { DesktopRect cropped_rect; if (!GetCroppedWindowRect(window_, &cropped_rect, &original_rect)) { LOG(LS_WARNING) << "Failed to get window info: " << GetLastError(); - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } HDC window_dc = GetWindowDC(window_); if (!window_dc) { LOG(LS_WARNING) << "Failed to get window DC: " << GetLastError(); - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } std::unique_ptr frame( - DesktopFrameWin::Create(cropped_rect.size(), nullptr, window_dc)); + DesktopFrameWin::Create(cropped_rect.size(), NULL, window_dc)); if (!frame.get()) { ReleaseDC(window_, window_dc); - callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -259,7 +263,7 @@ void WindowCapturerWin::Capture(const DesktopRegion& region) { frame.reset(); } - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame.release()); } } // namespace diff --git a/webrtc/modules/desktop_capture/window_capturer_x11.cc b/webrtc/modules/desktop_capture/window_capturer_x11.cc old mode 100644 new mode 100755 index d88585bfa8..8ead98109a --- a/webrtc/modules/desktop_capture/window_capturer_x11.cc +++ b/webrtc/modules/desktop_capture/window_capturer_x11.cc @@ -36,7 +36,10 @@ namespace { template class XWindowProperty { public: - XWindowProperty(Display* display, Window window, Atom property) { + XWindowProperty(Display* display, Window window, Atom property) + : is_valid_(false), + size_(0), + data_(NULL) { const int kBitsPerByte = 8; Atom actual_type; int actual_format; @@ -46,7 +49,7 @@ class XWindowProperty { &actual_format, &size_, &bytes_after, &data_); if (status != Success) { - data_ = nullptr; + data_ = NULL; return; } if (sizeof(PropertyType) * kBitsPerByte != actual_format) { @@ -75,9 +78,9 @@ class XWindowProperty { } private: - bool is_valid_ = false; - unsigned long size_ = 0; // NOLINT: type required by XGetWindowProperty - unsigned char* data_ = nullptr; + bool is_valid_; + unsigned long size_; // NOLINT: type required by XGetWindowProperty + unsigned char* data_; RTC_DISALLOW_COPY_AND_ASSIGN(XWindowProperty); }; @@ -114,23 +117,26 @@ class WindowCapturerLinux : public WindowCapturer, // Returns window title for the specified X |window|. bool GetWindowTitle(::Window window, std::string* title); - Callback* callback_ = nullptr; + Callback* callback_; rtc::scoped_refptr x_display_; Atom wm_state_atom_; Atom window_type_atom_; Atom normal_window_type_atom_; - bool has_composite_extension_ = false; + bool has_composite_extension_; - ::Window selected_window_ = 0; + ::Window selected_window_; XServerPixelBuffer x_server_pixel_buffer_; RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerLinux); }; WindowCapturerLinux::WindowCapturerLinux(const DesktopCaptureOptions& options) - : x_display_(options.x_display()) { + : callback_(NULL), + x_display_(options.x_display()), + has_composite_extension_(false), + selected_window_(0) { // Create Atoms so we don't need to do it every time they are used. wm_state_atom_ = XInternAtom(display(), "WM_STATE", True); window_type_atom_ = XInternAtom(display(), "_NET_WM_WINDOW_TYPE", True); @@ -274,7 +280,7 @@ void WindowCapturerLinux::Start(Callback* callback) { void WindowCapturerLinux::Capture(const DesktopRegion& region) { if (!x_server_pixel_buffer_.IsWindowValid()) { LOG(LS_INFO) << "The window is no longer valid."; - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } @@ -285,21 +291,21 @@ void WindowCapturerLinux::Capture(const DesktopRegion& region) { // visible on screen and not covered by any other window. This is not // something we want so instead, just bail out. LOG(LS_INFO) << "No Xcomposite extension detected."; - callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); + callback_->OnCaptureCompleted(NULL); return; } - std::unique_ptr frame( - new BasicDesktopFrame(x_server_pixel_buffer_.window_size())); + DesktopFrame* frame = + new BasicDesktopFrame(x_server_pixel_buffer_.window_size()); x_server_pixel_buffer_.Synchronize(); x_server_pixel_buffer_.CaptureRect(DesktopRect::MakeSize(frame->size()), - frame.get()); + frame); frame->mutable_updated_region()->SetRect( DesktopRect::MakeSize(frame->size())); - callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); + callback_->OnCaptureCompleted(frame); } bool WindowCapturerLinux::HandleXEvent(const XEvent& event) { @@ -393,12 +399,12 @@ bool WindowCapturerLinux::GetWindowTitle(::Window window, std::string* title) { int status; bool result = false; XTextProperty window_name; - window_name.value = nullptr; + window_name.value = NULL; if (window) { status = XGetWMName(display(), window, &window_name); if (status && window_name.value && window_name.nitems) { int cnt; - char** list = nullptr; + char **list = NULL; status = Xutf8TextPropertyToTextList(display(), &window_name, &list, &cnt); if (status >= Success && cnt && *list) { @@ -423,7 +429,7 @@ bool WindowCapturerLinux::GetWindowTitle(::Window window, std::string* title) { // static WindowCapturer* WindowCapturer::Create(const DesktopCaptureOptions& options) { if (!options.x_display()) - return nullptr; + return NULL; return new WindowCapturerLinux(options); }