























随着用户数量的增多,没有想过光一个推流还需要分这么多种,最开始只有个点播预览推流,就是直接在设备树双击通道点播画面,点播成功后,自动将这个通道的视音频流数据推出去,比如推到zlm或者mediamtx等,这样外部可以通过rtsp/rtmp/hls/flv/webrtc等方式访问,用久了你会发现,每次这样操作都很累,于是需要有个开关,开启自动推流,每次设备通道上线后,自动点播推流,相当于作为后台服务使用,这样方便是很方便了,但是挺浪费带宽的,只要是上线就推流,白白占用拉流带宽,于是后面有用户要求定义一套交互协议,通过指令来点播预览,而且还有超时时间,超时无人观看,自动关闭点播。后面又加了过滤,如果该通道已经存在点播,不是每个点播指令都点播,直接复用已经点播好了的流地址,返回给点播的用户即可,又精进了很多。
光有实时视频流的点播还是不够的,还有录像回放需要点播,而且切换播放进度或者倍速播放的时候,会立即推流最新的画面,指令回放也支持指定时间段回放录像文件,整个流程走下来,现在已经算是非常完善了,能够满足目前遇到的各种需求场景。

#include "gb28181widgetpush.h"
#include "gb28181helper.h"
#include "gb28181widget.h"
#include "gb28181server.h"
#include "rtpthreadreceive.h"
SINGLETON_IMPL(GB28181WidgetPush)
GB28181WidgetPush::GB28181WidgetPush(QObject *parent) : QObject(parent)
{
server = NULL;
//定时器排队推流
timerPush = new QTimer(this);
connect(timerPush, SIGNAL(timeout()), this, SLOT(checkPush()));
//定时器处理重连
timerStatus = new QTimer(this);
connect(timerStatus, SIGNAL(timeout()), this, SLOT(checkStatus()));
}
void GB28181WidgetPush::setPara(int videoCore, int decodeType, bool decodeAudio, bool disableDecode, const QString &hardware, int profile, TransmitMode mode, const QString &serverIp, const QString &pushUrl)
{
this->videoCore = videoCore;
this->decodeType = decodeType;
this->decodeAudio = decodeAudio;
this->disableDecode = disableDecode;
this->hardware = hardware;
this->profile = profile;
this->mode = mode;
this->serverIp = serverIp;
this->pushUrl = pushUrl;
}
void GB28181WidgetPush::setDecodeAudio(bool decodeAudio)
{
this->decodeAudio = decodeAudio;
}
void GB28181WidgetPush::setServer(GB28181Server *server)
{
this->server = server;
}
void GB28181WidgetPush::start(int interval)
{
timerPush->start(1000);
if (interval > 0) {
timerStatus->stop();
timerStatus->start(interval);
}
}
void GB28181WidgetPush::stop()
{
QMutexLocker locker(&mutex);
timerPush->stop();
timerStatus->stop();
if (widgets.count() > 0) {
qApp->processEvents();
}
foreach (GB28181Widget *widget, widgets) {
widget->stop();
widget->deleteLater();
}
server = NULL;
times.clear();
widgets.clear();
deviceIds.clear();
channelIds.clear();
onlines.clear();
}
void GB28181WidgetPush::getLiveInfo(QStringList &deviceIds, QStringList &channelIds)
{
foreach (GB28181Widget *widget, widgets) {
if (widget->getIsRunning()) {
deviceIds << widget->getDeviceId();
channelIds << widget->getChannelId();
}
}
}
void GB28181WidgetPush::openVideo(const QString &deviceId, const QString &channelId, const QString &pushUrl, const QString &startTime, const QString &endTime)
{
#ifdef betaversion
int maxCount = 4;
if (widgets.count() >= maxCount) {
//QMessageBox::critical(0, "错误", QString("试用版最多同时推流 %1 路, 请联系作者(微信feiyangqingyun)购买源码编译正式版!").arg(maxCount));
return;
}
#endif
//服务不存在或者推流地址为空则不用继续
if (!server || pushUrl.isEmpty()) {
return;
}
//已经存在的则不用继续
if (this->existPush(deviceId, channelId, pushUrl) >= 0) {
return;
}
//创建一个视频窗体用来拉流
bool preview = (startTime.isEmpty() && endTime.isEmpty());
GB28181Widget *widget = new GB28181Widget;
widget->setProperty("disableDecode", disableDecode);
widget->setObjectName(QString("push_%1_%2").arg(preview ? "preview" : "record").arg(deviceId.right(3)));
connect(widget, SIGNAL(pushStart(GB28181Widget *, QString)), this, SIGNAL(pushStart(GB28181Widget *, QString)));
widget->initWidgetPara(ScaleMode_Auto, VideoMode_Hwnd, VideoCore(videoCore), decodeType, decodeAudio, hardware, 10000, true, false);
//带了时间范围的是回放或者下载
widget->setServer(server);
widget->setTime(startTime, endTime);
widget->setPara(1, profile, mode, serverIp, pushUrl, preview ? PlayType_Preview : PlayType_Playback);
widget->openVideo(deviceId, channelId);
times << QDateTime::currentDateTime();
widgets << widget;
//qDebug() << TIMEMS << "openVideo" << deviceId << channelId << pushUrl;
}
void GB28181WidgetPush::closeVideo(const QString &deviceId, const QString &channelId, const QString &pushUrl)
{
this->existPush(deviceId, channelId, pushUrl, true);
//qDebug() << TIMEMS << "closeVideo" << deviceId << channelId << pushUrl;
}
void GB28181WidgetPush::removeWidget(int index, GB28181Widget *widget, const QString &url)
{
times.removeAt(index);
widgets.removeAt(index);
widget->stop();
widget->deleteLater();
emit pushStop(url);
}
int GB28181WidgetPush::existPush(const QString &deviceId, const QString &channelId, const QString &key, bool remove)
{
//同一个通道可以支持同时预览和回放/限定两个同时预览或者同时回放
//除了设备编码和通道编码一致/还需要过滤推流地址前缀/预览和录像推流地址前缀不一样
for (int i = widgets.count() - 1; i >= 0; --i) {
GB28181Widget *widget = widgets.at(i);
QString url = widget->getRecordUrl();
if (widget->getDeviceId() != deviceId) {
continue;
}
//如果设备离线/通道编码为空/需要对这个设备的所有通道进行处理
if (channelId.isEmpty()) {
if (remove) {
this->removeWidget(i, widget, url);
}
} else {
if (widget->getChannelId() == channelId && url.contains(key)) {
if (remove) {
this->removeWidget(i, widget, url);
}
return i;
}
}
}
return -1;
}
QString GB28181WidgetPush::getPushSsrc(const QString &flag, const QString &deviceId, const QString &channelId)
{
int index = this->existPush(deviceId, channelId, flag);
if (index >= 0) {
return widgets.at(index)->getSsrcVideo();
} else {
return QString();
}
}
void GB28181WidgetPush::checkPush()
{
QMutexLocker locker(&mutex);
if (deviceIds.count() == 0) {
return;
}
QString deviceId = deviceIds.takeFirst();
QString channelId = channelIds.takeFirst();
bool online = onlines.takeFirst();
if (online) {
this->openVideo(deviceId, channelId, pushUrl);
} else {
this->closeVideo(deviceId, channelId, pushUrl);
}
}
void GB28181WidgetPush::checkStatus()
{
int count = widgets.count();
QDateTime now = QDateTime::currentDateTime();
for (int i = 0; i < count; ++i) {
GB28181Widget *widget = widgets.at(i);
RtpThreadReceive *rtpThread = widget->getRtpThread();
int timeout = widget->getVideoPara().readTimeout;
if (timeout <= 0) {
continue;
}
qint64 offset = rtpThread->getLastTime().msecsTo(now);
//qDebug() << TIMEMS << "GB28181WidgetPush" << widget->objectName() << widget->getIsRecord() << offset;
if (widget->isPushOk() && offset < timeout) {
continue;
}
//避免频繁的打开
offset = times.at(i).msecsTo(now);
QString deviceId = widget->getDeviceId();
QString channelId = widget->getChannelId();
emit pushStop(widget->getRecordUrl());
//qDebug() << TIMEMS << "GB28181WidgetPush" << widget->objectName() << offset;
if (offset > 15000) {
times[i] = QDateTime::currentDateTime();
widget->closeVideo(deviceId, channelId);
GB28181Helper::sleep(1500);
widget->openVideo(deviceId, channelId);
qDebug() << TIMEMS << QString("推流重连 -> 设备: %1 通道: %2").arg(deviceId).arg(channelId);
break;
}
}
}
//自动推流关联了这个信号/通知上线打开下线关闭
void GB28181WidgetPush::receiveEvent(GB28181Event event)
{
if (event.alarmMethod != 5 || (event.alarmInfo != 13 && event.alarmInfo != 14)) {
return;
}
this->channelChanged(event.deviceId, event.channelId, event.alarmInfo == 14);
}
//自动推流关联了这个信号/设备下线关闭所有通道
void GB28181WidgetPush::deviceChanged(const QString &deviceId, bool online)
{
//只需要关注下线/上线会通过通道变化信号触发
if (!online) {
this->channelChanged(deviceId, "", online);
}
}
//自动推流关联了这个信号/通道上线打开下线关闭
void GB28181WidgetPush::channelChanged(const QString &deviceId, const QList<GB28181Channel> &channels)
{
foreach (GB28181Channel channel, channels) {
this->channelChanged(deviceId, channel.channelId, channel.online);
}
}
void GB28181WidgetPush::channelChanged(const QString &deviceId, const QString &channelId, bool online)
{
QMutexLocker locker(&mutex);
#if 0
if (online) {
this->openVideo(deviceId, channelId, pushUrl);
} else {
this->closeVideo(deviceId, channelId, pushUrl);
}
#else
//过滤下重复的/设备上线后可能会触发多次通道变化信号
int count = deviceIds.count();
for (int i = 0; i < count; ++i) {
if (deviceIds.at(i) == deviceId && channelIds.at(i) == channelId && onlines.at(i) == online) {
return;
}
}
//加入队列排队处理
deviceIds << deviceId;
channelIds << channelId;
onlines << online;
#endif
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。