Email:[email protected]
老外用C++编写的开源RTP协议库,用来进行实时数据传输,可以运行在 Windows、Linux、 FreeBSD、Solaris、Unix和VxWorks 等多种操作系统上,主页为:http://research.edm.uhasselt.be/~jori/page/index.php?n=Main.HomePage
jrtplib: http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib
jthread: http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jthread
cmake: http://www.cmake.org/cmake/resources/software.html
① 打开cmake,添加好输入(where..)和输出路径(where to...),完成configure配置(选visual studio 10),配置结果如下图:
② 点击generate,生成VS2010工程文件
③ 打开工程文件并编译,在debug和release下分别生成jthread.lib和jthread_d.lib
编译的具体方法为:选择Solution Explorer里的 Solution jthread,点右键,运行"Rebuild Solution";如编译无错误,再选择INSTALL项目,运行"Build"。
④ 如果编译成功(如下图),会在C:\Program Files\jthread的include\jthread下生成头文件;在lib下生成lib和cmake文件
温馨提示:在win7下,你必须拥有管理者权限,否则编译不会通过,因为无法在C:\Program Files创建jthread文件,当然你可以手动创建。
① 同2-①,其中configure会稍微麻烦一些,详细配置结果如下:
② 点击generate,生成VS2010工程文件
③ 打开工程文件并编译,在debug和release下分别生成jrtplib_d.lib和jrtplib.lib
④ 编译成功(如下图),在C:\Program Files\jrtplib下include\jrtplib3下会生成一堆头文件;在lib下会生成jrtplib_d.lib和jrtplib.lib以及cmake文件
说明:网上提到的一些用VS2008和VC6.0方法中提到了两个细节: 一是要把"jmutex.h"和"jthread.h"两个头文件放入jrtplib/src目录下,二是要把src文件夹下所有头文件中的<jmutex.h>和<jthread.h>语句修改为"jmutex.h"和"jthread.h"。
我在编译时没有处理这两个细节成功了,后续调试出现相应问题相应修改一下即可。
①步骤一:
方法1. 将编译生成的jrtplib.lib和jthread.lib库拷贝到“*:\Program Files\Microsoft Visual Studio 10.0\VC\lib”下面
方法2. 将编译生成的四个lib库库拷贝到当前工程的cpp文件下
②步骤二:
方法1. [菜单]“项目->属性->配置属性->连接器->输入->附加依赖项”里填写“jrtplib.lib;jthread.lib;WS2_32.lib”
方法2. pragma 方式,在stdafx.h文件中 添加
#ifdef DEBUG
#pragma comment(lib, "jrtplib_d.lib")
#pragma comment(lib,"jthread_d.lib")
#pragma comment(lib,"WS2_32.lib")
#else
#pragma comment(lib, "jrtplib.lib")
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"WS2_32.lib")
#endif
①步骤一:将所有的.h文件放到一起,如myJRTPLIBHeader里面,再添加include
②步骤二:
方法1.“项目->属性->配置属性->C/C++->常规->附加包含目录”
方法2.“工具->选项->项目和解决方案->C++ 目录”,选择对应平台,然后添加所需“包括文件”目录(此法VS2010不通)
cpp文件:
// jrtplibTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
// 头文件
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif // WIN32
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace jrtplib;
//
// This function checks if there was a RTP error. If so, it displays an error
// message and exists.
//
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
//
// The main routine
//
int main(void)
{
#ifdef WIN32
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32
RTPSession sess;
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i,num;
// First, we'll ask for the necessary information
std::cout << "Enter local portbase:" << std::endl;
std::cin >> portbase;
std::cout << std::endl;
std::cout << "Enter the destination IP address" << std::endl;
std::cin >> ipstr;
// 获得接收端的IP地址和端口号
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;
std::cout << std::endl;
std::cout << "Number of packets you wish to be sent:" << std::endl;
std::cin >> num;
// 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);
// 创建RTP会话
status = sess.Create(sessparams,&transparams);
checkerror(status);
RTPIPv4Address addr(destip,destport);
// 指定RTP数据接收端
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();
// check incoming packets
if (sess.GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
while ((pack = sess.GetNextPacket()) != NULL)
{
// You can examine the data here
printf("Got packet !\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));
}
sess.BYEDestroy(RTPTime(10,0),0,0);
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}
在VS2010+Win7下编译好的JRTPLIB库及相关头文件下载:(刚传CSDN,现在打不开,等等,明天补上...)
补充:下载(猛击)
1 http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib
2 http://research.edm.uhasselt.be/jori/jrtplib/documentation/index.html
3 http://blog.csdn.net/nickche300/article/details/6408099
4 http://blog.csdn.net/sunloverain2/article/details/5398694
5 http://blog.csdn.net/aaronalan/article/details/5153604