blob: 97fb8411e25ea8432c5147d18115e3a0cf029b6c [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;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070040dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080041
Glenn Kasten2301acc2014-01-17 10:21:00 -080042// Cached values for output handles
Glenn Kasten9ea65d02014-01-17 10:21:24 -080043DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL);
Eric Laurentc2f1f072009-07-17 12:17:14 -070044
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -080045// Cached values for recording queries, all protected by gLock
Glenn Kasten5446e542014-01-08 08:58:53 -080046uint32_t AudioSystem::gPrevInSamplingRate;
47audio_format_t AudioSystem::gPrevInFormat;
48audio_channel_mask_t AudioSystem::gPrevInChannelMask;
49size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080052const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080054 sp<IAudioFlinger> af;
55 sp<AudioFlingerClient> afc;
56 {
57 Mutex::Autolock _l(gLock);
58 if (gAudioFlinger == 0) {
59 sp<IServiceManager> sm = defaultServiceManager();
60 sp<IBinder> binder;
61 do {
62 binder = sm->getService(String16("media.audio_flinger"));
63 if (binder != 0)
64 break;
65 ALOGW("AudioFlinger not published, waiting...");
66 usleep(500000); // 0.5 s
67 } while (true);
68 if (gAudioFlingerClient == NULL) {
69 gAudioFlingerClient = new AudioFlingerClient();
70 } else {
71 if (gAudioErrorCallback) {
72 gAudioErrorCallback(NO_ERROR);
73 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080075 binder->linkToDeath(gAudioFlingerClient);
76 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
77 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
78 afc = gAudioFlingerClient;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070079 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080080 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080082 if (afc != 0) {
83 af->registerClient(afc);
84 }
85 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086}
87
Eric Laurent46291612013-07-18 14:38:44 -070088/* static */ status_t AudioSystem::checkAudioFlinger()
89{
90 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
91 return NO_ERROR;
92 }
93 return DEAD_OBJECT;
94}
95
Glenn Kasten4944acb2013-08-19 08:39:20 -070096status_t AudioSystem::muteMicrophone(bool state)
97{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
99 if (af == 0) return PERMISSION_DENIED;
100 return af->setMicMute(state);
101}
102
Glenn Kasten4944acb2013-08-19 08:39:20 -0700103status_t AudioSystem::isMicrophoneMuted(bool* state)
104{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
106 if (af == 0) return PERMISSION_DENIED;
107 *state = af->getMicMute();
108 return NO_ERROR;
109}
110
111status_t AudioSystem::setMasterVolume(float value)
112{
113 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
114 if (af == 0) return PERMISSION_DENIED;
115 af->setMasterVolume(value);
116 return NO_ERROR;
117}
118
119status_t AudioSystem::setMasterMute(bool mute)
120{
121 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
122 if (af == 0) return PERMISSION_DENIED;
123 af->setMasterMute(mute);
124 return NO_ERROR;
125}
126
127status_t AudioSystem::getMasterVolume(float* volume)
128{
129 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
130 if (af == 0) return PERMISSION_DENIED;
131 *volume = af->masterVolume();
132 return NO_ERROR;
133}
134
135status_t AudioSystem::getMasterMute(bool* mute)
136{
137 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
138 if (af == 0) return PERMISSION_DENIED;
139 *mute = af->masterMute();
140 return NO_ERROR;
141}
142
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800143status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
144 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145{
Dima Zavinfce7a472011-04-19 22:30:36 -0700146 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
148 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700149 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 return NO_ERROR;
151}
152
Glenn Kastenfff6d712012-01-12 16:38:12 -0800153status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154{
Dima Zavinfce7a472011-04-19 22:30:36 -0700155 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
157 if (af == 0) return PERMISSION_DENIED;
158 af->setStreamMute(stream, mute);
159 return NO_ERROR;
160}
161
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800162status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
163 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164{
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
167 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700168 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 return NO_ERROR;
170}
171
Glenn Kastenfff6d712012-01-12 16:38:12 -0800172status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
177 *mute = af->streamMute(stream);
178 return NO_ERROR;
179}
180
Glenn Kastenf78aee72012-01-04 11:00:47 -0800181status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800183 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
185 if (af == 0) return PERMISSION_DENIED;
186 return af->setMode(mode);
187}
188
Glenn Kasten4944acb2013-08-19 08:39:20 -0700189status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
190{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
192 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700193 return af->setParameters(ioHandle, keyValuePairs);
194}
195
Glenn Kasten4944acb2013-08-19 08:39:20 -0700196String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
197{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700198 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
199 String8 result = String8("");
200 if (af == 0) return result;
201
202 result = af->getParameters(ioHandle, keys);
203 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204}
205
Glenn Kastenc23885e2013-12-19 16:35:18 -0800206status_t AudioSystem::setParameters(const String8& keyValuePairs)
207{
Glenn Kasten142f5192014-03-25 17:44:59 -0700208 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800209}
210
211String8 AudioSystem::getParameters(const String8& keys)
212{
Glenn Kasten142f5192014-03-25 17:44:59 -0700213 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800214}
215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216// convert volume steps to natural log scale
217
218// change this value to change volume scaling
219static const float dBPerStep = 0.5f;
220// shouldn't need to touch these
221static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
222static const float dBConvertInverse = 1.0f / dBConvert;
223
224float AudioSystem::linearToLog(int volume)
225{
226 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000227 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 // return v;
229 return volume ? exp(float(100 - volume) * dBConvert) : 0;
230}
231
232int AudioSystem::logToLinear(float volume)
233{
234 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000235 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236 // return v;
237 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
238}
239
Glenn Kasten3b16c762012-11-14 08:44:39 -0800240status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700242 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (streamType == AUDIO_STREAM_DEFAULT) {
245 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700246 }
247
Glenn Kastenfff6d712012-01-12 16:38:12 -0800248 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 if (output == 0) {
250 return PERMISSION_DENIED;
251 }
252
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700253 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700254}
255
256status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800257 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700258{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800259 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
260 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700261
Eric Laurentf6778fd2014-11-18 17:26:58 -0800262 Mutex::Autolock _l(gLockCache);
263
264 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800265 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800267 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 *samplingRate = af->sampleRate(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800269 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 } else {
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 *samplingRate = outputDesc->samplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700273 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800274 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700275 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800276 return BAD_VALUE;
277 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700279 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 return NO_ERROR;
282}
283
Glenn Kastene33054e2012-11-14 12:54:39 -0800284status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 if (streamType == AUDIO_STREAM_DEFAULT) {
289 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700291
Glenn Kastenfff6d712012-01-12 16:38:12 -0800292 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700293 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 return PERMISSION_DENIED;
295 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700297 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700298}
299
300status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800301 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700302{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800303 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
304 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700305
Eric Laurentf6778fd2014-11-18 17:26:58 -0800306 Mutex::Autolock _l(gLockCache);
307
308 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800309 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800310 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 *frameCount = af->frameCount(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800312 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 } else {
314 *frameCount = outputDesc->frameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800316 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700317 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800318 return BAD_VALUE;
319 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700321 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700322
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 return NO_ERROR;
324}
325
Glenn Kastenfff6d712012-01-12 16:38:12 -0800326status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329
Dima Zavinfce7a472011-04-19 22:30:36 -0700330 if (streamType == AUDIO_STREAM_DEFAULT) {
331 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700333
Glenn Kastenfff6d712012-01-12 16:38:12 -0800334 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700335 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336 return PERMISSION_DENIED;
337 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338
Glenn Kasten241618f2014-03-25 17:48:57 -0700339 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700340}
341
342status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700343 uint32_t* latency)
344{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800345 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
346 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700347
Eric Laurentf6778fd2014-11-18 17:26:58 -0800348 Mutex::Autolock _l(gLockCache);
349
350 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800351 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800352 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 *latency = af->latency(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800354 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 } else {
356 *latency = outputDesc->latency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 }
358
Glenn Kasten241618f2014-03-25 17:48:57 -0700359 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 return NO_ERROR;
362}
363
Glenn Kastendd8104c2012-07-02 12:42:44 -0700364status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
365 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800367 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
368 if (af == 0) {
369 return PERMISSION_DENIED;
370 }
371 Mutex::Autolock _l(gLockCache);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800373 size_t inBuffSize = gInBuffSize;
374 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700375 || (channelMask != gPrevInChannelMask)) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800376 gLockCache.unlock();
Glenn Kastendd8104c2012-07-02 12:42:44 -0700377 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800378 gLockCache.lock();
Glenn Kasten5446e542014-01-08 08:58:53 -0800379 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800380 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800381 sampleRate, format, channelMask);
382 return BAD_VALUE;
383 }
384 // A benign race is possible here: we could overwrite a fresher cache entry
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 // save the request params
386 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700388 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800390 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700391 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800392 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700393
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 return NO_ERROR;
395}
396
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700397status_t AudioSystem::setVoiceVolume(float value)
398{
399 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
400 if (af == 0) return PERMISSION_DENIED;
401 return af->setVoiceVolume(value);
402}
403
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000404status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700405 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800406{
407 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
408 if (af == 0) return PERMISSION_DENIED;
409
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000410 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800411}
412
Glenn Kasten4944acb2013-08-19 08:39:20 -0700413uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
414{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800415 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800416 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800417 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700418 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800419
420 result = af->getInputFramesLost(ioHandle);
421 return result;
422}
423
Eric Laurentde3f8392014-07-27 18:38:22 -0700424audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700425{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700426 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700427 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
428 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700429}
430
Marco Nelissend457c972014-02-11 08:47:07 -0800431void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700432{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700433 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
434 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800435 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700436 }
437}
438
Marco Nelissend457c972014-02-11 08:47:07 -0800439void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700440{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700441 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
442 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800443 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700444 }
445}
446
Eric Laurent93c3d412014-08-01 14:48:35 -0700447audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
448{
449 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
450 if (af == 0) return AUDIO_HW_SYNC_INVALID;
451 return af->getAudioHwSyncForSession(sessionId);
452}
453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454// ---------------------------------------------------------------------------
455
Glenn Kasten4944acb2013-08-19 08:39:20 -0700456void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
457{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800458 audio_error_callback cb = NULL;
459 {
460 Mutex::Autolock _l(AudioSystem::gLock);
461 AudioSystem::gAudioFlinger.clear();
462 cb = gAudioErrorCallback;
463 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464
Eric Laurentf6778fd2014-11-18 17:26:58 -0800465 {
466 // clear output handles and stream to output map caches
467 Mutex::Autolock _l(gLockCache);
468 AudioSystem::gOutputs.clear();
469 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470
Eric Laurentf6778fd2014-11-18 17:26:58 -0800471 if (cb) {
472 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000474 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475}
476
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800477void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800478 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800480 const OutputDescriptor *desc;
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
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700540/*static*/ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700541{
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
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700546/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
547{
548 Mutex::Autolock _l(gLock);
549 gDynPolicyCallback = cb;
550}
551
Eric Laurentc2f1f072009-07-17 12:17:14 -0700552// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800553// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700554sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
555sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
556
557
Glenn Kasten18a6d902012-09-24 11:27:56 -0700558// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800559const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700560{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800561 sp<IAudioPolicyService> ap;
562 sp<AudioPolicyServiceClient> apc;
563 {
564 Mutex::Autolock _l(gLockAPS);
565 if (gAudioPolicyService == 0) {
566 sp<IServiceManager> sm = defaultServiceManager();
567 sp<IBinder> binder;
568 do {
569 binder = sm->getService(String16("media.audio_policy"));
570 if (binder != 0)
571 break;
572 ALOGW("AudioPolicyService not published, waiting...");
573 usleep(500000); // 0.5 s
574 } while (true);
575 if (gAudioPolicyServiceClient == NULL) {
576 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
577 }
578 binder->linkToDeath(gAudioPolicyServiceClient);
579 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
580 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
581 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700582 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800583 ap = gAudioPolicyService;
584 }
585 if (apc != 0) {
586 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700587 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800588
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800589 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700590}
591
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700592// ---------------------------------------------------------------------------
593
Dima Zavinfce7a472011-04-19 22:30:36 -0700594status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
595 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800596 const char *device_address,
597 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598{
599 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700600 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800601 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700602
Eric Laurentc2f1f072009-07-17 12:17:14 -0700603 if (aps == 0) return PERMISSION_DENIED;
604
Eric Laurent71b63e32011-09-02 14:20:56 -0700605 if (device_address != NULL) {
606 address = device_address;
607 }
Paul McLeane743a472015-01-28 11:07:31 -0800608 if (device_name != NULL) {
609 name = device_name;
610 }
611 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700612}
613
Dima Zavinfce7a472011-04-19 22:30:36 -0700614audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700615 const char *device_address)
616{
617 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700618 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619
620 return aps->getDeviceConnectionState(device, device_address);
621}
622
Glenn Kastenf78aee72012-01-04 11:00:47 -0800623status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700624{
Glenn Kasten347966c2012-01-18 14:58:32 -0800625 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700626 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
627 if (aps == 0) return PERMISSION_DENIED;
628
629 return aps->setPhoneState(state);
630}
631
Dima Zavinfce7a472011-04-19 22:30:36 -0700632status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700633{
634 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
635 if (aps == 0) return PERMISSION_DENIED;
636 return aps->setForceUse(usage, config);
637}
638
Dima Zavinfce7a472011-04-19 22:30:36 -0700639audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700640{
641 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700642 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700643 return aps->getForceUse(usage);
644}
645
646
Dima Zavinfce7a472011-04-19 22:30:36 -0700647audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700648 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800649 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700650 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000651 audio_output_flags_t flags,
652 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700653{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700654 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
655 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000656 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700657}
658
Eric Laurente83b55d2014-11-14 10:06:21 -0800659status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
660 audio_io_handle_t *output,
661 audio_session_t session,
662 audio_stream_type_t *stream,
663 uint32_t samplingRate,
664 audio_format_t format,
665 audio_channel_mask_t channelMask,
666 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700667 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800668 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700669{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800671 if (aps == 0) return NO_INIT;
672 return aps->getOutputForAttr(attr, output, session, stream,
673 samplingRate, format, channelMask,
Paul McLeanaa981192015-03-21 09:55:15 -0700674 flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700675}
676
Eric Laurentde070132010-07-13 04:45:46 -0700677status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700678 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800679 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700680{
681 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
682 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700683 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700684}
685
Eric Laurentde070132010-07-13 04:45:46 -0700686status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700687 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800688 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700689{
690 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
691 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700692 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700693}
694
Eric Laurente83b55d2014-11-14 10:06:21 -0800695void AudioSystem::releaseOutput(audio_io_handle_t output,
696 audio_stream_type_t stream,
697 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700698{
699 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
700 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800701 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700702}
703
Eric Laurentcaf7f482014-11-25 17:50:47 -0800704status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
705 audio_io_handle_t *input,
706 audio_session_t session,
707 uint32_t samplingRate,
708 audio_format_t format,
709 audio_channel_mask_t channelMask,
710 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700711{
712 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800713 if (aps == 0) return NO_INIT;
714 return aps->getInputForAttr(attr, input, session, samplingRate, format, channelMask, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700715}
716
Eric Laurent4dc68062014-07-28 17:26:49 -0700717status_t AudioSystem::startInput(audio_io_handle_t input,
718 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700719{
720 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
721 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700722 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700723}
724
Eric Laurent4dc68062014-07-28 17:26:49 -0700725status_t AudioSystem::stopInput(audio_io_handle_t input,
726 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700727{
728 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
729 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700730 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700731}
732
Eric Laurent4dc68062014-07-28 17:26:49 -0700733void AudioSystem::releaseInput(audio_io_handle_t input,
734 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700735{
736 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
737 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700738 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700739}
740
Dima Zavinfce7a472011-04-19 22:30:36 -0700741status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700742 int indexMin,
743 int indexMax)
744{
745 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
746 if (aps == 0) return PERMISSION_DENIED;
747 return aps->initStreamVolume(stream, indexMin, indexMax);
748}
749
Eric Laurent83844cc2011-11-18 16:43:31 -0800750status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
751 int index,
752 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700753{
754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
755 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800756 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757}
758
Eric Laurent83844cc2011-11-18 16:43:31 -0800759status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
760 int *index,
761 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700762{
763 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
764 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800765 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700766}
767
Dima Zavinfce7a472011-04-19 22:30:36 -0700768uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700769{
770 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
771 if (aps == 0) return 0;
772 return aps->getStrategyForStream(stream);
773}
774
Eric Laurent63742522012-03-08 13:42:42 -0800775audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800776{
777 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800778 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800779 return aps->getDevicesForStream(stream);
780}
781
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700782audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700783{
784 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800785 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700786 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700787 return aps->getOutputForEffect(desc);
788}
789
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700790status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700791 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700792 uint32_t strategy,
793 int session,
794 int id)
795{
796 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
797 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700798 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700799}
800
801status_t AudioSystem::unregisterEffect(int id)
802{
803 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
804 if (aps == 0) return PERMISSION_DENIED;
805 return aps->unregisterEffect(id);
806}
807
Eric Laurentdb7c0792011-08-10 10:37:50 -0700808status_t AudioSystem::setEffectEnabled(int id, bool enabled)
809{
810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
811 if (aps == 0) return PERMISSION_DENIED;
812 return aps->setEffectEnabled(id, enabled);
813}
814
Glenn Kastenfff6d712012-01-12 16:38:12 -0800815status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700816{
Eric Laurenteda6c362011-02-02 09:33:30 -0800817 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
818 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700819 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800820 *state = aps->isStreamActive(stream, inPastMs);
821 return NO_ERROR;
822}
823
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800824status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
825 uint32_t inPastMs)
826{
827 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
828 if (aps == 0) return PERMISSION_DENIED;
829 if (state == NULL) return BAD_VALUE;
830 *state = aps->isStreamActiveRemotely(stream, inPastMs);
831 return NO_ERROR;
832}
833
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700834status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
835{
836 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
837 if (aps == 0) return PERMISSION_DENIED;
838 if (state == NULL) return BAD_VALUE;
839 *state = aps->isSourceActive(stream);
840 return NO_ERROR;
841}
842
Glenn Kasten3b16c762012-11-14 08:44:39 -0800843uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700844{
845 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
846 if (af == 0) return 0;
847 return af->getPrimaryOutputSamplingRate();
848}
849
Glenn Kastene33054e2012-11-14 12:54:39 -0800850size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700851{
852 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
853 if (af == 0) return 0;
854 return af->getPrimaryOutputFrameCount();
855}
Eric Laurenteda6c362011-02-02 09:33:30 -0800856
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700857status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
858{
859 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
860 if (af == 0) return PERMISSION_DENIED;
861 return af->setLowRamDevice(isLowRamDevice);
862}
863
Eric Laurent9f6530f2011-08-30 10:18:54 -0700864void AudioSystem::clearAudioConfigCache()
865{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800866 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100867 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800868 {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800869 Mutex::Autolock _l(gLockCache);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800870 gOutputs.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800871 }
872 {
873 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800874 gAudioFlinger.clear();
875 }
876 {
877 Mutex::Autolock _l(gLockAPS);
878 gAudioPolicyService.clear();
879 }
Eric Laurent9f6530f2011-08-30 10:18:54 -0700880}
881
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000882bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
883{
884 ALOGV("isOffloadSupported()");
885 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
886 if (aps == 0) return false;
887 return aps->isOffloadSupported(info);
888}
889
Eric Laurent203b1a12014-04-01 10:34:16 -0700890status_t AudioSystem::listAudioPorts(audio_port_role_t role,
891 audio_port_type_t type,
892 unsigned int *num_ports,
893 struct audio_port *ports,
894 unsigned int *generation)
895{
896 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
897 if (aps == 0) return PERMISSION_DENIED;
898 return aps->listAudioPorts(role, type, num_ports, ports, generation);
899}
900
901status_t AudioSystem::getAudioPort(struct audio_port *port)
902{
903 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
904 if (aps == 0) return PERMISSION_DENIED;
905 return aps->getAudioPort(port);
906}
907
908status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
909 audio_patch_handle_t *handle)
910{
911 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
912 if (aps == 0) return PERMISSION_DENIED;
913 return aps->createAudioPatch(patch, handle);
914}
915
916status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
917{
918 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
919 if (aps == 0) return PERMISSION_DENIED;
920 return aps->releaseAudioPatch(handle);
921}
922
923status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
924 struct audio_patch *patches,
925 unsigned int *generation)
926{
927 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
928 if (aps == 0) return PERMISSION_DENIED;
929 return aps->listAudioPatches(num_patches, patches, generation);
930}
931
932status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
933{
934 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
935 if (aps == 0) return PERMISSION_DENIED;
936 return aps->setAudioPortConfig(config);
937}
938
Eric Laurentb28753e2015-04-01 13:06:28 -0700939status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callBack)
Eric Laurentb52c1522014-05-20 11:27:36 -0700940{
Eric Laurentb28753e2015-04-01 13:06:28 -0700941 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
942 if (aps == 0) return PERMISSION_DENIED;
943
944 Mutex::Autolock _l(gLockAPS);
945 if (gAudioPolicyServiceClient == 0) {
946 return NO_INIT;
947 }
948 return gAudioPolicyServiceClient->addAudioPortCallback(callBack);
Eric Laurentb52c1522014-05-20 11:27:36 -0700949}
950
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700951/*static*/
Eric Laurentb28753e2015-04-01 13:06:28 -0700952status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callBack)
953{
954 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
955 if (aps == 0) return PERMISSION_DENIED;
956
957 Mutex::Autolock _l(gLockAPS);
958 if (gAudioPolicyServiceClient == 0) {
959 return NO_INIT;
960 }
961 return gAudioPolicyServiceClient->removeAudioPortCallback(callBack);
962}
963
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700964status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
965 audio_io_handle_t *ioHandle,
966 audio_devices_t *device)
967{
968 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
969 if (aps == 0) return PERMISSION_DENIED;
970 return aps->acquireSoundTriggerSession(session, ioHandle, device);
971}
972
973status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
974{
975 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
976 if (aps == 0) return PERMISSION_DENIED;
977 return aps->releaseSoundTriggerSession(session);
978}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700979
980audio_mode_t AudioSystem::getPhoneState()
981{
982 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
983 if (aps == 0) return AUDIO_MODE_INVALID;
984 return aps->getPhoneState();
985}
986
Eric Laurentbaac1832014-12-01 17:52:59 -0800987status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
988{
989 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
990 if (aps == 0) return PERMISSION_DENIED;
991 return aps->registerPolicyMixes(mixes, registration);
992}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700993
Eric Laurent554a2772015-04-10 11:29:24 -0700994status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
995 const audio_attributes_t *attributes,
996 audio_io_handle_t *handle)
997{
998 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
999 if (aps == 0) return PERMISSION_DENIED;
1000 return aps->startAudioSource(source, attributes, handle);
1001}
1002
1003status_t AudioSystem::stopAudioSource(audio_io_handle_t handle)
1004{
1005 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1006 if (aps == 0) return PERMISSION_DENIED;
1007 return aps->stopAudioSource(handle);
1008}
1009
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010// ---------------------------------------------------------------------------
1011
Eric Laurentb28753e2015-04-01 13:06:28 -07001012status_t AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1013 const sp<AudioPortCallback>& callBack)
1014{
1015 Mutex::Autolock _l(mLock);
1016 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1017 if (mAudioPortCallbacks[i] == callBack) {
1018 return INVALID_OPERATION;
1019 }
1020 }
1021 mAudioPortCallbacks.add(callBack);
1022 return NO_ERROR;
1023}
1024
1025status_t AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1026 const sp<AudioPortCallback>& callBack)
1027{
1028 Mutex::Autolock _l(mLock);
1029 size_t i;
1030 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1031 if (mAudioPortCallbacks[i] == callBack) {
1032 break;
1033 }
1034 }
1035 if (i == mAudioPortCallbacks.size()) {
1036 return INVALID_OPERATION;
1037 }
1038 mAudioPortCallbacks.removeAt(i);
1039 return NO_ERROR;
1040}
1041
1042void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1043{
1044 Mutex::Autolock _l(mLock);
1045 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1046 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1047 }
1048}
1049
1050void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1051{
1052 Mutex::Autolock _l(mLock);
1053 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1054 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1055 }
1056}
1057
Jean-Michel Trivide801052015-04-14 19:10:14 -07001058void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1059 String8 regId, int32_t state)
1060{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001061 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1062 dynamic_policy_callback cb = NULL;
1063 {
1064 Mutex::Autolock _l(AudioSystem::gLock);
1065 cb = gDynPolicyCallback;
1066 }
1067
1068 if (cb != NULL) {
1069 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1070 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001071}
1072
Glenn Kasten4944acb2013-08-19 08:39:20 -07001073void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1074{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001075 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001076 Mutex::Autolock _l(mLock);
1077 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1078 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001079 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001080 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001081 {
1082 Mutex::Autolock _l(gLockAPS);
1083 AudioSystem::gAudioPolicyService.clear();
1084 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001085
Steve Block5ff1dd52012-01-05 23:22:43 +00001086 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001087}
1088
Glenn Kasten40bc9062015-03-20 09:09:33 -07001089} // namespace android