audio_recorder.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef __AUDIO_RECORDER_H__
  2. #define __AUDIO_RECORDER_H__
  3. #include "capturer/audio/audio_capturer.h"
  4. #include "encoder/audio_mixer.h"
  5. #include "muxer/av_muxer.h"
  6. #include "basic/timer.h"
  7. class AudioRecorder
  8. {
  9. public:
  10. AudioRecorder();
  11. ~AudioRecorder();
  12. struct Info
  13. {
  14. AudioMixer* mixer = nullptr;
  15. AvMuxer* muxer = nullptr;
  16. bool* isRecord = nullptr;
  17. int mixIndex;
  18. int* streamIndex = nullptr;
  19. };
  20. bool Open(const std::vector<AudioCapturer::Type>& deviceTypes,
  21. Encoder<MediaType::AUDIO>::Param& param,
  22. const uint32_t sampleRate = AUDIO_SAMPLE_RATE,
  23. const uint32_t channels = AUDIO_CHANNEL,
  24. const uint32_t bitsPerSample = 32,
  25. const AVSampleFormat format = AUDIO_FMT);
  26. bool LoadMuxer(AvMuxer& muxer);
  27. bool StartRecord();
  28. void StopRecord();
  29. void Close();
  30. void PullAndProcessAudio(); // 新增:主动拉取音频数据
  31. auto GetCaptureInfo(int mixIndex) { return _mixer.GetInputInfo(mixIndex); }
  32. void SetVolumeScale(float scale, int mixIndex);
  33. private:
  34. std::vector<IAudioCapturer*> m_audioCapturers;
  35. AudioMixer _mixer;
  36. std::vector<Info> _infos;
  37. bool _isRecord = false;
  38. int _streamIndex;
  39. Encoder<MediaType::AUDIO>::Param _param;
  40. Timer m_audioTimer; // 新增高精度定时器
  41. static constexpr int AUDIO_PULL_INTERVAL_MS = 10;
  42. static void _Callback(void* data, size_t size, void* userInfo);
  43. AVSampleFormat _GetAVSampleFormat(int wBitsPerSample)
  44. {
  45. return wBitsPerSample == 16 ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_S32;
  46. }
  47. };
  48. #endif