audio_mixer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. __CheckBool(_swrCtx = swr_alloc());
  99. AVChannelLayout tmpLayout;
  100. av_channel_layout_default(&tmpLayout, inChannelNums);
  101. av_opt_set_chlayout(_swrCtx, "in_chlayout", &tmpLayout, 0);
  102. av_opt_set_int(_swrCtx, "in_sample_rate", inSampleRate, 0);
  103. av_opt_set_sample_fmt(_swrCtx, "in_sample_fmt", inFmt, 0);
  104. __CheckBool(_fromQueue.Init(inChannelNums, inSampleRate, inFmt, inSampleRate / 100 * 2));
  105. av_channel_layout_default(&tmpLayout, outChannelNums);
  106. av_opt_set_chlayout(_swrCtx, "out_chlayout", &tmpLayout, 0);
  107. av_opt_set_int(_swrCtx, "out_sample_rate", outSampleRate, 0);
  108. av_opt_set_sample_fmt(_swrCtx, "out_sample_fmt", outFmt, 0);
  109. if (swr_init(_swrCtx) < 0) {
  110. Close();
  111. __DebugPrint("swr_init(_swrCtx) failed\n");
  112. return false;
  113. }
  114. __CheckBool(_toQueue.Init(outChannelNums, outSampleRate, outFmt, outNbSample));
  115. __CheckBool(_swrFrame = Frame<MediaType::AUDIO>::Alloc(outFmt,
  116. &tmpLayout,
  117. outSampleRate,
  118. outSampleRate / 100 * 2));
  119. return true;
  120. }
  121. void Resampler::Close()
  122. {
  123. Free(_swrCtx, [this] { swr_free(&_swrCtx); });
  124. Free(_swrFrame, [this] { av_frame_free(&_swrFrame); });
  125. }
  126. bool Resampler::Convert(uint8_t* data, int size)
  127. {
  128. std::vector<Frame<MediaType::AUDIO>> ret;
  129. if (data == nullptr) {
  130. return false;
  131. }
  132. _fromQueue.Push(data, size);
  133. for (; true;) { // 转换
  134. auto frame = _fromQueue.Pop();
  135. if (frame.frame == nullptr) {
  136. break;
  137. }
  138. int ret = swr_convert(_swrCtx,
  139. _swrFrame->data,
  140. _swrFrame->nb_samples,
  141. (const uint8_t**) frame.frame->data,
  142. frame.frame->nb_samples);
  143. if (ret < 0) {
  144. char buffer[AV_ERROR_MAX_STRING_SIZE];
  145. av_strerror(ret, buffer, sizeof(buffer));
  146. qDebug() << "swr_convert ----------" << ret << QString::fromLatin1(buffer);
  147. break;
  148. }
  149. _toQueue.Push(_swrFrame->data[0], _swrFrame->linesize[0]);
  150. }
  151. return true;
  152. }
  153. AVFrame* AudioMixer::Convert(uint32_t index, uint8_t* inBuf, uint32_t size)
  154. {
  155. std::lock_guard<std::mutex> locker(_mutex);
  156. auto iter = _audioInputInfos.find(index);
  157. __CheckNullptr(iter != _audioInputInfos.end());
  158. // // 添加调试信息
  159. // static int debugCounter = 0;
  160. // if (++debugCounter % 100 == 0) {
  161. // qDebug() << "AudioMixer::Convert - Input size:" << size << "bytes, Input index: " << index;
  162. // }
  163. __CheckNullptr(iter->second.resampler->Convert(inBuf, size));
  164. return _AdjustVolume() ? _outputFrame : nullptr;
  165. }
  166. bool AudioMixer::_AdjustVolume()
  167. {
  168. // 检测所有流之间是不是相差太大了以及缓存的数据是不是太多了
  169. // 如果缓存的数据太多,直接将所有的队列删除同样的数据
  170. // 如果两个流直接数据相差太大,将多的那个减到和少的那个一样
  171. constexpr int MAX_DIFF = 10;
  172. constexpr int MAX_BUF_SIZE = 20;
  173. int minSize = INT_MAX;
  174. int maxSize = INT_MIN;
  175. FrameQueue* maxQueue = nullptr;
  176. #undef min
  177. for (auto&& iter : _audioInputInfos) {
  178. auto&& queue = iter.second.resampler->GetQueue();
  179. if (queue.IsEmpty()) {
  180. return false;
  181. }
  182. minSize = std::min(minSize, (int) queue.GetSize());
  183. if (maxSize < (int) queue.GetSize()) {
  184. maxSize = (int) queue.GetSize();
  185. maxQueue = &queue;
  186. }
  187. }
  188. if (maxSize - minSize > MAX_DIFF) {
  189. // __DebugPrint("Clear MAX_DIFF");
  190. for (int i = 0; i < maxSize - minSize; ++i) {
  191. maxQueue->Pop();
  192. }
  193. }
  194. auto writeStream = (float*)(_outputFrame->data[0]);
  195. memset(writeStream, 0, _outputFrame->linesize[0]);
  196. for (auto iter = _audioInputInfos.begin(); iter != _audioInputInfos.end(); ++iter) {
  197. auto&& frameQueue = iter->second.resampler->GetQueue();
  198. if (minSize > MAX_BUF_SIZE) {
  199. // __DebugPrint("Clear MAX_BUF_SIZE");
  200. for (int i = 0; i < minSize - 2; ++i) {
  201. frameQueue.Pop();
  202. }
  203. }
  204. auto frame = frameQueue.Pop();
  205. auto scale = iter->second.scale;
  206. auto readStream = (float*) (frame.frame->data[0]);
  207. iter->second.volume = readStream[0] * scale;
  208. // 逐个计算赋值,优化音量处理
  209. for (int idx = 0; idx < _outputFrame->nb_samples; ++idx) {
  210. float sample = readStream[idx] * scale;
  211. // 使用软限制而不是硬限制,减少失真
  212. if (sample > 0.8f) {
  213. sample = 0.8f + (sample - 0.8f) * 0.3f; // 软限制
  214. } else if (sample < -0.8f) {
  215. sample = -0.8f + (sample + 0.8f) * 0.3f; // 软限制
  216. }
  217. writeStream[idx] += sample;
  218. }
  219. }
  220. return true;
  221. }
  222. AudioMixer::AudioMixer()
  223. : _inited(false)
  224. {}
  225. AudioMixer::~AudioMixer()
  226. {
  227. // delete out_buf;
  228. if (_inited) {
  229. Close();
  230. }
  231. }
  232. bool AudioMixer::AddAudioInput(uint32_t index,
  233. uint32_t sampleRate,
  234. uint32_t channels,
  235. uint32_t bitsPerSample,
  236. AVSampleFormat format)
  237. {
  238. std::lock_guard<std::mutex> locker(_mutex);
  239. __CheckBool(!_inited);
  240. // 根据index保存是否已经存在
  241. __CheckBool(_audioInputInfos.find(index) == _audioInputInfos.end());
  242. auto& filterInfo = _audioInputInfos[index];
  243. // 设置音频相关参数
  244. filterInfo.sampleRate = sampleRate;
  245. filterInfo.channels = channels;
  246. filterInfo.bitsPerSample = bitsPerSample;
  247. filterInfo.format = format;
  248. filterInfo.name = std::string("input") + std::to_string(index);
  249. return true;
  250. }
  251. bool AudioMixer::AddAudioOutput(const uint32_t sampleRate,
  252. const uint32_t channels,
  253. const uint32_t bitsPerSample,
  254. const AVSampleFormat format)
  255. {
  256. std::lock_guard<std::mutex> locker(_mutex);
  257. __CheckBool(!_inited);
  258. // 设置音频相关参数
  259. _audioOutputInfo.sampleRate = sampleRate;
  260. _audioOutputInfo.channels = channels;
  261. _audioOutputInfo.bitsPerSample = bitsPerSample;
  262. _audioOutputInfo.format = format;
  263. _audioOutputInfo.name = "output";
  264. return true;
  265. }
  266. bool AudioMixer::SetOutFrameSize(int outFrameSize)
  267. {
  268. if (_outFrameSize == outFrameSize) {
  269. return true;
  270. }
  271. _outFrameSize = outFrameSize;
  272. for (auto&& filterInfoPair : _audioInputInfos) {
  273. auto&& filterInfo = filterInfoPair.second;
  274. filterInfo.resampler = std::make_unique<Resampler>();
  275. __CheckBool(filterInfo.resampler->Open(filterInfo.channels,
  276. filterInfo.sampleRate,
  277. filterInfo.format,
  278. _audioOutputInfo.channels,
  279. _audioOutputInfo.sampleRate,
  280. _audioOutputInfo.format,
  281. outFrameSize));
  282. }
  283. AVChannelLayout tmpLayout;
  284. av_channel_layout_default(&tmpLayout, _audioOutputInfo.channels);
  285. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  286. __CheckBool(_outputFrame = Frame<MediaType::AUDIO>::Alloc(_audioOutputInfo.format,
  287. &tmpLayout,
  288. _audioOutputInfo.sampleRate,
  289. outFrameSize));
  290. _inited = true;
  291. return true;
  292. }
  293. bool AudioMixer::Close()
  294. {
  295. if (!_inited) {
  296. return true;
  297. }
  298. _inited = false;
  299. std::lock_guard<std::mutex> locker(_mutex);
  300. _audioInputInfos.clear();
  301. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  302. _outFrameSize = 0;
  303. return true;
  304. }
  305. AudioMixer::AudioInfo* AudioMixer::GetInputInfo(uint32_t index)
  306. {
  307. auto iter = _audioInputInfos.find(index);
  308. return iter == _audioInputInfos.end() ? nullptr : &(iter->second);
  309. }