⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 使用RTP协议写的一个传输程序
💻 CPP
字号:
/*---------------------------------------------------------------------------*/
/* main.cpp
/*---------------------------------------------------------------------------*/

#pragma comment(lib, "jrtplib.lib")

#pragma comment(lib, "jthread.lib")

#pragma comment(lib, "WS2_32.lib")

#include <crtdbg.h> //for _ASSERT
//JRTP includes
#include "RTPSession.h"
#include "RTPIPv4Address.h"
#include "RTPSessionParams.h"
#include "RTPUDPv4Transmitter.h"
/*
#ifndef WIN32
	#include <netinet/in.h>
	#include <arpa/inet.h>
#else
	#include <winsock2.h>
#endif // WIN32
*/
//#define MCAST_IP		"239.216.30.54"
//#define MCAST_IP		"192.168.1.204"
#define MCAST_IP		"127.0.0.1"
#define MCAST_PORT		9000
#define SERVER_PORT		5000

#define MAX_PACKET_SIZE	((1024 * 64) - 1)

/*-------------------------------------------------------*/
/* 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 / 30.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);
	//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);

	//SET TRANSMISSION DEFAULTS if we want to use them
	//rtpSession.SetDefaultPayloadType(96);
	//rtpSession.SetDefaultMark(false);
	//rtpSession.SetDefaultTimestampIncrement(160);

	//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);
	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);
			rtpSession.SendPacket(testBuf, byteCnt, 0, false, 10UL);
			printf("%d bytes sent\n", byteCnt);
		}

		//LEAVE THE MULTICAST
		rtpSession.DeleteDestination(rtpAddr);
	}
	rtpSession.Destroy();
}

/*-------------------------------------------------------*/
/* main
/* entry point for the application
/*-------------------------------------------------------*/
int main()
{
	//MUST call WSAStartup() to use WS2_32.DLL
	WSADATA wsaData;
	WORD wVersionRequested = MAKEWORD( 2, 2 );
	WSAStartup(wVersionRequested, &wsaData); //WSACleanup called in OnDestroy

	RunRtpServer();

	WSACleanup();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -