RTSPServer实现多路推送封装

本文档展示了如何使用xop库启动RTSP服务器并创建50个媒体会话,每个会话对应一个实时通道。通过session_id列表进行访问和音视频数据推送。当客户端连接或断开时,会有回调通知。推送数据时,根据视频或音频类型设置相应的时间戳和帧类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同时创建50个live通道进行推送,只需要创建50个session ,通过session_id[]列表进行访问即可,创建方法如下代码:

1、启动服务器

    try
	{
		std::string suffix = "live";
		std::string ip = "127.0.0.1";
		std::string port = std::to_string(nPort);
		//std::string rtsp_url = "rtsp://" + ip + ":" + port + "/" + suffix;
		rtsp_server = xop::RtspServer::Create(event_loop.get());
		if (!rtsp_server->Start("0.0.0.0", atoi(port.c_str()))) {
			printf("RTSP Server listen on %s failed.\n", port.c_str());
			Ret = false;
		}
		for (int i = 0; i < MaxChannel; i++)
		{
			session = xop::MediaSession::CreateNew(suffix + std::to_string(i));
			session->AddSource(xop::channel_0, xop::H264Source::CreateNew());
			session->AddSource(xop::channel_1, xop::G711USource::CreateNew());
			//session->StartMulticast(); 
			session->AddNotifyConnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
				printf("RTSP client connect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
				});

			session->AddNotifyDisconnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
				printf("RTSP client disconnect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
				});

			session_id[i] = rtsp_server->AddSession(session);
		}
	}
	catch (const std::exception&)
	{

	}

2、推送音视频数据 

void EasyRtsp::PushFrame(BYTE* pdata, int datasize, bool keyframe, bool video, int index)
{
	try
	{
		if (index < MaxChannel)
		{
			xop::AVFrame RtspFrame = { 0 };
			RtspFrame.size = datasize;
			RtspFrame.buffer.reset(new uint8_t[RtspFrame.size]);
			memcpy(RtspFrame.buffer.get(), pdata, RtspFrame.size);
			if (video)
			{
				RtspFrame.timestamp = xop::H264Source::GetTimestamp(); // 时间戳, 建议使用编码器提供的时间戳
				RtspFrame.type = keyframe ? xop::VIDEO_FRAME_I : xop::VIDEO_FRAME_P;
			}
			else
			{
				RtspFrame.timestamp = xop::G711USource::GetTimestamp(); // 时间戳, 建议使用编码器提供的时间戳
				RtspFrame.type = xop::AUDIO_FRAME;
			}
			rtsp_server->PushFrame(session_id[index], video ? xop::channel_0 : xop::channel_1, RtspFrame); //推流到服务器, 接口线程安全
		}
	}
	catch (const std::exception&)
	{

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冀石程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值