blob: f5c338380909df7a9f07f38bcf65b6202586c462 [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
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700250status_t AudioSystem::getOutputSamplingRateForAttr(uint32_t* samplingRate,
251 const audio_attributes_t *attr)
252{
253 if (attr == NULL) {
254 return BAD_VALUE;
255 }
256 audio_io_handle_t output = getOutputForAttr(attr);
257 if (output == 0) {
258 return PERMISSION_DENIED;
259 }
260 return getSamplingRate(output, samplingRate);
261}
262
Eric Laurent1a9ed112012-03-20 18:36:01 -0700263status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800264 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700265{
266 OutputDescriptor *outputDesc;
267
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 gLock.lock();
269 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800270 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 gLock.unlock();
273 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
274 if (af == 0) return PERMISSION_DENIED;
275 *samplingRate = af->sampleRate(output);
276 } else {
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278 *samplingRate = outputDesc->samplingRate;
279 gLock.unlock();
280 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800281 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700282 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800283 return BAD_VALUE;
284 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700286 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 return NO_ERROR;
289}
290
Glenn Kastene33054e2012-11-14 12:54:39 -0800291status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294
Dima Zavinfce7a472011-04-19 22:30:36 -0700295 if (streamType == AUDIO_STREAM_DEFAULT) {
296 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700298
Glenn Kastenfff6d712012-01-12 16:38:12 -0800299 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700300 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700301 return PERMISSION_DENIED;
302 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700304 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700305}
306
307status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800308 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700309{
310 OutputDescriptor *outputDesc;
311
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 gLock.lock();
313 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800314 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 gLock.unlock();
316 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
317 if (af == 0) return PERMISSION_DENIED;
318 *frameCount = af->frameCount(output);
319 } else {
320 *frameCount = outputDesc->frameCount;
321 gLock.unlock();
322 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800323 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700324 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800325 return BAD_VALUE;
326 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700328 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return NO_ERROR;
331}
332
Glenn Kastenfff6d712012-01-12 16:38:12 -0800333status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (streamType == AUDIO_STREAM_DEFAULT) {
338 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700340
Glenn Kastenfff6d712012-01-12 16:38:12 -0800341 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700342 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700343 return PERMISSION_DENIED;
344 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345
Glenn Kasten241618f2014-03-25 17:48:57 -0700346 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700347}
348
349status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700350 uint32_t* latency)
351{
352 OutputDescriptor *outputDesc;
353
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354 gLock.lock();
355 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800356 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 gLock.unlock();
358 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
359 if (af == 0) return PERMISSION_DENIED;
360 *latency = af->latency(output);
361 } else {
362 *latency = outputDesc->latency;
363 gLock.unlock();
364 }
365
Glenn Kasten241618f2014-03-25 17:48:57 -0700366 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700367
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 return NO_ERROR;
369}
370
Glenn Kastendd8104c2012-07-02 12:42:44 -0700371status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
372 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800374 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800376 size_t inBuffSize = gInBuffSize;
377 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700378 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800379 gLock.unlock();
380 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
381 if (af == 0) {
382 return PERMISSION_DENIED;
383 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700384 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kasten5446e542014-01-08 08:58:53 -0800385 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800386 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800387 sampleRate, format, channelMask);
388 return BAD_VALUE;
389 }
390 // A benign race is possible here: we could overwrite a fresher cache entry
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800391 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 // save the request params
393 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700394 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700395 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800397 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700398 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800399 gLock.unlock();
400 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 return NO_ERROR;
403}
404
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700405status_t AudioSystem::setVoiceVolume(float value)
406{
407 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
408 if (af == 0) return PERMISSION_DENIED;
409 return af->setVoiceVolume(value);
410}
411
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000412status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700413 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800414{
415 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
416 if (af == 0) return PERMISSION_DENIED;
417
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000418 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800419}
420
Glenn Kasten4944acb2013-08-19 08:39:20 -0700421uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
422{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800423 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800424 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800425 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700426 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800427
428 result = af->getInputFramesLost(ioHandle);
429 return result;
430}
431
Eric Laurentde3f8392014-07-27 18:38:22 -0700432audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700433{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700434 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700435 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
436 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700437}
438
Marco Nelissend457c972014-02-11 08:47:07 -0800439void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700440{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700441 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
442 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800443 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700444 }
445}
446
Marco Nelissend457c972014-02-11 08:47:07 -0800447void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700448{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700449 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
450 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800451 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700452 }
453}
454
Eric Laurent93c3d412014-08-01 14:48:35 -0700455audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
456{
457 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
458 if (af == 0) return AUDIO_HW_SYNC_INVALID;
459 return af->getAudioHwSyncForSession(sessionId);
460}
461
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462// ---------------------------------------------------------------------------
463
Glenn Kasten4944acb2013-08-19 08:39:20 -0700464void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
465{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800469 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800470 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471
472 if (gAudioErrorCallback) {
473 gAudioErrorCallback(DEAD_OBJECT);
474 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000475 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476}
477
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800478void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800479 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800481 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800482 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483
Glenn Kasten142f5192014-03-25 17:44:59 -0700484 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700485
486 Mutex::Autolock _l(AudioSystem::gLock);
487
488 switch (event) {
489 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700490 break;
491 case OUTPUT_OPENED: {
492 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494 break;
495 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800496 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800497 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700498
499 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
500 gOutputs.add(ioHandle, outputDesc);
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700501 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700502 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700503 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700504 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700505 } break;
506 case OUTPUT_CLOSED: {
507 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800508 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700509 break;
510 }
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700512
513 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514 } break;
515
516 case OUTPUT_CONFIG_CHANGED: {
517 int index = gOutputs.indexOfKey(ioHandle);
518 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800519 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700520 break;
521 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800522 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800523 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
Glenn Kastencac3daa2014-02-07 09:47:14 -0800525 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700526 "frameCount %zu latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700528 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700529 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
530 delete outputDesc;
531 outputDesc = new OutputDescriptor(*desc);
532 gOutputs.replaceValueFor(ioHandle, outputDesc);
533 } break;
534 case INPUT_OPENED:
535 case INPUT_CLOSED:
536 case INPUT_CONFIG_CHANGED:
537 break;
538
539 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540}
541
Glenn Kasten4944acb2013-08-19 08:39:20 -0700542void AudioSystem::setErrorCallback(audio_error_callback cb)
543{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700544 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 gAudioErrorCallback = cb;
546}
547
Eric Laurentc2f1f072009-07-17 12:17:14 -0700548// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800549// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700550sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
551sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
552
553
Glenn Kasten18a6d902012-09-24 11:27:56 -0700554// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700555const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
556{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800557 Mutex::Autolock _l(gLockAPS);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800558 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700559 sp<IServiceManager> sm = defaultServiceManager();
560 sp<IBinder> binder;
561 do {
562 binder = sm->getService(String16("media.audio_policy"));
563 if (binder != 0)
564 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000565 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700566 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700567 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700568 if (gAudioPolicyServiceClient == NULL) {
569 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
570 }
571 binder->linkToDeath(gAudioPolicyServiceClient);
572 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800573 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
Andy Hungb4453752014-09-08 11:47:24 -0700574 gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700575 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800576
Eric Laurentc2f1f072009-07-17 12:17:14 -0700577 return gAudioPolicyService;
578}
579
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700580// ---------------------------------------------------------------------------
581
Dima Zavinfce7a472011-04-19 22:30:36 -0700582status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
583 audio_policy_dev_state_t state,
584 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700585{
586 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700587 const char *address = "";
588
Eric Laurentc2f1f072009-07-17 12:17:14 -0700589 if (aps == 0) return PERMISSION_DENIED;
590
Eric Laurent71b63e32011-09-02 14:20:56 -0700591 if (device_address != NULL) {
592 address = device_address;
593 }
594
595 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700596}
597
Dima Zavinfce7a472011-04-19 22:30:36 -0700598audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700599 const char *device_address)
600{
601 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700602 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700603
604 return aps->getDeviceConnectionState(device, device_address);
605}
606
Glenn Kastenf78aee72012-01-04 11:00:47 -0800607status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700608{
Glenn Kasten347966c2012-01-18 14:58:32 -0800609 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700610 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
611 if (aps == 0) return PERMISSION_DENIED;
612
613 return aps->setPhoneState(state);
614}
615
Dima Zavinfce7a472011-04-19 22:30:36 -0700616status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700617{
618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
619 if (aps == 0) return PERMISSION_DENIED;
620 return aps->setForceUse(usage, config);
621}
622
Dima Zavinfce7a472011-04-19 22:30:36 -0700623audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700624{
625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700626 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700627 return aps->getForceUse(usage);
628}
629
630
Dima Zavinfce7a472011-04-19 22:30:36 -0700631audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700632 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800633 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700634 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000635 audio_output_flags_t flags,
636 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700637{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700638 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
639 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000640 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641}
642
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700643audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
644 uint32_t samplingRate,
645 audio_format_t format,
646 audio_channel_mask_t channelMask,
647 audio_output_flags_t flags,
648 const audio_offload_info_t *offloadInfo)
649{
650 if (attr == NULL) return 0;
651 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
652 if (aps == 0) return 0;
653 return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo);
654}
655
Eric Laurentde070132010-07-13 04:45:46 -0700656status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700657 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700658 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700659{
660 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
661 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700662 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700663}
664
Eric Laurentde070132010-07-13 04:45:46 -0700665status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700666 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700667 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700668{
669 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
670 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700671 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700672}
673
674void AudioSystem::releaseOutput(audio_io_handle_t output)
675{
676 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
677 if (aps == 0) return;
678 aps->releaseOutput(output);
679}
680
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800681audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700682 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800683 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700684 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700685 int sessionId,
686 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700687{
688 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700689 if (aps == 0) return 0;
Glenn Kastenb3b16602014-07-16 08:36:31 -0700690 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700691}
692
Eric Laurent4dc68062014-07-28 17:26:49 -0700693status_t AudioSystem::startInput(audio_io_handle_t input,
694 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700695{
696 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
697 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700698 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700699}
700
Eric Laurent4dc68062014-07-28 17:26:49 -0700701status_t AudioSystem::stopInput(audio_io_handle_t input,
702 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700703{
704 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
705 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700706 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700707}
708
Eric Laurent4dc68062014-07-28 17:26:49 -0700709void AudioSystem::releaseInput(audio_io_handle_t input,
710 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700711{
712 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
713 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700714 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700715}
716
Dima Zavinfce7a472011-04-19 22:30:36 -0700717status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718 int indexMin,
719 int indexMax)
720{
721 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
722 if (aps == 0) return PERMISSION_DENIED;
723 return aps->initStreamVolume(stream, indexMin, indexMax);
724}
725
Eric Laurent83844cc2011-11-18 16:43:31 -0800726status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
727 int index,
728 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729{
730 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
731 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800732 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700733}
734
Eric Laurent83844cc2011-11-18 16:43:31 -0800735status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
736 int *index,
737 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700738{
739 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
740 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800741 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700742}
743
Dima Zavinfce7a472011-04-19 22:30:36 -0700744uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700745{
746 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
747 if (aps == 0) return 0;
748 return aps->getStrategyForStream(stream);
749}
750
Eric Laurent63742522012-03-08 13:42:42 -0800751audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800752{
753 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800754 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800755 return aps->getDevicesForStream(stream);
756}
757
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700758audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700759{
760 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800761 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700762 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700763 return aps->getOutputForEffect(desc);
764}
765
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700766status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700767 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700768 uint32_t strategy,
769 int session,
770 int id)
771{
772 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
773 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700774 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700775}
776
777status_t AudioSystem::unregisterEffect(int id)
778{
779 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
780 if (aps == 0) return PERMISSION_DENIED;
781 return aps->unregisterEffect(id);
782}
783
Eric Laurentdb7c0792011-08-10 10:37:50 -0700784status_t AudioSystem::setEffectEnabled(int id, bool enabled)
785{
786 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
787 if (aps == 0) return PERMISSION_DENIED;
788 return aps->setEffectEnabled(id, enabled);
789}
790
Glenn Kastenfff6d712012-01-12 16:38:12 -0800791status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700792{
Eric Laurenteda6c362011-02-02 09:33:30 -0800793 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
794 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700795 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800796 *state = aps->isStreamActive(stream, inPastMs);
797 return NO_ERROR;
798}
799
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800800status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
801 uint32_t inPastMs)
802{
803 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
804 if (aps == 0) return PERMISSION_DENIED;
805 if (state == NULL) return BAD_VALUE;
806 *state = aps->isStreamActiveRemotely(stream, inPastMs);
807 return NO_ERROR;
808}
809
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700810status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
811{
812 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
813 if (aps == 0) return PERMISSION_DENIED;
814 if (state == NULL) return BAD_VALUE;
815 *state = aps->isSourceActive(stream);
816 return NO_ERROR;
817}
818
Glenn Kasten3b16c762012-11-14 08:44:39 -0800819uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700820{
821 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
822 if (af == 0) return 0;
823 return af->getPrimaryOutputSamplingRate();
824}
825
Glenn Kastene33054e2012-11-14 12:54:39 -0800826size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700827{
828 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
829 if (af == 0) return 0;
830 return af->getPrimaryOutputFrameCount();
831}
Eric Laurenteda6c362011-02-02 09:33:30 -0800832
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700833status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
834{
835 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
836 if (af == 0) return PERMISSION_DENIED;
837 return af->setLowRamDevice(isLowRamDevice);
838}
839
Eric Laurent9f6530f2011-08-30 10:18:54 -0700840void AudioSystem::clearAudioConfigCache()
841{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800842 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100843 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800844 {
845 Mutex::Autolock _l(gLock);
846 gOutputs.clear();
847 gAudioFlinger.clear();
848 }
849 {
850 Mutex::Autolock _l(gLockAPS);
851 gAudioPolicyService.clear();
852 }
853 // Do not clear gAudioPortCallback
Eric Laurent9f6530f2011-08-30 10:18:54 -0700854}
855
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000856bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
857{
858 ALOGV("isOffloadSupported()");
859 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
860 if (aps == 0) return false;
861 return aps->isOffloadSupported(info);
862}
863
Eric Laurent203b1a12014-04-01 10:34:16 -0700864status_t AudioSystem::listAudioPorts(audio_port_role_t role,
865 audio_port_type_t type,
866 unsigned int *num_ports,
867 struct audio_port *ports,
868 unsigned int *generation)
869{
870 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
871 if (aps == 0) return PERMISSION_DENIED;
872 return aps->listAudioPorts(role, type, num_ports, ports, generation);
873}
874
875status_t AudioSystem::getAudioPort(struct audio_port *port)
876{
877 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
878 if (aps == 0) return PERMISSION_DENIED;
879 return aps->getAudioPort(port);
880}
881
882status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
883 audio_patch_handle_t *handle)
884{
885 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
886 if (aps == 0) return PERMISSION_DENIED;
887 return aps->createAudioPatch(patch, handle);
888}
889
890status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
891{
892 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
893 if (aps == 0) return PERMISSION_DENIED;
894 return aps->releaseAudioPatch(handle);
895}
896
897status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
898 struct audio_patch *patches,
899 unsigned int *generation)
900{
901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
902 if (aps == 0) return PERMISSION_DENIED;
903 return aps->listAudioPatches(num_patches, patches, generation);
904}
905
906status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
907{
908 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
909 if (aps == 0) return PERMISSION_DENIED;
910 return aps->setAudioPortConfig(config);
911}
912
Eric Laurentb52c1522014-05-20 11:27:36 -0700913void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
914{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800915 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700916 gAudioPortCallback = callBack;
917}
918
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700919status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
920 audio_io_handle_t *ioHandle,
921 audio_devices_t *device)
922{
923 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
924 if (aps == 0) return PERMISSION_DENIED;
925 return aps->acquireSoundTriggerSession(session, ioHandle, device);
926}
927
928status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
929{
930 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
931 if (aps == 0) return PERMISSION_DENIED;
932 return aps->releaseSoundTriggerSession(session);
933}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700934
935audio_mode_t AudioSystem::getPhoneState()
936{
937 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
938 if (aps == 0) return AUDIO_MODE_INVALID;
939 return aps->getPhoneState();
940}
941
942
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943// ---------------------------------------------------------------------------
944
Glenn Kasten4944acb2013-08-19 08:39:20 -0700945void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
946{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800947 {
948 Mutex::Autolock _l(gLockAPC);
949 if (gAudioPortCallback != 0) {
950 gAudioPortCallback->onServiceDied();
951 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700952 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800953 {
954 Mutex::Autolock _l(gLockAPS);
955 AudioSystem::gAudioPolicyService.clear();
956 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957
Steve Block5ff1dd52012-01-05 23:22:43 +0000958 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959}
960
Eric Laurentb52c1522014-05-20 11:27:36 -0700961void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
962{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800963 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700964 if (gAudioPortCallback != 0) {
965 gAudioPortCallback->onAudioPortListUpdate();
966 }
967}
968
969void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
970{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800971 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700972 if (gAudioPortCallback != 0) {
973 gAudioPortCallback->onAudioPatchListUpdate();
974 }
975}
976
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977}; // namespace android