123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- #include "VideoPlayer.h"
- #include "DecodeVedio.h"
- #include <QPainter>
- #include <QResizeEvent>
- #include <QEventLoop>
- #include "spdlog/spdlog.h"
- VideoPlayer::VideoPlayer(QWidget *parent) : QWidget(parent)
- {
-
- m_threadDecode = new QThread(this);
- m_decodeVedio = new DecodeVedio(m_threadDecode);
- m_semRefresh = new QSemaphore(0);
- m_timerRefreshUI.setSingleShot(false);
-
- m_timerRefreshUI.setTimerType(Qt::PreciseTimer);
- connect(&m_timerRefreshUI, &QTimer::timeout, this, &VideoPlayer::do_refreshUI);
- connect(m_decodeVedio, &DecodeVedio::signal_oneImage, this, &VideoPlayer::do_refreshOneUI);
- connect(m_decodeVedio, &DecodeVedio::signal_playCompleted, this, &VideoPlayer::do_playCompleted);
- SPDLOG_TRACE("UI线程ID:{}", QThread::currentThreadId());
- }
- VideoPlayer::~VideoPlayer()
- {
- if(m_timerRefreshUI.isActive())
- {
- m_timerRefreshUI.stop();
- }
- delete m_decodeVedio;
- if(m_image)
- {
- delete m_image;
- }
- }
- void VideoPlayer::setPlayVedio(const QString& fileName)
- {
- if(isSetVedioFile)
- {
- isSetVedioFile = false;
- }
- if(m_decodeVedio->isInitFFmpeg())
- {
- m_decodeVedio->unInitFFmpeg();
- }
- m_fileName = fileName;
- isSetVedioFile = true;
- m_decodeVedio->initFFmpeg(m_fileName);
-
-
- m_srcWidth = m_decodeVedio->getSrcVideoWidth();
- m_srcHeight = m_decodeVedio->getSrcVideoHeight();
- m_frameCount = m_decodeVedio->getFrameCount();
- SPDLOG_DEBUG("视频宽:{} 高:{} 帧数:{}", m_srcWidth, m_srcHeight, m_frameCount);
-
- this->setMinimumSize(160,90);
-
- if(m_frameCount < 0)
- {
-
- if(m_frameCount == -2)
- {
- m_frameCount = 24;
- }
- else {
- m_frameCount = 25;
- }
- }
- else if(m_frameCount == 0)
- {
- m_frameCount = 25;
- }
- SPDLOG_INFO("帧率:{}", m_frameCount);
-
- m_decodeVedio->startDecodeVedio();
- m_semRefresh->release(2);
-
-
- }
- bool VideoPlayer::play()
- {
- if(!isSetVedioFile)
- {
- SPDLOG_ERROR("文件名为空");
- return false;
- }
- if(m_playStatus)
- {
- return false;
- }
-
-
- m_timerRefreshUI.setSingleShot(false);
- m_interval = 1000/m_frameCount;
- if(1000 % m_frameCount > 5)
- {
- m_frameCount += 1;
- }
- SPDLOG_TRACE("刷新UI的定时间隔:{}",m_interval);
- m_timerRefreshUI.start(m_interval);
- m_playStatus = true;
-
- return true;
- }
- void VideoPlayer::pause()
- {
- if(!m_playStatus)
- {
- return;
- }
- m_timerRefreshUI.stop();
- m_playStatus = false;
- }
- void VideoPlayer::stop()
- {
- SPDLOG_DEBUG("...停止播放...");
-
- if(m_timerRefreshUI.isActive())
- {
- m_timerRefreshUI.stop();
- }
-
-
-
- setPlayVedio(m_fileName);
-
-
-
-
-
- m_playStatus = false;
-
- }
- qint64 VideoPlayer::getDuration()
- {
- return m_decodeVedio->getDuration();
- }
- qint64 VideoPlayer::getCurrentPos()
- {
- return m_decodeVedio->getCurrentPos();
- }
- void VideoPlayer::setCurrentPos(quint64 pos)
- {
-
- bool temp = m_playStatus;
- if(m_playStatus)
- {
- m_timerRefreshUI.stop();
- m_playStatus = false;
- }
- m_decodeVedio->setCurrentPos(pos);
-
- if(temp)
- {
-
- m_timerRefreshUI.start(m_interval);
- m_playStatus = true;
- }else
- {
-
- m_semRefresh->release(5);
- }
- }
- void VideoPlayer::setPlayVedioSize(int width,int height)
- {
-
- double srcRatio = m_srcWidth*1.0 / m_srcHeight;
- double ratio = width*1.0 / height;
- long w1 = 0, h1 = 0;
- int srcX = this->pos().rx(), srcY = this->pos().ry();
- int x1 = srcX, y1 = srcY;
- if(ratio > srcRatio)
- {
- w1 = height * srcRatio;
- x1 = (width - w1) / 2;
- h1 = height;
- y1 = srcY;
- }
- else if(ratio < srcRatio)
- {
- h1 = width / srcRatio;
- y1 = (height - h1) / 2;
- w1 = width;
- x1 = srcX;
- }else {
- w1 = width;
- h1 = height;
- x1 = srcX;
- y1 = srcY;
- }
- this->move(x1, y1);
-
- m_nowWidth = w1;
- m_nowHeight = h1;
- this->resize(w1, h1);
-
- SPDLOG_DEBUG("现在位置和大小:{}x{}, {}x{}", this->pos().rx(), this->pos().ry(), this->width(), this->height());
- }
- void VideoPlayer::setPlayCallBack(std::function<Play_CallBack> playCallBack,void* context)
- {
- m_funcPlayCB = playCallBack;
- m_context = context;
- }
- void VideoPlayer::paintEvent(QPaintEvent *event)
- {
- if(m_image)
- {
-
-
- QImage image;
- if(m_srcWidth != m_nowWidth || m_srcHeight != m_nowHeight)
- {
- image = m_image->scaled(m_nowWidth, m_nowHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- }
- QPainter painter(this);
- painter.drawImage(0, 0, image);
- }
- }
- void VideoPlayer::resizeEvent(QResizeEvent *event)
- {
- SPDLOG_TRACE("窗口大小改变...");
- m_nowWidth = event->size().width();
- m_nowHeight = event->size().height();
-
-
- QWidget::resizeEvent(event);
- }
- void VideoPlayer::refreshOneUIUntilHave()
- {
- if(m_decodeVedio != nullptr)
- {
-
-
- if(m_image != nullptr)
- {
- delete m_image;
- m_image = nullptr;
- }
-
- m_image = m_decodeVedio->getOneImageUntilHave();
-
- if(m_image)
- {
-
-
-
-
-
- update();
- }
- m_decodeVedio->wakeUpCondQueueNoEmpty();
- }
- }
- void VideoPlayer::mouseDoubleClickEvent(QMouseEvent *event)
- {
- if(event->button() == Qt::LeftButton)
- {
-
- if(m_funcPlayCB != nullptr)
- {
- m_funcPlayCB(this, 5, nullptr, 0, m_context);
- }else {
- SPDLOG_INFO("没有设置回调函数");
- }
- }
- }
- void VideoPlayer::do_refreshUI()
- {
- if(m_decodeVedio != nullptr)
- {
-
-
- if(m_image != nullptr)
- {
- delete m_image;
- m_image = nullptr;
- }
- m_image = m_decodeVedio->getOneImage();
-
- if(m_image)
- {
- if(m_srcWidth != m_nowWidth || m_srcHeight != m_nowHeight)
- {
- *m_image = m_image->scaled(m_nowWidth, m_nowHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- }
-
- update();
- }
- m_decodeVedio->wakeUpCondQueueNoEmpty();
- }
- }
- void VideoPlayer::do_refreshOneUI()
- {
- if(!m_semRefresh->tryAcquire(1))
- {
- return;
- }
-
- if(m_decodeVedio != nullptr)
- {
-
-
- if(m_image != nullptr)
- {
- delete m_image;
- m_image = nullptr;
- }
- m_image = m_decodeVedio->getOneImage();
-
- if(m_image)
- {
- if(m_srcWidth != m_nowWidth || m_srcHeight != m_nowHeight)
- {
- *m_image = m_image->scaled(m_nowWidth, m_nowHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- }
- SPDLOG_DEBUG("绘制预览画面...");
- update();
- }
- m_decodeVedio->wakeUpCondQueueNoEmpty();
- }
- }
- void VideoPlayer::do_playCompleted()
- {
- SPDLOG_INFO("播放完成...");
- m_timerRefreshUI.stop();
- m_playStatus = false;
- if(m_funcPlayCB != nullptr)
- {
-
- m_funcPlayCB(this, 2, nullptr, 0, m_context);
- }
- }
|