blob: e9ee16906bcf70bbfd82d5e0ab7d5af561e9bd0c [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;
Eric Laurentf6778fd2014-11-18 17:26:58 -080035Mutex AudioSystem::gLockCache;
Glenn Kastend2d089f2014-11-05 11:48:12 -080036Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037sp<IAudioFlinger> AudioSystem::gAudioFlinger;
38sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
39audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080040
Glenn Kasten2301acc2014-01-17 10:21:00 -080041// Cached values for output handles
Glenn Kasten9ea65d02014-01-17 10:21:24 -080042DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL);
Eric Laurentc2f1f072009-07-17 12:17:14 -070043
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -080044// Cached values for recording queries, all protected by gLock
Glenn Kasten5446e542014-01-08 08:58:53 -080045uint32_t AudioSystem::gPrevInSamplingRate;
46audio_format_t AudioSystem::gPrevInFormat;
47audio_channel_mask_t AudioSystem::gPrevInChannelMask;
48size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080051const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080053 sp<IAudioFlinger> af;
54 sp<AudioFlingerClient> afc;
55 {
56 Mutex::Autolock _l(gLock);
57 if (gAudioFlinger == 0) {
58 sp<IServiceManager> sm = defaultServiceManager();
59 sp<IBinder> binder;
60 do {
61 binder = sm->getService(String16("media.audio_flinger"));
62 if (binder != 0)
63 break;
64 ALOGW("AudioFlinger not published, waiting...");
65 usleep(500000); // 0.5 s
66 } while (true);
67 if (gAudioFlingerClient == NULL) {
68 gAudioFlingerClient = new AudioFlingerClient();
69 } else {
70 if (gAudioErrorCallback) {
71 gAudioErrorCallback(NO_ERROR);
72 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080074 binder->linkToDeath(gAudioFlingerClient);
75 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
76 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
77 afc = gAudioFlingerClient;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070078 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080079 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080081 if (afc != 0) {
82 af->registerClient(afc);
83 }
84 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085}
86
Eric Laurent46291612013-07-18 14:38:44 -070087/* static */ status_t AudioSystem::checkAudioFlinger()
88{
89 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
90 return NO_ERROR;
91 }
92 return DEAD_OBJECT;
93}
94
Glenn Kasten4944acb2013-08-19 08:39:20 -070095status_t AudioSystem::muteMicrophone(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 return af->setMicMute(state);
100}
101
Glenn Kasten4944acb2013-08-19 08:39:20 -0700102status_t AudioSystem::isMicrophoneMuted(bool* state)
103{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
105 if (af == 0) return PERMISSION_DENIED;
106 *state = af->getMicMute();
107 return NO_ERROR;
108}
109
110status_t AudioSystem::setMasterVolume(float value)
111{
112 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
113 if (af == 0) return PERMISSION_DENIED;
114 af->setMasterVolume(value);
115 return NO_ERROR;
116}
117
118status_t AudioSystem::setMasterMute(bool mute)
119{
120 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
121 if (af == 0) return PERMISSION_DENIED;
122 af->setMasterMute(mute);
123 return NO_ERROR;
124}
125
126status_t AudioSystem::getMasterVolume(float* volume)
127{
128 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
129 if (af == 0) return PERMISSION_DENIED;
130 *volume = af->masterVolume();
131 return NO_ERROR;
132}
133
134status_t AudioSystem::getMasterMute(bool* mute)
135{
136 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
137 if (af == 0) return PERMISSION_DENIED;
138 *mute = af->masterMute();
139 return NO_ERROR;
140}
141
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800142status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
143 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144{
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
147 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700148 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 return NO_ERROR;
150}
151
Glenn Kastenfff6d712012-01-12 16:38:12 -0800152status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153{
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
156 if (af == 0) return PERMISSION_DENIED;
157 af->setStreamMute(stream, mute);
158 return NO_ERROR;
159}
160
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800161status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
162 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163{
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
166 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700167 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 return NO_ERROR;
169}
170
Glenn Kastenfff6d712012-01-12 16:38:12 -0800171status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172{
Dima Zavinfce7a472011-04-19 22:30:36 -0700173 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
176 *mute = af->streamMute(stream);
177 return NO_ERROR;
178}
179
Glenn Kastenf78aee72012-01-04 11:00:47 -0800180status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800182 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
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;
185 return af->setMode(mode);
186}
187
Glenn Kasten4944acb2013-08-19 08:39:20 -0700188status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
189{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
191 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700192 return af->setParameters(ioHandle, keyValuePairs);
193}
194
Glenn Kasten4944acb2013-08-19 08:39:20 -0700195String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
196{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700197 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
198 String8 result = String8("");
199 if (af == 0) return result;
200
201 result = af->getParameters(ioHandle, keys);
202 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203}
204
Glenn Kastenc23885e2013-12-19 16:35:18 -0800205status_t AudioSystem::setParameters(const String8& keyValuePairs)
206{
Glenn Kasten142f5192014-03-25 17:44:59 -0700207 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800208}
209
210String8 AudioSystem::getParameters(const String8& keys)
211{
Glenn Kasten142f5192014-03-25 17:44:59 -0700212 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800213}
214
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215// convert volume steps to natural log scale
216
217// change this value to change volume scaling
218static const float dBPerStep = 0.5f;
219// shouldn't need to touch these
220static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
221static const float dBConvertInverse = 1.0f / dBConvert;
222
223float AudioSystem::linearToLog(int volume)
224{
225 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000226 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 // return v;
228 return volume ? exp(float(100 - volume) * dBConvert) : 0;
229}
230
231int AudioSystem::logToLinear(float volume)
232{
233 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000234 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 // return v;
236 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
237}
238
Glenn Kasten3b16c762012-11-14 08:44:39 -0800239status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800242
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 if (streamType == AUDIO_STREAM_DEFAULT) {
244 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700245 }
246
Glenn Kastenfff6d712012-01-12 16:38:12 -0800247 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700248 if (output == 0) {
249 return PERMISSION_DENIED;
250 }
251
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700252 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700253}
254
255status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800256 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700257{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800258 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
259 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700260
Eric Laurentf6778fd2014-11-18 17:26:58 -0800261 Mutex::Autolock _l(gLockCache);
262
263 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800264 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800266 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700267 *samplingRate = af->sampleRate(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800268 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700269 } else {
Steve Block3856b092011-10-20 11:56:00 +0100270 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700271 *samplingRate = outputDesc->samplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800273 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700274 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800275 return BAD_VALUE;
276 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700278 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700279
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 return NO_ERROR;
281}
282
Glenn Kastene33054e2012-11-14 12:54:39 -0800283status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286
Dima Zavinfce7a472011-04-19 22:30:36 -0700287 if (streamType == AUDIO_STREAM_DEFAULT) {
288 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700290
Glenn Kastenfff6d712012-01-12 16:38:12 -0800291 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700292 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293 return PERMISSION_DENIED;
294 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700296 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700297}
298
299status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800300 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700301{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800302 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
303 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700304
Eric Laurentf6778fd2014-11-18 17:26:58 -0800305 Mutex::Autolock _l(gLockCache);
306
307 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800308 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800309 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 *frameCount = af->frameCount(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800311 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 } else {
313 *frameCount = outputDesc->frameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800315 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700316 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800317 return BAD_VALUE;
318 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700319
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700320 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 return NO_ERROR;
323}
324
Glenn Kastenfff6d712012-01-12 16:38:12 -0800325status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 if (streamType == AUDIO_STREAM_DEFAULT) {
330 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700332
Glenn Kastenfff6d712012-01-12 16:38:12 -0800333 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700334 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 return PERMISSION_DENIED;
336 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337
Glenn Kasten241618f2014-03-25 17:48:57 -0700338 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700339}
340
341status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700342 uint32_t* latency)
343{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800344 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
345 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700346
Eric Laurentf6778fd2014-11-18 17:26:58 -0800347 Mutex::Autolock _l(gLockCache);
348
349 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800350 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800351 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352 *latency = af->latency(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800353 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354 } else {
355 *latency = outputDesc->latency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356 }
357
Glenn Kasten241618f2014-03-25 17:48:57 -0700358 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700359
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 return NO_ERROR;
361}
362
Glenn Kastendd8104c2012-07-02 12:42:44 -0700363status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
364 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800366 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
367 if (af == 0) {
368 return PERMISSION_DENIED;
369 }
370 Mutex::Autolock _l(gLockCache);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800372 size_t inBuffSize = gInBuffSize;
373 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700374 || (channelMask != gPrevInChannelMask)) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800375 gLockCache.unlock();
Glenn Kastendd8104c2012-07-02 12:42:44 -0700376 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800377 gLockCache.lock();
Glenn Kasten5446e542014-01-08 08:58:53 -0800378 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800379 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800380 sampleRate, format, channelMask);
381 return BAD_VALUE;
382 }
383 // A benign race is possible here: we could overwrite a fresher cache entry
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 // save the request params
385 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700386 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700387 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800389 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700390 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800391 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 return NO_ERROR;
394}
395
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700396status_t AudioSystem::setVoiceVolume(float value)
397{
398 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
399 if (af == 0) return PERMISSION_DENIED;
400 return af->setVoiceVolume(value);
401}
402
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000403status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700404 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800405{
406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af == 0) return PERMISSION_DENIED;
408
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000409 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800410}
411
Glenn Kasten4944acb2013-08-19 08:39:20 -0700412uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
413{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800414 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800415 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800416 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700417 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800418
419 result = af->getInputFramesLost(ioHandle);
420 return result;
421}
422
Eric Laurentde3f8392014-07-27 18:38:22 -0700423audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700424{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700426 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
427 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700428}
429
Marco Nelissend457c972014-02-11 08:47:07 -0800430void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700431{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800434 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700435 }
436}
437
Marco Nelissend457c972014-02-11 08:47:07 -0800438void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700439{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800442 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700443 }
444}
445
Eric Laurent93c3d412014-08-01 14:48:35 -0700446audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
447{
448 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
449 if (af == 0) return AUDIO_HW_SYNC_INVALID;
450 return af->getAudioHwSyncForSession(sessionId);
451}
452
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453// ---------------------------------------------------------------------------
454
Glenn Kasten4944acb2013-08-19 08:39:20 -0700455void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
456{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800457 audio_error_callback cb = NULL;
458 {
459 Mutex::Autolock _l(AudioSystem::gLock);
460 AudioSystem::gAudioFlinger.clear();
461 cb = gAudioErrorCallback;
462 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463
Eric Laurentf6778fd2014-11-18 17:26:58 -0800464 {
465 // clear output handles and stream to output map caches
466 Mutex::Autolock _l(gLockCache);
467 AudioSystem::gOutputs.clear();
468 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469
Eric Laurentf6778fd2014-11-18 17:26:58 -0800470 if (cb) {
471 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 }
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
Eric Laurentf6778fd2014-11-18 17:26:58 -0800484 Mutex::Autolock _l(AudioSystem::gLockCache);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700485
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);
Glenn Kastenb187de12014-12-30 08:18:15 -0800499 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x "
500 "frameCount %zu 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 Kastenb187de12014-12-30 08:18:15 -0800523 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x "
524 "channel mask %#x 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 Laurentc2f1f072009-07-17 12:17:14 -0700546// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800547// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700548sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
549sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
550
551
Glenn Kasten18a6d902012-09-24 11:27:56 -0700552// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800553const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700554{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800555 sp<IAudioPolicyService> ap;
556 sp<AudioPolicyServiceClient> apc;
557 {
558 Mutex::Autolock _l(gLockAPS);
559 if (gAudioPolicyService == 0) {
560 sp<IServiceManager> sm = defaultServiceManager();
561 sp<IBinder> binder;
562 do {
563 binder = sm->getService(String16("media.audio_policy"));
564 if (binder != 0)
565 break;
566 ALOGW("AudioPolicyService not published, waiting...");
567 usleep(500000); // 0.5 s
568 } while (true);
569 if (gAudioPolicyServiceClient == NULL) {
570 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
571 }
572 binder->linkToDeath(gAudioPolicyServiceClient);
573 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
574 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
575 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700576 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800577 ap = gAudioPolicyService;
578 }
579 if (apc != 0) {
580 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700581 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800582
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800583 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700584}
585
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700586// ---------------------------------------------------------------------------
587
Dima Zavinfce7a472011-04-19 22:30:36 -0700588status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
589 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800590 const char *device_address,
591 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700592{
593 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700594 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800595 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700596
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597 if (aps == 0) return PERMISSION_DENIED;
598
Eric Laurent71b63e32011-09-02 14:20:56 -0700599 if (device_address != NULL) {
600 address = device_address;
601 }
Paul McLeane743a472015-01-28 11:07:31 -0800602 if (device_name != NULL) {
603 name = device_name;
604 }
605 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700606}
607
Dima Zavinfce7a472011-04-19 22:30:36 -0700608audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700609 const char *device_address)
610{
611 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700612 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700613
614 return aps->getDeviceConnectionState(device, device_address);
615}
616
Glenn Kastenf78aee72012-01-04 11:00:47 -0800617status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700618{
Glenn Kasten347966c2012-01-18 14:58:32 -0800619 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700620 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
621 if (aps == 0) return PERMISSION_DENIED;
622
623 return aps->setPhoneState(state);
624}
625
Dima Zavinfce7a472011-04-19 22:30:36 -0700626status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700627{
628 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
629 if (aps == 0) return PERMISSION_DENIED;
630 return aps->setForceUse(usage, config);
631}
632
Dima Zavinfce7a472011-04-19 22:30:36 -0700633audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700634{
635 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700636 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700637 return aps->getForceUse(usage);
638}
639
640
Dima Zavinfce7a472011-04-19 22:30:36 -0700641audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700642 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800643 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700644 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000645 audio_output_flags_t flags,
646 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700647{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700648 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
649 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000650 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700651}
652
Eric Laurente83b55d2014-11-14 10:06:21 -0800653status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
654 audio_io_handle_t *output,
655 audio_session_t session,
656 audio_stream_type_t *stream,
657 uint32_t samplingRate,
658 audio_format_t format,
659 audio_channel_mask_t channelMask,
660 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700661 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800662 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700663{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700664 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800665 if (aps == 0) return NO_INIT;
666 return aps->getOutputForAttr(attr, output, session, stream,
667 samplingRate, format, channelMask,
Paul McLeanaa981192015-03-21 09:55:15 -0700668 flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700669}
670
Eric Laurentde070132010-07-13 04:45:46 -0700671status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700672 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800673 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700674{
675 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
676 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700677 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700678}
679
Eric Laurentde070132010-07-13 04:45:46 -0700680status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700681 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800682 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700683{
684 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
685 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700686 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700687}
688
Eric Laurente83b55d2014-11-14 10:06:21 -0800689void AudioSystem::releaseOutput(audio_io_handle_t output,
690 audio_stream_type_t stream,
691 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700692{
693 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
694 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800695 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700696}
697
Eric Laurentcaf7f482014-11-25 17:50:47 -0800698status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
699 audio_io_handle_t *input,
700 audio_session_t session,
701 uint32_t samplingRate,
702 audio_format_t format,
703 audio_channel_mask_t channelMask,
704 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700705{
706 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800707 if (aps == 0) return NO_INIT;
708 return aps->getInputForAttr(attr, input, session, samplingRate, format, channelMask, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709}
710
Eric Laurent4dc68062014-07-28 17:26:49 -0700711status_t AudioSystem::startInput(audio_io_handle_t input,
712 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700713{
714 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
715 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700716 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717}
718
Eric Laurent4dc68062014-07-28 17:26:49 -0700719status_t AudioSystem::stopInput(audio_io_handle_t input,
720 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700721{
722 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
723 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700724 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700725}
726
Eric Laurent4dc68062014-07-28 17:26:49 -0700727void AudioSystem::releaseInput(audio_io_handle_t input,
728 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729{
730 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
731 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700732 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700733}
734
Dima Zavinfce7a472011-04-19 22:30:36 -0700735status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700736 int indexMin,
737 int indexMax)
738{
739 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
740 if (aps == 0) return PERMISSION_DENIED;
741 return aps->initStreamVolume(stream, indexMin, indexMax);
742}
743
Eric Laurent83844cc2011-11-18 16:43:31 -0800744status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
745 int index,
746 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700747{
748 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
749 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800750 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700751}
752
Eric Laurent83844cc2011-11-18 16:43:31 -0800753status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
754 int *index,
755 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700756{
757 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
758 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800759 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700760}
761
Dima Zavinfce7a472011-04-19 22:30:36 -0700762uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700763{
764 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
765 if (aps == 0) return 0;
766 return aps->getStrategyForStream(stream);
767}
768
Eric Laurent63742522012-03-08 13:42:42 -0800769audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800770{
771 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800772 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800773 return aps->getDevicesForStream(stream);
774}
775
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700776audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700777{
778 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800779 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700780 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700781 return aps->getOutputForEffect(desc);
782}
783
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700784status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700785 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700786 uint32_t strategy,
787 int session,
788 int id)
789{
790 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
791 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700792 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700793}
794
795status_t AudioSystem::unregisterEffect(int id)
796{
797 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
798 if (aps == 0) return PERMISSION_DENIED;
799 return aps->unregisterEffect(id);
800}
801
Eric Laurentdb7c0792011-08-10 10:37:50 -0700802status_t AudioSystem::setEffectEnabled(int id, bool enabled)
803{
804 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
805 if (aps == 0) return PERMISSION_DENIED;
806 return aps->setEffectEnabled(id, enabled);
807}
808
Glenn Kastenfff6d712012-01-12 16:38:12 -0800809status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700810{
Eric Laurenteda6c362011-02-02 09:33:30 -0800811 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
812 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700813 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800814 *state = aps->isStreamActive(stream, inPastMs);
815 return NO_ERROR;
816}
817
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800818status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
819 uint32_t inPastMs)
820{
821 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
822 if (aps == 0) return PERMISSION_DENIED;
823 if (state == NULL) return BAD_VALUE;
824 *state = aps->isStreamActiveRemotely(stream, inPastMs);
825 return NO_ERROR;
826}
827
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700828status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
829{
830 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
831 if (aps == 0) return PERMISSION_DENIED;
832 if (state == NULL) return BAD_VALUE;
833 *state = aps->isSourceActive(stream);
834 return NO_ERROR;
835}
836
Glenn Kasten3b16c762012-11-14 08:44:39 -0800837uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700838{
839 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
840 if (af == 0) return 0;
841 return af->getPrimaryOutputSamplingRate();
842}
843
Glenn Kastene33054e2012-11-14 12:54:39 -0800844size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700845{
846 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
847 if (af == 0) return 0;
848 return af->getPrimaryOutputFrameCount();
849}
Eric Laurenteda6c362011-02-02 09:33:30 -0800850
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700851status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
852{
853 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
854 if (af == 0) return PERMISSION_DENIED;
855 return af->setLowRamDevice(isLowRamDevice);
856}
857
Eric Laurent9f6530f2011-08-30 10:18:54 -0700858void AudioSystem::clearAudioConfigCache()
859{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800860 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800862 {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800863 Mutex::Autolock _l(gLockCache);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800864 gOutputs.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800865 }
866 {
867 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800868 gAudioFlinger.clear();
869 }
870 {
871 Mutex::Autolock _l(gLockAPS);
872 gAudioPolicyService.clear();
873 }
Eric Laurent9f6530f2011-08-30 10:18:54 -0700874}
875
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000876bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
877{
878 ALOGV("isOffloadSupported()");
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
880 if (aps == 0) return false;
881 return aps->isOffloadSupported(info);
882}
883
Eric Laurent203b1a12014-04-01 10:34:16 -0700884status_t AudioSystem::listAudioPorts(audio_port_role_t role,
885 audio_port_type_t type,
886 unsigned int *num_ports,
887 struct audio_port *ports,
888 unsigned int *generation)
889{
890 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
891 if (aps == 0) return PERMISSION_DENIED;
892 return aps->listAudioPorts(role, type, num_ports, ports, generation);
893}
894
895status_t AudioSystem::getAudioPort(struct audio_port *port)
896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
898 if (aps == 0) return PERMISSION_DENIED;
899 return aps->getAudioPort(port);
900}
901
902status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
903 audio_patch_handle_t *handle)
904{
905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
906 if (aps == 0) return PERMISSION_DENIED;
907 return aps->createAudioPatch(patch, handle);
908}
909
910status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
911{
912 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
913 if (aps == 0) return PERMISSION_DENIED;
914 return aps->releaseAudioPatch(handle);
915}
916
917status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
918 struct audio_patch *patches,
919 unsigned int *generation)
920{
921 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
922 if (aps == 0) return PERMISSION_DENIED;
923 return aps->listAudioPatches(num_patches, patches, generation);
924}
925
926status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
927{
928 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
929 if (aps == 0) return PERMISSION_DENIED;
930 return aps->setAudioPortConfig(config);
931}
932
Eric Laurentb28753e2015-04-01 13:06:28 -0700933status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callBack)
Eric Laurentb52c1522014-05-20 11:27:36 -0700934{
Eric Laurentb28753e2015-04-01 13:06:28 -0700935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
936 if (aps == 0) return PERMISSION_DENIED;
937
938 Mutex::Autolock _l(gLockAPS);
939 if (gAudioPolicyServiceClient == 0) {
940 return NO_INIT;
941 }
942 return gAudioPolicyServiceClient->addAudioPortCallback(callBack);
Eric Laurentb52c1522014-05-20 11:27:36 -0700943}
944
Eric Laurentb28753e2015-04-01 13:06:28 -0700945status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callBack)
946{
947 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
948 if (aps == 0) return PERMISSION_DENIED;
949
950 Mutex::Autolock _l(gLockAPS);
951 if (gAudioPolicyServiceClient == 0) {
952 return NO_INIT;
953 }
954 return gAudioPolicyServiceClient->removeAudioPortCallback(callBack);
955}
956
957
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700958status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
959 audio_io_handle_t *ioHandle,
960 audio_devices_t *device)
961{
962 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
963 if (aps == 0) return PERMISSION_DENIED;
964 return aps->acquireSoundTriggerSession(session, ioHandle, device);
965}
966
967status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
968{
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
970 if (aps == 0) return PERMISSION_DENIED;
971 return aps->releaseSoundTriggerSession(session);
972}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700973
974audio_mode_t AudioSystem::getPhoneState()
975{
976 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
977 if (aps == 0) return AUDIO_MODE_INVALID;
978 return aps->getPhoneState();
979}
980
Eric Laurentbaac1832014-12-01 17:52:59 -0800981status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
982{
983 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
984 if (aps == 0) return PERMISSION_DENIED;
985 return aps->registerPolicyMixes(mixes, registration);
986}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700987
Eric Laurent554a2772015-04-10 11:29:24 -0700988status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
989 const audio_attributes_t *attributes,
990 audio_io_handle_t *handle)
991{
992 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
993 if (aps == 0) return PERMISSION_DENIED;
994 return aps->startAudioSource(source, attributes, handle);
995}
996
997status_t AudioSystem::stopAudioSource(audio_io_handle_t handle)
998{
999 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1000 if (aps == 0) return PERMISSION_DENIED;
1001 return aps->stopAudioSource(handle);
1002}
1003
Eric Laurentc2f1f072009-07-17 12:17:14 -07001004// ---------------------------------------------------------------------------
1005
Eric Laurentb28753e2015-04-01 13:06:28 -07001006status_t AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1007 const sp<AudioPortCallback>& callBack)
1008{
1009 Mutex::Autolock _l(mLock);
1010 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1011 if (mAudioPortCallbacks[i] == callBack) {
1012 return INVALID_OPERATION;
1013 }
1014 }
1015 mAudioPortCallbacks.add(callBack);
1016 return NO_ERROR;
1017}
1018
1019status_t AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1020 const sp<AudioPortCallback>& callBack)
1021{
1022 Mutex::Autolock _l(mLock);
1023 size_t i;
1024 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1025 if (mAudioPortCallbacks[i] == callBack) {
1026 break;
1027 }
1028 }
1029 if (i == mAudioPortCallbacks.size()) {
1030 return INVALID_OPERATION;
1031 }
1032 mAudioPortCallbacks.removeAt(i);
1033 return NO_ERROR;
1034}
1035
1036void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1037{
1038 Mutex::Autolock _l(mLock);
1039 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1040 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1041 }
1042}
1043
1044void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1045{
1046 Mutex::Autolock _l(mLock);
1047 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1048 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1049 }
1050}
1051
Jean-Michel Trivide801052015-04-14 19:10:14 -07001052void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1053 String8 regId, int32_t state)
1054{
1055 ALOGV("TODO propagate onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1056}
1057
Glenn Kasten4944acb2013-08-19 08:39:20 -07001058void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1059{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001060 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001061 Mutex::Autolock _l(mLock);
1062 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1063 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001064 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001065 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001066 {
1067 Mutex::Autolock _l(gLockAPS);
1068 AudioSystem::gAudioPolicyService.clear();
1069 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001070
Steve Block5ff1dd52012-01-05 23:22:43 +00001071 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001072}
1073
Glenn Kasten40bc9062015-03-20 09:09:33 -07001074} // namespace android