blob: eafb3adc484d0bcb18155f342e87a769f33affa8 [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;
35sp<IAudioFlinger> AudioSystem::gAudioFlinger;
36sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
37audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080038
Glenn Kasten2301acc2014-01-17 10:21:00 -080039// Cached values for output handles
Glenn Kasten9ea65d02014-01-17 10:21:24 -080040DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL);
Eric Laurentc2f1f072009-07-17 12:17:14 -070041
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -080042// Cached values for recording queries, all protected by gLock
Glenn Kasten5446e542014-01-08 08:58:53 -080043uint32_t AudioSystem::gPrevInSamplingRate;
44audio_format_t AudioSystem::gPrevInFormat;
45audio_channel_mask_t AudioSystem::gPrevInChannelMask;
46size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Eric Laurentb52c1522014-05-20 11:27:36 -070048sp<AudioSystem::AudioPortCallback> AudioSystem::gAudioPortCallback;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049
50// establish binder interface to AudioFlinger service
51const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
52{
53 Mutex::Autolock _l(gLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080054 if (gAudioFlinger == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 sp<IServiceManager> sm = defaultServiceManager();
56 sp<IBinder> binder;
57 do {
58 binder = sm->getService(String16("media.audio_flinger"));
59 if (binder != 0)
60 break;
Steve Block5ff1dd52012-01-05 23:22:43 +000061 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070063 } while (true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 if (gAudioFlingerClient == NULL) {
65 gAudioFlingerClient = new AudioFlingerClient();
66 } else {
67 if (gAudioErrorCallback) {
68 gAudioErrorCallback(NO_ERROR);
69 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070070 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 binder->linkToDeath(gAudioFlingerClient);
72 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
73 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074 }
Steve Block29357bc2012-01-06 19:20:56 +000075 ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurentc2f1f072009-07-17 12:17:14 -070076
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 return gAudioFlinger;
78}
79
Eric Laurent46291612013-07-18 14:38:44 -070080/* static */ status_t AudioSystem::checkAudioFlinger()
81{
82 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
83 return NO_ERROR;
84 }
85 return DEAD_OBJECT;
86}
87
Glenn Kasten4944acb2013-08-19 08:39:20 -070088status_t AudioSystem::muteMicrophone(bool state)
89{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
91 if (af == 0) return PERMISSION_DENIED;
92 return af->setMicMute(state);
93}
94
Glenn Kasten4944acb2013-08-19 08:39:20 -070095status_t AudioSystem::isMicrophoneMuted(bool* state)
96{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
98 if (af == 0) return PERMISSION_DENIED;
99 *state = af->getMicMute();
100 return NO_ERROR;
101}
102
103status_t AudioSystem::setMasterVolume(float value)
104{
105 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
106 if (af == 0) return PERMISSION_DENIED;
107 af->setMasterVolume(value);
108 return NO_ERROR;
109}
110
111status_t AudioSystem::setMasterMute(bool mute)
112{
113 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
114 if (af == 0) return PERMISSION_DENIED;
115 af->setMasterMute(mute);
116 return NO_ERROR;
117}
118
119status_t AudioSystem::getMasterVolume(float* volume)
120{
121 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
122 if (af == 0) return PERMISSION_DENIED;
123 *volume = af->masterVolume();
124 return NO_ERROR;
125}
126
127status_t AudioSystem::getMasterMute(bool* mute)
128{
129 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
130 if (af == 0) return PERMISSION_DENIED;
131 *mute = af->masterMute();
132 return NO_ERROR;
133}
134
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800135status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
136 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137{
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
140 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700141 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 return NO_ERROR;
143}
144
Glenn Kastenfff6d712012-01-12 16:38:12 -0800145status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146{
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
149 if (af == 0) return PERMISSION_DENIED;
150 af->setStreamMute(stream, mute);
151 return NO_ERROR;
152}
153
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800154status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
155 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156{
Dima Zavinfce7a472011-04-19 22:30:36 -0700157 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
159 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700160 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 return NO_ERROR;
162}
163
Glenn Kastenfff6d712012-01-12 16:38:12 -0800164status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165{
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
169 *mute = af->streamMute(stream);
170 return NO_ERROR;
171}
172
Glenn Kastenf78aee72012-01-04 11:00:47 -0800173status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800175 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
177 if (af == 0) return PERMISSION_DENIED;
178 return af->setMode(mode);
179}
180
Glenn Kasten4944acb2013-08-19 08:39:20 -0700181status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
182{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
184 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700185 return af->setParameters(ioHandle, keyValuePairs);
186}
187
Glenn Kasten4944acb2013-08-19 08:39:20 -0700188String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
189{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700190 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
191 String8 result = String8("");
192 if (af == 0) return result;
193
194 result = af->getParameters(ioHandle, keys);
195 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196}
197
Glenn Kastenc23885e2013-12-19 16:35:18 -0800198status_t AudioSystem::setParameters(const String8& keyValuePairs)
199{
Glenn Kasten142f5192014-03-25 17:44:59 -0700200 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800201}
202
203String8 AudioSystem::getParameters(const String8& keys)
204{
Glenn Kasten142f5192014-03-25 17:44:59 -0700205 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800206}
207
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208// convert volume steps to natural log scale
209
210// change this value to change volume scaling
211static const float dBPerStep = 0.5f;
212// shouldn't need to touch these
213static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
214static const float dBConvertInverse = 1.0f / dBConvert;
215
216float AudioSystem::linearToLog(int volume)
217{
218 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000219 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 // return v;
221 return volume ? exp(float(100 - volume) * dBConvert) : 0;
222}
223
224int AudioSystem::logToLinear(float volume)
225{
226 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000227 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 // return v;
229 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
230}
231
Glenn Kasten3b16c762012-11-14 08:44:39 -0800232status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 if (streamType == AUDIO_STREAM_DEFAULT) {
237 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700238 }
239
Glenn Kastenfff6d712012-01-12 16:38:12 -0800240 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 if (output == 0) {
242 return PERMISSION_DENIED;
243 }
244
Eric Laurent1a9ed112012-03-20 18:36:01 -0700245 return getSamplingRate(output, streamType, samplingRate);
246}
247
248status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
249 audio_stream_type_t streamType,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800250 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700251{
252 OutputDescriptor *outputDesc;
253
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254 gLock.lock();
255 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800256 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700258 gLock.unlock();
259 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
260 if (af == 0) return PERMISSION_DENIED;
261 *samplingRate = af->sampleRate(output);
262 } else {
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700264 *samplingRate = outputDesc->samplingRate;
265 gLock.unlock();
266 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800267 if (*samplingRate == 0) {
268 ALOGE("AudioSystem::getSamplingRate failed for output %d stream type %d",
269 output, streamType);
270 return BAD_VALUE;
271 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272
Glenn Kasten3b16c762012-11-14 08:44:39 -0800273 ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %u", streamType, output,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700274 *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 return NO_ERROR;
277}
278
Glenn Kastene33054e2012-11-14 12:54:39 -0800279status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282
Dima Zavinfce7a472011-04-19 22:30:36 -0700283 if (streamType == AUDIO_STREAM_DEFAULT) {
284 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700286
Glenn Kastenfff6d712012-01-12 16:38:12 -0800287 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700288 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 return PERMISSION_DENIED;
290 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291
Eric Laurent1a9ed112012-03-20 18:36:01 -0700292 return getFrameCount(output, streamType, frameCount);
293}
294
295status_t AudioSystem::getFrameCount(audio_io_handle_t output,
296 audio_stream_type_t streamType,
Glenn Kastene33054e2012-11-14 12:54:39 -0800297 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700298{
299 OutputDescriptor *outputDesc;
300
Eric Laurentc2f1f072009-07-17 12:17:14 -0700301 gLock.lock();
302 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800303 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304 gLock.unlock();
305 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
306 if (af == 0) return PERMISSION_DENIED;
307 *frameCount = af->frameCount(output);
308 } else {
309 *frameCount = outputDesc->frameCount;
310 gLock.unlock();
311 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800312 if (*frameCount == 0) {
313 ALOGE("AudioSystem::getFrameCount failed for output %d stream type %d",
314 output, streamType);
315 return BAD_VALUE;
316 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700318 ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output,
319 *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 return NO_ERROR;
322}
323
Glenn Kastenfff6d712012-01-12 16:38:12 -0800324status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 if (streamType == AUDIO_STREAM_DEFAULT) {
329 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700330 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700331
Glenn Kastenfff6d712012-01-12 16:38:12 -0800332 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700333 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334 return PERMISSION_DENIED;
335 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336
Glenn Kasten241618f2014-03-25 17:48:57 -0700337 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700338}
339
340status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700341 uint32_t* latency)
342{
343 OutputDescriptor *outputDesc;
344
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 gLock.lock();
346 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800347 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 gLock.unlock();
349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
350 if (af == 0) return PERMISSION_DENIED;
351 *latency = af->latency(output);
352 } else {
353 *latency = outputDesc->latency;
354 gLock.unlock();
355 }
356
Glenn Kasten241618f2014-03-25 17:48:57 -0700357 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 return NO_ERROR;
360}
361
Glenn Kastendd8104c2012-07-02 12:42:44 -0700362status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
363 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800365 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800367 size_t inBuffSize = gInBuffSize;
368 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700369 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800370 gLock.unlock();
371 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
372 if (af == 0) {
373 return PERMISSION_DENIED;
374 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700375 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kasten5446e542014-01-08 08:58:53 -0800376 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800377 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800378 sampleRate, format, channelMask);
379 return BAD_VALUE;
380 }
381 // A benign race is possible here: we could overwrite a fresher cache entry
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800382 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383 // save the request params
384 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700386 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800388 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700389 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800390 gLock.unlock();
391 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 return NO_ERROR;
394}
395
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700396status_t AudioSystem::setVoiceVolume(float value)
397{
398 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
399 if (af == 0) return PERMISSION_DENIED;
400 return af->setVoiceVolume(value);
401}
402
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000403status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700404 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800405{
406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af == 0) return PERMISSION_DENIED;
408
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000409 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800410}
411
Glenn Kasten4944acb2013-08-19 08:39:20 -0700412uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
413{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800414 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800415 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800416 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700417 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800418
419 result = af->getInputFramesLost(ioHandle);
420 return result;
421}
422
Glenn Kasten4944acb2013-08-19 08:39:20 -0700423int AudioSystem::newAudioSessionId()
424{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten85d109a2014-01-17 10:25:08 -0800426 if (af == 0) return AUDIO_SESSION_ALLOCATE;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700427 return af->newAudioSessionId();
428}
429
Marco Nelissend457c972014-02-11 08:47:07 -0800430void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700431{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800434 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700435 }
436}
437
Marco Nelissend457c972014-02-11 08:47:07 -0800438void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700439{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800442 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700443 }
444}
445
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446// ---------------------------------------------------------------------------
447
Glenn Kasten4944acb2013-08-19 08:39:20 -0700448void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
449{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451
Eric Laurentc2f1f072009-07-17 12:17:14 -0700452 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800453 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800454 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455
456 if (gAudioErrorCallback) {
457 gAudioErrorCallback(DEAD_OBJECT);
458 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000459 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460}
461
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800462void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800463 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100464 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800465 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800466 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700467
Glenn Kasten142f5192014-03-25 17:44:59 -0700468 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700469
470 Mutex::Autolock _l(AudioSystem::gLock);
471
472 switch (event) {
473 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700474 break;
475 case OUTPUT_OPENED: {
476 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700478 break;
479 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800480 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800481 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700482
483 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
484 gOutputs.add(ioHandle, outputDesc);
Glenn Kastencac3daa2014-02-07 09:47:14 -0800485 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %u "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700486 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700487 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700488 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700489 } break;
490 case OUTPUT_CLOSED: {
491 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800492 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700493 break;
494 }
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700496
497 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700498 } break;
499
500 case OUTPUT_CONFIG_CHANGED: {
501 int index = gOutputs.indexOfKey(ioHandle);
502 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800503 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700504 break;
505 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800506 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800507 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700508
Glenn Kastencac3daa2014-02-07 09:47:14 -0800509 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700510 "frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700511 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700512 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700513 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
514 delete outputDesc;
515 outputDesc = new OutputDescriptor(*desc);
516 gOutputs.replaceValueFor(ioHandle, outputDesc);
517 } break;
518 case INPUT_OPENED:
519 case INPUT_CLOSED:
520 case INPUT_CONFIG_CHANGED:
521 break;
522
523 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524}
525
Glenn Kasten4944acb2013-08-19 08:39:20 -0700526void AudioSystem::setErrorCallback(audio_error_callback cb)
527{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700528 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 gAudioErrorCallback = cb;
530}
531
Eric Laurentb52c1522014-05-20 11:27:36 -0700532
Glenn Kasten4944acb2013-08-19 08:39:20 -0700533bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType)
534{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700535 switch (streamType) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700536 case AUDIO_STREAM_MUSIC:
537 case AUDIO_STREAM_VOICE_CALL:
538 case AUDIO_STREAM_BLUETOOTH_SCO:
539 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 return true;
541 default:
542 return false;
543 }
544}
545
546
Eric Laurentc2f1f072009-07-17 12:17:14 -0700547// client singleton for AudioPolicyService binder interface
548sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
549sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
550
551
Glenn Kasten18a6d902012-09-24 11:27:56 -0700552// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700553const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
554{
555 gLock.lock();
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800556 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700557 sp<IServiceManager> sm = defaultServiceManager();
558 sp<IBinder> binder;
559 do {
560 binder = sm->getService(String16("media.audio_policy"));
561 if (binder != 0)
562 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000563 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700564 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700565 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700566 if (gAudioPolicyServiceClient == NULL) {
567 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
568 }
569 binder->linkToDeath(gAudioPolicyServiceClient);
570 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
Eric Laurentb52c1522014-05-20 11:27:36 -0700571 gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572 gLock.unlock();
573 } else {
574 gLock.unlock();
575 }
576 return gAudioPolicyService;
577}
578
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700579// ---------------------------------------------------------------------------
580
Dima Zavinfce7a472011-04-19 22:30:36 -0700581status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
582 audio_policy_dev_state_t state,
583 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700584{
585 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700586 const char *address = "";
587
Eric Laurentc2f1f072009-07-17 12:17:14 -0700588 if (aps == 0) return PERMISSION_DENIED;
589
Eric Laurent71b63e32011-09-02 14:20:56 -0700590 if (device_address != NULL) {
591 address = device_address;
592 }
593
594 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700595}
596
Dima Zavinfce7a472011-04-19 22:30:36 -0700597audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598 const char *device_address)
599{
600 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700601 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700602
603 return aps->getDeviceConnectionState(device, device_address);
604}
605
Glenn Kastenf78aee72012-01-04 11:00:47 -0800606status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700607{
Glenn Kasten347966c2012-01-18 14:58:32 -0800608 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
610 if (aps == 0) return PERMISSION_DENIED;
611
612 return aps->setPhoneState(state);
613}
614
Dima Zavinfce7a472011-04-19 22:30:36 -0700615status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700616{
617 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
618 if (aps == 0) return PERMISSION_DENIED;
619 return aps->setForceUse(usage, config);
620}
621
Dima Zavinfce7a472011-04-19 22:30:36 -0700622audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700623{
624 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700625 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700626 return aps->getForceUse(usage);
627}
628
629
Dima Zavinfce7a472011-04-19 22:30:36 -0700630audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700631 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800632 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700633 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000634 audio_output_flags_t flags,
635 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700636{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700637 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
638 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000639 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700640}
641
Eric Laurentde070132010-07-13 04:45:46 -0700642status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700643 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700644 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700645{
646 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
647 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700648 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700649}
650
Eric Laurentde070132010-07-13 04:45:46 -0700651status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700652 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700653 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700654{
655 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
656 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700657 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700658}
659
660void AudioSystem::releaseOutput(audio_io_handle_t output)
661{
662 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
663 if (aps == 0) return;
664 aps->releaseOutput(output);
665}
666
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800667audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700668 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800669 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700670 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700671 int sessionId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700672{
673 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700674 if (aps == 0) return 0;
Glenn Kasten254af182012-07-03 14:59:05 -0700675 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676}
677
678status_t AudioSystem::startInput(audio_io_handle_t input)
679{
680 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
681 if (aps == 0) return PERMISSION_DENIED;
682 return aps->startInput(input);
683}
684
685status_t AudioSystem::stopInput(audio_io_handle_t input)
686{
687 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
688 if (aps == 0) return PERMISSION_DENIED;
689 return aps->stopInput(input);
690}
691
692void AudioSystem::releaseInput(audio_io_handle_t input)
693{
694 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
695 if (aps == 0) return;
696 aps->releaseInput(input);
697}
698
Dima Zavinfce7a472011-04-19 22:30:36 -0700699status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700700 int indexMin,
701 int indexMax)
702{
703 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
704 if (aps == 0) return PERMISSION_DENIED;
705 return aps->initStreamVolume(stream, indexMin, indexMax);
706}
707
Eric Laurent83844cc2011-11-18 16:43:31 -0800708status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
709 int index,
710 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700711{
712 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
713 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800714 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700715}
716
Eric Laurent83844cc2011-11-18 16:43:31 -0800717status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
718 int *index,
719 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700720{
721 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
722 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800723 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700724}
725
Dima Zavinfce7a472011-04-19 22:30:36 -0700726uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700727{
728 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
729 if (aps == 0) return 0;
730 return aps->getStrategyForStream(stream);
731}
732
Eric Laurent63742522012-03-08 13:42:42 -0800733audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800734{
735 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800736 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800737 return aps->getDevicesForStream(stream);
738}
739
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700740audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700741{
742 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800743 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700744 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700745 return aps->getOutputForEffect(desc);
746}
747
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700748status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700749 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700750 uint32_t strategy,
751 int session,
752 int id)
753{
754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
755 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700756 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700757}
758
759status_t AudioSystem::unregisterEffect(int id)
760{
761 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
762 if (aps == 0) return PERMISSION_DENIED;
763 return aps->unregisterEffect(id);
764}
765
Eric Laurentdb7c0792011-08-10 10:37:50 -0700766status_t AudioSystem::setEffectEnabled(int id, bool enabled)
767{
768 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
769 if (aps == 0) return PERMISSION_DENIED;
770 return aps->setEffectEnabled(id, enabled);
771}
772
Glenn Kastenfff6d712012-01-12 16:38:12 -0800773status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700774{
Eric Laurenteda6c362011-02-02 09:33:30 -0800775 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
776 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700777 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800778 *state = aps->isStreamActive(stream, inPastMs);
779 return NO_ERROR;
780}
781
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800782status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
783 uint32_t inPastMs)
784{
785 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
786 if (aps == 0) return PERMISSION_DENIED;
787 if (state == NULL) return BAD_VALUE;
788 *state = aps->isStreamActiveRemotely(stream, inPastMs);
789 return NO_ERROR;
790}
791
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700792status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
793{
794 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
795 if (aps == 0) return PERMISSION_DENIED;
796 if (state == NULL) return BAD_VALUE;
797 *state = aps->isSourceActive(stream);
798 return NO_ERROR;
799}
800
Glenn Kasten3b16c762012-11-14 08:44:39 -0800801uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700802{
803 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
804 if (af == 0) return 0;
805 return af->getPrimaryOutputSamplingRate();
806}
807
Glenn Kastene33054e2012-11-14 12:54:39 -0800808size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700809{
810 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
811 if (af == 0) return 0;
812 return af->getPrimaryOutputFrameCount();
813}
Eric Laurenteda6c362011-02-02 09:33:30 -0800814
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700815status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
816{
817 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
818 if (af == 0) return PERMISSION_DENIED;
819 return af->setLowRamDevice(isLowRamDevice);
820}
821
Eric Laurent9f6530f2011-08-30 10:18:54 -0700822void AudioSystem::clearAudioConfigCache()
823{
824 Mutex::Autolock _l(gLock);
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("clearAudioConfigCache()");
Eric Laurent9f6530f2011-08-30 10:18:54 -0700826 gOutputs.clear();
827}
828
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000829bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
830{
831 ALOGV("isOffloadSupported()");
832 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
833 if (aps == 0) return false;
834 return aps->isOffloadSupported(info);
835}
836
Eric Laurent203b1a12014-04-01 10:34:16 -0700837status_t AudioSystem::listAudioPorts(audio_port_role_t role,
838 audio_port_type_t type,
839 unsigned int *num_ports,
840 struct audio_port *ports,
841 unsigned int *generation)
842{
843 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
844 if (aps == 0) return PERMISSION_DENIED;
845 return aps->listAudioPorts(role, type, num_ports, ports, generation);
846}
847
848status_t AudioSystem::getAudioPort(struct audio_port *port)
849{
850 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
851 if (aps == 0) return PERMISSION_DENIED;
852 return aps->getAudioPort(port);
853}
854
855status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
856 audio_patch_handle_t *handle)
857{
858 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
859 if (aps == 0) return PERMISSION_DENIED;
860 return aps->createAudioPatch(patch, handle);
861}
862
863status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
864{
865 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
866 if (aps == 0) return PERMISSION_DENIED;
867 return aps->releaseAudioPatch(handle);
868}
869
870status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
871 struct audio_patch *patches,
872 unsigned int *generation)
873{
874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
875 if (aps == 0) return PERMISSION_DENIED;
876 return aps->listAudioPatches(num_patches, patches, generation);
877}
878
879status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
880{
881 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
882 if (aps == 0) return PERMISSION_DENIED;
883 return aps->setAudioPortConfig(config);
884}
885
Eric Laurentb52c1522014-05-20 11:27:36 -0700886void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
887{
888 Mutex::Autolock _l(gLock);
889 gAudioPortCallback = callBack;
890}
891
Eric Laurentc2f1f072009-07-17 12:17:14 -0700892// ---------------------------------------------------------------------------
893
Glenn Kasten4944acb2013-08-19 08:39:20 -0700894void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
895{
Eric Laurentb52c1522014-05-20 11:27:36 -0700896 Mutex::Autolock _l(gLock);
897 if (gAudioPortCallback != 0) {
898 gAudioPortCallback->onServiceDied();
899 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700900 AudioSystem::gAudioPolicyService.clear();
901
Steve Block5ff1dd52012-01-05 23:22:43 +0000902 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700903}
904
Eric Laurentb52c1522014-05-20 11:27:36 -0700905void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
906{
907 Mutex::Autolock _l(gLock);
908 if (gAudioPortCallback != 0) {
909 gAudioPortCallback->onAudioPortListUpdate();
910 }
911}
912
913void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
914{
915 Mutex::Autolock _l(gLock);
916 if (gAudioPortCallback != 0) {
917 gAudioPortCallback->onAudioPatchListUpdate();
918 }
919}
920
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800921}; // namespace android