forked from opentok/accelerator-core-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OTScreenCapture.m
358 lines (290 loc) · 11.6 KB
/
OTScreenCapture.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// TBScreenCapture.m
//
// Copyright (c) 2014 TokBox Inc. All rights reserved.
//
#include <mach/mach.h>
#include <mach/mach_time.h>
#import "OTScreenCapture.h"
// defines for image scaling
// From https://bugs.chromium.org/p/webrtc/issues/detail?id=4643#c7 :
// Don't send any image larger than 1280px on either edge. Additionally, don't
// send any image with dimensions %16 != 0
#define MAX_EDGE_SIZE_LIMIT 1280.0f
#define EDGE_DIMENSION_COMMON_FACTOR 16.0f
@implementation OTScreenCapture {
CMTime _minFrameDuration;
dispatch_queue_t _queue;
dispatch_source_t _timer;
CVPixelBufferRef _pixelBuffer;
BOOL _capturing;
OTVideoFrame* _videoFrame;
}
@synthesize videoCaptureConsumer;
#pragma mark - Class Lifecycle.
- (instancetype)initWithView:(UIView *)view
{
self = [super init];
if (self) {
_view = view;
// Recommend sending 5 frames per second: Allows for higher image
// quality per frame
_minFrameDuration = CMTimeMake(1, 5);
_queue = dispatch_queue_create("SCREEN_CAPTURE", NULL);
OTVideoFormat *format = [[OTVideoFormat alloc] init];
[format setPixelFormat:OTPixelFormatARGB];
_videoFrame = [[OTVideoFrame alloc] initWithFormat:format];
}
return self;
}
- (void)dealloc
{
[self stopCapture];
CVPixelBufferRelease(_pixelBuffer);
}
#pragma mark - Private Methods
/**
* Make sure receiving video frame container is setup for this image.
*/
- (void)checkImageSize:(CGImageRef)image {
CGFloat width = CGImageGetWidth(image);
CGFloat height = CGImageGetHeight(image);
if (_videoFrame.format.imageHeight == height &&
_videoFrame.format.imageWidth == width)
{
// don't rock the boat. if nothing has changed, don't update anything.
return;
}
[_videoFrame.format.bytesPerRow removeAllObjects];
[_videoFrame.format.bytesPerRow addObject:@(width * 4)];
[_videoFrame.format setImageHeight:height];
[_videoFrame.format setImageWidth:width];
CGSize frameSize = CGSizeMake(width, height);
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
@NO,
kCVPixelBufferCGImageCompatibilityKey,
@NO,
kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
if (NULL != _pixelBuffer) {
CVPixelBufferRelease(_pixelBuffer);
}
CVReturn status;
status = CVPixelBufferCreate(kCFAllocatorDefault,
frameSize.width,
frameSize.height,
kCVPixelFormatType_32ARGB,
(__bridge CFDictionaryRef)(options),
&_pixelBuffer);
NSParameterAssert(status == kCVReturnSuccess && _pixelBuffer != NULL);
}
#pragma mark - Capture lifecycle
/**
* Allocate capture resources; in this case we're just setting up a timer and
* block to execute periodically to send video frames.
*/
- (void)initCapture {
__unsafe_unretained OTScreenCapture* _self = self;
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0),
100ull * NSEC_PER_MSEC, 100ull * NSEC_PER_MSEC);
dispatch_source_set_event_handler(_timer, ^{
@autoreleasepool {
__block UIImage* screen = [_self screenshot];
CGImageRef paddedScreen = [self resizeAndPadImage:screen];
[_self consumeFrame:paddedScreen];
}
});
}
- (void)releaseCapture {
_timer = nil;
}
- (int32_t)startCapture
{
_capturing = YES;
if (_timer) {
dispatch_resume(_timer);
}
return 0;
}
- (int32_t)stopCapture
{
_capturing = NO;
dispatch_sync(_queue, ^{
if (self->_timer) {
dispatch_source_cancel(self->_timer);
}
});
return 0;
}
- (BOOL)isCaptureStarted
{
return _capturing;
}
#pragma mark - Screen capture implementation
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image
{
CGFloat width = CGImageGetWidth(image);
CGFloat height = CGImageGetHeight(image);
CGSize frameSize = CGSizeMake(width, height);
CVPixelBufferLockBaseAddress(_pixelBuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(_pixelBuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context =
CGBitmapContextCreate(pxdata,
frameSize.width,
frameSize.height,
8,
CVPixelBufferGetBytesPerRow(_pixelBuffer),
rgbColorSpace,
kCGImageAlphaPremultipliedFirst |
kCGBitmapByteOrder32Little);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(_pixelBuffer, 0);
return _pixelBuffer;
}
- (int32_t)captureSettings:(OTVideoFormat*)videoFormat
{
videoFormat.pixelFormat = OTPixelFormatARGB;
return 0;
}
+ (void)dimensionsForInputSize:(CGSize)input
containerSize:(CGSize*)destContainerSize
drawRect:(CGRect*)destDrawRect
{
CGFloat sourceWidth = input.width;
CGFloat sourceHeight = input.height;
double sourceAspectRatio = sourceWidth / sourceHeight;
CGFloat destContainerWidth = sourceWidth;
CGFloat destContainerHeight = sourceHeight;
CGFloat destImageWidth = sourceWidth;
CGFloat destImageHeight = sourceHeight;
// if image is wider than tall and width breaks edge size limit
if (MAX_EDGE_SIZE_LIMIT < sourceWidth && sourceAspectRatio >= 1.0) {
destContainerWidth = MAX_EDGE_SIZE_LIMIT;
destContainerHeight = destContainerWidth / sourceAspectRatio;
if (0 != fmod(destContainerHeight, EDGE_DIMENSION_COMMON_FACTOR)) {
// add padding to make height % 16 == 0
destContainerHeight +=
(EDGE_DIMENSION_COMMON_FACTOR - fmod(destContainerHeight,
EDGE_DIMENSION_COMMON_FACTOR));
}
destImageWidth = destContainerWidth;
destImageHeight = destContainerWidth / sourceAspectRatio;
}
// if image is taller than wide and height breaks edge size limit
if (MAX_EDGE_SIZE_LIMIT < destContainerHeight && sourceAspectRatio <= 1.0) {
destContainerHeight = MAX_EDGE_SIZE_LIMIT;
destContainerWidth = destContainerHeight * sourceAspectRatio;
if (0 != fmod(destContainerWidth, EDGE_DIMENSION_COMMON_FACTOR)) {
// add padding to make width % 16 == 0
destContainerWidth +=
(EDGE_DIMENSION_COMMON_FACTOR - fmod(destContainerWidth,
EDGE_DIMENSION_COMMON_FACTOR));
}
destImageHeight = destContainerHeight;
destImageWidth = destContainerHeight * sourceAspectRatio;
}
// ensure the dimensions of the resulting container are safe
if (fmod(destContainerWidth, EDGE_DIMENSION_COMMON_FACTOR) != 0) {
double remainder = fmod(destContainerWidth,
EDGE_DIMENSION_COMMON_FACTOR);
// increase the edge size only if doing so does not break the edge limit
if (destContainerWidth + (EDGE_DIMENSION_COMMON_FACTOR - remainder) >
MAX_EDGE_SIZE_LIMIT)
{
destContainerWidth -= remainder;
} else {
destContainerWidth += EDGE_DIMENSION_COMMON_FACTOR - remainder;
}
}
// ensure the dimensions of the resulting container are safe
if (fmod(destContainerHeight, EDGE_DIMENSION_COMMON_FACTOR) != 0) {
double remainder = fmod(destContainerHeight,
EDGE_DIMENSION_COMMON_FACTOR);
// increase the edge size only if doing so does not break the edge limit
if (destContainerHeight + (EDGE_DIMENSION_COMMON_FACTOR - remainder) >
MAX_EDGE_SIZE_LIMIT)
{
destContainerHeight -= remainder;
} else {
destContainerHeight += EDGE_DIMENSION_COMMON_FACTOR - remainder;
}
}
destContainerSize->width = destContainerWidth;
destContainerSize->height = destContainerHeight;
// scale and recenter source image to fit in destination container
if (sourceAspectRatio > 1.0) {
destDrawRect->origin.x = 0;
destDrawRect->origin.y =
(destContainerHeight - destImageHeight) / 2;
destDrawRect->size.width = destContainerWidth;
destDrawRect->size.height =
destContainerWidth / sourceAspectRatio;
} else {
destDrawRect->origin.x =
(destContainerWidth - destImageWidth) / 2;
destDrawRect->origin.y = 0;
destDrawRect->size.height = destContainerHeight;
destDrawRect->size.width =
destContainerHeight * sourceAspectRatio;
}
}
- (CGImageRef)resizeAndPadImage:(UIImage*)sourceUIImage {
CGImageRef sourceCGImage = [sourceUIImage CGImage];
CGFloat sourceWidth = CGImageGetWidth(sourceCGImage);
CGFloat sourceHeight = CGImageGetHeight(sourceCGImage);
CGSize sourceSize = CGSizeMake(sourceWidth, sourceHeight);
CGSize destContainerSize = CGSizeZero;
CGRect destRectForSourceImage = CGRectZero;
[OTScreenCapture dimensionsForInputSize:sourceSize
containerSize:&destContainerSize
drawRect:&destRectForSourceImage];
UIGraphicsBeginImageContextWithOptions(destContainerSize, NO, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// flip source image to match destination coordinate system
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -destRectForSourceImage.size.height);
CGContextDrawImage(context, destRectForSourceImage, sourceCGImage);
// Clean up and get the new image.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [newImage CGImage];
}
- (UIImage *)screenshot
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void) consumeFrame:(CGImageRef)frame {
[self checkImageSize:frame];
static mach_timebase_info_data_t time_info;
uint64_t time_stamp = 0;
if (!(_capturing && self.videoCaptureConsumer)) {
return;
}
if (time_info.denom == 0) {
(void) mach_timebase_info(&time_info);
}
time_stamp = mach_absolute_time();
time_stamp *= time_info.numer;
time_stamp /= time_info.denom;
CMTime time = CMTimeMake(time_stamp, 1000);
CVImageBufferRef ref = [self pixelBufferFromCGImage:frame];
CVPixelBufferLockBaseAddress(ref, 0);
_videoFrame.timestamp = time;
_videoFrame.format.estimatedFramesPerSecond =
_minFrameDuration.timescale / _minFrameDuration.value;
_videoFrame.format.estimatedCaptureDelay = 100;
_videoFrame.orientation = OTVideoOrientationUp;
[_videoFrame clearPlanes];
[_videoFrame.planes addPointer:CVPixelBufferGetBaseAddress(ref)];
[self.videoCaptureConsumer consumeFrame:_videoFrame];
CVPixelBufferUnlockBaseAddress(ref, 0);
}
@end