发送程序
#include "stdafx.h"
#pragma comment(lib, "jrtplib.lib")
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"WS2_32.lib")
#include "rtpsession.h"
#include "rtppacket.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>
char *buf=NULL;;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char c;
#ifdef WIN32
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);//指示程序所采用的socket的版本
#endif // WIN32
RTPSession sess;//本次会话实例
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i,num;
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;
destip = inet_addr(ipstr.c_str());
if (destip == INADDR_NONE)
{
std::cerr << "Bad IP address specified" << std::endl;
return -1;
}
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;
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);//设置端口用于接受数据或发送数据
status = sess.Create(sessparams,&transparams); //创建RTP任务
checkerror(status);
RTPIPv4Address addr(destip,destport);//目标地址
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);
RTPTime::Wait(RTPTime(1,0));
}
sess.BYEDestroy(RTPTime(10,0),0,0);
#ifdef WIN32
WSACleanup();
#endif // WIN32
c=getchar();
return 0;
}
接受程序
#include "stdafx.h"
#pragma comment(lib, "jrtplib.lib")
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"WS2_32.lib")
#include "rtpsession.h"
#include "rtppacket.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>
char *buf=NULL;;
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char c;
#ifdef WIN32
WSADATA dat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif // WIN32
RTPSession sess;//标示此次会话
uint16_t portbase;
int status;
// First, we'll ask for the necessary information
std::cout << "Enter local portbase:" << std::endl;
std::cin >> portbase;
std::cout << std::endl;
RTPUDPv4TransmissionParams transparams;
RTPSessionParams sessparams;
sessparams.SetOwnTimestampUnit(1.0/10.0);//设置时间戳(发送频率)
sessparams.SetAcceptOwnPackets(true);//设置是否允许接受自己的数据包
transparams.SetPortbase(portbase);//设置端口,本地进行接收或发送数据时候所用的端口
status = sess.Create(sessparams,&transparams); //创建RTP任务
checkerror(status);//检查创建RTP任务的状态
while(TRUE)
{
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");
buf=(char *)pack->GetPayloadData();//获得数据
int i=pack->GetPayloadLength();
buf[10]='/0';
printf("pack->GetPayloadLength() %d/n",i);
printf("%s/n",buf);//输出数据
//pack->
// 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
c=getchar();
return 0;
}