以live555为例来分析H264码流的打包发送

来源:互联网 发布:欧美电影推荐知乎 编辑:程序博客网 时间:2024/06/04 19:05

H.264 视频 RTP 负载格式

1. 网络抽象层单元类型 (NALU)

NALU 头由一个字节组成, 它的语法如下:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+

F: 1 个比特.
  forbidden_zero_bit. 在 H.264 规范中规定了这一位必须为 0.

NRI: 2 个比特.
  nal_ref_idc. 取 00 ~ 11, 似乎指示这个 NALU 的重要性, 如 00 的 NALU 解码器可以丢弃它而不影响图像的回放. 不过一般情况下不太关心这个属性.

Type: 5 个比特.
  nal_unit_type. 这个 NALU 单元的类型. 简述如下:

  0     没有定义
  1-23  NAL单元  单个 NAL 单元包.
  24    STAP-A   单一时间的组合包
  25    STAP-B   单一时间的组合包
  26    MTAP16   多个时间的组合包
  27    MTAP24   多个时间的组合包
  28    FU-A     分片的单元
  29    FU-B     分片的单元
  30-31 没有定义

2. 打包模式

  下面是 RFC 3550 中规定的 RTP 头的结构.

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |V=2|P|X|  CC   |M|     PT      |       sequence number         |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                           timestamp                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |           synchronization source (SSRC) identifier            |
      +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
      |            contributing source (CSRC) identifiers             |
      |                             ....                              |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

  负载类型 Payload type (PT): 7 bits
  序列号 Sequence number (SN): 16 bits
  时间戳 Timestamp: 32 bits
  
  H.264 Payload 格式定义了三种不同的基本的负载(Payload)结构. 接收端可能通过 RTP Payload 
  的第一个字节来识别它们. 这一个字节类似 NALU 头的格式, 而这个头结构的 NAL 单元类型字段
  则指出了代表的是哪一种结构,

  这个字节的结构如下, 可以看出它和 H.264 的 NALU 头结构是一样的.
      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+
  字段 Type: 这个 RTP payload 中 NAL 单元的类型. 这个字段和 H.264 中类型字段的区别是, 当 type
  的值为 24 ~ 31 表示这是一个特别格式的 NAL 单元, 而 H.264 中, 只取 1~23 是有效的值.
   
  24    STAP-A   单一时间的组合包
  25    STAP-B   单一时间的组合包
  26    MTAP16   多个时间的组合包
  27    MTAP24   多个时间的组合包
  28    FU-A     分片的单元
  29    FU-B     分片的单元
  30-31 没有定义

  可能的结构类型分别有:

  1. 单一 NAL 单元模式
     即一个 RTP 包仅由一个完整的 NALU 组成. 这种情况下 RTP NAL 头类型字段和原始的 H.264的
  NALU 头类型字段是一样的.

  2. 组合封包模式
    即可能是由多个 NAL 单元组成一个 RTP 包. 分别有4种组合方式: STAP-A, STAP-B, MTAP16, MTAP24.
  那么这里的类型值分别是 24, 25, 26 以及 27.

  3. 分片封包模式
    用于把一个 NALU 单元封装成多个 RTP 包. 存在两种类型 FU-A 和 FU-B. 类型值分别是 28 和 29.


2.1 单一 NAL 单元模式

  对于 NALU 的长度小于 MTU 大小的包, 一般采用单一 NAL 单元模式.
  对于一个原始的 H.264 NALU 单元常由 [Start Code] [NALU Header] [NALU Payload] 三部分组成, 其中 Start Code 用于标示这是一个

NALU 单元的开始, 必须是 "00 00 00 01" 或 "00 00 01", NALU 头仅一个字节, 其后都是 NALU 单元内容.
  打包时去除 "00 00 01" 或 "00 00 00 01" 的开始码, 把其他数据封包的 RTP 包即可.

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |F|NRI|  type   |                                               |
      +-+-+-+-+-+-+-+-+                                               |
      |                                                               |
      |               Bytes 2..n of a Single NAL unit                 |
      |                                                               |
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


  如有一个 H.264 的 NALU 是这样的:

  [00 00 00 01 67 42 A0 1E 23 56 0E 2F ... ]

  这是一个序列参数集 NAL 单元. [00 00 00 01] 是四个字节的开始码, 67 是 NALU 头, 42 开始的数据是 NALU 内容.

  封装成 RTP 包将如下:

  [ RTP Header ] [ 67 42 A0 1E 23 56 0E 2F ]

  即只要去掉 4 个字节的开始码就可以了.


2.2 组合封包模式

  其次, 当 NALU 的长度特别小时, 可以把几个 NALU 单元封在一个 RTP 包中.
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                          RTP Header                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |STAP-A NAL HDR |         NALU 1 Size           | NALU 1 HDR    |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         NALU 1 Data                           |
      :                                                               :
      +               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |               | NALU 2 Size                   | NALU 2 HDR    |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         NALU 2 Data                           |
      :                                                               :
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


2.3 Fragmentation Units (FUs).

  而当 NALU 的长度超过 MTU 时, 就必须对 NALU 单元进行分片封包. 也称为 Fragmentation Units (FUs).
  
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      | FU indicator  |   FU header   |                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
      |                                                               |
      |                         FU payload                            |
      |                                                               |
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

      Figure 14.  RTP payload format for FU-A

   The FU indicator octet has the following format:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+

   The FU header has the following format:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |S|E|R|  Type   |
      +---------------+


3. SDP 参数

  下面描述了如何在 SDP 中表示一个 H.264 流:

  . "m=" 行中的媒体名必须是 "video"
  . "a=rtpmap" 行中的编码名称必须是 "H264".
  . "a=rtpmap" 行中的时钟频率必须是 90000.
  . 其他参数都包括在 "a=fmtp" 行中.

  如:

  m=video 49170 RTP/AVP 98
  a=rtpmap:98 H264/90000
  a=fmtp:98 profile-level-id=42A01E; sprop-parameter-sets=Z0IACpZTBYmI,aMljiA==

  下面介绍一些常用的参数.

3.1 packetization-mode:
  表示支持的封包模式. 
  当 packetization-mode 的值为 0 时或不存在时, 必须使用单一 NALU 单元模式.
  当 packetization-mode 的值为 1 时必须使用非交错(non-interleaved)封包模式.
  当 packetization-mode 的值为 2 时必须使用交错(interleaved)封包模式.
  这个参数不可以取其他的值.

3.2 sprop-parameter-sets:
  这个参数可以用于传输 H.264 的序列参数集和图像参数 NAL 单元. 这个参数的值采用 Base64 进行编码. 不同的参数集间用","号隔开.
  
3.3 profile-level-id:
  这个参数用于指示 H.264 流的 profile 类型和级别. 由 Base16(十六进制) 表示的 3 个字节. 第一个字节表示 H.264 的 Profile 类型, 第

三个字节表示 H.264 的 Profile 级别:
  
3.4 max-mbps:
  这个参数的值是一个整型, 指出了每一秒最大的宏块处理速度.


live555的打包发送

现有的H264VideoFileServerMediaSubsession中,sink使用了H264VideoRTPSink,source使用了H264VideoStreamFramer,然而这个连接是很复杂的,在这两个节点间要插入了很多其它的节点,其实际情况是这样的:
ByteStreamFileSource-->H264VideoStreamParser-->H264VideoStreamFramer-->H264FUAFragmenter-->H264VideoRTPSink.类的作用如下:
ByteStreamFileSource:264数据源。
H264VideoStreamParser:用来获取BUF。
H264VideoStreamFramer:获取一帧数据。
H264FUAFragmenter:分包。
H264VideoRTPSink:得到包数据。
 参考:http://blog.csdn.net/nkmnkm/article/details/7212181

代码:

下面是LIVE555打包H264的代码

[cpp] view plain copy
  1. void H264FUAFragmenter::doGetNextFrame() {  
  2.   if (fNumValidDataBytes == 1) {  
  3.     // We have no NAL unit data currently in the buffer.  Read a new one:  
  4.     fInputSource->getNextFrame(&fInputBuffer[1], fInputBufferSize - 1,  
  5.                    afterGettingFrame, this,  
  6.                    FramedSource::handleClosure, this);  
  7.   } else {  
  8.     // We have NAL unit data in the buffer.  There are three cases to consider:  
  9.     // 1. There is a new NAL unit in the buffer, and it's small enough to deliver  
  10.     //    to the RTP sink (as is). 小包  
  11.     // 2. There is a new NAL unit in the buffer, but it's too large to deliver to  
  12.     //    the RTP sink in its entirety.  Deliver the first fragment of this data,  
  13.     //    as a FU-A packet, with one extra preceding header byte. 大包的头包  
  14.     // 3. There is a NAL unit in the buffer, and we've already delivered some  
  15.     //    fragment(s) of this.  Deliver the next fragment of this data,  
  16.     //    as a FU-A packet, with two extra preceding header bytes. 大包的非头包  
  17.   
  18.     if (fMaxSize < fMaxOutputPacketSize) { // shouldn't happen  
  19.       envir() << "H264FUAFragmenter::doGetNextFrame(): fMaxSize ("  
  20.           << fMaxSize << ") is smaller than expected\n";  
  21.     } else {  
  22.       fMaxSize = fMaxOutputPacketSize;  
  23.     }  
  24.   
  25.     fLastFragmentCompletedNALUnit = True; // by default  
  26.     if (fCurDataOffset == 1) { // case 1 or 2  
  27.       if (fNumValidDataBytes - 1 <= fMaxSize) { // case 1  
  28.     memmove(fTo, &fInputBuffer[1], fNumValidDataBytes - 1);  
  29.     fFrameSize = fNumValidDataBytes - 1;  
  30.     fCurDataOffset = fNumValidDataBytes;  
  31.       } else { // case 2  
  32.     // We need to send the NAL unit data as FU-A packets.  Deliver the first  
  33.     // packet now.  Note that we add FU indicator and FU header bytes to the front  
  34.     // of the packet (reusing the existing NAL header byte for the FU header).  
  35.     fInputBuffer[0] = (fInputBuffer[1] & 0xE0) | 28; // FU indicator  
  36.     fInputBuffer[1] = 0x80 | (fInputBuffer[1] & 0x1F); // FU header (with S bit)  
  37.     memmove(fTo, fInputBuffer, fMaxSize);  
  38.     fFrameSize = fMaxSize;  
  39.     fCurDataOffset += fMaxSize - 1;  
  40.     fLastFragmentCompletedNALUnit = False;  
  41.       }  
  42.     } else { // case 3  
  43.       // We are sending this NAL unit data as FU-A packets.  We've already sent the  
  44.       // first packet (fragment).  Now, send the next fragment.  Note that we add  
  45.       // FU indicator and FU header bytes to the front.  (We reuse these bytes that  
  46.       // we already sent for the first fragment, but clear the S bit, and add the E  
  47.       // bit if this is the last fragment.)  
  48.       fInputBuffer[fCurDataOffset-2] = fInputBuffer[0]; // FU indicator  
  49.       fInputBuffer[fCurDataOffset-1] = fInputBuffer[1]&~0x80; // FU header (no S bit)  
  50.       unsigned numBytesToSend = 2 + fNumValidDataBytes - fCurDataOffset;  
  51.       if (numBytesToSend > fMaxSize) {  
  52.     // We can't send all of the remaining data this time:  
  53.     numBytesToSend = fMaxSize;  
  54.     fLastFragmentCompletedNALUnit = False;  
  55.       } else {  
  56.     // This is the last fragment:  
  57.     fInputBuffer[fCurDataOffset-1] |= 0x40; // set the E bit in the FU header  
  58.     fNumTruncatedBytes = fSaveNumTruncatedBytes;  
  59.       }  
  60.       memmove(fTo, &fInputBuffer[fCurDataOffset-2], numBytesToSend);  
  61.       fFrameSize = numBytesToSend;  
  62.       fCurDataOffset += numBytesToSend - 2;  
  63.     }  
  64.   
  65.     if (fCurDataOffset >= fNumValidDataBytes) {  
  66.       // We're done with this data.  Reset the pointers for receiving new data:  
  67.       fNumValidDataBytes = fCurDataOffset = 1;  
  68.     }  
  69.   
  70.     // Complete delivery to the client:  
  71.     FramedSource::afterGetting(this);  
  72.   }  
  73. }  
原创粉丝点击