记 windows 编译 jrtp,并在QT中使用
编译环境
win10 + Visual Studio 2017 + Qt 5.11.3 (Qt Creator 4.7.2)+ CMAKE 3.9.0
源码版本
jthread-1.3.3 + jrtplib-3.11.1
源码下载网址
http://research.edm.uhasselt.be/jori/page/CS/Jrtplib.html
注意
对于jrtplib而言,jthread为可选依赖,但建议选用
编译步骤
- 下载源码并解压
- 新建文件夹src(存放源码), build(存放CMAKE生成的文件), install(存放编译的结果), lib(存放库文件)
- 在build和install内都新建两个文件夹,jthread-1.3.3和jrtplib-3.11.1
- 运行CMAKE,为编译jthread进行配置,指明编译结果的安装路径为install/jthread-1.3.3
- 打开Visual Studio 2017,打开build/jthread-1.3.3内的解决方案jthread.sln,在debug-x64模式下生成解决方案,再选中解决方案中的INSTALL项目,单独生成此项目。然后,修改为release-x64模式进行相同的操作。
- 运行CMAKE,为编译jrtplib进行配置,配置中指明已编译的jthread的包含路径,库路径,指明编译结果的安装路径为install/jrtplib-3.11.1
- 用Visual Studio 2017打开/build/jrtplib-3.11.1内由CMAKE生成的解决方案jrtplib.sln,在debug-x64和release-x64两种模式下,都生成一次解决方案,都单独生成一次项目INSTALL
- 将编译生成的所有的jthread和jrtplib的库,复制到lib文件夹,将此文件夹添加到系统搜索路径
- 复制jrtplib源码包附带的实例项目example1的代码,在Qt Creator中创建项目,黏贴代码,修改pro文件, 运行qmake,即可成功编译并正常运行。
过程图示
- 配置jthread-1.3.3,生成VS2017的解决方案
- 打开VS2017,编译源码,安装编译结果
jthread编译结果如下图:
- 使用CMAKE配置jrtplib源码
- 打开CMAKE针对jrtplib生成的VS2017解决方案,进行编译,流程与编译jthread一致,jrtplib编译结果如下图
- 把编译生成的所有库文件,存放到一个lib文件夹,并把此文件夹添加到系统搜索路径。
- 打开QT Creator, 新建测试项目,复制如下源码到main.cpp
#include <QCoreApplication>
/*
Here's a small IPv4 example: it asks for a portbase and a destination and
starts sending packets to that destination.
*/
#include <WinSock2.h>
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtpipv4address.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtperrors.h>
#include <jrtplib3/rtplibraryversion.h>
#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(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
#ifdef RTP_SOCKETTYPE_WINSOCK
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // RTP_SOCKETTYPE_WINSOCK
RTPSession sess;
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i,num;
std::cout << "Using version " << RTPLibraryVersion::GetVersion().GetVersionString() << std::endl;
// 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;
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);
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();
// 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 RTP_SOCKETTYPE_WINSOCK
WSACleanup();
#endif // RTP_SOCKETTYPE_WINSOCK
return a.exec();
}
- 选中项目,右键进行添加库,注意jthread和jrtplib的库都要添加,下列图片仅展示添加jthread,jrtplib库的添加方式如出一辙。
- 打开pro文件,按自身实际情况稍作修改,注意红色箭头处的内容
- 编译运行即可