- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.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 "192.168.0.25"
- #define DEST_PORT 1234
-
- #define H264 96
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- typedef struct
- {
-
- unsigned char csrc_len:4;
- unsigned char extension:1;
- unsigned char padding:1;
- unsigned char version:2;
-
- unsigned char payload:7;
- unsigned char marker:1;
-
- unsigned short seq_no;
-
- unsigned long timestamp;
-
- unsigned long ssrc;
- } RTP_FIXED_HEADER;
-
-
-
-
-
-
-
-
-
- typedef struct {
-
- unsigned char TYPE:5;
- unsigned char NRI:2;
- unsigned char F:1;
- } NALU_HEADER;
-
-
-
-
-
-
-
-
-
-
- typedef struct {
-
- unsigned char TYPE:5;
- unsigned char NRI:2;
- unsigned char F:1;
- } FU_INDICATOR;
-
-
-
-
-
-
-
-
-
-
- typedef struct {
-
- unsigned char TYPE:5;
- unsigned char R:1;
- unsigned char E:1;
- unsigned char S:1;
- } FU_HEADER;
H264.h头文件内容:
RTPSend.cpp文件内容:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <memory.h>
- #include "h264.h"
-
- #define UDP_MAX_SIZE 1400
-
- typedef struct
- {
- int startcodeprefix_len;
- unsigned len;
- unsigned max_size;
- int forbidden_bit;
- int nal_reference_idc;
- int nal_unit_type;
- char *buf;
- unsigned short lost_packets;
- } NALU_t;
-
- FILE *bits = NULL;
- static int FindStartCode2(unsigned char *Buf);
- static int FindStartCode3(unsigned char *Buf);
-
-
- static int info2=0, info3=0;
- RTP_FIXED_HEADER *rtp_hdr;
-
- NALU_HEADER *nalu_hdr;
- FU_INDICATOR *fu_ind;
- FU_HEADER *fu_hdr;
-
- BOOL InitWinsock()
- {
- int Error;
- WORD VersionRequested;
- WSADATA WsaData;
- VersionRequested=MAKEWORD(2,2);
- Error=WSAStartup(VersionRequested,&WsaData);
- if(Error!=0)
- {
- return FALSE;
- }
- else
- {
- if(LOBYTE(WsaData.wVersion)!=2||HIBYTE(WsaData.wHighVersion)!=2)
- {
- WSACleanup();
- return FALSE;
- }
-
- }
- return TRUE;
- }
-
-
- 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);
- }
- }
-
- void OpenBitstreamFile (char *fn)
- {
- if (NULL == (bits=fopen(fn, "rb")))
- {
- printf("open file error\n");
- exit(0);
- }
- }
-
-
-
-
- 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;
-
- if (3 != fread (Buf, 1, 3, bits))
- {
- free(Buf);
- return 0;
- }
- info2 = FindStartCode2 (Buf);
- if(info2 != 1)
- {
-
- if(1 != fread(Buf+3, 1, 1, bits))
- {
- free(Buf);
- return 0;
- }
- info3 = FindStartCode3 (Buf);
- if (info3 != 1)
- {
- free(Buf);
- return -1;
- }
- else
- {
-
- pos = 4;
- nalu->startcodeprefix_len = 4;
- }
- }
- else
- {
-
- 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;
- nalu->nal_reference_idc = nalu->buf[0] & 0x60;
- nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;
- free(Buf);
- return pos-1;
- }
- Buf[pos++] = fgetc (bits);
- info3 = FindStartCode3(&Buf[pos-4]);
- if(info3 != 1)
- info2 = FindStartCode2(&Buf[pos-3]);
- StartCodeFound = (info2 == 1 || info3 == 1);
- }
-
-
-
- rewind = (info3 == 1)? -4 : -3;
-
- if (0 != fseek (bits, rewind, SEEK_CUR))
- {
- free(Buf);
- printf("GetAnnexbNALU: Cannot fseek in the bit stream file");
- }
-
-
-
-
-
- nalu->len = (pos+rewind)-nalu->startcodeprefix_len;
- memcpy (nalu->buf, &Buf[nalu->startcodeprefix_len], nalu->len);
- nalu->forbidden_bit = nalu->buf[0] & 0x80;
- nalu->nal_reference_idc = nalu->buf[0] & 0x60;
- nalu->nal_unit_type = (nalu->buf[0]) & 0x1f;
- free(Buf);
-
- return (pos+rewind);
- }
-
- static int FindStartCode2 (unsigned char *Buf)
- {
- if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=1) return 0;
- else return 1;
- }
-
- static int FindStartCode3 (unsigned char *Buf)
- {
- if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1) return 0;
- else return 1;
- }
-
- int rtpnum = 0;
-
-
- void dump(NALU_t *n)
- {
- if (!n)return;
- printf("%3d, len: %6d ",rtpnum++, n->len);
- printf("nal_unit_type: %x\n", n->nal_unit_type);
- }
-
- int main(int argc, char* argv[])
- {
- OpenBitstreamFile("E:\\测试数据\\tv480x320.264");
- NALU_t *n;
- char* nalu_payload;
- char sendbuf[1500];
-
- unsigned short seq_num =0;
- int bytes=0;
- InitWinsock();
- SOCKET socket1;
- struct sockaddr_in server;
- int len =sizeof(server);
- float framerate=15;
- unsigned int timestamp_increse=0,ts_current=0;
- timestamp_increse=(unsigned int)(90000.0 / framerate);
-
- 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 sockaddr *)&server, len) ;
- n = AllocNALU(8000000);
-
- while(!feof(bits))
- {
- GetAnnexbNALU(n);
- dump(n);
-
- memset(sendbuf,0,1500);
-
-
- rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];
-
-
- rtp_hdr->version = 2;
- rtp_hdr->marker = 0;
- rtp_hdr->payload = H264;
- rtp_hdr->ssrc = htonl(10);
-
-
- if(n->len<=UDP_MAX_SIZE){
-
- rtp_hdr->marker=1;
- rtp_hdr->seq_no = htons(seq_num ++);
-
-
- nalu_hdr =(NALU_HEADER*)&sendbuf[12];
- nalu_hdr->F=n->forbidden_bit;
- nalu_hdr->NRI=n->nal_reference_idc>>5;
- nalu_hdr->TYPE=n->nal_unit_type;
-
- nalu_payload=&sendbuf[13];
- memcpy(nalu_payload,n->buf+1,n->len-1);
-
- ts_current=ts_current+timestamp_increse;
- rtp_hdr->timestamp=htonl(ts_current);
- bytes=n->len + 12 ;
- send(socket1,sendbuf,bytes,0);
-
- }else{
- int packetNum = n->len/UDP_MAX_SIZE;
- if (n->len%UDP_MAX_SIZE != 0)
- packetNum ++;
-
- int lastPackSize = n->len - (packetNum-1)*UDP_MAX_SIZE;
- int packetIndex = 1 ;
-
- ts_current=ts_current+timestamp_increse;
-
- rtp_hdr->timestamp=htonl(ts_current);
-
-
-
- rtp_hdr->seq_no = htons(seq_num ++);
-
- rtp_hdr->marker=0;
-
- fu_ind =(FU_INDICATOR*)&sendbuf[12];
- fu_ind->F=n->forbidden_bit;
- fu_ind->NRI=n->nal_reference_idc>>5;
- fu_ind->TYPE=28;
-
-
- fu_hdr =(FU_HEADER*)&sendbuf[13];
- fu_hdr->S=1;
- fu_hdr->E=0;
- fu_hdr->R=0;
- fu_hdr->TYPE=n->nal_unit_type;
-
- nalu_payload=&sendbuf[14];
- memcpy(nalu_payload,n->buf+1,UDP_MAX_SIZE);
- bytes=UDP_MAX_SIZE+14;
- send( socket1, sendbuf, bytes, 0 );
-
-
- for(packetIndex=2;packetIndex<packetNum;packetIndex++)
- {
- rtp_hdr->seq_no = htons(seq_num ++);
-
-
- rtp_hdr->marker=0;
-
- fu_ind =(FU_INDICATOR*)&sendbuf[12];
- fu_ind->F=n->forbidden_bit;
- fu_ind->NRI=n->nal_reference_idc>>5;
- fu_ind->TYPE=28;
-
-
- fu_hdr =(FU_HEADER*)&sendbuf[13];
- fu_hdr->S=0;
- fu_hdr->E=0;
- fu_hdr->R=0;
- fu_hdr->TYPE=n->nal_unit_type;
-
- nalu_payload=&sendbuf[14];
- memcpy(nalu_payload,n->buf+(packetIndex-1)*UDP_MAX_SIZE+1,UDP_MAX_SIZE);
- bytes=UDP_MAX_SIZE+14;
- send( socket1, sendbuf, bytes, 0 );
- }
-
-
-
- rtp_hdr->seq_no = htons(seq_num ++);
-
- rtp_hdr->marker=1;
-
- fu_ind =(FU_INDICATOR*)&sendbuf[12];
- fu_ind->F=n->forbidden_bit;
- fu_ind->NRI=n->nal_reference_idc>>5;
- fu_ind->TYPE=28;
-
-
- fu_hdr =(FU_HEADER*)&sendbuf[13];
- fu_hdr->S=0;
- fu_hdr->E=1;
- fu_hdr->R=0;
- fu_hdr->TYPE=n->nal_unit_type;
-
- nalu_payload=&sendbuf[14];
- memcpy(nalu_payload,n->buf+(packetIndex-1)*UDP_MAX_SIZE+1,lastPackSize-1);
- bytes=lastPackSize-1+14;
- send( socket1, sendbuf, bytes, 0 );
- }
-
-
- }
-
- FreeNALU(n);
- return 0;
- }
将以下文本保存为rtpPara.sdp文件
m=video 1234 RTP/AVP 96
a=rtpmap:96 H264
a=framerate:30
c=IN IP4 192.168.0.25
用VLC打开该文件,然后运行编译好的发送程序。
注意头文件和sdp文件的IP都要与实验机器的IP吻合。
该代码在NALDecoder程序基础上修改了一个内存bug而已。感谢前人的开源。