-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFScreenRecorder.m
290 lines (250 loc) · 11.4 KB
/
BFScreenRecorder.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
//
// BFScreenRecorder.m
// BFViewDebugger
//
// Created by Sachin Kesiraju on 12/19/16.
// Copyright © 2016 Sachin Kesiraju. All rights reserved.
//
#import "BFScreenRecorder.h"
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>
@interface BFScreenRecorder()
@property (strong, nonatomic) AVAssetWriter *videoWriter;
@property (strong, nonatomic) AVAssetWriterInput *videoWriterInput;
@property (strong, nonatomic) AVAssetWriterInputPixelBufferAdaptor *videoWriterAdapter;
@property (strong, nonatomic) CADisplayLink *displayLink;
@property (nonatomic) CVPixelBufferPoolRef outputBufferPool;
@property (nonatomic) CFTimeInterval firstTimeStamp;
//rendering queues
@property (strong, nonatomic) dispatch_queue_t videoRenderQueue;
@property (strong, nonatomic) dispatch_queue_t pixelBufferQueue;
@property (strong, nonatomic) dispatch_semaphore_t frameRenderingSemaphore;
@property (strong, nonatomic) dispatch_semaphore_t pixelAppendingSemaphore;
@end
@implementation BFScreenRecorder
#pragma mark - Init
+ (instancetype) sharedRecorder
{
static BFScreenRecorder *screenRecorder = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
screenRecorder = [[self alloc] init];
});
return screenRecorder;
}
- (instancetype) init
{
self = [super init];
if (self) {
_pixelBufferQueue = dispatch_queue_create("BFScreenRecorder.pixelBufferQueue", DISPATCH_QUEUE_SERIAL);
_videoRenderQueue = dispatch_queue_create("BFScreenRecorder.videoRenderQueue", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(_videoRenderQueue, dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0));
_frameRenderingSemaphore = dispatch_semaphore_create(1);
_pixelAppendingSemaphore = dispatch_semaphore_create(1);
[self setupVideoWriter];
}
return self;
}
#pragma mark - Recording
- (void) startRecording
{
if (!self.isRecording) {
[self videoWriterStartRecording];
self.isRecording = (self.videoWriter.status == AVAssetWriterStatusWriting);
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(writeVideoFrame)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
if ([self.delegate respondsToSelector:@selector(screenRecorderDidStartRecording:)]) {
[self.delegate screenRecorderDidStartRecording:self];
}
}
}
- (void) stopRecording
{
if (self.isRecording) {
self.isRecording = !self.isRecording;
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[self completeRecordingSessionWithCompletion:^(NSURL *videoURL) {
[self saveVideoToPhotoLibrary:videoURL]; //save video to camera roll
if ([self.delegate respondsToSelector:@selector(screenRecorder:didStopRecordingWithCapturedVideo:)]) {
[self.delegate screenRecorder:self didStopRecordingWithCapturedVideo:videoURL];
}
}];
}
}
- (void) saveVideoToPhotoLibrary:(NSURL *) videoURL
{
//Save recorded video to photos
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (!error) {
NSLog(@"Video saved to camera roll");
} else {
NSLog(@"Could not save video to camera roll");
}
}];
}
#pragma mark - Internal
-(void) setupVideoWriter
{
CGFloat videoScale = [UIScreen mainScreen].scale;
CGSize viewSize = [[UIScreen mainScreen] bounds].size;
NSDictionary *bufferAttributes = @{(id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
(id)kCVPixelBufferCGBitmapContextCompatibilityKey : @YES,
(id)kCVPixelBufferWidthKey : @(viewSize.width * videoScale),
(id)kCVPixelBufferHeightKey : @(viewSize.height * videoScale),
(id)kCVPixelBufferBytesPerRowAlignmentKey : @(viewSize.width * videoScale * 4)
};
_outputBufferPool = NULL;
CVPixelBufferPoolCreate(NULL, NULL, (__bridge CFDictionaryRef)(bufferAttributes), &_outputBufferPool);
NSError* error = nil;
self.videoWriter = [[AVAssetWriter alloc] initWithURL:[self videoFileURL]
fileType:AVFileTypeQuickTimeMovie
error:&error];
NSParameterAssert(self.videoWriter);
NSInteger pixelNumber = viewSize.width * viewSize.height * videoScale;
NSDictionary* videoCompression = @{AVVideoAverageBitRateKey: @(pixelNumber * 11.4)};
NSDictionary* videoSettings = @{AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: [NSNumber numberWithInt: viewSize.width * videoScale],
AVVideoHeightKey: [NSNumber numberWithInt: viewSize.height * videoScale],
AVVideoCompressionPropertiesKey: videoCompression};
//Initialize video writer with settings
self.videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSParameterAssert(self.videoWriterInput);
self.videoWriterInput.expectsMediaDataInRealTime = YES;
self.videoWriterInput.transform = [self videoTransformForDeviceOrientation];
self.videoWriterAdapter = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:self.videoWriterInput sourcePixelBufferAttributes:nil];
[self.videoWriter addInput:self.videoWriterInput];
}
- (void) videoWriterStartRecording
{
[self.videoWriter startWriting];
[self.videoWriter startSessionAtSourceTime:CMTimeMake(0, 1000)];
}
- (void) completeRecordingSessionWithCompletion:(void(^)(NSURL *videoURL)) completionBlock
{
dispatch_async(self.videoRenderQueue, ^{
dispatch_sync(self.pixelBufferQueue, ^{
[self.videoWriterInput markAsFinished];
[self.videoWriter finishWritingWithCompletionHandler:^{
[self removeVideoFilePath:self.videoWriter.outputURL.path];
NSURL *videoURL = self.videoWriter.outputURL;
dispatch_async(dispatch_get_main_queue(), ^{
if (completionBlock) {
[self cleanup];
completionBlock(videoURL);
}
});
}];
});
});
}
- (void) writeVideoFrame
{
if (dispatch_semaphore_wait(self.frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0) {
return;
}
dispatch_async(self.videoRenderQueue, ^{
if (![_videoWriterInput isReadyForMoreMediaData]) {
return;
}
if (!self.firstTimeStamp) {
self.firstTimeStamp = self.displayLink.timestamp;
}
CFTimeInterval elapsed = (self.displayLink.timestamp - self.firstTimeStamp);
CMTime time = CMTimeMakeWithSeconds(elapsed, 1000);
CVPixelBufferRef pixelBuffer = NULL;
CGContextRef bitmapContext = [self createPixelBufferAndBitmapContext:&pixelBuffer];
//draw each window into the context
CGSize viewSize = [UIScreen mainScreen].bounds.size;
dispatch_sync(dispatch_get_main_queue(), ^{
UIGraphicsPushContext(bitmapContext); {
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
[window drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:NO];
}
} UIGraphicsPopContext();
});
//append pixelBuffer on a async dispatch_queue and simultaneously render next frame
//to prevent overwhelming queue with pixel buffers, check if append_pixelBuffer_queue ready else release pixelBuffer and bitmapContext
if (dispatch_semaphore_wait(self.pixelAppendingSemaphore, DISPATCH_TIME_NOW) == 0) {
dispatch_async(self.pixelBufferQueue, ^{
BOOL success = [self.videoWriterAdapter appendPixelBuffer:pixelBuffer withPresentationTime:time];
if (!success) {
NSLog(@"Warning: Unable to write buffer to video");
}
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
dispatch_semaphore_signal(self.pixelAppendingSemaphore);
});
} else {
CGContextRelease(bitmapContext);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CVPixelBufferRelease(pixelBuffer);
}
dispatch_semaphore_signal(self.frameRenderingSemaphore);
});
}
- (void)cleanup
{
self.videoWriterAdapter = nil;
self.videoWriterInput = nil;
self.videoWriter = nil;
self.firstTimeStamp = 0;
CVPixelBufferPoolRelease(_outputBufferPool);
}
#pragma mark - Util
- (NSURL *) videoFileURL
{
NSString *outputPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/screenCapture.mp4"];
[self removeVideoFilePath:outputPath];
return [NSURL fileURLWithPath:outputPath];
}
- (void) removeVideoFilePath:(NSString *) filePath
{
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
NSError* error;
if (![fileManager removeItemAtPath:filePath error:&error]) {
NSLog(@"Could not delete old recording:%@", [error localizedDescription]);
}
}
}
- (CGAffineTransform)videoTransformForDeviceOrientation
{
CGAffineTransform videoTransform;
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationLandscapeLeft:
videoTransform = CGAffineTransformMakeRotation(-M_PI_2);
break;
case UIDeviceOrientationLandscapeRight:
videoTransform = CGAffineTransformMakeRotation(M_PI_2);
break;
case UIDeviceOrientationPortraitUpsideDown:
videoTransform = CGAffineTransformMakeRotation(M_PI);
break;
default:
videoTransform = CGAffineTransformIdentity;
}
return videoTransform;
}
- (CGContextRef)createPixelBufferAndBitmapContext:(CVPixelBufferRef *)pixelBuffer
{
CVPixelBufferPoolCreatePixelBuffer(NULL, _outputBufferPool, pixelBuffer);
CVPixelBufferLockBaseAddress(*pixelBuffer, 0);
CGContextRef bitmapContext = NULL;
bitmapContext = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(*pixelBuffer),
CVPixelBufferGetWidth(*pixelBuffer),
CVPixelBufferGetHeight(*pixelBuffer),
8, CVPixelBufferGetBytesPerRow(*pixelBuffer), CGColorSpaceCreateDeviceRGB(),
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst
);
CGFloat videoScale = [UIScreen mainScreen].scale;
CGSize viewSize = [[UIScreen mainScreen] bounds].size;
CGContextScaleCTM(bitmapContext, videoScale, videoScale);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, viewSize.height);
CGContextConcatCTM(bitmapContext, flipVertical);
return bitmapContext;
}
@end