blob: 0e608f866fd81736ff8fbcc8e0dba24f83c056a4 [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;
37Mutex AudioSystem::gLockAPC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080038sp<IAudioFlinger> AudioSystem::gAudioFlinger;
39sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
40audio_error_callback AudioSystem::gAudioErrorCallback = 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
Eric Laurentb52c1522014-05-20 11:27:36 -070051sp<AudioSystem::AudioPortCallback> AudioSystem::gAudioPortCallback;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052
53// establish binder interface to AudioFlinger service
54const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
55{
56 Mutex::Autolock _l(gLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080057 if (gAudioFlinger == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080058 sp<IServiceManager> sm = defaultServiceManager();
59 sp<IBinder> binder;
60 do {
61 binder = sm->getService(String16("media.audio_flinger"));
62 if (binder != 0)
63 break;
Steve Block5ff1dd52012-01-05 23:22:43 +000064 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070066 } while (true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067 if (gAudioFlingerClient == NULL) {
68 gAudioFlingerClient = new AudioFlingerClient();
69 } else {
70 if (gAudioErrorCallback) {
71 gAudioErrorCallback(NO_ERROR);
72 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -070073 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080074 binder->linkToDeath(gAudioFlingerClient);
75 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
Glenn Kastend2d089f2014-11-05 11:48:12 -080076 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078 }
Eric Laurentc2f1f072009-07-17 12:17:14 -070079
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 return gAudioFlinger;
81}
82
Eric Laurent46291612013-07-18 14:38:44 -070083/* static */ status_t AudioSystem::checkAudioFlinger()
84{
85 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
86 return NO_ERROR;
87 }
88 return DEAD_OBJECT;
89}
90
Glenn Kasten4944acb2013-08-19 08:39:20 -070091status_t AudioSystem::muteMicrophone(bool state)
92{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
94 if (af == 0) return PERMISSION_DENIED;
95 return af->setMicMute(state);
96}
97
Glenn Kasten4944acb2013-08-19 08:39:20 -070098status_t AudioSystem::isMicrophoneMuted(bool* state)
99{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
101 if (af == 0) return PERMISSION_DENIED;
102 *state = af->getMicMute();
103 return NO_ERROR;
104}
105
106status_t AudioSystem::setMasterVolume(float value)
107{
108 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
109 if (af == 0) return PERMISSION_DENIED;
110 af->setMasterVolume(value);
111 return NO_ERROR;
112}
113
114status_t AudioSystem::setMasterMute(bool mute)
115{
116 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
117 if (af == 0) return PERMISSION_DENIED;
118 af->setMasterMute(mute);
119 return NO_ERROR;
120}
121
122status_t AudioSystem::getMasterVolume(float* volume)
123{
124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
125 if (af == 0) return PERMISSION_DENIED;
126 *volume = af->masterVolume();
127 return NO_ERROR;
128}
129
130status_t AudioSystem::getMasterMute(bool* mute)
131{
132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
133 if (af == 0) return PERMISSION_DENIED;
134 *mute = af->masterMute();
135 return NO_ERROR;
136}
137
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800138status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
139 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140{
Dima Zavinfce7a472011-04-19 22:30:36 -0700141 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
143 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700144 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 return NO_ERROR;
146}
147
Glenn Kastenfff6d712012-01-12 16:38:12 -0800148status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149{
Dima Zavinfce7a472011-04-19 22:30:36 -0700150 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
152 if (af == 0) return PERMISSION_DENIED;
153 af->setStreamMute(stream, mute);
154 return NO_ERROR;
155}
156
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800157status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
158 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159{
Dima Zavinfce7a472011-04-19 22:30:36 -0700160 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
162 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700163 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 return NO_ERROR;
165}
166
Glenn Kastenfff6d712012-01-12 16:38:12 -0800167status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
171 if (af == 0) return PERMISSION_DENIED;
172 *mute = af->streamMute(stream);
173 return NO_ERROR;
174}
175
Glenn Kastenf78aee72012-01-04 11:00:47 -0800176status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800178 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
180 if (af == 0) return PERMISSION_DENIED;
181 return af->setMode(mode);
182}
183
Glenn Kasten4944acb2013-08-19 08:39:20 -0700184status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
185{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
187 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700188 return af->setParameters(ioHandle, keyValuePairs);
189}
190
Glenn Kasten4944acb2013-08-19 08:39:20 -0700191String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
192{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700193 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
194 String8 result = String8("");
195 if (af == 0) return result;
196
197 result = af->getParameters(ioHandle, keys);
198 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199}
200
Glenn Kastenc23885e2013-12-19 16:35:18 -0800201status_t AudioSystem::setParameters(const String8& keyValuePairs)
202{
Glenn Kasten142f5192014-03-25 17:44:59 -0700203 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800204}
205
206String8 AudioSystem::getParameters(const String8& keys)
207{
Glenn Kasten142f5192014-03-25 17:44:59 -0700208 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800209}
210
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211// convert volume steps to natural log scale
212
213// change this value to change volume scaling
214static const float dBPerStep = 0.5f;
215// shouldn't need to touch these
216static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
217static const float dBConvertInverse = 1.0f / dBConvert;
218
219float AudioSystem::linearToLog(int volume)
220{
221 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000222 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 // return v;
224 return volume ? exp(float(100 - volume) * dBConvert) : 0;
225}
226
227int AudioSystem::logToLinear(float volume)
228{
229 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000230 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 // return v;
232 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
233}
234
Glenn Kasten3b16c762012-11-14 08:44:39 -0800235status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (streamType == AUDIO_STREAM_DEFAULT) {
240 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 }
242
Glenn Kastenfff6d712012-01-12 16:38:12 -0800243 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244 if (output == 0) {
245 return PERMISSION_DENIED;
246 }
247
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700248 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700249}
250
251status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800252 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700253{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800254 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
255 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700256
Eric Laurentf6778fd2014-11-18 17:26:58 -0800257 Mutex::Autolock _l(gLockCache);
258
259 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800260 if (outputDesc == NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800262 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700263 *samplingRate = af->sampleRate(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800264 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265 } else {
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700267 *samplingRate = outputDesc->samplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800269 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700270 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800271 return BAD_VALUE;
272 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700273
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700274 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 return NO_ERROR;
277}
278
Glenn Kastene33054e2012-11-14 12:54:39 -0800279status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282
Dima Zavinfce7a472011-04-19 22:30:36 -0700283 if (streamType == AUDIO_STREAM_DEFAULT) {
284 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700286
Glenn Kastenfff6d712012-01-12 16:38:12 -0800287 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700288 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 return PERMISSION_DENIED;
290 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700292 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700293}
294
295status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800296 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700297{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800298 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
299 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700300
Eric Laurentf6778fd2014-11-18 17:26:58 -0800301 Mutex::Autolock _l(gLockCache);
302
303 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800304 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800305 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306 *frameCount = af->frameCount(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800307 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308 } else {
309 *frameCount = outputDesc->frameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800311 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700312 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800313 return BAD_VALUE;
314 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700316 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return NO_ERROR;
319}
320
Glenn Kastenfff6d712012-01-12 16:38:12 -0800321status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324
Dima Zavinfce7a472011-04-19 22:30:36 -0700325 if (streamType == AUDIO_STREAM_DEFAULT) {
326 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700328
Glenn Kastenfff6d712012-01-12 16:38:12 -0800329 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700330 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 return PERMISSION_DENIED;
332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333
Glenn Kasten241618f2014-03-25 17:48:57 -0700334 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700335}
336
337status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700338 uint32_t* latency)
339{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
341 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700342
Eric Laurentf6778fd2014-11-18 17:26:58 -0800343 Mutex::Autolock _l(gLockCache);
344
345 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output);
Glenn Kastena0d68332012-01-27 16:47:15 -0800346 if (outputDesc == NULL) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800347 gLockCache.unlock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 *latency = af->latency(output);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800349 gLockCache.lock();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 } else {
351 *latency = outputDesc->latency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352 }
353
Glenn Kasten241618f2014-03-25 17:48:57 -0700354 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 return NO_ERROR;
357}
358
Glenn Kastendd8104c2012-07-02 12:42:44 -0700359status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
360 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800362 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
363 if (af == 0) {
364 return PERMISSION_DENIED;
365 }
366 Mutex::Autolock _l(gLockCache);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800368 size_t inBuffSize = gInBuffSize;
369 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
Glenn Kastendd8104c2012-07-02 12:42:44 -0700370 || (channelMask != gPrevInChannelMask)) {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800371 gLockCache.unlock();
Glenn Kastendd8104c2012-07-02 12:42:44 -0700372 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
Eric Laurentf6778fd2014-11-18 17:26:58 -0800373 gLockCache.lock();
Glenn Kasten5446e542014-01-08 08:58:53 -0800374 if (inBuffSize == 0) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800375 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
Glenn Kasten5446e542014-01-08 08:58:53 -0800376 sampleRate, format, channelMask);
377 return BAD_VALUE;
378 }
379 // A benign race is possible here: we could overwrite a fresher cache entry
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 // save the request params
381 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 gPrevInFormat = format;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700383 gPrevInChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800385 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700386 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800387 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 return NO_ERROR;
390}
391
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700392status_t AudioSystem::setVoiceVolume(float value)
393{
394 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
395 if (af == 0) return PERMISSION_DENIED;
396 return af->setVoiceVolume(value);
397}
398
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000399status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700400 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800401{
402 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
403 if (af == 0) return PERMISSION_DENIED;
404
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000405 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800406}
407
Glenn Kasten4944acb2013-08-19 08:39:20 -0700408uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
409{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800410 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800411 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800412 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700413 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800414
415 result = af->getInputFramesLost(ioHandle);
416 return result;
417}
418
Eric Laurentde3f8392014-07-27 18:38:22 -0700419audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700420{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700422 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
423 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700424}
425
Marco Nelissend457c972014-02-11 08:47:07 -0800426void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700427{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
429 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800430 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700431 }
432}
433
Marco Nelissend457c972014-02-11 08:47:07 -0800434void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700435{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700436 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
437 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800438 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700439 }
440}
441
Eric Laurent93c3d412014-08-01 14:48:35 -0700442audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
443{
444 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
445 if (af == 0) return AUDIO_HW_SYNC_INVALID;
446 return af->getAudioHwSyncForSession(sessionId);
447}
448
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449// ---------------------------------------------------------------------------
450
Glenn Kasten4944acb2013-08-19 08:39:20 -0700451void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
452{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800453 audio_error_callback cb = NULL;
454 {
455 Mutex::Autolock _l(AudioSystem::gLock);
456 AudioSystem::gAudioFlinger.clear();
457 cb = gAudioErrorCallback;
458 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459
Eric Laurentf6778fd2014-11-18 17:26:58 -0800460 {
461 // clear output handles and stream to output map caches
462 Mutex::Autolock _l(gLockCache);
463 AudioSystem::gOutputs.clear();
464 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465
Eric Laurentf6778fd2014-11-18 17:26:58 -0800466 if (cb) {
467 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000469 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470}
471
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800472void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800473 const void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100474 ALOGV("ioConfigChanged() event %d", event);
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800475 const OutputDescriptor *desc;
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800476 audio_stream_type_t stream;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700477
Glenn Kasten142f5192014-03-25 17:44:59 -0700478 if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479
Eric Laurentf6778fd2014-11-18 17:26:58 -0800480 Mutex::Autolock _l(AudioSystem::gLockCache);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481
482 switch (event) {
483 case STREAM_CONFIG_CHANGED:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484 break;
485 case OUTPUT_OPENED: {
486 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100487 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700488 break;
489 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800490 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800491 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700492
493 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
494 gOutputs.add(ioHandle, outputDesc);
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700495 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu "
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700496 "latency %d",
Glenn Kastenfad226a2013-07-16 17:19:58 -0700497 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700498 outputDesc->frameCount, outputDesc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700499 } break;
500 case OUTPUT_CLOSED: {
501 if (gOutputs.indexOfKey(ioHandle) < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800502 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700503 break;
504 }
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700506
507 gOutputs.removeItem(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700508 } break;
509
510 case OUTPUT_CONFIG_CHANGED: {
511 int index = gOutputs.indexOfKey(ioHandle);
512 if (index < 0) {
Glenn Kasten85007a92012-11-13 15:06:37 -0800513 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514 break;
515 }
Glenn Kastena0d68332012-01-27 16:47:15 -0800516 if (param2 == NULL) break;
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800517 desc = (const OutputDescriptor *)param2;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Glenn Kastencac3daa2014-02-07 09:47:14 -0800519 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700520 "frameCount %zu latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521 ioHandle, desc->samplingRate, desc->format,
Glenn Kastenfad226a2013-07-16 17:19:58 -0700522 desc->channelMask, desc->frameCount, desc->latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
524 delete outputDesc;
525 outputDesc = new OutputDescriptor(*desc);
526 gOutputs.replaceValueFor(ioHandle, outputDesc);
527 } break;
528 case INPUT_OPENED:
529 case INPUT_CLOSED:
530 case INPUT_CONFIG_CHANGED:
531 break;
532
533 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534}
535
Glenn Kasten4944acb2013-08-19 08:39:20 -0700536void AudioSystem::setErrorCallback(audio_error_callback cb)
537{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700538 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 gAudioErrorCallback = cb;
540}
541
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800543// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700544sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
545sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
546
547
Glenn Kasten18a6d902012-09-24 11:27:56 -0700548// establish binder interface to AudioPolicy service
Eric Laurentc2f1f072009-07-17 12:17:14 -0700549const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
550{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800551 Mutex::Autolock _l(gLockAPS);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800552 if (gAudioPolicyService == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700553 sp<IServiceManager> sm = defaultServiceManager();
554 sp<IBinder> binder;
555 do {
556 binder = sm->getService(String16("media.audio_policy"));
557 if (binder != 0)
558 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000559 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700560 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700561 } while (true);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700562 if (gAudioPolicyServiceClient == NULL) {
563 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
564 }
565 binder->linkToDeath(gAudioPolicyServiceClient);
566 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800567 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
Andy Hungb4453752014-09-08 11:47:24 -0700568 gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700569 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800570
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571 return gAudioPolicyService;
572}
573
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700574// ---------------------------------------------------------------------------
575
Dima Zavinfce7a472011-04-19 22:30:36 -0700576status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
577 audio_policy_dev_state_t state,
578 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700579{
580 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700581 const char *address = "";
582
Eric Laurentc2f1f072009-07-17 12:17:14 -0700583 if (aps == 0) return PERMISSION_DENIED;
584
Eric Laurent71b63e32011-09-02 14:20:56 -0700585 if (device_address != NULL) {
586 address = device_address;
587 }
588
589 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700590}
591
Dima Zavinfce7a472011-04-19 22:30:36 -0700592audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700593 const char *device_address)
594{
595 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700596 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700597
598 return aps->getDeviceConnectionState(device, device_address);
599}
600
Glenn Kastenf78aee72012-01-04 11:00:47 -0800601status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700602{
Glenn Kasten347966c2012-01-18 14:58:32 -0800603 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700604 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
605 if (aps == 0) return PERMISSION_DENIED;
606
607 return aps->setPhoneState(state);
608}
609
Dima Zavinfce7a472011-04-19 22:30:36 -0700610status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700611{
612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
613 if (aps == 0) return PERMISSION_DENIED;
614 return aps->setForceUse(usage, config);
615}
616
Dima Zavinfce7a472011-04-19 22:30:36 -0700617audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700618{
619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700620 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700621 return aps->getForceUse(usage);
622}
623
624
Dima Zavinfce7a472011-04-19 22:30:36 -0700625audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700626 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800627 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700628 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000629 audio_output_flags_t flags,
630 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700631{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700632 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
633 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000634 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700635}
636
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700637audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
638 uint32_t samplingRate,
639 audio_format_t format,
640 audio_channel_mask_t channelMask,
641 audio_output_flags_t flags,
642 const audio_offload_info_t *offloadInfo)
643{
644 if (attr == NULL) return 0;
645 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
646 if (aps == 0) return 0;
647 return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo);
648}
649
Eric Laurentde070132010-07-13 04:45:46 -0700650status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700651 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700652 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700653{
654 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
655 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700656 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700657}
658
Eric Laurentde070132010-07-13 04:45:46 -0700659status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700660 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700661 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700662{
663 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
664 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700665 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700666}
667
668void AudioSystem::releaseOutput(audio_io_handle_t output)
669{
670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
671 if (aps == 0) return;
672 aps->releaseOutput(output);
673}
674
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800675audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800677 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700678 audio_channel_mask_t channelMask,
Glenn Kastenb3b16602014-07-16 08:36:31 -0700679 int sessionId,
680 audio_input_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700681{
682 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700683 if (aps == 0) return 0;
Glenn Kastenb3b16602014-07-16 08:36:31 -0700684 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700685}
686
Eric Laurent4dc68062014-07-28 17:26:49 -0700687status_t AudioSystem::startInput(audio_io_handle_t input,
688 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 Laurent4dc68062014-07-28 17:26:49 -0700692 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700693}
694
Eric Laurent4dc68062014-07-28 17:26:49 -0700695status_t AudioSystem::stopInput(audio_io_handle_t input,
696 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700697{
698 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
699 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700700 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700701}
702
Eric Laurent4dc68062014-07-28 17:26:49 -0700703void AudioSystem::releaseInput(audio_io_handle_t input,
704 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700705{
706 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
707 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700708 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709}
710
Dima Zavinfce7a472011-04-19 22:30:36 -0700711status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700712 int indexMin,
713 int indexMax)
714{
715 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
716 if (aps == 0) return PERMISSION_DENIED;
717 return aps->initStreamVolume(stream, indexMin, indexMax);
718}
719
Eric Laurent83844cc2011-11-18 16:43:31 -0800720status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
721 int index,
722 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700723{
724 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
725 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800726 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700727}
728
Eric Laurent83844cc2011-11-18 16:43:31 -0800729status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
730 int *index,
731 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700732{
733 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
734 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800735 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700736}
737
Dima Zavinfce7a472011-04-19 22:30:36 -0700738uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700739{
740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
741 if (aps == 0) return 0;
742 return aps->getStrategyForStream(stream);
743}
744
Eric Laurent63742522012-03-08 13:42:42 -0800745audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800746{
747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800748 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800749 return aps->getDevicesForStream(stream);
750}
751
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700752audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700753{
754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800755 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700756 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700757 return aps->getOutputForEffect(desc);
758}
759
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700760status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700761 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700762 uint32_t strategy,
763 int session,
764 int id)
765{
766 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
767 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700768 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700769}
770
771status_t AudioSystem::unregisterEffect(int id)
772{
773 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
774 if (aps == 0) return PERMISSION_DENIED;
775 return aps->unregisterEffect(id);
776}
777
Eric Laurentdb7c0792011-08-10 10:37:50 -0700778status_t AudioSystem::setEffectEnabled(int id, bool enabled)
779{
780 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
781 if (aps == 0) return PERMISSION_DENIED;
782 return aps->setEffectEnabled(id, enabled);
783}
784
Glenn Kastenfff6d712012-01-12 16:38:12 -0800785status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700786{
Eric Laurenteda6c362011-02-02 09:33:30 -0800787 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
788 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700789 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800790 *state = aps->isStreamActive(stream, inPastMs);
791 return NO_ERROR;
792}
793
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800794status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
795 uint32_t inPastMs)
796{
797 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
798 if (aps == 0) return PERMISSION_DENIED;
799 if (state == NULL) return BAD_VALUE;
800 *state = aps->isStreamActiveRemotely(stream, inPastMs);
801 return NO_ERROR;
802}
803
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700804status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
805{
806 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
807 if (aps == 0) return PERMISSION_DENIED;
808 if (state == NULL) return BAD_VALUE;
809 *state = aps->isSourceActive(stream);
810 return NO_ERROR;
811}
812
Glenn Kasten3b16c762012-11-14 08:44:39 -0800813uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700814{
815 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
816 if (af == 0) return 0;
817 return af->getPrimaryOutputSamplingRate();
818}
819
Glenn Kastene33054e2012-11-14 12:54:39 -0800820size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700821{
822 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
823 if (af == 0) return 0;
824 return af->getPrimaryOutputFrameCount();
825}
Eric Laurenteda6c362011-02-02 09:33:30 -0800826
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700827status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
828{
829 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
830 if (af == 0) return PERMISSION_DENIED;
831 return af->setLowRamDevice(isLowRamDevice);
832}
833
Eric Laurent9f6530f2011-08-30 10:18:54 -0700834void AudioSystem::clearAudioConfigCache()
835{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800836 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("clearAudioConfigCache()");
Glenn Kastend2d089f2014-11-05 11:48:12 -0800838 {
Eric Laurentf6778fd2014-11-18 17:26:58 -0800839 Mutex::Autolock _l(gLockCache);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800840 gOutputs.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800841 }
842 {
843 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800844 gAudioFlinger.clear();
845 }
846 {
847 Mutex::Autolock _l(gLockAPS);
848 gAudioPolicyService.clear();
849 }
850 // Do not clear gAudioPortCallback
Eric Laurent9f6530f2011-08-30 10:18:54 -0700851}
852
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000853bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
854{
855 ALOGV("isOffloadSupported()");
856 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
857 if (aps == 0) return false;
858 return aps->isOffloadSupported(info);
859}
860
Eric Laurent203b1a12014-04-01 10:34:16 -0700861status_t AudioSystem::listAudioPorts(audio_port_role_t role,
862 audio_port_type_t type,
863 unsigned int *num_ports,
864 struct audio_port *ports,
865 unsigned int *generation)
866{
867 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
868 if (aps == 0) return PERMISSION_DENIED;
869 return aps->listAudioPorts(role, type, num_ports, ports, generation);
870}
871
872status_t AudioSystem::getAudioPort(struct audio_port *port)
873{
874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
875 if (aps == 0) return PERMISSION_DENIED;
876 return aps->getAudioPort(port);
877}
878
879status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
880 audio_patch_handle_t *handle)
881{
882 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
883 if (aps == 0) return PERMISSION_DENIED;
884 return aps->createAudioPatch(patch, handle);
885}
886
887status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
888{
889 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
890 if (aps == 0) return PERMISSION_DENIED;
891 return aps->releaseAudioPatch(handle);
892}
893
894status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
895 struct audio_patch *patches,
896 unsigned int *generation)
897{
898 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
899 if (aps == 0) return PERMISSION_DENIED;
900 return aps->listAudioPatches(num_patches, patches, generation);
901}
902
903status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
904{
905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
906 if (aps == 0) return PERMISSION_DENIED;
907 return aps->setAudioPortConfig(config);
908}
909
Eric Laurentb52c1522014-05-20 11:27:36 -0700910void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
911{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800912 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700913 gAudioPortCallback = callBack;
914}
915
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700916status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
917 audio_io_handle_t *ioHandle,
918 audio_devices_t *device)
919{
920 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
921 if (aps == 0) return PERMISSION_DENIED;
922 return aps->acquireSoundTriggerSession(session, ioHandle, device);
923}
924
925status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
926{
927 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
928 if (aps == 0) return PERMISSION_DENIED;
929 return aps->releaseSoundTriggerSession(session);
930}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700931
932audio_mode_t AudioSystem::getPhoneState()
933{
934 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
935 if (aps == 0) return AUDIO_MODE_INVALID;
936 return aps->getPhoneState();
937}
938
939
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940// ---------------------------------------------------------------------------
941
Glenn Kasten4944acb2013-08-19 08:39:20 -0700942void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
943{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800944 {
945 Mutex::Autolock _l(gLockAPC);
946 if (gAudioPortCallback != 0) {
947 gAudioPortCallback->onServiceDied();
948 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700949 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800950 {
951 Mutex::Autolock _l(gLockAPS);
952 AudioSystem::gAudioPolicyService.clear();
953 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954
Steve Block5ff1dd52012-01-05 23:22:43 +0000955 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956}
957
Eric Laurentb52c1522014-05-20 11:27:36 -0700958void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
959{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800960 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700961 if (gAudioPortCallback != 0) {
962 gAudioPortCallback->onAudioPortListUpdate();
963 }
964}
965
966void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
967{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800968 Mutex::Autolock _l(gLockAPC);
Eric Laurentb52c1522014-05-20 11:27:36 -0700969 if (gAudioPortCallback != 0) {
970 gAudioPortCallback->onAudioPatchListUpdate();
971 }
972}
973
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974}; // namespace android