audio_play_thread.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <QAudio>
  3. #include <QAudioDeviceInfo>
  4. #include <QAudioOutput>
  5. #include <QDebug>
  6. #include <QFile>
  7. #include <QIODevice>
  8. #include <QQueue>
  9. #include <QThread>
  10. #include <QWaitCondition>
  11. #include <memory>
  12. #include "packets_sync.h"
  13. #define BUFFER_LEN 8192 // 1024
  14. typedef struct AudioData
  15. {
  16. uint16_t len = 0;
  17. char buffer[BUFFER_LEN] = {0};
  18. } AudioData;
  19. typedef struct AudioFrameFmt
  20. {
  21. uint sample_rate;
  22. uint sample_fmt; // AV_SAMPLE_FMT_S16
  23. uint channel;
  24. int byte_order; // QAudioFormat::LittleEndian;
  25. int sample_type; // QAudioFormat::SignedInt
  26. } AudioFrameFmt;
  27. class AudioPlayThread : public QThread
  28. {
  29. Q_OBJECT
  30. public:
  31. explicit AudioPlayThread(QObject* parent = nullptr, VideoState* pState = nullptr);
  32. virtual ~AudioPlayThread();
  33. public:
  34. void print_device() const;
  35. bool init_device(int sample_rate = 8000, int channel = 1, AVSampleFormat sample_fmt = AV_SAMPLE_FMT_S16, float default_vol = 0.8);
  36. void stop_device();
  37. void play_file(const QString& file);
  38. void play_buf(const uint8_t* buf, int datasize);
  39. bool init_resample_param(AVCodecContext* pAudio, AVSampleFormat sample_fmt, VideoState* is);
  40. void final_resample_param();
  41. float get_device_volume() const;
  42. void set_device_volume(float volume);
  43. void send_visual_open(bool bSend = true) { m_bSendToVisual = bSend; };
  44. signals:
  45. void update_play_time();
  46. void data_visual_ready(const AudioData& data);
  47. public slots:
  48. void stop_thread();
  49. protected:
  50. void run() override;
  51. private:
  52. int audio_decode_frame(VideoState* is);
  53. private:
  54. typedef struct Audio_Resample
  55. {
  56. // AVFrame* pFrame;
  57. // uint8_t* buffer;
  58. struct SwrContext* swrCtx{nullptr};
  59. // uint64_t channel_layout; // out
  60. // AVChannelLayout channel_layout;
  61. // AVSampleFormat sample_fmt;
  62. // int sample_rate;
  63. } Audio_Resample;
  64. private:
  65. std::unique_ptr<QAudioOutput> m_pOutput;
  66. QIODevice* m_audioDevice{nullptr};
  67. VideoState* m_pState{nullptr};
  68. Audio_Resample m_audioResample;
  69. bool m_bExitThread{false};
  70. bool m_bSendToVisual{false};
  71. };