原文见:




http://www.2cto.com/kf/201211/165696.html


一、JRTPLIB简介

  老外用C++编写的开源RTP协议库,用来进行实时数据传输,可以运行在 Windows、Linux、 FreeBSD、Solaris、Unix和VxWorks 等多种操作系统上,主页为:http://research.edm.uhasselt.be/~jori/page/index.php?n=Main.HomePage

http://research.edm.uhasselt.be/jori/jrtplib/jrtplib_old.html

 


二、相关下载

jrtplib:  http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib 

jthread:   http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jthread 

cmake:    http://www.cmake.org/cmake/resources/software.html


三、 编译步骤

1    下载jrtplib和jthread并解压缩。阅读README。

2  编译jthread生成jthread.lib和jthread_d.lib。

① 打开cmake,添加好输入(where..)和输出路径(where to...),完成configure配置(选visual studio 10),配置结果如下图:


rstp java rstp java开源_#pragma

  ② 点击generate,生成VS2010工程文件

  ③ 打开工程文件并编译,在debug和release下分别生成jthread.lib和jthread_d.lib

     编译的具体方法为:选择Solution Explorer里的 Solution jthread,点右键,运行"Rebuild Solution";如编译无错误,再选择INSTALL项目,运行"Build"。

  ④ 如果编译成功(如下图),会在C:\Program Files\jthread的include\jthread下生成头文件;在lib下生成lib和cmake文件

 


rstp java rstp java开源_#include_02

    温馨提示:在win7下,你必须拥有管理者权限,否则编译不会通过,因为无法在C:\Program Files创建jthread文件,当然你可以手动创建。

3  编译jrtplib生成jrtplib.lib和jrtplib_d.lib。

  ① 同2-①,其中configure会稍微麻烦一些,详细配置结果如下:


rstp java rstp java开源_#include_03

 

  ② 点击generate,生成VS2010工程文件

  ③ 打开工程文件并编译,在debug和release下分别生成jrtplib_d.lib和jrtplib.lib

④ 编译成功(如下图),在C:\Program Files\jrtplib下include\jrtplib3下会生成一堆头文件;在lib下会生成jrtplib_d.lib和jrtplib.lib以及cmake文件


rstp java rstp java开源_头文件_04

说明:网上提到的一些用VS2008和VC6.0方法中提到了两个细节:  一是要把"jmutex.h"和"jthread.h"两个头文件放入jrtplib/src目录下,二是要把src文件夹下所有头文件中的<jmutex.h>和<jthread.h>语句修改为"jmutex.h"和"jthread.h"。

     我在编译时没有处理这两个细节成功了,后续调试出现相应问题相应修改一下即可。

 


四、 使用实例

1  添加库

①步骤一:

方法1. 将编译生成的jrtplib.lib和jthread.lib库拷贝到“*:\Program Files\Microsoft Visual Studio 10.0\VC\lib”下面

方法2. 将编译生成的四个lib库库拷贝到当前工程的cpp文件下

②步骤二:

方法1. [菜单]“项目->属性->配置属性->连接器->输入->附加依赖项”里填写“jrtplib.lib;jthread.lib;WS2_32.lib”

方法2.  pragma 方式,在stdafx.h文件中 添加


rstp java rstp java开源_#include_05



#ifdef DEBUG
#pragma comment(lib, "jrtplib_d.lib") 
#pragma comment(lib,"jthread_d.lib")
#pragma comment(lib,"WS2_32.lib")
#else
#pragma comment(lib, "jrtplib.lib") 
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"WS2_32.lib")
#endif



rstp java rstp java开源_#include_05


2  添加头文件

①步骤一:将所有的.h文件放到一起,如myJRTPLIBHeader里面,再添加include

②步骤二:

方法1.“项目->属性->配置属性->C/C++->常规->附加包含目录”

方法2.“工具->选项->项目和解决方案->C++ 目录”,选择对应平台,然后添加所需“包括文件”目录(此法VS2010不通)

 

3  测试代码(sample1)

cpp文件:


rstp java rstp java开源_#include_05



// jrtplibTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"



//  头文件
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#ifndef WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif // WIN32
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib;


//
// This function checks if there was a RTP error. If so, it displays an error
// message and exists.
//
void checkerror(int rtperr)
{
if (rtperr < 0)
    {
        std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
        exit(-1);
    }
}

//
// The main routine
//
int main(void)
{
#ifdef WIN32
    WSADATA dat;
    WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32

    RTPSession sess;
    uint16_t portbase,destport;
    uint32_t destip;
    std::string ipstr;
int status,i,num;

// First, we'll ask for the necessary information

    std::cout << "Enter local portbase:" << std::endl;
    std::cin >> portbase;
    std::cout << std::endl;

    std::cout << "Enter the destination IP address" << std::endl;
    std::cin >> ipstr;

// 获得接收端的IP地址和端口号
    destip = inet_addr(ipstr.c_str());
if (destip == INADDR_NONE)
    {
        std::cerr << "Bad IP address specified" << std::endl;
return -1;
    }

// The inet_addr function returns a value in network byte order, but
// we need the IP address in host byte order, so we use a call to
// ntohl
    destip = ntohl(destip);

    std::cout << "Enter the destination port" << std::endl;
    std::cin >> destport;

    std::cout << std::endl;
    std::cout << "Number of packets you wish to be sent:" << std::endl;
    std::cin >> num;

// Now, we'll create a RTP session, set the destination, send some
// packets and poll for incoming data.

    RTPUDPv4TransmissionParams transparams;
    RTPSessionParams sessparams;

// IMPORTANT: The local timestamp unit MUST be set, otherwise
//            RTCP Sender Report info will be calculated wrong
// In this case, we'll be sending 10 samples each second, so we'll
// put the timestamp unit to (1.0/10.0)
    sessparams.SetOwnTimestampUnit(1.0/10.0);        

    sessparams.SetAcceptOwnPackets(true);
    transparams.SetPortbase(portbase);
// 创建RTP会话
    status = sess.Create(sessparams,&transparams);    
    checkerror(status);

    RTPIPv4Address addr(destip,destport);
// 指定RTP数据接收端
    status = sess.AddDestination(addr);
    checkerror(status);

for (i = 1 ; i <= num ; i++)
    {
        printf("\nSending packet %d/%d\n",i,num);

// send the packet
        status = sess.SendPacket((void *)"1234567890",10,0,false,10);
        checkerror(status);

        sess.BeginDataAccess();

// check incoming packets
        if (sess.GotoFirstSourceWithData())
        {
do
            {
                RTPPacket *pack;

while ((pack = sess.GetNextPacket()) != NULL)
                {
// You can examine the data here
                    printf("Got packet !\n");

// we don't longer need the packet, so
// we'll delete it
                    sess.DeletePacket(pack);
                }
            } while (sess.GotoNextSourceWithData());
        }

        sess.EndDataAccess();

#ifndef RTP_SUPPORT_THREAD
        status = sess.Poll();
        checkerror(status);
#endif // RTP_SUPPORT_THREAD

        RTPTime::Wait(RTPTime(1,0));
    }

    sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdef WIN32
    WSACleanup();
#endif // WIN32
return 0;
}



rstp java rstp java开源_#include_05


4  下载

  在VS2010+Win7下编译好的JRTPLIB库及相关头文件下载:(刚传CSDN,现在打不开,等等,明天补上...)

补充:下载(猛击

 


Ref/Related

http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib

http://research.edm.uhasselt.be/jori/jrtplib/documentation/index.html

Linux下编译jrtplib和jthread

http://blog.sina.com.cn/s/blog_9f149699010122c5.html

1. 下载开发包解压。分别下载jrtplib-3.7.1.tar.gz和jthread-1.2.1.tar.gz。下载地址为:

http://research.edm.uhasselt.be/jori/jrtplib/jrtplib-3.7.1.tar.gz和
http://research.edm.uhasselt.be/jori/jthread/jthread-1.2.1.tar.gz


2. 将源文件放到如下任意目录中,这里假设放到主目录下:/home/nick/。分别解压#tar -xzvf xxx,xxx为文件名。解压后出现两个目录,一个是jrtplib-3.7.1,一个是jthread-1.2.1。然后进行安装,先安装jthread,再安装jrtplib。

3. jthread安装:进入jthead解压目录,运行#./configure配置环境,如果出现permission denied,说明我们需要给文件configure执行的权限,运行#chmod a+x configure即可。配置完毕后运行#make,成功后再运行#make install。安装成功以后会在/urs/local/include目录下多出一个文件夹jthread。

4. jrtplib安装同上,全部安装好以后会在/urs/local/include目录下多出两个文件夹jrtplib3(可能由于版本不同,生成的文件夹名字略有不同,比如叫jrtplib-3.7.1)和jthead。复制jthead下所有文件(都是后缀为.h的头文件)到jrtplib文件夹下,目的是为了让所有头文件在一个目录下方便调用。

5. 装好以后系统环境如下,静态动态库安装到了/usr/local/lib目录下,包括libjrtplib-3.7.1.so和libjthread-1.2.1.so等。头文件在/usr/local/include/jrtplib-3.7.1目录下。

6. 在jrtplib源代码目录里有示例程序,在目录example下,试验一下编译example1.cpp,使用静态库libjrtp.a链接,编译链接语句如下(注意用g++而不是gcc编译,因为是链接阶段需要g++):
g++ -o example1 example1.cpp -I /usr/local/include/jrtplib/ -ljrtp
即生成应用程序exmaple1,注意,-I(大写的i)参数中路径为jrtplib头文件目录,根据实际情况修改。如果出现错误,说明动态链接库路径不对,有以下两种方法可以对动态链接库的路径进行设置:

a、修改动态链接库宏LD_LIBRARY_PATH。LD_LIBRARY_PATH 这个环境变量是大家最为熟悉的,它告诉loader:在哪些目录中可以找到共享库。可以设置多个搜索目录,这些目录之间用冒号分隔开。export LD_LIBRARY_PATH=/usr/local/lib,然后再运行编译,即可通过。这种方法只是暂时修改路径,在重启shell后会失效。

b、永久生效的方法为修改动态链接库配置文件/etc/ld.so.conf,或者在/etc/ld.so.conf.d里创建一个新文件,并把需要的目录加到这个文件里。具体方法如下:

#cd /etc/ld.so.conf.d

#vim jrtplib 在