| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #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())
- , 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<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)
- {
- 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'));
- }
|