blob: 845ee2055480ab1f57bef516276494ac1bd38c52 [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
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
Glenn Kasten4944acb2013-08-19 08:39:20 -070087status_t AudioSystem::muteMicrophone(bool state)
88{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
90 if (af == 0) return PERMISSION_DENIED;
91 return af->setMicMute(state);
92}
93
Glenn Kasten4944acb2013-08-19 08:39:20 -070094status_t AudioSystem::isMicrophoneMuted(bool* state)
95{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
97 if (af == 0) return PERMISSION_DENIED;
98 *state = af->getMicMute();
99 return NO_ERROR;
100}
101
102status_t AudioSystem::setMasterVolume(float value)
103{
104 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
105 if (af == 0) return PERMISSION_DENIED;
106 af->setMasterVolume(value);
107 return NO_ERROR;
108}
109
110status_t AudioSystem::setMasterMute(bool mute)
111{
112 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
113 if (af == 0) return PERMISSION_DENIED;
114 af->setMasterMute(mute);
115 return NO_ERROR;
116}
117
118status_t AudioSystem::getMasterVolume(float* volume)
119{
120 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
121 if (af == 0) return PERMISSION_DENIED;
122 *volume = af->masterVolume();
123 return NO_ERROR;
124}
125
126status_t AudioSystem::getMasterMute(bool* mute)
127{
128 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
129 if (af == 0) return PERMISSION_DENIED;
130 *mute = af->masterMute();
131 return NO_ERROR;
132}
133
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800134status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
135 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136{
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
139 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700140 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 return NO_ERROR;
142}
143
Glenn Kastenfff6d712012-01-12 16:38:12 -0800144status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145{
Dima Zavinfce7a472011-04-19 22:30:36 -0700146 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
148 if (af == 0) return PERMISSION_DENIED;
149 af->setStreamMute(stream, mute);
150 return NO_ERROR;
151}
152
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800153status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
154 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155{
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700159 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 return NO_ERROR;
161}
162
Glenn Kastenfff6d712012-01-12 16:38:12 -0800163status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164{
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
167 if (af == 0) return PERMISSION_DENIED;
168 *mute = af->streamMute(stream);
169 return NO_ERROR;
170}
171
Glenn Kastenf78aee72012-01-04 11:00:47 -0800172status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800174 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
177 return af->setMode(mode);
178}
179
Glenn Kasten4944acb2013-08-19 08:39:20 -0700180status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
181{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700184 return af->setParameters(ioHandle, keyValuePairs);
185}
186
Glenn Kasten4944acb2013-08-19 08:39:20 -0700187String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
188{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700189 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
190 String8 result = String8("");
191 if (af == 0) return result;
192
193 result = af->getParameters(ioHandle, keys);
194 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195}
196
Glenn Kastenc23885e2013-12-19 16:35:18 -0800197status_t AudioSystem::setParameters(const String8& keyValuePairs)
198{
Glenn Kasten142f5192014-03-25 17:44:59 -0700199 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800200}
201
202String8 AudioSystem::getParameters(const String8& keys)
203{
Glenn Kasten142f5192014-03-25 17:44:59 -0700204 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800205}
206
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207// convert volume steps to natural log scale
208
209// change this value to change volume scaling
210static const float dBPerStep = 0.5f;
211// shouldn't need to touch these
212static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
213static const float dBConvertInverse = 1.0f / dBConvert;
214
215float AudioSystem::linearToLog(int volume)
216{
217 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000218 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 // return v;
220 return volume ? exp(float(100 - volume) * dBConvert) : 0;
221}
222
223int AudioSystem::logToLinear(float volume)
224{
225 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000226 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 // return v;
228 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
229}
230
Glenn Kasten3b16c762012-11-14 08:44:39 -0800231status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234
Dima Zavinfce7a472011-04-19 22:30:36 -0700235 if (streamType == AUDIO_STREAM_DEFAULT) {
236 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237 }
238
Glenn Kastenfff6d712012-01-12 16:38:12 -0800239 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700240 if (output == 0) {
241 return PERMISSION_DENIED;
242 }
243
Eric Laurent1a9ed112012-03-20 18:36:01 -0700244 return getSamplingRate(output, streamType, samplingRate);
245}
246
247status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
248 audio_stream_type_t streamType,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800249 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700250{
251 OutputDescriptor *outputDesc;
252
Eric Laurentc2f1f072009-07-17 12:17:14 -0700253 gLock.lock();
254 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800255 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 gLock.unlock();
258 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
259 if (af == 0) return PERMISSION_DENIED;
260 *samplingRate = af->sampleRate(output);
261 } else {
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700263 *samplingRate = outputDesc->samplingRate;
264 gLock.unlock();
265 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800266 if (*samplingRate == 0) {
267 ALOGE("AudioSystem::getSamplingRate failed for output %d stream type %d",
268 output, streamType);
269 return BAD_VALUE;
270 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700271
Glenn Kasten3b16c762012-11-14 08:44:39 -0800272 ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %u", streamType, output,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700273 *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 return NO_ERROR;
276}
277
Glenn Kastene33054e2012-11-14 12:54:39 -0800278status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 if (streamType == AUDIO_STREAM_DEFAULT) {
283 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700285
Glenn Kastenfff6d712012-01-12 16:38:12 -0800286 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700287 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 return PERMISSION_DENIED;
289 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290
Eric Laurent1a9ed112012-03-20 18:36:01 -0700291 return getFrameCount(output, streamType, frameCount);
292}
293
294status_t AudioSystem::getFrameCount(audio_io_handle_t output,
295 audio_stream_type_t streamType,
Glenn Kastene33054e2012-11-14 12:54:39 -0800296 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700297{
298 OutputDescriptor *outputDesc;
299
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300 gLock.lock();
301 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800302 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303 gLock.unlock();
304 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
305 if (af == 0) return PERMISSION_DENIED;
306 *frameCount = af->frameCount(output);
307 } else {
308 *frameCount = outputDesc->frameCount;
309 gLock.unlock();
310 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800311 if (*frameCount == 0) {
312 ALOGE("AudioSystem::getFrameCount failed for output %d stream type %d",
313 output, streamType);
314 return BAD_VALUE;
315 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700317 ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output,
318 *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700319
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 return NO_ERROR;
321}
322
Glenn Kastenfff6d712012-01-12 16:38:12 -0800323status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326
Dima Zavinfce7a472011-04-19 22:30:36 -0700327 if (streamType == AUDIO_STREAM_DEFAULT) {
328 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700330
Glenn Kastenfff6d712012-01-12 16:38:12 -0800331 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700332 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 return PERMISSION_DENIED;
334 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335
Glenn Kasten241618f2014-03-25 17:48:57 -0700336 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700337}
338
339status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700340 uint32_t* latency)
341{
342 OutputDescriptor *outputDesc;
343
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 gLock.lock();
345 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800346 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700347 gLock.unlock();
348 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
349 if (af == 0) return PERMISSION_DENIED;
350 *latency = af->latency(output);
351 } else {
352 *latency = outputDesc->latency;
353 gLock.unlock();
354 }
355
Glenn Kasten241618f2014-03-25 17:48:57 -0700356 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 return NO_ERROR;
359}
360
Glenn Kastendd8104c2012-07-02 12:42:44 -0700361status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
362 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800364 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800366 size_t inBuffSize = gInBuffSize;
367 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700368 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800369 gLock.unlock();
370 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
371 if (af == 0) {
372 return PERMISSION_DENIED;
373 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700374 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kasten5446e542014-01-08 08:58:53 -0800375 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800376 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800377 sampleRate, format, channelMask);
378 return BAD_VALUE;
379 }
380 // A benign race is possible here: we could overwrite a fresher cache entry
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800381 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 // save the request params
383 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700384 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700385 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800387 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700388 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800389 gLock.unlock();
390 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700391
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 return NO_ERROR;
393}
394
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700395status_t AudioSystem::setVoiceVolume(float value)
396{
397 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
398 if (af == 0) return PERMISSION_DENIED;
399 return af->setVoiceVolume(value);
400}
401
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000402status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700403 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800404{
405 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
406 if (af == 0) return PERMISSION_DENIED;
407
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000408 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800409}
410
Glenn Kasten4944acb2013-08-19 08:39:20 -0700411uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
412{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800413 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800414 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800415 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700416 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800417
418 result = af->getInputFramesLost(ioHandle);
419 return result;
420}
421
Glenn Kasten4944acb2013-08-19 08:39:20 -0700422int AudioSystem::newAudioSessionId()
423{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700424 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten85d109a2014-01-17 10:25:08 -0800425 if (af == 0) return AUDIO_SESSION_ALLOCATE;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700426 return af->newAudioSessionId();
427}
428
Marco Nelissend457c972014-02-11 08:47:07 -0800429void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700430{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700431 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
432 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800433 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700434 }
435}
436
Marco Nelissend457c972014-02-11 08:47:07 -0800437void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700438{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700439 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
440 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800441 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700442 }
443}
444
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445// ---------------------------------------------------------------------------
446
Glenn Kasten4944acb2013-08-19 08:39:20 -0700447void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
448{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450
Eric Laurentc2f1f072009-07-17 12:17:14 -0700451 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800452 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800453 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454
455 if (gAudioErrorCallback) {
456 gAudioErrorCallback(DEAD_OBJECT);
457 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000458 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459}
460
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800461void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800462 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800464 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800465 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466
Glenn Kasten142f5192014-03-25 17:44:59 -0700467 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468
469 Mutex::Autolock _l(AudioSystem::gLock);
470
471 switch (event) {
472 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700473 break;
474 case OUTPUT_OPENED: {
475 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700477 break;
478 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800479 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800480 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481
482 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
483 gOutputs.add(ioHandle, outputDesc);
Glenn Kastencac3daa2014-02-07 09:47:14 -0800484 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %u "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700485 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700486 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700487 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488 } break;
489 case OUTPUT_CLOSED: {
490 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800491 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492 break;
493 }
Steve Block3856b092011-10-20 11:56:00 +0100494 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700495
496 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700497 } break;
498
499 case OUTPUT_CONFIG_CHANGED: {
500 int index = gOutputs.indexOfKey(ioHandle);
501 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800502 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700503 break;
504 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800505 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800506 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700507
Glenn Kastencac3daa2014-02-07 09:47:14 -0800508 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700509 "frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700510 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700511 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700512 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
513 delete outputDesc;
514 outputDesc = new OutputDescriptor(*desc);
515 gOutputs.replaceValueFor(ioHandle, outputDesc);
516 } break;
517 case INPUT_OPENED:
518 case INPUT_CLOSED:
519 case INPUT_CONFIG_CHANGED:
520 break;
521
522 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523}
524
Glenn Kasten4944acb2013-08-19 08:39:20 -0700525void AudioSystem::setErrorCallback(audio_error_callback cb)
526{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 gAudioErrorCallback = cb;
529}
530
Glenn Kasten4944acb2013-08-19 08:39:20 -0700531bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType)
532{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700533 switch (streamType) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700534 case AUDIO_STREAM_MUSIC:
535 case AUDIO_STREAM_VOICE_CALL:
536 case AUDIO_STREAM_BLUETOOTH_SCO:
537 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 return true;
539 default:
540 return false;
541 }
542}
543
544
Eric Laurentc2f1f072009-07-17 12:17:14 -0700545// client singleton for AudioPolicyService binder interface
546sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
547sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
548
549
Glenn Kasten18a6d902012-09-24 11:27:56 -0700550// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700551const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
552{
553 gLock.lock();
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800554 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700555 sp<IServiceManager> sm = defaultServiceManager();
556 sp<IBinder> binder;
557 do {
558 binder = sm->getService(String16("media.audio_policy"));
559 if (binder != 0)
560 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000561 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700562 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700563 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700564 if (gAudioPolicyServiceClient == NULL) {
565 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
566 }
567 binder->linkToDeath(gAudioPolicyServiceClient);
568 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
569 gLock.unlock();
570 } else {
571 gLock.unlock();
572 }
573 return gAudioPolicyService;
574}
575
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700576// ---------------------------------------------------------------------------
577
Dima Zavinfce7a472011-04-19 22:30:36 -0700578status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
579 audio_policy_dev_state_t state,
580 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700581{
582 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700583 const char *address = "";
584
Eric Laurentc2f1f072009-07-17 12:17:14 -0700585 if (aps == 0) return PERMISSION_DENIED;
586
Eric Laurent71b63e32011-09-02 14:20:56 -0700587 if (device_address != NULL) {
588 address = device_address;
589 }
590
591 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700592}
593
Dima Zavinfce7a472011-04-19 22:30:36 -0700594audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700595 const char *device_address)
596{
597 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700598 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700599
600 return aps->getDeviceConnectionState(device, device_address);
601}
602
Glenn Kastenf78aee72012-01-04 11:00:47 -0800603status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700604{
Glenn Kasten347966c2012-01-18 14:58:32 -0800605 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700606 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
607 if (aps == 0) return PERMISSION_DENIED;
608
609 return aps->setPhoneState(state);
610}
611
Dima Zavinfce7a472011-04-19 22:30:36 -0700612status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700613{
614 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
615 if (aps == 0) return PERMISSION_DENIED;
616 return aps->setForceUse(usage, config);
617}
618
Dima Zavinfce7a472011-04-19 22:30:36 -0700619audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700620{
621 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700622 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700623 return aps->getForceUse(usage);
624}
625
626
Dima Zavinfce7a472011-04-19 22:30:36 -0700627audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800629 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700630 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000631 audio_output_flags_t flags,
632 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700633{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700634 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
635 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000636 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700637}
638
Eric Laurentde070132010-07-13 04:45:46 -0700639status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700640 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700641 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700642{
643 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
644 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700645 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646}
647
Eric Laurentde070132010-07-13 04:45:46 -0700648status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700649 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700650 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700651{
652 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
653 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700654 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700655}
656
657void AudioSystem::releaseOutput(audio_io_handle_t output)
658{
659 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
660 if (aps == 0) return;
661 aps->releaseOutput(output);
662}
663
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800664audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700665 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800666 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700667 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700668 int sessionId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700669{
670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700671 if (aps == 0) return 0;
Glenn Kasten254af182012-07-03 14:59:05 -0700672 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700673}
674
675status_t AudioSystem::startInput(audio_io_handle_t input)
676{
677 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
678 if (aps == 0) return PERMISSION_DENIED;
679 return aps->startInput(input);
680}
681
682status_t AudioSystem::stopInput(audio_io_handle_t input)
683{
684 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
685 if (aps == 0) return PERMISSION_DENIED;
686 return aps->stopInput(input);
687}
688
689void AudioSystem::releaseInput(audio_io_handle_t input)
690{
691 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
692 if (aps == 0) return;
693 aps->releaseInput(input);
694}
695
Dima Zavinfce7a472011-04-19 22:30:36 -0700696status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700697 int indexMin,
698 int indexMax)
699{
700 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
701 if (aps == 0) return PERMISSION_DENIED;
702 return aps->initStreamVolume(stream, indexMin, indexMax);
703}
704
Eric Laurent83844cc2011-11-18 16:43:31 -0800705status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
706 int index,
707 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708{
709 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
710 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800711 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700712}
713
Eric Laurent83844cc2011-11-18 16:43:31 -0800714status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
715 int *index,
716 audio_devices_t device)
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 Laurent83844cc2011-11-18 16:43:31 -0800720 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700721}
722
Dima Zavinfce7a472011-04-19 22:30:36 -0700723uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700724{
725 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
726 if (aps == 0) return 0;
727 return aps->getStrategyForStream(stream);
728}
729
Eric Laurent63742522012-03-08 13:42:42 -0800730audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800731{
732 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800733 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800734 return aps->getDevicesForStream(stream);
735}
736
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700737audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700738{
739 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800740 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700741 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700742 return aps->getOutputForEffect(desc);
743}
744
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700745status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700746 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700747 uint32_t strategy,
748 int session,
749 int id)
750{
751 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
752 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700753 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700754}
755
756status_t AudioSystem::unregisterEffect(int id)
757{
758 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
759 if (aps == 0) return PERMISSION_DENIED;
760 return aps->unregisterEffect(id);
761}
762
Eric Laurentdb7c0792011-08-10 10:37:50 -0700763status_t AudioSystem::setEffectEnabled(int id, bool enabled)
764{
765 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
766 if (aps == 0) return PERMISSION_DENIED;
767 return aps->setEffectEnabled(id, enabled);
768}
769
Glenn Kastenfff6d712012-01-12 16:38:12 -0800770status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700771{
Eric Laurenteda6c362011-02-02 09:33:30 -0800772 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
773 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700774 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800775 *state = aps->isStreamActive(stream, inPastMs);
776 return NO_ERROR;
777}
778
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800779status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
780 uint32_t inPastMs)
781{
782 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
783 if (aps == 0) return PERMISSION_DENIED;
784 if (state == NULL) return BAD_VALUE;
785 *state = aps->isStreamActiveRemotely(stream, inPastMs);
786 return NO_ERROR;
787}
788
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700789status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
790{
791 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
792 if (aps == 0) return PERMISSION_DENIED;
793 if (state == NULL) return BAD_VALUE;
794 *state = aps->isSourceActive(stream);
795 return NO_ERROR;
796}
797
Glenn Kasten3b16c762012-11-14 08:44:39 -0800798uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700799{
800 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
801 if (af == 0) return 0;
802 return af->getPrimaryOutputSamplingRate();
803}
804
Glenn Kastene33054e2012-11-14 12:54:39 -0800805size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700806{
807 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
808 if (af == 0) return 0;
809 return af->getPrimaryOutputFrameCount();
810}
Eric Laurenteda6c362011-02-02 09:33:30 -0800811
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700812status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
813{
814 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
815 if (af == 0) return PERMISSION_DENIED;
816 return af->setLowRamDevice(isLowRamDevice);
817}
818
Eric Laurent9f6530f2011-08-30 10:18:54 -0700819void AudioSystem::clearAudioConfigCache()
820{
821 Mutex::Autolock _l(gLock);
Steve Block3856b092011-10-20 11:56:00 +0100822 ALOGV("clearAudioConfigCache()");
Eric Laurent9f6530f2011-08-30 10:18:54 -0700823 gOutputs.clear();
824}
825
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000826bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
827{
828 ALOGV("isOffloadSupported()");
829 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
830 if (aps == 0) return false;
831 return aps->isOffloadSupported(info);
832}
833
Eric Laurent203b1a12014-04-01 10:34:16 -0700834status_t AudioSystem::listAudioPorts(audio_port_role_t role,
835 audio_port_type_t type,
836 unsigned int *num_ports,
837 struct audio_port *ports,
838 unsigned int *generation)
839{
840 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
841 if (aps == 0) return PERMISSION_DENIED;
842 return aps->listAudioPorts(role, type, num_ports, ports, generation);
843}
844
845status_t AudioSystem::getAudioPort(struct audio_port *port)
846{
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 if (aps == 0) return PERMISSION_DENIED;
849 return aps->getAudioPort(port);
850}
851
852status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
853 audio_patch_handle_t *handle)
854{
855 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
856 if (aps == 0) return PERMISSION_DENIED;
857 return aps->createAudioPatch(patch, handle);
858}
859
860status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
861{
862 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
863 if (aps == 0) return PERMISSION_DENIED;
864 return aps->releaseAudioPatch(handle);
865}
866
867status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
868 struct audio_patch *patches,
869 unsigned int *generation)
870{
871 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
872 if (aps == 0) return PERMISSION_DENIED;
873 return aps->listAudioPatches(num_patches, patches, generation);
874}
875
876status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
877{
878 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
879 if (aps == 0) return PERMISSION_DENIED;
880 return aps->setAudioPortConfig(config);
881}
882
Eric Laurentc2f1f072009-07-17 12:17:14 -0700883// ---------------------------------------------------------------------------
884
Glenn Kasten4944acb2013-08-19 08:39:20 -0700885void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
886{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887 Mutex::Autolock _l(AudioSystem::gLock);
888 AudioSystem::gAudioPolicyService.clear();
889
Steve Block5ff1dd52012-01-05 23:22:43 +0000890 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700891}
892
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800893}; // namespace android