blob: 3478441d4d4378ec4e54083cf4c9db7d5758fe07 [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;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700480
Glenn Kasten142f5192014-03-25 17:44:59 -0700481 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700482
Eric Laurentf6778fd2014-11-18 17:26:58 -0800483 Mutex::Autolock _l(AudioSystem::gLockCache);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484
485 switch (event) {
486 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700487 break;
488 case OUTPUT_OPENED: {
489 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100490 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700491 break;
492 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800493 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800494 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700495
496 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
497 gOutputs.add(ioHandle, outputDesc);
Glenn Kastenb187de12014-12-30 08:18:15 -0800498 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x "
499 "frameCount %zu latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700500 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700501 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700502 } break;
503 case OUTPUT_CLOSED: {
504 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800505 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700506 break;
507 }
Steve Block3856b092011-10-20 11:56:00 +0100508 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700509
510 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700511 } break;
512
513 case OUTPUT_CONFIG_CHANGED: {
514 int index = gOutputs.indexOfKey(ioHandle);
515 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800516 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517 break;
518 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800519 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800520 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521
Glenn Kastenb187de12014-12-30 08:18:15 -0800522 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x "
523 "channel mask %#x frameCount %zu latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700525 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700526 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
527 delete outputDesc;
528 outputDesc = new OutputDescriptor(*desc);
529 gOutputs.replaceValueFor(ioHandle, outputDesc);
530 } break;
531 case INPUT_OPENED:
532 case INPUT_CLOSED:
533 case INPUT_CONFIG_CHANGED:
534 break;
535
536 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800537}
538
Glenn Kasten4944acb2013-08-19 08:39:20 -0700539void AudioSystem::setErrorCallback(audio_error_callback cb)
540{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700541 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 gAudioErrorCallback = cb;
543}
544
Eric Laurentc2f1f072009-07-17 12:17:14 -0700545// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800546// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700547sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
548sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
549
550
Glenn Kasten18a6d902012-09-24 11:27:56 -0700551// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800552const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700553{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800554 sp<IAudioPolicyService> ap;
555 sp<AudioPolicyServiceClient> apc;
556 {
557 Mutex::Autolock _l(gLockAPS);
558 if (gAudioPolicyService == 0) {
559 sp<IServiceManager> sm = defaultServiceManager();
560 sp<IBinder> binder;
561 do {
562 binder = sm->getService(String16("media.audio_policy"));
563 if (binder != 0)
564 break;
565 ALOGW("AudioPolicyService not published, waiting...");
566 usleep(500000); // 0.5 s
567 } while (true);
568 if (gAudioPolicyServiceClient == NULL) {
569 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
570 }
571 binder->linkToDeath(gAudioPolicyServiceClient);
572 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
573 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
574 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700575 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800576 ap = gAudioPolicyService;
577 }
578 if (apc != 0) {
579 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700580 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800581
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800582 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700583}
584
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700585// ---------------------------------------------------------------------------
586
Dima Zavinfce7a472011-04-19 22:30:36 -0700587status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
588 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800589 const char *device_address,
590 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700591{
592 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700593 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800594 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700595
Eric Laurentc2f1f072009-07-17 12:17:14 -0700596 if (aps == 0) return PERMISSION_DENIED;
597
Eric Laurent71b63e32011-09-02 14:20:56 -0700598 if (device_address != NULL) {
599 address = device_address;
600 }
Paul McLeane743a472015-01-28 11:07:31 -0800601 if (device_name != NULL) {
602 name = device_name;
603 }
604 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700605}
606
Dima Zavinfce7a472011-04-19 22:30:36 -0700607audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700608 const char *device_address)
609{
610 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700611 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700612
613 return aps->getDeviceConnectionState(device, device_address);
614}
615
Glenn Kastenf78aee72012-01-04 11:00:47 -0800616status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700617{
Glenn Kasten347966c2012-01-18 14:58:32 -0800618 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
620 if (aps == 0) return PERMISSION_DENIED;
621
622 return aps->setPhoneState(state);
623}
624
Dima Zavinfce7a472011-04-19 22:30:36 -0700625status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700626{
627 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
628 if (aps == 0) return PERMISSION_DENIED;
629 return aps->setForceUse(usage, config);
630}
631
Dima Zavinfce7a472011-04-19 22:30:36 -0700632audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700633{
634 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700635 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700636 return aps->getForceUse(usage);
637}
638
639
Dima Zavinfce7a472011-04-19 22:30:36 -0700640audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800642 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700643 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000644 audio_output_flags_t flags,
645 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700647 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
648 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000649 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700650}
651
Eric Laurente83b55d2014-11-14 10:06:21 -0800652status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
653 audio_io_handle_t *output,
654 audio_session_t session,
655 audio_stream_type_t *stream,
656 uint32_t samplingRate,
657 audio_format_t format,
658 audio_channel_mask_t channelMask,
659 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700660 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800661 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700662{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700663 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800664 if (aps == 0) return NO_INIT;
665 return aps->getOutputForAttr(attr, output, session, stream,
666 samplingRate, format, channelMask,
Paul McLeanaa981192015-03-21 09:55:15 -0700667 flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700668}
669
Eric Laurentde070132010-07-13 04:45:46 -0700670status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700671 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800672 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700673{
674 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
675 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700676 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700677}
678
Eric Laurentde070132010-07-13 04:45:46 -0700679status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700680 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800681 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700682{
683 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
684 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700685 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686}
687
Eric Laurente83b55d2014-11-14 10:06:21 -0800688void AudioSystem::releaseOutput(audio_io_handle_t output,
689 audio_stream_type_t stream,
690 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700691{
692 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
693 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800694 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700695}
696
Eric Laurentcaf7f482014-11-25 17:50:47 -0800697status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
698 audio_io_handle_t *input,
699 audio_session_t session,
700 uint32_t samplingRate,
701 audio_format_t format,
702 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600703 audio_input_flags_t flags,
704 audio_port_handle_t selectedDeviceId)
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;
Paul McLean466dc8e2015-04-17 13:15:36 -0600708 return aps->getInputForAttr(
709 attr, input, session, samplingRate, format, channelMask, flags, selectedDeviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700710}
711
Eric Laurent4dc68062014-07-28 17:26:49 -0700712status_t AudioSystem::startInput(audio_io_handle_t input,
713 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700714{
715 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
716 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700717 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718}
719
Eric Laurent4dc68062014-07-28 17:26:49 -0700720status_t AudioSystem::stopInput(audio_io_handle_t input,
721 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700722{
723 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
724 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700725 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700726}
727
Eric Laurent4dc68062014-07-28 17:26:49 -0700728void AudioSystem::releaseInput(audio_io_handle_t input,
729 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700730{
731 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
732 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700733 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700734}
735
Dima Zavinfce7a472011-04-19 22:30:36 -0700736status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700737 int indexMin,
738 int indexMax)
739{
740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
741 if (aps == 0) return PERMISSION_DENIED;
742 return aps->initStreamVolume(stream, indexMin, indexMax);
743}
744
Eric Laurent83844cc2011-11-18 16:43:31 -0800745status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
746 int index,
747 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700748{
749 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
750 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800751 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700752}
753
Eric Laurent83844cc2011-11-18 16:43:31 -0800754status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
755 int *index,
756 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757{
758 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
759 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800760 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700761}
762
Dima Zavinfce7a472011-04-19 22:30:36 -0700763uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700764{
765 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
766 if (aps == 0) return 0;
767 return aps->getStrategyForStream(stream);
768}
769
Eric Laurent63742522012-03-08 13:42:42 -0800770audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800771{
772 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800773 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800774 return aps->getDevicesForStream(stream);
775}
776
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700777audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700778{
779 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800780 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700781 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700782 return aps->getOutputForEffect(desc);
783}
784
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700785status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700786 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700787 uint32_t strategy,
788 int session,
789 int id)
790{
791 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
792 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700793 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700794}
795
796status_t AudioSystem::unregisterEffect(int id)
797{
798 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
799 if (aps == 0) return PERMISSION_DENIED;
800 return aps->unregisterEffect(id);
801}
802
Eric Laurentdb7c0792011-08-10 10:37:50 -0700803status_t AudioSystem::setEffectEnabled(int id, bool enabled)
804{
805 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
806 if (aps == 0) return PERMISSION_DENIED;
807 return aps->setEffectEnabled(id, enabled);
808}
809
Glenn Kastenfff6d712012-01-12 16:38:12 -0800810status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700811{
Eric Laurenteda6c362011-02-02 09:33:30 -0800812 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
813 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700814 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800815 *state = aps->isStreamActive(stream, inPastMs);
816 return NO_ERROR;
817}
818
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800819status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
820 uint32_t inPastMs)
821{
822 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
823 if (aps == 0) return PERMISSION_DENIED;
824 if (state == NULL) return BAD_VALUE;
825 *state = aps->isStreamActiveRemotely(stream, inPastMs);
826 return NO_ERROR;
827}
828
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700829status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
830{
831 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
832 if (aps == 0) return PERMISSION_DENIED;
833 if (state == NULL) return BAD_VALUE;
834 *state = aps->isSourceActive(stream);
835 return NO_ERROR;
836}
837
Glenn Kasten3b16c762012-11-14 08:44:39 -0800838uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700839{
840 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
841 if (af == 0) return 0;
842 return af->getPrimaryOutputSamplingRate();
843}
844
Glenn Kastene33054e2012-11-14 12:54:39 -0800845size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700846{
847 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
848 if (af == 0) return 0;
849 return af->getPrimaryOutputFrameCount();
850}
Eric Laurenteda6c362011-02-02 09:33:30 -0800851
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700852status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
853{
854 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
855 if (af == 0) return PERMISSION_DENIED;
856 return af->setLowRamDevice(isLowRamDevice);
857}
858
Eric Laurent9f6530f2011-08-30 10:18:54 -0700859void AudioSystem::clearAudioConfigCache()
860{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800861 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800863 {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800864 Mutex::Autolock _l(gLockCache);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800865 gOutputs.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800866 }
867 {
868 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800869 gAudioFlinger.clear();
870 }
871 {
872 Mutex::Autolock _l(gLockAPS);
873 gAudioPolicyService.clear();
874 }
Eric Laurent9f6530f2011-08-30 10:18:54 -0700875}
876
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000877bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
878{
879 ALOGV("isOffloadSupported()");
880 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
881 if (aps == 0) return false;
882 return aps->isOffloadSupported(info);
883}
884
Eric Laurent203b1a12014-04-01 10:34:16 -0700885status_t AudioSystem::listAudioPorts(audio_port_role_t role,
886 audio_port_type_t type,
887 unsigned int *num_ports,
888 struct audio_port *ports,
889 unsigned int *generation)
890{
891 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
892 if (aps == 0) return PERMISSION_DENIED;
893 return aps->listAudioPorts(role, type, num_ports, ports, generation);
894}
895
896status_t AudioSystem::getAudioPort(struct audio_port *port)
897{
898 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
899 if (aps == 0) return PERMISSION_DENIED;
900 return aps->getAudioPort(port);
901}
902
903status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
904 audio_patch_handle_t *handle)
905{
906 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
907 if (aps == 0) return PERMISSION_DENIED;
908 return aps->createAudioPatch(patch, handle);
909}
910
911status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
912{
913 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
914 if (aps == 0) return PERMISSION_DENIED;
915 return aps->releaseAudioPatch(handle);
916}
917
918status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
919 struct audio_patch *patches,
920 unsigned int *generation)
921{
922 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
923 if (aps == 0) return PERMISSION_DENIED;
924 return aps->listAudioPatches(num_patches, patches, generation);
925}
926
927status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
928{
929 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
930 if (aps == 0) return PERMISSION_DENIED;
931 return aps->setAudioPortConfig(config);
932}
933
Eric Laurentb28753e2015-04-01 13:06:28 -0700934status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callBack)
Eric Laurentb52c1522014-05-20 11:27:36 -0700935{
Eric Laurentb28753e2015-04-01 13:06:28 -0700936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
937 if (aps == 0) return PERMISSION_DENIED;
938
939 Mutex::Autolock _l(gLockAPS);
940 if (gAudioPolicyServiceClient == 0) {
941 return NO_INIT;
942 }
943 return gAudioPolicyServiceClient->addAudioPortCallback(callBack);
Eric Laurentb52c1522014-05-20 11:27:36 -0700944}
945
Eric Laurentb28753e2015-04-01 13:06:28 -0700946status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callBack)
947{
948 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
949 if (aps == 0) return PERMISSION_DENIED;
950
951 Mutex::Autolock _l(gLockAPS);
952 if (gAudioPolicyServiceClient == 0) {
953 return NO_INIT;
954 }
955 return gAudioPolicyServiceClient->removeAudioPortCallback(callBack);
956}
957
958
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700959status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
960 audio_io_handle_t *ioHandle,
961 audio_devices_t *device)
962{
963 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
964 if (aps == 0) return PERMISSION_DENIED;
965 return aps->acquireSoundTriggerSession(session, ioHandle, device);
966}
967
968status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
969{
970 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
971 if (aps == 0) return PERMISSION_DENIED;
972 return aps->releaseSoundTriggerSession(session);
973}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700974
975audio_mode_t AudioSystem::getPhoneState()
976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return AUDIO_MODE_INVALID;
979 return aps->getPhoneState();
980}
981
Eric Laurentbaac1832014-12-01 17:52:59 -0800982status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
983{
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
985 if (aps == 0) return PERMISSION_DENIED;
986 return aps->registerPolicyMixes(mixes, registration);
987}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700988
Eric Laurent554a2772015-04-10 11:29:24 -0700989status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
990 const audio_attributes_t *attributes,
991 audio_io_handle_t *handle)
992{
993 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
994 if (aps == 0) return PERMISSION_DENIED;
995 return aps->startAudioSource(source, attributes, handle);
996}
997
998status_t AudioSystem::stopAudioSource(audio_io_handle_t handle)
999{
1000 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1001 if (aps == 0) return PERMISSION_DENIED;
1002 return aps->stopAudioSource(handle);
1003}
1004
Eric Laurentc2f1f072009-07-17 12:17:14 -07001005// ---------------------------------------------------------------------------
1006
Eric Laurentb28753e2015-04-01 13:06:28 -07001007status_t AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1008 const sp<AudioPortCallback>& callBack)
1009{
1010 Mutex::Autolock _l(mLock);
1011 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1012 if (mAudioPortCallbacks[i] == callBack) {
1013 return INVALID_OPERATION;
1014 }
1015 }
1016 mAudioPortCallbacks.add(callBack);
1017 return NO_ERROR;
1018}
1019
1020status_t AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1021 const sp<AudioPortCallback>& callBack)
1022{
1023 Mutex::Autolock _l(mLock);
1024 size_t i;
1025 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1026 if (mAudioPortCallbacks[i] == callBack) {
1027 break;
1028 }
1029 }
1030 if (i == mAudioPortCallbacks.size()) {
1031 return INVALID_OPERATION;
1032 }
1033 mAudioPortCallbacks.removeAt(i);
1034 return NO_ERROR;
1035}
1036
1037void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1038{
1039 Mutex::Autolock _l(mLock);
1040 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1041 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1042 }
1043}
1044
1045void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1046{
1047 Mutex::Autolock _l(mLock);
1048 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1049 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1050 }
1051}
1052
Jean-Michel Trivide801052015-04-14 19:10:14 -07001053void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1054 String8 regId, int32_t state)
1055{
1056 ALOGV("TODO propagate onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1057}
1058
Glenn Kasten4944acb2013-08-19 08:39:20 -07001059void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1060{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001061 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001062 Mutex::Autolock _l(mLock);
1063 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1064 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001065 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001066 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001067 {
1068 Mutex::Autolock _l(gLockAPS);
1069 AudioSystem::gAudioPolicyService.clear();
1070 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001071
Steve Block5ff1dd52012-01-05 23:22:43 +00001072 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001073}
1074
Glenn Kasten40bc9062015-03-20 09:09:33 -07001075} // namespace android