audio_recorder.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "audio_recorder.h"
  2. #include "capturer/audio/audio_qt_capturer.h"
  3. #include "qdebug.h"
  4. AudioRecorder::AudioRecorder() {}
  5. AudioRecorder::~AudioRecorder()
  6. {
  7. Close();
  8. }
  9. bool AudioRecorder::Open(const std::vector<AudioCapturer::Type>& deviceTypes,
  10. Encoder<MediaType::AUDIO>::Param& param,
  11. const uint32_t sampleRate,
  12. const uint32_t channels,
  13. const uint32_t bitsPerSample,
  14. const AVSampleFormat format)
  15. {
  16. qDebug() << "AudioRecorder::Open called, deviceTypes size:" << deviceTypes.size();
  17. Close();
  18. Info mixInfo;
  19. mixInfo.mixer = &_mixer;
  20. mixInfo.isRecord = &_isRecord;
  21. mixInfo.streamIndex = &_streamIndex;
  22. // 清空并重新创建音频捕获器
  23. m_audioCapturers.clear();
  24. for (int index = 0; index < deviceTypes.size(); ++index) {
  25. mixInfo.mixIndex = index;
  26. _infos.push_back(mixInfo);
  27. m_audioCapturers.push_back(new QtAudioCapturer());
  28. }
  29. // 初始化每个音频捕获器
  30. for (int index = 0; index < deviceTypes.size(); ++index) {
  31. auto capturer = m_audioCapturers[index];
  32. if (!capturer->Init(deviceTypes[index])) {
  33. continue;
  34. }
  35. auto&& format = capturer->GetFormat();
  36. __CheckBool(_mixer.AddAudioInput(index,
  37. format.sampleRate,
  38. format.channels,
  39. format.bitsPerSample,
  40. _GetAVSampleFormat(format.bitsPerSample)));
  41. }
  42. __CheckBool(_mixer.AddAudioOutput(sampleRate, channels, bitsPerSample, format));
  43. _param = param;
  44. __CheckBool(_mixer.SetOutFrameSize(1024));
  45. // 启动所有成功初始化的音频捕获器
  46. for (auto capturer : m_audioCapturers) {
  47. capturer->Start();
  48. }
  49. return true;
  50. }
  51. void AudioRecorder::Close()
  52. {
  53. StopRecord();
  54. // 停止并释放所有音频捕获器
  55. for (auto capturer : m_audioCapturers) {
  56. if (capturer) {
  57. capturer->Stop();
  58. delete capturer;
  59. }
  60. }
  61. m_audioCapturers.clear();
  62. _mixer.Close();
  63. _infos.clear();
  64. }
  65. void AudioRecorder::SetVolumeScale(float scale, int mixIndex)
  66. {
  67. auto info = _mixer.GetInputInfo(mixIndex);
  68. if (info != nullptr) {
  69. info->scale = scale;
  70. }
  71. }
  72. bool AudioRecorder::LoadMuxer(AvMuxer& muxer)
  73. {
  74. for (auto&& info : _infos) {
  75. info.muxer = &muxer;
  76. }
  77. __CheckBool((_streamIndex = muxer.AddAudioStream(_param)) != -1);
  78. return true;
  79. }
  80. bool AudioRecorder::StartRecord()
  81. {
  82. _isRecord = true;
  83. m_audioTimer.Start(AUDIO_PULL_INTERVAL_MS, [this] {
  84. this->PullAndProcessAudio();
  85. });
  86. return true;
  87. }
  88. void AudioRecorder::StopRecord()
  89. {
  90. _isRecord = false;
  91. m_audioTimer.Stop();
  92. }
  93. // 新增:主动拉取音频数据的接口
  94. void AudioRecorder::PullAndProcessAudio()
  95. {
  96. for (int index = 0; index < m_audioCapturers.size(); ++index) {
  97. // 每次多次拉取小块数据,提升音频帧写入频率
  98. while (true) {
  99. char buf[1024];
  100. int bytes = m_audioCapturers[index]->readAudioData(buf, sizeof(buf));
  101. if (bytes <= 0) break;
  102. auto frame = _mixer.Convert(index, (uint8_t*)buf, bytes);
  103. if (frame && _isRecord && _streamIndex != -1) {
  104. int frameSize = _mixer.GetOutFrameSize();
  105. if (_mixer.GetOutFrameSize() != frameSize) {
  106. _mixer.SetOutFrameSize(frameSize);
  107. continue;
  108. }
  109. _infos[index].muxer->Write(frame, _streamIndex);
  110. }
  111. }
  112. }
  113. }