我們知道, RTP(Real-timeTransportProtocol)是用于Internet上針對多媒體數(shù)據(jù)流的一種傳輸協(xié)議,做流媒體傳輸方面的應(yīng)用離不開RTP協(xié)議的實(shí)現(xiàn)及使用,為了更加快速地在項(xiàng)目中應(yīng)用RTP協(xié)議實(shí)現(xiàn)流媒體的傳輸,我們一般會選擇使用一些RTP庫,例如使用c++語言編寫的JRTPLIB庫,網(wǎng)上關(guān)于RTP協(xié)議以及JRTPLIB庫的介紹已經(jīng)很多了,在此我也不再贅述,文本主要介紹實(shí)現(xiàn)了RTP協(xié)議的另一種開源庫——ORTP庫,這個庫是純使用c語言編寫,由于我們的項(xiàng)目是基于Linux下的c語言編程,故我們選擇了ortp作為我們的第三方庫,在此我也對該庫進(jìn)行一個簡單地介紹,希望對其他ortp的初學(xué)者有所幫助。
一、簡介
ORTP是一個支持RTP以及RFC3550協(xié)議的庫,有如下的特性:
(1)使用C語言編寫,可以工作于windows, Linux, 以及 Unix平臺
(2)實(shí)現(xiàn)了RFC3550協(xié)議,提供簡單易用的API。支持多種配置,RFC3551為默認(rèn)的配置。
(3)支持單線程下的多個RTP會話,支持自適應(yīng)抖動處理。
(4)基于GPL版權(quán)聲明。
ORTP可以在其官方網(wǎng)站上(http://www.linphone.org/index.php/eng/code_review/ortp)下載,下載解壓后得到ORTP的源碼包和示例程序(tests)。其幫助文檔在docs目錄下,也可以在http://download.savannah.gnu.org/releases/linphone/ortp/docs/在線查看。
關(guān)于ORTP的資料并不多,主要是其源碼、幫助文檔以及示例程序,關(guān)于示例程序說明如下:
rtprecv.c 和rtpsend.c 展示了如何接收和發(fā)送單RTP數(shù)據(jù)流。
mrtprecv.c mrtpsend.c 展示了如何同時接收和發(fā)送多個RTP數(shù)據(jù)流。
二、主要函數(shù)介紹
rtp_session_init
函數(shù)原型:void rtp_session_init (RtpSession * session, int mode)
函數(shù)功能:執(zhí)行rtp會話的一些必要的初始化工作
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體,含有一些rtp會話的基本信息
mode: 傳輸模式,有以下幾種,決定本會話的一些特性。
RTP_SESSION_RECVONLY:只進(jìn)行rtp數(shù)據(jù)的接收
RTP_SESSION_SENDONLY:只進(jìn)行rtp數(shù)據(jù)的發(fā)送
RTP_SESSION_SENDRECV:可以進(jìn)行rtp數(shù)據(jù)的接收和發(fā)送
執(zhí)行的操作:
1. 設(shè)置rtp包緩沖隊(duì)列的最大長度
2. 根據(jù)傳輸模式設(shè)置標(biāo)志變量的值
3. 隨機(jī)產(chǎn)生SSRC和同步源描述信息
4. 傳入全局的av_profile,即使用默認(rèn)的profile配置
5. 初始化rtp包緩沖區(qū)隊(duì)列
6. 發(fā)送負(fù)載類型默認(rèn)設(shè)置為0(pcmu音頻),接收負(fù)載類型默認(rèn)設(shè)置為-1(未定義)
7. 將session的其他成員的值均設(shè)置一個默認(rèn)值。
rtp_session_set_scheduling_mode
函數(shù)原型:void rtp_session_set_scheduling_mode (RtpSession * session, int yesno)
函數(shù)功能: RtpScheduler管理多個session的調(diào)度和收發(fā)的控制,本函數(shù)設(shè)置是否使用該session調(diào)度管理功能。
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
yesno: 是否使用rtp session的系統(tǒng)調(diào)度功能
說明:
如果yesno為1,則表明使用系統(tǒng)的session調(diào)度管理功能,意味著可以使用以下功能:
1. 可以使用session_set_select在多個rtp會話之間進(jìn)行選擇,根據(jù)時間戳判定某個會話是否到達(dá)了收發(fā)的時間。
2. 可以使用rtp_session_set_blocking_mode()設(shè)置是否使用阻塞模式來進(jìn)行rtp包的發(fā)送和接收。
如果yesno為0,則表明該會話不受系統(tǒng)管理和調(diào)度。
關(guān)于rtp session的管理和調(diào)度,由全局的變量RtpScheduler *__ortp_scheduler來負(fù)責(zé),該變量必須通過ortp_scheduler_init() 來進(jìn)行初始化操作。
rtp_session_set_blocking_mode
函數(shù)原型:void rtp_session_set_blocking_mode (RtpSession * session, int yesno)
函數(shù)功能:設(shè)置是否使用阻塞模式,
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
yesno: 是否使用阻塞模式
說明:
阻塞模式只有在scheduling mode被開啟的情況下才能使用,本函數(shù)決定了rtp_session_recv_with_ts() 和 rtp_session_send_with_ts()兩個函數(shù)的行為,如果啟用了阻塞模式,則rtp_session_recv_with_ts()會一直阻塞直到接收RTP包的時間點(diǎn)到達(dá)(這個時間點(diǎn)由該函數(shù)參數(shù)中所定義的時間戳來決定),當(dāng)接收完RTP數(shù)據(jù)包后,該函數(shù)才會返回。同樣,rtp_session_send_with_ts()也會一直阻塞直到需要被發(fā)送的RTP包的時間點(diǎn)到達(dá),發(fā)送結(jié)束后,函數(shù)才返回。
rtp_session_signal_connect
函數(shù)原型:int rtp_session_signal_connect (RtpSession * session, const char *signal, RtpCallback cb, unsigned long user_data)
函數(shù)功能:本函數(shù)提供一種方式,用于通知應(yīng)用程序各種可能發(fā)生的RTP事件(信號)。可能通過注冊回調(diào)函數(shù)的形式來實(shí)現(xiàn)本功能。
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
signal: 信號的名稱
cb: 回調(diào)函數(shù)
user_data:傳遞給回調(diào)函數(shù)的數(shù)據(jù)
返回值:0表示成功,-EOPNOTSUPP表示信號名稱不存在,-1表示回調(diào)函數(shù)綁定錯誤
說明:
信號的名稱必須是以下字符串中的一種:
"ssrc_changed" : 數(shù)據(jù)流的同步源標(biāo)識改變
"payload_type_changed" : 數(shù)據(jù)流的負(fù)載類型改變
"telephone-event_packet" : telephone-event RTP包(RFC2833)被接收
"telephone-event" : telephone event 發(fā)生
"network_error" : 網(wǎng)絡(luò)錯誤產(chǎn)生,傳遞給回調(diào)函數(shù)的是描述錯誤的字符串(const char *型)或者錯誤碼(int型)
"timestamp_jump" : 接收到的數(shù)據(jù)包發(fā)生了時間戳的跳躍。
要取消事件(信號)的監(jiān)聽,可以使用下面這個函數(shù)
int rtp_session_signal_disconnect_by_callback ( RtpSession * session, const char * signal_name, RtpCallback cb )
rtp_session_set_local_addr
函數(shù)原型:int rtp_session_set_local_addr( RtpSession * session, const char * addr,int port)
函數(shù)功能:設(shè)置本地rtp數(shù)據(jù)監(jiān)聽地址
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
addr: 本地IP地址,例如127.0.0.1,如果為NULL,則系統(tǒng)分配0.0.0.0
port: 監(jiān)聽端口,如果設(shè)置為-1,則系統(tǒng)為其自動分配端口
返回值: 0表示成功
說明:
如果是RTP_SESSION_SENDONLY(只發(fā)送)型會話,則不需要進(jìn)行本設(shè)置,而必須設(shè)置rtp_session_set_remote_addr() 來設(shè)置遠(yuǎn)程目的地址。
如果采用了系統(tǒng)自動分配監(jiān)聽端口,則可以通過int rtp_session_get_local_port(const RtpSession *session) 來獲取系統(tǒng)分配的監(jiān)聽端口號。
rtp_session_set_remote_addr
函數(shù)原型:int rtp_session_set_remote_addr (RtpSession * session, const char * addr, int port)
函數(shù)功能:設(shè)置RTP發(fā)送的目的地址
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
addr: 目的IP地址
port: 目的地址的監(jiān)聽端口號
返回值: 0表示成功
rtp_session_set_send_payload_type
函數(shù)原型:int rtp_session_set_send_payload_type (RtpSession * session, int paytype)
函數(shù)功能:設(shè)置RTP發(fā)送數(shù)據(jù)的負(fù)載類型
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
paytype:負(fù)載類型
返回值: 0表示成功,-1表示負(fù)載未定義
說明:
負(fù)載類型在payloadtype.h文件中有詳細(xì)的定義,RTP接收端有著類似的負(fù)載類型設(shè)置函數(shù),int rtp_session_set_recv_payload_type ( RtpSession * session, int paytype ) ,注意,發(fā)送的負(fù)載類型必須與接收的負(fù)載類型一致才能正常完成收發(fā)。
rtp_session_send_with_ts
函數(shù)原型:int rtp_session_send_with_ts (RtpSession * session, const char * buffer, int len,uint32_t userts)
函數(shù)功能:發(fā)送RTP數(shù)據(jù)包
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
buffer: 需要發(fā)送的RTP數(shù)據(jù)的緩沖區(qū)
len: 需要發(fā)送的RTP數(shù)據(jù)的長度
userts: 本RTP數(shù)據(jù)包的時間戳
返回值: 成功發(fā)送到網(wǎng)絡(luò)中的字節(jié)數(shù)
說明:
發(fā)送RTP數(shù)據(jù)需要自己管理時間戳的遞增,每調(diào)用一次本函數(shù),請根據(jù)實(shí)際情況對userts進(jìn)行遞增,具體遞增的規(guī)則見RTP協(xié)議中的說明。
例如:如果發(fā)送的是采樣率為90000Hz的視頻數(shù)據(jù)包,每秒25幀,則時間戳的增量為:90000/25 = 3600
時間戳的起始值為隨機(jī)值,建議設(shè)置為0 。
rtp_session_recv_with_ts
函數(shù)原型:int rtp_session_recv_with_ts (RtpSession * session, char * buffer,int len, uint32_t time, int * have_more)
函數(shù)功能:接收RTP數(shù)據(jù)包
參數(shù)含義:
session: rtp會話結(jié)構(gòu)體
buffer: 存放接收的RTP數(shù)據(jù)的緩沖區(qū)
len: 期望接收的RTP數(shù)據(jù)的長度
time: 期望接收的RTP數(shù)據(jù)的時間戳
have_more:標(biāo)識接收緩沖區(qū)是否還有數(shù)據(jù)沒有傳遞完。當(dāng)用戶給出的緩沖區(qū)不夠大時,為了標(biāo)識緩沖區(qū)數(shù)據(jù)未取完,則have_more指向的數(shù)據(jù)為1,期望用戶以同樣的時間戳再次調(diào)用本函數(shù);否則為0,標(biāo)識取完。
rtp_session_destroy
【原型】: void rtp_session_destroy(RtpSession *session)
【功能】:摧毀rtp會話對象,釋放資源
【參數(shù)】:session已經(jīng)創(chuàng)建的RTP會話對象
三、程序示例
下面,我簡單地通過程序演示了怎么使用ortp進(jìn)行rtp數(shù)據(jù)包的發(fā)送,接收端的程序待以后有時間再整理出來吧。
注:示例代碼我已經(jīng)整理出來了,見博文: 《ortp編程示例代碼》
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #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;
-
-
-
-
-
-
-
-
-
- RtpSession * rtpInit(char * ipStr,int port)
- {
-
- RtpSession *session;
- char *ssrc;
-
-
- g_user_ts = 0;
-
-
- ortp_init();
- ortp_scheduler_init();
-
- session=rtp_session_new(RTP_SESSION_SENDONLY);
-
- rtp_session_set_scheduling_mode(session,1);
- rtp_session_set_blocking_mode(session,1);
-
- rtp_session_set_remote_addr(session,ipStr,port);
-
- rtp_session_set_payload_type(session,0);
-
-
- ssrc=getenv("SSRC");
- if (ssrc!=NULL)
- {
- printf("using SSRC=%i.\n",atoi(ssrc));
- rtp_session_set_ssrc=\'#\'"
- }
-
- return session;
-
- }
-
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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;
-
- 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;
- }
本文出自 “對影成三人” 博客,請務(wù)必保留此出處http://ticktick.blog.51cto.com/823160/345642