blob: 2301e4480a083dc00a8559df36b006a0096ba391 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
22#include <android/media/BnCaptureStateListener.h>
Mathias Agopian75624082009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070024#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020025#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070026#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080027#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070029#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070030#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010031#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <math.h>
33
Dima Zavin64760242011-05-11 14:15:23 -070034#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070035
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -080036#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070037 ({ auto _tmp = (x); \
38 if (!_tmp.ok()) return Status::fromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070039 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070040
Eric Laurentc2f1f072009-07-17 12:17:14 -070041// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070042
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043namespace android {
44
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045using binder::Status;
46
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047// client singleton for AudioFlinger binder interface
48Mutex AudioSystem::gLock;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080049Mutex AudioSystem::gLockErrorCallbacks;
Glenn Kastend2d089f2014-11-05 11:48:12 -080050Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051sp<IAudioFlinger> AudioSystem::gAudioFlinger;
52sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080053std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070054dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070057// Required to be held while calling into gSoundTriggerCaptureStateListener.
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070058class CaptureStateListenerImpl;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070059Mutex gSoundTriggerCaptureStateListenerLock;
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070060sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070061
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080063const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080065 sp<IAudioFlinger> af;
66 sp<AudioFlingerClient> afc;
Mikhail Naganov69330d42020-04-08 19:29:50 +000067 bool reportNoError = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -080068 {
69 Mutex::Autolock _l(gLock);
70 if (gAudioFlinger == 0) {
71 sp<IServiceManager> sm = defaultServiceManager();
72 sp<IBinder> binder;
73 do {
Ytai Ben-Tsvi50b8ccb2020-11-24 13:47:54 -080074 binder = sm->getService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME));
Eric Laurent0ebd5f92014-11-19 19:04:52 -080075 if (binder != 0)
76 break;
77 ALOGW("AudioFlinger not published, waiting...");
78 usleep(500000); // 0.5 s
79 } while (true);
80 if (gAudioFlingerClient == NULL) {
81 gAudioFlingerClient = new AudioFlingerClient();
82 } else {
Mikhail Naganov69330d42020-04-08 19:29:50 +000083 reportNoError = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080085 binder->linkToDeath(gAudioFlingerClient);
Ytai Ben-Tsvi50b8ccb2020-11-24 13:47:54 -080086 gAudioFlinger = new AudioFlingerClientAdapter(
87 interface_cast<media::IAudioFlingerService>(binder));
Eric Laurent0ebd5f92014-11-19 19:04:52 -080088 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
89 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070090 // Make sure callbacks can be received by gAudioFlingerClient
91 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070092 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080093 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080095 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020096 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080097 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020098 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080099 }
Mikhail Naganov69330d42020-04-08 19:29:50 +0000100 if (reportNoError) reportError(NO_ERROR);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800101 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102}
103
Eric Laurent296fb132015-05-01 11:38:42 -0700104const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
105{
106 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
107 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
108 if (af == 0) return 0;
109 Mutex::Autolock _l(gLock);
110 return gAudioFlingerClient;
111}
112
113sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
114{
115 sp<AudioIoDescriptor> desc;
116 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
117 if (afc != 0) {
118 desc = afc->getIoDescriptor(ioHandle);
119 }
120 return desc;
121}
122
Eric Laurent46291612013-07-18 14:38:44 -0700123/* static */ status_t AudioSystem::checkAudioFlinger()
124{
125 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
126 return NO_ERROR;
127 }
128 return DEAD_OBJECT;
129}
130
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700131// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
132
Glenn Kasten4944acb2013-08-19 08:39:20 -0700133status_t AudioSystem::muteMicrophone(bool state)
134{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
136 if (af == 0) return PERMISSION_DENIED;
137 return af->setMicMute(state);
138}
139
Glenn Kasten4944acb2013-08-19 08:39:20 -0700140status_t AudioSystem::isMicrophoneMuted(bool* state)
141{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
143 if (af == 0) return PERMISSION_DENIED;
144 *state = af->getMicMute();
145 return NO_ERROR;
146}
147
148status_t AudioSystem::setMasterVolume(float value)
149{
150 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
151 if (af == 0) return PERMISSION_DENIED;
152 af->setMasterVolume(value);
153 return NO_ERROR;
154}
155
156status_t AudioSystem::setMasterMute(bool mute)
157{
158 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
159 if (af == 0) return PERMISSION_DENIED;
160 af->setMasterMute(mute);
161 return NO_ERROR;
162}
163
164status_t AudioSystem::getMasterVolume(float* volume)
165{
166 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
167 if (af == 0) return PERMISSION_DENIED;
168 *volume = af->masterVolume();
169 return NO_ERROR;
170}
171
172status_t AudioSystem::getMasterMute(bool* mute)
173{
174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
176 *mute = af->masterMute();
177 return NO_ERROR;
178}
179
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800180status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
181 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182{
Dima Zavinfce7a472011-04-19 22:30:36 -0700183 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
185 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700186 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 return NO_ERROR;
188}
189
Glenn Kastenfff6d712012-01-12 16:38:12 -0800190status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191{
Dima Zavinfce7a472011-04-19 22:30:36 -0700192 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
194 if (af == 0) return PERMISSION_DENIED;
195 af->setStreamMute(stream, mute);
196 return NO_ERROR;
197}
198
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800199status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
200 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201{
Dima Zavinfce7a472011-04-19 22:30:36 -0700202 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
204 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700205 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 return NO_ERROR;
207}
208
Glenn Kastenfff6d712012-01-12 16:38:12 -0800209status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210{
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
213 if (af == 0) return PERMISSION_DENIED;
214 *mute = af->streamMute(stream);
215 return NO_ERROR;
216}
217
Glenn Kastenf78aee72012-01-04 11:00:47 -0800218status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800220 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
222 if (af == 0) return PERMISSION_DENIED;
223 return af->setMode(mode);
224}
225
Glenn Kasten4944acb2013-08-19 08:39:20 -0700226status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
227{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
229 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700230 return af->setParameters(ioHandle, keyValuePairs);
231}
232
Glenn Kasten4944acb2013-08-19 08:39:20 -0700233String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
234{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700235 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
236 String8 result = String8("");
237 if (af == 0) return result;
238
239 result = af->getParameters(ioHandle, keys);
240 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241}
242
Glenn Kastenc23885e2013-12-19 16:35:18 -0800243status_t AudioSystem::setParameters(const String8& keyValuePairs)
244{
Glenn Kasten142f5192014-03-25 17:44:59 -0700245 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800246}
247
248String8 AudioSystem::getParameters(const String8& keys)
249{
Glenn Kasten142f5192014-03-25 17:44:59 -0700250 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800251}
252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253// convert volume steps to natural log scale
254
255// change this value to change volume scaling
256static const float dBPerStep = 0.5f;
257// shouldn't need to touch these
258static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
259static const float dBConvertInverse = 1.0f / dBConvert;
260
261float AudioSystem::linearToLog(int volume)
262{
263 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000264 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 // return v;
266 return volume ? exp(float(100 - volume) * dBConvert) : 0;
267}
268
269int AudioSystem::logToLinear(float volume)
270{
271 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000272 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 // return v;
274 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
275}
276
Eric Laurent21da6472017-11-09 16:29:26 -0800277/* static */ size_t AudioSystem::calculateMinFrameCount(
278 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
279 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
280{
281 // Ensure that buffer depth covers at least audio hardware latency
282 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
283 if (minBufCount < 2) {
284 minBufCount = 2;
285 }
286#if 0
287 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
288 // but keeping the code here to make it easier to add later.
289 if (minBufCount < notificationsPerBufferReq) {
290 minBufCount = notificationsPerBufferReq;
291 }
292#endif
293 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
294 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
295 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
296 /*, notificationsPerBufferReq*/);
297 return minBufCount * sourceFramesNeededWithTimestretch(
298 sampleRate, afFrameCount, afSampleRate, speed);
299}
300
301
Glenn Kasten3b16c762012-11-14 08:44:39 -0800302status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305
Dima Zavinfce7a472011-04-19 22:30:36 -0700306 if (streamType == AUDIO_STREAM_DEFAULT) {
307 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308 }
309
Glenn Kastenfff6d712012-01-12 16:38:12 -0800310 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 if (output == 0) {
312 return PERMISSION_DENIED;
313 }
314
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700315 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700316}
317
Glenn Kasten2c073da2016-02-26 09:14:08 -0800318status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800319 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700320{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800321 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
322 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800323 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
324 if (desc == 0) {
325 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800327 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800329 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800330 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800331 return BAD_VALUE;
332 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333
Glenn Kasten2c073da2016-02-26 09:14:08 -0800334 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 return NO_ERROR;
337}
338
Glenn Kastene33054e2012-11-14 12:54:39 -0800339status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342
Dima Zavinfce7a472011-04-19 22:30:36 -0700343 if (streamType == AUDIO_STREAM_DEFAULT) {
344 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700346
Glenn Kastenfff6d712012-01-12 16:38:12 -0800347 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700348 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700349 return PERMISSION_DENIED;
350 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700352 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700353}
354
Glenn Kasten2c073da2016-02-26 09:14:08 -0800355status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800356 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700357{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800358 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
359 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800360 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
361 if (desc == 0) {
362 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700363 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800364 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800366 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800367 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800368 return BAD_VALUE;
369 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700370
Glenn Kasten2c073da2016-02-26 09:14:08 -0800371 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 return NO_ERROR;
374}
375
Glenn Kastenfff6d712012-01-12 16:38:12 -0800376status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700378 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379
Dima Zavinfce7a472011-04-19 22:30:36 -0700380 if (streamType == AUDIO_STREAM_DEFAULT) {
381 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700383
Glenn Kastenfff6d712012-01-12 16:38:12 -0800384 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700385 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700386 return PERMISSION_DENIED;
387 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388
Glenn Kasten241618f2014-03-25 17:48:57 -0700389 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700390}
391
392status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700393 uint32_t* latency)
394{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800395 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
396 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700397 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700398 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700399 *latency = af->latency(output);
400 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700401 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700402 }
403
Glenn Kasten241618f2014-03-25 17:48:57 -0700404 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700405
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 return NO_ERROR;
407}
408
Glenn Kastendd8104c2012-07-02 12:42:44 -0700409status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
410 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411{
Eric Laurent296fb132015-05-01 11:38:42 -0700412 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
413 if (afc == 0) {
414 return NO_INIT;
415 }
416 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417}
418
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700419status_t AudioSystem::setVoiceVolume(float value)
420{
421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
422 if (af == 0) return PERMISSION_DENIED;
423 return af->setVoiceVolume(value);
424}
425
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000426status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700427 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800428{
429 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
430 if (af == 0) return PERMISSION_DENIED;
431
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000432 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800433}
434
Glenn Kasten4944acb2013-08-19 08:39:20 -0700435uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
436{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800437 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800438 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800439 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700440 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800441
442 result = af->getInputFramesLost(ioHandle);
443 return result;
444}
445
Glenn Kasteneeecb982016-02-26 10:44:04 -0800446audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700447{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700448 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700449 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700450 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800451 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700452}
453
Andy Hung8b0bfd92019-12-23 13:11:11 -0800454void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700455{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700456 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
457 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800458 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700459 }
460}
461
Glenn Kastend848eb42016-03-08 13:42:11 -0800462void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700463{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700464 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
465 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800466 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700467 }
468}
469
Eric Laurent93c3d412014-08-01 14:48:35 -0700470audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
471{
472 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
473 if (af == 0) return AUDIO_HW_SYNC_INVALID;
474 return af->getAudioHwSyncForSession(sessionId);
475}
476
Eric Laurent72e3f392015-05-20 14:43:50 -0700477status_t AudioSystem::systemReady()
478{
479 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
480 if (af == 0) return NO_INIT;
481 return af->systemReady();
482}
483
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700484status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
485 size_t* frameCount)
486{
487 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
488 if (af == 0) return PERMISSION_DENIED;
489 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
490 if (desc == 0) {
491 *frameCount = af->frameCountHAL(ioHandle);
492 } else {
493 *frameCount = desc->mFrameCountHAL;
494 }
495 if (*frameCount == 0) {
496 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
497 return BAD_VALUE;
498 }
499
500 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
501
502 return NO_ERROR;
503}
504
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505// ---------------------------------------------------------------------------
506
Eric Laurent73e26b62015-04-27 16:55:58 -0700507
508void AudioSystem::AudioFlingerClient::clearIoCache()
509{
510 Mutex::Autolock _l(mLock);
511 mIoDescriptors.clear();
512 mInBuffSize = 0;
513 mInSamplingRate = 0;
514 mInFormat = AUDIO_FORMAT_DEFAULT;
515 mInChannelMask = AUDIO_CHANNEL_NONE;
516}
517
Glenn Kasten4944acb2013-08-19 08:39:20 -0700518void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
519{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800520 {
521 Mutex::Autolock _l(AudioSystem::gLock);
522 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800523 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524
Eric Laurent73e26b62015-04-27 16:55:58 -0700525 // clear output handles and stream to output map caches
526 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800528 reportError(DEAD_OBJECT);
529
Steve Block5ff1dd52012-01-05 23:22:43 +0000530 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531}
532
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700533Status AudioSystem::AudioFlingerClient::ioConfigChanged(
534 media::AudioIoConfigEvent _event,
535 const media::AudioIoDescriptor& _ioDesc) {
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -0800536 audio_io_config_event event = VALUE_OR_RETURN_BINDER_STATUS(
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700537 aidl2legacy_AudioIoConfigEvent_audio_io_config_event(_event));
538 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -0800539 VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700540
Steve Block3856b092011-10-20 11:56:00 +0100541 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700543 if (ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700544
Eric Laurent296fb132015-05-01 11:38:42 -0700545 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700546 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700547 {
548 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700549 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700550
551 switch (event) {
552 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700553 case AUDIO_OUTPUT_REGISTERED:
554 case AUDIO_INPUT_OPENED:
555 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700556 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700557 if (oldDesc == 0) {
558 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
559 } else {
560 deviceId = oldDesc->getDeviceId();
561 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700562 }
Eric Laurent296fb132015-05-01 11:38:42 -0700563
564 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
565 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700566 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700567 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
568 if (it != mAudioDeviceCallbacks.end()) {
569 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100570 }
571 }
Eric Laurent296fb132015-05-01 11:38:42 -0700572 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700573 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
574 "frameCount %zu deviceId %d",
575 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
576 "output" : "input",
577 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
578 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700579 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
580 ioDesc->mFrameCount, ioDesc->getDeviceId());
581 } break;
582 case AUDIO_OUTPUT_CLOSED:
583 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700584 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700585 ALOGW("ioConfigChanged() closing unknown %s %d",
586 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
587 break;
588 }
589 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700590 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700591
Eric Laurent296fb132015-05-01 11:38:42 -0700592 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700593 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700594 } break;
595
596 case AUDIO_OUTPUT_CONFIG_CHANGED:
597 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700598 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700599 if (oldDesc == 0) {
Dean Wheatley8f992a12020-05-08 20:33:51 +1000600 ALOGW("ioConfigChanged() modifying unknown %s! %d",
601 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700602 break;
603 }
604
605 deviceId = oldDesc->getDeviceId();
606 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
607
608 if (deviceId != ioDesc->getDeviceId()) {
609 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700610 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
611 if (it != mAudioDeviceCallbacks.end()) {
612 callbacks = it->second;
613 }
Eric Laurent296fb132015-05-01 11:38:42 -0700614 }
615 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700616 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700617 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
618 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800619 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
620 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700621
Eric Laurentc2f1f072009-07-17 12:17:14 -0700622 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700623 case AUDIO_CLIENT_STARTED: {
624 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
625 if (oldDesc == 0) {
626 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
627 break;
628 }
629 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
630 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
631 oldDesc->mPatch = ioDesc->mPatch;
632 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
633 if (it != mAudioDeviceCallbacks.end()) {
634 auto cbks = it->second;
635 auto it2 = cbks.find(ioDesc->mPortId);
636 if (it2 != cbks.end()) {
637 callbacks.emplace(ioDesc->mPortId, it2->second);
638 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100639 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100640 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700641 } break;
642 }
643
644 for (auto wpCbk : callbacks) {
645 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
646 if (spCbk != nullptr) {
647 callbacksToCall.push_back(spCbk);
648 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700649 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800650 }
651
652 // Callbacks must be called without mLock held. May lead to dead lock if calling for
653 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700654 for (auto cb : callbacksToCall) {
655 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
656 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700657 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700658
659 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660}
661
Eric Laurent73e26b62015-04-27 16:55:58 -0700662status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
663 uint32_t sampleRate, audio_format_t format,
664 audio_channel_mask_t channelMask, size_t* buffSize)
665{
666 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
667 if (af == 0) {
668 return PERMISSION_DENIED;
669 }
670 Mutex::Autolock _l(mLock);
671 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
672 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
673 || (channelMask != mInChannelMask)) {
674 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
675 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800676 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700677 sampleRate, format, channelMask);
678 return BAD_VALUE;
679 }
680 // A benign race is possible here: we could overwrite a fresher cache entry
681 // save the request params
682 mInSamplingRate = sampleRate;
683 mInFormat = format;
684 mInChannelMask = channelMask;
685
686 mInBuffSize = inBuffSize;
687 }
688
689 *buffSize = mInBuffSize;
690
691 return NO_ERROR;
692}
693
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700694sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700695{
696 sp<AudioIoDescriptor> desc;
697 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
698 if (index >= 0) {
699 desc = mIoDescriptors.valueAt(index);
700 }
701 return desc;
702}
703
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700704sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
705{
706 Mutex::Autolock _l(mLock);
707 return getIoDescriptor_l(ioHandle);
708}
709
Eric Laurent296fb132015-05-01 11:38:42 -0700710status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700711 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
712 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700713{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700714 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800715 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700716 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
717 auto result = callbacks.try_emplace(portId, callback);
718 if (!result.second) {
719 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700720 }
Eric Laurent296fb132015-05-01 11:38:42 -0700721 return NO_ERROR;
722}
723
724status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700725 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
726 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700727{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700728 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800729 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700730 auto it = mAudioDeviceCallbacks.find(audioIo);
731 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700732 return INVALID_OPERATION;
733 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700734 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700735 return INVALID_OPERATION;
736 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700737 if (it->second.size() == 0) {
738 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700739 }
740 return NO_ERROR;
741}
742
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800743/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700744{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800745 Mutex::Autolock _l(gLockErrorCallbacks);
746 gAudioErrorCallbacks.insert(cb);
747 return reinterpret_cast<uintptr_t>(cb);
748}
749
750/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
751 Mutex::Autolock _l(gLockErrorCallbacks);
752 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
753}
754
755/* static */ void AudioSystem::reportError(status_t err) {
756 Mutex::Autolock _l(gLockErrorCallbacks);
757 for (auto callback : gAudioErrorCallbacks) {
758 callback(err);
759 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760}
761
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700762/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
763{
764 Mutex::Autolock _l(gLock);
765 gDynPolicyCallback = cb;
766}
767
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800768/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
769{
770 Mutex::Autolock _l(gLock);
771 gRecordConfigCallback = cb;
772}
773
Eric Laurentc2f1f072009-07-17 12:17:14 -0700774// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800775// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700776sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
777sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
778
779
Glenn Kasten18a6d902012-09-24 11:27:56 -0700780// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800781const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700782{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800783 sp<IAudioPolicyService> ap;
784 sp<AudioPolicyServiceClient> apc;
785 {
786 Mutex::Autolock _l(gLockAPS);
787 if (gAudioPolicyService == 0) {
788 sp<IServiceManager> sm = defaultServiceManager();
789 sp<IBinder> binder;
790 do {
791 binder = sm->getService(String16("media.audio_policy"));
792 if (binder != 0)
793 break;
794 ALOGW("AudioPolicyService not published, waiting...");
795 usleep(500000); // 0.5 s
796 } while (true);
797 if (gAudioPolicyServiceClient == NULL) {
798 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
799 }
800 binder->linkToDeath(gAudioPolicyServiceClient);
801 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
802 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
803 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700804 // Make sure callbacks can be received by gAudioPolicyServiceClient
805 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700806 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800807 ap = gAudioPolicyService;
808 }
809 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200810 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800811 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200812 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100813 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200814 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700815 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800816
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800817 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700818}
819
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700820// ---------------------------------------------------------------------------
821
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000822void AudioSystem::onNewAudioModulesAvailable()
823{
824 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
825 if (aps == 0) return;
826 aps->onNewAudioModulesAvailable();
827}
828
Dima Zavinfce7a472011-04-19 22:30:36 -0700829status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
830 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800831 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800832 const char *device_name,
833 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700834{
835 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700836 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800837 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700838
Eric Laurentc2f1f072009-07-17 12:17:14 -0700839 if (aps == 0) return PERMISSION_DENIED;
840
Eric Laurent71b63e32011-09-02 14:20:56 -0700841 if (device_address != NULL) {
842 address = device_address;
843 }
Paul McLeane743a472015-01-28 11:07:31 -0800844 if (device_name != NULL) {
845 name = device_name;
846 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800847 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700848}
849
Dima Zavinfce7a472011-04-19 22:30:36 -0700850audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700851 const char *device_address)
852{
853 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700854 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855
856 return aps->getDeviceConnectionState(device, device_address);
857}
858
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800859status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
860 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800861 const char *device_name,
862 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800863{
864 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
865 const char *address = "";
866 const char *name = "";
867
868 if (aps == 0) return PERMISSION_DENIED;
869
870 if (device_address != NULL) {
871 address = device_address;
872 }
873 if (device_name != NULL) {
874 name = device_name;
875 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800876 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800877}
878
Eric Laurent00dba062020-02-11 15:52:09 -0800879status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700880{
Glenn Kasten347966c2012-01-18 14:58:32 -0800881 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
883 if (aps == 0) return PERMISSION_DENIED;
884
Eric Laurent00dba062020-02-11 15:52:09 -0800885 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700886}
887
Dima Zavinfce7a472011-04-19 22:30:36 -0700888status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700889{
890 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
891 if (aps == 0) return PERMISSION_DENIED;
892 return aps->setForceUse(usage, config);
893}
894
Dima Zavinfce7a472011-04-19 22:30:36 -0700895audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700898 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700899 return aps->getForceUse(usage);
900}
901
902
Eric Laurentf4e63452017-11-06 19:31:46 +0000903audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
906 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000907 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700908}
909
Eric Laurent42984412019-05-09 17:57:03 -0700910status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800911 audio_io_handle_t *output,
912 audio_session_t session,
913 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200914 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700915 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800916 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800917 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700918 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800919 audio_port_handle_t *portId,
920 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700921{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700922 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800923 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200924 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700925 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800926 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700927}
928
Eric Laurentd7fe0862018-07-14 16:48:01 -0700929status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700930{
931 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
932 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700933 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700934}
935
Eric Laurentd7fe0862018-07-14 16:48:01 -0700936status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700937{
938 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
939 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700940 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941}
942
Eric Laurentd7fe0862018-07-14 16:48:01 -0700943void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
946 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700947 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948}
949
Eric Laurentcaf7f482014-11-25 17:50:47 -0800950status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
951 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700952 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800953 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700954 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700955 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800956 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800957 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600958 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700959 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800960 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700961{
962 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800963 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600964 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700965 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800966 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700967}
968
Eric Laurent4eb58f12018-12-07 16:41:02 -0800969status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700970{
971 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
972 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800973 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974}
975
Eric Laurentfee19762018-01-29 18:44:13 -0800976status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700977{
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800980 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981}
982
Eric Laurentfee19762018-01-29 18:44:13 -0800983void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984{
985 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
986 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800987 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988}
989
Dima Zavinfce7a472011-04-19 22:30:36 -0700990status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700991 int indexMin,
992 int indexMax)
993{
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
996 return aps->initStreamVolume(stream, indexMin, indexMax);
997}
998
Eric Laurent83844cc2011-11-18 16:43:31 -0800999status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1000 int index,
1001 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001005 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006}
1007
Eric Laurent83844cc2011-11-18 16:43:31 -08001008status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
1009 int *index,
1010 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001011{
1012 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1013 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001014 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001015}
1016
François Gaffiecfe17322018-11-07 13:41:29 +01001017status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
1018 int index,
1019 audio_devices_t device)
1020{
1021 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1022 if (aps == 0) return PERMISSION_DENIED;
1023 return aps->setVolumeIndexForAttributes(attr, index, device);
1024}
1025
1026status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1027 int &index,
1028 audio_devices_t device)
1029{
1030 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1031 if (aps == 0) return PERMISSION_DENIED;
1032 return aps->getVolumeIndexForAttributes(attr, index, device);
1033}
1034
1035status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1036{
1037 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1038 if (aps == 0) return PERMISSION_DENIED;
1039 return aps->getMaxVolumeIndexForAttributes(attr, index);
1040}
1041
1042status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1043{
1044 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1045 if (aps == 0) return PERMISSION_DENIED;
1046 return aps->getMinVolumeIndexForAttributes(attr, index);
1047}
1048
Dima Zavinfce7a472011-04-19 22:30:36 -07001049uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001050{
1051 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001052 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001053 return aps->getStrategyForStream(stream);
1054}
1055
Eric Laurent63742522012-03-08 13:42:42 -08001056audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001057{
1058 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001059 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001060 return aps->getDevicesForStream(stream);
1061}
1062
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001063status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1064 AudioDeviceTypeAddrVector *devices) {
1065 if (devices == nullptr) {
1066 return BAD_VALUE;
1067 }
1068 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1069 if (aps == 0) return PERMISSION_DENIED;
1070 return aps->getDevicesForAttributes(aa, devices);
1071}
1072
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001073audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001074{
1075 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001076 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001077 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001078 return aps->getOutputForEffect(desc);
1079}
1080
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001081status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001082 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001083 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001084 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001085 int id)
1086{
1087 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1088 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001089 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001090}
1091
1092status_t AudioSystem::unregisterEffect(int id)
1093{
1094 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1095 if (aps == 0) return PERMISSION_DENIED;
1096 return aps->unregisterEffect(id);
1097}
1098
Eric Laurentdb7c0792011-08-10 10:37:50 -07001099status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1100{
1101 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1102 if (aps == 0) return PERMISSION_DENIED;
1103 return aps->setEffectEnabled(id, enabled);
1104}
1105
Eric Laurent6c796322019-04-09 14:13:17 -07001106status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1107{
1108 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1109 if (aps == 0) return PERMISSION_DENIED;
1110 return aps->moveEffectsToIo(ids, io);
1111}
1112
Glenn Kastenfff6d712012-01-12 16:38:12 -08001113status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001114{
Eric Laurenteda6c362011-02-02 09:33:30 -08001115 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1116 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001117 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001118 *state = aps->isStreamActive(stream, inPastMs);
1119 return NO_ERROR;
1120}
1121
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001122status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1123 uint32_t inPastMs)
1124{
1125 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1126 if (aps == 0) return PERMISSION_DENIED;
1127 if (state == NULL) return BAD_VALUE;
1128 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1129 return NO_ERROR;
1130}
1131
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001132status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1133{
1134 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1135 if (aps == 0) return PERMISSION_DENIED;
1136 if (state == NULL) return BAD_VALUE;
1137 *state = aps->isSourceActive(stream);
1138 return NO_ERROR;
1139}
1140
Glenn Kasten3b16c762012-11-14 08:44:39 -08001141uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001142{
1143 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1144 if (af == 0) return 0;
1145 return af->getPrimaryOutputSamplingRate();
1146}
1147
Glenn Kastene33054e2012-11-14 12:54:39 -08001148size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001149{
1150 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1151 if (af == 0) return 0;
1152 return af->getPrimaryOutputFrameCount();
1153}
Eric Laurenteda6c362011-02-02 09:33:30 -08001154
Andy Hung6f248bb2018-01-23 14:04:37 -08001155status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001156{
1157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1158 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001159 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001160}
1161
Eric Laurent9f6530f2011-08-30 10:18:54 -07001162void AudioSystem::clearAudioConfigCache()
1163{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001164 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001166 {
1167 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001168 if (gAudioFlingerClient != 0) {
1169 gAudioFlingerClient->clearIoCache();
1170 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001171 gAudioFlinger.clear();
1172 }
1173 {
1174 Mutex::Autolock _l(gLockAPS);
1175 gAudioPolicyService.clear();
1176 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001177}
1178
Hayden Gomes524159d2019-12-23 14:41:47 -08001179status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1180 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1181 if (aps == nullptr) return PERMISSION_DENIED;
1182 return aps->setSupportedSystemUsages(systemUsages);
1183}
1184
Kevin Rocardb99cc752019-03-21 20:52:24 -07001185status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == nullptr) return PERMISSION_DENIED;
1188 return aps->setAllowedCapturePolicy(uid, flags);
1189}
1190
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001191bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1192{
1193 ALOGV("isOffloadSupported()");
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return false;
1196 return aps->isOffloadSupported(info);
1197}
1198
Eric Laurent203b1a12014-04-01 10:34:16 -07001199status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1200 audio_port_type_t type,
1201 unsigned int *num_ports,
jiabin19cdba52020-11-24 11:28:58 -08001202 struct audio_port_v7 *ports,
Eric Laurent203b1a12014-04-01 10:34:16 -07001203 unsigned int *generation)
1204{
1205 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1206 if (aps == 0) return PERMISSION_DENIED;
1207 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1208}
1209
jiabin19cdba52020-11-24 11:28:58 -08001210status_t AudioSystem::getAudioPort(struct audio_port_v7 *port)
Eric Laurent203b1a12014-04-01 10:34:16 -07001211{
1212 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1213 if (aps == 0) return PERMISSION_DENIED;
1214 return aps->getAudioPort(port);
1215}
1216
1217status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1218 audio_patch_handle_t *handle)
1219{
1220 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1221 if (aps == 0) return PERMISSION_DENIED;
1222 return aps->createAudioPatch(patch, handle);
1223}
1224
1225status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1226{
1227 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1228 if (aps == 0) return PERMISSION_DENIED;
1229 return aps->releaseAudioPatch(handle);
1230}
1231
1232status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1233 struct audio_patch *patches,
1234 unsigned int *generation)
1235{
1236 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1237 if (aps == 0) return PERMISSION_DENIED;
1238 return aps->listAudioPatches(num_patches, patches, generation);
1239}
1240
1241status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1242{
1243 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1244 if (aps == 0) return PERMISSION_DENIED;
1245 return aps->setAudioPortConfig(config);
1246}
1247
Eric Laurent296fb132015-05-01 11:38:42 -07001248status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001249{
Eric Laurentb28753e2015-04-01 13:06:28 -07001250 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1251 if (aps == 0) return PERMISSION_DENIED;
1252
1253 Mutex::Autolock _l(gLockAPS);
1254 if (gAudioPolicyServiceClient == 0) {
1255 return NO_INIT;
1256 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001257 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1258 if (ret == 1) {
1259 aps->setAudioPortCallbacksEnabled(true);
1260 }
1261 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001262}
1263
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001264/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001265status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001266{
1267 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1268 if (aps == 0) return PERMISSION_DENIED;
1269
1270 Mutex::Autolock _l(gLockAPS);
1271 if (gAudioPolicyServiceClient == 0) {
1272 return NO_INIT;
1273 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001274 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1275 if (ret == 0) {
1276 aps->setAudioPortCallbacksEnabled(false);
1277 }
1278 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001279}
1280
François Gaffiecfe17322018-11-07 13:41:29 +01001281status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1282{
1283 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1284 if (aps == 0) return PERMISSION_DENIED;
1285
1286 Mutex::Autolock _l(gLockAPS);
1287 if (gAudioPolicyServiceClient == 0) {
1288 return NO_INIT;
1289 }
1290 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1291 if (ret == 1) {
1292 aps->setAudioVolumeGroupCallbacksEnabled(true);
1293 }
1294 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1295}
1296
1297status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1298{
1299 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1300 if (aps == 0) return PERMISSION_DENIED;
1301
1302 Mutex::Autolock _l(gLockAPS);
1303 if (gAudioPolicyServiceClient == 0) {
1304 return NO_INIT;
1305 }
1306 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1307 if (ret == 0) {
1308 aps->setAudioVolumeGroupCallbacksEnabled(false);
1309 }
1310 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1311}
1312
Eric Laurent296fb132015-05-01 11:38:42 -07001313status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001314 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1315 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001316{
1317 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1318 if (afc == 0) {
1319 return NO_INIT;
1320 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001321 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001322 if (status == NO_ERROR) {
1323 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1324 if (af != 0) {
1325 af->registerClient(afc);
1326 }
1327 }
1328 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001329}
1330
1331status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001332 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1333 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001334{
1335 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1336 if (afc == 0) {
1337 return NO_INIT;
1338 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001339 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001340}
1341
1342audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1343{
1344 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1345 if (af == 0) return PERMISSION_DENIED;
1346 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1347 if (desc == 0) {
1348 return AUDIO_PORT_HANDLE_NONE;
1349 }
1350 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001351}
1352
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001353status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1354 audio_io_handle_t *ioHandle,
1355 audio_devices_t *device)
1356{
1357 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1358 if (aps == 0) return PERMISSION_DENIED;
1359 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1360}
1361
1362status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->releaseSoundTriggerSession(session);
1367}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001368
1369audio_mode_t AudioSystem::getPhoneState()
1370{
1371 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1372 if (aps == 0) return AUDIO_MODE_INVALID;
1373 return aps->getPhoneState();
1374}
1375
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001376status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001377{
1378 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1379 if (aps == 0) return PERMISSION_DENIED;
1380 return aps->registerPolicyMixes(mixes, registration);
1381}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001382
jiabin6a02d532020-08-07 11:56:38 -07001383status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices)
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001384{
1385 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1386 if (aps == 0) return PERMISSION_DENIED;
1387 return aps->setUidDeviceAffinities(uid, devices);
1388}
1389
1390status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1391 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1392 if (aps == 0) return PERMISSION_DENIED;
1393 return aps->removeUidDeviceAffinities(uid);
1394}
1395
Oscar Azucena90e77632019-11-27 17:12:28 -08001396status_t AudioSystem::setUserIdDeviceAffinities(int userId,
jiabin6a02d532020-08-07 11:56:38 -07001397 const AudioDeviceTypeAddrVector& devices)
Oscar Azucena90e77632019-11-27 17:12:28 -08001398{
1399 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1400 if (aps == 0) return PERMISSION_DENIED;
1401 return aps->setUserIdDeviceAffinities(userId, devices);
1402}
1403
1404status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1405{
1406 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1407 if (aps == 0) return PERMISSION_DENIED;
1408 return aps->removeUserIdDeviceAffinities(userId);
1409}
1410
Eric Laurent554a2772015-04-10 11:29:24 -07001411status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1412 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001413 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001414{
1415 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1416 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001417 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001418}
1419
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001420status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001421{
1422 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1423 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001424 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001425}
1426
Andy Hung2ddee192015-12-18 17:34:44 -08001427status_t AudioSystem::setMasterMono(bool mono)
1428{
1429 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1430 if (aps == 0) return PERMISSION_DENIED;
1431 return aps->setMasterMono(mono);
1432}
1433
1434status_t AudioSystem::getMasterMono(bool *mono)
1435{
1436 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1437 if (aps == 0) return PERMISSION_DENIED;
1438 return aps->getMasterMono(mono);
1439}
1440
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001441status_t AudioSystem::setMasterBalance(float balance)
1442{
1443 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1444 if (af == 0) return PERMISSION_DENIED;
1445 return af->setMasterBalance(balance);
1446}
1447
1448status_t AudioSystem::getMasterBalance(float *balance)
1449{
1450 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1451 if (af == 0) return PERMISSION_DENIED;
1452 return af->getMasterBalance(balance);
1453}
1454
Eric Laurentac9cef52017-06-09 15:46:26 -07001455float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1456{
1457 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1458 if (aps == 0) return NAN;
1459 return aps->getStreamVolumeDB(stream, index, device);
1460}
1461
jiabin46a76fa2018-01-05 10:18:21 -08001462status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1463{
1464 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1465 if (af == 0) return PERMISSION_DENIED;
1466 return af->getMicrophones(microphones);
1467}
1468
Eric Laurent42896a02019-09-27 15:40:33 -07001469status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1470 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1471 if (af == nullptr) return PERMISSION_DENIED;
1472 return af->setAudioHalPids(pids);
1473}
1474
jiabin81772902018-04-02 17:52:27 -07001475status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1476 audio_format_t *surroundFormats,
1477 bool *surroundFormatsEnabled,
1478 bool reported)
1479{
1480 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1481 if (aps == 0) return PERMISSION_DENIED;
1482 return aps->getSurroundFormats(
1483 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1484}
1485
1486status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1487{
1488 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1489 if (aps == 0) return PERMISSION_DENIED;
1490 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1491}
1492
Eric Laurentb78763e2018-10-17 10:08:02 -07001493status_t AudioSystem::setAssistantUid(uid_t uid)
1494{
1495 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1496 if (aps == 0) return PERMISSION_DENIED;
1497
1498 return aps->setAssistantUid(uid);
1499}
1500
1501status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1502{
1503 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1504 if (aps == 0) return PERMISSION_DENIED;
1505
1506 return aps->setA11yServicesUids(uids);
1507}
1508
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001509status_t AudioSystem::setCurrentImeUid(uid_t uid)
1510{
1511 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1512 if (aps == 0) return PERMISSION_DENIED;
1513
1514 return aps->setCurrentImeUid(uid);
1515}
1516
jiabin6012f912018-11-02 17:06:30 -07001517bool AudioSystem::isHapticPlaybackSupported()
1518{
1519 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1520 if (aps == 0) return false;
1521 return aps->isHapticPlaybackSupported();
1522}
1523
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001524status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001525 std::vector<audio_format_t> *formats) {
1526 const sp <IAudioPolicyService>
1527 & aps = AudioSystem::get_audio_policy_service();
1528 if (aps == 0) return PERMISSION_DENIED;
1529 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1530}
1531
1532status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001533{
1534 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1535 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001536 return aps->listAudioProductStrategies(strategies);
1537}
1538
1539audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1540{
1541 AudioProductStrategyVector strategies;
1542 listAudioProductStrategies(strategies);
1543 for (const auto &strategy : strategies) {
1544 auto attrVect = strategy.getAudioAttributes();
1545 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1546 return attributes.getStreamType() == stream; });
1547 if (iter != end(attrVect)) {
1548 return iter->getAttributes();
1549 }
1550 }
1551 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1552 return AUDIO_ATTRIBUTES_INITIALIZER;
1553}
1554
1555audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1556{
François Gaffie4b2018b2018-11-07 11:18:59 +01001557 product_strategy_t psId;
1558 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1559 if (ret != NO_ERROR) {
1560 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1561 return AUDIO_STREAM_MUSIC;
1562 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001563 AudioProductStrategyVector strategies;
1564 listAudioProductStrategies(strategies);
1565 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001566 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001567 auto attrVect = strategy.getAudioAttributes();
1568 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1569 return AudioProductStrategy::attributesMatches(
1570 refAttr.getAttributes(), attr); });
1571 if (iter != end(attrVect)) {
1572 return iter->getStreamType();
1573 }
1574 }
1575 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001576 switch (attr.usage) {
1577 case AUDIO_USAGE_VIRTUAL_SOURCE:
1578 // virtual source is not expected to have an associated product strategy
1579 break;
1580 default:
1581 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1582 break;
1583 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001584 return AUDIO_STREAM_MUSIC;
1585}
1586
François Gaffie4b2018b2018-11-07 11:18:59 +01001587status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1588 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001589{
1590 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001591 if (aps == 0) return PERMISSION_DENIED;
1592 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1593}
1594
1595status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1596{
1597 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1598 if (aps == 0) return PERMISSION_DENIED;
1599 return aps->listAudioVolumeGroups(groups);
1600}
1601
1602status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1603 volume_group_t &volumeGroup)
1604{
1605 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1606 if (aps == 0) return PERMISSION_DENIED;
1607 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001608}
Eric Laurentb78763e2018-10-17 10:08:02 -07001609
Eric Laurent6ede98f2019-06-11 14:50:30 -07001610status_t AudioSystem::setRttEnabled(bool enabled)
1611{
1612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1613 if (aps == 0) return PERMISSION_DENIED;
1614 return aps->setRttEnabled(enabled);
1615}
1616
Eric Laurent8340e672019-11-06 11:01:08 -08001617bool AudioSystem::isCallScreenModeSupported()
1618{
1619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1620 if (aps == 0) return false;
1621 return aps->isCallScreenModeSupported();
1622}
1623
jiabin0a488932020-08-07 17:32:40 -07001624status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
1625 device_role_t role,
1626 const AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001627{
1628 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1629 if (aps == 0) {
1630 return PERMISSION_DENIED;
1631 }
jiabin0a488932020-08-07 17:32:40 -07001632 return aps->setDevicesRoleForStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001633}
1634
jiabin0a488932020-08-07 17:32:40 -07001635status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001636{
1637 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1638 if (aps == 0) {
1639 return PERMISSION_DENIED;
1640 }
jiabin0a488932020-08-07 17:32:40 -07001641 return aps->removeDevicesRoleForStrategy(strategy, role);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001642}
1643
jiabin0a488932020-08-07 17:32:40 -07001644status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
1645 device_role_t role,
1646 AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001647{
1648 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1649 if (aps == 0) {
1650 return PERMISSION_DENIED;
1651 }
jiabin0a488932020-08-07 17:32:40 -07001652 return aps->getDevicesForRoleAndStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001653}
1654
Jiabin Huang3b98d322020-09-03 17:54:16 +00001655status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
1656 device_role_t role,
1657 const AudioDeviceTypeAddrVector &devices)
1658{
1659 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1660 if (aps == 0) {
1661 return PERMISSION_DENIED;
1662 }
1663 return aps->setDevicesRoleForCapturePreset(audioSource, role, devices);
1664}
1665
1666status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
1667 device_role_t role,
1668 const AudioDeviceTypeAddrVector &devices)
1669{
1670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1671 if (aps == 0) {
1672 return PERMISSION_DENIED;
1673 }
1674 return aps->addDevicesRoleForCapturePreset(audioSource, role, devices);
1675}
1676
1677status_t AudioSystem::removeDevicesRoleForCapturePreset(
1678 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices)
1679{
1680 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1681 if (aps == 0) {
1682 return PERMISSION_DENIED;
1683 }
1684 return aps->removeDevicesRoleForCapturePreset(audioSource, role, devices);
1685}
1686
1687status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
1688 device_role_t role)
1689{
1690 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1691 if (aps == 0) {
1692 return PERMISSION_DENIED;
1693 }
1694 return aps->clearDevicesRoleForCapturePreset(audioSource, role);
1695}
1696
1697status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
1698 device_role_t role,
1699 AudioDeviceTypeAddrVector &devices)
1700{
1701 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1702 if (aps == 0) {
1703 return PERMISSION_DENIED;
1704 }
1705 return aps->getDevicesForRoleAndCapturePreset(audioSource, role, devices);
1706}
1707
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001708class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1709 public IBinder::DeathRecipient {
1710public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001711 CaptureStateListenerImpl(
1712 const sp<IAudioPolicyService>& aps,
1713 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001714 : mAps(aps), mListener(listener) {}
1715
1716 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001717 bool active;
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001718 status_t status = mAps->registerSoundTriggerCaptureStateListener(this, &active);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001719 if (status != NO_ERROR) {
1720 mListener->onServiceDied();
1721 return;
1722 }
1723 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001724 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001725 }
1726
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001727 binder::Status setCaptureState(bool active) override {
1728 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001729 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001730 return binder::Status::ok();
1731 }
1732
1733 void binderDied(const wp<IBinder>&) override {
1734 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001735 mListener->onServiceDied();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001736 gSoundTriggerCaptureStateListener = nullptr;
1737 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001738
1739private:
1740 // Need this in order to keep the death receipent alive.
1741 sp<IAudioPolicyService> mAps;
1742 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001743};
1744
1745status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1746 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001747 LOG_ALWAYS_FATAL_IF(listener == nullptr);
1748
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001749 const sp<IAudioPolicyService>& aps =
1750 AudioSystem::get_audio_policy_service();
1751 if (aps == 0) {
1752 return PERMISSION_DENIED;
1753 }
1754
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001755 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001756 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001757 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001758
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001759 return NO_ERROR;
1760}
1761
Eric Laurentc2f1f072009-07-17 12:17:14 -07001762// ---------------------------------------------------------------------------
1763
Eric Laurente8726fe2015-06-26 09:39:24 -07001764int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001765 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001766{
1767 Mutex::Autolock _l(mLock);
1768 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001769 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001770 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001771 }
1772 }
Eric Laurent296fb132015-05-01 11:38:42 -07001773 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001774 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001775}
1776
Eric Laurente8726fe2015-06-26 09:39:24 -07001777int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001778 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001779{
1780 Mutex::Autolock _l(mLock);
1781 size_t i;
1782 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001783 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001784 break;
1785 }
1786 }
1787 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001788 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001789 }
1790 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001791 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001792}
1793
Eric Laurent296fb132015-05-01 11:38:42 -07001794
Eric Laurentb28753e2015-04-01 13:06:28 -07001795void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1796{
1797 Mutex::Autolock _l(mLock);
1798 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1799 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1800 }
1801}
1802
1803void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1804{
1805 Mutex::Autolock _l(mLock);
1806 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1807 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1808 }
1809}
1810
François Gaffiecfe17322018-11-07 13:41:29 +01001811// ----------------------------------------------------------------------------
1812int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1813 const sp<AudioVolumeGroupCallback>& callback)
1814{
1815 Mutex::Autolock _l(mLock);
1816 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1817 if (mAudioVolumeGroupCallback[i] == callback) {
1818 return -1;
1819 }
1820 }
1821 mAudioVolumeGroupCallback.add(callback);
1822 return mAudioVolumeGroupCallback.size();
1823}
1824
1825int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1826 const sp<AudioVolumeGroupCallback>& callback)
1827{
1828 Mutex::Autolock _l(mLock);
1829 size_t i;
1830 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1831 if (mAudioVolumeGroupCallback[i] == callback) {
1832 break;
1833 }
1834 }
1835 if (i == mAudioVolumeGroupCallback.size()) {
1836 return -1;
1837 }
1838 mAudioVolumeGroupCallback.removeAt(i);
1839 return mAudioVolumeGroupCallback.size();
1840}
1841
1842void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1843 int flags)
1844{
1845 Mutex::Autolock _l(mLock);
1846 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1847 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1848 }
1849}
1850// ----------------------------------------------------------------------------
1851
Jean-Michel Trivide801052015-04-14 19:10:14 -07001852void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1853 String8 regId, int32_t state)
1854{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001855 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1856 dynamic_policy_callback cb = NULL;
1857 {
1858 Mutex::Autolock _l(AudioSystem::gLock);
1859 cb = gDynPolicyCallback;
1860 }
1861
1862 if (cb != NULL) {
1863 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1864 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001865}
1866
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001867void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001868 int event,
1869 const record_client_info_t *clientInfo,
1870 const audio_config_base_t *clientConfig,
1871 std::vector<effect_descriptor_t> clientEffects,
1872 const audio_config_base_t *deviceConfig,
1873 std::vector<effect_descriptor_t> effects,
1874 audio_patch_handle_t patchHandle,
1875 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001876 record_config_callback cb = NULL;
1877 {
1878 Mutex::Autolock _l(AudioSystem::gLock);
1879 cb = gRecordConfigCallback;
1880 }
1881
1882 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001883 cb(event, clientInfo, clientConfig, clientEffects,
1884 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001885 }
1886}
1887
Glenn Kasten4944acb2013-08-19 08:39:20 -07001888void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1889{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001890 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001891 Mutex::Autolock _l(mLock);
1892 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1893 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001894 }
François Gaffiecfe17322018-11-07 13:41:29 +01001895 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1896 mAudioVolumeGroupCallback[i]->onServiceDied();
1897 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001898 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001899 {
1900 Mutex::Autolock _l(gLockAPS);
1901 AudioSystem::gAudioPolicyService.clear();
1902 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001903
Steve Block5ff1dd52012-01-05 23:22:43 +00001904 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001905}
1906
Glenn Kasten40bc9062015-03-20 09:09:33 -07001907} // namespace android