RTP 简单的发送端程序和接收端程序

发送端

/*
 * =====================================================================================
 *
 *       Filename:  jrtp2.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月28日 14时36分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <iostream>
#include <string>

#include <jrtplib/rtpsession.h>
#include <jrtplib/rtpsessionparams.h>
#include <jrtplib/rtpudpv4transmitter.h>
#include <jrtplib/rtpipv4address.h>
#include <jrtplib/rtppacket.h>
#include <jrtplib/rtptimeutilities.h>


#include <netinet/in.h>
#include <arpa/inet.h>

using namespace jrtplib;
//错误处理函数

void checkerror(int err)
{
	if(err<0)
	{
		std::string errstr=RTPGetErrorString(err);
		std::cout<<"Error: "<<errstr<<std::endl;
		exit(-1);
	}
}


int main()
{
	RTPSession sess;
	unsigned long destip;
	int destport;
//	int portbase=6000;
	int status,index;
	char buffer[128];

	std::string ipstr("127.0.0.1"); //接收端IP

	destip=inet_addr(ipstr.c_str());
	if(destip==INADDR_NONE)
	{
		std::cout<<"Bad IP address specified"<<std::endl;
		return -1;
	}

	destip=ntohl(destip);
	destport=8000;

	//创建RTP会话
	RTPSessionParams sessparams;
	sessparams.SetOwnTimestampUnit(1.0/10.0);

	RTPUDPv4TransmissionParams transparams;
	transparams.SetPortbase(6000);

	status=sess.Create(sessparams,&transparams);
	checkerror(status);

	//指定RTP数据接收端
	RTPIPv4Address addr(destip,destport);

	status=sess.AddDestination(addr);
	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));
		std::cout<<"send packet!"<<"  "<<buffer<<std::endl;
	}while(index<100);

	return 0;
}

接收端:



/*
 * =====================================================================================
 *
 *       Filename:  jrtp2_receiver.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月28日 15时17分31秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */


#include <iostream>
#include <string>
#include <stdio.h>
#include <jrtplib/rtpsession.h>
#include <jrtplib/rtppacket.h>
#include <jrtplib/rtpsessionparams.h>
#include <jrtplib/rtpudpv4transmitter.h>
#include <jrtplib/rtpipv4address.h>
#include <jrtplib/rtptimeutilities.h>
#include <jrtplib/rtppacket.h>


using namespace jrtplib;


void checkerror(int err)
{
	if(err<0)
	{
		std::string errstr=RTPGetErrorString(err);
		std::cout<<"Error: "<<errstr<<std::endl;
		exit(-1);
	}
}


int main()
{
	RTPSession sess;
	int status;

	RTPSessionParams sessionparams;
	sessionparams.SetOwnTimestampUnit(1.0/10.0);

	RTPUDPv4TransmissionParams transparams;
	transparams.SetPortbase(8000);

	status=sess.Create(sessionparams,&transparams);
	checkerror(status);
	int index=1;

	do
	{
		status=sess.Poll();
		if(sess.GotoFirstSourceWithData())
		{
			do
			{
				RTPPacket *packet;

				while((packet=sess.GetNextPacket())!=NULL)
				{
					std::cout<<"Got packet:  "<<index<<std::endl;
					index++;
					delete packet;
				}
			}while(sess.GotoNextSourceWithData());
		}
	}while(1);

	return 0;
}

你可能感兴趣的:(RTP 简单的发送端程序和接收端程序)