blob: 6914d5ad0d3617b5df0d852c1ff7c669e52cb872 [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
Dima Zavinfce7a472011-04-19 22:30:36 -0700795status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
796 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800797 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800798 const char *device_name,
799 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700800{
801 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700802 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800803 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700804
Eric Laurentc2f1f072009-07-17 12:17:14 -0700805 if (aps == 0) return PERMISSION_DENIED;
806
Eric Laurent71b63e32011-09-02 14:20:56 -0700807 if (device_address != NULL) {
808 address = device_address;
809 }
Paul McLeane743a472015-01-28 11:07:31 -0800810 if (device_name != NULL) {
811 name = device_name;
812 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800813 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700814}
815
Dima Zavinfce7a472011-04-19 22:30:36 -0700816audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700817 const char *device_address)
818{
819 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700820 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700821
822 return aps->getDeviceConnectionState(device, device_address);
823}
824
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800825status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
826 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800827 const char *device_name,
828 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800829{
830 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
831 const char *address = "";
832 const char *name = "";
833
834 if (aps == 0) return PERMISSION_DENIED;
835
836 if (device_address != NULL) {
837 address = device_address;
838 }
839 if (device_name != NULL) {
840 name = device_name;
841 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800842 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800843}
844
Glenn Kastenf78aee72012-01-04 11:00:47 -0800845status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700846{
Glenn Kasten347966c2012-01-18 14:58:32 -0800847 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700848 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
849 if (aps == 0) return PERMISSION_DENIED;
850
851 return aps->setPhoneState(state);
852}
853
Dima Zavinfce7a472011-04-19 22:30:36 -0700854status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855{
856 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
857 if (aps == 0) return PERMISSION_DENIED;
858 return aps->setForceUse(usage, config);
859}
860
Dima Zavinfce7a472011-04-19 22:30:36 -0700861audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862{
863 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700864 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700865 return aps->getForceUse(usage);
866}
867
868
Eric Laurentf4e63452017-11-06 19:31:46 +0000869audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700870{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700871 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
872 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000873 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700874}
875
Eric Laurent42984412019-05-09 17:57:03 -0700876status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800877 audio_io_handle_t *output,
878 audio_session_t session,
879 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200880 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700881 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800882 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800883 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700884 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800885 audio_port_handle_t *portId,
886 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700887{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700888 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800889 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200890 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800891 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800892 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700893}
894
Eric Laurentd7fe0862018-07-14 16:48:01 -0700895status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
898 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700899 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700900}
901
Eric Laurentd7fe0862018-07-14 16:48:01 -0700902status_t AudioSystem::stopOutput(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->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907}
908
Eric Laurentd7fe0862018-07-14 16:48:01 -0700909void AudioSystem::releaseOutput(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;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700913 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700914}
915
Eric Laurentcaf7f482014-11-25 17:50:47 -0800916status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
917 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700918 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800919 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700920 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700921 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800922 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800923 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600924 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700925 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800926 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700927{
928 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800929 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600930 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700931 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800932 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700933}
934
Eric Laurent4eb58f12018-12-07 16:41:02 -0800935status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700936{
937 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
938 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800939 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940}
941
Eric Laurentfee19762018-01-29 18:44:13 -0800942status_t AudioSystem::stopInput(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 Laurentfee19762018-01-29 18:44:13 -0800946 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947}
948
Eric Laurentfee19762018-01-29 18:44:13 -0800949void AudioSystem::releaseInput(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;
Eric Laurentfee19762018-01-29 18:44:13 -0800953 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954}
955
Dima Zavinfce7a472011-04-19 22:30:36 -0700956status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957 int indexMin,
958 int indexMax)
959{
960 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
961 if (aps == 0) return PERMISSION_DENIED;
962 return aps->initStreamVolume(stream, indexMin, indexMax);
963}
964
Eric Laurent83844cc2011-11-18 16:43:31 -0800965status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
966 int index,
967 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700968{
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
970 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800971 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700972}
973
Eric Laurent83844cc2011-11-18 16:43:31 -0800974status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
975 int *index,
976 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700977{
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800980 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981}
982
François Gaffiecfe17322018-11-07 13:41:29 +0100983status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
984 int index,
985 audio_devices_t device)
986{
987 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
988 if (aps == 0) return PERMISSION_DENIED;
989 return aps->setVolumeIndexForAttributes(attr, index, device);
990}
991
992status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
993 int &index,
994 audio_devices_t device)
995{
996 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
997 if (aps == 0) return PERMISSION_DENIED;
998 return aps->getVolumeIndexForAttributes(attr, index, device);
999}
1000
1001status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
1005 return aps->getMaxVolumeIndexForAttributes(attr, index);
1006}
1007
1008status_t AudioSystem::getMinVolumeIndexForAttributes(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->getMinVolumeIndexForAttributes(attr, index);
1013}
1014
Dima Zavinfce7a472011-04-19 22:30:36 -07001015uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001016{
1017 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001018 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001019 return aps->getStrategyForStream(stream);
1020}
1021
Eric Laurent63742522012-03-08 13:42:42 -08001022audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001023{
1024 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001025 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001026 return aps->getDevicesForStream(stream);
1027}
1028
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001029status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1030 AudioDeviceTypeAddrVector *devices) {
1031 if (devices == nullptr) {
1032 return BAD_VALUE;
1033 }
1034 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1035 if (aps == 0) return PERMISSION_DENIED;
1036 return aps->getDevicesForAttributes(aa, devices);
1037}
1038
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001039audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001040{
1041 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001042 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001043 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001044 return aps->getOutputForEffect(desc);
1045}
1046
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001047status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001048 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001049 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001050 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001051 int id)
1052{
1053 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1054 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001055 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001056}
1057
1058status_t AudioSystem::unregisterEffect(int id)
1059{
1060 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1061 if (aps == 0) return PERMISSION_DENIED;
1062 return aps->unregisterEffect(id);
1063}
1064
Eric Laurentdb7c0792011-08-10 10:37:50 -07001065status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1066{
1067 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1068 if (aps == 0) return PERMISSION_DENIED;
1069 return aps->setEffectEnabled(id, enabled);
1070}
1071
Eric Laurent6c796322019-04-09 14:13:17 -07001072status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1073{
1074 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1075 if (aps == 0) return PERMISSION_DENIED;
1076 return aps->moveEffectsToIo(ids, io);
1077}
1078
Glenn Kastenfff6d712012-01-12 16:38:12 -08001079status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001080{
Eric Laurenteda6c362011-02-02 09:33:30 -08001081 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1082 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001083 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001084 *state = aps->isStreamActive(stream, inPastMs);
1085 return NO_ERROR;
1086}
1087
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001088status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1089 uint32_t inPastMs)
1090{
1091 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1092 if (aps == 0) return PERMISSION_DENIED;
1093 if (state == NULL) return BAD_VALUE;
1094 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1095 return NO_ERROR;
1096}
1097
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001098status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1099{
1100 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1101 if (aps == 0) return PERMISSION_DENIED;
1102 if (state == NULL) return BAD_VALUE;
1103 *state = aps->isSourceActive(stream);
1104 return NO_ERROR;
1105}
1106
Glenn Kasten3b16c762012-11-14 08:44:39 -08001107uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001108{
1109 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1110 if (af == 0) return 0;
1111 return af->getPrimaryOutputSamplingRate();
1112}
1113
Glenn Kastene33054e2012-11-14 12:54:39 -08001114size_t AudioSystem::getPrimaryOutputFrameCount()
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->getPrimaryOutputFrameCount();
1119}
Eric Laurenteda6c362011-02-02 09:33:30 -08001120
Andy Hung6f248bb2018-01-23 14:04:37 -08001121status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001122{
1123 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1124 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001125 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001126}
1127
Eric Laurent9f6530f2011-08-30 10:18:54 -07001128void AudioSystem::clearAudioConfigCache()
1129{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001130 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001132 {
1133 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001134 if (gAudioFlingerClient != 0) {
1135 gAudioFlingerClient->clearIoCache();
1136 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001137 gAudioFlinger.clear();
1138 }
1139 {
1140 Mutex::Autolock _l(gLockAPS);
1141 gAudioPolicyService.clear();
1142 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001143}
1144
Hayden Gomes524159d2019-12-23 14:41:47 -08001145status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1146 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1147 if (aps == nullptr) return PERMISSION_DENIED;
1148 return aps->setSupportedSystemUsages(systemUsages);
1149}
1150
Kevin Rocardb99cc752019-03-21 20:52:24 -07001151status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1152 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1153 if (aps == nullptr) return PERMISSION_DENIED;
1154 return aps->setAllowedCapturePolicy(uid, flags);
1155}
1156
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001157bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1158{
1159 ALOGV("isOffloadSupported()");
1160 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1161 if (aps == 0) return false;
1162 return aps->isOffloadSupported(info);
1163}
1164
Eric Laurent203b1a12014-04-01 10:34:16 -07001165status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1166 audio_port_type_t type,
1167 unsigned int *num_ports,
1168 struct audio_port *ports,
1169 unsigned int *generation)
1170{
1171 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1172 if (aps == 0) return PERMISSION_DENIED;
1173 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1174}
1175
1176status_t AudioSystem::getAudioPort(struct audio_port *port)
1177{
1178 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1179 if (aps == 0) return PERMISSION_DENIED;
1180 return aps->getAudioPort(port);
1181}
1182
1183status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1184 audio_patch_handle_t *handle)
1185{
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == 0) return PERMISSION_DENIED;
1188 return aps->createAudioPatch(patch, handle);
1189}
1190
1191status_t AudioSystem::releaseAudioPatch(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->releaseAudioPatch(handle);
1196}
1197
1198status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1199 struct audio_patch *patches,
1200 unsigned int *generation)
1201{
1202 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1203 if (aps == 0) return PERMISSION_DENIED;
1204 return aps->listAudioPatches(num_patches, patches, generation);
1205}
1206
1207status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1208{
1209 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1210 if (aps == 0) return PERMISSION_DENIED;
1211 return aps->setAudioPortConfig(config);
1212}
1213
Eric Laurent296fb132015-05-01 11:38:42 -07001214status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001215{
Eric Laurentb28753e2015-04-01 13:06:28 -07001216 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1217 if (aps == 0) return PERMISSION_DENIED;
1218
1219 Mutex::Autolock _l(gLockAPS);
1220 if (gAudioPolicyServiceClient == 0) {
1221 return NO_INIT;
1222 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001223 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1224 if (ret == 1) {
1225 aps->setAudioPortCallbacksEnabled(true);
1226 }
1227 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001228}
1229
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001230/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001231status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001232{
1233 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1234 if (aps == 0) return PERMISSION_DENIED;
1235
1236 Mutex::Autolock _l(gLockAPS);
1237 if (gAudioPolicyServiceClient == 0) {
1238 return NO_INIT;
1239 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001240 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1241 if (ret == 0) {
1242 aps->setAudioPortCallbacksEnabled(false);
1243 }
1244 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001245}
1246
François Gaffiecfe17322018-11-07 13:41:29 +01001247status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1248{
1249 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1250 if (aps == 0) return PERMISSION_DENIED;
1251
1252 Mutex::Autolock _l(gLockAPS);
1253 if (gAudioPolicyServiceClient == 0) {
1254 return NO_INIT;
1255 }
1256 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1257 if (ret == 1) {
1258 aps->setAudioVolumeGroupCallbacksEnabled(true);
1259 }
1260 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1261}
1262
1263status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1264{
1265 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1266 if (aps == 0) return PERMISSION_DENIED;
1267
1268 Mutex::Autolock _l(gLockAPS);
1269 if (gAudioPolicyServiceClient == 0) {
1270 return NO_INIT;
1271 }
1272 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1273 if (ret == 0) {
1274 aps->setAudioVolumeGroupCallbacksEnabled(false);
1275 }
1276 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1277}
1278
Eric Laurent296fb132015-05-01 11:38:42 -07001279status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001280 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1281 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001282{
1283 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1284 if (afc == 0) {
1285 return NO_INIT;
1286 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001287 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001288 if (status == NO_ERROR) {
1289 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1290 if (af != 0) {
1291 af->registerClient(afc);
1292 }
1293 }
1294 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001295}
1296
1297status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001298 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1299 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001300{
1301 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1302 if (afc == 0) {
1303 return NO_INIT;
1304 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001305 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001306}
1307
1308audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1309{
1310 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1311 if (af == 0) return PERMISSION_DENIED;
1312 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1313 if (desc == 0) {
1314 return AUDIO_PORT_HANDLE_NONE;
1315 }
1316 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001317}
1318
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001319status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1320 audio_io_handle_t *ioHandle,
1321 audio_devices_t *device)
1322{
1323 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1324 if (aps == 0) return PERMISSION_DENIED;
1325 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1326}
1327
1328status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1329{
1330 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1331 if (aps == 0) return PERMISSION_DENIED;
1332 return aps->releaseSoundTriggerSession(session);
1333}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001334
1335audio_mode_t AudioSystem::getPhoneState()
1336{
1337 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1338 if (aps == 0) return AUDIO_MODE_INVALID;
1339 return aps->getPhoneState();
1340}
1341
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001342status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001343{
1344 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1345 if (aps == 0) return PERMISSION_DENIED;
1346 return aps->registerPolicyMixes(mixes, registration);
1347}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001348
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001349status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1350{
1351 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1352 if (aps == 0) return PERMISSION_DENIED;
1353 return aps->setUidDeviceAffinities(uid, devices);
1354}
1355
1356status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1357 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1358 if (aps == 0) return PERMISSION_DENIED;
1359 return aps->removeUidDeviceAffinities(uid);
1360}
1361
Eric Laurent554a2772015-04-10 11:29:24 -07001362status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1363 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001364 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001365{
1366 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1367 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001368 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001369}
1370
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001371status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001372{
1373 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1374 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001375 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001376}
1377
Andy Hung2ddee192015-12-18 17:34:44 -08001378status_t AudioSystem::setMasterMono(bool mono)
1379{
1380 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1381 if (aps == 0) return PERMISSION_DENIED;
1382 return aps->setMasterMono(mono);
1383}
1384
1385status_t AudioSystem::getMasterMono(bool *mono)
1386{
1387 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1388 if (aps == 0) return PERMISSION_DENIED;
1389 return aps->getMasterMono(mono);
1390}
1391
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001392status_t AudioSystem::setMasterBalance(float balance)
1393{
1394 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1395 if (af == 0) return PERMISSION_DENIED;
1396 return af->setMasterBalance(balance);
1397}
1398
1399status_t AudioSystem::getMasterBalance(float *balance)
1400{
1401 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1402 if (af == 0) return PERMISSION_DENIED;
1403 return af->getMasterBalance(balance);
1404}
1405
Eric Laurentac9cef52017-06-09 15:46:26 -07001406float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1407{
1408 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1409 if (aps == 0) return NAN;
1410 return aps->getStreamVolumeDB(stream, index, device);
1411}
1412
jiabin46a76fa2018-01-05 10:18:21 -08001413status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1414{
1415 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1416 if (af == 0) return PERMISSION_DENIED;
1417 return af->getMicrophones(microphones);
1418}
1419
Eric Laurent42896a02019-09-27 15:40:33 -07001420status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1422 if (af == nullptr) return PERMISSION_DENIED;
1423 return af->setAudioHalPids(pids);
1424}
1425
jiabin81772902018-04-02 17:52:27 -07001426status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1427 audio_format_t *surroundFormats,
1428 bool *surroundFormatsEnabled,
1429 bool reported)
1430{
1431 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1432 if (aps == 0) return PERMISSION_DENIED;
1433 return aps->getSurroundFormats(
1434 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1435}
1436
1437status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1438{
1439 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1440 if (aps == 0) return PERMISSION_DENIED;
1441 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1442}
1443
Eric Laurentb78763e2018-10-17 10:08:02 -07001444status_t AudioSystem::setAssistantUid(uid_t uid)
1445{
1446 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1447 if (aps == 0) return PERMISSION_DENIED;
1448
1449 return aps->setAssistantUid(uid);
1450}
1451
1452status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1453{
1454 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1455 if (aps == 0) return PERMISSION_DENIED;
1456
1457 return aps->setA11yServicesUids(uids);
1458}
1459
jiabin6012f912018-11-02 17:06:30 -07001460bool AudioSystem::isHapticPlaybackSupported()
1461{
1462 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1463 if (aps == 0) return false;
1464 return aps->isHapticPlaybackSupported();
1465}
1466
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001467status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001468 std::vector<audio_format_t> *formats) {
1469 const sp <IAudioPolicyService>
1470 & aps = AudioSystem::get_audio_policy_service();
1471 if (aps == 0) return PERMISSION_DENIED;
1472 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1473}
1474
1475status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001476{
1477 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1478 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001479 return aps->listAudioProductStrategies(strategies);
1480}
1481
1482audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1483{
1484 AudioProductStrategyVector strategies;
1485 listAudioProductStrategies(strategies);
1486 for (const auto &strategy : strategies) {
1487 auto attrVect = strategy.getAudioAttributes();
1488 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1489 return attributes.getStreamType() == stream; });
1490 if (iter != end(attrVect)) {
1491 return iter->getAttributes();
1492 }
1493 }
1494 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1495 return AUDIO_ATTRIBUTES_INITIALIZER;
1496}
1497
1498audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1499{
François Gaffie4b2018b2018-11-07 11:18:59 +01001500 product_strategy_t psId;
1501 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1502 if (ret != NO_ERROR) {
1503 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1504 return AUDIO_STREAM_MUSIC;
1505 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001506 AudioProductStrategyVector strategies;
1507 listAudioProductStrategies(strategies);
1508 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001509 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001510 auto attrVect = strategy.getAudioAttributes();
1511 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1512 return AudioProductStrategy::attributesMatches(
1513 refAttr.getAttributes(), attr); });
1514 if (iter != end(attrVect)) {
1515 return iter->getStreamType();
1516 }
1517 }
1518 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001519 switch (attr.usage) {
1520 case AUDIO_USAGE_VIRTUAL_SOURCE:
1521 // virtual source is not expected to have an associated product strategy
1522 break;
1523 default:
1524 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1525 break;
1526 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001527 return AUDIO_STREAM_MUSIC;
1528}
1529
François Gaffie4b2018b2018-11-07 11:18:59 +01001530status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1531 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001532{
1533 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001534 if (aps == 0) return PERMISSION_DENIED;
1535 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1536}
1537
1538status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1539{
1540 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1541 if (aps == 0) return PERMISSION_DENIED;
1542 return aps->listAudioVolumeGroups(groups);
1543}
1544
1545status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1546 volume_group_t &volumeGroup)
1547{
1548 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1549 if (aps == 0) return PERMISSION_DENIED;
1550 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001551}
Eric Laurentb78763e2018-10-17 10:08:02 -07001552
Eric Laurent6ede98f2019-06-11 14:50:30 -07001553status_t AudioSystem::setRttEnabled(bool enabled)
1554{
1555 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1556 if (aps == 0) return PERMISSION_DENIED;
1557 return aps->setRttEnabled(enabled);
1558}
1559
Eric Laurent8340e672019-11-06 11:01:08 -08001560bool AudioSystem::isCallScreenModeSupported()
1561{
1562 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1563 if (aps == 0) return false;
1564 return aps->isCallScreenModeSupported();
1565}
1566
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001567status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1568 const AudioDeviceTypeAddr &device)
1569{
1570 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1571 if (aps == 0) {
1572 return PERMISSION_DENIED;
1573 }
1574 return aps->setPreferredDeviceForStrategy(strategy, device);
1575}
1576
1577status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1578{
1579 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1580 if (aps == 0) {
1581 return PERMISSION_DENIED;
1582 }
1583 return aps->removePreferredDeviceForStrategy(strategy);
1584}
1585
1586status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1587 AudioDeviceTypeAddr &device)
1588{
1589 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1590 if (aps == 0) {
1591 return PERMISSION_DENIED;
1592 }
1593 return aps->getPreferredDeviceForStrategy(strategy, device);
1594}
1595
Eric Laurentc2f1f072009-07-17 12:17:14 -07001596// ---------------------------------------------------------------------------
1597
Eric Laurente8726fe2015-06-26 09:39:24 -07001598int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001599 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001600{
1601 Mutex::Autolock _l(mLock);
1602 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001603 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001604 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001605 }
1606 }
Eric Laurent296fb132015-05-01 11:38:42 -07001607 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001608 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001609}
1610
Eric Laurente8726fe2015-06-26 09:39:24 -07001611int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001612 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001613{
1614 Mutex::Autolock _l(mLock);
1615 size_t i;
1616 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001617 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001618 break;
1619 }
1620 }
1621 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001622 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001623 }
1624 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001625 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001626}
1627
Eric Laurent296fb132015-05-01 11:38:42 -07001628
Eric Laurentb28753e2015-04-01 13:06:28 -07001629void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1630{
1631 Mutex::Autolock _l(mLock);
1632 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1633 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1634 }
1635}
1636
1637void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1638{
1639 Mutex::Autolock _l(mLock);
1640 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1641 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1642 }
1643}
1644
François Gaffiecfe17322018-11-07 13:41:29 +01001645// ----------------------------------------------------------------------------
1646int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1647 const sp<AudioVolumeGroupCallback>& callback)
1648{
1649 Mutex::Autolock _l(mLock);
1650 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1651 if (mAudioVolumeGroupCallback[i] == callback) {
1652 return -1;
1653 }
1654 }
1655 mAudioVolumeGroupCallback.add(callback);
1656 return mAudioVolumeGroupCallback.size();
1657}
1658
1659int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1660 const sp<AudioVolumeGroupCallback>& callback)
1661{
1662 Mutex::Autolock _l(mLock);
1663 size_t i;
1664 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1665 if (mAudioVolumeGroupCallback[i] == callback) {
1666 break;
1667 }
1668 }
1669 if (i == mAudioVolumeGroupCallback.size()) {
1670 return -1;
1671 }
1672 mAudioVolumeGroupCallback.removeAt(i);
1673 return mAudioVolumeGroupCallback.size();
1674}
1675
1676void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1677 int flags)
1678{
1679 Mutex::Autolock _l(mLock);
1680 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1681 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1682 }
1683}
1684// ----------------------------------------------------------------------------
1685
Jean-Michel Trivide801052015-04-14 19:10:14 -07001686void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1687 String8 regId, int32_t state)
1688{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001689 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1690 dynamic_policy_callback cb = NULL;
1691 {
1692 Mutex::Autolock _l(AudioSystem::gLock);
1693 cb = gDynPolicyCallback;
1694 }
1695
1696 if (cb != NULL) {
1697 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1698 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001699}
1700
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001701void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001702 int event,
1703 const record_client_info_t *clientInfo,
1704 const audio_config_base_t *clientConfig,
1705 std::vector<effect_descriptor_t> clientEffects,
1706 const audio_config_base_t *deviceConfig,
1707 std::vector<effect_descriptor_t> effects,
1708 audio_patch_handle_t patchHandle,
1709 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001710 record_config_callback cb = NULL;
1711 {
1712 Mutex::Autolock _l(AudioSystem::gLock);
1713 cb = gRecordConfigCallback;
1714 }
1715
1716 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001717 cb(event, clientInfo, clientConfig, clientEffects,
1718 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001719 }
1720}
1721
Glenn Kasten4944acb2013-08-19 08:39:20 -07001722void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1723{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001724 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001725 Mutex::Autolock _l(mLock);
1726 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1727 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001728 }
François Gaffiecfe17322018-11-07 13:41:29 +01001729 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1730 mAudioVolumeGroupCallback[i]->onServiceDied();
1731 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001732 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001733 {
1734 Mutex::Autolock _l(gLockAPS);
1735 AudioSystem::gAudioPolicyService.clear();
1736 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001737
Steve Block5ff1dd52012-01-05 23:22:43 +00001738 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001739}
1740
Glenn Kasten40bc9062015-03-20 09:09:33 -07001741} // namespace android