|
|
|
#include "ffmpegvideodlg.h"
|
|
|
|
#include "ui_ffmpegvideodlg.h"
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
ffmpegvideoDlg::ffmpegvideoDlg(QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::ffmpegvideoDlg) {
|
|
|
|
ui->setupUi(this);
|
|
|
|
ffmpegPushStream = nullptr;
|
|
|
|
pushStreamThread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ffmpegvideoDlg::~ffmpegvideoDlg() {
|
|
|
|
stop();
|
|
|
|
delete ui;
|
|
|
|
if (ffmpegPushStream != nullptr)
|
|
|
|
ffmpegPushStream->deleteLater();
|
|
|
|
if (pushStreamThread != nullptr) {
|
|
|
|
pushStreamThread->quit();
|
|
|
|
pushStreamThread->wait();
|
|
|
|
pushStreamThread->deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::setVedioSaveFileDirPath(QString saveDirPath) {
|
|
|
|
videoSaveDirPath = saveDirPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::play(QString url) {
|
|
|
|
if (!m_PlayStatus) {
|
|
|
|
m_PlayStatus = true;
|
|
|
|
ffmpeg = new Cffmpeg_decode;
|
|
|
|
ffmpeg->setSaveFileDirPath(videoSaveDirPath);
|
|
|
|
ffmpeg->IsstopPlay = false;
|
|
|
|
if (!pushStreamIP.isEmpty()) // 推流
|
|
|
|
startPushStream(m_PlayStatus);
|
|
|
|
ffmpeg->moveToThread(&workerThread);
|
|
|
|
|
|
|
|
connect(&workerThread, &QThread::finished, ffmpeg,
|
|
|
|
&QObject::deleteLater); // 线程发送结束标志
|
|
|
|
connect(this, &ffmpegvideoDlg::operate, ffmpeg,
|
|
|
|
&Cffmpeg_decode::run); // 线程开始处理数据
|
|
|
|
connect(this, &ffmpegvideoDlg::setUrlSign, ffmpeg,
|
|
|
|
&Cffmpeg_decode::setUrl); // 设置URL
|
|
|
|
connect(ffmpeg, SIGNAL(sendQImage(QImage)), this,
|
|
|
|
SLOT(receiveQImage(QImage))); // 发送解析的图片
|
|
|
|
connect(ffmpeg, SIGNAL(sendConnectFail(int)), this,
|
|
|
|
SLOT(showMessagBox(int))); // 发送错误信息提示
|
|
|
|
|
|
|
|
workerThread.start();
|
|
|
|
emit this->setUrlSign(url); // 设置URL
|
|
|
|
emit this->operate(); // 启用线程信号
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::stop() {
|
|
|
|
if (m_PlayStatus) {
|
|
|
|
ffmpeg->stop();
|
|
|
|
m_PlayStatus = false;
|
|
|
|
ffmpeg->IsstopPlay = true; // 线程结束标志
|
|
|
|
if (!pushStreamIP.isEmpty()) // 停止推流
|
|
|
|
startPushStream(m_PlayStatus);
|
|
|
|
workerThread.quit(); // 线程退出时自动delete线程里的类
|
|
|
|
workerThread.wait();
|
|
|
|
img.fill(Qt::black);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 绘图事件
|
|
|
|
void ffmpegvideoDlg::paintEvent(QPaintEvent *) {
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.drawImage(m_ax, m_ay, img);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 区域大小改变事件
|
|
|
|
void ffmpegvideoDlg::resizeEvent(QResizeEvent *event) {
|
|
|
|
// 区域1920*1080自适应
|
|
|
|
double VideoAreaHeight = this->height();
|
|
|
|
double VideoAreaWidth = this->width();
|
|
|
|
// 高度值相对比较大
|
|
|
|
if (VideoAreaHeight * 16 > VideoAreaWidth * 9) {
|
|
|
|
m_setVideoAreaWidth = VideoAreaWidth;
|
|
|
|
m_setVideoAreaHeight = VideoAreaWidth * 9 / 16;
|
|
|
|
m_ax = 0;
|
|
|
|
m_ay = 0.5 * (VideoAreaHeight - m_setVideoAreaHeight);
|
|
|
|
}
|
|
|
|
// 宽度值相对比较大
|
|
|
|
else {
|
|
|
|
m_setVideoAreaHeight = VideoAreaHeight;
|
|
|
|
m_setVideoAreaWidth = VideoAreaHeight * 16 / 9;
|
|
|
|
m_ax = 0.5 * (VideoAreaWidth - m_setVideoAreaWidth);
|
|
|
|
m_ay = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::receiveQImage(const QImage &rImg) {
|
|
|
|
|
|
|
|
// img = rImg.scaled(this->size());
|
|
|
|
QSize imgsize(m_setVideoAreaWidth, m_setVideoAreaHeight);
|
|
|
|
img = rImg.scaled(imgsize);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
// 显示不同提示消息
|
|
|
|
void ffmpegvideoDlg::showMessagBox(int type) {
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
QMessageBox::information(NULL, tr("提示"), "获取视频失败", QMessageBox::Ok);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool ffmpegvideoDlg::Isplay(bool IsstopPlay) {
|
|
|
|
return ffmpeg->IsstopPlay = IsstopPlay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::startPushStream(bool bPush) {
|
|
|
|
if (bPush) {
|
|
|
|
if (ffmpegPushStream == nullptr) {
|
|
|
|
ffmpegPushStream = new FFmpegPushStream;
|
|
|
|
ffmpegPushStream->setRemoteIP(pushStreamIP); // 设置推流地址
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pushStreamThread == nullptr) {
|
|
|
|
pushStreamThread = new QThread;
|
|
|
|
pushStreamThread->start();
|
|
|
|
ffmpegPushStream->moveToThread(pushStreamThread);
|
|
|
|
}
|
|
|
|
connect(ffmpeg, &Cffmpeg_decode::sendInitPushStream_Signal,
|
|
|
|
ffmpegPushStream, &FFmpegPushStream::openNetworkStream);
|
|
|
|
connect(ffmpeg, &Cffmpeg_decode::sendStreamData_Signal, ffmpegPushStream,
|
|
|
|
&FFmpegPushStream::pushStream);
|
|
|
|
ffmpeg->bPushStreamFlag = true;
|
|
|
|
} else {
|
|
|
|
if (ffmpeg)
|
|
|
|
ffmpeg->bPushStreamFlag = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ffmpegvideoDlg::setPushStreamIP(QString pushStreamURL) {
|
|
|
|
pushStreamIP = pushStreamURL;
|
|
|
|
}
|