資源簡介
通過RTP發送h264碼流,網上比較多的版本,可在上位機用vlc播放,是一個學習rtp協議的好例子

代碼片段和文件信息
//?NALDecoder.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?
#include?
#include?
#include?
#include?“rtp.h“
#include?
#include?
#include?
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;
FILE?*bits?=?NULL;????????????????//!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;
/*BOOL?InitWinsock()
{
????int?Error;
????WORD?VersionRequested;
????WSADATA?WsaData;
????VersionRequested=MAKEWORD(22);
????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);
??}
}
void?OpenBitstreamFile?(char?*fn)
{
??if?(NULL?==?(bits=fopen(fn?“rb“)))
??{
??printf(“open?file?error\n“);
??exit(0);
??}
}
//這個函數輸入為一個NAL結構體,主要功能為得到一個完整的NALU并保存在NALU_t的buf中,獲取他的長度,填充FIDCTYPE位。
//并且返回兩個開始字符之間間隔的字節數,即包含有前綴的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
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-04-18?16:27??RTP_H264\
?????文件???????13192??2015-04-15?17:32??RTP_H264\rtp.c
?????文件????????1516??2013-09-23?11:43??RTP_H264\rtp.h
- 上一篇:address.txt
- 下一篇:沈鑫剡《路由和交換技術實驗實訓》一書配套實驗文件
評論
共有 條評論