UnityAudioTrackSource.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "pch.h"
  2. #include <common_audio/include/audio_util.h>
  3. #include <rtc_base/ref_counted_object.h>
  4. #include "UnityAudioTrackSource.h"
  5. namespace unity
  6. {
  7. namespace webrtc
  8. {
  9. rtc::scoped_refptr<UnityAudioTrackSource> UnityAudioTrackSource::Create()
  10. {
  11. rtc::scoped_refptr<UnityAudioTrackSource> source(new rtc::RefCountedObject<UnityAudioTrackSource>());
  12. return source;
  13. }
  14. rtc::scoped_refptr<UnityAudioTrackSource> UnityAudioTrackSource::Create(const cricket::AudioOptions& audio_options)
  15. {
  16. rtc::scoped_refptr<UnityAudioTrackSource> source(
  17. new rtc::RefCountedObject<UnityAudioTrackSource>(audio_options));
  18. return source;
  19. }
  20. void UnityAudioTrackSource::AddSink(AudioTrackSinkInterface* sink)
  21. {
  22. std::lock_guard<std::mutex> lock(_mutex);
  23. _arrSink.push_back(sink);
  24. }
  25. void UnityAudioTrackSource::RemoveSink(AudioTrackSinkInterface* sink)
  26. {
  27. std::lock_guard<std::mutex> lock(_mutex);
  28. auto i = std::find(_arrSink.begin(), _arrSink.end(), sink);
  29. if (i != _arrSink.end())
  30. _arrSink.erase(i);
  31. }
  32. void UnityAudioTrackSource::PushAudioData(
  33. const float* pAudioData, int nSampleRate, size_t nNumChannels, size_t nNumFrames)
  34. {
  35. RTC_DCHECK(pAudioData);
  36. RTC_DCHECK(nSampleRate);
  37. RTC_DCHECK(nNumChannels);
  38. RTC_DCHECK(nNumFrames);
  39. std::lock_guard<std::mutex> lock(_mutex);
  40. // eg. 80 for 8KHz and 160 for 16kHz
  41. size_t nNumFramesFor10ms = static_cast<size_t>(nSampleRate / 100);
  42. size_t nNumSamplesFor10ms = nNumFramesFor10ms * nNumChannels;
  43. constexpr size_t nBitPerSample = sizeof(int16_t) * 8;
  44. if (_sampleRate != nSampleRate || _numChannels != nNumChannels || _numFrames != nNumFrames)
  45. {
  46. _sampleRate = nSampleRate;
  47. _numChannels = nNumChannels;
  48. _numFrames = nNumFrames;
  49. _convertedAudioData.clear();
  50. _convertedAudioData.reserve(nNumSamplesFor10ms * 20);
  51. }
  52. for (size_t i = 0; i < nNumFrames; i++)
  53. _convertedAudioData.push_back(::webrtc::FloatToS16(pAudioData[i]));
  54. while (_convertedAudioData.size() >= nNumSamplesFor10ms)
  55. {
  56. for (auto sink : _arrSink)
  57. sink->OnData(_convertedAudioData.data(), nBitPerSample, nSampleRate, nNumChannels, nNumFramesFor10ms);
  58. _convertedAudioData.erase(_convertedAudioData.begin(), _convertedAudioData.begin() + nNumSamplesFor10ms);
  59. }
  60. }
  61. UnityAudioTrackSource::UnityAudioTrackSource() { }
  62. UnityAudioTrackSource::UnityAudioTrackSource(const cricket::AudioOptions& audio_options)
  63. : _options(audio_options)
  64. {
  65. }
  66. UnityAudioTrackSource::~UnityAudioTrackSource() { }
  67. } // end namespace webrtc
  68. } // end namespace unity