-
Notifications
You must be signed in to change notification settings - Fork 432
/
FBSimulatorVideoStream.m
696 lines (586 loc) · 25.6 KB
/
FBSimulatorVideoStream.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "FBSimulatorVideoStream.h"
#import <CoreVideo/CoreVideo.h>
#import <CoreVideo/CVPixelBufferIOSurface.h>
#import <FBControlCore/FBControlCore.h>
#import <IOSurface/IOSurface.h>
#import <VideoToolbox/VideoToolbox.h>
#import <CoreImage/CIContext.h>
#import "FBSimulatorError.h"
@protocol FBSimulatorVideoStreamFramePusher <NSObject>
- (BOOL)setupWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError **)error;
- (BOOL)tearDown:(NSError **)error;
- (BOOL)writeEncodedFrame:(CVPixelBufferRef)pixelBuffer frameNumber:(NSUInteger)frameNumber timeAtFirstFrame:(CFTimeInterval)timeAtFirstFrame error:(NSError **)error;
@end
@interface FBSimulatorVideoStreamFramePusher_Bitmap : NSObject <FBSimulatorVideoStreamFramePusher>
- (instancetype)initWithConsumer:(id<FBDataConsumer>)consumer scaleFactor:(NSNumber *)scaleFactor;
@property (nonatomic, strong, readonly) id<FBDataConsumer> consumer;
/**
The scale factor between 0-1. nil for no scaling.
*/
@property (nonatomic, copy, nullable, readonly) NSNumber *scaleFactor;
@end
@interface FBSimulatorVideoStreamFramePusher_VideoToolbox : NSObject <FBSimulatorVideoStreamFramePusher>
- (instancetype)initWithConfiguration:(FBVideoStreamConfiguration *)configuration compressionSessionProperties:(NSDictionary<NSString *, id> *)compressionSessionProperties videoCodec:(CMVideoCodecType)videoCodec consumer:(id<FBDataConsumer>)consumer compressorCallback:(VTCompressionOutputCallback)compressorCallback logger:(id<FBControlCoreLogger>)logger;
@property (nonatomic, copy, readonly) FBVideoStreamConfiguration *configuration;
@property (nonatomic, assign, nullable, readwrite) VTCompressionSessionRef compressionSession;
@property (nonatomic, assign, readonly) CMVideoCodecType videoCodec;
@property (nonatomic, assign, readonly) VTCompressionOutputCallback compressorCallback;
@property (nonatomic, strong, readonly) id<FBControlCoreLogger> logger;
@property (nonatomic, strong, readonly) id<FBDataConsumer> consumer;
@property (nonatomic, strong, readonly) NSDictionary<NSString *, id> *compressionSessionProperties;
@end
static NSDictionary<NSString *, id> *FBBitmapStreamPixelBufferAttributesFromPixelBuffer(CVPixelBufferRef pixelBuffer)
{
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
size_t frameSize = CVPixelBufferGetDataSize(pixelBuffer);
size_t rowSize = CVPixelBufferGetBytesPerRow(pixelBuffer);
OSType pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);
NSString *pixelFormatString = (__bridge_transfer NSString *) UTCreateStringForOSType(pixelFormat);
size_t columnLeft;
size_t columnRight;
size_t rowsTop;
size_t rowsBottom;
CVPixelBufferGetExtendedPixels(pixelBuffer, &columnLeft, &columnRight, &rowsTop, &rowsBottom);
return @{
@"width" : @(width),
@"height" : @(height),
@"row_size" : @(rowSize),
@"frame_size" : @(frameSize),
@"padding_column_left" : @(columnLeft),
@"padding_column_right" : @(columnRight),
@"padding_row_top" : @(rowsTop),
@"padding_row_bottom" : @(rowsBottom),
@"format" : pixelFormatString,
};
}
static CVPixelBufferRef createScaledPixelBuffer(CVPixelBufferRef pixelBuffer,
NSNumber *scaleFactor,
CIContext *context) {
if (scaleFactor == nil || scaleFactor.doubleValue == 1.0) {
return pixelBuffer;
}
CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
CIImage *image = [[CIImage imageWithCVImageBuffer:pixelBuffer] imageByApplyingTransform:CGAffineTransformMakeScale(scaleFactor.doubleValue, scaleFactor.doubleValue)];
CVPixelBufferRef output = NULL;
CVPixelBufferCreate(NULL,
(size_t)CGRectGetWidth(image.extent),
(size_t)CGRectGetHeight(image.extent),
CVPixelBufferGetPixelFormatType(pixelBuffer),
NULL,
&output);
if (output != NULL) {
[context render:image toCVPixelBuffer:output];
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
return output;
}
static void H264AnnexBCompressorCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus encodeStats, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer)
{
FBSimulatorVideoStreamFramePusher_VideoToolbox *pusher = (__bridge FBSimulatorVideoStreamFramePusher_VideoToolbox *)(outputCallbackRefCon);
WriteFrameToAnnexBStream(sampleBuffer, pusher.consumer, pusher.logger, nil);
}
static void MJPEGCompressorCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus encodeStats, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer)
{
FBSimulatorVideoStreamFramePusher_VideoToolbox *pusher = (__bridge FBSimulatorVideoStreamFramePusher_VideoToolbox *)(outputCallbackRefCon);
CMBlockBufferRef blockBufffer = CMSampleBufferGetDataBuffer(sampleBuffer);
WriteJPEGDataToMJPEGStream(blockBufffer, pusher.consumer, pusher.logger, nil);
}
static void MinicapCompressorCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus encodeStats, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer)
{
NSUInteger frameNumber = (NSUInteger) sourceFrameRefCon;
FBSimulatorVideoStreamFramePusher_VideoToolbox *pusher = (__bridge FBSimulatorVideoStreamFramePusher_VideoToolbox *)(outputCallbackRefCon);
if (frameNumber == 0) {
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
WriteMinicapHeaderToStream((uint32) dimensions.width, (uint32) dimensions.height, pusher.consumer, pusher.logger, nil);
}
CMBlockBufferRef blockBufffer = CMSampleBufferGetDataBuffer(sampleBuffer);
WriteJPEGDataToMinicapStream(blockBufffer, pusher.consumer, pusher.logger, nil);
}
@implementation FBSimulatorVideoStreamFramePusher_Bitmap
- (instancetype)initWithConsumer:(id<FBDataConsumer>)consumer scaleFactor:(NSNumber *)scaleFactor
{
self = [super init];
if (!self) {
return nil;
}
_consumer = consumer;
_scaleFactor = scaleFactor;
return self;
}
- (BOOL)setupWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError **)error
{
return YES;
}
- (BOOL)tearDown:(NSError **)error
{
return YES;
}
- (BOOL)writeEncodedFrame:(CVPixelBufferRef)pixelBuffer frameNumber:(NSUInteger)frameNumber timeAtFirstFrame:(CFTimeInterval)timeAtFirstFrame error:(NSError **)error
{
CIContext* context = [CIContext context];
CVPixelBufferRef resizedBuffer = createScaledPixelBuffer(pixelBuffer, self.scaleFactor, context);
CVPixelBufferLockBaseAddress(resizedBuffer, kCVPixelBufferLock_ReadOnly);
void *baseAddress = CVPixelBufferGetBaseAddress(resizedBuffer);
size_t size = CVPixelBufferGetDataSize(resizedBuffer);
if ([self.consumer conformsToProtocol:@protocol(FBDataConsumerSync)]) {
NSData *data = [NSData dataWithBytesNoCopy:baseAddress length:size freeWhenDone:NO];
[self.consumer consumeData:data];
} else {
NSData *data = [NSData dataWithBytes:baseAddress length:size];
[self.consumer consumeData:data];
}
CVPixelBufferUnlockBaseAddress(resizedBuffer, kCVPixelBufferLock_ReadOnly);
return YES;
}
@end
@implementation FBSimulatorVideoStreamFramePusher_VideoToolbox
- (instancetype)initWithConfiguration:(FBVideoStreamConfiguration *)configuration compressionSessionProperties:(NSDictionary<NSString *, id> *)compressionSessionProperties videoCodec:(CMVideoCodecType)videoCodec consumer:(id<FBDataConsumer>)consumer compressorCallback:(VTCompressionOutputCallback)compressorCallback logger:(id<FBControlCoreLogger>)logger
{
self = [super init];
if (!self) {
return nil;
}
_configuration = configuration;
_compressionSessionProperties = compressionSessionProperties;
_compressorCallback = compressorCallback;
_consumer = consumer;
_logger = logger;
_videoCodec = videoCodec;
return self;
}
- (BOOL)setupWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError **)error
{
NSDictionary<NSString *, id> * encoderSpecification = @{
(NSString *) kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder: @YES,
};
if (@available(macOS 12.1, *)) {
encoderSpecification = @{
(NSString *) kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder: @YES,
(NSString *) kVTVideoEncoderSpecification_EnableLowLatencyRateControl: @YES,
};
}
size_t sourceWidth = CVPixelBufferGetWidth(pixelBuffer);
size_t sourceHeight = CVPixelBufferGetHeight(pixelBuffer);
int32_t destinationWidth = (int32_t) sourceWidth;
int32_t destinationHeight = (int32_t) sourceHeight;
NSDictionary<NSString *, id> *sourceImageBufferAttributes = @{
(NSString *) kCVPixelBufferWidthKey: @(sourceWidth),
(NSString *) kCVPixelBufferHeightKey: @(sourceHeight),
};
NSNumber *scaleFactor = self.configuration.scaleFactor;
if (scaleFactor && [scaleFactor isGreaterThan:@0] && [scaleFactor isLessThan:@1]) {
destinationWidth = (int32_t) floor(scaleFactor.doubleValue * sourceWidth);
destinationHeight = (int32_t) floor(scaleFactor.doubleValue * sourceHeight);
[self.logger.info logFormat:@"Applying %@ scale from w=%zu/h=%zu to w=%d/h=%d", scaleFactor, sourceWidth, sourceHeight, destinationWidth, destinationHeight];
}
VTCompressionSessionRef compressionSession = NULL;
OSStatus status = VTCompressionSessionCreate(
nil, // Allocator
destinationWidth,
destinationHeight,
self.videoCodec,
(__bridge CFDictionaryRef) encoderSpecification,
(__bridge CFDictionaryRef) sourceImageBufferAttributes,
nil, // Compressed Data Allocator
self.compressorCallback,
(__bridge void * _Nullable)(self), // Callback Ref.
&compressionSession
);
if (status != noErr) {
return [[FBSimulatorError
describeFormat:@"Failed to start Compression Session %d", status]
failBool:error];
}
status = VTSessionSetProperties(
compressionSession,
(__bridge CFDictionaryRef) self.compressionSessionProperties
);
if (status != noErr) {
return [[FBSimulatorError
describeFormat:@"Failed to set compression session properties %d", status]
failBool:error];
}
status = VTCompressionSessionPrepareToEncodeFrames(compressionSession);
if (status != noErr) {
return [[FBSimulatorError
describeFormat:@"Failed to prepare compression session %d", status]
failBool:error];
}
self.compressionSession = compressionSession;
return YES;
}
- (BOOL)tearDown:(NSError **)error
{
if (self.compressionSession) {
VTCompressionSessionCompleteFrames(self.compressionSession, kCMTimeInvalid);
VTCompressionSessionInvalidate(self.compressionSession);
self.compressionSession = nil;
}
return YES;
}
- (BOOL)writeEncodedFrame:(CVPixelBufferRef)pixelBuffer frameNumber:(NSUInteger)frameNumber timeAtFirstFrame:(CFTimeInterval)timeAtFirstFrame error:(NSError **)error
{
VTCompressionSessionRef compressionSession = self.compressionSession;
if (!compressionSession) {
return [[FBControlCoreError
describeFormat:@"No compression session"]
failBool:error];
}
VTEncodeInfoFlags flags;
CMTime time = CMTimeMakeWithSeconds(CFAbsoluteTimeGetCurrent() - timeAtFirstFrame, NSEC_PER_SEC);
OSStatus status = VTCompressionSessionEncodeFrame(
compressionSession,
pixelBuffer,
time,
kCMTimeInvalid, // Frame duration
NULL, // Frame properties
(void *) frameNumber, // Source Frame Reference for callback.
&flags
);
if (status != 0) {
return [[FBControlCoreError
describeFormat:@"Failed to compress %d", status]
failBool:error];
}
return YES;
}
@end
@interface FBSimulatorVideoStream_Lazy : FBSimulatorVideoStream
@end
@interface FBSimulatorVideoStream_Eager : FBSimulatorVideoStream
@property (nonatomic, assign, readonly) NSUInteger framesPerSecond;
@property (nonatomic, strong, readwrite) NSThread *framePusherThread;
- (instancetype)initWithFramebuffer:(FBFramebuffer *)framebuffer configuration:(FBVideoStreamConfiguration *)configuration framesPerSecond:(NSUInteger)framesPerSecond writeQueue:(dispatch_queue_t)writeQueue logger:(id<FBControlCoreLogger>)logger;
@end
@interface FBSimulatorVideoStream ()
@property (nonatomic, weak, readonly) FBFramebuffer *framebuffer;
@property (nonatomic, copy, readonly) FBVideoStreamConfiguration *configuration;
@property (nonatomic, strong, readonly) dispatch_queue_t writeQueue;
@property (nonatomic, strong, readonly) id<FBControlCoreLogger> logger;
@property (nonatomic, strong, readonly) FBMutableFuture<NSNull *> *startedFuture;
@property (nonatomic, strong, readonly) FBMutableFuture<NSNull *> *stoppedFuture;
@property (nonatomic, assign, nullable, readwrite) CVPixelBufferRef pixelBuffer;
@property (nonatomic, assign, readwrite) CFTimeInterval timeAtFirstFrame;
@property (nonatomic, assign, readwrite) NSUInteger frameNumber;
@property (nonatomic, copy, nullable, readwrite) NSDictionary<NSString *, id> *pixelBufferAttributes;
@property (nonatomic, strong, nullable, readwrite) id<FBDataConsumer> consumer;
@property (nonatomic, strong, nullable, readwrite) id<FBSimulatorVideoStreamFramePusher> framePusher;
- (void)pushFrame;
@end
@implementation FBSimulatorVideoStream
+ (dispatch_queue_t)writeQueue
{
return dispatch_queue_create("com.facebook.FBSimulatorControl.BitmapStream", DISPATCH_QUEUE_SERIAL);
}
+ (nullable instancetype)streamWithFramebuffer:(FBFramebuffer *)framebuffer configuration:(FBVideoStreamConfiguration *)configuration logger:(id<FBControlCoreLogger>)logger
{
NSNumber *framesPerSecondNumber = configuration.framesPerSecond;
NSUInteger framesPerSecond = framesPerSecondNumber.unsignedIntegerValue;
if (framesPerSecondNumber && framesPerSecond > 0) {
return [[FBSimulatorVideoStream_Eager alloc] initWithFramebuffer:framebuffer configuration:configuration framesPerSecond:framesPerSecond writeQueue:self.writeQueue logger:logger];
}
return [[FBSimulatorVideoStream_Lazy alloc] initWithFramebuffer:framebuffer configuration:configuration writeQueue:self.writeQueue logger:logger];
}
- (instancetype)initWithFramebuffer:(FBFramebuffer *)framebuffer configuration:(FBVideoStreamConfiguration *)configuration writeQueue:(dispatch_queue_t)writeQueue logger:(id<FBControlCoreLogger>)logger
{
self = [super init];
if (!self) {
return nil;
}
_framebuffer = framebuffer;
_configuration = configuration;
_writeQueue = writeQueue;
_logger = logger;
_startedFuture = FBMutableFuture.future;
_stoppedFuture = FBMutableFuture.future;
return self;
}
#pragma mark Public
- (FBFuture<NSNull *> *)startStreaming:(id<FBDataConsumer>)consumer
{
return [[FBFuture
onQueue:self.writeQueue resolve:^ FBFuture<NSNull *> * {
if (self.startedFuture.hasCompleted) {
return [[FBSimulatorError
describe:@"Cannot start streaming, since streaming is stopped"]
failFuture];
}
if (self.consumer) {
return [[FBSimulatorError
describe:@"Cannot start streaming, since streaming has already has started"]
failFuture];
}
self.consumer = consumer;
return [self attachConsumerIfNeeded];
}]
onQueue:self.writeQueue fmap:^(id _) {
return self.startedFuture;
}];
}
- (FBFuture<NSNull *> *)stopStreaming
{
return [FBFuture
onQueue:self.writeQueue resolve:^ FBFuture<NSNull *> *{
if (self.stoppedFuture.hasCompleted) {
return self.stoppedFuture;
}
id<FBDataConsumer> consumer = self.consumer;
if (!consumer) {
return [[FBSimulatorError
describe:@"Cannot stop streaming, no consumer attached"]
failFuture];
}
if (![self.framebuffer isConsumerAttached:self]) {
return [[FBSimulatorError
describe:@"Cannot stop streaming, is not attached to a surface"]
failFuture];
}
self.consumer = nil;
[self.framebuffer detachConsumer:self];
[consumer consumeEndOfFile];
if (self.framePusher) {
NSError *error = nil;
if (![self.framePusher tearDown:&error]) {
return [[FBSimulatorError
describeFormat:@"Failed to tear down frame pusher: %@", error]
failFuture];
}
}
[self.stoppedFuture resolveWithResult:NSNull.null];
return self.stoppedFuture;
}];
}
#pragma mark Private
- (FBFuture<NSNull *> *)attachConsumerIfNeeded
{
return [FBFuture
onQueue:self.writeQueue resolve:^{
if ([self.framebuffer isConsumerAttached:self]) {
[self.logger logFormat:@"Already attached %@ as a consumer", self];
return FBFuture.empty;
}
// If we have a surface now, we can start rendering, so mount the surface.
IOSurface *surface = [self.framebuffer attachConsumer:self onQueue:self.writeQueue];
[self didChangeIOSurface:surface];
return FBFuture.empty;
}];
}
#pragma mark FBFramebufferConsumer
- (void)didChangeIOSurface:(IOSurface *)surface
{
[self mountSurface:surface error:nil];
[self pushFrame];
}
- (void)didReceiveDamageRect:(CGRect)rect
{
}
#pragma mark Private
- (BOOL)mountSurface:(IOSurface *)surface error:(NSError **)error
{
// Remove the old pixel buffer.
CVPixelBufferRef oldBuffer = self.pixelBuffer;
if (oldBuffer) {
CVPixelBufferRelease(oldBuffer);
}
// Make a Buffer from the Surface
CVPixelBufferRef buffer = NULL;
CVReturn status = CVPixelBufferCreateWithIOSurface(
NULL,
(__bridge IOSurfaceRef _Nonnull)(surface),
NULL,
&buffer
);
if (status != kCVReturnSuccess) {
return [[FBSimulatorError
describeFormat:@"Failed to create Pixel Buffer from Surface with errorCode %d", status]
failBool:error];
}
id<FBDataConsumer> consumer = self.consumer;
if (!consumer) {
return [[FBSimulatorError
describe:@"Cannot mount surface when there is no consumer"]
failBool:error];
}
// Get the Attributes
NSDictionary<NSString *, id> *attributes = FBBitmapStreamPixelBufferAttributesFromPixelBuffer(buffer);
[self.logger logFormat:@"Mounting Surface with Attributes: %@", attributes];
// Swap the pixel buffers.
self.pixelBuffer = buffer;
self.pixelBufferAttributes = attributes;
id<FBSimulatorVideoStreamFramePusher> framePusher = [self.class framePusherForConfiguration:self.configuration compressionSessionProperties:self.compressionSessionProperties consumer:consumer logger:self.logger error:nil];
if (!framePusher) {
return NO;
}
if (![framePusher setupWithPixelBuffer:buffer error:error]) {
return NO;
}
self.framePusher = framePusher;
// Signal that we've started
[self.startedFuture resolveWithResult:NSNull.null];
return YES;
}
- (void)pushFrame
{
// Ensure that we have all preconditions in place before pushing.
CVPixelBufferRef pixelBufer = self.pixelBuffer;
id<FBDataConsumer> consumer = self.consumer;
id<FBSimulatorVideoStreamFramePusher> framePusher = self.framePusher;
if (!pixelBufer || !consumer || !framePusher) {
return;
}
if (!checkConsumerBufferLimit(consumer, self.logger)) {
return;
}
NSUInteger frameNumber = self.frameNumber;
if (frameNumber == 0) {
self.timeAtFirstFrame = CFAbsoluteTimeGetCurrent();
}
CFTimeInterval timeAtFirstFrame = self.timeAtFirstFrame;
// Push the Frame
[framePusher writeEncodedFrame:pixelBufer frameNumber:frameNumber timeAtFirstFrame:timeAtFirstFrame error:nil];
// Increment frame counter
self.frameNumber = frameNumber + 1;
}
+ (id<FBSimulatorVideoStreamFramePusher>)framePusherForConfiguration:(FBVideoStreamConfiguration *)configuration compressionSessionProperties:(NSDictionary<NSString *, id> *)compressionSessionProperties consumer:(id<FBDataConsumer>)consumer logger:(id<FBControlCoreLogger>)logger error:(NSError **)error
{
// Get the base compression session properties, and add the class-cluster properties to them.
NSMutableDictionary<NSString *, id> *derivedCompressionSessionProperties = [NSMutableDictionary dictionaryWithDictionary:@{
(NSString *) kVTCompressionPropertyKey_RealTime: @YES,
(NSString *) kVTCompressionPropertyKey_AllowFrameReordering: @NO,
}];
[derivedCompressionSessionProperties addEntriesFromDictionary:compressionSessionProperties];
FBVideoStreamEncoding encoding = configuration.encoding;
if ([encoding isEqualToString:FBVideoStreamEncodingH264]) {
derivedCompressionSessionProperties[(NSString *) kVTCompressionPropertyKey_ProfileLevel] = (NSString *)kVTProfileLevel_H264_Baseline_AutoLevel; // ref: http://blog.mediacoderhq.com/h264-profiles-and-levels/
derivedCompressionSessionProperties[(NSString *) kVTCompressionPropertyKey_H264EntropyMode] = (NSString *)kVTH264EntropyMode_CAVLC;
return [[FBSimulatorVideoStreamFramePusher_VideoToolbox alloc]
initWithConfiguration:configuration
compressionSessionProperties:[derivedCompressionSessionProperties copy]
videoCodec:kCMVideoCodecType_H264
consumer:consumer
compressorCallback:H264AnnexBCompressorCallback
logger:logger];
}
if ([encoding isEqualToString:FBVideoStreamEncodingMJPEG]) {
derivedCompressionSessionProperties[(NSString *) kVTCompressionPropertyKey_Quality] = configuration.compressionQuality;
return [[FBSimulatorVideoStreamFramePusher_VideoToolbox alloc]
initWithConfiguration:configuration
compressionSessionProperties:[derivedCompressionSessionProperties copy]
videoCodec:kCMVideoCodecType_JPEG
consumer:consumer
compressorCallback:MJPEGCompressorCallback
logger:logger];
}
if ([encoding isEqualToString:FBVideoStreamEncodingMinicap]) {
derivedCompressionSessionProperties[(NSString *) kVTCompressionPropertyKey_Quality] = configuration.compressionQuality;
return [[FBSimulatorVideoStreamFramePusher_VideoToolbox alloc]
initWithConfiguration:configuration
compressionSessionProperties:[derivedCompressionSessionProperties copy]
videoCodec:kCMVideoCodecType_JPEG
consumer:consumer
compressorCallback:MinicapCompressorCallback
logger:logger];
}
if ([encoding isEqual:FBVideoStreamEncodingBGRA]) {
return [[FBSimulatorVideoStreamFramePusher_Bitmap alloc] initWithConsumer:consumer scaleFactor:configuration.scaleFactor];
}
return [[FBControlCoreError
describeFormat:@"%@ is not supported for Simulators", encoding]
fail:error];
}
- (NSDictionary<NSString *, id> *)compressionSessionProperties
{
return @{};
}
#pragma mark FBiOSTargetOperation
- (FBFuture<NSNull *> *)completed
{
return [[FBMutableFuture.future
resolveFromFuture:self.stoppedFuture]
onQueue:self.writeQueue respondToCancellation:^{
return [self stopStreaming];
}];
}
@end
@implementation FBSimulatorVideoStream_Lazy
- (void)didReceiveDamageRect:(CGRect)rect
{
[self pushFrame];
}
@end
@implementation FBSimulatorVideoStream_Eager
- (instancetype)initWithFramebuffer:(FBFramebuffer *)framebuffer configuration:(FBVideoStreamConfiguration *)configuration framesPerSecond:(NSUInteger)framesPerSecond writeQueue:(dispatch_queue_t)writeQueue logger:(id<FBControlCoreLogger>)logger
{
self = [super initWithFramebuffer:framebuffer configuration:configuration writeQueue:writeQueue logger:logger];
if (!self) {
return nil;
}
_framesPerSecond = framesPerSecond;
return self;
}
#pragma mark Private
- (BOOL)mountSurface:(IOSurface *)surface error:(NSError **)error
{
if (![super mountSurface:surface error:error]) {
return NO;
}
self.framePusherThread = [[NSThread alloc] initWithBlock:^{
[self runFramePushLoop];
}];
[[NSThread currentThread] setThreadPriority:1.0]; //highest priority
self.framePusherThread.qualityOfService = NSQualityOfServiceUserInteractive;
[self.framePusherThread start];
return YES;
}
- (NSDictionary<NSString *, id> *)compressionSessionProperties
{
return @{
(NSString *) kVTCompressionPropertyKey_ExpectedFrameRate: @(2 * self.framesPerSecond),
(NSString *) kVTCompressionPropertyKey_MaxKeyFrameInterval: @60,
(NSString *) kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration: @10, // key frame at least every 10 seconds
(NSString *) kVTCompressionPropertyKey_MaxFrameDelayCount: @0,
(NSString *) kVTCompressionPropertyKey_AverageBitRate: @(800 * 1024), // avg kbps // TODO: make this configurable
(NSString *) kVTCompressionPropertyKey_DataRateLimits: @[@(1200 * 1024), @1], // max kbps // TODO: make this configurable
};
}
// nanosleep can be off up to 8x when invoked for very precise time intervals
// to minimize the drift run a polling loop with shorter sleep interval
// returning when total elapsed time reaches intended sleep interval
- (void)sleep:(uint64_t)timeIntervalNano
{
const uint64_t startTime = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
const long sleepInterval = (long)(timeIntervalNano/8);
const struct timespec sleepTime = {
.tv_sec = 0,
.tv_nsec = sleepInterval,
};
struct timespec remainingTime;
while (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) < startTime + timeIntervalNano) {
nanosleep(&sleepTime, &remainingTime);
}
}
- (void)runFramePushLoop
{
const uint64_t frameInterval = NSEC_PER_SEC / self.framesPerSecond;
uint64_t lastPushedTime = 0;
while (self.stoppedFuture.state == FBFutureStateRunning) {
const uint64_t loopDuration = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - lastPushedTime;
if (lastPushedTime > 0 && loopDuration <= frameInterval) {
const uint64_t sleepInterval = frameInterval - loopDuration;
[self sleep:sleepInterval];
} else if (lastPushedTime > 0) {
[self.logger logFormat:@"Push duration exceeded budget"];
}
lastPushedTime = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
[self pushFrame];
}
}
@end