capture_audio_capturer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #ifndef AV_CAPTURE_AUDIO_CAPTURER_H
  2. #define AV_CAPTURE_AUDIO_CAPTURER_H
  3. #include "capture_abstract_capturer.h"
  4. #include <thread>
  5. #include <condition_variable>
  6. #include <queue>
  7. #include <atomic>
  8. extern "C" {
  9. #include <libavformat/avformat.h>
  10. #include <libavdevice/avdevice.h>
  11. #include <libswresample/swresample.h>
  12. }
  13. namespace av {
  14. namespace capture {
  15. /**
  16. * 音频采集器参数
  17. */
  18. struct AudioCaptureParams : public CapturerParams {
  19. int sampleRate = 44100; // 采样率
  20. int channels = 2; // 声道数
  21. AVSampleFormat sampleFormat = AV_SAMPLE_FMT_S16; // 采样格式
  22. int bufferSize = 1024; // 缓冲区大小(采样数)
  23. // 麦克风特定参数
  24. int micIndex = 0; // 麦克风索引
  25. std::string micFormat; // 麦克风格式 (如 "dshow", "alsa")
  26. float volume = 1.0f; // 音量 (0.0-2.0)
  27. bool enableNoiseReduction = false; // 降噪
  28. bool enableEchoCancellation = false; // 回声消除
  29. // 系统音频特定参数
  30. bool captureLoopback = false; // 捕获回环音频
  31. std::string audioDevice; // 音频设备名称
  32. AudioCaptureParams(CapturerType type) : CapturerParams(type, MediaType::AUDIO) {}
  33. };
  34. /**
  35. * 音频设备信息
  36. */
  37. struct AudioDeviceInfo {
  38. std::string id; // 设备ID
  39. std::string name; // 设备名称
  40. std::string description; // 设备描述
  41. bool isDefault = false; // 是否为默认设备
  42. bool isInput = true; // 是否为输入设备
  43. std::vector<int> supportedSampleRates; // 支持的采样率
  44. std::vector<int> supportedChannels; // 支持的声道数
  45. std::vector<AVSampleFormat> supportedFormats; // 支持的采样格式
  46. };
  47. /**
  48. * 音频采集器类
  49. * 支持麦克风和系统音频采集
  50. */
  51. class AudioCapturer : public AbstractCapturer {
  52. public:
  53. AudioCapturer();
  54. virtual ~AudioCapturer();
  55. // 基础接口实现
  56. ErrorCode initialize(const CapturerParams& params) override;
  57. ErrorCode start() override;
  58. ErrorCode stop() override;
  59. ErrorCode pause() override;
  60. ErrorCode resume() override;
  61. ErrorCode reset() override;
  62. ErrorCode close() override;
  63. // 设备信息
  64. std::vector<std::string> getAvailableDevices() const override;
  65. std::string getCurrentDevice() const override;
  66. /**
  67. * 获取详细设备信息
  68. * @return 设备信息列表
  69. */
  70. std::vector<AudioDeviceInfo> getDetailedDeviceInfo() const;
  71. /**
  72. * 设置音频参数
  73. * @param sampleRate 采样率
  74. * @param channels 声道数
  75. * @param sampleFormat 采样格式
  76. * @return 错误码
  77. */
  78. ErrorCode setAudioParams(int sampleRate, int channels, AVSampleFormat sampleFormat);
  79. /**
  80. * 设置音量
  81. * @param volume 音量 (0.0-2.0)
  82. * @return 错误码
  83. */
  84. ErrorCode setVolume(float volume);
  85. /**
  86. * 获取当前音量
  87. * @return 音量值
  88. */
  89. float getVolume() const;
  90. /**
  91. * 设置降噪
  92. * @param enable 是否启用
  93. * @return 错误码
  94. */
  95. ErrorCode setNoiseReduction(bool enable);
  96. /**
  97. * 设置回声消除
  98. * @param enable 是否启用
  99. * @return 错误码
  100. */
  101. ErrorCode setEchoCancellation(bool enable);
  102. /**
  103. * 获取当前音频参数
  104. * @return 音频参数
  105. */
  106. AudioCaptureParams getCurrentParams() const;
  107. /**
  108. * 获取音频电平
  109. * @return 电平值 (0.0-1.0)
  110. */
  111. float getAudioLevel() const;
  112. /**
  113. * 音频采集器工厂
  114. */
  115. class AudioCaptureFactory {
  116. public:
  117. /**
  118. * 创建麦克风采集器
  119. * @param micIndex 麦克风索引
  120. * @return 采集器实例
  121. */
  122. static std::unique_ptr<AudioCapturer> createMicrophone(int micIndex = 0);
  123. /**
  124. * 创建系统音频采集器
  125. * @param loopback 是否为回环模式
  126. * @return 采集器实例
  127. */
  128. static std::unique_ptr<AudioCapturer> createSystemAudio(bool loopback = false);
  129. /**
  130. * 创建最佳麦克风采集器
  131. * @return 采集器实例
  132. */
  133. static std::unique_ptr<AudioCapturer> createBestMicrophone();
  134. };
  135. protected:
  136. bool validateParams(const CapturerParams& params) override;
  137. private:
  138. // 内部实现方法
  139. ErrorCode initializeMicrophone();
  140. ErrorCode initializeSystemAudio();
  141. ErrorCode openInputDevice();
  142. ErrorCode setupAudioParams();
  143. ErrorCode setupAudioResampling();
  144. ErrorCode setupAudioProcessing();
  145. // 采集线程
  146. void captureThreadFunc();
  147. ErrorCode captureFrame();
  148. // 音频处理
  149. AVFramePtr processAudioFrame(const AVFramePtr& frame);
  150. AVFramePtr resampleAudioFrame(const AVFramePtr& frame);
  151. AVFramePtr applyVolumeControl(const AVFramePtr& frame);
  152. AVFramePtr applyNoiseReduction(const AVFramePtr& frame);
  153. void calculateAudioLevel(const AVFramePtr& frame);
  154. // 资源清理
  155. void cleanupResampler();
  156. void cleanupAudioProcessing();
  157. // 设备枚举
  158. std::vector<AudioDeviceInfo> enumerateMicrophones() const;
  159. std::vector<AudioDeviceInfo> enumerateSystemAudioDevices() const;
  160. // 平台特定实现
  161. #ifdef _WIN32
  162. std::vector<AudioDeviceInfo> enumerateDirectSoundDevices() const;
  163. std::vector<AudioDeviceInfo> enumerateWASAPIDevices() const;
  164. ErrorCode setupDirectSoundMicrophone();
  165. ErrorCode setupWASAPISystemAudio();
  166. #elif defined(__linux__)
  167. std::vector<AudioDeviceInfo> enumerateALSADevices() const;
  168. std::vector<AudioDeviceInfo> enumeratePulseAudioDevices() const;
  169. ErrorCode setupALSAMicrophone();
  170. ErrorCode setupPulseAudioCapture();
  171. #elif defined(__APPLE__)
  172. std::vector<AudioDeviceInfo> enumerateCoreAudioDevices() const;
  173. ErrorCode setupCoreAudioMicrophone();
  174. ErrorCode setupCoreAudioSystemCapture();
  175. #endif
  176. // 获取平台特定的输入格式
  177. const AVInputFormat* getPlatformInputFormat() const;
  178. std::string getPlatformDeviceName() const;
  179. private:
  180. // 采集参数
  181. AudioCaptureParams audioParams_;
  182. // FFmpeg 相关
  183. AVFormatContext* formatCtx_ = nullptr;
  184. AVCodecContext* codecCtx_ = nullptr;
  185. const AVCodec* codec_ = nullptr;
  186. int audioStreamIndex_ = -1;
  187. // 音频重采样
  188. SwrContext* swrCtx_ = nullptr;
  189. AVFramePtr resampledFrame_;
  190. bool needResampling_ = false;
  191. // 音频处理
  192. float currentVolume_ = 1.0f;
  193. bool noiseReductionEnabled_ = false;
  194. bool echoCancellationEnabled_ = false;
  195. std::atomic<float> audioLevel_{0.0f};
  196. // 采集线程
  197. std::thread captureThread_;
  198. std::atomic<bool> shouldStop_{false};
  199. std::condition_variable pauseCondition_;
  200. std::mutex pauseMutex_;
  201. // 帧缓冲
  202. std::queue<AVFramePtr> frameQueue_;
  203. std::mutex queueMutex_;
  204. std::condition_variable queueCondition_;
  205. static constexpr size_t MAX_QUEUE_SIZE = 20;
  206. // 设备信息缓存
  207. mutable std::vector<AudioDeviceInfo> cachedDevices_;
  208. mutable std::mutex deviceCacheMutex_;
  209. mutable bool devicesCached_ = false;
  210. // 音频电平计算
  211. mutable std::mutex levelMutex_;
  212. std::chrono::steady_clock::time_point lastLevelUpdate_;
  213. static constexpr double LEVEL_UPDATE_INTERVAL = 0.1; // 100ms
  214. };
  215. } // namespace capture
  216. } // namespace av
  217. #endif // AV_CAPTURE_AUDIO_CAPTURER_H