//h264视频流打包代码



// NALDecoder.cpp : Defines the entry point for the console application.



#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <memory.h>

#include "h264.h"

#include "initsock.h"



CInitSock initSock;  // 初始化Winsock库



//为NALU_t结构体分配内存空间



NALU_t *AllocNALU(int buffersize)

{

 NALU_t *pNalu;

 if ((pNalu = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL) {

  printf("AllocNALU: Nalu");

  exit(0);

 }



 pNalu->max_size=buffersize;

 if ((pNalu->buf = (char*)calloc (buffersize, sizeof (char))) == NULL) {

  free (pNalu);

  printf ("AllocNALU: Nalu->buf");

  exit(0);

 }

 return pNalu;

}



//释放

void FreeNALU(NALU_t *pNalu)

{

 if (pNalu) {

  if (pNalu->buf) {

   free(pNalu->buf);

   pNalu->buf=NULL;

  }

  free (pNalu);

 }

}



static int FindStartCode2 (unsigned char *Buf)

{

 if(Buf[0]!=0 Buf[1]!=0 Buf[2] !=1) return 0; //推断是否为0x000001,假设是返回1

 else return 1;

}



static int FindStartCode3 (unsigned char *Buf)

{

 if(Buf[0]!=0 Buf[1]!=0 Buf[2] !=0 Buf[3] !=1) return 0;//推断是否为0x00000001,假设是返回1

 else return 1;

}



// 这个函数输入为一个NAL结构体。主要功能为得到一个完整的NALU并保存在NALU_t的buf中,获取他的长度。填充F,IDC,TYPE位。

// 而且返回两个開始字符之间间隔的字节数,即包括有前缀的NALU的长度

int GetAnnexbNALU (NALU_t *pNalu, FILE *bits)

{

 int info2=0, info3=0;

 int pos = 0;

 int StartCodeFound, rewind;

 unsigned char *Buf;



 if ((Buf = (unsigned char*)calloc (pNalu->max_size , sizeof(char))) == NULL)

  printf ("GetAnnexbNALU: Could not allocate Buf memory\n");

  

 if (3 != fread (Buf, 1, 3, bits)) {  //从码流中读3个字节

  free(Buf);

  return -1;

    }

 if (Buf[0]!=0 Buf[1]!=0) {

  free(Buf);

  return -1;

 }

 if (Buf[2]==1) {

  pNalu->startcodeprefix_len=3;   //初始化码流序列的開始字符为3个字节

  pos =3;

 }else {

  if (1 != fread (Buf+3, 1, 1, bits)) {  //从码流中读1个字节

   free(Buf);

   return -1;

  }

  if (Buf[2]!=0 Buf[3]!=1) {

   free(Buf);

   return -1;

  }

  pos = 4;

  pNalu->startcodeprefix_len = 4;

 }



 //查找下一个開始字符的标志位

 StartCodeFound = 0;

 info2 = 0;

 info3 = 0;

    while (!StartCodeFound)  {

  if (feof (bits)) { //推断是否到了文件尾

   break;

  }

  Buf[pos++] = fgetc (bits);//读一个字节到BUF中

  info3 = FindStartCode3(&Buf[pos-4]);//推断是否为0x00000001

  if(info3 != 1)

   info2 = FindStartCode2(&Buf[pos-3]);//推断是否为0x000001

  StartCodeFound =(info2 info3);

 }

 if (StartCodeFound) {

  // Here, we have found another start code (and read length of startcode bytes more than we should

  // have.  Hence, go back in the file

  rewind = (info3 == 1)?

-4 : -3;

  if (0 != fseek (bits, rewind, SEEK_CUR)) { // 把文件指针指向前一个NALU的末尾

   free(Buf);

   printf("GetAnnexbNALU: Cannot fseek in the bit stream file");

  }

 

 } else {

  rewind = -1;

 }



 // Here the Start code, the complete NALU, and the next start code is in the Buf. 

 // The size of Buf is pos, pos+rewind are the number of bytes excluding the next

 // start code, and (pos+rewind)-startcodeprefix_len is the size of the NALU excluding the start code

 pNalu->len = (pos+rewind)-pNalu->startcodeprefix_len;

 //拷贝一个完整NALU。不拷贝起始前缀0x000001或0x00000001

 memcpy (pNalu->buf, &Buf[pNalu->startcodeprefix_len], pNalu->len);

 pNalu->forbidden_bit = pNalu->buf[0] & 0x80; //1 bit

 pNalu->nal_reference_idc = pNalu->buf[0] & 0x60; // 2 bit

 pNalu->nal_unit_type = (pNalu->buf[0]) & 0x1f;// 5 bit

 free(Buf);

 return (pos+rewind);  //返回两个開始字符之间间隔的字节数,即包括有前缀的NALU的长度

}



//输出NALU长度和TYPE

void dump(NALU_t *pNalu)

{

 if (!pNalu) return;

 printf(" len: %d  ", pNalu->len);

 printf("nal_unit_type: %x\n", pNalu->nal_unit_type);

}



int main(int argc, char* argv[])

{

 NALU_t *pNalu;

 char* nalu_payload; 

 char sendbuf[1500];

 RTP_FIXED_HEADER *rtp_hdr;



 NALU_HEADER  *nalu_hdr;

 FU_INDICATOR *fu_ind;

 FU_HEADER  *fu_hdr;

 

 unsigned short seq_num =0;

 int bytes=0;

 FILE *bits;     //!< the bit stream file



    // 打开264文件,在此改动文件名称实现打开别的264文件。

if ((bits=fopen("./test.264", "rb"))==NULL) {

  printf("open file error\n");

  exit(1);

 }



SOCKET    socket1;

struct sockaddr_in server;

int len =sizeof(server);

server.sin_family=AF_INET;

server.sin_port=htons(DEST_PORT);   // 播放器的端口号

server.sin_addr.s_addr=inet_addr(DEST_IP);  // 播放器的IP地址

socket1=socket(AF_INET,SOCK_DGRAM,0);  // 建立UDP套接字

connect(socket1, (const sockaddr *)&server, len) ; // 与播放器建立连接



float framerate=15;

unsigned int timestamp_increse=0, ts_current=0;

timestamp_increse=(unsigned int)(90000.0 / framerate);  //+0.5); 90000 =  ??

pNalu = AllocNALU(8000000);//为结构体nalu_t及其成员buf分配空间。返回值为指向nalu_t存储空间的指针  ??

while(!feof(bits))

{

  GetAnnexbNALU(pNalu,bits);//每运行一次,文件的指针指向本次找到的NALU的末尾。下一个位置即为下个NALU的起始码0x000001

  dump(pNalu);  //输出NALU长度和TYPE

  

  memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此须要ts_current来保存上次的时间戳值

  // rtp固定包头,为12字节,该句将sendbuf[0]的地址赋给rtp_hdr,以后对rtp_hdr的写入操作将直接写入sendbuf。

rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];

  //设置RTP HEADER。

  rtp_hdr->payload   = H264;  //负载类型号,

  rtp_hdr->version   = 2;   //版本号号。此版本号固定为2

  rtp_hdr->marker    = 0;   //标志位,由详细协议规定其值。

 rtp_hdr->ssrc      = htonl(10); //随机指定为10,而且在本RTP会话中全局唯一

  

  // 当一个NALU小于1400字节的时候,採用一个单RTP包发送

  if(pNalu->len<=1400) { 

   //设置rtp M 位。

   rtp_hdr->marker=1;

   rtp_hdr->seq_no = htons(seq_num ++); //序列号,每发送一个RTP包增1

   //设置NALU HEADER,并将这个HEADER填入sendbuf[12]

   nalu_hdr =(NALU_HEADER*)&sendbuf[12]; //将sendbuf[12]的地址赋给nalu_hdr,之后对nalu_hdr的写入就将写入sendbuf中。

   nalu_hdr->F=pNalu->forbidden_bit;

   nalu_hdr->NRI=pNalu->nal_reference_idc>>5;//有效数据在n->nal_reference_idc的第6。7位,须要右移5位才干将其值赋给nalu_hdr->NRI。

   nalu_hdr->TYPE=pNalu->nal_unit_type;



   nalu_payload=&sendbuf[13];//同理将sendbuf[13]赋给nalu_payload

   memcpy(nalu_payload,pNalu->buf+1,pNalu->len-1);//去掉nalu头的nalu剩余内容写入sendbuf[13]開始的字符串。

ts_current=ts_current+timestamp_increse;

   rtp_hdr->timestamp=htonl(ts_current);

   bytes=pNalu->len + 12 ;     //获得sendbuf的长度,为nalu的长度(包括NALU头但除去起始前缀)加上rtp_header的固定长度12字节

   send(socket1, sendbuf, bytes, 0 );  //发送rtp包

   // Sleep(100);   

  } else {   

   //得到该nalu须要用多少长度为1400字节的RTP包来发送

   int k=0, l=0;

   k=pNalu->len/1400; //须要k个1400字节的RTP包

   l=pNalu->len%1400; //最后一个RTP包的须要装载的字节数

   int t=0;//用于指示当前发送的是第几个分片RTP包

   ts_current=ts_current+timestamp_increse;

   rtp_hdr->timestamp=htonl(ts_current);

   while(t<=k) {

    rtp_hdr->seq_no = htons(seq_num ++); //序列号,每发送一个RTP包增1

    if(!t) //发送一个须要分片的NALU的第一个分片,置FU HEADER的S位

    {

     //设置rtp M 位;

     rtp_hdr->marker=0;

     //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]

     fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind,之后对fu_ind的写入就将写入sendbuf中;

     fu_ind->F=pNalu->forbidden_bit;

     fu_ind->NRI=pNalu->nal_reference_idc>>5;

     fu_ind->TYPE=28;     

     //设置FU HEADER,并将这个HEADER填入sendbuf[13]

     fu_hdr =(FU_HEADER*)&sendbuf[13];

     fu_hdr->E=0;

     fu_hdr->R=0;

     fu_hdr->S=1;

     fu_hdr->TYPE=pNalu->nal_unit_type;     

    

     nalu_payload=&sendbuf[14];//同理将sendbuf[14]赋给nalu_payload

     memcpy(nalu_payload,pNalu->buf+1,1400);//去掉NALU头

     

     bytes=1400+14;      //获得sendbuf的长度,为nalu的长度(除去起始前缀和NALU头)加上rtp_header,fu_ind,fu_hdr的固定长度14字节

     send(socket1, sendbuf, bytes, 0 );//发送rtp包

     t++;     

    } else {

     if(k==t) { // 发送最后一个零头,清零FU HEADER的S位,置FU HEADER的E位.注意最后一个分片的长度

 // 可能超过1400字节(当l>1386时)。      

 // 设置rtp M 位;当前传输的是最后一个分片时该位置1

      rtp_hdr->marker=1;

      //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]

      fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind,之后对fu_ind的写入就将写入sendbuf中;

      fu_ind->F=pNalu->forbidden_bit;

      fu_ind->NRI=pNalu->nal_reference_idc>>5;

      fu_ind->TYPE=28;

      

      //设置FU HEADER,并将这个HEADER填入sendbuf[13]

      fu_hdr =(FU_HEADER*)&sendbuf[13];

      fu_hdr->R=0;

      fu_hdr->S=0;

      fu_hdr->TYPE=pNalu->nal_unit_type;

      fu_hdr->E=1;



      nalu_payload=&sendbuf[14];//同理将sendbuf[14]的地址赋给nalu_payload

      memcpy(nalu_payload,pNalu->buf+t*1400+1,l-1);//将nalu最后剩余的l-1(去掉了一个字节的NALU头)字节内容写入sendbuf[14]開始的字符串。

      bytes=l-1+14;  //获得sendbuf的长度,为剩余nalu的长度l-1加上rtp_header,FU_INDICATOR,FU_HEADER三个包头共14字节

      send(socket1, sendbuf, bytes, 0 );//发送rtp包

      t++;

      // Sleep(100);

     }else {

      if(t<k) {  // 发送其它整块(1400)

       //设置rtp M 位;

       rtp_hdr->marker=0;

       //设置FU INDICATOR,并将这个HEADER填入sendbuf[12]

       fu_ind =(FU_INDICATOR*)&sendbuf[12]; //将sendbuf[12]的地址赋给fu_ind。之后对fu_ind的写入就将写入sendbuf中;

       fu_ind->F=pNalu->forbidden_bit;

       fu_ind->NRI=pNalu->nal_reference_idc>>5;

       fu_ind->TYPE=28;

      

       //设置FU HEADER,并将这个HEADER填入sendbuf[13]

       fu_hdr =(FU_HEADER*)&sendbuf[13];

       //fu_hdr->E=0;

       fu_hdr->R=0;

       fu_hdr->S=0;

       fu_hdr->E=0;

       fu_hdr->TYPE=pNalu->nal_unit_type;

    

       nalu_payload=&sendbuf[14];//同理将sendbuf[14]的地址赋给nalu_payload

       memcpy(nalu_payload,pNalu->buf+t*1400+1,1400);//去掉起始前缀的nalu剩余内容写入sendbuf[14]開始的字符串。

       bytes=1400+14;      //获得sendbuf的长度,为nalu的长度(除去原NALU头)加上rtp_header。fu_ind,fu_hdr的固定长度14字节

       send( socket1, sendbuf, bytes, 0 );//发送rtp包

       t++;

      }

     }

    }

   }

  }

 }

 FreeNALU(pNalu);

 return 0;

}



//--------------------------------------------



//以下为音频



memset(sendbuf,0,MAX_RTP_PKT_LENGTH );//清空sendbuf。此时会将上次的时间戳清空,因此须要ts_current来保存上次的时间戳值

//rtp固定包头。为12字节,该句将sendbuf[0]的地址赋给rtp_hdr,以后对rtp_hdr的写入操作将直接写入sendbuf。

rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];

rtp_hdr->payload     = AAC;  //负载类型号,

rtp_hdr->version     = 2;  //版本号号,此版本号固定为2

rtp_hdr->marker    = 1;   //标志位,由详细协议规定其值。

rtp_hdr->ssrc = htonl(10);    //随机指定为10,而且在本RTP会话中全局唯一

rtp_hdr->seq_no     = htons(seq_num ++); //序列号。每发送一个RTP包增1   



rc = mp4ff_read_sample(infile,track,sampleId,&buffer,(unsigned int *)&buffer_size);//FAAD里自带的 MP4FF读取 Sample

memset(buff,0,MAX_RTP_PKT_LENGTH);//清空sample buff

memcpy(buff,buffer,buffer_size);、、把取到的buffer 放入 buff中



latm_hdr = (LATM_HEADER*)&sendbuf[12];

latm_hdr->p0 = 0x00;

latm_hdr->p1 = 0x10;

latm_hdr->p2 = (rc & 0x1fe0) >> 5; 

latm_hdr->p3 = (rc & 0x1f) << 3; 

ts_current=ts_current+timestamp_increse;

rtp_hdr->timestamp=htonl(ts_current);

memcpy(&sendbuf[16],buff,len);

send( socket1, sendbuf, rc+16, 0 );//发送rtp包

printf("samplesId:%d rc:%d ts:%ld\n",sampleId,rc,rtp_hdr->timestamp);



上述RTP_FIXED_HEADER结构



// 定义RTP固定头结构

typedef struct

{

    /* byte 0 */

    unsigned char csrc_len:4;       /* CSRC 计数4位 */

    unsigned char extension:1;      /* 扩展1位 */

    unsigned char padding:1; /* 填充1位 */

    unsigned char version:2; /* 版本号2位 */



    /* byte 1 */

    unsigned char payload:7; /* 负载类型 */

    unsigned char marker:1;  /* 标志1位 */



    /* bytes 2, 3 */

    unsigned short seq_no; 

 

    /* bytes 4-7 */

    unsigned  long timestamp; 

 

    /* bytes 8-11 */

    unsigned long ssrc;  /* 事实上,它是一个随机生成ID,他表示RTP联系。在申请时。确保ID唯一的就可以了。

*/

} RTP_FIXED_HEADER;

最近做RTSP流媒体的实时广播节目的更多相关文章

  1. 使用vlc播放器做rtsp流媒体服务器

    可参考: 使用vlc播放器播放rtsp视频 web网页中使用vlc插件播放相机rtsp流视频 使用vlc进行二次开发做自己的播放器 首先需要安装vlc播放器,下载及安装步骤略 使用vlc播放器做rts ...

  2. EasyDSS流媒体视频实时回传与录像管理解决方案

    一.背景 1.1 方案背景 随着互联网基础设施建设的不断完善和发展,带宽的不断提速,尤其是光纤入户,4G/5G/NB-IoT各种技术的大规模商用,视频在各行各业越来越受到重视,无论是传统的视频媒体转向 ...

  3. Android、iOS平台RTMP/RTSP播放器实时音量调节

    介绍移动端RTMP.RTSP播放器实时音量调节之前,我们之前也写过,为什么windows播放端加这样的接口,windows端播放器在多窗口大屏显示的场景下尤其需要,尽管我们老早就有了实时静音接口,相对 ...

  4. (转)CentOS6.5安装Darwin Streaming Server搭建RTSP流媒体服务器

    参考: 1,CentOS6.5安装Darwin Streaming Server搭建RTSP流媒体服务器 http://www.yimiju.com/articles/567.html

  5. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器功能简介---实时数据统计报表、视频文件上传、点播、分享、集成

    熟悉EasyDSS流媒体服务器的小伙伴应该都知道,EasyDSS通过将EasyRTMP推流的直播流进行直播转码.智能处理.视频分发,再通过 CDN 分发节点分发到终端播放 SDK为观众播放高清低延时的 ...

  6. 在做RTSP摄像机H5无插件直播中遇到的对接海康摄像机发送OPTIONS心跳时遇到的坑

    我们在实现一套EasyNVR无插件直播方案时,选择了采用厂家无关化的通用协议RTSP/Onvif接入摄像机IPC/NVR设备,总所周知,Onvif是摄像机的发现与控制管理协议,Onvif用到的流媒体协 ...

  7. 流媒体与实时计算,Netflix公司Druid应用实践

    Netflix(Nasdaq NFLX),也就是网飞公司,成立于1997年,是一家在线影片[租赁]提供商,主要提供Netflix超大数量的[DVD]并免费递送,总部位于美国加利福尼亚州洛斯盖图.199 ...

  8. rtsp 流媒体服务器,播放器

    https://github.com/EasyDSS/EasyPlayer-RTSP-Android EasyPlayer EasyPlayer RTSP Android 播放器是由紫鲸团队开发和维护 ...

  9. EasyNVR NVR网页无插件直播在兼容宇视NVR RTSP流媒体时PLAY过程对Scale的兼容

    前一段在维护EasyNVR客户的过程中遇到一个问题,在接入宇视NVR的时候,就是明明在vlc中能非常正常播放的视频流,却用EasyRTSPClient RTSP客户端拉流的协议交互过程中,PLAY命令 ...

随机推荐

  1. 基于SIFT+Kmeans+LDA的图片分类器的实现

    原地址:http://www.cnblogs.com/freedomshe/archive/2012/04/24/2468747.html 题记:2012年4月1日回到家,南大计算机研究僧复试以后,等 ...

  2. [半原创]指纹识别+谷歌图片识别技术之C++代码

    原地址:http://blog.csdn.net/guoming0000/article/details/8138223 以前看到一个http://topic.csdn.net/u/20120417/ ...

  3. [Backbone]Make Backbone Better With Extensions

    Backbone is becoming wildly popular as a web application development framework. Along with this popu ...

  4. 使用CXF创建REST WEBSERVICE

    简单小结下CXF跟REST搭配webservice的做法,直接举代码为样例: 1 order.java   package com.example.rest; import javax.xml.bin ...

  5. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  6. POJ 2151 Check the difficulty of problems (动态规划-可能DP)

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4522   ...

  7. hdu4586(概率、期望)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4586 题意:有一个色子,n面,每面有个分值a[i],其中有m面比较特殊,当该面出现时,可以再投一次.求 ...

  8. zoj2059(经典dp)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1059 分析:dp[i][j]表示前i个石头组成两座塔高度差为j的较低 ...

  9. js使用栈来实现10进制转8进制 js取除数 余数

    function ten2eight(x){ var s=[]; var r=''; while(x>0){ s.push(x%8); x=parseInt(x/8); } while(s.le ...

  10. poj2777--Count Color(线段树,二进制转化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34950   Accepted: 10542 Des ...