#ifndef __AUDIO_RECORDER_H__ #define __AUDIO_RECORDER_H__ #include "capturer/audio/audio_capturer.h" #include "encoder/audio_mixer.h" #include "muxer/av_muxer.h" #include "basic/timer.h" class AudioRecorder { public: AudioRecorder(); ~AudioRecorder(); struct Info { AudioMixer* mixer = nullptr; AvMuxer* muxer = nullptr; bool* isRecord = nullptr; int mixIndex; int* streamIndex = nullptr; }; bool Open(const std::vector& deviceTypes, Encoder::Param& param, const uint32_t sampleRate = AUDIO_SAMPLE_RATE, const uint32_t channels = AUDIO_CHANNEL, const uint32_t bitsPerSample = 32, const AVSampleFormat format = AUDIO_FMT); bool LoadMuxer(AvMuxer& muxer); bool StartRecord(); void StopRecord(); void Close(); void PullAndProcessAudio(); // 新增:主动拉取音频数据 auto GetCaptureInfo(int mixIndex) { return _mixer.GetInputInfo(mixIndex); } void SetVolumeScale(float scale, int mixIndex); private: std::vector m_audioCapturers; AudioMixer _mixer; std::vector _infos; bool _isRecord = false; int _streamIndex; Encoder::Param _param; Timer m_audioTimer; // 新增高精度定时器 static constexpr int AUDIO_PULL_INTERVAL_MS = 10; AVSampleFormat _GetAVSampleFormat(int wBitsPerSample, bool isFloat = true) { // isFloat=true 表示32/64位时优先返回浮点格式,否则返回整型 switch (wBitsPerSample) { case 8: return AV_SAMPLE_FMT_U8; case 16: return AV_SAMPLE_FMT_S16; case 24: // FFmpeg没有24bit整型,通常用32bit整型或float return isFloat ? AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S32; case 32: return isFloat ? AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S32; case 64: return isFloat ? AV_SAMPLE_FMT_DBL : AV_SAMPLE_FMT_S64; default: // 默认返回float return AV_SAMPLE_FMT_FLT; } } }; #endif