-
Notifications
You must be signed in to change notification settings - Fork 43
/
ECVMovieRecorder.m
executable file
·442 lines (354 loc) · 16.1 KB
/
ECVMovieRecorder.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
/* Copyright (c) 2009, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#if !__LP64__
#import "ECVMovieRecorder.h"
// Models
#import "ECVVideoFormat.h"
#import "ECVVideoStorage.h"
#import "ECVVideoFrame.h"
#import "ECVFrameRateConverter.h"
// Other Sources
#if defined(ECV_ENABLE_AUDIO)
#import "ECVAudioDevice.h"
#import "ECVAudioPipe.h"
#endif
#import "ECVDebug.h"
#import "ECVICM.h"
#define ECVAudioBufferBytesSize (ECVStandardAudioStreamBasicDescription.mBytesPerPacket * 1000) // Should be more than enough to keep up with the incoming data.
@protocol ECVCompressionDelegate
- (void)addEncodedFrame:(ICMEncodedFrameRef const)frame;
@end
static OSStatus ECVCompressionDelegateHandler(id<ECVCompressionDelegate> const movieRecorder, ICMCompressionSessionRef const session, OSStatus const error, ICMEncodedFrameRef const frame)
{
[movieRecorder addEncodedFrame:noErr == error ? frame : NULL];
return noErr;
}
@implementation ECVMovieRecordingOptions
#pragma mark -ECVMovieRecordingOptions
@synthesize URL = _URL;
@synthesize videoStorage = _videoStorage;
@synthesize audioInput = _audioInput;
#pragma mark -
@synthesize videoCodec = _videoCodec;
@synthesize videoQuality = _videoQuality;
@synthesize stretchOutput = _stretchOutput;
@synthesize outputSize = _outputSize;
@synthesize cropRect = _cropRect;
@synthesize upconvertsFromMono = _upconvertsFromMono;
@synthesize frameRate = _frameRate;
#pragma mark -
- (NSDictionary *)cleanAperatureDictionary
{
NSRect const c = [self cropRect];
ECVIntegerSize const s1 = [[_videoStorage videoFormat] frameSize];
ECVIntegerSize const s2 = (ECVIntegerSize){round(NSWidth(c) * s1.width), round(NSHeight(c) * s1.height)};
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:s2.width], kCVImageBufferCleanApertureWidthKey,
[NSNumber numberWithDouble:s2.height], kCVImageBufferCleanApertureHeightKey,
[NSNumber numberWithDouble:round(NSMinX(c) * s1.width - (s1.width - s2.width) / 2.0)], kCVImageBufferCleanApertureHorizontalOffsetKey,
[NSNumber numberWithDouble:round(NSMinY(c) * s1.height - (s1.height - s2.height) / 2.0)], kCVImageBufferCleanApertureVerticalOffsetKey,
nil];
}
#pragma mark -
@synthesize volume = _volume;
#pragma mark -ECVMovieRecordingOptions(Private)
- (ECVFrameRateConverter *)_frameRateConverter
{
return [[[ECVFrameRateConverter alloc] initWithSourceFrameRate:[[_videoStorage videoFormat] frameRate] targetFrameRate:_frameRate] autorelease];
}
- (ECVIntegerSize)_outputSize
{
return _stretchOutput ? _outputSize : [[_videoStorage videoFormat] frameSize];
}
- (ICMCompressionSessionRef)_compressionSessionWithDelegate:(id<ECVCompressionDelegate> const)delegate
{
ICMCompressionSessionOptionsRef opts = NULL;
ECVOSStatus(ICMCompressionSessionOptionsCreate(kCFAllocatorDefault, &opts));
ECVICMCSOSetProperty(opts, DurationsNeeded, (Boolean)true);
ECVICMCSOSetProperty(opts, AllowAsyncCompletion, (Boolean)true);
NSTimeInterval frameRateInterval = 0.0;
if(QTGetTimeInterval(_frameRate, &frameRateInterval)) ECVICMCSOSetProperty(opts, ExpectedFrameRate, X2Fix(1.0 / frameRateInterval));
ECVICMCSOSetProperty(opts, CPUTimeBudget, (UInt32)QTMakeTimeScaled(_frameRate, ECVMicrosecondsPerSecond).timeValue);
ECVICMCSOSetProperty(opts, ScalingMode, (OSType)kICMScalingMode_StretchCleanAperture);
ECVICMCSOSetProperty(opts, Quality, (CodecQ)round([self videoQuality] * codecMaxQuality));
ECVICMCSOSetProperty(opts, Depth, [_videoStorage pixelFormat]);
ICMEncodedFrameOutputRecord callback = {};
callback.frameDataAllocator = kCFAllocatorDefault;
callback.encodedFrameOutputCallback = (ICMEncodedFrameOutputCallback)ECVCompressionDelegateHandler;
callback.encodedFrameOutputRefCon = delegate;
ECVIntegerSize const frameSize = [[_videoStorage videoFormat] frameSize];
ICMCompressionSessionRef compressionSession = NULL;
ECVOSStatus(ICMCompressionSessionCreate(kCFAllocatorDefault, _outputSize.width, _outputSize.height, [self videoCodec], _frameRate.timeScale, opts, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInteger:frameSize.width], kCVPixelBufferWidthKey,
[NSNumber numberWithUnsignedInteger:frameSize.height], kCVPixelBufferHeightKey,
[NSNumber numberWithUnsignedInt:[_videoStorage pixelFormat]], kCVPixelBufferPixelFormatTypeKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[self cleanAperatureDictionary], kCVImageBufferCleanApertureKey,
nil], kCVBufferNonPropagatedAttachmentsKey,
nil], &callback, &compressionSession));
ICMCompressionSessionOptionsRelease(opts);
return compressionSession;
}
- (ECVAudioPipe *)_audioPipe
{
ECVAudioStream *const inputStream = [[[_audioInput streams] objectEnumerator] nextObject];
if(!inputStream) return nil;
ECVAudioPipe *const pipe = [[[ECVAudioPipe alloc] initWithInputDescription:[inputStream basicDescription] outputDescription:ECVStandardAudioStreamBasicDescription upconvertFromMono:_upconvertsFromMono] autorelease];
[pipe setDropsBuffers:NO];
return pipe;
}
#pragma mark -NSObject
- (id)init
{
if((self = [super init])) {
_videoCodec = '@jpeg';
_videoQuality = 0.5f;
_stretchOutput = YES;
_cropRect = ECVUncroppedRect;
_volume = 1.0f;
}
return self;
}
- (void)dealloc
{
[_URL release];
[_videoStorage release];
[_audioInput release];
[super dealloc];
}
@end
enum {
ECVThreadWait,
ECVThreadRun,
ECVThreadFinished,
};
@interface ECVMovieRecorder(Private)<ECVCompressionDelegate>
- (void)_thread_compress:(ECVMovieRecordingOptions *const)options;
- (void)_thread_record:(ECVMovieRecordingOptions *const)options;
- (void)_addEncodedFrame:(ICMEncodedFrameRef const)frame frameRateConverter:(ECVFrameRateConverter *const)frameRateConverter media:(Media const)media;
- (void)_addAudioBufferFromPipe:(ECVAudioPipe *const)audioPipe description:(SoundDescriptionHandle const)description buffer:(void *const)buffer media:(Media const)media;
@end
@implementation ECVMovieRecorder
#pragma mark +NSObject
+ (void)initialize
{
if([ECVMovieRecorder class] == self) ECVOSErr(EnterMovies());
}
#pragma mark -ECVMovieRecorder
- (id)initWithOptions:(ECVMovieRecordingOptions *const)options error:(out NSError **const)outError
{
if(outError) *outError = nil;
if(!(self = [super init])) return nil;
_compressLock = [[NSConditionLock alloc] initWithCondition:ECVThreadWait];
_compressQueue = [[NSMutableArray alloc] init];
_recordLock = [[NSConditionLock alloc] initWithCondition:ECVThreadWait];
_recordQueue = [[NSMutableArray alloc] init];
_audioPipe = [[options _audioPipe] retain];
[NSThread detachNewThreadSelector:@selector(_thread_compress:) toTarget:self withObject:options];
[NSThread detachNewThreadSelector:@selector(_thread_record:) toTarget:self withObject:options];
return self;
}
#pragma mark -
- (void)addVideoFrame:(ECVVideoFrame *const)frame
{
[_compressLock lock];
if(ECVThreadFinished == [_compressLock condition]) return [_compressLock unlock];
[_compressQueue insertObject:frame atIndex:0];
[_compressLock unlockWithCondition:ECVThreadRun];
}
- (void)addAudioBufferList:(AudioBufferList const *const)bufferList
{
[_recordLock lock];
if(ECVThreadFinished == [_recordLock condition]) return [_recordLock unlock];
[_audioPipe receiveInputBufferList:bufferList];
[_recordLock unlockWithCondition:ECVThreadRun];
}
#pragma mark -
- (void)stopRecording
{
[_compressLock lock];
[_recordLock lock];
if(_stop) {
[_recordLock unlock];
[_compressLock unlock];
return;
}
_stop = YES;
[_recordLock unlockWithCondition:ECVThreadRun];
[_compressLock unlockWithCondition:ECVThreadRun];
[_recordLock lockWhenCondition:ECVThreadFinished];
[_recordLock unlock];
}
#pragma mark -ECVMovieRecorder(Private)
- (void)_thread_compress:(ECVMovieRecordingOptions *const)options
{
NSAutoreleasePool *const outerPool = [[NSAutoreleasePool alloc] init];
ECVOSErr(EnterMoviesOnThread(kNilOptions));
ICMCompressionSessionRef const compressionSession = [options _compressionSessionWithDelegate:self];
CVPixelBufferRef pixelBuffer = NULL;
ECVCVReturn(CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, ICMCompressionSessionGetPixelBufferPool(compressionSession), &pixelBuffer));
for(;;) {
NSAutoreleasePool *const innerPool = [[NSAutoreleasePool alloc] init];
[_compressLock lockWhenCondition:ECVThreadRun];
ECVVideoFrame *const frame = [[[_compressQueue lastObject] retain] autorelease];
if(frame) [_compressQueue removeLastObject];
BOOL const remaining = !![_compressQueue count];
BOOL const stop = _stop;
[_compressLock unlockWithCondition:remaining ? ECVThreadRun : ECVThreadWait];
if([frame lockIfHasBytes]) {
ECVCVPixelBuffer *const buffer = [[[ECVCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer] autorelease];
[buffer lock];
[buffer drawPixelBuffer:frame];
[buffer unlock];
[frame unlock];
ECVOSStatus(ICMCompressionSessionEncodeFrame(compressionSession, pixelBuffer, 0, [options frameRate].timeValue, kICMValidTime_DisplayDurationIsValid, NULL, NULL, NULL));
} else if(frame) {
[self addEncodedFrame:NULL];
}
if(stop && !remaining) {
[innerPool drain];
break;
}
[innerPool release];
}
if(compressionSession) ECVOSStatus(ICMCompressionSessionCompleteFrames(compressionSession, true, 0, 0));
if(compressionSession) ICMCompressionSessionRelease(compressionSession);
CVPixelBufferRelease(pixelBuffer);
ICMEncodedFrameRelease(_encodedFrame);
_encodedFrame = NULL;
[_compressLock lock];
[_compressLock unlockWithCondition:ECVThreadFinished];
ECVOSErr(ExitMoviesOnThread());
[outerPool release];
}
- (void)_thread_record:(ECVMovieRecordingOptions *const)options
{
NSAutoreleasePool *const outerPool = [[NSAutoreleasePool alloc] init];
ECVOSErr(EnterMoviesOnThread(kNilOptions));
Handle dataRef = NULL;
OSType dataRefType = 0;
Movie movie = NULL;
DataHandler dataHandler = NULL;
ECVOSErr(QTNewDataReferenceFromCFURL((CFURLRef)[options URL], kNilOptions, &dataRef, &dataRefType));
ECVOSErr(CreateMovieStorage(dataRef, dataRefType, 'TVOD', smSystemScript, createMovieFileDeleteCurFile, &dataHandler, &movie));
if(!movie) {
ECVLog(ECVError, @"Movie could not be created.");
goto bail;
}
ECVIntegerSize const outputSize = [options _outputSize];
ECVVideoStorage *const videoStorage = [options videoStorage];
ECVFrameRateConverter *const frameRateConverter = [options _frameRateConverter];
Track const videoTrack = NewMovieTrack(movie, Long2Fix(outputSize.width), Long2Fix(outputSize.height), kNoVolume);
Media const videoMedia = NewTrackMedia(videoTrack, VideoMediaType, [options frameRate].timeScale, NULL, 0);
ECVOSErr(BeginMediaEdits(videoMedia));
Track const audioTrack = _audioPipe ? NewMovieTrack(movie, 0, 0, (short)round([options volume] * kFullVolume)) : NULL;
Media const audioMedia = audioTrack ? NewTrackMedia(audioTrack, SoundMediaType, ECVStandardAudioStreamBasicDescription.mSampleRate, NULL, 0) : NULL;
SoundDescriptionHandle soundDescription = NULL;
void *const audioBuffer = audioMedia ? malloc(ECVAudioBufferBytesSize) : NULL;
if(audioMedia) {
ECVOSErr(BeginMediaEdits(audioMedia));
ECVOSStatus(QTSoundDescriptionCreate((AudioStreamBasicDescription *)&ECVStandardAudioStreamBasicDescription, NULL, 0, NULL, 0, kQTSoundDescriptionKind_Movie_AnyVersion, &soundDescription));
}
for(;;) {
NSAutoreleasePool *const innerPool = [[NSAutoreleasePool alloc] init];
[_recordLock lockWhenCondition:ECVThreadRun];
ICMEncodedFrameRef const frame = (ICMEncodedFrameRef)[[[_recordQueue lastObject] retain] autorelease];
if(frame) [_recordQueue removeLastObject];
BOOL const remaining = [_recordQueue count] || [_audioPipe hasReadyBuffers];
BOOL const stop = _stop;
[_recordLock unlockWithCondition:remaining ? ECVThreadRun : ECVThreadWait];
[self _addEncodedFrame:frame frameRateConverter:frameRateConverter media:videoMedia];
[self _addAudioBufferFromPipe:_audioPipe description:soundDescription buffer:audioBuffer media:audioMedia];
if(stop && !remaining) {
[innerPool release];
break;
}
[innerPool release];
}
[_compressLock lockWhenCondition:ECVThreadFinished];
[_compressLock unlock];
if(videoMedia) ECVOSErr(InsertMediaIntoTrack(GetMediaTrack(videoMedia), 0, GetMediaDisplayStartTime(videoMedia), GetMediaDisplayDuration(videoMedia), fixed1));
if(audioMedia) ECVOSErr(InsertMediaIntoTrack(GetMediaTrack(audioMedia), 0, GetMediaDisplayStartTime(audioMedia), GetMediaDisplayDuration(audioMedia), fixed1));
if(videoMedia) ECVOSErr(EndMediaEdits(videoMedia));
if(audioMedia) ECVOSErr(EndMediaEdits(audioMedia));
UpdateMovieInStorage(movie, dataHandler);
CloseMovieStorage(dataHandler);
if(soundDescription) DisposeHandle((Handle)soundDescription);
if(audioBuffer) free(audioBuffer);
if(videoMedia) DisposeTrackMedia(videoMedia);
if(videoTrack) DisposeMovieTrack(videoTrack);
if(audioMedia) DisposeTrackMedia(audioMedia);
if(audioTrack) DisposeMovieTrack(audioTrack);
DisposeMovie(movie);
bail:
[_recordLock lock];
[_recordLock unlockWithCondition:ECVThreadFinished];
ECVOSErr(ExitMoviesOnThread());
[outerPool release];
}
#pragma mark -
- (void)_addEncodedFrame:(ICMEncodedFrameRef const)frame frameRateConverter:(ECVFrameRateConverter *const)frameRateConverter media:(Media const)media
{
if(!frame) return;
UInt8 const *const dataPtr = ICMEncodedFrameGetDataPtr(frame);
ByteCount const bufferSize = ICMEncodedFrameGetDataSize(frame);
TimeValue64 const decodeDuration = ICMEncodedFrameGetDecodeDuration(frame);
TimeValue64 const displayOffset = ICMEncodedFrameGetDisplayOffset(frame);
ImageDescriptionHandle descriptionHandle = NULL;
ECVOSStatus(ICMEncodedFrameGetImageDescription(frame, &descriptionHandle));
MediaSampleFlags const mediaSampleFlags = ICMEncodedFrameGetMediaSampleFlags(frame);
NSUInteger const count = [frameRateConverter nextFrameRepeatCount];
for(NSUInteger i = 0; i < count; ++i) {
ECVOSStatus(AddMediaSample2(media, dataPtr, bufferSize, decodeDuration, displayOffset, (SampleDescriptionHandle)descriptionHandle, 1, mediaSampleFlags, NULL));
}
}
- (void)_addAudioBufferFromPipe:(ECVAudioPipe *const)audioPipe description:(SoundDescriptionHandle const)description buffer:(void *const)buffer media:(Media const)media
{
if(![audioPipe hasReadyBuffers]) return;
AudioBufferList outputBufferList = {1, {2, ECVAudioBufferBytesSize, buffer}};
[audioPipe requestOutputBufferList:&outputBufferList];
ByteCount const size = outputBufferList.mBuffers[0].mDataByteSize;
if(!size || !outputBufferList.mBuffers[0].mData) return;
AddMediaSample2(media, outputBufferList.mBuffers[0].mData, size, 1, 0, (SampleDescriptionHandle)description, size / ECVStandardAudioStreamBasicDescription.mBytesPerFrame, 0, NULL);
}
#pragma mark -ECVMovieRecorder(Private)<ECVCompressionDelegate>
- (void)addEncodedFrame:(ICMEncodedFrameRef const)frame
{
if(frame && frame != _encodedFrame) {
ICMEncodedFrameRelease(_encodedFrame);
_encodedFrame = ICMEncodedFrameRetain(frame);
}
if(!_encodedFrame) return;
[_recordLock lock];
[_recordQueue insertObject:(id)_encodedFrame atIndex:0];
[_recordLock unlockWithCondition:ECVThreadRun];
}
#pragma mark -NSObject
- (void)dealloc
{
[_compressLock release];
[_compressQueue release];
[_recordLock release];
[_recordQueue release];
[_audioPipe release];
[super dealloc];
}
@end
#endif