jrtplib开源库系列之二:jrtplib库example1分析说明

简介: 这个官方example还是比较简单的,主要是测试发一个数据包,然后自己接收发出的包,注意,这里需要注意将目的地址的端口和基端口(portbase)设置为相同,同时需要将这个参数设置为真sessparams.SetAcceptOwnPackets(true);

源代码

void checkerror(int rtperr)
{
  if (rtperr < 0)
  {
  std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
  exit(-1);
  }
}
//
// The main routine
//
int main(void)
{
#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;//设置目的地址的端口,如果测试自己给自己发送的包,请和portbase设置一致
  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)
  //在其他应用中,必须设置这个参数,比如使用RTP协议传输H264,这里必须设置为1/90000,关于如何使用RTP传输H264,请参考RFC3984协议规范
  sessparams.SetOwnTimestampUnit(1.0/10.0); 
  sessparams.SetAcceptOwnPackets(true);//注意,如果想测试自己调用自己发送的包,需要设置为true
  transparams.SetPortbase(portbase);
  status = sess.Create(sessparams,&transparams);  
  checkerror(status);
  RTPIPv4Address addr(destip,destport);
  status = sess.AddDestination(addr);
  checkerror(status);
  for(int i = 0; i < num; ++i)
  {
  // send the packet
  status = sess.SendPacket((void *)"1234567890",10,0,false,10);
       checkerror(status);
    //由于可能使用多线程,需要保证数据源数据在多线程下是安全的
    //用户在获取其他数据源发送数据之前需要调用这个接口
    //在调用完成之后需要调用EndDataAccess()接口
  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 0;
}

代码说明

这个官方example还是比较简单的,主要是测试发一个数据包,然后自己接收发出的包,注意,这里需要注意将目的地址的端口和基端口(portbase)设置为相同,同时需要将这个参数设置为真sessparams.SetAcceptOwnPackets(true);


相关文章
jrtplib开源库系列之一:jrtplib介绍、安装和测试(window 10环境介绍)
关于jrtplib库网上已经有很多介绍,而且目前jrtplib作者已经停止更新(Apr 18, 2020),最新版本为v3.11.2。本系列内容也以该版本进行介绍。 相信你已经对RTP/RTCP协议有一定的了解,并想更深入的了解RTP协议的具体实现,jrtplib就是使用使用C++实现的RTP/RTCP协议。具体标准为RFC3550,如果想仔细阅读原文,但是对英文又有点吃力,可以参考我的博客RTP/RTCP中英文对照,在博客的后面有百度链接,是对RFC3550的中文翻译,可能很多地方不太准确,有些内容是自己添加进去的,希望不会影响你的阅读。
312 0
|
存储 编解码 算法
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换(一)
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换
1092 1
|
API 数据安全/隐私保护
jrtplib开源库系列之三:jrtplib发送接收数据流程
前面2篇文章主要说明了如何安装jrtplib库,以及对example1进行了说明,这篇文章主要说下jrtplib库数据的收发流程。
699 0
|
12月前
|
存储 Linux 编译器
cmake的单目录和多目录的使用(Linux和Windows)
本文介绍了在Windows和Linux平台上使用CMake构建单目录和多目录项目的步骤,包括如何配置CMakeLists.txt文件以及如何生成和使用可执行文件、库文件。
516 2
|
编解码 Linux
libfdk-aac和x264安装编译
libfdk-aac和x264安装编译
613 0
libfdk-aac和x264安装编译
|
数据可视化
【Qt 学习笔记】Qt常用控件 | 输入类控件 | Date/Time Edit的使用及说明
【Qt 学习笔记】Qt常用控件 | 输入类控件 | Date/Time Edit的使用及说明
1415 2
|
安全
qt.qpa.xcb: could not connect to display 问题解决
【5月更文挑战第16天】qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. 问题解决
6899 1
|
人工智能 Ubuntu Shell
【Ubuntu工具】安装教程:Ubuntu系统上源码编译安装QT5.15.13(有坑)
【Ubuntu工具】安装教程:Ubuntu系统上源码编译安装QT5.15.13(有坑)
2029 0
|
编解码 网络协议 程序员
【RTP 传输协议】实时视频传输的艺术:深入探索 RTP 协议及其在 C++ 中的实现
【RTP 传输协议】实时视频传输的艺术:深入探索 RTP 协议及其在 C++ 中的实现
2009 0
阿里云域名优惠:批量注册和续费优惠价格表
阿里云域名优惠:批量注册和续费优惠价格表,com和cn域名均有活动
1015 5