audio_mixer.cpp 11 KB

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