设备播放模块是后面增加的,核心就是通过组合rtsp视频流地址来播放实时视频和历史视频,目前市面上很多厂家比如排第一的海康都是支持直接rtsp通过NVR来播放某个通道视频流和回放某个通道的视频流,这些格式在网上都可以搜索到的,每个厂家的第一可能有点不一样,但是大致的信息都一样,比如要播放实时视频流,需要提供的信息有用户名、密码、NVR地址、对应的通道、码流类型(主码流/子码流),如果要播放历史视频流即回放视频,需要提供的信息除了上面的以外还有时间范围,需要限定一个时间范围才能拿到对应的视频流文件,这个时间戳有些厂家是1970年经过的秒数计算,有些是时间时间等,都需要按照具体厂家的格式约定来。
设备播放的原理流程其实就是厂家重新将拿到的视频流文件或者存储的视频文件打包再发出来,有些厂家用自己的算法,有些用live555之类的。整体来说可能多多少少都会参照一些开源的推流库,咨询过很多同行的朋友,基本上都会参考ffmpeg、live555之类的开源库,其实ffmpeg养活了国内不少的厂家,甚至不乏一些大厂,再放大点说github养活了N多的公司,尤其是AI人工智能企业,业内有段话说:如果github不能允许访问了,国内的AI水平倒退5年。
通用视频控件开源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名称:videowidget
体验地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名称:bin_video_system.zip
#include "frmvideoplaynvr.h"
#include "ui_frmvideoplaynvr.h"
#include "quiwidget.h"
#include "iconfont.h"
#include "videowidget.h"
#ifdef videovlc
#include "vlc.h"
#elif videoffmpeg
#include "ffmpeg.h"
#elif easyplayer
#include "easyplayer.h"
#endif
frmVideoPlayNvr::frmVideoPlayNvr(QWidget *parent) : QWidget(parent), ui(new Ui::frmVideoPlayNvr)
{
ui->setupUi(this);
this->initForm();
this->initIcon();
this->initAddr();
this->initVideo();
}
frmVideoPlayNvr::~frmVideoPlayNvr()
{
delete ui;
}
bool frmVideoPlayNvr::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
if (watched->inherits("QWidget")) {
QWidget *widget = (QWidget *) watched;
videoIndex = widget->property("index").toInt();
ui->labTip->setText(QString("当前选中 %1").arg(widgets.at(videoIndex)->getBgText()));
}
} else if (event->type() == QEvent::MouseButtonDblClick) {
if (watched->inherits("QWidget")) {
QWidget *widget = (QWidget *) watched;
if (!videoMax) {
for (int i = 0; i < videoCount; i++) {
widgets.at(i)->setVisible(false);
}
videoMax = true;
widget->setVisible(true);
} else {
for (int i = 0; i < videoCount; i++) {
widgets.at(i)->setVisible(true);
}
videoMax = false;
}
widget->setFocus();
}
}
return QWidget::eventFilter(watched, event);
}
void frmVideoPlayNvr::initForm()
{
ui->cboxCompany->addItems(DBData::NvrTypes);
ui->cboxCompany->setCurrentIndex(ui->cboxCompany->findText("深广"));
ui->cboxType->addItem("实时视频");
ui->cboxType->addItem("回放视频");
for (int i = 1; i <= 16; i++) {
ui->cboxCh->addItem(QString("通道%1").arg(i));
}
ui->cboxRtsp->addItem("主码流");
ui->cboxRtsp->addItem("子码流");
ui->dateTimeStart->calendarWidget()->setLocale(QLocale::Chinese);
ui->dateTimeEnd->calendarWidget()->setLocale(QLocale::Chinese);
ui->dateTimeStart->setDate(QDate::currentDate().addDays(-1));
ui->dateTimeEnd->setDate(QDate::currentDate());
//绑定变动自动填入视频流地址
connect(ui->cboxCompany, SIGNAL(currentIndexChanged(int)), this, SLOT(initAddr()));
connect(ui->cboxType, SIGNAL(currentIndexChanged(int)), this, SLOT(initAddr()));
connect(ui->txtName, SIGNAL(textChanged(QString)), this, SLOT(initAddr()));
connect(ui->txtPwd, SIGNAL(textChanged(QString)), this, SLOT(initAddr()));
connect(ui->txtIP, SIGNAL(textChanged(QString)), this, SLOT(initAddr()));
connect(ui->cboxCh, SIGNAL(currentIndexChanged(int)), this, SLOT(initAddr()));
connect(ui->cboxRtsp, SIGNAL(currentIndexChanged(int)), this, SLOT(initAddr()));
connect(ui->dateTimeStart, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(initAddr()));
connect(ui->dateTimeEnd, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(initAddr()));
}
void frmVideoPlayNvr::initIcon()
{
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 15;
QSize iconSize = QSize(pixWidth, pixHeight);
QPixmap pix1 = IconHelper::Instance()->getPixmap(QUIConfig::TextColor, 0xf04b, size, pixWidth, pixHeight);
QPixmap pix2 = IconHelper::Instance()->getPixmap(QUIConfig::TextColor, 0xf00d, size, pixWidth, pixHeight);
QPixmap pix3 = IconHelper::Instance()->getPixmap(QUIConfig::TextColor, 0xf04d, size, pixWidth, pixHeight);
QPixmap pix4 = IconHelper::Instance()->getPixmap(QUIConfig::TextColor, 0xf061, size, pixWidth, pixHeight);
ui->btnPlay->setIconSize(iconSize);
ui->btnDelete->setIconSize(iconSize);
ui->btnPause->setIconSize(iconSize);
ui->btnNext->setIconSize(iconSize);
ui->btnPlay->setIcon(QIcon(pix1));
ui->btnDelete->setIcon(QIcon(pix2));
ui->btnPause->setIcon(QIcon(pix3));
ui->btnNext->setIcon(QIcon(pix4));
}
void frmVideoPlayNvr::initAddr()
{
QString company = ui->cboxCompany->currentText();
QString type = ui->cboxType->currentText();
QString name = ui->txtName->text().trimmed();
QString pwd = ui->txtPwd->text().trimmed();
QString ip = ui->txtIP->text().trimmed();
int ch = ui->cboxCh->currentIndex();
int rtsp = ui->cboxRtsp->currentIndex();
QString dateStart = ui->dateTimeStart->dateTime().toString("yyyy-MM-dd HH:mm:ss");
QString dateEnd = ui->dateTimeEnd->dateTime().toString("yyyy-MM-dd HH:mm:ss");
//深广NVR
//实时预览格式 rtsp://admin:[email protected]:554/live?channel=1&stream=1
//视频回放格式 rtsp://admin:[email protected]:554/file?channel=1&start=1494485280&stop=1494485480
//先转换时间戳,1970年到该时间经过的秒数
QDateTime startTime = QDateTime::fromString(dateStart, "yyyy-MM-dd HH:mm:ss");
QDateTime stopTime = QDateTime::fromString(dateEnd, "yyyy-MM-dd HH:mm:ss");
qint64 startTimeSec = startTime.toTime_t();
qint64 stopTimeSec = stopTime.toTime_t();
//海康NVR
//实时预览格式 rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast
//视频回放格式 rtsp://admin:[email protected]:554/Streaming/tracks/101?starttime=20120802t063812z&endtime=20120802t064816z
//流媒体取流 rtsp://172.6.24.15:554/Devicehc8://172.6.22.106:8000:0:0?username=admin&password=12345
//日期时间格式 ISO 8601 表示Zulu(GMT) 时间 YYYYMMDD”T”HHmmSS.fraction”Z”,
//unicast表示单播,multicast表示多播,默认单播可以省略
//101,1是通道号 01是通道的码流编号 也可以是02 03
QString starttime = ui->dateTimeStart->dateTime().toString(Qt::ISODate);
QString endtime = ui->dateTimeEnd->dateTime().toString(Qt::ISODate);
starttime = starttime.replace("-", "");
starttime = starttime.replace(":", "");
starttime = starttime.toLower();
endtime = endtime.replace("-", "");
endtime = endtime.replace(":", "");
endtime = endtime.toLower();
QString addr;
if (company == "深广") {
if (type == "实时视频") {
addr = QString("rtsp://%1:%2@%3:554/live?channel=%4&stream=%5")
.arg(name).arg(pwd).arg(ip).arg(ch + 1).arg(rtsp);
} else if (type == "回放视频") {
addr = QString("rtsp://%1:%2@%3:554/file?channel=%4&start=%5&stop=%6")
.arg(name).arg(pwd).arg(ip).arg(ch + 1).arg(startTimeSec).arg(stopTimeSec);
}
} else if (company == "海康") {
if (type == "实时视频") {
addr = QString("rtsp://%1:%2@%3:554/Streaming/Channels/%4%5%6")
.arg(name).arg(pwd).arg(ip).arg(ch + 1).arg(0).arg(rtsp + 1);
} else if (type == "回放视频") {
addr = QString("rtsp://%1:%2@%3:554/Streaming/tracks/%4%5?starttime=%6&endtime=%7")
.arg(name).arg(pwd).arg(ip).arg(ch + 1).arg("01").arg(starttime).arg(endtime);
}
} else if (company == "大华") {
if (type == "实时视频") {
} else if (type == "回放视频") {
}
}
ui->txtAddr->setText(addr);
}
void frmVideoPlayNvr::initVideo()
{
videoMax = false;
videoCount = 4;
videoIndex = 0;
for (int i = 0; i < videoCount; i++) {
#ifdef videovlc
VlcWidget *widget = new VlcWidget;
widget->setCallback(true);
//widget->setHardware("auto");
#elif videoffmpeg
FFmpegWidget *widget = new FFmpegWidget;
//widget->setHardware("d3d11va");
#elif easyplayer
EasyPlayerWidget *widget = new EasyPlayerWidget;
#else
VideoWidget *widget = new VideoWidget;
#endif
//设置背景文字
widget->setBgText(QString("通道 %1").arg(i + 1));
//设置背景图片
widget->setBgImage(QImage(":/bg_novideo.png"));
//设置url地址
widget->setUrl("");
//设置悬浮条可见
widget->setFlowEnable(false);
//设置是否自动重连
widget->setCheckLive(false);
widget->installEventFilter(this);
widget->setProperty("index", i);
widget->setObjectName(QString("video%1").arg(i + 1));
widgets.append(widget);
}
//加入到布局中
ui->gridLayout->addWidget(widgets.at(0), 0, 0);
ui->gridLayout->addWidget(widgets.at(1), 0, 1);
ui->gridLayout->addWidget(widgets.at(2), 1, 0);
ui->gridLayout->addWidget(widgets.at(3), 1, 1);
}
void frmVideoPlayNvr::on_btnPlay_clicked()
{
QString addr = ui->txtAddr->toPlainText();
if (addr.isEmpty()) {
return;
}
widgets.at(videoIndex)->setUrl(addr);
widgets.at(videoIndex)->close();
widgets.at(videoIndex)->open();
widgets.at(videoIndex)->setFocus();
}
void frmVideoPlayNvr::on_btnDelete_clicked()
{
widgets.at(videoIndex)->close();
widgets.at(videoIndex)->setFocus();
}
void frmVideoPlayNvr::on_btnPause_clicked()
{
widgets.at(videoIndex)->pause();
widgets.at(videoIndex)->setFocus();
}
void frmVideoPlayNvr::on_btnNext_clicked()
{
widgets.at(videoIndex)->next();
widgets.at(videoIndex)->setFocus();
}