forked from rFlex/SCRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCAudioConfiguration.m
74 lines (61 loc) · 2.19 KB
/
SCAudioConfiguration.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
//
// SCAudioConfiguration.m
// SCRecorder
//
// Created by Simon CORSIN on 21/11/14.
// Copyright (c) 2014 rFlex. All rights reserved.
//
#import "SCAudioConfiguration.h"
@implementation SCAudioConfiguration
- (id)init {
self = [super init];
if (self) {
self.bitrate = kSCAudioConfigurationDefaultBitrate;
_format = kSCAudioConfigurationDefaultAudioFormat;
}
return self;
}
- (NSDictionary *)createAssetWriterOptionsUsingSampleBuffer:(CMSampleBufferRef)sampleBuffer {
NSDictionary *options = self.options;
if (options != nil) {
return options;
}
Float64 sampleRate = self.sampleRate;
int channels = self.channelsCount;
unsigned long bitrate = (unsigned long)self.bitrate;
if (self.preset != nil) {
if ([self.preset isEqualToString:SCPresetLowQuality]) {
bitrate = 64000;
channels = 1;
} else if ([self.preset isEqualToString:SCPresetMediumQuality]) {
bitrate = 128000;
} else if ([self.preset isEqualToString:SCPresetHighestQuality]) {
bitrate = 320000;
} else {
NSLog(@"Unrecognized video preset %@", self.preset);
}
}
if (sampleBuffer != nil) {
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
const AudioStreamBasicDescription *streamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
if (sampleRate == 0) {
sampleRate = streamBasicDescription->mSampleRate;
}
if (channels == 0) {
channels = streamBasicDescription->mChannelsPerFrame;
}
}
if (sampleRate == 0) {
sampleRate = kSCAudioConfigurationDefaultSampleRate;
}
if (channels == 0) {
channels = kSCAudioConfigurationDefaultNumberOfChannels;
}
return @{
AVFormatIDKey : [NSNumber numberWithInt: self.format],
AVEncoderBitRateKey : [NSNumber numberWithUnsignedLong: bitrate],
AVNumberOfChannelsKey : [NSNumber numberWithInt: channels],
AVSampleRateKey : [NSNumber numberWithInt: sampleRate]
};
}
@end