#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()) { 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) { stopPlay(); m_puller = new FFmpegVideoPuller(); m_audioPlayer = new AudioPlayer(); m_videoPlayer = new VideoPlayer(); if (!m_puller->open(url, 300, 300)) { QMessageBox::critical(this, "错误", "无法打开流"); return; } m_puller->setSpeed(m_speedCombo->currentData().toFloat()); // 启动主同步播放线程 m_syncPlayRunning = true; m_syncPlayThread = QThread::create([this]() { this->syncPlayLoop(); }); m_syncPlayThread->start(); m_firstPts = m_puller->getFirstPts(); m_lastPts = m_puller->getLastPts(); m_duration = m_lastPts - m_firstPts; m_progressSlider->setValue(0); updateProgress(); m_playing = true; } void PlayerDemoWindow::stopPlay() { m_syncPlayRunning = false; if (m_syncPlayThread) { m_syncPlayThread->quit(); m_syncPlayThread->wait(); delete m_syncPlayThread; m_syncPlayThread = nullptr; } if (m_puller) { m_puller->stop(); delete m_puller; m_puller = nullptr; } if (m_audioPlayer) { delete m_audioPlayer; m_audioPlayer = nullptr; } if (m_videoPlayer) { delete m_videoPlayer; m_videoPlayer = nullptr; } m_playing = false; } void PlayerDemoWindow::setSpeed(float speed) { if (m_puller) { m_puller->setSpeed(speed); } } 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); } 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')); } void PlayerDemoWindow::syncPlayLoop() { using namespace std::chrono; double basePts = 0.0; bool baseSet = false; auto startTime = high_resolution_clock::now(); while (m_syncPlayRunning) { auto now = high_resolution_clock::now(); double elapsed = duration_cast(now - startTime).count(); if (!baseSet) { AVFrame* af = m_puller->getCurrentAudioFrame(); if (af) { basePts = af->best_effort_timestamp * av_q2d(m_puller->m_fmtCtx->streams[m_puller->m_audioStreamIdx]->time_base); baseSet = true; } else { std::this_thread::sleep_for(milliseconds(1)); continue; } } double curPts = basePts + elapsed / 1000.0 * m_puller->getSpeed(); // 音频 AVFrame* audioFrame = m_puller->popNearestAudioFrame(curPts); if (audioFrame && m_audioPlayer) { m_audioPlayer->play(audioFrame, m_puller->getSpeed()); } // 视频 AVFrame* videoFrame = m_puller->popNearestVideoFrame(curPts); if (videoFrame && m_videoPlayer) { m_videoPlayer->render(videoFrame); } std::this_thread::sleep_for(milliseconds(1)); } }