blob: 3ea248f79e94a10d3653c1556e1d22d13a7d7144 [file] [log] [blame]
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
12#define MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000013
Artem Titovd15a5752021-02-10 14:31:24 +010014#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_device/android/audio_manager.h"
16#include "modules/audio_device/audio_device_generic.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
henrikafe55c382015-06-05 11:45:56 +020019
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000020namespace webrtc {
21
22// InputType/OutputType can be any class that implements the capturing/rendering
23// part of the AudioDeviceGeneric API.
henrikab2619892015-05-18 16:49:16 +020024// Construction and destruction must be done on one and the same thread. Each
henrikg91d6ede2015-09-17 00:24:34 -070025// 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.
henrikab2619892015-05-18 16:49:16 +020028// It is possible to call the two static methods (SetAndroidAudioDeviceObjects
29// and ClearAndroidAudioDeviceObjects) from a different thread but both will
henrikg91d6ede2015-09-17 00:24:34 -070030// RTC_CHECK that the calling thread is attached to a Java VM.
henrikab2619892015-05-18 16:49:16 +020031
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000032template <class InputType, class OutputType>
33class AudioDeviceTemplate : public AudioDeviceGeneric {
34 public:
henrikab2619892015-05-18 16:49:16 +020035 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) {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010042 RTC_DLOG(INFO) << __FUNCTION__;
henrikg91d6ede2015-09-17 00:24:34 -070043 RTC_CHECK(audio_manager);
henrikab2619892015-05-18 16:49:16 +020044 audio_manager_->SetActiveAudioLayer(audio_layer);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000045 }
46
Mirko Bonadei675513b2017-11-09 11:09:25 +010047 virtual ~AudioDeviceTemplate() { RTC_LOG(INFO) << __FUNCTION__; }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000048
49 int32_t ActiveAudioLayer(
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000050 AudioDeviceModule::AudioLayer& audioLayer) const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010051 RTC_DLOG(INFO) << __FUNCTION__;
henrikab2619892015-05-18 16:49:16 +020052 audioLayer = audio_layer_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000053 return 0;
henrikab2619892015-05-18 16:49:16 +020054 }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000055
Max Morin84cab202016-07-01 13:35:19 +020056 InitStatus Init() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010057 RTC_DLOG(INFO) << __FUNCTION__;
Sebastian Janssonc01367d2019-04-08 15:20:44 +020058 RTC_DCHECK(thread_checker_.IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -070059 RTC_DCHECK(!initialized_);
Max Morin84cab202016-07-01 13:35:19 +020060 if (!audio_manager_->Init()) {
61 return InitStatus::OTHER_ERROR;
62 }
kaorimatz9deaa862015-08-21 18:38:50 -070063 if (output_.Init() != 0) {
64 audio_manager_->Close();
Max Morin84cab202016-07-01 13:35:19 +020065 return InitStatus::PLAYOUT_ERROR;
kaorimatz9deaa862015-08-21 18:38:50 -070066 }
67 if (input_.Init() != 0) {
68 output_.Terminate();
69 audio_manager_->Close();
Max Morin84cab202016-07-01 13:35:19 +020070 return InitStatus::RECORDING_ERROR;
kaorimatz9deaa862015-08-21 18:38:50 -070071 }
72 initialized_ = true;
Max Morin84cab202016-07-01 13:35:19 +020073 return InitStatus::OK;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000074 }
75
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000076 int32_t Terminate() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010077 RTC_DLOG(INFO) << __FUNCTION__;
Sebastian Janssonc01367d2019-04-08 15:20:44 +020078 RTC_DCHECK(thread_checker_.IsCurrent());
kaorimatz9deaa862015-08-21 18:38:50 -070079 int32_t err = input_.Terminate();
80 err |= output_.Terminate();
81 err |= !audio_manager_->Close();
82 initialized_ = false;
henrikg91d6ede2015-09-17 00:24:34 -070083 RTC_DCHECK_EQ(err, 0);
kaorimatz9deaa862015-08-21 18:38:50 -070084 return err;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000085 }
86
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000087 bool Initialized() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010088 RTC_DLOG(INFO) << __FUNCTION__;
Sebastian Janssonc01367d2019-04-08 15:20:44 +020089 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +020090 return initialized_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000091 }
92
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000093 int16_t PlayoutDevices() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010094 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org962c6242015-02-23 11:54:05 +000095 return 1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +000096 }
97
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000098 int16_t RecordingDevices() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +010099 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000100 return 1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000101 }
102
Mirko Bonadei72c42502017-11-09 09:33:23 +0100103 int32_t PlayoutDeviceName(uint16_t index,
104 char name[kAdmMaxDeviceNameSize],
105 char guid[kAdmMaxGuidSize]) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100106 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000107 }
108
Mirko Bonadei72c42502017-11-09 09:33:23 +0100109 int32_t RecordingDeviceName(uint16_t index,
110 char name[kAdmMaxDeviceNameSize],
111 char guid[kAdmMaxGuidSize]) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100112 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000113 }
114
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000115 int32_t SetPlayoutDevice(uint16_t index) override {
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000116 // OK to use but it has no effect currently since device selection is
117 // done using Andoid APIs instead.
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100118 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000119 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000120 }
121
122 int32_t SetPlayoutDevice(
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000123 AudioDeviceModule::WindowsDeviceType device) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100124 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000125 }
126
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000127 int32_t SetRecordingDevice(uint16_t index) override {
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000128 // OK to use but it has no effect currently since device selection is
129 // done using Andoid APIs instead.
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100130 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000131 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000132 }
133
134 int32_t SetRecordingDevice(
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000135 AudioDeviceModule::WindowsDeviceType device) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100136 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000137 }
138
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000139 int32_t PlayoutIsAvailable(bool& available) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100140 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000141 available = true;
142 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000143 }
144
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000145 int32_t InitPlayout() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100146 RTC_DLOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000147 return output_.InitPlayout();
148 }
149
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000150 bool PlayoutIsInitialized() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100151 RTC_DLOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000152 return output_.PlayoutIsInitialized();
153 }
154
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000155 int32_t RecordingIsAvailable(bool& available) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100156 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000157 available = true;
158 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000159 }
160
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000161 int32_t InitRecording() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100162 RTC_DLOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000163 return input_.InitRecording();
164 }
165
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000166 bool RecordingIsInitialized() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100167 RTC_DLOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000168 return input_.RecordingIsInitialized();
169 }
170
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000171 int32_t StartPlayout() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100172 RTC_DLOG(INFO) << __FUNCTION__;
henrikafe55c382015-06-05 11:45:56 +0200173 if (!audio_manager_->IsCommunicationModeEnabled()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(WARNING)
Max Morin098e6c52016-06-28 09:36:25 +0200175 << "The application should use MODE_IN_COMMUNICATION audio mode!";
henrikafe55c382015-06-05 11:45:56 +0200176 }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000177 return output_.StartPlayout();
178 }
179
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000180 int32_t StopPlayout() override {
henrikaa125d7d2015-04-10 15:19:24 +0200181 // Avoid using audio manger (JNI/Java cost) if playout was inactive.
182 if (!Playing())
183 return 0;
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100184 RTC_DLOG(INFO) << __FUNCTION__;
henrika09bf1a12015-04-10 11:46:55 +0200185 int32_t err = output_.StopPlayout();
henrika09bf1a12015-04-10 11:46:55 +0200186 return err;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000187 }
188
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000189 bool Playing() const override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100190 RTC_LOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000191 return output_.Playing();
192 }
193
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000194 int32_t StartRecording() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100195 RTC_DLOG(INFO) << __FUNCTION__;
henrikafe55c382015-06-05 11:45:56 +0200196 if (!audio_manager_->IsCommunicationModeEnabled()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100197 RTC_LOG(WARNING)
Max Morin098e6c52016-06-28 09:36:25 +0200198 << "The application should use MODE_IN_COMMUNICATION audio mode!";
henrikafe55c382015-06-05 11:45:56 +0200199 }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000200 return input_.StartRecording();
201 }
202
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000203 int32_t StopRecording() override {
henrikaa125d7d2015-04-10 15:19:24 +0200204 // Avoid using audio manger (JNI/Java cost) if recording was inactive.
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100205 RTC_DLOG(INFO) << __FUNCTION__;
henrikaa125d7d2015-04-10 15:19:24 +0200206 if (!Recording())
207 return 0;
henrika09bf1a12015-04-10 11:46:55 +0200208 int32_t err = input_.StopRecording();
henrika09bf1a12015-04-10 11:46:55 +0200209 return err;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000210 }
211
Mirko Bonadei72c42502017-11-09 09:33:23 +0100212 bool Recording() const override { return input_.Recording(); }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000213
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000214 int32_t InitSpeaker() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100215 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000216 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000217 }
218
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000219 bool SpeakerIsInitialized() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100220 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000221 return true;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000222 }
223
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000224 int32_t InitMicrophone() override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100225 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000226 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000227 }
228
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000229 bool MicrophoneIsInitialized() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100230 RTC_DLOG(INFO) << __FUNCTION__;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000231 return true;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000232 }
233
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000234 int32_t SpeakerVolumeIsAvailable(bool& available) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100235 RTC_DLOG(INFO) << __FUNCTION__;
henrika8324b522015-03-27 10:56:23 +0100236 return output_.SpeakerVolumeIsAvailable(available);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000237 }
238
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000239 int32_t SetSpeakerVolume(uint32_t volume) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100240 RTC_DLOG(INFO) << __FUNCTION__;
henrika8324b522015-03-27 10:56:23 +0100241 return output_.SetSpeakerVolume(volume);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000242 }
243
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000244 int32_t SpeakerVolume(uint32_t& volume) const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100245 RTC_DLOG(INFO) << __FUNCTION__;
henrika8324b522015-03-27 10:56:23 +0100246 return output_.SpeakerVolume(volume);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000247 }
248
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000249 int32_t MaxSpeakerVolume(uint32_t& maxVolume) const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100250 RTC_DLOG(INFO) << __FUNCTION__;
henrika8324b522015-03-27 10:56:23 +0100251 return output_.MaxSpeakerVolume(maxVolume);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000252 }
253
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000254 int32_t MinSpeakerVolume(uint32_t& minVolume) const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100255 RTC_DLOG(INFO) << __FUNCTION__;
henrika8324b522015-03-27 10:56:23 +0100256 return output_.MinSpeakerVolume(minVolume);
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000257 }
258
Mirko Bonadei72c42502017-11-09 09:33:23 +0100259 int32_t MicrophoneVolumeIsAvailable(bool& available) override {
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000260 available = false;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000261 return -1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000262 }
263
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000264 int32_t SetMicrophoneVolume(uint32_t volume) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100265 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000266 }
267
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000268 int32_t MicrophoneVolume(uint32_t& volume) const override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100269 RTC_CHECK_NOTREACHED();
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000270 return -1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000271 }
272
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000273 int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100274 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000275 }
276
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000277 int32_t MinMicrophoneVolume(uint32_t& minVolume) const override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100278 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000279 }
280
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000281 int32_t SpeakerMuteIsAvailable(bool& available) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100282 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000283 }
284
Karl Wibergc95b9392020-11-08 00:49:37 +0100285 int32_t SetSpeakerMute(bool enable) override { RTC_CHECK_NOTREACHED(); }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000286
Karl Wibergc95b9392020-11-08 00:49:37 +0100287 int32_t SpeakerMute(bool& enabled) const override { RTC_CHECK_NOTREACHED(); }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000288
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000289 int32_t MicrophoneMuteIsAvailable(bool& available) override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100290 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000291 }
292
Karl Wibergc95b9392020-11-08 00:49:37 +0100293 int32_t SetMicrophoneMute(bool enable) override { RTC_CHECK_NOTREACHED(); }
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000294
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000295 int32_t MicrophoneMute(bool& enabled) const override {
Karl Wibergc95b9392020-11-08 00:49:37 +0100296 RTC_CHECK_NOTREACHED();
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000297 }
298
henrika76535de2017-09-11 01:25:55 -0700299 // Returns true if the audio manager has been configured to support stereo
300 // and false otherwised. Default is mono.
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000301 int32_t StereoPlayoutIsAvailable(bool& available) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100302 RTC_DLOG(INFO) << __FUNCTION__;
henrika76535de2017-09-11 01:25:55 -0700303 available = audio_manager_->IsStereoPlayoutSupported();
henrika@webrtc.org962c6242015-02-23 11:54:05 +0000304 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000305 }
306
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000307 int32_t SetStereoPlayout(bool enable) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100308 RTC_DLOG(INFO) << __FUNCTION__;
henrika76535de2017-09-11 01:25:55 -0700309 bool available = audio_manager_->IsStereoPlayoutSupported();
310 // Android does not support changes between mono and stero on the fly.
311 // Instead, the native audio layer is configured via the audio manager
312 // to either support mono or stereo. It is allowed to call this method
313 // if that same state is not modified.
314 return (enable == available) ? 0 : -1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000315 }
316
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000317 int32_t StereoPlayout(bool& enabled) const override {
henrika76535de2017-09-11 01:25:55 -0700318 enabled = audio_manager_->IsStereoPlayoutSupported();
319 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000320 }
321
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000322 int32_t StereoRecordingIsAvailable(bool& available) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100323 RTC_DLOG(INFO) << __FUNCTION__;
henrika76535de2017-09-11 01:25:55 -0700324 available = audio_manager_->IsStereoRecordSupported();
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000325 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000326 }
327
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000328 int32_t SetStereoRecording(bool enable) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100329 RTC_DLOG(INFO) << __FUNCTION__;
henrika76535de2017-09-11 01:25:55 -0700330 bool available = audio_manager_->IsStereoRecordSupported();
331 // Android does not support changes between mono and stero on the fly.
332 // Instead, the native audio layer is configured via the audio manager
333 // to either support mono or stereo. It is allowed to call this method
334 // if that same state is not modified.
335 return (enable == available) ? 0 : -1;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000336 }
337
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000338 int32_t StereoRecording(bool& enabled) const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100339 RTC_DLOG(INFO) << __FUNCTION__;
henrika76535de2017-09-11 01:25:55 -0700340 enabled = audio_manager_->IsStereoRecordSupported();
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000341 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000342 }
343
henrikab2619892015-05-18 16:49:16 +0200344 int32_t PlayoutDelay(uint16_t& delay_ms) const override {
345 // Best guess we can do is to use half of the estimated total delay.
346 delay_ms = audio_manager_->GetDelayEstimateInMilliseconds() / 2;
henrikg91d6ede2015-09-17 00:24:34 -0700347 RTC_DCHECK_GT(delay_ms, 0);
henrikab2619892015-05-18 16:49:16 +0200348 return 0;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000349 }
350
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000351 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100352 RTC_DLOG(INFO) << __FUNCTION__;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000353 output_.AttachAudioBuffer(audioBuffer);
354 input_.AttachAudioBuffer(audioBuffer);
355 }
356
henrikab2619892015-05-18 16:49:16 +0200357 // Returns true if the device both supports built in AEC and the device
358 // is not blacklisted.
henrika918b5542016-09-19 15:44:09 +0200359 // Currently, if OpenSL ES is used in both directions, this method will still
360 // report the correct value and it has the correct effect. As an example:
361 // a device supports built in AEC and this method returns true. Libjingle
362 // will then disable the WebRTC based AEC and that will work for all devices
363 // (mainly Nexus) even when OpenSL ES is used for input since our current
364 // implementation will enable built-in AEC by default also for OpenSL ES.
365 // The only "bad" thing that happens today is that when Libjingle calls
366 // OpenSLESRecorder::EnableBuiltInAEC() it will not have any real effect and
367 // a "Not Implemented" log will be filed. This non-perfect state will remain
368 // until I have added full support for audio effects based on OpenSL ES APIs.
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000369 bool BuiltInAECIsAvailable() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100370 RTC_DLOG(INFO) << __FUNCTION__;
henrikab2619892015-05-18 16:49:16 +0200371 return audio_manager_->IsAcousticEchoCancelerSupported();
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000372 }
373
henrika918b5542016-09-19 15:44:09 +0200374 // TODO(henrika): add implementation for OpenSL ES based audio as well.
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000375 int32_t EnableBuiltInAEC(bool enable) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100376 RTC_DLOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrikg91d6ede2015-09-17 00:24:34 -0700377 RTC_CHECK(BuiltInAECIsAvailable()) << "HW AEC is not available";
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000378 return input_.EnableBuiltInAEC(enable);
379 }
380
henrikac14f5ff2015-09-23 14:08:33 +0200381 // Returns true if the device both supports built in AGC and the device
382 // is not blacklisted.
henrika918b5542016-09-19 15:44:09 +0200383 // TODO(henrika): add implementation for OpenSL ES based audio as well.
384 // In addition, see comments for BuiltInAECIsAvailable().
henrikac14f5ff2015-09-23 14:08:33 +0200385 bool BuiltInAGCIsAvailable() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100386 RTC_DLOG(INFO) << __FUNCTION__;
henrikac14f5ff2015-09-23 14:08:33 +0200387 return audio_manager_->IsAutomaticGainControlSupported();
388 }
389
henrika918b5542016-09-19 15:44:09 +0200390 // TODO(henrika): add implementation for OpenSL ES based audio as well.
henrikac14f5ff2015-09-23 14:08:33 +0200391 int32_t EnableBuiltInAGC(bool enable) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100392 RTC_DLOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrikac14f5ff2015-09-23 14:08:33 +0200393 RTC_CHECK(BuiltInAGCIsAvailable()) << "HW AGC is not available";
394 return input_.EnableBuiltInAGC(enable);
395 }
396
397 // Returns true if the device both supports built in NS and the device
398 // is not blacklisted.
henrika918b5542016-09-19 15:44:09 +0200399 // TODO(henrika): add implementation for OpenSL ES based audio as well.
400 // In addition, see comments for BuiltInAECIsAvailable().
henrikac14f5ff2015-09-23 14:08:33 +0200401 bool BuiltInNSIsAvailable() const override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100402 RTC_DLOG(INFO) << __FUNCTION__;
henrikac14f5ff2015-09-23 14:08:33 +0200403 return audio_manager_->IsNoiseSuppressorSupported();
404 }
405
henrika918b5542016-09-19 15:44:09 +0200406 // TODO(henrika): add implementation for OpenSL ES based audio as well.
henrikac14f5ff2015-09-23 14:08:33 +0200407 int32_t EnableBuiltInNS(bool enable) override {
Mirko Bonadei3b68aa32021-01-28 09:21:06 +0100408 RTC_DLOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrikac14f5ff2015-09-23 14:08:33 +0200409 RTC_CHECK(BuiltInNSIsAvailable()) << "HW NS is not available";
410 return input_.EnableBuiltInNS(enable);
411 }
412
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000413 private:
Artem Titovc8421c42021-02-02 10:57:19 +0100414 SequenceChecker thread_checker_;
henrikab2619892015-05-18 16:49:16 +0200415
416 // Local copy of the audio layer set during construction of the
417 // AudioDeviceModuleImpl instance. Read only value.
418 const AudioDeviceModule::AudioLayer audio_layer_;
419
420 // Non-owning raw pointer to AudioManager instance given to use at
421 // construction. The real object is owned by AudioDeviceModuleImpl and the
422 // life time is the same as that of the AudioDeviceModuleImpl, hence there
423 // is no risk of reading a NULL pointer at any time in this class.
424 AudioManager* const audio_manager_;
425
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000426 OutputType output_;
henrikab2619892015-05-18 16:49:16 +0200427
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000428 InputType input_;
henrikab2619892015-05-18 16:49:16 +0200429
430 bool initialized_;
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000431};
432
433} // namespace webrtc
434
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200435#endif // MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_