HxNvr/HxRtspBase/HxVideoDevices.cpp
2024-04-17 15:31:43 +08:00

145 lines
3.8 KiB
C++

#include "HxTrace.h"
#include "HxVideoDevices.h"
#include "HxVideoLiveStream.h"
#include <QMap>
QMap<QString, HxVideoLiveStream *> streams;
QMap<QUuid, HxVideoDeviceContext> contexts;
void video_status_callback(QString url, ConnectionStatus status)
{
switch (status)
{
case ConnectionStatus::Idling:
HxTrace::debug_write_line(url, "Idling");
break;
case ConnectionStatus::Connected:
HxTrace::debug_write_line(url, "Connected");
break;
case ConnectionStatus::Connecting:
HxTrace::debug_write_line(url, "Connecting");
break;
case ConnectionStatus::Disconnected:
HxTrace::debug_write_line(url, "Disconnected");
break;
}
QMapIterator<QUuid, HxVideoDeviceContext> array(contexts);
while (array.hasNext())
{
auto context = array.next().value();
if (context.rtspurl == url)
{
if (context.status_callback)
context.status_callback(context.uuid, status);
}
}
}
void video_stream_callback(QString url, HxVideoFrame videoframe)
{
QMapIterator<QUuid, HxVideoDeviceContext> array(contexts);
while (array.hasNext())
{
auto context = array.next().value();
if (context.rtspurl == url)
{
if (context.stream_callback)
context.stream_callback(context.uuid, videoframe.copy());
}
}
videoframe.free();
}
QString HxVideoDevices::rtspurl(QString type, QString address, int port, int channel, int stream)
{
QString rtspurl = QString::Null();
auto value = type.toUpper();
if (value == "LION" || value == "HIK")
rtspurl = QString("rtsp://admin:hik12345@%1:%2/h264/ch1/%3/av_stream").arg(address).arg(port).arg(stream == 0 ? "main" : "sub");
else if (value == "XM")
rtspurl = QString("rtsp://%1:%2/user=admin&password=&channel=%3&stream=%4.sdp?real_stream").arg(address).arg(port).arg(channel+1).arg(stream);
else if (value == "XIANGFEI")
rtspurl = QString("rtsp://%1:%2/stander/livestream/0/%3").arg(address).arg(port).arg(stream);
else if (value == "BSM")
rtspurl = QString("rtsp://%1:%2/%3").arg(address).arg(port).arg(stream == 0 ? 11 : 12);
else if (value == "AHV")
rtspurl = QString("rtsp://%1:%2/dev/video%3").arg(address).arg(port).arg(channel);
return rtspurl;
}
void HxVideoDevices::connect(QString rtspurl)
{
if (rtspurl.isNull())
return;
if (streams.contains(rtspurl))
return;
auto stream = new HxVideoLiveStream(0);
stream->set_status_callback(video_status_callback);
stream->set_stream_callback(video_stream_callback);
stream->set_url(rtspurl);
stream->set_uuid(rtspurl);
stream->start();
streams.insert(rtspurl, stream);
}
void HxVideoDevices::connect(QString type, QString address, int port, int channel, int stream)
{
if(stream == -1)
{
HxVideoDevices::connect(HxVideoDevices::rtspurl(type, address, port, channel, 0));
HxVideoDevices::connect(HxVideoDevices::rtspurl(type, address, port, channel, 1));
}
else
HxVideoDevices::connect(HxVideoDevices::rtspurl(type, address, port, channel, stream));
}
void HxVideoDevices::disconnect()
{
QMapIterator<QString, HxVideoLiveStream *> array(streams);
while (array.hasNext())
{
auto context = array.next();
auto stream = context.value();
stream->stop();
delete stream;
stream = nullptr;
}
streams.clear();
}
void HxVideoDevices::subscribe(HxVideoDeviceContext context)
{
if(!contexts.contains(context.uuid))
{
contexts.insert(context.uuid, context);
}
}
void HxVideoDevices::unsubscribe(HxVideoDeviceContext context)
{
if(contexts.contains(context.uuid))
{
contexts.remove(context.uuid);
}
}