| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #include "playerdemowindow.h"
- #include <QAudioDeviceInfo>
- #include <QAudioFormat>
- #include <QDebug>
- #include <QHBoxLayout>
- #include <QMessageBox>
- #include <QVBoxLayout>
- #include "AudioPlayer.h"
- #include "VideoPlayer.h"
- // 静态回调函数
- static int audioWriteFunc(const char* data, int size, void* ctx) {
- QIODevice* device = static_cast<QIODevice*>(ctx);
- return device->write(data, size);
- }
- static void videoRenderFunc(AVFrame* frame, void* ctx) {
- OpenGLVideoWidget* widget = static_cast<OpenGLVideoWidget*>(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<int>::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<milliseconds>(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));
- }
- }
|