playerdemowindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "playerdemowindow.h"
  2. #include <QAudioDeviceInfo>
  3. #include <QAudioFormat>
  4. #include <QDebug>
  5. #include <QHBoxLayout>
  6. #include <QMessageBox>
  7. #include <QVBoxLayout>
  8. PlayerDemoWindow::PlayerDemoWindow(QWidget* parent)
  9. : QWidget(parent)
  10. {
  11. QVBoxLayout* layout = new QVBoxLayout(this);
  12. m_videoWidget = new OpenGLVideoWidget(this);
  13. layout->addWidget(m_videoWidget);
  14. // 进度条和时间
  15. QHBoxLayout* progressLayout = new QHBoxLayout();
  16. m_progressSlider = new QSlider(Qt::Horizontal, this);
  17. m_progressSlider->setRange(0, 1000);
  18. m_timeLabel = new QLabel("00:00:00 / 00:00:00", this);
  19. progressLayout->addWidget(m_progressSlider, 1);
  20. progressLayout->addWidget(m_timeLabel);
  21. layout->addLayout(progressLayout);
  22. // 倍速选择和播放按钮
  23. QHBoxLayout* controlLayout = new QHBoxLayout();
  24. m_playBtn = new QPushButton("播放", this);
  25. m_speedCombo = new QComboBox(this);
  26. m_speedCombo->addItem("0.5x", 0.5);
  27. m_speedCombo->addItem("1.0x", 1.0);
  28. m_speedCombo->addItem("1.5x", 1.5);
  29. m_speedCombo->addItem("2.0x", 2.0);
  30. controlLayout->addWidget(m_playBtn);
  31. controlLayout->addWidget(new QLabel("倍速:", this));
  32. controlLayout->addWidget(m_speedCombo);
  33. layout->addLayout(controlLayout);
  34. m_speedCombo->setCurrentText("1.0x");
  35. connect(m_progressSlider, &QSlider::sliderPressed, [this]() { m_sliderPressed = true; });
  36. connect(m_progressSlider,
  37. &QSlider::sliderReleased,
  38. this,
  39. &PlayerDemoWindow::onProgressSliderReleased);
  40. connect(m_progressSlider, &QSlider::sliderMoved, this, &PlayerDemoWindow::onProgressSliderMoved);
  41. connect(m_speedCombo,
  42. QOverload<int>::of(&QComboBox::currentIndexChanged),
  43. this,
  44. &PlayerDemoWindow::onSpeedChanged);
  45. connect(m_playBtn, &QPushButton::clicked, this, &PlayerDemoWindow::onPlayClicked);
  46. }
  47. PlayerDemoWindow::~PlayerDemoWindow()
  48. {
  49. if (m_puller) {
  50. m_puller->stop();
  51. delete m_puller;
  52. }
  53. if (m_audioOutput) {
  54. m_audioOutput->stop();
  55. delete m_audioOutput;
  56. }
  57. if (m_swrCtx) {
  58. swr_free(&m_swrCtx);
  59. }
  60. if (m_swrBuffer) {
  61. av_free(m_swrBuffer);
  62. }
  63. }
  64. void PlayerDemoWindow::startPlay(const QString& url)
  65. {
  66. if (m_puller) {
  67. m_puller->stop();
  68. delete m_puller;
  69. m_puller = nullptr;
  70. }
  71. m_puller = new FFmpegVideoPuller();
  72. if (!m_puller->open(url, 300, 300)) {
  73. QMessageBox::critical(this, "错误", "无法打开流");
  74. return;
  75. }
  76. m_puller->setSpeed(m_speedCombo->currentData().toFloat());
  77. // 设置回调
  78. m_puller->setVideoRenderCallback([this](AVFrame* frame) {
  79. m_videoWidget->Render(frame);
  80. updateProgress(); // 每次渲染视频帧时刷新进度条
  81. });
  82. m_puller->setAudioPlayCallback([this](AVFrame* frame) {
  83. if (!m_audioOutput) {
  84. initAudio(frame);
  85. }
  86. playAudioFrame(frame);
  87. });
  88. m_puller->start();
  89. // 进度条初始化
  90. m_firstPts = m_puller->getFirstPts();
  91. m_lastPts = m_puller->getLastPts();
  92. m_duration = m_lastPts - m_firstPts;
  93. m_progressSlider->setValue(0);
  94. updateProgress();
  95. }
  96. void PlayerDemoWindow::onPlayClicked()
  97. {
  98. if (!m_puller)
  99. return;
  100. float curSpeed = m_puller->getSpeed();
  101. if (curSpeed > 0.01f) {
  102. m_puller->setSpeed(0.0f);
  103. m_playBtn->setText("播放");
  104. } else {
  105. float speed = m_speedCombo->currentData().toFloat();
  106. m_puller->setSpeed(speed);
  107. m_playBtn->setText("暂停");
  108. }
  109. }
  110. void PlayerDemoWindow::initAudio(const AVFrame* frame)
  111. {
  112. if (m_audioOutput) {
  113. m_audioOutput->stop();
  114. delete m_audioOutput;
  115. m_audioOutput = nullptr;
  116. }
  117. if (m_swrCtx) {
  118. swr_free(&m_swrCtx);
  119. m_swrCtx = nullptr;
  120. }
  121. if (m_swrBuffer) {
  122. av_free(m_swrBuffer);
  123. m_swrBuffer = nullptr;
  124. m_swrBufferSize = 0;
  125. }
  126. QAudioFormat fmt;
  127. m_audioSampleRate = frame->sample_rate;
  128. m_audioChannels = frame->ch_layout.nb_channels; // FFmpeg 7.x
  129. m_audioFormat = (AVSampleFormat) frame->format;
  130. fmt.setSampleRate(m_audioSampleRate);
  131. fmt.setChannelCount(m_audioChannels);
  132. fmt.setSampleSize(16); // 目标格式 S16
  133. fmt.setCodec("audio/pcm");
  134. fmt.setByteOrder(QAudioFormat::LittleEndian);
  135. fmt.setSampleType(QAudioFormat::SignedInt);
  136. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  137. if (!info.isFormatSupported(fmt)) {
  138. qWarning() << "音频格式不支持,尝试最近格式";
  139. fmt = info.nearestFormat(fmt);
  140. }
  141. m_audioOutput = new QAudioOutput(fmt, this);
  142. m_audioDevice = m_audioOutput->start();
  143. qDebug() << "initAudio, m_audioOutput=" << m_audioOutput << " m_audioDevice=" << m_audioDevice;
  144. if (m_audioOutput)
  145. qDebug() << "QAudioOutput error:" << m_audioOutput->error();
  146. AVChannelLayout out_ch_layout;
  147. av_channel_layout_default(&out_ch_layout, m_audioChannels);
  148. m_swrCtx = swr_alloc();
  149. if (!m_swrCtx) {
  150. qWarning() << "swr_alloc 失败";
  151. return;
  152. }
  153. if (swr_alloc_set_opts2(&m_swrCtx,
  154. &out_ch_layout,
  155. AV_SAMPLE_FMT_S16,
  156. m_audioSampleRate,
  157. &frame->ch_layout,
  158. (AVSampleFormat) frame->format,
  159. frame->sample_rate,
  160. 0,
  161. nullptr)
  162. < 0) {
  163. qWarning() << "swr_alloc_set_opts2 失败";
  164. swr_free(&m_swrCtx);
  165. return;
  166. }
  167. if (swr_init(m_swrCtx) < 0) {
  168. qWarning() << "swr_init 失败";
  169. swr_free(&m_swrCtx);
  170. m_swrCtx = nullptr;
  171. }
  172. av_channel_layout_uninit(&out_ch_layout);
  173. }
  174. void PlayerDemoWindow::playAudioFrame(AVFrame* frame)
  175. {
  176. qDebug() << "--------------" << m_audioOutput << m_audioDevice;
  177. if (!m_audioOutput || !m_audioDevice)
  178. return;
  179. int out_channels = m_audioChannels;
  180. int out_samples = frame->nb_samples;
  181. int out_bytes_per_sample = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
  182. // 目标缓冲区大小
  183. int needed_bufsize = av_samples_get_buffer_size(nullptr,
  184. out_channels,
  185. out_samples,
  186. AV_SAMPLE_FMT_S16,
  187. 1);
  188. if (!m_swrBuffer || m_swrBufferSize < needed_bufsize) {
  189. if (m_swrBuffer)
  190. av_free(m_swrBuffer);
  191. m_swrBuffer = (uint8_t*) av_malloc(needed_bufsize);
  192. m_swrBufferSize = needed_bufsize;
  193. }
  194. uint8_t* out[] = {m_swrBuffer};
  195. int converted = 0;
  196. // qDebug() << "playAudioFrame, m_audioOutput=" << m_audioOutput
  197. // << " m_audioDevice=" << m_audioDevice;
  198. if (m_swrCtx) {
  199. converted = swr_convert(m_swrCtx,
  200. out,
  201. out_samples,
  202. (const uint8_t**) frame->extended_data,
  203. frame->nb_samples);
  204. qDebug() << "swr_convert 返回" << converted;
  205. if (converted > 0) {
  206. int out_size = converted * out_channels * out_bytes_per_sample;
  207. qint64 written = m_audioDevice->write((const char*) m_swrBuffer, out_size);
  208. (void) written;
  209. // qDebug() << "写入音频数据, 大小=" << out_size << " 实际写入=" << written;
  210. }
  211. } else if (frame->format == AV_SAMPLE_FMT_S16) {
  212. // 直接写
  213. int dataSize = av_samples_get_buffer_size(nullptr,
  214. frame->ch_layout.nb_channels,
  215. frame->nb_samples,
  216. (AVSampleFormat) frame->format,
  217. 1);
  218. m_audioDevice->write((const char*) frame->data[0], dataSize);
  219. }
  220. }
  221. void PlayerDemoWindow::onProgressSliderMoved(int value)
  222. {
  223. m_sliderPressed = true;
  224. if (m_duration <= 0) {
  225. m_timeLabel->setText("00:00:00 / 00:00:00");
  226. return;
  227. }
  228. double seekPts = m_firstPts + (m_duration * value / 1000.0);
  229. QString cur = formatTime(seekPts - m_firstPts);
  230. QString total = formatTime(m_duration);
  231. m_timeLabel->setText(cur + " / " + total);
  232. }
  233. void PlayerDemoWindow::onProgressSliderReleased()
  234. {
  235. if (!m_puller || m_duration <= 0)
  236. return;
  237. int value = m_progressSlider->value();
  238. double seekPts = m_firstPts + (m_duration * value / 1000.0);
  239. if (seekPts < m_firstPts)
  240. seekPts = m_firstPts;
  241. if (seekPts > m_lastPts)
  242. seekPts = m_lastPts;
  243. m_puller->seekToPts(seekPts);
  244. m_sliderPressed = false;
  245. }
  246. void PlayerDemoWindow::onSpeedChanged(int index)
  247. {
  248. if (!m_puller)
  249. return;
  250. float speed = m_speedCombo->itemData(index).toFloat();
  251. m_puller->setSpeed(speed);
  252. }
  253. void PlayerDemoWindow::updateProgress()
  254. {
  255. if (!m_puller || m_sliderPressed)
  256. return;
  257. double duration = m_puller->getTotalDuration();
  258. if (duration > 0) {
  259. m_duration = duration;
  260. m_firstPts = 0;
  261. m_lastPts = duration;
  262. } else {
  263. m_firstPts = m_puller->getFirstPts();
  264. m_lastPts = m_puller->getLastPts();
  265. m_duration = m_lastPts - m_firstPts;
  266. }
  267. // 检查有效性
  268. if (m_firstPts < 0 || m_lastPts < 0 || m_duration <= 0) {
  269. m_progressSlider->setValue(0);
  270. m_timeLabel->setText("00:00:00 / 00:00:00");
  271. return;
  272. }
  273. double curPts = m_puller->getCurrentPts();
  274. if (curPts < m_firstPts)
  275. curPts = m_firstPts;
  276. if (curPts > m_lastPts)
  277. curPts = m_lastPts;
  278. int value = int((curPts - m_firstPts) / m_duration * 1000);
  279. m_progressSlider->setValue(value);
  280. QString cur = formatTime(curPts - m_firstPts);
  281. QString total = formatTime(m_duration);
  282. m_timeLabel->setText(cur + " / " + total);
  283. }
  284. QString PlayerDemoWindow::formatTime(double seconds) const
  285. {
  286. if (seconds < 0)
  287. seconds = 0;
  288. int sec = int(seconds + 0.5);
  289. int h = sec / 3600;
  290. int m = (sec % 3600) / 60;
  291. int s = sec % 60;
  292. return QString("%1:%2:%3")
  293. .arg(h, 2, 10, QChar('0'))
  294. .arg(m, 2, 10, QChar('0'))
  295. .arg(s, 2, 10, QChar('0'));
  296. }