(轉)VLC播放RTP打包發送的.264檔案

來源:互聯網
上載者:User

標籤:

VLC播放RTP打包發送的.264檔案

1,要有一個發送RTP包的264檔案的伺服器;

具體代碼如下:

rtp.h

#include <WinSock2.h>#pragma comment(lib,"ws2_32.lib")  #define PACKET_BUFFER_END            (unsigned int)0x00000000  #define MAX_RTP_PKT_LENGTH     1400  #define DEST_IP                "172.18.191.194"  #define DEST_PORT            554#define H264                    96  typedef struct   {  /**//* byte 0 */  unsigned char csrc_len:4;        /**//* expect 0 */  unsigned char extension:1;        /**//* expect 1, see RTP_OP below */  unsigned char padding:1;        /**//* expect 0 */  unsigned char version:2;        /**//* expect 2 */  /**//* byte 1 */  unsigned char payload:7;        /**//* RTP_PAYLOAD_RTSP */  unsigned char marker:1;        /**//* expect 1 */  /**//* bytes 2, 3 */  unsigned short seq_no;              /**//* bytes 4-7 */  unsigned  long timestamp;          /**//* bytes 8-11 */  unsigned long ssrc;            /**//* stream number is used here. */  } RTP_FIXED_HEADER;  typedef struct {  //byte 0  unsigned char TYPE:5;  unsigned char NRI:2;  unsigned char F:1;      } NALU_HEADER; /**//* 1 BYTES */  typedef struct {  //byte 0  unsigned char TYPE:5;  unsigned char NRI:2;   unsigned char F:1;      } FU_INDICATOR; /**//* 1 BYTES */  typedef struct {  //byte 0  unsigned char TYPE:5;  unsigned char R:1;  unsigned char E:1;  unsigned char S:1;      } FU_HEADER; /**//* 1 BYTES */  

  rtp.cpp

#include "rtp.h"  #include <stdio.h>FILE * bits = NULL;                //!< the bit stream file  static int FindStartCode2 (unsigned char *Buf);//尋找開始字元0x000001  static int FindStartCode3 (unsigned char *Buf);//尋找開始字元0x00000001  //static bool flag = true;  static int info2=0, info3=0;  RTP_FIXED_HEADER        *rtp_hdr;  NALU_HEADER     *nalu_hdr;  FU_INDICATOR    *fu_ind;  FU_HEADER       *fu_hdr; typedef struct  {  int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)  unsigned len;                 //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)  unsigned max_size;            //! Nal Unit Buffer size  int forbidden_bit;            //! should be always FALSE  int nal_reference_idc;        //! NALU_PRIORITY_xxxx  int nal_unit_type;            //! NALU_TYPE_xxxx      char *buf;                    //! contains the first byte followed by the EBSP  unsigned short lost_packets;  //! true, if packet loss is detected  } NALU_t;  BOOL InitWinsock() { int Error; WORD VersionRequested; WSADATA WsaData; VersionRequested=MAKEWORD(2,2); Error=WSAStartup(VersionRequested,&WsaData); //啟動WinSock2 if(Error!=0) { return FALSE; } else { if(LOBYTE(WsaData.wVersion)!=2||HIBYTE(WsaData.wHighVersion)!=2) { WSACleanup(); return FALSE; } } return TRUE; }//為NALU_t結構體分配記憶體空間  NALU_t *AllocNALU(int buffersize)  {  NALU_t *n;  if ((n = (NALU_t*)calloc (1, sizeof (NALU_t))) == NULL)  {  printf("AllocNALU: n");  exit(0);  }  n->max_size=buffersize;  if ((n->buf = (char*)calloc (buffersize, sizeof (char))) == NULL)  {  free (n);  printf ("AllocNALU: n->buf");  exit(0);  }  return n;  }  //釋放  void FreeNALU(NALU_t *n)  {  if (n)  {  if (n->buf)  {  free(n->buf);  n->buf=NULL;  }  free (n);  }  }  //這個函數輸入為一個NAL結構體,主要功能為得到一個完整的NALU並儲存在NALU_t的buf中,擷取他的長度,填充F,IDC,TYPE位。  //並且返回兩個開始字元之間間隔的位元組數,即包含有首碼的NALU的長度  int GetAnnexbNALU (NALU_t *nalu)  {  int pos = 0;  int StartCodeFound, rewind;  unsigned char *Buf;  if ((Buf = (unsigned char*)calloc (nalu->max_size , sizeof(char))) == NULL)   printf ("GetAnnexbNALU: Could not allocate Buf memory\n");  nalu->startcodeprefix_len=3;//初始化碼流序列的開始字元為3個位元組  if (3 != fread (Buf, 1, 3, bits))//從碼流中讀3個位元組  {  free(Buf);  return 0;  }  info2 = FindStartCode2 (Buf);//判斷是否為0x000001   if(info2 != 1)   {  //如果不是,再讀一個位元組  if(1 != fread(Buf+3, 1, 1, bits))//讀一個位元組  {  free(Buf);  return 0;  }  info3 = FindStartCode3 (Buf);//判斷是否為0x00000001  if (info3 != 1)//如果不是,返回-1  {   free(Buf);  return -1;  }  else   {  //如果是0x00000001,得到開始首碼為4個位元組  pos = 4;  nalu->startcodeprefix_len = 4;  }  }  else  {  //如果是0x000001,得到開始首碼為3個位元組  nalu->startcodeprefix_len = 3;  pos = 3;  }  //尋找下一個開始字元的標誌位  StartCodeFound = 0;  info2 = 0;  info3 = 0;  while (!StartCodeFound)  {  if (feof (bits))//判斷是否到了檔案尾  {  nalu->len = (pos-1)-nalu->startcodeprefix_len;  memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);       nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit  nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  free(Buf);  return pos-1;  }  Buf[pos++] = fgetc (bits);//讀一個位元組到BUF中  info3 = FindStartCode3(&Buf[pos-4]);//判斷是否為0x00000001  if(info3 != 1)  info2 = FindStartCode2(&Buf[pos-3]);//判斷是否為0x000001  StartCodeFound = (info2 == 1 || info3 == 1);  }  // 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");  }  // 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  nalu->len = (pos+rewind)-nalu->startcodeprefix_len;  memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);//拷貝一個完整NALU,不拷貝起始首碼0x000001或0x00000001  nalu->forbidden_bit = nalu->buf[0] & 0x80; //1 bit  nalu->nal_reference_idc = nalu->buf[0] & 0x60; // 2 bit  nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;// 5 bit  free(Buf);  return (pos+rewind);//返回兩個開始字元之間間隔的位元組數,即包含有首碼的NALU的長度  }  //輸出NALU長度和TYPE  void dump(NALU_t *n)  {  if (!n)return;  //printf("a new nal:");  printf(" len: %d  ", n->len);  printf("nal_unit_type: %x\n", n->nal_unit_type);  }  int main(int argc, char* argv[])  {   NALU_t * n;  char* nalu_payload;    char sendbuf[1500]; unsigned short seq_num =0;  int bytes=0;  SOCKET    socket1;struct sockaddr_in server;  int len =sizeof(server);  float framerate=25;  unsigned int timestamp_increase=0,ts_current=0;  timestamp_increase=(unsigned int)(90000.0 / framerate); //+0.5);  bits=fopen("my.h264","rb");InitWinsock(); //初始化通訊端庫  server.sin_family=AF_INET;  server.sin_port=htons(DEST_PORT);            server.sin_addr.s_addr=inet_addr(DEST_IP);   socket1=socket(AF_INET,SOCK_DGRAM,0);  connect(socket1, (const struct sockaddr *)&server, len) ;//申請UDP通訊端  n = AllocNALU(8000000);//為結構體nalu_t及其成員buf分配空間。傳回值為指向nalu_t儲存空間的指標  while(!feof(bits))   {  GetAnnexbNALU(n);//每執行一次,檔案的指標指向本次找到的NALU的末尾,下一個位置即為下個NALU的起始碼0x000001  dump(n);//輸出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(n->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=n->forbidden_bit;  nalu_hdr->NRI=n->nal_reference_idc>>5;//有效資料在n->nal_reference_idc的第6,7位,需要右移5位才能將其值賦給nalu_hdr->NRI。  nalu_hdr->TYPE=n->nal_unit_type;  nalu_payload=&sendbuf[13];//同理將sendbuf[13]賦給nalu_payload  memcpy(nalu_payload,n->buf+1,n->len-1);//去掉nalu頭的nalu剩餘內容寫入sendbuf[13]開始的字串。  ts_current=ts_current+timestamp_increase;  rtp_hdr->timestamp=htonl(ts_current);  bytes=n->len + 13 ;                      //獲得sendbuf的長度,為nalu的長度(包含NALU頭但除去起始首碼)加上rtp_header的固定長度12位元組  send( socket1, sendbuf, bytes, 0 );//發送rtp包  //sleep(1);  //fwrite(sendbuf,bytes, 1, stream);   }  else if(n->len>1400)  {  //得到該nalu需要用多少長度為1400位元組的RTP包來發送  int k=0,l=0;  k=n->len/1400;//需要k個1400位元組的RTP包  l=n->len%1400;//最後一個RTP包的需要裝載的位元組數  int t=0;//用於指示當前發送的是第幾個分區RTP包  ts_current=ts_current+timestamp_increase;  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=n->forbidden_bit;  fu_ind->NRI=n->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=n->nal_unit_type;  nalu_payload=&sendbuf[14];//同理將sendbuf[14]賦給nalu_payload  memcpy(nalu_payload,n->buf+1,1400);//去掉NALU頭  bytes=1400+14;                      //獲得sendbuf的長度,為nalu的長度(除去起始首碼和NALU頭)加上rtp_header,fu_ind,fu_hdr的固定長度14位元組  send( socket1, sendbuf, bytes, 0 );//發送rtp包  //fwrite(sendbuf,bytes, 1, stream);  //sleep(1);  t++;  }  //發送一個需要分區的NALU的非第一個分區,清零FU HEADER的S位,如果該分區是該NALU的最後一個分區,置FU HEADER的E位  else if(k==t)//發送的是最後一個分區,注意最後一個分區的長度可能超過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=n->forbidden_bit;  fu_ind->NRI=n->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=n->nal_unit_type;  fu_hdr->E=1;  nalu_payload=&sendbuf[14];//同理將sendbuf[14]的地址賦給nalu_payload  memcpy(nalu_payload,n->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包  //fwrite(sendbuf,bytes, 1, stream);  t++;  //sleep(1);  }  else if(t<k&&0!=t)  {  //設定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=n->forbidden_bit;  fu_ind->NRI=n->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=n->nal_unit_type;  nalu_payload=&sendbuf[14];//同理將sendbuf[14]的地址賦給nalu_payload  memcpy(nalu_payload,n->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包  //fwrite(sendbuf,bytes, 1, stream);  //sleep(1);  t++;  }  }  }  //usleep(40000);  }  FreeNALU(n);      return 0;  }    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;  }  

2,下載vlc;

3,進行如下設定:
1>設定w.sdp
a,開啟vlc->Media->Open network stream
b,-->network->rtp://@192.168.1.101:1234
c,-->file->add...w.sdp->play
這裡要講一下sdp檔案,sdp檔案時vlc播放是需要的檔案,這裡是必須的,否則是無法播放的,w.sdp檔案其實只是幾句話,這裡我貼出來:

m=video 1234 RTP/AVP 96 //96表示的是動態淨荷類型號 意思就是負載類型不確定,要通過其他方式來確定 a=rtpmap:96 H264 a=framerate:15 c=IN IP4 192.168.0.30

上面的1234和192.168.1.101就是RTP的連接埠和IP

2〉點擊vlc播放按鈕
3〉再運行264檔案的RTP包發送的伺服器
4〉一會兒就看看vlc播放視頻了

 

參考:

1,VLC播放RTP打包發送的.264檔案

http://blog.csdn.net/liuzongming1988/article/details/8292455

(轉)VLC播放RTP打包發送的.264檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.