blob: 74fa34342742f850f4687eeb935de55df67efdf6 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
22#include <android/media/BnCaptureStateListener.h>
Mathias Agopian75624082009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070024#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020025#include <binder/IPCThreadState.h>
Eric Laurent21da6472017-11-09 16:29:26 -080026#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070029#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010030#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <math.h>
32
Dima Zavin64760242011-05-11 14:15:23 -070033#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070034
Eric Laurentc2f1f072009-07-17 12:17:14 -070035// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070036
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037namespace android {
38
39// client singleton for AudioFlinger binder interface
40Mutex AudioSystem::gLock;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080041Mutex AudioSystem::gLockErrorCallbacks;
Glenn Kastend2d089f2014-11-05 11:48:12 -080042Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043sp<IAudioFlinger> AudioSystem::gAudioFlinger;
44sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080045std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070046dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080047record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070049// Required to be held while calling into gSoundTriggerCaptureStateListener.
50Mutex gSoundTriggerCaptureStateListenerLock;
51sp<AudioSystem::CaptureStateListener> gSoundTriggerCaptureStateListener = nullptr;
52
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080054const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080056 sp<IAudioFlinger> af;
57 sp<AudioFlingerClient> afc;
58 {
59 Mutex::Autolock _l(gLock);
60 if (gAudioFlinger == 0) {
61 sp<IServiceManager> sm = defaultServiceManager();
62 sp<IBinder> binder;
63 do {
64 binder = sm->getService(String16("media.audio_flinger"));
65 if (binder != 0)
66 break;
67 ALOGW("AudioFlinger not published, waiting...");
68 usleep(500000); // 0.5 s
69 } while (true);
70 if (gAudioFlingerClient == NULL) {
71 gAudioFlingerClient = new AudioFlingerClient();
72 } else {
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080073 reportError(NO_ERROR);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080075 binder->linkToDeath(gAudioFlingerClient);
76 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
77 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
78 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070079 // Make sure callbacks can be received by gAudioFlingerClient
80 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070081 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080082 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080084 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020085 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080086 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020087 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080088 }
89 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090}
91
Eric Laurent296fb132015-05-01 11:38:42 -070092const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
93{
94 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
95 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
96 if (af == 0) return 0;
97 Mutex::Autolock _l(gLock);
98 return gAudioFlingerClient;
99}
100
101sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
102{
103 sp<AudioIoDescriptor> desc;
104 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
105 if (afc != 0) {
106 desc = afc->getIoDescriptor(ioHandle);
107 }
108 return desc;
109}
110
Eric Laurent46291612013-07-18 14:38:44 -0700111/* static */ status_t AudioSystem::checkAudioFlinger()
112{
113 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
114 return NO_ERROR;
115 }
116 return DEAD_OBJECT;
117}
118
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700119// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
120
Glenn Kasten4944acb2013-08-19 08:39:20 -0700121status_t AudioSystem::muteMicrophone(bool state)
122{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
124 if (af == 0) return PERMISSION_DENIED;
125 return af->setMicMute(state);
126}
127
Glenn Kasten4944acb2013-08-19 08:39:20 -0700128status_t AudioSystem::isMicrophoneMuted(bool* state)
129{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
131 if (af == 0) return PERMISSION_DENIED;
132 *state = af->getMicMute();
133 return NO_ERROR;
134}
135
136status_t AudioSystem::setMasterVolume(float value)
137{
138 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
139 if (af == 0) return PERMISSION_DENIED;
140 af->setMasterVolume(value);
141 return NO_ERROR;
142}
143
144status_t AudioSystem::setMasterMute(bool mute)
145{
146 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
147 if (af == 0) return PERMISSION_DENIED;
148 af->setMasterMute(mute);
149 return NO_ERROR;
150}
151
152status_t AudioSystem::getMasterVolume(float* volume)
153{
154 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
155 if (af == 0) return PERMISSION_DENIED;
156 *volume = af->masterVolume();
157 return NO_ERROR;
158}
159
160status_t AudioSystem::getMasterMute(bool* mute)
161{
162 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
163 if (af == 0) return PERMISSION_DENIED;
164 *mute = af->masterMute();
165 return NO_ERROR;
166}
167
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800168status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
169 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170{
Dima Zavinfce7a472011-04-19 22:30:36 -0700171 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
173 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700174 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 return NO_ERROR;
176}
177
Glenn Kastenfff6d712012-01-12 16:38:12 -0800178status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179{
Dima Zavinfce7a472011-04-19 22:30:36 -0700180 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
182 if (af == 0) return PERMISSION_DENIED;
183 af->setStreamMute(stream, mute);
184 return NO_ERROR;
185}
186
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800187status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
188 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189{
Dima Zavinfce7a472011-04-19 22:30:36 -0700190 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
192 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700193 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 return NO_ERROR;
195}
196
Glenn Kastenfff6d712012-01-12 16:38:12 -0800197status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198{
Dima Zavinfce7a472011-04-19 22:30:36 -0700199 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
201 if (af == 0) return PERMISSION_DENIED;
202 *mute = af->streamMute(stream);
203 return NO_ERROR;
204}
205
Glenn Kastenf78aee72012-01-04 11:00:47 -0800206status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800208 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
210 if (af == 0) return PERMISSION_DENIED;
211 return af->setMode(mode);
212}
213
Glenn Kasten4944acb2013-08-19 08:39:20 -0700214status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
215{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
217 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700218 return af->setParameters(ioHandle, keyValuePairs);
219}
220
Glenn Kasten4944acb2013-08-19 08:39:20 -0700221String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
222{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700223 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
224 String8 result = String8("");
225 if (af == 0) return result;
226
227 result = af->getParameters(ioHandle, keys);
228 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229}
230
Glenn Kastenc23885e2013-12-19 16:35:18 -0800231status_t AudioSystem::setParameters(const String8& keyValuePairs)
232{
Glenn Kasten142f5192014-03-25 17:44:59 -0700233 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800234}
235
236String8 AudioSystem::getParameters(const String8& keys)
237{
Glenn Kasten142f5192014-03-25 17:44:59 -0700238 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800239}
240
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241// convert volume steps to natural log scale
242
243// change this value to change volume scaling
244static const float dBPerStep = 0.5f;
245// shouldn't need to touch these
246static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
247static const float dBConvertInverse = 1.0f / dBConvert;
248
249float AudioSystem::linearToLog(int volume)
250{
251 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000252 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 // return v;
254 return volume ? exp(float(100 - volume) * dBConvert) : 0;
255}
256
257int AudioSystem::logToLinear(float volume)
258{
259 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000260 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 // return v;
262 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
263}
264
Eric Laurent21da6472017-11-09 16:29:26 -0800265/* static */ size_t AudioSystem::calculateMinFrameCount(
266 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
267 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
268{
269 // Ensure that buffer depth covers at least audio hardware latency
270 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
271 if (minBufCount < 2) {
272 minBufCount = 2;
273 }
274#if 0
275 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
276 // but keeping the code here to make it easier to add later.
277 if (minBufCount < notificationsPerBufferReq) {
278 minBufCount = notificationsPerBufferReq;
279 }
280#endif
281 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
282 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
283 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
284 /*, notificationsPerBufferReq*/);
285 return minBufCount * sourceFramesNeededWithTimestretch(
286 sampleRate, afFrameCount, afSampleRate, speed);
287}
288
289
Glenn Kasten3b16c762012-11-14 08:44:39 -0800290status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293
Dima Zavinfce7a472011-04-19 22:30:36 -0700294 if (streamType == AUDIO_STREAM_DEFAULT) {
295 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296 }
297
Glenn Kastenfff6d712012-01-12 16:38:12 -0800298 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 if (output == 0) {
300 return PERMISSION_DENIED;
301 }
302
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700303 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700304}
305
Glenn Kasten2c073da2016-02-26 09:14:08 -0800306status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800307 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700308{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800309 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
310 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800311 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
312 if (desc == 0) {
313 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800315 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800317 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800318 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800319 return BAD_VALUE;
320 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
Glenn Kasten2c073da2016-02-26 09:14:08 -0800322 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 return NO_ERROR;
325}
326
Glenn Kastene33054e2012-11-14 12:54:39 -0800327status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 if (streamType == AUDIO_STREAM_DEFAULT) {
332 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700334
Glenn Kastenfff6d712012-01-12 16:38:12 -0800335 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700336 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 return PERMISSION_DENIED;
338 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700340 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700341}
342
Glenn Kasten2c073da2016-02-26 09:14:08 -0800343status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800344 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700345{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800346 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
347 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800348 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
349 if (desc == 0) {
350 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800352 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800354 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800355 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800356 return BAD_VALUE;
357 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358
Glenn Kasten2c073da2016-02-26 09:14:08 -0800359 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 return NO_ERROR;
362}
363
Glenn Kastenfff6d712012-01-12 16:38:12 -0800364status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700366 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 if (streamType == AUDIO_STREAM_DEFAULT) {
369 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700370 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700371
Glenn Kastenfff6d712012-01-12 16:38:12 -0800372 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700373 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700374 return PERMISSION_DENIED;
375 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376
Glenn Kasten241618f2014-03-25 17:48:57 -0700377 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700378}
379
380status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700381 uint32_t* latency)
382{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800383 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
384 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700385 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700386 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387 *latency = af->latency(output);
388 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700389 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700390 }
391
Glenn Kasten241618f2014-03-25 17:48:57 -0700392 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700393
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 return NO_ERROR;
395}
396
Glenn Kastendd8104c2012-07-02 12:42:44 -0700397status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
398 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399{
Eric Laurent296fb132015-05-01 11:38:42 -0700400 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
401 if (afc == 0) {
402 return NO_INIT;
403 }
404 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405}
406
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700407status_t AudioSystem::setVoiceVolume(float value)
408{
409 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
410 if (af == 0) return PERMISSION_DENIED;
411 return af->setVoiceVolume(value);
412}
413
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000414status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700415 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800416{
417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
418 if (af == 0) return PERMISSION_DENIED;
419
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000420 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800421}
422
Glenn Kasten4944acb2013-08-19 08:39:20 -0700423uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
424{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800426 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800427 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700428 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800429
430 result = af->getInputFramesLost(ioHandle);
431 return result;
432}
433
Glenn Kasteneeecb982016-02-26 10:44:04 -0800434audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700435{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700436 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700437 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700438 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800439 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700440}
441
Andy Hung8b0bfd92019-12-23 13:11:11 -0800442void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700443{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700444 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
445 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800446 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700447 }
448}
449
Glenn Kastend848eb42016-03-08 13:42:11 -0800450void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700451{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700452 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
453 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800454 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700455 }
456}
457
Eric Laurent93c3d412014-08-01 14:48:35 -0700458audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
459{
460 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
461 if (af == 0) return AUDIO_HW_SYNC_INVALID;
462 return af->getAudioHwSyncForSession(sessionId);
463}
464
Eric Laurent72e3f392015-05-20 14:43:50 -0700465status_t AudioSystem::systemReady()
466{
467 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
468 if (af == 0) return NO_INIT;
469 return af->systemReady();
470}
471
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700472status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
473 size_t* frameCount)
474{
475 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
476 if (af == 0) return PERMISSION_DENIED;
477 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
478 if (desc == 0) {
479 *frameCount = af->frameCountHAL(ioHandle);
480 } else {
481 *frameCount = desc->mFrameCountHAL;
482 }
483 if (*frameCount == 0) {
484 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
485 return BAD_VALUE;
486 }
487
488 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
489
490 return NO_ERROR;
491}
492
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493// ---------------------------------------------------------------------------
494
Eric Laurent73e26b62015-04-27 16:55:58 -0700495
496void AudioSystem::AudioFlingerClient::clearIoCache()
497{
498 Mutex::Autolock _l(mLock);
499 mIoDescriptors.clear();
500 mInBuffSize = 0;
501 mInSamplingRate = 0;
502 mInFormat = AUDIO_FORMAT_DEFAULT;
503 mInChannelMask = AUDIO_CHANNEL_NONE;
504}
505
Glenn Kasten4944acb2013-08-19 08:39:20 -0700506void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
507{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800508 {
509 Mutex::Autolock _l(AudioSystem::gLock);
510 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800511 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512
Eric Laurent73e26b62015-04-27 16:55:58 -0700513 // clear output handles and stream to output map caches
514 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800516 reportError(DEAD_OBJECT);
517
Steve Block5ff1dd52012-01-05 23:22:43 +0000518 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519}
520
Eric Laurent73e26b62015-04-27 16:55:58 -0700521void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
522 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100523 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
Eric Laurent73e26b62015-04-27 16:55:58 -0700525 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700526
Eric Laurent296fb132015-05-01 11:38:42 -0700527 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700528 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700529 {
530 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700531 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700532
533 switch (event) {
534 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700535 case AUDIO_OUTPUT_REGISTERED:
536 case AUDIO_INPUT_OPENED:
537 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700538 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700539 if (oldDesc == 0) {
540 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
541 } else {
542 deviceId = oldDesc->getDeviceId();
543 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700544 }
Eric Laurent296fb132015-05-01 11:38:42 -0700545
546 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
547 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700548 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700549 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
550 if (it != mAudioDeviceCallbacks.end()) {
551 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100552 }
553 }
Eric Laurent296fb132015-05-01 11:38:42 -0700554 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700555 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
556 "frameCount %zu deviceId %d",
557 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
558 "output" : "input",
559 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
560 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700561 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
562 ioDesc->mFrameCount, ioDesc->getDeviceId());
563 } break;
564 case AUDIO_OUTPUT_CLOSED:
565 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700566 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700567 ALOGW("ioConfigChanged() closing unknown %s %d",
568 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
569 break;
570 }
571 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700572 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700573
Eric Laurent296fb132015-05-01 11:38:42 -0700574 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700575 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700576 } break;
577
578 case AUDIO_OUTPUT_CONFIG_CHANGED:
579 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700580 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700581 if (oldDesc == 0) {
582 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
583 break;
584 }
585
586 deviceId = oldDesc->getDeviceId();
587 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
588
589 if (deviceId != ioDesc->getDeviceId()) {
590 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700591 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
592 if (it != mAudioDeviceCallbacks.end()) {
593 callbacks = it->second;
594 }
Eric Laurent296fb132015-05-01 11:38:42 -0700595 }
596 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700597 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700598 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
599 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800600 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
601 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700602
Eric Laurentc2f1f072009-07-17 12:17:14 -0700603 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700604 case AUDIO_CLIENT_STARTED: {
605 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
606 if (oldDesc == 0) {
607 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
608 break;
609 }
610 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
611 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
612 oldDesc->mPatch = ioDesc->mPatch;
613 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
614 if (it != mAudioDeviceCallbacks.end()) {
615 auto cbks = it->second;
616 auto it2 = cbks.find(ioDesc->mPortId);
617 if (it2 != cbks.end()) {
618 callbacks.emplace(ioDesc->mPortId, it2->second);
619 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100620 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100621 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700622 } break;
623 }
624
625 for (auto wpCbk : callbacks) {
626 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
627 if (spCbk != nullptr) {
628 callbacksToCall.push_back(spCbk);
629 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700630 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800631 }
632
633 // Callbacks must be called without mLock held. May lead to dead lock if calling for
634 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700635 for (auto cb : callbacksToCall) {
636 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
637 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700638 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639}
640
Eric Laurent73e26b62015-04-27 16:55:58 -0700641status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
642 uint32_t sampleRate, audio_format_t format,
643 audio_channel_mask_t channelMask, size_t* buffSize)
644{
645 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
646 if (af == 0) {
647 return PERMISSION_DENIED;
648 }
649 Mutex::Autolock _l(mLock);
650 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
651 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
652 || (channelMask != mInChannelMask)) {
653 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
654 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800655 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700656 sampleRate, format, channelMask);
657 return BAD_VALUE;
658 }
659 // A benign race is possible here: we could overwrite a fresher cache entry
660 // save the request params
661 mInSamplingRate = sampleRate;
662 mInFormat = format;
663 mInChannelMask = channelMask;
664
665 mInBuffSize = inBuffSize;
666 }
667
668 *buffSize = mInBuffSize;
669
670 return NO_ERROR;
671}
672
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700673sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700674{
675 sp<AudioIoDescriptor> desc;
676 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
677 if (index >= 0) {
678 desc = mIoDescriptors.valueAt(index);
679 }
680 return desc;
681}
682
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700683sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
684{
685 Mutex::Autolock _l(mLock);
686 return getIoDescriptor_l(ioHandle);
687}
688
Eric Laurent296fb132015-05-01 11:38:42 -0700689status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700690 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
691 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700692{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700693 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800694 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700695 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
696 auto result = callbacks.try_emplace(portId, callback);
697 if (!result.second) {
698 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700699 }
Eric Laurent296fb132015-05-01 11:38:42 -0700700 return NO_ERROR;
701}
702
703status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700704 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
705 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700706{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700707 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800708 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700709 auto it = mAudioDeviceCallbacks.find(audioIo);
710 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700711 return INVALID_OPERATION;
712 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700713 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700714 return INVALID_OPERATION;
715 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700716 if (it->second.size() == 0) {
717 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700718 }
719 return NO_ERROR;
720}
721
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800722/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700723{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800724 Mutex::Autolock _l(gLockErrorCallbacks);
725 gAudioErrorCallbacks.insert(cb);
726 return reinterpret_cast<uintptr_t>(cb);
727}
728
729/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
730 Mutex::Autolock _l(gLockErrorCallbacks);
731 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
732}
733
734/* static */ void AudioSystem::reportError(status_t err) {
735 Mutex::Autolock _l(gLockErrorCallbacks);
736 for (auto callback : gAudioErrorCallbacks) {
737 callback(err);
738 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739}
740
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700741/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
742{
743 Mutex::Autolock _l(gLock);
744 gDynPolicyCallback = cb;
745}
746
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800747/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
748{
749 Mutex::Autolock _l(gLock);
750 gRecordConfigCallback = cb;
751}
752
Eric Laurentc2f1f072009-07-17 12:17:14 -0700753// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800754// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
756sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
757
758
Glenn Kasten18a6d902012-09-24 11:27:56 -0700759// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800760const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700761{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800762 sp<IAudioPolicyService> ap;
763 sp<AudioPolicyServiceClient> apc;
764 {
765 Mutex::Autolock _l(gLockAPS);
766 if (gAudioPolicyService == 0) {
767 sp<IServiceManager> sm = defaultServiceManager();
768 sp<IBinder> binder;
769 do {
770 binder = sm->getService(String16("media.audio_policy"));
771 if (binder != 0)
772 break;
773 ALOGW("AudioPolicyService not published, waiting...");
774 usleep(500000); // 0.5 s
775 } while (true);
776 if (gAudioPolicyServiceClient == NULL) {
777 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
778 }
779 binder->linkToDeath(gAudioPolicyServiceClient);
780 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
781 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
782 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700783 // Make sure callbacks can be received by gAudioPolicyServiceClient
784 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700785 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800786 ap = gAudioPolicyService;
787 }
788 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200789 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800790 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200791 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100792 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200793 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700794 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800795
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800796 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700797}
798
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700799// ---------------------------------------------------------------------------
800
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000801void AudioSystem::onNewAudioModulesAvailable()
802{
803 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
804 if (aps == 0) return;
805 aps->onNewAudioModulesAvailable();
806}
807
Dima Zavinfce7a472011-04-19 22:30:36 -0700808status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
809 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800810 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800811 const char *device_name,
812 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700813{
814 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700815 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800816 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700817
Eric Laurentc2f1f072009-07-17 12:17:14 -0700818 if (aps == 0) return PERMISSION_DENIED;
819
Eric Laurent71b63e32011-09-02 14:20:56 -0700820 if (device_address != NULL) {
821 address = device_address;
822 }
Paul McLeane743a472015-01-28 11:07:31 -0800823 if (device_name != NULL) {
824 name = device_name;
825 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800826 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700827}
828
Dima Zavinfce7a472011-04-19 22:30:36 -0700829audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830 const char *device_address)
831{
832 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700833 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700834
835 return aps->getDeviceConnectionState(device, device_address);
836}
837
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800838status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
839 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800840 const char *device_name,
841 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800842{
843 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
844 const char *address = "";
845 const char *name = "";
846
847 if (aps == 0) return PERMISSION_DENIED;
848
849 if (device_address != NULL) {
850 address = device_address;
851 }
852 if (device_name != NULL) {
853 name = device_name;
854 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800855 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800856}
857
Eric Laurent00dba062020-02-11 15:52:09 -0800858status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700859{
Glenn Kasten347966c2012-01-18 14:58:32 -0800860 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700861 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
862 if (aps == 0) return PERMISSION_DENIED;
863
Eric Laurent00dba062020-02-11 15:52:09 -0800864 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700865}
866
Dima Zavinfce7a472011-04-19 22:30:36 -0700867status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700868{
869 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
870 if (aps == 0) return PERMISSION_DENIED;
871 return aps->setForceUse(usage, config);
872}
873
Dima Zavinfce7a472011-04-19 22:30:36 -0700874audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700875{
876 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700877 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700878 return aps->getForceUse(usage);
879}
880
881
Eric Laurentf4e63452017-11-06 19:31:46 +0000882audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700883{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700884 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
885 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000886 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887}
888
Eric Laurent42984412019-05-09 17:57:03 -0700889status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800890 audio_io_handle_t *output,
891 audio_session_t session,
892 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200893 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700894 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800895 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800896 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700897 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800898 audio_port_handle_t *portId,
899 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700900{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800902 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200903 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700904 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800905 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700906}
907
Eric Laurentd7fe0862018-07-14 16:48:01 -0700908status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700909{
910 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
911 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700912 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700913}
914
Eric Laurentd7fe0862018-07-14 16:48:01 -0700915status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700916{
917 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
918 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700919 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920}
921
Eric Laurentd7fe0862018-07-14 16:48:01 -0700922void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700923{
924 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
925 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700926 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700927}
928
Eric Laurentcaf7f482014-11-25 17:50:47 -0800929status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
930 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700931 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800932 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700933 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700934 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800935 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800936 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600937 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700938 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800939 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940{
941 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800942 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600943 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700944 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800945 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700946}
947
Eric Laurent4eb58f12018-12-07 16:41:02 -0800948status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700949{
950 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
951 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800952 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953}
954
Eric Laurentfee19762018-01-29 18:44:13 -0800955status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956{
957 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
958 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800959 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960}
961
Eric Laurentfee19762018-01-29 18:44:13 -0800962void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700963{
964 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
965 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800966 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700967}
968
Dima Zavinfce7a472011-04-19 22:30:36 -0700969status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700970 int indexMin,
971 int indexMax)
972{
973 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
974 if (aps == 0) return PERMISSION_DENIED;
975 return aps->initStreamVolume(stream, indexMin, indexMax);
976}
977
Eric Laurent83844cc2011-11-18 16:43:31 -0800978status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
979 int index,
980 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981{
982 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
983 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800984 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985}
986
Eric Laurent83844cc2011-11-18 16:43:31 -0800987status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
988 int *index,
989 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700990{
991 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
992 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800993 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994}
995
François Gaffiecfe17322018-11-07 13:41:29 +0100996status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
997 int index,
998 audio_devices_t device)
999{
1000 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1001 if (aps == 0) return PERMISSION_DENIED;
1002 return aps->setVolumeIndexForAttributes(attr, index, device);
1003}
1004
1005status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1006 int &index,
1007 audio_devices_t device)
1008{
1009 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1010 if (aps == 0) return PERMISSION_DENIED;
1011 return aps->getVolumeIndexForAttributes(attr, index, device);
1012}
1013
1014status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1015{
1016 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1017 if (aps == 0) return PERMISSION_DENIED;
1018 return aps->getMaxVolumeIndexForAttributes(attr, index);
1019}
1020
1021status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1022{
1023 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1024 if (aps == 0) return PERMISSION_DENIED;
1025 return aps->getMinVolumeIndexForAttributes(attr, index);
1026}
1027
Dima Zavinfce7a472011-04-19 22:30:36 -07001028uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001029{
1030 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001031 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001032 return aps->getStrategyForStream(stream);
1033}
1034
Eric Laurent63742522012-03-08 13:42:42 -08001035audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001036{
1037 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001038 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001039 return aps->getDevicesForStream(stream);
1040}
1041
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001042status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1043 AudioDeviceTypeAddrVector *devices) {
1044 if (devices == nullptr) {
1045 return BAD_VALUE;
1046 }
1047 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1048 if (aps == 0) return PERMISSION_DENIED;
1049 return aps->getDevicesForAttributes(aa, devices);
1050}
1051
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001052audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001053{
1054 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001055 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001056 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001057 return aps->getOutputForEffect(desc);
1058}
1059
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001060status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001061 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001062 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001063 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001064 int id)
1065{
1066 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1067 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001068 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001069}
1070
1071status_t AudioSystem::unregisterEffect(int id)
1072{
1073 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1074 if (aps == 0) return PERMISSION_DENIED;
1075 return aps->unregisterEffect(id);
1076}
1077
Eric Laurentdb7c0792011-08-10 10:37:50 -07001078status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1079{
1080 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1081 if (aps == 0) return PERMISSION_DENIED;
1082 return aps->setEffectEnabled(id, enabled);
1083}
1084
Eric Laurent6c796322019-04-09 14:13:17 -07001085status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1086{
1087 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1088 if (aps == 0) return PERMISSION_DENIED;
1089 return aps->moveEffectsToIo(ids, io);
1090}
1091
Glenn Kastenfff6d712012-01-12 16:38:12 -08001092status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001093{
Eric Laurenteda6c362011-02-02 09:33:30 -08001094 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1095 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001096 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001097 *state = aps->isStreamActive(stream, inPastMs);
1098 return NO_ERROR;
1099}
1100
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001101status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1102 uint32_t inPastMs)
1103{
1104 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1105 if (aps == 0) return PERMISSION_DENIED;
1106 if (state == NULL) return BAD_VALUE;
1107 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1108 return NO_ERROR;
1109}
1110
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001111status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1112{
1113 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1114 if (aps == 0) return PERMISSION_DENIED;
1115 if (state == NULL) return BAD_VALUE;
1116 *state = aps->isSourceActive(stream);
1117 return NO_ERROR;
1118}
1119
Glenn Kasten3b16c762012-11-14 08:44:39 -08001120uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001121{
1122 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1123 if (af == 0) return 0;
1124 return af->getPrimaryOutputSamplingRate();
1125}
1126
Glenn Kastene33054e2012-11-14 12:54:39 -08001127size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001128{
1129 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1130 if (af == 0) return 0;
1131 return af->getPrimaryOutputFrameCount();
1132}
Eric Laurenteda6c362011-02-02 09:33:30 -08001133
Andy Hung6f248bb2018-01-23 14:04:37 -08001134status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001135{
1136 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1137 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001138 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001139}
1140
Eric Laurent9f6530f2011-08-30 10:18:54 -07001141void AudioSystem::clearAudioConfigCache()
1142{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001143 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001144 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001145 {
1146 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001147 if (gAudioFlingerClient != 0) {
1148 gAudioFlingerClient->clearIoCache();
1149 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001150 gAudioFlinger.clear();
1151 }
1152 {
1153 Mutex::Autolock _l(gLockAPS);
1154 gAudioPolicyService.clear();
1155 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001156}
1157
Hayden Gomes524159d2019-12-23 14:41:47 -08001158status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1159 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1160 if (aps == nullptr) return PERMISSION_DENIED;
1161 return aps->setSupportedSystemUsages(systemUsages);
1162}
1163
Kevin Rocardb99cc752019-03-21 20:52:24 -07001164status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1165 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1166 if (aps == nullptr) return PERMISSION_DENIED;
1167 return aps->setAllowedCapturePolicy(uid, flags);
1168}
1169
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001170bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1171{
1172 ALOGV("isOffloadSupported()");
1173 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1174 if (aps == 0) return false;
1175 return aps->isOffloadSupported(info);
1176}
1177
Eric Laurent203b1a12014-04-01 10:34:16 -07001178status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1179 audio_port_type_t type,
1180 unsigned int *num_ports,
1181 struct audio_port *ports,
1182 unsigned int *generation)
1183{
1184 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1185 if (aps == 0) return PERMISSION_DENIED;
1186 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1187}
1188
1189status_t AudioSystem::getAudioPort(struct audio_port *port)
1190{
1191 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1192 if (aps == 0) return PERMISSION_DENIED;
1193 return aps->getAudioPort(port);
1194}
1195
1196status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1197 audio_patch_handle_t *handle)
1198{
1199 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1200 if (aps == 0) return PERMISSION_DENIED;
1201 return aps->createAudioPatch(patch, handle);
1202}
1203
1204status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1205{
1206 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1207 if (aps == 0) return PERMISSION_DENIED;
1208 return aps->releaseAudioPatch(handle);
1209}
1210
1211status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1212 struct audio_patch *patches,
1213 unsigned int *generation)
1214{
1215 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1216 if (aps == 0) return PERMISSION_DENIED;
1217 return aps->listAudioPatches(num_patches, patches, generation);
1218}
1219
1220status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1221{
1222 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1223 if (aps == 0) return PERMISSION_DENIED;
1224 return aps->setAudioPortConfig(config);
1225}
1226
Eric Laurent296fb132015-05-01 11:38:42 -07001227status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001228{
Eric Laurentb28753e2015-04-01 13:06:28 -07001229 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1230 if (aps == 0) return PERMISSION_DENIED;
1231
1232 Mutex::Autolock _l(gLockAPS);
1233 if (gAudioPolicyServiceClient == 0) {
1234 return NO_INIT;
1235 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001236 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1237 if (ret == 1) {
1238 aps->setAudioPortCallbacksEnabled(true);
1239 }
1240 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001241}
1242
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001243/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001244status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001245{
1246 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1247 if (aps == 0) return PERMISSION_DENIED;
1248
1249 Mutex::Autolock _l(gLockAPS);
1250 if (gAudioPolicyServiceClient == 0) {
1251 return NO_INIT;
1252 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001253 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1254 if (ret == 0) {
1255 aps->setAudioPortCallbacksEnabled(false);
1256 }
1257 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001258}
1259
François Gaffiecfe17322018-11-07 13:41:29 +01001260status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1261{
1262 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1263 if (aps == 0) return PERMISSION_DENIED;
1264
1265 Mutex::Autolock _l(gLockAPS);
1266 if (gAudioPolicyServiceClient == 0) {
1267 return NO_INIT;
1268 }
1269 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1270 if (ret == 1) {
1271 aps->setAudioVolumeGroupCallbacksEnabled(true);
1272 }
1273 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1274}
1275
1276status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1277{
1278 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1279 if (aps == 0) return PERMISSION_DENIED;
1280
1281 Mutex::Autolock _l(gLockAPS);
1282 if (gAudioPolicyServiceClient == 0) {
1283 return NO_INIT;
1284 }
1285 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1286 if (ret == 0) {
1287 aps->setAudioVolumeGroupCallbacksEnabled(false);
1288 }
1289 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1290}
1291
Eric Laurent296fb132015-05-01 11:38:42 -07001292status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001293 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1294 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001295{
1296 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1297 if (afc == 0) {
1298 return NO_INIT;
1299 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001300 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001301 if (status == NO_ERROR) {
1302 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1303 if (af != 0) {
1304 af->registerClient(afc);
1305 }
1306 }
1307 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001308}
1309
1310status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001311 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1312 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001313{
1314 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1315 if (afc == 0) {
1316 return NO_INIT;
1317 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001318 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001319}
1320
1321audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1322{
1323 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1324 if (af == 0) return PERMISSION_DENIED;
1325 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1326 if (desc == 0) {
1327 return AUDIO_PORT_HANDLE_NONE;
1328 }
1329 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001330}
1331
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001332status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1333 audio_io_handle_t *ioHandle,
1334 audio_devices_t *device)
1335{
1336 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1337 if (aps == 0) return PERMISSION_DENIED;
1338 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1339}
1340
1341status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1342{
1343 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1344 if (aps == 0) return PERMISSION_DENIED;
1345 return aps->releaseSoundTriggerSession(session);
1346}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001347
1348audio_mode_t AudioSystem::getPhoneState()
1349{
1350 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1351 if (aps == 0) return AUDIO_MODE_INVALID;
1352 return aps->getPhoneState();
1353}
1354
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001355status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001356{
1357 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1358 if (aps == 0) return PERMISSION_DENIED;
1359 return aps->registerPolicyMixes(mixes, registration);
1360}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001361
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001362status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->setUidDeviceAffinities(uid, devices);
1367}
1368
1369status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1370 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1371 if (aps == 0) return PERMISSION_DENIED;
1372 return aps->removeUidDeviceAffinities(uid);
1373}
1374
Oscar Azucena90e77632019-11-27 17:12:28 -08001375status_t AudioSystem::setUserIdDeviceAffinities(int userId,
1376 const Vector<AudioDeviceTypeAddr>& devices)
1377{
1378 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1379 if (aps == 0) return PERMISSION_DENIED;
1380 return aps->setUserIdDeviceAffinities(userId, devices);
1381}
1382
1383status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1384{
1385 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1386 if (aps == 0) return PERMISSION_DENIED;
1387 return aps->removeUserIdDeviceAffinities(userId);
1388}
1389
Eric Laurent554a2772015-04-10 11:29:24 -07001390status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1391 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001392 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001393{
1394 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1395 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001396 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001397}
1398
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001399status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001400{
1401 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1402 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001403 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001404}
1405
Andy Hung2ddee192015-12-18 17:34:44 -08001406status_t AudioSystem::setMasterMono(bool mono)
1407{
1408 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1409 if (aps == 0) return PERMISSION_DENIED;
1410 return aps->setMasterMono(mono);
1411}
1412
1413status_t AudioSystem::getMasterMono(bool *mono)
1414{
1415 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1416 if (aps == 0) return PERMISSION_DENIED;
1417 return aps->getMasterMono(mono);
1418}
1419
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001420status_t AudioSystem::setMasterBalance(float balance)
1421{
1422 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1423 if (af == 0) return PERMISSION_DENIED;
1424 return af->setMasterBalance(balance);
1425}
1426
1427status_t AudioSystem::getMasterBalance(float *balance)
1428{
1429 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1430 if (af == 0) return PERMISSION_DENIED;
1431 return af->getMasterBalance(balance);
1432}
1433
Eric Laurentac9cef52017-06-09 15:46:26 -07001434float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1435{
1436 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1437 if (aps == 0) return NAN;
1438 return aps->getStreamVolumeDB(stream, index, device);
1439}
1440
jiabin46a76fa2018-01-05 10:18:21 -08001441status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1442{
1443 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1444 if (af == 0) return PERMISSION_DENIED;
1445 return af->getMicrophones(microphones);
1446}
1447
Eric Laurent42896a02019-09-27 15:40:33 -07001448status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1449 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1450 if (af == nullptr) return PERMISSION_DENIED;
1451 return af->setAudioHalPids(pids);
1452}
1453
jiabin81772902018-04-02 17:52:27 -07001454status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1455 audio_format_t *surroundFormats,
1456 bool *surroundFormatsEnabled,
1457 bool reported)
1458{
1459 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1460 if (aps == 0) return PERMISSION_DENIED;
1461 return aps->getSurroundFormats(
1462 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1463}
1464
1465status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1466{
1467 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1468 if (aps == 0) return PERMISSION_DENIED;
1469 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1470}
1471
Eric Laurentb78763e2018-10-17 10:08:02 -07001472status_t AudioSystem::setAssistantUid(uid_t uid)
1473{
1474 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1475 if (aps == 0) return PERMISSION_DENIED;
1476
1477 return aps->setAssistantUid(uid);
1478}
1479
1480status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1481{
1482 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1483 if (aps == 0) return PERMISSION_DENIED;
1484
1485 return aps->setA11yServicesUids(uids);
1486}
1487
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001488status_t AudioSystem::setCurrentImeUid(uid_t uid)
1489{
1490 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1491 if (aps == 0) return PERMISSION_DENIED;
1492
1493 return aps->setCurrentImeUid(uid);
1494}
1495
jiabin6012f912018-11-02 17:06:30 -07001496bool AudioSystem::isHapticPlaybackSupported()
1497{
1498 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1499 if (aps == 0) return false;
1500 return aps->isHapticPlaybackSupported();
1501}
1502
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001503status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001504 std::vector<audio_format_t> *formats) {
1505 const sp <IAudioPolicyService>
1506 & aps = AudioSystem::get_audio_policy_service();
1507 if (aps == 0) return PERMISSION_DENIED;
1508 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1509}
1510
1511status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001512{
1513 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1514 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001515 return aps->listAudioProductStrategies(strategies);
1516}
1517
1518audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1519{
1520 AudioProductStrategyVector strategies;
1521 listAudioProductStrategies(strategies);
1522 for (const auto &strategy : strategies) {
1523 auto attrVect = strategy.getAudioAttributes();
1524 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1525 return attributes.getStreamType() == stream; });
1526 if (iter != end(attrVect)) {
1527 return iter->getAttributes();
1528 }
1529 }
1530 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1531 return AUDIO_ATTRIBUTES_INITIALIZER;
1532}
1533
1534audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1535{
François Gaffie4b2018b2018-11-07 11:18:59 +01001536 product_strategy_t psId;
1537 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1538 if (ret != NO_ERROR) {
1539 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1540 return AUDIO_STREAM_MUSIC;
1541 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001542 AudioProductStrategyVector strategies;
1543 listAudioProductStrategies(strategies);
1544 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001545 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001546 auto attrVect = strategy.getAudioAttributes();
1547 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1548 return AudioProductStrategy::attributesMatches(
1549 refAttr.getAttributes(), attr); });
1550 if (iter != end(attrVect)) {
1551 return iter->getStreamType();
1552 }
1553 }
1554 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001555 switch (attr.usage) {
1556 case AUDIO_USAGE_VIRTUAL_SOURCE:
1557 // virtual source is not expected to have an associated product strategy
1558 break;
1559 default:
1560 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1561 break;
1562 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001563 return AUDIO_STREAM_MUSIC;
1564}
1565
François Gaffie4b2018b2018-11-07 11:18:59 +01001566status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1567 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001568{
1569 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001570 if (aps == 0) return PERMISSION_DENIED;
1571 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1572}
1573
1574status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1575{
1576 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1577 if (aps == 0) return PERMISSION_DENIED;
1578 return aps->listAudioVolumeGroups(groups);
1579}
1580
1581status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1582 volume_group_t &volumeGroup)
1583{
1584 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1585 if (aps == 0) return PERMISSION_DENIED;
1586 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001587}
Eric Laurentb78763e2018-10-17 10:08:02 -07001588
Eric Laurent6ede98f2019-06-11 14:50:30 -07001589status_t AudioSystem::setRttEnabled(bool enabled)
1590{
1591 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1592 if (aps == 0) return PERMISSION_DENIED;
1593 return aps->setRttEnabled(enabled);
1594}
1595
Eric Laurent8340e672019-11-06 11:01:08 -08001596bool AudioSystem::isCallScreenModeSupported()
1597{
1598 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1599 if (aps == 0) return false;
1600 return aps->isCallScreenModeSupported();
1601}
1602
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001603status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1604 const AudioDeviceTypeAddr &device)
1605{
1606 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1607 if (aps == 0) {
1608 return PERMISSION_DENIED;
1609 }
1610 return aps->setPreferredDeviceForStrategy(strategy, device);
1611}
1612
1613status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1614{
1615 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1616 if (aps == 0) {
1617 return PERMISSION_DENIED;
1618 }
1619 return aps->removePreferredDeviceForStrategy(strategy);
1620}
1621
1622status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1623 AudioDeviceTypeAddr &device)
1624{
1625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1626 if (aps == 0) {
1627 return PERMISSION_DENIED;
1628 }
1629 return aps->getPreferredDeviceForStrategy(strategy, device);
1630}
1631
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001632class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1633 public IBinder::DeathRecipient {
1634public:
1635 binder::Status setCaptureState(bool active) override {
1636 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1637 gSoundTriggerCaptureStateListener->onStateChanged(active);
1638 return binder::Status::ok();
1639 }
1640
1641 void binderDied(const wp<IBinder>&) override {
1642 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1643 gSoundTriggerCaptureStateListener->onServiceDied();
1644 gSoundTriggerCaptureStateListener = nullptr;
1645 }
1646};
1647
1648status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1649 const sp<CaptureStateListener>& listener) {
1650 const sp<IAudioPolicyService>& aps =
1651 AudioSystem::get_audio_policy_service();
1652 if (aps == 0) {
1653 return PERMISSION_DENIED;
1654 }
1655
1656 sp<CaptureStateListenerImpl> wrapper = new CaptureStateListenerImpl();
1657
1658 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1659
1660 bool active;
1661 status_t status =
1662 aps->registerSoundTriggerCaptureStateListener(wrapper, &active);
1663 if (status != NO_ERROR) {
1664 listener->onServiceDied();
1665 return NO_ERROR;
1666 }
1667 gSoundTriggerCaptureStateListener = listener;
1668 listener->onStateChanged(active);
1669 sp<IBinder> binder = IInterface::asBinder(aps);
1670 binder->linkToDeath(wrapper);
1671 return NO_ERROR;
1672}
1673
Eric Laurentc2f1f072009-07-17 12:17:14 -07001674// ---------------------------------------------------------------------------
1675
Eric Laurente8726fe2015-06-26 09:39:24 -07001676int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001677 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001678{
1679 Mutex::Autolock _l(mLock);
1680 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001681 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001682 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001683 }
1684 }
Eric Laurent296fb132015-05-01 11:38:42 -07001685 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001686 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001687}
1688
Eric Laurente8726fe2015-06-26 09:39:24 -07001689int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001690 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001691{
1692 Mutex::Autolock _l(mLock);
1693 size_t i;
1694 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001695 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001696 break;
1697 }
1698 }
1699 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001700 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001701 }
1702 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001703 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001704}
1705
Eric Laurent296fb132015-05-01 11:38:42 -07001706
Eric Laurentb28753e2015-04-01 13:06:28 -07001707void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1708{
1709 Mutex::Autolock _l(mLock);
1710 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1711 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1712 }
1713}
1714
1715void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1716{
1717 Mutex::Autolock _l(mLock);
1718 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1719 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1720 }
1721}
1722
François Gaffiecfe17322018-11-07 13:41:29 +01001723// ----------------------------------------------------------------------------
1724int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1725 const sp<AudioVolumeGroupCallback>& callback)
1726{
1727 Mutex::Autolock _l(mLock);
1728 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1729 if (mAudioVolumeGroupCallback[i] == callback) {
1730 return -1;
1731 }
1732 }
1733 mAudioVolumeGroupCallback.add(callback);
1734 return mAudioVolumeGroupCallback.size();
1735}
1736
1737int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1738 const sp<AudioVolumeGroupCallback>& callback)
1739{
1740 Mutex::Autolock _l(mLock);
1741 size_t i;
1742 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1743 if (mAudioVolumeGroupCallback[i] == callback) {
1744 break;
1745 }
1746 }
1747 if (i == mAudioVolumeGroupCallback.size()) {
1748 return -1;
1749 }
1750 mAudioVolumeGroupCallback.removeAt(i);
1751 return mAudioVolumeGroupCallback.size();
1752}
1753
1754void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1755 int flags)
1756{
1757 Mutex::Autolock _l(mLock);
1758 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1759 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1760 }
1761}
1762// ----------------------------------------------------------------------------
1763
Jean-Michel Trivide801052015-04-14 19:10:14 -07001764void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1765 String8 regId, int32_t state)
1766{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001767 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1768 dynamic_policy_callback cb = NULL;
1769 {
1770 Mutex::Autolock _l(AudioSystem::gLock);
1771 cb = gDynPolicyCallback;
1772 }
1773
1774 if (cb != NULL) {
1775 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1776 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001777}
1778
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001779void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001780 int event,
1781 const record_client_info_t *clientInfo,
1782 const audio_config_base_t *clientConfig,
1783 std::vector<effect_descriptor_t> clientEffects,
1784 const audio_config_base_t *deviceConfig,
1785 std::vector<effect_descriptor_t> effects,
1786 audio_patch_handle_t patchHandle,
1787 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001788 record_config_callback cb = NULL;
1789 {
1790 Mutex::Autolock _l(AudioSystem::gLock);
1791 cb = gRecordConfigCallback;
1792 }
1793
1794 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001795 cb(event, clientInfo, clientConfig, clientEffects,
1796 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001797 }
1798}
1799
Glenn Kasten4944acb2013-08-19 08:39:20 -07001800void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1801{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001802 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001803 Mutex::Autolock _l(mLock);
1804 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1805 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001806 }
François Gaffiecfe17322018-11-07 13:41:29 +01001807 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1808 mAudioVolumeGroupCallback[i]->onServiceDied();
1809 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001810 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001811 {
1812 Mutex::Autolock _l(gLockAPS);
1813 AudioSystem::gAudioPolicyService.clear();
1814 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001815
Steve Block5ff1dd52012-01-05 23:22:43 +00001816 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001817}
1818
Glenn Kasten40bc9062015-03-20 09:09:33 -07001819} // namespace android