AudioPlayer.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. extern "C" {
  3. #include <libavutil/channel_layout.h>
  4. #include <libavutil/frame.h>
  5. #include <libavutil/samplefmt.h>
  6. #include <libswresample/swresample.h>
  7. }
  8. struct sonicStreamStruct;
  9. #include <QAudioOutput>
  10. #include <QIODevice>
  11. class AudioPlayer {
  12. public:
  13. AudioPlayer();
  14. ~AudioPlayer();
  15. void init(const AVFrame* frame, float speed);
  16. void setSpeed(float speed);
  17. void play(AVFrame* frame, float speed = 1.0f);
  18. void reset();
  19. void setOutputDevice(QIODevice* device);
  20. void setAudioOutput(QAudioOutput* output);
  21. QIODevice* getOutputDevice() const;
  22. QAudioOutput* getAudioOutput() const;
  23. bool needReinit(const AVFrame* frame, float speed) const;
  24. private:
  25. int m_sampleRate = 0;
  26. int m_channels = 0;
  27. AVSampleFormat m_format = AV_SAMPLE_FMT_NONE;
  28. sonicStreamStruct* m_sonicCtx = nullptr;
  29. int m_maxBufSize = -1;
  30. uint8_t* m_abuf = nullptr;
  31. uint8_t* m_abufOut = nullptr;
  32. unsigned int m_abufOutSize = 0;
  33. void freeBuffers();
  34. // swresample相关
  35. SwrContext* m_swrCtx = nullptr;
  36. uint8_t* m_swrBuffer = nullptr;
  37. int m_swrBufferSize = 0;
  38. // Qt音频相关
  39. QAudioOutput* m_audioOutput = nullptr;
  40. QIODevice* m_audioDevice = nullptr;
  41. float m_lastSpeed = 1.0f;
  42. void initAudioOutput(const AVFrame* frame, float speed);
  43. };