blob: 41a4f88339ea8d5540d4a9c72499a4fb2dd9fd0c [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{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700431 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700433 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800434 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700435}
436
Andy Hung8b0bfd92019-12-23 13:11:11 -0800437void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700438{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700439 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
440 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800441 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700442 }
443}
444
Glenn Kastend848eb42016-03-08 13:42:11 -0800445void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
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) {
Marco Nelissend457c972014-02-11 08:47:07 -0800449 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700450 }
451}
452
Eric Laurent93c3d412014-08-01 14:48:35 -0700453audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
454{
455 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456 if (af == 0) return AUDIO_HW_SYNC_INVALID;
457 return af->getAudioHwSyncForSession(sessionId);
458}
459
Eric Laurent72e3f392015-05-20 14:43:50 -0700460status_t AudioSystem::systemReady()
461{
462 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
463 if (af == 0) return NO_INIT;
464 return af->systemReady();
465}
466
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700467status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
468 size_t* frameCount)
469{
470 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
471 if (af == 0) return PERMISSION_DENIED;
472 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
473 if (desc == 0) {
474 *frameCount = af->frameCountHAL(ioHandle);
475 } else {
476 *frameCount = desc->mFrameCountHAL;
477 }
478 if (*frameCount == 0) {
479 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
480 return BAD_VALUE;
481 }
482
483 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
484
485 return NO_ERROR;
486}
487
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488// ---------------------------------------------------------------------------
489
Eric Laurent73e26b62015-04-27 16:55:58 -0700490
491void AudioSystem::AudioFlingerClient::clearIoCache()
492{
493 Mutex::Autolock _l(mLock);
494 mIoDescriptors.clear();
495 mInBuffSize = 0;
496 mInSamplingRate = 0;
497 mInFormat = AUDIO_FORMAT_DEFAULT;
498 mInChannelMask = AUDIO_CHANNEL_NONE;
499}
500
Glenn Kasten4944acb2013-08-19 08:39:20 -0700501void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
502{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800503 audio_error_callback cb = NULL;
504 {
505 Mutex::Autolock _l(AudioSystem::gLock);
506 AudioSystem::gAudioFlinger.clear();
507 cb = gAudioErrorCallback;
508 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509
Eric Laurent73e26b62015-04-27 16:55:58 -0700510 // clear output handles and stream to output map caches
511 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512
Eric Laurentf6778fd2014-11-18 17:26:58 -0800513 if (cb) {
514 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000516 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517}
518
Eric Laurent73e26b62015-04-27 16:55:58 -0700519void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
520 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100521 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Eric Laurent73e26b62015-04-27 16:55:58 -0700523 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
Eric Laurent296fb132015-05-01 11:38:42 -0700525 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700526 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700527 {
528 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700529 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700530
531 switch (event) {
532 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700533 case AUDIO_OUTPUT_REGISTERED:
534 case AUDIO_INPUT_OPENED:
535 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700536 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700537 if (oldDesc == 0) {
538 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
539 } else {
540 deviceId = oldDesc->getDeviceId();
541 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700542 }
Eric Laurent296fb132015-05-01 11:38:42 -0700543
544 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
545 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700546 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700547 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
548 if (it != mAudioDeviceCallbacks.end()) {
549 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100550 }
551 }
Eric Laurent296fb132015-05-01 11:38:42 -0700552 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700553 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
554 "frameCount %zu deviceId %d",
555 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
556 "output" : "input",
557 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
558 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700559 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
560 ioDesc->mFrameCount, ioDesc->getDeviceId());
561 } break;
562 case AUDIO_OUTPUT_CLOSED:
563 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700564 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700565 ALOGW("ioConfigChanged() closing unknown %s %d",
566 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
567 break;
568 }
569 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700570 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571
Eric Laurent296fb132015-05-01 11:38:42 -0700572 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700573 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700574 } break;
575
576 case AUDIO_OUTPUT_CONFIG_CHANGED:
577 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700578 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700579 if (oldDesc == 0) {
580 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
581 break;
582 }
583
584 deviceId = oldDesc->getDeviceId();
585 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
586
587 if (deviceId != ioDesc->getDeviceId()) {
588 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700589 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
590 if (it != mAudioDeviceCallbacks.end()) {
591 callbacks = it->second;
592 }
Eric Laurent296fb132015-05-01 11:38:42 -0700593 }
594 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700595 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700596 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
597 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800598 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
599 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700600
Eric Laurentc2f1f072009-07-17 12:17:14 -0700601 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700602 case AUDIO_CLIENT_STARTED: {
603 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
604 if (oldDesc == 0) {
605 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
606 break;
607 }
608 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
609 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
610 oldDesc->mPatch = ioDesc->mPatch;
611 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
612 if (it != mAudioDeviceCallbacks.end()) {
613 auto cbks = it->second;
614 auto it2 = cbks.find(ioDesc->mPortId);
615 if (it2 != cbks.end()) {
616 callbacks.emplace(ioDesc->mPortId, it2->second);
617 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100618 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100619 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700620 } break;
621 }
622
623 for (auto wpCbk : callbacks) {
624 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
625 if (spCbk != nullptr) {
626 callbacksToCall.push_back(spCbk);
627 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700628 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800629 }
630
631 // Callbacks must be called without mLock held. May lead to dead lock if calling for
632 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700633 for (auto cb : callbacksToCall) {
634 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
635 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700636 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637}
638
Eric Laurent73e26b62015-04-27 16:55:58 -0700639status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
640 uint32_t sampleRate, audio_format_t format,
641 audio_channel_mask_t channelMask, size_t* buffSize)
642{
643 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
644 if (af == 0) {
645 return PERMISSION_DENIED;
646 }
647 Mutex::Autolock _l(mLock);
648 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
649 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
650 || (channelMask != mInChannelMask)) {
651 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
652 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800653 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700654 sampleRate, format, channelMask);
655 return BAD_VALUE;
656 }
657 // A benign race is possible here: we could overwrite a fresher cache entry
658 // save the request params
659 mInSamplingRate = sampleRate;
660 mInFormat = format;
661 mInChannelMask = channelMask;
662
663 mInBuffSize = inBuffSize;
664 }
665
666 *buffSize = mInBuffSize;
667
668 return NO_ERROR;
669}
670
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700671sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700672{
673 sp<AudioIoDescriptor> desc;
674 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
675 if (index >= 0) {
676 desc = mIoDescriptors.valueAt(index);
677 }
678 return desc;
679}
680
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700681sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
682{
683 Mutex::Autolock _l(mLock);
684 return getIoDescriptor_l(ioHandle);
685}
686
Eric Laurent296fb132015-05-01 11:38:42 -0700687status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700688 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
689 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700690{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700691 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800692 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700693 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
694 auto result = callbacks.try_emplace(portId, callback);
695 if (!result.second) {
696 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700697 }
Eric Laurent296fb132015-05-01 11:38:42 -0700698 return NO_ERROR;
699}
700
701status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700702 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
703 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700704{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700705 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800706 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700707 auto it = mAudioDeviceCallbacks.find(audioIo);
708 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700709 return INVALID_OPERATION;
710 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700711 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700712 return INVALID_OPERATION;
713 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700714 if (it->second.size() == 0) {
715 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700716 }
717 return NO_ERROR;
718}
719
720/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700721{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700722 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723 gAudioErrorCallback = cb;
724}
725
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700726/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
727{
728 Mutex::Autolock _l(gLock);
729 gDynPolicyCallback = cb;
730}
731
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800732/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
733{
734 Mutex::Autolock _l(gLock);
735 gRecordConfigCallback = cb;
736}
737
Eric Laurentc2f1f072009-07-17 12:17:14 -0700738// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800739// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700740sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
741sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
742
743
Glenn Kasten18a6d902012-09-24 11:27:56 -0700744// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800745const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700746{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800747 sp<IAudioPolicyService> ap;
748 sp<AudioPolicyServiceClient> apc;
749 {
750 Mutex::Autolock _l(gLockAPS);
751 if (gAudioPolicyService == 0) {
752 sp<IServiceManager> sm = defaultServiceManager();
753 sp<IBinder> binder;
754 do {
755 binder = sm->getService(String16("media.audio_policy"));
756 if (binder != 0)
757 break;
758 ALOGW("AudioPolicyService not published, waiting...");
759 usleep(500000); // 0.5 s
760 } while (true);
761 if (gAudioPolicyServiceClient == NULL) {
762 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
763 }
764 binder->linkToDeath(gAudioPolicyServiceClient);
765 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
766 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
767 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700768 // Make sure callbacks can be received by gAudioPolicyServiceClient
769 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700770 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800771 ap = gAudioPolicyService;
772 }
773 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200774 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800775 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200776 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100777 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200778 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700779 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800780
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800781 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700782}
783
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700784// ---------------------------------------------------------------------------
785
Dima Zavinfce7a472011-04-19 22:30:36 -0700786status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
787 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800788 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800789 const char *device_name,
790 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700791{
792 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700793 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800794 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700795
Eric Laurentc2f1f072009-07-17 12:17:14 -0700796 if (aps == 0) return PERMISSION_DENIED;
797
Eric Laurent71b63e32011-09-02 14:20:56 -0700798 if (device_address != NULL) {
799 address = device_address;
800 }
Paul McLeane743a472015-01-28 11:07:31 -0800801 if (device_name != NULL) {
802 name = device_name;
803 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800804 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700805}
806
Dima Zavinfce7a472011-04-19 22:30:36 -0700807audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700808 const char *device_address)
809{
810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700811 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700812
813 return aps->getDeviceConnectionState(device, device_address);
814}
815
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800816status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
817 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800818 const char *device_name,
819 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800820{
821 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
822 const char *address = "";
823 const char *name = "";
824
825 if (aps == 0) return PERMISSION_DENIED;
826
827 if (device_address != NULL) {
828 address = device_address;
829 }
830 if (device_name != NULL) {
831 name = device_name;
832 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800833 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800834}
835
Glenn Kastenf78aee72012-01-04 11:00:47 -0800836status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700837{
Glenn Kasten347966c2012-01-18 14:58:32 -0800838 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700839 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
840 if (aps == 0) return PERMISSION_DENIED;
841
842 return aps->setPhoneState(state);
843}
844
Dima Zavinfce7a472011-04-19 22:30:36 -0700845status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700846{
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 if (aps == 0) return PERMISSION_DENIED;
849 return aps->setForceUse(usage, config);
850}
851
Dima Zavinfce7a472011-04-19 22:30:36 -0700852audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700853{
854 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700855 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700856 return aps->getForceUse(usage);
857}
858
859
Eric Laurentf4e63452017-11-06 19:31:46 +0000860audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700861{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700862 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
863 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000864 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700865}
866
Eric Laurent42984412019-05-09 17:57:03 -0700867status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800868 audio_io_handle_t *output,
869 audio_session_t session,
870 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200871 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700872 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800873 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800874 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700875 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800876 audio_port_handle_t *portId,
877 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700878{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800880 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200881 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800882 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800883 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700884}
885
Eric Laurentd7fe0862018-07-14 16:48:01 -0700886status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887{
888 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
889 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700890 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700891}
892
Eric Laurentd7fe0862018-07-14 16:48:01 -0700893status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700894{
895 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
896 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700897 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700898}
899
Eric Laurentd7fe0862018-07-14 16:48:01 -0700900void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700901{
902 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
903 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700904 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700905}
906
Eric Laurentcaf7f482014-11-25 17:50:47 -0800907status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
908 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700909 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800910 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700911 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700912 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800913 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800914 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600915 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700916 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800917 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918{
919 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800920 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600921 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700922 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800923 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700924}
925
Eric Laurent4eb58f12018-12-07 16:41:02 -0800926status_t AudioSystem::startInput(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 PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800930 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700931}
932
Eric Laurentfee19762018-01-29 18:44:13 -0800933status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700934{
935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
936 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800937 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700938}
939
Eric Laurentfee19762018-01-29 18:44:13 -0800940void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941{
942 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
943 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800944 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700945}
946
Dima Zavinfce7a472011-04-19 22:30:36 -0700947status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948 int indexMin,
949 int indexMax)
950{
951 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
952 if (aps == 0) return PERMISSION_DENIED;
953 return aps->initStreamVolume(stream, indexMin, indexMax);
954}
955
Eric Laurent83844cc2011-11-18 16:43:31 -0800956status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
957 int index,
958 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959{
960 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
961 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800962 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700963}
964
Eric Laurent83844cc2011-11-18 16:43:31 -0800965status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
966 int *index,
967 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700968{
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
970 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800971 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700972}
973
François Gaffiecfe17322018-11-07 13:41:29 +0100974status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
975 int index,
976 audio_devices_t device)
977{
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
980 return aps->setVolumeIndexForAttributes(attr, index, device);
981}
982
983status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
984 int &index,
985 audio_devices_t device)
986{
987 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
988 if (aps == 0) return PERMISSION_DENIED;
989 return aps->getVolumeIndexForAttributes(attr, index, device);
990}
991
992status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
993{
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
996 return aps->getMaxVolumeIndexForAttributes(attr, index);
997}
998
999status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1000{
1001 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1002 if (aps == 0) return PERMISSION_DENIED;
1003 return aps->getMinVolumeIndexForAttributes(attr, index);
1004}
1005
Dima Zavinfce7a472011-04-19 22:30:36 -07001006uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001007{
1008 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001009 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001010 return aps->getStrategyForStream(stream);
1011}
1012
Eric Laurent63742522012-03-08 13:42:42 -08001013audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001014{
1015 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001016 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001017 return aps->getDevicesForStream(stream);
1018}
1019
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001020status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1021 AudioDeviceTypeAddrVector *devices) {
1022 if (devices == nullptr) {
1023 return BAD_VALUE;
1024 }
1025 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1026 if (aps == 0) return PERMISSION_DENIED;
1027 return aps->getDevicesForAttributes(aa, devices);
1028}
1029
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001030audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001031{
1032 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001033 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001034 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001035 return aps->getOutputForEffect(desc);
1036}
1037
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001038status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001039 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001040 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001041 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001042 int id)
1043{
1044 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1045 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001046 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001047}
1048
1049status_t AudioSystem::unregisterEffect(int id)
1050{
1051 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1052 if (aps == 0) return PERMISSION_DENIED;
1053 return aps->unregisterEffect(id);
1054}
1055
Eric Laurentdb7c0792011-08-10 10:37:50 -07001056status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1057{
1058 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1059 if (aps == 0) return PERMISSION_DENIED;
1060 return aps->setEffectEnabled(id, enabled);
1061}
1062
Eric Laurent6c796322019-04-09 14:13:17 -07001063status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1064{
1065 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1066 if (aps == 0) return PERMISSION_DENIED;
1067 return aps->moveEffectsToIo(ids, io);
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
Kevin Rocardb99cc752019-03-21 20:52:24 -07001136status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1137 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1138 if (aps == nullptr) return PERMISSION_DENIED;
1139 return aps->setAllowedCapturePolicy(uid, flags);
1140}
1141
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001142bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1143{
1144 ALOGV("isOffloadSupported()");
1145 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1146 if (aps == 0) return false;
1147 return aps->isOffloadSupported(info);
1148}
1149
Eric Laurent203b1a12014-04-01 10:34:16 -07001150status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1151 audio_port_type_t type,
1152 unsigned int *num_ports,
1153 struct audio_port *ports,
1154 unsigned int *generation)
1155{
1156 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1157 if (aps == 0) return PERMISSION_DENIED;
1158 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1159}
1160
1161status_t AudioSystem::getAudioPort(struct audio_port *port)
1162{
1163 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1164 if (aps == 0) return PERMISSION_DENIED;
1165 return aps->getAudioPort(port);
1166}
1167
1168status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1169 audio_patch_handle_t *handle)
1170{
1171 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1172 if (aps == 0) return PERMISSION_DENIED;
1173 return aps->createAudioPatch(patch, handle);
1174}
1175
1176status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1177{
1178 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1179 if (aps == 0) return PERMISSION_DENIED;
1180 return aps->releaseAudioPatch(handle);
1181}
1182
1183status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1184 struct audio_patch *patches,
1185 unsigned int *generation)
1186{
1187 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1188 if (aps == 0) return PERMISSION_DENIED;
1189 return aps->listAudioPatches(num_patches, patches, generation);
1190}
1191
1192status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1193{
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return PERMISSION_DENIED;
1196 return aps->setAudioPortConfig(config);
1197}
1198
Eric Laurent296fb132015-05-01 11:38:42 -07001199status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001200{
Eric Laurentb28753e2015-04-01 13:06:28 -07001201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1202 if (aps == 0) return PERMISSION_DENIED;
1203
1204 Mutex::Autolock _l(gLockAPS);
1205 if (gAudioPolicyServiceClient == 0) {
1206 return NO_INIT;
1207 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001208 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1209 if (ret == 1) {
1210 aps->setAudioPortCallbacksEnabled(true);
1211 }
1212 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001213}
1214
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001215/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001216status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001217{
1218 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1219 if (aps == 0) return PERMISSION_DENIED;
1220
1221 Mutex::Autolock _l(gLockAPS);
1222 if (gAudioPolicyServiceClient == 0) {
1223 return NO_INIT;
1224 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001225 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1226 if (ret == 0) {
1227 aps->setAudioPortCallbacksEnabled(false);
1228 }
1229 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001230}
1231
François Gaffiecfe17322018-11-07 13:41:29 +01001232status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1233{
1234 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1235 if (aps == 0) return PERMISSION_DENIED;
1236
1237 Mutex::Autolock _l(gLockAPS);
1238 if (gAudioPolicyServiceClient == 0) {
1239 return NO_INIT;
1240 }
1241 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1242 if (ret == 1) {
1243 aps->setAudioVolumeGroupCallbacksEnabled(true);
1244 }
1245 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1246}
1247
1248status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1249{
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 }
1257 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1258 if (ret == 0) {
1259 aps->setAudioVolumeGroupCallbacksEnabled(false);
1260 }
1261 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1262}
1263
Eric Laurent296fb132015-05-01 11:38:42 -07001264status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001265 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1266 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001267{
1268 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1269 if (afc == 0) {
1270 return NO_INIT;
1271 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001272 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001273 if (status == NO_ERROR) {
1274 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1275 if (af != 0) {
1276 af->registerClient(afc);
1277 }
1278 }
1279 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001280}
1281
1282status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001283 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1284 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001285{
1286 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1287 if (afc == 0) {
1288 return NO_INIT;
1289 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001290 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001291}
1292
1293audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1294{
1295 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1296 if (af == 0) return PERMISSION_DENIED;
1297 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1298 if (desc == 0) {
1299 return AUDIO_PORT_HANDLE_NONE;
1300 }
1301 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001302}
1303
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001304status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1305 audio_io_handle_t *ioHandle,
1306 audio_devices_t *device)
1307{
1308 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1309 if (aps == 0) return PERMISSION_DENIED;
1310 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1311}
1312
1313status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1314{
1315 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1316 if (aps == 0) return PERMISSION_DENIED;
1317 return aps->releaseSoundTriggerSession(session);
1318}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001319
1320audio_mode_t AudioSystem::getPhoneState()
1321{
1322 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1323 if (aps == 0) return AUDIO_MODE_INVALID;
1324 return aps->getPhoneState();
1325}
1326
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001327status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001328{
1329 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1330 if (aps == 0) return PERMISSION_DENIED;
1331 return aps->registerPolicyMixes(mixes, registration);
1332}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001333
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001334status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1335{
1336 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1337 if (aps == 0) return PERMISSION_DENIED;
1338 return aps->setUidDeviceAffinities(uid, devices);
1339}
1340
1341status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1342 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1343 if (aps == 0) return PERMISSION_DENIED;
1344 return aps->removeUidDeviceAffinities(uid);
1345}
1346
Eric Laurent554a2772015-04-10 11:29:24 -07001347status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1348 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001349 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001350{
1351 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1352 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001353 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001354}
1355
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001356status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001357{
1358 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1359 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001360 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001361}
1362
Andy Hung2ddee192015-12-18 17:34:44 -08001363status_t AudioSystem::setMasterMono(bool mono)
1364{
1365 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1366 if (aps == 0) return PERMISSION_DENIED;
1367 return aps->setMasterMono(mono);
1368}
1369
1370status_t AudioSystem::getMasterMono(bool *mono)
1371{
1372 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1373 if (aps == 0) return PERMISSION_DENIED;
1374 return aps->getMasterMono(mono);
1375}
1376
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001377status_t AudioSystem::setMasterBalance(float balance)
1378{
1379 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1380 if (af == 0) return PERMISSION_DENIED;
1381 return af->setMasterBalance(balance);
1382}
1383
1384status_t AudioSystem::getMasterBalance(float *balance)
1385{
1386 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1387 if (af == 0) return PERMISSION_DENIED;
1388 return af->getMasterBalance(balance);
1389}
1390
Eric Laurentac9cef52017-06-09 15:46:26 -07001391float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1392{
1393 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1394 if (aps == 0) return NAN;
1395 return aps->getStreamVolumeDB(stream, index, device);
1396}
1397
jiabin46a76fa2018-01-05 10:18:21 -08001398status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1399{
1400 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1401 if (af == 0) return PERMISSION_DENIED;
1402 return af->getMicrophones(microphones);
1403}
1404
Eric Laurent42896a02019-09-27 15:40:33 -07001405status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1407 if (af == nullptr) return PERMISSION_DENIED;
1408 return af->setAudioHalPids(pids);
1409}
1410
jiabin81772902018-04-02 17:52:27 -07001411status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1412 audio_format_t *surroundFormats,
1413 bool *surroundFormatsEnabled,
1414 bool reported)
1415{
1416 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1417 if (aps == 0) return PERMISSION_DENIED;
1418 return aps->getSurroundFormats(
1419 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1420}
1421
1422status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1423{
1424 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1425 if (aps == 0) return PERMISSION_DENIED;
1426 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1427}
1428
Eric Laurentb78763e2018-10-17 10:08:02 -07001429status_t AudioSystem::setAssistantUid(uid_t uid)
1430{
1431 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1432 if (aps == 0) return PERMISSION_DENIED;
1433
1434 return aps->setAssistantUid(uid);
1435}
1436
1437status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1438{
1439 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1440 if (aps == 0) return PERMISSION_DENIED;
1441
1442 return aps->setA11yServicesUids(uids);
1443}
1444
jiabin6012f912018-11-02 17:06:30 -07001445bool AudioSystem::isHapticPlaybackSupported()
1446{
1447 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1448 if (aps == 0) return false;
1449 return aps->isHapticPlaybackSupported();
1450}
1451
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001452status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001453 std::vector<audio_format_t> *formats) {
1454 const sp <IAudioPolicyService>
1455 & aps = AudioSystem::get_audio_policy_service();
1456 if (aps == 0) return PERMISSION_DENIED;
1457 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1458}
1459
1460status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001461{
1462 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1463 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001464 return aps->listAudioProductStrategies(strategies);
1465}
1466
1467audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1468{
1469 AudioProductStrategyVector strategies;
1470 listAudioProductStrategies(strategies);
1471 for (const auto &strategy : strategies) {
1472 auto attrVect = strategy.getAudioAttributes();
1473 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1474 return attributes.getStreamType() == stream; });
1475 if (iter != end(attrVect)) {
1476 return iter->getAttributes();
1477 }
1478 }
1479 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1480 return AUDIO_ATTRIBUTES_INITIALIZER;
1481}
1482
1483audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1484{
François Gaffie4b2018b2018-11-07 11:18:59 +01001485 product_strategy_t psId;
1486 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1487 if (ret != NO_ERROR) {
1488 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1489 return AUDIO_STREAM_MUSIC;
1490 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001491 AudioProductStrategyVector strategies;
1492 listAudioProductStrategies(strategies);
1493 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001494 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001495 auto attrVect = strategy.getAudioAttributes();
1496 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1497 return AudioProductStrategy::attributesMatches(
1498 refAttr.getAttributes(), attr); });
1499 if (iter != end(attrVect)) {
1500 return iter->getStreamType();
1501 }
1502 }
1503 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001504 switch (attr.usage) {
1505 case AUDIO_USAGE_VIRTUAL_SOURCE:
1506 // virtual source is not expected to have an associated product strategy
1507 break;
1508 default:
1509 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1510 break;
1511 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001512 return AUDIO_STREAM_MUSIC;
1513}
1514
François Gaffie4b2018b2018-11-07 11:18:59 +01001515status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1516 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001517{
1518 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001519 if (aps == 0) return PERMISSION_DENIED;
1520 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1521}
1522
1523status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1524{
1525 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1526 if (aps == 0) return PERMISSION_DENIED;
1527 return aps->listAudioVolumeGroups(groups);
1528}
1529
1530status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1531 volume_group_t &volumeGroup)
1532{
1533 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1534 if (aps == 0) return PERMISSION_DENIED;
1535 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001536}
Eric Laurentb78763e2018-10-17 10:08:02 -07001537
Eric Laurent6ede98f2019-06-11 14:50:30 -07001538status_t AudioSystem::setRttEnabled(bool enabled)
1539{
1540 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1541 if (aps == 0) return PERMISSION_DENIED;
1542 return aps->setRttEnabled(enabled);
1543}
1544
Eric Laurent8340e672019-11-06 11:01:08 -08001545bool AudioSystem::isCallScreenModeSupported()
1546{
1547 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1548 if (aps == 0) return false;
1549 return aps->isCallScreenModeSupported();
1550}
1551
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001552status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1553 const AudioDeviceTypeAddr &device)
1554{
1555 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1556 if (aps == 0) {
1557 return PERMISSION_DENIED;
1558 }
1559 return aps->setPreferredDeviceForStrategy(strategy, device);
1560}
1561
1562status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1563{
1564 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1565 if (aps == 0) {
1566 return PERMISSION_DENIED;
1567 }
1568 return aps->removePreferredDeviceForStrategy(strategy);
1569}
1570
1571status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1572 AudioDeviceTypeAddr &device)
1573{
1574 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1575 if (aps == 0) {
1576 return PERMISSION_DENIED;
1577 }
1578 return aps->getPreferredDeviceForStrategy(strategy, device);
1579}
1580
Eric Laurentc2f1f072009-07-17 12:17:14 -07001581// ---------------------------------------------------------------------------
1582
Eric Laurente8726fe2015-06-26 09:39:24 -07001583int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001584 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001585{
1586 Mutex::Autolock _l(mLock);
1587 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001588 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001589 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001590 }
1591 }
Eric Laurent296fb132015-05-01 11:38:42 -07001592 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001593 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001594}
1595
Eric Laurente8726fe2015-06-26 09:39:24 -07001596int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001597 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001598{
1599 Mutex::Autolock _l(mLock);
1600 size_t i;
1601 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001602 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001603 break;
1604 }
1605 }
1606 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001607 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001608 }
1609 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001610 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001611}
1612
Eric Laurent296fb132015-05-01 11:38:42 -07001613
Eric Laurentb28753e2015-04-01 13:06:28 -07001614void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1615{
1616 Mutex::Autolock _l(mLock);
1617 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1618 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1619 }
1620}
1621
1622void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1623{
1624 Mutex::Autolock _l(mLock);
1625 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1626 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1627 }
1628}
1629
François Gaffiecfe17322018-11-07 13:41:29 +01001630// ----------------------------------------------------------------------------
1631int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1632 const sp<AudioVolumeGroupCallback>& callback)
1633{
1634 Mutex::Autolock _l(mLock);
1635 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1636 if (mAudioVolumeGroupCallback[i] == callback) {
1637 return -1;
1638 }
1639 }
1640 mAudioVolumeGroupCallback.add(callback);
1641 return mAudioVolumeGroupCallback.size();
1642}
1643
1644int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1645 const sp<AudioVolumeGroupCallback>& callback)
1646{
1647 Mutex::Autolock _l(mLock);
1648 size_t i;
1649 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1650 if (mAudioVolumeGroupCallback[i] == callback) {
1651 break;
1652 }
1653 }
1654 if (i == mAudioVolumeGroupCallback.size()) {
1655 return -1;
1656 }
1657 mAudioVolumeGroupCallback.removeAt(i);
1658 return mAudioVolumeGroupCallback.size();
1659}
1660
1661void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1662 int flags)
1663{
1664 Mutex::Autolock _l(mLock);
1665 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1666 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1667 }
1668}
1669// ----------------------------------------------------------------------------
1670
Jean-Michel Trivide801052015-04-14 19:10:14 -07001671void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1672 String8 regId, int32_t state)
1673{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001674 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1675 dynamic_policy_callback cb = NULL;
1676 {
1677 Mutex::Autolock _l(AudioSystem::gLock);
1678 cb = gDynPolicyCallback;
1679 }
1680
1681 if (cb != NULL) {
1682 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1683 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001684}
1685
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001686void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001687 int event,
1688 const record_client_info_t *clientInfo,
1689 const audio_config_base_t *clientConfig,
1690 std::vector<effect_descriptor_t> clientEffects,
1691 const audio_config_base_t *deviceConfig,
1692 std::vector<effect_descriptor_t> effects,
1693 audio_patch_handle_t patchHandle,
1694 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001695 record_config_callback cb = NULL;
1696 {
1697 Mutex::Autolock _l(AudioSystem::gLock);
1698 cb = gRecordConfigCallback;
1699 }
1700
1701 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001702 cb(event, clientInfo, clientConfig, clientEffects,
1703 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001704 }
1705}
1706
Glenn Kasten4944acb2013-08-19 08:39:20 -07001707void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1708{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001709 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001710 Mutex::Autolock _l(mLock);
1711 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1712 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001713 }
François Gaffiecfe17322018-11-07 13:41:29 +01001714 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1715 mAudioVolumeGroupCallback[i]->onServiceDied();
1716 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001717 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001718 {
1719 Mutex::Autolock _l(gLockAPS);
1720 AudioSystem::gAudioPolicyService.clear();
1721 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001722
Steve Block5ff1dd52012-01-05 23:22:43 +00001723 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001724}
1725
Glenn Kasten40bc9062015-03-20 09:09:33 -07001726} // namespace android