VC 6 RTP流媒体传输协议编程实例(jrtplib)
www.diybl.com 时间 : 2010-10-15 作者:佚名 编辑:fnw 点击: [ 评论 ]
// RTPClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "rtpsession.h"
#include "rtppacket.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include "windows.h"
#include <iostream>
#include <string>
using namespace std;
#pragma comment(lib,"jrtplib.lib")
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"WS2_32.lib")
void checkerror(int rtperr)
{
if (rtperr < 0)
{
std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
exit(-1);
}
}
int main(int argc, char* argv[])
{
#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;
BYTE *pBuffer;
BYTE *pfBuffer;
//输入一些必要信息
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;
// 创建RTP session
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);
checkerror(status);
RTPIPv4Address addr(destip,destport);
status = sess.AddDestination(addr);
checkerror(status);
for (i = 1 ; i <= num ; i++)
{
printf(" Sending packet %d/%d ",i,num);
// 发送数据“1234567890”
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 ! ");
std::cout << "Got packet with "
<< "extended sequence number "
<< pack->GetExtendedSequenceNumber()
<< " from SSRC " << pack->GetSSRC()
<< std::endl;
int dataLength = pack->GetPayloadLength();
pfBuffer =(unsigned char*)pack->GetPayloadData();
pBuffer = new BYTE[dataLength + 1];
memcpy(pBuffer, pfBuffer, dataLength);
pBuffer[dataLength] = 0;
std::cout << pBuffer << std::endl;
// 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;
}