// IMPORTANT: The local timestamp unit MUST be set, otherwise // RTCP Sender Report info will be calculated wrong // In this case, we''ll be sending 10 samples each second, so we''ll // put the timestamp unit to (1.0/10.0) sessparams.SetOwnTimestampUnit(1.0/10.0);
sessparams.SetAcceptOwnPackets(true); transparams.SetPortbase(portbase); status = sess.Create(sessparams,&transparams); checkerror(status);
RTPIPv4Address addr(destip,destport);
status = sess.AddDestination(addr); checkerror(status);
for (i =1 ; i <= num ; i++) ...{ printf(" Sending packet %d/%d ",i,num);
// 发送数据“1234567890” status = sess.SendPacket((void*)"1234567890",10,0,false,10); checkerror(status);
sess.BeginDataAccess();
// check incoming packets if (sess.GotoFirstSourceWithData()) ...{ do ...{ RTPPacket *pack;
while ((pack = sess.GetNextPacket()) != NULL) ...{ // You can examine the data here printf("Got packet ! ");
std::cout <<"Got packet with " <<"extended sequence number " << pack->GetExtendedSequenceNumber() <<" from SSRC "<< pack->GetSSRC() << std::endl;
int dataLength = pack->GetPayloadLength(); pfBuffer =(unsigned char*)pack->GetPayloadData(); pBuffer =new BYTE[dataLength +1]; memcpy(pBuffer, pfBuffer, dataLength); pBuffer[dataLength] =0; std::cout << pBuffer << std::endl; // we don''t longer need the packet, so // we''ll delete it sess.DeletePacket(pack); } }while (sess.GotoNextSourceWithData()); }
sess.EndDataAccess();
#ifndef RTP_SUPPORT_THREAD status = sess.Poll(); checkerror(status); #endif// RTP_SUPPORT_THREAD
执行测试程序的效果如下: Enter local portbase: 8000 Enter the destination IP address 127.0.0.1 Enter the destination port 8000 Number of packets you wish to be sent: 5
Sending packet 1/5 Got packet ! Got packet with extended sequence number 59262 from SSRC 3029241192 1234567890
Sending packet 2/5 Got packet ! Got packet with extended sequence number 59263 from SSRC 3029241192 1234567890
Sending packet 3/5
Sending packet 4/5 Got packet ! Got packet with extended sequence number 59264 from SSRC 3029241192 1234567890 Got packet ! Got packet with extended sequence number 59265 from SSRC 3029241192 1234567890