📄 rtp_client.cpp
字号:
/*
Here's a small IPv4 example: it asks for a portbase and a destination and
starts sending packets to that destination.
*/
#pragma comment(lib, "jrtplib.lib")
#pragma comment(lib, "jthread.lib")
#pragma comment(lib, "WS2_32.lib")
#include "stdafx.h"
#include "rtpsession.h"
#include "rtpappsession.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>
//
// 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
RTPAppSession sess1;
uint16_t portbase,destport;
uint32_t destip;
std::string ipstr;
int status,i,num;
BYTE *pfBuffer;
BYTE *pBuffer;
BYTE BUFFER[1000*100];
int count=0;
CFile mfile,dstfile;
UINT nlen;
UINT dwRead=0;
// First, we'll ask for the necessary information
/*
std::cout << "Enter local portbase:" << std::endl;
std::cin >> portbase;
std::cout << std::endl;
*/
portbase = 8000;
/*
std::cout << "Enter the destination IP address" << std::endl;
std::cin >> ipstr;
*/
ipstr = "172.31.5.188";
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;
*/
destport = 8888;
/*
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/44000.0);
sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status = sess1.Create(sessparams,&transparams);
checkerror(status);
RTPIPv4Address addr(destip,destport);
status = sess1.AddDestination(addr);
checkerror(status);
// mfile.Open(TEXT("E:\\陈慧琳 - 记事本.mp3"),CFile::modeRead|CFile::shareDenyNone);
// nlen=mfile.GetLength();
// mfile.SeekToBegin();
dstfile.Open(TEXT("E:\\11"),CFile::modeCreate | CFile::modeReadWrite|CFile::shareDenyNone);
while (1)
{
// printf("\nSending packet %d/%d\n",i,num);
// send the packet
// char data[1000];
// int dw=mfile.Read(data, sizeof(data));
// printf("%d\n",dw);
// status = sess.SendPacket(data,dw,0,false,1);
// dwRead+=dw;
// checkerror(status);
// status = sess.SendPacket((void *)"1234567890",10,0,false,100);
// checkerror(status);
sess1.BeginDataAccess();
// check incoming packets
if (sess1.GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
while ((pack = sess1.GetNextPacket()) != NULL)
{
// You can examine the data here
printf("Got packet !\n");
int length = pack->GetPayloadLength();
printf("%d bytes of data received\n", pack->GetPayloadLength());
pfBuffer = (unsigned char*)pack->GetPayloadData();
pBuffer = new BYTE[length+1];
memcpy(pBuffer,pfBuffer,length);
pBuffer[length] = 0;
if(count+length>100000)
{
dstfile.Write(BUFFER,count);
count=0;
}
memcpy(&BUFFER[count],pBuffer,length);
count=count+length;
// dstfile.Write(pBuffer,length);
// printf("%s hello\n",pBuffer);
// we don't longer need the packet, so
// we'll delete it
sess1.DeletePacket(pack);
}
} while (sess1.GotoNextSourceWithData());
}
sess1.EndDataAccess();
#ifndef RTP_SUPPORT_THREAD
status = sess.Poll();
checkerror(status);
#endif // RTP_SUPPORT_THREAD
RTPTime::Wait(RTPTime(0,111111));
}
sess1.BYEDestroy(RTPTime(10,0),0,0);
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -