JRTPLIB@Conference(转)

 

JRTPLIB@Conference(转)  

2011-03-28 14:56:41|  分类: 流媒体 |  标签: |字号大中小 订阅

本文转自:http://www.cnitblog.com/tinnal/archive/2009/01/01/53342.html

JRTPLIB@Conference DIY视频会议系统 一、开编

      前段时间一直在研究视频相关的东西。也有了一定的成果,包括MPEG流媒体服务器代码(当然了,是实验而已,当中的RTP也是手工写的,没有RTCP)和H263@S3C2410系列(打算在S3C2410上进行视频的H263采集、压缩、解压缩和显示)。收到了一些读者的来信,我也很高兴,但要声明的是我从做的行业与视频无关,有时候我对一些问题也是无能为力,我也只是因为感兴趣玩玩而已。下面谈谈JRTPLIB@Conference这个系列的情况。

      当时写MPEG流媒体服务器时,到网上找了很多RTP的代码,JRTPLIB就是其中一个,发现想做流媒体相关的应用,这些库所能提供的帮助是很小的。当时狠下心了,终于对MPEG有了一定的了解,RTP部分也是自己写的,虽然能工作但很不完善,写完后就有用JRTP的意图。这是写这个系列的原因之一,其二就是在我搜索有关JRTPLIB的相关内容时,竟然发现这么大一个因特网,竟然找不到用JRTPLIB实现的和流媒体相关的内容。整个国内在谈JRTPLIB的非常多,PUDN、CSDN等原码网站收集的原码也非常多,但令人气愤的是,这些不同名称的压缩包里头竟然全是JRTPLIB的原例程。JRTPLIB的官方例程?那不很好吗?NO!JRTPLIB的原列程(4个example和一个JRTPConsole)全和媒体流没关,example里就发发一些数字;JRTPConsole里就做一个服务器和一个客户端,也是发发数字。作者傻了吗,作者不知RTP是流媒体用的吗?NO,作者本来就没想过把JRTPLIB做为独立的软件包,JRTPLIB只是开发过程的其中一步,以这为出发点,测试程序发发数字,也没什么不可,能把JRTPLIB测试过就可以啦呀。如果大家去作者的网站看看就知道,作者写JRTPLIB是为了JVOIPLIB服务的,这才是作者想做的。而国内的人,却把这个测试程序当RTP的例程,我晕。更有“高手”做出了其于JRTPLIB的文件传送程序,狂晕,不知道RTP是传媒体流的,应该知道现的传文件都用FTP、HTTP呀。

      看到了如上这些现像,我有写点东西的想法,同时,我也可以过过兴趣。

      这是JRTPLIB@Conference 系列的第一编 开编。JRTPLIB@Conference系列的主要任务是通过JRTPLIB实现一个基于组播的会议系统,这里没有采用H323,也没有采用SIP,因为RTP本身就为媒体会议考虑得很周全。

      同时,在这里,我强调一下,JRTPLIB只是RTP协议的一个封装库,除此之外它不会实现任务的应用功能,想学完JRTPLIB就能写出媒体程序的就不用发白日梦了,想会用JRTPLIB用不用看RTP协议的也该醒了。要完最终的软件,我们必须清楚RTP协议和媒体相关的规范,周时会进行多媒体的编程(如MPEG的压缩、解压缩、采集、播放)。

      之前转载过一篇文章说RTP的,有图,也提到了JRTPLIB,把链接放在下面。

      http://www.cnitblog.com/tinnal/archive/2008/09/03/48674.html

      一些JRTPLIB的其本用法我就不说了,大家看看民JRTPLIB里的example就很清楚了。看不懂?那就BAIDU吧,国内全部网页都在给你解释那只个example.

      RTP的RFC:www.ietf.org/rfc/rfc3550.txt 

 

JRTPLIB@Conference DIY视频会议系统 二、基本例程分析

     这是JRTPLIB@Conference系列的第二部《基本例程分析》本系列的主要工作是实现一个基于JRTPLIB的,建立在RTP组播基础上的多媒体视频会议系统。这只是一个实验系统,用于学习JRTPLIB、RTP、和多媒体相关的编程,不是一个完善的软件工程。而且,我只会在业余的时间出于兴趣写一写。有志同道合的朋友可以通过tinnal@136.com这个邮箱或博客回复(推荐)和我交流。
      上一部:《
JRTPLIB@Conference DIY视频会议系统 一、开编
      这一部的原文为《
linux下基于jrtplib库的实时传送实现》,这编文章虽然没有和媒体流相关,但作为JRTPLIB库的函数说明来看还是不错的。 主要的任务是通过例子初步看看如何通过JRTPLIB进行编程。这是进行JRTPLIB或RTP编程的入门编。

一、RTP 是进行实时流媒体传输的标准协议和关键技术
实时传输协议(Real-time Transport Protocol,RTP)是在 Internet 上处理多媒体数据流的一种网络协议,利用它能够在一对一(unicast,单播)或者一对多(multicast,多播)的网络环境中实现传流媒体数据的实时传输。RTP 通常使用 UDP 来进行多媒体数据的传输,但如果需要的话可以使用 TCP 或者 ATM 等其它协议。

协议分析 :每一个RTP数据报都由头部(Header)和负载(Payload)两个部分组成,其中头部前 12 个字节的含义是固定的,而负载则可以是音频或者视频数据。

     RTP 是目前解决流媒体实时传输问题的最好办法,要在 Linux 平台上进行实时传送编程,可以考虑使用一些开放源代码的 RTP 库,如 LIBRTP、JRTPLIB 等。JRTPLIB 是一个面向对象的 RTP 库,它完全遵循 RFC 1889 设计,在很多场合下是一个非常不错的选择。JRTPLIB 是一个用 C++ 语言实现的 RTP 库,这个库使用socket 机制实现网络通讯 因此可以运行在 Windows、Linux、FreeBSD、Solaris、Unix和VxWorks 等多种操作系统上。
二、JRTPLIB 库的使用方法及程序实现
(1)JRTPLIB  函数 的使用
a、在使用 JRTPLIB 进行实时流媒体数据传输之前,首先应该生成 RTPSession 类的一个实例来表示此次 RTP 会话,然后调用 Create() 方法来对其进行初始化操作。RTPSession 类的 Create() 方法只有一个参数,用来指明此次 RTP 会话所采用的端口号。
RTPSession sess;

sess.Create(5000); //端口号

b、设置恰当的时戳单元,是 RTP 会话初始化过程所要进行的另外一项重要工作,这是通过调用 RTPSession 类的 SetTimestampUnit() 方法来实现的,该方法同样也只有一个参数,表示的是以秒为单元的时戳单元。
sess.SetTimestampUnit(1.0/8000.0);//时戳

c、当 RTP 会话成功建立起来之后,接下去就可以开始进行流媒体数据的实时传输了。首先需要设置好数据发送的目标地址,RTP 协议允许同一会话存在多个目标地址,这可以通过调用 RTPSession 类的 AddDestination()、DeleteDestination() 和 ClearDestinations() 方法来完成。例如,下面的语句表示的是让 RTP 会话将数据发送到本地主机的 6000 端口:

unsigned long addr = ntohl(inet_addr("127.0.0.1"));
sess.AddDestination(addr, 6000);

d、目标地址全部指定之后,接着就可以调用 RTPSession 类的 SendPacket() 方法,向所有的目标地址发送流媒体数据。SendPacket() 是 RTPSession 类提供的一个重载函数
对于同一个 RTP 会话来讲,负载类型、标识和时戳增量通常来讲都是相同的,JRTPLIB 允许将它们设置为会话的默认参数,这是通过调用 RTPSession 类的 SetDefaultPayloadType()、SetDefaultMark() 和 SetDefaultTimeStampIncrement() 方法来完成的。为 RTP 会话设置这些默认参数的好处是可以简化数据的发送,例如,如果为 RTP 会话设置了默认参数:

sess.SetDefaultPayloadType(0);
 sess.SetDefaultMark(false);  
sess.SetDefaultTimeStampIncrement(10);



之后在进行数据发送时只需指明要发送的数据及其长度就可以了

sess.SendPacket(buffer, 5);


e、对于流媒体数据的接收端,首先需要调用 RTPSession 类的 PollData() 方法来接收发送过来的 RTP 或者 RTCP 数据报。由于同一个 RTP 会话中允许有多个参与者(源),你既可以通过调用 RTPSession 类的 GotoFirstSource() 和 GotoNextSource() 方法来遍历所有的源,也可以通过调用 RTPSession 类的 GotoFirstSourceWithData() 和 GotoNextSourceWithData() 方法来遍历那些携带有数据的源。在从 RTP 会话中检测出有效的数据源之后,接下去就可以调用 RTPSession 类的 GetNextPacket() 方法从中抽取 RTP 数据报,当接收到的 RTP 数据报处理完之后,一定要记得及时释放。

JRTPLIB 为 RTP 数据报定义了三种接收模式,其中每种接收模式都具体规定了哪些到达的 RTP 数据报将会被接受,而哪些到达的 RTP 数据报将会被拒绝。通过调用 RTPSession 类的 SetReceiveMode() 方法可以设置下列这些接收模式: 
RECEIVEMODE_ALL  缺省的接收模式,所有到达的 RTP 数据报都将被接受; 
RECEIVEMODE_IGNORESOME  除了某些特定的发送者之外,所有到达的 RTP 数据报都将被接受,而被拒绝的发送者列表可以通过调用 AddToIgnoreList()、DeleteFromIgnoreList() 和 ClearIgnoreList() 方法来进行设置; 
RECEIVEMODE_ACCEPTSOME  除了某些特定的发送者之外,所有到达的 RTP 数据报都将被拒绝,而被接受的发送者列表可以通过调用 AddToAcceptList ()、DeleteFromAcceptList 和 ClearAcceptList () 方法来进行设置。 下面是采用第三种接收模式的程序示例。
if (sess.GotoFirstSourceWithData()) {  
         do {  
               sess.AddToAcceptList(remoteIP, allports,portbase);
               sess.SetReceiveMode(RECEIVEMODE_ACCEPTSOME);

               RTPPacket *pack;        
               pack = sess.GetNextPacket();            // 处理接收到的数据    
               delete pack;  

            }  while (sess.GotoNextSourceWithData());
 }


 (2)程序流程图
发送:获得接收端的 IP 地址和端口号        创建 RTP 会话        指定 RTP 数据接收端 设置 RTP 会话默认参数   发送流媒体数据
接收:获得用户指定的端口号  创建RTP会话  设置接收模式  接受RTP数据  检索RTP数据源  获取RTP数据报  删除RTP数据报


三、环境搭建及编译方法

引用
(1)Toolchain的安装
首先找到xscale-arm-toolchain.tgz文件,假设该文件包放在/tmp/下
#cd /
#tar -zxvf /tmp/xscale-arm-toolchain.tgz
再设置环境变量
#export PATH=/usr/local/arm-linux/bin:$PATH
最后检查一下交叉编译工具是否安装成功
#arm-linux-g++ --version
看是否显示arm-linux-g++的版本,如有则安装成功。
(2)JRTPLIB 库的交叉编译及安装
首先从 JRTPLIB 的网站(http://lumumba.luc.ac.be/jori/jrtplib/jrtplib.htmll) 下载最新的源码包,此处使用的是jrtplib-2.8.tar,假设下载后的源码包放在/tmp下,执 行下面的命令对其解压缩:
#cd /tmp
#tar -zxvf jrtplib-2.8.tar
然后要对jrtplib进行配置和编译
#cd jrtplib-2.8
#./configure CC=arm-linux-g++ cross-compile=yes
修改Makefile文件
将链接命令ld 和ar改为arm-linux-ld和 arm-linux-ar
#make
最后再执行如下命令就可以完成 JRTPLIB 的安装:
#make install
(3)程序编译
a、配置编译环境
可以用export来配置,也可以用编写Makefile的方法。这里采用Makefile。
编写Makefile&:
INCL = -I/usr/local/include
CFLAGS = -pipe -O2 -fno-strength-reduce
LFLAGS = /usr/local/lib/libjrtp.a -L/usr/X11R6/lib
LIBS = -LX11 -LXext /usr/local/lib/libjrtp.a
CC = arm-linux-g++

main:main.o
$(CC) $(LFLAGS) $(INCL) -o main main.o $(LIBS)
main.o:main.cpp

clean:
rm -f main
rm -f *.o

.SUFFIXES:.cpp
.cpp.o:
$(CC) -c $(CFLAGS) $(INCL) -o $@ $<         /*  $@表示目标的完整名字      */
         /* $<表示第一个依赖文件的名字 */
b、编译
假设发送和接收程序分别放在/tmp/send和/tmp/receive目录下
#cd /tmp/send
#make
#cd /tmp/receive
#make


四、易出错误及注意问题

引用
1、找不到一些标准的最 基本的一些头文件。
 主要是因为Toolchain路径没安装对,要 严格按照步骤安装。
2、找不到使用的jrtplib库中的一些头文件。
 在 jrtplib的安装目录下,include路径下不能再有别的目录。
3、recieve函数接收数据包不能正确提出所要数据。
  由于每一个RTP数据报都由头部(Header)和负载(Payload)两个部分组成,若使用getrawdata()是返回整个数据包的数据,包含传输媒体的类型、格式、序列号、时间戳以及是否有附加数据等信息。getpayload()函数是返回所发送的数据。两者一定要分清。
4、设置RECEIVEMODE_ACCEPTSOME  接收模式后,运行程序接收端不能接包。
 IP地址格式出了问题。iner_addr()与ntohl()函数要用对,否则参数传不进去,接受列表中无值,当然接收不了数据包。
5、编译通过,但测试时接收端不能接收到数据。
 可能是接收机防火墙未关闭。运行:
 #iptables -F
 也可能是IP地址没有设置好。运行:
 #ifocnfig eth0  *.*.*.*  netmask *.*.*.*
6、使用jrtolib库时,在程序中include 后最好加上库所在的路径。

五、程序

send:

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include "rtpsession.h"   
  4.   
  5. // 错误处理函数   
  6. void checkerror(int err)   
  7. {   
  8.   if (err < 0) {   
  9.     char* errstr = RTPGetErrorString(err);   
  10.     printf("Error:%s\\n", errstr);   
  11.     exit(-1);   
  12.   }   
  13. }   
  14.   
  15. int main(int argc, char** argv)   
  16. {   
  17.   RTPSession sess;   
  18.   unsigned long destip;   
  19.   int destport;   
  20.   int portbase = 6000;   
  21.   int status, index;   
  22.   char buffer[128];   
  23.   
  24.   if (argc != 3) {   
  25.     printf("Usage: ./sender destip destport\\n");   
  26.     return -1;   
  27.   }   
  28.   
  29.   // 获得接收端的IP地址和端口号   
  30.   destip = inet_addr(argv[1]);   
  31.   if (destip == INADDR_NONE) {   
  32.     printf("Bad IP address specified.\\n");   
  33.     return -1;   
  34.   }   
  35.   destip = ntohl(destip);   
  36.   destport = atoi(argv[2]);   
  37.   
  38.   // 创建RTP会话   
  39.   status = sess.Create(portbase);   
  40.   checkerror(status);   
  41.   
  42.   // 指定RTP数据接收端   
  43.   status = sess.AddDestination(destip, destport);   
  44.   checkerror(status);   
  45.   
  46.   // 设置RTP会话默认参数   
  47.   sess.SetDefaultPayloadType(0);   
  48.   sess.SetDefaultMark(false);   
  49.   sess.SetDefaultTimeStampIncrement(10);   
  50.   
  51.   // 发送流媒体数据   
  52.   index = 1;   
  53.   do {   
  54.     sprintf(buffer, "%d: RTP packet", index ++);   
  55.     sess.SendPacket(buffer, strlen(buffer));   
  56.     printf("Send packet !\\n");   
  57.   } while(1);   
  58.   
  59.   return 0;   
  60. }   
  61.   

#include <stdio.h> #include <string.h> #include "rtpsession.h" // 错误处理函数 void checkerror(int err) {  if (err < 0) {    char* errstr = RTPGetErrorString(err);    printf("Error:%s\\n", errstr);    exit(-1);  } } int main(int argc, char** argv) {  RTPSession sess;  unsigned long destip;  int destport;  int portbase = 6000;  int status, index;  char buffer[128];  if (argc != 3) {    printf("Usage: ./sender destip destport\\n");    return -1;  }  // 获得接收端的IP地址和端口号  destip = inet_addr(argv[1]);  if (destip == INADDR_NONE) {    printf("Bad IP address specified.\\n");    return -1;  }  destip = ntohl(destip);  destport = atoi(argv[2]);  // 创建RTP会话  status = sess.Create(portbase);  checkerror(status);  // 指定RTP数据接收端  status = sess.AddDestination(destip, destport);  checkerror(status);  // 设置RTP会话默认参数  sess.SetDefaultPayloadType(0);  sess.SetDefaultMark(false);  sess.SetDefaultTimeStampIncrement(10);  // 发送流媒体数据  index = 1;  do {    sprintf(buffer, "%d: RTP packet", index ++);    sess.SendPacket(buffer, strlen(buffer));    printf("Send packet !\\n");  } while(1);  return 0; }




receive:

  1. #include <stdio.h>   
  2. #include "rtpsession.h"   
  3. #include "rtppacket.h"   
  4.   
  5. // 错误处理函数   
  6. void checkerror(int err)   
  7. {   
  8.   if (err < 0) {   
  9.     char* errstr = RTPGetErrorString(err);   
  10.     printf("Error:%s\\n", errstr);   
  11.     exit(-1);   
  12.   }   
  13. }   
  14.   
  15. int main(int argc, char** argv)   
  16. {   
  17.   RTPSession sess;   
  18.   int localport,portbase;   
  19.   int status;   
  20.   unsigned long remoteIP;   
  21.   if (argc != 4) {   
  22.     printf("Usage: ./sender localport\\n");   
  23.     return -1;   
  24.   }   
  25.   
  26.    // 获得用户指定的端口号   
  27.       
  28.   remoteIP = inet_addr(argv[1]);   
  29.   localport = atoi(argv[2]);   
  30.   portbase = atoi(argv[3]);   
  31.   // 创建RTP会话   
  32.   status = sess.Create(localport);   
  33.   checkerror(status);   
  34.      
  35.   //RTPHeader *rtphdr;   
  36.   unsigned long timestamp1;   
  37.   unsigned char * RawData;   
  38.   unsigned char temp[30];   
  39.   int lengh ,i;   
  40.   bool allports = 1;   
  41.      
  42.   sess.AddToAcceptList(remoteIP, allports,portbase);   
  43.      
  44.      do {   
  45.  //设置接收模式   
  46.         sess.SetReceiveMode(RECEIVEMODE_ACCEPTSOME);   
  47.    sess.AddToAcceptList(remoteIP, allports,portbase);   
  48.   
  49.     // 接受RTP数据   
  50.     status = sess.PollData();   
  51.   
  52.        
  53.  // 检索RTP数据源   
  54.     if (sess.GotoFirstSourceWithData()) {   
  55.       do {   
  56.            
  57.         RTPPacket* packet;   
  58.         // 获取RTP数据报   
  59.         while ((packet = sess.GetNextPacket()) != NULL) {   
  60.           printf("Got packet !\n");   
  61.   
  62.    timestamp1 = packet->GetTimeStamp();   
  63.    lengh=packet->GetPayloadLength();   
  64.    RawData=packet->GetPayload();   
  65.       
  66.    for(i=0;i<lengh;i++){   
  67.       temp[i]=RawData[i];   
  68.   printf("%c",temp[i]);   
  69.    }   
  70.    temp[i]='\0';   
  71.    printf("  timestamp: %d lengh=%d data:%s\n",timestamp1,lengh,&temp);   
  72.           // 删除RTP数据报   
  73.       
  74.           delete packet;   
  75.         }   
  76.       } while (sess.GotoNextSourceWithData());   
  77.     }   
  78.   } while(1);   
  79.   
  80.   return 0;   
  81. }   

JRTPLIB@Conference DIY视频会议系统 三、JRTPLIB的几个重要类说明

 

      这是 JRTPLIB@Conference系列的第三编《JRTPLIB的几个重要类说明》,本系列的主要工作是实现一个基于JRTPLIB的,建立在RTP组播基础上的多媒体视频会议系统。这只是一个实验系统,用于学习JRTPLIB、RTP、和多媒体相关的编程,不是一个完善的软件工程。而且,我只会在业余的时间出于兴趣写一写。有志同道合的朋友可以通过 tinnal@136.com这个邮箱或博客回复(推荐)和我交流。
      上一部《 JRTPLIB@Conference DIY视频会议系统 二、基本例程分析
      这一部的主要内容是要研究一个JRTPLIB常用的几个非常重要的类,在进行JRTPLIB或RTP编程时会经常和这个几类打交道,或都从这些类中继承。

一、RTPSession
      对于大多数的RTP应用程序,RTPSession类可能是JRTPLIB唯一使用的类。它能完全处理RTCP部份的数据包,所以用户可以把精力集中在真正的数据收发。
      要知道 RTPSession类在多线程下并不是安全的,因此,用户要通过某些锁同步机制来保证不会出现在不同线程当中调用同一个RTPSession实例。
      RTPSession类有如下的接口。

      。 RTPSession(RTPTransmitter::TransmissionProtocol proto = RTPTransmitter::IPv4UDPProto)
      使用proto类型传输层创建一个PRTSession实例。如果proto使用用户自定义(user-defined)传输层,则相应的NewUserDefinedTransmitter()函数必须实现。ps:这里默认就行了,默认就是IPV4网络。

      。int Create(const RTPSessionParams &sessparams, const RTPTransmissionParams*transparams = 0)
      使用RTPSession参数sessparams和RTPTransmission参数transparams 真正创建一个RTP会话。如果transparams 为NULL,则使用默认的参数。ps:RTPSessionParams 我们可能要设得比较多,RTPTransmissionParams参数就只要设置其中的端口就行了,端口一定要设对,不然进行组播时,这个进程将不接收数据。设置方式可以看example.cpp。

      。 void Destroy()
      离开一个会话但不向其它组成员发送BYE包。ps:我不推荐用这个函数除非是错误处理,正常离开我们应该用ByeDestroy()。
      
      。 void BYEDestroy(const RTPTime &maxwaittime, const void *reason,size t reasonlength)
      发送一个BYE包并且离开会话。在发送BYE包前等待maxwaittime,如果超时,会不发送BYE包直接离开,BYE包会包含你的离开原因reason。相应的reasonlength表示reason长度。ps:因为BYE包是一个RTCP包,RTCP不是要发就发的,它的发送时间是为了平衡带宽通过计算得出来的,那就很有可能到了要发的时候以经超过了maxwaittime时间了,作者可能认一直保留个这会话这么久没意义。当然,我常常把maxwaittime设得很大。

      。 bool IsActive()
      看看这个RTPSession实例是否以经通过Create建立了真实的会话。

      。 uint32 t GetLocalSSRC()
      返回我们的SSRC。ps:至于什么是SSRC,去看看RFC3550吧。我说过JRTPLIB只是RTP协议的包装,并没有做任何应用的事情。

      。 int AddDestination(const RTPAddress &addr)
      添加一个发送目标。ps:当然,如果我们使用组播,这里只用调用一次,把我们的组播地址写进去。这样,这组的全部人都能收到你发的包。但是组播可因特网的上设置很烦。而且用组播测试也很烦(组播必须BIND一个端口,如果你想在同一台机器上运行两个软件实例来没试,你就会发现同一个端口BIND两次,当然,后面那次会失败,也就是说测试不了,要测?找两台机器,或用虚拟机 JRTPLIB@Conference(转) - 黑黑的大鲨鱼 - 黑黑的大鲨鱼),如果组播不满足,我们就要把组播变在单播,这时就要返复调用这个函数把其它组成员的IP都加进来了。具体可以看看example3.cpp。

      。 int DeleteDestination(const RTPAddress &addr)
      从发送地址列表中删除一下地址。

      。 void ClearDestinations()
      清除发送地址列表。

      。 bool SupportsMulticasting()
      返回JRTPLIB是否支持组播。ps:这里指JRTPLIB本身,不是你的真实网络。编译JRTPLIB库时可能指定。
.
      。 int JoinMulticastGroup(const RTPAddress &addr)
      加入一个组播组addr。

      。 int LeaveMulticastGroup(const RTPAddress &addr)
       离开一个组播组addr。

      。 void LeaveAllMulticastGroups()
      离开所有组播组。ps:我们可以同时加入多个组播组。.

      。 int SendPacket(const void *data, size t len)
      。 int SendPacket(const void *data, size t len, uint8 t pt, bool mark,uint32 t timestampinc)
      。 int SendPacketEx(const void *data, size t len, uint16 t hdrextID,const void *hdrextdata, size t numhdrextwords)
      。 int SendPacketEx(const void *data, size t len, uint8 t pt, boolmark, uint32 t timestampinc, uint16 t hdrextID, const void *hdrextdata,size t numhdrextwords)
      上面的4个函数都是发送数据包的,我想如果你没有看RTP协议,我说了你也晕。如果你RTP协议看了,再看看RTPSession.h的注识,你就懂了。

      。 int SetDefaultPayloadType(uint8 t pt)
      设定默认的RTP PayloadType为PT。ps:和上面的第一个和第三个发送函数配套。至于应该设个什么数,如果你看BAIDU上乱七八糟的文章,当然的乱设就可能了。其实应该按RFC3551,根据你要传输的媒体类型来设。

      。 int SetDefaultMark(bool m)
      这设RTP数据包的Mark标识。ps:设为什么值好?这个,呵呵,连RFC3550也不能确定了。要看具体的RTP Payload规范,MPEG的,H263的都不一样。
      MPEG2     www.ietf.org/rfc/rfc2250.txt 
      MPEG4     www.rfc-editor.org/rfc/rfc3016.txt 
      H263        www.ietf.org/rfc/rfc2190.txt 

      。 int SetDefaultTimestampIncrement(uint32 t timestampinc)
      设置默认的时间戳的增量。ps:也是和上的第一和第三个函数配套的。每发一个RTP数据包timestamp就会自动增加

      。 int IncrementTimestamp(uint32 t inc)
      这个函数用来手工增加Timestamp。有时我这很好用,例如,一个RTP数据包因为只含有静音数据,我们没有发送,这是我们就应手工增加Timestamp以便发下一个RTP数据包时它的Timestamp是正确的。

       。 int IncrementTimestampDefault()
      这个函数用于增加由SetDefaultTimestampIncrement设定的值。有时候这很有用,例如,一个RTP数据包因为只含有静音数据,我们没有发送。这时,这个函数就会被调用用来设置Timestamp以便下一个RTP包的Timestamp是正确的。

      。 int SetPreTransmissionDelay(const RTPTime &delay)
      This function allows you to inform the library about the delay between
sampling the first sample of a packet and sending the packet. This delay is
taken into account when calculating the relation between RTP timestamp
and wallclock time, used for inter-media synchronization.

      。 RTPTransmissionInfo *GetTransmissionInfo()
      This function returns an instance of a subclass of RTPTransmissionInfo
which will give some additional information about the transmitter (a list
of local IP addresses for example). The user has to delete the returned
instance when it is no longer needed.
      
      。 int Poll()
      If you’re not using the poll thread, this function must be called regularly
to process incoming data and to send RTCP data when necessary.
61

      。 int WaitForIncomingData(const RTPTime &delay,bool *dataavailable= 0)
      Waits at most a time delay until incoming data has been detected. Only
works when you’re not using the poll thread. If dataavailable is not NULL,
it should be set to true if data was actually read and to false otherwise.

      。 int AbortWait()
      If the previous function has been called, this one aborts the waiting. Only
works when you’re not using the poll thread.

      。 RTPTime GetRTCPDelay()
      Returns the time interval after which an RTCP compound packet may have
to be sent. Only works when you’re not using the poll thread.

      。 int BeginDataAccess()
      下面的函数(直到EndDataAccess)要在BeginDataAccess 和EndDataAccess之间被调用, BeginDataAccess确保轮询(poll)线程不会在这期间访问source table 。EndDataAccess 调用完成后,轮询(poll)线程会得到锁而继续访问。 ps:首先,你里的source table中的每一个source表示参与会议中的每一个参与者的每一个独立的媒体流。我们会在下面用到他们,但同时,poll线程也会轮询它们以正确处理和RTCP有关的内容。
  

      。 bool GotoFirstSource()
      开始递归参与者的第一个流,如果找到了,就返回tree,否则返回false。ps:我们通过这个函数和下面的GotoNextSource遍历source table中的每一个source。
      
      。 bool GotoNextSource()
      设置当前的源(source)为source table中的下一个源。如果已经到尾部了就返回false.
     
      。 bool GotoPreviousSource()
      设置当前的源(source)为source table中上一个源。如果已经到头部了就返回false.


      。 bool GotoFirstSourceWithData()
      开始递归参与者中第一个有RTP数据的流,如果找到了,就返回tree,否则返回false。PS:在接收数据是我们常用的是这套函数,因为如果没有数据要来都没用。

      。 bool GotoNextSourceWithData()
      设置当前的源(source)为source table中有RTP数据的下一个源。如果已经到尾部了就返回false.

      。 bool GotoPreviousSourceWithData()
      设置当前的源(source)为source table中有RTP数据的上一个源。如果已经到头部了就返回false.

      。 RTPSourceData *GetCurrentSourceInfo()
      返回当前参与者的当前源(source)的RTPSourceData 实列。ps:返回的这个RTPSourceData 就是本进程从期它参与者的RTCP数据包中收集得到的信息,对我们来说其实很有用,只是作者的例程没有用上,国内的网络也没有提到。在RFC3550中有关RTCP的东西都在这了,看过RFC3550的人都知到,里头谈得最多的就是RTCP。这个类我们以后会专门说。
      
      
      。 RTPSourceData *GetSourceInfo(uint32 t ssrc)
      返回由ssrc指定的RTPSourceData ,或都NULL(当这个条目不存在)。ps:这个函数也很有用。因为GetCurrentSourceInfo只有在GotoFirstSource等上下文当中才能用。如果我们是在RTPSource子类的成员函数中,我们没有这个上下文,就只能用这个函数。
 
      。 RTPPacket *GetNextPacket()
      得到当前参与者当前媒体流的下一个RTP数据包。

      。 int EndDataAccess()
      请看BeginDataAccess

      。 int SetReceiveMode(RTPTransmitter::ReceiveMode m)
      Sets the receive mode to m, which can be one of the following:
      – RTPTransmitter::AcceptAll
            All incoming data is accepted, no matter where it originated from.
      – RTPTransmitter::AcceptSome
            Only data coming from specific sources will be accepted.
      – RTPTransmitter::IgnoreSome
            All incoming data is accepted, except for data coming from a specificset of sources.
        Note that when the receive mode is changed, the list of addressed to be ignored or accepted will be cleared.

      。 int AddToIgnoreList(const RTPAddress &addr)
      Adds addr to the list of addresses to ignore.

      。 int DeleteFromIgnoreList(const RTPAddress &addr)
      Deletes addr from the list of addresses to ignore.

      。 void ClearIgnoreList()
      Clears the list of addresses to ignore.

      。 int AddToAcceptList(const RTPAddress &addr)
      Adds addr to the list of addresses to accept.

      。 int DeleteFromAcceptList(const RTPAddress &addr)
      Deletes addr from the list of addresses to accept.

      。 void ClearAcceptList()
      Clears the list of addresses to accept.

      。 int SetMaximumPacketSize(size t s)
      Sets the maximum allowed packet size to s.

      。 int SetSessionBandwidth(double bw)
      Sets the session bandwidth to bw, which is specified in bytes per second.

      。 int SetTimestampUnit(double u)
      Sets our own timestamp unit to u. The timestamp unit is defined as a time
interval divided by the number of samples in that interval: for 8000Hz
audio this would be 1.0/8000.0.

      。 void SetNameInterval(int count)
      在处理source table中的sourcese后,RTCP packet builder(我们不用理这个内部的东西)会检查是否有其它(non-CNAME)SDES项目要发送。如果count为零或负数,则不发送,如果count为正数,则在sources table处理count次后会把SDES name item加到当前RTCP包中。ps:其实每次处理sources table都会伴随都SDES RTCP数据包的发送,在这个数据包当中CNAME是必须的,但其它的项目不是必须的,这就函数确定了NAME项目发送的频度,如果为1,则表不每个SDES RTCP数据包都带着它,如果为2则每两个SDES数据包就发送一次NAME项目,下面的SetEMailInterval、SetLocationInterval、SetPhoneInterval、SetToolInterval、SetNoteInterval都是同一原理。关于这个ITEM的描述,请看RFC3550.老版本的JRTPLIB没有使用这套函数,而是用EnableSendName()等函数。

      。 void SetEMailInterval(int count)
      After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES e-mail item will be added after the sources in the source table
have been processed count times.

      。 void SetLocationInterval(int count)
      After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES location item will be added after the sources in the source table
have been processed count times.

      。 void SetPhoneInterval(int count)
      After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES phone item will be added after the sources in the source table
have been processed count times.

      。 void SetToolInterval(int count)
      After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES tool item will be added after the sources in the source table have
been processed count times.

      。 void SetNoteInterval(int count)
      After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES note item will be added after the sources in the source table have
been processed count times.

      。 int SetLocalName(const void *s, size t len)
      设置NAME SDES项目,以遍会议的其它人员看到你的名称。下同。
     
      。 int SetLocalEMail(const void *s, size t len)
      Sets the SDES e-mail item for the local participant to the value s with
length len.

      。 int SetLocalLocation(const void *s, size t len)
Sets the SDES location item for the local participant to the value s with
length len.

      。 int SetLocalPhone(const void *s, size t len)
      Sets the SDES phone item for the local participant to the value s with
length len.

      。 int SetLocalTool(const void *s, size t len)
      Sets the SDES tool item for the local participant to the value s with length
len.
      
      。 int SetLocalNote(const void *s, size t len)
Sets the SDES note item for the local participant to the value s with length
len.
In case you specified in the constructor that you want to use your own transmission
component, you should override the following function:
      。 RTPTransmitter *NewUserDefinedTransmitter()
      The RTPTransmitter instance returned by this function will then be used to send
and receive RTP and RTCP packets. Note that when the session is destroyed,
this RTPTransmitter instance will be destroyed with a delete call.
By inheriting your own class from RTPSession and overriding one or more of the
functions below, certain events can be detected:

      。 void OnRTPPacket(RTPPacket *pack, const RTPTime &receivetime, const  RTPAddress *senderaddress)
      如果有RTPPacket数据包来到,会调用这个函数处理。ps:这个函数在我们继承RTPSession类时很可能重载,这是获取RTP数据包除了上面所说的方法以外的另外一种方法,这个方法比较适合异步的情况。默认这个是一个空虚函数。除了这个函数以外,下面的几个函数了会经常重载。

      。 void OnRTCPCompoundPacket(RTCPCompoundPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
      Is called when an incoming RTCP packet is about to be processed.

      。 void OnSSRCCollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, bool isrtp)
      Is called when an SSRC collision was detected. The instance srcdat is the
one present in the table, the address senderaddress is the one that collided
with one of the addresses and isrtp indicates against which address
of srcdat the check failed.
      
      。 void OnCNAMECollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, const uint8 t *cname, size t cnamelength)
      Is called when another CNAME was received than the one already present for source srcdat.

      。 void OnNewSource(RTPSourceData *srcdat)
      当有一个新的条目加到source table时,调用这个函数。ps:这也是一个比较重要的函数,因为这意味着很有可能有一个新的与会者加入。但令我很不高兴的是,这时候的RTPSourceData 里头的CNAME和NAME等字段都还是无效的,这不是RTCP的责任,因为在这个SDES RTCP数据包中所有的信息都以经有了(通过抓包证实了这一点)。我们的函数被调用后,需要延时一会才能得到有关这个Source的CNAME和NAME等相关的信息。当然,如果你不想软件死掉,不能在这个函数体内以阻塞的方式延时。

      。 void OnRemoveSource(RTPSourceData *srcdat)
      当有一个条目从source table中移除时调用这个函数。ps:这通常意味着有一个与会者离开了,和OnNewSource不一样,这时的CNAME和NAME等都是有效的。用这个函数要注意,我们的“意味着两个字”因为“加入”的可能不是一个新的与会者,而是一个现有与会者的一个新的媒体流。“离开”的也可能不是一个与会者,而只是其中一个与会者的其中一个媒体流,这两个函数只能给我们更新与会者提供一个触发条件而已。当OnNewSource调用时,我们要看看这个CNAME是不是以经在我们与会者名单中,如果不是,那就是一个新与会者。同时,如果OnRemoveSource被调用,则我们要看看这个CNAME的与会者还有没有其它的Source,如果没有了,这个与会者才是真正离开。这么很麻烦??那就对了,那就是现在的H323和SIP要做的事情--会话管理。
     
      。 void OnTimeout(RTPSourceData *srcdat)
      Is called when participant srcdat is timed out.

      。 void OnBYETimeout(RTPSourceData *srcdat)
      Is called when participant srcdat is timed after having sent a BYE packet.

      。 void OnBYEPacket(RTPSourceData *srcdat)
      Is called when a BYE packet has been processed for source srcdat.

      。 void OnAPPPacket(RTCPAPPPacket *apppacket, const RTPTime &receivetime,
const RTPAddress *senderaddress)
      In called when an RTCP APP packet apppacket has been received at time
receivetime from address senderaddress.

      。 void OnUnknownPacketType(RTCPPacket *rtcppack, const RTPTime &receivetime,
const RTPAddress *senderaddress)
      Is called when an unknown RTCP packet type was detected.

      。 void OnUnknownPacketFormat(RTCPPacket *rtcppack, const RTPTime &receivetime,
const RTPAddress *senderaddress)
      Is called when an unknown packet format for a known packet type was
detected.

      。 void OnNoteTimeout(RTPSourceData *srcdat)
      Is called when the SDES NOTE item for source srcdat has been timed out.

      。 void OnSendRTCPCompoundPacket(RTCPCompoundPacket *pack)
      Is called when an RTCP compound packet has just been sent. Useful to
inspect outgoing RTCP data.

      。 void OnPollThreadError(int errcode)
      Is called when error errcode was detected in the poll thread.

      。 void OnPollThreadStep()
      Is called each time the poll thread loops. This happens when incoming data
was detected or when its time to send an RTCP compound packet.


      原来想全部翻译的,翻译真的累,后我我只翻译了一部份,其它有时间我会慢慢翻译的。PS后面的话是原文没有的,是我加上去的。其它是原文有的。
      原文: jrtplib.pdf(英文原搞)


以下内容转自: http://hi.baidu.com/qishang_huang/blog/item/e90e543b29276d28b8998f4a.html

分析jrtplib如何处理RTP和RTCP
2010-04-28 19:22

    最近在用jrtplib做一个项目,仔细看了看源代码,做个记录。

    jrtplib有两种数据接收方式:

    1.后台线程对数据进行接收,用到了该作者写的jthreadjmutex类,调用过程如下:

      当使用RTPSessionCreate方法创建一个session时,会调用InternalCreate方法,看看里面有什么东西~~发现有这么一行status = pollthread->Start(rtptrans) ,这是啥?看名字就知道是开启轮询线程,点开rtppollthread.cpp,找到Start函数,惊奇地发现它调用了JThread::Start() 这不就是作者自己写的线程类么?继续看代码,点开jthread.cpp,找到了创建线程的地方,

status = pthread_create(&threadid,&attr,TheThread,this); 可以看出来,线程函数就是TheThread,再往下看,TheThread里面又有啥?一不小心看到了这行

ret = jthread->Thread(); 再看看jthread.h,里面说Thread()是个虚函数!再回过头来看rtppollthread.h,发现类RTPPollThread继承了JThread类,并实现了Thread()!

OK,现在一切都清楚了。

    2.用户自己调用RTPSession中的Poll方法。

       jrtplib是怎么处理RTPRTCP包的呢?看RTPSession中的Poll方法,先是调用了rtptrans->Poll(),再调用了ProcessPolledData()。(后台线程在Thread函数中调用了这两个!)

       先看rtptrans->Poll(),rtpudpv4transmitter.cpp中,它调用了PollSocket(),当参数为true时,处理RTP socket,false时处理RTCP socket,里面可以看到recvfrom,说明在接收数据,再往下看rawpacketlist.push_back(pack),将得到的包放到包的队列中去。

该函数结束后调用ProcessPolledData(),可以看到,

status = sources.ProcessRawPacket(rawpack,rtptrans,acceptownpackets)

说明在处理我们前面得到的包,在rtpsource.cpp里面找到该函数,它调用了另一个ProcessRawPacket(参数不同),在这个ProcessRawPacket里面我们就可以看到了ProcessRTPPacketProcessRTCPCompoundPacket,光看名字也就知道干啥的了。

再往下看代码,就知道OnRTPPacketOnRTCPCompoundPacket函数怎么用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值