一、前言
上一篇文章,分享了视频h264硬编码,有了视频,怎么能少了音频呢!接下来分享音频aac硬编码。
二、音频硬编码
1.编码器类DDHardwareAudioEncoder.h文件中,该类继承自DDAudioEncoding(编码器抽象接口类)其中,DDLiveAudioConfiguration是音频配置文件,里面是音频采样率、码率、声道数目等音频相关属性,具体文件实现如下:
#import "DDAudioEncoding.h"
@interface DDHardwareAudioEncoder : NSObject
#pragma mark - Initializer
///=============================================================================
/// @name Initializer
///=============================================================================
- (nullable instancetype)init UNAVAILABLE_ATTRIBUTE;
+ (nullable instancetype)new UNAVAILABLE_ATTRIBUTE;
@end
2.编码器抽象接口类DDAudioEncoding.h文件实现如下:
其中DDAudioFrame类是编码成功后数据处理类,里面有每帧编码成功后的data、nSamplePerSec(采样率)、nChannel(通道数)、audioHeader(音频头)等属性。
#import
#import
#import "DDAudioFrame.h"
#import "DDLiveAudioConfiguration.h"
@protocol DDAudioEncoding;
/// 编码器编码后回调
@protocol DDAudioEncodingDelegate
@required
- (void)audioEncoder:(nullable id)encoder audioFrame:(nullable DDAudioFrame*)frame;
@end
/// 编码器抽象的接口
@protocol DDAudioEncoding
@required
- (void)encodeAudioData:(AudioBufferList)inBufferList timeStamp:(uint64_t)timeStamp;
@optional
- (nullable instancetype)initWithAudioStreamConfiguration:(nullable DDLiveAudioConfiguration*)configuration;
- (void)setDelegate:(nullable id)delegate;
- (nullable NSData*)adtsData:(NSInteger)channel rawDataLength:(NSInteger)rawDataLength;
@end
3.下面是具体实现的DDHardwareAudioEncoder.m类文件
#import "DDHardwareAudioEncoder.h"
@interface DDHardwareAudioEncoder (){
AudioConverterRef m_converter;
char *aacBuf;
}
@property (nonatomic, strong) DDLiveAudioConfiguration *configuration;
@property (nonatomic, weak) id aacDeleage;
@end
@implementation DDHardwareAudioEncoder
- (instancetype)initWithAudioStreamConfiguration:(DDLiveAudioConfiguration *)configuration{
if(self = [super init]){
_configuration = configuration;
}
return self;
}
- (void)dealloc{
if(aacBuf) free(aacBuf);
}
#pragma mark -- DDAudioEncoder
- (void)setDelegate:(id)delegate{
_aacDeleage = delegate;
}
- (void)encodeAudioData:(AudioBufferList)inBufferList timeStamp:(uint64_t)timeStamp{
if (![self createAudioConvert]){
return;
}
if(!aacBuf){
aacBuf = malloc(inBufferList.mBuffers[0].mDataByteSize);
}
// 初始化一个输出缓冲列表
AudioBufferList outBufferList;
outBufferList.mNumberBuffers = 1;
outBufferList.mBuffers[0].mNumberChannels = inBufferList.mBuffers[0].mNumberChannels;
outBufferList.mBuffers[0].mDataByteSize = inBufferList.mBuffers[0].mDataByteSize; // 设置缓冲区大小
outBufferList.mBuffers[0].mData = aacBuf; // 设置AAC缓冲区
UInt32 outputDataPacketSize = 1;
if (AudioConverterFillComplexBuffer(m_converter, inputDataProc, &inBufferList, &outputDataPacketSize, &outBufferList, NULL) != noErr){
return;
}
DDAudioFrame *audioFrame = [[DDAudioFrame alloc] init];
audioFrame.timestamp = timeStamp;
audioFrame.nSamplePerSec = self.configuration.audioSampleRate;
audioFrame.nChannel = self.configuration.numberOfChannels;
NSData *rawAAC = [NSData dataWithBytes:outBufferList.mBuffers[0].mData length:outBufferList.mBuffers[0].mDataByteSize];
NSData *adtsHeader = [self adtsData:2 rawDataLength:rawAAC.length];
NSMutableData *fullData = [NSMutableData dataWithData:adtsHeader];
[fullData appendData:rawAAC];
audioFrame.data = fullData;
char exeData[2];
exeData[0] = _configuration.asc[0];
exeData[1] = _configuration.asc[1];
if(self.aacDeleage && [self.aacDeleage respondsToSelector:@selector(audioEncoder:audioFrame:)]){
[self.aacDeleage audioEncoder:self audioFrame:audioFrame]; // 数据传出去之后,实现该代理方法,根据后台数据格式进行数据封装,然后发送
}
}
#pragma mark -- CustomMethod
-(BOOL)createAudioConvert{ //根据输入样本初始化一个编码转换器
if (m_converter != nil){
return TRUE;
}
AudioStreamBasicDescription inputFormat = {0};
inputFormat.mSampleRate = _configuration.audioSampleRate;
inputFormat.mFormatID = kAudioFormatLinearPCM;
inputFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
inputFormat.mChannelsPerFrame = (UInt32)_configuration.numberOfChannels;
inputFormat.mFramesPerPacket = 1;
inputFormat.mBitsPerChannel = 16;
inputFormat.mBytesPerFrame = inputFormat.mBitsPerChannel / 8 * inputFormat.mChannelsPerFrame;
inputFormat.mBytesPerPacket = inputFormat.mBytesPerFrame * inputFormat.mFramesPerPacket;
AudioStreamBasicDescription outputFormat; // 这里开始是输出音频格式
memset(&outputFormat, 0, sizeof(outputFormat));
outputFormat.mSampleRate = inputFormat.mSampleRate; // 采样率保持一致
outputFormat.mFormatID = kAudioFormatMPEG4AAC; // AAC编码 kAudioFormatMPEG4AAC kAudioFormatMPEG4AAC_HE_V2
outputFormat.mChannelsPerFrame = (UInt32)_configuration.numberOfChannels;;
outputFormat.mFramesPerPacket = 1024; // AAC一帧是1024个字节
const OSType subtype = kAudioFormatMPEG4AAC;
AudioClassDescription requestedCodecs[2] = {
{
kAudioEncoderComponentType,
subtype,
kAppleSoftwareAudioCodecManufacturer
},
{
kAudioEncoderComponentType,
subtype,
kAppleHardwareAudioCodecManufacturer
}
};
OSStatus result = AudioConverterNewSpecific(&inputFormat, &outputFormat, 2, requestedCodecs, &m_converter);
if(result != noErr) return NO;
return YES;
}
#pragma mark -- AudioCallBack
OSStatus inputDataProc(AudioConverterRef inConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData,AudioStreamPacketDescription **outDataPacketDescription, void *inUserData) {
AudioBufferList bufferList = *(AudioBufferList*)inUserData;
ioData->mBuffers[0].mNumberChannels = 1;
ioData->mBuffers[0].mData = bufferList.mBuffers[0].mData;
ioData->mBuffers[0].mDataByteSize = bufferList.mBuffers[0].mDataByteSize;
return noErr;
}
/**
* Add ADTS header at the beginning of each and every AAC packet.
* This is needed as MediaCodec encoder generates a packet of raw
* AAC data.
*
* Note the packetLen must count in the ADTS header itself.
* See: http://wiki.multimedia.cx/index.php?title=ADTS
* Also: http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Channel_Configurations
**/
- (NSData*)adtsData:(NSInteger)channel rawDataLength:(NSInteger)rawDataLength {
int adtsLength = 7;
char *packet = malloc(sizeof(char) * adtsLength);
// Variables Recycled by addADTStoPacket
int profile = 2; //AAC LC
//39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
int freqIdx = 4; //44.1KHz
int chanCfg = (int)channel; //MPEG-4 Audio Channel Configuration. 1 Channel front-center
NSUInteger fullLength = adtsLength + rawDataLength;
// fill in ADTS data
packet[0] = (char)0xFF; // 11111111 = syncword
packet[1] = (char)0xF9; // 1111 1 00 1 = syncword MPEG-2 Layer CRC
packet[2] = (char)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
packet[3] = (char)(((chanCfg&3)<<6) + (fullLength>>11));
packet[4] = (char)((fullLength&0x7FF) >> 3);
packet[5] = (char)(((fullLength&7)<<5) + 0x1F);
packet[6] = (char)0xFC;
NSData *data = [NSData dataWithBytesNoCopy:packet length:adtsLength freeWhenDone:YES];
return data;
}
@end
三、FFLiveKit
如果做视频拉流端既采集音视频、编码、封装、推流,推荐参考FFLiveKit这一框架,很详细,自己看源码就行了,这里就不再赘述。