【RTP】 入门实践总结【精华】

一,windows 下资料
      http://www.cnblogs.com/skyseraph/archive/2012/04/07/2435540.html

一,安装Cmake
二,在 jrtplib-3.9.1_1 中运行 cmake . 生成Makefile
三,然后 make
        接着 make install
        头文件被安装在 /usr/local/include/jrtplib3/ 所以编译程序时候需要添加 -I /usr/local/include/jrtplib3/
        库文件被安装在 /usr/local/lib/libjrtp.a 所以编译程序时候需要添加 -ljrtp
四,编译 example1
        g++ -o test example1.cpp -ljrtp -I /usr/local/include/jrtplib3/

       报错:./test: error while loading shared libraries: libjrtp.so.3.9.1: cannot open shared object file: No such file or directory
       分析:已经将 jrtp 安装完毕,为什么找不到动态库呢?
       解决:将 usr/local/lib/libjrtp.so.3.9.1 拷贝到 usr/lib 下面就可以执行了
五,调试
      报错:The specified port base is not an even number

      分析:jrtp 不支持奇数端口


附:

发送端代码:send.cpp

/*
   Here's a small IPv4 example: it asks for a portbase and a destination and 
   starts sending packets to that destination.
*/

#include "jrtplib3/rtpsession.h"
#include "jrtplib3/rtpudpv4transmitter.h"
#include "jrtplib3/rtpipv4address.h"
#include "jrtplib3/rtpsessionparams.h"
#include "jrtplib3/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;

void checkerror(int rtperr)
{
	if (rtperr < 0) //如果为负数 则返回失败信息
	{
		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
		exit(-1);
	}
}

int main(void)
{
#ifdef WIN32
	WSADATA dat;
	WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32
	
	RTPSession sess;
        
	uint16_t portbase=8000,destport=7800;
	uint32_t destip;
	std::string ipstr="127.0.0.1";
	int status,i,num=10;	
	
	destip = inet_addr(ipstr.c_str());//IP地址的建立
	if (destip == INADDR_NONE)
	{
		std::cerr << "Bad IP address specified" << std::endl;
		return -1;
	}
	
	destip = ntohl(destip);
	
	
	RTPUDPv4TransmissionParams transparams;
	RTPSessionParams sessparams;

	sessparams.SetOwnTimestampUnit(1.0/10.0);		
	
	sessparams.SetAcceptOwnPackets(true);
	transparams.SetPortbase(portbase);
        
	status = sess.Create(sessparams,&transparams);	
	checkerror(status);//检查RTP会话创建过程是否失败
	
	RTPIPv4Address addr(destip,destport);//套接字
	
	status = sess.AddDestination(addr);//设置数据发送的目标地址(允许有多个目的地址)
	checkerror(status);
	
	for (i = 1 ; i <= num ; i++)
	{
		printf("\nSending packet %d/%d\n",i,num);
		
		status = sess.SendPacket((void *)"1234567890",10,0,false,10);
		checkerror(status);
		

               // #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;
}
编译:g++ -o send send.cpp  -ljrtp -I /usr/local/include/jrtplib3/


接受端代码:

#include "jrtplib3/rtpsession.h"
#include "jrtplib3/rtpudpv4transmitter.h"
#include "jrtplib3/rtpipv4address.h"
#include "jrtplib3/rtpsessionparams.h"
#include "jrtplib3/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;

// 错误处理函数
void checkerror(int rtperr)
{
	if (rtperr < 0) //如果为负数 则返回失败信息
	{
		std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
		exit(-1);
	}
}


int main(int argc, char** argv)
{
     
  RTPSession sess;
  int localport;
  int portbase = 7800;
  int status;

  
  
  // 创建RTP会话
        RTPUDPv4TransmissionParams transparams;
	RTPSessionParams sessparams;
	
	sessparams.SetOwnTimestampUnit(1.0/10.0);		
	
	sessparams.SetAcceptOwnPackets(true);
	transparams.SetPortbase(portbase);
        
	status = sess.Create(sessparams,&transparams);
        checkerror(status);
  
        
        uint32_t destip;
	std::string ipstr="127.0.0.1";
        int destport=7800;
	destip = inet_addr(ipstr.c_str());//IP地址的建立
	if (destip == INADDR_NONE)
	{
		std::cerr << "Bad IP address specified" << std::endl;
		return -1;
	}
	destip = ntohl(destip);
        RTPIPv4Address addr(destip,destport);//套接字
        
        sess.AddToAcceptList(addr);
        
        std::cout<<"Begin receive: \n";
        
        sess.BeginDataAccess();
		
        do{
                  status = sess.Poll();
                  checkerror(status);
           
		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());
		}
        }
        while(1);
		sess.EndDataAccess();
                
  
  
  return 0;
}
编译:g++ -o receive  receive.cpp  -ljrtp -I /usr/local/include/jrtplib3/



你可能感兴趣的:(【RTP】 入门实践总结【精华】)