| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #pragma once
- extern "C" {
- #include <libavutil/channel_layout.h>
- #include <libavutil/frame.h>
- #include <libavutil/samplefmt.h>
- #include <libswresample/swresample.h>
- }
- struct sonicStreamStruct;
- #include <QAudioOutput>
- #include <QIODevice>
- class AudioPlayer {
- public:
- AudioPlayer();
- ~AudioPlayer();
- void init(const AVFrame* frame, float speed);
- void setSpeed(float speed);
- void play(AVFrame* frame, float speed = 1.0f);
- void reset();
- void setOutputDevice(QIODevice* device);
- void setAudioOutput(QAudioOutput* output);
- QIODevice* getOutputDevice() const;
- QAudioOutput* getAudioOutput() const;
- bool needReinit(const AVFrame* frame, float speed) const;
- private:
- int m_sampleRate = 0;
- int m_channels = 0;
- AVSampleFormat m_format = AV_SAMPLE_FMT_NONE;
- sonicStreamStruct* m_sonicCtx = nullptr;
- int m_maxBufSize = -1;
- uint8_t* m_abuf = nullptr;
- uint8_t* m_abufOut = nullptr;
- unsigned int m_abufOutSize = 0;
- void freeBuffers();
- // swresample相关
- SwrContext* m_swrCtx = nullptr;
- uint8_t* m_swrBuffer = nullptr;
- int m_swrBufferSize = 0;
- // Qt音频相关
- QAudioOutput* m_audioOutput = nullptr;
- QIODevice* m_audioDevice = nullptr;
- float m_lastSpeed = 1.0f;
- void initAudioOutput(const AVFrame* frame, float speed);
- };
|