blob: 1908f0e109befd5d023b9948dc41e2f13047941f [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 Kasten4a8308b2016-04-18 14:10:01 -0700557 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700558
Eric Laurentc2f1f072009-07-17 12:17:14 -0700559 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700560 }
Eric Laurent296fb132015-05-01 11:38:42 -0700561 }
562 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
563 for (size_t i = 0; i < callbacks.size(); i++) {
564 callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700565 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566}
567
Eric Laurent73e26b62015-04-27 16:55:58 -0700568status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
569 uint32_t sampleRate, audio_format_t format,
570 audio_channel_mask_t channelMask, size_t* buffSize)
571{
572 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
573 if (af == 0) {
574 return PERMISSION_DENIED;
575 }
576 Mutex::Autolock _l(mLock);
577 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
578 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
579 || (channelMask != mInChannelMask)) {
580 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
581 if (inBuffSize == 0) {
582 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
583 sampleRate, format, channelMask);
584 return BAD_VALUE;
585 }
586 // A benign race is possible here: we could overwrite a fresher cache entry
587 // save the request params
588 mInSamplingRate = sampleRate;
589 mInFormat = format;
590 mInChannelMask = channelMask;
591
592 mInBuffSize = inBuffSize;
593 }
594
595 *buffSize = mInBuffSize;
596
597 return NO_ERROR;
598}
599
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700600sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700601{
602 sp<AudioIoDescriptor> desc;
603 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
604 if (index >= 0) {
605 desc = mIoDescriptors.valueAt(index);
606 }
607 return desc;
608}
609
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700610sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
611{
612 Mutex::Autolock _l(mLock);
613 return getIoDescriptor_l(ioHandle);
614}
615
Eric Laurent296fb132015-05-01 11:38:42 -0700616status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
617 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
618{
619 Mutex::Autolock _l(mLock);
620 Vector < sp<AudioDeviceCallback> > callbacks;
621 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
622 if (ioIndex >= 0) {
623 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
624 }
625
626 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
627 if (callbacks[cbIndex] == callback) {
628 return INVALID_OPERATION;
629 }
630 }
631 callbacks.add(callback);
632
633 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
634 return NO_ERROR;
635}
636
637status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
638 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
639{
640 Mutex::Autolock _l(mLock);
641 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
642 if (ioIndex < 0) {
643 return INVALID_OPERATION;
644 }
645 Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
646
647 size_t cbIndex;
648 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
649 if (callbacks[cbIndex] == callback) {
650 break;
651 }
652 }
653 if (cbIndex == callbacks.size()) {
654 return INVALID_OPERATION;
655 }
656 callbacks.removeAt(cbIndex);
657 if (callbacks.size() != 0) {
658 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
659 } else {
660 mAudioDeviceCallbacks.removeItem(audioIo);
661 }
662 return NO_ERROR;
663}
664
665/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700666{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700667 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 gAudioErrorCallback = cb;
669}
670
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700671/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
672{
673 Mutex::Autolock _l(gLock);
674 gDynPolicyCallback = cb;
675}
676
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800677/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
678{
679 Mutex::Autolock _l(gLock);
680 gRecordConfigCallback = cb;
681}
682
Eric Laurentc2f1f072009-07-17 12:17:14 -0700683// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800684// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700685sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
686sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
687
688
Glenn Kasten18a6d902012-09-24 11:27:56 -0700689// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800690const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700691{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800692 sp<IAudioPolicyService> ap;
693 sp<AudioPolicyServiceClient> apc;
694 {
695 Mutex::Autolock _l(gLockAPS);
696 if (gAudioPolicyService == 0) {
697 sp<IServiceManager> sm = defaultServiceManager();
698 sp<IBinder> binder;
699 do {
700 binder = sm->getService(String16("media.audio_policy"));
701 if (binder != 0)
702 break;
703 ALOGW("AudioPolicyService not published, waiting...");
704 usleep(500000); // 0.5 s
705 } while (true);
706 if (gAudioPolicyServiceClient == NULL) {
707 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
708 }
709 binder->linkToDeath(gAudioPolicyServiceClient);
710 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
711 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
712 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700713 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800714 ap = gAudioPolicyService;
715 }
716 if (apc != 0) {
717 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800719
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800720 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700721}
722
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700723// ---------------------------------------------------------------------------
724
Dima Zavinfce7a472011-04-19 22:30:36 -0700725status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
726 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800727 const char *device_address,
728 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729{
730 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700731 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800732 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700733
Eric Laurentc2f1f072009-07-17 12:17:14 -0700734 if (aps == 0) return PERMISSION_DENIED;
735
Eric Laurent71b63e32011-09-02 14:20:56 -0700736 if (device_address != NULL) {
737 address = device_address;
738 }
Paul McLeane743a472015-01-28 11:07:31 -0800739 if (device_name != NULL) {
740 name = device_name;
741 }
742 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743}
744
Dima Zavinfce7a472011-04-19 22:30:36 -0700745audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700746 const char *device_address)
747{
748 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700749 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700750
751 return aps->getDeviceConnectionState(device, device_address);
752}
753
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800754status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
755 const char *device_address,
756 const char *device_name)
757{
758 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
759 const char *address = "";
760 const char *name = "";
761
762 if (aps == 0) return PERMISSION_DENIED;
763
764 if (device_address != NULL) {
765 address = device_address;
766 }
767 if (device_name != NULL) {
768 name = device_name;
769 }
770 return aps->handleDeviceConfigChange(device, address, name);
771}
772
Glenn Kastenf78aee72012-01-04 11:00:47 -0800773status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700774{
Glenn Kasten347966c2012-01-18 14:58:32 -0800775 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700776 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
777 if (aps == 0) return PERMISSION_DENIED;
778
779 return aps->setPhoneState(state);
780}
781
Dima Zavinfce7a472011-04-19 22:30:36 -0700782status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700783{
784 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
785 if (aps == 0) return PERMISSION_DENIED;
786 return aps->setForceUse(usage, config);
787}
788
Dima Zavinfce7a472011-04-19 22:30:36 -0700789audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700790{
791 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700792 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700793 return aps->getForceUse(usage);
794}
795
796
Dima Zavinfce7a472011-04-19 22:30:36 -0700797audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700798 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800799 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700800 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000801 audio_output_flags_t flags,
802 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700803{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700804 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
805 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000806 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700807}
808
Eric Laurente83b55d2014-11-14 10:06:21 -0800809status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
810 audio_io_handle_t *output,
811 audio_session_t session,
812 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700813 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800814 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800815 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700816 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800817 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700818{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700819 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800820 if (aps == 0) return NO_INIT;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700821 return aps->getOutputForAttr(attr, output, session, stream, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800822 config,
823 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700824}
825
Eric Laurentde070132010-07-13 04:45:46 -0700826status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700827 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800828 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700829{
830 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
831 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700832 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700833}
834
Eric Laurentde070132010-07-13 04:45:46 -0700835status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700836 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800837 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700838{
839 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
840 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700841 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700842}
843
Eric Laurente83b55d2014-11-14 10:06:21 -0800844void AudioSystem::releaseOutput(audio_io_handle_t output,
845 audio_stream_type_t stream,
846 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700847{
848 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
849 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800850 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700851}
852
Eric Laurentcaf7f482014-11-25 17:50:47 -0800853status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
854 audio_io_handle_t *input,
855 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700856 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700857 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800858 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600859 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800860 audio_port_handle_t selectedDeviceId,
861 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862{
863 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800864 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600865 return aps->getInputForAttr(
Eric Laurentb2379ba2016-05-23 17:42:12 -0700866 attr, input, session, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800867 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700868}
869
Eric Laurent4dc68062014-07-28 17:26:49 -0700870status_t AudioSystem::startInput(audio_io_handle_t input,
871 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700872{
873 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
874 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700875 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700876}
877
Eric Laurent4dc68062014-07-28 17:26:49 -0700878status_t AudioSystem::stopInput(audio_io_handle_t input,
879 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700880{
881 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
882 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700883 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700884}
885
Eric Laurent4dc68062014-07-28 17:26:49 -0700886void AudioSystem::releaseInput(audio_io_handle_t input,
887 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700888{
889 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
890 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700891 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700892}
893
Dima Zavinfce7a472011-04-19 22:30:36 -0700894status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700895 int indexMin,
896 int indexMax)
897{
898 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
899 if (aps == 0) return PERMISSION_DENIED;
900 return aps->initStreamVolume(stream, indexMin, indexMax);
901}
902
Eric Laurent83844cc2011-11-18 16:43:31 -0800903status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
904 int index,
905 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700906{
907 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
908 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800909 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700910}
911
Eric Laurent83844cc2011-11-18 16:43:31 -0800912status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
913 int *index,
914 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700915{
916 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
917 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800918 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700919}
920
Dima Zavinfce7a472011-04-19 22:30:36 -0700921uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700922{
923 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
924 if (aps == 0) return 0;
925 return aps->getStrategyForStream(stream);
926}
927
Eric Laurent63742522012-03-08 13:42:42 -0800928audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800929{
930 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800931 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800932 return aps->getDevicesForStream(stream);
933}
934
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700935audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700936{
937 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800938 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700939 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700940 return aps->getOutputForEffect(desc);
941}
942
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700943status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700944 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700945 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800946 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700947 int id)
948{
949 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
950 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700951 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700952}
953
954status_t AudioSystem::unregisterEffect(int id)
955{
956 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
957 if (aps == 0) return PERMISSION_DENIED;
958 return aps->unregisterEffect(id);
959}
960
Eric Laurentdb7c0792011-08-10 10:37:50 -0700961status_t AudioSystem::setEffectEnabled(int id, bool enabled)
962{
963 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
964 if (aps == 0) return PERMISSION_DENIED;
965 return aps->setEffectEnabled(id, enabled);
966}
967
Glenn Kastenfff6d712012-01-12 16:38:12 -0800968status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700969{
Eric Laurenteda6c362011-02-02 09:33:30 -0800970 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
971 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700972 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800973 *state = aps->isStreamActive(stream, inPastMs);
974 return NO_ERROR;
975}
976
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800977status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
978 uint32_t inPastMs)
979{
980 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
981 if (aps == 0) return PERMISSION_DENIED;
982 if (state == NULL) return BAD_VALUE;
983 *state = aps->isStreamActiveRemotely(stream, inPastMs);
984 return NO_ERROR;
985}
986
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700987status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
988{
989 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
990 if (aps == 0) return PERMISSION_DENIED;
991 if (state == NULL) return BAD_VALUE;
992 *state = aps->isSourceActive(stream);
993 return NO_ERROR;
994}
995
Glenn Kasten3b16c762012-11-14 08:44:39 -0800996uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700997{
998 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
999 if (af == 0) return 0;
1000 return af->getPrimaryOutputSamplingRate();
1001}
1002
Glenn Kastene33054e2012-11-14 12:54:39 -08001003size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001004{
1005 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1006 if (af == 0) return 0;
1007 return af->getPrimaryOutputFrameCount();
1008}
Eric Laurenteda6c362011-02-02 09:33:30 -08001009
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001010status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1011{
1012 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1013 if (af == 0) return PERMISSION_DENIED;
1014 return af->setLowRamDevice(isLowRamDevice);
1015}
1016
Eric Laurent9f6530f2011-08-30 10:18:54 -07001017void AudioSystem::clearAudioConfigCache()
1018{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001019 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001021 {
1022 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001023 if (gAudioFlingerClient != 0) {
1024 gAudioFlingerClient->clearIoCache();
1025 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001026 gAudioFlinger.clear();
1027 }
1028 {
1029 Mutex::Autolock _l(gLockAPS);
1030 gAudioPolicyService.clear();
1031 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001032}
1033
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001034bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1035{
1036 ALOGV("isOffloadSupported()");
1037 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1038 if (aps == 0) return false;
1039 return aps->isOffloadSupported(info);
1040}
1041
Eric Laurent203b1a12014-04-01 10:34:16 -07001042status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1043 audio_port_type_t type,
1044 unsigned int *num_ports,
1045 struct audio_port *ports,
1046 unsigned int *generation)
1047{
1048 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1049 if (aps == 0) return PERMISSION_DENIED;
1050 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1051}
1052
1053status_t AudioSystem::getAudioPort(struct audio_port *port)
1054{
1055 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1056 if (aps == 0) return PERMISSION_DENIED;
1057 return aps->getAudioPort(port);
1058}
1059
1060status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1061 audio_patch_handle_t *handle)
1062{
1063 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1064 if (aps == 0) return PERMISSION_DENIED;
1065 return aps->createAudioPatch(patch, handle);
1066}
1067
1068status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1069{
1070 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1071 if (aps == 0) return PERMISSION_DENIED;
1072 return aps->releaseAudioPatch(handle);
1073}
1074
1075status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1076 struct audio_patch *patches,
1077 unsigned int *generation)
1078{
1079 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1080 if (aps == 0) return PERMISSION_DENIED;
1081 return aps->listAudioPatches(num_patches, patches, generation);
1082}
1083
1084status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1085{
1086 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1087 if (aps == 0) return PERMISSION_DENIED;
1088 return aps->setAudioPortConfig(config);
1089}
1090
Eric Laurent296fb132015-05-01 11:38:42 -07001091status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001092{
Eric Laurentb28753e2015-04-01 13:06:28 -07001093 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1094 if (aps == 0) return PERMISSION_DENIED;
1095
1096 Mutex::Autolock _l(gLockAPS);
1097 if (gAudioPolicyServiceClient == 0) {
1098 return NO_INIT;
1099 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001100 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1101 if (ret == 1) {
1102 aps->setAudioPortCallbacksEnabled(true);
1103 }
1104 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001105}
1106
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001107/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001108status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001109{
1110 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1111 if (aps == 0) return PERMISSION_DENIED;
1112
1113 Mutex::Autolock _l(gLockAPS);
1114 if (gAudioPolicyServiceClient == 0) {
1115 return NO_INIT;
1116 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001117 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1118 if (ret == 0) {
1119 aps->setAudioPortCallbacksEnabled(false);
1120 }
1121 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001122}
1123
1124status_t AudioSystem::addAudioDeviceCallback(
1125 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1126{
1127 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1128 if (afc == 0) {
1129 return NO_INIT;
1130 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001131 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1132 if (status == NO_ERROR) {
1133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1134 if (af != 0) {
1135 af->registerClient(afc);
1136 }
1137 }
1138 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001139}
1140
1141status_t AudioSystem::removeAudioDeviceCallback(
1142 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1143{
1144 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1145 if (afc == 0) {
1146 return NO_INIT;
1147 }
1148 return afc->removeAudioDeviceCallback(callback, audioIo);
1149}
1150
1151audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1152{
1153 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1154 if (af == 0) return PERMISSION_DENIED;
1155 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1156 if (desc == 0) {
1157 return AUDIO_PORT_HANDLE_NONE;
1158 }
1159 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001160}
1161
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001162status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1163 audio_io_handle_t *ioHandle,
1164 audio_devices_t *device)
1165{
1166 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1167 if (aps == 0) return PERMISSION_DENIED;
1168 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1169}
1170
1171status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1172{
1173 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1174 if (aps == 0) return PERMISSION_DENIED;
1175 return aps->releaseSoundTriggerSession(session);
1176}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001177
1178audio_mode_t AudioSystem::getPhoneState()
1179{
1180 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1181 if (aps == 0) return AUDIO_MODE_INVALID;
1182 return aps->getPhoneState();
1183}
1184
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001185status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001186{
1187 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1188 if (aps == 0) return PERMISSION_DENIED;
1189 return aps->registerPolicyMixes(mixes, registration);
1190}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001191
Eric Laurent554a2772015-04-10 11:29:24 -07001192status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1193 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -07001194 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001195{
1196 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1197 if (aps == 0) return PERMISSION_DENIED;
1198 return aps->startAudioSource(source, attributes, handle);
1199}
1200
Glenn Kasten559d4392016-03-29 13:42:57 -07001201status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -07001202{
1203 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1204 if (aps == 0) return PERMISSION_DENIED;
1205 return aps->stopAudioSource(handle);
1206}
1207
Andy Hung2ddee192015-12-18 17:34:44 -08001208status_t AudioSystem::setMasterMono(bool mono)
1209{
1210 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1211 if (aps == 0) return PERMISSION_DENIED;
1212 return aps->setMasterMono(mono);
1213}
1214
1215status_t AudioSystem::getMasterMono(bool *mono)
1216{
1217 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1218 if (aps == 0) return PERMISSION_DENIED;
1219 return aps->getMasterMono(mono);
1220}
1221
Eric Laurentc2f1f072009-07-17 12:17:14 -07001222// ---------------------------------------------------------------------------
1223
Eric Laurente8726fe2015-06-26 09:39:24 -07001224int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001225 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001226{
1227 Mutex::Autolock _l(mLock);
1228 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001229 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001230 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001231 }
1232 }
Eric Laurent296fb132015-05-01 11:38:42 -07001233 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001234 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001235}
1236
Eric Laurente8726fe2015-06-26 09:39:24 -07001237int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001238 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001239{
1240 Mutex::Autolock _l(mLock);
1241 size_t i;
1242 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001243 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001244 break;
1245 }
1246 }
1247 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001248 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001249 }
1250 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001251 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001252}
1253
Eric Laurent296fb132015-05-01 11:38:42 -07001254
Eric Laurentb28753e2015-04-01 13:06:28 -07001255void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1256{
1257 Mutex::Autolock _l(mLock);
1258 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1259 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1260 }
1261}
1262
1263void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1264{
1265 Mutex::Autolock _l(mLock);
1266 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1267 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1268 }
1269}
1270
Jean-Michel Trivide801052015-04-14 19:10:14 -07001271void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1272 String8 regId, int32_t state)
1273{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001274 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1275 dynamic_policy_callback cb = NULL;
1276 {
1277 Mutex::Autolock _l(AudioSystem::gLock);
1278 cb = gDynPolicyCallback;
1279 }
1280
1281 if (cb != NULL) {
1282 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1283 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001284}
1285
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001286void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001287 int event, audio_session_t session, audio_source_t source,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001288 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1289 audio_patch_handle_t patchHandle) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001290 record_config_callback cb = NULL;
1291 {
1292 Mutex::Autolock _l(AudioSystem::gLock);
1293 cb = gRecordConfigCallback;
1294 }
1295
1296 if (cb != NULL) {
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001297 cb(event, session, source, clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001298 }
1299}
1300
Glenn Kasten4944acb2013-08-19 08:39:20 -07001301void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1302{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001303 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001304 Mutex::Autolock _l(mLock);
1305 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1306 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001307 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001308 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001309 {
1310 Mutex::Autolock _l(gLockAPS);
1311 AudioSystem::gAudioPolicyService.clear();
1312 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001313
Steve Block5ff1dd52012-01-05 23:22:43 +00001314 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001315}
1316
Glenn Kasten40bc9062015-03-20 09:09:33 -07001317} // namespace android