我們知道, 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
rtp_session_destroy
【原型】: void rtp_session_destroy(RtpSession *session)
【功能】:摧毀rtp會話對象,釋放資源
【參數】:session已經創建的RTP會話對象
三、程序示例
下面,我簡單地通過程序演示了怎麼使用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 [email protected]
- ///
- /// 修訂說明:創建文件
- //////////////////////////////////////////////////////////////////////////
- #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 *ssrc;
- // 時間戳初始化
- 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);
- // 獲取同步源標識
- ssrc=getenv("SSRC");
- if (ssrc!=NULL)
- {
- printf("using SSRC=%i.\n",atoi(ssrc));
- rtp_session_set_ssrc(session,atoi(ssrc));
- }
- 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;
- }
本文出自 “對影成三人” 博客,請務必保留此出處http://ticktick.blog.51cto.com/823160/345642