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

📄 main.cpp

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

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

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

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

#include "RTPAppSession.h"
#include <crtdbg.h> //for _ASSERT
//JRTP includes
#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 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;
}

/*-------------------------------------------------------*/
/* RunRtpClient
/*-------------------------------------------------------*/
void RunRtpClient()
{
	RTPAppSession 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(MCAST_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", MCAST_PORT);

	//JOIN THE MULTICAST
	// - data will be handled in the RTPAppSession::OnRTPPacket callback
	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.JoinMulticastGroup(rtpAddr);
	if (!ReportError(status)) {
		printf("Multicast group joined: %s port %d\n", MCAST_IP, MCAST_PORT);

		//just sit here, with the background thread doing the work...
		printf("\n======================================\nListening for data - Press Enter key to exit\n");
		getchar();

		//LEAVE THE MULTICAST
		rtpSession.LeaveMulticastGroup(rtpAddr);
	}
	//DESTROY THE SESSION
	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

	RunRtpClient();

	WSACleanup();
	return 0;
}

⌨️ 快捷键说明

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