ortp庫使用入門

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

1、簡介

ORTP是一個支持RTP以及RFC3550協議的庫,有以下的特性:c++


(1)使用C語言編寫,能夠工做於windows, Linux, 以及 Unix平臺編程


(2)實現了RFC3550協議,提供簡單易用的API。支持多種配置,RFC3551爲默認的配置。windows


(3)支持單線程下的多個RTP會話,支持自適應抖動處理。網絡


(4)基於GPL版權聲明。session


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


關於ORTP的資料並很少,主要是其源碼、幫助文檔以及示例程序,關於示例程序說明以下:函數


rtprecv.c 和 rtpsend.c 展現瞭如何接收和發送單RTP數據流。
mrtprecv.c mrtpsend.c 展現瞭如何同時接收和發送多個RTP數據流。測試

2、主要函數介紹

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. 隨機產生×××C和同步源描述信息

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調度管理功能。

參數含義:

sessionrtp會話結構體

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)

函數功能:設置是否使用阻塞模式,

參數含義:

sessionrtp會話結構體

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表示回調函數綁定錯誤

說明:

信號的名稱必須是如下字符串中的一種:

"***c_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會話對象
 

3、程序示例

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


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


//////////////////////////////////////////////////////////////////////////     
/// COPYRIGHT NOTICE     
 // Copyright (c) 2009, 華中科技大學ticktick Group     
/// All rights reserved.      
///      
/// @file    ortpSend.c       
/// @brief   ortpSend的測試     
///     
/// 本文件示例使用ortp庫進行rtp數據包的發送    
///      
/// @version 1.0        
/// @author  tickTick   
/// @date    2010/07/07      
/// @E-mail  lujun.hust@gmail.com     
///     
/// 修訂說明:建立文件    
//////////////////////////////////////////////////////////////////////////      
 
#include <ortp/ortp.h>  
#include <signal.h>  
#include <stdlib.h>  
 
#ifndef _WIN32   
#include <sys/types.h>  
#include <sys/time.h>  
#include <stdio.h>  
#endif  
 
// 時間戳增量
#define TIME_STAMP_INC  160   
#define BYTES_PER_COUNT 65535
// 時間戳   
uint32_t g_user_ts;  
 
/**  初始化     
 *       
 *   主要用於對ortp以及其它參數進行初始化    
 *   @param:  char * ipStr 目的端IP地址描述串     
 *   @param:  iint port 目的端RTP監聽端口     
 *   @return:  RtpSession * 返回指向RtpSession對象的指針,若是爲NULL,則初始化失敗     
 *   @note:       
 */   
RtpSession * rtpInit(char * ipStr,int port)  
{  
    // Rtp會話對象    
    RtpSession *session;  
    char ****c;  
      
    // 時間戳初始化    
    g_user_ts = 0;  
 
    // ortp的一些基本初始化操做   
    ortp_init();  
    ortp_scheduler_init();  
    // 建立新的rtp會話對象  
    session=rtp_session_new(RTP_SESSION_SENDONLY);    
      
    rtp_session_set_scheduling_mode(session,1);  
    rtp_session_set_blocking_mode(session,1);  
    // 設置遠程RTP客戶端的的IP和監聽端口(即本rtp數據包的發送目的地址)   
    rtp_session_set_remote_addr(session,ipStr,port);  
    // 設置負載類型    
    rtp_session_set_payload_type(session,0);  
      
    // 獲取同步源標識    
    ***c=getenv("×××C");  
    if (***c!=NULL)   
    {  
        printf("using ×××C=%i.\n",atoi(***c));  
        rtp_session_set_***c(session,atoi(***c));  
    }  
      
    return session;  
 
}  
 
/**  發送rtp數據包     
 *       
 *   主要用於發送rtp數據包     
 *   @param:  RtpSession *session RTP會話對象的指針     
 *   @param:  const char *buffer 要發送的數據的緩衝區地址      
 *   @param: int len 要發送的數據長度     
 *   @return:  int 實際發送的數據包數目     
 *   @note:     若是要發送的數據包長度大於BYTES_PER_COUNT,本函數內部會進行分包處理     
 */ 
int rtpSend(RtpSession *session,const char *buffer, int len)  
{  
    int curOffset = 0;  
    int sendBytes = 0;  
    int clockslide=500;   
    // 發送包的個數  
    int sendCount = 0;  
 
    ortp_message("send data len %i\n ",len);  
 
    // 是否所有發送完畢    
    while(curOffset < len )  
    {  
        // 若是須要發送的數據長度小於等於闕值,則直接發送  
        if( len <= BYTES_PER_COUNT )  
        {  
            sendBytes = len;  
        }  
        else 
        {  
            // 若是當前偏移 + 閾值 小於等於 總長度,則發送閾值大小的數據  
            if( curOffset + BYTES_PER_COUNT <= len )  
            {  
                sendBytes = BYTES_PER_COUNT;  
            }  
            // 不然就發送剩餘長度的數據  
            else 
            {  
                sendBytes = len - curOffset;  
            }  
        }  
          
        ortp_message("send data bytes %i\n ",sendBytes);  
          
        rtp_session_send_with_ts(session,(char *)(buffer+curOffset),sendBytes,g_user_ts);  
          
        // 累加  
        sendCount ++;  
        curOffset += sendBytes;                   
        g_user_ts += TIME_STAMP_INC;  
      
        // 發送必定數據包後休眠一會  
        if (sendCount%10==0)   
        {  
            usleep(20000);  
        }     
    }  
    return 0;  
}  
 
/**  結束ortp的發送,釋放資源     
 *      
 *   @param:  RtpSession *session RTP會話對象的指針     
 *   @return:  0表示成功     
 *   @note:         
 */ 
int rtpExit(RtpSession *session)  
{  
    g_user_ts = 0;  
      
    rtp_session_destroy(session);  
    ortp_exit();  
    ortp_global_stats_display();  
 
    return 0;  
}  
 
// 主函數,進行測試  
int main()  
{  
    // 待發送的數據緩衝區  
    char * pBuffer = "123445356234134234532523654323413453425236244123425234";  
      
    RtpSession * pRtpSession = NULL;  
    // 向(192.201.0.51,8000)目的地址發送rtp包  
    pRtpSession = rtpInit("192.201.0.51",8000);  
    if(pRtpSession==NULL)  
    {  
        printf("error rtpInit");  
        return 0;  
    }  
      
    // 循環發送  
    while(1)  
    {  
        if( rtpSend(pRtpSession,pBuffer,20) != 0)  
        {  
            printf("error rtpInit");  
            break;  
        }  
        usleep(10000);  
        printf("sleep");  
    }  
      
    // 退出  
    rtpExit(pRtpSession);  
      
    return 0;  
}
相關文章
相關標籤/搜索