本文原创,若有转发,请注明原文链接,谢谢合作!
发表时间: 2010-11-30 21:27:58
1.如何在VS2008中使用jrtplib
在网友@向左向右走的一篇文章中说的很详细,参见原文链接: http://blog.csdn.net/sunloverain2/archive/2010/03/20/5398694.aspx
我补充一点:在VS2008中,lib库和头文件可以不加到VS的安装目录下,可以拷贝到自己所建的工程目录下,并在Solution Explorer窗口 下,在你所建的工程名上“右击”,选择“Properties"-->"Configuration Properties"-->"C/C++"-->"General"-->
"Addtional Include Directory" 把包含所有头文件的文件夹添加进去;把jrtplib.lib,jthread.lib拷贝到.cpp的同一目录下,在程序中使 用#pragma comment(lib,"jrtplib.lib") 和 #pragma comment(lib,"jthread.lib")即可。
lib库在VS2008中还有另一种加载方法,可以在网上找找。
2.使用jrtplib写的一个简单服务器程序代码:
该程序是从 官方下载jrtplib文件时所带的example的基础上 修改得到,代码如下:
RTPSession sess;
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i(1);
int packetlen(0);
unsigned char *ptr;
// First, we'll ask for the necessary information
// std::cout << "Enter local portbase:" << std::endl;
// std::cin >> portbase;
// std::cout << std::endl;
portbase = 6000;
// Now, we'll create a RTP session, set the destination, send some
// packets and poll for incoming data.
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
// 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);
do{
sess.BeginDataAccess();
// check incoming packets
if (sess.GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
while ((pack = sess.GetNextPacket()) != NULL)
{
// You can examine the data here
ptr = (unsigned char *)(pack->GetPayloadData());
packetlen = pack->GetPayloadLength();
printf("Got packet %d: ", i);
++i;
for(int ix=0;ix<packetlen;++ix)
printf("%c",*ptr++);
printf("/n");
// 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
RTPTime::Wait(RTPTime(1,0));
}while(1);
sess.BYEDestroy(RTPTime(10,0),0,0);
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}
3.使用jrtplib写的一个简单客户端程序代码:
该程序是从 官方下载jrtplib文件时所带的example的基础上 修改得到,代码如下:
RTPSession sess;
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i,num;
unsigned char *ptr;
// First, we'll ask for the necessary information
// std::cout << "Enter the destination IP address" << std::endl;
// std::cin >> ipstr;
ipstr = "127.0.0.1";
destip = inet_addr(ipstr.c_str());
if (destip == INADDR_NONE)
{
std::cerr << "Bad IP address specified" << std::endl;
return -1;
}
// The inet_addr function returns a value in network byte order, but
// we need the IP address in host byte order, so we use a call to
// ntohl
destip = ntohl(destip);
// std::cout << "Enter the destination port" << std::endl;
// std::cin >> destport;
destport = 6000;
// std::cout << std::endl;
// std::cout << "Number of packets you wish to be sent:" << std::endl;
// std::cin >> num;
num = 10;
// Now, we'll create a RTP session, set the destination, send some
// packets and poll for incoming data.
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
// 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("/nSending packet %d/%d/n",i,num);
// send the packet
status = sess.SendPacket((void *)"1234567890",10,0,false,10);
checkerror(status);
sess.BeginDataAccess();
sess.EndDataAccess();
#ifndef RTP_SUPPORT_THREAD
status = sess.Poll();
checkerror(status);
#endif // RTP_SUPPORT_THREAD
RTPTime::Wait(RTPTime(1,0));
}
sess.BYEDestroy(RTPTime(10,0),0,0);
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}