ortp库使用入门
来源:互联网 发布:招银网络科技 知乎 编辑:程序博客网 时间:2024/05/16 09:44
我们知道, 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 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 *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; }
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门 .
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门
- ortp库使用入门(源)
- 循环双链表
- Lucas定理证明
- win7 64, matlab2012a文件关联
- JAVA系列-设计模式-责任链模式
- 在Elcipse导入项目时提示:editor does not contain a main type的解决方案
- ortp库使用入门
- android布局设计中不想要listview的分割线,不想滚动列表时条目背景变颜色
- java.lang.OutOfMemoryError的两种解决办法
- 有你要用到的:邮箱输入框+自动补全
- 【wordpress】插件安装
- 使手机浏览器输入www网址自动跳转手机版页面
- 嵌套循环连接
- Java正则表达式 总结
- em12c OMS推送部署agent报错