blob: cf7dfef42a70d7d043b71838a7c3196c80c97694 [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;
38// Cached values
Glenn Kasten211eeaf2012-01-20 09:37:45 -080039
Eric Laurentc2f1f072009-07-17 12:17:14 -070040DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
41
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -080042// Cached values for recording queries, all protected by gLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043uint32_t AudioSystem::gPrevInSamplingRate = 16000;
Glenn Kasten58f30212012-01-12 12:27:51 -080044audio_format_t AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
Glenn Kastendd8104c2012-07-02 12:42:44 -070045audio_channel_mask_t AudioSystem::gPrevInChannelMask = AUDIO_CHANNEL_IN_MONO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046size_t AudioSystem::gInBuffSize = 0;
47
48
49// establish binder interface to AudioFlinger service
50const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
51{
52 Mutex::Autolock _l(gLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080053 if (gAudioFlinger == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 sp<IServiceManager> sm = defaultServiceManager();
55 sp<IBinder> binder;
56 do {
57 binder = sm->getService(String16("media.audio_flinger"));
58 if (binder != 0)
59 break;
Steve Block5ff1dd52012-01-05 23:22:43 +000060 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070062 } while (true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063 if (gAudioFlingerClient == NULL) {
64 gAudioFlingerClient = new AudioFlingerClient();
65 } else {
66 if (gAudioErrorCallback) {
67 gAudioErrorCallback(NO_ERROR);
68 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070069 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070 binder->linkToDeath(gAudioFlingerClient);
71 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
72 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 }
Steve Block29357bc2012-01-06 19:20:56 +000074 ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurentc2f1f072009-07-17 12:17:14 -070075
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 return gAudioFlinger;
77}
78
Eric Laurent46291612013-07-18 14:38:44 -070079/* static */ status_t AudioSystem::checkAudioFlinger()
80{
81 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
82 return NO_ERROR;
83 }
84 return DEAD_OBJECT;
85}
86
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080087status_t AudioSystem::muteMicrophone(bool state) {
88 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
89 if (af == 0) return PERMISSION_DENIED;
90 return af->setMicMute(state);
91}
92
93status_t AudioSystem::isMicrophoneMuted(bool* state) {
94 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
95 if (af == 0) return PERMISSION_DENIED;
96 *state = af->getMicMute();
97 return NO_ERROR;
98}
99
100status_t AudioSystem::setMasterVolume(float value)
101{
102 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
103 if (af == 0) return PERMISSION_DENIED;
104 af->setMasterVolume(value);
105 return NO_ERROR;
106}
107
108status_t AudioSystem::setMasterMute(bool mute)
109{
110 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
111 if (af == 0) return PERMISSION_DENIED;
112 af->setMasterMute(mute);
113 return NO_ERROR;
114}
115
116status_t AudioSystem::getMasterVolume(float* volume)
117{
118 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
119 if (af == 0) return PERMISSION_DENIED;
120 *volume = af->masterVolume();
121 return NO_ERROR;
122}
123
124status_t AudioSystem::getMasterMute(bool* mute)
125{
126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
127 if (af == 0) return PERMISSION_DENIED;
128 *mute = af->masterMute();
129 return NO_ERROR;
130}
131
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800132status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
133 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134{
Dima Zavinfce7a472011-04-19 22:30:36 -0700135 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
137 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700138 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 return NO_ERROR;
140}
141
Glenn Kastenfff6d712012-01-12 16:38:12 -0800142status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Dima Zavinfce7a472011-04-19 22:30:36 -0700144 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
146 if (af == 0) return PERMISSION_DENIED;
147 af->setStreamMute(stream, mute);
148 return NO_ERROR;
149}
150
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800151status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
152 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153{
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
156 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700157 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 return NO_ERROR;
159}
160
Glenn Kastenfff6d712012-01-12 16:38:12 -0800161status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162{
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
165 if (af == 0) return PERMISSION_DENIED;
166 *mute = af->streamMute(stream);
167 return NO_ERROR;
168}
169
Glenn Kastenf78aee72012-01-04 11:00:47 -0800170status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800172 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
174 if (af == 0) return PERMISSION_DENIED;
175 return af->setMode(mode);
176}
177
Eric Laurentc2f1f072009-07-17 12:17:14 -0700178status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
180 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700181 return af->setParameters(ioHandle, keyValuePairs);
182}
183
184String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
185 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
186 String8 result = String8("");
187 if (af == 0) return result;
188
189 result = af->getParameters(ioHandle, keys);
190 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191}
192
Glenn Kastenc23885e2013-12-19 16:35:18 -0800193status_t AudioSystem::setParameters(const String8& keyValuePairs)
194{
195 return setParameters((audio_io_handle_t) 0, keyValuePairs);
196}
197
198String8 AudioSystem::getParameters(const String8& keys)
199{
200 return getParameters((audio_io_handle_t) 0, keys);
201}
202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203// convert volume steps to natural log scale
204
205// change this value to change volume scaling
206static const float dBPerStep = 0.5f;
207// shouldn't need to touch these
208static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
209static const float dBConvertInverse = 1.0f / dBConvert;
210
211float AudioSystem::linearToLog(int volume)
212{
213 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000214 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 // return v;
216 return volume ? exp(float(100 - volume) * dBConvert) : 0;
217}
218
219int AudioSystem::logToLinear(float volume)
220{
221 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000222 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 // return v;
224 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
225}
226
Glenn Kasten3b16c762012-11-14 08:44:39 -0800227status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700229 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230
Dima Zavinfce7a472011-04-19 22:30:36 -0700231 if (streamType == AUDIO_STREAM_DEFAULT) {
232 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233 }
234
Glenn Kastenfff6d712012-01-12 16:38:12 -0800235 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 if (output == 0) {
237 return PERMISSION_DENIED;
238 }
239
Eric Laurent1a9ed112012-03-20 18:36:01 -0700240 return getSamplingRate(output, streamType, samplingRate);
241}
242
243status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
244 audio_stream_type_t streamType,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800245 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700246{
247 OutputDescriptor *outputDesc;
248
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 gLock.lock();
250 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800251 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700253 gLock.unlock();
254 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
255 if (af == 0) return PERMISSION_DENIED;
256 *samplingRate = af->sampleRate(output);
257 } else {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259 *samplingRate = outputDesc->samplingRate;
260 gLock.unlock();
261 }
262
Glenn Kasten3b16c762012-11-14 08:44:39 -0800263 ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %u", streamType, output,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700264 *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 return NO_ERROR;
267}
268
Glenn Kastene33054e2012-11-14 12:54:39 -0800269status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700271 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272
Dima Zavinfce7a472011-04-19 22:30:36 -0700273 if (streamType == AUDIO_STREAM_DEFAULT) {
274 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700276
Glenn Kastenfff6d712012-01-12 16:38:12 -0800277 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278 if (output == 0) {
279 return PERMISSION_DENIED;
280 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281
Eric Laurent1a9ed112012-03-20 18:36:01 -0700282 return getFrameCount(output, streamType, frameCount);
283}
284
285status_t AudioSystem::getFrameCount(audio_io_handle_t output,
286 audio_stream_type_t streamType,
Glenn Kastene33054e2012-11-14 12:54:39 -0800287 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700288{
289 OutputDescriptor *outputDesc;
290
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 gLock.lock();
292 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800293 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 gLock.unlock();
295 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
296 if (af == 0) return PERMISSION_DENIED;
297 *frameCount = af->frameCount(output);
298 } else {
299 *frameCount = outputDesc->frameCount;
300 gLock.unlock();
301 }
302
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700303 ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output,
304 *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700305
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 return NO_ERROR;
307}
308
Glenn Kastenfff6d712012-01-12 16:38:12 -0800309status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312
Dima Zavinfce7a472011-04-19 22:30:36 -0700313 if (streamType == AUDIO_STREAM_DEFAULT) {
314 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700316
Glenn Kastenfff6d712012-01-12 16:38:12 -0800317 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318 if (output == 0) {
319 return PERMISSION_DENIED;
320 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321
Eric Laurent1a9ed112012-03-20 18:36:01 -0700322 return getLatency(output, streamType, latency);
323}
324
325status_t AudioSystem::getLatency(audio_io_handle_t output,
326 audio_stream_type_t streamType,
327 uint32_t* latency)
328{
329 OutputDescriptor *outputDesc;
330
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 gLock.lock();
332 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800333 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334 gLock.unlock();
335 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
336 if (af == 0) return PERMISSION_DENIED;
337 *latency = af->latency(output);
338 } else {
339 *latency = outputDesc->latency;
340 gLock.unlock();
341 }
342
Eric Laurent1a9ed112012-03-20 18:36:01 -0700343 ALOGV("getLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 return NO_ERROR;
346}
347
Glenn Kastendd8104c2012-07-02 12:42:44 -0700348status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
349 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800351 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800353 size_t inBuffSize = gInBuffSize;
354 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700355 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800356 gLock.unlock();
357 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
358 if (af == 0) {
359 return PERMISSION_DENIED;
360 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700361 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800362 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 // save the request params
364 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700366 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800368 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800370 gLock.unlock();
371 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 return NO_ERROR;
374}
375
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700376status_t AudioSystem::setVoiceVolume(float value)
377{
378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) return PERMISSION_DENIED;
380 return af->setVoiceVolume(value);
381}
382
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000383status_t AudioSystem::getRenderPosition(audio_io_handle_t output, size_t *halFrames,
384 size_t *dspFrames, audio_stream_type_t stream)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800385{
386 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
387 if (af == 0) return PERMISSION_DENIED;
388
Dima Zavinfce7a472011-04-19 22:30:36 -0700389 if (stream == AUDIO_STREAM_DEFAULT) {
390 stream = AUDIO_STREAM_MUSIC;
Eric Laurent342e9cf2010-01-19 17:37:09 -0800391 }
392
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000393 if (output == 0) {
394 output = getOutput(stream);
395 }
396
397 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800398}
399
Glenn Kastene33054e2012-11-14 12:54:39 -0800400size_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
Eric Laurent05bca2f2010-02-26 02:47:27 -0800401 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
402 unsigned int result = 0;
403 if (af == 0) return result;
Eric Laurentbe55a2d2010-03-11 14:47:00 -0800404 if (ioHandle == 0) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800405
406 result = af->getInputFramesLost(ioHandle);
407 return result;
408}
409
Eric Laurentbe916aa2010-06-01 23:49:17 -0700410int AudioSystem::newAudioSessionId() {
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return 0;
413 return af->newAudioSessionId();
414}
415
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700416void AudioSystem::acquireAudioSessionId(int audioSession) {
417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
418 if (af != 0) {
419 af->acquireAudioSessionId(audioSession);
420 }
421}
422
423void AudioSystem::releaseAudioSessionId(int audioSession) {
424 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
425 if (af != 0) {
426 af->releaseAudioSessionId(audioSession);
427 }
428}
429
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430// ---------------------------------------------------------------------------
431
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800432void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434
Eric Laurentc2f1f072009-07-17 12:17:14 -0700435 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800436 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800437 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438
439 if (gAudioErrorCallback) {
440 gAudioErrorCallback(DEAD_OBJECT);
441 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000442 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443}
444
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800445void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800446 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800448 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800449 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700450
Eric Laurentfa2877b2009-07-28 08:44:33 -0700451 if (ioHandle == 0) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700452
453 Mutex::Autolock _l(AudioSystem::gLock);
454
455 switch (event) {
456 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700457 break;
458 case OUTPUT_OPENED: {
459 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100460 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700461 break;
462 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800463 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800464 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700465
466 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
467 gOutputs.add(ioHandle, outputDesc);
Glenn Kastenfad226a2013-07-16 17:19:58 -0700468 ALOGV("ioConfigChanged() new output samplingRate %u, format %d channel mask %#x frameCount %u "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700469 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700470 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700471 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472 } break;
473 case OUTPUT_CLOSED: {
474 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800475 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700476 break;
477 }
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479
480 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481 } break;
482
483 case OUTPUT_CONFIG_CHANGED: {
484 int index = gOutputs.indexOfKey(ioHandle);
485 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800486 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700487 break;
488 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800489 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800490 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491
Glenn Kastenfad226a2013-07-16 17:19:58 -0700492 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %d channel mask %#x "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700493 "frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700494 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700495 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700496 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
497 delete outputDesc;
498 outputDesc = new OutputDescriptor(*desc);
499 gOutputs.replaceValueFor(ioHandle, outputDesc);
500 } break;
501 case INPUT_OPENED:
502 case INPUT_CLOSED:
503 case INPUT_CONFIG_CHANGED:
504 break;
505
506 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507}
508
509void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700510 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 gAudioErrorCallback = cb;
512}
513
Glenn Kastenfff6d712012-01-12 16:38:12 -0800514bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700515 switch (streamType) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700516 case AUDIO_STREAM_MUSIC:
517 case AUDIO_STREAM_VOICE_CALL:
518 case AUDIO_STREAM_BLUETOOTH_SCO:
519 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 return true;
521 default:
522 return false;
523 }
524}
525
526
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527// client singleton for AudioPolicyService binder interface
528sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
529sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
530
531
Glenn Kasten18a6d902012-09-24 11:27:56 -0700532// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700533const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
534{
535 gLock.lock();
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800536 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700537 sp<IServiceManager> sm = defaultServiceManager();
538 sp<IBinder> binder;
539 do {
540 binder = sm->getService(String16("media.audio_policy"));
541 if (binder != 0)
542 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000543 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700544 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700545 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700546 if (gAudioPolicyServiceClient == NULL) {
547 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
548 }
549 binder->linkToDeath(gAudioPolicyServiceClient);
550 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
551 gLock.unlock();
552 } else {
553 gLock.unlock();
554 }
555 return gAudioPolicyService;
556}
557
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700558// ---------------------------------------------------------------------------
559
Dima Zavinfce7a472011-04-19 22:30:36 -0700560status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
561 audio_policy_dev_state_t state,
562 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700563{
564 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700565 const char *address = "";
566
Eric Laurentc2f1f072009-07-17 12:17:14 -0700567 if (aps == 0) return PERMISSION_DENIED;
568
Eric Laurent71b63e32011-09-02 14:20:56 -0700569 if (device_address != NULL) {
570 address = device_address;
571 }
572
573 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700574}
575
Dima Zavinfce7a472011-04-19 22:30:36 -0700576audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700577 const char *device_address)
578{
579 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700580 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700581
582 return aps->getDeviceConnectionState(device, device_address);
583}
584
Glenn Kastenf78aee72012-01-04 11:00:47 -0800585status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700586{
Glenn Kasten347966c2012-01-18 14:58:32 -0800587 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700588 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
589 if (aps == 0) return PERMISSION_DENIED;
590
591 return aps->setPhoneState(state);
592}
593
Dima Zavinfce7a472011-04-19 22:30:36 -0700594status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700595{
596 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
597 if (aps == 0) return PERMISSION_DENIED;
598 return aps->setForceUse(usage, config);
599}
600
Dima Zavinfce7a472011-04-19 22:30:36 -0700601audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700602{
603 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700604 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700605 return aps->getForceUse(usage);
606}
607
608
Dima Zavinfce7a472011-04-19 22:30:36 -0700609audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700610 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800611 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700612 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000613 audio_output_flags_t flags,
614 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700615{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700616 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
617 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000618 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619}
620
Eric Laurentde070132010-07-13 04:45:46 -0700621status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700622 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700623 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700624{
625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
626 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700627 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628}
629
Eric Laurentde070132010-07-13 04:45:46 -0700630status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700631 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700632 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700633{
634 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
635 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700636 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700637}
638
639void AudioSystem::releaseOutput(audio_io_handle_t output)
640{
641 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
642 if (aps == 0) return;
643 aps->releaseOutput(output);
644}
645
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800646audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700647 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800648 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700649 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700650 int sessionId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700651{
652 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700653 if (aps == 0) return 0;
Glenn Kasten254af182012-07-03 14:59:05 -0700654 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700655}
656
657status_t AudioSystem::startInput(audio_io_handle_t input)
658{
659 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
660 if (aps == 0) return PERMISSION_DENIED;
661 return aps->startInput(input);
662}
663
664status_t AudioSystem::stopInput(audio_io_handle_t input)
665{
666 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
667 if (aps == 0) return PERMISSION_DENIED;
668 return aps->stopInput(input);
669}
670
671void AudioSystem::releaseInput(audio_io_handle_t input)
672{
673 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
674 if (aps == 0) return;
675 aps->releaseInput(input);
676}
677
Dima Zavinfce7a472011-04-19 22:30:36 -0700678status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700679 int indexMin,
680 int indexMax)
681{
682 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
683 if (aps == 0) return PERMISSION_DENIED;
684 return aps->initStreamVolume(stream, indexMin, indexMax);
685}
686
Eric Laurent83844cc2011-11-18 16:43:31 -0800687status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
688 int index,
689 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700690{
691 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
692 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800693 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694}
695
Eric Laurent83844cc2011-11-18 16:43:31 -0800696status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
697 int *index,
698 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700699{
700 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
701 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800702 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700703}
704
Dima Zavinfce7a472011-04-19 22:30:36 -0700705uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700706{
707 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
708 if (aps == 0) return 0;
709 return aps->getStrategyForStream(stream);
710}
711
Eric Laurent63742522012-03-08 13:42:42 -0800712audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800713{
714 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent63742522012-03-08 13:42:42 -0800715 if (aps == 0) return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800716 return aps->getDevicesForStream(stream);
717}
718
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700719audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700720{
721 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800722 // FIXME change return type to status_t, and return PERMISSION_DENIED here
723 if (aps == 0) return 0;
Eric Laurentde070132010-07-13 04:45:46 -0700724 return aps->getOutputForEffect(desc);
725}
726
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700727status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700728 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700729 uint32_t strategy,
730 int session,
731 int id)
732{
733 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
734 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700735 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700736}
737
738status_t AudioSystem::unregisterEffect(int id)
739{
740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
741 if (aps == 0) return PERMISSION_DENIED;
742 return aps->unregisterEffect(id);
743}
744
Eric Laurentdb7c0792011-08-10 10:37:50 -0700745status_t AudioSystem::setEffectEnabled(int id, bool enabled)
746{
747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
748 if (aps == 0) return PERMISSION_DENIED;
749 return aps->setEffectEnabled(id, enabled);
750}
751
Glenn Kastenfff6d712012-01-12 16:38:12 -0800752status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700753{
Eric Laurenteda6c362011-02-02 09:33:30 -0800754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
755 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700756 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800757 *state = aps->isStreamActive(stream, inPastMs);
758 return NO_ERROR;
759}
760
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800761status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
762 uint32_t inPastMs)
763{
764 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
765 if (aps == 0) return PERMISSION_DENIED;
766 if (state == NULL) return BAD_VALUE;
767 *state = aps->isStreamActiveRemotely(stream, inPastMs);
768 return NO_ERROR;
769}
770
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700771status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
772{
773 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
774 if (aps == 0) return PERMISSION_DENIED;
775 if (state == NULL) return BAD_VALUE;
776 *state = aps->isSourceActive(stream);
777 return NO_ERROR;
778}
779
Glenn Kasten3b16c762012-11-14 08:44:39 -0800780uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700781{
782 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
783 if (af == 0) return 0;
784 return af->getPrimaryOutputSamplingRate();
785}
786
Glenn Kastene33054e2012-11-14 12:54:39 -0800787size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700788{
789 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
790 if (af == 0) return 0;
791 return af->getPrimaryOutputFrameCount();
792}
Eric Laurenteda6c362011-02-02 09:33:30 -0800793
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700794status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
795{
796 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
797 if (af == 0) return PERMISSION_DENIED;
798 return af->setLowRamDevice(isLowRamDevice);
799}
800
Eric Laurent9f6530f2011-08-30 10:18:54 -0700801void AudioSystem::clearAudioConfigCache()
802{
803 Mutex::Autolock _l(gLock);
Steve Block3856b092011-10-20 11:56:00 +0100804 ALOGV("clearAudioConfigCache()");
Eric Laurent9f6530f2011-08-30 10:18:54 -0700805 gOutputs.clear();
806}
807
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000808bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
809{
810 ALOGV("isOffloadSupported()");
811 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
812 if (aps == 0) return false;
813 return aps->isOffloadSupported(info);
814}
815
Eric Laurentc2f1f072009-07-17 12:17:14 -0700816// ---------------------------------------------------------------------------
817
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800818void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700819 Mutex::Autolock _l(AudioSystem::gLock);
820 AudioSystem::gAudioPolicyService.clear();
821
Steve Block5ff1dd52012-01-05 23:22:43 +0000822 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700823}
824
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800825}; // namespace android