| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #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"
- #include <string>
- class AudioRecorder
- {
- public:
- AudioRecorder();
- ~AudioRecorder();
- struct Info
- {
- AudioMixer* mixer = nullptr;
- AvMuxer* muxer = nullptr;
- bool* isRecord = nullptr;
- int mixIndex;
- int* streamIndex = nullptr;
- };
-
- struct MuxerInfo
- {
- AvMuxer* muxer;
- int streamIndex;
- MuxerInfo(AvMuxer* m, int idx) : muxer(m), streamIndex(idx) {}
- };
- bool Open(const std::vector<AudioCapturer::Type>& deviceTypes,
- Encoder<MediaType::AUDIO>::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 UnloadMuxer(AvMuxer& muxer);
- bool StartRecord();
- void StopRecord();
- void Close();
- void PullAndProcessAudio(); // 新增:主动拉取音频数据
- auto GetCaptureInfo(int mixIndex) { return _mixer.GetInputInfo(mixIndex); }
- void SetVolumeScale(float scale, int mixIndex);
- std::string GetEncoderNameForMuxer(AvMuxer& muxer);
- private:
- std::vector<IAudioCapturer*> m_audioCapturers;
- AudioMixer _mixer;
- std::vector<Info> _infos;
- std::vector<MuxerInfo> _muxers;
- std::mutex _muxersMtx;
- bool _isRecord = false;
- int _streamIndex;
- Encoder<MediaType::AUDIO>::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
|