audio_recorder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "audio_recorder.h"
  2. #include "capturer/audio/audio_qt_capturer.h"
  3. #include "capturer/audio/wasapi_loopback_capturer.h"
  4. #include "qdebug.h"
  5. AudioRecorder::AudioRecorder() {}
  6. AudioRecorder::~AudioRecorder()
  7. {
  8. Close();
  9. }
  10. bool AudioRecorder::Open(const std::vector<AudioCapturer::Type>& deviceTypes,
  11. Encoder<MediaType::AUDIO>::Param& param,
  12. const uint32_t sampleRate,
  13. const uint32_t channels,
  14. const uint32_t bitsPerSample,
  15. const AVSampleFormat format)
  16. {
  17. qDebug() << "AudioRecorder::Open called, deviceTypes size:" << deviceTypes.size();
  18. Close();
  19. Info mixInfo;
  20. mixInfo.mixer = &_mixer;
  21. mixInfo.isRecord = &_isRecord;
  22. mixInfo.streamIndex = &_streamIndex;
  23. // 清空并重新创建音频捕获器
  24. m_audioCapturers.clear();
  25. for (int index = 0; index < deviceTypes.size(); ++index) {
  26. mixInfo.mixIndex = index;
  27. _infos.push_back(mixInfo);
  28. if (deviceTypes[index] == AudioCapturer::Type::Speaker) {
  29. m_audioCapturers.push_back(new WASAPILoopbackCapturer());
  30. } else {
  31. m_audioCapturers.push_back(new QtAudioCapturer());
  32. }
  33. }
  34. // 初始化每个音频捕获器
  35. for (int index = 0; index < deviceTypes.size(); ++index) {
  36. if (!m_audioCapturers[index]->Init(deviceTypes[index])) {
  37. qDebug() << "Failed to initialize audio capturer" << index;
  38. return false;
  39. }
  40. // 添加音频格式验证
  41. const AudioFormat& format = m_audioCapturers[index]->GetFormat();
  42. qDebug() << "Audio Capturer" << index << "Format:" << "SampleRate:" << format.sampleRate
  43. << "Channels:" << format.channels << "BitsPerSample:" << format.bitsPerSample
  44. << "BlockAlign:" << format.blockAlign
  45. << "AvgBytesPerSec:" << format.avgBytesPerSec;
  46. // 验证格式是否与预期一致
  47. if (format.sampleRate != sampleRate) {
  48. qDebug() << "Warning: Sample rate mismatch. Expected:" << sampleRate
  49. << "Got:" << format.sampleRate;
  50. }
  51. if (format.channels != channels) {
  52. qDebug() << "Warning: Channel count mismatch. Expected:" << channels
  53. << "Got:" << format.channels;
  54. }
  55. __CheckBool(_mixer.AddAudioInput(index,
  56. format.sampleRate,
  57. format.channels,
  58. format.bitsPerSample,
  59. _GetAVSampleFormat(format.bitsPerSample)));
  60. }
  61. __CheckBool(_mixer.AddAudioOutput(sampleRate, channels, bitsPerSample, format));
  62. _param = param;
  63. __CheckBool(_mixer.SetOutFrameSize(1024));
  64. // 启动所有成功初始化的音频捕获器
  65. for (auto capturer : m_audioCapturers) {
  66. capturer->Start();
  67. }
  68. return true;
  69. }
  70. void AudioRecorder::Close()
  71. {
  72. StopRecord();
  73. // 停止并释放所有音频捕获器
  74. for (auto capturer : m_audioCapturers) {
  75. if (capturer) {
  76. capturer->Stop();
  77. delete capturer;
  78. }
  79. }
  80. m_audioCapturers.clear();
  81. _mixer.Close();
  82. _infos.clear();
  83. }
  84. void AudioRecorder::SetVolumeScale(float scale, int mixIndex)
  85. {
  86. auto info = _mixer.GetInputInfo(mixIndex);
  87. if (info != nullptr) {
  88. info->scale = scale;
  89. }
  90. }
  91. bool AudioRecorder::LoadMuxer(AvMuxer& muxer)
  92. {
  93. for (auto&& info : _infos) {
  94. info.muxer = &muxer;
  95. }
  96. __CheckBool((_streamIndex = muxer.AddAudioStream(_param)) != -1);
  97. return true;
  98. }
  99. bool AudioRecorder::StartRecord()
  100. {
  101. _isRecord = true;
  102. m_audioTimer.Start(AUDIO_PULL_INTERVAL_MS, [this] {
  103. this->PullAndProcessAudio();
  104. });
  105. return true;
  106. }
  107. void AudioRecorder::StopRecord()
  108. {
  109. _isRecord = false;
  110. m_audioTimer.Stop();
  111. }
  112. // 新增:主动拉取音频数据的接口
  113. void AudioRecorder::PullAndProcessAudio()
  114. {
  115. for (int index = 0; index < m_audioCapturers.size(); ++index) {
  116. // 每次多次拉取小块数据,提升音频帧写入频率
  117. while (true) {
  118. char buf[1024];
  119. int bytes = m_audioCapturers[index]->readAudioData(buf, sizeof(buf));
  120. // static int debugCounter = 0;
  121. // if (++debugCounter % 100 == 0) { // 每100次打印一次,避免日志过多
  122. // qDebug() << "Capturer" << index << "read" << bytes << "bytes";
  123. // }
  124. if (bytes <= 0) {
  125. break;
  126. }
  127. // 添加调试信息,显示数据大小
  128. auto frame = _mixer.Convert(index, (uint8_t*) buf, bytes);
  129. if (frame && _isRecord && _streamIndex != -1) {
  130. int frameSize = _mixer.GetOutFrameSize();
  131. if (_mixer.GetOutFrameSize() != frameSize) {
  132. _mixer.SetOutFrameSize(frameSize);
  133. continue;
  134. }
  135. _infos[index].muxer->Write(frame, _streamIndex);
  136. }
  137. }
  138. }
  139. }