blob: 2c202218555cf94adead31b5a429b085d80af330 [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.
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -070050class CaptureStateListenerImpl;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070051Mutex gSoundTriggerCaptureStateListenerLock;
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -070052sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070053
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080055const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080057 sp<IAudioFlinger> af;
58 sp<AudioFlingerClient> afc;
Mikhail Naganov69330d42020-04-08 19:29:50 +000059 bool reportNoError = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -080060 {
61 Mutex::Autolock _l(gLock);
62 if (gAudioFlinger == 0) {
63 sp<IServiceManager> sm = defaultServiceManager();
64 sp<IBinder> binder;
65 do {
66 binder = sm->getService(String16("media.audio_flinger"));
67 if (binder != 0)
68 break;
69 ALOGW("AudioFlinger not published, waiting...");
70 usleep(500000); // 0.5 s
71 } while (true);
72 if (gAudioFlingerClient == NULL) {
73 gAudioFlingerClient = new AudioFlingerClient();
74 } else {
Mikhail Naganov69330d42020-04-08 19:29:50 +000075 reportNoError = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080077 binder->linkToDeath(gAudioFlingerClient);
78 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
79 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
80 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070081 // Make sure callbacks can be received by gAudioFlingerClient
82 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070083 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080084 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080086 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020087 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080088 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020089 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080090 }
Mikhail Naganov69330d42020-04-08 19:29:50 +000091 if (reportNoError) reportError(NO_ERROR);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080092 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093}
94
Eric Laurent296fb132015-05-01 11:38:42 -070095const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
96{
97 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
98 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
99 if (af == 0) return 0;
100 Mutex::Autolock _l(gLock);
101 return gAudioFlingerClient;
102}
103
104sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
105{
106 sp<AudioIoDescriptor> desc;
107 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
108 if (afc != 0) {
109 desc = afc->getIoDescriptor(ioHandle);
110 }
111 return desc;
112}
113
Eric Laurent46291612013-07-18 14:38:44 -0700114/* static */ status_t AudioSystem::checkAudioFlinger()
115{
116 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
117 return NO_ERROR;
118 }
119 return DEAD_OBJECT;
120}
121
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700122// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
123
Glenn Kasten4944acb2013-08-19 08:39:20 -0700124status_t AudioSystem::muteMicrophone(bool state)
125{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
127 if (af == 0) return PERMISSION_DENIED;
128 return af->setMicMute(state);
129}
130
Glenn Kasten4944acb2013-08-19 08:39:20 -0700131status_t AudioSystem::isMicrophoneMuted(bool* state)
132{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
134 if (af == 0) return PERMISSION_DENIED;
135 *state = af->getMicMute();
136 return NO_ERROR;
137}
138
139status_t AudioSystem::setMasterVolume(float value)
140{
141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
143 af->setMasterVolume(value);
144 return NO_ERROR;
145}
146
147status_t AudioSystem::setMasterMute(bool mute)
148{
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
151 af->setMasterMute(mute);
152 return NO_ERROR;
153}
154
155status_t AudioSystem::getMasterVolume(float* volume)
156{
157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
159 *volume = af->masterVolume();
160 return NO_ERROR;
161}
162
163status_t AudioSystem::getMasterMute(bool* mute)
164{
165 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
166 if (af == 0) return PERMISSION_DENIED;
167 *mute = af->masterMute();
168 return NO_ERROR;
169}
170
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800171status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
172 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700177 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 return NO_ERROR;
179}
180
Glenn Kastenfff6d712012-01-12 16:38:12 -0800181status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182{
Dima Zavinfce7a472011-04-19 22:30:36 -0700183 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
185 if (af == 0) return PERMISSION_DENIED;
186 af->setStreamMute(stream, mute);
187 return NO_ERROR;
188}
189
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800190status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
191 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192{
Dima Zavinfce7a472011-04-19 22:30:36 -0700193 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
195 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700196 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197 return NO_ERROR;
198}
199
Glenn Kastenfff6d712012-01-12 16:38:12 -0800200status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201{
Dima Zavinfce7a472011-04-19 22:30:36 -0700202 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
204 if (af == 0) return PERMISSION_DENIED;
205 *mute = af->streamMute(stream);
206 return NO_ERROR;
207}
208
Glenn Kastenf78aee72012-01-04 11:00:47 -0800209status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800211 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
213 if (af == 0) return PERMISSION_DENIED;
214 return af->setMode(mode);
215}
216
Glenn Kasten4944acb2013-08-19 08:39:20 -0700217status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
218{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
220 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700221 return af->setParameters(ioHandle, keyValuePairs);
222}
223
Glenn Kasten4944acb2013-08-19 08:39:20 -0700224String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
225{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700226 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
227 String8 result = String8("");
228 if (af == 0) return result;
229
230 result = af->getParameters(ioHandle, keys);
231 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232}
233
Glenn Kastenc23885e2013-12-19 16:35:18 -0800234status_t AudioSystem::setParameters(const String8& keyValuePairs)
235{
Glenn Kasten142f5192014-03-25 17:44:59 -0700236 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800237}
238
239String8 AudioSystem::getParameters(const String8& keys)
240{
Glenn Kasten142f5192014-03-25 17:44:59 -0700241 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800242}
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244// convert volume steps to natural log scale
245
246// change this value to change volume scaling
247static const float dBPerStep = 0.5f;
248// shouldn't need to touch these
249static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
250static const float dBConvertInverse = 1.0f / dBConvert;
251
252float AudioSystem::linearToLog(int volume)
253{
254 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000255 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 // return v;
257 return volume ? exp(float(100 - volume) * dBConvert) : 0;
258}
259
260int AudioSystem::logToLinear(float volume)
261{
262 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000263 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 // return v;
265 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
266}
267
Eric Laurent21da6472017-11-09 16:29:26 -0800268/* static */ size_t AudioSystem::calculateMinFrameCount(
269 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
270 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
271{
272 // Ensure that buffer depth covers at least audio hardware latency
273 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
274 if (minBufCount < 2) {
275 minBufCount = 2;
276 }
277#if 0
278 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
279 // but keeping the code here to make it easier to add later.
280 if (minBufCount < notificationsPerBufferReq) {
281 minBufCount = notificationsPerBufferReq;
282 }
283#endif
284 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
285 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
286 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
287 /*, notificationsPerBufferReq*/);
288 return minBufCount * sourceFramesNeededWithTimestretch(
289 sampleRate, afFrameCount, afSampleRate, speed);
290}
291
292
Glenn Kasten3b16c762012-11-14 08:44:39 -0800293status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296
Dima Zavinfce7a472011-04-19 22:30:36 -0700297 if (streamType == AUDIO_STREAM_DEFAULT) {
298 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 }
300
Glenn Kastenfff6d712012-01-12 16:38:12 -0800301 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302 if (output == 0) {
303 return PERMISSION_DENIED;
304 }
305
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700306 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700307}
308
Glenn Kasten2c073da2016-02-26 09:14:08 -0800309status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800310 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700311{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800312 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
313 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800314 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
315 if (desc == 0) {
316 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800318 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700319 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800320 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800321 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800322 return BAD_VALUE;
323 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324
Glenn Kasten2c073da2016-02-26 09:14:08 -0800325 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 return NO_ERROR;
328}
329
Glenn Kastene33054e2012-11-14 12:54:39 -0800330status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 if (streamType == AUDIO_STREAM_DEFAULT) {
335 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700337
Glenn Kastenfff6d712012-01-12 16:38:12 -0800338 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700339 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 return PERMISSION_DENIED;
341 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700343 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700344}
345
Glenn Kasten2c073da2016-02-26 09:14:08 -0800346status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800347 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700348{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
350 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800351 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
352 if (desc == 0) {
353 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800355 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800357 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800358 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800359 return BAD_VALUE;
360 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361
Glenn Kasten2c073da2016-02-26 09:14:08 -0800362 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700363
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 return NO_ERROR;
365}
366
Glenn Kastenfff6d712012-01-12 16:38:12 -0800367status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370
Dima Zavinfce7a472011-04-19 22:30:36 -0700371 if (streamType == AUDIO_STREAM_DEFAULT) {
372 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700373 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700374
Glenn Kastenfff6d712012-01-12 16:38:12 -0800375 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700376 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700377 return PERMISSION_DENIED;
378 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379
Glenn Kasten241618f2014-03-25 17:48:57 -0700380 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700381}
382
383status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700384 uint32_t* latency)
385{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800386 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
387 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700388 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700389 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700390 *latency = af->latency(output);
391 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700392 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700393 }
394
Glenn Kasten241618f2014-03-25 17:48:57 -0700395 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700396
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 return NO_ERROR;
398}
399
Glenn Kastendd8104c2012-07-02 12:42:44 -0700400status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
401 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402{
Eric Laurent296fb132015-05-01 11:38:42 -0700403 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
404 if (afc == 0) {
405 return NO_INIT;
406 }
407 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408}
409
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700410status_t AudioSystem::setVoiceVolume(float value)
411{
412 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
413 if (af == 0) return PERMISSION_DENIED;
414 return af->setVoiceVolume(value);
415}
416
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000417status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700418 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800419{
420 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
421 if (af == 0) return PERMISSION_DENIED;
422
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000423 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800424}
425
Glenn Kasten4944acb2013-08-19 08:39:20 -0700426uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
427{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800429 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800430 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700431 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800432
433 result = af->getInputFramesLost(ioHandle);
434 return result;
435}
436
Glenn Kasteneeecb982016-02-26 10:44:04 -0800437audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700438{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700439 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700441 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800442 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700443}
444
Andy Hung8b0bfd92019-12-23 13:11:11 -0800445void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700446{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700447 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
448 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800449 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700450 }
451}
452
Glenn Kastend848eb42016-03-08 13:42:11 -0800453void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700454{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700455 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800457 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700458 }
459}
460
Eric Laurent93c3d412014-08-01 14:48:35 -0700461audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
462{
463 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
464 if (af == 0) return AUDIO_HW_SYNC_INVALID;
465 return af->getAudioHwSyncForSession(sessionId);
466}
467
Eric Laurent72e3f392015-05-20 14:43:50 -0700468status_t AudioSystem::systemReady()
469{
470 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
471 if (af == 0) return NO_INIT;
472 return af->systemReady();
473}
474
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700475status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
476 size_t* frameCount)
477{
478 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
479 if (af == 0) return PERMISSION_DENIED;
480 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
481 if (desc == 0) {
482 *frameCount = af->frameCountHAL(ioHandle);
483 } else {
484 *frameCount = desc->mFrameCountHAL;
485 }
486 if (*frameCount == 0) {
487 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
488 return BAD_VALUE;
489 }
490
491 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
492
493 return NO_ERROR;
494}
495
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496// ---------------------------------------------------------------------------
497
Eric Laurent73e26b62015-04-27 16:55:58 -0700498
499void AudioSystem::AudioFlingerClient::clearIoCache()
500{
501 Mutex::Autolock _l(mLock);
502 mIoDescriptors.clear();
503 mInBuffSize = 0;
504 mInSamplingRate = 0;
505 mInFormat = AUDIO_FORMAT_DEFAULT;
506 mInChannelMask = AUDIO_CHANNEL_NONE;
507}
508
Glenn Kasten4944acb2013-08-19 08:39:20 -0700509void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
510{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800511 {
512 Mutex::Autolock _l(AudioSystem::gLock);
513 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800514 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515
Eric Laurent73e26b62015-04-27 16:55:58 -0700516 // clear output handles and stream to output map caches
517 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800519 reportError(DEAD_OBJECT);
520
Steve Block5ff1dd52012-01-05 23:22:43 +0000521 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522}
523
Eric Laurent73e26b62015-04-27 16:55:58 -0700524void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
525 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100526 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527
Eric Laurent73e26b62015-04-27 16:55:58 -0700528 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700529
Eric Laurent296fb132015-05-01 11:38:42 -0700530 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700531 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700532 {
533 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700534 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700535
536 switch (event) {
537 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700538 case AUDIO_OUTPUT_REGISTERED:
539 case AUDIO_INPUT_OPENED:
540 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700541 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700542 if (oldDesc == 0) {
543 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
544 } else {
545 deviceId = oldDesc->getDeviceId();
546 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700547 }
Eric Laurent296fb132015-05-01 11:38:42 -0700548
549 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
550 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700551 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700552 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
553 if (it != mAudioDeviceCallbacks.end()) {
554 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100555 }
556 }
Eric Laurent296fb132015-05-01 11:38:42 -0700557 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700558 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
559 "frameCount %zu deviceId %d",
560 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
561 "output" : "input",
562 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
563 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700564 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
565 ioDesc->mFrameCount, ioDesc->getDeviceId());
566 } break;
567 case AUDIO_OUTPUT_CLOSED:
568 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700569 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700570 ALOGW("ioConfigChanged() closing unknown %s %d",
571 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
572 break;
573 }
574 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700575 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700576
Eric Laurent296fb132015-05-01 11:38:42 -0700577 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700578 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700579 } break;
580
581 case AUDIO_OUTPUT_CONFIG_CHANGED:
582 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700583 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700584 if (oldDesc == 0) {
Dean Wheatley8f992a12020-05-08 20:33:51 +1000585 ALOGW("ioConfigChanged() modifying unknown %s! %d",
586 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700587 break;
588 }
589
590 deviceId = oldDesc->getDeviceId();
591 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
592
593 if (deviceId != ioDesc->getDeviceId()) {
594 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700595 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
596 if (it != mAudioDeviceCallbacks.end()) {
597 callbacks = it->second;
598 }
Eric Laurent296fb132015-05-01 11:38:42 -0700599 }
600 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700601 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700602 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
603 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800604 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
605 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700606
Eric Laurentc2f1f072009-07-17 12:17:14 -0700607 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700608 case AUDIO_CLIENT_STARTED: {
609 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
610 if (oldDesc == 0) {
611 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
612 break;
613 }
614 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
615 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
616 oldDesc->mPatch = ioDesc->mPatch;
617 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
618 if (it != mAudioDeviceCallbacks.end()) {
619 auto cbks = it->second;
620 auto it2 = cbks.find(ioDesc->mPortId);
621 if (it2 != cbks.end()) {
622 callbacks.emplace(ioDesc->mPortId, it2->second);
623 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100624 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100625 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700626 } break;
627 }
628
629 for (auto wpCbk : callbacks) {
630 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
631 if (spCbk != nullptr) {
632 callbacksToCall.push_back(spCbk);
633 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700634 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800635 }
636
637 // Callbacks must be called without mLock held. May lead to dead lock if calling for
638 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700639 for (auto cb : callbacksToCall) {
640 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
641 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700642 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643}
644
Eric Laurent73e26b62015-04-27 16:55:58 -0700645status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
646 uint32_t sampleRate, audio_format_t format,
647 audio_channel_mask_t channelMask, size_t* buffSize)
648{
649 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
650 if (af == 0) {
651 return PERMISSION_DENIED;
652 }
653 Mutex::Autolock _l(mLock);
654 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
655 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
656 || (channelMask != mInChannelMask)) {
657 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
658 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800659 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700660 sampleRate, format, channelMask);
661 return BAD_VALUE;
662 }
663 // A benign race is possible here: we could overwrite a fresher cache entry
664 // save the request params
665 mInSamplingRate = sampleRate;
666 mInFormat = format;
667 mInChannelMask = channelMask;
668
669 mInBuffSize = inBuffSize;
670 }
671
672 *buffSize = mInBuffSize;
673
674 return NO_ERROR;
675}
676
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700677sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700678{
679 sp<AudioIoDescriptor> desc;
680 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
681 if (index >= 0) {
682 desc = mIoDescriptors.valueAt(index);
683 }
684 return desc;
685}
686
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700687sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
688{
689 Mutex::Autolock _l(mLock);
690 return getIoDescriptor_l(ioHandle);
691}
692
Eric Laurent296fb132015-05-01 11:38:42 -0700693status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700694 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
695 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700696{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700697 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800698 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700699 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
700 auto result = callbacks.try_emplace(portId, callback);
701 if (!result.second) {
702 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700703 }
Eric Laurent296fb132015-05-01 11:38:42 -0700704 return NO_ERROR;
705}
706
707status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700708 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
709 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700710{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700711 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800712 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700713 auto it = mAudioDeviceCallbacks.find(audioIo);
714 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700715 return INVALID_OPERATION;
716 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700717 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700718 return INVALID_OPERATION;
719 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700720 if (it->second.size() == 0) {
721 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700722 }
723 return NO_ERROR;
724}
725
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800726/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700727{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800728 Mutex::Autolock _l(gLockErrorCallbacks);
729 gAudioErrorCallbacks.insert(cb);
730 return reinterpret_cast<uintptr_t>(cb);
731}
732
733/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
734 Mutex::Autolock _l(gLockErrorCallbacks);
735 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
736}
737
738/* static */ void AudioSystem::reportError(status_t err) {
739 Mutex::Autolock _l(gLockErrorCallbacks);
740 for (auto callback : gAudioErrorCallbacks) {
741 callback(err);
742 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743}
744
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700745/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
746{
747 Mutex::Autolock _l(gLock);
748 gDynPolicyCallback = cb;
749}
750
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800751/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
752{
753 Mutex::Autolock _l(gLock);
754 gRecordConfigCallback = cb;
755}
756
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800758// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700759sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
760sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
761
762
Glenn Kasten18a6d902012-09-24 11:27:56 -0700763// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800764const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700765{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800766 sp<IAudioPolicyService> ap;
767 sp<AudioPolicyServiceClient> apc;
768 {
769 Mutex::Autolock _l(gLockAPS);
770 if (gAudioPolicyService == 0) {
771 sp<IServiceManager> sm = defaultServiceManager();
772 sp<IBinder> binder;
773 do {
774 binder = sm->getService(String16("media.audio_policy"));
775 if (binder != 0)
776 break;
777 ALOGW("AudioPolicyService not published, waiting...");
778 usleep(500000); // 0.5 s
779 } while (true);
780 if (gAudioPolicyServiceClient == NULL) {
781 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
782 }
783 binder->linkToDeath(gAudioPolicyServiceClient);
784 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
785 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
786 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700787 // Make sure callbacks can be received by gAudioPolicyServiceClient
788 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700789 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800790 ap = gAudioPolicyService;
791 }
792 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200793 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800794 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200795 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100796 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200797 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700798 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800799
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800800 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700801}
802
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700803// ---------------------------------------------------------------------------
804
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000805void AudioSystem::onNewAudioModulesAvailable()
806{
807 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
808 if (aps == 0) return;
809 aps->onNewAudioModulesAvailable();
810}
811
Dima Zavinfce7a472011-04-19 22:30:36 -0700812status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
813 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800814 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800815 const char *device_name,
816 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700817{
818 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700819 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800820 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700821
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822 if (aps == 0) return PERMISSION_DENIED;
823
Eric Laurent71b63e32011-09-02 14:20:56 -0700824 if (device_address != NULL) {
825 address = device_address;
826 }
Paul McLeane743a472015-01-28 11:07:31 -0800827 if (device_name != NULL) {
828 name = device_name;
829 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800830 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700831}
832
Dima Zavinfce7a472011-04-19 22:30:36 -0700833audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700834 const char *device_address)
835{
836 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700837 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700838
839 return aps->getDeviceConnectionState(device, device_address);
840}
841
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800842status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
843 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800844 const char *device_name,
845 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800846{
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 const char *address = "";
849 const char *name = "";
850
851 if (aps == 0) return PERMISSION_DENIED;
852
853 if (device_address != NULL) {
854 address = device_address;
855 }
856 if (device_name != NULL) {
857 name = device_name;
858 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800859 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800860}
861
Eric Laurent00dba062020-02-11 15:52:09 -0800862status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700863{
Glenn Kasten347966c2012-01-18 14:58:32 -0800864 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700865 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
866 if (aps == 0) return PERMISSION_DENIED;
867
Eric Laurent00dba062020-02-11 15:52:09 -0800868 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700869}
870
Dima Zavinfce7a472011-04-19 22:30:36 -0700871status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700872{
873 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
874 if (aps == 0) return PERMISSION_DENIED;
875 return aps->setForceUse(usage, config);
876}
877
Dima Zavinfce7a472011-04-19 22:30:36 -0700878audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700879{
880 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700881 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882 return aps->getForceUse(usage);
883}
884
885
Eric Laurentf4e63452017-11-06 19:31:46 +0000886audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700888 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
889 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000890 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700891}
892
Eric Laurent42984412019-05-09 17:57:03 -0700893status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800894 audio_io_handle_t *output,
895 audio_session_t session,
896 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200897 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700898 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800899 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800900 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700901 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800902 audio_port_handle_t *portId,
903 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700904{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800906 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200907 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700908 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800909 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700910}
911
Eric Laurentd7fe0862018-07-14 16:48:01 -0700912status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700913{
914 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
915 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700916 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700917}
918
Eric Laurentd7fe0862018-07-14 16:48:01 -0700919status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920{
921 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
922 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700923 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700924}
925
Eric Laurentd7fe0862018-07-14 16:48:01 -0700926void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700927{
928 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
929 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700930 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700931}
932
Eric Laurentcaf7f482014-11-25 17:50:47 -0800933status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
934 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700935 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800936 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700937 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700938 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800939 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800940 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600941 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700942 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800943 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800946 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600947 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700948 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800949 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700950}
951
Eric Laurent4eb58f12018-12-07 16:41:02 -0800952status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953{
954 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
955 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800956 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957}
958
Eric Laurentfee19762018-01-29 18:44:13 -0800959status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960{
961 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
962 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800963 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700964}
965
Eric Laurentfee19762018-01-29 18:44:13 -0800966void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700967{
968 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
969 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800970 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700971}
972
Dima Zavinfce7a472011-04-19 22:30:36 -0700973status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974 int indexMin,
975 int indexMax)
976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
979 return aps->initStreamVolume(stream, indexMin, indexMax);
980}
981
Eric Laurent83844cc2011-11-18 16:43:31 -0800982status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
983 int index,
984 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985{
986 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
987 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800988 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700989}
990
Eric Laurent83844cc2011-11-18 16:43:31 -0800991status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
992 int *index,
993 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994{
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
996 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800997 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998}
999
François Gaffiecfe17322018-11-07 13:41:29 +01001000status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
1001 int index,
1002 audio_devices_t device)
1003{
1004 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1005 if (aps == 0) return PERMISSION_DENIED;
1006 return aps->setVolumeIndexForAttributes(attr, index, device);
1007}
1008
1009status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1010 int &index,
1011 audio_devices_t device)
1012{
1013 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1014 if (aps == 0) return PERMISSION_DENIED;
1015 return aps->getVolumeIndexForAttributes(attr, index, device);
1016}
1017
1018status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1019{
1020 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1021 if (aps == 0) return PERMISSION_DENIED;
1022 return aps->getMaxVolumeIndexForAttributes(attr, index);
1023}
1024
1025status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1026{
1027 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1028 if (aps == 0) return PERMISSION_DENIED;
1029 return aps->getMinVolumeIndexForAttributes(attr, index);
1030}
1031
Dima Zavinfce7a472011-04-19 22:30:36 -07001032uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001033{
1034 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001035 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001036 return aps->getStrategyForStream(stream);
1037}
1038
Eric Laurent63742522012-03-08 13:42:42 -08001039audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001040{
1041 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001042 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001043 return aps->getDevicesForStream(stream);
1044}
1045
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001046status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1047 AudioDeviceTypeAddrVector *devices) {
1048 if (devices == nullptr) {
1049 return BAD_VALUE;
1050 }
1051 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1052 if (aps == 0) return PERMISSION_DENIED;
1053 return aps->getDevicesForAttributes(aa, devices);
1054}
1055
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001056audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001057{
1058 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001059 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001060 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001061 return aps->getOutputForEffect(desc);
1062}
1063
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001064status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001065 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001066 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001067 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001068 int id)
1069{
1070 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1071 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001072 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001073}
1074
1075status_t AudioSystem::unregisterEffect(int id)
1076{
1077 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1078 if (aps == 0) return PERMISSION_DENIED;
1079 return aps->unregisterEffect(id);
1080}
1081
Eric Laurentdb7c0792011-08-10 10:37:50 -07001082status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1083{
1084 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1085 if (aps == 0) return PERMISSION_DENIED;
1086 return aps->setEffectEnabled(id, enabled);
1087}
1088
Eric Laurent6c796322019-04-09 14:13:17 -07001089status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1090{
1091 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1092 if (aps == 0) return PERMISSION_DENIED;
1093 return aps->moveEffectsToIo(ids, io);
1094}
1095
Glenn Kastenfff6d712012-01-12 16:38:12 -08001096status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001097{
Eric Laurenteda6c362011-02-02 09:33:30 -08001098 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1099 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001100 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001101 *state = aps->isStreamActive(stream, inPastMs);
1102 return NO_ERROR;
1103}
1104
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001105status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1106 uint32_t inPastMs)
1107{
1108 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1109 if (aps == 0) return PERMISSION_DENIED;
1110 if (state == NULL) return BAD_VALUE;
1111 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1112 return NO_ERROR;
1113}
1114
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001115status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1116{
1117 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1118 if (aps == 0) return PERMISSION_DENIED;
1119 if (state == NULL) return BAD_VALUE;
1120 *state = aps->isSourceActive(stream);
1121 return NO_ERROR;
1122}
1123
Glenn Kasten3b16c762012-11-14 08:44:39 -08001124uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001125{
1126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1127 if (af == 0) return 0;
1128 return af->getPrimaryOutputSamplingRate();
1129}
1130
Glenn Kastene33054e2012-11-14 12:54:39 -08001131size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001132{
1133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1134 if (af == 0) return 0;
1135 return af->getPrimaryOutputFrameCount();
1136}
Eric Laurenteda6c362011-02-02 09:33:30 -08001137
Andy Hung6f248bb2018-01-23 14:04:37 -08001138status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001139{
1140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1141 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001142 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001143}
1144
Eric Laurent9f6530f2011-08-30 10:18:54 -07001145void AudioSystem::clearAudioConfigCache()
1146{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001147 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001148 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001149 {
1150 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001151 if (gAudioFlingerClient != 0) {
1152 gAudioFlingerClient->clearIoCache();
1153 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001154 gAudioFlinger.clear();
1155 }
1156 {
1157 Mutex::Autolock _l(gLockAPS);
1158 gAudioPolicyService.clear();
1159 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001160}
1161
Hayden Gomes524159d2019-12-23 14:41:47 -08001162status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1163 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1164 if (aps == nullptr) return PERMISSION_DENIED;
1165 return aps->setSupportedSystemUsages(systemUsages);
1166}
1167
Kevin Rocardb99cc752019-03-21 20:52:24 -07001168status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1169 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1170 if (aps == nullptr) return PERMISSION_DENIED;
1171 return aps->setAllowedCapturePolicy(uid, flags);
1172}
1173
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001174bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1175{
1176 ALOGV("isOffloadSupported()");
1177 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1178 if (aps == 0) return false;
1179 return aps->isOffloadSupported(info);
1180}
1181
Eric Laurent203b1a12014-04-01 10:34:16 -07001182status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1183 audio_port_type_t type,
1184 unsigned int *num_ports,
1185 struct audio_port *ports,
1186 unsigned int *generation)
1187{
1188 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1189 if (aps == 0) return PERMISSION_DENIED;
1190 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1191}
1192
1193status_t AudioSystem::getAudioPort(struct audio_port *port)
1194{
1195 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1196 if (aps == 0) return PERMISSION_DENIED;
1197 return aps->getAudioPort(port);
1198}
1199
1200status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1201 audio_patch_handle_t *handle)
1202{
1203 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1204 if (aps == 0) return PERMISSION_DENIED;
1205 return aps->createAudioPatch(patch, handle);
1206}
1207
1208status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1209{
1210 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1211 if (aps == 0) return PERMISSION_DENIED;
1212 return aps->releaseAudioPatch(handle);
1213}
1214
1215status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1216 struct audio_patch *patches,
1217 unsigned int *generation)
1218{
1219 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1220 if (aps == 0) return PERMISSION_DENIED;
1221 return aps->listAudioPatches(num_patches, patches, generation);
1222}
1223
1224status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1225{
1226 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1227 if (aps == 0) return PERMISSION_DENIED;
1228 return aps->setAudioPortConfig(config);
1229}
1230
Eric Laurent296fb132015-05-01 11:38:42 -07001231status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001232{
Eric Laurentb28753e2015-04-01 13:06:28 -07001233 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1234 if (aps == 0) return PERMISSION_DENIED;
1235
1236 Mutex::Autolock _l(gLockAPS);
1237 if (gAudioPolicyServiceClient == 0) {
1238 return NO_INIT;
1239 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001240 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1241 if (ret == 1) {
1242 aps->setAudioPortCallbacksEnabled(true);
1243 }
1244 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001245}
1246
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001247/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001248status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001249{
1250 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1251 if (aps == 0) return PERMISSION_DENIED;
1252
1253 Mutex::Autolock _l(gLockAPS);
1254 if (gAudioPolicyServiceClient == 0) {
1255 return NO_INIT;
1256 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001257 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1258 if (ret == 0) {
1259 aps->setAudioPortCallbacksEnabled(false);
1260 }
1261 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001262}
1263
François Gaffiecfe17322018-11-07 13:41:29 +01001264status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1265{
1266 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1267 if (aps == 0) return PERMISSION_DENIED;
1268
1269 Mutex::Autolock _l(gLockAPS);
1270 if (gAudioPolicyServiceClient == 0) {
1271 return NO_INIT;
1272 }
1273 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1274 if (ret == 1) {
1275 aps->setAudioVolumeGroupCallbacksEnabled(true);
1276 }
1277 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1278}
1279
1280status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1281{
1282 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1283 if (aps == 0) return PERMISSION_DENIED;
1284
1285 Mutex::Autolock _l(gLockAPS);
1286 if (gAudioPolicyServiceClient == 0) {
1287 return NO_INIT;
1288 }
1289 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1290 if (ret == 0) {
1291 aps->setAudioVolumeGroupCallbacksEnabled(false);
1292 }
1293 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1294}
1295
Eric Laurent296fb132015-05-01 11:38:42 -07001296status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001297 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1298 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001299{
1300 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1301 if (afc == 0) {
1302 return NO_INIT;
1303 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001304 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001305 if (status == NO_ERROR) {
1306 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1307 if (af != 0) {
1308 af->registerClient(afc);
1309 }
1310 }
1311 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001312}
1313
1314status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001315 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1316 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001317{
1318 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1319 if (afc == 0) {
1320 return NO_INIT;
1321 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001322 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001323}
1324
1325audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1326{
1327 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1328 if (af == 0) return PERMISSION_DENIED;
1329 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1330 if (desc == 0) {
1331 return AUDIO_PORT_HANDLE_NONE;
1332 }
1333 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001334}
1335
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001336status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1337 audio_io_handle_t *ioHandle,
1338 audio_devices_t *device)
1339{
1340 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1341 if (aps == 0) return PERMISSION_DENIED;
1342 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1343}
1344
1345status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1346{
1347 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1348 if (aps == 0) return PERMISSION_DENIED;
1349 return aps->releaseSoundTriggerSession(session);
1350}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001351
1352audio_mode_t AudioSystem::getPhoneState()
1353{
1354 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1355 if (aps == 0) return AUDIO_MODE_INVALID;
1356 return aps->getPhoneState();
1357}
1358
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001359status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001360{
1361 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1362 if (aps == 0) return PERMISSION_DENIED;
1363 return aps->registerPolicyMixes(mixes, registration);
1364}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001365
jiabinc1afe3b2020-08-07 11:56:38 -07001366status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices)
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001367{
1368 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1369 if (aps == 0) return PERMISSION_DENIED;
1370 return aps->setUidDeviceAffinities(uid, devices);
1371}
1372
1373status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1374 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1375 if (aps == 0) return PERMISSION_DENIED;
1376 return aps->removeUidDeviceAffinities(uid);
1377}
1378
Oscar Azucena90e77632019-11-27 17:12:28 -08001379status_t AudioSystem::setUserIdDeviceAffinities(int userId,
jiabinc1afe3b2020-08-07 11:56:38 -07001380 const AudioDeviceTypeAddrVector& devices)
Oscar Azucena90e77632019-11-27 17:12:28 -08001381{
1382 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1383 if (aps == 0) return PERMISSION_DENIED;
1384 return aps->setUserIdDeviceAffinities(userId, devices);
1385}
1386
1387status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1388{
1389 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1390 if (aps == 0) return PERMISSION_DENIED;
1391 return aps->removeUserIdDeviceAffinities(userId);
1392}
1393
Eric Laurent554a2772015-04-10 11:29:24 -07001394status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1395 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001396 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001397{
1398 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1399 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001400 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001401}
1402
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001403status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001404{
1405 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1406 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001407 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001408}
1409
Andy Hung2ddee192015-12-18 17:34:44 -08001410status_t AudioSystem::setMasterMono(bool mono)
1411{
1412 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1413 if (aps == 0) return PERMISSION_DENIED;
1414 return aps->setMasterMono(mono);
1415}
1416
1417status_t AudioSystem::getMasterMono(bool *mono)
1418{
1419 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1420 if (aps == 0) return PERMISSION_DENIED;
1421 return aps->getMasterMono(mono);
1422}
1423
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001424status_t AudioSystem::setMasterBalance(float balance)
1425{
1426 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1427 if (af == 0) return PERMISSION_DENIED;
1428 return af->setMasterBalance(balance);
1429}
1430
1431status_t AudioSystem::getMasterBalance(float *balance)
1432{
1433 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1434 if (af == 0) return PERMISSION_DENIED;
1435 return af->getMasterBalance(balance);
1436}
1437
Eric Laurentac9cef52017-06-09 15:46:26 -07001438float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1439{
1440 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1441 if (aps == 0) return NAN;
1442 return aps->getStreamVolumeDB(stream, index, device);
1443}
1444
jiabin46a76fa2018-01-05 10:18:21 -08001445status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1446{
1447 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1448 if (af == 0) return PERMISSION_DENIED;
1449 return af->getMicrophones(microphones);
1450}
1451
Eric Laurent42896a02019-09-27 15:40:33 -07001452status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1453 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1454 if (af == nullptr) return PERMISSION_DENIED;
1455 return af->setAudioHalPids(pids);
1456}
1457
jiabin81772902018-04-02 17:52:27 -07001458status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1459 audio_format_t *surroundFormats,
1460 bool *surroundFormatsEnabled,
1461 bool reported)
1462{
1463 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1464 if (aps == 0) return PERMISSION_DENIED;
1465 return aps->getSurroundFormats(
1466 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1467}
1468
1469status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1470{
1471 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1472 if (aps == 0) return PERMISSION_DENIED;
1473 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1474}
1475
Eric Laurentb78763e2018-10-17 10:08:02 -07001476status_t AudioSystem::setAssistantUid(uid_t uid)
1477{
1478 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1479 if (aps == 0) return PERMISSION_DENIED;
1480
1481 return aps->setAssistantUid(uid);
1482}
1483
1484status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1485{
1486 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1487 if (aps == 0) return PERMISSION_DENIED;
1488
1489 return aps->setA11yServicesUids(uids);
1490}
1491
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001492status_t AudioSystem::setCurrentImeUid(uid_t uid)
1493{
1494 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1495 if (aps == 0) return PERMISSION_DENIED;
1496
1497 return aps->setCurrentImeUid(uid);
1498}
1499
jiabin6012f912018-11-02 17:06:30 -07001500bool AudioSystem::isHapticPlaybackSupported()
1501{
1502 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1503 if (aps == 0) return false;
1504 return aps->isHapticPlaybackSupported();
1505}
1506
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001507status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001508 std::vector<audio_format_t> *formats) {
1509 const sp <IAudioPolicyService>
1510 & aps = AudioSystem::get_audio_policy_service();
1511 if (aps == 0) return PERMISSION_DENIED;
1512 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1513}
1514
1515status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001516{
1517 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1518 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001519 return aps->listAudioProductStrategies(strategies);
1520}
1521
1522audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1523{
1524 AudioProductStrategyVector strategies;
1525 listAudioProductStrategies(strategies);
1526 for (const auto &strategy : strategies) {
1527 auto attrVect = strategy.getAudioAttributes();
1528 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1529 return attributes.getStreamType() == stream; });
1530 if (iter != end(attrVect)) {
1531 return iter->getAttributes();
1532 }
1533 }
1534 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1535 return AUDIO_ATTRIBUTES_INITIALIZER;
1536}
1537
1538audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1539{
François Gaffie4b2018b2018-11-07 11:18:59 +01001540 product_strategy_t psId;
1541 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1542 if (ret != NO_ERROR) {
1543 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1544 return AUDIO_STREAM_MUSIC;
1545 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001546 AudioProductStrategyVector strategies;
1547 listAudioProductStrategies(strategies);
1548 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001549 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001550 auto attrVect = strategy.getAudioAttributes();
1551 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1552 return AudioProductStrategy::attributesMatches(
1553 refAttr.getAttributes(), attr); });
1554 if (iter != end(attrVect)) {
1555 return iter->getStreamType();
1556 }
1557 }
1558 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001559 switch (attr.usage) {
1560 case AUDIO_USAGE_VIRTUAL_SOURCE:
1561 // virtual source is not expected to have an associated product strategy
1562 break;
1563 default:
1564 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1565 break;
1566 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001567 return AUDIO_STREAM_MUSIC;
1568}
1569
François Gaffie4b2018b2018-11-07 11:18:59 +01001570status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1571 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001572{
1573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001574 if (aps == 0) return PERMISSION_DENIED;
1575 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1576}
1577
1578status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1579{
1580 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1581 if (aps == 0) return PERMISSION_DENIED;
1582 return aps->listAudioVolumeGroups(groups);
1583}
1584
1585status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1586 volume_group_t &volumeGroup)
1587{
1588 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1589 if (aps == 0) return PERMISSION_DENIED;
1590 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001591}
Eric Laurentb78763e2018-10-17 10:08:02 -07001592
Eric Laurent6ede98f2019-06-11 14:50:30 -07001593status_t AudioSystem::setRttEnabled(bool enabled)
1594{
1595 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1596 if (aps == 0) return PERMISSION_DENIED;
1597 return aps->setRttEnabled(enabled);
1598}
1599
Eric Laurent8340e672019-11-06 11:01:08 -08001600bool AudioSystem::isCallScreenModeSupported()
1601{
1602 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1603 if (aps == 0) return false;
1604 return aps->isCallScreenModeSupported();
1605}
1606
jiabin4e826212020-08-07 17:32:40 -07001607status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
1608 device_role_t role,
1609 const AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001610{
1611 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1612 if (aps == 0) {
1613 return PERMISSION_DENIED;
1614 }
jiabin4e826212020-08-07 17:32:40 -07001615 return aps->setDevicesRoleForStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001616}
1617
jiabin4e826212020-08-07 17:32:40 -07001618status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001619{
1620 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1621 if (aps == 0) {
1622 return PERMISSION_DENIED;
1623 }
jiabin4e826212020-08-07 17:32:40 -07001624 return aps->removeDevicesRoleForStrategy(strategy, role);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001625}
1626
jiabin4e826212020-08-07 17:32:40 -07001627status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
1628 device_role_t role,
1629 AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001630{
1631 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1632 if (aps == 0) {
1633 return PERMISSION_DENIED;
1634 }
jiabin4e826212020-08-07 17:32:40 -07001635 return aps->getDevicesForRoleAndStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001636}
1637
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001638class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1639 public IBinder::DeathRecipient {
1640public:
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001641 CaptureStateListenerImpl(
1642 const sp<IAudioPolicyService>& aps,
1643 const sp<AudioSystem::CaptureStateListener>& listener)
1644 : mAps(aps), mListener(listener) {}
1645
1646 void init() {
1647 bool active;
1648 status_t status = mAps->registerSoundTriggerCaptureStateListener(this, &active);
1649 if (status != NO_ERROR) {
1650 mListener->onServiceDied();
1651 return;
1652 }
1653 mListener->onStateChanged(active);
1654 IInterface::asBinder(mAps)->linkToDeath(this);
1655 }
1656
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001657 binder::Status setCaptureState(bool active) override {
1658 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001659 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001660 return binder::Status::ok();
1661 }
1662
1663 void binderDied(const wp<IBinder>&) override {
1664 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001665 mListener->onServiceDied();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001666 gSoundTriggerCaptureStateListener = nullptr;
1667 }
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001668
1669private:
1670 // Need this in order to keep the death receipent alive.
1671 sp<IAudioPolicyService> mAps;
1672 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001673};
1674
1675status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1676 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001677 LOG_ALWAYS_FATAL_IF(listener == nullptr);
1678
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001679 const sp<IAudioPolicyService>& aps =
1680 AudioSystem::get_audio_policy_service();
1681 if (aps == 0) {
1682 return PERMISSION_DENIED;
1683 }
1684
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001685 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvi067cd6c2020-08-27 09:31:10 -07001686 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
1687 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001688
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001689 return NO_ERROR;
1690}
1691
Eric Laurentc2f1f072009-07-17 12:17:14 -07001692// ---------------------------------------------------------------------------
1693
Eric Laurente8726fe2015-06-26 09:39:24 -07001694int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001695 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001696{
1697 Mutex::Autolock _l(mLock);
1698 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001699 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001700 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001701 }
1702 }
Eric Laurent296fb132015-05-01 11:38:42 -07001703 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001704 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001705}
1706
Eric Laurente8726fe2015-06-26 09:39:24 -07001707int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001708 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001709{
1710 Mutex::Autolock _l(mLock);
1711 size_t i;
1712 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001713 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001714 break;
1715 }
1716 }
1717 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001718 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001719 }
1720 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001721 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001722}
1723
Eric Laurent296fb132015-05-01 11:38:42 -07001724
Eric Laurentb28753e2015-04-01 13:06:28 -07001725void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1726{
1727 Mutex::Autolock _l(mLock);
1728 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1729 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1730 }
1731}
1732
1733void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1734{
1735 Mutex::Autolock _l(mLock);
1736 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1737 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1738 }
1739}
1740
François Gaffiecfe17322018-11-07 13:41:29 +01001741// ----------------------------------------------------------------------------
1742int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1743 const sp<AudioVolumeGroupCallback>& callback)
1744{
1745 Mutex::Autolock _l(mLock);
1746 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1747 if (mAudioVolumeGroupCallback[i] == callback) {
1748 return -1;
1749 }
1750 }
1751 mAudioVolumeGroupCallback.add(callback);
1752 return mAudioVolumeGroupCallback.size();
1753}
1754
1755int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1756 const sp<AudioVolumeGroupCallback>& callback)
1757{
1758 Mutex::Autolock _l(mLock);
1759 size_t i;
1760 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1761 if (mAudioVolumeGroupCallback[i] == callback) {
1762 break;
1763 }
1764 }
1765 if (i == mAudioVolumeGroupCallback.size()) {
1766 return -1;
1767 }
1768 mAudioVolumeGroupCallback.removeAt(i);
1769 return mAudioVolumeGroupCallback.size();
1770}
1771
1772void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1773 int flags)
1774{
1775 Mutex::Autolock _l(mLock);
1776 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1777 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1778 }
1779}
1780// ----------------------------------------------------------------------------
1781
Jean-Michel Trivide801052015-04-14 19:10:14 -07001782void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1783 String8 regId, int32_t state)
1784{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001785 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1786 dynamic_policy_callback cb = NULL;
1787 {
1788 Mutex::Autolock _l(AudioSystem::gLock);
1789 cb = gDynPolicyCallback;
1790 }
1791
1792 if (cb != NULL) {
1793 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1794 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001795}
1796
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001797void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001798 int event,
1799 const record_client_info_t *clientInfo,
1800 const audio_config_base_t *clientConfig,
1801 std::vector<effect_descriptor_t> clientEffects,
1802 const audio_config_base_t *deviceConfig,
1803 std::vector<effect_descriptor_t> effects,
1804 audio_patch_handle_t patchHandle,
1805 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001806 record_config_callback cb = NULL;
1807 {
1808 Mutex::Autolock _l(AudioSystem::gLock);
1809 cb = gRecordConfigCallback;
1810 }
1811
1812 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001813 cb(event, clientInfo, clientConfig, clientEffects,
1814 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001815 }
1816}
1817
Glenn Kasten4944acb2013-08-19 08:39:20 -07001818void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1819{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001820 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001821 Mutex::Autolock _l(mLock);
1822 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1823 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001824 }
François Gaffiecfe17322018-11-07 13:41:29 +01001825 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1826 mAudioVolumeGroupCallback[i]->onServiceDied();
1827 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001828 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001829 {
1830 Mutex::Autolock _l(gLockAPS);
1831 AudioSystem::gAudioPolicyService.clear();
1832 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001833
Steve Block5ff1dd52012-01-05 23:22:43 +00001834 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001835}
1836
Glenn Kasten40bc9062015-03-20 09:09:33 -07001837} // namespace android