forked from CommercialTribe/react-native-opentok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTKBasicVideoCapturer.m
269 lines (221 loc) · 8.81 KB
/
OTKBasicVideoCapturer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// OTKBasicVideoCapturer.m
// Getting Started
//
// Created by rpc on 03/03/15.
// Copyright (c) 2015 OpenTok. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "OTKBasicVideoCapturer.h"
#import <math.h>
@interface OTKBasicVideoCapturer ()<AVCaptureVideoDataOutputSampleBufferDelegate>
@property (nonatomic, assign) BOOL captureStarted;
@property (nonatomic, strong) OTVideoFormat *format;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureDeviceInput *inputDevice;
@property (nonatomic, strong) NSString *sessionPreset;
@property (nonatomic, assign) NSUInteger inputWidth;
@property (nonatomic, assign) NSUInteger inputHeight;
@property (nonatomic, assign) NSUInteger outputWidth;
@property (nonatomic, assign) NSUInteger outputHeight;
@property (nonatomic, assign) NSUInteger desiredFrameRate;
@property (nonatomic, strong) dispatch_queue_t captureQueue;
- (CGSize)sizeFromAVCapturePreset:(NSString *)capturePreset;
- (double)bestFrameRateForDevice;
@end
@implementation OTKBasicVideoCapturer
@synthesize videoCaptureConsumer;
- (id)initWithPreset:(NSString *)preset andDesiredFrameRate:(NSUInteger)frameRate
{
self = [super init];
if (self) {
self.sessionPreset = preset;
CGSize imageSize = [self sizeFromAVCapturePreset:self.sessionPreset];
_inputHeight = imageSize.height;
_inputWidth = imageSize.width;
_outputHeight = _inputHeight < _inputWidth ? _inputHeight : _inputWidth; //pick the smaller axis
_outputWidth = _outputHeight; //Should be square
_desiredFrameRate = frameRate;
_captureQueue = dispatch_queue_create("com.tokbox.OTKBasicVideoCapturer",DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)initCapture
{
NSError *error;
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession beginConfiguration];
// Set device capture
self.captureSession.sessionPreset = self.sessionPreset;
AVCaptureDevice *videoDevice = [self frontCamera];
self.inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
[self.captureSession addInput:self.inputDevice];
AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];
outputDevice.alwaysDiscardsLateVideoFrames = YES;
outputDevice.videoSettings = @{
(NSString *)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
};
[outputDevice setSampleBufferDelegate:self queue:self.captureQueue];
[self.captureSession addOutput:outputDevice];
// Set framerate
double bestFrameRate = [self bestFrameRateForDevice];
CMTime desiredMinFrameDuration = CMTimeMake(1, bestFrameRate);
CMTime desiredMaxFrameDuration = CMTimeMake(1, bestFrameRate);
[self.inputDevice.device lockForConfiguration:&error];
self.inputDevice.device.activeVideoMaxFrameDuration = desiredMaxFrameDuration;
self.inputDevice.device.activeVideoMinFrameDuration = desiredMinFrameDuration;
[self.captureSession commitConfiguration];
self.format = [OTVideoFormat videoFormatNV12WithWidth: (int) self.outputWidth
height: (int) self.outputHeight];
}
- (void)releaseCapture
{
self.format = nil;
}
- (int32_t)startCapture
{
self.captureStarted = YES;
[self.captureSession startRunning];
return 0;
}
- (int32_t)stopCapture
{
self.captureStarted = NO;
[self.captureSession stopRunning];
return 0;
}
- (BOOL)isCaptureStarted
{
return self.captureStarted;
}
- (int32_t)captureSettings:(OTVideoFormat*)videoFormat
{
videoFormat.pixelFormat = OTPixelFormatNV12;
videoFormat.imageWidth = (int) self.inputWidth;
videoFormat.imageHeight = (int) self.inputHeight;
return 0;
}
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"Frame dropped");
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
if (!self.captureStarted)
return;
int cropHeight = (int) self.outputHeight;
int cropWidth = (int) self.outputWidth;
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
OTVideoFrame *frame = [[OTVideoFrame alloc] initWithFormat:self.format];
NSUInteger planeCount = CVPixelBufferGetPlaneCount(imageBuffer);
uint8_t *buffer = malloc(sizeof(uint8_t) * cropWidth * cropHeight * 1.5f );
uint8_t *dst = buffer;
uint8_t *planes[planeCount];
uint8_t *rowBaseAddress;
CVPixelBufferLockBaseAddress(imageBuffer, 0);
for (int i = 0; i < planeCount; i++) {
// Account for UV plane being half sized.
if (i == 1) {
cropHeight /= 2;
cropWidth /= 2;
}
int inputBytesPerRow = (int) CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, i);
int inputPixelWidth = (int) CVPixelBufferGetWidthOfPlane(imageBuffer, i);
int bytesPerPixel = inputBytesPerRow / inputPixelWidth; // 1 for Y, 2 for U/V
int bytesToCopyPerRow = cropWidth * bytesPerPixel;
rowBaseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, i);
planes[i] = dst;
for (int row = 0; row < cropHeight; row++) {
memcpy(dst,
rowBaseAddress,
bytesToCopyPerRow);
rowBaseAddress += inputBytesPerRow;
dst += bytesToCopyPerRow;
}
}
CMTime minFrameDuration = self.inputDevice.device.activeVideoMinFrameDuration;
frame.format.estimatedFramesPerSecond = minFrameDuration.timescale / minFrameDuration.value;
frame.format.estimatedCaptureDelay = 100;
frame.orientation = [self currentDeviceOrientation];
CMTime time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
frame.timestamp = time;
[frame setPlanesWithPointers:planes numPlanes: (int) planeCount];
[self.videoCaptureConsumer consumeFrame:frame];
free(buffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
}
#pragma mark - Private methods
- (CGSize)sizeFromAVCapturePreset:(NSString *)capturePreset
{
if ([capturePreset isEqualToString:AVCaptureSessionPreset1280x720])
return CGSizeMake(1280, 720);
if ([capturePreset isEqualToString:AVCaptureSessionPreset1920x1080])
return CGSizeMake(1920, 1080);
if ([capturePreset isEqualToString:AVCaptureSessionPreset640x480])
return CGSizeMake(640, 480);
if ([capturePreset isEqualToString:AVCaptureSessionPreset352x288])
return CGSizeMake(352, 288);
// Not supported preset
return CGSizeMake(0, 0);
}
- (OTVideoOrientation)currentDeviceOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (AVCaptureDevicePositionFront == self.inputDevice.device.position) {
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
return OTVideoOrientationUp;
case UIInterfaceOrientationLandscapeRight:
return OTVideoOrientationDown;
case UIInterfaceOrientationPortrait:
return OTVideoOrientationLeft;
case UIInterfaceOrientationPortraitUpsideDown:
return OTVideoOrientationRight;
default:
return OTVideoOrientationUp;
}
} else {
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
return OTVideoOrientationDown;
case UIInterfaceOrientationLandscapeRight:
return OTVideoOrientationUp;
case UIInterfaceOrientationPortrait:
return OTVideoOrientationLeft;
case UIInterfaceOrientationPortraitUpsideDown:
return OTVideoOrientationRight;
default:
return OTVideoOrientationUp;
}
}
}
- (double)bestFrameRateForDevice
{
double bestFrameRate = 0;
for (AVFrameRateRange* range in
self.inputDevice.device.activeFormat.videoSupportedFrameRateRanges)
{
CMTime currentDuration = range.minFrameDuration;
double currentFrameRate = currentDuration.timescale / currentDuration.value;
if (currentFrameRate > bestFrameRate && currentFrameRate < self.desiredFrameRate) {
bestFrameRate = currentFrameRate;
}
}
return bestFrameRate;
}
- (AVCaptureDevice *)frontCamera
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionFront) {
return device;
}
}
return nil;
}
@end