blob: 9f880743df5004928c33da882ef4be95788e1c48 [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;
Mikhail Naganov69330d42020-04-08 19:29:50 +000058 bool reportNoError = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -080059 {
60 Mutex::Autolock _l(gLock);
61 if (gAudioFlinger == 0) {
62 sp<IServiceManager> sm = defaultServiceManager();
63 sp<IBinder> binder;
64 do {
65 binder = sm->getService(String16("media.audio_flinger"));
66 if (binder != 0)
67 break;
68 ALOGW("AudioFlinger not published, waiting...");
69 usleep(500000); // 0.5 s
70 } while (true);
71 if (gAudioFlingerClient == NULL) {
72 gAudioFlingerClient = new AudioFlingerClient();
73 } else {
Mikhail Naganov69330d42020-04-08 19:29:50 +000074 reportNoError = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080076 binder->linkToDeath(gAudioFlingerClient);
77 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
78 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
79 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070080 // Make sure callbacks can be received by gAudioFlingerClient
81 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070082 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080083 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080085 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020086 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080087 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020088 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080089 }
Mikhail Naganov69330d42020-04-08 19:29:50 +000090 if (reportNoError) reportError(NO_ERROR);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080091 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092}
93
Eric Laurent296fb132015-05-01 11:38:42 -070094const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
95{
96 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
97 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
98 if (af == 0) return 0;
99 Mutex::Autolock _l(gLock);
100 return gAudioFlingerClient;
101}
102
103sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
104{
105 sp<AudioIoDescriptor> desc;
106 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
107 if (afc != 0) {
108 desc = afc->getIoDescriptor(ioHandle);
109 }
110 return desc;
111}
112
Eric Laurent46291612013-07-18 14:38:44 -0700113/* static */ status_t AudioSystem::checkAudioFlinger()
114{
115 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
116 return NO_ERROR;
117 }
118 return DEAD_OBJECT;
119}
120
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700121// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
122
Glenn Kasten4944acb2013-08-19 08:39:20 -0700123status_t AudioSystem::muteMicrophone(bool state)
124{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
126 if (af == 0) return PERMISSION_DENIED;
127 return af->setMicMute(state);
128}
129
Glenn Kasten4944acb2013-08-19 08:39:20 -0700130status_t AudioSystem::isMicrophoneMuted(bool* state)
131{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
133 if (af == 0) return PERMISSION_DENIED;
134 *state = af->getMicMute();
135 return NO_ERROR;
136}
137
138status_t AudioSystem::setMasterVolume(float value)
139{
140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
141 if (af == 0) return PERMISSION_DENIED;
142 af->setMasterVolume(value);
143 return NO_ERROR;
144}
145
146status_t AudioSystem::setMasterMute(bool mute)
147{
148 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
149 if (af == 0) return PERMISSION_DENIED;
150 af->setMasterMute(mute);
151 return NO_ERROR;
152}
153
154status_t AudioSystem::getMasterVolume(float* volume)
155{
156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
157 if (af == 0) return PERMISSION_DENIED;
158 *volume = af->masterVolume();
159 return NO_ERROR;
160}
161
162status_t AudioSystem::getMasterMute(bool* mute)
163{
164 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
165 if (af == 0) return PERMISSION_DENIED;
166 *mute = af->masterMute();
167 return NO_ERROR;
168}
169
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800170status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
171 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172{
Dima Zavinfce7a472011-04-19 22:30:36 -0700173 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700176 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 return NO_ERROR;
178}
179
Glenn Kastenfff6d712012-01-12 16:38:12 -0800180status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181{
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
184 if (af == 0) return PERMISSION_DENIED;
185 af->setStreamMute(stream, mute);
186 return NO_ERROR;
187}
188
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800189status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
190 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191{
Dima Zavinfce7a472011-04-19 22:30:36 -0700192 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
194 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700195 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 return NO_ERROR;
197}
198
Glenn Kastenfff6d712012-01-12 16:38:12 -0800199status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200{
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
203 if (af == 0) return PERMISSION_DENIED;
204 *mute = af->streamMute(stream);
205 return NO_ERROR;
206}
207
Glenn Kastenf78aee72012-01-04 11:00:47 -0800208status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800210 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
212 if (af == 0) return PERMISSION_DENIED;
213 return af->setMode(mode);
214}
215
Glenn Kasten4944acb2013-08-19 08:39:20 -0700216status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
217{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800218 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
219 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700220 return af->setParameters(ioHandle, keyValuePairs);
221}
222
Glenn Kasten4944acb2013-08-19 08:39:20 -0700223String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
224{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700225 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
226 String8 result = String8("");
227 if (af == 0) return result;
228
229 result = af->getParameters(ioHandle, keys);
230 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231}
232
Glenn Kastenc23885e2013-12-19 16:35:18 -0800233status_t AudioSystem::setParameters(const String8& keyValuePairs)
234{
Glenn Kasten142f5192014-03-25 17:44:59 -0700235 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800236}
237
238String8 AudioSystem::getParameters(const String8& keys)
239{
Glenn Kasten142f5192014-03-25 17:44:59 -0700240 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800241}
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243// convert volume steps to natural log scale
244
245// change this value to change volume scaling
246static const float dBPerStep = 0.5f;
247// shouldn't need to touch these
248static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
249static const float dBConvertInverse = 1.0f / dBConvert;
250
251float AudioSystem::linearToLog(int volume)
252{
253 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000254 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 // return v;
256 return volume ? exp(float(100 - volume) * dBConvert) : 0;
257}
258
259int AudioSystem::logToLinear(float volume)
260{
261 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000262 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263 // return v;
264 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
265}
266
Eric Laurent21da6472017-11-09 16:29:26 -0800267/* static */ size_t AudioSystem::calculateMinFrameCount(
268 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
269 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
270{
271 // Ensure that buffer depth covers at least audio hardware latency
272 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
273 if (minBufCount < 2) {
274 minBufCount = 2;
275 }
276#if 0
277 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
278 // but keeping the code here to make it easier to add later.
279 if (minBufCount < notificationsPerBufferReq) {
280 minBufCount = notificationsPerBufferReq;
281 }
282#endif
283 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
284 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
285 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
286 /*, notificationsPerBufferReq*/);
287 return minBufCount * sourceFramesNeededWithTimestretch(
288 sampleRate, afFrameCount, afSampleRate, speed);
289}
290
291
Glenn Kasten3b16c762012-11-14 08:44:39 -0800292status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295
Dima Zavinfce7a472011-04-19 22:30:36 -0700296 if (streamType == AUDIO_STREAM_DEFAULT) {
297 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700298 }
299
Glenn Kastenfff6d712012-01-12 16:38:12 -0800300 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700301 if (output == 0) {
302 return PERMISSION_DENIED;
303 }
304
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700305 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700306}
307
Glenn Kasten2c073da2016-02-26 09:14:08 -0800308status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800309 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700310{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800311 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
312 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800313 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
314 if (desc == 0) {
315 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800317 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800319 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800320 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800321 return BAD_VALUE;
322 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323
Glenn Kasten2c073da2016-02-26 09:14:08 -0800324 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 return NO_ERROR;
327}
328
Glenn Kastene33054e2012-11-14 12:54:39 -0800329status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332
Dima Zavinfce7a472011-04-19 22:30:36 -0700333 if (streamType == AUDIO_STREAM_DEFAULT) {
334 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700336
Glenn Kastenfff6d712012-01-12 16:38:12 -0800337 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700338 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 return PERMISSION_DENIED;
340 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700342 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700343}
344
Glenn Kasten2c073da2016-02-26 09:14:08 -0800345status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800346 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700347{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800348 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
349 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800350 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
351 if (desc == 0) {
352 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800354 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800356 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800357 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800358 return BAD_VALUE;
359 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360
Glenn Kasten2c073da2016-02-26 09:14:08 -0800361 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 return NO_ERROR;
364}
365
Glenn Kastenfff6d712012-01-12 16:38:12 -0800366status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700368 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369
Dima Zavinfce7a472011-04-19 22:30:36 -0700370 if (streamType == AUDIO_STREAM_DEFAULT) {
371 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700373
Glenn Kastenfff6d712012-01-12 16:38:12 -0800374 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700375 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700376 return PERMISSION_DENIED;
377 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378
Glenn Kasten241618f2014-03-25 17:48:57 -0700379 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700380}
381
382status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700383 uint32_t* latency)
384{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800385 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
386 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700387 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700388 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700389 *latency = af->latency(output);
390 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700391 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392 }
393
Glenn Kasten241618f2014-03-25 17:48:57 -0700394 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700395
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 return NO_ERROR;
397}
398
Glenn Kastendd8104c2012-07-02 12:42:44 -0700399status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
400 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401{
Eric Laurent296fb132015-05-01 11:38:42 -0700402 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
403 if (afc == 0) {
404 return NO_INIT;
405 }
406 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407}
408
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700409status_t AudioSystem::setVoiceVolume(float value)
410{
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return PERMISSION_DENIED;
413 return af->setVoiceVolume(value);
414}
415
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000416status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700417 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800418{
419 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
420 if (af == 0) return PERMISSION_DENIED;
421
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000422 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800423}
424
Glenn Kasten4944acb2013-08-19 08:39:20 -0700425uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
426{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800427 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800428 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800429 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700430 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800431
432 result = af->getInputFramesLost(ioHandle);
433 return result;
434}
435
Glenn Kasteneeecb982016-02-26 10:44:04 -0800436audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700437{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700438 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700439 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700440 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800441 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700442}
443
Andy Hung8b0bfd92019-12-23 13:11:11 -0800444void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
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) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800448 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700449 }
450}
451
Glenn Kastend848eb42016-03-08 13:42:11 -0800452void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700453{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700454 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
455 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800456 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700457 }
458}
459
Eric Laurent93c3d412014-08-01 14:48:35 -0700460audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
461{
462 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
463 if (af == 0) return AUDIO_HW_SYNC_INVALID;
464 return af->getAudioHwSyncForSession(sessionId);
465}
466
Eric Laurent72e3f392015-05-20 14:43:50 -0700467status_t AudioSystem::systemReady()
468{
469 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
470 if (af == 0) return NO_INIT;
471 return af->systemReady();
472}
473
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700474status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
475 size_t* frameCount)
476{
477 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
478 if (af == 0) return PERMISSION_DENIED;
479 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
480 if (desc == 0) {
481 *frameCount = af->frameCountHAL(ioHandle);
482 } else {
483 *frameCount = desc->mFrameCountHAL;
484 }
485 if (*frameCount == 0) {
486 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
487 return BAD_VALUE;
488 }
489
490 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
491
492 return NO_ERROR;
493}
494
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495// ---------------------------------------------------------------------------
496
Eric Laurent73e26b62015-04-27 16:55:58 -0700497
498void AudioSystem::AudioFlingerClient::clearIoCache()
499{
500 Mutex::Autolock _l(mLock);
501 mIoDescriptors.clear();
502 mInBuffSize = 0;
503 mInSamplingRate = 0;
504 mInFormat = AUDIO_FORMAT_DEFAULT;
505 mInChannelMask = AUDIO_CHANNEL_NONE;
506}
507
Glenn Kasten4944acb2013-08-19 08:39:20 -0700508void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
509{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800510 {
511 Mutex::Autolock _l(AudioSystem::gLock);
512 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800513 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514
Eric Laurent73e26b62015-04-27 16:55:58 -0700515 // clear output handles and stream to output map caches
516 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800518 reportError(DEAD_OBJECT);
519
Steve Block5ff1dd52012-01-05 23:22:43 +0000520 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521}
522
Eric Laurent73e26b62015-04-27 16:55:58 -0700523void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
524 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700526
Eric Laurent73e26b62015-04-27 16:55:58 -0700527 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700528
Eric Laurent296fb132015-05-01 11:38:42 -0700529 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700530 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700531 {
532 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700533 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700534
535 switch (event) {
536 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700537 case AUDIO_OUTPUT_REGISTERED:
538 case AUDIO_INPUT_OPENED:
539 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700540 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700541 if (oldDesc == 0) {
542 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
543 } else {
544 deviceId = oldDesc->getDeviceId();
545 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700546 }
Eric Laurent296fb132015-05-01 11:38:42 -0700547
548 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
549 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700550 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700551 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
552 if (it != mAudioDeviceCallbacks.end()) {
553 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100554 }
555 }
Eric Laurent296fb132015-05-01 11:38:42 -0700556 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700557 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
558 "frameCount %zu deviceId %d",
559 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
560 "output" : "input",
561 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
562 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700563 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
564 ioDesc->mFrameCount, ioDesc->getDeviceId());
565 } break;
566 case AUDIO_OUTPUT_CLOSED:
567 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700568 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700569 ALOGW("ioConfigChanged() closing unknown %s %d",
570 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
571 break;
572 }
573 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700574 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700575
Eric Laurent296fb132015-05-01 11:38:42 -0700576 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700577 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700578 } break;
579
580 case AUDIO_OUTPUT_CONFIG_CHANGED:
581 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700582 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700583 if (oldDesc == 0) {
Dean Wheatley8f992a12020-05-08 20:33:51 +1000584 ALOGW("ioConfigChanged() modifying unknown %s! %d",
585 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700586 break;
587 }
588
589 deviceId = oldDesc->getDeviceId();
590 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
591
592 if (deviceId != ioDesc->getDeviceId()) {
593 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700594 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
595 if (it != mAudioDeviceCallbacks.end()) {
596 callbacks = it->second;
597 }
Eric Laurent296fb132015-05-01 11:38:42 -0700598 }
599 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700600 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700601 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
602 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800603 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
604 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700605
Eric Laurentc2f1f072009-07-17 12:17:14 -0700606 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700607 case AUDIO_CLIENT_STARTED: {
608 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
609 if (oldDesc == 0) {
610 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
611 break;
612 }
613 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
614 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
615 oldDesc->mPatch = ioDesc->mPatch;
616 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
617 if (it != mAudioDeviceCallbacks.end()) {
618 auto cbks = it->second;
619 auto it2 = cbks.find(ioDesc->mPortId);
620 if (it2 != cbks.end()) {
621 callbacks.emplace(ioDesc->mPortId, it2->second);
622 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100623 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100624 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700625 } break;
626 }
627
628 for (auto wpCbk : callbacks) {
629 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
630 if (spCbk != nullptr) {
631 callbacksToCall.push_back(spCbk);
632 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700633 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800634 }
635
636 // Callbacks must be called without mLock held. May lead to dead lock if calling for
637 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700638 for (auto cb : callbacksToCall) {
639 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
640 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642}
643
Eric Laurent73e26b62015-04-27 16:55:58 -0700644status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
645 uint32_t sampleRate, audio_format_t format,
646 audio_channel_mask_t channelMask, size_t* buffSize)
647{
648 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
649 if (af == 0) {
650 return PERMISSION_DENIED;
651 }
652 Mutex::Autolock _l(mLock);
653 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
654 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
655 || (channelMask != mInChannelMask)) {
656 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
657 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800658 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700659 sampleRate, format, channelMask);
660 return BAD_VALUE;
661 }
662 // A benign race is possible here: we could overwrite a fresher cache entry
663 // save the request params
664 mInSamplingRate = sampleRate;
665 mInFormat = format;
666 mInChannelMask = channelMask;
667
668 mInBuffSize = inBuffSize;
669 }
670
671 *buffSize = mInBuffSize;
672
673 return NO_ERROR;
674}
675
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700676sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700677{
678 sp<AudioIoDescriptor> desc;
679 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
680 if (index >= 0) {
681 desc = mIoDescriptors.valueAt(index);
682 }
683 return desc;
684}
685
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700686sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
687{
688 Mutex::Autolock _l(mLock);
689 return getIoDescriptor_l(ioHandle);
690}
691
Eric Laurent296fb132015-05-01 11:38:42 -0700692status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700693 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
694 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700695{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700696 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800697 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700698 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
699 auto result = callbacks.try_emplace(portId, callback);
700 if (!result.second) {
701 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700702 }
Eric Laurent296fb132015-05-01 11:38:42 -0700703 return NO_ERROR;
704}
705
706status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700707 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
708 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700709{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700710 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800711 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700712 auto it = mAudioDeviceCallbacks.find(audioIo);
713 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700714 return INVALID_OPERATION;
715 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700716 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700717 return INVALID_OPERATION;
718 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700719 if (it->second.size() == 0) {
720 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700721 }
722 return NO_ERROR;
723}
724
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800725/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700726{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800727 Mutex::Autolock _l(gLockErrorCallbacks);
728 gAudioErrorCallbacks.insert(cb);
729 return reinterpret_cast<uintptr_t>(cb);
730}
731
732/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
733 Mutex::Autolock _l(gLockErrorCallbacks);
734 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
735}
736
737/* static */ void AudioSystem::reportError(status_t err) {
738 Mutex::Autolock _l(gLockErrorCallbacks);
739 for (auto callback : gAudioErrorCallbacks) {
740 callback(err);
741 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742}
743
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700744/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
745{
746 Mutex::Autolock _l(gLock);
747 gDynPolicyCallback = cb;
748}
749
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800750/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
751{
752 Mutex::Autolock _l(gLock);
753 gRecordConfigCallback = cb;
754}
755
Eric Laurentc2f1f072009-07-17 12:17:14 -0700756// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800757// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700758sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
759sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
760
761
Glenn Kasten18a6d902012-09-24 11:27:56 -0700762// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800763const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700764{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800765 sp<IAudioPolicyService> ap;
766 sp<AudioPolicyServiceClient> apc;
767 {
768 Mutex::Autolock _l(gLockAPS);
769 if (gAudioPolicyService == 0) {
770 sp<IServiceManager> sm = defaultServiceManager();
771 sp<IBinder> binder;
772 do {
773 binder = sm->getService(String16("media.audio_policy"));
774 if (binder != 0)
775 break;
776 ALOGW("AudioPolicyService not published, waiting...");
777 usleep(500000); // 0.5 s
778 } while (true);
779 if (gAudioPolicyServiceClient == NULL) {
780 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
781 }
782 binder->linkToDeath(gAudioPolicyServiceClient);
783 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
784 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
785 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700786 // Make sure callbacks can be received by gAudioPolicyServiceClient
787 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700788 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800789 ap = gAudioPolicyService;
790 }
791 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200792 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800793 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200794 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100795 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200796 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700797 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800798
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800799 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700800}
801
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700802// ---------------------------------------------------------------------------
803
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000804void AudioSystem::onNewAudioModulesAvailable()
805{
806 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
807 if (aps == 0) return;
808 aps->onNewAudioModulesAvailable();
809}
810
Dima Zavinfce7a472011-04-19 22:30:36 -0700811status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
812 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800813 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800814 const char *device_name,
815 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700816{
817 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700818 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800819 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700820
Eric Laurentc2f1f072009-07-17 12:17:14 -0700821 if (aps == 0) return PERMISSION_DENIED;
822
Eric Laurent71b63e32011-09-02 14:20:56 -0700823 if (device_address != NULL) {
824 address = device_address;
825 }
Paul McLeane743a472015-01-28 11:07:31 -0800826 if (device_name != NULL) {
827 name = device_name;
828 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800829 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830}
831
Dima Zavinfce7a472011-04-19 22:30:36 -0700832audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700833 const char *device_address)
834{
835 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700836 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700837
838 return aps->getDeviceConnectionState(device, device_address);
839}
840
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800841status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
842 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800843 const char *device_name,
844 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800845{
846 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
847 const char *address = "";
848 const char *name = "";
849
850 if (aps == 0) return PERMISSION_DENIED;
851
852 if (device_address != NULL) {
853 address = device_address;
854 }
855 if (device_name != NULL) {
856 name = device_name;
857 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800858 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800859}
860
Eric Laurent00dba062020-02-11 15:52:09 -0800861status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862{
Glenn Kasten347966c2012-01-18 14:58:32 -0800863 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700864 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
865 if (aps == 0) return PERMISSION_DENIED;
866
Eric Laurent00dba062020-02-11 15:52:09 -0800867 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700868}
869
Dima Zavinfce7a472011-04-19 22:30:36 -0700870status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700871{
872 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
873 if (aps == 0) return PERMISSION_DENIED;
874 return aps->setForceUse(usage, config);
875}
876
Dima Zavinfce7a472011-04-19 22:30:36 -0700877audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700878{
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700880 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881 return aps->getForceUse(usage);
882}
883
884
Eric Laurentf4e63452017-11-06 19:31:46 +0000885audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700886{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700887 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
888 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000889 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700890}
891
Eric Laurent42984412019-05-09 17:57:03 -0700892status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800893 audio_io_handle_t *output,
894 audio_session_t session,
895 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200896 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700897 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800898 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800899 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700900 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800901 audio_port_handle_t *portId,
902 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700903{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700904 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800905 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200906 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700907 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800908 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700909}
910
Eric Laurentd7fe0862018-07-14 16:48:01 -0700911status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700912{
913 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
914 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700915 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700916}
917
Eric Laurentd7fe0862018-07-14 16:48:01 -0700918status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700919{
920 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
921 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700922 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700923}
924
Eric Laurentd7fe0862018-07-14 16:48:01 -0700925void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700926{
927 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
928 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700929 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700930}
931
Eric Laurentcaf7f482014-11-25 17:50:47 -0800932status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
933 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700934 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800935 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700936 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700937 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800938 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800939 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600940 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700941 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800942 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943{
944 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800945 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600946 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700947 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800948 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700949}
950
Eric Laurent4eb58f12018-12-07 16:41:02 -0800951status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700952{
953 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
954 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800955 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956}
957
Eric Laurentfee19762018-01-29 18:44:13 -0800958status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959{
960 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
961 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800962 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700963}
964
Eric Laurentfee19762018-01-29 18:44:13 -0800965void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966{
967 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
968 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800969 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700970}
971
Dima Zavinfce7a472011-04-19 22:30:36 -0700972status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 int indexMin,
974 int indexMax)
975{
976 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
977 if (aps == 0) return PERMISSION_DENIED;
978 return aps->initStreamVolume(stream, indexMin, indexMax);
979}
980
Eric Laurent83844cc2011-11-18 16:43:31 -0800981status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
982 int index,
983 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984{
985 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
986 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800987 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988}
989
Eric Laurent83844cc2011-11-18 16:43:31 -0800990status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
991 int *index,
992 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700993{
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800996 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700997}
998
François Gaffiecfe17322018-11-07 13:41:29 +0100999status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
1000 int index,
1001 audio_devices_t device)
1002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
1005 return aps->setVolumeIndexForAttributes(attr, index, device);
1006}
1007
1008status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1009 int &index,
1010 audio_devices_t device)
1011{
1012 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1013 if (aps == 0) return PERMISSION_DENIED;
1014 return aps->getVolumeIndexForAttributes(attr, index, device);
1015}
1016
1017status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1018{
1019 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1020 if (aps == 0) return PERMISSION_DENIED;
1021 return aps->getMaxVolumeIndexForAttributes(attr, index);
1022}
1023
1024status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1025{
1026 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1027 if (aps == 0) return PERMISSION_DENIED;
1028 return aps->getMinVolumeIndexForAttributes(attr, index);
1029}
1030
Dima Zavinfce7a472011-04-19 22:30:36 -07001031uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001032{
1033 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001034 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001035 return aps->getStrategyForStream(stream);
1036}
1037
Eric Laurent63742522012-03-08 13:42:42 -08001038audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001039{
1040 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001041 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001042 return aps->getDevicesForStream(stream);
1043}
1044
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001045status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1046 AudioDeviceTypeAddrVector *devices) {
1047 if (devices == nullptr) {
1048 return BAD_VALUE;
1049 }
1050 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1051 if (aps == 0) return PERMISSION_DENIED;
1052 return aps->getDevicesForAttributes(aa, devices);
1053}
1054
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001055audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001056{
1057 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001058 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001059 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001060 return aps->getOutputForEffect(desc);
1061}
1062
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001063status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001064 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001065 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001066 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001067 int id)
1068{
1069 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1070 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001071 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001072}
1073
1074status_t AudioSystem::unregisterEffect(int id)
1075{
1076 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1077 if (aps == 0) return PERMISSION_DENIED;
1078 return aps->unregisterEffect(id);
1079}
1080
Eric Laurentdb7c0792011-08-10 10:37:50 -07001081status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1082{
1083 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1084 if (aps == 0) return PERMISSION_DENIED;
1085 return aps->setEffectEnabled(id, enabled);
1086}
1087
Eric Laurent6c796322019-04-09 14:13:17 -07001088status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1089{
1090 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1091 if (aps == 0) return PERMISSION_DENIED;
1092 return aps->moveEffectsToIo(ids, io);
1093}
1094
Glenn Kastenfff6d712012-01-12 16:38:12 -08001095status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001096{
Eric Laurenteda6c362011-02-02 09:33:30 -08001097 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1098 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001099 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001100 *state = aps->isStreamActive(stream, inPastMs);
1101 return NO_ERROR;
1102}
1103
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001104status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1105 uint32_t inPastMs)
1106{
1107 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1108 if (aps == 0) return PERMISSION_DENIED;
1109 if (state == NULL) return BAD_VALUE;
1110 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1111 return NO_ERROR;
1112}
1113
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001114status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1115{
1116 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1117 if (aps == 0) return PERMISSION_DENIED;
1118 if (state == NULL) return BAD_VALUE;
1119 *state = aps->isSourceActive(stream);
1120 return NO_ERROR;
1121}
1122
Glenn Kasten3b16c762012-11-14 08:44:39 -08001123uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001124{
1125 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1126 if (af == 0) return 0;
1127 return af->getPrimaryOutputSamplingRate();
1128}
1129
Glenn Kastene33054e2012-11-14 12:54:39 -08001130size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001131{
1132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1133 if (af == 0) return 0;
1134 return af->getPrimaryOutputFrameCount();
1135}
Eric Laurenteda6c362011-02-02 09:33:30 -08001136
Andy Hung6f248bb2018-01-23 14:04:37 -08001137status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001138{
1139 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1140 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001141 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001142}
1143
Eric Laurent9f6530f2011-08-30 10:18:54 -07001144void AudioSystem::clearAudioConfigCache()
1145{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001146 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001148 {
1149 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001150 if (gAudioFlingerClient != 0) {
1151 gAudioFlingerClient->clearIoCache();
1152 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001153 gAudioFlinger.clear();
1154 }
1155 {
1156 Mutex::Autolock _l(gLockAPS);
1157 gAudioPolicyService.clear();
1158 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001159}
1160
Hayden Gomes524159d2019-12-23 14:41:47 -08001161status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1162 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1163 if (aps == nullptr) return PERMISSION_DENIED;
1164 return aps->setSupportedSystemUsages(systemUsages);
1165}
1166
Kevin Rocardb99cc752019-03-21 20:52:24 -07001167status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1168 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1169 if (aps == nullptr) return PERMISSION_DENIED;
1170 return aps->setAllowedCapturePolicy(uid, flags);
1171}
1172
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001173bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1174{
1175 ALOGV("isOffloadSupported()");
1176 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1177 if (aps == 0) return false;
1178 return aps->isOffloadSupported(info);
1179}
1180
Eric Laurent203b1a12014-04-01 10:34:16 -07001181status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1182 audio_port_type_t type,
1183 unsigned int *num_ports,
1184 struct audio_port *ports,
1185 unsigned int *generation)
1186{
1187 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1188 if (aps == 0) return PERMISSION_DENIED;
1189 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1190}
1191
1192status_t AudioSystem::getAudioPort(struct audio_port *port)
1193{
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return PERMISSION_DENIED;
1196 return aps->getAudioPort(port);
1197}
1198
1199status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1200 audio_patch_handle_t *handle)
1201{
1202 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1203 if (aps == 0) return PERMISSION_DENIED;
1204 return aps->createAudioPatch(patch, handle);
1205}
1206
1207status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1208{
1209 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1210 if (aps == 0) return PERMISSION_DENIED;
1211 return aps->releaseAudioPatch(handle);
1212}
1213
1214status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1215 struct audio_patch *patches,
1216 unsigned int *generation)
1217{
1218 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1219 if (aps == 0) return PERMISSION_DENIED;
1220 return aps->listAudioPatches(num_patches, patches, generation);
1221}
1222
1223status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1224{
1225 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1226 if (aps == 0) return PERMISSION_DENIED;
1227 return aps->setAudioPortConfig(config);
1228}
1229
Eric Laurent296fb132015-05-01 11:38:42 -07001230status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001231{
Eric Laurentb28753e2015-04-01 13:06:28 -07001232 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1233 if (aps == 0) return PERMISSION_DENIED;
1234
1235 Mutex::Autolock _l(gLockAPS);
1236 if (gAudioPolicyServiceClient == 0) {
1237 return NO_INIT;
1238 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001239 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1240 if (ret == 1) {
1241 aps->setAudioPortCallbacksEnabled(true);
1242 }
1243 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001244}
1245
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001246/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001247status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001248{
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 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001256 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1257 if (ret == 0) {
1258 aps->setAudioPortCallbacksEnabled(false);
1259 }
1260 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001261}
1262
François Gaffiecfe17322018-11-07 13:41:29 +01001263status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1264{
1265 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1266 if (aps == 0) return PERMISSION_DENIED;
1267
1268 Mutex::Autolock _l(gLockAPS);
1269 if (gAudioPolicyServiceClient == 0) {
1270 return NO_INIT;
1271 }
1272 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1273 if (ret == 1) {
1274 aps->setAudioVolumeGroupCallbacksEnabled(true);
1275 }
1276 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1277}
1278
1279status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1280{
1281 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1282 if (aps == 0) return PERMISSION_DENIED;
1283
1284 Mutex::Autolock _l(gLockAPS);
1285 if (gAudioPolicyServiceClient == 0) {
1286 return NO_INIT;
1287 }
1288 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1289 if (ret == 0) {
1290 aps->setAudioVolumeGroupCallbacksEnabled(false);
1291 }
1292 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1293}
1294
Eric Laurent296fb132015-05-01 11:38:42 -07001295status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001296 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1297 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001298{
1299 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1300 if (afc == 0) {
1301 return NO_INIT;
1302 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001303 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001304 if (status == NO_ERROR) {
1305 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1306 if (af != 0) {
1307 af->registerClient(afc);
1308 }
1309 }
1310 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001311}
1312
1313status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001314 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1315 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001316{
1317 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1318 if (afc == 0) {
1319 return NO_INIT;
1320 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001321 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001322}
1323
1324audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1325{
1326 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1327 if (af == 0) return PERMISSION_DENIED;
1328 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1329 if (desc == 0) {
1330 return AUDIO_PORT_HANDLE_NONE;
1331 }
1332 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001333}
1334
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001335status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1336 audio_io_handle_t *ioHandle,
1337 audio_devices_t *device)
1338{
1339 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1340 if (aps == 0) return PERMISSION_DENIED;
1341 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1342}
1343
1344status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1345{
1346 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1347 if (aps == 0) return PERMISSION_DENIED;
1348 return aps->releaseSoundTriggerSession(session);
1349}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001350
1351audio_mode_t AudioSystem::getPhoneState()
1352{
1353 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1354 if (aps == 0) return AUDIO_MODE_INVALID;
1355 return aps->getPhoneState();
1356}
1357
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001358status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001359{
1360 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1361 if (aps == 0) return PERMISSION_DENIED;
1362 return aps->registerPolicyMixes(mixes, registration);
1363}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001364
jiabin6a02d532020-08-07 11:56:38 -07001365status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices)
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001366{
1367 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1368 if (aps == 0) return PERMISSION_DENIED;
1369 return aps->setUidDeviceAffinities(uid, devices);
1370}
1371
1372status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1373 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1374 if (aps == 0) return PERMISSION_DENIED;
1375 return aps->removeUidDeviceAffinities(uid);
1376}
1377
Oscar Azucena90e77632019-11-27 17:12:28 -08001378status_t AudioSystem::setUserIdDeviceAffinities(int userId,
jiabin6a02d532020-08-07 11:56:38 -07001379 const AudioDeviceTypeAddrVector& devices)
Oscar Azucena90e77632019-11-27 17:12:28 -08001380{
1381 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1382 if (aps == 0) return PERMISSION_DENIED;
1383 return aps->setUserIdDeviceAffinities(userId, devices);
1384}
1385
1386status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1387{
1388 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1389 if (aps == 0) return PERMISSION_DENIED;
1390 return aps->removeUserIdDeviceAffinities(userId);
1391}
1392
Eric Laurent554a2772015-04-10 11:29:24 -07001393status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1394 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001395 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001396{
1397 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1398 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001399 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001400}
1401
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001402status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001403{
1404 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1405 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001406 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001407}
1408
Andy Hung2ddee192015-12-18 17:34:44 -08001409status_t AudioSystem::setMasterMono(bool mono)
1410{
1411 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1412 if (aps == 0) return PERMISSION_DENIED;
1413 return aps->setMasterMono(mono);
1414}
1415
1416status_t AudioSystem::getMasterMono(bool *mono)
1417{
1418 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1419 if (aps == 0) return PERMISSION_DENIED;
1420 return aps->getMasterMono(mono);
1421}
1422
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001423status_t AudioSystem::setMasterBalance(float balance)
1424{
1425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1426 if (af == 0) return PERMISSION_DENIED;
1427 return af->setMasterBalance(balance);
1428}
1429
1430status_t AudioSystem::getMasterBalance(float *balance)
1431{
1432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1433 if (af == 0) return PERMISSION_DENIED;
1434 return af->getMasterBalance(balance);
1435}
1436
Eric Laurentac9cef52017-06-09 15:46:26 -07001437float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1438{
1439 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1440 if (aps == 0) return NAN;
1441 return aps->getStreamVolumeDB(stream, index, device);
1442}
1443
jiabin46a76fa2018-01-05 10:18:21 -08001444status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1445{
1446 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1447 if (af == 0) return PERMISSION_DENIED;
1448 return af->getMicrophones(microphones);
1449}
1450
Eric Laurent42896a02019-09-27 15:40:33 -07001451status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1452 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1453 if (af == nullptr) return PERMISSION_DENIED;
1454 return af->setAudioHalPids(pids);
1455}
1456
jiabin81772902018-04-02 17:52:27 -07001457status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1458 audio_format_t *surroundFormats,
1459 bool *surroundFormatsEnabled,
1460 bool reported)
1461{
1462 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1463 if (aps == 0) return PERMISSION_DENIED;
1464 return aps->getSurroundFormats(
1465 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1466}
1467
1468status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1469{
1470 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1471 if (aps == 0) return PERMISSION_DENIED;
1472 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1473}
1474
Eric Laurentb78763e2018-10-17 10:08:02 -07001475status_t AudioSystem::setAssistantUid(uid_t uid)
1476{
1477 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1478 if (aps == 0) return PERMISSION_DENIED;
1479
1480 return aps->setAssistantUid(uid);
1481}
1482
1483status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1484{
1485 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1486 if (aps == 0) return PERMISSION_DENIED;
1487
1488 return aps->setA11yServicesUids(uids);
1489}
1490
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001491status_t AudioSystem::setCurrentImeUid(uid_t uid)
1492{
1493 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1494 if (aps == 0) return PERMISSION_DENIED;
1495
1496 return aps->setCurrentImeUid(uid);
1497}
1498
jiabin6012f912018-11-02 17:06:30 -07001499bool AudioSystem::isHapticPlaybackSupported()
1500{
1501 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1502 if (aps == 0) return false;
1503 return aps->isHapticPlaybackSupported();
1504}
1505
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001506status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001507 std::vector<audio_format_t> *formats) {
1508 const sp <IAudioPolicyService>
1509 & aps = AudioSystem::get_audio_policy_service();
1510 if (aps == 0) return PERMISSION_DENIED;
1511 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1512}
1513
1514status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001515{
1516 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1517 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001518 return aps->listAudioProductStrategies(strategies);
1519}
1520
1521audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1522{
1523 AudioProductStrategyVector strategies;
1524 listAudioProductStrategies(strategies);
1525 for (const auto &strategy : strategies) {
1526 auto attrVect = strategy.getAudioAttributes();
1527 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1528 return attributes.getStreamType() == stream; });
1529 if (iter != end(attrVect)) {
1530 return iter->getAttributes();
1531 }
1532 }
1533 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1534 return AUDIO_ATTRIBUTES_INITIALIZER;
1535}
1536
1537audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1538{
François Gaffie4b2018b2018-11-07 11:18:59 +01001539 product_strategy_t psId;
1540 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1541 if (ret != NO_ERROR) {
1542 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1543 return AUDIO_STREAM_MUSIC;
1544 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001545 AudioProductStrategyVector strategies;
1546 listAudioProductStrategies(strategies);
1547 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001548 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001549 auto attrVect = strategy.getAudioAttributes();
1550 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1551 return AudioProductStrategy::attributesMatches(
1552 refAttr.getAttributes(), attr); });
1553 if (iter != end(attrVect)) {
1554 return iter->getStreamType();
1555 }
1556 }
1557 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001558 switch (attr.usage) {
1559 case AUDIO_USAGE_VIRTUAL_SOURCE:
1560 // virtual source is not expected to have an associated product strategy
1561 break;
1562 default:
1563 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1564 break;
1565 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001566 return AUDIO_STREAM_MUSIC;
1567}
1568
François Gaffie4b2018b2018-11-07 11:18:59 +01001569status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1570 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001571{
1572 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001573 if (aps == 0) return PERMISSION_DENIED;
1574 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1575}
1576
1577status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1578{
1579 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1580 if (aps == 0) return PERMISSION_DENIED;
1581 return aps->listAudioVolumeGroups(groups);
1582}
1583
1584status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1585 volume_group_t &volumeGroup)
1586{
1587 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1588 if (aps == 0) return PERMISSION_DENIED;
1589 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001590}
Eric Laurentb78763e2018-10-17 10:08:02 -07001591
Eric Laurent6ede98f2019-06-11 14:50:30 -07001592status_t AudioSystem::setRttEnabled(bool enabled)
1593{
1594 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1595 if (aps == 0) return PERMISSION_DENIED;
1596 return aps->setRttEnabled(enabled);
1597}
1598
Eric Laurent8340e672019-11-06 11:01:08 -08001599bool AudioSystem::isCallScreenModeSupported()
1600{
1601 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1602 if (aps == 0) return false;
1603 return aps->isCallScreenModeSupported();
1604}
1605
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001606status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1607 const AudioDeviceTypeAddr &device)
1608{
1609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1610 if (aps == 0) {
1611 return PERMISSION_DENIED;
1612 }
1613 return aps->setPreferredDeviceForStrategy(strategy, device);
1614}
1615
1616status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1617{
1618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1619 if (aps == 0) {
1620 return PERMISSION_DENIED;
1621 }
1622 return aps->removePreferredDeviceForStrategy(strategy);
1623}
1624
1625status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1626 AudioDeviceTypeAddr &device)
1627{
1628 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1629 if (aps == 0) {
1630 return PERMISSION_DENIED;
1631 }
1632 return aps->getPreferredDeviceForStrategy(strategy, device);
1633}
1634
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001635class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1636 public IBinder::DeathRecipient {
1637public:
1638 binder::Status setCaptureState(bool active) override {
1639 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1640 gSoundTriggerCaptureStateListener->onStateChanged(active);
1641 return binder::Status::ok();
1642 }
1643
1644 void binderDied(const wp<IBinder>&) override {
1645 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1646 gSoundTriggerCaptureStateListener->onServiceDied();
1647 gSoundTriggerCaptureStateListener = nullptr;
1648 }
1649};
1650
1651status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1652 const sp<CaptureStateListener>& listener) {
1653 const sp<IAudioPolicyService>& aps =
1654 AudioSystem::get_audio_policy_service();
1655 if (aps == 0) {
1656 return PERMISSION_DENIED;
1657 }
1658
1659 sp<CaptureStateListenerImpl> wrapper = new CaptureStateListenerImpl();
1660
1661 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1662
1663 bool active;
1664 status_t status =
1665 aps->registerSoundTriggerCaptureStateListener(wrapper, &active);
1666 if (status != NO_ERROR) {
1667 listener->onServiceDied();
1668 return NO_ERROR;
1669 }
1670 gSoundTriggerCaptureStateListener = listener;
1671 listener->onStateChanged(active);
1672 sp<IBinder> binder = IInterface::asBinder(aps);
1673 binder->linkToDeath(wrapper);
1674 return NO_ERROR;
1675}
1676
Eric Laurentc2f1f072009-07-17 12:17:14 -07001677// ---------------------------------------------------------------------------
1678
Eric Laurente8726fe2015-06-26 09:39:24 -07001679int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001680 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001681{
1682 Mutex::Autolock _l(mLock);
1683 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001684 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001685 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001686 }
1687 }
Eric Laurent296fb132015-05-01 11:38:42 -07001688 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001689 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001690}
1691
Eric Laurente8726fe2015-06-26 09:39:24 -07001692int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001693 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001694{
1695 Mutex::Autolock _l(mLock);
1696 size_t i;
1697 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001698 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001699 break;
1700 }
1701 }
1702 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001703 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001704 }
1705 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001706 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001707}
1708
Eric Laurent296fb132015-05-01 11:38:42 -07001709
Eric Laurentb28753e2015-04-01 13:06:28 -07001710void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1711{
1712 Mutex::Autolock _l(mLock);
1713 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1714 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1715 }
1716}
1717
1718void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1719{
1720 Mutex::Autolock _l(mLock);
1721 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1722 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1723 }
1724}
1725
François Gaffiecfe17322018-11-07 13:41:29 +01001726// ----------------------------------------------------------------------------
1727int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1728 const sp<AudioVolumeGroupCallback>& callback)
1729{
1730 Mutex::Autolock _l(mLock);
1731 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1732 if (mAudioVolumeGroupCallback[i] == callback) {
1733 return -1;
1734 }
1735 }
1736 mAudioVolumeGroupCallback.add(callback);
1737 return mAudioVolumeGroupCallback.size();
1738}
1739
1740int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1741 const sp<AudioVolumeGroupCallback>& callback)
1742{
1743 Mutex::Autolock _l(mLock);
1744 size_t i;
1745 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1746 if (mAudioVolumeGroupCallback[i] == callback) {
1747 break;
1748 }
1749 }
1750 if (i == mAudioVolumeGroupCallback.size()) {
1751 return -1;
1752 }
1753 mAudioVolumeGroupCallback.removeAt(i);
1754 return mAudioVolumeGroupCallback.size();
1755}
1756
1757void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1758 int flags)
1759{
1760 Mutex::Autolock _l(mLock);
1761 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1762 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1763 }
1764}
1765// ----------------------------------------------------------------------------
1766
Jean-Michel Trivide801052015-04-14 19:10:14 -07001767void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1768 String8 regId, int32_t state)
1769{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001770 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1771 dynamic_policy_callback cb = NULL;
1772 {
1773 Mutex::Autolock _l(AudioSystem::gLock);
1774 cb = gDynPolicyCallback;
1775 }
1776
1777 if (cb != NULL) {
1778 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1779 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001780}
1781
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001782void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001783 int event,
1784 const record_client_info_t *clientInfo,
1785 const audio_config_base_t *clientConfig,
1786 std::vector<effect_descriptor_t> clientEffects,
1787 const audio_config_base_t *deviceConfig,
1788 std::vector<effect_descriptor_t> effects,
1789 audio_patch_handle_t patchHandle,
1790 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001791 record_config_callback cb = NULL;
1792 {
1793 Mutex::Autolock _l(AudioSystem::gLock);
1794 cb = gRecordConfigCallback;
1795 }
1796
1797 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001798 cb(event, clientInfo, clientConfig, clientEffects,
1799 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001800 }
1801}
1802
Glenn Kasten4944acb2013-08-19 08:39:20 -07001803void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1804{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001805 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001806 Mutex::Autolock _l(mLock);
1807 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1808 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001809 }
François Gaffiecfe17322018-11-07 13:41:29 +01001810 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1811 mAudioVolumeGroupCallback[i]->onServiceDied();
1812 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001813 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001814 {
1815 Mutex::Autolock _l(gLockAPS);
1816 AudioSystem::gAudioPolicyService.clear();
1817 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001818
Steve Block5ff1dd52012-01-05 23:22:43 +00001819 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001820}
1821
Glenn Kasten40bc9062015-03-20 09:09:33 -07001822} // namespace android