blob: c284f7301be029e4f50eb479a8d7806180c43b09 [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>
Eric Laurent21da6472017-11-09 16:29:26 -080023#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080024#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070025#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070026#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <math.h>
28
Dima Zavin64760242011-05-11 14:15:23 -070029#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070030
Eric Laurentc2f1f072009-07-17 12:17:14 -070031// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070032
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033namespace android {
34
35// client singleton for AudioFlinger binder interface
36Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080037Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080038sp<IAudioFlinger> AudioSystem::gAudioFlinger;
39sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
40audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070041dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -080042record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080043
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080046const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080048 sp<IAudioFlinger> af;
49 sp<AudioFlingerClient> afc;
50 {
51 Mutex::Autolock _l(gLock);
52 if (gAudioFlinger == 0) {
53 sp<IServiceManager> sm = defaultServiceManager();
54 sp<IBinder> binder;
55 do {
56 binder = sm->getService(String16("media.audio_flinger"));
57 if (binder != 0)
58 break;
59 ALOGW("AudioFlinger not published, waiting...");
60 usleep(500000); // 0.5 s
61 } while (true);
62 if (gAudioFlingerClient == NULL) {
63 gAudioFlingerClient = new AudioFlingerClient();
64 } else {
65 if (gAudioErrorCallback) {
66 gAudioErrorCallback(NO_ERROR);
67 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080069 binder->linkToDeath(gAudioFlingerClient);
70 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
71 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
72 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070073 // Make sure callbacks can be received by gAudioFlingerClient
74 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070075 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080076 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080078 if (afc != 0) {
79 af->registerClient(afc);
80 }
81 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080082}
83
Eric Laurent296fb132015-05-01 11:38:42 -070084const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
85{
86 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
87 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
88 if (af == 0) return 0;
89 Mutex::Autolock _l(gLock);
90 return gAudioFlingerClient;
91}
92
93sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
94{
95 sp<AudioIoDescriptor> desc;
96 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
97 if (afc != 0) {
98 desc = afc->getIoDescriptor(ioHandle);
99 }
100 return desc;
101}
102
Eric Laurent46291612013-07-18 14:38:44 -0700103/* static */ status_t AudioSystem::checkAudioFlinger()
104{
105 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
106 return NO_ERROR;
107 }
108 return DEAD_OBJECT;
109}
110
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700111// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
112
Glenn Kasten4944acb2013-08-19 08:39:20 -0700113status_t AudioSystem::muteMicrophone(bool state)
114{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
116 if (af == 0) return PERMISSION_DENIED;
117 return af->setMicMute(state);
118}
119
Glenn Kasten4944acb2013-08-19 08:39:20 -0700120status_t AudioSystem::isMicrophoneMuted(bool* state)
121{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
123 if (af == 0) return PERMISSION_DENIED;
124 *state = af->getMicMute();
125 return NO_ERROR;
126}
127
128status_t AudioSystem::setMasterVolume(float value)
129{
130 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
131 if (af == 0) return PERMISSION_DENIED;
132 af->setMasterVolume(value);
133 return NO_ERROR;
134}
135
136status_t AudioSystem::setMasterMute(bool mute)
137{
138 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
139 if (af == 0) return PERMISSION_DENIED;
140 af->setMasterMute(mute);
141 return NO_ERROR;
142}
143
144status_t AudioSystem::getMasterVolume(float* volume)
145{
146 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
147 if (af == 0) return PERMISSION_DENIED;
148 *volume = af->masterVolume();
149 return NO_ERROR;
150}
151
152status_t AudioSystem::getMasterMute(bool* mute)
153{
154 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
155 if (af == 0) return PERMISSION_DENIED;
156 *mute = af->masterMute();
157 return NO_ERROR;
158}
159
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800160status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
161 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162{
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
165 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700166 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 return NO_ERROR;
168}
169
Glenn Kastenfff6d712012-01-12 16:38:12 -0800170status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171{
Dima Zavinfce7a472011-04-19 22:30:36 -0700172 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
174 if (af == 0) return PERMISSION_DENIED;
175 af->setStreamMute(stream, mute);
176 return NO_ERROR;
177}
178
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800179status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
180 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181{
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
184 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700185 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 return NO_ERROR;
187}
188
Glenn Kastenfff6d712012-01-12 16:38:12 -0800189status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190{
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
193 if (af == 0) return PERMISSION_DENIED;
194 *mute = af->streamMute(stream);
195 return NO_ERROR;
196}
197
Glenn Kastenf78aee72012-01-04 11:00:47 -0800198status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800200 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
202 if (af == 0) return PERMISSION_DENIED;
203 return af->setMode(mode);
204}
205
Glenn Kasten4944acb2013-08-19 08:39:20 -0700206status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
207{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
209 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700210 return af->setParameters(ioHandle, keyValuePairs);
211}
212
Glenn Kasten4944acb2013-08-19 08:39:20 -0700213String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
214{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700215 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
216 String8 result = String8("");
217 if (af == 0) return result;
218
219 result = af->getParameters(ioHandle, keys);
220 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221}
222
Glenn Kastenc23885e2013-12-19 16:35:18 -0800223status_t AudioSystem::setParameters(const String8& keyValuePairs)
224{
Glenn Kasten142f5192014-03-25 17:44:59 -0700225 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800226}
227
228String8 AudioSystem::getParameters(const String8& keys)
229{
Glenn Kasten142f5192014-03-25 17:44:59 -0700230 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800231}
232
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233// convert volume steps to natural log scale
234
235// change this value to change volume scaling
236static const float dBPerStep = 0.5f;
237// shouldn't need to touch these
238static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
239static const float dBConvertInverse = 1.0f / dBConvert;
240
241float AudioSystem::linearToLog(int volume)
242{
243 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000244 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 // return v;
246 return volume ? exp(float(100 - volume) * dBConvert) : 0;
247}
248
249int AudioSystem::logToLinear(float volume)
250{
251 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000252 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 // return v;
254 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
255}
256
Eric Laurent21da6472017-11-09 16:29:26 -0800257/* static */ size_t AudioSystem::calculateMinFrameCount(
258 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
259 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
260{
261 // Ensure that buffer depth covers at least audio hardware latency
262 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
263 if (minBufCount < 2) {
264 minBufCount = 2;
265 }
266#if 0
267 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
268 // but keeping the code here to make it easier to add later.
269 if (minBufCount < notificationsPerBufferReq) {
270 minBufCount = notificationsPerBufferReq;
271 }
272#endif
273 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
274 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
275 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
276 /*, notificationsPerBufferReq*/);
277 return minBufCount * sourceFramesNeededWithTimestretch(
278 sampleRate, afFrameCount, afSampleRate, speed);
279}
280
281
Glenn Kasten3b16c762012-11-14 08:44:39 -0800282status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285
Dima Zavinfce7a472011-04-19 22:30:36 -0700286 if (streamType == AUDIO_STREAM_DEFAULT) {
287 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 }
289
Glenn Kastenfff6d712012-01-12 16:38:12 -0800290 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 if (output == 0) {
292 return PERMISSION_DENIED;
293 }
294
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700295 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700296}
297
Glenn Kasten2c073da2016-02-26 09:14:08 -0800298status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800299 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700300{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800301 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
302 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800303 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
304 if (desc == 0) {
305 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800307 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800309 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800310 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800311 return BAD_VALUE;
312 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313
Glenn Kasten2c073da2016-02-26 09:14:08 -0800314 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
Glenn Kastene33054e2012-11-14 12:54:39 -0800319status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322
Dima Zavinfce7a472011-04-19 22:30:36 -0700323 if (streamType == AUDIO_STREAM_DEFAULT) {
324 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700326
Glenn Kastenfff6d712012-01-12 16:38:12 -0800327 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700328 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 return PERMISSION_DENIED;
330 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700332 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700333}
334
Glenn Kasten2c073da2016-02-26 09:14:08 -0800335status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800336 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700337{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800338 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
339 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800340 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
341 if (desc == 0) {
342 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700343 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800344 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800346 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800347 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800348 return BAD_VALUE;
349 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350
Glenn Kasten2c073da2016-02-26 09:14:08 -0800351 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 return NO_ERROR;
354}
355
Glenn Kastenfff6d712012-01-12 16:38:12 -0800356status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359
Dima Zavinfce7a472011-04-19 22:30:36 -0700360 if (streamType == AUDIO_STREAM_DEFAULT) {
361 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700363
Glenn Kastenfff6d712012-01-12 16:38:12 -0800364 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700365 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700366 return PERMISSION_DENIED;
367 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368
Glenn Kasten241618f2014-03-25 17:48:57 -0700369 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700370}
371
372status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700373 uint32_t* latency)
374{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800375 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
376 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700377 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700378 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700379 *latency = af->latency(output);
380 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700381 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 }
383
Glenn Kasten241618f2014-03-25 17:48:57 -0700384 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 return NO_ERROR;
387}
388
Glenn Kastendd8104c2012-07-02 12:42:44 -0700389status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
390 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391{
Eric Laurent296fb132015-05-01 11:38:42 -0700392 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
393 if (afc == 0) {
394 return NO_INIT;
395 }
396 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397}
398
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700399status_t AudioSystem::setVoiceVolume(float value)
400{
401 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
402 if (af == 0) return PERMISSION_DENIED;
403 return af->setVoiceVolume(value);
404}
405
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000406status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700407 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800408{
409 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
410 if (af == 0) return PERMISSION_DENIED;
411
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000412 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800413}
414
Glenn Kasten4944acb2013-08-19 08:39:20 -0700415uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
416{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800418 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800419 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700420 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800421
422 result = af->getInputFramesLost(ioHandle);
423 return result;
424}
425
Glenn Kasteneeecb982016-02-26 10:44:04 -0800426audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700427{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700429 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800430 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700431}
432
Glenn Kastend848eb42016-03-08 13:42:11 -0800433void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700434{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700435 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
436 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800437 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700438 }
439}
440
Glenn Kastend848eb42016-03-08 13:42:11 -0800441void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700442{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700443 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
444 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800445 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700446 }
447}
448
Eric Laurent93c3d412014-08-01 14:48:35 -0700449audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
450{
451 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
452 if (af == 0) return AUDIO_HW_SYNC_INVALID;
453 return af->getAudioHwSyncForSession(sessionId);
454}
455
Eric Laurent72e3f392015-05-20 14:43:50 -0700456status_t AudioSystem::systemReady()
457{
458 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
459 if (af == 0) return NO_INIT;
460 return af->systemReady();
461}
462
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700463status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
464 size_t* frameCount)
465{
466 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
467 if (af == 0) return PERMISSION_DENIED;
468 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
469 if (desc == 0) {
470 *frameCount = af->frameCountHAL(ioHandle);
471 } else {
472 *frameCount = desc->mFrameCountHAL;
473 }
474 if (*frameCount == 0) {
475 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
476 return BAD_VALUE;
477 }
478
479 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
480
481 return NO_ERROR;
482}
483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484// ---------------------------------------------------------------------------
485
Eric Laurent73e26b62015-04-27 16:55:58 -0700486
487void AudioSystem::AudioFlingerClient::clearIoCache()
488{
489 Mutex::Autolock _l(mLock);
490 mIoDescriptors.clear();
491 mInBuffSize = 0;
492 mInSamplingRate = 0;
493 mInFormat = AUDIO_FORMAT_DEFAULT;
494 mInChannelMask = AUDIO_CHANNEL_NONE;
495}
496
Glenn Kasten4944acb2013-08-19 08:39:20 -0700497void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
498{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800499 audio_error_callback cb = NULL;
500 {
501 Mutex::Autolock _l(AudioSystem::gLock);
502 AudioSystem::gAudioFlinger.clear();
503 cb = gAudioErrorCallback;
504 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505
Eric Laurent73e26b62015-04-27 16:55:58 -0700506 // clear output handles and stream to output map caches
507 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508
Eric Laurentf6778fd2014-11-18 17:26:58 -0800509 if (cb) {
510 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000512 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513}
514
Eric Laurent73e26b62015-04-27 16:55:58 -0700515void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
516 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100517 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Eric Laurent73e26b62015-04-27 16:55:58 -0700519 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700520
Eric Laurent296fb132015-05-01 11:38:42 -0700521 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurentad2e7b92017-09-14 20:06:42 -0700522 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523
Eric Laurent296fb132015-05-01 11:38:42 -0700524 {
525 Mutex::Autolock _l(mLock);
526
527 switch (event) {
528 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700529 case AUDIO_OUTPUT_REGISTERED:
530 case AUDIO_INPUT_OPENED:
531 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700532 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700533 if (oldDesc == 0) {
534 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
535 } else {
536 deviceId = oldDesc->getDeviceId();
537 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700538 }
Eric Laurent296fb132015-05-01 11:38:42 -0700539
540 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
541 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700542 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
543 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
544 if (ioIndex >= 0) {
545 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
546 }
Eric Laurent296fb132015-05-01 11:38:42 -0700547 }
548 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700549 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
550 "frameCount %zu deviceId %d",
551 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
552 "output" : "input",
553 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
554 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700555 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
556 ioDesc->mFrameCount, ioDesc->getDeviceId());
557 } break;
558 case AUDIO_OUTPUT_CLOSED:
559 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700560 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700561 ALOGW("ioConfigChanged() closing unknown %s %d",
562 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
563 break;
564 }
565 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700566 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700567
Eric Laurent296fb132015-05-01 11:38:42 -0700568 mIoDescriptors.removeItem(ioDesc->mIoHandle);
569 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
570 } break;
571
572 case AUDIO_OUTPUT_CONFIG_CHANGED:
573 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700574 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700575 if (oldDesc == 0) {
576 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
577 break;
578 }
579
580 deviceId = oldDesc->getDeviceId();
581 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
582
583 if (deviceId != ioDesc->getDeviceId()) {
584 deviceId = ioDesc->getDeviceId();
585 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
586 if (ioIndex >= 0) {
587 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
588 }
589 }
590 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700591 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700592 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
593 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800594 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
595 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700596
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598 }
Eric Laurent296fb132015-05-01 11:38:42 -0700599 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700600 bool callbackRemoved = false;
Eric Laurent296fb132015-05-01 11:38:42 -0700601 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
Eric Laurentad2e7b92017-09-14 20:06:42 -0700602 for (size_t i = 0; i < callbacks.size(); ) {
603 sp<AudioDeviceCallback> callback = callbacks[i].promote();
604 if (callback.get() != nullptr) {
605 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
606 i++;
607 } else {
608 callbacks.removeAt(i);
609 callbackRemoved = true;
610 }
611 }
612 // clean up callback list while we are here if some clients have disappeared without
613 // unregistering their callback
614 if (callbackRemoved) {
615 Mutex::Autolock _l(mLock);
616 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700617 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618}
619
Eric Laurent73e26b62015-04-27 16:55:58 -0700620status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
621 uint32_t sampleRate, audio_format_t format,
622 audio_channel_mask_t channelMask, size_t* buffSize)
623{
624 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
625 if (af == 0) {
626 return PERMISSION_DENIED;
627 }
628 Mutex::Autolock _l(mLock);
629 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
630 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
631 || (channelMask != mInChannelMask)) {
632 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
633 if (inBuffSize == 0) {
634 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
635 sampleRate, format, channelMask);
636 return BAD_VALUE;
637 }
638 // A benign race is possible here: we could overwrite a fresher cache entry
639 // save the request params
640 mInSamplingRate = sampleRate;
641 mInFormat = format;
642 mInChannelMask = channelMask;
643
644 mInBuffSize = inBuffSize;
645 }
646
647 *buffSize = mInBuffSize;
648
649 return NO_ERROR;
650}
651
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700652sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700653{
654 sp<AudioIoDescriptor> desc;
655 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
656 if (index >= 0) {
657 desc = mIoDescriptors.valueAt(index);
658 }
659 return desc;
660}
661
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700662sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
663{
664 Mutex::Autolock _l(mLock);
665 return getIoDescriptor_l(ioHandle);
666}
667
Eric Laurent296fb132015-05-01 11:38:42 -0700668status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700669 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700670{
671 Mutex::Autolock _l(mLock);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700672 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurent296fb132015-05-01 11:38:42 -0700673 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
674 if (ioIndex >= 0) {
675 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
676 }
677
678 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700679 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700680 return INVALID_OPERATION;
681 }
682 }
683 callbacks.add(callback);
684
685 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
686 return NO_ERROR;
687}
688
689status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700690 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700691{
692 Mutex::Autolock _l(mLock);
693 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
694 if (ioIndex < 0) {
695 return INVALID_OPERATION;
696 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700697 Vector < wp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700698
699 size_t cbIndex;
700 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700701 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700702 break;
703 }
704 }
705 if (cbIndex == callbacks.size()) {
706 return INVALID_OPERATION;
707 }
708 callbacks.removeAt(cbIndex);
709 if (callbacks.size() != 0) {
710 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
711 } else {
712 mAudioDeviceCallbacks.removeItem(audioIo);
713 }
714 return NO_ERROR;
715}
716
717/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700718{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700719 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 gAudioErrorCallback = cb;
721}
722
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700723/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
724{
725 Mutex::Autolock _l(gLock);
726 gDynPolicyCallback = cb;
727}
728
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800729/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
730{
731 Mutex::Autolock _l(gLock);
732 gRecordConfigCallback = cb;
733}
734
Eric Laurentc2f1f072009-07-17 12:17:14 -0700735// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800736// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700737sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
738sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
739
740
Glenn Kasten18a6d902012-09-24 11:27:56 -0700741// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800742const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800744 sp<IAudioPolicyService> ap;
745 sp<AudioPolicyServiceClient> apc;
746 {
747 Mutex::Autolock _l(gLockAPS);
748 if (gAudioPolicyService == 0) {
749 sp<IServiceManager> sm = defaultServiceManager();
750 sp<IBinder> binder;
751 do {
752 binder = sm->getService(String16("media.audio_policy"));
753 if (binder != 0)
754 break;
755 ALOGW("AudioPolicyService not published, waiting...");
756 usleep(500000); // 0.5 s
757 } while (true);
758 if (gAudioPolicyServiceClient == NULL) {
759 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
760 }
761 binder->linkToDeath(gAudioPolicyServiceClient);
762 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
763 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
764 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700765 // Make sure callbacks can be received by gAudioPolicyServiceClient
766 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700767 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800768 ap = gAudioPolicyService;
769 }
770 if (apc != 0) {
771 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700772 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800773
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800774 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700775}
776
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700777// ---------------------------------------------------------------------------
778
Dima Zavinfce7a472011-04-19 22:30:36 -0700779status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
780 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800781 const char *device_address,
782 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700783{
784 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700785 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800786 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700787
Eric Laurentc2f1f072009-07-17 12:17:14 -0700788 if (aps == 0) return PERMISSION_DENIED;
789
Eric Laurent71b63e32011-09-02 14:20:56 -0700790 if (device_address != NULL) {
791 address = device_address;
792 }
Paul McLeane743a472015-01-28 11:07:31 -0800793 if (device_name != NULL) {
794 name = device_name;
795 }
796 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700797}
798
Dima Zavinfce7a472011-04-19 22:30:36 -0700799audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700800 const char *device_address)
801{
802 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700803 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700804
805 return aps->getDeviceConnectionState(device, device_address);
806}
807
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800808status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
809 const char *device_address,
810 const char *device_name)
811{
812 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
813 const char *address = "";
814 const char *name = "";
815
816 if (aps == 0) return PERMISSION_DENIED;
817
818 if (device_address != NULL) {
819 address = device_address;
820 }
821 if (device_name != NULL) {
822 name = device_name;
823 }
824 return aps->handleDeviceConfigChange(device, address, name);
825}
826
Glenn Kastenf78aee72012-01-04 11:00:47 -0800827status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700828{
Glenn Kasten347966c2012-01-18 14:58:32 -0800829 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
831 if (aps == 0) return PERMISSION_DENIED;
832
833 return aps->setPhoneState(state);
834}
835
Dima Zavinfce7a472011-04-19 22:30:36 -0700836status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700837{
838 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
839 if (aps == 0) return PERMISSION_DENIED;
840 return aps->setForceUse(usage, config);
841}
842
Dima Zavinfce7a472011-04-19 22:30:36 -0700843audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700844{
845 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700846 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700847 return aps->getForceUse(usage);
848}
849
850
Eric Laurentf4e63452017-11-06 19:31:46 +0000851audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700852{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700853 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
854 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000855 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700856}
857
Eric Laurente83b55d2014-11-14 10:06:21 -0800858status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
859 audio_io_handle_t *output,
860 audio_session_t session,
861 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700862 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800863 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800864 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700865 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800866 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700867{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700868 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800869 if (aps == 0) return NO_INIT;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700870 return aps->getOutputForAttr(attr, output, session, stream, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800871 config,
872 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700873}
874
Eric Laurentde070132010-07-13 04:45:46 -0700875status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700876 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800877 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700878{
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
880 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700881 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882}
883
Eric Laurentde070132010-07-13 04:45:46 -0700884status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700885 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800886 audio_session_t session)
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 Laurentde070132010-07-13 04:45:46 -0700890 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700891}
892
Eric Laurente83b55d2014-11-14 10:06:21 -0800893void AudioSystem::releaseOutput(audio_io_handle_t output,
894 audio_stream_type_t stream,
895 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
898 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800899 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700900}
901
Eric Laurentcaf7f482014-11-25 17:50:47 -0800902status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
903 audio_io_handle_t *input,
904 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700905 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700906 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800907 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600908 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700909 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800910 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911{
912 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800913 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600914 return aps->getInputForAttr(
Eric Laurentb2379ba2016-05-23 17:42:12 -0700915 attr, input, session, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800916 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700917}
918
Eric Laurent4dc68062014-07-28 17:26:49 -0700919status_t AudioSystem::startInput(audio_io_handle_t input,
920 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700921{
922 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
923 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700924 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700925}
926
Eric Laurent4dc68062014-07-28 17:26:49 -0700927status_t AudioSystem::stopInput(audio_io_handle_t input,
928 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700929{
930 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
931 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700932 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700933}
934
Eric Laurent4dc68062014-07-28 17:26:49 -0700935void AudioSystem::releaseInput(audio_io_handle_t input,
936 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700937{
938 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
939 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700940 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941}
942
Dima Zavinfce7a472011-04-19 22:30:36 -0700943status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944 int indexMin,
945 int indexMax)
946{
947 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
948 if (aps == 0) return PERMISSION_DENIED;
949 return aps->initStreamVolume(stream, indexMin, indexMax);
950}
951
Eric Laurent83844cc2011-11-18 16:43:31 -0800952status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
953 int index,
954 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955{
956 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
957 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800958 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959}
960
Eric Laurent83844cc2011-11-18 16:43:31 -0800961status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
962 int *index,
963 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700964{
965 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
966 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800967 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700968}
969
Dima Zavinfce7a472011-04-19 22:30:36 -0700970uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700971{
972 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
973 if (aps == 0) return 0;
974 return aps->getStrategyForStream(stream);
975}
976
Eric Laurent63742522012-03-08 13:42:42 -0800977audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800978{
979 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800980 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800981 return aps->getDevicesForStream(stream);
982}
983
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700984audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700985{
986 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800987 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700988 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700989 return aps->getOutputForEffect(desc);
990}
991
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700992status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700993 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700994 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800995 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700996 int id)
997{
998 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
999 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001000 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001001}
1002
1003status_t AudioSystem::unregisterEffect(int id)
1004{
1005 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1006 if (aps == 0) return PERMISSION_DENIED;
1007 return aps->unregisterEffect(id);
1008}
1009
Eric Laurentdb7c0792011-08-10 10:37:50 -07001010status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1011{
1012 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1013 if (aps == 0) return PERMISSION_DENIED;
1014 return aps->setEffectEnabled(id, enabled);
1015}
1016
Glenn Kastenfff6d712012-01-12 16:38:12 -08001017status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001018{
Eric Laurenteda6c362011-02-02 09:33:30 -08001019 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1020 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001021 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001022 *state = aps->isStreamActive(stream, inPastMs);
1023 return NO_ERROR;
1024}
1025
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001026status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1027 uint32_t inPastMs)
1028{
1029 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1030 if (aps == 0) return PERMISSION_DENIED;
1031 if (state == NULL) return BAD_VALUE;
1032 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1033 return NO_ERROR;
1034}
1035
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001036status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1037{
1038 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1039 if (aps == 0) return PERMISSION_DENIED;
1040 if (state == NULL) return BAD_VALUE;
1041 *state = aps->isSourceActive(stream);
1042 return NO_ERROR;
1043}
1044
Glenn Kasten3b16c762012-11-14 08:44:39 -08001045uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001046{
1047 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1048 if (af == 0) return 0;
1049 return af->getPrimaryOutputSamplingRate();
1050}
1051
Glenn Kastene33054e2012-11-14 12:54:39 -08001052size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001053{
1054 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1055 if (af == 0) return 0;
1056 return af->getPrimaryOutputFrameCount();
1057}
Eric Laurenteda6c362011-02-02 09:33:30 -08001058
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001059status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1060{
1061 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1062 if (af == 0) return PERMISSION_DENIED;
1063 return af->setLowRamDevice(isLowRamDevice);
1064}
1065
Eric Laurent9f6530f2011-08-30 10:18:54 -07001066void AudioSystem::clearAudioConfigCache()
1067{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001068 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001069 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001070 {
1071 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001072 if (gAudioFlingerClient != 0) {
1073 gAudioFlingerClient->clearIoCache();
1074 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001075 gAudioFlinger.clear();
1076 }
1077 {
1078 Mutex::Autolock _l(gLockAPS);
1079 gAudioPolicyService.clear();
1080 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001081}
1082
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001083bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1084{
1085 ALOGV("isOffloadSupported()");
1086 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1087 if (aps == 0) return false;
1088 return aps->isOffloadSupported(info);
1089}
1090
Eric Laurent203b1a12014-04-01 10:34:16 -07001091status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1092 audio_port_type_t type,
1093 unsigned int *num_ports,
1094 struct audio_port *ports,
1095 unsigned int *generation)
1096{
1097 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1098 if (aps == 0) return PERMISSION_DENIED;
1099 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1100}
1101
1102status_t AudioSystem::getAudioPort(struct audio_port *port)
1103{
1104 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1105 if (aps == 0) return PERMISSION_DENIED;
1106 return aps->getAudioPort(port);
1107}
1108
1109status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1110 audio_patch_handle_t *handle)
1111{
1112 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1113 if (aps == 0) return PERMISSION_DENIED;
1114 return aps->createAudioPatch(patch, handle);
1115}
1116
1117status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1118{
1119 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1120 if (aps == 0) return PERMISSION_DENIED;
1121 return aps->releaseAudioPatch(handle);
1122}
1123
1124status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1125 struct audio_patch *patches,
1126 unsigned int *generation)
1127{
1128 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1129 if (aps == 0) return PERMISSION_DENIED;
1130 return aps->listAudioPatches(num_patches, patches, generation);
1131}
1132
1133status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1134{
1135 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1136 if (aps == 0) return PERMISSION_DENIED;
1137 return aps->setAudioPortConfig(config);
1138}
1139
Eric Laurent296fb132015-05-01 11:38:42 -07001140status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001141{
Eric Laurentb28753e2015-04-01 13:06:28 -07001142 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1143 if (aps == 0) return PERMISSION_DENIED;
1144
1145 Mutex::Autolock _l(gLockAPS);
1146 if (gAudioPolicyServiceClient == 0) {
1147 return NO_INIT;
1148 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001149 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1150 if (ret == 1) {
1151 aps->setAudioPortCallbacksEnabled(true);
1152 }
1153 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001154}
1155
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001156/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001157status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001158{
1159 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1160 if (aps == 0) return PERMISSION_DENIED;
1161
1162 Mutex::Autolock _l(gLockAPS);
1163 if (gAudioPolicyServiceClient == 0) {
1164 return NO_INIT;
1165 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001166 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1167 if (ret == 0) {
1168 aps->setAudioPortCallbacksEnabled(false);
1169 }
1170 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001171}
1172
1173status_t AudioSystem::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001174 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001175{
1176 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1177 if (afc == 0) {
1178 return NO_INIT;
1179 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001180 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1181 if (status == NO_ERROR) {
1182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1183 if (af != 0) {
1184 af->registerClient(afc);
1185 }
1186 }
1187 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001188}
1189
1190status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001191 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001192{
1193 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1194 if (afc == 0) {
1195 return NO_INIT;
1196 }
1197 return afc->removeAudioDeviceCallback(callback, audioIo);
1198}
1199
1200audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1201{
1202 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1203 if (af == 0) return PERMISSION_DENIED;
1204 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1205 if (desc == 0) {
1206 return AUDIO_PORT_HANDLE_NONE;
1207 }
1208 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001209}
1210
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001211status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1212 audio_io_handle_t *ioHandle,
1213 audio_devices_t *device)
1214{
1215 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1216 if (aps == 0) return PERMISSION_DENIED;
1217 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1218}
1219
1220status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1221{
1222 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1223 if (aps == 0) return PERMISSION_DENIED;
1224 return aps->releaseSoundTriggerSession(session);
1225}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001226
1227audio_mode_t AudioSystem::getPhoneState()
1228{
1229 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1230 if (aps == 0) return AUDIO_MODE_INVALID;
1231 return aps->getPhoneState();
1232}
1233
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001234status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001235{
1236 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1237 if (aps == 0) return PERMISSION_DENIED;
1238 return aps->registerPolicyMixes(mixes, registration);
1239}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001240
Eric Laurent554a2772015-04-10 11:29:24 -07001241status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1242 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07001243 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001244{
1245 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1246 if (aps == 0) return PERMISSION_DENIED;
1247 return aps->startAudioSource(source, attributes, handle);
1248}
1249
Glenn Kasten559d4392016-03-29 13:42:57 -07001250status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001251{
1252 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1253 if (aps == 0) return PERMISSION_DENIED;
1254 return aps->stopAudioSource(handle);
1255}
1256
Andy Hung2ddee192015-12-18 17:34:44 -08001257status_t AudioSystem::setMasterMono(bool mono)
1258{
1259 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1260 if (aps == 0) return PERMISSION_DENIED;
1261 return aps->setMasterMono(mono);
1262}
1263
1264status_t AudioSystem::getMasterMono(bool *mono)
1265{
1266 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1267 if (aps == 0) return PERMISSION_DENIED;
1268 return aps->getMasterMono(mono);
1269}
1270
Eric Laurentac9cef52017-06-09 15:46:26 -07001271float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1272{
1273 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1274 if (aps == 0) return NAN;
1275 return aps->getStreamVolumeDB(stream, index, device);
1276}
1277
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278// ---------------------------------------------------------------------------
1279
Eric Laurente8726fe2015-06-26 09:39:24 -07001280int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001281 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001282{
1283 Mutex::Autolock _l(mLock);
1284 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001285 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001286 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001287 }
1288 }
Eric Laurent296fb132015-05-01 11:38:42 -07001289 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001290 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001291}
1292
Eric Laurente8726fe2015-06-26 09:39:24 -07001293int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001294 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001295{
1296 Mutex::Autolock _l(mLock);
1297 size_t i;
1298 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001299 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001300 break;
1301 }
1302 }
1303 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001304 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001305 }
1306 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001307 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001308}
1309
Eric Laurent296fb132015-05-01 11:38:42 -07001310
Eric Laurentb28753e2015-04-01 13:06:28 -07001311void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1312{
1313 Mutex::Autolock _l(mLock);
1314 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1315 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1316 }
1317}
1318
1319void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1320{
1321 Mutex::Autolock _l(mLock);
1322 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1323 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1324 }
1325}
1326
Jean-Michel Trivide801052015-04-14 19:10:14 -07001327void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1328 String8 regId, int32_t state)
1329{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001330 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1331 dynamic_policy_callback cb = NULL;
1332 {
1333 Mutex::Autolock _l(AudioSystem::gLock);
1334 cb = gDynPolicyCallback;
1335 }
1336
1337 if (cb != NULL) {
1338 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1339 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001340}
1341
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001342void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001343 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001344 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1345 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001346 record_config_callback cb = NULL;
1347 {
1348 Mutex::Autolock _l(AudioSystem::gLock);
1349 cb = gRecordConfigCallback;
1350 }
1351
1352 if (cb != NULL) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001353 cb(event, clientInfo, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001354 }
1355}
1356
Glenn Kasten4944acb2013-08-19 08:39:20 -07001357void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1358{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001359 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001360 Mutex::Autolock _l(mLock);
1361 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1362 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001363 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001364 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001365 {
1366 Mutex::Autolock _l(gLockAPS);
1367 AudioSystem::gAudioPolicyService.clear();
1368 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001369
Steve Block5ff1dd52012-01-05 23:22:43 +00001370 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001371}
1372
Glenn Kasten40bc9062015-03-20 09:09:33 -07001373} // namespace android