audio_mixer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "audio_mixer.h"
  2. #include "basic/basic.h"
  3. #include "basic/frame.h"
  4. #include "libavutil/error.h"
  5. #include <QDebug>
  6. #include <windows.h>
  7. AVSampleFormat BitsToFmt(int bits)
  8. {
  9. switch (bits) {
  10. case 8:
  11. return AV_SAMPLE_FMT_U8;
  12. case 16:
  13. return AV_SAMPLE_FMT_S16;
  14. case 32:
  15. return AV_SAMPLE_FMT_S32;
  16. case 64:
  17. return AV_SAMPLE_FMT_S64;
  18. default:
  19. return AV_SAMPLE_FMT_FLT;
  20. }
  21. }
  22. int FmtToBits(AVSampleFormat fmt)
  23. {
  24. switch (fmt) {
  25. case AV_SAMPLE_FMT_U8:
  26. return 8;
  27. case AV_SAMPLE_FMT_S16:
  28. return 16;
  29. case AV_SAMPLE_FMT_S32:
  30. return 32;
  31. case AV_SAMPLE_FMT_S64:
  32. return 64;
  33. default:
  34. return 32;
  35. }
  36. }
  37. int SizeToNbSamples(int size, int bitsPerSample, int nbChannels)
  38. {
  39. return (size << 3) / bitsPerSample / nbChannels;
  40. }
  41. int NbSamplesToSize(int nbSamples, int bitsPerSample, int nbChannels)
  42. {
  43. return (nbSamples * bitsPerSample * nbChannels) >> 3;
  44. }
  45. bool FrameQueue::Init(int channelNums, int sampleRate, AVSampleFormat fmt, int nbSamples)
  46. {
  47. _front = 0;
  48. _sampleRate = sampleRate;
  49. _fmt = fmt;
  50. _nbSamples = nbSamples;
  51. _usedLinesize = nbSamples * channelNums * (fmt == AV_SAMPLE_FMT_S16 ? 2 : 4);
  52. av_channel_layout_default(&_layout, channelNums);
  53. _queue.emplace(fmt, &_layout, sampleRate, nbSamples);
  54. return true;
  55. }
  56. Frame<MediaType::AUDIO> FrameQueue::Pop()
  57. {
  58. if (_queue.size() > 1) {
  59. auto frame = std::move(_queue.front());
  60. _queue.pop();
  61. return frame;
  62. }
  63. return Frame<MediaType::AUDIO>();
  64. }
  65. void FrameQueue::Push(uint8_t* data, int length)
  66. {
  67. if (length > _usedLinesize) { // 递归调用
  68. Push(data, length / 2);
  69. Push(data + length / 2, length / 2 + length % 2);
  70. return;
  71. }
  72. auto&& frame = _queue.back().frame;
  73. int secondLength = _front + length - _usedLinesize;
  74. if (secondLength <= 0) { // 第一段缓存是够用的
  75. memcpy(frame->data[0] + _front, data, length);
  76. _front += length;
  77. return;
  78. }
  79. // 第一段缓存不够用
  80. int firstLength = length - secondLength;
  81. if (firstLength > 0) {
  82. memcpy(frame->data[0] + _front, data, firstLength);
  83. }
  84. // 载入一段新缓存
  85. _queue.emplace(_fmt, &_layout, _sampleRate, _nbSamples);
  86. memcpy(_queue.back().frame->data[0], data + firstLength, secondLength);
  87. _front = secondLength;
  88. }
  89. bool Resampler::Open(int inChannelNums,
  90. int inSampleRate,
  91. AVSampleFormat inFmt,
  92. int outChannelNums,
  93. int outSampleRate,
  94. AVSampleFormat outFmt,
  95. int outNbSample)
  96. {
  97. Close();
  98. _swrCtx = swr_alloc();
  99. if (!_swrCtx) {
  100. __DebugPrint("swr_alloc failed");
  101. return false;
  102. }
  103. AVChannelLayout tmpLayout;
  104. av_channel_layout_default(&tmpLayout, inChannelNums);
  105. av_opt_set_chlayout(_swrCtx, "in_chlayout", &tmpLayout, 0);
  106. av_opt_set_int(_swrCtx, "in_sample_rate", inSampleRate, 0);
  107. av_opt_set_sample_fmt(_swrCtx, "in_sample_fmt", inFmt, 0);
  108. if (!_fromQueue.Init(inChannelNums, inSampleRate, inFmt, inSampleRate / 100 * 2)) {
  109. __DebugPrint("_fromQueue.Init failed");
  110. return false;
  111. }
  112. av_channel_layout_default(&tmpLayout, outChannelNums);
  113. av_opt_set_chlayout(_swrCtx, "out_chlayout", &tmpLayout, 0);
  114. av_opt_set_int(_swrCtx, "out_sample_rate", outSampleRate, 0);
  115. av_opt_set_sample_fmt(_swrCtx, "out_sample_fmt", outFmt, 0);
  116. if (swr_init(_swrCtx) < 0) {
  117. Close();
  118. __DebugPrint("swr_init(_swrCtx) failed\n");
  119. return false;
  120. }
  121. if (!_toQueue.Init(outChannelNums, outSampleRate, outFmt, outNbSample)) {
  122. __DebugPrint("_toQueue.Init failed");
  123. Close();
  124. return false;
  125. }
  126. _swrFrame = Frame<MediaType::AUDIO>::Alloc(outFmt,
  127. &tmpLayout,
  128. outSampleRate,
  129. outSampleRate / 100 * 2);
  130. if (!_swrFrame) {
  131. __DebugPrint("Alloc _swrFrame failed");
  132. Close();
  133. return false;
  134. }
  135. return true;
  136. }
  137. void Resampler::Close()
  138. {
  139. Free(_swrCtx, [this] { swr_free(&_swrCtx); });
  140. Free(_swrFrame, [this] { av_frame_free(&_swrFrame); });
  141. }
  142. AVFrame* AudioMixer::Convert(uint32_t index, uint8_t* inBuf, uint32_t size)
  143. {
  144. std::lock_guard<std::mutex> locker(_mutex);
  145. auto iter = _audioInputInfos.find(index);
  146. if (iter == _audioInputInfos.end()) {
  147. __DebugPrint("Audio input index %u not found", index);
  148. return nullptr;
  149. }
  150. // // 添加调试信息
  151. // static int debugCounter = 0;
  152. // if (++debugCounter % 100 == 0) {
  153. // qDebug() << "AudioMixer::Convert - Input size:" << size << "bytes, Input index: " << index;
  154. // }
  155. if (!iter->second.resampler->Convert(inBuf, size)) {
  156. __DebugPrint("Resampler::Convert failed");
  157. return nullptr;
  158. }
  159. return _AdjustVolume() ? _outputFrame : nullptr;
  160. }
  161. bool AudioMixer::_AdjustVolume()
  162. {
  163. // 检测所有流之间是不是相差太大了以及缓存的数据是不是太多了
  164. // 如果缓存的数据太多,直接将所有的队列删除同样的数据
  165. // 如果两个流直接数据相差太大,将多的那个减到和少的那个一样
  166. constexpr int MAX_DIFF = 10;
  167. constexpr int MAX_BUF_SIZE = 20;
  168. int minSize = INT_MAX;
  169. int maxSize = INT_MIN;
  170. FrameQueue* maxQueue = nullptr;
  171. #undef min
  172. for (auto&& iter : _audioInputInfos) {
  173. auto&& queue = iter.second.resampler->GetQueue();
  174. if (queue.IsEmpty()) {
  175. return false;
  176. }
  177. minSize = std::min(minSize, (int) queue.GetSize());
  178. if (maxSize < (int) queue.GetSize()) {
  179. maxSize = (int) queue.GetSize();
  180. maxQueue = &queue;
  181. }
  182. }
  183. if (maxSize - minSize > MAX_DIFF) {
  184. // __DebugPrint("Clear MAX_DIFF");
  185. for (int i = 0; i < maxSize - minSize; ++i) {
  186. maxQueue->Pop();
  187. }
  188. }
  189. auto writeStream = (float*)(_outputFrame->data[0]);
  190. memset(writeStream, 0, _outputFrame->linesize[0]);
  191. for (auto iter = _audioInputInfos.begin(); iter != _audioInputInfos.end(); ++iter) {
  192. auto&& frameQueue = iter->second.resampler->GetQueue();
  193. if (minSize > MAX_BUF_SIZE) {
  194. // __DebugPrint("Clear MAX_BUF_SIZE");
  195. for (int i = 0; i < minSize - 2; ++i) {
  196. frameQueue.Pop();
  197. }
  198. }
  199. auto frame = frameQueue.Pop();
  200. auto scale = iter->second.scale;
  201. auto readStream = (float*) (frame.frame->data[0]);
  202. iter->second.volume = readStream[0] * scale;
  203. // 逐个计算赋值,优化音量处理
  204. for (int idx = 0; idx < _outputFrame->nb_samples; ++idx) {
  205. float sample = readStream[idx] * scale;
  206. // 使用软限制而不是硬限制,减少失真
  207. if (sample > 0.8f) {
  208. sample = 0.8f + (sample - 0.8f) * 0.3f; // 软限制
  209. } else if (sample < -0.8f) {
  210. sample = -0.8f + (sample + 0.8f) * 0.3f; // 软限制
  211. }
  212. writeStream[idx] += sample;
  213. }
  214. }
  215. return true;
  216. }
  217. AudioMixer::AudioMixer()
  218. : _inited(false)
  219. {}
  220. AudioMixer::~AudioMixer()
  221. {
  222. // delete out_buf;
  223. if (_inited) {
  224. Close();
  225. }
  226. }
  227. bool AudioMixer::AddAudioInput(uint32_t index,
  228. uint32_t sampleRate,
  229. uint32_t channels,
  230. uint32_t bitsPerSample,
  231. AVSampleFormat format)
  232. {
  233. std::lock_guard<std::mutex> locker(_mutex);
  234. if (_inited) {
  235. __DebugPrint("AddAudioInput called after init");
  236. return false;
  237. }
  238. // 根据index保存是否已经存在
  239. if (_audioInputInfos.find(index) != _audioInputInfos.end()) {
  240. __DebugPrint("AddAudioInput duplicate index %u", index);
  241. return false;
  242. }
  243. auto& filterInfo = _audioInputInfos[index];
  244. // 设置音频相关参数
  245. filterInfo.sampleRate = sampleRate;
  246. filterInfo.channels = channels;
  247. filterInfo.bitsPerSample = bitsPerSample;
  248. filterInfo.format = format;
  249. filterInfo.name = std::string("input") + std::to_string(index);
  250. return true;
  251. }
  252. bool AudioMixer::AddAudioOutput(const uint32_t sampleRate,
  253. const uint32_t channels,
  254. const uint32_t bitsPerSample,
  255. const AVSampleFormat format)
  256. {
  257. std::lock_guard<std::mutex> locker(_mutex);
  258. if (_inited) {
  259. __DebugPrint("AddAudioOutput called after init");
  260. return false;
  261. }
  262. // 设置音频相关参数
  263. _audioOutputInfo.sampleRate = sampleRate;
  264. _audioOutputInfo.channels = channels;
  265. _audioOutputInfo.bitsPerSample = bitsPerSample;
  266. _audioOutputInfo.format = format;
  267. _audioOutputInfo.name = "output";
  268. return true;
  269. }
  270. bool AudioMixer::SetOutFrameSize(int outFrameSize)
  271. {
  272. if (_outFrameSize == outFrameSize) {
  273. return true;
  274. }
  275. _outFrameSize = outFrameSize;
  276. for (auto&& filterInfoPair : _audioInputInfos) {
  277. auto&& filterInfo = filterInfoPair.second;
  278. filterInfo.resampler = std::make_unique<Resampler>();
  279. if (!filterInfo.resampler->Open(filterInfo.channels,
  280. filterInfo.sampleRate,
  281. filterInfo.format,
  282. _audioOutputInfo.channels,
  283. _audioOutputInfo.sampleRate,
  284. _audioOutputInfo.format,
  285. outFrameSize)) {
  286. __DebugPrint("Resampler::Open failed for input %s", filterInfo.name.c_str());
  287. return false;
  288. }
  289. }
  290. AVChannelLayout tmpLayout;
  291. av_channel_layout_default(&tmpLayout, _audioOutputInfo.channels);
  292. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  293. _outputFrame = Frame<MediaType::AUDIO>::Alloc(_audioOutputInfo.format,
  294. &tmpLayout,
  295. _audioOutputInfo.sampleRate,
  296. outFrameSize);
  297. if (!_outputFrame) {
  298. __DebugPrint("Alloc _outputFrame failed");
  299. return false;
  300. }
  301. _inited = true;
  302. return true;
  303. }
  304. bool AudioMixer::Close()
  305. {
  306. if (!_inited) {
  307. return true;
  308. }
  309. _inited = false;
  310. std::lock_guard<std::mutex> locker(_mutex);
  311. _audioInputInfos.clear();
  312. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  313. _outFrameSize = 0;
  314. return true;
  315. }
  316. AudioMixer::AudioInfo* AudioMixer::GetInputInfo(uint32_t index)
  317. {
  318. auto iter = _audioInputInfos.find(index);
  319. return iter == _audioInputInfos.end() ? nullptr : &(iter->second);
  320. }
  321. bool Resampler::Convert(uint8_t* data, int size)
  322. {
  323. if (data == nullptr) {
  324. return false;
  325. }
  326. _fromQueue.Push(data, size);
  327. for (;;) { // 转换
  328. auto frame = _fromQueue.Pop();
  329. if (frame.frame == nullptr) {
  330. break;
  331. }
  332. int ret = swr_convert(_swrCtx,
  333. _swrFrame->data,
  334. _swrFrame->nb_samples,
  335. (const uint8_t**) frame.frame->data,
  336. frame.frame->nb_samples);
  337. if (ret < 0) {
  338. char buffer[AV_ERROR_MAX_STRING_SIZE];
  339. av_strerror(ret, buffer, sizeof(buffer));
  340. qDebug() << "swr_convert failed" << ret << QString::fromLatin1(buffer);
  341. return false;
  342. }
  343. _toQueue.Push(_swrFrame->data[0], _swrFrame->linesize[0]);
  344. }
  345. return true;
  346. }