DummyAudioDevice.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include <mutex>
  3. #include <unordered_map>
  4. #include <modules/audio_device/include/audio_device.h>
  5. #include <rtc_base/platform_thread.h>
  6. #include <rtc_base/task_queue.h>
  7. #include <rtc_base/task_utils/repeating_task.h>
  8. #include "WebRTCPlugin.h"
  9. namespace unity
  10. {
  11. namespace webrtc
  12. {
  13. using namespace ::webrtc;
  14. class UnityAudioTrackSource;
  15. class DummyAudioDevice : public webrtc::AudioDeviceModule
  16. {
  17. public:
  18. DummyAudioDevice(TaskQueueFactory* factory);
  19. ~DummyAudioDevice() override { Terminate(); }
  20. // webrtc::AudioDeviceModule
  21. // Retrieve the currently utilized audio layer
  22. int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override
  23. {
  24. *audioLayer = AudioDeviceModule::kDummyAudio;
  25. return 0;
  26. }
  27. // Full-duplex transportation of PCM audio
  28. int32_t RegisterAudioCallback(webrtc::AudioTransport* transport) override
  29. {
  30. std::lock_guard<std::mutex> lock(mutex_);
  31. RTC_DCHECK_EQ(!audio_transport_, !!transport);
  32. audio_transport_ = transport;
  33. return 0;
  34. }
  35. // Main initialization and termination
  36. int32_t Init() override;
  37. int32_t Terminate() override;
  38. virtual bool Initialized() const override
  39. {
  40. std::lock_guard<std::mutex> lock(mutex_);
  41. return initialized_;
  42. }
  43. // Device enumeration
  44. virtual int16_t PlayoutDevices() override { return 0; }
  45. virtual int16_t RecordingDevices() override { return 0; }
  46. virtual int32_t PlayoutDeviceName(
  47. uint16_t index, char name[webrtc::kAdmMaxDeviceNameSize], char guid[webrtc::kAdmMaxGuidSize]) override
  48. {
  49. return 0;
  50. }
  51. virtual int32_t RecordingDeviceName(
  52. uint16_t index, char name[webrtc::kAdmMaxDeviceNameSize], char guid[webrtc::kAdmMaxGuidSize]) override
  53. {
  54. return 0;
  55. }
  56. // Device selection
  57. virtual int32_t SetPlayoutDevice(uint16_t index) override { return 0; }
  58. virtual int32_t SetPlayoutDevice(WindowsDeviceType device) override { return 0; }
  59. virtual int32_t SetRecordingDevice(uint16_t index) override { return 0; }
  60. virtual int32_t SetRecordingDevice(WindowsDeviceType device) override { return 0; }
  61. // Audio transport initialization
  62. virtual int32_t PlayoutIsAvailable(bool* available) override { return 0; }
  63. virtual int32_t InitPlayout() override { return 0; }
  64. virtual bool PlayoutIsInitialized() const override { return false; }
  65. virtual int32_t RecordingIsAvailable(bool* available) override { return 0; }
  66. virtual int32_t InitRecording() override { return 0; }
  67. virtual bool RecordingIsInitialized() const override { return false; }
  68. virtual int32_t StartPlayout() override
  69. {
  70. std::lock_guard<std::mutex> lock(mutex_);
  71. playing_ = true;
  72. return 0;
  73. }
  74. virtual int32_t StopPlayout() override
  75. {
  76. std::lock_guard<std::mutex> lock(mutex_);
  77. playing_ = false;
  78. return 0;
  79. }
  80. virtual bool Playing() const override
  81. {
  82. std::lock_guard<std::mutex> lock(mutex_);
  83. return playing_;
  84. }
  85. virtual int32_t StartRecording() override
  86. {
  87. std::lock_guard<std::mutex> lock(mutex_);
  88. recording_ = true;
  89. return 0;
  90. }
  91. virtual int32_t StopRecording() override
  92. {
  93. std::lock_guard<std::mutex> lock(mutex_);
  94. recording_ = false;
  95. return 0;
  96. }
  97. virtual bool Recording() const override
  98. {
  99. std::lock_guard<std::mutex> lock(mutex_);
  100. return recording_;
  101. }
  102. // Audio mixer initialization
  103. virtual int32_t InitSpeaker() override { return 0; }
  104. virtual bool SpeakerIsInitialized() const override { return false; }
  105. virtual int32_t InitMicrophone() override { return 0; }
  106. virtual bool MicrophoneIsInitialized() const override { return false; }
  107. // Speaker volume controls
  108. virtual int32_t SpeakerVolumeIsAvailable(bool* available) override { return 0; }
  109. virtual int32_t SetSpeakerVolume(uint32_t volume) override { return 0; }
  110. virtual int32_t SpeakerVolume(uint32_t* volume) const override { return 0; }
  111. virtual int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override { return 0; }
  112. virtual int32_t MinSpeakerVolume(uint32_t* minVolume) const override { return 0; }
  113. // Microphone volume controls
  114. virtual int32_t MicrophoneVolumeIsAvailable(bool* available) override { return 0; }
  115. virtual int32_t SetMicrophoneVolume(uint32_t volume) override { return 0; }
  116. virtual int32_t MicrophoneVolume(uint32_t* volume) const override { return 0; }
  117. virtual int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override { return 0; }
  118. virtual int32_t MinMicrophoneVolume(uint32_t* minVolume) const override { return 0; }
  119. // Speaker mute control
  120. virtual int32_t SpeakerMuteIsAvailable(bool* available) override { return 0; }
  121. virtual int32_t SetSpeakerMute(bool enable) override { return 0; }
  122. virtual int32_t SpeakerMute(bool* enabled) const override { return 0; }
  123. // Microphone mute control
  124. virtual int32_t MicrophoneMuteIsAvailable(bool* available) override { return 0; }
  125. virtual int32_t SetMicrophoneMute(bool enable) override { return 0; }
  126. virtual int32_t MicrophoneMute(bool* enabled) const override { return 0; }
  127. // Stereo support
  128. virtual int32_t StereoPlayoutIsAvailable(bool* available) const override { return 0; }
  129. virtual int32_t SetStereoPlayout(bool enable) override { return 0; }
  130. virtual int32_t StereoPlayout(bool* enabled) const override { return 0; }
  131. virtual int32_t StereoRecordingIsAvailable(bool* available) const override
  132. {
  133. *available = true;
  134. return 0;
  135. }
  136. virtual int32_t SetStereoRecording(bool enable) override { return 0; }
  137. virtual int32_t StereoRecording(bool* enabled) const override
  138. {
  139. *enabled = true;
  140. return 0;
  141. }
  142. // Playout delay
  143. virtual int32_t PlayoutDelay(uint16_t* delayMS) const override { return 0; }
  144. // Only supported on Android.
  145. virtual bool BuiltInAECIsAvailable() const override { return false; }
  146. virtual bool BuiltInAGCIsAvailable() const override { return false; }
  147. virtual bool BuiltInNSIsAvailable() const override { return false; }
  148. // Enables the built-in audio effects. Only supported on Android.
  149. virtual int32_t EnableBuiltInAEC(bool enable) override { return 0; }
  150. virtual int32_t EnableBuiltInAGC(bool enable) override { return 0; }
  151. virtual int32_t EnableBuiltInNS(bool enable) override { return 0; }
  152. #if defined(WEBRTC_IOS)
  153. virtual int GetPlayoutAudioParameters(webrtc::AudioParameters* params) const override { return 0; }
  154. virtual int GetRecordAudioParameters(webrtc::AudioParameters* params) const override { return 0; }
  155. #endif
  156. private:
  157. void ProcessAudio();
  158. bool PlayoutThreadProcess();
  159. const int32_t kFrameLengthMs = 10;
  160. const int32_t kBytesPerSample = 2;
  161. const size_t kChannels = 2;
  162. const int32_t kSamplingRate = 48000;
  163. const size_t kSamplesPerFrame = static_cast<size_t>(kSamplingRate * kFrameLengthMs / 1000);
  164. std::vector<int16_t> audio_data;
  165. std::unique_ptr<rtc::TaskQueue> taskQueue_;
  166. RepeatingTaskHandle task_;
  167. std::atomic<bool> initialized_ { false };
  168. std::atomic<bool> playing_ { false };
  169. std::atomic<bool> recording_ { false };
  170. mutable std::mutex mutex_;
  171. webrtc::AudioTransport* audio_transport_ { nullptr };
  172. TaskQueueFactory* tackQueueFactory_;
  173. };
  174. } // end namespace webrtc
  175. } // end namespace unity