desktop_frame_provider.mm (1992B)
1 /* 2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "modules/desktop_capture/mac/desktop_frame_provider.h" 12 13 #include <utility> 14 15 #include "modules/desktop_capture/mac/desktop_frame_cgimage.h" 16 #include "modules/desktop_capture/mac/desktop_frame_iosurface.h" 17 18 namespace webrtc { 19 20 DesktopFrameProvider::DesktopFrameProvider(bool allow_iosurface) 21 : allow_iosurface_(allow_iosurface) { 22 thread_checker_.Detach(); 23 } 24 25 DesktopFrameProvider::~DesktopFrameProvider() { 26 RTC_DCHECK(thread_checker_.IsCurrent()); 27 28 Release(); 29 } 30 31 std::unique_ptr<DesktopFrame> DesktopFrameProvider::TakeLatestFrameForDisplay( 32 CGDirectDisplayID display_id) { 33 RTC_DCHECK(thread_checker_.IsCurrent()); 34 35 if (!allow_iosurface_ || !io_surfaces_[display_id]) { 36 // Regenerate a snapshot. If iosurface is on it will be empty until the 37 // stream handler is called. 38 return DesktopFrameCGImage::CreateForDisplay(display_id); 39 } 40 41 return io_surfaces_[display_id]->Share(); 42 } 43 44 void DesktopFrameProvider::InvalidateIOSurface( 45 CGDirectDisplayID display_id, 46 webrtc::ScopedCFTypeRef<IOSurfaceRef> io_surface) { 47 RTC_DCHECK(thread_checker_.IsCurrent()); 48 49 if (!allow_iosurface_) { 50 return; 51 } 52 53 std::unique_ptr<DesktopFrameIOSurface> desktop_frame_iosurface = 54 DesktopFrameIOSurface::Wrap(io_surface); 55 56 io_surfaces_[display_id] = desktop_frame_iosurface ? 57 SharedDesktopFrame::Wrap(std::move(desktop_frame_iosurface)) : 58 nullptr; 59 } 60 61 void DesktopFrameProvider::Release() { 62 RTC_DCHECK(thread_checker_.IsCurrent()); 63 64 if (!allow_iosurface_) { 65 return; 66 } 67 68 io_surfaces_.clear(); 69 } 70 71 } // namespace webrtc