最简 jrtplib 收发数据实例
来源:互联网 发布:淘宝上井江茶油造假 编辑:程序博客网 时间:2024/06/05 19:19
【来源】http://blog.csdn.net/li_wen01/article/details/70045185
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;
- }
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((voidvoid *)"1234567890",10,0,false,10);
- checkerror(status);
- RTPTime::Wait(RTPTime(1,0));
- }
- sess.BYEDestroy(RTPTime(10,0),0,0);
- return 0;
- }
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 收发数据实例
- 最简 jrtplib 收发数据实例
- 最简 jrtplib 收发数据实例
- 最简 jrtplib 收发数据实例——非常详细简洁
- Jrtplib的最基本的操作实例
- 分析jrtplib收发RTCP包
- 分析jrtplib收发RTCP包
- linux 使用jrtplib收发h.264视频文件
- JRTPLIB使用实例
- JRTPLIB使用实例
- JRTPLIB使用实例
- JRTPLIB使用实例
- JRTPLIB使用实例
- JRTPLIB使用实例
- JRTPLIB使用实例
- Java最简单的UDP收发数据的例子
- Jrtplib收发H264文件 + FFMPEG解码+VFW播放视频
- Python实例 -- 收发邮件
- ActiveMQ-1.收发消息之最简例子
- 553. Optimal Division
- android安卓调用相册及相机并将图片保存本地
- 解决Vue项目在IE浏览器中显示空白的问题
- 数据库的备份
- 编码器学习第二记—STM32正点原子第14章学习
- 最简 jrtplib 收发数据实例
- Java 线程简介
- java-static关键解读
- PYTHON smtp 发送qq邮件
- vue中如何使用less
- Sqrt(x)
- 实现文本域输入字数的限制
- pcl::ExtractPolygonalPrismData类
- keras2 predict和fit_generator的坑