#include "playerdemowindow.h" #include #include #include #include #include #include #include "AudioPlayer.h" #include "VideoPlayer.h" // 静态回调函数 static int audioWriteFunc(const char* data, int size, void* ctx) { QIODevice* device = static_cast(ctx); return device->write(data, size); } static void videoRenderFunc(AVFrame* frame, void* ctx) { OpenGLVideoWidget* widget = static_cast(ctx); widget->Render(frame); } PlayerDemoWindow::PlayerDemoWindow(QWidget* parent) : QWidget(parent) , m_audioPlayer(new AudioPlayer()) , m_videoPlayer(new VideoPlayer()) , speedFlag(false) { QVBoxLayout* layout = new QVBoxLayout(this); m_videoWidget = new OpenGLVideoWidget(this); layout->addWidget(m_videoWidget); // 进度条和时间 QHBoxLayout* progressLayout = new QHBoxLayout(); m_progressSlider = new QSlider(Qt::Horizontal, this); m_progressSlider->setRange(0, 1000); m_timeLabel = new QLabel("00:00:00 / 00:00:00", this); progressLayout->addWidget(m_progressSlider, 1); progressLayout->addWidget(m_timeLabel); layout->addLayout(progressLayout); // 倍速选择和播放按钮 QHBoxLayout* controlLayout = new QHBoxLayout(); m_playBtn = new QPushButton("播放", this); m_speedCombo = new QComboBox(this); m_speedCombo->addItem("0.5x", 0.5); m_speedCombo->addItem("1.0x", 1.0); m_speedCombo->addItem("1.5x", 1.5); m_speedCombo->addItem("2.0x", 2.0); controlLayout->addWidget(m_playBtn); controlLayout->addWidget(new QLabel("倍速:", this)); controlLayout->addWidget(m_speedCombo); // 添加保持比例复选框 m_keepAspectCheck = new QCheckBox("保持比例", this); m_keepAspectCheck->setChecked(true); controlLayout->addWidget(m_keepAspectCheck); layout->addLayout(controlLayout); m_speedCombo->setCurrentText("1.0x"); connect(m_progressSlider, &QSlider::sliderPressed, [this]() { m_sliderPressed = true; }); connect(m_progressSlider, &QSlider::sliderReleased, this, &PlayerDemoWindow::onProgressSliderReleased); connect(m_progressSlider, &QSlider::sliderMoved, this, &PlayerDemoWindow::onProgressSliderMoved); connect(m_speedCombo, QOverload::of(&QComboBox::currentIndexChanged), this, &PlayerDemoWindow::onSpeedChanged); connect(m_playBtn, &QPushButton::clicked, this, &PlayerDemoWindow::onPlayClicked); connect(m_keepAspectCheck, &QCheckBox::toggled, this, [this](bool checked) { m_videoWidget->setKeepAspectRatio(checked); }); } PlayerDemoWindow::~PlayerDemoWindow() { if (m_puller) { m_puller->stop(); delete m_puller; } if (m_swrCtx) { swr_free(&m_swrCtx); } if (m_swrBuffer) { av_free(m_swrBuffer); } if (m_audioPlayer) { delete m_audioPlayer; } if (m_videoPlayer) { delete m_videoPlayer; } } void PlayerDemoWindow::startPlay(const QString& url) { if (m_puller) { m_puller->stop(); delete m_puller; m_puller = nullptr; } m_puller = new FFmpegVideoPuller(); if (!m_puller->open(url, 300, 300)) { QMessageBox::critical(this, "错误", "无法打开流"); return; } m_puller->setSpeed(m_speedCombo->currentData().toFloat()); // 设置回调 m_puller->setVideoRenderCallback([this](AVFrame* frame) { static bool firstFrame = true; if (firstFrame) { m_videoPlayer->init(frame); m_videoWidget->Open(frame->width, frame->height); firstFrame = false; } m_videoPlayer->render(frame, (void*)videoRenderFunc, m_videoWidget); updateProgress(); }); m_puller->setAudioPlayCallback([this](AVFrame* frame) { float speed = m_puller->getSpeed(); if (m_audioPlayer->needReinit(frame, speed)) { m_audioPlayer->init(frame, speed); } m_audioPlayer->play(frame, speed); }); m_puller->start(); // 进度条初始化 m_firstPts = m_puller->getFirstPts(); m_lastPts = m_puller->getLastPts(); m_duration = m_lastPts - m_firstPts; m_progressSlider->setValue(0); updateProgress(); } void PlayerDemoWindow::onPlayClicked() { if (!m_puller) return; float curSpeed = m_puller->getSpeed(); if (curSpeed > 0.01f) { m_puller->setSpeed(0.0f); m_playBtn->setText("播放"); } else { float speed = m_speedCombo->currentData().toFloat(); m_puller->setSpeed(speed); m_playBtn->setText("暂停"); } } void PlayerDemoWindow::onProgressSliderMoved(int value) { m_sliderPressed = true; if (m_duration <= 0) { m_timeLabel->setText("00:00:00 / 00:00:00"); return; } double seekPts = m_firstPts + (m_duration * value / 1000.0); QString cur = formatTime(seekPts - m_firstPts); QString total = formatTime(m_duration); m_timeLabel->setText(cur + " / " + total); } void PlayerDemoWindow::onProgressSliderReleased() { if (!m_puller || m_duration <= 0) return; int value = m_progressSlider->value(); double seekPts = m_firstPts + (m_duration * value / 1000.0); if (seekPts < m_firstPts) seekPts = m_firstPts; if (seekPts > m_lastPts) seekPts = m_lastPts; m_puller->seekToPts(seekPts); m_sliderPressed = false; } void PlayerDemoWindow::onSpeedChanged(int index) { if (!m_puller) return; float speed = m_speedCombo->itemData(index).toFloat(); m_puller->setSpeed(speed); speedFlag.store(true, std::memory_order_release); } void PlayerDemoWindow::updateProgress() { if (!m_puller || m_sliderPressed) return; double duration = m_puller->getTotalDuration(); if (duration > 0) { m_duration = duration; m_firstPts = 0; m_lastPts = duration; } else { m_firstPts = m_puller->getFirstPts(); m_lastPts = m_puller->getLastPts(); m_duration = m_lastPts - m_firstPts; } // 检查有效性 if (m_firstPts < 0 || m_lastPts < 0 || m_duration <= 0) { m_progressSlider->setValue(0); m_timeLabel->setText("00:00:00 / 00:00:00"); return; } double curPts = m_puller->getCurrentPts(); if (curPts < m_firstPts) curPts = m_firstPts; if (curPts > m_lastPts) curPts = m_lastPts; int value = int((curPts - m_firstPts) / m_duration * 1000); m_progressSlider->setValue(value); QString cur = formatTime(curPts - m_firstPts); QString total = formatTime(m_duration); m_timeLabel->setText(cur + " / " + total); } QString PlayerDemoWindow::formatTime(double seconds) const { if (seconds < 0) seconds = 0; int sec = int(seconds + 0.5); int h = sec / 3600; int m = (sec % 3600) / 60; int s = sec % 60; return QString("%1:%2:%3") .arg(h, 2, 10, QChar('0')) .arg(m, 2, 10, QChar('0')) .arg(s, 2, 10, QChar('0')); }