blob: f030ab0e8828feb1ba693a0bd341b53a62116e11 [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>
Mathias Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070022#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020023#include <binder/IPCThreadState.h>
Eric Laurent21da6472017-11-09 16:29:26 -080024#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070026#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070027#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010028#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029#include <math.h>
30
Dima Zavin64760242011-05-11 14:15:23 -070031#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Eric Laurentc2f1f072009-07-17 12:17:14 -070033// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035namespace android {
36
37// client singleton for AudioFlinger binder interface
38Mutex AudioSystem::gLock;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080039Mutex AudioSystem::gLockErrorCallbacks;
Glenn Kastend2d089f2014-11-05 11:48:12 -080040Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041sp<IAudioFlinger> AudioSystem::gAudioFlinger;
42sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080043std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070044dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080045record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080048const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080050 sp<IAudioFlinger> af;
51 sp<AudioFlingerClient> afc;
52 {
53 Mutex::Autolock _l(gLock);
54 if (gAudioFlinger == 0) {
55 sp<IServiceManager> sm = defaultServiceManager();
56 sp<IBinder> binder;
57 do {
58 binder = sm->getService(String16("media.audio_flinger"));
59 if (binder != 0)
60 break;
61 ALOGW("AudioFlinger not published, waiting...");
62 usleep(500000); // 0.5 s
63 } while (true);
64 if (gAudioFlingerClient == NULL) {
65 gAudioFlingerClient = new AudioFlingerClient();
66 } else {
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080067 reportError(NO_ERROR);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080069 binder->linkToDeath(gAudioFlingerClient);
70 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
71 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
72 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070073 // Make sure callbacks can be received by gAudioFlingerClient
74 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070075 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080076 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080078 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020079 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080080 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020081 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080082 }
83 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084}
85
Eric Laurent296fb132015-05-01 11:38:42 -070086const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
87{
88 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
89 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
90 if (af == 0) return 0;
91 Mutex::Autolock _l(gLock);
92 return gAudioFlingerClient;
93}
94
95sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
96{
97 sp<AudioIoDescriptor> desc;
98 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
99 if (afc != 0) {
100 desc = afc->getIoDescriptor(ioHandle);
101 }
102 return desc;
103}
104
Eric Laurent46291612013-07-18 14:38:44 -0700105/* static */ status_t AudioSystem::checkAudioFlinger()
106{
107 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
108 return NO_ERROR;
109 }
110 return DEAD_OBJECT;
111}
112
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700113// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
114
Glenn Kasten4944acb2013-08-19 08:39:20 -0700115status_t AudioSystem::muteMicrophone(bool state)
116{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
118 if (af == 0) return PERMISSION_DENIED;
119 return af->setMicMute(state);
120}
121
Glenn Kasten4944acb2013-08-19 08:39:20 -0700122status_t AudioSystem::isMicrophoneMuted(bool* state)
123{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
125 if (af == 0) return PERMISSION_DENIED;
126 *state = af->getMicMute();
127 return NO_ERROR;
128}
129
130status_t AudioSystem::setMasterVolume(float value)
131{
132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
133 if (af == 0) return PERMISSION_DENIED;
134 af->setMasterVolume(value);
135 return NO_ERROR;
136}
137
138status_t AudioSystem::setMasterMute(bool mute)
139{
140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
141 if (af == 0) return PERMISSION_DENIED;
142 af->setMasterMute(mute);
143 return NO_ERROR;
144}
145
146status_t AudioSystem::getMasterVolume(float* volume)
147{
148 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
149 if (af == 0) return PERMISSION_DENIED;
150 *volume = af->masterVolume();
151 return NO_ERROR;
152}
153
154status_t AudioSystem::getMasterMute(bool* mute)
155{
156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
157 if (af == 0) return PERMISSION_DENIED;
158 *mute = af->masterMute();
159 return NO_ERROR;
160}
161
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800162status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
163 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164{
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
167 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700168 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 return NO_ERROR;
170}
171
Glenn Kastenfff6d712012-01-12 16:38:12 -0800172status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
177 af->setStreamMute(stream, mute);
178 return NO_ERROR;
179}
180
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800181status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
182 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183{
Dima Zavinfce7a472011-04-19 22:30:36 -0700184 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
186 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700187 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 return NO_ERROR;
189}
190
Glenn Kastenfff6d712012-01-12 16:38:12 -0800191status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192{
Dima Zavinfce7a472011-04-19 22:30:36 -0700193 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
195 if (af == 0) return PERMISSION_DENIED;
196 *mute = af->streamMute(stream);
197 return NO_ERROR;
198}
199
Glenn Kastenf78aee72012-01-04 11:00:47 -0800200status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800202 if (uint32_t(mode) >= AUDIO_MODE_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;
205 return af->setMode(mode);
206}
207
Glenn Kasten4944acb2013-08-19 08:39:20 -0700208status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
209{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
211 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700212 return af->setParameters(ioHandle, keyValuePairs);
213}
214
Glenn Kasten4944acb2013-08-19 08:39:20 -0700215String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
216{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700217 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
218 String8 result = String8("");
219 if (af == 0) return result;
220
221 result = af->getParameters(ioHandle, keys);
222 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223}
224
Glenn Kastenc23885e2013-12-19 16:35:18 -0800225status_t AudioSystem::setParameters(const String8& keyValuePairs)
226{
Glenn Kasten142f5192014-03-25 17:44:59 -0700227 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800228}
229
230String8 AudioSystem::getParameters(const String8& keys)
231{
Glenn Kasten142f5192014-03-25 17:44:59 -0700232 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800233}
234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235// convert volume steps to natural log scale
236
237// change this value to change volume scaling
238static const float dBPerStep = 0.5f;
239// shouldn't need to touch these
240static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
241static const float dBConvertInverse = 1.0f / dBConvert;
242
243float AudioSystem::linearToLog(int volume)
244{
245 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000246 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 // return v;
248 return volume ? exp(float(100 - volume) * dBConvert) : 0;
249}
250
251int AudioSystem::logToLinear(float volume)
252{
253 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000254 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 // return v;
256 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
257}
258
Eric Laurent21da6472017-11-09 16:29:26 -0800259/* static */ size_t AudioSystem::calculateMinFrameCount(
260 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
261 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
262{
263 // Ensure that buffer depth covers at least audio hardware latency
264 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
265 if (minBufCount < 2) {
266 minBufCount = 2;
267 }
268#if 0
269 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
270 // but keeping the code here to make it easier to add later.
271 if (minBufCount < notificationsPerBufferReq) {
272 minBufCount = notificationsPerBufferReq;
273 }
274#endif
275 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
276 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
277 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
278 /*, notificationsPerBufferReq*/);
279 return minBufCount * sourceFramesNeededWithTimestretch(
280 sampleRate, afFrameCount, afSampleRate, speed);
281}
282
283
Glenn Kasten3b16c762012-11-14 08:44:39 -0800284status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 if (streamType == AUDIO_STREAM_DEFAULT) {
289 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 }
291
Glenn Kastenfff6d712012-01-12 16:38:12 -0800292 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293 if (output == 0) {
294 return PERMISSION_DENIED;
295 }
296
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700297 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700298}
299
Glenn Kasten2c073da2016-02-26 09:14:08 -0800300status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800301 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700302{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800303 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
304 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800305 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
306 if (desc == 0) {
307 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800309 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800311 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800312 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800313 return BAD_VALUE;
314 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315
Glenn Kasten2c073da2016-02-26 09:14:08 -0800316 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return NO_ERROR;
319}
320
Glenn Kastene33054e2012-11-14 12:54:39 -0800321status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324
Dima Zavinfce7a472011-04-19 22:30:36 -0700325 if (streamType == AUDIO_STREAM_DEFAULT) {
326 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700328
Glenn Kastenfff6d712012-01-12 16:38:12 -0800329 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700330 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 return PERMISSION_DENIED;
332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700334 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700335}
336
Glenn Kasten2c073da2016-02-26 09:14:08 -0800337status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800338 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700339{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
341 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800342 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
343 if (desc == 0) {
344 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800346 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700347 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800348 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800349 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800350 return BAD_VALUE;
351 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352
Glenn Kasten2c073da2016-02-26 09:14:08 -0800353 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 return NO_ERROR;
356}
357
Glenn Kastenfff6d712012-01-12 16:38:12 -0800358status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361
Dima Zavinfce7a472011-04-19 22:30:36 -0700362 if (streamType == AUDIO_STREAM_DEFAULT) {
363 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700365
Glenn Kastenfff6d712012-01-12 16:38:12 -0800366 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700367 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700368 return PERMISSION_DENIED;
369 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370
Glenn Kasten241618f2014-03-25 17:48:57 -0700371 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700372}
373
374status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700375 uint32_t* latency)
376{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800377 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
378 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700379 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700380 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700381 *latency = af->latency(output);
382 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700383 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700384 }
385
Glenn Kasten241618f2014-03-25 17:48:57 -0700386 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 return NO_ERROR;
389}
390
Glenn Kastendd8104c2012-07-02 12:42:44 -0700391status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
392 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393{
Eric Laurent296fb132015-05-01 11:38:42 -0700394 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
395 if (afc == 0) {
396 return NO_INIT;
397 }
398 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399}
400
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700401status_t AudioSystem::setVoiceVolume(float value)
402{
403 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
404 if (af == 0) return PERMISSION_DENIED;
405 return af->setVoiceVolume(value);
406}
407
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000408status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700409 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800410{
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return PERMISSION_DENIED;
413
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000414 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800415}
416
Glenn Kasten4944acb2013-08-19 08:39:20 -0700417uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
418{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800419 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800420 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800421 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700422 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800423
424 result = af->getInputFramesLost(ioHandle);
425 return result;
426}
427
Glenn Kasteneeecb982016-02-26 10:44:04 -0800428audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700429{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700430 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700431 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700432 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800433 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700434}
435
Andy Hung8b0bfd92019-12-23 13:11:11 -0800436void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700437{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700438 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
439 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800440 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700441 }
442}
443
Glenn Kastend848eb42016-03-08 13:42:11 -0800444void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700445{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700446 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
447 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800448 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700449 }
450}
451
Eric Laurent93c3d412014-08-01 14:48:35 -0700452audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
453{
454 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
455 if (af == 0) return AUDIO_HW_SYNC_INVALID;
456 return af->getAudioHwSyncForSession(sessionId);
457}
458
Eric Laurent72e3f392015-05-20 14:43:50 -0700459status_t AudioSystem::systemReady()
460{
461 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
462 if (af == 0) return NO_INIT;
463 return af->systemReady();
464}
465
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700466status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
467 size_t* frameCount)
468{
469 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
470 if (af == 0) return PERMISSION_DENIED;
471 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
472 if (desc == 0) {
473 *frameCount = af->frameCountHAL(ioHandle);
474 } else {
475 *frameCount = desc->mFrameCountHAL;
476 }
477 if (*frameCount == 0) {
478 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
479 return BAD_VALUE;
480 }
481
482 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
483
484 return NO_ERROR;
485}
486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487// ---------------------------------------------------------------------------
488
Eric Laurent73e26b62015-04-27 16:55:58 -0700489
490void AudioSystem::AudioFlingerClient::clearIoCache()
491{
492 Mutex::Autolock _l(mLock);
493 mIoDescriptors.clear();
494 mInBuffSize = 0;
495 mInSamplingRate = 0;
496 mInFormat = AUDIO_FORMAT_DEFAULT;
497 mInChannelMask = AUDIO_CHANNEL_NONE;
498}
499
Glenn Kasten4944acb2013-08-19 08:39:20 -0700500void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
501{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800502 {
503 Mutex::Autolock _l(AudioSystem::gLock);
504 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800505 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506
Eric Laurent73e26b62015-04-27 16:55:58 -0700507 // clear output handles and stream to output map caches
508 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800510 reportError(DEAD_OBJECT);
511
Steve Block5ff1dd52012-01-05 23:22:43 +0000512 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513}
514
Eric Laurent73e26b62015-04-27 16:55:58 -0700515void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
516 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100517 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Eric Laurent73e26b62015-04-27 16:55:58 -0700519 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700520
Eric Laurent296fb132015-05-01 11:38:42 -0700521 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700522 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700523 {
524 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700525 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700526
527 switch (event) {
528 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700529 case AUDIO_OUTPUT_REGISTERED:
530 case AUDIO_INPUT_OPENED:
531 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700532 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700533 if (oldDesc == 0) {
534 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
535 } else {
536 deviceId = oldDesc->getDeviceId();
537 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700538 }
Eric Laurent296fb132015-05-01 11:38:42 -0700539
540 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
541 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700542 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700543 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
544 if (it != mAudioDeviceCallbacks.end()) {
545 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100546 }
547 }
Eric Laurent296fb132015-05-01 11:38:42 -0700548 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700549 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
550 "frameCount %zu deviceId %d",
551 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
552 "output" : "input",
553 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
554 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700555 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
556 ioDesc->mFrameCount, ioDesc->getDeviceId());
557 } break;
558 case AUDIO_OUTPUT_CLOSED:
559 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700560 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700561 ALOGW("ioConfigChanged() closing unknown %s %d",
562 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
563 break;
564 }
565 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700566 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700567
Eric Laurent296fb132015-05-01 11:38:42 -0700568 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700569 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700570 } break;
571
572 case AUDIO_OUTPUT_CONFIG_CHANGED:
573 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700574 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700575 if (oldDesc == 0) {
576 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
577 break;
578 }
579
580 deviceId = oldDesc->getDeviceId();
581 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
582
583 if (deviceId != ioDesc->getDeviceId()) {
584 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700585 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
586 if (it != mAudioDeviceCallbacks.end()) {
587 callbacks = it->second;
588 }
Eric Laurent296fb132015-05-01 11:38:42 -0700589 }
590 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700591 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700592 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
593 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800594 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
595 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700596
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700598 case AUDIO_CLIENT_STARTED: {
599 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
600 if (oldDesc == 0) {
601 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
602 break;
603 }
604 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
605 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
606 oldDesc->mPatch = ioDesc->mPatch;
607 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
608 if (it != mAudioDeviceCallbacks.end()) {
609 auto cbks = it->second;
610 auto it2 = cbks.find(ioDesc->mPortId);
611 if (it2 != cbks.end()) {
612 callbacks.emplace(ioDesc->mPortId, it2->second);
613 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100614 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100615 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700616 } break;
617 }
618
619 for (auto wpCbk : callbacks) {
620 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
621 if (spCbk != nullptr) {
622 callbacksToCall.push_back(spCbk);
623 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700624 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800625 }
626
627 // Callbacks must be called without mLock held. May lead to dead lock if calling for
628 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700629 for (auto cb : callbacksToCall) {
630 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
631 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700632 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633}
634
Eric Laurent73e26b62015-04-27 16:55:58 -0700635status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
636 uint32_t sampleRate, audio_format_t format,
637 audio_channel_mask_t channelMask, size_t* buffSize)
638{
639 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
640 if (af == 0) {
641 return PERMISSION_DENIED;
642 }
643 Mutex::Autolock _l(mLock);
644 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
645 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
646 || (channelMask != mInChannelMask)) {
647 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
648 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800649 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700650 sampleRate, format, channelMask);
651 return BAD_VALUE;
652 }
653 // A benign race is possible here: we could overwrite a fresher cache entry
654 // save the request params
655 mInSamplingRate = sampleRate;
656 mInFormat = format;
657 mInChannelMask = channelMask;
658
659 mInBuffSize = inBuffSize;
660 }
661
662 *buffSize = mInBuffSize;
663
664 return NO_ERROR;
665}
666
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700667sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700668{
669 sp<AudioIoDescriptor> desc;
670 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
671 if (index >= 0) {
672 desc = mIoDescriptors.valueAt(index);
673 }
674 return desc;
675}
676
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700677sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
678{
679 Mutex::Autolock _l(mLock);
680 return getIoDescriptor_l(ioHandle);
681}
682
Eric Laurent296fb132015-05-01 11:38:42 -0700683status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700684 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
685 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700686{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700687 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800688 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700689 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
690 auto result = callbacks.try_emplace(portId, callback);
691 if (!result.second) {
692 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700693 }
Eric Laurent296fb132015-05-01 11:38:42 -0700694 return NO_ERROR;
695}
696
697status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700698 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
699 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700700{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700701 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800702 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700703 auto it = mAudioDeviceCallbacks.find(audioIo);
704 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700705 return INVALID_OPERATION;
706 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700707 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700708 return INVALID_OPERATION;
709 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700710 if (it->second.size() == 0) {
711 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700712 }
713 return NO_ERROR;
714}
715
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800716/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700717{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800718 Mutex::Autolock _l(gLockErrorCallbacks);
719 gAudioErrorCallbacks.insert(cb);
720 return reinterpret_cast<uintptr_t>(cb);
721}
722
723/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
724 Mutex::Autolock _l(gLockErrorCallbacks);
725 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
726}
727
728/* static */ void AudioSystem::reportError(status_t err) {
729 Mutex::Autolock _l(gLockErrorCallbacks);
730 for (auto callback : gAudioErrorCallbacks) {
731 callback(err);
732 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733}
734
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700735/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
736{
737 Mutex::Autolock _l(gLock);
738 gDynPolicyCallback = cb;
739}
740
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800741/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
742{
743 Mutex::Autolock _l(gLock);
744 gRecordConfigCallback = cb;
745}
746
Eric Laurentc2f1f072009-07-17 12:17:14 -0700747// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800748// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
750sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
751
752
Glenn Kasten18a6d902012-09-24 11:27:56 -0700753// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800754const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800756 sp<IAudioPolicyService> ap;
757 sp<AudioPolicyServiceClient> apc;
758 {
759 Mutex::Autolock _l(gLockAPS);
760 if (gAudioPolicyService == 0) {
761 sp<IServiceManager> sm = defaultServiceManager();
762 sp<IBinder> binder;
763 do {
764 binder = sm->getService(String16("media.audio_policy"));
765 if (binder != 0)
766 break;
767 ALOGW("AudioPolicyService not published, waiting...");
768 usleep(500000); // 0.5 s
769 } while (true);
770 if (gAudioPolicyServiceClient == NULL) {
771 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
772 }
773 binder->linkToDeath(gAudioPolicyServiceClient);
774 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
775 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
776 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700777 // Make sure callbacks can be received by gAudioPolicyServiceClient
778 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700779 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800780 ap = gAudioPolicyService;
781 }
782 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200783 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800784 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200785 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100786 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200787 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700788 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800789
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800790 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700791}
792
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700793// ---------------------------------------------------------------------------
794
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000795void AudioSystem::onNewAudioModulesAvailable()
796{
797 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
798 if (aps == 0) return;
799 aps->onNewAudioModulesAvailable();
800}
801
Dima Zavinfce7a472011-04-19 22:30:36 -0700802status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
803 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800804 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800805 const char *device_name,
806 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700807{
808 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700809 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800810 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700811
Eric Laurentc2f1f072009-07-17 12:17:14 -0700812 if (aps == 0) return PERMISSION_DENIED;
813
Eric Laurent71b63e32011-09-02 14:20:56 -0700814 if (device_address != NULL) {
815 address = device_address;
816 }
Paul McLeane743a472015-01-28 11:07:31 -0800817 if (device_name != NULL) {
818 name = device_name;
819 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800820 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700821}
822
Dima Zavinfce7a472011-04-19 22:30:36 -0700823audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700824 const char *device_address)
825{
826 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700827 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700828
829 return aps->getDeviceConnectionState(device, device_address);
830}
831
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800832status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
833 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800834 const char *device_name,
835 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800836{
837 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
838 const char *address = "";
839 const char *name = "";
840
841 if (aps == 0) return PERMISSION_DENIED;
842
843 if (device_address != NULL) {
844 address = device_address;
845 }
846 if (device_name != NULL) {
847 name = device_name;
848 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800849 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800850}
851
Eric Laurent00dba062020-02-11 15:52:09 -0800852status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700853{
Glenn Kasten347966c2012-01-18 14:58:32 -0800854 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
856 if (aps == 0) return PERMISSION_DENIED;
857
Eric Laurent00dba062020-02-11 15:52:09 -0800858 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700859}
860
Dima Zavinfce7a472011-04-19 22:30:36 -0700861status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862{
863 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
864 if (aps == 0) return PERMISSION_DENIED;
865 return aps->setForceUse(usage, config);
866}
867
Dima Zavinfce7a472011-04-19 22:30:36 -0700868audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700869{
870 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700871 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700872 return aps->getForceUse(usage);
873}
874
875
Eric Laurentf4e63452017-11-06 19:31:46 +0000876audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700877{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700878 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
879 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000880 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881}
882
Eric Laurent42984412019-05-09 17:57:03 -0700883status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800884 audio_io_handle_t *output,
885 audio_session_t session,
886 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200887 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700888 uid_t uid,
Ricardo Correaac26cf72020-01-06 14:43:38 -0800889 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800890 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800891 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700892 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800893 audio_port_handle_t *portId,
894 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700895{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700896 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800897 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200898 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correaac26cf72020-01-06 14:43:38 -0800899 opPackageName, config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800900 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700901}
902
Eric Laurentd7fe0862018-07-14 16:48:01 -0700903status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904{
905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
906 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700907 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700908}
909
Eric Laurentd7fe0862018-07-14 16:48:01 -0700910status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911{
912 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
913 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700914 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700915}
916
Eric Laurentd7fe0862018-07-14 16:48:01 -0700917void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918{
919 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
920 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700921 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700922}
923
Eric Laurentcaf7f482014-11-25 17:50:47 -0800924status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
925 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700926 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800927 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700928 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700929 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800930 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800931 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600932 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700933 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800934 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700935{
936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800937 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600938 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700939 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800940 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941}
942
Eric Laurent4eb58f12018-12-07 16:41:02 -0800943status_t AudioSystem::startInput(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 PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800947 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948}
949
Eric Laurentfee19762018-01-29 18:44:13 -0800950status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700951{
952 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
953 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800954 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955}
956
Eric Laurentfee19762018-01-29 18:44:13 -0800957void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700958{
959 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
960 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800961 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962}
963
Dima Zavinfce7a472011-04-19 22:30:36 -0700964status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965 int indexMin,
966 int indexMax)
967{
968 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
969 if (aps == 0) return PERMISSION_DENIED;
970 return aps->initStreamVolume(stream, indexMin, indexMax);
971}
972
Eric Laurent83844cc2011-11-18 16:43:31 -0800973status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
974 int index,
975 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800979 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980}
981
Eric Laurent83844cc2011-11-18 16:43:31 -0800982status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
983 int *index,
984 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985{
986 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
987 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800988 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700989}
990
François Gaffiecfe17322018-11-07 13:41:29 +0100991status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
992 int index,
993 audio_devices_t device)
994{
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
996 if (aps == 0) return PERMISSION_DENIED;
997 return aps->setVolumeIndexForAttributes(attr, index, device);
998}
999
1000status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1001 int &index,
1002 audio_devices_t device)
1003{
1004 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1005 if (aps == 0) return PERMISSION_DENIED;
1006 return aps->getVolumeIndexForAttributes(attr, index, device);
1007}
1008
1009status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1010{
1011 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1012 if (aps == 0) return PERMISSION_DENIED;
1013 return aps->getMaxVolumeIndexForAttributes(attr, index);
1014}
1015
1016status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1017{
1018 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1019 if (aps == 0) return PERMISSION_DENIED;
1020 return aps->getMinVolumeIndexForAttributes(attr, index);
1021}
1022
Dima Zavinfce7a472011-04-19 22:30:36 -07001023uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001024{
1025 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001026 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001027 return aps->getStrategyForStream(stream);
1028}
1029
Eric Laurent63742522012-03-08 13:42:42 -08001030audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001031{
1032 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001033 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001034 return aps->getDevicesForStream(stream);
1035}
1036
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001037status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1038 AudioDeviceTypeAddrVector *devices) {
1039 if (devices == nullptr) {
1040 return BAD_VALUE;
1041 }
1042 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1043 if (aps == 0) return PERMISSION_DENIED;
1044 return aps->getDevicesForAttributes(aa, devices);
1045}
1046
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001047audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001048{
1049 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001050 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001051 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001052 return aps->getOutputForEffect(desc);
1053}
1054
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001055status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001056 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001057 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001058 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001059 int id)
1060{
1061 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1062 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001063 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001064}
1065
1066status_t AudioSystem::unregisterEffect(int id)
1067{
1068 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1069 if (aps == 0) return PERMISSION_DENIED;
1070 return aps->unregisterEffect(id);
1071}
1072
Eric Laurentdb7c0792011-08-10 10:37:50 -07001073status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1074{
1075 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1076 if (aps == 0) return PERMISSION_DENIED;
1077 return aps->setEffectEnabled(id, enabled);
1078}
1079
Eric Laurent6c796322019-04-09 14:13:17 -07001080status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1081{
1082 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1083 if (aps == 0) return PERMISSION_DENIED;
1084 return aps->moveEffectsToIo(ids, io);
1085}
1086
Glenn Kastenfff6d712012-01-12 16:38:12 -08001087status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001088{
Eric Laurenteda6c362011-02-02 09:33:30 -08001089 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1090 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001091 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001092 *state = aps->isStreamActive(stream, inPastMs);
1093 return NO_ERROR;
1094}
1095
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001096status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1097 uint32_t inPastMs)
1098{
1099 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1100 if (aps == 0) return PERMISSION_DENIED;
1101 if (state == NULL) return BAD_VALUE;
1102 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1103 return NO_ERROR;
1104}
1105
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001106status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1107{
1108 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1109 if (aps == 0) return PERMISSION_DENIED;
1110 if (state == NULL) return BAD_VALUE;
1111 *state = aps->isSourceActive(stream);
1112 return NO_ERROR;
1113}
1114
Glenn Kasten3b16c762012-11-14 08:44:39 -08001115uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001116{
1117 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1118 if (af == 0) return 0;
1119 return af->getPrimaryOutputSamplingRate();
1120}
1121
Glenn Kastene33054e2012-11-14 12:54:39 -08001122size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001123{
1124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1125 if (af == 0) return 0;
1126 return af->getPrimaryOutputFrameCount();
1127}
Eric Laurenteda6c362011-02-02 09:33:30 -08001128
Andy Hung6f248bb2018-01-23 14:04:37 -08001129status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001130{
1131 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1132 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001133 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001134}
1135
Eric Laurent9f6530f2011-08-30 10:18:54 -07001136void AudioSystem::clearAudioConfigCache()
1137{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001138 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001139 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001140 {
1141 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001142 if (gAudioFlingerClient != 0) {
1143 gAudioFlingerClient->clearIoCache();
1144 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001145 gAudioFlinger.clear();
1146 }
1147 {
1148 Mutex::Autolock _l(gLockAPS);
1149 gAudioPolicyService.clear();
1150 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001151}
1152
Hayden Gomes524159d2019-12-23 14:41:47 -08001153status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1154 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1155 if (aps == nullptr) return PERMISSION_DENIED;
1156 return aps->setSupportedSystemUsages(systemUsages);
1157}
1158
Kevin Rocardb99cc752019-03-21 20:52:24 -07001159status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1160 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1161 if (aps == nullptr) return PERMISSION_DENIED;
1162 return aps->setAllowedCapturePolicy(uid, flags);
1163}
1164
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001165bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1166{
1167 ALOGV("isOffloadSupported()");
1168 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1169 if (aps == 0) return false;
1170 return aps->isOffloadSupported(info);
1171}
1172
Eric Laurent203b1a12014-04-01 10:34:16 -07001173status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1174 audio_port_type_t type,
1175 unsigned int *num_ports,
1176 struct audio_port *ports,
1177 unsigned int *generation)
1178{
1179 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1180 if (aps == 0) return PERMISSION_DENIED;
1181 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1182}
1183
1184status_t AudioSystem::getAudioPort(struct audio_port *port)
1185{
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == 0) return PERMISSION_DENIED;
1188 return aps->getAudioPort(port);
1189}
1190
1191status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1192 audio_patch_handle_t *handle)
1193{
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return PERMISSION_DENIED;
1196 return aps->createAudioPatch(patch, handle);
1197}
1198
1199status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1200{
1201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1202 if (aps == 0) return PERMISSION_DENIED;
1203 return aps->releaseAudioPatch(handle);
1204}
1205
1206status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1207 struct audio_patch *patches,
1208 unsigned int *generation)
1209{
1210 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1211 if (aps == 0) return PERMISSION_DENIED;
1212 return aps->listAudioPatches(num_patches, patches, generation);
1213}
1214
1215status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1216{
1217 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1218 if (aps == 0) return PERMISSION_DENIED;
1219 return aps->setAudioPortConfig(config);
1220}
1221
Eric Laurent296fb132015-05-01 11:38:42 -07001222status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001223{
Eric Laurentb28753e2015-04-01 13:06:28 -07001224 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1225 if (aps == 0) return PERMISSION_DENIED;
1226
1227 Mutex::Autolock _l(gLockAPS);
1228 if (gAudioPolicyServiceClient == 0) {
1229 return NO_INIT;
1230 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001231 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1232 if (ret == 1) {
1233 aps->setAudioPortCallbacksEnabled(true);
1234 }
1235 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001236}
1237
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001238/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001239status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001240{
1241 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1242 if (aps == 0) return PERMISSION_DENIED;
1243
1244 Mutex::Autolock _l(gLockAPS);
1245 if (gAudioPolicyServiceClient == 0) {
1246 return NO_INIT;
1247 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001248 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1249 if (ret == 0) {
1250 aps->setAudioPortCallbacksEnabled(false);
1251 }
1252 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001253}
1254
François Gaffiecfe17322018-11-07 13:41:29 +01001255status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1256{
1257 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1258 if (aps == 0) return PERMISSION_DENIED;
1259
1260 Mutex::Autolock _l(gLockAPS);
1261 if (gAudioPolicyServiceClient == 0) {
1262 return NO_INIT;
1263 }
1264 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1265 if (ret == 1) {
1266 aps->setAudioVolumeGroupCallbacksEnabled(true);
1267 }
1268 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1269}
1270
1271status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1272{
1273 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1274 if (aps == 0) return PERMISSION_DENIED;
1275
1276 Mutex::Autolock _l(gLockAPS);
1277 if (gAudioPolicyServiceClient == 0) {
1278 return NO_INIT;
1279 }
1280 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1281 if (ret == 0) {
1282 aps->setAudioVolumeGroupCallbacksEnabled(false);
1283 }
1284 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1285}
1286
Eric Laurent296fb132015-05-01 11:38:42 -07001287status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001288 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1289 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001290{
1291 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1292 if (afc == 0) {
1293 return NO_INIT;
1294 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001295 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001296 if (status == NO_ERROR) {
1297 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1298 if (af != 0) {
1299 af->registerClient(afc);
1300 }
1301 }
1302 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001303}
1304
1305status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001306 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1307 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001308{
1309 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1310 if (afc == 0) {
1311 return NO_INIT;
1312 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001313 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001314}
1315
1316audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1317{
1318 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1319 if (af == 0) return PERMISSION_DENIED;
1320 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1321 if (desc == 0) {
1322 return AUDIO_PORT_HANDLE_NONE;
1323 }
1324 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001325}
1326
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001327status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1328 audio_io_handle_t *ioHandle,
1329 audio_devices_t *device)
1330{
1331 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1332 if (aps == 0) return PERMISSION_DENIED;
1333 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1334}
1335
1336status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1337{
1338 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1339 if (aps == 0) return PERMISSION_DENIED;
1340 return aps->releaseSoundTriggerSession(session);
1341}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001342
1343audio_mode_t AudioSystem::getPhoneState()
1344{
1345 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1346 if (aps == 0) return AUDIO_MODE_INVALID;
1347 return aps->getPhoneState();
1348}
1349
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001350status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001351{
1352 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1353 if (aps == 0) return PERMISSION_DENIED;
1354 return aps->registerPolicyMixes(mixes, registration);
1355}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001356
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001357status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1358{
1359 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1360 if (aps == 0) return PERMISSION_DENIED;
1361 return aps->setUidDeviceAffinities(uid, devices);
1362}
1363
1364status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1365 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1366 if (aps == 0) return PERMISSION_DENIED;
1367 return aps->removeUidDeviceAffinities(uid);
1368}
1369
Oscar Azucena90e77632019-11-27 17:12:28 -08001370status_t AudioSystem::setUserIdDeviceAffinities(int userId,
1371 const Vector<AudioDeviceTypeAddr>& devices)
1372{
1373 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1374 if (aps == 0) return PERMISSION_DENIED;
1375 return aps->setUserIdDeviceAffinities(userId, devices);
1376}
1377
1378status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1379{
1380 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1381 if (aps == 0) return PERMISSION_DENIED;
1382 return aps->removeUserIdDeviceAffinities(userId);
1383}
1384
Eric Laurent554a2772015-04-10 11:29:24 -07001385status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1386 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001387 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001388{
1389 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1390 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001391 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001392}
1393
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001394status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001395{
1396 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1397 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001398 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001399}
1400
Andy Hung2ddee192015-12-18 17:34:44 -08001401status_t AudioSystem::setMasterMono(bool mono)
1402{
1403 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1404 if (aps == 0) return PERMISSION_DENIED;
1405 return aps->setMasterMono(mono);
1406}
1407
1408status_t AudioSystem::getMasterMono(bool *mono)
1409{
1410 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1411 if (aps == 0) return PERMISSION_DENIED;
1412 return aps->getMasterMono(mono);
1413}
1414
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001415status_t AudioSystem::setMasterBalance(float balance)
1416{
1417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1418 if (af == 0) return PERMISSION_DENIED;
1419 return af->setMasterBalance(balance);
1420}
1421
1422status_t AudioSystem::getMasterBalance(float *balance)
1423{
1424 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1425 if (af == 0) return PERMISSION_DENIED;
1426 return af->getMasterBalance(balance);
1427}
1428
Eric Laurentac9cef52017-06-09 15:46:26 -07001429float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1430{
1431 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1432 if (aps == 0) return NAN;
1433 return aps->getStreamVolumeDB(stream, index, device);
1434}
1435
jiabin46a76fa2018-01-05 10:18:21 -08001436status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1437{
1438 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1439 if (af == 0) return PERMISSION_DENIED;
1440 return af->getMicrophones(microphones);
1441}
1442
Eric Laurent42896a02019-09-27 15:40:33 -07001443status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1444 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1445 if (af == nullptr) return PERMISSION_DENIED;
1446 return af->setAudioHalPids(pids);
1447}
1448
jiabin81772902018-04-02 17:52:27 -07001449status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1450 audio_format_t *surroundFormats,
1451 bool *surroundFormatsEnabled,
1452 bool reported)
1453{
1454 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1455 if (aps == 0) return PERMISSION_DENIED;
1456 return aps->getSurroundFormats(
1457 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1458}
1459
1460status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1461{
1462 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1463 if (aps == 0) return PERMISSION_DENIED;
1464 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1465}
1466
Eric Laurentb78763e2018-10-17 10:08:02 -07001467status_t AudioSystem::setAssistantUid(uid_t uid)
1468{
1469 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1470 if (aps == 0) return PERMISSION_DENIED;
1471
1472 return aps->setAssistantUid(uid);
1473}
1474
1475status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1476{
1477 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1478 if (aps == 0) return PERMISSION_DENIED;
1479
1480 return aps->setA11yServicesUids(uids);
1481}
1482
jiabin6012f912018-11-02 17:06:30 -07001483bool AudioSystem::isHapticPlaybackSupported()
1484{
1485 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1486 if (aps == 0) return false;
1487 return aps->isHapticPlaybackSupported();
1488}
1489
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001490status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001491 std::vector<audio_format_t> *formats) {
1492 const sp <IAudioPolicyService>
1493 & aps = AudioSystem::get_audio_policy_service();
1494 if (aps == 0) return PERMISSION_DENIED;
1495 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1496}
1497
1498status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001499{
1500 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1501 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001502 return aps->listAudioProductStrategies(strategies);
1503}
1504
1505audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1506{
1507 AudioProductStrategyVector strategies;
1508 listAudioProductStrategies(strategies);
1509 for (const auto &strategy : strategies) {
1510 auto attrVect = strategy.getAudioAttributes();
1511 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1512 return attributes.getStreamType() == stream; });
1513 if (iter != end(attrVect)) {
1514 return iter->getAttributes();
1515 }
1516 }
1517 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1518 return AUDIO_ATTRIBUTES_INITIALIZER;
1519}
1520
1521audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1522{
François Gaffie4b2018b2018-11-07 11:18:59 +01001523 product_strategy_t psId;
1524 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1525 if (ret != NO_ERROR) {
1526 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1527 return AUDIO_STREAM_MUSIC;
1528 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001529 AudioProductStrategyVector strategies;
1530 listAudioProductStrategies(strategies);
1531 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001532 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001533 auto attrVect = strategy.getAudioAttributes();
1534 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1535 return AudioProductStrategy::attributesMatches(
1536 refAttr.getAttributes(), attr); });
1537 if (iter != end(attrVect)) {
1538 return iter->getStreamType();
1539 }
1540 }
1541 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001542 switch (attr.usage) {
1543 case AUDIO_USAGE_VIRTUAL_SOURCE:
1544 // virtual source is not expected to have an associated product strategy
1545 break;
1546 default:
1547 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1548 break;
1549 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001550 return AUDIO_STREAM_MUSIC;
1551}
1552
François Gaffie4b2018b2018-11-07 11:18:59 +01001553status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1554 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001555{
1556 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001557 if (aps == 0) return PERMISSION_DENIED;
1558 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1559}
1560
1561status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1562{
1563 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1564 if (aps == 0) return PERMISSION_DENIED;
1565 return aps->listAudioVolumeGroups(groups);
1566}
1567
1568status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1569 volume_group_t &volumeGroup)
1570{
1571 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1572 if (aps == 0) return PERMISSION_DENIED;
1573 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001574}
Eric Laurentb78763e2018-10-17 10:08:02 -07001575
Eric Laurent6ede98f2019-06-11 14:50:30 -07001576status_t AudioSystem::setRttEnabled(bool enabled)
1577{
1578 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1579 if (aps == 0) return PERMISSION_DENIED;
1580 return aps->setRttEnabled(enabled);
1581}
1582
Eric Laurent8340e672019-11-06 11:01:08 -08001583bool AudioSystem::isCallScreenModeSupported()
1584{
1585 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1586 if (aps == 0) return false;
1587 return aps->isCallScreenModeSupported();
1588}
1589
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001590status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1591 const AudioDeviceTypeAddr &device)
1592{
1593 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1594 if (aps == 0) {
1595 return PERMISSION_DENIED;
1596 }
1597 return aps->setPreferredDeviceForStrategy(strategy, device);
1598}
1599
1600status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1601{
1602 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1603 if (aps == 0) {
1604 return PERMISSION_DENIED;
1605 }
1606 return aps->removePreferredDeviceForStrategy(strategy);
1607}
1608
1609status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1610 AudioDeviceTypeAddr &device)
1611{
1612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1613 if (aps == 0) {
1614 return PERMISSION_DENIED;
1615 }
1616 return aps->getPreferredDeviceForStrategy(strategy, device);
1617}
1618
Eric Laurentc2f1f072009-07-17 12:17:14 -07001619// ---------------------------------------------------------------------------
1620
Eric Laurente8726fe2015-06-26 09:39:24 -07001621int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001622 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001623{
1624 Mutex::Autolock _l(mLock);
1625 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001626 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001627 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001628 }
1629 }
Eric Laurent296fb132015-05-01 11:38:42 -07001630 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001631 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001632}
1633
Eric Laurente8726fe2015-06-26 09:39:24 -07001634int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001635 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001636{
1637 Mutex::Autolock _l(mLock);
1638 size_t i;
1639 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001640 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001641 break;
1642 }
1643 }
1644 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001645 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001646 }
1647 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001648 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001649}
1650
Eric Laurent296fb132015-05-01 11:38:42 -07001651
Eric Laurentb28753e2015-04-01 13:06:28 -07001652void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1653{
1654 Mutex::Autolock _l(mLock);
1655 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1656 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1657 }
1658}
1659
1660void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1661{
1662 Mutex::Autolock _l(mLock);
1663 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1664 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1665 }
1666}
1667
François Gaffiecfe17322018-11-07 13:41:29 +01001668// ----------------------------------------------------------------------------
1669int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1670 const sp<AudioVolumeGroupCallback>& callback)
1671{
1672 Mutex::Autolock _l(mLock);
1673 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1674 if (mAudioVolumeGroupCallback[i] == callback) {
1675 return -1;
1676 }
1677 }
1678 mAudioVolumeGroupCallback.add(callback);
1679 return mAudioVolumeGroupCallback.size();
1680}
1681
1682int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1683 const sp<AudioVolumeGroupCallback>& callback)
1684{
1685 Mutex::Autolock _l(mLock);
1686 size_t i;
1687 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1688 if (mAudioVolumeGroupCallback[i] == callback) {
1689 break;
1690 }
1691 }
1692 if (i == mAudioVolumeGroupCallback.size()) {
1693 return -1;
1694 }
1695 mAudioVolumeGroupCallback.removeAt(i);
1696 return mAudioVolumeGroupCallback.size();
1697}
1698
1699void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1700 int flags)
1701{
1702 Mutex::Autolock _l(mLock);
1703 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1704 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1705 }
1706}
1707// ----------------------------------------------------------------------------
1708
Jean-Michel Trivide801052015-04-14 19:10:14 -07001709void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1710 String8 regId, int32_t state)
1711{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001712 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1713 dynamic_policy_callback cb = NULL;
1714 {
1715 Mutex::Autolock _l(AudioSystem::gLock);
1716 cb = gDynPolicyCallback;
1717 }
1718
1719 if (cb != NULL) {
1720 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1721 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001722}
1723
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001724void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001725 int event,
1726 const record_client_info_t *clientInfo,
1727 const audio_config_base_t *clientConfig,
1728 std::vector<effect_descriptor_t> clientEffects,
1729 const audio_config_base_t *deviceConfig,
1730 std::vector<effect_descriptor_t> effects,
1731 audio_patch_handle_t patchHandle,
1732 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001733 record_config_callback cb = NULL;
1734 {
1735 Mutex::Autolock _l(AudioSystem::gLock);
1736 cb = gRecordConfigCallback;
1737 }
1738
1739 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001740 cb(event, clientInfo, clientConfig, clientEffects,
1741 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001742 }
1743}
1744
Glenn Kasten4944acb2013-08-19 08:39:20 -07001745void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1746{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001747 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001748 Mutex::Autolock _l(mLock);
1749 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1750 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001751 }
François Gaffiecfe17322018-11-07 13:41:29 +01001752 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1753 mAudioVolumeGroupCallback[i]->onServiceDied();
1754 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001755 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001756 {
1757 Mutex::Autolock _l(gLockAPS);
1758 AudioSystem::gAudioPolicyService.clear();
1759 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001760
Steve Block5ff1dd52012-01-05 23:22:43 +00001761 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001762}
1763
Glenn Kasten40bc9062015-03-20 09:09:33 -07001764} // namespace android