首页 > 代码库 > (转)VLC播放RTP打包发送的.264文件
(转)VLC播放RTP打包发送的.264文件
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 = http://www.mamicode.com/htonl(10); //随机指定为10,并且在本RTP会话中全局唯一 >
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。