H264和AAC數據流
RTMP推送的音視頻流的封裝形式和FLV格式相似,由此可知,向FMS推送H264和AAC直播流,需要首先發送"AVC sequence header"和"AAC sequence header",這兩項數據包含的是重要的編碼信息,沒有它們,解碼器將無法解碼。
AVC sequence header就是AVCDecoderConfigurationRecord結構,該結構在標準文檔“ISO-14496-15 AVC file format”中有詳細說明。
AAC sequence header存放的是AudioSpecificConfig結構,該結構則在“ISO-14496-3 Audio”中描述。AudioSpecificConfig結構的描述非常複雜,這裏我做一下簡化,事先設定要將要編碼的音頻格式,其中,選擇"AAC-LC"爲音頻編碼,音頻採樣率爲44100,於是AudioSpecificConfig簡化爲下表:
這樣,AVC sequence header和AAC sequence header的內容可以基本確定了,更詳細的信息,大家可以去翻閱相關文檔。
舉例
上圖並非自己畫的,因此遺漏了sequenceParameterSetLength,pictureParameterSetLength
下面藍色的部分就是 FLV 文件中的 AVCDecoderConfigurationRecord 部分:
00000130h: 00 00 00 17 00 00 00 00 01 4D 40 15 FF E1 00 0A ; .........M@.?.
00000140h: 67 4D 40 15 96 53 01 00 4A 20 01 00 05 68 E9 23 ; gM@.朣..J ...h?
00000150h: 88 00 00 00 00 2A 08 00 00 52 00 00 00 00 00 00 ; ?...*...R......
根據 AVCDecoderConfigurationRecord 結構的定義:
- configurationVersion = 01
- AVCProfileIndication = 4D
- profile_compatibility = 40
- AVCLevelIndication = 15
- lengthSizeMinusOne = FF <- 非常重要,是 H.264 視頻中 NALU 的長度,計算方法是 1 + (lengthSizeMinusOne & 3),實際計算結果一直是4
- numOfSequenceParameterSets = E1 <- SPS 的個數,計算方法是 numOfSequenceParameterSets & 0x1F,實際計算結果一直爲1
- sequenceParameterSetLength = 00 0A <- SPS 的長度
- sequenceParameterSetNALUnits = 67 4D 40 15 96 53 01 00 4A 20 <- SPS
- numOfPictureParameterSets = 01 <- PPS 的個數,一直爲1
- pictureParameterSetLength = 00 05 <- PPS 的長度
- pictureParameterSetNALUnits = 68 E9 23 88 00 <- PPS
下面藍色的爲AudioSpecificConfig部分:
000001e0h: 00 00 00 00 00 AF 0011 90 00 00 00 0F 09 00 02; .....?.?......
根據AudioSpecificConfig結構的定義:
- audioObjectType = (0x11 & 0xF8) >> 3 <- 取前5bit,結果爲2
- samplingFrequencyIndex = ((0x11 & 0x7) << 1) | (0x90 >> 7)<- 取後4bit,結果爲3
- channelConfiguration = (0x90 >> 3) & 0x0F <- 取後4bit,結果爲2
- frameLengthFlag = (0x90 >> 2) & 0x01<- 取後1bit,結果爲0
- dependsOnCoreCoder = (0x90 >> 1) & 0x01 <- 取後1bit,結果爲0
- extensionFlag = 0x90 & 0x01 <- 最後1bit,結果始終爲0