iOS 使用AVAssetWriter和AVCaptureSession 录制视频

使用AVAssetWriter和AVCaptureSession 录制视频

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
//
// ViewController.m
// rd-screen-record
//
// Created by Austin on 14-9-27.
// Copyright (c) 2014年 rd-screen-record. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
#import <Foundation/Foundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()<AVCaptureAudioDataOutputSampleBufferDelegate,AVCaptureVideoDataOutputSampleBufferDelegate>
{
AVAssetWriter *assetWriter;
AVAssetWriterInput *audioWriterInput;
AVAssetWriterInput *videoWriterInput;
AVCaptureConnection *_audioConnection;
AVCaptureConnection *_videoConnection;
UIView *preview;
BOOL _isRecording;
BOOL readyForAudio;
BOOL readyForVideo;
BOOL videoWritten;
CMTime _timeOffset;
CMTime _audioTimestamp;
CMTime _videoTimestamp;
IBOutlet UIButton *_recordBtn;
IBOutlet UIButton *_playBtn;
dispatch_queue_t _captureVideoDispatchQueue;
AVCaptureDevice *_captureDeviceFront;
AVCaptureDevice *_captureDeviceBack;
AVCaptureDeviceInput *_captureDeviceInputFront;
AVCaptureDeviceInput *_captureDeviceInputBack;
}
@property(nonatomic,retain) NSURL *outputPath;
@property(nonatomic,retain) AVCaptureSession * captureSession;
@property(nonatomic,retain) AVCaptureAudioDataOutput * output;
@end
@implementation ViewController
@synthesize captureSession = _captureSession;
@synthesize output = _output;
@synthesize outputPath = _outputPath;
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (void)viewDidLoad {
_captureVideoDispatchQueue = dispatch_queue_create("RD-SCREEN-RECORD", DISPATCH_QUEUE_SERIAL);
_isRecording=NO;
readyForAudio=NO;
readyForVideo=NO;
videoWritten=NO;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *outPath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), @"video.mp4"];
NSString *outPath=[[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"video.mp4"];
unlink([outPath UTF8String]);
self.outputPath = [NSURL fileURLWithPath:outPath];
[self setupAssetWriter];
[self setupCaptureSession];
[self setupPreview];
[super viewDidLoad];
}
#pragma mark - Camera Preview
- (void)setupPreview{
preview = [[UIView alloc]init];
preview.frame = CGRectMake(0, 0, 320, 320);
[self.view addSubview:preview];
AVCaptureVideoPreviewLayer* previewLayer = [AVCaptureVideoPreviewLayer layerWithSession: _captureSession];
previewLayer.frame = preview.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[preview.layer addSublayer: previewLayer];
}
#pragma mark - AVCaptureSession
- (void)setupCaptureSession{
_captureSession = [[AVCaptureSession alloc] init];
[_captureSession beginConfiguration];
_captureSession.sessionPreset=AVCaptureSessionPresetMedium;
_captureDeviceFront = [self captureDeviceForPosition:AVCaptureDevicePositionFront];
_captureDeviceBack = [self captureDeviceForPosition:AVCaptureDevicePositionBack];
NSError *error = nil;
_captureDeviceInputFront = [AVCaptureDeviceInput deviceInputWithDevice:_captureDeviceFront error:&error];
if (error) {
NSLog(@"error setting up front camera input (%@)", error);
error = nil;
}
_captureDeviceInputBack = [AVCaptureDeviceInput deviceInputWithDevice:_captureDeviceBack error:&error];
if (error) {
NSLog(@"error setting up back camera input (%@)", error);
error = nil;
}
if ([_captureSession canAddInput:_captureDeviceInputFront])
{
[_captureSession addInput:_captureDeviceInputFront];
}
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioIn = [[AVCaptureDeviceInput alloc] initWithDevice:audioCaptureDevice error:nil];
if ([_captureSession canAddInput:audioIn])
{
[_captureSession addInput:audioIn];
}
AVCaptureAudioDataOutput *audioOut = [[AVCaptureAudioDataOutput alloc] init];
[audioOut setSampleBufferDelegate:self queue:_captureVideoDispatchQueue];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoIn = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:nil];
if ([_captureSession canAddInput:videoIn])
{
[_captureSession addInput:videoIn];
}
AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
[videoOut setAlwaysDiscardsLateVideoFrames:NO];
[videoOut setVideoSettings:[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]//kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[videoOut setSampleBufferDelegate:self queue:_captureVideoDispatchQueue];
if ([_captureSession canAddOutput:audioOut])
{
[_captureSession addOutput:audioOut];
}
_audioConnection = [audioOut connectionWithMediaType:AVMediaTypeAudio];
if ([_captureSession canAddOutput:videoOut])
{
[_captureSession addOutput:videoOut];
}
NSString *sessionPreset = [_captureSession sessionPreset];
// apply presets
if ([_captureSession canSetSessionPreset:sessionPreset]) {
[_captureSession setSessionPreset:sessionPreset];
}
_videoConnection = [videoOut connectionWithMediaType:AVMediaTypeVideo];
if ([_videoConnection isVideoStabilizationSupported])
[_videoConnection setEnablesVideoStabilizationWhenAvailable:YES];
[_captureSession commitConfiguration];
[_captureSession startRunning];
}
- (AVCaptureDevice *)captureDeviceForPosition:(AVCaptureDevicePosition)position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
return nil;
}
#pragma mark - AVAssetWriter
- (void)setupAssetWriter{
assetWriter = [[AVAssetWriter alloc] initWithURL:_outputPath fileType:AVFileTypeQuickTimeMovie error:nil];
#if 0
// only audio test
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono; //kAudioChannelLayoutTag_Stereo;
NSDictionary *audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatULaw],AVFormatIDKey,
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,
[NSData dataWithBytes: &acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:64000],AVEncoderBitRateKey,
nil];
audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
[audioWriterInput setExpectsMediaDataInRealTime:YES];
assetWriter = [AVAssetWriter assetWriterWithURL:_outputPath fileType:AVFileTypeWAVE error:nil];
[assetWriter addInput:audioWriterInput];
readyForAudio=YES;
readyForVideo=YES;
#else
// video Configuration
NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:480], AVVideoCleanApertureWidthKey,
[NSNumber numberWithInt:480], AVVideoCleanApertureHeightKey,
[NSNumber numberWithInt:2], AVVideoCleanApertureHorizontalOffsetKey,
[NSNumber numberWithInt:2], AVVideoCleanApertureVerticalOffsetKey,
nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], AVVideoPixelAspectRatioHorizontalSpacingKey,
[NSNumber numberWithInt:1],AVVideoPixelAspectRatioVerticalSpacingKey,
nil];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1024*1000], AVVideoAverageBitRateKey,
[NSNumber numberWithInt:30],AVVideoMaxKeyFrameIntervalKey,
videoCleanApertureSettings, AVVideoCleanApertureKey,
videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
AVVideoProfileLevelH264Main30, AVVideoProfileLevelKey,
nil];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
codecSettings,AVVideoCompressionPropertiesKey,
[NSNumber numberWithInt:480], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSParameterAssert(videoWriterInput);
videoWriterInput.expectsMediaDataInRealTime = YES;
// audio Configuration
AudioChannelLayout acl;
bzero( &acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;//kAudioChannelLayoutTag_Mono
NSDictionary* audioOutputSettings = nil;
audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
[ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
[ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
nil];
audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeAudio outputSettings: audioOutputSettings];
audioWriterInput.expectsMediaDataInRealTime = YES;
#endif
// if ( [assetWriter canAddInput:videoWriterInput] )
// {
// [assetWriter addInput:videoWriterInput];
// }
//
// audioWriterInput.expectsMediaDataInRealTime = YES;
//
// if ( [assetWriter canAddInput:audioWriterInput] )
// {
// [assetWriter addInput:audioWriterInput];
// }
// readyForAudio=YES;
// readyForVideo=YES;
}
#pragma mark - AssetWriterAudioInput, AssetWriterVideoInput
- (BOOL)_setupAssetWriterAudioInput:(CMFormatDescriptionRef)currentFormatDescription
{
const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(currentFormatDescription);
if (!asbd) {
NSLog(@"audio stream description used with non-audio format description");
return NO;
}
unsigned int channels = asbd->mChannelsPerFrame;
double sampleRate = asbd->mSampleRate;
int bitRate = 64000;
NSLog(@"audio stream setup, channels (%d) sampleRate (%f)", channels, sampleRate);
size_t aclSize = 0;
const AudioChannelLayout *currentChannelLayout = CMAudioFormatDescriptionGetChannelLayout(currentFormatDescription, &aclSize);
NSData *currentChannelLayoutData = ( currentChannelLayout && aclSize > 0 ) ? [NSData dataWithBytes:currentChannelLayout length:aclSize] : [NSData data];
NSDictionary *audioCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithUnsignedInt:channels], AVNumberOfChannelsKey,
[NSNumber numberWithDouble:sampleRate], AVSampleRateKey,
[NSNumber numberWithInt:bitRate], AVEncoderBitRateKey,
currentChannelLayoutData, AVChannelLayoutKey, nil];
if ([assetWriter canApplyOutputSettings:audioCompressionSettings forMediaType:AVMediaTypeAudio]) {
audioWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:audioCompressionSettings];
audioWriterInput.expectsMediaDataInRealTime = YES;
NSLog(@"prepared audio-in with compression settings sampleRate (%f) channels (%d) bitRate (%d)", sampleRate, channels, bitRate);
if ([assetWriter canAddInput:audioWriterInput]) {
[assetWriter addInput:audioWriterInput];
} else {
NSLog(@"couldn't add asset writer audio input");
return NO;
}
} else {
NSLog(@"couldn't apply audio output settings");
return NO;
}
return YES;
}
- (BOOL)_setupAssetWriterVideoInput:(CMFormatDescriptionRef)currentFormatDescription
{
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(currentFormatDescription);
float bitRate = 87500.0f * 8.0f;
NSInteger frameInterval = 30;
NSDictionary *compressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:bitRate], AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:frameInterval], AVVideoMaxKeyFrameIntervalKey,
nil];
NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
AVVideoScalingModeResizeAspectFill, AVVideoScalingModeKey,
[NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
[NSNumber numberWithInteger:dimensions.width], AVVideoHeightKey, // square format
compressionSettings, AVVideoCompressionPropertiesKey,
nil];
if ([assetWriter canApplyOutputSettings:videoCompressionSettings forMediaType:AVMediaTypeVideo]) {
videoWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
videoWriterInput.expectsMediaDataInRealTime = YES;
videoWriterInput.transform = CGAffineTransformIdentity;
NSLog(@"prepared video-in with compression settings bps (%f) frameInterval (%d)", bitRate, frameInterval);
if ([assetWriter canAddInput:videoWriterInput]) {
[assetWriter addInput:videoWriterInput];
} else {
NSLog(@"couldn't add asset writer video input");
return NO;
}
} else {
NSLog(@"couldn't apply video output settings");
return NO;
}
return YES;
}
#pragma mark - Record, Stop
- (IBAction)onRecord:(id)sender {
if (_isRecording) {
NSLog(@"stop recording");
[self stopRecording];
}else{
NSLog(@"start recording");
_timeOffset = kCMTimeZero;
_audioTimestamp = kCMTimeZero;
_videoTimestamp = kCMTimeZero;
_isRecording=YES;
}
}
-(IBAction)onStop
{
[self stopRecording];
// NSError *error;
// AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.outputPath error:&error];
// audioPlayer.numberOfLoops = -1;
//
// if (audioPlayer == nil){
// NSLog(@"error: %@",[error description]);
// }else{
// NSLog(@"playing");
// [audioPlayer play];
// }
}
-(void)stopRecording
{
_isRecording=NO;
readyForAudio=NO;
readyForVideo=NO;
videoWritten=NO;
[videoWriterInput markAsFinished];
[audioWriterInput markAsFinished];
[assetWriter finishWritingWithCompletionHandler:^{
NSLog(@"assetWriterStatus:%u",assetWriter.status);
}];
NSDictionary *outputFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@",self.outputPath] error:nil];
NSLog (@"file size: %llu", [outputFileAttributes fileSize]);
}
#pragma mark - AVCaptureAudioDataOutputSampleBufferDelegate, AVCaptureVideoDataOutputSampleBufferDelegate
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
CFRetain(sampleBuffer);
CFRetain(formatDescription);
dispatch_async(_captureVideoDispatchQueue, ^{
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
NSLog(@"sample buffer data is not ready");
CFRelease(sampleBuffer);
CFRelease(formatDescription);
return;
}
if (!_isRecording) {
CFRelease(sampleBuffer);
CFRelease(formatDescription);
return;
}
if (!assetWriter) {
CFRelease(sampleBuffer);
CFRelease(formatDescription);
return;
}
BOOL isAudio = (connection == _audioConnection?YES:NO);
BOOL isVideo = (connection == _videoConnection?YES:NO);
BOOL wasReadyToRecord = (readyForAudio &&readyForVideo);
if (isAudio && !readyForAudio) {
readyForAudio = (unsigned int)[self _setupAssetWriterAudioInput:formatDescription];
NSLog(@"ready for audio (%d)",readyForAudio);
}
if (isVideo && !readyForVideo) {
readyForVideo = (unsigned int)[self _setupAssetWriterVideoInput:formatDescription];
NSLog(@"ready for video (%d)",readyForVideo);
}
BOOL isReadyToRecord = (readyForAudio && readyForVideo);
// calculate the length of the interruption
if (isAudio) {
CMTime time = isVideo ? _videoTimestamp : _audioTimestamp;
// calculate the appropriate time offset
if (CMTIME_IS_VALID(time)) {
CMTime pTimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
if (CMTIME_IS_VALID(_timeOffset)) {
pTimestamp = CMTimeSubtract(pTimestamp, _timeOffset);
}
CMTime offset = CMTimeSubtract(pTimestamp, _audioTimestamp);
_timeOffset = (_timeOffset.value == 0) ? offset : CMTimeAdd(_timeOffset, offset);
NSLog(@"new calculated offset %f valid (%d)", CMTimeGetSeconds(_timeOffset), CMTIME_IS_VALID(_timeOffset));
} else {
NSLog(@"invalid audio timestamp, no offset update");
}
_audioTimestamp.flags = 0;
_videoTimestamp.flags = 0;
}
if (isVideo && isReadyToRecord) {
CMSampleBufferRef bufferToWrite = NULL;
if (_timeOffset.value > 0) {
bufferToWrite = [self _createOffsetSampleBuffer:sampleBuffer withTimeOffset:_timeOffset];
if (!bufferToWrite) {
NSLog(@"error subtracting the timeoffset from the sampleBuffer");
}
} else {
bufferToWrite = sampleBuffer;
CFRetain(bufferToWrite);
}
if (bufferToWrite) {
// update the last video timestamp
CMTime time = CMSampleBufferGetPresentationTimeStamp(bufferToWrite);
CMTime duration = CMSampleBufferGetDuration(bufferToWrite);
if (duration.value > 0)
time = CMTimeAdd(time, duration);
if (time.value > _videoTimestamp.value) {
// [self _writeSampleBuffer:bufferToWrite ofType:AVMediaTypeVideo];
_videoTimestamp = time;
videoWritten = YES;
}
CFRelease(bufferToWrite);
}
} else if (isAudio && isReadyToRecord) {
CMSampleBufferRef bufferToWrite = NULL;
if (_timeOffset.value > 0) {
bufferToWrite = [self _createOffsetSampleBuffer:sampleBuffer withTimeOffset:_timeOffset];
if (!bufferToWrite) {
NSLog(@"error subtracting the timeoffset from the sampleBuffer");
}
} else {
bufferToWrite = sampleBuffer;
CFRetain(bufferToWrite);
}
if (bufferToWrite && videoWritten) {
// update the last audio timestamp
CMTime time = CMSampleBufferGetPresentationTimeStamp(bufferToWrite);
CMTime duration = CMSampleBufferGetDuration(bufferToWrite);
if (duration.value > 0)
time = CMTimeAdd(time, duration);
if (time.value > _audioTimestamp.value) {
[self _writeSampleBuffer:bufferToWrite ofType:AVMediaTypeAudio];
_audioTimestamp = time;
}
CFRelease(bufferToWrite);
}
}
if ( !wasReadyToRecord && isReadyToRecord ) {
}
CFRelease(sampleBuffer);
CFRelease(formatDescription);
// }];
});
}
- (CMSampleBufferRef)_createOffsetSampleBuffer:(CMSampleBufferRef)sampleBuffer withTimeOffset:(CMTime)timeOffset
{
CMItemCount itemCount;
OSStatus status = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, 0, NULL, &itemCount);
if (status) {
NSLog(@"couldn't determine the timing info count");
return NULL;
}
CMSampleTimingInfo *timingInfo = (CMSampleTimingInfo *)malloc(sizeof(CMSampleTimingInfo) * (unsigned long)itemCount);
if (!timingInfo) {
NSLog(@"couldn't allocate timing info");
return NULL;
}
status = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, itemCount, timingInfo, &itemCount);
if (status) {
free(timingInfo);
timingInfo = NULL;
NSLog(@"failure getting sample timing info array");
return NULL;
}
for (CMItemCount i = 0; i < itemCount; i++) {
timingInfo[i].presentationTimeStamp = CMTimeSubtract(timingInfo[i].presentationTimeStamp, timeOffset);
timingInfo[i].decodeTimeStamp = CMTimeSubtract(timingInfo[i].decodeTimeStamp, timeOffset);
}
CMSampleBufferRef outputSampleBuffer;
CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, sampleBuffer, itemCount, timingInfo, &outputSampleBuffer);
if (timingInfo) {
free(timingInfo);
timingInfo = NULL;
}
return outputSampleBuffer;
}
- (void)_writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType
{
if ( assetWriter.status == AVAssetWriterStatusUnknown ) {
if ([assetWriter startWriting]) {
CMTime startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
[assetWriter startSessionAtSourceTime:startTime];
NSLog(@"asset writer started writing with status (%d)", assetWriter.status);
} else {
NSLog(@"asset writer error when starting to write (%@)", [assetWriter error]);
}
}
if ( assetWriter.status == AVAssetWriterStatusFailed ) {
NSLog(@"asset writer failure, (%@)", assetWriter.error.localizedDescription);
return;
}
if ( assetWriter.status == AVAssetWriterStatusWriting ) {
if (mediaType == AVMediaTypeVideo) {
if (videoWriterInput.readyForMoreMediaData) {
if (![videoWriterInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"asset writer error appending video (%@)", [assetWriter error]);
}
}
} else if (mediaType == AVMediaTypeAudio) {
if (audioWriterInput.readyForMoreMediaData) {
if (![audioWriterInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"asset writer error appending audio (%@)", [assetWriter error]);
}
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) saveVideoToCameraRoll{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:self.outputPath completionBlock:^(NSURL *assetURL, NSError *error){
NSLog(@"ASSET URL: %@", [assetURL path]);
if(error) {
NSLog(@"CameraViewController: Error on saving movie : %@ {imagePickerController}", error);
}
else {
NSLog(@"Video salvato correttamente in URL: %@", assetURL);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self.outputPath path]];
NSLog(@"IL FILE ESISTE: %hhd", fileExists);
NSLog(@"E PESA: %@", [[[NSFileManager defaultManager] attributesOfItemAtPath: [self.outputPath path] error:&error] objectForKey:NSFileSize]);
}
}];
}
@end