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 代码:
-
-
-
-
-
-
- #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 代码:
-
-
-
-
-
-
- #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;
- }
上面程序,通过标准输入确定需要发送数据的包数,然后以1s的周期发送数据。这里是p2p传输,发送之前需要指定发送地址和发送端口。这里的地址就是接收端主机的IP地址,这里的目的端口,就是接收端程序设置的本地端口。
Makefile文件:
- APP = test
- LINK_OPTS = -ljrtp
- OBJ = jrtplib_receive.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 收发数据实例