【WebRTC】【macOS】
macOS截屏分享实现有两种情况,
1.抓“指定运行中的App 窗口”内容。
2.抓当前屏幕内容(Screen或者Display)
本文讨论第1种。
【说明】:
用WebRTC实现macOS的截屏分享-WindowCapturerMac。
最近需要在macOS实现截屏分享的功能,但参考资料少。
写一贴,分享。
关于截屏分享的实现,
Windows版的实现很多;
还有专门的的书讨论iOS版和Android版的截屏分享,
见许建林先生所著《WebRTC Native开发实践》第7章。
【实现】:
macOS截屏分享实现之一,抓“指定运行中的App 窗口”内容。
使用 WindowCapturerMac 类,
其代码,在 window_capturer_mac.mm中定义和实现。
【注意】:
1.以下代码仅供参考。
获取当前运行的App的窗口列表,需要添加界面让用户选择。
2.在macOS 12.2.1和Xcode 13.2.1运行,可以获得截取的App Window数据成功。
GMDesktopCapturerTest *test = new GMDesktopCapturerTest();
DesktopCaptureOptions options = webrtc::DesktopCaptureOptions::CreateDefault();
std::unique_ptr<WindowCapturerMac> winCapture(new WindowCapturerMac(options.full_screen_window_detector(),
options.configuration_monitor()));
webrtc::DesktopCapturer::SourceList appWindowSourceList;
winCapture->GetSourceList(&appWindowSourceList);
// winCapture->SelectSource(sources->front().id);
NSLog(@"appWindowSourceList[0].id : %ld", appWindowSourceList[0].id);
winCapture->SelectSource(appWindowSourceList[0].id);
winCapture->FocusOnSelectedSource();
winCapture->Start(test);
while (1) {
winCapture->CaptureFrame();
}