blob: dda36575f755a9d0f67eb796d50e78cf17d452ab [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
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700245 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700246}
247
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700248status_t AudioSystem::getOutputSamplingRateForAttr(uint32_t* samplingRate,
249 const audio_attributes_t *attr)
250{
251 if (attr == NULL) {
252 return BAD_VALUE;
253 }
254 audio_io_handle_t output = getOutputForAttr(attr);
255 if (output == 0) {
256 return PERMISSION_DENIED;
257 }
258 return getSamplingRate(output, samplingRate);
259}
260
Eric Laurent1a9ed112012-03-20 18:36:01 -0700261status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800262 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700263{
264 OutputDescriptor *outputDesc;
265
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266 gLock.lock();
267 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800268 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 gLock.unlock();
271 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
272 if (af == 0) return PERMISSION_DENIED;
273 *samplingRate = af->sampleRate(output);
274 } else {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700276 *samplingRate = outputDesc->samplingRate;
277 gLock.unlock();
278 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800279 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700280 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800281 return BAD_VALUE;
282 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700283
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700284 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286 return NO_ERROR;
287}
288
Glenn Kastene33054e2012-11-14 12:54:39 -0800289status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292
Dima Zavinfce7a472011-04-19 22:30:36 -0700293 if (streamType == AUDIO_STREAM_DEFAULT) {
294 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700296
Glenn Kastenfff6d712012-01-12 16:38:12 -0800297 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700298 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 return PERMISSION_DENIED;
300 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700302 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700303}
304
305status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800306 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700307{
308 OutputDescriptor *outputDesc;
309
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 gLock.lock();
311 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800312 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 gLock.unlock();
314 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
315 if (af == 0) return PERMISSION_DENIED;
316 *frameCount = af->frameCount(output);
317 } else {
318 *frameCount = outputDesc->frameCount;
319 gLock.unlock();
320 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800321 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700322 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800323 return BAD_VALUE;
324 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700326 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 return NO_ERROR;
329}
330
Glenn Kastenfff6d712012-01-12 16:38:12 -0800331status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334
Dima Zavinfce7a472011-04-19 22:30:36 -0700335 if (streamType == AUDIO_STREAM_DEFAULT) {
336 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700338
Glenn Kastenfff6d712012-01-12 16:38:12 -0800339 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700340 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 return PERMISSION_DENIED;
342 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343
Glenn Kasten241618f2014-03-25 17:48:57 -0700344 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700345}
346
347status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700348 uint32_t* latency)
349{
350 OutputDescriptor *outputDesc;
351
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352 gLock.lock();
353 outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800354 if (outputDesc == NULL) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 gLock.unlock();
356 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
357 if (af == 0) return PERMISSION_DENIED;
358 *latency = af->latency(output);
359 } else {
360 *latency = outputDesc->latency;
361 gLock.unlock();
362 }
363
Glenn Kasten241618f2014-03-25 17:48:57 -0700364 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 return NO_ERROR;
367}
368
Glenn Kastendd8104c2012-07-02 12:42:44 -0700369status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
370 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800372 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800374 size_t inBuffSize = gInBuffSize;
375 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700376 || (channelMask != gPrevInChannelMask)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800377 gLock.unlock();
378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) {
380 return PERMISSION_DENIED;
381 }
Glenn Kastendd8104c2012-07-02 12:42:44 -0700382 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Glenn Kasten5446e542014-01-08 08:58:53 -0800383 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800384 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800385 sampleRate, format, channelMask);
386 return BAD_VALUE;
387 }
388 // A benign race is possible here: we could overwrite a fresher cache entry
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800389 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 // save the request params
391 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700393 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800395 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700396 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800397 gLock.unlock();
398 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700399
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400 return NO_ERROR;
401}
402
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700403status_t AudioSystem::setVoiceVolume(float value)
404{
405 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
406 if (af == 0) return PERMISSION_DENIED;
407 return af->setVoiceVolume(value);
408}
409
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000410status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700411 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800412{
413 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
414 if (af == 0) return PERMISSION_DENIED;
415
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000416 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800417}
418
Glenn Kasten4944acb2013-08-19 08:39:20 -0700419uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
420{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800422 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800423 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700424 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800425
426 result = af->getInputFramesLost(ioHandle);
427 return result;
428}
429
Eric Laurentde3f8392014-07-27 18:38:22 -0700430audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700431{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700433 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
434 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700435}
436
Marco Nelissend457c972014-02-11 08:47:07 -0800437void AudioSystem::acquireAudioSessionId(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->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700442 }
443}
444
Marco Nelissend457c972014-02-11 08:47:07 -0800445void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700446{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700447 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
448 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800449 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700450 }
451}
452
Eric Laurent93c3d412014-08-01 14:48:35 -0700453audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
454{
455 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456 if (af == 0) return AUDIO_HW_SYNC_INVALID;
457 return af->getAudioHwSyncForSession(sessionId);
458}
459
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460// ---------------------------------------------------------------------------
461
Glenn Kasten4944acb2013-08-19 08:39:20 -0700462void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
463{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800467 // clear output handles and stream to output map caches
Eric Laurent0ef583f2010-01-25 10:27:15 -0800468 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469
470 if (gAudioErrorCallback) {
471 gAudioErrorCallback(DEAD_OBJECT);
472 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000473 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474}
475
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800476void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800477 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800479 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800480 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481
Glenn Kasten142f5192014-03-25 17:44:59 -0700482 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483
484 Mutex::Autolock _l(AudioSystem::gLock);
485
486 switch (event) {
487 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488 break;
489 case OUTPUT_OPENED: {
490 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492 break;
493 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800494 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800495 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700496
497 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
498 gOutputs.add(ioHandle, outputDesc);
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700499 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700500 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700501 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700502 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700503 } break;
504 case OUTPUT_CLOSED: {
505 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800506 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700507 break;
508 }
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700510
511 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700512 } break;
513
514 case OUTPUT_CONFIG_CHANGED: {
515 int index = gOutputs.indexOfKey(ioHandle);
516 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800517 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518 break;
519 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800520 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800521 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Glenn Kastencac3daa2014-02-07 09:47:14 -0800523 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700524 "frameCount %zu latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700525 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700526 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
528 delete outputDesc;
529 outputDesc = new OutputDescriptor(*desc);
530 gOutputs.replaceValueFor(ioHandle, outputDesc);
531 } break;
532 case INPUT_OPENED:
533 case INPUT_CLOSED:
534 case INPUT_CONFIG_CHANGED:
535 break;
536
537 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538}
539
Glenn Kasten4944acb2013-08-19 08:39:20 -0700540void AudioSystem::setErrorCallback(audio_error_callback cb)
541{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543 gAudioErrorCallback = cb;
544}
545
Eric Laurentb52c1522014-05-20 11:27:36 -0700546
Glenn Kasten4944acb2013-08-19 08:39:20 -0700547bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType)
548{
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700549 switch (streamType) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700550 case AUDIO_STREAM_MUSIC:
551 case AUDIO_STREAM_VOICE_CALL:
552 case AUDIO_STREAM_BLUETOOTH_SCO:
553 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 return true;
555 default:
556 return false;
557 }
558}
559
560
Eric Laurentc2f1f072009-07-17 12:17:14 -0700561// client singleton for AudioPolicyService binder interface
562sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
563sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
564
565
Glenn Kasten18a6d902012-09-24 11:27:56 -0700566// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700567const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
568{
569 gLock.lock();
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800570 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571 sp<IServiceManager> sm = defaultServiceManager();
572 sp<IBinder> binder;
573 do {
574 binder = sm->getService(String16("media.audio_policy"));
575 if (binder != 0)
576 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000577 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700578 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700579 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700580 if (gAudioPolicyServiceClient == NULL) {
581 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
582 }
583 binder->linkToDeath(gAudioPolicyServiceClient);
584 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
585 gLock.unlock();
Andy Hungb4453752014-09-08 11:47:24 -0700586 // Registering the client takes the AudioPolicyService lock.
587 // Don't hold the AudioSystem lock at the same time.
588 gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700589 } else {
Andy Hungb4453752014-09-08 11:47:24 -0700590 // There exists a benign race condition where gAudioPolicyService
591 // is set, but gAudioPolicyServiceClient is not yet registered.
Eric Laurentc2f1f072009-07-17 12:17:14 -0700592 gLock.unlock();
593 }
594 return gAudioPolicyService;
595}
596
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700597// ---------------------------------------------------------------------------
598
Dima Zavinfce7a472011-04-19 22:30:36 -0700599status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
600 audio_policy_dev_state_t state,
601 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700602{
603 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700604 const char *address = "";
605
Eric Laurentc2f1f072009-07-17 12:17:14 -0700606 if (aps == 0) return PERMISSION_DENIED;
607
Eric Laurent71b63e32011-09-02 14:20:56 -0700608 if (device_address != NULL) {
609 address = device_address;
610 }
611
612 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700613}
614
Dima Zavinfce7a472011-04-19 22:30:36 -0700615audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700616 const char *device_address)
617{
618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700619 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700620
621 return aps->getDeviceConnectionState(device, device_address);
622}
623
Glenn Kastenf78aee72012-01-04 11:00:47 -0800624status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700625{
Glenn Kasten347966c2012-01-18 14:58:32 -0800626 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700627 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
628 if (aps == 0) return PERMISSION_DENIED;
629
630 return aps->setPhoneState(state);
631}
632
Dima Zavinfce7a472011-04-19 22:30:36 -0700633status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700634{
635 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
636 if (aps == 0) return PERMISSION_DENIED;
637 return aps->setForceUse(usage, config);
638}
639
Dima Zavinfce7a472011-04-19 22:30:36 -0700640audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641{
642 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700643 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700644 return aps->getForceUse(usage);
645}
646
647
Dima Zavinfce7a472011-04-19 22:30:36 -0700648audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700649 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800650 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700651 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000652 audio_output_flags_t flags,
653 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700654{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700655 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
656 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000657 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700658}
659
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700660audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
661 uint32_t samplingRate,
662 audio_format_t format,
663 audio_channel_mask_t channelMask,
664 audio_output_flags_t flags,
665 const audio_offload_info_t *offloadInfo)
666{
667 if (attr == NULL) return 0;
668 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
669 if (aps == 0) return 0;
670 return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo);
671}
672
Eric Laurentde070132010-07-13 04:45:46 -0700673status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700674 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700675 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676{
677 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
678 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700679 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700680}
681
Eric Laurentde070132010-07-13 04:45:46 -0700682status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700683 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700684 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700685{
686 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
687 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700688 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700689}
690
691void AudioSystem::releaseOutput(audio_io_handle_t output)
692{
693 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
694 if (aps == 0) return;
695 aps->releaseOutput(output);
696}
697
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800698audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700699 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800700 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700701 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700702 int sessionId,
703 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700704{
705 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700706 if (aps == 0) return 0;
Glenn Kastenb3b16602014-07-16 08:36:31 -0700707 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708}
709
Eric Laurent4dc68062014-07-28 17:26:49 -0700710status_t AudioSystem::startInput(audio_io_handle_t input,
711 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700712{
713 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
714 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700715 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700716}
717
Eric Laurent4dc68062014-07-28 17:26:49 -0700718status_t AudioSystem::stopInput(audio_io_handle_t input,
719 audio_session_t session)
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 Laurent4dc68062014-07-28 17:26:49 -0700723 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700724}
725
Eric Laurent4dc68062014-07-28 17:26:49 -0700726void AudioSystem::releaseInput(audio_io_handle_t input,
727 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700728{
729 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
730 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700731 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700732}
733
Dima Zavinfce7a472011-04-19 22:30:36 -0700734status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700735 int indexMin,
736 int indexMax)
737{
738 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
739 if (aps == 0) return PERMISSION_DENIED;
740 return aps->initStreamVolume(stream, indexMin, indexMax);
741}
742
Eric Laurent83844cc2011-11-18 16:43:31 -0800743status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
744 int index,
745 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700746{
747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
748 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800749 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700750}
751
Eric Laurent83844cc2011-11-18 16:43:31 -0800752status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
753 int *index,
754 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755{
756 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
757 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800758 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700759}
760
Dima Zavinfce7a472011-04-19 22:30:36 -0700761uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700762{
763 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
764 if (aps == 0) return 0;
765 return aps->getStrategyForStream(stream);
766}
767
Eric Laurent63742522012-03-08 13:42:42 -0800768audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800769{
770 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800771 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800772 return aps->getDevicesForStream(stream);
773}
774
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700775audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700776{
777 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800778 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700779 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700780 return aps->getOutputForEffect(desc);
781}
782
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700783status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700784 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700785 uint32_t strategy,
786 int session,
787 int id)
788{
789 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
790 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700791 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700792}
793
794status_t AudioSystem::unregisterEffect(int id)
795{
796 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
797 if (aps == 0) return PERMISSION_DENIED;
798 return aps->unregisterEffect(id);
799}
800
Eric Laurentdb7c0792011-08-10 10:37:50 -0700801status_t AudioSystem::setEffectEnabled(int id, bool enabled)
802{
803 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
804 if (aps == 0) return PERMISSION_DENIED;
805 return aps->setEffectEnabled(id, enabled);
806}
807
Glenn Kastenfff6d712012-01-12 16:38:12 -0800808status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700809{
Eric Laurenteda6c362011-02-02 09:33:30 -0800810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
811 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700812 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800813 *state = aps->isStreamActive(stream, inPastMs);
814 return NO_ERROR;
815}
816
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800817status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
818 uint32_t inPastMs)
819{
820 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
821 if (aps == 0) return PERMISSION_DENIED;
822 if (state == NULL) return BAD_VALUE;
823 *state = aps->isStreamActiveRemotely(stream, inPastMs);
824 return NO_ERROR;
825}
826
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700827status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
828{
829 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
830 if (aps == 0) return PERMISSION_DENIED;
831 if (state == NULL) return BAD_VALUE;
832 *state = aps->isSourceActive(stream);
833 return NO_ERROR;
834}
835
Glenn Kasten3b16c762012-11-14 08:44:39 -0800836uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700837{
838 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
839 if (af == 0) return 0;
840 return af->getPrimaryOutputSamplingRate();
841}
842
Glenn Kastene33054e2012-11-14 12:54:39 -0800843size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700844{
845 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
846 if (af == 0) return 0;
847 return af->getPrimaryOutputFrameCount();
848}
Eric Laurenteda6c362011-02-02 09:33:30 -0800849
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700850status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
851{
852 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
853 if (af == 0) return PERMISSION_DENIED;
854 return af->setLowRamDevice(isLowRamDevice);
855}
856
Eric Laurent9f6530f2011-08-30 10:18:54 -0700857void AudioSystem::clearAudioConfigCache()
858{
859 Mutex::Autolock _l(gLock);
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("clearAudioConfigCache()");
Eric Laurent9f6530f2011-08-30 10:18:54 -0700861 gOutputs.clear();
862}
863
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000864bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
865{
866 ALOGV("isOffloadSupported()");
867 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
868 if (aps == 0) return false;
869 return aps->isOffloadSupported(info);
870}
871
Eric Laurent203b1a12014-04-01 10:34:16 -0700872status_t AudioSystem::listAudioPorts(audio_port_role_t role,
873 audio_port_type_t type,
874 unsigned int *num_ports,
875 struct audio_port *ports,
876 unsigned int *generation)
877{
878 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
879 if (aps == 0) return PERMISSION_DENIED;
880 return aps->listAudioPorts(role, type, num_ports, ports, generation);
881}
882
883status_t AudioSystem::getAudioPort(struct audio_port *port)
884{
885 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
886 if (aps == 0) return PERMISSION_DENIED;
887 return aps->getAudioPort(port);
888}
889
890status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
891 audio_patch_handle_t *handle)
892{
893 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
894 if (aps == 0) return PERMISSION_DENIED;
895 return aps->createAudioPatch(patch, handle);
896}
897
898status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
899{
900 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
901 if (aps == 0) return PERMISSION_DENIED;
902 return aps->releaseAudioPatch(handle);
903}
904
905status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
906 struct audio_patch *patches,
907 unsigned int *generation)
908{
909 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
910 if (aps == 0) return PERMISSION_DENIED;
911 return aps->listAudioPatches(num_patches, patches, generation);
912}
913
914status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
915{
916 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
917 if (aps == 0) return PERMISSION_DENIED;
918 return aps->setAudioPortConfig(config);
919}
920
Eric Laurentb52c1522014-05-20 11:27:36 -0700921void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
922{
923 Mutex::Autolock _l(gLock);
924 gAudioPortCallback = callBack;
925}
926
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700927status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
928 audio_io_handle_t *ioHandle,
929 audio_devices_t *device)
930{
931 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
932 if (aps == 0) return PERMISSION_DENIED;
933 return aps->acquireSoundTriggerSession(session, ioHandle, device);
934}
935
936status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
937{
938 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
939 if (aps == 0) return PERMISSION_DENIED;
940 return aps->releaseSoundTriggerSession(session);
941}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700942
943audio_mode_t AudioSystem::getPhoneState()
944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
946 if (aps == 0) return AUDIO_MODE_INVALID;
947 return aps->getPhoneState();
948}
949
950
Eric Laurentc2f1f072009-07-17 12:17:14 -0700951// ---------------------------------------------------------------------------
952
Glenn Kasten4944acb2013-08-19 08:39:20 -0700953void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
954{
Eric Laurentb52c1522014-05-20 11:27:36 -0700955 Mutex::Autolock _l(gLock);
956 if (gAudioPortCallback != 0) {
957 gAudioPortCallback->onServiceDied();
958 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959 AudioSystem::gAudioPolicyService.clear();
960
Steve Block5ff1dd52012-01-05 23:22:43 +0000961 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962}
963
Eric Laurentb52c1522014-05-20 11:27:36 -0700964void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
965{
966 Mutex::Autolock _l(gLock);
967 if (gAudioPortCallback != 0) {
968 gAudioPortCallback->onAudioPortListUpdate();
969 }
970}
971
972void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
973{
974 Mutex::Autolock _l(gLock);
975 if (gAudioPortCallback != 0) {
976 gAudioPortCallback->onAudioPatchListUpdate();
977 }
978}
979
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800980}; // namespace android