blob: 004900a42e5da9f4b8443c25440ef4604edb6442 [file] [log] [blame]
Kári Tristan Helgasonede7cb22019-03-06 10:34:09 +01001/*
2 * Copyright (c) 2019 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 "test/mac_capturer.h"
12
13#import "sdk/objc/base/RTCVideoCapturer.h"
14#import "sdk/objc/components/capturer/RTCCameraVideoCapturer.h"
15#import "sdk/objc/native/api/video_capturer.h"
16#import "sdk/objc/native/src/objc_frame_buffer.h"
17
18@interface RTCTestVideoSourceAdapter : NSObject <RTCVideoCapturerDelegate>
19@property(nonatomic) webrtc::test::MacCapturer *capturer;
20@end
21
22@implementation RTCTestVideoSourceAdapter
23@synthesize capturer = _capturer;
24
25- (void)capturer:(RTCVideoCapturer *)capturer didCaptureVideoFrame:(RTCVideoFrame *)frame {
26 const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec;
27 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
28 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(frame.buffer);
29 _capturer->OnFrame(webrtc::VideoFrame::Builder()
30 .set_video_frame_buffer(buffer)
31 .set_rotation(webrtc::kVideoRotation_0)
32 .set_timestamp_us(timestamp_us)
33 .build());
34}
35
36@end
37
38namespace webrtc {
39namespace test {
40
41MacCapturer::MacCapturer(size_t width,
42 size_t height,
43 size_t target_fps,
44 size_t capture_device_index) {
45 RTCTestVideoSourceAdapter *adapter = [[RTCTestVideoSourceAdapter alloc] init];
46 adapter_ = (__bridge_retained void *)adapter;
47 adapter.capturer = this;
48
49 RTCCameraVideoCapturer *capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:adapter];
50 capturer_ = (__bridge_retained void *)capturer;
51
52 AVCaptureDevice *device =
53 [[RTCCameraVideoCapturer captureDevices] objectAtIndex:capture_device_index];
54 AVCaptureDeviceFormat *format =
55 [[RTCCameraVideoCapturer supportedFormatsForDevice:device] objectAtIndex:0];
56 [capturer startCaptureWithDevice:device format:format fps:target_fps];
57}
58
59MacCapturer *MacCapturer::Create(size_t width,
60 size_t height,
61 size_t target_fps,
62 size_t capture_device_index) {
63 return new MacCapturer(width, height, target_fps, capture_device_index);
64}
65
66void MacCapturer::Destroy() {
67#pragma clang diagnostic push
68#pragma clang diagnostic ignored "-Wunused-variable"
69 RTCTestVideoSourceAdapter *adapter = (__bridge_transfer RTCTestVideoSourceAdapter *)adapter_;
70 RTCCameraVideoCapturer *capturer = (__bridge_transfer RTCCameraVideoCapturer *)capturer_;
71 [capturer stopCapture];
72#pragma clang diagnostic pop
73}
74
75MacCapturer::~MacCapturer() {
76 Destroy();
77}
78
79void MacCapturer::OnFrame(const VideoFrame &frame) {
80 TestVideoCapturer::OnFrame(frame);
81}
82
83} // namespace test
84} // namespace webrtc