blob: fce4389b53a5bcbc640b07d2a43bb836fab51602 [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;
36Mutex AudioSystem::gLockAPC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037sp<IAudioFlinger> AudioSystem::gAudioFlinger;
38sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
39audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080040
Glenn Kasten2301acc2014-01-17 10:21:00 -080041// Cached values for output handles
Glenn Kasten9ea65d02014-01-17 10:21:24 -080042DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL);
Eric Laurentc2f1f072009-07-17 12:17:14 -070043
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -080044// Cached values for recording queries, all protected by gLock
Glenn Kasten5446e542014-01-08 08:58:53 -080045uint32_t AudioSystem::gPrevInSamplingRate;
46audio_format_t AudioSystem::gPrevInFormat;
47audio_channel_mask_t AudioSystem::gPrevInChannelMask;
48size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049
Eric Laurentb52c1522014-05-20 11:27:36 -070050sp<AudioSystem::AudioPortCallback> AudioSystem::gAudioPortCallback;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051
52// establish binder interface to AudioFlinger service
53const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
54{
55 Mutex::Autolock _l(gLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080056 if (gAudioFlinger == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057 sp<IServiceManager> sm = defaultServiceManager();
58 sp<IBinder> binder;
59 do {
60 binder = sm->getService(String16("media.audio_flinger"));
61 if (binder != 0)
62 break;
Steve Block5ff1dd52012-01-05 23:22:43 +000063 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070065 } while (true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066 if (gAudioFlingerClient == NULL) {
67 gAudioFlingerClient = new AudioFlingerClient();
68 } else {
69 if (gAudioErrorCallback) {
70 gAudioErrorCallback(NO_ERROR);
71 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070072 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 binder->linkToDeath(gAudioFlingerClient);
74 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
Glenn Kastend2d089f2014-11-05 11:48:12 -080075 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 }
Eric Laurentc2f1f072009-07-17 12:17:14 -070078
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080079 return gAudioFlinger;
80}
81
Eric Laurent46291612013-07-18 14:38:44 -070082/* static */ status_t AudioSystem::checkAudioFlinger()
83{
84 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
85 return NO_ERROR;
86 }
87 return DEAD_OBJECT;
88}
89
Glenn Kasten4944acb2013-08-19 08:39:20 -070090status_t AudioSystem::muteMicrophone(bool state)
91{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
93 if (af == 0) return PERMISSION_DENIED;
94 return af->setMicMute(state);
95}
96
Glenn Kasten4944acb2013-08-19 08:39:20 -070097status_t AudioSystem::isMicrophoneMuted(bool* state)
98{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
100 if (af == 0) return PERMISSION_DENIED;
101 *state = af->getMicMute();
102 return NO_ERROR;
103}
104
105status_t AudioSystem::setMasterVolume(float value)
106{
107 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
108 if (af == 0) return PERMISSION_DENIED;
109 af->setMasterVolume(value);
110 return NO_ERROR;
111}
112
113status_t AudioSystem::setMasterMute(bool mute)
114{
115 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
116 if (af == 0) return PERMISSION_DENIED;
117 af->setMasterMute(mute);
118 return NO_ERROR;
119}
120
121status_t AudioSystem::getMasterVolume(float* volume)
122{
123 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
124 if (af == 0) return PERMISSION_DENIED;
125 *volume = af->masterVolume();
126 return NO_ERROR;
127}
128
129status_t AudioSystem::getMasterMute(bool* mute)
130{
131 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
132 if (af == 0) return PERMISSION_DENIED;
133 *mute = af->masterMute();
134 return NO_ERROR;
135}
136
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800137status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
138 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139{
Dima Zavinfce7a472011-04-19 22:30:36 -0700140 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700143 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144 return NO_ERROR;
145}
146
Glenn Kastenfff6d712012-01-12 16:38:12 -0800147status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148{
Dima Zavinfce7a472011-04-19 22:30:36 -0700149 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
151 if (af == 0) return PERMISSION_DENIED;
152 af->setStreamMute(stream, mute);
153 return NO_ERROR;
154}
155
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800156status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
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 *volume = af->streamVolume(stream, 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::getStreamMute(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 *mute = af->streamMute(stream);
172 return NO_ERROR;
173}
174
Glenn Kastenf78aee72012-01-04 11:00:47 -0800175status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800177 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
179 if (af == 0) return PERMISSION_DENIED;
180 return af->setMode(mode);
181}
182
Glenn Kasten4944acb2013-08-19 08:39:20 -0700183status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
184{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
186 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700187 return af->setParameters(ioHandle, keyValuePairs);
188}
189
Glenn Kasten4944acb2013-08-19 08:39:20 -0700190String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
191{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700192 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
193 String8 result = String8("");
194 if (af == 0) return result;
195
196 result = af->getParameters(ioHandle, keys);
197 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198}
199
Glenn Kastenc23885e2013-12-19 16:35:18 -0800200status_t AudioSystem::setParameters(const String8& keyValuePairs)
201{
Glenn Kasten142f5192014-03-25 17:44:59 -0700202 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800203}
204
205String8 AudioSystem::getParameters(const String8& keys)
206{
Glenn Kasten142f5192014-03-25 17:44:59 -0700207 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800208}
209
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210// convert volume steps to natural log scale
211
212// change this value to change volume scaling
213static const float dBPerStep = 0.5f;
214// shouldn't need to touch these
215static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
216static const float dBConvertInverse = 1.0f / dBConvert;
217
218float AudioSystem::linearToLog(int volume)
219{
220 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000221 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 // return v;
223 return volume ? exp(float(100 - volume) * dBConvert) : 0;
224}
225
226int AudioSystem::logToLinear(float volume)
227{
228 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000229 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 // return v;
231 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
232}
233
Glenn Kasten3b16c762012-11-14 08:44:39 -0800234status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237
Dima Zavinfce7a472011-04-19 22:30:36 -0700238 if (streamType == AUDIO_STREAM_DEFAULT) {
239 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700240 }
241
Glenn Kastenfff6d712012-01-12 16:38:12 -0800242 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700243 if (output == 0) {
244 return PERMISSION_DENIED;
245 }
246
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700247 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700248}
249
250status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800251 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700252{
253 OutputDescriptor *outputDesc;
254
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255 gLock.lock();
256 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800257 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259 gLock.unlock();
260 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
261 if (af == 0) return PERMISSION_DENIED;
262 *samplingRate = af->sampleRate(output);
263 } else {
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265 *samplingRate = outputDesc->samplingRate;
266 gLock.unlock();
267 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800268 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700269 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800270 return BAD_VALUE;
271 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700273 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 return NO_ERROR;
276}
277
Glenn Kastene33054e2012-11-14 12:54:39 -0800278status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 if (streamType == AUDIO_STREAM_DEFAULT) {
283 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700285
Glenn Kastenfff6d712012-01-12 16:38:12 -0800286 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700287 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 return PERMISSION_DENIED;
289 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700291 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700292}
293
294status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800295 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700296{
297 OutputDescriptor *outputDesc;
298
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 gLock.lock();
300 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800301 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302 gLock.unlock();
303 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
304 if (af == 0) return PERMISSION_DENIED;
305 *frameCount = af->frameCount(output);
306 } else {
307 *frameCount = outputDesc->frameCount;
308 gLock.unlock();
309 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800310 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700311 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800312 return BAD_VALUE;
313 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700315 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return NO_ERROR;
318}
319
Glenn Kastenfff6d712012-01-12 16:38:12 -0800320status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700322 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323
Dima Zavinfce7a472011-04-19 22:30:36 -0700324 if (streamType == AUDIO_STREAM_DEFAULT) {
325 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700327
Glenn Kastenfff6d712012-01-12 16:38:12 -0800328 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700329 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700330 return PERMISSION_DENIED;
331 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332
Glenn Kasten241618f2014-03-25 17:48:57 -0700333 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700334}
335
336status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700337 uint32_t* latency)
338{
339 OutputDescriptor *outputDesc;
340
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 gLock.lock();
342 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800343 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 gLock.unlock();
345 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
346 if (af == 0) return PERMISSION_DENIED;
347 *latency = af->latency(output);
348 } else {
349 *latency = outputDesc->latency;
350 gLock.unlock();
351 }
352
Glenn Kasten241618f2014-03-25 17:48:57 -0700353 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 return NO_ERROR;
356}
357
Glenn Kastendd8104c2012-07-02 12:42:44 -0700358status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
359 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800361 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800363 size_t inBuffSize = gInBuffSize;
364 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700365 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800366 gLock.unlock();
367 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
368 if (af == 0) {
369 return PERMISSION_DENIED;
370 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700371 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kasten5446e542014-01-08 08:58:53 -0800372 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800373 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800374 sampleRate, format, channelMask);
375 return BAD_VALUE;
376 }
377 // A benign race is possible here: we could overwrite a fresher cache entry
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800378 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379 // save the request params
380 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700381 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700382 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800384 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800386 gLock.unlock();
387 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 return NO_ERROR;
390}
391
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700392status_t AudioSystem::setVoiceVolume(float value)
393{
394 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
395 if (af == 0) return PERMISSION_DENIED;
396 return af->setVoiceVolume(value);
397}
398
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000399status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700400 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800401{
402 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
403 if (af == 0) return PERMISSION_DENIED;
404
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000405 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800406}
407
Glenn Kasten4944acb2013-08-19 08:39:20 -0700408uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
409{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800410 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800411 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800412 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700413 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800414
415 result = af->getInputFramesLost(ioHandle);
416 return result;
417}
418
Eric Laurentde3f8392014-07-27 18:38:22 -0700419audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700420{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700422 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
423 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700424}
425
Marco Nelissend457c972014-02-11 08:47:07 -0800426void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700427{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
429 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800430 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700431 }
432}
433
Marco Nelissend457c972014-02-11 08:47:07 -0800434void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700435{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700436 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
437 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800438 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700439 }
440}
441
Eric Laurent93c3d412014-08-01 14:48:35 -0700442audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
443{
444 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
445 if (af == 0) return AUDIO_HW_SYNC_INVALID;
446 return af->getAudioHwSyncForSession(sessionId);
447}
448
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449// ---------------------------------------------------------------------------
450
Glenn Kasten4944acb2013-08-19 08:39:20 -0700451void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
452{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454
Eric Laurentc2f1f072009-07-17 12:17:14 -0700455 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800456 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800457 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458
459 if (gAudioErrorCallback) {
460 gAudioErrorCallback(DEAD_OBJECT);
461 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000462 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463}
464
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800465void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800466 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800468 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800469 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700470
Glenn Kasten142f5192014-03-25 17:44:59 -0700471 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472
473 Mutex::Autolock _l(AudioSystem::gLock);
474
475 switch (event) {
476 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700477 break;
478 case OUTPUT_OPENED: {
479 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481 break;
482 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800483 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800484 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700485
486 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
487 gOutputs.add(ioHandle, outputDesc);
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700488 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700489 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700490 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700491 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492 } break;
493 case OUTPUT_CLOSED: {
494 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800495 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700496 break;
497 }
Steve Block3856b092011-10-20 11:56:00 +0100498 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499
500 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700501 } break;
502
503 case OUTPUT_CONFIG_CHANGED: {
504 int index = gOutputs.indexOfKey(ioHandle);
505 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800506 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700507 break;
508 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800509 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800510 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700511
Glenn Kastencac3daa2014-02-07 09:47:14 -0800512 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700513 "frameCount %zu latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700515 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700516 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
517 delete outputDesc;
518 outputDesc = new OutputDescriptor(*desc);
519 gOutputs.replaceValueFor(ioHandle, outputDesc);
520 } break;
521 case INPUT_OPENED:
522 case INPUT_CLOSED:
523 case INPUT_CONFIG_CHANGED:
524 break;
525
526 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527}
528
Glenn Kasten4944acb2013-08-19 08:39:20 -0700529void AudioSystem::setErrorCallback(audio_error_callback cb)
530{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700531 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 gAudioErrorCallback = cb;
533}
534
Eric Laurentc2f1f072009-07-17 12:17:14 -0700535// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800536// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700537sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
538sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
539
540
Glenn Kasten18a6d902012-09-24 11:27:56 -0700541// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
543{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800544 Mutex::Autolock _l(gLockAPS);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800545 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700546 sp<IServiceManager> sm = defaultServiceManager();
547 sp<IBinder> binder;
548 do {
549 binder = sm->getService(String16("media.audio_policy"));
550 if (binder != 0)
551 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000552 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700553 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700554 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700555 if (gAudioPolicyServiceClient == NULL) {
556 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
557 }
558 binder->linkToDeath(gAudioPolicyServiceClient);
559 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800560 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
Andy Hungb4453752014-09-08 11:47:24 -0700561 gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700562 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800563
Eric Laurentc2f1f072009-07-17 12:17:14 -0700564 return gAudioPolicyService;
565}
566
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700567// ---------------------------------------------------------------------------
568
Dima Zavinfce7a472011-04-19 22:30:36 -0700569status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
570 audio_policy_dev_state_t state,
571 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572{
573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700574 const char *address = "";
575
Eric Laurentc2f1f072009-07-17 12:17:14 -0700576 if (aps == 0) return PERMISSION_DENIED;
577
Eric Laurent71b63e32011-09-02 14:20:56 -0700578 if (device_address != NULL) {
579 address = device_address;
580 }
581
582 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700583}
584
Dima Zavinfce7a472011-04-19 22:30:36 -0700585audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700586 const char *device_address)
587{
588 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700589 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700590
591 return aps->getDeviceConnectionState(device, device_address);
592}
593
Glenn Kastenf78aee72012-01-04 11:00:47 -0800594status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700595{
Glenn Kasten347966c2012-01-18 14:58:32 -0800596 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
598 if (aps == 0) return PERMISSION_DENIED;
599
600 return aps->setPhoneState(state);
601}
602
Dima Zavinfce7a472011-04-19 22:30:36 -0700603status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700604{
605 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
606 if (aps == 0) return PERMISSION_DENIED;
607 return aps->setForceUse(usage, config);
608}
609
Dima Zavinfce7a472011-04-19 22:30:36 -0700610audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700611{
612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700613 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700614 return aps->getForceUse(usage);
615}
616
617
Dima Zavinfce7a472011-04-19 22:30:36 -0700618audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800620 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700621 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000622 audio_output_flags_t flags,
623 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700624{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
626 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000627 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628}
629
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700630audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
631 uint32_t samplingRate,
632 audio_format_t format,
633 audio_channel_mask_t channelMask,
634 audio_output_flags_t flags,
635 const audio_offload_info_t *offloadInfo)
636{
637 if (attr == NULL) return 0;
638 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
639 if (aps == 0) return 0;
640 return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo);
641}
642
Eric Laurentde070132010-07-13 04:45:46 -0700643status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700644 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700645 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646{
647 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
648 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700649 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700650}
651
Eric Laurentde070132010-07-13 04:45:46 -0700652status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700653 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700654 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700655{
656 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
657 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700658 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700659}
660
661void AudioSystem::releaseOutput(audio_io_handle_t output)
662{
663 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
664 if (aps == 0) return;
665 aps->releaseOutput(output);
666}
667
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800668audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700669 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800670 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700671 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700672 int sessionId,
673 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700674{
675 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700676 if (aps == 0) return 0;
Glenn Kastenb3b16602014-07-16 08:36:31 -0700677 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700678}
679
Eric Laurent4dc68062014-07-28 17:26:49 -0700680status_t AudioSystem::startInput(audio_io_handle_t input,
681 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700682{
683 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
684 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700685 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686}
687
Eric Laurent4dc68062014-07-28 17:26:49 -0700688status_t AudioSystem::stopInput(audio_io_handle_t input,
689 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700690{
691 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
692 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700693 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694}
695
Eric Laurent4dc68062014-07-28 17:26:49 -0700696void AudioSystem::releaseInput(audio_io_handle_t input,
697 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700698{
699 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
700 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700701 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700702}
703
Dima Zavinfce7a472011-04-19 22:30:36 -0700704status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700705 int indexMin,
706 int indexMax)
707{
708 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
709 if (aps == 0) return PERMISSION_DENIED;
710 return aps->initStreamVolume(stream, indexMin, indexMax);
711}
712
Eric Laurent83844cc2011-11-18 16:43:31 -0800713status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
714 int index,
715 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700716{
717 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
718 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800719 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700720}
721
Eric Laurent83844cc2011-11-18 16:43:31 -0800722status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
723 int *index,
724 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700725{
726 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
727 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800728 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729}
730
Dima Zavinfce7a472011-04-19 22:30:36 -0700731uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700732{
733 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
734 if (aps == 0) return 0;
735 return aps->getStrategyForStream(stream);
736}
737
Eric Laurent63742522012-03-08 13:42:42 -0800738audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800739{
740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800741 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800742 return aps->getDevicesForStream(stream);
743}
744
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700745audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700746{
747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800748 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700749 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700750 return aps->getOutputForEffect(desc);
751}
752
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700753status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700754 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700755 uint32_t strategy,
756 int session,
757 int id)
758{
759 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
760 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700761 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700762}
763
764status_t AudioSystem::unregisterEffect(int id)
765{
766 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
767 if (aps == 0) return PERMISSION_DENIED;
768 return aps->unregisterEffect(id);
769}
770
Eric Laurentdb7c0792011-08-10 10:37:50 -0700771status_t AudioSystem::setEffectEnabled(int id, bool enabled)
772{
773 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
774 if (aps == 0) return PERMISSION_DENIED;
775 return aps->setEffectEnabled(id, enabled);
776}
777
Glenn Kastenfff6d712012-01-12 16:38:12 -0800778status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700779{
Eric Laurenteda6c362011-02-02 09:33:30 -0800780 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
781 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700782 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800783 *state = aps->isStreamActive(stream, inPastMs);
784 return NO_ERROR;
785}
786
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800787status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
788 uint32_t inPastMs)
789{
790 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
791 if (aps == 0) return PERMISSION_DENIED;
792 if (state == NULL) return BAD_VALUE;
793 *state = aps->isStreamActiveRemotely(stream, inPastMs);
794 return NO_ERROR;
795}
796
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700797status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
798{
799 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
800 if (aps == 0) return PERMISSION_DENIED;
801 if (state == NULL) return BAD_VALUE;
802 *state = aps->isSourceActive(stream);
803 return NO_ERROR;
804}
805
Glenn Kasten3b16c762012-11-14 08:44:39 -0800806uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700807{
808 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
809 if (af == 0) return 0;
810 return af->getPrimaryOutputSamplingRate();
811}
812
Glenn Kastene33054e2012-11-14 12:54:39 -0800813size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700814{
815 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
816 if (af == 0) return 0;
817 return af->getPrimaryOutputFrameCount();
818}
Eric Laurenteda6c362011-02-02 09:33:30 -0800819
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700820status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
821{
822 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
823 if (af == 0) return PERMISSION_DENIED;
824 return af->setLowRamDevice(isLowRamDevice);
825}
826
Eric Laurent9f6530f2011-08-30 10:18:54 -0700827void AudioSystem::clearAudioConfigCache()
828{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800829 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800831 {
832 Mutex::Autolock _l(gLock);
833 gOutputs.clear();
834 gAudioFlinger.clear();
835 }
836 {
837 Mutex::Autolock _l(gLockAPS);
838 gAudioPolicyService.clear();
839 }
840 // Do not clear gAudioPortCallback
Eric Laurent9f6530f2011-08-30 10:18:54 -0700841}
842
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000843bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
844{
845 ALOGV("isOffloadSupported()");
846 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
847 if (aps == 0) return false;
848 return aps->isOffloadSupported(info);
849}
850
Eric Laurent203b1a12014-04-01 10:34:16 -0700851status_t AudioSystem::listAudioPorts(audio_port_role_t role,
852 audio_port_type_t type,
853 unsigned int *num_ports,
854 struct audio_port *ports,
855 unsigned int *generation)
856{
857 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
858 if (aps == 0) return PERMISSION_DENIED;
859 return aps->listAudioPorts(role, type, num_ports, ports, generation);
860}
861
862status_t AudioSystem::getAudioPort(struct audio_port *port)
863{
864 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
865 if (aps == 0) return PERMISSION_DENIED;
866 return aps->getAudioPort(port);
867}
868
869status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
870 audio_patch_handle_t *handle)
871{
872 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
873 if (aps == 0) return PERMISSION_DENIED;
874 return aps->createAudioPatch(patch, handle);
875}
876
877status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
878{
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
880 if (aps == 0) return PERMISSION_DENIED;
881 return aps->releaseAudioPatch(handle);
882}
883
884status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
885 struct audio_patch *patches,
886 unsigned int *generation)
887{
888 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
889 if (aps == 0) return PERMISSION_DENIED;
890 return aps->listAudioPatches(num_patches, patches, generation);
891}
892
893status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
894{
895 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
896 if (aps == 0) return PERMISSION_DENIED;
897 return aps->setAudioPortConfig(config);
898}
899
Eric Laurentb52c1522014-05-20 11:27:36 -0700900void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
901{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800902 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700903 gAudioPortCallback = callBack;
904}
905
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700906status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
907 audio_io_handle_t *ioHandle,
908 audio_devices_t *device)
909{
910 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
911 if (aps == 0) return PERMISSION_DENIED;
912 return aps->acquireSoundTriggerSession(session, ioHandle, device);
913}
914
915status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
916{
917 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
918 if (aps == 0) return PERMISSION_DENIED;
919 return aps->releaseSoundTriggerSession(session);
920}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700921
922audio_mode_t AudioSystem::getPhoneState()
923{
924 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
925 if (aps == 0) return AUDIO_MODE_INVALID;
926 return aps->getPhoneState();
927}
928
929
Eric Laurentc2f1f072009-07-17 12:17:14 -0700930// ---------------------------------------------------------------------------
931
Glenn Kasten4944acb2013-08-19 08:39:20 -0700932void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
933{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800934 {
935 Mutex::Autolock _l(gLockAPC);
936 if (gAudioPortCallback != 0) {
937 gAudioPortCallback->onServiceDied();
938 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700939 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800940 {
941 Mutex::Autolock _l(gLockAPS);
942 AudioSystem::gAudioPolicyService.clear();
943 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944
Steve Block5ff1dd52012-01-05 23:22:43 +0000945 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700946}
947
Eric Laurentb52c1522014-05-20 11:27:36 -0700948void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
949{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800950 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700951 if (gAudioPortCallback != 0) {
952 gAudioPortCallback->onAudioPortListUpdate();
953 }
954}
955
956void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
957{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800958 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700959 if (gAudioPortCallback != 0) {
960 gAudioPortCallback->onAudioPatchListUpdate();
961 }
962}
963
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964}; // namespace android