blob: 01e6d71e8385383ddb310b3b6f6ff72acc835ba3 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070023#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070024#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <math.h>
26
Dima Zavin64760242011-05-11 14:15:23 -070027#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070028
Eric Laurentc2f1f072009-07-17 12:17:14 -070029// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070030
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031namespace android {
32
33// client singleton for AudioFlinger binder interface
34Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080035Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036sp<IAudioFlinger> AudioSystem::gAudioFlinger;
37sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
38audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080042const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080044 sp<IAudioFlinger> af;
45 sp<AudioFlingerClient> afc;
46 {
47 Mutex::Autolock _l(gLock);
48 if (gAudioFlinger == 0) {
49 sp<IServiceManager> sm = defaultServiceManager();
50 sp<IBinder> binder;
51 do {
52 binder = sm->getService(String16("media.audio_flinger"));
53 if (binder != 0)
54 break;
55 ALOGW("AudioFlinger not published, waiting...");
56 usleep(500000); // 0.5 s
57 } while (true);
58 if (gAudioFlingerClient == NULL) {
59 gAudioFlingerClient = new AudioFlingerClient();
60 } else {
61 if (gAudioErrorCallback) {
62 gAudioErrorCallback(NO_ERROR);
63 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080065 binder->linkToDeath(gAudioFlingerClient);
66 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
67 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
68 afc = gAudioFlingerClient;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070069 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080070 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080072 if (afc != 0) {
73 af->registerClient(afc);
74 }
75 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076}
77
Eric Laurent46291612013-07-18 14:38:44 -070078/* static */ status_t AudioSystem::checkAudioFlinger()
79{
80 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
81 return NO_ERROR;
82 }
83 return DEAD_OBJECT;
84}
85
Glenn Kasten4944acb2013-08-19 08:39:20 -070086status_t AudioSystem::muteMicrophone(bool state)
87{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080088 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
89 if (af == 0) return PERMISSION_DENIED;
90 return af->setMicMute(state);
91}
92
Glenn Kasten4944acb2013-08-19 08:39:20 -070093status_t AudioSystem::isMicrophoneMuted(bool* state)
94{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
96 if (af == 0) return PERMISSION_DENIED;
97 *state = af->getMicMute();
98 return NO_ERROR;
99}
100
101status_t AudioSystem::setMasterVolume(float value)
102{
103 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
104 if (af == 0) return PERMISSION_DENIED;
105 af->setMasterVolume(value);
106 return NO_ERROR;
107}
108
109status_t AudioSystem::setMasterMute(bool mute)
110{
111 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
112 if (af == 0) return PERMISSION_DENIED;
113 af->setMasterMute(mute);
114 return NO_ERROR;
115}
116
117status_t AudioSystem::getMasterVolume(float* volume)
118{
119 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
120 if (af == 0) return PERMISSION_DENIED;
121 *volume = af->masterVolume();
122 return NO_ERROR;
123}
124
125status_t AudioSystem::getMasterMute(bool* mute)
126{
127 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
128 if (af == 0) return PERMISSION_DENIED;
129 *mute = af->masterMute();
130 return NO_ERROR;
131}
132
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800133status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
134 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135{
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
138 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700139 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140 return NO_ERROR;
141}
142
Glenn Kastenfff6d712012-01-12 16:38:12 -0800143status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144{
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
147 if (af == 0) return PERMISSION_DENIED;
148 af->setStreamMute(stream, mute);
149 return NO_ERROR;
150}
151
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800152status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
153 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154{
Dima Zavinfce7a472011-04-19 22:30:36 -0700155 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
157 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700158 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 return NO_ERROR;
160}
161
Glenn Kastenfff6d712012-01-12 16:38:12 -0800162status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163{
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
166 if (af == 0) return PERMISSION_DENIED;
167 *mute = af->streamMute(stream);
168 return NO_ERROR;
169}
170
Glenn Kastenf78aee72012-01-04 11:00:47 -0800171status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800173 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
176 return af->setMode(mode);
177}
178
Glenn Kasten4944acb2013-08-19 08:39:20 -0700179status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
180{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
182 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700183 return af->setParameters(ioHandle, keyValuePairs);
184}
185
Glenn Kasten4944acb2013-08-19 08:39:20 -0700186String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
187{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700188 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
189 String8 result = String8("");
190 if (af == 0) return result;
191
192 result = af->getParameters(ioHandle, keys);
193 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194}
195
Glenn Kastenc23885e2013-12-19 16:35:18 -0800196status_t AudioSystem::setParameters(const String8& keyValuePairs)
197{
Glenn Kasten142f5192014-03-25 17:44:59 -0700198 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800199}
200
201String8 AudioSystem::getParameters(const String8& keys)
202{
Glenn Kasten142f5192014-03-25 17:44:59 -0700203 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800204}
205
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206// convert volume steps to natural log scale
207
208// change this value to change volume scaling
209static const float dBPerStep = 0.5f;
210// shouldn't need to touch these
211static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
212static const float dBConvertInverse = 1.0f / dBConvert;
213
214float AudioSystem::linearToLog(int volume)
215{
216 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000217 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800218 // return v;
219 return volume ? exp(float(100 - volume) * dBConvert) : 0;
220}
221
222int AudioSystem::logToLinear(float volume)
223{
224 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000225 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226 // return v;
227 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
228}
229
Glenn Kasten3b16c762012-11-14 08:44:39 -0800230status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700232 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 if (streamType == AUDIO_STREAM_DEFAULT) {
235 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 }
237
Glenn Kastenfff6d712012-01-12 16:38:12 -0800238 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239 if (output == 0) {
240 return PERMISSION_DENIED;
241 }
242
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700243 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700244}
245
246status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800247 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700248{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800249 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
250 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700251
Eric Laurent73e26b62015-04-27 16:55:58 -0700252 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
253 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
254 if (outputDesc == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700256 *samplingRate = af->sampleRate(output);
257 } else {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurent73e26b62015-04-27 16:55:58 -0700259 *samplingRate = outputDesc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700260 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800261 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700262 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800263 return BAD_VALUE;
264 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700266 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700267
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 return NO_ERROR;
269}
270
Glenn Kastene33054e2012-11-14 12:54:39 -0800271status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700273 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (streamType == AUDIO_STREAM_DEFAULT) {
276 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700278
Glenn Kastenfff6d712012-01-12 16:38:12 -0800279 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700280 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 return PERMISSION_DENIED;
282 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700284 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700285}
286
287status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800288 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700289{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800290 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
291 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700292
Eric Laurent73e26b62015-04-27 16:55:58 -0700293 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
294 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
295 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296 *frameCount = af->frameCount(output);
297 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700298 *frameCount = outputDesc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800300 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700301 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800302 return BAD_VALUE;
303 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700305 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 return NO_ERROR;
308}
309
Glenn Kastenfff6d712012-01-12 16:38:12 -0800310status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313
Dima Zavinfce7a472011-04-19 22:30:36 -0700314 if (streamType == AUDIO_STREAM_DEFAULT) {
315 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700317
Glenn Kastenfff6d712012-01-12 16:38:12 -0800318 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700319 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320 return PERMISSION_DENIED;
321 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322
Glenn Kasten241618f2014-03-25 17:48:57 -0700323 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700324}
325
326status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700327 uint32_t* latency)
328{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800329 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
330 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700331
Eric Laurent73e26b62015-04-27 16:55:58 -0700332 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
333 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
334 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 *latency = af->latency(output);
336 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700337 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700338 }
339
Glenn Kasten241618f2014-03-25 17:48:57 -0700340 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 return NO_ERROR;
343}
344
Glenn Kastendd8104c2012-07-02 12:42:44 -0700345status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
346 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800348 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700349 if (af == 0) return PERMISSION_DENIED;
350 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
351 return gAudioFlingerClient->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352}
353
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700354status_t AudioSystem::setVoiceVolume(float value)
355{
356 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
357 if (af == 0) return PERMISSION_DENIED;
358 return af->setVoiceVolume(value);
359}
360
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000361status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700362 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800363{
364 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
365 if (af == 0) return PERMISSION_DENIED;
366
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000367 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800368}
369
Glenn Kasten4944acb2013-08-19 08:39:20 -0700370uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
371{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800372 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800373 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800374 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700375 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800376
377 result = af->getInputFramesLost(ioHandle);
378 return result;
379}
380
Eric Laurentde3f8392014-07-27 18:38:22 -0700381audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700382{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700383 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700384 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
385 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700386}
387
Marco Nelissend457c972014-02-11 08:47:07 -0800388void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700389{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700390 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
391 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800392 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700393 }
394}
395
Marco Nelissend457c972014-02-11 08:47:07 -0800396void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700397{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700398 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
399 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800400 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700401 }
402}
403
Eric Laurent93c3d412014-08-01 14:48:35 -0700404audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
405{
406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af == 0) return AUDIO_HW_SYNC_INVALID;
408 return af->getAudioHwSyncForSession(sessionId);
409}
410
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411// ---------------------------------------------------------------------------
412
Eric Laurent73e26b62015-04-27 16:55:58 -0700413
414void AudioSystem::AudioFlingerClient::clearIoCache()
415{
416 Mutex::Autolock _l(mLock);
417 mIoDescriptors.clear();
418 mInBuffSize = 0;
419 mInSamplingRate = 0;
420 mInFormat = AUDIO_FORMAT_DEFAULT;
421 mInChannelMask = AUDIO_CHANNEL_NONE;
422}
423
Glenn Kasten4944acb2013-08-19 08:39:20 -0700424void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
425{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800426 audio_error_callback cb = NULL;
427 {
428 Mutex::Autolock _l(AudioSystem::gLock);
429 AudioSystem::gAudioFlinger.clear();
430 cb = gAudioErrorCallback;
431 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432
Eric Laurent73e26b62015-04-27 16:55:58 -0700433 // clear output handles and stream to output map caches
434 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435
Eric Laurentf6778fd2014-11-18 17:26:58 -0800436 if (cb) {
437 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000439 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440}
441
Eric Laurent73e26b62015-04-27 16:55:58 -0700442void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
443 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100444 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700445
Eric Laurent73e26b62015-04-27 16:55:58 -0700446 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700447
Eric Laurent73e26b62015-04-27 16:55:58 -0700448 Mutex::Autolock _l(mLock);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700449
450 switch (event) {
Eric Laurent73e26b62015-04-27 16:55:58 -0700451 case AUDIO_OUTPUT_OPENED:
452 case AUDIO_INPUT_OPENED: {
453 if (getIoDescriptor(ioDesc->mIoHandle) != 0) {
454 ALOGV("ioConfigChanged() opening already existing output! %d", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700455 break;
456 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700457 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
458 ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x "
459 "frameCount %zu", event == AUDIO_OUTPUT_OPENED ? "output" : "input",
460 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
461 ioDesc->mFrameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700462 } break;
Eric Laurent73e26b62015-04-27 16:55:58 -0700463 case AUDIO_OUTPUT_CLOSED:
464 case AUDIO_INPUT_CLOSED: {
465 if (getIoDescriptor(ioDesc->mIoHandle) == 0) {
466 ALOGW("ioConfigChanged() closing unknown %s %d",
467 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468 break;
469 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700470 ALOGV("ioConfigChanged() %s %d closed", event == AUDIO_OUTPUT_CLOSED ? "output" : "input",
471 ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472
Eric Laurent73e26b62015-04-27 16:55:58 -0700473 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700474 } break;
475
Eric Laurent73e26b62015-04-27 16:55:58 -0700476 case AUDIO_OUTPUT_CONFIG_CHANGED:
477 case AUDIO_INPUT_CONFIG_CHANGED: {
478 if (getIoDescriptor(ioDesc->mIoHandle) == 0) {
479 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700480 break;
481 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700482 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
483 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
484 "channel mask %#x frameCount %zu",
485 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
486 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
487 ioDesc->mChannelMask, ioDesc->mFrameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700489 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490}
491
Eric Laurent73e26b62015-04-27 16:55:58 -0700492status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
493 uint32_t sampleRate, audio_format_t format,
494 audio_channel_mask_t channelMask, size_t* buffSize)
495{
496 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
497 if (af == 0) {
498 return PERMISSION_DENIED;
499 }
500 Mutex::Autolock _l(mLock);
501 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
502 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
503 || (channelMask != mInChannelMask)) {
504 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
505 if (inBuffSize == 0) {
506 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
507 sampleRate, format, channelMask);
508 return BAD_VALUE;
509 }
510 // A benign race is possible here: we could overwrite a fresher cache entry
511 // save the request params
512 mInSamplingRate = sampleRate;
513 mInFormat = format;
514 mInChannelMask = channelMask;
515
516 mInBuffSize = inBuffSize;
517 }
518
519 *buffSize = mInBuffSize;
520
521 return NO_ERROR;
522}
523
524sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
525{
526 sp<AudioIoDescriptor> desc;
527 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
528 if (index >= 0) {
529 desc = mIoDescriptors.valueAt(index);
530 }
531 return desc;
532}
533
Glenn Kasten4944acb2013-08-19 08:39:20 -0700534void AudioSystem::setErrorCallback(audio_error_callback cb)
535{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700536 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800537 gAudioErrorCallback = cb;
538}
539
Eric Laurentc2f1f072009-07-17 12:17:14 -0700540// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800541// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
543sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
544
545
Glenn Kasten18a6d902012-09-24 11:27:56 -0700546// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800547const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700548{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800549 sp<IAudioPolicyService> ap;
550 sp<AudioPolicyServiceClient> apc;
551 {
552 Mutex::Autolock _l(gLockAPS);
553 if (gAudioPolicyService == 0) {
554 sp<IServiceManager> sm = defaultServiceManager();
555 sp<IBinder> binder;
556 do {
557 binder = sm->getService(String16("media.audio_policy"));
558 if (binder != 0)
559 break;
560 ALOGW("AudioPolicyService not published, waiting...");
561 usleep(500000); // 0.5 s
562 } while (true);
563 if (gAudioPolicyServiceClient == NULL) {
564 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
565 }
566 binder->linkToDeath(gAudioPolicyServiceClient);
567 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
568 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
569 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700570 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800571 ap = gAudioPolicyService;
572 }
573 if (apc != 0) {
574 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700575 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800576
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800577 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700578}
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,
Paul McLeane743a472015-01-28 11:07:31 -0800584 const char *device_address,
585 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700586{
587 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700588 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800589 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700590
Eric Laurentc2f1f072009-07-17 12:17:14 -0700591 if (aps == 0) return PERMISSION_DENIED;
592
Eric Laurent71b63e32011-09-02 14:20:56 -0700593 if (device_address != NULL) {
594 address = device_address;
595 }
Paul McLeane743a472015-01-28 11:07:31 -0800596 if (device_name != NULL) {
597 name = device_name;
598 }
599 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700600}
601
Dima Zavinfce7a472011-04-19 22:30:36 -0700602audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700603 const char *device_address)
604{
605 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700606 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700607
608 return aps->getDeviceConnectionState(device, device_address);
609}
610
Glenn Kastenf78aee72012-01-04 11:00:47 -0800611status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700612{
Glenn Kasten347966c2012-01-18 14:58:32 -0800613 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700614 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
615 if (aps == 0) return PERMISSION_DENIED;
616
617 return aps->setPhoneState(state);
618}
619
Dima Zavinfce7a472011-04-19 22:30:36 -0700620status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700621{
622 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
623 if (aps == 0) return PERMISSION_DENIED;
624 return aps->setForceUse(usage, config);
625}
626
Dima Zavinfce7a472011-04-19 22:30:36 -0700627audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628{
629 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700630 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700631 return aps->getForceUse(usage);
632}
633
634
Dima Zavinfce7a472011-04-19 22:30:36 -0700635audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700636 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800637 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700638 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000639 audio_output_flags_t flags,
640 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700642 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
643 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000644 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700645}
646
Eric Laurente83b55d2014-11-14 10:06:21 -0800647status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
648 audio_io_handle_t *output,
649 audio_session_t session,
650 audio_stream_type_t *stream,
651 uint32_t samplingRate,
652 audio_format_t format,
653 audio_channel_mask_t channelMask,
654 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700655 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800656 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700657{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700658 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800659 if (aps == 0) return NO_INIT;
660 return aps->getOutputForAttr(attr, output, session, stream,
661 samplingRate, format, channelMask,
Paul McLeanaa981192015-03-21 09:55:15 -0700662 flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700663}
664
Eric Laurentde070132010-07-13 04:45:46 -0700665status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700666 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800667 audio_session_t 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->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700672}
673
Eric Laurentde070132010-07-13 04:45:46 -0700674status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700675 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800676 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700677{
678 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
679 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700680 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700681}
682
Eric Laurente83b55d2014-11-14 10:06:21 -0800683void AudioSystem::releaseOutput(audio_io_handle_t output,
684 audio_stream_type_t stream,
685 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686{
687 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
688 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800689 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700690}
691
Eric Laurentcaf7f482014-11-25 17:50:47 -0800692status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
693 audio_io_handle_t *input,
694 audio_session_t session,
695 uint32_t samplingRate,
696 audio_format_t format,
697 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600698 audio_input_flags_t flags,
699 audio_port_handle_t selectedDeviceId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700700{
701 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800702 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600703 return aps->getInputForAttr(
704 attr, input, session, samplingRate, format, channelMask, flags, selectedDeviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700705}
706
Eric Laurent4dc68062014-07-28 17:26:49 -0700707status_t AudioSystem::startInput(audio_io_handle_t input,
708 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709{
710 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
711 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700712 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700713}
714
Eric Laurent4dc68062014-07-28 17:26:49 -0700715status_t AudioSystem::stopInput(audio_io_handle_t input,
716 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717{
718 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
719 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700720 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700721}
722
Eric Laurent4dc68062014-07-28 17:26:49 -0700723void AudioSystem::releaseInput(audio_io_handle_t input,
724 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700725{
726 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
727 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700728 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729}
730
Dima Zavinfce7a472011-04-19 22:30:36 -0700731status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700732 int indexMin,
733 int indexMax)
734{
735 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
736 if (aps == 0) return PERMISSION_DENIED;
737 return aps->initStreamVolume(stream, indexMin, indexMax);
738}
739
Eric Laurent83844cc2011-11-18 16:43:31 -0800740status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
741 int index,
742 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743{
744 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
745 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800746 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700747}
748
Eric Laurent83844cc2011-11-18 16:43:31 -0800749status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
750 int *index,
751 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700752{
753 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
754 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800755 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700756}
757
Dima Zavinfce7a472011-04-19 22:30:36 -0700758uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700759{
760 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
761 if (aps == 0) return 0;
762 return aps->getStrategyForStream(stream);
763}
764
Eric Laurent63742522012-03-08 13:42:42 -0800765audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800766{
767 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800768 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800769 return aps->getDevicesForStream(stream);
770}
771
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700772audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700773{
774 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800775 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700776 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700777 return aps->getOutputForEffect(desc);
778}
779
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700780status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700781 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700782 uint32_t strategy,
783 int session,
784 int id)
785{
786 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
787 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700788 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700789}
790
791status_t AudioSystem::unregisterEffect(int id)
792{
793 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
794 if (aps == 0) return PERMISSION_DENIED;
795 return aps->unregisterEffect(id);
796}
797
Eric Laurentdb7c0792011-08-10 10:37:50 -0700798status_t AudioSystem::setEffectEnabled(int id, bool enabled)
799{
800 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
801 if (aps == 0) return PERMISSION_DENIED;
802 return aps->setEffectEnabled(id, enabled);
803}
804
Glenn Kastenfff6d712012-01-12 16:38:12 -0800805status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700806{
Eric Laurenteda6c362011-02-02 09:33:30 -0800807 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
808 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700809 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800810 *state = aps->isStreamActive(stream, inPastMs);
811 return NO_ERROR;
812}
813
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800814status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
815 uint32_t inPastMs)
816{
817 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
818 if (aps == 0) return PERMISSION_DENIED;
819 if (state == NULL) return BAD_VALUE;
820 *state = aps->isStreamActiveRemotely(stream, inPastMs);
821 return NO_ERROR;
822}
823
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700824status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
825{
826 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
827 if (aps == 0) return PERMISSION_DENIED;
828 if (state == NULL) return BAD_VALUE;
829 *state = aps->isSourceActive(stream);
830 return NO_ERROR;
831}
832
Glenn Kasten3b16c762012-11-14 08:44:39 -0800833uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700834{
835 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
836 if (af == 0) return 0;
837 return af->getPrimaryOutputSamplingRate();
838}
839
Glenn Kastene33054e2012-11-14 12:54:39 -0800840size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700841{
842 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
843 if (af == 0) return 0;
844 return af->getPrimaryOutputFrameCount();
845}
Eric Laurenteda6c362011-02-02 09:33:30 -0800846
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700847status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
848{
849 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
850 if (af == 0) return PERMISSION_DENIED;
851 return af->setLowRamDevice(isLowRamDevice);
852}
853
Eric Laurent9f6530f2011-08-30 10:18:54 -0700854void AudioSystem::clearAudioConfigCache()
855{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800856 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100857 ALOGV("clearAudioConfigCache()");
Eric Laurent73e26b62015-04-27 16:55:58 -0700858 if (gAudioFlingerClient != 0) {
859 gAudioFlingerClient->clearIoCache();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800860 }
861 {
862 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800863 gAudioFlinger.clear();
864 }
865 {
866 Mutex::Autolock _l(gLockAPS);
867 gAudioPolicyService.clear();
868 }
Eric Laurent9f6530f2011-08-30 10:18:54 -0700869}
870
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000871bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
872{
873 ALOGV("isOffloadSupported()");
874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
875 if (aps == 0) return false;
876 return aps->isOffloadSupported(info);
877}
878
Eric Laurent203b1a12014-04-01 10:34:16 -0700879status_t AudioSystem::listAudioPorts(audio_port_role_t role,
880 audio_port_type_t type,
881 unsigned int *num_ports,
882 struct audio_port *ports,
883 unsigned int *generation)
884{
885 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
886 if (aps == 0) return PERMISSION_DENIED;
887 return aps->listAudioPorts(role, type, num_ports, ports, generation);
888}
889
890status_t AudioSystem::getAudioPort(struct audio_port *port)
891{
892 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
893 if (aps == 0) return PERMISSION_DENIED;
894 return aps->getAudioPort(port);
895}
896
897status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
898 audio_patch_handle_t *handle)
899{
900 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
901 if (aps == 0) return PERMISSION_DENIED;
902 return aps->createAudioPatch(patch, handle);
903}
904
905status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
906{
907 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
908 if (aps == 0) return PERMISSION_DENIED;
909 return aps->releaseAudioPatch(handle);
910}
911
912status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
913 struct audio_patch *patches,
914 unsigned int *generation)
915{
916 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
917 if (aps == 0) return PERMISSION_DENIED;
918 return aps->listAudioPatches(num_patches, patches, generation);
919}
920
921status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
922{
923 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
924 if (aps == 0) return PERMISSION_DENIED;
925 return aps->setAudioPortConfig(config);
926}
927
Eric Laurentb28753e2015-04-01 13:06:28 -0700928status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callBack)
Eric Laurentb52c1522014-05-20 11:27:36 -0700929{
Eric Laurentb28753e2015-04-01 13:06:28 -0700930 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
931 if (aps == 0) return PERMISSION_DENIED;
932
933 Mutex::Autolock _l(gLockAPS);
934 if (gAudioPolicyServiceClient == 0) {
935 return NO_INIT;
936 }
937 return gAudioPolicyServiceClient->addAudioPortCallback(callBack);
Eric Laurentb52c1522014-05-20 11:27:36 -0700938}
939
Eric Laurentb28753e2015-04-01 13:06:28 -0700940status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callBack)
941{
942 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
943 if (aps == 0) return PERMISSION_DENIED;
944
945 Mutex::Autolock _l(gLockAPS);
946 if (gAudioPolicyServiceClient == 0) {
947 return NO_INIT;
948 }
949 return gAudioPolicyServiceClient->removeAudioPortCallback(callBack);
950}
951
952
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700953status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
954 audio_io_handle_t *ioHandle,
955 audio_devices_t *device)
956{
957 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
958 if (aps == 0) return PERMISSION_DENIED;
959 return aps->acquireSoundTriggerSession(session, ioHandle, device);
960}
961
962status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
963{
964 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
965 if (aps == 0) return PERMISSION_DENIED;
966 return aps->releaseSoundTriggerSession(session);
967}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700968
969audio_mode_t AudioSystem::getPhoneState()
970{
971 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
972 if (aps == 0) return AUDIO_MODE_INVALID;
973 return aps->getPhoneState();
974}
975
Eric Laurentbaac1832014-12-01 17:52:59 -0800976status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
977{
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
980 return aps->registerPolicyMixes(mixes, registration);
981}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700982
Eric Laurent554a2772015-04-10 11:29:24 -0700983status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
984 const audio_attributes_t *attributes,
985 audio_io_handle_t *handle)
986{
987 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
988 if (aps == 0) return PERMISSION_DENIED;
989 return aps->startAudioSource(source, attributes, handle);
990}
991
992status_t AudioSystem::stopAudioSource(audio_io_handle_t handle)
993{
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
996 return aps->stopAudioSource(handle);
997}
998
Eric Laurentc2f1f072009-07-17 12:17:14 -0700999// ---------------------------------------------------------------------------
1000
Eric Laurentb28753e2015-04-01 13:06:28 -07001001status_t AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1002 const sp<AudioPortCallback>& callBack)
1003{
1004 Mutex::Autolock _l(mLock);
1005 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1006 if (mAudioPortCallbacks[i] == callBack) {
1007 return INVALID_OPERATION;
1008 }
1009 }
1010 mAudioPortCallbacks.add(callBack);
1011 return NO_ERROR;
1012}
1013
1014status_t AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1015 const sp<AudioPortCallback>& callBack)
1016{
1017 Mutex::Autolock _l(mLock);
1018 size_t i;
1019 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1020 if (mAudioPortCallbacks[i] == callBack) {
1021 break;
1022 }
1023 }
1024 if (i == mAudioPortCallbacks.size()) {
1025 return INVALID_OPERATION;
1026 }
1027 mAudioPortCallbacks.removeAt(i);
1028 return NO_ERROR;
1029}
1030
1031void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1032{
1033 Mutex::Autolock _l(mLock);
1034 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1035 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1036 }
1037}
1038
1039void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1040{
1041 Mutex::Autolock _l(mLock);
1042 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1043 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1044 }
1045}
1046
Jean-Michel Trivide801052015-04-14 19:10:14 -07001047void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1048 String8 regId, int32_t state)
1049{
1050 ALOGV("TODO propagate onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1051}
1052
Glenn Kasten4944acb2013-08-19 08:39:20 -07001053void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1054{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001055 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001056 Mutex::Autolock _l(mLock);
1057 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1058 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001059 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001060 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001061 {
1062 Mutex::Autolock _l(gLockAPS);
1063 AudioSystem::gAudioPolicyService.clear();
1064 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001065
Steve Block5ff1dd52012-01-05 23:22:43 +00001066 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001067}
1068
Glenn Kasten40bc9062015-03-20 09:09:33 -07001069} // namespace android