ortp

 http://ticktick.blog.51cto.com/823160/345642

    我們知道, RTP(Real-timeTransportProtocol)是用於Internet上針對多媒體數據流的一種傳輸協議,做流媒體傳輸方面的應用離不開RTP協議的實現及使用,爲了更加快速地在項目中應用RTP協議實現流媒體的傳輸,我們一般會選擇使用一些RTP庫,例如使用c++語言編寫的JRTPLIB庫,網上關於RTP協議以及JRTPLIB庫的介紹已經很多了,在此我也不再贅述,文本主要介紹實現了RTP協議的另一種開源庫——ORTP庫,這個庫是純使用c語言編寫,由於我們的項目是基於Linux下的c語言編程,故我們選擇了ortp作爲我們的第三方庫,在此我也對該庫進行一個簡單地介紹,希望對其他ortp的初學者有所幫助。

一、簡介

ORTP是一個支持RTP以及RFC3550協議的庫,有如下的特性:
(1)使用C語言編寫,可以工作於windows, Linux, 以及 Unix平臺
(2)實現了RFC3550協議,提供簡單易用的API。支持多種配置,RFC3551爲默認的配置。
(3)支持單線程下的多個RTP會話,支持自適應抖動處理。
(4)基於GPL版權聲明。

ORTP可以在其官方網站上(http://www.linphone.org/index.php/eng/code_review/ortp)下載,下載解壓後得到ORTP的源碼包和示例程序(tests)。其幫助文檔在docs目錄下,也可以在http://download.savannah.gnu.org/releases/linphone/ortp/docs/在線查看。


關於ORTP的資料並不多,主要是其源碼、幫助文檔以及示例程序,關於示例程序說明如下:
rtprecv.c 和rtpsend.c 展示瞭如何接收和發送單RTP數據流。
mrtprecv.c mrtpsend.c 展示瞭如何同時接收和發送多個RTP數據流。

二、主要函數介紹

 rtp_session_init

函數原型:void rtp_session_init (RtpSession * session, int mode)
函數功能:執行rtp會話的一些必要的初始化工作
參數含義:
session: rtp會話結構體,含有一些rtp會話的基本信息
mode   傳輸模式,有以下幾種,決定本會話的一些特性。
RTP_SESSION_RECVONLY:只進行rtp數據的接收
RTP_SESSION_SENDONLY:只進行rtp數據的發送
RTP_SESSION_SENDRECV:可以進行rtp數據的接收和發送
 
執行的操作:
1.       設置rtp包緩衝隊列的最大長度
2.       根據傳輸模式設置標誌變量的值
3.       隨機產生SSRC和同步源描述信息
4.       傳入全局的av_profile,即使用默認的profile配置
5.       初始化rtp包緩衝區隊列
6.       發送負載類型默認設置爲0(pcmu音頻),接收負載類型默認設置爲-1(未定義)
7.       session的其他成員的值均設置一個默認值。
 
rtp_session_set_scheduling_mode
函數原型:void rtp_session_set_scheduling_mode (RtpSession * session, int yesno)
函數功能: RtpScheduler管理多個session的調度和收發的控制,本函數設置是否使用該session調度管理功能。
參數含義:
session: rtp會話結構體
yesno:   是否使用rtp session的系統調度功能
說明:
如果yesno爲1,則表明使用系統的session調度管理功能,意味着可以使用以下功能:
1. 可以使用session_set_select在多個rtp會話之間進行選擇,根據時間戳判定某個會話是否到達了收發的時間。
2. 可以使用rtp_session_set_blocking_mode()設置是否使用阻塞模式來進行rtp包的發送和接收。
如果yesno爲0,則表明該會話不受系統管理和調度。
關於rtp session的管理和調度,由全局的變量RtpScheduler *__ortp_scheduler來負責,該變量必須通過ortp_scheduler_init() 來進行初始化操作。
 
rtp_session_set_blocking_mode
函數原型:void rtp_session_set_blocking_mode (RtpSession * session, int yesno)
函數功能:設置是否使用阻塞模式,
參數含義:
session: rtp會話結構體
yesno:  是否使用阻塞模式
說明:
阻塞模式只有在scheduling mode被開啓的情況下才能使用,本函數決定了rtp_session_recv_with_ts() 和 rtp_session_send_with_ts()兩個函數的行爲,如果啓用了阻塞模式,則rtp_session_recv_with_ts()會一直阻塞直到接收RTP包的時間點到達(這個時間點由該函數參數中所定義的時間戳來決定),當接收完RTP數據包後,該函數纔會返回。同樣,rtp_session_send_with_ts()也會一直阻塞直到需要被髮送的RTP包的時間點到達,發送結束後,函數才返回。
 
rtp_session_signal_connect
函數原型:int rtp_session_signal_connect (RtpSession * session, const char *signal,   RtpCallback cb, unsigned long user_data)
函數功能:本函數提供一種方式,用於通知應用程序各種可能發生的RTP事件(信號)。可能通過註冊回調函數的形式來實現本功能。
參數含義:
session: rtp會話結構體
signal: 信號的名稱
cb:     回調函數
user_data:傳遞給回調函數的數據
返回值:0表示成功,-EOPNOTSUPP表示信號名稱不存在,-1表示回調函數綁定錯誤
說明:
信號的名稱必須是以下字符串中的一種:
"ssrc_changed" : 數據流的同步源標識改變
"payload_type_changed" : 數據流的負載類型改變
"telephone-event_packet" : telephone-event RTP包(RFC2833)被接收
"telephone-event" : telephone event 發生
"network_error" : 網絡錯誤產生,傳遞給回調函數的是描述錯誤的字符串(const char *型)或者錯誤碼(int型)
"timestamp_jump" : 接收到的數據包發生了時間戳的跳躍。
要取消事件(信號)的監聽,可以使用下面這個函數
int rtp_session_signal_disconnect_by_callback ( RtpSession * session, const char * signal_name, RtpCallback cb )
 
rtp_session_set_local_addr
函數原型:int rtp_session_set_local_addr( RtpSession * session, const char * addr,int port)
函數功能:設置本地rtp數據監聽地址
參數含義:
session: rtp會話結構體
addr: 本地IP地址,例如127.0.0.1,如果爲NULL,則系統分配0.0.0.0
port:   監聽端口,如果設置爲-1,則系統爲其自動分配端口
返回值: 0表示成功
說明:
如果是RTP_SESSION_SENDONLY(只發送)型會話,則不需要進行本設置,而必須設置rtp_session_set_remote_addr() 來設置遠程目的地址。
如果採用了系統自動分配監聽端口,則可以通過int rtp_session_get_local_port(const RtpSession *session) 來獲取系統分配的監聽端口號。
 
rtp_session_set_remote_addr
函數原型:int rtp_session_set_remote_addr (RtpSession * session, const char * addr, int port)
函數功能:設置RTP發送的目的地址
參數含義:
session: rtp會話結構體
addr: 目的IP地址
port:   目的地址的監聽端口號
返回值: 0表示成功
 
rtp_session_set_send_payload_type
函數原型:int rtp_session_set_send_payload_type (RtpSession * session, int paytype)
函數功能:設置RTP發送數據的負載類型
參數含義:
session: rtp會話結構體
paytype:負載類型
返回值: 0表示成功,-1表示負載未定義
說明:
負載類型在payloadtype.h文件中有詳細的定義,RTP接收端有着類似的負載類型設置函數,int rtp_session_set_recv_payload_type ( RtpSession * session, int paytype ) ,注意,發送的負載類型必須與接收的負載類型一致才能正常完成收發。
 
rtp_session_send_with_ts
函數原型:int rtp_session_send_with_ts (RtpSession * session, const char * buffer, int len,uint32_t userts)
函數功能:發送RTP數據包
參數含義:
session: rtp會話結構體
buffer: 需要發送的RTP數據的緩衝區
len:    需要發送的RTP數據的長度
userts: 本RTP數據包的時間戳
返回值: 成功發送到網絡中的字節數
說明:
發送RTP數據需要自己管理時間戳的遞增,每調用一次本函數,請根據實際情況對userts進行遞增,具體遞增的規則見RTP協議中的說明。
例如:如果發送的是採樣率爲90000Hz的視頻數據包,每秒25幀,則時間戳的增量爲:90000/25 = 3600
時間戳的起始值爲隨機值,建議設置爲0 。
 
rtp_session_recv_with_ts
函數原型:int rtp_session_recv_with_ts (RtpSession * session, char * buffer,int len, uint32_t time, int * have_more)
函數功能:接收RTP數據包
參數含義:
session: rtp會話結構體
buffer: 存放接收的RTP數據的緩衝區
len:    期望接收的RTP數據的長度
time:   期望接收的RTP數據的時間戳
have_more:標識接收緩衝區是否還有數據沒有傳遞完。當用戶給出的緩衝區不夠大時,爲了標識緩衝區數據未取完,則have_more指向的數據爲1,期望用戶以同樣的時間戳再次調用本函數;否則爲0,標識取完。

rtp_session_destroy
【原型】: void rtp_session_destroy(RtpSession *session)
【功能】:摧毀rtp會話對象,釋放資源
【參數】:session已經創建的RTP會話對象
 

三、程序示例

      下面,我簡單地通過程序演示了怎麼使用ortp進行rtp數據包的發送,接收端的程序待以後有時間再整理出來吧。

       注:示例代碼我已經整理出來了,見博文: 《ortp編程示例代碼》

  1. //////////////////////////////////////////////////////////////////////////     
  2. /// COPYRIGHT NOTICE     
  3.  // Copyright (c) 2009, 華中科技大學ticktick Group     
  4. /// All rights reserved.      
  5. ///      
  6. /// @file    ortpSend.c       
  7. /// @brief   ortpSend的測試     
  8. ///     
  9. /// 本文件示例使用ortp庫進行rtp數據包的發送    
  10. ///      
  11. /// @version 1.0        
  12. /// @author  tickTick   
  13. /// @date    2010/07/07      
  14. /// @E-mail  [email protected]     
  15. ///     
  16. /// 修訂說明:創建文件    
  17. //////////////////////////////////////////////////////////////////////////      
  18.  
  19. #include <ortp/ortp.h>  
  20. #include <signal.h>  
  21. #include <stdlib.h>  
  22.  
  23. #ifndef _WIN32   
  24. #include <sys/types.h>  
  25. #include <sys/time.h>  
  26. #include <stdio.h>  
  27. #endif  
  28.  
  29. // 時間戳增量
  30. #define TIME_STAMP_INC  160   
  31. #define BYTES_PER_COUNT 65535
  1. // 時間戳   
  2. uint32_t g_user_ts;  
  3.  
  4. /**  初始化     
  5.  *       
  6.  *   主要用於對ortp以及其它參數進行初始化    
  7.  *   @param:  char * ipStr 目的端IP地址描述串     
  8.  *   @param:  iint port 目的端RTP監聽端口     
  9.  *   @return:  RtpSession * 返回指向RtpSession對象的指針,如果爲NULL,則初始化失敗     
  10.  *   @note:       
  11.  */   
  12. RtpSession * rtpInit(char * ipStr,int port)  
  13. {  
  14.     // Rtp會話對象    
  15.     RtpSession *session;  
  16.     char *ssrc;  
  17.       
  18.     // 時間戳初始化    
  19.     g_user_ts = 0;  
  20.  
  21.     // ortp的一些基本初始化操作   
  22.     ortp_init();  
  23.     ortp_scheduler_init();  
  24.     // 創建新的rtp會話對象  
  25.     session=rtp_session_new(RTP_SESSION_SENDONLY);    
  26.       
  27.     rtp_session_set_scheduling_mode(session,1);  
  28.     rtp_session_set_blocking_mode(session,1);  
  29.     // 設置遠程RTP客戶端的的IP和監聽端口(即本rtp數據包的發送目的地址)   
  30.     rtp_session_set_remote_addr(session,ipStr,port);  
  31.     // 設置負載類型    
  32.     rtp_session_set_payload_type(session,0);  
  33.       
  34.     // 獲取同步源標識    
  35.     ssrc=getenv("SSRC");  
  36.     if (ssrc!=NULL)   
  37.     {  
  38.         printf("using SSRC=%i.\n",atoi(ssrc));  
  39.         rtp_session_set_ssrc(session,atoi(ssrc));  
  40.     }  
  41.       
  42.     return session;  
  43.  
  44. }  
  45.  
  46. /**  發送rtp數據包     
  47.  *       
  48.  *   主要用於發送rtp數據包     
  49.  *   @param:  RtpSession *session RTP會話對象的指針     
  50.  *   @param:  const char *buffer 要發送的數據的緩衝區地址      
  51.  *   @param: int len 要發送的數據長度     
  52.  *   @return:  int 實際發送的數據包數目     
  53.  *   @note:     如果要發送的數據包長度大於BYTES_PER_COUNT,本函數內部會進行分包處理     
  54.  */ 
  55. int rtpSend(RtpSession *session,const char *buffer, int len)  
  56. {  
  57.     int curOffset = 0;  
  58.     int sendBytes = 0;  
  59.     int clockslide=500;   
  60.     // 發送包的個數  
  61.     int sendCount = 0;  
  62.  
  63.     ortp_message("send data len %i\n ",len);  
  64.  
  65.     // 是否全部發送完畢    
  66.     while(curOffset < len )  
  67.     {  
  68.      // 如果需要發送的數據長度小於等於闕值,則直接發送  
  69.         if( len <= BYTES_PER_COUNT )  
  70.         {  
  71.             sendBytes = len;  
  72.         }  
  73.         else 
  74.         {  
  75.             // 如果當前偏移 + 閾值 小於等於 總長度,則發送閾值大小的數據  
  76.             if( curOffset + BYTES_PER_COUNT <= len )  
  77.             {  
  78.                 sendBytes = BYTES_PER_COUNT;  
  79.             }  
  80.             // 否則就發送剩餘長度的數據  
  81.             else 
  82.             {  
  83.                 sendBytes = len - curOffset;  
  84.             }  
  85.         }  
  86.           
  87.         ortp_message("send data bytes %i\n ",sendBytes);  
  88.           
  89.         rtp_session_send_with_ts(session,(char *)(buffer+curOffset),sendBytes,g_user_ts);  
  90.           
  91.         // 累加  
  92.         sendCount ++;  
  93.         curOffset += sendBytes;                   
  94.         g_user_ts += TIME_STAMP_INC;  
  95.       
  96.         // 發送一定數據包後休眠一會  
  97.         if (sendCount%10==0)   
  98.         {  
  99.             usleep(20000);  
  100.         }     
  101.     }  
  102.     return 0;  
  103. }  
  104.  
  105. /**  結束ortp的發送,釋放資源     
  106.  *      
  107.  *   @param:  RtpSession *session RTP會話對象的指針     
  108.  *   @return:  0表示成功     
  109.  *   @note:         
  110.  */ 
  111. int rtpExit(RtpSession *session)  
  112. {  
  113.     g_user_ts = 0;  
  114.       
  115.     rtp_session_destroy(session);  
  116.     ortp_exit();  
  117.     ortp_global_stats_display();  
  118.  
  119.     return 0;  
  120. }  
  121.  
  122. // 主函數,進行測試  
  123. int main()  
  124. {  
  125.     // 待發送的數據緩衝區  
  126.     char * pBuffer = "123445356234134234532523654323413453425236244123425234";  
  127.       
  128.     RtpSession * pRtpSession = NULL;  
  129.     // 向(192.201.0.51,8000)目的地址發送rtp包  
  130.     pRtpSession = rtpInit("192.201.0.51",8000);  
  131.     if(pRtpSession==NULL)  
  132.     {  
  133.         printf("error rtpInit");  
  134.         return 0;  
  135.     }  
  136.       
  137.     // 循環發送  
  138.     while(1)  
  139.     {  
  140.         if( rtpSend(pRtpSession,pBuffer,20) != 0)  
  141.         {  
  142.             printf("error rtpInit");  
  143.             break;  
  144.         }  
  145.         usleep(10000);  
  146.         printf("sleep");  
  147.     }  
  148.       
  149.     // 退出  
  150.     rtpExit(pRtpSession);  
  151.       
  152.     return 0;  

 

本文出自 “對影成三人” 博客,請務必保留此出處http://ticktick.blog.51cto.com/823160/345642

發佈了29 篇原創文章 · 獲贊 19 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章