大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
一. 前言
JRTPLIB 是 C++ 语言编写的 RTP 库,它帮助我们封装了 RTP 协议细节,用户通过提供好的接口可以设置 RTP 包信息并发送到指定地址,也可以接收 RTP 包取出信息。
本文仅介绍如何使用 JRTPLIB 发送/接收 RTP 数据包,我在这篇博客又介绍了如何使用 JRTPLIB 构造 RTP 数据包来荷载 H264 码流数据。
二. 下载编译安装
git clone https://github.com/j0r1/JRTPLIB.git
cd JRTPLIB
cmake CMakeLists.txt
make
sudo make install
三. 代码实战
1. JRTPLIB接收RTP包程序
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtplibraryversion.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtppacket.h>
#include <iostream>
#include <arpa/inet.h>
using namespace jrtplib;
void checkerror(int rtperr) {
if (rtperr < 0) {
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main() {
std::cout << "Using JRTPLib version: " << RTPLibraryVersion::GetVersion().GetVersionString() << std::endl;
uint16_t localport;
std::cout << "Enter local port(even): ";
std::cin >> localport;
RTPUDPv4TransmissionParams tranparams;
tranparams.SetPortbase(localport);
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0 / 10.0);
sessparams.SetAcceptOwnPackets(true);
RTPSession sess;
int status = sess.Create(sessparams, &tranparams);
checkerror(status);
RTPTime delay(0.020);
RTPTime startTime = RTPTime::CurrentTime();
bool done = false;
while (!done) {
sess.BeginDataAccess();
if (sess.GotoFirstSourceWithData()) {
do {
RTPPacket* packet;
while ((packet = sess.GetNextPacket()) != NULL) {
std::cout << "Got packet!" << std::endl;
std::cout << "SSRC: " << packet->GetSSRC() << std::endl;
std::cout << "Payload data: " << packet->GetPayloadData() << std::endl;
std::cout << "Payload type: " << packet->GetPayloadType() << std::endl;
std::cout << "Packet length: " <<packet->GetPacketLength() << std::endl;
std::cout << "Sequence number: " << packet->GetSequenceNumber() << std::endl;
std::cout << "Timestamp: " << packet->GetTimestamp() << std::endl;
std::cout << std::endl;
sess.DeletePacket(packet);
}
} while (sess.GotoNextSourceWithData());
}
sess.EndDataAccess();
RTPTime::Wait(delay);
}
sess.EndDataAccess();
return 0;
}
编译后运行如上,设置本地监听端口后就会开始等待 RTP 数据。
2. JRTPLIB发送RTP包程序
#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtplibraryversion.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtppacket.h>
#include <iostream>
#include <arpa/inet.h>
using namespace jrtplib;
void checkerror(int rtperr) {
if (rtperr < 0) {
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main() {
std::cout << "Using JRTPLib version: " << RTPLibraryVersion::GetVersion().GetVersionString() << std::endl;
uint16_t localport;
std::cout << "Enter local port(even): ";
std::cin >> localport;
std::string ipstr;
std::cout << "Enter the destination IP address: ";
std::cin >> ipstr;
uint32_t 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);
uint16_t destport;
std::cout << "Enter the destination port: ";
std::cin >> destport;
int num;
std::cout << "Number of packets you wish to be sent: ";
std::cin >> num;
RTPUDPv4TransmissionParams tranparams;
tranparams.SetPortbase(localport);
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0 / 10.0);
sessparams.SetAcceptOwnPackets(true);
// Now, we'll create a RTP session, set the destination, send some
// packets and poll for incoming data.
RTPSession sess;
int status = sess.Create(sessparams, &tranparams);
checkerror(status);
RTPIPv4Address destAddr(destip, destport);
status = sess.AddDestination(destAddr);
checkerror(status);
sess.SetDefaultPayloadType(96);
sess.SetDefaultMark(false);
for (int i = 1;i <= num;++i) {
std::cout << "Sending packet " << i << "/" << num << std::endl;
status = sess.SendPacket((void*)"0123456789", 10, 0, false, 10);
checkerror(status);
RTPTime::Wait(RTPTime(1, 0));
}
sess.BYEDestroy(RTPTime(10, 0), 0, 0);
return 0;
}
编译后运行如上,输入本地 UDP 使用的端口以及对端监听的端口,设置需要发送的 RTP 包数即可发送,发送后接收端打印的信息如下。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/164749.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...