blob: ff2608c8b7234e94258eb803c977d95bd42d5f10 [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,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800889 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800890 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700891 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800892 audio_port_handle_t *portId,
893 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700894{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700895 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800896 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200897 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700898 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800899 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700900}
901
Eric Laurentd7fe0862018-07-14 16:48:01 -0700902status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700903{
904 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
905 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700906 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907}
908
Eric Laurentd7fe0862018-07-14 16:48:01 -0700909status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700910{
911 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
912 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700913 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700914}
915
Eric Laurentd7fe0862018-07-14 16:48:01 -0700916void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700917{
918 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
919 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700920 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700921}
922
Eric Laurentcaf7f482014-11-25 17:50:47 -0800923status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
924 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700925 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800926 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700927 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700928 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800929 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800930 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600931 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700932 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800933 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700934{
935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800936 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600937 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700938 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800939 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940}
941
Eric Laurent4eb58f12018-12-07 16:41:02 -0800942status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943{
944 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
945 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800946 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947}
948
Eric Laurentfee19762018-01-29 18:44:13 -0800949status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700950{
951 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
952 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800953 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954}
955
Eric Laurentfee19762018-01-29 18:44:13 -0800956void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957{
958 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
959 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800960 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700961}
962
Dima Zavinfce7a472011-04-19 22:30:36 -0700963status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700964 int indexMin,
965 int indexMax)
966{
967 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
968 if (aps == 0) return PERMISSION_DENIED;
969 return aps->initStreamVolume(stream, indexMin, indexMax);
970}
971
Eric Laurent83844cc2011-11-18 16:43:31 -0800972status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
973 int index,
974 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700975{
976 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
977 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800978 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700979}
980
Eric Laurent83844cc2011-11-18 16:43:31 -0800981status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
982 int *index,
983 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984{
985 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
986 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800987 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988}
989
François Gaffiecfe17322018-11-07 13:41:29 +0100990status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
991 int index,
992 audio_devices_t device)
993{
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
996 return aps->setVolumeIndexForAttributes(attr, index, device);
997}
998
999status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1000 int &index,
1001 audio_devices_t device)
1002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
1005 return aps->getVolumeIndexForAttributes(attr, index, device);
1006}
1007
1008status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1009{
1010 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1011 if (aps == 0) return PERMISSION_DENIED;
1012 return aps->getMaxVolumeIndexForAttributes(attr, index);
1013}
1014
1015status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1016{
1017 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1018 if (aps == 0) return PERMISSION_DENIED;
1019 return aps->getMinVolumeIndexForAttributes(attr, index);
1020}
1021
Dima Zavinfce7a472011-04-19 22:30:36 -07001022uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001023{
1024 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001025 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001026 return aps->getStrategyForStream(stream);
1027}
1028
Eric Laurent63742522012-03-08 13:42:42 -08001029audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001030{
1031 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001032 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001033 return aps->getDevicesForStream(stream);
1034}
1035
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001036status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1037 AudioDeviceTypeAddrVector *devices) {
1038 if (devices == nullptr) {
1039 return BAD_VALUE;
1040 }
1041 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1042 if (aps == 0) return PERMISSION_DENIED;
1043 return aps->getDevicesForAttributes(aa, devices);
1044}
1045
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001046audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001047{
1048 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001049 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001050 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001051 return aps->getOutputForEffect(desc);
1052}
1053
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001054status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001055 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001056 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001057 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001058 int id)
1059{
1060 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1061 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001062 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001063}
1064
1065status_t AudioSystem::unregisterEffect(int id)
1066{
1067 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1068 if (aps == 0) return PERMISSION_DENIED;
1069 return aps->unregisterEffect(id);
1070}
1071
Eric Laurentdb7c0792011-08-10 10:37:50 -07001072status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1073{
1074 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1075 if (aps == 0) return PERMISSION_DENIED;
1076 return aps->setEffectEnabled(id, enabled);
1077}
1078
Eric Laurent6c796322019-04-09 14:13:17 -07001079status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1080{
1081 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1082 if (aps == 0) return PERMISSION_DENIED;
1083 return aps->moveEffectsToIo(ids, io);
1084}
1085
Glenn Kastenfff6d712012-01-12 16:38:12 -08001086status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001087{
Eric Laurenteda6c362011-02-02 09:33:30 -08001088 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1089 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001090 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001091 *state = aps->isStreamActive(stream, inPastMs);
1092 return NO_ERROR;
1093}
1094
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001095status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1096 uint32_t inPastMs)
1097{
1098 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1099 if (aps == 0) return PERMISSION_DENIED;
1100 if (state == NULL) return BAD_VALUE;
1101 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1102 return NO_ERROR;
1103}
1104
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001105status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1106{
1107 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1108 if (aps == 0) return PERMISSION_DENIED;
1109 if (state == NULL) return BAD_VALUE;
1110 *state = aps->isSourceActive(stream);
1111 return NO_ERROR;
1112}
1113
Glenn Kasten3b16c762012-11-14 08:44:39 -08001114uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001115{
1116 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1117 if (af == 0) return 0;
1118 return af->getPrimaryOutputSamplingRate();
1119}
1120
Glenn Kastene33054e2012-11-14 12:54:39 -08001121size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001122{
1123 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1124 if (af == 0) return 0;
1125 return af->getPrimaryOutputFrameCount();
1126}
Eric Laurenteda6c362011-02-02 09:33:30 -08001127
Andy Hung6f248bb2018-01-23 14:04:37 -08001128status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001129{
1130 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1131 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001132 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001133}
1134
Eric Laurent9f6530f2011-08-30 10:18:54 -07001135void AudioSystem::clearAudioConfigCache()
1136{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001137 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001138 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001139 {
1140 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001141 if (gAudioFlingerClient != 0) {
1142 gAudioFlingerClient->clearIoCache();
1143 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001144 gAudioFlinger.clear();
1145 }
1146 {
1147 Mutex::Autolock _l(gLockAPS);
1148 gAudioPolicyService.clear();
1149 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001150}
1151
Hayden Gomes524159d2019-12-23 14:41:47 -08001152status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1153 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1154 if (aps == nullptr) return PERMISSION_DENIED;
1155 return aps->setSupportedSystemUsages(systemUsages);
1156}
1157
Kevin Rocardb99cc752019-03-21 20:52:24 -07001158status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1159 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1160 if (aps == nullptr) return PERMISSION_DENIED;
1161 return aps->setAllowedCapturePolicy(uid, flags);
1162}
1163
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001164bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1165{
1166 ALOGV("isOffloadSupported()");
1167 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1168 if (aps == 0) return false;
1169 return aps->isOffloadSupported(info);
1170}
1171
Eric Laurent203b1a12014-04-01 10:34:16 -07001172status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1173 audio_port_type_t type,
1174 unsigned int *num_ports,
1175 struct audio_port *ports,
1176 unsigned int *generation)
1177{
1178 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1179 if (aps == 0) return PERMISSION_DENIED;
1180 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1181}
1182
1183status_t AudioSystem::getAudioPort(struct audio_port *port)
1184{
1185 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1186 if (aps == 0) return PERMISSION_DENIED;
1187 return aps->getAudioPort(port);
1188}
1189
1190status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1191 audio_patch_handle_t *handle)
1192{
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == 0) return PERMISSION_DENIED;
1195 return aps->createAudioPatch(patch, handle);
1196}
1197
1198status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1199{
1200 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1201 if (aps == 0) return PERMISSION_DENIED;
1202 return aps->releaseAudioPatch(handle);
1203}
1204
1205status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1206 struct audio_patch *patches,
1207 unsigned int *generation)
1208{
1209 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1210 if (aps == 0) return PERMISSION_DENIED;
1211 return aps->listAudioPatches(num_patches, patches, generation);
1212}
1213
1214status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1215{
1216 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1217 if (aps == 0) return PERMISSION_DENIED;
1218 return aps->setAudioPortConfig(config);
1219}
1220
Eric Laurent296fb132015-05-01 11:38:42 -07001221status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001222{
Eric Laurentb28753e2015-04-01 13:06:28 -07001223 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1224 if (aps == 0) return PERMISSION_DENIED;
1225
1226 Mutex::Autolock _l(gLockAPS);
1227 if (gAudioPolicyServiceClient == 0) {
1228 return NO_INIT;
1229 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001230 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1231 if (ret == 1) {
1232 aps->setAudioPortCallbacksEnabled(true);
1233 }
1234 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001235}
1236
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001237/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001238status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001239{
1240 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1241 if (aps == 0) return PERMISSION_DENIED;
1242
1243 Mutex::Autolock _l(gLockAPS);
1244 if (gAudioPolicyServiceClient == 0) {
1245 return NO_INIT;
1246 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001247 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1248 if (ret == 0) {
1249 aps->setAudioPortCallbacksEnabled(false);
1250 }
1251 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001252}
1253
François Gaffiecfe17322018-11-07 13:41:29 +01001254status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1255{
1256 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1257 if (aps == 0) return PERMISSION_DENIED;
1258
1259 Mutex::Autolock _l(gLockAPS);
1260 if (gAudioPolicyServiceClient == 0) {
1261 return NO_INIT;
1262 }
1263 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1264 if (ret == 1) {
1265 aps->setAudioVolumeGroupCallbacksEnabled(true);
1266 }
1267 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1268}
1269
1270status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1271{
1272 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1273 if (aps == 0) return PERMISSION_DENIED;
1274
1275 Mutex::Autolock _l(gLockAPS);
1276 if (gAudioPolicyServiceClient == 0) {
1277 return NO_INIT;
1278 }
1279 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1280 if (ret == 0) {
1281 aps->setAudioVolumeGroupCallbacksEnabled(false);
1282 }
1283 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1284}
1285
Eric Laurent296fb132015-05-01 11:38:42 -07001286status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001287 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1288 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001289{
1290 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1291 if (afc == 0) {
1292 return NO_INIT;
1293 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001294 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001295 if (status == NO_ERROR) {
1296 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1297 if (af != 0) {
1298 af->registerClient(afc);
1299 }
1300 }
1301 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001302}
1303
1304status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001305 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1306 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001307{
1308 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1309 if (afc == 0) {
1310 return NO_INIT;
1311 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001312 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001313}
1314
1315audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1316{
1317 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1318 if (af == 0) return PERMISSION_DENIED;
1319 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1320 if (desc == 0) {
1321 return AUDIO_PORT_HANDLE_NONE;
1322 }
1323 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001324}
1325
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001326status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1327 audio_io_handle_t *ioHandle,
1328 audio_devices_t *device)
1329{
1330 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1331 if (aps == 0) return PERMISSION_DENIED;
1332 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1333}
1334
1335status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1336{
1337 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1338 if (aps == 0) return PERMISSION_DENIED;
1339 return aps->releaseSoundTriggerSession(session);
1340}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001341
1342audio_mode_t AudioSystem::getPhoneState()
1343{
1344 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1345 if (aps == 0) return AUDIO_MODE_INVALID;
1346 return aps->getPhoneState();
1347}
1348
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001349status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001350{
1351 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1352 if (aps == 0) return PERMISSION_DENIED;
1353 return aps->registerPolicyMixes(mixes, registration);
1354}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001355
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001356status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1357{
1358 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1359 if (aps == 0) return PERMISSION_DENIED;
1360 return aps->setUidDeviceAffinities(uid, devices);
1361}
1362
1363status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->removeUidDeviceAffinities(uid);
1367}
1368
Oscar Azucena90e77632019-11-27 17:12:28 -08001369status_t AudioSystem::setUserIdDeviceAffinities(int userId,
1370 const Vector<AudioDeviceTypeAddr>& devices)
1371{
1372 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1373 if (aps == 0) return PERMISSION_DENIED;
1374 return aps->setUserIdDeviceAffinities(userId, devices);
1375}
1376
1377status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1378{
1379 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1380 if (aps == 0) return PERMISSION_DENIED;
1381 return aps->removeUserIdDeviceAffinities(userId);
1382}
1383
Eric Laurent554a2772015-04-10 11:29:24 -07001384status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1385 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001386 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001387{
1388 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1389 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001390 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001391}
1392
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001393status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001394{
1395 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1396 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001397 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001398}
1399
Andy Hung2ddee192015-12-18 17:34:44 -08001400status_t AudioSystem::setMasterMono(bool mono)
1401{
1402 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1403 if (aps == 0) return PERMISSION_DENIED;
1404 return aps->setMasterMono(mono);
1405}
1406
1407status_t AudioSystem::getMasterMono(bool *mono)
1408{
1409 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1410 if (aps == 0) return PERMISSION_DENIED;
1411 return aps->getMasterMono(mono);
1412}
1413
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001414status_t AudioSystem::setMasterBalance(float balance)
1415{
1416 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1417 if (af == 0) return PERMISSION_DENIED;
1418 return af->setMasterBalance(balance);
1419}
1420
1421status_t AudioSystem::getMasterBalance(float *balance)
1422{
1423 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1424 if (af == 0) return PERMISSION_DENIED;
1425 return af->getMasterBalance(balance);
1426}
1427
Eric Laurentac9cef52017-06-09 15:46:26 -07001428float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1429{
1430 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1431 if (aps == 0) return NAN;
1432 return aps->getStreamVolumeDB(stream, index, device);
1433}
1434
jiabin46a76fa2018-01-05 10:18:21 -08001435status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1436{
1437 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1438 if (af == 0) return PERMISSION_DENIED;
1439 return af->getMicrophones(microphones);
1440}
1441
Eric Laurent42896a02019-09-27 15:40:33 -07001442status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1443 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1444 if (af == nullptr) return PERMISSION_DENIED;
1445 return af->setAudioHalPids(pids);
1446}
1447
jiabin81772902018-04-02 17:52:27 -07001448status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1449 audio_format_t *surroundFormats,
1450 bool *surroundFormatsEnabled,
1451 bool reported)
1452{
1453 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1454 if (aps == 0) return PERMISSION_DENIED;
1455 return aps->getSurroundFormats(
1456 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1457}
1458
1459status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1460{
1461 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1462 if (aps == 0) return PERMISSION_DENIED;
1463 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1464}
1465
Eric Laurentb78763e2018-10-17 10:08:02 -07001466status_t AudioSystem::setAssistantUid(uid_t uid)
1467{
1468 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1469 if (aps == 0) return PERMISSION_DENIED;
1470
1471 return aps->setAssistantUid(uid);
1472}
1473
1474status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1475{
1476 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1477 if (aps == 0) return PERMISSION_DENIED;
1478
1479 return aps->setA11yServicesUids(uids);
1480}
1481
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001482status_t AudioSystem::setCurrentImeUid(uid_t uid)
1483{
1484 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1485 if (aps == 0) return PERMISSION_DENIED;
1486
1487 return aps->setCurrentImeUid(uid);
1488}
1489
jiabin6012f912018-11-02 17:06:30 -07001490bool AudioSystem::isHapticPlaybackSupported()
1491{
1492 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1493 if (aps == 0) return false;
1494 return aps->isHapticPlaybackSupported();
1495}
1496
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001497status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001498 std::vector<audio_format_t> *formats) {
1499 const sp <IAudioPolicyService>
1500 & aps = AudioSystem::get_audio_policy_service();
1501 if (aps == 0) return PERMISSION_DENIED;
1502 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1503}
1504
1505status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001506{
1507 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1508 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001509 return aps->listAudioProductStrategies(strategies);
1510}
1511
1512audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1513{
1514 AudioProductStrategyVector strategies;
1515 listAudioProductStrategies(strategies);
1516 for (const auto &strategy : strategies) {
1517 auto attrVect = strategy.getAudioAttributes();
1518 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1519 return attributes.getStreamType() == stream; });
1520 if (iter != end(attrVect)) {
1521 return iter->getAttributes();
1522 }
1523 }
1524 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1525 return AUDIO_ATTRIBUTES_INITIALIZER;
1526}
1527
1528audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1529{
François Gaffie4b2018b2018-11-07 11:18:59 +01001530 product_strategy_t psId;
1531 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1532 if (ret != NO_ERROR) {
1533 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1534 return AUDIO_STREAM_MUSIC;
1535 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001536 AudioProductStrategyVector strategies;
1537 listAudioProductStrategies(strategies);
1538 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001539 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001540 auto attrVect = strategy.getAudioAttributes();
1541 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1542 return AudioProductStrategy::attributesMatches(
1543 refAttr.getAttributes(), attr); });
1544 if (iter != end(attrVect)) {
1545 return iter->getStreamType();
1546 }
1547 }
1548 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001549 switch (attr.usage) {
1550 case AUDIO_USAGE_VIRTUAL_SOURCE:
1551 // virtual source is not expected to have an associated product strategy
1552 break;
1553 default:
1554 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1555 break;
1556 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001557 return AUDIO_STREAM_MUSIC;
1558}
1559
François Gaffie4b2018b2018-11-07 11:18:59 +01001560status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1561 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001562{
1563 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001564 if (aps == 0) return PERMISSION_DENIED;
1565 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1566}
1567
1568status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1569{
1570 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1571 if (aps == 0) return PERMISSION_DENIED;
1572 return aps->listAudioVolumeGroups(groups);
1573}
1574
1575status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1576 volume_group_t &volumeGroup)
1577{
1578 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1579 if (aps == 0) return PERMISSION_DENIED;
1580 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001581}
Eric Laurentb78763e2018-10-17 10:08:02 -07001582
Eric Laurent6ede98f2019-06-11 14:50:30 -07001583status_t AudioSystem::setRttEnabled(bool enabled)
1584{
1585 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1586 if (aps == 0) return PERMISSION_DENIED;
1587 return aps->setRttEnabled(enabled);
1588}
1589
Eric Laurent8340e672019-11-06 11:01:08 -08001590bool AudioSystem::isCallScreenModeSupported()
1591{
1592 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1593 if (aps == 0) return false;
1594 return aps->isCallScreenModeSupported();
1595}
1596
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001597status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1598 const AudioDeviceTypeAddr &device)
1599{
1600 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1601 if (aps == 0) {
1602 return PERMISSION_DENIED;
1603 }
1604 return aps->setPreferredDeviceForStrategy(strategy, device);
1605}
1606
1607status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1608{
1609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1610 if (aps == 0) {
1611 return PERMISSION_DENIED;
1612 }
1613 return aps->removePreferredDeviceForStrategy(strategy);
1614}
1615
1616status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1617 AudioDeviceTypeAddr &device)
1618{
1619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1620 if (aps == 0) {
1621 return PERMISSION_DENIED;
1622 }
1623 return aps->getPreferredDeviceForStrategy(strategy, device);
1624}
1625
Eric Laurentc2f1f072009-07-17 12:17:14 -07001626// ---------------------------------------------------------------------------
1627
Eric Laurente8726fe2015-06-26 09:39:24 -07001628int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001629 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001630{
1631 Mutex::Autolock _l(mLock);
1632 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001633 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001634 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001635 }
1636 }
Eric Laurent296fb132015-05-01 11:38:42 -07001637 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001638 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001639}
1640
Eric Laurente8726fe2015-06-26 09:39:24 -07001641int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001642 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001643{
1644 Mutex::Autolock _l(mLock);
1645 size_t i;
1646 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001647 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001648 break;
1649 }
1650 }
1651 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001652 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001653 }
1654 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001655 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001656}
1657
Eric Laurent296fb132015-05-01 11:38:42 -07001658
Eric Laurentb28753e2015-04-01 13:06:28 -07001659void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1660{
1661 Mutex::Autolock _l(mLock);
1662 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1663 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1664 }
1665}
1666
1667void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1668{
1669 Mutex::Autolock _l(mLock);
1670 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1671 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1672 }
1673}
1674
François Gaffiecfe17322018-11-07 13:41:29 +01001675// ----------------------------------------------------------------------------
1676int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1677 const sp<AudioVolumeGroupCallback>& callback)
1678{
1679 Mutex::Autolock _l(mLock);
1680 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1681 if (mAudioVolumeGroupCallback[i] == callback) {
1682 return -1;
1683 }
1684 }
1685 mAudioVolumeGroupCallback.add(callback);
1686 return mAudioVolumeGroupCallback.size();
1687}
1688
1689int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1690 const sp<AudioVolumeGroupCallback>& callback)
1691{
1692 Mutex::Autolock _l(mLock);
1693 size_t i;
1694 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1695 if (mAudioVolumeGroupCallback[i] == callback) {
1696 break;
1697 }
1698 }
1699 if (i == mAudioVolumeGroupCallback.size()) {
1700 return -1;
1701 }
1702 mAudioVolumeGroupCallback.removeAt(i);
1703 return mAudioVolumeGroupCallback.size();
1704}
1705
1706void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1707 int flags)
1708{
1709 Mutex::Autolock _l(mLock);
1710 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1711 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1712 }
1713}
1714// ----------------------------------------------------------------------------
1715
Jean-Michel Trivide801052015-04-14 19:10:14 -07001716void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1717 String8 regId, int32_t state)
1718{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001719 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1720 dynamic_policy_callback cb = NULL;
1721 {
1722 Mutex::Autolock _l(AudioSystem::gLock);
1723 cb = gDynPolicyCallback;
1724 }
1725
1726 if (cb != NULL) {
1727 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1728 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001729}
1730
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001731void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001732 int event,
1733 const record_client_info_t *clientInfo,
1734 const audio_config_base_t *clientConfig,
1735 std::vector<effect_descriptor_t> clientEffects,
1736 const audio_config_base_t *deviceConfig,
1737 std::vector<effect_descriptor_t> effects,
1738 audio_patch_handle_t patchHandle,
1739 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001740 record_config_callback cb = NULL;
1741 {
1742 Mutex::Autolock _l(AudioSystem::gLock);
1743 cb = gRecordConfigCallback;
1744 }
1745
1746 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001747 cb(event, clientInfo, clientConfig, clientEffects,
1748 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001749 }
1750}
1751
Glenn Kasten4944acb2013-08-19 08:39:20 -07001752void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1753{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001754 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001755 Mutex::Autolock _l(mLock);
1756 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1757 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001758 }
François Gaffiecfe17322018-11-07 13:41:29 +01001759 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1760 mAudioVolumeGroupCallback[i]->onServiceDied();
1761 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001762 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001763 {
1764 Mutex::Autolock _l(gLockAPS);
1765 AudioSystem::gAudioPolicyService.clear();
1766 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001767
Steve Block5ff1dd52012-01-05 23:22:43 +00001768 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001769}
1770
Glenn Kasten40bc9062015-03-20 09:09:33 -07001771} // namespace android