1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_device/include/audio_device_data_observer.h"
12 #include "rtc_base/checks.h"
13 #include "rtc_base/refcountedobject.h"
14 
15 namespace webrtc {
16 
17 namespace {
18 
19 // A wrapper over AudioDeviceModule that registers itself as AudioTransport
20 // callback and redirects the PCM data to AudioDeviceDataObserver callback.
21 class ADMWrapper : public AudioDeviceModule, public AudioTransport {
22  public:
ADMWrapper(const AudioLayer audio_layer,AudioDeviceDataObserver * observer)23   ADMWrapper(const AudioLayer audio_layer, AudioDeviceDataObserver* observer)
24       : impl_(AudioDeviceModule::Create(audio_layer)), observer_(observer) {
25     // Register self as the audio transport callback for underlying ADM impl.
26     auto res = impl_->RegisterAudioCallback(this);
27     is_valid_ = (impl_.get() != nullptr) && (res == 0);
28   }
~ADMWrapper()29   virtual ~ADMWrapper() {
30     audio_transport_ = nullptr;
31     observer_ = nullptr;
32   }
33 
34   // Make sure we have a valid ADM before returning it to user.
IsValid()35   bool IsValid() { return is_valid_; }
36 
37   // AudioTransport methods overrides.
RecordedDataIsAvailable(const void * audioSamples,const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samples_per_sec,const uint32_t total_delay_ms,const int32_t clockDrift,const uint32_t currentMicLevel,const bool keyPressed,uint32_t & newMicLevel)38   int32_t RecordedDataIsAvailable(const void* audioSamples,
39                                   const size_t nSamples,
40                                   const size_t nBytesPerSample,
41                                   const size_t nChannels,
42                                   const uint32_t samples_per_sec,
43                                   const uint32_t total_delay_ms,
44                                   const int32_t clockDrift,
45                                   const uint32_t currentMicLevel,
46                                   const bool keyPressed,
47                                   uint32_t& newMicLevel) override {
48     int32_t res = 0;
49     // Capture PCM data of locally captured audio.
50     if (observer_) {
51       observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
52                                nChannels, samples_per_sec);
53     }
54 
55     // Send to the actual audio transport.
56     if (audio_transport_) {
57       res = audio_transport_->RecordedDataIsAvailable(
58           audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
59           total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel);
60     }
61 
62     return res;
63   }
64 
NeedMorePlayData(const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samples_per_sec,void * audioSamples,size_t & nSamplesOut,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)65   int32_t NeedMorePlayData(const size_t nSamples,
66                            const size_t nBytesPerSample,
67                            const size_t nChannels,
68                            const uint32_t samples_per_sec,
69                            void* audioSamples,
70                            size_t& nSamplesOut,
71                            int64_t* elapsed_time_ms,
72                            int64_t* ntp_time_ms) override {
73     int32_t res = 0;
74     // Request data from audio transport.
75     if (audio_transport_) {
76       res = audio_transport_->NeedMorePlayData(
77           nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
78           nSamplesOut, elapsed_time_ms, ntp_time_ms);
79     }
80 
81     // Capture rendered data.
82     if (observer_) {
83       observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
84                               nChannels, samples_per_sec);
85     }
86 
87     return res;
88   }
89 
PushCaptureData(int voe_channel,const void * audio_data,int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames)90   void PushCaptureData(int voe_channel,
91                        const void* audio_data,
92                        int bits_per_sample,
93                        int sample_rate,
94                        size_t number_of_channels,
95                        size_t number_of_frames) override {
96     RTC_NOTREACHED();
97   }
98 
PullRenderData(int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames,void * audio_data,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)99   void PullRenderData(int bits_per_sample,
100                       int sample_rate,
101                       size_t number_of_channels,
102                       size_t number_of_frames,
103                       void* audio_data,
104                       int64_t* elapsed_time_ms,
105                       int64_t* ntp_time_ms) override {
106     RTC_NOTREACHED();
107   }
108 
109   // Override AudioDeviceModule's RegisterAudioCallback method to remember the
110   // actual audio transport (e.g.: voice engine).
RegisterAudioCallback(AudioTransport * audio_callback)111   int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
112     // Remember the audio callback to forward PCM data
113     audio_transport_ = audio_callback;
114     return 0;
115   }
116 
117   // AudioDeviceModule pass through method overrides.
ActiveAudioLayer(AudioLayer * audio_layer) const118   int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
119     return impl_->ActiveAudioLayer(audio_layer);
120   }
Init()121   int32_t Init() override { return impl_->Init(); }
Terminate()122   int32_t Terminate() override { return impl_->Terminate(); }
Initialized() const123   bool Initialized() const override { return impl_->Initialized(); }
PlayoutDevices()124   int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
RecordingDevices()125   int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])126   int32_t PlayoutDeviceName(uint16_t index,
127                             char name[kAdmMaxDeviceNameSize],
128                             char guid[kAdmMaxGuidSize]) override {
129     return impl_->PlayoutDeviceName(index, name, guid);
130   }
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])131   int32_t RecordingDeviceName(uint16_t index,
132                               char name[kAdmMaxDeviceNameSize],
133                               char guid[kAdmMaxGuidSize]) override {
134     return impl_->RecordingDeviceName(index, name, guid);
135   }
SetPlayoutDevice(uint16_t index)136   int32_t SetPlayoutDevice(uint16_t index) override {
137     return impl_->SetPlayoutDevice(index);
138   }
SetPlayoutDevice(WindowsDeviceType device)139   int32_t SetPlayoutDevice(WindowsDeviceType device) override {
140     return impl_->SetPlayoutDevice(device);
141   }
SetRecordingDevice(uint16_t index)142   int32_t SetRecordingDevice(uint16_t index) override {
143     return impl_->SetRecordingDevice(index);
144   }
SetRecordingDevice(WindowsDeviceType device)145   int32_t SetRecordingDevice(WindowsDeviceType device) override {
146     return impl_->SetRecordingDevice(device);
147   }
PlayoutIsAvailable(bool * available)148   int32_t PlayoutIsAvailable(bool* available) override {
149     return impl_->PlayoutIsAvailable(available);
150   }
InitPlayout()151   int32_t InitPlayout() override { return impl_->InitPlayout(); }
PlayoutIsInitialized() const152   bool PlayoutIsInitialized() const override {
153     return impl_->PlayoutIsInitialized();
154   }
RecordingIsAvailable(bool * available)155   int32_t RecordingIsAvailable(bool* available) override {
156     return impl_->RecordingIsAvailable(available);
157   }
InitRecording()158   int32_t InitRecording() override { return impl_->InitRecording(); }
RecordingIsInitialized() const159   bool RecordingIsInitialized() const override {
160     return impl_->RecordingIsInitialized();
161   }
StartPlayout()162   int32_t StartPlayout() override { return impl_->StartPlayout(); }
StopPlayout()163   int32_t StopPlayout() override { return impl_->StopPlayout(); }
Playing() const164   bool Playing() const override { return impl_->Playing(); }
StartRecording()165   int32_t StartRecording() override { return impl_->StartRecording(); }
StopRecording()166   int32_t StopRecording() override { return impl_->StopRecording(); }
Recording() const167   bool Recording() const override { return impl_->Recording(); }
SetAGC(bool enable)168   int32_t SetAGC(bool enable) override { return impl_->SetAGC(enable); }
AGC() const169   bool AGC() const override { return impl_->AGC(); }
InitSpeaker()170   int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
SpeakerIsInitialized() const171   bool SpeakerIsInitialized() const override {
172     return impl_->SpeakerIsInitialized();
173   }
InitMicrophone()174   int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
MicrophoneIsInitialized() const175   bool MicrophoneIsInitialized() const override {
176     return impl_->MicrophoneIsInitialized();
177   }
SpeakerVolumeIsAvailable(bool * available)178   int32_t SpeakerVolumeIsAvailable(bool* available) override {
179     return impl_->SpeakerVolumeIsAvailable(available);
180   }
SetSpeakerVolume(uint32_t volume)181   int32_t SetSpeakerVolume(uint32_t volume) override {
182     return impl_->SetSpeakerVolume(volume);
183   }
SpeakerVolume(uint32_t * volume) const184   int32_t SpeakerVolume(uint32_t* volume) const override {
185     return impl_->SpeakerVolume(volume);
186   }
MaxSpeakerVolume(uint32_t * max_volume) const187   int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
188     return impl_->MaxSpeakerVolume(max_volume);
189   }
MinSpeakerVolume(uint32_t * min_volume) const190   int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
191     return impl_->MinSpeakerVolume(min_volume);
192   }
MicrophoneVolumeIsAvailable(bool * available)193   int32_t MicrophoneVolumeIsAvailable(bool* available) override {
194     return impl_->MicrophoneVolumeIsAvailable(available);
195   }
SetMicrophoneVolume(uint32_t volume)196   int32_t SetMicrophoneVolume(uint32_t volume) override {
197     return impl_->SetMicrophoneVolume(volume);
198   }
MicrophoneVolume(uint32_t * volume) const199   int32_t MicrophoneVolume(uint32_t* volume) const override {
200     return impl_->MicrophoneVolume(volume);
201   }
MaxMicrophoneVolume(uint32_t * max_volume) const202   int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
203     return impl_->MaxMicrophoneVolume(max_volume);
204   }
MinMicrophoneVolume(uint32_t * min_volume) const205   int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
206     return impl_->MinMicrophoneVolume(min_volume);
207   }
SpeakerMuteIsAvailable(bool * available)208   int32_t SpeakerMuteIsAvailable(bool* available) override {
209     return impl_->SpeakerMuteIsAvailable(available);
210   }
SetSpeakerMute(bool enable)211   int32_t SetSpeakerMute(bool enable) override {
212     return impl_->SetSpeakerMute(enable);
213   }
SpeakerMute(bool * enabled) const214   int32_t SpeakerMute(bool* enabled) const override {
215     return impl_->SpeakerMute(enabled);
216   }
MicrophoneMuteIsAvailable(bool * available)217   int32_t MicrophoneMuteIsAvailable(bool* available) override {
218     return impl_->MicrophoneMuteIsAvailable(available);
219   }
SetMicrophoneMute(bool enable)220   int32_t SetMicrophoneMute(bool enable) override {
221     return impl_->SetMicrophoneMute(enable);
222   }
MicrophoneMute(bool * enabled) const223   int32_t MicrophoneMute(bool* enabled) const override {
224     return impl_->MicrophoneMute(enabled);
225   }
StereoPlayoutIsAvailable(bool * available) const226   int32_t StereoPlayoutIsAvailable(bool* available) const override {
227     return impl_->StereoPlayoutIsAvailable(available);
228   }
SetStereoPlayout(bool enable)229   int32_t SetStereoPlayout(bool enable) override {
230     return impl_->SetStereoPlayout(enable);
231   }
StereoPlayout(bool * enabled) const232   int32_t StereoPlayout(bool* enabled) const override {
233     return impl_->StereoPlayout(enabled);
234   }
StereoRecordingIsAvailable(bool * available) const235   int32_t StereoRecordingIsAvailable(bool* available) const override {
236     return impl_->StereoRecordingIsAvailable(available);
237   }
SetStereoRecording(bool enable)238   int32_t SetStereoRecording(bool enable) override {
239     return impl_->SetStereoRecording(enable);
240   }
StereoRecording(bool * enabled) const241   int32_t StereoRecording(bool* enabled) const override {
242     return impl_->StereoRecording(enabled);
243   }
PlayoutDelay(uint16_t * delay_ms) const244   int32_t PlayoutDelay(uint16_t* delay_ms) const override {
245     return impl_->PlayoutDelay(delay_ms);
246   }
BuiltInAECIsAvailable() const247   bool BuiltInAECIsAvailable() const override {
248     return impl_->BuiltInAECIsAvailable();
249   }
BuiltInAGCIsAvailable() const250   bool BuiltInAGCIsAvailable() const override {
251     return impl_->BuiltInAGCIsAvailable();
252   }
BuiltInNSIsAvailable() const253   bool BuiltInNSIsAvailable() const override {
254     return impl_->BuiltInNSIsAvailable();
255   }
EnableBuiltInAEC(bool enable)256   int32_t EnableBuiltInAEC(bool enable) override {
257     return impl_->EnableBuiltInAEC(enable);
258   }
EnableBuiltInAGC(bool enable)259   int32_t EnableBuiltInAGC(bool enable) override {
260     return impl_->EnableBuiltInAGC(enable);
261   }
EnableBuiltInNS(bool enable)262   int32_t EnableBuiltInNS(bool enable) override {
263     return impl_->EnableBuiltInNS(enable);
264   }
265 // Only supported on iOS.
266 #if defined(WEBRTC_IOS)
GetPlayoutAudioParameters(AudioParameters * params) const267   int GetPlayoutAudioParameters(AudioParameters* params) const override {
268     return impl_->GetPlayoutAudioParameters(params);
269   }
GetRecordAudioParameters(AudioParameters * params) const270   int GetRecordAudioParameters(AudioParameters* params) const override {
271     return impl_->GetRecordAudioParameters(params);
272   }
273 #endif  // WEBRTC_IOS
274 
275  protected:
276   rtc::scoped_refptr<AudioDeviceModule> impl_;
277   AudioDeviceDataObserver* observer_ = nullptr;
278   AudioTransport* audio_transport_ = nullptr;
279   bool is_valid_ = false;
280 };
281 
282 }  // namespace
283 
CreateAudioDeviceWithDataObserver(const AudioDeviceModule::AudioLayer audio_layer,AudioDeviceDataObserver * observer)284 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
285     const AudioDeviceModule::AudioLayer audio_layer,
286     AudioDeviceDataObserver* observer) {
287   rtc::scoped_refptr<ADMWrapper> audio_device(
288       new rtc::RefCountedObject<ADMWrapper>(audio_layer, observer));
289 
290   if (!audio_device->IsValid()) {
291     return nullptr;
292   }
293 
294   return audio_device;
295 }
296 
297 // TODO(bugs.webrtc.org/7306): deprecated.
CreateAudioDeviceWithDataObserver(const int32_t id,const AudioDeviceModule::AudioLayer audio_layer,AudioDeviceDataObserver * observer)298 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
299     const int32_t id,
300     const AudioDeviceModule::AudioLayer audio_layer,
301     AudioDeviceDataObserver* observer) {
302   return CreateAudioDeviceWithDataObserver(audio_layer, observer);
303 }
304 
305 }  // namespace webrtc
306