audio_output.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  94. * 设置同步器
  95. * @param synchronizer 外部同步器实例
  96. */
  97. void setSynchronizer(std::shared_ptr<av::utils::SynchronizerV2> synchronizer);
  98. private slots:
  99. void onStateChanged(QAudio::State state);
  100. private:
  101. /**
  102. * 初始化重采样器
  103. */
  104. bool initResampler();
  105. /**
  106. * 清理重采样器
  107. */
  108. void cleanupResampler();
  109. /**
  110. * 转换音频帧格式
  111. */
  112. QByteArray convertFrame(const AVFramePtr& frame);
  113. /**
  114. * 应用音量控制
  115. */
  116. void applyVolume(QByteArray& data);
  117. private:
  118. QAudioOutput* m_audioOutput;
  119. QIODevice* m_audioDevice;
  120. QAudioFormat m_audioFormat;
  121. // 音频参数
  122. int m_sampleRate;
  123. int m_channels;
  124. AVSampleFormat m_inputFormat;
  125. // 重采样器
  126. SwrContext* m_swrContext;
  127. bool m_needResampling;
  128. // 音量控制
  129. std::atomic<double> m_volume;
  130. // 播放速度控制
  131. std::atomic<double> m_playbackSpeed;
  132. // 状态
  133. std::atomic<bool> m_initialized;
  134. std::atomic<bool> m_playing;
  135. // 时间同步 - 使用Synchronizer进行统一管理
  136. std::shared_ptr<av::utils::SynchronizerV2> m_synchronizer;
  137. double m_lastAudioPts; // 上一次的音频PTS
  138. mutable QMutex m_mutex;
  139. };
  140. } // namespace player
  141. } // namespace av
  142. #endif // AV_PLAYER_AUDIO_OUTPUT_H