#include "rtpsessionparams.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtptimeutilities.h"
#include "rtppacket.h"
#include "h264rtppacket.h"
using namespace jrtplib;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
typedef struct tagRtpPacket
{
tagRtpPacket()
{
pktBuf = (unsigned char*)malloc(500*1020*sizeof(unsigned char));
m_nBufLen = 4;
pktBuf[0] = 0;
pktBuf[1] = 0;
pktBuf[2] = 0;
pktBuf[3] = 1;
}
~tagRtpPacket()
{
if (NULL != pktBuf)
{
free(pktBuf);
pktBuf = NULL;
}
}
void AppendBuf(unsigned char *buf,int nLen)
{
if (NULL != buf)
{
memcpy(pktBuf + m_nBufLen,buf,nLen);
m_nBufLen += nLen;
}
}
void ReleaseBuffer()
{
m_nBufLen = 4;
}
unsigned char *pktBuf;
unsigned int m_nBufLen;
}H264RtpPacket;
int main(int argc, char **argv)
{
H264RtpPacket m_rtpBuf;
int i = 0;
RTPSession session;
RTPSessionParams sessionparams;
sessionparams.SetOwnTimestampUnit(1.0/90000.0);
RTPUDPv4TransmissionParams transparams;
transparams.SetPortbase(9000);//在客户端端程序中,这个是接收服务端数据的端口,服务端发往//客户端是哪个端口就填哪个端口
int status = session.Create(sessionparams,&transparams);
checkerror(status);
uint8_t localip[]={192,168,2,128};///该地址是服务端地址
RTPIPv4Address addr(localip,10000);//这个端口可以跟上面的端口一样填9000
status = session.AddDestination(addr);
checkerror(status);
session.SetDefaultPayloadType(96);//h264
session.SetDefaultMark(false);
session.SetDefaultTimestampIncrement(3600);
RTPTime delay(0.020);
RTPTime starttime = RTPTime::CurrentTime();
bool done = false;
while (!done)
{
session.BeginDataAccess();
if (session.GotoFirstSourceWithData())
{
do
{
RTPPacket *packet;
while ((packet = session.GetNextPacket()) != 0)
{
bool bMark = packet->HasMarker();
if(!bMark)
{
m_rtpBuf.AppendBuf(packet->GetPayloadData(),packet->GetPayloadLength());
}
else
{
m_rtpBuf.AppendBuf(packet->GetPayloadData(),packet->GetPayloadLength());
///h264SendStream2Vdec(0, m_rtpBuf.pktBuf, m_rtpBuf.m_nBufLen, 0);
m_rtpBuf.ReleaseBuffer();
}
session.DeletePacket(packet);
}
} while (session.GotoNextSource());
}
session.EndDataAccess();
RTPTime::Wait(delay);
RTPTime t = RTPTime::CurrentTime();
t -= starttime;
if (t > RTPTime(60.0))
done = true;
}
delay = RTPTime(10.0);
session.BYEDestroy(delay,"Time's up",9);
#ifdef WIN32
WSACleanup();
#endif // WIN32
getchar();
return 1;
}