推送H.264和AAC的重要前提

RTMP的音视频流的封装形式和FLV格式相似, 流媒体服务器向客户端发送包含H264和AAC的RTMP直播流,需要首先发送:

  • AVC sequence header
  • AAC sequence header
    这两个header非常重要,是客户端解码的必需部分.

因为以上两个参数只在客户端开始拉流开始的时候接收一次.
所以当H264参数发生变化时(如分辨率或帧率等)发生变化或AAC音频参数发生变化(采样率等),这时候两个header的内容也会发生变化. 这时候如果不重新拉流,就会出现绿屏或花屏的现象(因为解码参数已经发生变化).
因此这时候应该客户端应该进行重新拉流(重新接受两个header),画面就会恢复正常.

那这两个神奇的header究竟都藏了些什么具体信息呢?

AVC sequence header

 

AVC sequence header & AAC sequence header_nginx-rtmp

AVCDecoderConfigurationRecord


Refer from H.264-AVC-ISO_IEC_14496-15.pdf

 

AVC sequence header & AAC sequence header_nginx-rtmp_02

AVCDecoderConfigurationRecord的一些参数

解码时,解码器通过Profile知道需要准备那些参数, 通过level知道图像可能的最大的分辨率和帧率,通过SPS信息可计算出宽和高(如果信息充分还可计算出帧率信息).

AAC sequence header

AAC中用AudioSpecificConfig结构体来表示AAC sequence header.

AudioSpecificConfig ()
{
audioObjectType = GetAudioObjectType();
samplingFrequencyIndex;
if ( samplingFrequencyIndex == 0xf )
{
samplingFrequency;
}
channelConfiguration;
...
}
Refer from ISO-14496-3(2009-09).pdf

AVC sequence header & AAC sequence header_nginx-rtmp_03

AudioSpecificConfig

Audio Object Types

AVC sequence header & AAC sequence header_nginx-rtmp_04

Audio Object Types部分列表

samplingFrequencyIndex

A four bit field indicating the sampling rate used. If samplingFrequencyIndex equals 15 then the actual sampling rate is signaled directly by the value of samplingFrequency . In all other cases samplingFrequency is set to the value of the corresponding entry in Table 1.18.

通过查询下表可获取实际的采样率, 如samplingFrequencyIndex等于0x5, 查表可知当前的音频采样率为32000.

AVC sequence header & AAC sequence header_nginx-rtmp_05

Sampling Frequency Index

 

samplingFrequency

The sampling frequency used for this audio object. Either transmitted directly, or coded in the form of samplingFrequencyIndex.

音频的采样率. 可用samplingFrequency直接来指定, 或者用samplingFrequencyIndex来指定.

channelConfiguration

用于描述声道信息, 常用的值有2, 立体声双声道.

AVC sequence header & AAC sequence header_nginx-rtmp_06

channelConfiguration