最簡(jiǎn)單的iOS 推流代碼,視頻捕獲,軟編碼(faac,x264),硬編碼(aac,h264),美顏,flv編碼,rtmp協(xié)議,陸續(xù)更新代碼解析,你想學(xué)的知識(shí)這里都有,愿意懂直播技術(shù)的同學(xué)快來看!!
簡(jiǎn)述sps/pps/AudioSpecificConfig
前文中已經(jīng)多次提到過sps&pps/AudioSpecificConfig。
sps&pps是h264中的概念,它包含了一些編碼信息,如profile,圖像尺寸等信息。在flv中,包含sps&pps的部分被稱為 AVC Sequence header(即AVCDecoderConfigurationRecord,參考ISO-14496-15 AVC file format)。
AudioSpecificConfig是aac中的概念,它包含了音頻信息,如采樣率,聲道數(shù)等信息。在flv中包含AudioSpecificConfig的部分被稱為 AAC Sequence header(即AudioSpecificConfig,參考ISO-14496-3 Audio)。
這兩種數(shù)據(jù)格式可參考標(biāo)準(zhǔn)文檔或者網(wǎng)絡(luò)上的博文,這里只介紹一下在硬編碼/軟編碼的情況下,如何獲取并處理這些數(shù)據(jù)。
可以看出,這兩個(gè)概念其實(shí)就是編碼的一個(gè)配置文件,保存的是后續(xù)音視頻數(shù)據(jù)的一些公共屬性。
sps&pps
h264編碼后,能夠直接獲取sps&pps數(shù)據(jù)。
軟編碼獲取sps&pps數(shù)據(jù)的代碼在aw_x264.c中
//軟編碼x264獲取sps&pps數(shù)據(jù)
static void aw_encode_x264_header(aw_x264_context *aw_ctx){
//主要就是libx264中的此方法
x264_encoder_headers(aw_ctx->x264_handler, &aw_ctx->nal, &aw_ctx->nal_count);
//將獲取到的sps&pps數(shù)據(jù)取出來,保存到aw_ctx->sps_pps_data中
//保存sps pps data
uint8_t *sps_bytes = NULL;
int8_t sps_len = 0;
uint8_t *pps_bytes = NULL;
int8_t pps_len = 0;
int i = 0;
for (; i < aw_ctx->nal_count; i++) {
if (aw_ctx->nal[i].i_type == NAL_SPS) {
sps_bytes = (uint8_t *)aw_ctx->nal[i].p_payload + 4;
sps_len = aw_ctx->nal[i].i_payload - 4;
}else if(aw_ctx->nal[i].i_type == NAL_PPS){
pps_bytes = (uint8_t *)aw_ctx->nal[i].p_payload + 4;
pps_len = aw_ctx->nal[i].i_payload - 4;
}
}
aw_data *avc_decoder_record = aw_create_sps_pps_data(sps_bytes, sps_len, pps_bytes, pps_len);
memcpy_aw_data(&aw_ctx->sps_pps_data, avc_decoder_record->data, avc_decoder_record->size);
free_aw_data(&avc_decoder_record);
}
硬編碼的sps&pps數(shù)據(jù)能夠通過關(guān)鍵幀獲取。代碼在AWHWH264Encoder.m中
//硬編碼h264獲取sps&pps數(shù)據(jù)
static void vtCompressionSessionCallback (void * CM_NULLABLE outputCallbackRefCon,
void * CM_NULLABLE sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CM_NULLABLE CMSampleBufferRef sampleBuffer ){
... ...
... ...
//是否是關(guān)鍵幀,關(guān)鍵幀和非關(guān)鍵幀要區(qū)分清楚。推流時(shí)也要注明。
BOOL isKeyFrame = !CFDictionaryContainsKey( (CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0)), kCMSampleAttachmentKey_NotSync);
//首先獲取sps 和pps
//sps pss 也是h264的一部分,可以認(rèn)為它們是特別的h264視頻幀,保存了h264視頻的一些必要信息。
//沒有這部分?jǐn)?shù)據(jù)h264視頻很難解析出來。
//數(shù)據(jù)處理時(shí),sps pps 數(shù)據(jù)可以作為一個(gè)普通h264幀,放在h264視頻流的最前面。
BOOL needSpsPps = NO;
//這里判斷一下只取一次sps&pps即可
if (!encoder.spsPpsData) {
if (isKeyFrame) {
//獲取avcC,這就是我們想要的sps和pps數(shù)據(jù)。
//如果保存到文件中,需要將此數(shù)據(jù)前加上 [0 0 0 1] 4個(gè)字節(jié),寫入到h264文件的最前面。
//如果推流,將此數(shù)據(jù)放入flv數(shù)據(jù)區(qū)即可。
CMFormatDescriptionRef sampleBufFormat = CMSampleBufferGetFormatDescription(sampleBuffer);
NSDictionary *dict = (__bridge NSDictionary *)CMFormatDescriptionGetExtensions(sampleBufFormat);
encoder.spsPpsData = dict[@"SampleDescriptionExtensionAtoms"][@"avcC"];
}
needSpsPps = YES;
}
... ...
... ...
成功獲取sps&pps數(shù)據(jù)后,可通過aw_sw_x264_encoder.c中的方法aw_encoder_create_sps_pps_tag創(chuàng)建對(duì)應(yīng)的video tag,之后可直接像普通video tag一樣發(fā)送。
//創(chuàng)建sps_pps_tag
extern aw_flv_video_tag *aw_encoder_create_sps_pps_tag(aw_data *sps_pps_data){
//創(chuàng)建普通video tag
aw_flv_video_tag *sps_pps_tag = aw_sw_encoder_create_flv_video_tag();
//關(guān)鍵幀
sps_pps_tag->frame_type = aw_flv_v_frame_type_key;
//package類型,固定的寫0即可
sps_pps_tag->h264_package_type = aw_flv_v_h264_packet_type_seq_header;
//cts寫0
sps_pps_tag->h264_composition_time = 0;
//sps&pps數(shù)據(jù),數(shù)據(jù)上同真實(shí)video tag的h264數(shù)據(jù)放同一個(gè)位置
sps_pps_tag->config_record_data = copy_aw_data(sps_pps_data);
//pts寫0
sps_pps_tag->common_tag.timestamp = 0;
//數(shù)據(jù)總長(zhǎng)度
sps_pps_tag->common_tag.data_size = sps_pps_data->size + 11 + sps_pps_tag->common_tag.header_size;
//返回
return sps_pps_tag;
}
AudioSpecificConfig
aac軟編碼庫faac初始化之后,能夠直接獲取AudioSpecificConfig數(shù)據(jù),在aw_faac.c中。
static void aw_open_faac_enc_handler(aw_faac_context *faac_ctx){
//開啟faac
faac_ctx->faac_handler = faacEncOpen(faac_ctx->config.sample_rate, faac_ctx->config.channel_count, &faac_ctx->max_input_sample_count, &faac_ctx->max_output_byte_count);
... ...
... ...
//配置好faac
faacEncSetConfiguration(faac_ctx->faac_handler, faac_config);
//主要通過此方法獲取AudioSpecificConfig,audio_specific_data就是想要的數(shù)據(jù)
uint8_t *audio_specific_data = NULL;
unsigned long audio_specific_data_len = 0;
faacEncGetDecoderSpecificInfo(faac_ctx->faac_handler, &audio_specific_data, &audio_specific_data_len);
... ...
}
另外,AudioSpecificConfig數(shù)據(jù)結(jié)構(gòu)很簡(jiǎn)單,可以自己簡(jiǎn)單構(gòu)造一份。可參考AWHWAACEncoder.m中的createAudioSpecificConfigFlvTag函數(shù)。
-(aw_flv_audio_tag *)createAudioSpecificConfigFlvTag{
//AudioSpecificConfig中包含3種元素:profile,sampleRate,channelCount
//結(jié)構(gòu)是:profile(5bit)-sampleRate(4bit)-channelCount(4bit)-空(3bit)
uint8_t profile = kMPEG4Object_AAC_LC;
uint8_t sampleRate = 4;
uint8_t chanCfg = 1;
uint8_t config1 = (profile << 3) | ((sampleRate & 0xe) >> 1);
uint8_t config2 = ((sampleRate & 0x1) << 7) | (chanCfg << 3);
//寫入config_data中
aw_data *config_data = NULL;
data_writer.write_uint8(&config_data, config1);
data_writer.write_uint8(&config_data, config2);
... ...
... ...
}
拿到AudioSpecificConfig數(shù)據(jù)后,可通過aw_sw_faac_encoder.c中的aw_encoder_create_audio_specific_config_tag來創(chuàng)建對(duì)應(yīng)的flv audio tag,之后可像正常audio tag一樣發(fā)送。
extern aw_flv_audio_tag *aw_encoder_create_audio_specific_config_tag(aw_data *audio_specific_config_data, aw_faac_config *faac_config){
//創(chuàng)建普通的audio tag
aw_flv_audio_tag *audio_tag = aw_sw_encoder_create_flv_audio_tag(faac_config);
//AudioSpecificConfig數(shù)據(jù),同正常的audio tag在相同位置
audio_tag->config_record_data = copy_aw_data(audio_specific_config_data);
//時(shí)間戳0
audio_tag->common_tag.timestamp = 0;
//整個(gè)tag長(zhǎng)度
audio_tag->common_tag.data_size = audio_specific_config_data->size + 11 + audio_tag->common_tag.header_size;
return audio_tag;
}
rtmp連接成功后,一定要先發(fā)送sps&pps和AudioSpecificConfig這兩個(gè)數(shù)據(jù)對(duì)應(yīng)的tag,否則視頻是播放不出來的。
文章列表
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(一)項(xiàng)目介紹
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(二)代碼架構(gòu)概述
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(三)使用系統(tǒng)接口捕獲音視頻
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(四)如何使用GPUImage,如何美顏
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(五)yuv、pcm數(shù)據(jù)的介紹和獲取
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(六)h264、aac、flv介紹
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(七)h264/aac 硬編碼
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(八)h264/aac 軟編碼
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(九)flv 編碼與音視頻時(shí)間戳同步
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(十)librtmp使用介紹
- 1小時(shí)學(xué)會(huì):最簡(jiǎn)單的iOS直播推流(十一)sps&pps和AudioSpecificConfig介紹(完結(jié))