• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
12 #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
13 
14 #include <android/log.h>
15 
16 #include "webrtc/base/checks.h"
17 #include "webrtc/base/thread_checker.h"
18 #include "webrtc/modules/audio_device/android/audio_manager.h"
19 #include "webrtc/modules/audio_device/audio_device_generic.h"
20 #include "webrtc/system_wrappers/include/trace.h"
21 
22 #define TAG "AudioDeviceTemplate"
23 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
24 
25 namespace webrtc {
26 
27 // InputType/OutputType can be any class that implements the capturing/rendering
28 // part of the AudioDeviceGeneric API.
29 // Construction and destruction must be done on one and the same thread. Each
30 // internal implementation of InputType and OutputType will RTC_DCHECK if that
31 // is not the case. All implemented methods must also be called on the same
32 // thread. See comments in each InputType/OutputType class for more info.
33 // It is possible to call the two static methods (SetAndroidAudioDeviceObjects
34 // and ClearAndroidAudioDeviceObjects) from a different thread but both will
35 // RTC_CHECK that the calling thread is attached to a Java VM.
36 
37 template <class InputType, class OutputType>
38 class AudioDeviceTemplate : public AudioDeviceGeneric {
39  public:
AudioDeviceTemplate(AudioDeviceModule::AudioLayer audio_layer,AudioManager * audio_manager)40   AudioDeviceTemplate(AudioDeviceModule::AudioLayer audio_layer,
41                       AudioManager* audio_manager)
42       : audio_layer_(audio_layer),
43         audio_manager_(audio_manager),
44         output_(audio_manager_),
45         input_(audio_manager_),
46         initialized_(false) {
47     RTC_CHECK(audio_manager);
48     audio_manager_->SetActiveAudioLayer(audio_layer);
49   }
50 
~AudioDeviceTemplate()51   virtual ~AudioDeviceTemplate() {
52   }
53 
ActiveAudioLayer(AudioDeviceModule::AudioLayer & audioLayer)54   int32_t ActiveAudioLayer(
55       AudioDeviceModule::AudioLayer& audioLayer) const override {
56     audioLayer = audio_layer_;
57     return 0;
58   }
59 
Init()60   int32_t Init() override {
61     RTC_DCHECK(thread_checker_.CalledOnValidThread());
62     RTC_DCHECK(!initialized_);
63     if (!audio_manager_->Init())
64       return -1;
65     if (output_.Init() != 0) {
66       audio_manager_->Close();
67       return -1;
68     }
69     if (input_.Init() != 0) {
70       output_.Terminate();
71       audio_manager_->Close();
72       return -1;
73     }
74     initialized_ = true;
75     return 0;
76   }
77 
Terminate()78   int32_t Terminate() override {
79     RTC_DCHECK(thread_checker_.CalledOnValidThread());
80     int32_t err = input_.Terminate();
81     err |= output_.Terminate();
82     err |= !audio_manager_->Close();
83     initialized_ = false;
84     RTC_DCHECK_EQ(err, 0);
85     return err;
86   }
87 
Initialized()88   bool Initialized() const override {
89     RTC_DCHECK(thread_checker_.CalledOnValidThread());
90     return initialized_;
91   }
92 
PlayoutDevices()93   int16_t PlayoutDevices() override {
94     return 1;
95   }
96 
RecordingDevices()97   int16_t RecordingDevices() override {
98     return 1;
99   }
100 
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])101   int32_t PlayoutDeviceName(
102       uint16_t index,
103       char name[kAdmMaxDeviceNameSize],
104       char guid[kAdmMaxGuidSize]) override {
105     FATAL() << "Should never be called";
106     return -1;
107   }
108 
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])109   int32_t RecordingDeviceName(
110       uint16_t index,
111       char name[kAdmMaxDeviceNameSize],
112       char guid[kAdmMaxGuidSize]) override {
113     FATAL() << "Should never be called";
114     return -1;
115   }
116 
SetPlayoutDevice(uint16_t index)117   int32_t SetPlayoutDevice(uint16_t index) override {
118     // OK to use but it has no effect currently since device selection is
119     // done using Andoid APIs instead.
120     return 0;
121   }
122 
SetPlayoutDevice(AudioDeviceModule::WindowsDeviceType device)123   int32_t SetPlayoutDevice(
124       AudioDeviceModule::WindowsDeviceType device) override {
125     FATAL() << "Should never be called";
126     return -1;
127   }
128 
SetRecordingDevice(uint16_t index)129   int32_t SetRecordingDevice(uint16_t index) override {
130     // OK to use but it has no effect currently since device selection is
131     // done using Andoid APIs instead.
132     return 0;
133   }
134 
SetRecordingDevice(AudioDeviceModule::WindowsDeviceType device)135   int32_t SetRecordingDevice(
136       AudioDeviceModule::WindowsDeviceType device) override {
137     FATAL() << "Should never be called";
138     return -1;
139   }
140 
PlayoutIsAvailable(bool & available)141   int32_t PlayoutIsAvailable(bool& available) override {
142     available = true;
143     return 0;
144   }
145 
InitPlayout()146   int32_t InitPlayout() override {
147     return output_.InitPlayout();
148   }
149 
PlayoutIsInitialized()150   bool PlayoutIsInitialized() const override {
151     return output_.PlayoutIsInitialized();
152   }
153 
RecordingIsAvailable(bool & available)154   int32_t RecordingIsAvailable(bool& available) override {
155     available = true;
156     return 0;
157   }
158 
InitRecording()159   int32_t InitRecording() override {
160     return input_.InitRecording();
161   }
162 
RecordingIsInitialized()163   bool RecordingIsInitialized() const override {
164     return input_.RecordingIsInitialized();
165   }
166 
StartPlayout()167   int32_t StartPlayout() override {
168     if (!audio_manager_->IsCommunicationModeEnabled()) {
169       ALOGW("The application should use MODE_IN_COMMUNICATION audio mode!");
170     }
171     return output_.StartPlayout();
172   }
173 
StopPlayout()174   int32_t StopPlayout() override {
175     // Avoid using audio manger (JNI/Java cost) if playout was inactive.
176     if (!Playing())
177       return 0;
178     int32_t err = output_.StopPlayout();
179     return err;
180   }
181 
Playing()182   bool Playing() const override {
183     return output_.Playing();
184   }
185 
StartRecording()186   int32_t StartRecording() override {
187     if (!audio_manager_->IsCommunicationModeEnabled()) {
188       ALOGW("The application should use MODE_IN_COMMUNICATION audio mode!");
189     }
190     return input_.StartRecording();
191   }
192 
StopRecording()193   int32_t StopRecording() override {
194     // Avoid using audio manger (JNI/Java cost) if recording was inactive.
195     if (!Recording())
196       return 0;
197     int32_t err = input_.StopRecording();
198     return err;
199   }
200 
Recording()201   bool Recording() const override {
202     return input_.Recording() ;
203   }
204 
SetAGC(bool enable)205   int32_t SetAGC(bool enable) override {
206     if (enable) {
207       FATAL() << "Should never be called";
208     }
209     return -1;
210   }
211 
AGC()212   bool AGC() const override {
213     return false;
214   }
215 
SetWaveOutVolume(uint16_t volumeLeft,uint16_t volumeRight)216   int32_t SetWaveOutVolume(
217       uint16_t volumeLeft, uint16_t volumeRight) override {
218      FATAL() << "Should never be called";
219     return -1;
220   }
221 
WaveOutVolume(uint16_t & volumeLeft,uint16_t & volumeRight)222   int32_t WaveOutVolume(
223       uint16_t& volumeLeft, uint16_t& volumeRight) const override {
224     FATAL() << "Should never be called";
225     return -1;
226   }
227 
InitSpeaker()228   int32_t InitSpeaker() override {
229     return 0;
230   }
231 
SpeakerIsInitialized()232   bool SpeakerIsInitialized() const override {
233     return true;
234   }
235 
InitMicrophone()236   int32_t InitMicrophone() override {
237     return 0;
238   }
239 
MicrophoneIsInitialized()240   bool MicrophoneIsInitialized() const override {
241     return true;
242   }
243 
SpeakerVolumeIsAvailable(bool & available)244   int32_t SpeakerVolumeIsAvailable(bool& available) override {
245     return output_.SpeakerVolumeIsAvailable(available);
246   }
247 
SetSpeakerVolume(uint32_t volume)248   int32_t SetSpeakerVolume(uint32_t volume) override {
249     return output_.SetSpeakerVolume(volume);
250   }
251 
SpeakerVolume(uint32_t & volume)252   int32_t SpeakerVolume(uint32_t& volume) const override {
253     return output_.SpeakerVolume(volume);
254   }
255 
MaxSpeakerVolume(uint32_t & maxVolume)256   int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override {
257     return output_.MaxSpeakerVolume(maxVolume);
258   }
259 
MinSpeakerVolume(uint32_t & minVolume)260   int32_t MinSpeakerVolume(uint32_t& minVolume) const override {
261     return output_.MinSpeakerVolume(minVolume);
262   }
263 
SpeakerVolumeStepSize(uint16_t & stepSize)264   int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const override {
265     FATAL() << "Should never be called";
266     return -1;
267   }
268 
MicrophoneVolumeIsAvailable(bool & available)269   int32_t MicrophoneVolumeIsAvailable(bool& available) override{
270     available = false;
271     FATAL() << "Should never be called";
272     return -1;
273   }
274 
SetMicrophoneVolume(uint32_t volume)275   int32_t SetMicrophoneVolume(uint32_t volume) override {
276     FATAL() << "Should never be called";
277     return -1;
278   }
279 
MicrophoneVolume(uint32_t & volume)280   int32_t MicrophoneVolume(uint32_t& volume) const override {
281     FATAL() << "Should never be called";
282     return -1;
283   }
284 
MaxMicrophoneVolume(uint32_t & maxVolume)285   int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override {
286     FATAL() << "Should never be called";
287     return -1;
288   }
289 
MinMicrophoneVolume(uint32_t & minVolume)290   int32_t MinMicrophoneVolume(uint32_t& minVolume) const override {
291     FATAL() << "Should never be called";
292     return -1;
293   }
294 
MicrophoneVolumeStepSize(uint16_t & stepSize)295   int32_t MicrophoneVolumeStepSize(uint16_t& stepSize) const override {
296     FATAL() << "Should never be called";
297     return -1;
298   }
299 
SpeakerMuteIsAvailable(bool & available)300   int32_t SpeakerMuteIsAvailable(bool& available) override {
301     FATAL() << "Should never be called";
302     return -1;
303   }
304 
SetSpeakerMute(bool enable)305   int32_t SetSpeakerMute(bool enable) override {
306     FATAL() << "Should never be called";
307     return -1;
308   }
309 
SpeakerMute(bool & enabled)310   int32_t SpeakerMute(bool& enabled) const override {
311     FATAL() << "Should never be called";
312     return -1;
313   }
314 
MicrophoneMuteIsAvailable(bool & available)315   int32_t MicrophoneMuteIsAvailable(bool& available) override {
316     FATAL() << "Not implemented";
317     return -1;
318   }
319 
SetMicrophoneMute(bool enable)320   int32_t SetMicrophoneMute(bool enable) override {
321     FATAL() << "Not implemented";
322     return -1;
323   }
324 
MicrophoneMute(bool & enabled)325   int32_t MicrophoneMute(bool& enabled) const override {
326     FATAL() << "Not implemented";
327     return -1;
328   }
329 
MicrophoneBoostIsAvailable(bool & available)330   int32_t MicrophoneBoostIsAvailable(bool& available) override {
331     FATAL() << "Should never be called";
332     return -1;
333   }
334 
SetMicrophoneBoost(bool enable)335   int32_t SetMicrophoneBoost(bool enable) override {
336     FATAL() << "Should never be called";
337     return -1;
338   }
339 
MicrophoneBoost(bool & enabled)340   int32_t MicrophoneBoost(bool& enabled) const override {
341     FATAL() << "Should never be called";
342     return -1;
343   }
344 
StereoPlayoutIsAvailable(bool & available)345   int32_t StereoPlayoutIsAvailable(bool& available) override {
346     available = false;
347     return 0;
348   }
349 
350   // TODO(henrika): add support.
SetStereoPlayout(bool enable)351   int32_t SetStereoPlayout(bool enable) override {
352     return -1;
353   }
354 
355   // TODO(henrika): add support.
StereoPlayout(bool & enabled)356   int32_t StereoPlayout(bool& enabled) const override {
357     enabled = false;
358     FATAL() << "Should never be called";
359     return -1;
360   }
361 
StereoRecordingIsAvailable(bool & available)362   int32_t StereoRecordingIsAvailable(bool& available) override {
363     available = false;
364     return 0;
365   }
366 
SetStereoRecording(bool enable)367   int32_t SetStereoRecording(bool enable) override {
368     return -1;
369   }
370 
StereoRecording(bool & enabled)371   int32_t StereoRecording(bool& enabled) const override {
372     enabled = false;
373     return 0;
374   }
375 
SetPlayoutBuffer(const AudioDeviceModule::BufferType type,uint16_t sizeMS)376   int32_t SetPlayoutBuffer(
377       const AudioDeviceModule::BufferType type, uint16_t sizeMS) override {
378     FATAL() << "Should never be called";
379     return -1;
380   }
381 
PlayoutBuffer(AudioDeviceModule::BufferType & type,uint16_t & sizeMS)382   int32_t PlayoutBuffer(
383       AudioDeviceModule::BufferType& type, uint16_t& sizeMS) const override {
384     FATAL() << "Should never be called";
385     return -1;
386   }
387 
PlayoutDelay(uint16_t & delay_ms)388   int32_t PlayoutDelay(uint16_t& delay_ms) const override {
389     // Best guess we can do is to use half of the estimated total delay.
390     delay_ms = audio_manager_->GetDelayEstimateInMilliseconds() / 2;
391     RTC_DCHECK_GT(delay_ms, 0);
392     return 0;
393   }
394 
RecordingDelay(uint16_t & delay_ms)395   int32_t RecordingDelay(uint16_t& delay_ms) const override {
396     // Best guess we can do is to use half of the estimated total delay.
397     delay_ms = audio_manager_->GetDelayEstimateInMilliseconds() / 2;
398     RTC_DCHECK_GT(delay_ms, 0);
399     return 0;
400   }
401 
CPULoad(uint16_t & load)402   int32_t CPULoad(uint16_t& load) const override {
403     FATAL() << "Should never be called";
404     return -1;
405   }
406 
PlayoutWarning()407   bool PlayoutWarning() const override {
408     return false;
409   }
410 
PlayoutError()411   bool PlayoutError() const override {
412     return false;
413   }
414 
RecordingWarning()415   bool RecordingWarning() const override {
416     return false;
417   }
418 
RecordingError()419   bool RecordingError() const override {
420     return false;
421   }
422 
ClearPlayoutWarning()423   void ClearPlayoutWarning() override {}
424 
ClearPlayoutError()425   void ClearPlayoutError() override {}
426 
ClearRecordingWarning()427   void ClearRecordingWarning() override {}
428 
ClearRecordingError()429   void ClearRecordingError() override {}
430 
AttachAudioBuffer(AudioDeviceBuffer * audioBuffer)431   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override {
432     output_.AttachAudioBuffer(audioBuffer);
433     input_.AttachAudioBuffer(audioBuffer);
434   }
435 
436   // TODO(henrika): remove
SetPlayoutSampleRate(const uint32_t samplesPerSec)437   int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec) override {
438     FATAL() << "Should never be called";
439     return -1;
440   }
441 
SetLoudspeakerStatus(bool enable)442   int32_t SetLoudspeakerStatus(bool enable) override {
443     FATAL() << "Should never be called";
444     return -1;
445   }
446 
GetLoudspeakerStatus(bool & enable)447   int32_t GetLoudspeakerStatus(bool& enable) const override {
448     FATAL() << "Should never be called";
449     return -1;
450   }
451 
452   // Returns true if the device both supports built in AEC and the device
453   // is not blacklisted.
BuiltInAECIsAvailable()454   bool BuiltInAECIsAvailable() const override {
455     return audio_manager_->IsAcousticEchoCancelerSupported();
456   }
457 
EnableBuiltInAEC(bool enable)458   int32_t EnableBuiltInAEC(bool enable) override {
459     RTC_CHECK(BuiltInAECIsAvailable()) << "HW AEC is not available";
460     return input_.EnableBuiltInAEC(enable);
461   }
462 
463   // Returns true if the device both supports built in AGC and the device
464   // is not blacklisted.
BuiltInAGCIsAvailable()465   bool BuiltInAGCIsAvailable() const override {
466     return audio_manager_->IsAutomaticGainControlSupported();
467   }
468 
EnableBuiltInAGC(bool enable)469   int32_t EnableBuiltInAGC(bool enable) override {
470     RTC_CHECK(BuiltInAGCIsAvailable()) << "HW AGC is not available";
471     return input_.EnableBuiltInAGC(enable);
472   }
473 
474   // Returns true if the device both supports built in NS and the device
475   // is not blacklisted.
BuiltInNSIsAvailable()476   bool BuiltInNSIsAvailable() const override {
477     return audio_manager_->IsNoiseSuppressorSupported();
478   }
479 
EnableBuiltInNS(bool enable)480   int32_t EnableBuiltInNS(bool enable) override {
481     RTC_CHECK(BuiltInNSIsAvailable()) << "HW NS is not available";
482     return input_.EnableBuiltInNS(enable);
483   }
484 
485  private:
486   rtc::ThreadChecker thread_checker_;
487 
488   // Local copy of the audio layer set during construction of the
489   // AudioDeviceModuleImpl instance. Read only value.
490   const AudioDeviceModule::AudioLayer audio_layer_;
491 
492   // Non-owning raw pointer to AudioManager instance given to use at
493   // construction. The real object is owned by AudioDeviceModuleImpl and the
494   // life time is the same as that of the AudioDeviceModuleImpl, hence there
495   // is no risk of reading a NULL pointer at any time in this class.
496   AudioManager* const audio_manager_;
497 
498   OutputType output_;
499 
500   InputType input_;
501 
502   bool initialized_;
503 };
504 
505 }  // namespace webrtc
506 
507 #endif  // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
508