H264 RTP打包发送 12323

来源:互联网 发布:js调用代码 编辑:程序博客网 时间:2024/05/16 14:05

声明:这个是根据网上VC程序修改的,NALdecoder.cpp,改成在linux环境下运用。

首先需要了解一下rtp封包的格式

H.264 RTP PAYLOAD 格式


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   单一时间的组合包
  24    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   单一时间的组合包
  24    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   |
      +---------------+

看了上面的解说会对程序理解有很大帮助,

源码文件如下:

[cpp] view plaincopyprint?
  1. // NALDecoder.cpp : Defines the entry point for the console application.  
  2.   
  3.   
  4.   
  5. // h264.h   
  6.   
  7. #include <stdio.h>   
  8.   
  9. #include <stdlib.h>   
  10.   
  11. #include <string.h>   
  12.   
  13. #include <sys/socket.h>   
  14.   
  15. #include <netinet/in.h>   
  16.   
  17. #include <unistd.h>   
  18.   
  19. #include <errno.h>   
  20.   
  21. #include <sys/types.h>   
  22.   
  23. #include <fcntl.h>   
  24.   
  25.   
  26.   
  27.   
  28.   
  29. #define PACKET_BUFFER_END       (unsigned int)0x00000000  
  30.   
  31.   
  32.   
  33.   
  34.   
  35. #define MAX_RTP_PKT_LENGTH      1400   
  36.   
  37.   
  38.   
  39. #define DEST_IP                 "172.19.72.56"  
  40.   
  41. #define DEST_PORT               1234   
  42.   
  43.   
  44.   
  45. #define H264                    96   
  46.   
  47.   
  48.   
  49. typedef struct   
  50.   
  51. {  
  52.   
  53.     /**//* byte 0 */  
  54.   
  55.     unsigned char csrc_len:4;        /**//* expect 0 */  
  56.   
  57.     unsigned char extension:1;        /**//* expect 1, see RTP_OP below */  
  58.   
  59.     unsigned char padding:1;        /**//* expect 0 */  
  60.   
  61.     unsigned char version:2;        /**//* expect 2 */  
  62.   
  63.     /**//* byte 1 */  
  64.   
  65.     unsigned char payload:7;        /**//* RTP_PAYLOAD_RTSP */  
  66.   
  67.     unsigned char marker:1;        /**//* expect 1 */  
  68.   
  69.     /**//* bytes 2, 3 */  
  70.   
  71.     unsigned short seq_no;              
  72.   
  73.     /**//* bytes 4-7 */  
  74.   
  75.     unsigned  long timestamp;          
  76.   
  77.     /**//* bytes 8-11 */  
  78.   
  79.     unsigned long ssrc;            /**//* stream number is used here. */  
  80.   
  81. } RTP_FIXED_HEADER;  
  82.   
  83.   
  84.   
  85. typedef struct {  
  86.   
  87.     //byte 0   
  88.   
  89.     unsigned char TYPE:5;  
  90.   
  91.     unsigned char NRI:2;  
  92.   
  93.     unsigned char F:1;      
  94.   
  95.            
  96.   
  97. } NALU_HEADER; /**//* 1 BYTES */  
  98.   
  99.   
  100.   
  101. typedef struct {  
  102.   
  103.     //byte 0   
  104.   
  105.     unsigned char TYPE:5;  
  106.   
  107.     unsigned char NRI:2;   
  108.   
  109.     unsigned char F:1;      
  110.   
  111.               
  112.   
  113.                
  114.   
  115. } FU_INDICATOR; /**//* 1 BYTES */  
  116.   
  117.   
  118.   
  119. typedef struct {  
  120.   
  121.     //byte 0   
  122.   
  123.     unsigned char TYPE:5;  
  124.   
  125.     unsigned char R:1;  
  126.   
  127.     unsigned char E:1;  
  128.   
  129.     unsigned char S:1;      
  130.   
  131. } FU_HEADER; /**//* 1 BYTES */  
  132.   
  133.   
  134.   
  135.   
  136.   
  137. typedef struct  
  138.   
  139. {  
  140.   
  141.     int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)  
  142.   
  143.     unsigned int len;             //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)  
  144.   
  145.     unsigned int max_size;        //! Nal Unit Buffer size  
  146.   
  147.     int forbidden_bit;            //! should be always FALSE  
  148.   
  149.     int nal_reference_idc;        //! NALU_PRIORITY_xxxx  
  150.   
  151.     int nal_unit_type;            //! NALU_TYPE_xxxx      
  152.   
  153.     char *buf;                    //! contains the first byte followed by the EBSP  
  154.   
  155.     unsigned short lost_packets;  //! true, if packet loss is detected  
  156.   
  157. } NALU_t;  
  158.   
  159.   
  160.   
  161. FILE *bits = NULL;                //!< the bit stream file  
  162.   
  163. static int FindStartCode2 (unsigned char *Buf);//查找开始字符0x000001  
  164.   
  165. static int FindStartCode3 (unsigned char *Buf);//查找开始字符0x00000001  
  166.   
  167.   
  168.   
  169. static int info2=0, info3=0;  
  170.   
  171.   
  172.   
  173. RTP_FIXED_HEADER        *rtp_hdr;  
  174.   
  175.   
  176.   
  177. NALU_HEADER     *nalu_hdr;  
  178.   
  179. FU_INDICATOR    *fu_ind;  
  180.   
  181. FU_HEADER       *fu_hdr;  
  182.   
  183.   
  184.   
  185.   
  186.   
  187. //为NALU_t结构体分配内存空间   
  188.   
  189. NALU_t *AllocNALU(int buffersize)  
  190.   
  191. {  
  192.   
  193.     NALU_t *n;  
  194.   
  195.   
  196.   
  197.     if ((n = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL)  
  198.   
  199.     {  
  200.   
  201.         printf("AllocNALU: n");  
  202.   
  203.         exit(0);  
  204.   
  205.     }  
  206.   
  207.   
  208.   
  209.     n->max_size=buffersize;  
  210.   
  211.   
  212.   
  213.     if ((n->buf = (char*)calloc (buffersize, sizeof (char))) == NULL)  
  214.   
  215.     {  
  216.   
  217.         free (n);  
  218.   
  219.         printf ("AllocNALU: n->buf");  
  220.   
  221.         exit(0);  
  222.   
  223.     }  
  224.   
  225.   
  226.   
  227.     return n;  
  228.   
  229. }  
  230.   
  231.   
  232.   
  233. //释放   
  234.   
  235. void FreeNALU(NALU_t *n)  
  236.   
  237. {  
  238.   
  239.     if (n)  
  240.   
  241.     {  
  242.   
  243.         if (n->buf)  
  244.   
  245.         {  
  246.   
  247.             free(n->buf);  
  248.   
  249.             n->buf=NULL;  
  250.   
  251.         }  
  252.   
  253.         free (n);  
  254.   
  255.     }  
  256.   
  257. }  
  258.   
  259.   
  260.   
  261. //打开二进制文件串流   
  262.   
  263. void OpenBitstreamFile (char *fn)  
  264.   
  265. {  
  266.   
  267.   if (NULL == (bits=fopen(fn, "rb")))  
  268.   
  269.   {  
  270.   
  271.       printf("open file error\n");  
  272.   
  273.       exit(0);  
  274.   
  275.   }  
  276.   
  277. }  
  278.   
  279.   
  280.   
  281. //这个函数输入为一个NAL结构体,主要功能为得到一个完整的NALU并保存在NALU_t的buf中,获取他的长度,填充F,IDC,TYPE位。  
  282.   
  283. //并且返回两个开始字符之间间隔的字节数,即包含有前缀的NALU的长度   
  284.   
  285. int GetAnnexbNALU (NALU_t *nalu)  
  286.   
  287. {  
  288.   
  289.     int pos = 0;  
  290.   
  291.     int StartCodeFound, rewind;  
  292.   
  293.     unsigned char *Buf;  
  294.   
  295.       
  296.   
  297.     if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL)   
  298.   
  299.         printf ("GetAnnexbNALU: Could not allocate Buf memory\n");  
  300.   
  301.   
  302.   
  303.     nalu->startcodeprefix_len=3;//初始化码流序列的开始字符为3个字节  
  304.   
  305.     
  306.   
  307.     if (3 != fread (Buf, 1, 3, bits))//从码流中读3个字节  
  308.   
  309.     {  
  310.   
  311.         free(Buf);  
  312.   
  313.         return 0;  
  314.   
  315.     }  
  316.   
  317.     info2 = FindStartCode2 (Buf);//判断是否为0x000001  
  318.   
  319.     if(info2 != 1) //如果不是,再读一个字节  
  320.   
  321.     {  
  322.   
  323.         if(1 != fread(Buf+3, 1, 1, bits))//读一个字节  
  324.   
  325.         {  
  326.   
  327.             free(Buf);  
  328.   
  329.             return 0;  
  330.   
  331.         }  
  332.   
  333.         info3 = FindStartCode3 (Buf);//判断是否为0x00000001  
  334.   
  335.         if (info3 != 1)//如果不是,返回-1  
  336.   
  337.         {   
  338.   
  339.             free(Buf);  
  340.   
  341.             return -1;  
  342.   
  343.         }  
  344.   
  345.         else   
  346.   
  347.         {  
  348.   
  349.             //如果是0x00000001,得到开始前缀为4个字节   
  350.   
  351.             pos = 4;  
  352.   
  353.             nalu->startcodeprefix_len = 4;  
  354.   
  355.         }  
  356.   
  357.     }  
  358.   
  359.      
  360.   
  361.     else  
  362.   
  363.     {  
  364.   
  365.         //如果是0x000001,得到开始前缀为3个字节   
  366.   
  367.         nalu->startcodeprefix_len = 3;  
  368.   
  369.         pos = 3;  
  370.   
  371.     }  
  372.   
  373.     //查找下一个开始字符的标志位   
  374.   
  375.     StartCodeFound = 0;  
  376.   
  377.     info2 = 0;  
  378.   
  379.     info3 = 0;  
  380.   
  381.     
  382.   
  383.     while (!StartCodeFound)  
  384.   
  385.     {  
  386.   
  387.         if (feof (bits))//判断是否到了文件尾  
  388.   
  389.         {  
  390.   
  391.             nalu->len = (pos-1)-nalu->startcodeprefix_len;  
  392.   
  393.             memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);       
  394.   
  395.             nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit  
  396.   
  397.             nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  
  398.   
  399.             nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  
  400.   
  401.             free(Buf);  
  402.   
  403.             return pos-1;  
  404.   
  405.         }  
  406.   
  407.         Buf[pos++] = fgetc (bits);//读一个字节到BUF中  
  408.   
  409.         info3 = FindStartCode3(&Buf[pos-4]);//判断是否为0x00000001  
  410.   
  411.         if(info3 != 1)  
  412.   
  413.             info2 = FindStartCode2(&Buf[pos-3]);//判断是否为0x000001  
  414.   
  415.         StartCodeFound = (info2 == 1 || info3 == 1);  
  416.   
  417.     }  
  418.   
  419.     
  420.   
  421.   
  422.   
  423.    
  424.   
  425.     // Here, we have found another start code (and read length of startcode bytes more than we should  
  426.   
  427.     // have.  Hence, go back in the file   
  428.   
  429.     rewind = (info3 == 1)? -4 : -3;  
  430.   
  431.   
  432.   
  433.     if (0 != fseek (bits, rewind, SEEK_CUR))//把文件指针指向前一个NALU的末尾  
  434.   
  435.     {  
  436.   
  437.         free(Buf);  
  438.   
  439.         printf("GetAnnexbNALU: Cannot fseek in the bit stream file");  
  440.   
  441.     }  
  442.   
  443.   
  444.   
  445.     // Here the Start code, the complete NALU, and the next start code is in the Buf.    
  446.   
  447.     // The size of Buf is pos, pos+rewind are the number of bytes excluding the next  
  448.   
  449.     // start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code  
  450.   
  451.   
  452.   
  453.     nalu->len = (pos+rewind)-nalu->startcodeprefix_len;   //得到一个NAL单元长度  
  454.   
  455.     memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//拷贝一个完整NALU,不拷贝起始前缀0x000001或0x00000001  
  456.   
  457.     nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit  
  458.   
  459.     nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  
  460.   
  461.     nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  
  462.   
  463.     free(Buf);  
  464.   
  465.   
  466.   
  467.     return (pos+rewind);//返回两个开始字符之间间隔的字节数,即包含有前缀的NALU的长度  
  468.   
  469. }  
  470.   
  471.   
  472.   
  473. //输出NALU长度和TYPE   
  474.   
  475. void dump(NALU_t *n)  
  476.   
  477. {  
  478.   
  479.     if (!n)  
  480.   
  481.         return;  
  482.   
  483.       
  484.   
  485.     printf(" len: %d  ", n->len);  
  486.   
  487.     printf("nal_unit_type: %x\n", n->nal_unit_type);  
  488.   
  489. }  
  490.   
  491.   
  492.   
  493.   
  494.   
  495. int main(int argc, char* argv[])  
  496.   
  497. {  
  498.   
  499.     OpenBitstreamFile("./test.264");//打开264文件,并将文件指针赋给bits,在此修改文件名实现打开别的264文件。  
  500.   
  501.     NALU_t *n;  
  502.   
  503.     char* nalu_payload;    
  504.   
  505.     char sendbuf[1500];  
  506.   
  507.       
  508.   
  509.     unsigned short seq_num =0;  
  510.   
  511.     int bytes=0;  
  512.   
  513.     int sockfd;  
  514.   
  515.     struct sockaddr_in serv_addr;  
  516.   
  517.   
  518.   
  519.   
  520.   
  521.     //int len =sizeof(serv_addr);   
  522.   
  523.     float framerate=15;  
  524.   
  525.     unsigned int timestamp_increse=0,ts_current=0;  
  526.   
  527.     timestamp_increse=(unsigned int)(90000.0 / framerate); //+0.5);  
  528.   
  529.   
  530.   
  531.     if ((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)  
  532.   
  533.     {  
  534.   
  535.         perror("socket");  
  536.   
  537.         exit(1);  
  538.   
  539.     }  
  540.   
  541.   
  542.   
  543.     /*设置socketaddr_in结构体中相关参数*/  
  544.   
  545.     serv_addr.sin_family=AF_INET;  
  546.   
  547.     serv_addr.sin_port=htons(DEST_PORT);            
  548.   
  549.     serv_addr.sin_addr.s_addr=inet_addr(DEST_IP);   
  550.   
  551.   
  552.   
  553.     /*调用connect函数主动发起对服务器端的连接*/  
  554.   
  555.     if(connect(sockfd,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr))== -1)  
  556.   
  557.     {  
  558.   
  559.         perror("connect");  
  560.   
  561.         exit(1);  
  562.   
  563.     }  
  564.   
  565.       
  566.   
  567.     n = AllocNALU(8000000);//为结构体nalu_t及其成员buf分配空间。返回值为指向nalu_t存储空间的指针  
  568.   
  569.       
  570.   
  571.   
  572.   
  573.     while(!feof(bits))   
  574.   
  575.     {  
  576.   
  577.         GetAnnexbNALU(n);//每执行一次,文件的指针指向本次找到的NALU的末尾,下一个位置即为下个NALU的起始码0x000001  
  578.   
  579.         dump(n);//输出NALU长度和TYPE   
  580.   
  581.           
  582.   
  583.         memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此需要ts_current来保存上次的时间戳值  
  584.   
  585.         //rtp固定包头,为12字节,该句将sendbuf[0]的地址赋给rtp_hdr,以后对rtp_hdr的写入操作将直接写入sendbuf。  
  586.   
  587.         rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];   
  588.   
  589.         //设置RTP HEADER£   
  590.   
  591.         rtp_hdr->payload     = H264;  //负载类型号  
  592.   
  593.         rtp_hdr->version     = 2;  //版本号,此版本固定为2  
  594.   
  595.         rtp_hdr->marker    = 0;   //标志位,由具体协议规定其值。  
  596.   
  597.         rtp_hdr->ssrc        = htonl(10);    //随机指定为10,并且在本RTP会话中全局唯一  
  598.   
  599.           
  600.   
  601.         //  当一个NALU小于1400字节的时候,采用一个单RTP包发送   
  602.   
  603.         if(n->len<=1400)  
  604.   
  605.         {     
  606.   
  607.             //设置rtp M 位;   
  608.   
  609.             rtp_hdr->marker=1;  
  610.   
  611.             rtp_hdr->seq_no     = htons(seq_num ++); //序列号,每发送一个RTP包增1  
  612.   
  613.             //设置NALU HEADER,并将这个HEADER填入sendbuf[12]  
  614.   
  615.             nalu_hdr =(NALU_HEADER*)&sendbuf[12]; //将sendbuf[12]的地址赋给nalu_hdr,之后对nalu_hdr的写入就将写入sendbuf中;  
  616.   
  617.             nalu_hdr->F=n->forbidden_bit;  
  618.   
  619.             nalu_hdr->NRI=n->nal_reference_idc>>5;//有效数据在n->nal_reference_idc的第6,7位,需要右移5位才能将其值赋给nalu_hdr->NRI。  
  620.   
  621.             nalu_hdr->TYPE=n->nal_unit_type;  
  622.   
  623.   
  624.   
  625.             nalu_payload=&sendbuf[13];//同理将sendbuf[13]赋给nalu_payload  
  626.   
  627.             memcpy(nalu_payload,n->buf+1,n->len-1);//去掉nalu头的nalu剩余内容写入sendbuf[13]开始的字符串。  
  628.   
  629.           
  630.   
  631.             ts_current=ts_current+timestamp_increse;  
  632.   
  633.             rtp_hdr->timestamp=htonl(ts_current);  
  634.   
  635.             bytes=n->len + 12 ;                      //获得sendbuf的长度,为nalu的长度(包含NALU头但除去起始前缀)加上rtp_header的固定长度12字节  
  636.   
  637.             send( sockfd, sendbuf, bytes, 0 );//发送rtp包  
  638.   
  639.             //  Sleep(100);   
  640.   
  641.               
  642.   
  643.         }  
  644.   
  645.           
  646.   
  647.         else if(n->len>1400)  
  648.   
  649.         {  
  650.   
  651.             //得到该nalu需要用多少长度为1400字节的RTP包来发送  
  652.   
  653.             int k=0,l=0;  
  654.   
  655.             k=n->len/1400;//需要k个1400字节的RTP包  
  656.   
  657.             l=n->len%1400;//最后一个RTP包的需要装载的字节数  
  658.   
  659.             int t=0;//用于指示当前发送的是第几个分片RTP包  
  660.   
  661.             ts_current=ts_current+timestamp_increse;  
  662.   
  663.             rtp_hdr->timestamp=htonl(ts_current);  
  664.   
  665.             while(t<=k)  
  666.   
  667.             {  
  668.   
  669.                 rtp_hdr->seq_no = htons(seq_num ++); //序列号,每发送一个RTP包增1  
  670.   
  671.                 if(!t)//发送一个需要分片的NALU的第一个分片,置FU HEADER的S位  
  672.   
  673.                 {  
  674.   
  675.                     //设置rtp M 位;   
  676.   
  677.                     rtp_hdr->marker=0;  
  678.   
  679.                     //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]  
  680.   
  681.                     fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind,之后对fu_ind的写入就将写入sendbuf中;  
  682.   
  683.                     fu_ind->F=n->forbidden_bit;  
  684.   
  685.                     fu_ind->NRI=n->nal_reference_idc>>5;  
  686.   
  687.                     fu_ind->TYPE=28;  
  688.   
  689.                       
  690.   
  691.                     //设置FU HEADER,并将这个HEADER填入sendbuf[13]  
  692.   
  693.                     fu_hdr =(FU_HEADER*)&sendbuf[13];  
  694.   
  695.                     fu_hdr->E=0;  
  696.   
  697.                     fu_hdr->R=0;  
  698.   
  699.                     fu_hdr->S=1;  
  700.   
  701.                     fu_hdr->TYPE=n->nal_unit_type;  
  702.   
  703.                       
  704.   
  705.                   
  706.   
  707.                     nalu_payload=&sendbuf[14];//同理将sendbuf[14]赋给nalu_payload  
  708.   
  709.                     memcpy(nalu_payload,n->buf+1,1400);//去掉NALU头  
  710.   
  711.                       
  712.   
  713.                     bytes=1400+14;                      //获得sendbuf的长度,为nalu的长度(除去起始前缀和NALU头)加上rtp_header,fu_ind,fu_hdr的固定长度14字节  
  714.   
  715.                     send( sockfd, sendbuf, bytes, 0 );//发送rtp包  
  716.   
  717.                     t++;  
  718.   
  719.                       
  720.   
  721.                 }  
  722.   
  723.                 //发送一个需要分片的NALU的非第一个分片,清零FU HEADER的S位,如果该分片是该NALU的最后一个分片,置FU HEADER的E位  
  724.   
  725.                 else if(k==t)//发送的是最后一个分片,注意最后一个分片的长度可能超过1400字节(当l>1386时)。  
  726.   
  727.                 {  
  728.   
  729.                       
  730.   
  731.                     //设置rtp M 位;当前传输的是最后一个分片时该位置1  
  732.   
  733.                     rtp_hdr->marker=1;  
  734.   
  735.                     //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]  
  736.   
  737.                     fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind,之后对fu_ind的写入就将写入sendbuf中;  
  738.   
  739.                     fu_ind->F=n->forbidden_bit;  
  740.   
  741.                     fu_ind->NRI=n->nal_reference_idc>>5;  
  742.   
  743.                     fu_ind->TYPE=28;  
  744.   
  745.                           
  746.   
  747.                     //设置FU HEADER,并将这个HEADER填入sendbuf[13]  
  748.   
  749.                     fu_hdr =(FU_HEADER*)&sendbuf[13];  
  750.   
  751.                     fu_hdr->R=0;  
  752.   
  753.                     fu_hdr->S=0;  
  754.   
  755.                     fu_hdr->TYPE=n->nal_unit_type;  
  756.   
  757.                     fu_hdr->E=1;  
  758.   
  759.   
  760.   
  761.                     nalu_payload=&sendbuf[14];//同理将sendbuf[14]的地址赋给nalu_payload  
  762.   
  763.                     memcpy(nalu_payload,n->buf+t*1400+1,l-1);//将nalu最后剩余的l-1(去掉了一个字节的NALU头)字节内容写入sendbuf[14]开始的字符串。  
  764.   
  765.                     bytes=l-1+14;       //获得sendbuf的长度,为剩余nalu的长度l-1加上rtp_header,FU_INDICATOR,FU_HEADER三个包头共14字节  
  766.   
  767.                     send( sockfd, sendbuf, bytes, 0 );//发送rtp包  
  768.   
  769.                     t++;  
  770.   
  771.                 //  Sleep(100);   
  772.   
  773.                 }  
  774.   
  775.                 else if(t<k&&0!=t)  
  776.   
  777.                 {  
  778.   
  779.                     //设置rtp M 位;   
  780.   
  781.                     rtp_hdr->marker=0;  
  782.   
  783.                     //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]  
  784.   
  785.                     fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind,之后对fu_ind的写入就将写入sendbuf中;  
  786.   
  787.                     fu_ind->F=n->forbidden_bit;  
  788.   
  789.                     fu_ind->NRI=n->nal_reference_idc>>5;  
  790.   
  791.                     fu_ind->TYPE=28;  
  792.   
  793.                           
  794.   
  795.                     //设置FU HEADER,并将这个HEADER填入sendbuf[13]  
  796.   
  797.                     fu_hdr =(FU_HEADER*)&sendbuf[13];  
  798.   
  799.                     //fu_hdr->E=0;   
  800.   
  801.                     fu_hdr->R=0;  
  802.   
  803.                     fu_hdr->S=0;  
  804.   
  805.                     fu_hdr->E=0;  
  806.   
  807.                     fu_hdr->TYPE=n->nal_unit_type;  
  808.   
  809.                   
  810.   
  811.                     nalu_payload=&sendbuf[14];//同理将sendbuf[14]的地址赋给nalu_payload  
  812.   
  813.                     memcpy(nalu_payload,n->buf+t*1400+1,1400);//去掉起始前缀的nalu剩余内容写入sendbuf[14]开始的字符串。  
  814.   
  815.                     bytes=1400+14;                      //获得sendbuf的长度,为nalu的长度(除去原NALU头)加上rtp_header,fu_ind,fu_hdr的固定长度14字节  
  816.   
  817.                     send( sockfd, sendbuf, bytes, 0 );//发送rtp包  
  818.   
  819.                     t++;  
  820.   
  821.                 }  
  822.   
  823.             }  
  824.   
  825.         }  
  826.   
  827.     }  
  828.   
  829.     FreeNALU(n);  
  830.   
  831.     return 0;  
  832.   
  833. }  
  834.   
  835.   
  836.   
  837. static int FindStartCode2 (unsigned char *Buf)  
  838.   
  839. {  
  840.   
  841.     if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1)   
  842.   
  843.         return 0; //判断是否为0x000001,如果是返回1  
  844.   
  845.     else   
  846.   
  847.         return 1;  
  848.   
  849. }  
  850.   
  851.   
  852.   
  853. static int FindStartCode3 (unsigned char *Buf)  
  854.   
  855. {  
  856.   
  857.     if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1)   
  858.   
  859.         return 0;//判断是否为0x00000001,如果是返回1  
  860.   
  861.     else   
  862.   
  863.         return 1;  
  864.   
  865. }  

在56的客户端上打开vlc,加载sdp文件即可看到h264经过rtp打包的视频数据

sdp文件内容如下:

[cpp] view plaincopyprint?
  1. m=video 1234 RTP/AVP 96  
  2. a=rtpmap:96 H264  
  3. a=framerate:15  
  4. c=IN IP4 172.19.72.56  
原创粉丝点击