- videoplayer_engine: декод в worker-потоке (FFmpeg), A/V sync по аудио-курсору, seek с epoch-защитой от устаревших кадров - videoplayer_window: окно без рамки, drag-перенос, dblclick/Alt+Enter fullscreen, Esc закрытие, timeline + play/pause/close, авто-скрытие контролов - видео-баббл в списке: превью-кадр (jpg), play-оверлей, длительность, прогресс - превью извлекается после скачивания (video_extract_thumbnail) в фоне - mainwindow: синглтон-плеер, openVideoRequested → openVideo - CMake: HAVE_FFMPEG для vibechat
301 lines
9.3 KiB
C++
301 lines
9.3 KiB
C++
#include "videoplayer_window.h"
|
|
#include "videoplayer_engine.h"
|
|
#include "../../lib/debug_config.h"
|
|
|
|
#include <QSlider>
|
|
#include <QPushButton>
|
|
#include <QLabel>
|
|
#include <QHBoxLayout>
|
|
#include <QVBoxLayout>
|
|
#include <QPainter>
|
|
#include <QKeyEvent>
|
|
#include <QMouseEvent>
|
|
#include <QCloseEvent>
|
|
#include <QApplication>
|
|
#include <QScreen>
|
|
#include <QStyle>
|
|
|
|
/* ─── VideoRenderWidget ─── */
|
|
|
|
VideoRenderWidget::VideoRenderWidget(QWidget* parent) : QWidget(parent) {
|
|
setMouseTracking(true);
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
}
|
|
|
|
void VideoRenderWidget::setFrame(const QImage& img) {
|
|
m_frame = img;
|
|
update();
|
|
}
|
|
|
|
void VideoRenderWidget::clearFrame() {
|
|
m_frame = QImage();
|
|
update();
|
|
}
|
|
|
|
void VideoRenderWidget::paintEvent(QPaintEvent*) {
|
|
QPainter p(this);
|
|
p.fillRect(rect(), Qt::black);
|
|
if (m_frame.isNull()) return;
|
|
|
|
QSize sz = m_frame.size();
|
|
sz.scale(size(), Qt::KeepAspectRatio);
|
|
QRect dst((width() - sz.width()) / 2, (height() - sz.height()) / 2, sz.width(), sz.height());
|
|
p.drawImage(dst, m_frame);
|
|
}
|
|
|
|
/* ─── VideoPlayerWindow ─── */
|
|
|
|
static QString fmtTime(qint64 ms) {
|
|
qint64 s = ms / 1000;
|
|
return QString("%1:%2").arg(s / 60).arg(s % 60, 2, 10, QChar('0'));
|
|
}
|
|
|
|
static QPoint mouseGlobalPos(const QMouseEvent* e) {
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
return e->globalPosition().toPoint();
|
|
#else
|
|
return e->globalPos();
|
|
#endif
|
|
}
|
|
|
|
VideoPlayerWindow::VideoPlayerWindow(QWidget* parent)
|
|
: QWidget(parent, Qt::Window | Qt::FramelessWindowHint)
|
|
{
|
|
setWindowTitle(QStringLiteral("Video"));
|
|
setFocusPolicy(Qt::StrongFocus);
|
|
setMouseTracking(true);
|
|
setStyleSheet("background-color: black;");
|
|
|
|
m_engine = new VideoPlayerEngine(this);
|
|
|
|
m_render = new VideoRenderWidget(this);
|
|
|
|
m_controls = new QWidget(this);
|
|
m_controls->setObjectName("videoControls");
|
|
m_controls->setStyleSheet(
|
|
"#videoControls { background-color: rgba(0,0,0,0.65); }"
|
|
"QPushButton { background: transparent; color: white; border: none; font-size: 16px; }"
|
|
"QPushButton:hover { background: rgba(255,255,255,0.2); }"
|
|
"QLabel { color: white; font-size: 12px; }"
|
|
"QSlider::groove:horizontal { height: 4px; background: #444; }"
|
|
"QSlider::sub-page:horizontal { background: #2a9; }"
|
|
"QSlider::handle:horizontal { width: 12px; margin: -4px 0; background: white; border-radius: 6px; }");
|
|
|
|
m_playBtn = new QPushButton(QStringLiteral("⏸"), m_controls);
|
|
m_playBtn->setToolTip(QStringLiteral("Пауза"));
|
|
m_closeBtn = new QPushButton(QStringLiteral("✕"), m_controls);
|
|
m_closeBtn->setToolTip(QStringLiteral("Закрыть"));
|
|
m_slider = new QSlider(Qt::Horizontal, m_controls);
|
|
m_slider->setRange(0, 0);
|
|
m_timeLabel = new QLabel(QStringLiteral("0:00 / 0:00"), m_controls);
|
|
|
|
auto* lay = new QHBoxLayout(m_controls);
|
|
lay->setContentsMargins(8, 4, 8, 4);
|
|
lay->setSpacing(6);
|
|
lay->addWidget(m_playBtn);
|
|
lay->addWidget(m_slider, 1);
|
|
lay->addWidget(m_timeLabel);
|
|
lay->addWidget(m_closeBtn);
|
|
|
|
m_hideTimer = new QTimer(this);
|
|
m_hideTimer->setSingleShot(true);
|
|
m_hideTimer->setInterval(2500);
|
|
connect(m_hideTimer, &QTimer::timeout, this, &VideoPlayerWindow::hideControls);
|
|
|
|
connect(m_playBtn, &QPushButton::clicked, this, &VideoPlayerWindow::onPlayPause);
|
|
connect(m_closeBtn, &QPushButton::clicked, this, &VideoPlayerWindow::onClose);
|
|
connect(m_slider, &QSlider::sliderPressed, this, &VideoPlayerWindow::onSliderPressed);
|
|
connect(m_slider, &QSlider::sliderReleased, this, &VideoPlayerWindow::onSliderReleased);
|
|
|
|
connect(m_engine, &VideoPlayerEngine::opened, this, &VideoPlayerWindow::onEngineOpened);
|
|
connect(m_engine, &VideoPlayerEngine::positionChanged, this, &VideoPlayerWindow::onPositionChanged);
|
|
connect(m_engine, &VideoPlayerEngine::stateChanged, this, &VideoPlayerWindow::onStateChanged);
|
|
connect(m_engine, &VideoPlayerEngine::error, this, &VideoPlayerWindow::onError);
|
|
connect(m_engine, &VideoPlayerEngine::framePresented, this, &VideoPlayerWindow::onFramePresented);
|
|
|
|
resize(640, 360);
|
|
}
|
|
|
|
VideoPlayerWindow::~VideoPlayerWindow() = default;
|
|
|
|
void VideoPlayerWindow::playFile(const QString& path) {
|
|
m_render->clearFrame();
|
|
m_slider->setRange(0, 0);
|
|
m_timeLabel->setText(QStringLiteral("0:00 / 0:00"));
|
|
show();
|
|
raise();
|
|
activateWindow();
|
|
m_engine->open(path);
|
|
}
|
|
|
|
void VideoPlayerWindow::onEngineOpened(int width, int height, qint64 durationMs, bool hasAudio) {
|
|
Q_UNUSED(hasAudio);
|
|
m_slider->setRange(0, (int)durationMs);
|
|
updateTimeLabels(0);
|
|
QSize s = fitWindowSize(width, height);
|
|
resize(s);
|
|
if (!m_fullscreen) {
|
|
QScreen* scr = QApplication::primaryScreen();
|
|
if (scr) {
|
|
QRect ag = scr->availableGeometry();
|
|
move(ag.center().x() - s.width() / 2, ag.center().y() - s.height() / 2);
|
|
}
|
|
}
|
|
DEBUG_INFO(DEBUG_CATEGORY_VIDEO, "VideoWindow: %dx%d dur=%lldms", width, height, (long long)durationMs);
|
|
}
|
|
|
|
void VideoPlayerWindow::onPositionChanged(qint64 ms) {
|
|
if (!m_sliderDragging) {
|
|
m_slider->blockSignals(true);
|
|
m_slider->setValue((int)ms);
|
|
m_slider->blockSignals(false);
|
|
}
|
|
updateTimeLabels(ms);
|
|
}
|
|
|
|
void VideoPlayerWindow::onStateChanged(int state) {
|
|
VideoPlayerEngine::State s = (VideoPlayerEngine::State)state;
|
|
if (s == VideoPlayerEngine::Playing) {
|
|
m_playBtn->setText(QStringLiteral("⏸"));
|
|
m_playBtn->setToolTip(QStringLiteral("Пауза"));
|
|
} else if (s == VideoPlayerEngine::Paused || s == VideoPlayerEngine::Finished) {
|
|
m_playBtn->setText(QStringLiteral("▶"));
|
|
m_playBtn->setToolTip(QStringLiteral("Играть"));
|
|
showControls();
|
|
}
|
|
}
|
|
|
|
void VideoPlayerWindow::onError(const QString& msg) {
|
|
DEBUG_ERROR(DEBUG_CATEGORY_VIDEO, "VideoWindow: %s", qPrintable(msg));
|
|
m_timeLabel->setText(msg);
|
|
showControls();
|
|
}
|
|
|
|
void VideoPlayerWindow::onFramePresented() {
|
|
m_render->setFrame(m_engine->currentFrame());
|
|
}
|
|
|
|
void VideoPlayerWindow::onSliderPressed() {
|
|
m_sliderDragging = true;
|
|
}
|
|
|
|
void VideoPlayerWindow::onSliderReleased() {
|
|
m_sliderDragging = false;
|
|
m_engine->seekMs(m_slider->value());
|
|
if (m_engine->state() == VideoPlayerEngine::Paused ||
|
|
m_engine->state() == VideoPlayerEngine::Finished) {
|
|
m_engine->play();
|
|
}
|
|
}
|
|
|
|
void VideoPlayerWindow::onPlayPause() {
|
|
if (m_engine->state() == VideoPlayerEngine::Playing) m_engine->pause();
|
|
else m_engine->togglePlayPause();
|
|
}
|
|
|
|
void VideoPlayerWindow::onClose() {
|
|
m_engine->close();
|
|
close();
|
|
}
|
|
|
|
void VideoPlayerWindow::toggleFullscreen() {
|
|
if (m_fullscreen) {
|
|
showNormal();
|
|
m_fullscreen = false;
|
|
setControlsVisible(true);
|
|
unsetCursor();
|
|
} else {
|
|
showFullScreen();
|
|
m_fullscreen = true;
|
|
m_hideTimer->start();
|
|
}
|
|
}
|
|
|
|
void VideoPlayerWindow::setControlsVisible(bool visible) {
|
|
m_controls->setVisible(visible);
|
|
}
|
|
|
|
void VideoPlayerWindow::showControls() {
|
|
setControlsVisible(true);
|
|
unsetCursor();
|
|
if (m_fullscreen) m_hideTimer->start();
|
|
}
|
|
|
|
void VideoPlayerWindow::hideControls() {
|
|
if (!m_fullscreen) return;
|
|
if (m_engine && (m_engine->state() == VideoPlayerEngine::Paused ||
|
|
m_engine->state() == VideoPlayerEngine::Finished)) return;
|
|
setControlsVisible(false);
|
|
setCursor(Qt::BlankCursor);
|
|
}
|
|
|
|
void VideoPlayerWindow::updateTimeLabels(qint64 posMs) {
|
|
qint64 dur = m_engine ? m_engine->durationMs() : 0;
|
|
m_timeLabel->setText(QString("%1 / %2").arg(fmtTime(posMs), fmtTime(dur)));
|
|
}
|
|
|
|
QSize VideoPlayerWindow::fitWindowSize(int vw, int vh) const {
|
|
if (vw <= 0 || vh <= 0) return QSize(640, 360);
|
|
QScreen* scr = QApplication::primaryScreen();
|
|
QSize avail(1280, 720);
|
|
if (scr) avail = scr->availableGeometry().size() * 0.85;
|
|
QSize s(vw, vh);
|
|
s.scale(avail, Qt::KeepAspectRatio);
|
|
return s;
|
|
}
|
|
|
|
void VideoPlayerWindow::keyPressEvent(QKeyEvent* e) {
|
|
if (e->key() == Qt::Key_Escape) {
|
|
if (m_fullscreen) { toggleFullscreen(); return; }
|
|
onClose();
|
|
return;
|
|
}
|
|
if (e->key() == Qt::Key_Return && (e->modifiers() & Qt::AltModifier)) {
|
|
toggleFullscreen();
|
|
return;
|
|
}
|
|
if (e->key() == Qt::Key_Space) {
|
|
onPlayPause();
|
|
return;
|
|
}
|
|
QWidget::keyPressEvent(e);
|
|
}
|
|
|
|
void VideoPlayerWindow::mousePressEvent(QMouseEvent* e) {
|
|
if (e->button() == Qt::LeftButton) {
|
|
if (m_fullscreen) {
|
|
/* клик в fullscreen — показать/скрыть контролы */
|
|
if (m_controls->isVisible()) hideControls();
|
|
else showControls();
|
|
} else {
|
|
m_dragging = true;
|
|
m_dragStart = mouseGlobalPos(e);
|
|
m_windowStart = frameGeometry().topLeft();
|
|
}
|
|
}
|
|
}
|
|
|
|
void VideoPlayerWindow::mouseMoveEvent(QMouseEvent* e) {
|
|
if (m_fullscreen) {
|
|
showControls();
|
|
return;
|
|
}
|
|
if (m_dragging && (e->buttons() & Qt::LeftButton)) {
|
|
move(m_windowStart + (mouseGlobalPos(e) - m_dragStart));
|
|
}
|
|
}
|
|
|
|
void VideoPlayerWindow::mouseReleaseEvent(QMouseEvent* e) {
|
|
if (e->button() == Qt::LeftButton) m_dragging = false;
|
|
}
|
|
|
|
void VideoPlayerWindow::mouseDoubleClickEvent(QMouseEvent* e) {
|
|
if (e->button() == Qt::LeftButton) toggleFullscreen();
|
|
}
|
|
|
|
void VideoPlayerWindow::closeEvent(QCloseEvent* e) {
|
|
m_engine->close();
|
|
emit closed();
|
|
QWidget::closeEvent(e);
|
|
}
|