blob: e260fd838a0ba6f587142d1232d1809e6a8d1df0 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070022#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020023#include <binder/IPCThreadState.h>
Eric Laurent21da6472017-11-09 16:29:26 -080024#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070026#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070027#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <math.h>
29
Dima Zavin64760242011-05-11 14:15:23 -070030#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070031
Eric Laurentc2f1f072009-07-17 12:17:14 -070032// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
35
36// client singleton for AudioFlinger binder interface
37Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080038Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039sp<IAudioFlinger> AudioSystem::gAudioFlinger;
40sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
41audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070042dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080043record_config_callback AudioSystem::gRecordConfigCallback = NULL;
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) {
François Gaffie24437602018-04-23 15:08:59 +020079 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080080 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020081 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080082 }
83 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084}
85
Eric Laurent296fb132015-05-01 11:38:42 -070086const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
87{
88 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
89 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
90 if (af == 0) return 0;
91 Mutex::Autolock _l(gLock);
92 return gAudioFlingerClient;
93}
94
95sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
96{
97 sp<AudioIoDescriptor> desc;
98 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
99 if (afc != 0) {
100 desc = afc->getIoDescriptor(ioHandle);
101 }
102 return desc;
103}
104
Eric Laurent46291612013-07-18 14:38:44 -0700105/* static */ status_t AudioSystem::checkAudioFlinger()
106{
107 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
108 return NO_ERROR;
109 }
110 return DEAD_OBJECT;
111}
112
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700113// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
114
Glenn Kasten4944acb2013-08-19 08:39:20 -0700115status_t AudioSystem::muteMicrophone(bool state)
116{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
118 if (af == 0) return PERMISSION_DENIED;
119 return af->setMicMute(state);
120}
121
Glenn Kasten4944acb2013-08-19 08:39:20 -0700122status_t AudioSystem::isMicrophoneMuted(bool* state)
123{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
125 if (af == 0) return PERMISSION_DENIED;
126 *state = af->getMicMute();
127 return NO_ERROR;
128}
129
130status_t AudioSystem::setMasterVolume(float value)
131{
132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
133 if (af == 0) return PERMISSION_DENIED;
134 af->setMasterVolume(value);
135 return NO_ERROR;
136}
137
138status_t AudioSystem::setMasterMute(bool mute)
139{
140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
141 if (af == 0) return PERMISSION_DENIED;
142 af->setMasterMute(mute);
143 return NO_ERROR;
144}
145
146status_t AudioSystem::getMasterVolume(float* volume)
147{
148 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
149 if (af == 0) return PERMISSION_DENIED;
150 *volume = af->masterVolume();
151 return NO_ERROR;
152}
153
154status_t AudioSystem::getMasterMute(bool* mute)
155{
156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
157 if (af == 0) return PERMISSION_DENIED;
158 *mute = af->masterMute();
159 return NO_ERROR;
160}
161
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800162status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
163 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164{
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
167 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700168 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 return NO_ERROR;
170}
171
Glenn Kastenfff6d712012-01-12 16:38:12 -0800172status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
177 af->setStreamMute(stream, mute);
178 return NO_ERROR;
179}
180
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800181status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
182 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183{
Dima Zavinfce7a472011-04-19 22:30:36 -0700184 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
186 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700187 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 return NO_ERROR;
189}
190
Glenn Kastenfff6d712012-01-12 16:38:12 -0800191status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192{
Dima Zavinfce7a472011-04-19 22:30:36 -0700193 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
195 if (af == 0) return PERMISSION_DENIED;
196 *mute = af->streamMute(stream);
197 return NO_ERROR;
198}
199
Glenn Kastenf78aee72012-01-04 11:00:47 -0800200status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800202 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
204 if (af == 0) return PERMISSION_DENIED;
205 return af->setMode(mode);
206}
207
Glenn Kasten4944acb2013-08-19 08:39:20 -0700208status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
209{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
211 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700212 return af->setParameters(ioHandle, keyValuePairs);
213}
214
Glenn Kasten4944acb2013-08-19 08:39:20 -0700215String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
216{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700217 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
218 String8 result = String8("");
219 if (af == 0) return result;
220
221 result = af->getParameters(ioHandle, keys);
222 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223}
224
Glenn Kastenc23885e2013-12-19 16:35:18 -0800225status_t AudioSystem::setParameters(const String8& keyValuePairs)
226{
Glenn Kasten142f5192014-03-25 17:44:59 -0700227 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800228}
229
230String8 AudioSystem::getParameters(const String8& keys)
231{
Glenn Kasten142f5192014-03-25 17:44:59 -0700232 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800233}
234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235// convert volume steps to natural log scale
236
237// change this value to change volume scaling
238static const float dBPerStep = 0.5f;
239// shouldn't need to touch these
240static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
241static const float dBConvertInverse = 1.0f / dBConvert;
242
243float AudioSystem::linearToLog(int volume)
244{
245 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000246 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 // return v;
248 return volume ? exp(float(100 - volume) * dBConvert) : 0;
249}
250
251int AudioSystem::logToLinear(float volume)
252{
253 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000254 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 // return v;
256 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
257}
258
Eric Laurent21da6472017-11-09 16:29:26 -0800259/* static */ size_t AudioSystem::calculateMinFrameCount(
260 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
261 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
262{
263 // Ensure that buffer depth covers at least audio hardware latency
264 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
265 if (minBufCount < 2) {
266 minBufCount = 2;
267 }
268#if 0
269 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
270 // but keeping the code here to make it easier to add later.
271 if (minBufCount < notificationsPerBufferReq) {
272 minBufCount = notificationsPerBufferReq;
273 }
274#endif
275 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
276 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
277 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
278 /*, notificationsPerBufferReq*/);
279 return minBufCount * sourceFramesNeededWithTimestretch(
280 sampleRate, afFrameCount, afSampleRate, speed);
281}
282
283
Glenn Kasten3b16c762012-11-14 08:44:39 -0800284status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 if (streamType == AUDIO_STREAM_DEFAULT) {
289 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 }
291
Glenn Kastenfff6d712012-01-12 16:38:12 -0800292 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293 if (output == 0) {
294 return PERMISSION_DENIED;
295 }
296
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700297 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700298}
299
Glenn Kasten2c073da2016-02-26 09:14:08 -0800300status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800301 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700302{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800303 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
304 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800305 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
306 if (desc == 0) {
307 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800309 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800311 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800312 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800313 return BAD_VALUE;
314 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315
Glenn Kasten2c073da2016-02-26 09:14:08 -0800316 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return NO_ERROR;
319}
320
Glenn Kastene33054e2012-11-14 12:54:39 -0800321status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324
Dima Zavinfce7a472011-04-19 22:30:36 -0700325 if (streamType == AUDIO_STREAM_DEFAULT) {
326 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700328
Glenn Kastenfff6d712012-01-12 16:38:12 -0800329 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700330 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 return PERMISSION_DENIED;
332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700334 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700335}
336
Glenn Kasten2c073da2016-02-26 09:14:08 -0800337status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800338 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700339{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
341 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800342 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
343 if (desc == 0) {
344 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800346 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700347 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800348 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800349 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800350 return BAD_VALUE;
351 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352
Glenn Kasten2c073da2016-02-26 09:14:08 -0800353 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 return NO_ERROR;
356}
357
Glenn Kastenfff6d712012-01-12 16:38:12 -0800358status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361
Dima Zavinfce7a472011-04-19 22:30:36 -0700362 if (streamType == AUDIO_STREAM_DEFAULT) {
363 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700365
Glenn Kastenfff6d712012-01-12 16:38:12 -0800366 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700367 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700368 return PERMISSION_DENIED;
369 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370
Glenn Kasten241618f2014-03-25 17:48:57 -0700371 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700372}
373
374status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700375 uint32_t* latency)
376{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800377 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
378 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700379 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700380 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700381 *latency = af->latency(output);
382 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700383 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700384 }
385
Glenn Kasten241618f2014-03-25 17:48:57 -0700386 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 return NO_ERROR;
389}
390
Glenn Kastendd8104c2012-07-02 12:42:44 -0700391status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
392 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393{
Eric Laurent296fb132015-05-01 11:38:42 -0700394 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
395 if (afc == 0) {
396 return NO_INIT;
397 }
398 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399}
400
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700401status_t AudioSystem::setVoiceVolume(float value)
402{
403 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
404 if (af == 0) return PERMISSION_DENIED;
405 return af->setVoiceVolume(value);
406}
407
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000408status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700409 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800410{
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return PERMISSION_DENIED;
413
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000414 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800415}
416
Glenn Kasten4944acb2013-08-19 08:39:20 -0700417uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
418{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800419 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800420 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800421 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700422 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800423
424 result = af->getInputFramesLost(ioHandle);
425 return result;
426}
427
Glenn Kasteneeecb982016-02-26 10:44:04 -0800428audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700429{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700430 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700431 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800432 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700433}
434
Glenn Kastend848eb42016-03-08 13:42:11 -0800435void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700436{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700437 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
438 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800439 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700440 }
441}
442
Glenn Kastend848eb42016-03-08 13:42:11 -0800443void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700444{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700445 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
446 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800447 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700448 }
449}
450
Eric Laurent93c3d412014-08-01 14:48:35 -0700451audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
452{
453 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
454 if (af == 0) return AUDIO_HW_SYNC_INVALID;
455 return af->getAudioHwSyncForSession(sessionId);
456}
457
Eric Laurent72e3f392015-05-20 14:43:50 -0700458status_t AudioSystem::systemReady()
459{
460 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
461 if (af == 0) return NO_INIT;
462 return af->systemReady();
463}
464
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700465status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
466 size_t* frameCount)
467{
468 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
469 if (af == 0) return PERMISSION_DENIED;
470 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
471 if (desc == 0) {
472 *frameCount = af->frameCountHAL(ioHandle);
473 } else {
474 *frameCount = desc->mFrameCountHAL;
475 }
476 if (*frameCount == 0) {
477 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
478 return BAD_VALUE;
479 }
480
481 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
482
483 return NO_ERROR;
484}
485
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486// ---------------------------------------------------------------------------
487
Eric Laurent73e26b62015-04-27 16:55:58 -0700488
489void AudioSystem::AudioFlingerClient::clearIoCache()
490{
491 Mutex::Autolock _l(mLock);
492 mIoDescriptors.clear();
493 mInBuffSize = 0;
494 mInSamplingRate = 0;
495 mInFormat = AUDIO_FORMAT_DEFAULT;
496 mInChannelMask = AUDIO_CHANNEL_NONE;
497}
498
Glenn Kasten4944acb2013-08-19 08:39:20 -0700499void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
500{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800501 audio_error_callback cb = NULL;
502 {
503 Mutex::Autolock _l(AudioSystem::gLock);
504 AudioSystem::gAudioFlinger.clear();
505 cb = gAudioErrorCallback;
506 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507
Eric Laurent73e26b62015-04-27 16:55:58 -0700508 // clear output handles and stream to output map caches
509 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510
Eric Laurentf6778fd2014-11-18 17:26:58 -0800511 if (cb) {
512 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000514 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515}
516
Eric Laurent73e26b62015-04-27 16:55:58 -0700517void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
518 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700520
Eric Laurent73e26b62015-04-27 16:55:58 -0700521 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Eric Laurent296fb132015-05-01 11:38:42 -0700523 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurentad2e7b92017-09-14 20:06:42 -0700524 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700525
Eric Laurent296fb132015-05-01 11:38:42 -0700526 {
527 Mutex::Autolock _l(mLock);
528
529 switch (event) {
530 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700531 case AUDIO_OUTPUT_REGISTERED:
532 case AUDIO_INPUT_OPENED:
533 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700534 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700535 if (oldDesc == 0) {
536 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
537 } else {
538 deviceId = oldDesc->getDeviceId();
539 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700540 }
Eric Laurent296fb132015-05-01 11:38:42 -0700541
542 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
543 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700544 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
545 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
546 if (ioIndex >= 0) {
547 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
548 }
Eric Laurent296fb132015-05-01 11:38:42 -0700549 }
550 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700551 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
552 "frameCount %zu deviceId %d",
553 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
554 "output" : "input",
555 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
556 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700557 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
558 ioDesc->mFrameCount, ioDesc->getDeviceId());
559 } break;
560 case AUDIO_OUTPUT_CLOSED:
561 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700562 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700563 ALOGW("ioConfigChanged() closing unknown %s %d",
564 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
565 break;
566 }
567 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700568 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700569
Eric Laurent296fb132015-05-01 11:38:42 -0700570 mIoDescriptors.removeItem(ioDesc->mIoHandle);
571 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
572 } break;
573
574 case AUDIO_OUTPUT_CONFIG_CHANGED:
575 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700576 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700577 if (oldDesc == 0) {
578 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
579 break;
580 }
581
582 deviceId = oldDesc->getDeviceId();
583 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
584
585 if (deviceId != ioDesc->getDeviceId()) {
586 deviceId = ioDesc->getDeviceId();
587 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
588 if (ioIndex >= 0) {
589 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
590 }
591 }
592 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700593 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700594 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
595 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800596 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
597 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700598
Eric Laurentc2f1f072009-07-17 12:17:14 -0700599 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700600 }
Eric Laurent296fb132015-05-01 11:38:42 -0700601 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700602 bool callbackRemoved = false;
Eric Laurent296fb132015-05-01 11:38:42 -0700603 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
Eric Laurentad2e7b92017-09-14 20:06:42 -0700604 for (size_t i = 0; i < callbacks.size(); ) {
605 sp<AudioDeviceCallback> callback = callbacks[i].promote();
606 if (callback.get() != nullptr) {
607 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
608 i++;
609 } else {
610 callbacks.removeAt(i);
611 callbackRemoved = true;
612 }
613 }
614 // clean up callback list while we are here if some clients have disappeared without
615 // unregistering their callback
616 if (callbackRemoved) {
617 Mutex::Autolock _l(mLock);
618 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620}
621
Eric Laurent73e26b62015-04-27 16:55:58 -0700622status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
623 uint32_t sampleRate, audio_format_t format,
624 audio_channel_mask_t channelMask, size_t* buffSize)
625{
626 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
627 if (af == 0) {
628 return PERMISSION_DENIED;
629 }
630 Mutex::Autolock _l(mLock);
631 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
632 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
633 || (channelMask != mInChannelMask)) {
634 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
635 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800636 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700637 sampleRate, format, channelMask);
638 return BAD_VALUE;
639 }
640 // A benign race is possible here: we could overwrite a fresher cache entry
641 // save the request params
642 mInSamplingRate = sampleRate;
643 mInFormat = format;
644 mInChannelMask = channelMask;
645
646 mInBuffSize = inBuffSize;
647 }
648
649 *buffSize = mInBuffSize;
650
651 return NO_ERROR;
652}
653
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700654sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700655{
656 sp<AudioIoDescriptor> desc;
657 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
658 if (index >= 0) {
659 desc = mIoDescriptors.valueAt(index);
660 }
661 return desc;
662}
663
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700664sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
665{
666 Mutex::Autolock _l(mLock);
667 return getIoDescriptor_l(ioHandle);
668}
669
Eric Laurent296fb132015-05-01 11:38:42 -0700670status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700671 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700672{
673 Mutex::Autolock _l(mLock);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700674 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurent296fb132015-05-01 11:38:42 -0700675 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
676 if (ioIndex >= 0) {
677 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
678 }
679
680 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700681 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700682 return INVALID_OPERATION;
683 }
684 }
685 callbacks.add(callback);
686
687 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
688 return NO_ERROR;
689}
690
691status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700692 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700693{
694 Mutex::Autolock _l(mLock);
695 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
696 if (ioIndex < 0) {
697 return INVALID_OPERATION;
698 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700699 Vector < wp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700700
701 size_t cbIndex;
702 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700703 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700704 break;
705 }
706 }
707 if (cbIndex == callbacks.size()) {
708 return INVALID_OPERATION;
709 }
710 callbacks.removeAt(cbIndex);
711 if (callbacks.size() != 0) {
712 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
713 } else {
714 mAudioDeviceCallbacks.removeItem(audioIo);
715 }
716 return NO_ERROR;
717}
718
719/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700720{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700721 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722 gAudioErrorCallback = cb;
723}
724
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700725/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
726{
727 Mutex::Autolock _l(gLock);
728 gDynPolicyCallback = cb;
729}
730
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800731/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
732{
733 Mutex::Autolock _l(gLock);
734 gRecordConfigCallback = cb;
735}
736
Eric Laurentc2f1f072009-07-17 12:17:14 -0700737// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800738// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700739sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
740sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
741
742
Glenn Kasten18a6d902012-09-24 11:27:56 -0700743// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800744const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700745{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800746 sp<IAudioPolicyService> ap;
747 sp<AudioPolicyServiceClient> apc;
748 {
749 Mutex::Autolock _l(gLockAPS);
750 if (gAudioPolicyService == 0) {
751 sp<IServiceManager> sm = defaultServiceManager();
752 sp<IBinder> binder;
753 do {
754 binder = sm->getService(String16("media.audio_policy"));
755 if (binder != 0)
756 break;
757 ALOGW("AudioPolicyService not published, waiting...");
758 usleep(500000); // 0.5 s
759 } while (true);
760 if (gAudioPolicyServiceClient == NULL) {
761 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
762 }
763 binder->linkToDeath(gAudioPolicyServiceClient);
764 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
765 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
766 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700767 // Make sure callbacks can be received by gAudioPolicyServiceClient
768 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700769 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800770 ap = gAudioPolicyService;
771 }
772 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200773 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800774 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200775 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
776 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700777 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800778
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800779 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700780}
781
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700782// ---------------------------------------------------------------------------
783
Dima Zavinfce7a472011-04-19 22:30:36 -0700784status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
785 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800786 const char *device_address,
787 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700788{
789 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700790 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800791 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700792
Eric Laurentc2f1f072009-07-17 12:17:14 -0700793 if (aps == 0) return PERMISSION_DENIED;
794
Eric Laurent71b63e32011-09-02 14:20:56 -0700795 if (device_address != NULL) {
796 address = device_address;
797 }
Paul McLeane743a472015-01-28 11:07:31 -0800798 if (device_name != NULL) {
799 name = device_name;
800 }
801 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700802}
803
Dima Zavinfce7a472011-04-19 22:30:36 -0700804audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700805 const char *device_address)
806{
807 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700808 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700809
810 return aps->getDeviceConnectionState(device, device_address);
811}
812
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800813status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
814 const char *device_address,
815 const char *device_name)
816{
817 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
818 const char *address = "";
819 const char *name = "";
820
821 if (aps == 0) return PERMISSION_DENIED;
822
823 if (device_address != NULL) {
824 address = device_address;
825 }
826 if (device_name != NULL) {
827 name = device_name;
828 }
829 return aps->handleDeviceConfigChange(device, address, name);
830}
831
Glenn Kastenf78aee72012-01-04 11:00:47 -0800832status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700833{
Glenn Kasten347966c2012-01-18 14:58:32 -0800834 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700835 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
836 if (aps == 0) return PERMISSION_DENIED;
837
838 return aps->setPhoneState(state);
839}
840
Dima Zavinfce7a472011-04-19 22:30:36 -0700841status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700842{
843 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
844 if (aps == 0) return PERMISSION_DENIED;
845 return aps->setForceUse(usage, config);
846}
847
Dima Zavinfce7a472011-04-19 22:30:36 -0700848audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700849{
850 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700851 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700852 return aps->getForceUse(usage);
853}
854
855
Eric Laurentf4e63452017-11-06 19:31:46 +0000856audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700857{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700858 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
859 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000860 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700861}
862
Eric Laurente83b55d2014-11-14 10:06:21 -0800863status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
864 audio_io_handle_t *output,
865 audio_session_t session,
866 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200867 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700868 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800869 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800870 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700871 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800872 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700873{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800875 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200876 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800877 config,
878 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700879}
880
Eric Laurentd7fe0862018-07-14 16:48:01 -0700881status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882{
883 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
884 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700885 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700886}
887
Eric Laurentd7fe0862018-07-14 16:48:01 -0700888status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700889{
890 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
891 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700892 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700893}
894
Eric Laurentd7fe0862018-07-14 16:48:01 -0700895void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
898 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700899 aps->releaseOutput(portId);
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,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001241 audio_port_handle_t *portId)
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;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001245 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001246}
1247
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001248status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
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;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001252 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001253}
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
jiabin81772902018-04-02 17:52:27 -07001283status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1284 audio_format_t *surroundFormats,
1285 bool *surroundFormatsEnabled,
1286 bool reported)
1287{
1288 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1289 if (aps == 0) return PERMISSION_DENIED;
1290 return aps->getSurroundFormats(
1291 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1292}
1293
1294status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1295{
1296 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1297 if (aps == 0) return PERMISSION_DENIED;
1298 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1299}
1300
Eric Laurentc2f1f072009-07-17 12:17:14 -07001301// ---------------------------------------------------------------------------
1302
Eric Laurente8726fe2015-06-26 09:39:24 -07001303int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001304 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001305{
1306 Mutex::Autolock _l(mLock);
1307 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001308 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001309 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001310 }
1311 }
Eric Laurent296fb132015-05-01 11:38:42 -07001312 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001313 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001314}
1315
Eric Laurente8726fe2015-06-26 09:39:24 -07001316int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001317 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001318{
1319 Mutex::Autolock _l(mLock);
1320 size_t i;
1321 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001322 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001323 break;
1324 }
1325 }
1326 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001327 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001328 }
1329 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001330 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001331}
1332
Eric Laurent296fb132015-05-01 11:38:42 -07001333
Eric Laurentb28753e2015-04-01 13:06:28 -07001334void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1335{
1336 Mutex::Autolock _l(mLock);
1337 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1338 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1339 }
1340}
1341
1342void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1343{
1344 Mutex::Autolock _l(mLock);
1345 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1346 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1347 }
1348}
1349
Jean-Michel Trivide801052015-04-14 19:10:14 -07001350void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1351 String8 regId, int32_t state)
1352{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001353 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1354 dynamic_policy_callback cb = NULL;
1355 {
1356 Mutex::Autolock _l(AudioSystem::gLock);
1357 cb = gDynPolicyCallback;
1358 }
1359
1360 if (cb != NULL) {
1361 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1362 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001363}
1364
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001365void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001366 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001367 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1368 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001369 record_config_callback cb = NULL;
1370 {
1371 Mutex::Autolock _l(AudioSystem::gLock);
1372 cb = gRecordConfigCallback;
1373 }
1374
1375 if (cb != NULL) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001376 cb(event, clientInfo, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001377 }
1378}
1379
Glenn Kasten4944acb2013-08-19 08:39:20 -07001380void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1381{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001382 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001383 Mutex::Autolock _l(mLock);
1384 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1385 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001386 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001387 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001388 {
1389 Mutex::Autolock _l(gLockAPS);
1390 AudioSystem::gAudioPolicyService.clear();
1391 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001392
Steve Block5ff1dd52012-01-05 23:22:43 +00001393 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001394}
1395
Glenn Kasten40bc9062015-03-20 09:09:33 -07001396} // namespace android