audio_mixer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. 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. __CheckBool(!_inited);
  235. // 根据index保存是否已经存在
  236. __CheckBool(_audioInputInfos.find(index) == _audioInputInfos.end());
  237. auto& filterInfo = _audioInputInfos[index];
  238. // 设置音频相关参数
  239. filterInfo.sampleRate = sampleRate;
  240. filterInfo.channels = channels;
  241. filterInfo.bitsPerSample = bitsPerSample;
  242. filterInfo.format = format;
  243. filterInfo.name = std::string("input") + std::to_string(index);
  244. return true;
  245. }
  246. bool AudioMixer::AddAudioOutput(const uint32_t sampleRate,
  247. const uint32_t channels,
  248. const uint32_t bitsPerSample,
  249. const AVSampleFormat format)
  250. {
  251. std::lock_guard<std::mutex> locker(_mutex);
  252. __CheckBool(!_inited);
  253. // 设置音频相关参数
  254. _audioOutputInfo.sampleRate = sampleRate;
  255. _audioOutputInfo.channels = channels;
  256. _audioOutputInfo.bitsPerSample = bitsPerSample;
  257. _audioOutputInfo.format = format;
  258. _audioOutputInfo.name = "output";
  259. return true;
  260. }
  261. bool AudioMixer::SetOutFrameSize(int outFrameSize)
  262. {
  263. if (_outFrameSize == outFrameSize) {
  264. return true;
  265. }
  266. _outFrameSize = outFrameSize;
  267. for (auto&& filterInfoPair : _audioInputInfos) {
  268. auto&& filterInfo = filterInfoPair.second;
  269. filterInfo.resampler = std::make_unique<Resampler>();
  270. __CheckBool(filterInfo.resampler->Open(filterInfo.channels,
  271. filterInfo.sampleRate,
  272. filterInfo.format,
  273. _audioOutputInfo.channels,
  274. _audioOutputInfo.sampleRate,
  275. _audioOutputInfo.format,
  276. outFrameSize));
  277. }
  278. AVChannelLayout tmpLayout;
  279. av_channel_layout_default(&tmpLayout, _audioOutputInfo.channels);
  280. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  281. __CheckBool(_outputFrame = Frame<MediaType::AUDIO>::Alloc(_audioOutputInfo.format,
  282. &tmpLayout,
  283. _audioOutputInfo.sampleRate,
  284. outFrameSize));
  285. _inited = true;
  286. return true;
  287. }
  288. bool AudioMixer::Close()
  289. {
  290. if (!_inited) {
  291. return true;
  292. }
  293. _inited = false;
  294. std::lock_guard<std::mutex> locker(_mutex);
  295. _audioInputInfos.clear();
  296. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  297. _outFrameSize = 0;
  298. return true;
  299. }
  300. AudioMixer::AudioInfo* AudioMixer::GetInputInfo(uint32_t index)
  301. {
  302. auto iter = _audioInputInfos.find(index);
  303. return iter == _audioInputInfos.end() ? nullptr : &(iter->second);
  304. }