blob: cdc75ace6640bd5c70683f63105b054a9241d04b [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>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080023#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070024#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070025#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080026#include <math.h>
27
Dima Zavin64760242011-05-11 14:15:23 -070028#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070029
Eric Laurentc2f1f072009-07-17 12:17:14 -070030// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032namespace android {
33
34// client singleton for AudioFlinger binder interface
35Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080036Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037sp<IAudioFlinger> AudioSystem::gAudioFlinger;
38sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
39audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070040dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -080041record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080042
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
Glenn Kasten3b16c762012-11-14 08:44:39 -0800256status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700258 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 if (streamType == AUDIO_STREAM_DEFAULT) {
261 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 }
263
Glenn Kastenfff6d712012-01-12 16:38:12 -0800264 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265 if (output == 0) {
266 return PERMISSION_DENIED;
267 }
268
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700269 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700270}
271
Glenn Kasten2c073da2016-02-26 09:14:08 -0800272status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800273 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700274{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800275 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
276 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800277 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
278 if (desc == 0) {
279 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800281 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800283 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800284 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800285 return BAD_VALUE;
286 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
Glenn Kasten2c073da2016-02-26 09:14:08 -0800288 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290 return NO_ERROR;
291}
292
Glenn Kastene33054e2012-11-14 12:54:39 -0800293status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296
Dima Zavinfce7a472011-04-19 22:30:36 -0700297 if (streamType == AUDIO_STREAM_DEFAULT) {
298 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700300
Glenn Kastenfff6d712012-01-12 16:38:12 -0800301 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700302 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303 return PERMISSION_DENIED;
304 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700306 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700307}
308
Glenn Kasten2c073da2016-02-26 09:14:08 -0800309status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800310 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700311{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800312 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
313 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800314 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
315 if (desc == 0) {
316 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800318 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700319 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800320 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800321 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800322 return BAD_VALUE;
323 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324
Glenn Kasten2c073da2016-02-26 09:14:08 -0800325 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 return NO_ERROR;
328}
329
Glenn Kastenfff6d712012-01-12 16:38:12 -0800330status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 if (streamType == AUDIO_STREAM_DEFAULT) {
335 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700337
Glenn Kastenfff6d712012-01-12 16:38:12 -0800338 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700339 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 return PERMISSION_DENIED;
341 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342
Glenn Kasten241618f2014-03-25 17:48:57 -0700343 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700344}
345
346status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700347 uint32_t* latency)
348{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
350 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700351 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700352 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 *latency = af->latency(output);
354 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700355 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356 }
357
Glenn Kasten241618f2014-03-25 17:48:57 -0700358 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700359
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 return NO_ERROR;
361}
362
Glenn Kastendd8104c2012-07-02 12:42:44 -0700363status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
364 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365{
Eric Laurent296fb132015-05-01 11:38:42 -0700366 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
367 if (afc == 0) {
368 return NO_INIT;
369 }
370 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371}
372
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700373status_t AudioSystem::setVoiceVolume(float value)
374{
375 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
376 if (af == 0) return PERMISSION_DENIED;
377 return af->setVoiceVolume(value);
378}
379
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000380status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700381 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800382{
383 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
384 if (af == 0) return PERMISSION_DENIED;
385
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000386 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800387}
388
Glenn Kasten4944acb2013-08-19 08:39:20 -0700389uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
390{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800391 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800392 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800393 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700394 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800395
396 result = af->getInputFramesLost(ioHandle);
397 return result;
398}
399
Glenn Kasteneeecb982016-02-26 10:44:04 -0800400audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700401{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700402 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700403 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800404 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700405}
406
Glenn Kastend848eb42016-03-08 13:42:11 -0800407void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700408{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700409 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
410 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800411 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700412 }
413}
414
Glenn Kastend848eb42016-03-08 13:42:11 -0800415void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700416{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
418 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800419 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700420 }
421}
422
Eric Laurent93c3d412014-08-01 14:48:35 -0700423audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
424{
425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
426 if (af == 0) return AUDIO_HW_SYNC_INVALID;
427 return af->getAudioHwSyncForSession(sessionId);
428}
429
Eric Laurent72e3f392015-05-20 14:43:50 -0700430status_t AudioSystem::systemReady()
431{
432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433 if (af == 0) return NO_INIT;
434 return af->systemReady();
435}
436
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700437status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
438 size_t* frameCount)
439{
440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441 if (af == 0) return PERMISSION_DENIED;
442 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
443 if (desc == 0) {
444 *frameCount = af->frameCountHAL(ioHandle);
445 } else {
446 *frameCount = desc->mFrameCountHAL;
447 }
448 if (*frameCount == 0) {
449 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
450 return BAD_VALUE;
451 }
452
453 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
454
455 return NO_ERROR;
456}
457
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458// ---------------------------------------------------------------------------
459
Eric Laurent73e26b62015-04-27 16:55:58 -0700460
461void AudioSystem::AudioFlingerClient::clearIoCache()
462{
463 Mutex::Autolock _l(mLock);
464 mIoDescriptors.clear();
465 mInBuffSize = 0;
466 mInSamplingRate = 0;
467 mInFormat = AUDIO_FORMAT_DEFAULT;
468 mInChannelMask = AUDIO_CHANNEL_NONE;
469}
470
Glenn Kasten4944acb2013-08-19 08:39:20 -0700471void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
472{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800473 audio_error_callback cb = NULL;
474 {
475 Mutex::Autolock _l(AudioSystem::gLock);
476 AudioSystem::gAudioFlinger.clear();
477 cb = gAudioErrorCallback;
478 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479
Eric Laurent73e26b62015-04-27 16:55:58 -0700480 // clear output handles and stream to output map caches
481 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482
Eric Laurentf6778fd2014-11-18 17:26:58 -0800483 if (cb) {
484 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000486 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487}
488
Eric Laurent73e26b62015-04-27 16:55:58 -0700489void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
490 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492
Eric Laurent73e26b62015-04-27 16:55:58 -0700493 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494
Eric Laurent296fb132015-05-01 11:38:42 -0700495 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurentad2e7b92017-09-14 20:06:42 -0700496 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700497
Eric Laurent296fb132015-05-01 11:38:42 -0700498 {
499 Mutex::Autolock _l(mLock);
500
501 switch (event) {
502 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700503 case AUDIO_OUTPUT_REGISTERED:
504 case AUDIO_INPUT_OPENED:
505 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700506 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700507 if (oldDesc == 0) {
508 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
509 } else {
510 deviceId = oldDesc->getDeviceId();
511 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700512 }
Eric Laurent296fb132015-05-01 11:38:42 -0700513
514 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
515 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700516 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
517 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
518 if (ioIndex >= 0) {
519 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
520 }
Eric Laurent296fb132015-05-01 11:38:42 -0700521 }
522 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700523 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
524 "frameCount %zu deviceId %d",
525 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
526 "output" : "input",
527 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
528 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700529 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
530 ioDesc->mFrameCount, ioDesc->getDeviceId());
531 } break;
532 case AUDIO_OUTPUT_CLOSED:
533 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700534 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700535 ALOGW("ioConfigChanged() closing unknown %s %d",
536 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
537 break;
538 }
539 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700540 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700541
Eric Laurent296fb132015-05-01 11:38:42 -0700542 mIoDescriptors.removeItem(ioDesc->mIoHandle);
543 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
544 } break;
545
546 case AUDIO_OUTPUT_CONFIG_CHANGED:
547 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700548 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700549 if (oldDesc == 0) {
550 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
551 break;
552 }
553
554 deviceId = oldDesc->getDeviceId();
555 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
556
557 if (deviceId != ioDesc->getDeviceId()) {
558 deviceId = ioDesc->getDeviceId();
559 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
560 if (ioIndex >= 0) {
561 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
562 }
563 }
564 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700565 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700566 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
567 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800568 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
569 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700570
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572 }
Eric Laurent296fb132015-05-01 11:38:42 -0700573 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700574 bool callbackRemoved = false;
Eric Laurent296fb132015-05-01 11:38:42 -0700575 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
Eric Laurentad2e7b92017-09-14 20:06:42 -0700576 for (size_t i = 0; i < callbacks.size(); ) {
577 sp<AudioDeviceCallback> callback = callbacks[i].promote();
578 if (callback.get() != nullptr) {
579 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
580 i++;
581 } else {
582 callbacks.removeAt(i);
583 callbackRemoved = true;
584 }
585 }
586 // clean up callback list while we are here if some clients have disappeared without
587 // unregistering their callback
588 if (callbackRemoved) {
589 Mutex::Autolock _l(mLock);
590 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700591 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592}
593
Eric Laurent73e26b62015-04-27 16:55:58 -0700594status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
595 uint32_t sampleRate, audio_format_t format,
596 audio_channel_mask_t channelMask, size_t* buffSize)
597{
598 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
599 if (af == 0) {
600 return PERMISSION_DENIED;
601 }
602 Mutex::Autolock _l(mLock);
603 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
604 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
605 || (channelMask != mInChannelMask)) {
606 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
607 if (inBuffSize == 0) {
608 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
609 sampleRate, format, channelMask);
610 return BAD_VALUE;
611 }
612 // A benign race is possible here: we could overwrite a fresher cache entry
613 // save the request params
614 mInSamplingRate = sampleRate;
615 mInFormat = format;
616 mInChannelMask = channelMask;
617
618 mInBuffSize = inBuffSize;
619 }
620
621 *buffSize = mInBuffSize;
622
623 return NO_ERROR;
624}
625
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700626sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700627{
628 sp<AudioIoDescriptor> desc;
629 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
630 if (index >= 0) {
631 desc = mIoDescriptors.valueAt(index);
632 }
633 return desc;
634}
635
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700636sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
637{
638 Mutex::Autolock _l(mLock);
639 return getIoDescriptor_l(ioHandle);
640}
641
Eric Laurent296fb132015-05-01 11:38:42 -0700642status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700643 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700644{
645 Mutex::Autolock _l(mLock);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700646 Vector < wp<AudioDeviceCallback> > callbacks;
Eric Laurent296fb132015-05-01 11:38:42 -0700647 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
648 if (ioIndex >= 0) {
649 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
650 }
651
652 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700653 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700654 return INVALID_OPERATION;
655 }
656 }
657 callbacks.add(callback);
658
659 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
660 return NO_ERROR;
661}
662
663status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700664 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700665{
666 Mutex::Autolock _l(mLock);
667 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
668 if (ioIndex < 0) {
669 return INVALID_OPERATION;
670 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700671 Vector < wp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700672
673 size_t cbIndex;
674 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700675 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700676 break;
677 }
678 }
679 if (cbIndex == callbacks.size()) {
680 return INVALID_OPERATION;
681 }
682 callbacks.removeAt(cbIndex);
683 if (callbacks.size() != 0) {
684 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
685 } else {
686 mAudioDeviceCallbacks.removeItem(audioIo);
687 }
688 return NO_ERROR;
689}
690
691/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700692{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700693 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694 gAudioErrorCallback = cb;
695}
696
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700697/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
698{
699 Mutex::Autolock _l(gLock);
700 gDynPolicyCallback = cb;
701}
702
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800703/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
704{
705 Mutex::Autolock _l(gLock);
706 gRecordConfigCallback = cb;
707}
708
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800710// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700711sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
712sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
713
714
Glenn Kasten18a6d902012-09-24 11:27:56 -0700715// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800716const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800718 sp<IAudioPolicyService> ap;
719 sp<AudioPolicyServiceClient> apc;
720 {
721 Mutex::Autolock _l(gLockAPS);
722 if (gAudioPolicyService == 0) {
723 sp<IServiceManager> sm = defaultServiceManager();
724 sp<IBinder> binder;
725 do {
726 binder = sm->getService(String16("media.audio_policy"));
727 if (binder != 0)
728 break;
729 ALOGW("AudioPolicyService not published, waiting...");
730 usleep(500000); // 0.5 s
731 } while (true);
732 if (gAudioPolicyServiceClient == NULL) {
733 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
734 }
735 binder->linkToDeath(gAudioPolicyServiceClient);
736 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
737 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
738 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700739 // Make sure callbacks can be received by gAudioPolicyServiceClient
740 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700741 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800742 ap = gAudioPolicyService;
743 }
744 if (apc != 0) {
745 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700746 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800747
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800748 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749}
750
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700751// ---------------------------------------------------------------------------
752
Dima Zavinfce7a472011-04-19 22:30:36 -0700753status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
754 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800755 const char *device_address,
756 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757{
758 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700759 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800760 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700761
Eric Laurentc2f1f072009-07-17 12:17:14 -0700762 if (aps == 0) return PERMISSION_DENIED;
763
Eric Laurent71b63e32011-09-02 14:20:56 -0700764 if (device_address != NULL) {
765 address = device_address;
766 }
Paul McLeane743a472015-01-28 11:07:31 -0800767 if (device_name != NULL) {
768 name = device_name;
769 }
770 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700771}
772
Dima Zavinfce7a472011-04-19 22:30:36 -0700773audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700774 const char *device_address)
775{
776 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700777 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700778
779 return aps->getDeviceConnectionState(device, device_address);
780}
781
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800782status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
783 const char *device_address,
784 const char *device_name)
785{
786 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
787 const char *address = "";
788 const char *name = "";
789
790 if (aps == 0) return PERMISSION_DENIED;
791
792 if (device_address != NULL) {
793 address = device_address;
794 }
795 if (device_name != NULL) {
796 name = device_name;
797 }
798 return aps->handleDeviceConfigChange(device, address, name);
799}
800
Glenn Kastenf78aee72012-01-04 11:00:47 -0800801status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700802{
Glenn Kasten347966c2012-01-18 14:58:32 -0800803 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700804 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
805 if (aps == 0) return PERMISSION_DENIED;
806
807 return aps->setPhoneState(state);
808}
809
Dima Zavinfce7a472011-04-19 22:30:36 -0700810status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700811{
812 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
813 if (aps == 0) return PERMISSION_DENIED;
814 return aps->setForceUse(usage, config);
815}
816
Dima Zavinfce7a472011-04-19 22:30:36 -0700817audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700818{
819 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700820 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700821 return aps->getForceUse(usage);
822}
823
824
Dima Zavinfce7a472011-04-19 22:30:36 -0700825audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700826 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800827 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700828 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000829 audio_output_flags_t flags,
830 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700831{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700832 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
833 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000834 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700835}
836
Eric Laurente83b55d2014-11-14 10:06:21 -0800837status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
838 audio_io_handle_t *output,
839 audio_session_t session,
840 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700841 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800842 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800843 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700844 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800845 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700846{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800848 if (aps == 0) return NO_INIT;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700849 return aps->getOutputForAttr(attr, output, session, stream, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800850 config,
851 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700852}
853
Eric Laurentde070132010-07-13 04:45:46 -0700854status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700855 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800856 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700857{
858 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
859 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700860 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700861}
862
Eric Laurentde070132010-07-13 04:45:46 -0700863status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700864 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800865 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700866{
867 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
868 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700869 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700870}
871
Eric Laurente83b55d2014-11-14 10:06:21 -0800872void AudioSystem::releaseOutput(audio_io_handle_t output,
873 audio_stream_type_t stream,
874 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700875{
876 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
877 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800878 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700879}
880
Eric Laurentcaf7f482014-11-25 17:50:47 -0800881status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
882 audio_io_handle_t *input,
883 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700884 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700885 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800886 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600887 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700888 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800889 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700890{
891 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800892 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600893 return aps->getInputForAttr(
Eric Laurentb2379ba2016-05-23 17:42:12 -0700894 attr, input, session, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800895 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896}
897
Eric Laurent4dc68062014-07-28 17:26:49 -0700898status_t AudioSystem::startInput(audio_io_handle_t input,
899 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700900{
901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
902 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700903 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904}
905
Eric Laurent4dc68062014-07-28 17:26:49 -0700906status_t AudioSystem::stopInput(audio_io_handle_t input,
907 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700908{
909 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
910 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700911 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700912}
913
Eric Laurent4dc68062014-07-28 17:26:49 -0700914void AudioSystem::releaseInput(audio_io_handle_t input,
915 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700916{
917 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
918 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700919 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920}
921
Dima Zavinfce7a472011-04-19 22:30:36 -0700922status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700923 int indexMin,
924 int indexMax)
925{
926 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
927 if (aps == 0) return PERMISSION_DENIED;
928 return aps->initStreamVolume(stream, indexMin, indexMax);
929}
930
Eric Laurent83844cc2011-11-18 16:43:31 -0800931status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
932 int index,
933 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700934{
935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
936 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800937 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700938}
939
Eric Laurent83844cc2011-11-18 16:43:31 -0800940status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
941 int *index,
942 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943{
944 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
945 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800946 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947}
948
Dima Zavinfce7a472011-04-19 22:30:36 -0700949uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700950{
951 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
952 if (aps == 0) return 0;
953 return aps->getStrategyForStream(stream);
954}
955
Eric Laurent63742522012-03-08 13:42:42 -0800956audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800957{
958 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800959 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800960 return aps->getDevicesForStream(stream);
961}
962
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700963audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700964{
965 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800966 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700967 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700968 return aps->getOutputForEffect(desc);
969}
970
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700971status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700972 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700973 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800974 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700975 int id)
976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700979 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700980}
981
982status_t AudioSystem::unregisterEffect(int id)
983{
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
985 if (aps == 0) return PERMISSION_DENIED;
986 return aps->unregisterEffect(id);
987}
988
Eric Laurentdb7c0792011-08-10 10:37:50 -0700989status_t AudioSystem::setEffectEnabled(int id, bool enabled)
990{
991 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
992 if (aps == 0) return PERMISSION_DENIED;
993 return aps->setEffectEnabled(id, enabled);
994}
995
Glenn Kastenfff6d712012-01-12 16:38:12 -0800996status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700997{
Eric Laurenteda6c362011-02-02 09:33:30 -0800998 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
999 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001000 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001001 *state = aps->isStreamActive(stream, inPastMs);
1002 return NO_ERROR;
1003}
1004
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001005status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1006 uint32_t inPastMs)
1007{
1008 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1009 if (aps == 0) return PERMISSION_DENIED;
1010 if (state == NULL) return BAD_VALUE;
1011 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1012 return NO_ERROR;
1013}
1014
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001015status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1016{
1017 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1018 if (aps == 0) return PERMISSION_DENIED;
1019 if (state == NULL) return BAD_VALUE;
1020 *state = aps->isSourceActive(stream);
1021 return NO_ERROR;
1022}
1023
Glenn Kasten3b16c762012-11-14 08:44:39 -08001024uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001025{
1026 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1027 if (af == 0) return 0;
1028 return af->getPrimaryOutputSamplingRate();
1029}
1030
Glenn Kastene33054e2012-11-14 12:54:39 -08001031size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001032{
1033 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1034 if (af == 0) return 0;
1035 return af->getPrimaryOutputFrameCount();
1036}
Eric Laurenteda6c362011-02-02 09:33:30 -08001037
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001038status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1039{
1040 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1041 if (af == 0) return PERMISSION_DENIED;
1042 return af->setLowRamDevice(isLowRamDevice);
1043}
1044
Eric Laurent9f6530f2011-08-30 10:18:54 -07001045void AudioSystem::clearAudioConfigCache()
1046{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001047 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001048 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001049 {
1050 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001051 if (gAudioFlingerClient != 0) {
1052 gAudioFlingerClient->clearIoCache();
1053 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001054 gAudioFlinger.clear();
1055 }
1056 {
1057 Mutex::Autolock _l(gLockAPS);
1058 gAudioPolicyService.clear();
1059 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001060}
1061
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001062bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1063{
1064 ALOGV("isOffloadSupported()");
1065 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1066 if (aps == 0) return false;
1067 return aps->isOffloadSupported(info);
1068}
1069
Eric Laurent203b1a12014-04-01 10:34:16 -07001070status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1071 audio_port_type_t type,
1072 unsigned int *num_ports,
1073 struct audio_port *ports,
1074 unsigned int *generation)
1075{
1076 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1077 if (aps == 0) return PERMISSION_DENIED;
1078 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1079}
1080
1081status_t AudioSystem::getAudioPort(struct audio_port *port)
1082{
1083 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1084 if (aps == 0) return PERMISSION_DENIED;
1085 return aps->getAudioPort(port);
1086}
1087
1088status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1089 audio_patch_handle_t *handle)
1090{
1091 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1092 if (aps == 0) return PERMISSION_DENIED;
1093 return aps->createAudioPatch(patch, handle);
1094}
1095
1096status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1097{
1098 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1099 if (aps == 0) return PERMISSION_DENIED;
1100 return aps->releaseAudioPatch(handle);
1101}
1102
1103status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1104 struct audio_patch *patches,
1105 unsigned int *generation)
1106{
1107 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1108 if (aps == 0) return PERMISSION_DENIED;
1109 return aps->listAudioPatches(num_patches, patches, generation);
1110}
1111
1112status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1113{
1114 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1115 if (aps == 0) return PERMISSION_DENIED;
1116 return aps->setAudioPortConfig(config);
1117}
1118
Eric Laurent296fb132015-05-01 11:38:42 -07001119status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001120{
Eric Laurentb28753e2015-04-01 13:06:28 -07001121 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1122 if (aps == 0) return PERMISSION_DENIED;
1123
1124 Mutex::Autolock _l(gLockAPS);
1125 if (gAudioPolicyServiceClient == 0) {
1126 return NO_INIT;
1127 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001128 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1129 if (ret == 1) {
1130 aps->setAudioPortCallbacksEnabled(true);
1131 }
1132 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001133}
1134
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001135/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001136status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001137{
1138 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1139 if (aps == 0) return PERMISSION_DENIED;
1140
1141 Mutex::Autolock _l(gLockAPS);
1142 if (gAudioPolicyServiceClient == 0) {
1143 return NO_INIT;
1144 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001145 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1146 if (ret == 0) {
1147 aps->setAudioPortCallbacksEnabled(false);
1148 }
1149 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001150}
1151
1152status_t AudioSystem::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001153 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001154{
1155 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1156 if (afc == 0) {
1157 return NO_INIT;
1158 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001159 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1160 if (status == NO_ERROR) {
1161 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1162 if (af != 0) {
1163 af->registerClient(afc);
1164 }
1165 }
1166 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001167}
1168
1169status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001170 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001171{
1172 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1173 if (afc == 0) {
1174 return NO_INIT;
1175 }
1176 return afc->removeAudioDeviceCallback(callback, audioIo);
1177}
1178
1179audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1180{
1181 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1182 if (af == 0) return PERMISSION_DENIED;
1183 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1184 if (desc == 0) {
1185 return AUDIO_PORT_HANDLE_NONE;
1186 }
1187 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001188}
1189
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001190status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1191 audio_io_handle_t *ioHandle,
1192 audio_devices_t *device)
1193{
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return PERMISSION_DENIED;
1196 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1197}
1198
1199status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1200{
1201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1202 if (aps == 0) return PERMISSION_DENIED;
1203 return aps->releaseSoundTriggerSession(session);
1204}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001205
1206audio_mode_t AudioSystem::getPhoneState()
1207{
1208 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1209 if (aps == 0) return AUDIO_MODE_INVALID;
1210 return aps->getPhoneState();
1211}
1212
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001213status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001214{
1215 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1216 if (aps == 0) return PERMISSION_DENIED;
1217 return aps->registerPolicyMixes(mixes, registration);
1218}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001219
Eric Laurent554a2772015-04-10 11:29:24 -07001220status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1221 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07001222 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001223{
1224 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1225 if (aps == 0) return PERMISSION_DENIED;
1226 return aps->startAudioSource(source, attributes, handle);
1227}
1228
Glenn Kasten559d4392016-03-29 13:42:57 -07001229status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001230{
1231 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1232 if (aps == 0) return PERMISSION_DENIED;
1233 return aps->stopAudioSource(handle);
1234}
1235
Andy Hung2ddee192015-12-18 17:34:44 -08001236status_t AudioSystem::setMasterMono(bool mono)
1237{
1238 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1239 if (aps == 0) return PERMISSION_DENIED;
1240 return aps->setMasterMono(mono);
1241}
1242
1243status_t AudioSystem::getMasterMono(bool *mono)
1244{
1245 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1246 if (aps == 0) return PERMISSION_DENIED;
1247 return aps->getMasterMono(mono);
1248}
1249
Eric Laurentac9cef52017-06-09 15:46:26 -07001250float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1251{
1252 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1253 if (aps == 0) return NAN;
1254 return aps->getStreamVolumeDB(stream, index, device);
1255}
1256
Eric Laurentc2f1f072009-07-17 12:17:14 -07001257// ---------------------------------------------------------------------------
1258
Eric Laurente8726fe2015-06-26 09:39:24 -07001259int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001260 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001261{
1262 Mutex::Autolock _l(mLock);
1263 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001264 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001265 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001266 }
1267 }
Eric Laurent296fb132015-05-01 11:38:42 -07001268 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001269 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001270}
1271
Eric Laurente8726fe2015-06-26 09:39:24 -07001272int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001273 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001274{
1275 Mutex::Autolock _l(mLock);
1276 size_t i;
1277 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001278 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001279 break;
1280 }
1281 }
1282 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001283 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001284 }
1285 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001286 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001287}
1288
Eric Laurent296fb132015-05-01 11:38:42 -07001289
Eric Laurentb28753e2015-04-01 13:06:28 -07001290void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1291{
1292 Mutex::Autolock _l(mLock);
1293 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1294 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1295 }
1296}
1297
1298void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1299{
1300 Mutex::Autolock _l(mLock);
1301 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1302 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1303 }
1304}
1305
Jean-Michel Trivide801052015-04-14 19:10:14 -07001306void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1307 String8 regId, int32_t state)
1308{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001309 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1310 dynamic_policy_callback cb = NULL;
1311 {
1312 Mutex::Autolock _l(AudioSystem::gLock);
1313 cb = gDynPolicyCallback;
1314 }
1315
1316 if (cb != NULL) {
1317 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1318 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001319}
1320
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001321void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001322 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001323 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1324 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001325 record_config_callback cb = NULL;
1326 {
1327 Mutex::Autolock _l(AudioSystem::gLock);
1328 cb = gRecordConfigCallback;
1329 }
1330
1331 if (cb != NULL) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001332 cb(event, clientInfo, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001333 }
1334}
1335
Glenn Kasten4944acb2013-08-19 08:39:20 -07001336void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1337{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001338 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001339 Mutex::Autolock _l(mLock);
1340 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1341 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001342 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001343 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001344 {
1345 Mutex::Autolock _l(gLockAPS);
1346 AudioSystem::gAudioPolicyService.clear();
1347 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001348
Steve Block5ff1dd52012-01-05 23:22:43 +00001349 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001350}
1351
Glenn Kasten40bc9062015-03-20 09:09:33 -07001352} // namespace android