最简 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 代码:

[objc] view plain copy
print?
  1. /*=============================================================================   
  2.  *     FileName: jrtplib_receive.cpp   
  3.  *         Desc: receive packet and print out the payloaddata     
  4.  *       Author: licaibiao   
  5.  *   LastChange: 2017-04-10   
  6.  * =============================================================================*/  
  7. #include <jrtplib3/rtpsession.h>  
  8. #include <jrtplib3/rtpudpv4transmitter.h>  
  9. #include <jrtplib3/rtpipv4address.h>  
  10. #include <jrtplib3/rtpsessionparams.h>  
  11. #include <jrtplib3/rtperrors.h>  
  12. #include <jrtplib3/rtplibraryversion.h>  
  13. #include <jrtplib3/rtppacket.h>  
  14. #include <stdlib.h>  
  15. #include <stdio.h>  
  16. #include <iostream>  
  17. #include <string>  
  18.   
  19. using namespace jrtplib;  
  20.   
  21. void checkerror(int rtperr)  
  22. {  
  23.     if (rtperr < 0)  
  24.     {  
  25.         std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;  
  26.         exit(-1);  
  27.     }  
  28. }  
  29.   
  30. int main(void)  
  31. {     
  32.     RTPSession sess;  
  33.     uint16_t portbase = 6664;  
  34.     int status;  
  35.     bool done = false;  
  36.   
  37.     RTPUDPv4TransmissionParams transparams;  
  38.     RTPSessionParams sessparams;  
  39.     sessparams.SetOwnTimestampUnit(1.0/10.0);         
  40.       
  41.     sessparams.SetAcceptOwnPackets(true);  
  42.   
  43.     transparams.SetPortbase(portbase);  
  44.     status = sess.Create(sessparams,&transparams);    
  45.     checkerror(status);  
  46.   
  47.     sess.BeginDataAccess();  
  48.     RTPTime delay(0.020);  
  49.     RTPTime starttime = RTPTime::CurrentTime();  
  50.       
  51.     while (!done)  
  52.     {  
  53.         status = sess.Poll();  
  54.         checkerror(status);  
  55.               
  56.         if (sess.GotoFirstSourceWithData())  
  57.         {  
  58.             do  
  59.             {  
  60.                 RTPPacket *pack;  
  61.                   
  62.                 while ((pack = sess.GetNextPacket()) != NULL)  
  63.                 {  
  64.                     std::cout << pack->GetPayloadData() << std::endl;  
  65.                     sess.DeletePacket(pack);  
  66.                 }  
  67.             } while (sess.GotoNextSourceWithData());  
  68.         }  
  69.                   
  70.         RTPTime::Wait(delay);  
  71.         RTPTime t = RTPTime::CurrentTime();  
  72.         t -= starttime;  
  73.         if (t > RTPTime(60.0))  
  74.             done = true;  
  75.     }  
  76.       
  77.     sess.EndDataAccess();  
  78.     delay = RTPTime(10.0);  
  79.     sess.BYEDestroy(delay,0,0);  
  80.       
  81.     return 0;  
  82. }  
    上面程序,以每0.02s的周期去查询是否有接收到数据,持续60s,接收到数据之后,将接收到的数据通过标准输出打印出来。

jrtplib_send.cpp 代码:

[objc] view plain copy
print?
  1. /*=============================================================================   
  2.  *     FileName: jrtplib_send.cpp   
  3.  *         Desc: sending packets to  destination port     
  4.  *       Author: licaibiao   
  5.  *   LastChange: 2017-04-10   
  6.  * =============================================================================*/  
  7. #include <jrtplib3/rtpsession.h>  
  8. #include <jrtplib3/rtpudpv4transmitter.h>  
  9. #include <jrtplib3/rtpipv4address.h>  
  10. #include <jrtplib3/rtpsessionparams.h>  
  11. #include <jrtplib3/rtperrors.h>  
  12. #include <jrtplib3/rtplibraryversion.h>  
  13. #include <stdlib.h>  
  14. #include <stdio.h>  
  15. #include <iostream>  
  16. #include <string>  
  17.   
  18. using namespace jrtplib;  
  19.   
  20. void checkerror(int rtperr)  
  21. {  
  22.     if (rtperr < 0)  
  23.     {  
  24.         std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;  
  25.         exit(-1);  
  26.     }  
  27. }  
  28.   
  29. int main(void)  
  30. {     
  31.     int i;  
  32.     int num;  
  33.     int status;  
  34.       
  35.     RTPSession sess;  
  36.     uint16_t portbase = 6666;    
  37.     uint16_t destport = 6664;  
  38.   
  39. #if 0  
  40.     uint32_t destip;      
  41.     destip    = inet_addr("192.168.0.6");   
  42.     if (destip == INADDR_NONE)  
  43.     {  
  44.         std::cerr << "Bad IP address specified" << std::endl;  
  45.         return -1;  
  46.     }  
  47.     destip = ntohl(destip);  
  48. #else  
  49.     uint8_t destip[]={192,168,0,6};  
  50. #endif   
  51.   
  52.     std::cout << "Number of packets you wish to be sent:" << std::endl;  
  53.     std::cin >> num;  
  54.       
  55.     RTPUDPv4TransmissionParams transparams;  
  56.     RTPSessionParams sessparams;  
  57.       
  58.     sessparams.SetOwnTimestampUnit(1.0/10.0);         
  59.       
  60.     sessparams.SetAcceptOwnPackets(true);  
  61.     transparams.SetPortbase(portbase);  
  62.     status = sess.Create(sessparams,&transparams);    
  63.     checkerror(status);  
  64.       
  65.     RTPIPv4Address addr(destip,destport);  
  66.       
  67.     status = sess.AddDestination(addr);  
  68.     checkerror(status);  
  69.       
  70.     for (i = 1 ; i <= num ; i++)  
  71.     {  
  72.         printf("\nSending packet %d/%d\n",i,num);  
  73.           
  74.         status = sess.SendPacket((voidvoid *)"1234567890",10,0,false,10);  
  75.         checkerror(status);       
  76.         RTPTime::Wait(RTPTime(1,0));  
  77.     }  
  78.       
  79.     sess.BYEDestroy(RTPTime(10,0),0,0);  
  80.       
  81.     return 0;  
  82. }  
    上面程序,通过标准输入确定需要发送数据的包数,然后以1s的周期发送数据。这里是p2p传输,发送之前需要指定发送地址和发送端口。这里的地址就是接收端主机的IP地址,这里的目的端口,就是接收端程序设置的本地端口。

Makefile文件:

[python] view plain copy
print?
  1. APP = test  
  2. LINK_OPTS = -ljrtp  
  3. OBJ  = jrtplib_receive.cpp  
  4. #OBJ  = jrtplib_send.cpp  
  5. out:   
  6.     g++ $(OBJ) -o $(APP)  $(LINK_OPTS)  
  7. clean:  
  8.     rm -rf *o $(APP)  
编译执行部分结果:

发送端:

[objc] view plain copy
print?
  1. licaibiao@lcb:~/test/RTP/test_jrtplib$ ./test   
  2. Number of packets you wish to be sent:  
  3. 100  
  4.   
  5. Sending packet 1/100  
  6.   
  7. Sending packet 2/100  
  8.   
  9. Sending packet 3/100  
  10.   
  11. Sending packet 4/100  
  12.   
  13. Sending packet 5/100  
  14.   
  15. Sending packet 6/100  

接收端:

[objc] view plain copy
print?
  1. licaibiao@ubuntu:~/test/RTP_TEST/JRTPLIB/biao$ ./test   
  2. 1234567890  
  3. 1234567890  
  4. 1234567890  
  5. 1234567890  
  6. 1234567890  
  7. 1234567890  
  8. 1234567890  
  9. 1234567890   

    注意:在编译运行本程序之前,需要正确安装好jrtplib 

   工程代码可在这里下载:最简jrtplib 收发数据实例





    


原创粉丝点击