audio_mixer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. __CheckBool(swr_convert(_swrCtx,
  137. _swrFrame->data,
  138. _swrFrame->nb_samples,
  139. (const uint8_t**) frame.frame->data,
  140. frame.frame->nb_samples)
  141. > 0);
  142. _toQueue.Push(_swrFrame->data[0], _swrFrame->linesize[0]);
  143. }
  144. return true;
  145. }
  146. AVFrame* AudioMixer::Convert(uint32_t index, uint8_t* inBuf, uint32_t size)
  147. {
  148. std::lock_guard<std::mutex> locker(_mutex);
  149. auto iter = _audioInputInfos.find(index);
  150. __CheckNullptr(iter != _audioInputInfos.end());
  151. __CheckNullptr(iter->second.resampler->Convert(inBuf, size));
  152. return _AdjustVolume() ? _outputFrame : nullptr;
  153. }
  154. bool AudioMixer::_AdjustVolume()
  155. {
  156. // 检测所有流之间是不是相差太大了以及缓存的数据是不是太多了
  157. // 如果缓存的数据太多,直接将所有的队列删除同样的数据
  158. // 如果两个流直接数据相差太大,将多的那个减到和少的那个一样
  159. constexpr int MAX_DIFF = 10;
  160. constexpr int MAX_BUF_SIZE = 20;
  161. int minSize = INT_MAX;
  162. int maxSize = INT_MIN;
  163. FrameQueue* maxQueue = nullptr;
  164. #undef min
  165. for (auto&& iter : _audioInputInfos) {
  166. auto&& queue = iter.second.resampler->GetQueue();
  167. if (queue.IsEmpty()) {
  168. return false;
  169. }
  170. minSize = std::min(minSize, (int) queue.GetSize());
  171. if (maxSize < (int) queue.GetSize()) {
  172. maxSize = (int) queue.GetSize();
  173. maxQueue = &queue;
  174. }
  175. }
  176. if (maxSize - minSize > MAX_DIFF) {
  177. // __DebugPrint("Clear MAX_DIFF");
  178. for (int i = 0; i < maxSize - minSize; ++i) {
  179. maxQueue->Pop();
  180. }
  181. }
  182. for (auto iter = _audioInputInfos.begin(); iter != _audioInputInfos.end(); ++iter) {
  183. auto&& frameQueue = iter->second.resampler->GetQueue();
  184. if (minSize > MAX_BUF_SIZE) {
  185. // __DebugPrint("Clear MAX_BUF_SIZE");
  186. for (int i = 0; i < minSize - 2; ++i) {
  187. frameQueue.Pop();
  188. }
  189. }
  190. auto frame = frameQueue.Pop();
  191. auto scale = iter->second.scale;
  192. auto writeStream = (float*) (_outputFrame->data[0]);
  193. auto readStream = (float*) (frame.frame->data[0]);
  194. iter->second.volume = readStream[0] * scale;
  195. if (iter == _audioInputInfos.begin()) {
  196. if (std::abs(scale - 1)
  197. < 0.01) { // 这种情况可以直接使用 memcpy 而不是下面那种低效率的逐个赋值
  198. memcpy(writeStream, readStream, _outputFrame->linesize[0]);
  199. continue;
  200. }
  201. // 要进行 scale, 只能逐个赋值
  202. // 所以这里要清零
  203. memset(writeStream, 0, _outputFrame->linesize[0]);
  204. }
  205. // 逐个计算赋值
  206. for (int idx = 0; idx < _outputFrame->nb_samples; ++idx) {
  207. writeStream[idx] += readStream[idx] * scale;
  208. if (writeStream[idx] > 0.99) {
  209. writeStream[idx] = 0.99f;
  210. }
  211. }
  212. }
  213. return true;
  214. }
  215. AudioMixer::AudioMixer()
  216. : _inited(false)
  217. {}
  218. AudioMixer::~AudioMixer()
  219. {
  220. // delete out_buf;
  221. if (_inited) {
  222. Close();
  223. }
  224. }
  225. bool AudioMixer::AddAudioInput(uint32_t index,
  226. uint32_t sampleRate,
  227. uint32_t channels,
  228. uint32_t bitsPerSample,
  229. AVSampleFormat format)
  230. {
  231. std::lock_guard<std::mutex> locker(_mutex);
  232. __CheckBool(!_inited);
  233. // 根据index保存是否已经存在
  234. __CheckBool(_audioInputInfos.find(index) == _audioInputInfos.end());
  235. auto& filterInfo = _audioInputInfos[index];
  236. // 设置音频相关参数
  237. filterInfo.sampleRate = sampleRate;
  238. filterInfo.channels = channels;
  239. filterInfo.bitsPerSample = bitsPerSample;
  240. filterInfo.format = format;
  241. filterInfo.name = std::string("input") + std::to_string(index);
  242. return true;
  243. }
  244. bool AudioMixer::AddAudioOutput(const uint32_t sampleRate,
  245. const uint32_t channels,
  246. const uint32_t bitsPerSample,
  247. const AVSampleFormat format)
  248. {
  249. std::lock_guard<std::mutex> locker(_mutex);
  250. __CheckBool(!_inited);
  251. // 设置音频相关参数
  252. _audioOutputInfo.sampleRate = sampleRate;
  253. _audioOutputInfo.channels = channels;
  254. _audioOutputInfo.bitsPerSample = bitsPerSample;
  255. _audioOutputInfo.format = format;
  256. _audioOutputInfo.name = "output";
  257. return true;
  258. }
  259. bool AudioMixer::SetOutFrameSize(int outFrameSize)
  260. {
  261. if (_outFrameSize == outFrameSize) {
  262. return true;
  263. }
  264. _outFrameSize = outFrameSize;
  265. for (auto&& filterInfoPair : _audioInputInfos) {
  266. auto&& filterInfo = filterInfoPair.second;
  267. filterInfo.resampler = std::make_unique<Resampler>();
  268. __CheckBool(filterInfo.resampler->Open(filterInfo.channels,
  269. filterInfo.sampleRate,
  270. filterInfo.format,
  271. _audioOutputInfo.channels,
  272. _audioOutputInfo.sampleRate,
  273. _audioOutputInfo.format,
  274. outFrameSize));
  275. }
  276. AVChannelLayout tmpLayout;
  277. av_channel_layout_default(&tmpLayout, _audioOutputInfo.channels);
  278. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  279. __CheckBool(_outputFrame = Frame<MediaType::AUDIO>::Alloc(_audioOutputInfo.format,
  280. &tmpLayout,
  281. _audioOutputInfo.sampleRate,
  282. outFrameSize));
  283. _inited = true;
  284. return true;
  285. }
  286. bool AudioMixer::Close()
  287. {
  288. if (!_inited) {
  289. return true;
  290. }
  291. _inited = false;
  292. std::lock_guard<std::mutex> locker(_mutex);
  293. _audioInputInfos.clear();
  294. Free(_outputFrame, [this] { av_frame_free(&_outputFrame); });
  295. _outFrameSize = 0;
  296. return true;
  297. }
  298. AudioMixer::AudioInfo* AudioMixer::GetInputInfo(uint32_t index)
  299. {
  300. auto iter = _audioInputInfos.find(index);
  301. return iter == _audioInputInfos.end() ? nullptr : &(iter->second);
  302. }