| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #ifndef AV_CAPTURE_AUDIO_CAPTURER_H
- #define AV_CAPTURE_AUDIO_CAPTURER_H
- #include "capture_abstract_capturer.h"
- #include <thread>
- #include <condition_variable>
- #include <queue>
- #include <atomic>
- extern "C" {
- #include <libavformat/avformat.h>
- #include <libavdevice/avdevice.h>
- #include <libswresample/swresample.h>
- }
- namespace av {
- namespace capture {
- /**
- * 音频采集器参数
- */
- struct AudioCaptureParams : public CapturerParams {
- int sampleRate = 44100; // 采样率
- int channels = 2; // 声道数
- AVSampleFormat sampleFormat = AV_SAMPLE_FMT_S16; // 采样格式
- int bufferSize = 1024; // 缓冲区大小(采样数)
-
- // 麦克风特定参数
- int micIndex = 0; // 麦克风索引
- std::string micFormat; // 麦克风格式 (如 "dshow", "alsa")
- float volume = 1.0f; // 音量 (0.0-2.0)
- bool enableNoiseReduction = false; // 降噪
- bool enableEchoCancellation = false; // 回声消除
-
- // 系统音频特定参数
- bool captureLoopback = false; // 捕获回环音频
- std::string audioDevice; // 音频设备名称
-
- AudioCaptureParams(CapturerType type) : CapturerParams(type, MediaType::AUDIO) {}
- };
- /**
- * 音频设备信息
- */
- struct AudioDeviceInfo {
- std::string id; // 设备ID
- std::string name; // 设备名称
- std::string description; // 设备描述
- bool isDefault = false; // 是否为默认设备
- bool isInput = true; // 是否为输入设备
- std::vector<int> supportedSampleRates; // 支持的采样率
- std::vector<int> supportedChannels; // 支持的声道数
- std::vector<AVSampleFormat> supportedFormats; // 支持的采样格式
- };
- /**
- * 音频采集器类
- * 支持麦克风和系统音频采集
- */
- class AudioCapturer : public AbstractCapturer {
- public:
- AudioCapturer();
- virtual ~AudioCapturer();
-
- // 基础接口实现
- ErrorCode initialize(const CapturerParams& params) override;
- ErrorCode start() override;
- ErrorCode stop() override;
- ErrorCode pause() override;
- ErrorCode resume() override;
- ErrorCode reset() override;
- ErrorCode close() override;
-
- // 设备信息
- std::vector<std::string> getAvailableDevices() const override;
- std::string getCurrentDevice() const override;
-
- /**
- * 获取详细设备信息
- * @return 设备信息列表
- */
- std::vector<AudioDeviceInfo> getDetailedDeviceInfo() const;
-
- /**
- * 设置音频参数
- * @param sampleRate 采样率
- * @param channels 声道数
- * @param sampleFormat 采样格式
- * @return 错误码
- */
- ErrorCode setAudioParams(int sampleRate, int channels, AVSampleFormat sampleFormat);
-
- /**
- * 设置音量
- * @param volume 音量 (0.0-2.0)
- * @return 错误码
- */
- ErrorCode setVolume(float volume);
-
- /**
- * 获取当前音量
- * @return 音量值
- */
- float getVolume() const;
-
- /**
- * 设置降噪
- * @param enable 是否启用
- * @return 错误码
- */
- ErrorCode setNoiseReduction(bool enable);
-
- /**
- * 设置回声消除
- * @param enable 是否启用
- * @return 错误码
- */
- ErrorCode setEchoCancellation(bool enable);
-
- /**
- * 获取当前音频参数
- * @return 音频参数
- */
- AudioCaptureParams getCurrentParams() const;
-
- /**
- * 获取音频电平
- * @return 电平值 (0.0-1.0)
- */
- float getAudioLevel() const;
-
- /**
- * 音频采集器工厂
- */
- class AudioCaptureFactory {
- public:
- /**
- * 创建麦克风采集器
- * @param micIndex 麦克风索引
- * @return 采集器实例
- */
- static std::unique_ptr<AudioCapturer> createMicrophone(int micIndex = 0);
-
- /**
- * 创建系统音频采集器
- * @param loopback 是否为回环模式
- * @return 采集器实例
- */
- static std::unique_ptr<AudioCapturer> createSystemAudio(bool loopback = false);
-
- /**
- * 创建最佳麦克风采集器
- * @return 采集器实例
- */
- static std::unique_ptr<AudioCapturer> createBestMicrophone();
- };
-
- protected:
- bool validateParams(const CapturerParams& params) override;
-
- private:
- // 内部实现方法
- ErrorCode initializeMicrophone();
- ErrorCode initializeSystemAudio();
- ErrorCode openInputDevice();
- ErrorCode setupAudioParams();
- ErrorCode setupAudioResampling();
- ErrorCode setupAudioProcessing();
-
- // 采集线程
- void captureThreadFunc();
- ErrorCode captureFrame();
-
- // 音频处理
- AVFramePtr processAudioFrame(const AVFramePtr& frame);
- AVFramePtr resampleAudioFrame(const AVFramePtr& frame);
- AVFramePtr applyVolumeControl(const AVFramePtr& frame);
- AVFramePtr applyNoiseReduction(const AVFramePtr& frame);
- void calculateAudioLevel(const AVFramePtr& frame);
-
- // 资源清理
- void cleanupResampler();
- void cleanupAudioProcessing();
-
- // 设备枚举
- std::vector<AudioDeviceInfo> enumerateMicrophones() const;
- std::vector<AudioDeviceInfo> enumerateSystemAudioDevices() const;
-
- // 平台特定实现
- #ifdef _WIN32
- std::vector<AudioDeviceInfo> enumerateDirectSoundDevices() const;
- std::vector<AudioDeviceInfo> enumerateWASAPIDevices() const;
- ErrorCode setupDirectSoundMicrophone();
- ErrorCode setupWASAPISystemAudio();
- #elif defined(__linux__)
- std::vector<AudioDeviceInfo> enumerateALSADevices() const;
- std::vector<AudioDeviceInfo> enumeratePulseAudioDevices() const;
- ErrorCode setupALSAMicrophone();
- ErrorCode setupPulseAudioCapture();
- #elif defined(__APPLE__)
- std::vector<AudioDeviceInfo> enumerateCoreAudioDevices() const;
- ErrorCode setupCoreAudioMicrophone();
- ErrorCode setupCoreAudioSystemCapture();
- #endif
-
- // 获取平台特定的输入格式
- const AVInputFormat* getPlatformInputFormat() const;
- std::string getPlatformDeviceName() const;
-
- private:
- // 采集参数
- AudioCaptureParams audioParams_;
-
- // FFmpeg 相关
- AVFormatContext* formatCtx_ = nullptr;
- AVCodecContext* codecCtx_ = nullptr;
- const AVCodec* codec_ = nullptr;
- int audioStreamIndex_ = -1;
-
- // 音频重采样
- SwrContext* swrCtx_ = nullptr;
- AVFramePtr resampledFrame_;
- bool needResampling_ = false;
-
- // 音频处理
- float currentVolume_ = 1.0f;
- bool noiseReductionEnabled_ = false;
- bool echoCancellationEnabled_ = false;
- std::atomic<float> audioLevel_{0.0f};
-
- // 采集线程
- std::thread captureThread_;
- std::atomic<bool> shouldStop_{false};
- std::condition_variable pauseCondition_;
- std::mutex pauseMutex_;
-
- // 帧缓冲
- std::queue<AVFramePtr> frameQueue_;
- std::mutex queueMutex_;
- std::condition_variable queueCondition_;
- static constexpr size_t MAX_QUEUE_SIZE = 20;
-
- // 设备信息缓存
- mutable std::vector<AudioDeviceInfo> cachedDevices_;
- mutable std::mutex deviceCacheMutex_;
- mutable bool devicesCached_ = false;
-
- // 音频电平计算
- mutable std::mutex levelMutex_;
- std::chrono::steady_clock::time_point lastLevelUpdate_;
- static constexpr double LEVEL_UPDATE_INTERVAL = 0.1; // 100ms
- };
- } // namespace capture
- } // namespace av
- #endif // AV_CAPTURE_AUDIO_CAPTURER_H
|