記 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文件,按自身實際情況稍作修改,注意紅色箭頭處的內容
- 編譯運行即可