一、简述

RTP 是目前解决流媒体实时传输问题的最好办法,而JRTPLIB 是一个用C++语言实现的RTP库,包括UDP通讯。刚使用JRTPLIB,对JRTPLIB的理解还不够深,当做使用时,积累的一些经验写个笔记吧。

二、RTP协议

实时传送协议(Real-time Transport Protocol或简写RTP,也可以写成RTTP)是一个网络传输协议,RTP协议详细说明了在互联网上传递音频和视频的标准数据包格式。它一开始被设计为一个多播协议,但后来被用在很多单播应用中。RTP协议常用于流媒体系统(配合RTCP协议或者RTSP协议)。因为RTP自身具有Time stamp所以在ffmpeg 中被用做一种formate。

RTP协议的详细介绍,请参考这篇文章http://www.360doc.com/content/11/1009/15/496343_154624612.shtml

三、RTPSession类

这里不介绍jrtplib的编译安装,这个很简单,网上很多地方都有讲解。

jrtplib的使用中,主要是围绕这个类来实现的,因此大家有必要去查看源码,看这类的实现。为了方便使用,我在这做了RTPSession的继承封装,下面直接贴代码了。

RTPSessionUtils.h

[cpp] view plaincopyprint?
  1. #include "rtpsession.h"
  2. #include "rtppacket.h"
  3. #include "rtpudpv4transmitter.h"
  4. #include "rtpipv4address.h"
  5. #include "rtpsessionparams.h"
  6. #include "rtperrors.h"
  7. #ifndef WIN32
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #else
  11. #include <winsock2.h>
  12. #endif // WIN32
  13. #include "rtpsourcedata.h"
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <iostream>
  17. #include <string>
  18. //jrtplib应用需链接的lib
  19. #pragma comment(lib,"ws2_32.lib")
  20. #pragma comment(lib, "jrtplib_d.lib")
  21. #pragma comment(lib,"jthread_d.lib")
  22. namespace jrtplib
  23. {
  24. class RTPSessionUtils : public RTPSession
  25. {
  26. typedef RTPSession base_type;
  27. public:
  28. RTPSessionUtils();
  29. ~RTPSessionUtils();
  30. int AddDestination(const std::string& ip, uint16_t port);
  31. int DeleteDestination(const std::string& ip, uint16_t port);
  32. int CreateDefault(uint16_t port);
  33. protected:
  34. void OnNewSource(RTPSourceData *dat);
  35. void OnBYEPacket(RTPSourceData *dat);
  36. void OnRemoveSource(RTPSourceData *dat);
  37. void OnRTPPacket(RTPPacket *pack,const RTPTime &receivetime,
  38. const RTPAddress *senderaddress);
  39. void OnRTCPCompoundPacket(RTCPCompoundPacket *pack,const RTPTime &receivetime,
  40. const RTPAddress *senderaddress);
  41. void OnPollThreadStep();
  42. private:
  43. int GetAddrFromSource(RTPSourceData *dat, uint32_t& ip, uint16_t& port);
  44. };
  45. }
  46. //整形的ip转成字符串ip
  47. static std::string IPToString(const unsigned int iIP)
  48. {
  49. struct in_addr inaddr;
  50. inaddr.s_addr = htonl(iIP);
  51. return std::string(inet_ntoa(inaddr));
  52. }
  53. //字符串ip转成整形ip
  54. static unsigned int IPToInt(const std::string& sIP)
  55. {
  56. return inet_addr(sIP.c_str());
  57. }

RTPSessionUtils.cpp

[cpp] view plaincopyprint?
  1. #include "RTPSessionUtils.h"
  2. namespace jrtplib{
  3. RTPSessionUtils::RTPSessionUtils()
  4. {
  5. #ifdef WIN32
  6. WSADATA dat;
  7. WSAStartup(MAKEWORD(2,2),&dat);
  8. #endif // WIN32
  9. }
  10. RTPSessionUtils::~RTPSessionUtils()
  11. {
  12. #ifdef WIN32
  13. WSACleanup();
  14. #endif // WIN32
  15. }
  16. int RTPSessionUtils::CreateDefault(uint16_t port)
  17. {
  18. RTPUDPv4TransmissionParams transparams;
  19. RTPSessionParams sessparams;
  20. sessparams.SetOwnTimestampUnit(1.0/10.0);//必须设置
  21. transparams.SetPortbase(port);//port必须是偶数
  22. return base_type::Create(sessparams, &transparams);
  23. base_type::SetDefaultPayloadType(0);
  24. base_type::SetDefaultTimestampIncrement(0);
  25. base_type::SetDefaultMark(false);
  26. }
  27. int RTPSessionUtils::AddDestination(const std::string& ip, uint16_t port)
  28. {
  29. return base_type::AddDestination(RTPIPv4Address(ntohl(inet_addr(ip.c_str())), port));
  30. }
  31. int RTPSessionUtils::DeleteDestination(const std::string& ip, uint16_t port)
  32. {
  33. return base_type::DeleteDestination(RTPIPv4Address(ntohl(inet_addr(ip.c_str())), port));
  34. }
  35. int RTPSessionUtils::GetAddrFromSource(RTPSourceData *dat, uint32_t& ip, uint16_t& port)
  36. {
  37. if (dat->IsOwnSSRC())
  38. return -1;
  39. if (dat->GetRTPDataAddress() != 0)
  40. {
  41. const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTPDataAddress());
  42. ip = addr->GetIP();
  43. port = addr->GetPort();
  44. }
  45. else if (dat->GetRTCPDataAddress() != 0)
  46. {
  47. const RTPIPv4Address *addr = (const RTPIPv4Address *)(dat->GetRTCPDataAddress());
  48. ip = addr->GetIP();
  49. port = addr->GetPort()-1;
  50. }
  51. return 0;
  52. }
  53. void RTPSessionUtils::OnNewSource(RTPSourceData *dat)
  54. {
  55. uint32_t ip;
  56. uint16_t port;
  57. if (GetAddrFromSource(dat, ip, port))
  58. return;
  59. RTPIPv4Address dest(ip,port);
  60. base_type::AddDestination(dest);
  61. std::cout << "OnNewSource Adding destination " << IPToString(ip) << ":" << port << std::endl;
  62. }
  63. void RTPSessionUtils::OnRemoveSource(RTPSourceData *dat)
  64. {
  65. if (dat->ReceivedBYE())
  66. return;
  67. uint32_t ip;
  68. uint16_t port;
  69. if (GetAddrFromSource(dat, ip, port))
  70. return;
  71. RTPIPv4Address dest(ip,port);
  72. base_type::DeleteDestination(dest);
  73. std::cout << "OnRemoveSource Deleting destination " << IPToString(ip) << ":" << port << std::endl;
  74. }
  75. void RTPSessionUtils::OnBYEPacket(RTPSourceData *dat)
  76. {
  77. uint32_t ip;
  78. uint16_t port;
  79. if (GetAddrFromSource(dat, ip, port))
  80. return;
  81. RTPIPv4Address dest(ip,port);
  82. base_type::DeleteDestination(dest);
  83. std::cout << "OnBYEPacket Deleting destination " << IPToString(ip) << ":" << port << std::endl;
  84. }
  85. //只要有rtp包就会触发
  86. void RTPSessionUtils::OnRTPPacket(RTPPacket *pack,const RTPTime &receivetime,
  87. const RTPAddress *senderaddress)
  88. {
  89. std::cout << "OnRTPPacket: data:" << pack->GetPayloadData() << std::endl;
  90. }
  91. //收到rtcp包触发
  92. void RTPSessionUtils::OnRTCPCompoundPacket(RTCPCompoundPacket *pack,const RTPTime &receivetime,
  93. const RTPAddress *senderaddress)
  94. {
  95. std::cout << "OnRTCPCompoundPacket: data:" << pack->GetCompoundPacketData() << std::endl;
  96. }
  97. //隔段时间就会触发,也可以用于收包回调函数
  98. //void RTPSessionUtils::OnPollThreadStep()
  99. //{
  100. //  BeginDataAccess();
  101. //  // check incoming packets
  102. //  if (GotoFirstSourceWithData())
  103. //  {
  104. //      do
  105. //      {
  106. //          RTPPacket *pack;
  107. //          RTPSourceData *srcdat;
  108. //          srcdat = GetCurrentSourceInfo();
  109. //          while ((pack = GetNextPacket()) != NULL)
  110. //          {
  111. //              std::cout << "Got packet " << pack->GetExtendedSequenceNumber() << " from SSRC " << srcdat->GetSSRC() << std::endl;
  112. //              DeletePacket(pack);
  113. //          }
  114. //      } while (GotoNextSourceWithData());
  115. //  }
  116. //  EndDataAccess();
  117. //}
  118. }

server.cpp

[cpp] view plaincopyprint?
  1. #include <iostream>
  2. #include "RTPSessionUtils.h"
  3. using namespace jrtplib;
  4. void main()
  5. {
  6. int status;
  7. RTPSessionUtils sess;
  8. status = sess.CreateDefault(8888);
  9. if(status)
  10. {
  11. std::cout << "RTP error:" << RTPGetErrorString(status) << std::endl;
  12. return;
  13. }
  14. while (1)
  15. {
  16. std::string buf;
  17. std::cout << "Input send data:" ;
  18. std::cin >> buf;
  19. sess.SendPacket((void*)buf.c_str(), buf.length(), 0, false, 0);
  20. if(status)
  21. {
  22. std::cout << "RTP error:" << RTPGetErrorString(status) << std::endl;
  23. continue;
  24. }
  25. }
  26. system("pause");
  27. }

client.cpp

[cpp] view plaincopyprint?
  1. #include <iostream>
  2. #include "RTPSessionUtils.h"
  3. using namespace jrtplib;
  4. void main()
  5. {
  6. int status;
  7. RTPSessionUtils sess;
  8. status = sess.CreateDefault(6666);
  9. if(status)
  10. {
  11. std::cout << "RTP error:" << RTPGetErrorString(status) << std::endl;
  12. return;
  13. }
  14. status = sess.AddDestination("127.0.0.1", 8888);
  15. if(status)
  16. {
  17. std::cout << "RTP error:" << RTPGetErrorString(status) << std::endl;
  18. return;
  19. }
  20. while (1)
  21. {
  22. std::string buf;
  23. std::cout << "Input send data:" ;
  24. std::cin >> buf;
  25. sess.SendPacket((void*)buf.c_str(), buf.length(), 0, false, 0);
  26. if(status)
  27. {
  28. std::cout << "RTP error:" << RTPGetErrorString(status) << std::endl;
  29. continue;
  30. }
  31. }
  32. system("pause");
  33. }

jrtplib使用笔记相关推荐

  1. JRtplib开发笔记(二):JRtplib库编译、示例演示

    原博主博客地址:https://blog.csdn.net/qq21497936 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/84 ...

  2. JRtplib开发笔记(四):JRtplib的VS开发环境搭建以及Demo

    原博主博客地址:https://blog.csdn.net/qq21497936 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/84 ...

  3. jrtplib学习笔记1

    1.下载jrtplib 3.5.2,要注意有时下载后,其中有些头文件是空的,如果这样的话,可以用相近版本的头文件来代替,或者到其它地方重新下载 2.编译jthread工程,并生成jthread.lib ...

  4. jrtplib使用注意事项

    一.说明 RTP 现在的问题是要解决的流媒体的实时传输的问题的最佳方法.和JRTPLIB 是一个用C++语言实现的RTP库.包含UDP通讯.刚使用JRTPLIB,对JRTPLIB的理解还不够深,当做使 ...

  5. H264和H265的学习笔记

    H264和H265的学习笔记记录分享 一.H264的组成: 二.H265的组成: 三.H264和H265由es打包为pes再打包为ps的代码思路如下: 四.补充说明 一.H264的组成: 1.H264 ...

  6. 【读书笔记】知易行难,多实践

    前言: 其实,我不喜欢看书,只是喜欢找答案,想通过专业的解答来解决我生活的困惑.所以,我听了很多书,也看了很多书,但看完书,没有很多的实践,导致我并不很深入在很多时候. 分享读书笔记: <高效1 ...

  7. 【运维学习笔记】生命不息,搞事开始。。。

    001生命不息,搞事不止!!! 这段时间和hexesdesu搞了很多事情! 之前是机械硬盘和固态硬盘的测速,我就在那默默的看着他一个硬盘一个机械测来测去. 坐在他后面,每天都能看到这位萌萌的小男孩,各 ...

  8. SSAN 关系抽取 论文笔记

    20210621 https://zhuanlan.zhihu.com/p/353183322 [KG笔记]八.文档级(Document Level)关系抽取任务 共指id嵌入一样 但是实体嵌入的时候 ...

  9. pandas以前笔记

    # -*- coding: utf-8 -*- """ Created on Sat Jul 21 20:06:20 2018@author: heimi "& ...

  10. PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call

    您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...

最新文章

  1. TF-IDF 原理及sklearn中的tf-idf实例分析
  2. 激光雷达 win10
  3. python有趣代码-一个有意思的 Python 训练项目集
  4. Objective-C Runtime
  5. 微信自动回复和自动抢红包实现原理(二):自动回复
  6. Eclipse新建web项目出现The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path
  7. 如何学习机器学习、看待算法竞赛?粉丝精选留言
  8. c语言下列循环的循环次数,在C语言中,若i=3,则语句 while (i) { i--; break;}的循环次数为 答案:1...
  9. linux怎样禁止他人远程,linux禁止用户远程登录的方法
  10. JSP→JavaWeb简介、Tomcat服务器安装启动测试目录、Tomcat手动创建项目、开发工具MyEclipse与Eclipse配置环境、MyEclipse创建Web程序目录、修改Tomcat端口
  11. 区块链100讲:详解Po.et 技术栈
  12. 【目标跟踪】基于matlab红外图像弱小目标检测与跟踪【含Matlab源码 374期】
  13. SpringCloud Netflix—微服务架构
  14. 2008 r2 server sql 中文版补丁_Microsoft SQL Server 2008 r2 sp2补丁 64位 官方免费版
  15. Hive集成Tez让大象飞起来
  16. mysql入门_高洛峰_简介_linux安装_远程连接配置_sql语句初始
  17. FFmpeg创作GIF表情包教程来了!赶紧说声多谢乌蝇哥?
  18. php 截取取最后一个字符
  19. A hybrid method of exponential smoothing and recurrent
  20. nav 计算机网络_计算机网络课件谢希仁

热门文章

  1. via浏览器如何拦截广告
  2. SVG-edit 是一个快速的、基于 Web 的、由 JavaScript 驱动的 SVG 绘图编辑器
  3. 研究svg编辑器过程中遇到的问题总结
  4. 小白版----使用vm安装win10
  5. unity序列帧动画的粒子特效
  6. ISO 28000供应链安全管理简述及标准
  7. PhotoShop简单案例(1)——利用时间轴功能制作简单动画
  8. Python 爬虫 书籍爬取实例
  9. 4份简约实用自我介绍PPT模板,总有一份适合你
  10. 怎么用谷歌学术检索下载外文文献