1小時學會:最簡單的iOS直播推流(十一)sps&pps和AudioSpecificConfig介紹(完結)

最簡單的iOS 推流代碼,視頻捕獲,軟編碼(faac,x264),硬編碼(aac,h264),美顏,flv編碼,rtmp協議,陸續更新代碼解析,你想學的知識這裏都有,願意懂直播技術的同窗快來看!!git

源代碼:https://github.com/hardman/AWLivegithub

#簡述sps/pps/AudioSpecificConfig 前文中已經屢次提到過sps&pps/AudioSpecificConfig。bash

sps&pps是h264中的概念,它包含了一些編碼信息,如profile,圖像尺寸等信息。在flv中,包含sps&pps的部分被稱爲 AVC Sequence header(即AVCDecoderConfigurationRecord,參考ISO-14496-15 AVC file format)。網絡

AudioSpecificConfig是aac中的概念,它包含了音頻信息,如採樣率,聲道數等信息。在flv中包含AudioSpecificConfig的部分被稱爲 AAC Sequence header(即AudioSpecificConfig,參考ISO-14496-3 Audio)。數據結構

這兩種數據格式可參考標準文檔或者網絡上的博文,這裏只介紹一下在硬編碼/軟編碼的狀況下,如何獲取並處理這些數據。架構

能夠看出,這兩個概念其實就是編碼的一個配置文件,保存的是後續音視頻數據的一些公共屬性。ide

sps&pps

h264編碼後,可以直接獲取sps&pps數據。函數

軟編碼獲取sps&pps數據的代碼在aw_x264.c中post

//軟編碼x264獲取sps&pps數據
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數據取出來,保存到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數據可以經過關鍵幀獲取。代碼在AWHWH264Encoder.m中ui

//硬編碼h264獲取sps&pps數據
static void vtCompressionSessionCallback (void * CM_NULLABLE outputCallbackRefCon,
                                          void * CM_NULLABLE sourceFrameRefCon,
                                          OSStatus status,
                                          VTEncodeInfoFlags infoFlags,
                                          CM_NULLABLE CMSampleBufferRef sampleBuffer ){
    ... ...
    ... ...
    //是不是關鍵幀,關鍵幀和非關鍵幀要區分清楚。推流時也要註明。
    BOOL isKeyFrame = !CFDictionaryContainsKey( (CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0)), kCMSampleAttachmentKey_NotSync);
    
    //首先獲取sps 和pps
    //sps pss 也是h264的一部分,能夠認爲它們是特別的h264視頻幀,保存了h264視頻的一些必要信息。
    //沒有這部分數據h264視頻很難解析出來。
    //數據處理時,sps pps 數據能夠做爲一個普通h264幀,放在h264視頻流的最前面。
    BOOL needSpsPps = NO;
    //這裏判斷一下只取一次sps&pps便可
    if (!encoder.spsPpsData) {
        if (isKeyFrame) {
            //獲取avcC,這就是咱們想要的sps和pps數據。
            //若是保存到文件中,須要將此數據前加上 [0 0 0 1] 4個字節,寫入到h264文件的最前面。
            //若是推流,將此數據放入flv數據區便可。
            CMFormatDescriptionRef sampleBufFormat = CMSampleBufferGetFormatDescription(sampleBuffer);
            NSDictionary *dict = (__bridge NSDictionary *)CMFormatDescriptionGetExtensions(sampleBufFormat);
            encoder.spsPpsData = dict[@"SampleDescriptionExtensionAtoms"][@"avcC"];
        }
        needSpsPps = YES;
    }
    ... ... 
    ... ...
複製代碼

成功獲取sps&pps數據後,可經過aw_sw_x264_encoder.c中的方法aw_encoder_create_sps_pps_tag建立對應的video tag,以後可直接像普通video tag同樣發送。

//建立sps_pps_tag
extern aw_flv_video_tag *aw_encoder_create_sps_pps_tag(aw_data *sps_pps_data){
    //建立普通video tag
    aw_flv_video_tag *sps_pps_tag = aw_sw_encoder_create_flv_video_tag();
    //關鍵幀
    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數據,數據上同真實video tag的h264數據放同一個位置
    sps_pps_tag->config_record_data = copy_aw_data(sps_pps_data);
    //pts寫0
    sps_pps_tag->common_tag.timestamp = 0;
    //數據總長度
    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數據,在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就是想要的數據
    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數據結構很簡單,能夠本身簡單構造一份。可參考AWHWAACEncoder.m中的createAudioSpecificConfigFlvTag函數。

-(aw_flv_audio_tag *)createAudioSpecificConfigFlvTag{
    //AudioSpecificConfig中包含3種元素:profile,sampleRate,channelCount
    //結構是: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數據後,可經過aw_sw_faac_encoder.c中的aw_encoder_create_audio_specific_config_tag來建立對應的flv audio tag,以後可像正常audio tag同樣發送。

extern aw_flv_audio_tag *aw_encoder_create_audio_specific_config_tag(aw_data *audio_specific_config_data, aw_faac_config *faac_config){
    //建立普通的audio tag
    aw_flv_audio_tag *audio_tag = aw_sw_encoder_create_flv_audio_tag(faac_config);
    
    //AudioSpecificConfig數據,同正常的audio tag在相同位置
    audio_tag->config_record_data = copy_aw_data(audio_specific_config_data);
    //時間戳0
    audio_tag->common_tag.timestamp = 0;
    //整個tag長度
    audio_tag->common_tag.data_size = audio_specific_config_data->size + 11 + audio_tag->common_tag.header_size;
    
    return audio_tag;
}
複製代碼

rtmp鏈接成功後,必定要先發送sps&pps和AudioSpecificConfig這兩個數據對應的tag,不然視頻是播放不出來的。

文章列表

  1. 1小時學會:最簡單的iOS直播推流(一)項目介紹
  2. 1小時學會:最簡單的iOS直播推流(二)代碼架構概述
  3. 1小時學會:最簡單的iOS直播推流(三)使用系統接口捕獲音視頻
  4. 1小時學會:最簡單的iOS直播推流(四)如何使用GPUImage,如何美顏
  5. 1小時學會:最簡單的iOS直播推流(五)yuv、pcm數據的介紹和獲取
  6. 1小時學會:最簡單的iOS直播推流(六)h26四、aac、flv介紹
  7. 1小時學會:最簡單的iOS直播推流(七)h264/aac 硬編碼
  8. 1小時學會:最簡單的iOS直播推流(八)h264/aac 軟編碼
  9. 1小時學會:最簡單的iOS直播推流(九)flv 編碼與音視頻時間戳同步
  10. 1小時學會:最簡單的iOS直播推流(十)librtmp使用介紹
  11. 1小時學會:最簡單的iOS直播推流(十一)sps&pps和AudioSpecificConfig介紹(完結)
相關文章
相關標籤/搜索