audio_mixer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "audio_mixer.h"
  2. #include "basic/basic.h"
  3. #include "basic/frame.h"
  4. #include <QDebug>
  5. #include <windows.h>
  6. AVSampleFormat BitsToFmt(int bits)
  7. {
  8. switch (bits) {
  9. case 8:
  10. return AV_SAMPLE_FMT_U8;
  11. case 16:
  12. return AV_SAMPLE_FMT_S16;
  13. case 32:
  14. return AV_SAMPLE_FMT_S32;
  15. case 64:
  16. return AV_SAMPLE_FMT_S64;
  17. default:
  18. return AV_SAMPLE_FMT_FLT;
  19. }
  20. }
  21. int FmtToBits(AVSampleFormat fmt)
  22. {
  23. switch (fmt) {
  24. case AV_SAMPLE_FMT_U8:
  25. return 8;
  26. case AV_SAMPLE_FMT_S16:
  27. return 16;
  28. case AV_SAMPLE_FMT_S32:
  29. return 32;
  30. case AV_SAMPLE_FMT_S64:
  31. return 64;
  32. default:
  33. return 32;
  34. }
  35. }
  36. int SizeToNbSamples(int size, int bitsPerSample, int nbChannels)
  37. {
  38. return (size << 3) / bitsPerSample / nbChannels;
  39. }
  40. int NbSamplesToSize(int nbSamples, int bitsPerSample, int nbChannels)
  41. {
  42. return (nbSamples * bitsPerSample * nbChannels) >> 3;
  43. }
  44. bool FrameQueue::Init(int channelNums, int sampleRate, AVSampleFormat fmt, int nbSamples)
  45. {
  46. _front = 0;
  47. _sampleRate = sampleRate;
  48. _fmt = fmt;
  49. _nbSamples = nbSamples;
  50. _usedLinesize = nbSamples * channelNums * (fmt == AV_SAMPLE_FMT_S16 ? 2 : 4);
  51. av_channel_layout_default(&_layout, channelNums);
  52. _queue.emplace(fmt, &_layout, sampleRate, nbSamples);
  53. return true;
  54. }
  55. Frame<MediaType::AUDIO> FrameQueue::Pop()
  56. {
  57. if (_queue.size() > 1) {
  58. auto frame = std::move(_queue.front());
  59. _queue.pop();
  60. return frame;
  61. }
  62. return Frame<MediaType::AUDIO>();
  63. }
  64. void FrameQueue::Push(uint8_t* data, int length)
  65. {
  66. if (length > _usedLinesize) { // 递归调用
  67. Push(data, length / 2);
  68. Push(data + length / 2, length / 2 + length % 2);
  69. return;
  70. }
  71. auto&& frame = _queue.back().frame;
  72. int secondLength = _front + length - _usedLinesize;
  73. if (secondLength <= 0) { // 第一段缓存是够用的
  74. memcpy(frame->data[0] + _front, data, length);
  75. _front += length;
  76. return;
  77. }
  78. // 第一段缓存不够用
  79. int firstLength = length - secondLength;
  80. if (firstLength > 0) {
  81. memcpy(frame->data[0] + _front, data, firstLength);
  82. }
  83. // 载入一段新缓存
  84. _queue.emplace(_fmt, &_layout, _sampleRate, _nbSamples);
  85. memcpy(_queue.back().frame->data[0], data + firstLength, secondLength);
  86. _front = secondLength;
  87. }
  88. bool Resampler::Open(int inChannelNums,
  89. int inSampleRate,
  90. AVSampleFormat inFmt,
  91. int outChannelNums,
  92. int outSampleRate,
  93. AVSampleFormat outFmt,
  94. int outNbSample)
  95. {
  96. Close();
  97. __CheckBool(_swrCtx = swr_alloc());
  98. AVChannelLayout tmpLayout;
  99. av_channel_layout_default(&tmpLayout, inChannelNums);
  100. av_opt_set_chlayout(_swrCtx, "in_chlayout", &tmpLayout, 0);
  101. av_opt_set_int(_swrCtx, "in_sample_rate", inSampleRate, 0);
  102. av_opt_set_sample_fmt(_swrCtx, "in_sample_fmt", inFmt, 0);
  103. __CheckBool(_fromQueue.Init(inChannelNums, inSampleRate, inFmt, inSampleRate / 100 * 2));
  104. av_channel_layout_default(&tmpLayout, outChannelNums);
  105. av_opt_set_chlayout(_swrCtx, "out_chlayout", &tmpLayout, 0);
  106. av_opt_set_int(_swrCtx, "out_sample_rate", outSampleRate, 0);
  107. av_opt_set_sample_fmt(_swrCtx, "out_sample_fmt", outFmt, 0);
  108. if (swr_init(_swrCtx) < 0) {
  109. Close();
  110. __DebugPrint("swr_init(_swrCtx) failed\n");
  111. return false;
  112. }
  113. __CheckBool(_toQueue.Init(outChannelNums, outSampleRate, outFmt, outNbSample));
  114. __CheckBool(_swrFrame = Frame<MediaType::AUDIO>::Alloc(outFmt,
  115. &tmpLayout,
  116. outSampleRate,
  117. outSampleRate / 100 * 2));
  118. return true;
  119. }
  120. void Resampler::Close()
  121. {
  122. Free(_swrCtx, [this] { swr_free(&_swrCtx); });
  123. Free(_swrFrame, [this] { av_frame_free(&_swrFrame); });
  124. }
  125. bool Resampler::Convert(uint8_t* data, int size)
  126. {
  127. std::vector<Frame<MediaType::AUDIO>> ret;
  128. if (data == nullptr) {
  129. return false;
  130. }
  131. _fromQueue.Push(data, size);
  132. for (; true;) { // 转换
  133. auto frame = _fromQueue.Pop();
  134. if (frame.frame == nullptr) {
  135. break;
  136. }
  137. __CheckBool(swr_convert(_swrCtx,
  138. _swrFrame->data,
  139. _swrFrame->nb_samples,
  140. (const uint8_t**) frame.frame->data,
  141. frame.frame->nb_samples)
  142. > 0);
  143. _toQueue.Push(_swrFrame->data[0], _swrFrame->linesize[0]);
  144. }
  145. return true;
  146. }
  147. AVFrame* AudioMixer::Convert(uint32_t index, uint8_t* inBuf, uint32_t size)
  148. {
  149. std::lock_guard<std::mutex> locker(_mutex);
  150. auto iter = _audioInputInfos.find(index);
  151. __CheckNullptr(iter != _audioInputInfos.end());
  152. // // 添加调试信息
  153. // static int debugCounter = 0;
  154. // if (++debugCounter % 100 == 0) {
  155. // qDebug() << "AudioMixer::Convert - Input size:" << size
  156. // << "bytes, Input index: " << index;
  157. // }
  158. __CheckNullptr(iter->second.resampler->Convert(inBuf, size));
  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. for (auto iter = _audioInputInfos.begin(); iter != _audioInputInfos.end(); ++iter) {
  190. auto&& frameQueue = iter->second.resampler->GetQueue();
  191. if (minSize > MAX_BUF_SIZE) {
  192. // __DebugPrint("Clear MAX_BUF_SIZE");
  193. for (int i = 0; i < minSize - 2; ++i) {
  194. frameQueue.Pop();
  195. }
  196. }
  197. auto frame = frameQueue.Pop();
  198. auto scale = iter->second.scale;
  199. auto writeStream = (float*) (_outputFrame->data[0]);
  200. auto readStream = (float*) (frame.frame->data[0]);
  201. iter->second.volume = readStream[0] * scale;
  202. if (iter == _audioInputInfos.begin()) {
  203. if (std::abs(scale - 1)
  204. < 0.01) { // 这种情况可以直接使用 memcpy 而不是下面那种低效率的逐个赋值
  205. memcpy(writeStream, readStream, _outputFrame->linesize[0]);
  206. continue;
  207. }
  208. // 要进行 scale, 只能逐个赋值
  209. // 所以这里要清零
  210. memset(writeStream, 0, _outputFrame->linesize[0]);
  211. }
  212. // 逐个计算赋值,优化音量处理
  213. for (int idx = 0; idx < _outputFrame->nb_samples; ++idx) {
  214. float sample = readStream[idx] * scale;
  215. // 使用软限制而不是硬限制,减少失真
  216. if (sample > 0.8f) {
  217. sample = 0.8f + (sample - 0.8f) * 0.3f; // 软限制
  218. } else if (sample < -0.8f) {
  219. sample = -0.8f + (sample + 0.8f) * 0.3f; // 软限制
  220. }
  221. writeStream[idx] = sample;
  222. }
  223. }
  224. return true;
  225. }
  226. AudioMixer::AudioMixer()
  227. : _inited(false)
  228. {}
  229. AudioMixer::~AudioMixer()
  230. {
  231. // delete out_buf;
  232. if (_inited) {
  233. Close();
  234. }
  235. }
  236. bool AudioMixer::AddAudioInput(uint32_t index,
  237. uint32_t sampleRate,
  238. uint32_t channels,
  239. uint32_t bitsPerSample,
  240. AVSampleFormat format)
  241. {
  242. std::lock_guard<std::mutex> locker(_mutex);
  243. __CheckBool(!_inited);
  244. // 根据index保存是否已经存在
  245. __CheckBool(_audioInputInfos.find(index) == _audioInputInfos.end());
  246. auto& filterInfo = _audioInputInfos[index];
  247. // 设置音频相关参数
  248. filterInfo.sampleRate = sampleRate;
  249. filterInfo.channels = channels;
  250. filterInfo.bitsPerSample = bitsPerSample;
  251. filterInfo.format = format;
  252. filterInfo.name = std::string("input") + std::to_string(index);
  253. return true;
  254. }
  255. bool AudioMixer::AddAudioOutput(const uint32_t sampleRate,
  256. const uint32_t channels,
  257. const uint32_t bitsPerSample,
  258. const AVSampleFormat format)
  259. {
  260. std::lock_guard<std::mutex> locker(_mutex);
  261. __CheckBool(!_inited);
  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. __CheckBool(filterInfo.resampler->Open(filterInfo.channels,
  280. filterInfo.sampleRate,
  281. filterInfo.format,
  282. _audioOutputInfo.channels,
  283. _audioOutputInfo.sampleRate,
  284. _audioOutputInfo.format,
  285. outFrameSize));
  286. }
  287. AVChannelLayout tmpLayout;
  288. av_channel_layout_default(&tmpLayout, _audioOutputInfo.channels);
  289. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  290. __CheckBool(_outputFrame = Frame<MediaType::AUDIO>::Alloc(_audioOutputInfo.format,
  291. &tmpLayout,
  292. _audioOutputInfo.sampleRate,
  293. outFrameSize));
  294. _inited = true;
  295. return true;
  296. }
  297. bool AudioMixer::Close()
  298. {
  299. if (!_inited) {
  300. return true;
  301. }
  302. _inited = false;
  303. std::lock_guard<std::mutex> locker(_mutex);
  304. _audioInputInfos.clear();
  305. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  306. _outFrameSize = 0;
  307. return true;
  308. }
  309. AudioMixer::AudioInfo* AudioMixer::GetInputInfo(uint32_t index)
  310. {
  311. auto iter = _audioInputInfos.find(index);
  312. return iter == _audioInputInfos.end() ? nullptr : &(iter->second);
  313. }