audio_output.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef AV_PLAYER_AUDIO_OUTPUT_H
  2. #define AV_PLAYER_AUDIO_OUTPUT_H
  3. #include <QAudioOutput>
  4. #include <QAudioFormat>
  5. #include <QIODevice>
  6. #include <QMutex>
  7. #include <QWaitCondition>
  8. #include <QThread>
  9. #include <memory>
  10. #include <atomic>
  11. #include <queue>
  12. #include <chrono>
  13. #include <thread>
  14. extern "C" {
  15. #include <libavutil/frame.h>
  16. #include <libavutil/samplefmt.h>
  17. #include <libswresample/swresample.h>
  18. }
  19. #include "../base/media_common.h"
  20. #include "../utils/utils_synchronizer_v2.h"
  21. namespace av {
  22. namespace player {
  23. /**
  24. * 音频输出设备类
  25. * 负责将解码后的音频帧输出到扬声器
  26. */
  27. class AudioOutput : public QObject
  28. {
  29. Q_OBJECT
  30. public:
  31. explicit AudioOutput(QObject* parent = nullptr);
  32. ~AudioOutput();
  33. /**
  34. * 初始化音频输出设备
  35. * @param sampleRate 采样率
  36. * @param channels 声道数
  37. * @param sampleFormat 采样格式
  38. * @return 是否成功
  39. */
  40. bool initialize(int sampleRate, int channels, AVSampleFormat sampleFormat);
  41. /**
  42. * 开始播放
  43. */
  44. void start();
  45. /**
  46. * 停止播放
  47. */
  48. void stop();
  49. /**
  50. * 暂停播放
  51. */
  52. void pause();
  53. /**
  54. * 恢复播放
  55. */
  56. void resume();
  57. /**
  58. * 写入音频帧
  59. * @param frame 音频帧
  60. * @return 是否成功
  61. */
  62. bool writeFrame(const AVFramePtr& frame);
  63. /**
  64. * 设置音量
  65. * @param volume 音量 (0.0 - 1.0)
  66. */
  67. void setVolume(double volume);
  68. /**
  69. * 获取音量
  70. */
  71. double getVolume() const;
  72. /**
  73. * 设置播放速度
  74. * @param speed 播放速度 (0.5 - 4.0)
  75. */
  76. void setPlaybackSpeed(double speed);
  77. /**
  78. * 获取播放速度
  79. */
  80. double getPlaybackSpeed() const;
  81. /**
  82. * 清空缓冲区
  83. */
  84. void flush();
  85. /**
  86. * 获取缓冲区大小(毫秒)
  87. */
  88. int getBufferSize() const;
  89. /**
  90. * 是否正在播放
  91. */
  92. bool isPlaying() const;
  93. private slots:
  94. void onStateChanged(QAudio::State state);
  95. private:
  96. /**
  97. * 初始化重采样器
  98. */
  99. bool initResampler();
  100. /**
  101. * 清理重采样器
  102. */
  103. void cleanupResampler();
  104. /**
  105. * 转换音频帧格式
  106. */
  107. QByteArray convertFrame(const AVFramePtr& frame);
  108. /**
  109. * 应用音量控制
  110. */
  111. void applyVolume(QByteArray& data);
  112. private:
  113. QAudioOutput* m_audioOutput;
  114. QIODevice* m_audioDevice;
  115. QAudioFormat m_audioFormat;
  116. // 音频参数
  117. int m_sampleRate;
  118. int m_channels;
  119. AVSampleFormat m_inputFormat;
  120. // 重采样器
  121. SwrContext* m_swrContext;
  122. bool m_needResampling;
  123. // 音量控制
  124. std::atomic<double> m_volume;
  125. // 播放速度控制
  126. std::atomic<double> m_playbackSpeed;
  127. // 状态
  128. std::atomic<bool> m_initialized;
  129. std::atomic<bool> m_playing;
  130. // 时间同步 - 使用Synchronizer进行统一管理
  131. std::shared_ptr<av::utils::SynchronizerV2> m_synchronizer;
  132. double m_lastAudioPts; // 上一次的音频PTS
  133. mutable QMutex m_mutex;
  134. };
  135. } // namespace player
  136. } // namespace av
  137. #endif // AV_PLAYER_AUDIO_OUTPUT_H