blob: 1c4a80e82a34a1eb9e4abee95c5cbd9941cd66d0 [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;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080042record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080045const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080047 sp<IAudioFlinger> af;
48 sp<AudioFlingerClient> afc;
49 {
50 Mutex::Autolock _l(gLock);
51 if (gAudioFlinger == 0) {
52 sp<IServiceManager> sm = defaultServiceManager();
53 sp<IBinder> binder;
54 do {
55 binder = sm->getService(String16("media.audio_flinger"));
56 if (binder != 0)
57 break;
58 ALOGW("AudioFlinger not published, waiting...");
59 usleep(500000); // 0.5 s
60 } while (true);
61 if (gAudioFlingerClient == NULL) {
62 gAudioFlingerClient = new AudioFlingerClient();
63 } else {
64 if (gAudioErrorCallback) {
65 gAudioErrorCallback(NO_ERROR);
66 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080068 binder->linkToDeath(gAudioFlingerClient);
69 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
70 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
71 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070072 // Make sure callbacks can be received by gAudioFlingerClient
73 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070074 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080075 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080077 if (afc != 0) {
78 af->registerClient(afc);
79 }
80 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081}
82
Eric Laurent296fb132015-05-01 11:38:42 -070083const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
84{
85 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
86 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
87 if (af == 0) return 0;
88 Mutex::Autolock _l(gLock);
89 return gAudioFlingerClient;
90}
91
92sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
93{
94 sp<AudioIoDescriptor> desc;
95 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
96 if (afc != 0) {
97 desc = afc->getIoDescriptor(ioHandle);
98 }
99 return desc;
100}
101
Eric Laurent46291612013-07-18 14:38:44 -0700102/* static */ status_t AudioSystem::checkAudioFlinger()
103{
104 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
105 return NO_ERROR;
106 }
107 return DEAD_OBJECT;
108}
109
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700110// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
111
Glenn Kasten4944acb2013-08-19 08:39:20 -0700112status_t AudioSystem::muteMicrophone(bool state)
113{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800114 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
115 if (af == 0) return PERMISSION_DENIED;
116 return af->setMicMute(state);
117}
118
Glenn Kasten4944acb2013-08-19 08:39:20 -0700119status_t AudioSystem::isMicrophoneMuted(bool* state)
120{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
122 if (af == 0) return PERMISSION_DENIED;
123 *state = af->getMicMute();
124 return NO_ERROR;
125}
126
127status_t AudioSystem::setMasterVolume(float value)
128{
129 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
130 if (af == 0) return PERMISSION_DENIED;
131 af->setMasterVolume(value);
132 return NO_ERROR;
133}
134
135status_t AudioSystem::setMasterMute(bool mute)
136{
137 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
138 if (af == 0) return PERMISSION_DENIED;
139 af->setMasterMute(mute);
140 return NO_ERROR;
141}
142
143status_t AudioSystem::getMasterVolume(float* volume)
144{
145 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
146 if (af == 0) return PERMISSION_DENIED;
147 *volume = af->masterVolume();
148 return NO_ERROR;
149}
150
151status_t AudioSystem::getMasterMute(bool* mute)
152{
153 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
154 if (af == 0) return PERMISSION_DENIED;
155 *mute = af->masterMute();
156 return NO_ERROR;
157}
158
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800159status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
160 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161{
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
164 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700165 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 return NO_ERROR;
167}
168
Glenn Kastenfff6d712012-01-12 16:38:12 -0800169status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170{
Dima Zavinfce7a472011-04-19 22:30:36 -0700171 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
173 if (af == 0) return PERMISSION_DENIED;
174 af->setStreamMute(stream, mute);
175 return NO_ERROR;
176}
177
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800178status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
179 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180{
Dima Zavinfce7a472011-04-19 22:30:36 -0700181 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700184 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 return NO_ERROR;
186}
187
Glenn Kastenfff6d712012-01-12 16:38:12 -0800188status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189{
Dima Zavinfce7a472011-04-19 22:30:36 -0700190 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
192 if (af == 0) return PERMISSION_DENIED;
193 *mute = af->streamMute(stream);
194 return NO_ERROR;
195}
196
Glenn Kastenf78aee72012-01-04 11:00:47 -0800197status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800199 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
201 if (af == 0) return PERMISSION_DENIED;
202 return af->setMode(mode);
203}
204
Glenn Kasten4944acb2013-08-19 08:39:20 -0700205status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
206{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
208 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700209 return af->setParameters(ioHandle, keyValuePairs);
210}
211
Glenn Kasten4944acb2013-08-19 08:39:20 -0700212String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
213{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700214 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
215 String8 result = String8("");
216 if (af == 0) return result;
217
218 result = af->getParameters(ioHandle, keys);
219 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220}
221
Glenn Kastenc23885e2013-12-19 16:35:18 -0800222status_t AudioSystem::setParameters(const String8& keyValuePairs)
223{
Glenn Kasten142f5192014-03-25 17:44:59 -0700224 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800225}
226
227String8 AudioSystem::getParameters(const String8& keys)
228{
Glenn Kasten142f5192014-03-25 17:44:59 -0700229 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800230}
231
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232// convert volume steps to natural log scale
233
234// change this value to change volume scaling
235static const float dBPerStep = 0.5f;
236// shouldn't need to touch these
237static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
238static const float dBConvertInverse = 1.0f / dBConvert;
239
240float AudioSystem::linearToLog(int volume)
241{
242 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000243 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 // return v;
245 return volume ? exp(float(100 - volume) * dBConvert) : 0;
246}
247
248int AudioSystem::logToLinear(float volume)
249{
250 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000251 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 // return v;
253 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
254}
255
Eric Laurent21da6472017-11-09 16:29:26 -0800256/* static */ size_t AudioSystem::calculateMinFrameCount(
257 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
258 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
259{
260 // Ensure that buffer depth covers at least audio hardware latency
261 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
262 if (minBufCount < 2) {
263 minBufCount = 2;
264 }
265#if 0
266 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
267 // but keeping the code here to make it easier to add later.
268 if (minBufCount < notificationsPerBufferReq) {
269 minBufCount = notificationsPerBufferReq;
270 }
271#endif
272 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
273 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
274 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
275 /*, notificationsPerBufferReq*/);
276 return minBufCount * sourceFramesNeededWithTimestretch(
277 sampleRate, afFrameCount, afSampleRate, speed);
278}
279
280
Glenn Kasten3b16c762012-11-14 08:44:39 -0800281status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700283 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284
Dima Zavinfce7a472011-04-19 22:30:36 -0700285 if (streamType == AUDIO_STREAM_DEFAULT) {
286 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 }
288
Glenn Kastenfff6d712012-01-12 16:38:12 -0800289 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 if (output == 0) {
291 return PERMISSION_DENIED;
292 }
293
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700294 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700295}
296
Glenn Kasten2c073da2016-02-26 09:14:08 -0800297status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800298 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700299{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800300 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
301 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800302 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
303 if (desc == 0) {
304 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700305 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800306 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800308 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800309 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800310 return BAD_VALUE;
311 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312
Glenn Kasten2c073da2016-02-26 09:14:08 -0800313 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
Glenn Kastene33054e2012-11-14 12:54:39 -0800318status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 if (streamType == AUDIO_STREAM_DEFAULT) {
323 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700325
Glenn Kastenfff6d712012-01-12 16:38:12 -0800326 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700327 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 return PERMISSION_DENIED;
329 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700331 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700332}
333
Glenn Kasten2c073da2016-02-26 09:14:08 -0800334status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800335 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700336{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800337 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
338 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800339 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
340 if (desc == 0) {
341 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700342 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800343 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800345 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800346 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800347 return BAD_VALUE;
348 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700349
Glenn Kasten2c073da2016-02-26 09:14:08 -0800350 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 return NO_ERROR;
353}
354
Glenn Kastenfff6d712012-01-12 16:38:12 -0800355status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358
Dima Zavinfce7a472011-04-19 22:30:36 -0700359 if (streamType == AUDIO_STREAM_DEFAULT) {
360 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700362
Glenn Kastenfff6d712012-01-12 16:38:12 -0800363 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700364 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 return PERMISSION_DENIED;
366 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367
Glenn Kasten241618f2014-03-25 17:48:57 -0700368 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700369}
370
371status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700372 uint32_t* latency)
373{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800374 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
375 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700376 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700377 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700378 *latency = af->latency(output);
379 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700380 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700381 }
382
Glenn Kasten241618f2014-03-25 17:48:57 -0700383 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 return NO_ERROR;
386}
387
Glenn Kastendd8104c2012-07-02 12:42:44 -0700388status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
389 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390{
Eric Laurent296fb132015-05-01 11:38:42 -0700391 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
392 if (afc == 0) {
393 return NO_INIT;
394 }
395 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396}
397
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700398status_t AudioSystem::setVoiceVolume(float value)
399{
400 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
401 if (af == 0) return PERMISSION_DENIED;
402 return af->setVoiceVolume(value);
403}
404
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000405status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700406 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800407{
408 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
409 if (af == 0) return PERMISSION_DENIED;
410
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000411 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800412}
413
Glenn Kasten4944acb2013-08-19 08:39:20 -0700414uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
415{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800416 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800417 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800418 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700419 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800420
421 result = af->getInputFramesLost(ioHandle);
422 return result;
423}
424
Glenn Kasteneeecb982016-02-26 10:44:04 -0800425audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700426{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700427 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700428 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800429 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700430}
431
Glenn Kastend848eb42016-03-08 13:42:11 -0800432void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700433{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700434 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
435 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800436 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700437 }
438}
439
Glenn Kastend848eb42016-03-08 13:42:11 -0800440void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700441{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700442 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
443 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800444 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700445 }
446}
447
Eric Laurent93c3d412014-08-01 14:48:35 -0700448audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
449{
450 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
451 if (af == 0) return AUDIO_HW_SYNC_INVALID;
452 return af->getAudioHwSyncForSession(sessionId);
453}
454
Eric Laurent72e3f392015-05-20 14:43:50 -0700455status_t AudioSystem::systemReady()
456{
457 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
458 if (af == 0) return NO_INIT;
459 return af->systemReady();
460}
461
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700462status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
463 size_t* frameCount)
464{
465 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
466 if (af == 0) return PERMISSION_DENIED;
467 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
468 if (desc == 0) {
469 *frameCount = af->frameCountHAL(ioHandle);
470 } else {
471 *frameCount = desc->mFrameCountHAL;
472 }
473 if (*frameCount == 0) {
474 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
475 return BAD_VALUE;
476 }
477
478 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
479
480 return NO_ERROR;
481}
482
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483// ---------------------------------------------------------------------------
484
Eric Laurent73e26b62015-04-27 16:55:58 -0700485
486void AudioSystem::AudioFlingerClient::clearIoCache()
487{
488 Mutex::Autolock _l(mLock);
489 mIoDescriptors.clear();
490 mInBuffSize = 0;
491 mInSamplingRate = 0;
492 mInFormat = AUDIO_FORMAT_DEFAULT;
493 mInChannelMask = AUDIO_CHANNEL_NONE;
494}
495
Glenn Kasten4944acb2013-08-19 08:39:20 -0700496void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
497{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800498 audio_error_callback cb = NULL;
499 {
500 Mutex::Autolock _l(AudioSystem::gLock);
501 AudioSystem::gAudioFlinger.clear();
502 cb = gAudioErrorCallback;
503 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504
Eric Laurent73e26b62015-04-27 16:55:58 -0700505 // clear output handles and stream to output map caches
506 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507
Eric Laurentf6778fd2014-11-18 17:26:58 -0800508 if (cb) {
509 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000511 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512}
513
Eric Laurent73e26b62015-04-27 16:55:58 -0700514void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
515 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100516 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517
Eric Laurent73e26b62015-04-27 16:55:58 -0700518 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700519
Eric Laurent296fb132015-05-01 11:38:42 -0700520 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurentad2e7b92017-09-14 20:06:42 -0700521 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Eric Laurent296fb132015-05-01 11:38:42 -0700523 {
524 Mutex::Autolock _l(mLock);
525
526 switch (event) {
527 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700528 case AUDIO_OUTPUT_REGISTERED:
529 case AUDIO_INPUT_OPENED:
530 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700531 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700532 if (oldDesc == 0) {
533 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
534 } else {
535 deviceId = oldDesc->getDeviceId();
536 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700537 }
Eric Laurent296fb132015-05-01 11:38:42 -0700538
539 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
540 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700541 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
542 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
543 if (ioIndex >= 0) {
544 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
545 }
Eric Laurent296fb132015-05-01 11:38:42 -0700546 }
547 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700548 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
549 "frameCount %zu deviceId %d",
550 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
551 "output" : "input",
552 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
553 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700554 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
555 ioDesc->mFrameCount, ioDesc->getDeviceId());
556 } break;
557 case AUDIO_OUTPUT_CLOSED:
558 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700559 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700560 ALOGW("ioConfigChanged() closing unknown %s %d",
561 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
562 break;
563 }
564 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700565 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700566
Eric Laurent296fb132015-05-01 11:38:42 -0700567 mIoDescriptors.removeItem(ioDesc->mIoHandle);
568 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
569 } break;
570
571 case AUDIO_OUTPUT_CONFIG_CHANGED:
572 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700573 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700574 if (oldDesc == 0) {
575 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
576 break;
577 }
578
579 deviceId = oldDesc->getDeviceId();
580 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
581
582 if (deviceId != ioDesc->getDeviceId()) {
583 deviceId = ioDesc->getDeviceId();
584 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
585 if (ioIndex >= 0) {
586 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
587 }
588 }
589 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700590 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700591 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
592 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800593 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
594 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700595
Eric Laurentc2f1f072009-07-17 12:17:14 -0700596 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597 }
Eric Laurent296fb132015-05-01 11:38:42 -0700598 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700599 bool callbackRemoved = false;
Eric Laurent296fb132015-05-01 11:38:42 -0700600 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
Eric Laurentad2e7b92017-09-14 20:06:42 -0700601 for (size_t i = 0; i < callbacks.size(); ) {
602 sp<AudioDeviceCallback> callback = callbacks[i].promote();
603 if (callback.get() != nullptr) {
604 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
605 i++;
606 } else {
607 callbacks.removeAt(i);
608 callbackRemoved = true;
609 }
610 }
611 // clean up callback list while we are here if some clients have disappeared without
612 // unregistering their callback
613 if (callbackRemoved) {
614 Mutex::Autolock _l(mLock);
615 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700616 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617}
618
Eric Laurent73e26b62015-04-27 16:55:58 -0700619status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
620 uint32_t sampleRate, audio_format_t format,
621 audio_channel_mask_t channelMask, size_t* buffSize)
622{
623 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
624 if (af == 0) {
625 return PERMISSION_DENIED;
626 }
627 Mutex::Autolock _l(mLock);
628 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
629 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
630 || (channelMask != mInChannelMask)) {
631 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
632 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800633 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700634 sampleRate, format, channelMask);
635 return BAD_VALUE;
636 }
637 // A benign race is possible here: we could overwrite a fresher cache entry
638 // save the request params
639 mInSamplingRate = sampleRate;
640 mInFormat = format;
641 mInChannelMask = channelMask;
642
643 mInBuffSize = inBuffSize;
644 }
645
646 *buffSize = mInBuffSize;
647
648 return NO_ERROR;
649}
650
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700651sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700652{
653 sp<AudioIoDescriptor> desc;
654 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
655 if (index >= 0) {
656 desc = mIoDescriptors.valueAt(index);
657 }
658 return desc;
659}
660
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700661sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
662{
663 Mutex::Autolock _l(mLock);
664 return getIoDescriptor_l(ioHandle);
665}
666
Eric Laurent296fb132015-05-01 11:38:42 -0700667status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700668 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700669{
670 Mutex::Autolock _l(mLock);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700671 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurent296fb132015-05-01 11:38:42 -0700672 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
673 if (ioIndex >= 0) {
674 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
675 }
676
677 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700678 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700679 return INVALID_OPERATION;
680 }
681 }
682 callbacks.add(callback);
683
684 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
685 return NO_ERROR;
686}
687
688status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700689 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700690{
691 Mutex::Autolock _l(mLock);
692 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
693 if (ioIndex < 0) {
694 return INVALID_OPERATION;
695 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700696 Vector < wp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700697
698 size_t cbIndex;
699 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700700 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700701 break;
702 }
703 }
704 if (cbIndex == callbacks.size()) {
705 return INVALID_OPERATION;
706 }
707 callbacks.removeAt(cbIndex);
708 if (callbacks.size() != 0) {
709 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
710 } else {
711 mAudioDeviceCallbacks.removeItem(audioIo);
712 }
713 return NO_ERROR;
714}
715
716/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700717{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 gAudioErrorCallback = cb;
720}
721
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700722/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
723{
724 Mutex::Autolock _l(gLock);
725 gDynPolicyCallback = cb;
726}
727
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800728/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
729{
730 Mutex::Autolock _l(gLock);
731 gRecordConfigCallback = cb;
732}
733
Eric Laurentc2f1f072009-07-17 12:17:14 -0700734// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800735// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700736sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
737sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
738
739
Glenn Kasten18a6d902012-09-24 11:27:56 -0700740// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800741const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700742{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800743 sp<IAudioPolicyService> ap;
744 sp<AudioPolicyServiceClient> apc;
745 {
746 Mutex::Autolock _l(gLockAPS);
747 if (gAudioPolicyService == 0) {
748 sp<IServiceManager> sm = defaultServiceManager();
749 sp<IBinder> binder;
750 do {
751 binder = sm->getService(String16("media.audio_policy"));
752 if (binder != 0)
753 break;
754 ALOGW("AudioPolicyService not published, waiting...");
755 usleep(500000); // 0.5 s
756 } while (true);
757 if (gAudioPolicyServiceClient == NULL) {
758 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
759 }
760 binder->linkToDeath(gAudioPolicyServiceClient);
761 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
762 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
763 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700764 // Make sure callbacks can be received by gAudioPolicyServiceClient
765 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700766 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800767 ap = gAudioPolicyService;
768 }
769 if (apc != 0) {
770 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700771 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800772
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800773 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700774}
775
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700776// ---------------------------------------------------------------------------
777
Dima Zavinfce7a472011-04-19 22:30:36 -0700778status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
779 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800780 const char *device_address,
781 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700782{
783 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700784 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800785 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700786
Eric Laurentc2f1f072009-07-17 12:17:14 -0700787 if (aps == 0) return PERMISSION_DENIED;
788
Eric Laurent71b63e32011-09-02 14:20:56 -0700789 if (device_address != NULL) {
790 address = device_address;
791 }
Paul McLeane743a472015-01-28 11:07:31 -0800792 if (device_name != NULL) {
793 name = device_name;
794 }
795 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700796}
797
Dima Zavinfce7a472011-04-19 22:30:36 -0700798audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700799 const char *device_address)
800{
801 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700802 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700803
804 return aps->getDeviceConnectionState(device, device_address);
805}
806
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800807status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
808 const char *device_address,
809 const char *device_name)
810{
811 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
812 const char *address = "";
813 const char *name = "";
814
815 if (aps == 0) return PERMISSION_DENIED;
816
817 if (device_address != NULL) {
818 address = device_address;
819 }
820 if (device_name != NULL) {
821 name = device_name;
822 }
823 return aps->handleDeviceConfigChange(device, address, name);
824}
825
Glenn Kastenf78aee72012-01-04 11:00:47 -0800826status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700827{
Glenn Kasten347966c2012-01-18 14:58:32 -0800828 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700829 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
830 if (aps == 0) return PERMISSION_DENIED;
831
832 return aps->setPhoneState(state);
833}
834
Dima Zavinfce7a472011-04-19 22:30:36 -0700835status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700836{
837 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
838 if (aps == 0) return PERMISSION_DENIED;
839 return aps->setForceUse(usage, config);
840}
841
Dima Zavinfce7a472011-04-19 22:30:36 -0700842audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700843{
844 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700845 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700846 return aps->getForceUse(usage);
847}
848
849
Eric Laurentf4e63452017-11-06 19:31:46 +0000850audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700851{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700852 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
853 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000854 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855}
856
Eric Laurente83b55d2014-11-14 10:06:21 -0800857status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
858 audio_io_handle_t *output,
859 audio_session_t session,
860 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200861 pid_t pid,
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;
Nadav Bar766fb022018-01-07 12:18:03 +0200870 return aps->getOutputForAttr(attr, output, session, stream, pid, 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 Laurentfee19762018-01-29 18:44:13 -0800907 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800908 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600909 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700910 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800911 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700912{
913 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800914 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600915 return aps->getInputForAttr(
Eric Laurentfee19762018-01-29 18:44:13 -0800916 attr, input, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800917 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918}
919
Eric Laurentfee19762018-01-29 18:44:13 -0800920status_t AudioSystem::startInput(audio_port_handle_t portId, bool *silenced)
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 Laurentfee19762018-01-29 18:44:13 -0800924 return aps->startInput(portId, silenced);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700925}
926
Eric Laurentfee19762018-01-29 18:44:13 -0800927status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700928{
929 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
930 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800931 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700932}
933
Eric Laurentfee19762018-01-29 18:44:13 -0800934void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700935{
936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
937 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800938 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700939}
940
Dima Zavinfce7a472011-04-19 22:30:36 -0700941status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700942 int indexMin,
943 int indexMax)
944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
946 if (aps == 0) return PERMISSION_DENIED;
947 return aps->initStreamVolume(stream, indexMin, indexMax);
948}
949
Eric Laurent83844cc2011-11-18 16:43:31 -0800950status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
951 int index,
952 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953{
954 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
955 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800956 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957}
958
Eric Laurent83844cc2011-11-18 16:43:31 -0800959status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
960 int *index,
961 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962{
963 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
964 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800965 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966}
967
Dima Zavinfce7a472011-04-19 22:30:36 -0700968uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700969{
970 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
971 if (aps == 0) return 0;
972 return aps->getStrategyForStream(stream);
973}
974
Eric Laurent63742522012-03-08 13:42:42 -0800975audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800978 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800979 return aps->getDevicesForStream(stream);
980}
981
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700982audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700983{
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800985 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700986 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700987 return aps->getOutputForEffect(desc);
988}
989
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700990status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700991 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700992 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800993 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700994 int id)
995{
996 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
997 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700998 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700999}
1000
1001status_t AudioSystem::unregisterEffect(int id)
1002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
1005 return aps->unregisterEffect(id);
1006}
1007
Eric Laurentdb7c0792011-08-10 10:37:50 -07001008status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1009{
1010 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1011 if (aps == 0) return PERMISSION_DENIED;
1012 return aps->setEffectEnabled(id, enabled);
1013}
1014
Glenn Kastenfff6d712012-01-12 16:38:12 -08001015status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001016{
Eric Laurenteda6c362011-02-02 09:33:30 -08001017 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1018 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001019 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001020 *state = aps->isStreamActive(stream, inPastMs);
1021 return NO_ERROR;
1022}
1023
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001024status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1025 uint32_t inPastMs)
1026{
1027 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1028 if (aps == 0) return PERMISSION_DENIED;
1029 if (state == NULL) return BAD_VALUE;
1030 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1031 return NO_ERROR;
1032}
1033
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001034status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1035{
1036 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1037 if (aps == 0) return PERMISSION_DENIED;
1038 if (state == NULL) return BAD_VALUE;
1039 *state = aps->isSourceActive(stream);
1040 return NO_ERROR;
1041}
1042
Glenn Kasten3b16c762012-11-14 08:44:39 -08001043uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001044{
1045 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1046 if (af == 0) return 0;
1047 return af->getPrimaryOutputSamplingRate();
1048}
1049
Glenn Kastene33054e2012-11-14 12:54:39 -08001050size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001051{
1052 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1053 if (af == 0) return 0;
1054 return af->getPrimaryOutputFrameCount();
1055}
Eric Laurenteda6c362011-02-02 09:33:30 -08001056
Andy Hung6f248bb2018-01-23 14:04:37 -08001057status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001058{
1059 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1060 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001061 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001062}
1063
Eric Laurent9f6530f2011-08-30 10:18:54 -07001064void AudioSystem::clearAudioConfigCache()
1065{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001066 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001068 {
1069 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001070 if (gAudioFlingerClient != 0) {
1071 gAudioFlingerClient->clearIoCache();
1072 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001073 gAudioFlinger.clear();
1074 }
1075 {
1076 Mutex::Autolock _l(gLockAPS);
1077 gAudioPolicyService.clear();
1078 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001079}
1080
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001081bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1082{
1083 ALOGV("isOffloadSupported()");
1084 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1085 if (aps == 0) return false;
1086 return aps->isOffloadSupported(info);
1087}
1088
Eric Laurent203b1a12014-04-01 10:34:16 -07001089status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1090 audio_port_type_t type,
1091 unsigned int *num_ports,
1092 struct audio_port *ports,
1093 unsigned int *generation)
1094{
1095 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1096 if (aps == 0) return PERMISSION_DENIED;
1097 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1098}
1099
1100status_t AudioSystem::getAudioPort(struct audio_port *port)
1101{
1102 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1103 if (aps == 0) return PERMISSION_DENIED;
1104 return aps->getAudioPort(port);
1105}
1106
1107status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1108 audio_patch_handle_t *handle)
1109{
1110 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1111 if (aps == 0) return PERMISSION_DENIED;
1112 return aps->createAudioPatch(patch, handle);
1113}
1114
1115status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1116{
1117 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1118 if (aps == 0) return PERMISSION_DENIED;
1119 return aps->releaseAudioPatch(handle);
1120}
1121
1122status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1123 struct audio_patch *patches,
1124 unsigned int *generation)
1125{
1126 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1127 if (aps == 0) return PERMISSION_DENIED;
1128 return aps->listAudioPatches(num_patches, patches, generation);
1129}
1130
1131status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1132{
1133 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1134 if (aps == 0) return PERMISSION_DENIED;
1135 return aps->setAudioPortConfig(config);
1136}
1137
Eric Laurent296fb132015-05-01 11:38:42 -07001138status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001139{
Eric Laurentb28753e2015-04-01 13:06:28 -07001140 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1141 if (aps == 0) return PERMISSION_DENIED;
1142
1143 Mutex::Autolock _l(gLockAPS);
1144 if (gAudioPolicyServiceClient == 0) {
1145 return NO_INIT;
1146 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001147 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1148 if (ret == 1) {
1149 aps->setAudioPortCallbacksEnabled(true);
1150 }
1151 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001152}
1153
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001154/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001155status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001156{
1157 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1158 if (aps == 0) return PERMISSION_DENIED;
1159
1160 Mutex::Autolock _l(gLockAPS);
1161 if (gAudioPolicyServiceClient == 0) {
1162 return NO_INIT;
1163 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001164 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1165 if (ret == 0) {
1166 aps->setAudioPortCallbacksEnabled(false);
1167 }
1168 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001169}
1170
1171status_t AudioSystem::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001172 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001173{
1174 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1175 if (afc == 0) {
1176 return NO_INIT;
1177 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001178 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1179 if (status == NO_ERROR) {
1180 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1181 if (af != 0) {
1182 af->registerClient(afc);
1183 }
1184 }
1185 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001186}
1187
1188status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001189 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001190{
1191 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1192 if (afc == 0) {
1193 return NO_INIT;
1194 }
1195 return afc->removeAudioDeviceCallback(callback, audioIo);
1196}
1197
1198audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1199{
1200 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1201 if (af == 0) return PERMISSION_DENIED;
1202 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1203 if (desc == 0) {
1204 return AUDIO_PORT_HANDLE_NONE;
1205 }
1206 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001207}
1208
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001209status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1210 audio_io_handle_t *ioHandle,
1211 audio_devices_t *device)
1212{
1213 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1214 if (aps == 0) return PERMISSION_DENIED;
1215 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1216}
1217
1218status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1219{
1220 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1221 if (aps == 0) return PERMISSION_DENIED;
1222 return aps->releaseSoundTriggerSession(session);
1223}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001224
1225audio_mode_t AudioSystem::getPhoneState()
1226{
1227 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1228 if (aps == 0) return AUDIO_MODE_INVALID;
1229 return aps->getPhoneState();
1230}
1231
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001232status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001233{
1234 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1235 if (aps == 0) return PERMISSION_DENIED;
1236 return aps->registerPolicyMixes(mixes, registration);
1237}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001238
Eric Laurent554a2772015-04-10 11:29:24 -07001239status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1240 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07001241 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001242{
1243 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1244 if (aps == 0) return PERMISSION_DENIED;
1245 return aps->startAudioSource(source, attributes, handle);
1246}
1247
Glenn Kasten559d4392016-03-29 13:42:57 -07001248status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001249{
1250 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1251 if (aps == 0) return PERMISSION_DENIED;
1252 return aps->stopAudioSource(handle);
1253}
1254
Andy Hung2ddee192015-12-18 17:34:44 -08001255status_t AudioSystem::setMasterMono(bool mono)
1256{
1257 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1258 if (aps == 0) return PERMISSION_DENIED;
1259 return aps->setMasterMono(mono);
1260}
1261
1262status_t AudioSystem::getMasterMono(bool *mono)
1263{
1264 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1265 if (aps == 0) return PERMISSION_DENIED;
1266 return aps->getMasterMono(mono);
1267}
1268
Eric Laurentac9cef52017-06-09 15:46:26 -07001269float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1270{
1271 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1272 if (aps == 0) return NAN;
1273 return aps->getStreamVolumeDB(stream, index, device);
1274}
1275
jiabin46a76fa2018-01-05 10:18:21 -08001276status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1277{
1278 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1279 if (af == 0) return PERMISSION_DENIED;
1280 return af->getMicrophones(microphones);
1281}
1282
Eric Laurentc2f1f072009-07-17 12:17:14 -07001283// ---------------------------------------------------------------------------
1284
Eric Laurente8726fe2015-06-26 09:39:24 -07001285int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001286 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001287{
1288 Mutex::Autolock _l(mLock);
1289 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001290 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001291 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001292 }
1293 }
Eric Laurent296fb132015-05-01 11:38:42 -07001294 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001295 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001296}
1297
Eric Laurente8726fe2015-06-26 09:39:24 -07001298int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001299 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001300{
1301 Mutex::Autolock _l(mLock);
1302 size_t i;
1303 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001304 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001305 break;
1306 }
1307 }
1308 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001309 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001310 }
1311 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001312 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001313}
1314
Eric Laurent296fb132015-05-01 11:38:42 -07001315
Eric Laurentb28753e2015-04-01 13:06:28 -07001316void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1317{
1318 Mutex::Autolock _l(mLock);
1319 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1320 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1321 }
1322}
1323
1324void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1325{
1326 Mutex::Autolock _l(mLock);
1327 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1328 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1329 }
1330}
1331
Jean-Michel Trivide801052015-04-14 19:10:14 -07001332void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1333 String8 regId, int32_t state)
1334{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001335 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1336 dynamic_policy_callback cb = NULL;
1337 {
1338 Mutex::Autolock _l(AudioSystem::gLock);
1339 cb = gDynPolicyCallback;
1340 }
1341
1342 if (cb != NULL) {
1343 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1344 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001345}
1346
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001347void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001348 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001349 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1350 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001351 record_config_callback cb = NULL;
1352 {
1353 Mutex::Autolock _l(AudioSystem::gLock);
1354 cb = gRecordConfigCallback;
1355 }
1356
1357 if (cb != NULL) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001358 cb(event, clientInfo, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001359 }
1360}
1361
Glenn Kasten4944acb2013-08-19 08:39:20 -07001362void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1363{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001364 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001365 Mutex::Autolock _l(mLock);
1366 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1367 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001368 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001369 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001370 {
1371 Mutex::Autolock _l(gLockAPS);
1372 AudioSystem::gAudioPolicyService.clear();
1373 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001374
Steve Block5ff1dd52012-01-05 23:22:43 +00001375 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001376}
1377
Glenn Kasten40bc9062015-03-20 09:09:33 -07001378} // namespace android