blob: e0ca07edab031ae260859a63475d0b1f50dcdb66 [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
Glenn Kastend848eb42016-03-08 13:42:11 -0800436void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
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) {
Marco Nelissend457c972014-02-11 08:47:07 -0800440 af->acquireAudioSessionId(audioSession, pid);
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
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001029audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001030{
1031 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001032 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001033 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001034 return aps->getOutputForEffect(desc);
1035}
1036
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001037status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001038 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001039 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001040 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001041 int id)
1042{
1043 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1044 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001045 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001046}
1047
1048status_t AudioSystem::unregisterEffect(int id)
1049{
1050 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1051 if (aps == 0) return PERMISSION_DENIED;
1052 return aps->unregisterEffect(id);
1053}
1054
Eric Laurentdb7c0792011-08-10 10:37:50 -07001055status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1056{
1057 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1058 if (aps == 0) return PERMISSION_DENIED;
1059 return aps->setEffectEnabled(id, enabled);
1060}
1061
Eric Laurent6c796322019-04-09 14:13:17 -07001062status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1063{
1064 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1065 if (aps == 0) return PERMISSION_DENIED;
1066 return aps->moveEffectsToIo(ids, io);
1067}
1068
Glenn Kastenfff6d712012-01-12 16:38:12 -08001069status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001070{
Eric Laurenteda6c362011-02-02 09:33:30 -08001071 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1072 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001073 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001074 *state = aps->isStreamActive(stream, inPastMs);
1075 return NO_ERROR;
1076}
1077
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001078status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1079 uint32_t inPastMs)
1080{
1081 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1082 if (aps == 0) return PERMISSION_DENIED;
1083 if (state == NULL) return BAD_VALUE;
1084 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1085 return NO_ERROR;
1086}
1087
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001088status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1089{
1090 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1091 if (aps == 0) return PERMISSION_DENIED;
1092 if (state == NULL) return BAD_VALUE;
1093 *state = aps->isSourceActive(stream);
1094 return NO_ERROR;
1095}
1096
Glenn Kasten3b16c762012-11-14 08:44:39 -08001097uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001098{
1099 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1100 if (af == 0) return 0;
1101 return af->getPrimaryOutputSamplingRate();
1102}
1103
Glenn Kastene33054e2012-11-14 12:54:39 -08001104size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001105{
1106 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1107 if (af == 0) return 0;
1108 return af->getPrimaryOutputFrameCount();
1109}
Eric Laurenteda6c362011-02-02 09:33:30 -08001110
Andy Hung6f248bb2018-01-23 14:04:37 -08001111status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001112{
1113 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1114 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001115 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001116}
1117
Eric Laurent9f6530f2011-08-30 10:18:54 -07001118void AudioSystem::clearAudioConfigCache()
1119{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001120 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001122 {
1123 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001124 if (gAudioFlingerClient != 0) {
1125 gAudioFlingerClient->clearIoCache();
1126 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001127 gAudioFlinger.clear();
1128 }
1129 {
1130 Mutex::Autolock _l(gLockAPS);
1131 gAudioPolicyService.clear();
1132 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001133}
1134
Kevin Rocardb99cc752019-03-21 20:52:24 -07001135status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1136 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1137 if (aps == nullptr) return PERMISSION_DENIED;
1138 return aps->setAllowedCapturePolicy(uid, flags);
1139}
1140
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001141bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1142{
1143 ALOGV("isOffloadSupported()");
1144 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1145 if (aps == 0) return false;
1146 return aps->isOffloadSupported(info);
1147}
1148
Eric Laurent203b1a12014-04-01 10:34:16 -07001149status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1150 audio_port_type_t type,
1151 unsigned int *num_ports,
1152 struct audio_port *ports,
1153 unsigned int *generation)
1154{
1155 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1156 if (aps == 0) return PERMISSION_DENIED;
1157 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1158}
1159
1160status_t AudioSystem::getAudioPort(struct audio_port *port)
1161{
1162 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1163 if (aps == 0) return PERMISSION_DENIED;
1164 return aps->getAudioPort(port);
1165}
1166
1167status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1168 audio_patch_handle_t *handle)
1169{
1170 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1171 if (aps == 0) return PERMISSION_DENIED;
1172 return aps->createAudioPatch(patch, handle);
1173}
1174
1175status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1176{
1177 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1178 if (aps == 0) return PERMISSION_DENIED;
1179 return aps->releaseAudioPatch(handle);
1180}
1181
1182status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1183 struct audio_patch *patches,
1184 unsigned int *generation)
1185{
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == 0) return PERMISSION_DENIED;
1188 return aps->listAudioPatches(num_patches, patches, generation);
1189}
1190
1191status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1192{
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == 0) return PERMISSION_DENIED;
1195 return aps->setAudioPortConfig(config);
1196}
1197
Eric Laurent296fb132015-05-01 11:38:42 -07001198status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001199{
Eric Laurentb28753e2015-04-01 13:06:28 -07001200 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1201 if (aps == 0) return PERMISSION_DENIED;
1202
1203 Mutex::Autolock _l(gLockAPS);
1204 if (gAudioPolicyServiceClient == 0) {
1205 return NO_INIT;
1206 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001207 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1208 if (ret == 1) {
1209 aps->setAudioPortCallbacksEnabled(true);
1210 }
1211 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001212}
1213
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001214/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001215status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001216{
1217 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1218 if (aps == 0) return PERMISSION_DENIED;
1219
1220 Mutex::Autolock _l(gLockAPS);
1221 if (gAudioPolicyServiceClient == 0) {
1222 return NO_INIT;
1223 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001224 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1225 if (ret == 0) {
1226 aps->setAudioPortCallbacksEnabled(false);
1227 }
1228 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001229}
1230
François Gaffiecfe17322018-11-07 13:41:29 +01001231status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1232{
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 }
1240 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1241 if (ret == 1) {
1242 aps->setAudioVolumeGroupCallbacksEnabled(true);
1243 }
1244 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1245}
1246
1247status_t AudioSystem::removeAudioVolumeGroupCallback(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->removeAudioVolumeGroupCallback(callback);
1257 if (ret == 0) {
1258 aps->setAudioVolumeGroupCallbacksEnabled(false);
1259 }
1260 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1261}
1262
Eric Laurent296fb132015-05-01 11:38:42 -07001263status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001264 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1265 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001266{
1267 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1268 if (afc == 0) {
1269 return NO_INIT;
1270 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001271 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001272 if (status == NO_ERROR) {
1273 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1274 if (af != 0) {
1275 af->registerClient(afc);
1276 }
1277 }
1278 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001279}
1280
1281status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001282 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1283 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001284{
1285 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1286 if (afc == 0) {
1287 return NO_INIT;
1288 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001289 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001290}
1291
1292audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1293{
1294 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1295 if (af == 0) return PERMISSION_DENIED;
1296 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1297 if (desc == 0) {
1298 return AUDIO_PORT_HANDLE_NONE;
1299 }
1300 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001301}
1302
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001303status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1304 audio_io_handle_t *ioHandle,
1305 audio_devices_t *device)
1306{
1307 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1308 if (aps == 0) return PERMISSION_DENIED;
1309 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1310}
1311
1312status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1313{
1314 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1315 if (aps == 0) return PERMISSION_DENIED;
1316 return aps->releaseSoundTriggerSession(session);
1317}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001318
1319audio_mode_t AudioSystem::getPhoneState()
1320{
1321 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1322 if (aps == 0) return AUDIO_MODE_INVALID;
1323 return aps->getPhoneState();
1324}
1325
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001326status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001327{
1328 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1329 if (aps == 0) return PERMISSION_DENIED;
1330 return aps->registerPolicyMixes(mixes, registration);
1331}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001332
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001333status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1334{
1335 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1336 if (aps == 0) return PERMISSION_DENIED;
1337 return aps->setUidDeviceAffinities(uid, devices);
1338}
1339
1340status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1341 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1342 if (aps == 0) return PERMISSION_DENIED;
1343 return aps->removeUidDeviceAffinities(uid);
1344}
1345
Eric Laurent554a2772015-04-10 11:29:24 -07001346status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1347 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001348 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001349{
1350 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1351 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001352 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001353}
1354
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001355status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001356{
1357 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1358 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001359 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001360}
1361
Andy Hung2ddee192015-12-18 17:34:44 -08001362status_t AudioSystem::setMasterMono(bool mono)
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->setMasterMono(mono);
1367}
1368
1369status_t AudioSystem::getMasterMono(bool *mono)
1370{
1371 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1372 if (aps == 0) return PERMISSION_DENIED;
1373 return aps->getMasterMono(mono);
1374}
1375
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001376status_t AudioSystem::setMasterBalance(float balance)
1377{
1378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1379 if (af == 0) return PERMISSION_DENIED;
1380 return af->setMasterBalance(balance);
1381}
1382
1383status_t AudioSystem::getMasterBalance(float *balance)
1384{
1385 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1386 if (af == 0) return PERMISSION_DENIED;
1387 return af->getMasterBalance(balance);
1388}
1389
Eric Laurentac9cef52017-06-09 15:46:26 -07001390float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1391{
1392 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1393 if (aps == 0) return NAN;
1394 return aps->getStreamVolumeDB(stream, index, device);
1395}
1396
jiabin46a76fa2018-01-05 10:18:21 -08001397status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1398{
1399 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1400 if (af == 0) return PERMISSION_DENIED;
1401 return af->getMicrophones(microphones);
1402}
1403
Eric Laurent42896a02019-09-27 15:40:33 -07001404status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1405 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1406 if (af == nullptr) return PERMISSION_DENIED;
1407 return af->setAudioHalPids(pids);
1408}
1409
jiabin81772902018-04-02 17:52:27 -07001410status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1411 audio_format_t *surroundFormats,
1412 bool *surroundFormatsEnabled,
1413 bool reported)
1414{
1415 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1416 if (aps == 0) return PERMISSION_DENIED;
1417 return aps->getSurroundFormats(
1418 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1419}
1420
1421status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1422{
1423 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1424 if (aps == 0) return PERMISSION_DENIED;
1425 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1426}
1427
Eric Laurentb78763e2018-10-17 10:08:02 -07001428status_t AudioSystem::setAssistantUid(uid_t uid)
1429{
1430 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1431 if (aps == 0) return PERMISSION_DENIED;
1432
1433 return aps->setAssistantUid(uid);
1434}
1435
1436status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1437{
1438 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1439 if (aps == 0) return PERMISSION_DENIED;
1440
1441 return aps->setA11yServicesUids(uids);
1442}
1443
jiabin6012f912018-11-02 17:06:30 -07001444bool AudioSystem::isHapticPlaybackSupported()
1445{
1446 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1447 if (aps == 0) return false;
1448 return aps->isHapticPlaybackSupported();
1449}
1450
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001451status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001452 std::vector<audio_format_t> *formats) {
1453 const sp <IAudioPolicyService>
1454 & aps = AudioSystem::get_audio_policy_service();
1455 if (aps == 0) return PERMISSION_DENIED;
1456 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1457}
1458
1459status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001460{
1461 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1462 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001463 return aps->listAudioProductStrategies(strategies);
1464}
1465
1466audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1467{
1468 AudioProductStrategyVector strategies;
1469 listAudioProductStrategies(strategies);
1470 for (const auto &strategy : strategies) {
1471 auto attrVect = strategy.getAudioAttributes();
1472 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1473 return attributes.getStreamType() == stream; });
1474 if (iter != end(attrVect)) {
1475 return iter->getAttributes();
1476 }
1477 }
1478 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1479 return AUDIO_ATTRIBUTES_INITIALIZER;
1480}
1481
1482audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1483{
François Gaffie4b2018b2018-11-07 11:18:59 +01001484 product_strategy_t psId;
1485 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1486 if (ret != NO_ERROR) {
1487 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1488 return AUDIO_STREAM_MUSIC;
1489 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001490 AudioProductStrategyVector strategies;
1491 listAudioProductStrategies(strategies);
1492 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001493 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001494 auto attrVect = strategy.getAudioAttributes();
1495 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1496 return AudioProductStrategy::attributesMatches(
1497 refAttr.getAttributes(), attr); });
1498 if (iter != end(attrVect)) {
1499 return iter->getStreamType();
1500 }
1501 }
1502 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001503 switch (attr.usage) {
1504 case AUDIO_USAGE_VIRTUAL_SOURCE:
1505 // virtual source is not expected to have an associated product strategy
1506 break;
1507 default:
1508 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1509 break;
1510 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001511 return AUDIO_STREAM_MUSIC;
1512}
1513
François Gaffie4b2018b2018-11-07 11:18:59 +01001514status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1515 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001516{
1517 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001518 if (aps == 0) return PERMISSION_DENIED;
1519 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1520}
1521
1522status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1523{
1524 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1525 if (aps == 0) return PERMISSION_DENIED;
1526 return aps->listAudioVolumeGroups(groups);
1527}
1528
1529status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1530 volume_group_t &volumeGroup)
1531{
1532 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1533 if (aps == 0) return PERMISSION_DENIED;
1534 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001535}
Eric Laurentb78763e2018-10-17 10:08:02 -07001536
Eric Laurent6ede98f2019-06-11 14:50:30 -07001537status_t AudioSystem::setRttEnabled(bool enabled)
1538{
1539 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1540 if (aps == 0) return PERMISSION_DENIED;
1541 return aps->setRttEnabled(enabled);
1542}
1543
Eric Laurent8340e672019-11-06 11:01:08 -08001544bool AudioSystem::isCallScreenModeSupported()
1545{
1546 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1547 if (aps == 0) return false;
1548 return aps->isCallScreenModeSupported();
1549}
1550
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001551status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1552 const AudioDeviceTypeAddr &device)
1553{
1554 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1555 if (aps == 0) {
1556 return PERMISSION_DENIED;
1557 }
1558 return aps->setPreferredDeviceForStrategy(strategy, device);
1559}
1560
1561status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1562{
1563 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1564 if (aps == 0) {
1565 return PERMISSION_DENIED;
1566 }
1567 return aps->removePreferredDeviceForStrategy(strategy);
1568}
1569
1570status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1571 AudioDeviceTypeAddr &device)
1572{
1573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1574 if (aps == 0) {
1575 return PERMISSION_DENIED;
1576 }
1577 return aps->getPreferredDeviceForStrategy(strategy, device);
1578}
1579
Eric Laurentc2f1f072009-07-17 12:17:14 -07001580// ---------------------------------------------------------------------------
1581
Eric Laurente8726fe2015-06-26 09:39:24 -07001582int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001583 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001584{
1585 Mutex::Autolock _l(mLock);
1586 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001587 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001588 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001589 }
1590 }
Eric Laurent296fb132015-05-01 11:38:42 -07001591 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001592 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001593}
1594
Eric Laurente8726fe2015-06-26 09:39:24 -07001595int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001596 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001597{
1598 Mutex::Autolock _l(mLock);
1599 size_t i;
1600 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001601 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001602 break;
1603 }
1604 }
1605 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001606 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001607 }
1608 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001609 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001610}
1611
Eric Laurent296fb132015-05-01 11:38:42 -07001612
Eric Laurentb28753e2015-04-01 13:06:28 -07001613void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1614{
1615 Mutex::Autolock _l(mLock);
1616 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1617 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1618 }
1619}
1620
1621void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1622{
1623 Mutex::Autolock _l(mLock);
1624 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1625 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1626 }
1627}
1628
François Gaffiecfe17322018-11-07 13:41:29 +01001629// ----------------------------------------------------------------------------
1630int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1631 const sp<AudioVolumeGroupCallback>& callback)
1632{
1633 Mutex::Autolock _l(mLock);
1634 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1635 if (mAudioVolumeGroupCallback[i] == callback) {
1636 return -1;
1637 }
1638 }
1639 mAudioVolumeGroupCallback.add(callback);
1640 return mAudioVolumeGroupCallback.size();
1641}
1642
1643int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1644 const sp<AudioVolumeGroupCallback>& callback)
1645{
1646 Mutex::Autolock _l(mLock);
1647 size_t i;
1648 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1649 if (mAudioVolumeGroupCallback[i] == callback) {
1650 break;
1651 }
1652 }
1653 if (i == mAudioVolumeGroupCallback.size()) {
1654 return -1;
1655 }
1656 mAudioVolumeGroupCallback.removeAt(i);
1657 return mAudioVolumeGroupCallback.size();
1658}
1659
1660void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1661 int flags)
1662{
1663 Mutex::Autolock _l(mLock);
1664 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1665 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1666 }
1667}
1668// ----------------------------------------------------------------------------
1669
Jean-Michel Trivide801052015-04-14 19:10:14 -07001670void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1671 String8 regId, int32_t state)
1672{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001673 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1674 dynamic_policy_callback cb = NULL;
1675 {
1676 Mutex::Autolock _l(AudioSystem::gLock);
1677 cb = gDynPolicyCallback;
1678 }
1679
1680 if (cb != NULL) {
1681 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1682 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001683}
1684
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001685void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001686 int event,
1687 const record_client_info_t *clientInfo,
1688 const audio_config_base_t *clientConfig,
1689 std::vector<effect_descriptor_t> clientEffects,
1690 const audio_config_base_t *deviceConfig,
1691 std::vector<effect_descriptor_t> effects,
1692 audio_patch_handle_t patchHandle,
1693 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001694 record_config_callback cb = NULL;
1695 {
1696 Mutex::Autolock _l(AudioSystem::gLock);
1697 cb = gRecordConfigCallback;
1698 }
1699
1700 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001701 cb(event, clientInfo, clientConfig, clientEffects,
1702 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001703 }
1704}
1705
Glenn Kasten4944acb2013-08-19 08:39:20 -07001706void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1707{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001708 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001709 Mutex::Autolock _l(mLock);
1710 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1711 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001712 }
François Gaffiecfe17322018-11-07 13:41:29 +01001713 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1714 mAudioVolumeGroupCallback[i]->onServiceDied();
1715 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001716 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001717 {
1718 Mutex::Autolock _l(gLockAPS);
1719 AudioSystem::gAudioPolicyService.clear();
1720 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001721
Steve Block5ff1dd52012-01-05 23:22:43 +00001722 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001723}
1724
Glenn Kasten40bc9062015-03-20 09:09:33 -07001725} // namespace android