#define PLOAD_TYPE 98
#define DefaultTimestampIncrement 90000/25
static RTPSession sess;
//创建rtp会话
static int RtpSetup( uint16_t portbase)
{
int status;
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0/90000.0);
sessparams.SetMaximumPacketSize(1200);
transparams.SetPortbase(portbase);
sess.SetDefaultPayloadType(PLOAD_TYPE);
sess.SetDefaultMark(false);
sess.SetDefaultTimestampIncrement(DefaultTimestampIncrement);
status = sess.Create(sessparams,&transparams);
checkerror(status);
return status;
}
//错误判断
void checkerror(int err)
{
if (err < 0)
{
char* errstr = RTPGetErrorString(err);
printf("Error:%s\\n", errstr);
exit(-1);
}
}
//增加rtp传输目标ip地址,参数为目标ip和端口
int AddDestination(uint32_t ipaddr, uint16_t destport)
{
int status;
RTPIPv4Address addr(ipaddr,destport);
status = sess.AddDestination(addr);
checkerror(status);
return status;
}
//rtp视频传输,val为一帧数据流(264的原始数据),包含0x00 0x00 0x00 0x01信息,length为数据的长度
int H264SendPacket(unsigned char *val, uint32_t length)
{
int status=0;
uint32_t TimestampIncrement;
uint32_t send_length,valid_len=length-4;
char NALU=val[4],*sendStartAddr=NULL;
#define MAX_STREAM_SLICE 1024
//获取默认设置
TimestampIncrement=sess.GetDefaultTimestampIncrement();
//如果数据小于1024字节,直接发送
if(valid_len <= MAX_STREAM_SLICE)
{
status = sess.SendPacket((void *)&val[4],valid_len,PLOAD_TYPE,true,DefaultTimestampIncrement);
checkerror(status);
}
else
{
int k=0,l=0,len=valid_len,pos=0;
k=len/MAX_STREAM_SLICE; //切分为很多个包:包的个数
l=len%MAX_STREAM_SLICE;
int t=0;
while(t<=k)
{
if(t==0)
{//发送第1包
sendStartAddr=(char *)(val+4);//发送数据的起始地址
sendStartAddr[pos-1]=(NALU&0x60)|28;
sendStartAddr[pos]=(NALU&0x1f)|0x80;
send_length=MAX_STREAM_SLICE+1;//要发送数据的长度
status = sess.SendPacket(sendStartAddr-1,send_length,PLOAD_TYPE,false,DefaultTimestampIncrement);
checkerror(status);
if(status<0)
{
goto end;
}
pos+=MAX_STREAM_SLICE;
}
else if(k==t&&l!=0)
{//最后一包
sendStartAddr[pos-2]=(NALU&0x60)|28;
sendStartAddr[pos-1]=(NALU&0x1f)|0x40;
send_length=l+2;//要发送数据的长度
status = sess.SendPacket(sendStartAddr+pos-2,send_length,PLOAD_TYPE,true,0);
checkerror(status);
if(status<0)
{
goto end;
}
pos+=l;
}
else if(t<k&&0!=t)
{//发送第2包--倒数第2包
if(l==0&&t==k-1)
{
sendStartAddr[pos-2]=(NALU&0x60)|28;
sendStartAddr[pos-1]=(NALU&0x1f)|0x40;
}
else
{
sendStartAddr[pos-2]=(NALU&0x60)|28;
sendStartAddr[pos-1]=(NALU&0x1f);
}
send_length=MAX_STREAM_SLICE+2;//要发送数据的长度
status = sess.SendPacket(sendStartAddr+pos-2,send_length,PLOAD_TYPE,false,0);
checkerror(status);
if(status<0)
{
goto end;
}
pos+=MAX_STREAM_SLICE;
}
t++;
}
}
checkerror(status);
end:
return status;
<