1*d9f75844SAndroid Build Coastguard Worker/* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker#include "test/mac_capturer.h" 12*d9f75844SAndroid Build Coastguard Worker 13*d9f75844SAndroid Build Coastguard Worker#import "sdk/objc/base/RTCVideoCapturer.h" 14*d9f75844SAndroid Build Coastguard Worker#import "sdk/objc/components/capturer/RTCCameraVideoCapturer.h" 15*d9f75844SAndroid Build Coastguard Worker#import "sdk/objc/native/api/video_capturer.h" 16*d9f75844SAndroid Build Coastguard Worker#import "sdk/objc/native/src/objc_frame_buffer.h" 17*d9f75844SAndroid Build Coastguard Worker 18*d9f75844SAndroid Build Coastguard Worker@interface RTCTestVideoSourceAdapter : NSObject <RTC_OBJC_TYPE (RTCVideoCapturerDelegate)> 19*d9f75844SAndroid Build Coastguard Worker@property(nonatomic) webrtc::test::MacCapturer *capturer; 20*d9f75844SAndroid Build Coastguard Worker@end 21*d9f75844SAndroid Build Coastguard Worker 22*d9f75844SAndroid Build Coastguard Worker@implementation RTCTestVideoSourceAdapter 23*d9f75844SAndroid Build Coastguard Worker@synthesize capturer = _capturer; 24*d9f75844SAndroid Build Coastguard Worker 25*d9f75844SAndroid Build Coastguard Worker- (void)capturer:(RTC_OBJC_TYPE(RTCVideoCapturer) *)capturer 26*d9f75844SAndroid Build Coastguard Worker didCaptureVideoFrame:(RTC_OBJC_TYPE(RTCVideoFrame) *)frame { 27*d9f75844SAndroid Build Coastguard Worker const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec; 28*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = 29*d9f75844SAndroid Build Coastguard Worker rtc::make_ref_counted<webrtc::ObjCFrameBuffer>(frame.buffer); 30*d9f75844SAndroid Build Coastguard Worker _capturer->OnFrame(webrtc::VideoFrame::Builder() 31*d9f75844SAndroid Build Coastguard Worker .set_video_frame_buffer(buffer) 32*d9f75844SAndroid Build Coastguard Worker .set_rotation(webrtc::kVideoRotation_0) 33*d9f75844SAndroid Build Coastguard Worker .set_timestamp_us(timestamp_us) 34*d9f75844SAndroid Build Coastguard Worker .build()); 35*d9f75844SAndroid Build Coastguard Worker} 36*d9f75844SAndroid Build Coastguard Worker 37*d9f75844SAndroid Build Coastguard Worker@end 38*d9f75844SAndroid Build Coastguard Worker 39*d9f75844SAndroid Build Coastguard Workernamespace { 40*d9f75844SAndroid Build Coastguard Worker 41*d9f75844SAndroid Build Coastguard WorkerAVCaptureDeviceFormat *SelectClosestFormat(AVCaptureDevice *device, size_t width, size_t height) { 42*d9f75844SAndroid Build Coastguard Worker NSArray<AVCaptureDeviceFormat *> *formats = 43*d9f75844SAndroid Build Coastguard Worker [RTC_OBJC_TYPE(RTCCameraVideoCapturer) supportedFormatsForDevice:device]; 44*d9f75844SAndroid Build Coastguard Worker AVCaptureDeviceFormat *selectedFormat = nil; 45*d9f75844SAndroid Build Coastguard Worker int currentDiff = INT_MAX; 46*d9f75844SAndroid Build Coastguard Worker for (AVCaptureDeviceFormat *format in formats) { 47*d9f75844SAndroid Build Coastguard Worker CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription); 48*d9f75844SAndroid Build Coastguard Worker int diff = 49*d9f75844SAndroid Build Coastguard Worker std::abs((int64_t)width - dimension.width) + std::abs((int64_t)height - dimension.height); 50*d9f75844SAndroid Build Coastguard Worker if (diff < currentDiff) { 51*d9f75844SAndroid Build Coastguard Worker selectedFormat = format; 52*d9f75844SAndroid Build Coastguard Worker currentDiff = diff; 53*d9f75844SAndroid Build Coastguard Worker } 54*d9f75844SAndroid Build Coastguard Worker } 55*d9f75844SAndroid Build Coastguard Worker return selectedFormat; 56*d9f75844SAndroid Build Coastguard Worker} 57*d9f75844SAndroid Build Coastguard Worker 58*d9f75844SAndroid Build Coastguard Worker} // namespace 59*d9f75844SAndroid Build Coastguard Worker 60*d9f75844SAndroid Build Coastguard Workernamespace webrtc { 61*d9f75844SAndroid Build Coastguard Workernamespace test { 62*d9f75844SAndroid Build Coastguard Worker 63*d9f75844SAndroid Build Coastguard WorkerMacCapturer::MacCapturer(size_t width, 64*d9f75844SAndroid Build Coastguard Worker size_t height, 65*d9f75844SAndroid Build Coastguard Worker size_t target_fps, 66*d9f75844SAndroid Build Coastguard Worker size_t capture_device_index) { 67*d9f75844SAndroid Build Coastguard Worker RTCTestVideoSourceAdapter *adapter = [[RTCTestVideoSourceAdapter alloc] init]; 68*d9f75844SAndroid Build Coastguard Worker adapter_ = (__bridge_retained void *)adapter; 69*d9f75844SAndroid Build Coastguard Worker adapter.capturer = this; 70*d9f75844SAndroid Build Coastguard Worker 71*d9f75844SAndroid Build Coastguard Worker RTC_OBJC_TYPE(RTCCameraVideoCapturer) *capturer = 72*d9f75844SAndroid Build Coastguard Worker [[RTC_OBJC_TYPE(RTCCameraVideoCapturer) alloc] initWithDelegate:adapter]; 73*d9f75844SAndroid Build Coastguard Worker capturer_ = (__bridge_retained void *)capturer; 74*d9f75844SAndroid Build Coastguard Worker 75*d9f75844SAndroid Build Coastguard Worker AVCaptureDevice *device = 76*d9f75844SAndroid Build Coastguard Worker [[RTC_OBJC_TYPE(RTCCameraVideoCapturer) captureDevices] objectAtIndex:capture_device_index]; 77*d9f75844SAndroid Build Coastguard Worker AVCaptureDeviceFormat *format = SelectClosestFormat(device, width, height); 78*d9f75844SAndroid Build Coastguard Worker [capturer startCaptureWithDevice:device format:format fps:target_fps]; 79*d9f75844SAndroid Build Coastguard Worker} 80*d9f75844SAndroid Build Coastguard Worker 81*d9f75844SAndroid Build Coastguard WorkerMacCapturer *MacCapturer::Create(size_t width, 82*d9f75844SAndroid Build Coastguard Worker size_t height, 83*d9f75844SAndroid Build Coastguard Worker size_t target_fps, 84*d9f75844SAndroid Build Coastguard Worker size_t capture_device_index) { 85*d9f75844SAndroid Build Coastguard Worker return new MacCapturer(width, height, target_fps, capture_device_index); 86*d9f75844SAndroid Build Coastguard Worker} 87*d9f75844SAndroid Build Coastguard Worker 88*d9f75844SAndroid Build Coastguard Workervoid MacCapturer::Destroy() { 89*d9f75844SAndroid Build Coastguard Worker#pragma clang diagnostic push 90*d9f75844SAndroid Build Coastguard Worker#pragma clang diagnostic ignored "-Wunused-variable" 91*d9f75844SAndroid Build Coastguard Worker RTCTestVideoSourceAdapter *adapter = (__bridge_transfer RTCTestVideoSourceAdapter *)adapter_; 92*d9f75844SAndroid Build Coastguard Worker RTC_OBJC_TYPE(RTCCameraVideoCapturer) *capturer = 93*d9f75844SAndroid Build Coastguard Worker (__bridge_transfer RTC_OBJC_TYPE(RTCCameraVideoCapturer) *)capturer_; 94*d9f75844SAndroid Build Coastguard Worker [capturer stopCapture]; 95*d9f75844SAndroid Build Coastguard Worker#pragma clang diagnostic pop 96*d9f75844SAndroid Build Coastguard Worker} 97*d9f75844SAndroid Build Coastguard Worker 98*d9f75844SAndroid Build Coastguard WorkerMacCapturer::~MacCapturer() { 99*d9f75844SAndroid Build Coastguard Worker Destroy(); 100*d9f75844SAndroid Build Coastguard Worker} 101*d9f75844SAndroid Build Coastguard Worker 102*d9f75844SAndroid Build Coastguard Workervoid MacCapturer::OnFrame(const VideoFrame &frame) { 103*d9f75844SAndroid Build Coastguard Worker TestVideoCapturer::OnFrame(frame); 104*d9f75844SAndroid Build Coastguard Worker} 105*d9f75844SAndroid Build Coastguard Worker 106*d9f75844SAndroid Build Coastguard Worker} // namespace test 107*d9f75844SAndroid Build Coastguard Worker} // namespace webrtc 108