Move ScreenCapturerMac to mac sub directory

1- git mv screen_capturer_mac.mm  mac/screen_capturer_mac.mm
2- extract class ScreenCapturerMac declaritions to its own header
3- extract static CreateRawScreenCapturer to screen_capturer_darwin.mm
   (Using 'darwin' instead of 'mac' allows to make happy the command
   git log --follow mac/screen_capturer_mac.mm)
4- git cl format

Bug: webrtc:8652
Change-Id: Ibb13bd5dec61aa9b92c9f5f30fedd0508a727dd9
Reviewed-on: https://webrtc-review.googlesource.com/64680
Commit-Queue: Julien Isorce <julien.isorce@chromium.org>
Reviewed-by: Zijie He <zijiehe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#22620}
This commit is contained in:
Julien Isorce 2018-03-27 08:39:11 +01:00 committed by Commit Bot
parent 16ccef7f11
commit 370228c312
4 changed files with 252 additions and 233 deletions

View File

@ -175,8 +175,10 @@ if (is_mac) {
visibility = [ ":desktop_capture" ]
sources = [
"mac/desktop_configuration.mm",
"mac/screen_capturer_mac.h",
"mac/screen_capturer_mac.mm",
"mouse_cursor_monitor_mac.mm",
"screen_capturer_mac.mm",
"screen_capturer_darwin.mm",
"window_capturer_mac.mm",
"window_finder_mac.h",
"window_finder_mac.mm",

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2018 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.
*/
#ifndef MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_
#define MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_
#include <CoreGraphics/CoreGraphics.h>
#include <memory>
#include "modules/desktop_capture/desktop_capture_options.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "modules/desktop_capture/desktop_frame.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "modules/desktop_capture/desktop_region.h"
#include "modules/desktop_capture/mac/desktop_configuration.h"
#include "modules/desktop_capture/mac/desktop_configuration_monitor.h"
#include "modules/desktop_capture/mac/scoped_pixel_buffer_object.h"
#include "modules/desktop_capture/screen_capture_frame_queue.h"
#include "modules/desktop_capture/screen_capturer_helper.h"
#include "modules/desktop_capture/shared_desktop_frame.h"
namespace webrtc {
class DisplayStreamManager;
// A class to perform video frame capturing for mac.
class ScreenCapturerMac final : public DesktopCapturer {
public:
explicit ScreenCapturerMac(
rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor,
bool detect_updated_region);
~ScreenCapturerMac() override;
// TODO(julien.isorce): Remove Init() or make it private.
bool Init();
// DesktopCapturer interface.
void Start(Callback* callback) override;
void CaptureFrame() override;
void SetExcludedWindow(WindowId window) override;
bool GetSourceList(SourceList* screens) override;
bool SelectSource(SourceId id) override;
private:
// Returns false if the selected screen is no longer valid.
bool CgBlit(const DesktopFrame& frame, const DesktopRegion& region);
// Called when the screen configuration is changed.
void ScreenConfigurationChanged();
bool RegisterRefreshAndMoveHandlers();
void UnregisterRefreshAndMoveHandlers();
void ScreenRefresh(CGRectCount count,
const CGRect* rect_array,
DesktopVector display_origin);
void ReleaseBuffers();
std::unique_ptr<DesktopFrame> CreateFrame();
const bool detect_updated_region_;
Callback* callback_ = nullptr;
ScopedPixelBufferObject pixel_buffer_object_;
// Queue of the frames buffers.
ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Current display configuration.
MacDesktopConfiguration desktop_config_;
// 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;
// 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;
// A thread-safe list of invalid rectangles, and the size of the most
// recently captured screen.
ScreenCapturerHelper helper_;
// Contains an invalid region from the previous capture.
DesktopRegion last_invalid_region_;
// Monitoring display reconfiguration.
rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor_;
CGWindowID excluded_window_ = 0;
// A self-owned object that will destroy itself after ScreenCapturerMac and
// all display streams have been destroyed..
DisplayStreamManager* display_stream_manager_;
RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerMac);
};
} // namespace webrtc
#endif // MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_

View File

@ -10,26 +10,15 @@
#include <stddef.h>
#include <memory>
#include <set>
#include <utility>
#include <ApplicationServices/ApplicationServices.h>
#include <Cocoa/Cocoa.h>
#include <CoreGraphics/CoreGraphics.h>
#include <dlfcn.h>
#include "modules/desktop_capture/desktop_capture_options.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "modules/desktop_capture/desktop_frame.h"
#include "modules/desktop_capture/desktop_geometry.h"
#include "modules/desktop_capture/desktop_region.h"
#include "modules/desktop_capture/mac/desktop_configuration.h"
#include "modules/desktop_capture/mac/desktop_configuration_monitor.h"
#include "modules/desktop_capture/mac/scoped_pixel_buffer_object.h"
#include "modules/desktop_capture/screen_capture_frame_queue.h"
#include "modules/desktop_capture/screen_capturer_helper.h"
#include "modules/desktop_capture/shared_desktop_frame.h"
#include "modules/desktop_capture/mac/screen_capturer_mac.h"
#include "rtc_base/checks.h"
#include "rtc_base/constructormagic.h"
#include "rtc_base/logging.h"
@ -39,8 +28,6 @@
namespace webrtc {
namespace {
// CGDisplayStreamRefs need to be destroyed asynchronously after receiving a
// kCGDisplayStreamFrameStatusStopped callback from CoreGraphics. This may
// happen after the ScreenCapturerMac has been destroyed. DisplayStreamManager
@ -56,12 +43,10 @@ class DisplayStreamManager {
CFRelease(it->second.stream);
display_stream_wrappers_.erase(it);
if (ready_for_self_destruction_ && display_stream_wrappers_.empty())
delete this;
if (ready_for_self_destruction_ && display_stream_wrappers_.empty()) delete this;
}
void SaveStream(int unique_id,
CGDisplayStreamRef stream) {
void SaveStream(int unique_id, CGDisplayStreamRef stream) {
RTC_CHECK(unique_id <= unique_id_generator_);
DisplayStreamWrapper wrapper;
wrapper.stream = stream;
@ -73,10 +58,8 @@ class DisplayStreamManager {
DisplayStreamWrapper& wrapper = pair.second;
if (wrapper.active) {
wrapper.active = false;
CFRunLoopSourceRef source =
CGDisplayStreamGetRunLoopSource(wrapper.stream);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source,
kCFRunLoopCommonModes);
CFRunLoopSourceRef source = CGDisplayStreamGetRunLoopSource(wrapper.stream);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
CGDisplayStreamStop(wrapper.stream);
}
}
@ -85,8 +68,7 @@ class DisplayStreamManager {
void PrepareForSelfDestruction() {
ready_for_self_destruction_ = true;
if (display_stream_wrappers_.empty())
delete this;
if (display_stream_wrappers_.empty()) delete this;
}
// Once the DisplayStreamManager is ready for destruction, the
@ -108,17 +90,18 @@ class DisplayStreamManager {
bool ready_for_self_destruction_ = false;
};
namespace {
// Standard Mac displays have 72dpi, but we report 96dpi for
// consistency with Windows and Linux.
const int kStandardDPI = 96;
// Scales all coordinates of a rect by a specified factor.
DesktopRect ScaleAndRoundCGRect(const CGRect& rect, float scale) {
return DesktopRect::MakeLTRB(
static_cast<int>(floor(rect.origin.x * scale)),
static_cast<int>(floor(rect.origin.y * scale)),
static_cast<int>(ceil((rect.origin.x + rect.size.width) * scale)),
static_cast<int>(ceil((rect.origin.y + rect.size.height) * scale)));
return DesktopRect::MakeLTRB(static_cast<int>(floor(rect.origin.x * scale)),
static_cast<int>(floor(rect.origin.y * scale)),
static_cast<int>(ceil((rect.origin.x + rect.size.width) * scale)),
static_cast<int>(ceil((rect.origin.y + rect.size.height) * scale)));
}
// Copy pixels in the |rect| from |src_place| to |dest_plane|. |rect| should be
@ -139,7 +122,7 @@ void CopyRect(const uint8_t* src_plane,
// Copy pixels in the rectangle line by line.
const int bytes_per_line = bytes_per_pixel * rect.width();
const int height = rect.height();
for (int i = 0 ; i < height; ++i) {
for (int i = 0; i < height; ++i) {
memcpy(dest_plane, src_plane, bytes_per_line);
src_plane += src_plane_stride;
dest_plane += dest_plane_stride;
@ -150,24 +133,22 @@ void CopyRect(const uint8_t* src_plane,
// |window_to_exclude|, or NULL if the window is not found or it fails. The
// caller should release the returned CFArrayRef.
CFArrayRef CreateWindowListWithExclusion(CGWindowID window_to_exclude) {
if (!window_to_exclude)
return nullptr;
if (!window_to_exclude) return nullptr;
CFArrayRef all_windows = CGWindowListCopyWindowInfo(
kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
if (!all_windows)
return nullptr;
CFArrayRef all_windows =
CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
if (!all_windows) return nullptr;
CFMutableArrayRef returned_array =
CFArrayCreateMutable(nullptr, CFArrayGetCount(all_windows), nullptr);
bool found = false;
for (CFIndex i = 0; i < CFArrayGetCount(all_windows); ++i) {
CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
CFArrayGetValueAtIndex(all_windows, i));
CFDictionaryRef window =
reinterpret_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(all_windows, i));
CFNumberRef id_ref = reinterpret_cast<CFNumberRef>(
CFDictionaryGetValue(window, kCGWindowNumber));
CFNumberRef id_ref =
reinterpret_cast<CFNumberRef>(CFDictionaryGetValue(window, kCGWindowNumber));
CGWindowID id;
CFNumberGetValue(id_ref, kCFNumberIntType, &id);
@ -175,7 +156,7 @@ CFArrayRef CreateWindowListWithExclusion(CGWindowID window_to_exclude) {
found = true;
continue;
}
CFArrayAppendValue(returned_array, reinterpret_cast<void *>(id));
CFArrayAppendValue(returned_array, reinterpret_cast<void*>(id));
}
CFRelease(all_windows);
@ -188,8 +169,7 @@ CFArrayRef CreateWindowListWithExclusion(CGWindowID window_to_exclude) {
// Returns the bounds of |window| in physical pixels, enlarged by a small amount
// on four edges to take account of the border/shadow effects.
DesktopRect GetExcludedWindowPixelBounds(CGWindowID window,
float dip_to_pixel_scale) {
DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, float dip_to_pixel_scale) {
// The amount of pixels to add to the actual window bounds to take into
// account of the border/shadow effects.
static const int kBorderEffectSize = 20;
@ -199,14 +179,13 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window,
CFArrayRef window_id_array =
CFArrayCreate(nullptr, reinterpret_cast<const void**>(&ids), 1, nullptr);
CFArrayRef window_array =
CGWindowListCreateDescriptionFromArray(window_id_array);
CFArrayRef window_array = CGWindowListCreateDescriptionFromArray(window_id_array);
if (CFArrayGetCount(window_array) > 0) {
CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(
CFArrayGetValueAtIndex(window_array, 0));
CFDictionaryRef bounds_ref = reinterpret_cast<CFDictionaryRef>(
CFDictionaryGetValue(window, kCGWindowBounds));
CFDictionaryRef window =
reinterpret_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(window_array, 0));
CFDictionaryRef bounds_ref =
reinterpret_cast<CFDictionaryRef>(CFDictionaryGetValue(window, kCGWindowBounds));
CGRectMakeWithDictionaryRepresentation(bounds_ref, &rect);
}
@ -235,95 +214,18 @@ CGImageRef CreateExcludedWindowRegionImage(const DesktopRect& pixel_bounds,
window_bounds.size.width = pixel_bounds.width();
window_bounds.size.height = pixel_bounds.height();
return CGWindowListCreateImageFromArray(
window_bounds, window_list, kCGWindowImageDefault);
return CGWindowListCreateImageFromArray(window_bounds, window_list, kCGWindowImageDefault);
}
// A class to perform video frame capturing for mac.
class ScreenCapturerMac : public DesktopCapturer {
public:
explicit ScreenCapturerMac(
rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor,
bool detect_updated_region);
~ScreenCapturerMac() override;
bool Init();
// DesktopCapturer interface.
void Start(Callback* callback) override;
void CaptureFrame() override;
void SetExcludedWindow(WindowId window) override;
bool GetSourceList(SourceList* screens) override;
bool SelectSource(SourceId id) override;
private:
// Returns false if the selected screen is no longer valid.
bool CgBlit(const DesktopFrame& frame, const DesktopRegion& region);
// Called when the screen configuration is changed.
void ScreenConfigurationChanged();
bool RegisterRefreshAndMoveHandlers();
void UnregisterRefreshAndMoveHandlers();
void ScreenRefresh(CGRectCount count,
const CGRect *rect_array,
DesktopVector display_origin);
void ReleaseBuffers();
std::unique_ptr<DesktopFrame> CreateFrame();
const bool detect_updated_region_;
Callback* callback_ = nullptr;
ScopedPixelBufferObject pixel_buffer_object_;
// Queue of the frames buffers.
ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
// Current display configuration.
MacDesktopConfiguration desktop_config_;
// 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;
// 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;
// A thread-safe list of invalid rectangles, and the size of the most
// recently captured screen.
ScreenCapturerHelper helper_;
// Contains an invalid region from the previous capture.
DesktopRegion last_invalid_region_;
// Monitoring display reconfiguration.
rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor_;
CGWindowID excluded_window_ = 0;
// A self-owned object that will destroy itself after ScreenCapturerMac and
// all display streams have been destroyed..
DisplayStreamManager* display_stream_manager_;
RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerMac);
};
// DesktopFrame wrapper that flips wrapped frame upside down by inverting
// stride.
class InvertedDesktopFrame : public DesktopFrame {
public:
InvertedDesktopFrame(std::unique_ptr<DesktopFrame> frame)
: DesktopFrame(
frame->size(),
-frame->stride(),
frame->data() + (frame->size().height() - 1) * frame->stride(),
frame->shared_memory()) {
: DesktopFrame(frame->size(),
-frame->stride(),
frame->data() + (frame->size().height() - 1) * frame->stride(),
frame->shared_memory()) {
original_frame_ = std::move(frame);
MoveFrameInfoFrom(original_frame_.get());
}
@ -335,6 +237,8 @@ class InvertedDesktopFrame : public DesktopFrame {
RTC_DISALLOW_COPY_AND_ASSIGN(InvertedDesktopFrame);
};
} // namespace
ScreenCapturerMac::ScreenCapturerMac(
rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor,
bool detect_updated_region)
@ -381,8 +285,7 @@ void ScreenCapturerMac::CaptureFrame() {
RTC_DCHECK(!queue_.current_frame() || !queue_.current_frame()->IsShared());
desktop_config_monitor_->Lock();
MacDesktopConfiguration new_config =
desktop_config_monitor_->desktop_configuration();
MacDesktopConfiguration new_config = desktop_config_monitor_->desktop_configuration();
if (!desktop_config_.Equals(new_config)) {
desktop_config_ = new_config;
// If the display configuraiton has changed then refresh capturer data
@ -399,8 +302,7 @@ void ScreenCapturerMac::CaptureFrame() {
// If the current buffer is from an older generation then allocate a new one.
// 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(CreateFrame()));
if (!queue_.current_frame()) queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(CreateFrame()));
DesktopFrame* current_frame = queue_.current_frame();
@ -413,16 +315,15 @@ void ScreenCapturerMac::CaptureFrame() {
if (detect_updated_region_) {
*new_frame->mutable_updated_region() = region;
} else {
new_frame->mutable_updated_region()->AddRect(
DesktopRect::MakeSize(new_frame->size()));
new_frame->mutable_updated_region()->AddRect(DesktopRect::MakeSize(new_frame->size()));
}
if (current_display_) {
const MacDisplayConfiguration* config =
desktop_config_.FindDisplayConfigurationById(current_display_);
if (config) {
new_frame->set_top_left(config->bounds.top_left().subtract(
desktop_config_.bounds.top_left()));
new_frame->set_top_left(
config->bounds.top_left().subtract(desktop_config_.bounds.top_left()));
}
}
@ -445,7 +346,8 @@ bool ScreenCapturerMac::GetSourceList(SourceList* screens) {
assert(screens->size() == 0);
for (MacDisplayConfigurations::iterator it = desktop_config_.displays.begin();
it != desktop_config_.displays.end(); ++it) {
it != desktop_config_.displays.end();
++it) {
screens->push_back({it->id, std::string()});
}
return true;
@ -456,10 +358,8 @@ bool ScreenCapturerMac::SelectSource(SourceId id) {
current_display_ = 0;
} else {
const MacDisplayConfiguration* config =
desktop_config_.FindDisplayConfigurationById(
static_cast<CGDirectDisplayID>(id));
if (!config)
return false;
desktop_config_.FindDisplayConfigurationById(static_cast<CGDirectDisplayID>(id));
if (!config) return false;
current_display_ = config->id;
}
@ -472,8 +372,7 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
// 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(),
frame.stride() * frame.size().height());
memcpy(frame.data(), queue_.previous_frame()->data(), frame.stride() * frame.size().height());
}
MacDisplayConfigurations displays_to_capture;
@ -502,20 +401,17 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
// Capturing mixed-DPI on one surface is hard, so we only return displays
// that match the "primary" display's DPI. The primary display is always
// the first in the list.
if (i > 0 && display_config.dip_to_pixel_scale !=
displays_to_capture[0].dip_to_pixel_scale) {
if (i > 0 && display_config.dip_to_pixel_scale != displays_to_capture[0].dip_to_pixel_scale) {
continue;
}
// Determine the display's position relative to the desktop, in pixels.
DesktopRect display_bounds = display_config.pixel_bounds;
display_bounds.Translate(-screen_pixel_bounds_.left(),
-screen_pixel_bounds_.top());
display_bounds.Translate(-screen_pixel_bounds_.left(), -screen_pixel_bounds_.top());
// Determine which parts of the blit region, if any, lay within the monitor.
DesktopRegion copy_region = region;
copy_region.IntersectWith(display_bounds);
if (copy_region.is_empty())
continue;
if (copy_region.is_empty()) continue;
// Translate the region to be copied into display-relative coordinates.
copy_region.Translate(-display_bounds.left(), -display_bounds.top());
@ -524,24 +420,22 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
CGImageRef excluded_image = nullptr;
if (excluded_window_ && window_list) {
// Get the region of the excluded window relative the primary display.
excluded_window_bounds = GetExcludedWindowPixelBounds(
excluded_window_, display_config.dip_to_pixel_scale);
excluded_window_bounds =
GetExcludedWindowPixelBounds(excluded_window_, display_config.dip_to_pixel_scale);
excluded_window_bounds.IntersectWith(display_config.pixel_bounds);
// Create the image under the excluded window first, because it's faster
// than captuing the whole display.
if (!excluded_window_bounds.is_empty()) {
excluded_image = CreateExcludedWindowRegionImage(
excluded_window_bounds, display_config.dip_to_pixel_scale,
window_list);
excluded_window_bounds, display_config.dip_to_pixel_scale, window_list);
}
}
// Create an image containing a snapshot of the display.
CGImageRef image = CGDisplayCreateImage(display_config.id);
if (!image) {
if (excluded_image)
CFRelease(excluded_image);
if (excluded_image) CFRelease(excluded_image);
continue;
}
@ -551,8 +445,7 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
RTC_LOG(LS_ERROR) << "CGDisplayCreateImage() returned imaged with " << bits_per_pixel
<< " bits per pixel. Only 32-bit depth is supported.";
CFRelease(image);
if (excluded_image)
CFRelease(excluded_image);
if (excluded_image) CFRelease(excluded_image);
return false;
}
@ -566,14 +459,17 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
// |image| size may be different from display_bounds in case the screen was
// resized recently.
copy_region.IntersectWith(
DesktopRect::MakeWH(CGImageGetWidth(image), CGImageGetHeight(image)));
copy_region.IntersectWith(DesktopRect::MakeWH(CGImageGetWidth(image), CGImageGetHeight(image)));
// Copy the dirty region from the display buffer into our desktop buffer.
uint8_t* out_ptr = frame.GetFrameDataAtPos(display_bounds.top_left());
for (DesktopRegion::Iterator i(copy_region); !i.IsAtEnd(); i.Advance()) {
CopyRect(display_base_address, src_bytes_per_row, out_ptr, frame.stride(),
DesktopFrame::kBytesPerPixel, i.rect());
CopyRect(display_base_address,
src_bytes_per_row,
out_ptr,
frame.stride(),
DesktopFrame::kBytesPerPixel,
i.rect());
}
CFRelease(data);
@ -592,25 +488,24 @@ bool ScreenCapturerMac::CgBlit(const DesktopFrame& frame, const DesktopRegion& r
window_bounds_relative_to_desktop.Translate(-screen_pixel_bounds_.left(),
-screen_pixel_bounds_.top());
DesktopRect rect_to_copy =
DesktopRect::MakeSize(excluded_window_bounds.size());
rect_to_copy.IntersectWith(DesktopRect::MakeWH(
CGImageGetWidth(excluded_image), CGImageGetHeight(excluded_image)));
DesktopRect rect_to_copy = DesktopRect::MakeSize(excluded_window_bounds.size());
rect_to_copy.IntersectWith(
DesktopRect::MakeWH(CGImageGetWidth(excluded_image), CGImageGetHeight(excluded_image)));
if (CGImageGetBitsPerPixel(excluded_image) / 8 ==
DesktopFrame::kBytesPerPixel) {
CopyRect(display_base_address, src_bytes_per_row,
frame.GetFrameDataAtPos(
window_bounds_relative_to_desktop.top_left()),
frame.stride(), DesktopFrame::kBytesPerPixel, rect_to_copy);
if (CGImageGetBitsPerPixel(excluded_image) / 8 == DesktopFrame::kBytesPerPixel) {
CopyRect(display_base_address,
src_bytes_per_row,
frame.GetFrameDataAtPos(window_bounds_relative_to_desktop.top_left()),
frame.stride(),
DesktopFrame::kBytesPerPixel,
rect_to_copy);
}
CFRelease(excluded_image_data);
CFRelease(excluded_image);
}
}
if (window_list)
CFRelease(window_list);
if (window_list) CFRelease(window_list);
return true;
}
@ -643,38 +538,36 @@ bool ScreenCapturerMac::RegisterRefreshAndMoveHandlers() {
for (const auto& config : desktop_config_.displays) {
size_t pixel_width = config.pixel_bounds.width();
size_t pixel_height = config.pixel_bounds.height();
if (pixel_width == 0 || pixel_height == 0)
continue;
if (pixel_width == 0 || pixel_height == 0) continue;
// Using a local variable forces the block to capture the raw pointer.
DisplayStreamManager* manager = display_stream_manager_;
int unique_id = manager->GetUniqueId();
CGDirectDisplayID display_id = config.id;
DesktopVector display_origin = config.pixel_bounds.top_left();
CGDisplayStreamFrameAvailableHandler handler =
^(CGDisplayStreamFrameStatus status, uint64_t display_time,
IOSurfaceRef frame_surface, CGDisplayStreamUpdateRef updateRef) {
if (status == kCGDisplayStreamFrameStatusStopped) {
manager->DestroyStream(unique_id);
return;
}
CGDisplayStreamFrameAvailableHandler handler = ^(CGDisplayStreamFrameStatus status,
uint64_t display_time,
IOSurfaceRef frame_surface,
CGDisplayStreamUpdateRef updateRef) {
if (status == kCGDisplayStreamFrameStatusStopped) {
manager->DestroyStream(unique_id);
return;
}
if (manager->ShouldIgnoreUpdates())
return;
if (manager->ShouldIgnoreUpdates()) return;
// Only pay attention to frame updates.
if (status != kCGDisplayStreamFrameStatusFrameComplete)
return;
// Only pay attention to frame updates.
if (status != kCGDisplayStreamFrameStatusFrameComplete) return;
size_t count = 0;
const CGRect* rects = CGDisplayStreamUpdateGetRects(
updateRef, kCGDisplayStreamUpdateDirtyRects, &count);
if (count != 0) {
// According to CGDisplayStream.h, it's safe to call
// CGDisplayStreamStop() from within the callback.
ScreenRefresh(count, rects, display_origin);
}
};
size_t count = 0;
const CGRect* rects =
CGDisplayStreamUpdateGetRects(updateRef, kCGDisplayStreamUpdateDirtyRects, &count);
if (count != 0) {
// According to CGDisplayStream.h, it's safe to call
// CGDisplayStreamStop() from within the callback.
ScreenRefresh(count, rects, display_origin);
}
};
rtc::ScopedCFTypeRef<CFDictionaryRef> properties_dict(
CFDictionaryCreate(kCFAllocatorDefault,
@ -689,11 +582,9 @@ bool ScreenCapturerMac::RegisterRefreshAndMoveHandlers() {
if (display_stream) {
CGError error = CGDisplayStreamStart(display_stream);
if (error != kCGErrorSuccess)
return false;
if (error != kCGErrorSuccess) return false;
CFRunLoopSourceRef source =
CGDisplayStreamGetRunLoopSource(display_stream);
CFRunLoopSourceRef source = CGDisplayStreamGetRunLoopSource(display_stream);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
display_stream_manager_->SaveStream(unique_id, display_stream);
}
@ -709,23 +600,22 @@ void ScreenCapturerMac::UnregisterRefreshAndMoveHandlers() {
void ScreenCapturerMac::ScreenRefresh(CGRectCount count,
const CGRect* rect_array,
DesktopVector display_origin) {
if (screen_pixel_bounds_.is_empty())
ScreenConfigurationChanged();
if (screen_pixel_bounds_.is_empty()) ScreenConfigurationChanged();
// The refresh rects are in display coordinates. We want to translate to
// framebuffer coordinates. If a specific display is being captured, then no
// change is necessary. If all displays are being captured, then we want to
// translate by the origin of the display.
DesktopVector translate_vector;
if (!current_display_)
translate_vector = display_origin;
if (!current_display_) translate_vector = display_origin;
DesktopRegion region;
for (CGRectCount i = 0; i < count; ++i) {
// All rects are already in physical pixel coordinates.
DesktopRect rect = DesktopRect::MakeXYWH(
rect_array[i].origin.x, rect_array[i].origin.y,
rect_array[i].size.width, rect_array[i].size.height);
DesktopRect rect = DesktopRect::MakeXYWH(rect_array[i].origin.x,
rect_array[i].origin.y,
rect_array[i].size.width,
rect_array[i].size.height);
rect.Translate(translate_vector);
@ -736,28 +626,10 @@ void ScreenCapturerMac::ScreenRefresh(CGRectCount count,
}
std::unique_ptr<DesktopFrame> ScreenCapturerMac::CreateFrame() {
std::unique_ptr<DesktopFrame> frame(
new BasicDesktopFrame(screen_pixel_bounds_.size()));
frame->set_dpi(DesktopVector(kStandardDPI * dip_to_pixel_scale_,
kStandardDPI * dip_to_pixel_scale_));
std::unique_ptr<DesktopFrame> frame(new BasicDesktopFrame(screen_pixel_bounds_.size()));
frame->set_dpi(
DesktopVector(kStandardDPI * dip_to_pixel_scale_, kStandardDPI * dip_to_pixel_scale_));
return frame;
}
} // namespace
// static
std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawScreenCapturer(
const DesktopCaptureOptions& options) {
if (!options.configuration_monitor())
return nullptr;
std::unique_ptr<ScreenCapturerMac> capturer(new ScreenCapturerMac(
options.configuration_monitor(), options.detect_updated_region()));
if (!capturer.get()->Init()) {
return nullptr;
}
return capturer;
}
} // namespace webrtc

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2018 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 <memory>
#include "modules/desktop_capture/mac/screen_capturer_mac.h"
namespace webrtc {
// static
std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawScreenCapturer(
const DesktopCaptureOptions& options) {
if (!options.configuration_monitor()) {
return nullptr;
}
std::unique_ptr<ScreenCapturerMac> capturer(
new ScreenCapturerMac(options.configuration_monitor(), options.detect_updated_region()));
if (!capturer.get()->Init()) {
return nullptr;
}
return capturer;
}
} // namespace webrtc