blob: 0ce251344fac28010133e9084962b9524e8f1138 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070022#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020023#include <binder/IPCThreadState.h>
Eric Laurent21da6472017-11-09 16:29:26 -080024#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070026#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070027#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010028#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029#include <math.h>
30
Dima Zavin64760242011-05-11 14:15:23 -070031#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Eric Laurentc2f1f072009-07-17 12:17:14 -070033// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035namespace android {
36
37// client singleton for AudioFlinger binder interface
38Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080039Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040sp<IAudioFlinger> AudioSystem::gAudioFlinger;
41sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
42audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070043dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080044record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080047const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080049 sp<IAudioFlinger> af;
50 sp<AudioFlingerClient> afc;
51 {
52 Mutex::Autolock _l(gLock);
53 if (gAudioFlinger == 0) {
54 sp<IServiceManager> sm = defaultServiceManager();
55 sp<IBinder> binder;
56 do {
57 binder = sm->getService(String16("media.audio_flinger"));
58 if (binder != 0)
59 break;
60 ALOGW("AudioFlinger not published, waiting...");
61 usleep(500000); // 0.5 s
62 } while (true);
63 if (gAudioFlingerClient == NULL) {
64 gAudioFlingerClient = new AudioFlingerClient();
65 } else {
66 if (gAudioErrorCallback) {
67 gAudioErrorCallback(NO_ERROR);
68 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080070 binder->linkToDeath(gAudioFlingerClient);
71 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
72 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
73 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070074 // Make sure callbacks can be received by gAudioFlingerClient
75 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080077 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080079 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020080 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080081 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020082 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080083 }
84 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085}
86
Eric Laurent296fb132015-05-01 11:38:42 -070087const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
88{
89 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
90 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
91 if (af == 0) return 0;
92 Mutex::Autolock _l(gLock);
93 return gAudioFlingerClient;
94}
95
96sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
97{
98 sp<AudioIoDescriptor> desc;
99 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
100 if (afc != 0) {
101 desc = afc->getIoDescriptor(ioHandle);
102 }
103 return desc;
104}
105
Eric Laurent46291612013-07-18 14:38:44 -0700106/* static */ status_t AudioSystem::checkAudioFlinger()
107{
108 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
109 return NO_ERROR;
110 }
111 return DEAD_OBJECT;
112}
113
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700114// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
115
Glenn Kasten4944acb2013-08-19 08:39:20 -0700116status_t AudioSystem::muteMicrophone(bool state)
117{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
119 if (af == 0) return PERMISSION_DENIED;
120 return af->setMicMute(state);
121}
122
Glenn Kasten4944acb2013-08-19 08:39:20 -0700123status_t AudioSystem::isMicrophoneMuted(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 *state = af->getMicMute();
128 return NO_ERROR;
129}
130
131status_t AudioSystem::setMasterVolume(float value)
132{
133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
134 if (af == 0) return PERMISSION_DENIED;
135 af->setMasterVolume(value);
136 return NO_ERROR;
137}
138
139status_t AudioSystem::setMasterMute(bool mute)
140{
141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
143 af->setMasterMute(mute);
144 return NO_ERROR;
145}
146
147status_t AudioSystem::getMasterVolume(float* volume)
148{
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
151 *volume = af->masterVolume();
152 return NO_ERROR;
153}
154
155status_t AudioSystem::getMasterMute(bool* mute)
156{
157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
159 *mute = af->masterMute();
160 return NO_ERROR;
161}
162
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800163status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
164 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165{
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700169 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 return NO_ERROR;
171}
172
Glenn Kastenfff6d712012-01-12 16:38:12 -0800173status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174{
Dima Zavinfce7a472011-04-19 22:30:36 -0700175 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
177 if (af == 0) return PERMISSION_DENIED;
178 af->setStreamMute(stream, mute);
179 return NO_ERROR;
180}
181
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800182status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
183 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184{
Dima Zavinfce7a472011-04-19 22:30:36 -0700185 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
187 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700188 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189 return NO_ERROR;
190}
191
Glenn Kastenfff6d712012-01-12 16:38:12 -0800192status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193{
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
196 if (af == 0) return PERMISSION_DENIED;
197 *mute = af->streamMute(stream);
198 return NO_ERROR;
199}
200
Glenn Kastenf78aee72012-01-04 11:00:47 -0800201status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800203 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
205 if (af == 0) return PERMISSION_DENIED;
206 return af->setMode(mode);
207}
208
Glenn Kasten4944acb2013-08-19 08:39:20 -0700209status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
210{
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;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700213 return af->setParameters(ioHandle, keyValuePairs);
214}
215
Glenn Kasten4944acb2013-08-19 08:39:20 -0700216String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
217{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700218 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
219 String8 result = String8("");
220 if (af == 0) return result;
221
222 result = af->getParameters(ioHandle, keys);
223 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224}
225
Glenn Kastenc23885e2013-12-19 16:35:18 -0800226status_t AudioSystem::setParameters(const String8& keyValuePairs)
227{
Glenn Kasten142f5192014-03-25 17:44:59 -0700228 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800229}
230
231String8 AudioSystem::getParameters(const String8& keys)
232{
Glenn Kasten142f5192014-03-25 17:44:59 -0700233 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800234}
235
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236// convert volume steps to natural log scale
237
238// change this value to change volume scaling
239static const float dBPerStep = 0.5f;
240// shouldn't need to touch these
241static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
242static const float dBConvertInverse = 1.0f / dBConvert;
243
244float AudioSystem::linearToLog(int volume)
245{
246 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000247 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 // return v;
249 return volume ? exp(float(100 - volume) * dBConvert) : 0;
250}
251
252int AudioSystem::logToLinear(float volume)
253{
254 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000255 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 // return v;
257 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
258}
259
Eric Laurent21da6472017-11-09 16:29:26 -0800260/* static */ size_t AudioSystem::calculateMinFrameCount(
261 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
262 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
263{
264 // Ensure that buffer depth covers at least audio hardware latency
265 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
266 if (minBufCount < 2) {
267 minBufCount = 2;
268 }
269#if 0
270 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
271 // but keeping the code here to make it easier to add later.
272 if (minBufCount < notificationsPerBufferReq) {
273 minBufCount = notificationsPerBufferReq;
274 }
275#endif
276 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
277 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
278 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
279 /*, notificationsPerBufferReq*/);
280 return minBufCount * sourceFramesNeededWithTimestretch(
281 sampleRate, afFrameCount, afSampleRate, speed);
282}
283
284
Glenn Kasten3b16c762012-11-14 08:44:39 -0800285status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288
Dima Zavinfce7a472011-04-19 22:30:36 -0700289 if (streamType == AUDIO_STREAM_DEFAULT) {
290 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 }
292
Glenn Kastenfff6d712012-01-12 16:38:12 -0800293 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 if (output == 0) {
295 return PERMISSION_DENIED;
296 }
297
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700298 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700299}
300
Glenn Kasten2c073da2016-02-26 09:14:08 -0800301status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800302 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700303{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800304 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
305 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800306 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
307 if (desc == 0) {
308 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800310 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800312 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800313 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800314 return BAD_VALUE;
315 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316
Glenn Kasten2c073da2016-02-26 09:14:08 -0800317 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 return NO_ERROR;
320}
321
Glenn Kastene33054e2012-11-14 12:54:39 -0800322status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325
Dima Zavinfce7a472011-04-19 22:30:36 -0700326 if (streamType == AUDIO_STREAM_DEFAULT) {
327 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700329
Glenn Kastenfff6d712012-01-12 16:38:12 -0800330 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700331 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 return PERMISSION_DENIED;
333 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700335 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700336}
337
Glenn Kasten2c073da2016-02-26 09:14:08 -0800338status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800339 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700340{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800341 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
342 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800343 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
344 if (desc == 0) {
345 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800347 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800349 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800350 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800351 return BAD_VALUE;
352 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353
Glenn Kasten2c073da2016-02-26 09:14:08 -0800354 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 return NO_ERROR;
357}
358
Glenn Kastenfff6d712012-01-12 16:38:12 -0800359status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (streamType == AUDIO_STREAM_DEFAULT) {
364 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700366
Glenn Kastenfff6d712012-01-12 16:38:12 -0800367 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700368 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369 return PERMISSION_DENIED;
370 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371
Glenn Kasten241618f2014-03-25 17:48:57 -0700372 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700373}
374
375status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700376 uint32_t* latency)
377{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700380 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700381 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 *latency = af->latency(output);
383 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700384 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 }
386
Glenn Kasten241618f2014-03-25 17:48:57 -0700387 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 return NO_ERROR;
390}
391
Glenn Kastendd8104c2012-07-02 12:42:44 -0700392status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
393 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394{
Eric Laurent296fb132015-05-01 11:38:42 -0700395 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
396 if (afc == 0) {
397 return NO_INIT;
398 }
399 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400}
401
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700402status_t AudioSystem::setVoiceVolume(float value)
403{
404 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
405 if (af == 0) return PERMISSION_DENIED;
406 return af->setVoiceVolume(value);
407}
408
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000409status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700410 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800411{
412 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
413 if (af == 0) return PERMISSION_DENIED;
414
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000415 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800416}
417
Glenn Kasten4944acb2013-08-19 08:39:20 -0700418uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
419{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800420 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800421 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800422 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700423 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800424
425 result = af->getInputFramesLost(ioHandle);
426 return result;
427}
428
Glenn Kasteneeecb982016-02-26 10:44:04 -0800429audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700430{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700431 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700432 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800433 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700434}
435
Glenn Kastend848eb42016-03-08 13:42:11 -0800436void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700437{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700438 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
439 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800440 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700441 }
442}
443
Glenn Kastend848eb42016-03-08 13:42:11 -0800444void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700445{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700446 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
447 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800448 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700449 }
450}
451
Eric Laurent93c3d412014-08-01 14:48:35 -0700452audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
453{
454 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
455 if (af == 0) return AUDIO_HW_SYNC_INVALID;
456 return af->getAudioHwSyncForSession(sessionId);
457}
458
Eric Laurent72e3f392015-05-20 14:43:50 -0700459status_t AudioSystem::systemReady()
460{
461 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
462 if (af == 0) return NO_INIT;
463 return af->systemReady();
464}
465
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700466status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
467 size_t* frameCount)
468{
469 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
470 if (af == 0) return PERMISSION_DENIED;
471 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
472 if (desc == 0) {
473 *frameCount = af->frameCountHAL(ioHandle);
474 } else {
475 *frameCount = desc->mFrameCountHAL;
476 }
477 if (*frameCount == 0) {
478 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
479 return BAD_VALUE;
480 }
481
482 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
483
484 return NO_ERROR;
485}
486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487// ---------------------------------------------------------------------------
488
Eric Laurent73e26b62015-04-27 16:55:58 -0700489
490void AudioSystem::AudioFlingerClient::clearIoCache()
491{
492 Mutex::Autolock _l(mLock);
493 mIoDescriptors.clear();
494 mInBuffSize = 0;
495 mInSamplingRate = 0;
496 mInFormat = AUDIO_FORMAT_DEFAULT;
497 mInChannelMask = AUDIO_CHANNEL_NONE;
498}
499
Glenn Kasten4944acb2013-08-19 08:39:20 -0700500void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
501{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800502 audio_error_callback cb = NULL;
503 {
504 Mutex::Autolock _l(AudioSystem::gLock);
505 AudioSystem::gAudioFlinger.clear();
506 cb = gAudioErrorCallback;
507 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508
Eric Laurent73e26b62015-04-27 16:55:58 -0700509 // clear output handles and stream to output map caches
510 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511
Eric Laurentf6778fd2014-11-18 17:26:58 -0800512 if (cb) {
513 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000515 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516}
517
Eric Laurent73e26b62015-04-27 16:55:58 -0700518void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
519 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521
Eric Laurent73e26b62015-04-27 16:55:58 -0700522 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523
Eric Laurent296fb132015-05-01 11:38:42 -0700524 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent4463ff52019-02-07 13:56:09 -0800525 Vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700526 {
527 Mutex::Autolock _l(mLock);
Eric Laurent4463ff52019-02-07 13:56:09 -0800528 bool deviceValidOrChanged = false;
529 bool sendCallbacks = false;
530 ssize_t ioIndex = -1;
Eric Laurent296fb132015-05-01 11:38:42 -0700531
532 switch (event) {
533 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700534 case AUDIO_OUTPUT_REGISTERED:
535 case AUDIO_INPUT_OPENED:
536 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700537 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700538 if (oldDesc == 0) {
539 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
540 } else {
541 deviceId = oldDesc->getDeviceId();
542 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700543 }
Eric Laurent296fb132015-05-01 11:38:42 -0700544
545 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
546 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700547 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent4463ff52019-02-07 13:56:09 -0800548 ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700549 if (ioIndex >= 0) {
Eric Laurent4463ff52019-02-07 13:56:09 -0800550 sendCallbacks = true;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100551 deviceValidOrChanged = true;
552 }
553 }
554 if (event == AUDIO_OUTPUT_REGISTERED || event == AUDIO_INPUT_REGISTERED) {
Eric Laurent4463ff52019-02-07 13:56:09 -0800555 ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle);
556 sendCallbacks = (ioIndex >= 0)
557 && !mAudioDeviceCallbackProxies.valueAt(ioIndex).notifiedOnce();
Eric Laurent296fb132015-05-01 11:38:42 -0700558 }
559 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700560 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
561 "frameCount %zu deviceId %d",
562 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
563 "output" : "input",
564 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
565 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700566 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
567 ioDesc->mFrameCount, ioDesc->getDeviceId());
568 } break;
569 case AUDIO_OUTPUT_CLOSED:
570 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700571 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700572 ALOGW("ioConfigChanged() closing unknown %s %d",
573 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
574 break;
575 }
576 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700577 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700578
Eric Laurent296fb132015-05-01 11:38:42 -0700579 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent4463ff52019-02-07 13:56:09 -0800580 mAudioDeviceCallbackProxies.removeItem(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700581 } break;
582
583 case AUDIO_OUTPUT_CONFIG_CHANGED:
584 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700585 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700586 if (oldDesc == 0) {
587 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
588 break;
589 }
590
591 deviceId = oldDesc->getDeviceId();
592 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
593
594 if (deviceId != ioDesc->getDeviceId()) {
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100595 deviceValidOrChanged = true;
Eric Laurent296fb132015-05-01 11:38:42 -0700596 deviceId = ioDesc->getDeviceId();
Eric Laurent4463ff52019-02-07 13:56:09 -0800597 ioIndex = mAudioDeviceCallbackProxies.indexOfKey(ioDesc->mIoHandle);
598 sendCallbacks = ioIndex >= 0;
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 Laurentc2f1f072009-07-17 12:17:14 -0700608 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800609
610 // sendCallbacks true => ioDesc->mIoHandle and deviceId are valid
611 if (sendCallbacks) {
612 AudioDeviceCallbackProxies &callbackProxies =
613 mAudioDeviceCallbackProxies.editValueAt(ioIndex);
614 for (size_t i = 0; i < callbackProxies.size(); ) {
615 sp<AudioDeviceCallback> callback = callbackProxies[i]->callback();
616 if (callback.get() != nullptr) {
617 // Call the callback only if the device actually changed, the input or output
618 // was opened or closed or the client was newly registered and the callback
619 // was never called
620 if (!callbackProxies[i]->notifiedOnce() || deviceValidOrChanged) {
621 callbacksToCall.add(callback);
622 callbackProxies[i]->setNotifiedOnce();
623 }
624 i++;
625 } else {
626 callbackProxies.removeAt(i);
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100627 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100628 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800629 callbackProxies.setNotifiedOnce();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700630 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800631 }
632
633 // Callbacks must be called without mLock held. May lead to dead lock if calling for
634 // example getRoutedDevice that updates the device and tries to acquire mLock.
635 for (size_t i = 0; i < callbacksToCall.size(); i++) {
636 callbacksToCall[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700637 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638}
639
Eric Laurent73e26b62015-04-27 16:55:58 -0700640status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
641 uint32_t sampleRate, audio_format_t format,
642 audio_channel_mask_t channelMask, size_t* buffSize)
643{
644 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
645 if (af == 0) {
646 return PERMISSION_DENIED;
647 }
648 Mutex::Autolock _l(mLock);
649 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
650 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
651 || (channelMask != mInChannelMask)) {
652 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
653 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800654 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700655 sampleRate, format, channelMask);
656 return BAD_VALUE;
657 }
658 // A benign race is possible here: we could overwrite a fresher cache entry
659 // save the request params
660 mInSamplingRate = sampleRate;
661 mInFormat = format;
662 mInChannelMask = channelMask;
663
664 mInBuffSize = inBuffSize;
665 }
666
667 *buffSize = mInBuffSize;
668
669 return NO_ERROR;
670}
671
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700672sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700673{
674 sp<AudioIoDescriptor> desc;
675 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
676 if (index >= 0) {
677 desc = mIoDescriptors.valueAt(index);
678 }
679 return desc;
680}
681
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700682sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
683{
684 Mutex::Autolock _l(mLock);
685 return getIoDescriptor_l(ioHandle);
686}
687
Eric Laurent296fb132015-05-01 11:38:42 -0700688status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700689 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700690{
Eric Laurent4463ff52019-02-07 13:56:09 -0800691 Mutex::Autolock _l(mLock);
692 AudioDeviceCallbackProxies callbackProxies;
693 ssize_t ioIndex = mAudioDeviceCallbackProxies.indexOfKey(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700694 if (ioIndex >= 0) {
Eric Laurent4463ff52019-02-07 13:56:09 -0800695 callbackProxies = mAudioDeviceCallbackProxies.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700696 }
697
Eric Laurent4463ff52019-02-07 13:56:09 -0800698 for (size_t cbIndex = 0; cbIndex < callbackProxies.size(); cbIndex++) {
699 sp<AudioDeviceCallback> cbk = callbackProxies[cbIndex]->callback();
700 if (cbk.get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700701 return INVALID_OPERATION;
702 }
703 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800704 callbackProxies.add(new AudioDeviceCallbackProxy(callback));
705 callbackProxies.resetNotifiedOnce();
706 mAudioDeviceCallbackProxies.replaceValueFor(audioIo, callbackProxies);
Eric Laurent296fb132015-05-01 11:38:42 -0700707 return NO_ERROR;
708}
709
710status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700711 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700712{
Eric Laurent4463ff52019-02-07 13:56:09 -0800713 Mutex::Autolock _l(mLock);
714 ssize_t ioIndex = mAudioDeviceCallbackProxies.indexOfKey(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700715 if (ioIndex < 0) {
716 return INVALID_OPERATION;
717 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800718 AudioDeviceCallbackProxies callbackProxies = mAudioDeviceCallbackProxies.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700719 size_t cbIndex;
Eric Laurent4463ff52019-02-07 13:56:09 -0800720 for (cbIndex = 0; cbIndex < callbackProxies.size(); cbIndex++) {
721 sp<AudioDeviceCallback> cbk = callbackProxies[cbIndex]->callback();
722 if (cbk.get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700723 break;
724 }
725 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800726 if (cbIndex == callbackProxies.size()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700727 return INVALID_OPERATION;
728 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800729 callbackProxies.removeAt(cbIndex);
730 if (callbackProxies.size() != 0) {
731 mAudioDeviceCallbackProxies.replaceValueFor(audioIo, callbackProxies);
Eric Laurent296fb132015-05-01 11:38:42 -0700732 } else {
Eric Laurent4463ff52019-02-07 13:56:09 -0800733 mAudioDeviceCallbackProxies.removeItem(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700734 }
735 return NO_ERROR;
736}
737
738/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700739{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700740 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741 gAudioErrorCallback = cb;
742}
743
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700744/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
745{
746 Mutex::Autolock _l(gLock);
747 gDynPolicyCallback = cb;
748}
749
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800750/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
751{
752 Mutex::Autolock _l(gLock);
753 gRecordConfigCallback = cb;
754}
755
Eric Laurentc2f1f072009-07-17 12:17:14 -0700756// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800757// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700758sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
759sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
760
761
Glenn Kasten18a6d902012-09-24 11:27:56 -0700762// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800763const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700764{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800765 sp<IAudioPolicyService> ap;
766 sp<AudioPolicyServiceClient> apc;
767 {
768 Mutex::Autolock _l(gLockAPS);
769 if (gAudioPolicyService == 0) {
770 sp<IServiceManager> sm = defaultServiceManager();
771 sp<IBinder> binder;
772 do {
773 binder = sm->getService(String16("media.audio_policy"));
774 if (binder != 0)
775 break;
776 ALOGW("AudioPolicyService not published, waiting...");
777 usleep(500000); // 0.5 s
778 } while (true);
779 if (gAudioPolicyServiceClient == NULL) {
780 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
781 }
782 binder->linkToDeath(gAudioPolicyServiceClient);
783 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
784 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
785 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700786 // Make sure callbacks can be received by gAudioPolicyServiceClient
787 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700788 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800789 ap = gAudioPolicyService;
790 }
791 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200792 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800793 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200794 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100795 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200796 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700797 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800798
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800799 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700800}
801
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700802// ---------------------------------------------------------------------------
803
Dima Zavinfce7a472011-04-19 22:30:36 -0700804status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
805 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800806 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800807 const char *device_name,
808 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700809{
810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700811 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800812 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700813
Eric Laurentc2f1f072009-07-17 12:17:14 -0700814 if (aps == 0) return PERMISSION_DENIED;
815
Eric Laurent71b63e32011-09-02 14:20:56 -0700816 if (device_address != NULL) {
817 address = device_address;
818 }
Paul McLeane743a472015-01-28 11:07:31 -0800819 if (device_name != NULL) {
820 name = device_name;
821 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800822 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700823}
824
Dima Zavinfce7a472011-04-19 22:30:36 -0700825audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700826 const char *device_address)
827{
828 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700829 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830
831 return aps->getDeviceConnectionState(device, device_address);
832}
833
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800834status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
835 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800836 const char *device_name,
837 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800838{
839 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
840 const char *address = "";
841 const char *name = "";
842
843 if (aps == 0) return PERMISSION_DENIED;
844
845 if (device_address != NULL) {
846 address = device_address;
847 }
848 if (device_name != NULL) {
849 name = device_name;
850 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800851 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800852}
853
Glenn Kastenf78aee72012-01-04 11:00:47 -0800854status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855{
Glenn Kasten347966c2012-01-18 14:58:32 -0800856 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700857 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
858 if (aps == 0) return PERMISSION_DENIED;
859
860 return aps->setPhoneState(state);
861}
862
Dima Zavinfce7a472011-04-19 22:30:36 -0700863status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700864{
865 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
866 if (aps == 0) return PERMISSION_DENIED;
867 return aps->setForceUse(usage, config);
868}
869
Dima Zavinfce7a472011-04-19 22:30:36 -0700870audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700871{
872 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700873 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700874 return aps->getForceUse(usage);
875}
876
877
Eric Laurentf4e63452017-11-06 19:31:46 +0000878audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700879{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700880 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
881 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000882 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700883}
884
Eric Laurente83b55d2014-11-14 10:06:21 -0800885status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
886 audio_io_handle_t *output,
887 audio_session_t session,
888 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200889 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700890 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800891 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800892 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700893 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800894 audio_port_handle_t *portId,
895 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700896{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800898 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200899 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800900 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800901 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700902}
903
Eric Laurentd7fe0862018-07-14 16:48:01 -0700904status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700905{
906 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
907 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700908 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700909}
910
Eric Laurentd7fe0862018-07-14 16:48:01 -0700911status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700912{
913 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
914 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700915 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700916}
917
Eric Laurentd7fe0862018-07-14 16:48:01 -0700918void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700919{
920 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
921 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700922 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700923}
924
Eric Laurentcaf7f482014-11-25 17:50:47 -0800925status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
926 audio_io_handle_t *input,
927 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700928 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700929 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800930 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800931 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600932 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700933 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800934 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700935{
936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800937 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600938 return aps->getInputForAttr(
Eric Laurentfee19762018-01-29 18:44:13 -0800939 attr, input, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800940 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941}
942
Eric Laurent4eb58f12018-12-07 16:41:02 -0800943status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
946 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800947 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948}
949
Eric Laurentfee19762018-01-29 18:44:13 -0800950status_t AudioSystem::stopInput(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 Laurentfee19762018-01-29 18:44:13 -0800954 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955}
956
Eric Laurentfee19762018-01-29 18:44:13 -0800957void AudioSystem::releaseInput(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;
Eric Laurentfee19762018-01-29 18:44:13 -0800961 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962}
963
Dima Zavinfce7a472011-04-19 22:30:36 -0700964status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965 int indexMin,
966 int indexMax)
967{
968 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
969 if (aps == 0) return PERMISSION_DENIED;
970 return aps->initStreamVolume(stream, indexMin, indexMax);
971}
972
Eric Laurent83844cc2011-11-18 16:43:31 -0800973status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
974 int index,
975 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800979 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980}
981
Eric Laurent83844cc2011-11-18 16:43:31 -0800982status_t AudioSystem::getStreamVolumeIndex(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->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700989}
990
François Gaffiecfe17322018-11-07 13:41:29 +0100991status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
992 int index,
993 audio_devices_t device)
994{
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
996 if (aps == 0) return PERMISSION_DENIED;
997 return aps->setVolumeIndexForAttributes(attr, index, device);
998}
999
1000status_t AudioSystem::getVolumeIndexForAttributes(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->getVolumeIndexForAttributes(attr, index, device);
1007}
1008
1009status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1010{
1011 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1012 if (aps == 0) return PERMISSION_DENIED;
1013 return aps->getMaxVolumeIndexForAttributes(attr, index);
1014}
1015
1016status_t AudioSystem::getMinVolumeIndexForAttributes(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->getMinVolumeIndexForAttributes(attr, index);
1021}
1022
Dima Zavinfce7a472011-04-19 22:30:36 -07001023uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001024{
1025 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001026 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001027 return aps->getStrategyForStream(stream);
1028}
1029
Eric Laurent63742522012-03-08 13:42:42 -08001030audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001031{
1032 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001033 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001034 return aps->getDevicesForStream(stream);
1035}
1036
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001037audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001038{
1039 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001040 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001041 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001042 return aps->getOutputForEffect(desc);
1043}
1044
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001045status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001046 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001047 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001048 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001049 int id)
1050{
1051 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1052 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001053 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001054}
1055
1056status_t AudioSystem::unregisterEffect(int id)
1057{
1058 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1059 if (aps == 0) return PERMISSION_DENIED;
1060 return aps->unregisterEffect(id);
1061}
1062
Eric Laurentdb7c0792011-08-10 10:37:50 -07001063status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1064{
1065 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1066 if (aps == 0) return PERMISSION_DENIED;
1067 return aps->setEffectEnabled(id, enabled);
1068}
1069
Glenn Kastenfff6d712012-01-12 16:38:12 -08001070status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001071{
Eric Laurenteda6c362011-02-02 09:33:30 -08001072 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1073 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001074 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001075 *state = aps->isStreamActive(stream, inPastMs);
1076 return NO_ERROR;
1077}
1078
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001079status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1080 uint32_t inPastMs)
1081{
1082 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1083 if (aps == 0) return PERMISSION_DENIED;
1084 if (state == NULL) return BAD_VALUE;
1085 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1086 return NO_ERROR;
1087}
1088
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001089status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1090{
1091 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1092 if (aps == 0) return PERMISSION_DENIED;
1093 if (state == NULL) return BAD_VALUE;
1094 *state = aps->isSourceActive(stream);
1095 return NO_ERROR;
1096}
1097
Glenn Kasten3b16c762012-11-14 08:44:39 -08001098uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001099{
1100 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1101 if (af == 0) return 0;
1102 return af->getPrimaryOutputSamplingRate();
1103}
1104
Glenn Kastene33054e2012-11-14 12:54:39 -08001105size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001106{
1107 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1108 if (af == 0) return 0;
1109 return af->getPrimaryOutputFrameCount();
1110}
Eric Laurenteda6c362011-02-02 09:33:30 -08001111
Andy Hung6f248bb2018-01-23 14:04:37 -08001112status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001113{
1114 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1115 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001116 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001117}
1118
Eric Laurent9f6530f2011-08-30 10:18:54 -07001119void AudioSystem::clearAudioConfigCache()
1120{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001121 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001123 {
1124 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001125 if (gAudioFlingerClient != 0) {
1126 gAudioFlingerClient->clearIoCache();
1127 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001128 gAudioFlinger.clear();
1129 }
1130 {
1131 Mutex::Autolock _l(gLockAPS);
1132 gAudioPolicyService.clear();
1133 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001134}
1135
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001136bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1137{
1138 ALOGV("isOffloadSupported()");
1139 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1140 if (aps == 0) return false;
1141 return aps->isOffloadSupported(info);
1142}
1143
Eric Laurent203b1a12014-04-01 10:34:16 -07001144status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1145 audio_port_type_t type,
1146 unsigned int *num_ports,
1147 struct audio_port *ports,
1148 unsigned int *generation)
1149{
1150 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1151 if (aps == 0) return PERMISSION_DENIED;
1152 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1153}
1154
1155status_t AudioSystem::getAudioPort(struct audio_port *port)
1156{
1157 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1158 if (aps == 0) return PERMISSION_DENIED;
1159 return aps->getAudioPort(port);
1160}
1161
1162status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1163 audio_patch_handle_t *handle)
1164{
1165 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1166 if (aps == 0) return PERMISSION_DENIED;
1167 return aps->createAudioPatch(patch, handle);
1168}
1169
1170status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1171{
1172 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1173 if (aps == 0) return PERMISSION_DENIED;
1174 return aps->releaseAudioPatch(handle);
1175}
1176
1177status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1178 struct audio_patch *patches,
1179 unsigned int *generation)
1180{
1181 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1182 if (aps == 0) return PERMISSION_DENIED;
1183 return aps->listAudioPatches(num_patches, patches, generation);
1184}
1185
1186status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1187{
1188 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1189 if (aps == 0) return PERMISSION_DENIED;
1190 return aps->setAudioPortConfig(config);
1191}
1192
Eric Laurent296fb132015-05-01 11:38:42 -07001193status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001194{
Eric Laurentb28753e2015-04-01 13:06:28 -07001195 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1196 if (aps == 0) return PERMISSION_DENIED;
1197
1198 Mutex::Autolock _l(gLockAPS);
1199 if (gAudioPolicyServiceClient == 0) {
1200 return NO_INIT;
1201 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001202 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1203 if (ret == 1) {
1204 aps->setAudioPortCallbacksEnabled(true);
1205 }
1206 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001207}
1208
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001209/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001210status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001211{
1212 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1213 if (aps == 0) return PERMISSION_DENIED;
1214
1215 Mutex::Autolock _l(gLockAPS);
1216 if (gAudioPolicyServiceClient == 0) {
1217 return NO_INIT;
1218 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001219 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1220 if (ret == 0) {
1221 aps->setAudioPortCallbacksEnabled(false);
1222 }
1223 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001224}
1225
François Gaffiecfe17322018-11-07 13:41:29 +01001226status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1227{
1228 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1229 if (aps == 0) return PERMISSION_DENIED;
1230
1231 Mutex::Autolock _l(gLockAPS);
1232 if (gAudioPolicyServiceClient == 0) {
1233 return NO_INIT;
1234 }
1235 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1236 if (ret == 1) {
1237 aps->setAudioVolumeGroupCallbacksEnabled(true);
1238 }
1239 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1240}
1241
1242status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1243{
1244 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1245 if (aps == 0) return PERMISSION_DENIED;
1246
1247 Mutex::Autolock _l(gLockAPS);
1248 if (gAudioPolicyServiceClient == 0) {
1249 return NO_INIT;
1250 }
1251 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1252 if (ret == 0) {
1253 aps->setAudioVolumeGroupCallbacksEnabled(false);
1254 }
1255 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1256}
1257
Eric Laurent296fb132015-05-01 11:38:42 -07001258status_t AudioSystem::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001259 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001260{
1261 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1262 if (afc == 0) {
1263 return NO_INIT;
1264 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001265 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1266 if (status == NO_ERROR) {
1267 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1268 if (af != 0) {
1269 af->registerClient(afc);
1270 }
1271 }
1272 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001273}
1274
1275status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001276 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001277{
1278 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1279 if (afc == 0) {
1280 return NO_INIT;
1281 }
1282 return afc->removeAudioDeviceCallback(callback, audioIo);
1283}
1284
1285audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1286{
1287 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1288 if (af == 0) return PERMISSION_DENIED;
1289 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1290 if (desc == 0) {
1291 return AUDIO_PORT_HANDLE_NONE;
1292 }
1293 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001294}
1295
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001296status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1297 audio_io_handle_t *ioHandle,
1298 audio_devices_t *device)
1299{
1300 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1301 if (aps == 0) return PERMISSION_DENIED;
1302 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1303}
1304
1305status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1306{
1307 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1308 if (aps == 0) return PERMISSION_DENIED;
1309 return aps->releaseSoundTriggerSession(session);
1310}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001311
1312audio_mode_t AudioSystem::getPhoneState()
1313{
1314 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1315 if (aps == 0) return AUDIO_MODE_INVALID;
1316 return aps->getPhoneState();
1317}
1318
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001319status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001320{
1321 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1322 if (aps == 0) return PERMISSION_DENIED;
1323 return aps->registerPolicyMixes(mixes, registration);
1324}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001325
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001326status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1327{
1328 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1329 if (aps == 0) return PERMISSION_DENIED;
1330 return aps->setUidDeviceAffinities(uid, devices);
1331}
1332
1333status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1334 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1335 if (aps == 0) return PERMISSION_DENIED;
1336 return aps->removeUidDeviceAffinities(uid);
1337}
1338
Eric Laurent554a2772015-04-10 11:29:24 -07001339status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1340 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001341 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001342{
1343 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1344 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001345 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001346}
1347
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001348status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001349{
1350 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1351 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001352 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001353}
1354
Andy Hung2ddee192015-12-18 17:34:44 -08001355status_t AudioSystem::setMasterMono(bool mono)
1356{
1357 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1358 if (aps == 0) return PERMISSION_DENIED;
1359 return aps->setMasterMono(mono);
1360}
1361
1362status_t AudioSystem::getMasterMono(bool *mono)
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->getMasterMono(mono);
1367}
1368
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001369status_t AudioSystem::setMasterBalance(float balance)
1370{
1371 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1372 if (af == 0) return PERMISSION_DENIED;
1373 return af->setMasterBalance(balance);
1374}
1375
1376status_t AudioSystem::getMasterBalance(float *balance)
1377{
1378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1379 if (af == 0) return PERMISSION_DENIED;
1380 return af->getMasterBalance(balance);
1381}
1382
Eric Laurentac9cef52017-06-09 15:46:26 -07001383float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1384{
1385 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1386 if (aps == 0) return NAN;
1387 return aps->getStreamVolumeDB(stream, index, device);
1388}
1389
jiabin46a76fa2018-01-05 10:18:21 -08001390status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1391{
1392 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1393 if (af == 0) return PERMISSION_DENIED;
1394 return af->getMicrophones(microphones);
1395}
1396
jiabin81772902018-04-02 17:52:27 -07001397status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1398 audio_format_t *surroundFormats,
1399 bool *surroundFormatsEnabled,
1400 bool reported)
1401{
1402 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1403 if (aps == 0) return PERMISSION_DENIED;
1404 return aps->getSurroundFormats(
1405 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1406}
1407
1408status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1409{
1410 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1411 if (aps == 0) return PERMISSION_DENIED;
1412 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1413}
1414
Eric Laurentb78763e2018-10-17 10:08:02 -07001415status_t AudioSystem::setAssistantUid(uid_t uid)
1416{
1417 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1418 if (aps == 0) return PERMISSION_DENIED;
1419
1420 return aps->setAssistantUid(uid);
1421}
1422
1423status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1424{
1425 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1426 if (aps == 0) return PERMISSION_DENIED;
1427
1428 return aps->setA11yServicesUids(uids);
1429}
1430
jiabin6012f912018-11-02 17:06:30 -07001431bool AudioSystem::isHapticPlaybackSupported()
1432{
1433 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1434 if (aps == 0) return false;
1435 return aps->isHapticPlaybackSupported();
1436}
1437
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001438status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001439 std::vector<audio_format_t> *formats) {
1440 const sp <IAudioPolicyService>
1441 & aps = AudioSystem::get_audio_policy_service();
1442 if (aps == 0) return PERMISSION_DENIED;
1443 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1444}
1445
1446status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001447{
1448 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1449 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001450 return aps->listAudioProductStrategies(strategies);
1451}
1452
1453audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1454{
1455 AudioProductStrategyVector strategies;
1456 listAudioProductStrategies(strategies);
1457 for (const auto &strategy : strategies) {
1458 auto attrVect = strategy.getAudioAttributes();
1459 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1460 return attributes.getStreamType() == stream; });
1461 if (iter != end(attrVect)) {
1462 return iter->getAttributes();
1463 }
1464 }
1465 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1466 return AUDIO_ATTRIBUTES_INITIALIZER;
1467}
1468
1469audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1470{
François Gaffie4b2018b2018-11-07 11:18:59 +01001471 product_strategy_t psId;
1472 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1473 if (ret != NO_ERROR) {
1474 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1475 return AUDIO_STREAM_MUSIC;
1476 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001477 AudioProductStrategyVector strategies;
1478 listAudioProductStrategies(strategies);
1479 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001480 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001481 auto attrVect = strategy.getAudioAttributes();
1482 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1483 return AudioProductStrategy::attributesMatches(
1484 refAttr.getAttributes(), attr); });
1485 if (iter != end(attrVect)) {
1486 return iter->getStreamType();
1487 }
1488 }
1489 }
1490 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1491 return AUDIO_STREAM_MUSIC;
1492}
1493
François Gaffie4b2018b2018-11-07 11:18:59 +01001494status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1495 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001496{
1497 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001498 if (aps == 0) return PERMISSION_DENIED;
1499 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1500}
1501
1502status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1503{
1504 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1505 if (aps == 0) return PERMISSION_DENIED;
1506 return aps->listAudioVolumeGroups(groups);
1507}
1508
1509status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1510 volume_group_t &volumeGroup)
1511{
1512 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1513 if (aps == 0) return PERMISSION_DENIED;
1514 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001515}
Eric Laurentb78763e2018-10-17 10:08:02 -07001516
Eric Laurentc2f1f072009-07-17 12:17:14 -07001517// ---------------------------------------------------------------------------
1518
Eric Laurente8726fe2015-06-26 09:39:24 -07001519int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001520 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001521{
1522 Mutex::Autolock _l(mLock);
1523 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001524 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001525 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001526 }
1527 }
Eric Laurent296fb132015-05-01 11:38:42 -07001528 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001529 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001530}
1531
Eric Laurente8726fe2015-06-26 09:39:24 -07001532int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001533 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001534{
1535 Mutex::Autolock _l(mLock);
1536 size_t i;
1537 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001538 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001539 break;
1540 }
1541 }
1542 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001543 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001544 }
1545 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001546 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001547}
1548
Eric Laurent296fb132015-05-01 11:38:42 -07001549
Eric Laurentb28753e2015-04-01 13:06:28 -07001550void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1551{
1552 Mutex::Autolock _l(mLock);
1553 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1554 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1555 }
1556}
1557
1558void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1559{
1560 Mutex::Autolock _l(mLock);
1561 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1562 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1563 }
1564}
1565
François Gaffiecfe17322018-11-07 13:41:29 +01001566// ----------------------------------------------------------------------------
1567int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1568 const sp<AudioVolumeGroupCallback>& callback)
1569{
1570 Mutex::Autolock _l(mLock);
1571 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1572 if (mAudioVolumeGroupCallback[i] == callback) {
1573 return -1;
1574 }
1575 }
1576 mAudioVolumeGroupCallback.add(callback);
1577 return mAudioVolumeGroupCallback.size();
1578}
1579
1580int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1581 const sp<AudioVolumeGroupCallback>& callback)
1582{
1583 Mutex::Autolock _l(mLock);
1584 size_t i;
1585 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1586 if (mAudioVolumeGroupCallback[i] == callback) {
1587 break;
1588 }
1589 }
1590 if (i == mAudioVolumeGroupCallback.size()) {
1591 return -1;
1592 }
1593 mAudioVolumeGroupCallback.removeAt(i);
1594 return mAudioVolumeGroupCallback.size();
1595}
1596
1597void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1598 int flags)
1599{
1600 Mutex::Autolock _l(mLock);
1601 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1602 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1603 }
1604}
1605// ----------------------------------------------------------------------------
1606
Jean-Michel Trivide801052015-04-14 19:10:14 -07001607void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1608 String8 regId, int32_t state)
1609{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001610 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1611 dynamic_policy_callback cb = NULL;
1612 {
1613 Mutex::Autolock _l(AudioSystem::gLock);
1614 cb = gDynPolicyCallback;
1615 }
1616
1617 if (cb != NULL) {
1618 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1619 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001620}
1621
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001622void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001623 int event,
1624 const record_client_info_t *clientInfo,
1625 const audio_config_base_t *clientConfig,
1626 std::vector<effect_descriptor_t> clientEffects,
1627 const audio_config_base_t *deviceConfig,
1628 std::vector<effect_descriptor_t> effects,
1629 audio_patch_handle_t patchHandle,
1630 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001631 record_config_callback cb = NULL;
1632 {
1633 Mutex::Autolock _l(AudioSystem::gLock);
1634 cb = gRecordConfigCallback;
1635 }
1636
1637 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001638 cb(event, clientInfo, clientConfig, clientEffects,
1639 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001640 }
1641}
1642
Glenn Kasten4944acb2013-08-19 08:39:20 -07001643void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1644{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001645 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001646 Mutex::Autolock _l(mLock);
1647 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1648 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001649 }
François Gaffiecfe17322018-11-07 13:41:29 +01001650 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1651 mAudioVolumeGroupCallback[i]->onServiceDied();
1652 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001653 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001654 {
1655 Mutex::Autolock _l(gLockAPS);
1656 AudioSystem::gAudioPolicyService.clear();
1657 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001658
Steve Block5ff1dd52012-01-05 23:22:43 +00001659 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001660}
1661
Glenn Kasten40bc9062015-03-20 09:09:33 -07001662} // namespace android