大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
jrtplib 的功能在它的说明文档中有介绍:For applications such as a mixer or translator using the RTPSession class will not be a good solution. Other components can be used for this purpose: a transmission component, an SSRC table,an RTCP scheduler etc. Using these, it should be much easier to build all kinds of applications. 简单翻译过来就是它适合传输组件,SSRC表,RTCP调度等应用程序,但是并不适合做混合器和翻译器。
在jrtplib的使用的时候,有下面的几点需要被别注意:
(1)端口不能是奇数,否者运行时会出现错误:
ERROR: The specified port base is not an even number
(2)默认编译的jrtplib是没有打开宏RTP_SUPPORT_THREAD
也就是在接收数据的时候,它不会自动的查询是否接收到数据,需要在应用程序中添加轮询函数:sess.Poll()
jrtplib_receive.cpp 代码:
/*=============================================================================
* FileName: jrtplib_receive.cpp
* Desc: receive packet and print out the payloaddata
* Author: licaibiao
* LastChange: 2017-04-10
* =============================================================================*/
#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 <jrtplib3/rtppacket.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace jrtplib;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main(void)
{
RTPSession sess;
uint16_t portbase = 6664;
int status;
bool done = false;
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0/10.0);
sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status = sess.Create(sessparams,&transparams);
checkerror(status);
sess.BeginDataAccess();
RTPTime delay(0.020);
RTPTime starttime = RTPTime::CurrentTime();
while (!done)
{
status = sess.Poll();
checkerror(status);
if (sess.GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
while ((pack = sess.GetNextPacket()) != NULL)
{
std::cout << pack->GetPayloadData() << std::endl;
sess.DeletePacket(pack);
}
} while (sess.GotoNextSourceWithData());
}
RTPTime::Wait(delay);
RTPTime t = RTPTime::CurrentTime();
t -= starttime;
if (t > RTPTime(60.0))
done = true;
}
sess.EndDataAccess();
delay = RTPTime(10.0);
sess.BYEDestroy(delay,0,0);
return 0;
}
上面程序,以每0.02s的周期去查询是否有接收到数据,持续60s,接收到数据之后,将接收到的数据通过标准输出打印出来。
jrtplib_send.cpp 代码:
/*=============================================================================
* FileName: jrtplib_send.cpp
* Desc: sending packets to destination port
* Author: licaibiao
* LastChange: 2017-04-10
* =============================================================================*/
#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;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main(void)
{
int i;
int num;
int status;
RTPSession sess;
uint16_t portbase = 6666;
uint16_t destport = 6664;
#if 0
uint32_t destip;
destip = inet_addr("192.168.0.6");
if (destip == INADDR_NONE)
{
std::cerr << "Bad IP address specified" << std::endl;
return -1;
}
destip = ntohl(destip);
#else
uint8_t destip[]={192,168,0,6};
#endif
std::cout << "Number of packets you wish to be sent:" << std::endl;
std::cin >> num;
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
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);
status = sess.SendPacket((void *)"1234567890",10,0,false,10);
checkerror(status);
RTPTime::Wait(RTPTime(1,0));
}
sess.BYEDestroy(RTPTime(10,0),0,0);
return 0;
}
上面程序,通过标准输入确定需要发送数据的包数,然后以1s的周期发送数据。这里是p2p传输,发送之前需要指定发送地址和发送端口。这里的地址就是接收端主机的IP地址,这里的目的端口,就是接收端程序设置的本地端口。
Makefile文件:
APP = test
LINK_OPTS = -ljrtp
OBJ = jrtplib_receive.cpp
#OBJ = jrtplib_send.cpp
out:
g++ $(OBJ) -o $(APP) $(LINK_OPTS)
clean:
rm -rf *o $(APP)
编译执行部分结果:
发送端:
licaibiao@lcb:~/test/RTP/test_jrtplib$ ./test
Number of packets you wish to be sent:
100
Sending packet 1/100
Sending packet 2/100
Sending packet 3/100
Sending packet 4/100
Sending packet 5/100
Sending packet 6/100
接收端:
licaibiao@ubuntu:~/test/RTP_TEST/JRTPLIB/biao$ ./test
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
注意:在编译运行本程序之前,需要正确安装好jrtplib
工程代码可在这里下载:最简jrtplib 收发数据实例
原文链接:https://blog.csdn.net/li_wen01/article/details/70045185
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/164753.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...