• 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 MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
12 #define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
13 
14 #include "modules/audio_device/android/audio_manager.h"
15 #include "modules/audio_device/audio_device_generic.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/logging.h"
18 #include "rtc_base/thread_checker.h"
19 
20 namespace webrtc {
21 
22 // InputType/OutputType can be any class that implements the capturing/rendering
23 // part of the AudioDeviceGeneric API.
24 // Construction and destruction must be done on one and the same thread. Each
25 // internal implementation of InputType and OutputType will RTC_DCHECK if that
26 // is not the case. All implemented methods must also be called on the same
27 // thread. See comments in each InputType/OutputType class for more info.
28 // It is possible to call the two static methods (SetAndroidAudioDeviceObjects
29 // and ClearAndroidAudioDeviceObjects) from a different thread but both will
30 // RTC_CHECK that the calling thread is attached to a Java VM.
31 
32 template <class InputType, class OutputType>
33 class AudioDeviceTemplate : public AudioDeviceGeneric {
34  public:
AudioDeviceTemplate(AudioDeviceModule::AudioLayer audio_layer,AudioManager * audio_manager)35   AudioDeviceTemplate(AudioDeviceModule::AudioLayer audio_layer,
36                       AudioManager* audio_manager)
37       : audio_layer_(audio_layer),
38         audio_manager_(audio_manager),
39         output_(audio_manager_),
40         input_(audio_manager_),
41         initialized_(false) {
42     RTC_LOG(INFO) << __FUNCTION__;
43     RTC_CHECK(audio_manager);
44     audio_manager_->SetActiveAudioLayer(audio_layer);
45   }
46 
~AudioDeviceTemplate()47   virtual ~AudioDeviceTemplate() { RTC_LOG(INFO) << __FUNCTION__; }
48 
ActiveAudioLayer(AudioDeviceModule::AudioLayer & audioLayer)49   int32_t ActiveAudioLayer(
50       AudioDeviceModule::AudioLayer& audioLayer) const override {
51     RTC_LOG(INFO) << __FUNCTION__;
52     audioLayer = audio_layer_;
53     return 0;
54   }
55 
Init()56   InitStatus Init() override {
57     RTC_LOG(INFO) << __FUNCTION__;
58     RTC_DCHECK(thread_checker_.IsCurrent());
59     RTC_DCHECK(!initialized_);
60     if (!audio_manager_->Init()) {
61       return InitStatus::OTHER_ERROR;
62     }
63     if (output_.Init() != 0) {
64       audio_manager_->Close();
65       return InitStatus::PLAYOUT_ERROR;
66     }
67     if (input_.Init() != 0) {
68       output_.Terminate();
69       audio_manager_->Close();
70       return InitStatus::RECORDING_ERROR;
71     }
72     initialized_ = true;
73     return InitStatus::OK;
74   }
75 
Terminate()76   int32_t Terminate() override {
77     RTC_LOG(INFO) << __FUNCTION__;
78     RTC_DCHECK(thread_checker_.IsCurrent());
79     int32_t err = input_.Terminate();
80     err |= output_.Terminate();
81     err |= !audio_manager_->Close();
82     initialized_ = false;
83     RTC_DCHECK_EQ(err, 0);
84     return err;
85   }
86 
Initialized()87   bool Initialized() const override {
88     RTC_LOG(INFO) << __FUNCTION__;
89     RTC_DCHECK(thread_checker_.IsCurrent());
90     return initialized_;
91   }
92 
PlayoutDevices()93   int16_t PlayoutDevices() override {
94     RTC_LOG(INFO) << __FUNCTION__;
95     return 1;
96   }
97 
RecordingDevices()98   int16_t RecordingDevices() override {
99     RTC_LOG(INFO) << __FUNCTION__;
100     return 1;
101   }
102 
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])103   int32_t PlayoutDeviceName(uint16_t index,
104                             char name[kAdmMaxDeviceNameSize],
105                             char guid[kAdmMaxGuidSize]) override {
106     FATAL() << "Should never be called";
107     return -1;
108   }
109 
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])110   int32_t RecordingDeviceName(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     RTC_LOG(INFO) << __FUNCTION__;
121     return 0;
122   }
123 
SetPlayoutDevice(AudioDeviceModule::WindowsDeviceType device)124   int32_t SetPlayoutDevice(
125       AudioDeviceModule::WindowsDeviceType device) override {
126     FATAL() << "Should never be called";
127     return -1;
128   }
129 
SetRecordingDevice(uint16_t index)130   int32_t SetRecordingDevice(uint16_t index) override {
131     // OK to use but it has no effect currently since device selection is
132     // done using Andoid APIs instead.
133     RTC_LOG(INFO) << __FUNCTION__;
134     return 0;
135   }
136 
SetRecordingDevice(AudioDeviceModule::WindowsDeviceType device)137   int32_t SetRecordingDevice(
138       AudioDeviceModule::WindowsDeviceType device) override {
139     FATAL() << "Should never be called";
140     return -1;
141   }
142 
PlayoutIsAvailable(bool & available)143   int32_t PlayoutIsAvailable(bool& available) override {
144     RTC_LOG(INFO) << __FUNCTION__;
145     available = true;
146     return 0;
147   }
148 
InitPlayout()149   int32_t InitPlayout() override {
150     RTC_LOG(INFO) << __FUNCTION__;
151     return output_.InitPlayout();
152   }
153 
PlayoutIsInitialized()154   bool PlayoutIsInitialized() const override {
155     RTC_LOG(INFO) << __FUNCTION__;
156     return output_.PlayoutIsInitialized();
157   }
158 
RecordingIsAvailable(bool & available)159   int32_t RecordingIsAvailable(bool& available) override {
160     RTC_LOG(INFO) << __FUNCTION__;
161     available = true;
162     return 0;
163   }
164 
InitRecording()165   int32_t InitRecording() override {
166     RTC_LOG(INFO) << __FUNCTION__;
167     return input_.InitRecording();
168   }
169 
RecordingIsInitialized()170   bool RecordingIsInitialized() const override {
171     RTC_LOG(INFO) << __FUNCTION__;
172     return input_.RecordingIsInitialized();
173   }
174 
StartPlayout()175   int32_t StartPlayout() override {
176     RTC_LOG(INFO) << __FUNCTION__;
177     if (!audio_manager_->IsCommunicationModeEnabled()) {
178       RTC_LOG(WARNING)
179           << "The application should use MODE_IN_COMMUNICATION audio mode!";
180     }
181     return output_.StartPlayout();
182   }
183 
StopPlayout()184   int32_t StopPlayout() override {
185     // Avoid using audio manger (JNI/Java cost) if playout was inactive.
186     if (!Playing())
187       return 0;
188     RTC_LOG(INFO) << __FUNCTION__;
189     int32_t err = output_.StopPlayout();
190     return err;
191   }
192 
Playing()193   bool Playing() const override {
194     RTC_LOG(INFO) << __FUNCTION__;
195     return output_.Playing();
196   }
197 
StartRecording()198   int32_t StartRecording() override {
199     RTC_LOG(INFO) << __FUNCTION__;
200     if (!audio_manager_->IsCommunicationModeEnabled()) {
201       RTC_LOG(WARNING)
202           << "The application should use MODE_IN_COMMUNICATION audio mode!";
203     }
204     return input_.StartRecording();
205   }
206 
StopRecording()207   int32_t StopRecording() override {
208     // Avoid using audio manger (JNI/Java cost) if recording was inactive.
209     RTC_LOG(INFO) << __FUNCTION__;
210     if (!Recording())
211       return 0;
212     int32_t err = input_.StopRecording();
213     return err;
214   }
215 
Recording()216   bool Recording() const override { return input_.Recording(); }
217 
InitSpeaker()218   int32_t InitSpeaker() override {
219     RTC_LOG(INFO) << __FUNCTION__;
220     return 0;
221   }
222 
SpeakerIsInitialized()223   bool SpeakerIsInitialized() const override {
224     RTC_LOG(INFO) << __FUNCTION__;
225     return true;
226   }
227 
InitMicrophone()228   int32_t InitMicrophone() override {
229     RTC_LOG(INFO) << __FUNCTION__;
230     return 0;
231   }
232 
MicrophoneIsInitialized()233   bool MicrophoneIsInitialized() const override {
234     RTC_LOG(INFO) << __FUNCTION__;
235     return true;
236   }
237 
SpeakerVolumeIsAvailable(bool & available)238   int32_t SpeakerVolumeIsAvailable(bool& available) override {
239     RTC_LOG(INFO) << __FUNCTION__;
240     return output_.SpeakerVolumeIsAvailable(available);
241   }
242 
SetSpeakerVolume(uint32_t volume)243   int32_t SetSpeakerVolume(uint32_t volume) override {
244     RTC_LOG(INFO) << __FUNCTION__;
245     return output_.SetSpeakerVolume(volume);
246   }
247 
SpeakerVolume(uint32_t & volume)248   int32_t SpeakerVolume(uint32_t& volume) const override {
249     RTC_LOG(INFO) << __FUNCTION__;
250     return output_.SpeakerVolume(volume);
251   }
252 
MaxSpeakerVolume(uint32_t & maxVolume)253   int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override {
254     RTC_LOG(INFO) << __FUNCTION__;
255     return output_.MaxSpeakerVolume(maxVolume);
256   }
257 
MinSpeakerVolume(uint32_t & minVolume)258   int32_t MinSpeakerVolume(uint32_t& minVolume) const override {
259     RTC_LOG(INFO) << __FUNCTION__;
260     return output_.MinSpeakerVolume(minVolume);
261   }
262 
MicrophoneVolumeIsAvailable(bool & available)263   int32_t MicrophoneVolumeIsAvailable(bool& available) override {
264     available = false;
265     return -1;
266   }
267 
SetMicrophoneVolume(uint32_t volume)268   int32_t SetMicrophoneVolume(uint32_t volume) override {
269     FATAL() << "Should never be called";
270     return -1;
271   }
272 
MicrophoneVolume(uint32_t & volume)273   int32_t MicrophoneVolume(uint32_t& volume) const override {
274     FATAL() << "Should never be called";
275     return -1;
276   }
277 
MaxMicrophoneVolume(uint32_t & maxVolume)278   int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override {
279     FATAL() << "Should never be called";
280     return -1;
281   }
282 
MinMicrophoneVolume(uint32_t & minVolume)283   int32_t MinMicrophoneVolume(uint32_t& minVolume) const override {
284     FATAL() << "Should never be called";
285     return -1;
286   }
287 
SpeakerMuteIsAvailable(bool & available)288   int32_t SpeakerMuteIsAvailable(bool& available) override {
289     FATAL() << "Should never be called";
290     return -1;
291   }
292 
SetSpeakerMute(bool enable)293   int32_t SetSpeakerMute(bool enable) override {
294     FATAL() << "Should never be called";
295     return -1;
296   }
297 
SpeakerMute(bool & enabled)298   int32_t SpeakerMute(bool& enabled) const override {
299     FATAL() << "Should never be called";
300     return -1;
301   }
302 
MicrophoneMuteIsAvailable(bool & available)303   int32_t MicrophoneMuteIsAvailable(bool& available) override {
304     FATAL() << "Not implemented";
305     return -1;
306   }
307 
SetMicrophoneMute(bool enable)308   int32_t SetMicrophoneMute(bool enable) override {
309     FATAL() << "Not implemented";
310     return -1;
311   }
312 
MicrophoneMute(bool & enabled)313   int32_t MicrophoneMute(bool& enabled) const override {
314     FATAL() << "Not implemented";
315     return -1;
316   }
317 
318   // Returns true if the audio manager has been configured to support stereo
319   // and false otherwised. Default is mono.
StereoPlayoutIsAvailable(bool & available)320   int32_t StereoPlayoutIsAvailable(bool& available) override {
321     RTC_LOG(INFO) << __FUNCTION__;
322     available = audio_manager_->IsStereoPlayoutSupported();
323     return 0;
324   }
325 
SetStereoPlayout(bool enable)326   int32_t SetStereoPlayout(bool enable) override {
327     RTC_LOG(INFO) << __FUNCTION__;
328     bool available = audio_manager_->IsStereoPlayoutSupported();
329     // Android does not support changes between mono and stero on the fly.
330     // Instead, the native audio layer is configured via the audio manager
331     // to either support mono or stereo. It is allowed to call this method
332     // if that same state is not modified.
333     return (enable == available) ? 0 : -1;
334   }
335 
StereoPlayout(bool & enabled)336   int32_t StereoPlayout(bool& enabled) const override {
337     enabled = audio_manager_->IsStereoPlayoutSupported();
338     return 0;
339   }
340 
StereoRecordingIsAvailable(bool & available)341   int32_t StereoRecordingIsAvailable(bool& available) override {
342     RTC_LOG(INFO) << __FUNCTION__;
343     available = audio_manager_->IsStereoRecordSupported();
344     return 0;
345   }
346 
SetStereoRecording(bool enable)347   int32_t SetStereoRecording(bool enable) override {
348     RTC_LOG(INFO) << __FUNCTION__;
349     bool available = audio_manager_->IsStereoRecordSupported();
350     // Android does not support changes between mono and stero on the fly.
351     // Instead, the native audio layer is configured via the audio manager
352     // to either support mono or stereo. It is allowed to call this method
353     // if that same state is not modified.
354     return (enable == available) ? 0 : -1;
355   }
356 
StereoRecording(bool & enabled)357   int32_t StereoRecording(bool& enabled) const override {
358     RTC_LOG(INFO) << __FUNCTION__;
359     enabled = audio_manager_->IsStereoRecordSupported();
360     return 0;
361   }
362 
PlayoutDelay(uint16_t & delay_ms)363   int32_t PlayoutDelay(uint16_t& delay_ms) const override {
364     // Best guess we can do is to use half of the estimated total delay.
365     delay_ms = audio_manager_->GetDelayEstimateInMilliseconds() / 2;
366     RTC_DCHECK_GT(delay_ms, 0);
367     return 0;
368   }
369 
AttachAudioBuffer(AudioDeviceBuffer * audioBuffer)370   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override {
371     RTC_LOG(INFO) << __FUNCTION__;
372     output_.AttachAudioBuffer(audioBuffer);
373     input_.AttachAudioBuffer(audioBuffer);
374   }
375 
376   // Returns true if the device both supports built in AEC and the device
377   // is not blacklisted.
378   // Currently, if OpenSL ES is used in both directions, this method will still
379   // report the correct value and it has the correct effect. As an example:
380   // a device supports built in AEC and this method returns true. Libjingle
381   // will then disable the WebRTC based AEC and that will work for all devices
382   // (mainly Nexus) even when OpenSL ES is used for input since our current
383   // implementation will enable built-in AEC by default also for OpenSL ES.
384   // The only "bad" thing that happens today is that when Libjingle calls
385   // OpenSLESRecorder::EnableBuiltInAEC() it will not have any real effect and
386   // a "Not Implemented" log will be filed. This non-perfect state will remain
387   // until I have added full support for audio effects based on OpenSL ES APIs.
BuiltInAECIsAvailable()388   bool BuiltInAECIsAvailable() const override {
389     RTC_LOG(INFO) << __FUNCTION__;
390     return audio_manager_->IsAcousticEchoCancelerSupported();
391   }
392 
393   // TODO(henrika): add implementation for OpenSL ES based audio as well.
EnableBuiltInAEC(bool enable)394   int32_t EnableBuiltInAEC(bool enable) override {
395     RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
396     RTC_CHECK(BuiltInAECIsAvailable()) << "HW AEC is not available";
397     return input_.EnableBuiltInAEC(enable);
398   }
399 
400   // Returns true if the device both supports built in AGC and the device
401   // is not blacklisted.
402   // TODO(henrika): add implementation for OpenSL ES based audio as well.
403   // In addition, see comments for BuiltInAECIsAvailable().
BuiltInAGCIsAvailable()404   bool BuiltInAGCIsAvailable() const override {
405     RTC_LOG(INFO) << __FUNCTION__;
406     return audio_manager_->IsAutomaticGainControlSupported();
407   }
408 
409   // TODO(henrika): add implementation for OpenSL ES based audio as well.
EnableBuiltInAGC(bool enable)410   int32_t EnableBuiltInAGC(bool enable) override {
411     RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
412     RTC_CHECK(BuiltInAGCIsAvailable()) << "HW AGC is not available";
413     return input_.EnableBuiltInAGC(enable);
414   }
415 
416   // Returns true if the device both supports built in NS and the device
417   // is not blacklisted.
418   // TODO(henrika): add implementation for OpenSL ES based audio as well.
419   // In addition, see comments for BuiltInAECIsAvailable().
BuiltInNSIsAvailable()420   bool BuiltInNSIsAvailable() const override {
421     RTC_LOG(INFO) << __FUNCTION__;
422     return audio_manager_->IsNoiseSuppressorSupported();
423   }
424 
425   // TODO(henrika): add implementation for OpenSL ES based audio as well.
EnableBuiltInNS(bool enable)426   int32_t EnableBuiltInNS(bool enable) override {
427     RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
428     RTC_CHECK(BuiltInNSIsAvailable()) << "HW NS is not available";
429     return input_.EnableBuiltInNS(enable);
430   }
431 
432  private:
433   rtc::ThreadChecker thread_checker_;
434 
435   // Local copy of the audio layer set during construction of the
436   // AudioDeviceModuleImpl instance. Read only value.
437   const AudioDeviceModule::AudioLayer audio_layer_;
438 
439   // Non-owning raw pointer to AudioManager instance given to use at
440   // construction. The real object is owned by AudioDeviceModuleImpl and the
441   // life time is the same as that of the AudioDeviceModuleImpl, hence there
442   // is no risk of reading a NULL pointer at any time in this class.
443   AudioManager* const audio_manager_;
444 
445   OutputType output_;
446 
447   InputType input_;
448 
449   bool initialized_;
450 };
451 
452 }  // namespace webrtc
453 
454 #endif  // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
455