/*---------------------------------------------------------------------------*/
/* main.cpp
/*---------------------------------------------------------------------------*/
#include <crtdbg.h> //for _ASSERT
//JRTP includes
#include <RTPSession.h>
#include <RTPIPv4Address.h>
#include <RTPSessionParams.h>
#include <RTPUDPv4Transmitter.h>
#pragma comment(lib,"jthread.lib")
#pragma comment(lib,"jrtplib.lib")
#pragma comment(lib,"Ws2_32.lib")
#define MCAST_IP "192.168.60.183"
#define MCAST_PORT 1234
#define SERVER_PORT 8000
#define MAX_PACKET_SIZE 9400
/*-------------------------------------------------------*/
/* ReportError
/* Checking & display of RTP status codes
/* Returns true if there was an error, false otherwise
/*-------------------------------------------------------*/
int ReportError(int errCode)
{
int isErr = (errCode < 0);
if (isErr) {
std::string stdErrStr = RTPGetErrorString(errCode);
printf("Error %d: %s\n", errCode, stdErrStr.c_str());
printf("======================================\nPress Enter to exit\n");
getchar();
}
return isErr;
}
/*-------------------------------------------------------*/
/* RunRtpServer
/*-------------------------------------------------------*/
void RunRtpServer()
{
RTPSession rtpSession;
//setup session parameters
RTPSessionParams sessParams;
sessParams.SetOwnTimestampUnit(1.0 / 115.0); //30 video frames per second
sessParams.SetUsePollThread(1); //background thread to call virtual callbacks - set by default, but just to be sure
sessParams.SetMaximumPacketSize(MAX_PACKET_SIZE);
//setup transmission parameters
RTPUDPv4TransmissionParams transParams;
transParams.SetPortbase(SERVER_PORT); // portbase 是本地端口的意思。
//CREATE THE SESSION
int status = rtpSession.Create(sessParams, &transParams);
if (ReportError(status))
return; //unable to create the session
printf("RTP session created with portbase %d\n", SERVER_PORT);
//ADD THE MULTICAST to our destination
unsigned long intIP = inet_addr(MCAST_IP);
_ASSERT(intIP != INADDR_NONE);
intIP = ntohl(intIP); //put in host byte order
RTPIPv4Address rtpAddr(intIP, MCAST_PORT);
status = rtpSession.AddDestination(rtpAddr); //将客户端的ip和端口号事先添加进来。(不事先添加不行吗?)
//liyani add
FILE *fp;
fp = fopen("./my.h264","rb");
if (fp == NULL)
{
printf("Can not open file \n");
}
//liyani add
if (!ReportError(status)) {
printf("Transmitting to multicast group %s port %d\n\n", MCAST_IP, MCAST_PORT);
char testBuf[MAX_PACKET_SIZE];
while (1) {
printf("\n_______________________________________________________\nPress Enter key to send data - 'x' to exit\n");
int ch = getchar();
if (tolower(ch) == 'x')
break;
//SEND a packet
//int byteCnt = rand() % sizeof(testBuf); //这个%不是取余,而是指0~sizeof(testBuf)之间的随机数。
while(!feof(fp))
{
memset(testBuf,0,sizeof(testBuf));
//DWORD dwRead = fread(testBuf,1,sizeof(testBuf),fp);
DWORD dwRead = fread(testBuf,1,940,fp); //一次发送的长度,不可以是sizeof(testBuf),65535太大了。
if (0 == dwRead)
{
printf("Read data from mobile.mpg is 0 \n");
}
rtpSession.SendPacket(testBuf, dwRead, 33, false, 160);
//printf("%d bytes sent\n", byteCnt);
printf("%d bytes sent\n", dwRead);
//Sleep(2);
}
}
fclose(fp);
//LEAVE THE MULTICAST
rtpSession.DeleteDestination(rtpAddr);
}
rtpSession.Destroy();
}
/*-------------------------------------------------------*/
/* main
/* entry point for the application
/*-------------------------------------------------------*/