| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #ifndef AV_PLAYER_AUDIO_OUTPUT_H
- #define AV_PLAYER_AUDIO_OUTPUT_H
- #include <QAudioOutput>
- #include <QAudioFormat>
- #include <QIODevice>
- #include <QMutex>
- #include <QWaitCondition>
- #include <QThread>
- #include <memory>
- #include <atomic>
- #include <queue>
- #include <chrono>
- #include <thread>
- extern "C" {
- #include <libavutil/frame.h>
- #include <libavutil/samplefmt.h>
- #include <libswresample/swresample.h>
- }
- #include "../base/media_common.h"
- #include "../utils/utils_synchronizer.h"
- namespace av {
- namespace player {
- /**
- * 音频输出设备类
- * 负责将解码后的音频帧输出到扬声器
- */
- class AudioOutput : public QObject
- {
- Q_OBJECT
- public:
- explicit AudioOutput(QObject* parent = nullptr);
- ~AudioOutput();
- /**
- * 初始化音频输出设备
- * @param sampleRate 采样率
- * @param channels 声道数
- * @param sampleFormat 采样格式
- * @return 是否成功
- */
- bool initialize(int sampleRate, int channels, AVSampleFormat sampleFormat);
- /**
- * 开始播放
- */
- void start();
- /**
- * 停止播放
- */
- void stop();
- /**
- * 暂停播放
- */
- void pause();
- /**
- * 恢复播放
- */
- void resume();
- /**
- * 写入音频帧
- * @param frame 音频帧
- * @return 是否成功
- */
- bool writeFrame(const AVFramePtr& frame);
- /**
- * 设置音量
- * @param volume 音量 (0.0 - 1.0)
- */
- void setVolume(double volume);
- /**
- * 获取音量
- */
- double getVolume() const;
- /**
- * 设置播放速度
- * @param speed 播放速度 (0.5 - 4.0)
- */
- void setPlaybackSpeed(double speed);
- /**
- * 获取播放速度
- */
- double getPlaybackSpeed() const;
- /**
- * 清空缓冲区
- */
- void flush();
- /**
- * 获取缓冲区大小(毫秒)
- */
- int getBufferSize() const;
- /**
- * 是否正在播放
- */
- bool isPlaying() const;
- private slots:
- void onStateChanged(QAudio::State state);
- private:
- /**
- * 初始化重采样器
- */
- bool initResampler();
- /**
- * 清理重采样器
- */
- void cleanupResampler();
- /**
- * 转换音频帧格式
- */
- QByteArray convertFrame(const AVFramePtr& frame);
- /**
- * 应用音量控制
- */
- void applyVolume(QByteArray& data);
- private:
- QAudioOutput* m_audioOutput;
- QIODevice* m_audioDevice;
- QAudioFormat m_audioFormat;
-
- // 音频参数
- int m_sampleRate;
- int m_channels;
- AVSampleFormat m_inputFormat;
-
- // 重采样器
- SwrContext* m_swrContext;
- bool m_needResampling;
-
- // 音量控制
- std::atomic<double> m_volume;
-
- // 播放速度控制
- std::atomic<double> m_playbackSpeed;
-
- // 状态
- std::atomic<bool> m_initialized;
- std::atomic<bool> m_playing;
-
- // 时间同步 - 使用Synchronizer进行统一管理
- std::shared_ptr<av::utils::Synchronizer> m_synchronizer;
- double m_lastAudioPts; // 上一次的音频PTS
-
- mutable QMutex m_mutex;
- };
- } // namespace player
- } // namespace av
- #endif // AV_PLAYER_AUDIO_OUTPUT_H
|