同时创建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&)
{
}
}