blob: 6357da431687430c15634d695bc3038cb35299d8 [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) {
584 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
585 break;
586 }
587
588 deviceId = oldDesc->getDeviceId();
589 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
590
591 if (deviceId != ioDesc->getDeviceId()) {
592 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700593 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
594 if (it != mAudioDeviceCallbacks.end()) {
595 callbacks = it->second;
596 }
Eric Laurent296fb132015-05-01 11:38:42 -0700597 }
598 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700599 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700600 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
601 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800602 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
603 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700604
Eric Laurentc2f1f072009-07-17 12:17:14 -0700605 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700606 case AUDIO_CLIENT_STARTED: {
607 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
608 if (oldDesc == 0) {
609 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
610 break;
611 }
612 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
613 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
614 oldDesc->mPatch = ioDesc->mPatch;
615 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
616 if (it != mAudioDeviceCallbacks.end()) {
617 auto cbks = it->second;
618 auto it2 = cbks.find(ioDesc->mPortId);
619 if (it2 != cbks.end()) {
620 callbacks.emplace(ioDesc->mPortId, it2->second);
621 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100622 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100623 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700624 } break;
625 }
626
627 for (auto wpCbk : callbacks) {
628 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
629 if (spCbk != nullptr) {
630 callbacksToCall.push_back(spCbk);
631 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700632 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800633 }
634
635 // Callbacks must be called without mLock held. May lead to dead lock if calling for
636 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700637 for (auto cb : callbacksToCall) {
638 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
639 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700640 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641}
642
Eric Laurent73e26b62015-04-27 16:55:58 -0700643status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
644 uint32_t sampleRate, audio_format_t format,
645 audio_channel_mask_t channelMask, size_t* buffSize)
646{
647 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
648 if (af == 0) {
649 return PERMISSION_DENIED;
650 }
651 Mutex::Autolock _l(mLock);
652 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
653 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
654 || (channelMask != mInChannelMask)) {
655 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
656 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800657 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700658 sampleRate, format, channelMask);
659 return BAD_VALUE;
660 }
661 // A benign race is possible here: we could overwrite a fresher cache entry
662 // save the request params
663 mInSamplingRate = sampleRate;
664 mInFormat = format;
665 mInChannelMask = channelMask;
666
667 mInBuffSize = inBuffSize;
668 }
669
670 *buffSize = mInBuffSize;
671
672 return NO_ERROR;
673}
674
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700675sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700676{
677 sp<AudioIoDescriptor> desc;
678 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
679 if (index >= 0) {
680 desc = mIoDescriptors.valueAt(index);
681 }
682 return desc;
683}
684
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700685sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
686{
687 Mutex::Autolock _l(mLock);
688 return getIoDescriptor_l(ioHandle);
689}
690
Eric Laurent296fb132015-05-01 11:38:42 -0700691status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700692 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
693 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700694{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700695 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800696 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700697 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
698 auto result = callbacks.try_emplace(portId, callback);
699 if (!result.second) {
700 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700701 }
Eric Laurent296fb132015-05-01 11:38:42 -0700702 return NO_ERROR;
703}
704
705status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700706 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
707 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700708{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700709 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800710 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700711 auto it = mAudioDeviceCallbacks.find(audioIo);
712 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700713 return INVALID_OPERATION;
714 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700715 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700716 return INVALID_OPERATION;
717 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700718 if (it->second.size() == 0) {
719 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700720 }
721 return NO_ERROR;
722}
723
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800724/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700725{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800726 Mutex::Autolock _l(gLockErrorCallbacks);
727 gAudioErrorCallbacks.insert(cb);
728 return reinterpret_cast<uintptr_t>(cb);
729}
730
731/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
732 Mutex::Autolock _l(gLockErrorCallbacks);
733 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
734}
735
736/* static */ void AudioSystem::reportError(status_t err) {
737 Mutex::Autolock _l(gLockErrorCallbacks);
738 for (auto callback : gAudioErrorCallbacks) {
739 callback(err);
740 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741}
742
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700743/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
744{
745 Mutex::Autolock _l(gLock);
746 gDynPolicyCallback = cb;
747}
748
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800749/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
750{
751 Mutex::Autolock _l(gLock);
752 gRecordConfigCallback = cb;
753}
754
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800756// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
758sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
759
760
Glenn Kasten18a6d902012-09-24 11:27:56 -0700761// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800762const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700763{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800764 sp<IAudioPolicyService> ap;
765 sp<AudioPolicyServiceClient> apc;
766 {
767 Mutex::Autolock _l(gLockAPS);
768 if (gAudioPolicyService == 0) {
769 sp<IServiceManager> sm = defaultServiceManager();
770 sp<IBinder> binder;
771 do {
772 binder = sm->getService(String16("media.audio_policy"));
773 if (binder != 0)
774 break;
775 ALOGW("AudioPolicyService not published, waiting...");
776 usleep(500000); // 0.5 s
777 } while (true);
778 if (gAudioPolicyServiceClient == NULL) {
779 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
780 }
781 binder->linkToDeath(gAudioPolicyServiceClient);
782 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
783 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
784 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700785 // Make sure callbacks can be received by gAudioPolicyServiceClient
786 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700787 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800788 ap = gAudioPolicyService;
789 }
790 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200791 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800792 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200793 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100794 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200795 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700796 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800797
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800798 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700799}
800
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700801// ---------------------------------------------------------------------------
802
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000803void AudioSystem::onNewAudioModulesAvailable()
804{
805 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
806 if (aps == 0) return;
807 aps->onNewAudioModulesAvailable();
808}
809
Dima Zavinfce7a472011-04-19 22:30:36 -0700810status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
811 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800812 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800813 const char *device_name,
814 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700815{
816 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700817 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800818 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700819
Eric Laurentc2f1f072009-07-17 12:17:14 -0700820 if (aps == 0) return PERMISSION_DENIED;
821
Eric Laurent71b63e32011-09-02 14:20:56 -0700822 if (device_address != NULL) {
823 address = device_address;
824 }
Paul McLeane743a472015-01-28 11:07:31 -0800825 if (device_name != NULL) {
826 name = device_name;
827 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800828 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700829}
830
Dima Zavinfce7a472011-04-19 22:30:36 -0700831audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700832 const char *device_address)
833{
834 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700835 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700836
837 return aps->getDeviceConnectionState(device, device_address);
838}
839
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800840status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
841 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800842 const char *device_name,
843 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800844{
845 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
846 const char *address = "";
847 const char *name = "";
848
849 if (aps == 0) return PERMISSION_DENIED;
850
851 if (device_address != NULL) {
852 address = device_address;
853 }
854 if (device_name != NULL) {
855 name = device_name;
856 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800857 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800858}
859
Eric Laurent00dba062020-02-11 15:52:09 -0800860status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700861{
Glenn Kasten347966c2012-01-18 14:58:32 -0800862 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700863 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
864 if (aps == 0) return PERMISSION_DENIED;
865
Eric Laurent00dba062020-02-11 15:52:09 -0800866 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700867}
868
Dima Zavinfce7a472011-04-19 22:30:36 -0700869status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700870{
871 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
872 if (aps == 0) return PERMISSION_DENIED;
873 return aps->setForceUse(usage, config);
874}
875
Dima Zavinfce7a472011-04-19 22:30:36 -0700876audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700877{
878 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700879 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700880 return aps->getForceUse(usage);
881}
882
883
Eric Laurentf4e63452017-11-06 19:31:46 +0000884audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700885{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700886 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
887 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000888 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700889}
890
Eric Laurent42984412019-05-09 17:57:03 -0700891status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800892 audio_io_handle_t *output,
893 audio_session_t session,
894 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200895 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700896 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800897 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800898 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700899 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800900 audio_port_handle_t *portId,
901 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700902{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700903 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800904 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200905 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700906 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800907 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700908}
909
Eric Laurentd7fe0862018-07-14 16:48:01 -0700910status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911{
912 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
913 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700914 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700915}
916
Eric Laurentd7fe0862018-07-14 16:48:01 -0700917status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918{
919 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
920 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700921 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700922}
923
Eric Laurentd7fe0862018-07-14 16:48:01 -0700924void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700925{
926 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
927 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700928 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700929}
930
Eric Laurentcaf7f482014-11-25 17:50:47 -0800931status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
932 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700933 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800934 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700935 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700936 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800937 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800938 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600939 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700940 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800941 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700942{
943 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800944 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600945 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700946 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800947 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948}
949
Eric Laurent4eb58f12018-12-07 16:41:02 -0800950status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700951{
952 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
953 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800954 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955}
956
Eric Laurentfee19762018-01-29 18:44:13 -0800957status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700958{
959 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
960 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800961 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962}
963
Eric Laurentfee19762018-01-29 18:44:13 -0800964void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965{
966 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
967 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800968 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700969}
970
Dima Zavinfce7a472011-04-19 22:30:36 -0700971status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700972 int indexMin,
973 int indexMax)
974{
975 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
976 if (aps == 0) return PERMISSION_DENIED;
977 return aps->initStreamVolume(stream, indexMin, indexMax);
978}
979
Eric Laurent83844cc2011-11-18 16:43:31 -0800980status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
981 int index,
982 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700983{
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
985 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800986 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700987}
988
Eric Laurent83844cc2011-11-18 16:43:31 -0800989status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
990 int *index,
991 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700992{
993 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
994 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800995 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700996}
997
François Gaffiecfe17322018-11-07 13:41:29 +0100998status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
999 int index,
1000 audio_devices_t device)
1001{
1002 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1003 if (aps == 0) return PERMISSION_DENIED;
1004 return aps->setVolumeIndexForAttributes(attr, index, device);
1005}
1006
1007status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1008 int &index,
1009 audio_devices_t device)
1010{
1011 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1012 if (aps == 0) return PERMISSION_DENIED;
1013 return aps->getVolumeIndexForAttributes(attr, index, device);
1014}
1015
1016status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1017{
1018 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1019 if (aps == 0) return PERMISSION_DENIED;
1020 return aps->getMaxVolumeIndexForAttributes(attr, index);
1021}
1022
1023status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1024{
1025 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1026 if (aps == 0) return PERMISSION_DENIED;
1027 return aps->getMinVolumeIndexForAttributes(attr, index);
1028}
1029
Dima Zavinfce7a472011-04-19 22:30:36 -07001030uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001031{
1032 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001033 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001034 return aps->getStrategyForStream(stream);
1035}
1036
Eric Laurent63742522012-03-08 13:42:42 -08001037audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001038{
1039 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001040 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001041 return aps->getDevicesForStream(stream);
1042}
1043
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001044status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1045 AudioDeviceTypeAddrVector *devices) {
1046 if (devices == nullptr) {
1047 return BAD_VALUE;
1048 }
1049 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1050 if (aps == 0) return PERMISSION_DENIED;
1051 return aps->getDevicesForAttributes(aa, devices);
1052}
1053
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001054audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001055{
1056 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001057 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001058 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001059 return aps->getOutputForEffect(desc);
1060}
1061
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001062status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001063 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001064 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001065 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001066 int id)
1067{
1068 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1069 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001070 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001071}
1072
1073status_t AudioSystem::unregisterEffect(int id)
1074{
1075 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1076 if (aps == 0) return PERMISSION_DENIED;
1077 return aps->unregisterEffect(id);
1078}
1079
Eric Laurentdb7c0792011-08-10 10:37:50 -07001080status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1081{
1082 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1083 if (aps == 0) return PERMISSION_DENIED;
1084 return aps->setEffectEnabled(id, enabled);
1085}
1086
Eric Laurent6c796322019-04-09 14:13:17 -07001087status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1088{
1089 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1090 if (aps == 0) return PERMISSION_DENIED;
1091 return aps->moveEffectsToIo(ids, io);
1092}
1093
Glenn Kastenfff6d712012-01-12 16:38:12 -08001094status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001095{
Eric Laurenteda6c362011-02-02 09:33:30 -08001096 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1097 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001098 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001099 *state = aps->isStreamActive(stream, inPastMs);
1100 return NO_ERROR;
1101}
1102
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001103status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1104 uint32_t inPastMs)
1105{
1106 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1107 if (aps == 0) return PERMISSION_DENIED;
1108 if (state == NULL) return BAD_VALUE;
1109 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1110 return NO_ERROR;
1111}
1112
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001113status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1114{
1115 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1116 if (aps == 0) return PERMISSION_DENIED;
1117 if (state == NULL) return BAD_VALUE;
1118 *state = aps->isSourceActive(stream);
1119 return NO_ERROR;
1120}
1121
Glenn Kasten3b16c762012-11-14 08:44:39 -08001122uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001123{
1124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1125 if (af == 0) return 0;
1126 return af->getPrimaryOutputSamplingRate();
1127}
1128
Glenn Kastene33054e2012-11-14 12:54:39 -08001129size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001130{
1131 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1132 if (af == 0) return 0;
1133 return af->getPrimaryOutputFrameCount();
1134}
Eric Laurenteda6c362011-02-02 09:33:30 -08001135
Andy Hung6f248bb2018-01-23 14:04:37 -08001136status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001137{
1138 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1139 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001140 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001141}
1142
Eric Laurent9f6530f2011-08-30 10:18:54 -07001143void AudioSystem::clearAudioConfigCache()
1144{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001145 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001146 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001147 {
1148 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001149 if (gAudioFlingerClient != 0) {
1150 gAudioFlingerClient->clearIoCache();
1151 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001152 gAudioFlinger.clear();
1153 }
1154 {
1155 Mutex::Autolock _l(gLockAPS);
1156 gAudioPolicyService.clear();
1157 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001158}
1159
Hayden Gomes524159d2019-12-23 14:41:47 -08001160status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1161 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1162 if (aps == nullptr) return PERMISSION_DENIED;
1163 return aps->setSupportedSystemUsages(systemUsages);
1164}
1165
Kevin Rocardb99cc752019-03-21 20:52:24 -07001166status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1167 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1168 if (aps == nullptr) return PERMISSION_DENIED;
1169 return aps->setAllowedCapturePolicy(uid, flags);
1170}
1171
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001172bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1173{
1174 ALOGV("isOffloadSupported()");
1175 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1176 if (aps == 0) return false;
1177 return aps->isOffloadSupported(info);
1178}
1179
Eric Laurent203b1a12014-04-01 10:34:16 -07001180status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1181 audio_port_type_t type,
1182 unsigned int *num_ports,
1183 struct audio_port *ports,
1184 unsigned int *generation)
1185{
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == 0) return PERMISSION_DENIED;
1188 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1189}
1190
1191status_t AudioSystem::getAudioPort(struct audio_port *port)
1192{
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == 0) return PERMISSION_DENIED;
1195 return aps->getAudioPort(port);
1196}
1197
1198status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1199 audio_patch_handle_t *handle)
1200{
1201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1202 if (aps == 0) return PERMISSION_DENIED;
1203 return aps->createAudioPatch(patch, handle);
1204}
1205
1206status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1207{
1208 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1209 if (aps == 0) return PERMISSION_DENIED;
1210 return aps->releaseAudioPatch(handle);
1211}
1212
1213status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1214 struct audio_patch *patches,
1215 unsigned int *generation)
1216{
1217 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1218 if (aps == 0) return PERMISSION_DENIED;
1219 return aps->listAudioPatches(num_patches, patches, generation);
1220}
1221
1222status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1223{
1224 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1225 if (aps == 0) return PERMISSION_DENIED;
1226 return aps->setAudioPortConfig(config);
1227}
1228
Eric Laurent296fb132015-05-01 11:38:42 -07001229status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001230{
Eric Laurentb28753e2015-04-01 13:06:28 -07001231 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1232 if (aps == 0) return PERMISSION_DENIED;
1233
1234 Mutex::Autolock _l(gLockAPS);
1235 if (gAudioPolicyServiceClient == 0) {
1236 return NO_INIT;
1237 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001238 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1239 if (ret == 1) {
1240 aps->setAudioPortCallbacksEnabled(true);
1241 }
1242 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001243}
1244
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001245/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001246status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001247{
1248 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1249 if (aps == 0) return PERMISSION_DENIED;
1250
1251 Mutex::Autolock _l(gLockAPS);
1252 if (gAudioPolicyServiceClient == 0) {
1253 return NO_INIT;
1254 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001255 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1256 if (ret == 0) {
1257 aps->setAudioPortCallbacksEnabled(false);
1258 }
1259 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001260}
1261
François Gaffiecfe17322018-11-07 13:41:29 +01001262status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1263{
1264 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1265 if (aps == 0) return PERMISSION_DENIED;
1266
1267 Mutex::Autolock _l(gLockAPS);
1268 if (gAudioPolicyServiceClient == 0) {
1269 return NO_INIT;
1270 }
1271 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1272 if (ret == 1) {
1273 aps->setAudioVolumeGroupCallbacksEnabled(true);
1274 }
1275 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1276}
1277
1278status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1279{
1280 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1281 if (aps == 0) return PERMISSION_DENIED;
1282
1283 Mutex::Autolock _l(gLockAPS);
1284 if (gAudioPolicyServiceClient == 0) {
1285 return NO_INIT;
1286 }
1287 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1288 if (ret == 0) {
1289 aps->setAudioVolumeGroupCallbacksEnabled(false);
1290 }
1291 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1292}
1293
Eric Laurent296fb132015-05-01 11:38:42 -07001294status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001295 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1296 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001297{
1298 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1299 if (afc == 0) {
1300 return NO_INIT;
1301 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001302 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001303 if (status == NO_ERROR) {
1304 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1305 if (af != 0) {
1306 af->registerClient(afc);
1307 }
1308 }
1309 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001310}
1311
1312status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001313 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1314 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001315{
1316 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1317 if (afc == 0) {
1318 return NO_INIT;
1319 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001320 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001321}
1322
1323audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1324{
1325 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1326 if (af == 0) return PERMISSION_DENIED;
1327 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1328 if (desc == 0) {
1329 return AUDIO_PORT_HANDLE_NONE;
1330 }
1331 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001332}
1333
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001334status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1335 audio_io_handle_t *ioHandle,
1336 audio_devices_t *device)
1337{
1338 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1339 if (aps == 0) return PERMISSION_DENIED;
1340 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1341}
1342
1343status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1344{
1345 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1346 if (aps == 0) return PERMISSION_DENIED;
1347 return aps->releaseSoundTriggerSession(session);
1348}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001349
1350audio_mode_t AudioSystem::getPhoneState()
1351{
1352 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1353 if (aps == 0) return AUDIO_MODE_INVALID;
1354 return aps->getPhoneState();
1355}
1356
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001357status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001358{
1359 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1360 if (aps == 0) return PERMISSION_DENIED;
1361 return aps->registerPolicyMixes(mixes, registration);
1362}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001363
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001364status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1365{
1366 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1367 if (aps == 0) return PERMISSION_DENIED;
1368 return aps->setUidDeviceAffinities(uid, devices);
1369}
1370
1371status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1372 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1373 if (aps == 0) return PERMISSION_DENIED;
1374 return aps->removeUidDeviceAffinities(uid);
1375}
1376
Oscar Azucena90e77632019-11-27 17:12:28 -08001377status_t AudioSystem::setUserIdDeviceAffinities(int userId,
1378 const Vector<AudioDeviceTypeAddr>& devices)
1379{
1380 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1381 if (aps == 0) return PERMISSION_DENIED;
1382 return aps->setUserIdDeviceAffinities(userId, devices);
1383}
1384
1385status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1386{
1387 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1388 if (aps == 0) return PERMISSION_DENIED;
1389 return aps->removeUserIdDeviceAffinities(userId);
1390}
1391
Eric Laurent554a2772015-04-10 11:29:24 -07001392status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1393 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001394 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001395{
1396 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1397 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001398 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001399}
1400
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001401status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001402{
1403 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1404 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001405 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001406}
1407
Andy Hung2ddee192015-12-18 17:34:44 -08001408status_t AudioSystem::setMasterMono(bool mono)
1409{
1410 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1411 if (aps == 0) return PERMISSION_DENIED;
1412 return aps->setMasterMono(mono);
1413}
1414
1415status_t AudioSystem::getMasterMono(bool *mono)
1416{
1417 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1418 if (aps == 0) return PERMISSION_DENIED;
1419 return aps->getMasterMono(mono);
1420}
1421
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001422status_t AudioSystem::setMasterBalance(float balance)
1423{
1424 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1425 if (af == 0) return PERMISSION_DENIED;
1426 return af->setMasterBalance(balance);
1427}
1428
1429status_t AudioSystem::getMasterBalance(float *balance)
1430{
1431 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1432 if (af == 0) return PERMISSION_DENIED;
1433 return af->getMasterBalance(balance);
1434}
1435
Eric Laurentac9cef52017-06-09 15:46:26 -07001436float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1437{
1438 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1439 if (aps == 0) return NAN;
1440 return aps->getStreamVolumeDB(stream, index, device);
1441}
1442
jiabin46a76fa2018-01-05 10:18:21 -08001443status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1444{
1445 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1446 if (af == 0) return PERMISSION_DENIED;
1447 return af->getMicrophones(microphones);
1448}
1449
Eric Laurent42896a02019-09-27 15:40:33 -07001450status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1451 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1452 if (af == nullptr) return PERMISSION_DENIED;
1453 return af->setAudioHalPids(pids);
1454}
1455
jiabin81772902018-04-02 17:52:27 -07001456status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1457 audio_format_t *surroundFormats,
1458 bool *surroundFormatsEnabled,
1459 bool reported)
1460{
1461 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1462 if (aps == 0) return PERMISSION_DENIED;
1463 return aps->getSurroundFormats(
1464 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1465}
1466
1467status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1468{
1469 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1470 if (aps == 0) return PERMISSION_DENIED;
1471 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1472}
1473
Eric Laurentb78763e2018-10-17 10:08:02 -07001474status_t AudioSystem::setAssistantUid(uid_t uid)
1475{
1476 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1477 if (aps == 0) return PERMISSION_DENIED;
1478
1479 return aps->setAssistantUid(uid);
1480}
1481
1482status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1483{
1484 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1485 if (aps == 0) return PERMISSION_DENIED;
1486
1487 return aps->setA11yServicesUids(uids);
1488}
1489
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001490status_t AudioSystem::setCurrentImeUid(uid_t uid)
1491{
1492 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1493 if (aps == 0) return PERMISSION_DENIED;
1494
1495 return aps->setCurrentImeUid(uid);
1496}
1497
jiabin6012f912018-11-02 17:06:30 -07001498bool AudioSystem::isHapticPlaybackSupported()
1499{
1500 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1501 if (aps == 0) return false;
1502 return aps->isHapticPlaybackSupported();
1503}
1504
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001505status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001506 std::vector<audio_format_t> *formats) {
1507 const sp <IAudioPolicyService>
1508 & aps = AudioSystem::get_audio_policy_service();
1509 if (aps == 0) return PERMISSION_DENIED;
1510 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1511}
1512
1513status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001514{
1515 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1516 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001517 return aps->listAudioProductStrategies(strategies);
1518}
1519
1520audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1521{
1522 AudioProductStrategyVector strategies;
1523 listAudioProductStrategies(strategies);
1524 for (const auto &strategy : strategies) {
1525 auto attrVect = strategy.getAudioAttributes();
1526 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1527 return attributes.getStreamType() == stream; });
1528 if (iter != end(attrVect)) {
1529 return iter->getAttributes();
1530 }
1531 }
1532 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1533 return AUDIO_ATTRIBUTES_INITIALIZER;
1534}
1535
1536audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1537{
François Gaffie4b2018b2018-11-07 11:18:59 +01001538 product_strategy_t psId;
1539 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1540 if (ret != NO_ERROR) {
1541 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1542 return AUDIO_STREAM_MUSIC;
1543 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001544 AudioProductStrategyVector strategies;
1545 listAudioProductStrategies(strategies);
1546 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001547 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001548 auto attrVect = strategy.getAudioAttributes();
1549 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1550 return AudioProductStrategy::attributesMatches(
1551 refAttr.getAttributes(), attr); });
1552 if (iter != end(attrVect)) {
1553 return iter->getStreamType();
1554 }
1555 }
1556 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001557 switch (attr.usage) {
1558 case AUDIO_USAGE_VIRTUAL_SOURCE:
1559 // virtual source is not expected to have an associated product strategy
1560 break;
1561 default:
1562 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1563 break;
1564 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001565 return AUDIO_STREAM_MUSIC;
1566}
1567
François Gaffie4b2018b2018-11-07 11:18:59 +01001568status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1569 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001570{
1571 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001572 if (aps == 0) return PERMISSION_DENIED;
1573 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1574}
1575
1576status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1577{
1578 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1579 if (aps == 0) return PERMISSION_DENIED;
1580 return aps->listAudioVolumeGroups(groups);
1581}
1582
1583status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1584 volume_group_t &volumeGroup)
1585{
1586 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1587 if (aps == 0) return PERMISSION_DENIED;
1588 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001589}
Eric Laurentb78763e2018-10-17 10:08:02 -07001590
Eric Laurent6ede98f2019-06-11 14:50:30 -07001591status_t AudioSystem::setRttEnabled(bool enabled)
1592{
1593 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1594 if (aps == 0) return PERMISSION_DENIED;
1595 return aps->setRttEnabled(enabled);
1596}
1597
Eric Laurent8340e672019-11-06 11:01:08 -08001598bool AudioSystem::isCallScreenModeSupported()
1599{
1600 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1601 if (aps == 0) return false;
1602 return aps->isCallScreenModeSupported();
1603}
1604
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001605status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1606 const AudioDeviceTypeAddr &device)
1607{
1608 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1609 if (aps == 0) {
1610 return PERMISSION_DENIED;
1611 }
1612 return aps->setPreferredDeviceForStrategy(strategy, device);
1613}
1614
1615status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1616{
1617 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1618 if (aps == 0) {
1619 return PERMISSION_DENIED;
1620 }
1621 return aps->removePreferredDeviceForStrategy(strategy);
1622}
1623
1624status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1625 AudioDeviceTypeAddr &device)
1626{
1627 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1628 if (aps == 0) {
1629 return PERMISSION_DENIED;
1630 }
1631 return aps->getPreferredDeviceForStrategy(strategy, device);
1632}
1633
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001634class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1635 public IBinder::DeathRecipient {
1636public:
1637 binder::Status setCaptureState(bool active) override {
1638 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1639 gSoundTriggerCaptureStateListener->onStateChanged(active);
1640 return binder::Status::ok();
1641 }
1642
1643 void binderDied(const wp<IBinder>&) override {
1644 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1645 gSoundTriggerCaptureStateListener->onServiceDied();
1646 gSoundTriggerCaptureStateListener = nullptr;
1647 }
1648};
1649
1650status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1651 const sp<CaptureStateListener>& listener) {
1652 const sp<IAudioPolicyService>& aps =
1653 AudioSystem::get_audio_policy_service();
1654 if (aps == 0) {
1655 return PERMISSION_DENIED;
1656 }
1657
1658 sp<CaptureStateListenerImpl> wrapper = new CaptureStateListenerImpl();
1659
1660 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
1661
1662 bool active;
1663 status_t status =
1664 aps->registerSoundTriggerCaptureStateListener(wrapper, &active);
1665 if (status != NO_ERROR) {
1666 listener->onServiceDied();
1667 return NO_ERROR;
1668 }
1669 gSoundTriggerCaptureStateListener = listener;
1670 listener->onStateChanged(active);
1671 sp<IBinder> binder = IInterface::asBinder(aps);
1672 binder->linkToDeath(wrapper);
1673 return NO_ERROR;
1674}
1675
Eric Laurentc2f1f072009-07-17 12:17:14 -07001676// ---------------------------------------------------------------------------
1677
Eric Laurente8726fe2015-06-26 09:39:24 -07001678int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001679 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001680{
1681 Mutex::Autolock _l(mLock);
1682 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001683 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001684 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001685 }
1686 }
Eric Laurent296fb132015-05-01 11:38:42 -07001687 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001688 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001689}
1690
Eric Laurente8726fe2015-06-26 09:39:24 -07001691int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001692 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001693{
1694 Mutex::Autolock _l(mLock);
1695 size_t i;
1696 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001697 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001698 break;
1699 }
1700 }
1701 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001702 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001703 }
1704 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001705 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001706}
1707
Eric Laurent296fb132015-05-01 11:38:42 -07001708
Eric Laurentb28753e2015-04-01 13:06:28 -07001709void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1710{
1711 Mutex::Autolock _l(mLock);
1712 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1713 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1714 }
1715}
1716
1717void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1718{
1719 Mutex::Autolock _l(mLock);
1720 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1721 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1722 }
1723}
1724
François Gaffiecfe17322018-11-07 13:41:29 +01001725// ----------------------------------------------------------------------------
1726int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1727 const sp<AudioVolumeGroupCallback>& callback)
1728{
1729 Mutex::Autolock _l(mLock);
1730 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1731 if (mAudioVolumeGroupCallback[i] == callback) {
1732 return -1;
1733 }
1734 }
1735 mAudioVolumeGroupCallback.add(callback);
1736 return mAudioVolumeGroupCallback.size();
1737}
1738
1739int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1740 const sp<AudioVolumeGroupCallback>& callback)
1741{
1742 Mutex::Autolock _l(mLock);
1743 size_t i;
1744 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1745 if (mAudioVolumeGroupCallback[i] == callback) {
1746 break;
1747 }
1748 }
1749 if (i == mAudioVolumeGroupCallback.size()) {
1750 return -1;
1751 }
1752 mAudioVolumeGroupCallback.removeAt(i);
1753 return mAudioVolumeGroupCallback.size();
1754}
1755
1756void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1757 int flags)
1758{
1759 Mutex::Autolock _l(mLock);
1760 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1761 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1762 }
1763}
1764// ----------------------------------------------------------------------------
1765
Jean-Michel Trivide801052015-04-14 19:10:14 -07001766void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1767 String8 regId, int32_t state)
1768{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001769 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1770 dynamic_policy_callback cb = NULL;
1771 {
1772 Mutex::Autolock _l(AudioSystem::gLock);
1773 cb = gDynPolicyCallback;
1774 }
1775
1776 if (cb != NULL) {
1777 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1778 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001779}
1780
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001781void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001782 int event,
1783 const record_client_info_t *clientInfo,
1784 const audio_config_base_t *clientConfig,
1785 std::vector<effect_descriptor_t> clientEffects,
1786 const audio_config_base_t *deviceConfig,
1787 std::vector<effect_descriptor_t> effects,
1788 audio_patch_handle_t patchHandle,
1789 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001790 record_config_callback cb = NULL;
1791 {
1792 Mutex::Autolock _l(AudioSystem::gLock);
1793 cb = gRecordConfigCallback;
1794 }
1795
1796 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001797 cb(event, clientInfo, clientConfig, clientEffects,
1798 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001799 }
1800}
1801
Glenn Kasten4944acb2013-08-19 08:39:20 -07001802void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1803{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001804 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001805 Mutex::Autolock _l(mLock);
1806 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1807 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001808 }
François Gaffiecfe17322018-11-07 13:41:29 +01001809 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1810 mAudioVolumeGroupCallback[i]->onServiceDied();
1811 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001812 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001813 {
1814 Mutex::Autolock _l(gLockAPS);
1815 AudioSystem::gAudioPolicyService.clear();
1816 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001817
Steve Block5ff1dd52012-01-05 23:22:43 +00001818 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001819}
1820
Glenn Kasten40bc9062015-03-20 09:09:33 -07001821} // namespace android