blob: 5cd27891bc7aba47330a8fdb14033a77851de0ae [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>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070023#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070024#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <math.h>
26
Dima Zavin64760242011-05-11 14:15:23 -070027#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070028
Eric Laurentc2f1f072009-07-17 12:17:14 -070029// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070030
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031namespace android {
32
33// client singleton for AudioFlinger binder interface
34Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080035Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036sp<IAudioFlinger> AudioSystem::gAudioFlinger;
37sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
38audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070039dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -080040record_config_callback AudioSystem::gRecordConfigCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080041
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080044const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080046 sp<IAudioFlinger> af;
47 sp<AudioFlingerClient> afc;
48 {
49 Mutex::Autolock _l(gLock);
50 if (gAudioFlinger == 0) {
51 sp<IServiceManager> sm = defaultServiceManager();
52 sp<IBinder> binder;
53 do {
54 binder = sm->getService(String16("media.audio_flinger"));
55 if (binder != 0)
56 break;
57 ALOGW("AudioFlinger not published, waiting...");
58 usleep(500000); // 0.5 s
59 } while (true);
60 if (gAudioFlingerClient == NULL) {
61 gAudioFlingerClient = new AudioFlingerClient();
62 } else {
63 if (gAudioErrorCallback) {
64 gAudioErrorCallback(NO_ERROR);
65 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080067 binder->linkToDeath(gAudioFlingerClient);
68 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
69 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
70 afc = gAudioFlingerClient;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070071 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080072 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080074 if (afc != 0) {
75 af->registerClient(afc);
76 }
77 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078}
79
Eric Laurent296fb132015-05-01 11:38:42 -070080const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
81{
82 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
83 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
84 if (af == 0) return 0;
85 Mutex::Autolock _l(gLock);
86 return gAudioFlingerClient;
87}
88
89sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
90{
91 sp<AudioIoDescriptor> desc;
92 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
93 if (afc != 0) {
94 desc = afc->getIoDescriptor(ioHandle);
95 }
96 return desc;
97}
98
Eric Laurent46291612013-07-18 14:38:44 -070099/* static */ status_t AudioSystem::checkAudioFlinger()
100{
101 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
102 return NO_ERROR;
103 }
104 return DEAD_OBJECT;
105}
106
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700107// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
108
Glenn Kasten4944acb2013-08-19 08:39:20 -0700109status_t AudioSystem::muteMicrophone(bool state)
110{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
112 if (af == 0) return PERMISSION_DENIED;
113 return af->setMicMute(state);
114}
115
Glenn Kasten4944acb2013-08-19 08:39:20 -0700116status_t AudioSystem::isMicrophoneMuted(bool* state)
117{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
119 if (af == 0) return PERMISSION_DENIED;
120 *state = af->getMicMute();
121 return NO_ERROR;
122}
123
124status_t AudioSystem::setMasterVolume(float value)
125{
126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
127 if (af == 0) return PERMISSION_DENIED;
128 af->setMasterVolume(value);
129 return NO_ERROR;
130}
131
132status_t AudioSystem::setMasterMute(bool mute)
133{
134 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
135 if (af == 0) return PERMISSION_DENIED;
136 af->setMasterMute(mute);
137 return NO_ERROR;
138}
139
140status_t AudioSystem::getMasterVolume(float* volume)
141{
142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
143 if (af == 0) return PERMISSION_DENIED;
144 *volume = af->masterVolume();
145 return NO_ERROR;
146}
147
148status_t AudioSystem::getMasterMute(bool* mute)
149{
150 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
151 if (af == 0) return PERMISSION_DENIED;
152 *mute = af->masterMute();
153 return NO_ERROR;
154}
155
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800156status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
157 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158{
Dima Zavinfce7a472011-04-19 22:30:36 -0700159 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
161 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700162 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 return NO_ERROR;
164}
165
Glenn Kastenfff6d712012-01-12 16:38:12 -0800166status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167{
Dima Zavinfce7a472011-04-19 22:30:36 -0700168 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
170 if (af == 0) return PERMISSION_DENIED;
171 af->setStreamMute(stream, mute);
172 return NO_ERROR;
173}
174
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800175status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
176 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Dima Zavinfce7a472011-04-19 22:30:36 -0700178 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
180 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700181 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 return NO_ERROR;
183}
184
Glenn Kastenfff6d712012-01-12 16:38:12 -0800185status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186{
Dima Zavinfce7a472011-04-19 22:30:36 -0700187 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
189 if (af == 0) return PERMISSION_DENIED;
190 *mute = af->streamMute(stream);
191 return NO_ERROR;
192}
193
Glenn Kastenf78aee72012-01-04 11:00:47 -0800194status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800196 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
198 if (af == 0) return PERMISSION_DENIED;
199 return af->setMode(mode);
200}
201
Glenn Kasten4944acb2013-08-19 08:39:20 -0700202status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
203{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
205 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700206 return af->setParameters(ioHandle, keyValuePairs);
207}
208
Glenn Kasten4944acb2013-08-19 08:39:20 -0700209String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
210{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700211 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
212 String8 result = String8("");
213 if (af == 0) return result;
214
215 result = af->getParameters(ioHandle, keys);
216 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217}
218
Glenn Kastenc23885e2013-12-19 16:35:18 -0800219status_t AudioSystem::setParameters(const String8& keyValuePairs)
220{
Glenn Kasten142f5192014-03-25 17:44:59 -0700221 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800222}
223
224String8 AudioSystem::getParameters(const String8& keys)
225{
Glenn Kasten142f5192014-03-25 17:44:59 -0700226 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800227}
228
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229// convert volume steps to natural log scale
230
231// change this value to change volume scaling
232static const float dBPerStep = 0.5f;
233// shouldn't need to touch these
234static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
235static const float dBConvertInverse = 1.0f / dBConvert;
236
237float AudioSystem::linearToLog(int volume)
238{
239 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000240 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 // return v;
242 return volume ? exp(float(100 - volume) * dBConvert) : 0;
243}
244
245int AudioSystem::logToLinear(float volume)
246{
247 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000248 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 // return v;
250 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
251}
252
Glenn Kasten3b16c762012-11-14 08:44:39 -0800253status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256
Dima Zavinfce7a472011-04-19 22:30:36 -0700257 if (streamType == AUDIO_STREAM_DEFAULT) {
258 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259 }
260
Glenn Kastenfff6d712012-01-12 16:38:12 -0800261 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 if (output == 0) {
263 return PERMISSION_DENIED;
264 }
265
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700266 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700267}
268
Glenn Kasten2c073da2016-02-26 09:14:08 -0800269status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800270 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700271{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800272 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
273 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800274 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
275 if (desc == 0) {
276 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800278 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700279 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800280 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800281 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800282 return BAD_VALUE;
283 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284
Glenn Kasten2c073da2016-02-26 09:14:08 -0800285 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 return NO_ERROR;
288}
289
Glenn Kastene33054e2012-11-14 12:54:39 -0800290status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293
Dima Zavinfce7a472011-04-19 22:30:36 -0700294 if (streamType == AUDIO_STREAM_DEFAULT) {
295 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700297
Glenn Kastenfff6d712012-01-12 16:38:12 -0800298 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700299 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300 return PERMISSION_DENIED;
301 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700303 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700304}
305
Glenn Kasten2c073da2016-02-26 09:14:08 -0800306status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800307 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700308{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800309 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
310 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800311 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
312 if (desc == 0) {
313 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800315 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800317 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800318 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800319 return BAD_VALUE;
320 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
Glenn Kasten2c073da2016-02-26 09:14:08 -0800322 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 return NO_ERROR;
325}
326
Glenn Kastenfff6d712012-01-12 16:38:12 -0800327status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 if (streamType == AUDIO_STREAM_DEFAULT) {
332 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700334
Glenn Kastenfff6d712012-01-12 16:38:12 -0800335 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700336 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 return PERMISSION_DENIED;
338 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339
Glenn Kasten241618f2014-03-25 17:48:57 -0700340 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700341}
342
343status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700344 uint32_t* latency)
345{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800346 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
347 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700348 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700349 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 *latency = af->latency(output);
351 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700352 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 }
354
Glenn Kasten241618f2014-03-25 17:48:57 -0700355 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 return NO_ERROR;
358}
359
Glenn Kastendd8104c2012-07-02 12:42:44 -0700360status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
361 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362{
Eric Laurent296fb132015-05-01 11:38:42 -0700363 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
364 if (afc == 0) {
365 return NO_INIT;
366 }
367 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368}
369
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700370status_t AudioSystem::setVoiceVolume(float value)
371{
372 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
373 if (af == 0) return PERMISSION_DENIED;
374 return af->setVoiceVolume(value);
375}
376
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000377status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700378 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800379{
380 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
381 if (af == 0) return PERMISSION_DENIED;
382
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000383 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800384}
385
Glenn Kasten4944acb2013-08-19 08:39:20 -0700386uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
387{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800388 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800389 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800390 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700391 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800392
393 result = af->getInputFramesLost(ioHandle);
394 return result;
395}
396
Glenn Kasteneeecb982016-02-26 10:44:04 -0800397audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700398{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700399 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700400 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800401 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700402}
403
Glenn Kastend848eb42016-03-08 13:42:11 -0800404void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700405{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800408 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700409 }
410}
411
Glenn Kastend848eb42016-03-08 13:42:11 -0800412void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700413{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700414 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
415 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800416 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700417 }
418}
419
Eric Laurent93c3d412014-08-01 14:48:35 -0700420audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
421{
422 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
423 if (af == 0) return AUDIO_HW_SYNC_INVALID;
424 return af->getAudioHwSyncForSession(sessionId);
425}
426
Eric Laurent72e3f392015-05-20 14:43:50 -0700427status_t AudioSystem::systemReady()
428{
429 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
430 if (af == 0) return NO_INIT;
431 return af->systemReady();
432}
433
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700434status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
435 size_t* frameCount)
436{
437 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
438 if (af == 0) return PERMISSION_DENIED;
439 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
440 if (desc == 0) {
441 *frameCount = af->frameCountHAL(ioHandle);
442 } else {
443 *frameCount = desc->mFrameCountHAL;
444 }
445 if (*frameCount == 0) {
446 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
447 return BAD_VALUE;
448 }
449
450 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
451
452 return NO_ERROR;
453}
454
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455// ---------------------------------------------------------------------------
456
Eric Laurent73e26b62015-04-27 16:55:58 -0700457
458void AudioSystem::AudioFlingerClient::clearIoCache()
459{
460 Mutex::Autolock _l(mLock);
461 mIoDescriptors.clear();
462 mInBuffSize = 0;
463 mInSamplingRate = 0;
464 mInFormat = AUDIO_FORMAT_DEFAULT;
465 mInChannelMask = AUDIO_CHANNEL_NONE;
466}
467
Glenn Kasten4944acb2013-08-19 08:39:20 -0700468void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
469{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800470 audio_error_callback cb = NULL;
471 {
472 Mutex::Autolock _l(AudioSystem::gLock);
473 AudioSystem::gAudioFlinger.clear();
474 cb = gAudioErrorCallback;
475 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476
Eric Laurent73e26b62015-04-27 16:55:58 -0700477 // clear output handles and stream to output map caches
478 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479
Eric Laurentf6778fd2014-11-18 17:26:58 -0800480 if (cb) {
481 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000483 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484}
485
Eric Laurent73e26b62015-04-27 16:55:58 -0700486void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
487 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100488 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700489
Eric Laurent73e26b62015-04-27 16:55:58 -0700490 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491
Eric Laurent296fb132015-05-01 11:38:42 -0700492 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
493 Vector < sp<AudioDeviceCallback> > callbacks;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494
Eric Laurent296fb132015-05-01 11:38:42 -0700495 {
496 Mutex::Autolock _l(mLock);
497
498 switch (event) {
499 case AUDIO_OUTPUT_OPENED:
500 case AUDIO_INPUT_OPENED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700501 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700502 if (oldDesc == 0) {
503 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
504 } else {
505 deviceId = oldDesc->getDeviceId();
506 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700507 }
Eric Laurent296fb132015-05-01 11:38:42 -0700508
509 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
510 deviceId = ioDesc->getDeviceId();
511 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
512 if (ioIndex >= 0) {
513 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
514 }
515 }
516 ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x "
517 "frameCount %zu deviceId %d", event == AUDIO_OUTPUT_OPENED ? "output" : "input",
518 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
519 ioDesc->mFrameCount, ioDesc->getDeviceId());
520 } break;
521 case AUDIO_OUTPUT_CLOSED:
522 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700523 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700524 ALOGW("ioConfigChanged() closing unknown %s %d",
525 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
526 break;
527 }
528 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700529 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700530
Eric Laurent296fb132015-05-01 11:38:42 -0700531 mIoDescriptors.removeItem(ioDesc->mIoHandle);
532 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
533 } break;
534
535 case AUDIO_OUTPUT_CONFIG_CHANGED:
536 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700537 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700538 if (oldDesc == 0) {
539 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
540 break;
541 }
542
543 deviceId = oldDesc->getDeviceId();
544 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
545
546 if (deviceId != ioDesc->getDeviceId()) {
547 deviceId = ioDesc->getDeviceId();
548 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
549 if (ioIndex >= 0) {
550 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
551 }
552 }
553 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700554 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700555 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
556 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800557 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
558 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700559
Eric Laurentc2f1f072009-07-17 12:17:14 -0700560 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700561 }
Eric Laurent296fb132015-05-01 11:38:42 -0700562 }
563 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
564 for (size_t i = 0; i < callbacks.size(); i++) {
565 callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700566 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567}
568
Eric Laurent73e26b62015-04-27 16:55:58 -0700569status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
570 uint32_t sampleRate, audio_format_t format,
571 audio_channel_mask_t channelMask, size_t* buffSize)
572{
573 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
574 if (af == 0) {
575 return PERMISSION_DENIED;
576 }
577 Mutex::Autolock _l(mLock);
578 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
579 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
580 || (channelMask != mInChannelMask)) {
581 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
582 if (inBuffSize == 0) {
583 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
584 sampleRate, format, channelMask);
585 return BAD_VALUE;
586 }
587 // A benign race is possible here: we could overwrite a fresher cache entry
588 // save the request params
589 mInSamplingRate = sampleRate;
590 mInFormat = format;
591 mInChannelMask = channelMask;
592
593 mInBuffSize = inBuffSize;
594 }
595
596 *buffSize = mInBuffSize;
597
598 return NO_ERROR;
599}
600
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700601sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700602{
603 sp<AudioIoDescriptor> desc;
604 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
605 if (index >= 0) {
606 desc = mIoDescriptors.valueAt(index);
607 }
608 return desc;
609}
610
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700611sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
612{
613 Mutex::Autolock _l(mLock);
614 return getIoDescriptor_l(ioHandle);
615}
616
Eric Laurent296fb132015-05-01 11:38:42 -0700617status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
618 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
619{
620 Mutex::Autolock _l(mLock);
621 Vector < sp<AudioDeviceCallback> > callbacks;
622 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
623 if (ioIndex >= 0) {
624 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
625 }
626
627 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
628 if (callbacks[cbIndex] == callback) {
629 return INVALID_OPERATION;
630 }
631 }
632 callbacks.add(callback);
633
634 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
635 return NO_ERROR;
636}
637
638status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
639 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
640{
641 Mutex::Autolock _l(mLock);
642 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
643 if (ioIndex < 0) {
644 return INVALID_OPERATION;
645 }
646 Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
647
648 size_t cbIndex;
649 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
650 if (callbacks[cbIndex] == callback) {
651 break;
652 }
653 }
654 if (cbIndex == callbacks.size()) {
655 return INVALID_OPERATION;
656 }
657 callbacks.removeAt(cbIndex);
658 if (callbacks.size() != 0) {
659 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
660 } else {
661 mAudioDeviceCallbacks.removeItem(audioIo);
662 }
663 return NO_ERROR;
664}
665
666/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700667{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700668 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 gAudioErrorCallback = cb;
670}
671
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700672/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
673{
674 Mutex::Autolock _l(gLock);
675 gDynPolicyCallback = cb;
676}
677
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800678/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
679{
680 Mutex::Autolock _l(gLock);
681 gRecordConfigCallback = cb;
682}
683
Eric Laurentc2f1f072009-07-17 12:17:14 -0700684// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800685// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
687sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
688
689
Glenn Kasten18a6d902012-09-24 11:27:56 -0700690// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800691const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700692{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800693 sp<IAudioPolicyService> ap;
694 sp<AudioPolicyServiceClient> apc;
695 {
696 Mutex::Autolock _l(gLockAPS);
697 if (gAudioPolicyService == 0) {
698 sp<IServiceManager> sm = defaultServiceManager();
699 sp<IBinder> binder;
700 do {
701 binder = sm->getService(String16("media.audio_policy"));
702 if (binder != 0)
703 break;
704 ALOGW("AudioPolicyService not published, waiting...");
705 usleep(500000); // 0.5 s
706 } while (true);
707 if (gAudioPolicyServiceClient == NULL) {
708 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
709 }
710 binder->linkToDeath(gAudioPolicyServiceClient);
711 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
712 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
713 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700714 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800715 ap = gAudioPolicyService;
716 }
717 if (apc != 0) {
718 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700719 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800720
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800721 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700722}
723
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700724// ---------------------------------------------------------------------------
725
Dima Zavinfce7a472011-04-19 22:30:36 -0700726status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
727 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800728 const char *device_address,
729 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700730{
731 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700732 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800733 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700734
Eric Laurentc2f1f072009-07-17 12:17:14 -0700735 if (aps == 0) return PERMISSION_DENIED;
736
Eric Laurent71b63e32011-09-02 14:20:56 -0700737 if (device_address != NULL) {
738 address = device_address;
739 }
Paul McLeane743a472015-01-28 11:07:31 -0800740 if (device_name != NULL) {
741 name = device_name;
742 }
743 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700744}
745
Dima Zavinfce7a472011-04-19 22:30:36 -0700746audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700747 const char *device_address)
748{
749 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700750 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700751
752 return aps->getDeviceConnectionState(device, device_address);
753}
754
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800755status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
756 const char *device_address,
757 const char *device_name)
758{
759 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
760 const char *address = "";
761 const char *name = "";
762
763 if (aps == 0) return PERMISSION_DENIED;
764
765 if (device_address != NULL) {
766 address = device_address;
767 }
768 if (device_name != NULL) {
769 name = device_name;
770 }
771 return aps->handleDeviceConfigChange(device, address, name);
772}
773
Glenn Kastenf78aee72012-01-04 11:00:47 -0800774status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700775{
Glenn Kasten347966c2012-01-18 14:58:32 -0800776 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700777 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
778 if (aps == 0) return PERMISSION_DENIED;
779
780 return aps->setPhoneState(state);
781}
782
Dima Zavinfce7a472011-04-19 22:30:36 -0700783status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700784{
785 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
786 if (aps == 0) return PERMISSION_DENIED;
787 return aps->setForceUse(usage, config);
788}
789
Dima Zavinfce7a472011-04-19 22:30:36 -0700790audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700791{
792 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700793 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700794 return aps->getForceUse(usage);
795}
796
797
Dima Zavinfce7a472011-04-19 22:30:36 -0700798audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700799 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800800 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700801 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000802 audio_output_flags_t flags,
803 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700804{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700805 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
806 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000807 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700808}
809
Eric Laurente83b55d2014-11-14 10:06:21 -0800810status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
811 audio_io_handle_t *output,
812 audio_session_t session,
813 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700814 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800815 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800816 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700817 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800818 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700819{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700820 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800821 if (aps == 0) return NO_INIT;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700822 return aps->getOutputForAttr(attr, output, session, stream, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800823 config,
824 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700825}
826
Eric Laurentde070132010-07-13 04:45:46 -0700827status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700828 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800829 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830{
831 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
832 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700833 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700834}
835
Eric Laurentde070132010-07-13 04:45:46 -0700836status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700837 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800838 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700839{
840 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
841 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700842 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700843}
844
Eric Laurente83b55d2014-11-14 10:06:21 -0800845void AudioSystem::releaseOutput(audio_io_handle_t output,
846 audio_stream_type_t stream,
847 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700848{
849 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
850 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800851 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700852}
853
Eric Laurentcaf7f482014-11-25 17:50:47 -0800854status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
855 audio_io_handle_t *input,
856 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700857 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700858 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800859 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600860 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800861 audio_port_handle_t selectedDeviceId,
862 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700863{
864 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800865 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600866 return aps->getInputForAttr(
Eric Laurentb2379ba2016-05-23 17:42:12 -0700867 attr, input, session, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800868 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700869}
870
Eric Laurent4dc68062014-07-28 17:26:49 -0700871status_t AudioSystem::startInput(audio_io_handle_t input,
872 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700873{
874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
875 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700876 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700877}
878
Eric Laurent4dc68062014-07-28 17:26:49 -0700879status_t AudioSystem::stopInput(audio_io_handle_t input,
880 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881{
882 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
883 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700884 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700885}
886
Eric Laurent4dc68062014-07-28 17:26:49 -0700887void AudioSystem::releaseInput(audio_io_handle_t input,
888 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700889{
890 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
891 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700892 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700893}
894
Dima Zavinfce7a472011-04-19 22:30:36 -0700895status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896 int indexMin,
897 int indexMax)
898{
899 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
900 if (aps == 0) return PERMISSION_DENIED;
901 return aps->initStreamVolume(stream, indexMin, indexMax);
902}
903
Eric Laurent83844cc2011-11-18 16:43:31 -0800904status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
905 int index,
906 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907{
908 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
909 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800910 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911}
912
Eric Laurent83844cc2011-11-18 16:43:31 -0800913status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
914 int *index,
915 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700916{
917 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
918 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800919 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920}
921
Dima Zavinfce7a472011-04-19 22:30:36 -0700922uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700923{
924 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
925 if (aps == 0) return 0;
926 return aps->getStrategyForStream(stream);
927}
928
Eric Laurent63742522012-03-08 13:42:42 -0800929audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800930{
931 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800932 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800933 return aps->getDevicesForStream(stream);
934}
935
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700936audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700937{
938 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800939 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700940 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700941 return aps->getOutputForEffect(desc);
942}
943
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700944status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700945 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700946 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800947 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700948 int id)
949{
950 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
951 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700952 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700953}
954
955status_t AudioSystem::unregisterEffect(int id)
956{
957 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
958 if (aps == 0) return PERMISSION_DENIED;
959 return aps->unregisterEffect(id);
960}
961
Eric Laurentdb7c0792011-08-10 10:37:50 -0700962status_t AudioSystem::setEffectEnabled(int id, bool enabled)
963{
964 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
965 if (aps == 0) return PERMISSION_DENIED;
966 return aps->setEffectEnabled(id, enabled);
967}
968
Glenn Kastenfff6d712012-01-12 16:38:12 -0800969status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700970{
Eric Laurenteda6c362011-02-02 09:33:30 -0800971 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
972 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700973 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800974 *state = aps->isStreamActive(stream, inPastMs);
975 return NO_ERROR;
976}
977
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800978status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
979 uint32_t inPastMs)
980{
981 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
982 if (aps == 0) return PERMISSION_DENIED;
983 if (state == NULL) return BAD_VALUE;
984 *state = aps->isStreamActiveRemotely(stream, inPastMs);
985 return NO_ERROR;
986}
987
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700988status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
989{
990 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
991 if (aps == 0) return PERMISSION_DENIED;
992 if (state == NULL) return BAD_VALUE;
993 *state = aps->isSourceActive(stream);
994 return NO_ERROR;
995}
996
Glenn Kasten3b16c762012-11-14 08:44:39 -0800997uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700998{
999 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1000 if (af == 0) return 0;
1001 return af->getPrimaryOutputSamplingRate();
1002}
1003
Glenn Kastene33054e2012-11-14 12:54:39 -08001004size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001005{
1006 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1007 if (af == 0) return 0;
1008 return af->getPrimaryOutputFrameCount();
1009}
Eric Laurenteda6c362011-02-02 09:33:30 -08001010
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001011status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1012{
1013 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1014 if (af == 0) return PERMISSION_DENIED;
1015 return af->setLowRamDevice(isLowRamDevice);
1016}
1017
Eric Laurent9f6530f2011-08-30 10:18:54 -07001018void AudioSystem::clearAudioConfigCache()
1019{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001020 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001021 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001022 {
1023 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001024 if (gAudioFlingerClient != 0) {
1025 gAudioFlingerClient->clearIoCache();
1026 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001027 gAudioFlinger.clear();
1028 }
1029 {
1030 Mutex::Autolock _l(gLockAPS);
1031 gAudioPolicyService.clear();
1032 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001033}
1034
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001035bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1036{
1037 ALOGV("isOffloadSupported()");
1038 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1039 if (aps == 0) return false;
1040 return aps->isOffloadSupported(info);
1041}
1042
Eric Laurent203b1a12014-04-01 10:34:16 -07001043status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1044 audio_port_type_t type,
1045 unsigned int *num_ports,
1046 struct audio_port *ports,
1047 unsigned int *generation)
1048{
1049 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1050 if (aps == 0) return PERMISSION_DENIED;
1051 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1052}
1053
1054status_t AudioSystem::getAudioPort(struct audio_port *port)
1055{
1056 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1057 if (aps == 0) return PERMISSION_DENIED;
1058 return aps->getAudioPort(port);
1059}
1060
1061status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1062 audio_patch_handle_t *handle)
1063{
1064 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1065 if (aps == 0) return PERMISSION_DENIED;
1066 return aps->createAudioPatch(patch, handle);
1067}
1068
1069status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1070{
1071 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1072 if (aps == 0) return PERMISSION_DENIED;
1073 return aps->releaseAudioPatch(handle);
1074}
1075
1076status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1077 struct audio_patch *patches,
1078 unsigned int *generation)
1079{
1080 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1081 if (aps == 0) return PERMISSION_DENIED;
1082 return aps->listAudioPatches(num_patches, patches, generation);
1083}
1084
1085status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1086{
1087 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1088 if (aps == 0) return PERMISSION_DENIED;
1089 return aps->setAudioPortConfig(config);
1090}
1091
Eric Laurent296fb132015-05-01 11:38:42 -07001092status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001093{
Eric Laurentb28753e2015-04-01 13:06:28 -07001094 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1095 if (aps == 0) return PERMISSION_DENIED;
1096
1097 Mutex::Autolock _l(gLockAPS);
1098 if (gAudioPolicyServiceClient == 0) {
1099 return NO_INIT;
1100 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001101 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1102 if (ret == 1) {
1103 aps->setAudioPortCallbacksEnabled(true);
1104 }
1105 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001106}
1107
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001108/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001109status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001110{
1111 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1112 if (aps == 0) return PERMISSION_DENIED;
1113
1114 Mutex::Autolock _l(gLockAPS);
1115 if (gAudioPolicyServiceClient == 0) {
1116 return NO_INIT;
1117 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001118 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1119 if (ret == 0) {
1120 aps->setAudioPortCallbacksEnabled(false);
1121 }
1122 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001123}
1124
1125status_t AudioSystem::addAudioDeviceCallback(
1126 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1127{
1128 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1129 if (afc == 0) {
1130 return NO_INIT;
1131 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001132 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1133 if (status == NO_ERROR) {
1134 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1135 if (af != 0) {
1136 af->registerClient(afc);
1137 }
1138 }
1139 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001140}
1141
1142status_t AudioSystem::removeAudioDeviceCallback(
1143 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1144{
1145 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1146 if (afc == 0) {
1147 return NO_INIT;
1148 }
1149 return afc->removeAudioDeviceCallback(callback, audioIo);
1150}
1151
1152audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1153{
1154 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1155 if (af == 0) return PERMISSION_DENIED;
1156 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1157 if (desc == 0) {
1158 return AUDIO_PORT_HANDLE_NONE;
1159 }
1160 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001161}
1162
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001163status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1164 audio_io_handle_t *ioHandle,
1165 audio_devices_t *device)
1166{
1167 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1168 if (aps == 0) return PERMISSION_DENIED;
1169 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1170}
1171
1172status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1173{
1174 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1175 if (aps == 0) return PERMISSION_DENIED;
1176 return aps->releaseSoundTriggerSession(session);
1177}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001178
1179audio_mode_t AudioSystem::getPhoneState()
1180{
1181 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1182 if (aps == 0) return AUDIO_MODE_INVALID;
1183 return aps->getPhoneState();
1184}
1185
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001186status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001187{
1188 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1189 if (aps == 0) return PERMISSION_DENIED;
1190 return aps->registerPolicyMixes(mixes, registration);
1191}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001192
Eric Laurent554a2772015-04-10 11:29:24 -07001193status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1194 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07001195 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001196{
1197 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1198 if (aps == 0) return PERMISSION_DENIED;
1199 return aps->startAudioSource(source, attributes, handle);
1200}
1201
Glenn Kasten559d4392016-03-29 13:42:57 -07001202status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001203{
1204 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1205 if (aps == 0) return PERMISSION_DENIED;
1206 return aps->stopAudioSource(handle);
1207}
1208
Andy Hung2ddee192015-12-18 17:34:44 -08001209status_t AudioSystem::setMasterMono(bool mono)
1210{
1211 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1212 if (aps == 0) return PERMISSION_DENIED;
1213 return aps->setMasterMono(mono);
1214}
1215
1216status_t AudioSystem::getMasterMono(bool *mono)
1217{
1218 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1219 if (aps == 0) return PERMISSION_DENIED;
1220 return aps->getMasterMono(mono);
1221}
1222
Eric Laurentc2f1f072009-07-17 12:17:14 -07001223// ---------------------------------------------------------------------------
1224
Eric Laurente8726fe2015-06-26 09:39:24 -07001225int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001226 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001227{
1228 Mutex::Autolock _l(mLock);
1229 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001230 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001231 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001232 }
1233 }
Eric Laurent296fb132015-05-01 11:38:42 -07001234 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001235 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001236}
1237
Eric Laurente8726fe2015-06-26 09:39:24 -07001238int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001239 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001240{
1241 Mutex::Autolock _l(mLock);
1242 size_t i;
1243 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001244 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001245 break;
1246 }
1247 }
1248 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001249 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001250 }
1251 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001252 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001253}
1254
Eric Laurent296fb132015-05-01 11:38:42 -07001255
Eric Laurentb28753e2015-04-01 13:06:28 -07001256void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1257{
1258 Mutex::Autolock _l(mLock);
1259 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1260 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1261 }
1262}
1263
1264void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1265{
1266 Mutex::Autolock _l(mLock);
1267 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1268 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1269 }
1270}
1271
Jean-Michel Trivide801052015-04-14 19:10:14 -07001272void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1273 String8 regId, int32_t state)
1274{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001275 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1276 dynamic_policy_callback cb = NULL;
1277 {
1278 Mutex::Autolock _l(AudioSystem::gLock);
1279 cb = gDynPolicyCallback;
1280 }
1281
1282 if (cb != NULL) {
1283 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1284 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001285}
1286
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001287void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001288 int event, audio_session_t session, audio_source_t source,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001289 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1290 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001291 record_config_callback cb = NULL;
1292 {
1293 Mutex::Autolock _l(AudioSystem::gLock);
1294 cb = gRecordConfigCallback;
1295 }
1296
1297 if (cb != NULL) {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001298 cb(event, session, source, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001299 }
1300}
1301
Glenn Kasten4944acb2013-08-19 08:39:20 -07001302void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1303{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001304 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001305 Mutex::Autolock _l(mLock);
1306 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1307 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001308 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001309 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001310 {
1311 Mutex::Autolock _l(gLockAPS);
1312 AudioSystem::gAudioPolicyService.clear();
1313 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001314
Steve Block5ff1dd52012-01-05 23:22:43 +00001315 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001316}
1317
Glenn Kasten40bc9062015-03-20 09:09:33 -07001318} // namespace android