blob: 85ed2b1deb0854fbdc0a87607168435f510677f1 [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;
Glenn Kastend2d089f2014-11-05 11:48:12 -080035Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036sp<IAudioFlinger> AudioSystem::gAudioFlinger;
37sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
38audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070039dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Glenn Kasten211eeaf2012-01-20 09:37:45 -080040
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080043const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080045 sp<IAudioFlinger> af;
46 sp<AudioFlingerClient> afc;
47 {
48 Mutex::Autolock _l(gLock);
49 if (gAudioFlinger == 0) {
50 sp<IServiceManager> sm = defaultServiceManager();
51 sp<IBinder> binder;
52 do {
53 binder = sm->getService(String16("media.audio_flinger"));
54 if (binder != 0)
55 break;
56 ALOGW("AudioFlinger not published, waiting...");
57 usleep(500000); // 0.5 s
58 } while (true);
59 if (gAudioFlingerClient == NULL) {
60 gAudioFlingerClient = new AudioFlingerClient();
61 } else {
62 if (gAudioErrorCallback) {
63 gAudioErrorCallback(NO_ERROR);
64 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080066 binder->linkToDeath(gAudioFlingerClient);
67 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
68 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
69 afc = gAudioFlingerClient;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070070 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080071 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080073 if (afc != 0) {
74 af->registerClient(afc);
75 }
76 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077}
78
Eric Laurent46291612013-07-18 14:38:44 -070079/* static */ status_t AudioSystem::checkAudioFlinger()
80{
81 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
82 return NO_ERROR;
83 }
84 return DEAD_OBJECT;
85}
86
Glenn Kasten4944acb2013-08-19 08:39:20 -070087status_t AudioSystem::muteMicrophone(bool state)
88{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
90 if (af == 0) return PERMISSION_DENIED;
91 return af->setMicMute(state);
92}
93
Glenn Kasten4944acb2013-08-19 08:39:20 -070094status_t AudioSystem::isMicrophoneMuted(bool* state)
95{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
97 if (af == 0) return PERMISSION_DENIED;
98 *state = af->getMicMute();
99 return NO_ERROR;
100}
101
102status_t AudioSystem::setMasterVolume(float value)
103{
104 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
105 if (af == 0) return PERMISSION_DENIED;
106 af->setMasterVolume(value);
107 return NO_ERROR;
108}
109
110status_t AudioSystem::setMasterMute(bool mute)
111{
112 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
113 if (af == 0) return PERMISSION_DENIED;
114 af->setMasterMute(mute);
115 return NO_ERROR;
116}
117
118status_t AudioSystem::getMasterVolume(float* volume)
119{
120 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
121 if (af == 0) return PERMISSION_DENIED;
122 *volume = af->masterVolume();
123 return NO_ERROR;
124}
125
126status_t AudioSystem::getMasterMute(bool* mute)
127{
128 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
129 if (af == 0) return PERMISSION_DENIED;
130 *mute = af->masterMute();
131 return NO_ERROR;
132}
133
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800134status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
135 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136{
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
139 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700140 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 return NO_ERROR;
142}
143
Glenn Kastenfff6d712012-01-12 16:38:12 -0800144status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
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;
149 af->setStreamMute(stream, mute);
150 return NO_ERROR;
151}
152
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800153status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
154 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155{
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700159 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 return NO_ERROR;
161}
162
Glenn Kastenfff6d712012-01-12 16:38:12 -0800163status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
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;
168 *mute = af->streamMute(stream);
169 return NO_ERROR;
170}
171
Glenn Kastenf78aee72012-01-04 11:00:47 -0800172status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800174 if (uint32_t(mode) >= AUDIO_MODE_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 return af->setMode(mode);
178}
179
Glenn Kasten4944acb2013-08-19 08:39:20 -0700180status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
181{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700184 return af->setParameters(ioHandle, keyValuePairs);
185}
186
Glenn Kasten4944acb2013-08-19 08:39:20 -0700187String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
188{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700189 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
190 String8 result = String8("");
191 if (af == 0) return result;
192
193 result = af->getParameters(ioHandle, keys);
194 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195}
196
Glenn Kastenc23885e2013-12-19 16:35:18 -0800197status_t AudioSystem::setParameters(const String8& keyValuePairs)
198{
Glenn Kasten142f5192014-03-25 17:44:59 -0700199 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800200}
201
202String8 AudioSystem::getParameters(const String8& keys)
203{
Glenn Kasten142f5192014-03-25 17:44:59 -0700204 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800205}
206
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207// convert volume steps to natural log scale
208
209// change this value to change volume scaling
210static const float dBPerStep = 0.5f;
211// shouldn't need to touch these
212static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
213static const float dBConvertInverse = 1.0f / dBConvert;
214
215float AudioSystem::linearToLog(int volume)
216{
217 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000218 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 // return v;
220 return volume ? exp(float(100 - volume) * dBConvert) : 0;
221}
222
223int AudioSystem::logToLinear(float volume)
224{
225 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000226 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 // return v;
228 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
229}
230
Glenn Kasten3b16c762012-11-14 08:44:39 -0800231status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234
Dima Zavinfce7a472011-04-19 22:30:36 -0700235 if (streamType == AUDIO_STREAM_DEFAULT) {
236 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237 }
238
Glenn Kastenfff6d712012-01-12 16:38:12 -0800239 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700240 if (output == 0) {
241 return PERMISSION_DENIED;
242 }
243
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700244 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700245}
246
247status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800248 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700249{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800250 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
251 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700252
Eric Laurent73e26b62015-04-27 16:55:58 -0700253 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
254 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
255 if (outputDesc == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 *samplingRate = af->sampleRate(output);
258 } else {
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurent73e26b62015-04-27 16:55:58 -0700260 *samplingRate = outputDesc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700261 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800262 if (*samplingRate == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700263 ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800264 return BAD_VALUE;
265 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700267 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 return NO_ERROR;
270}
271
Glenn Kastene33054e2012-11-14 12:54:39 -0800272status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275
Dima Zavinfce7a472011-04-19 22:30:36 -0700276 if (streamType == AUDIO_STREAM_DEFAULT) {
277 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700279
Glenn Kastenfff6d712012-01-12 16:38:12 -0800280 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700281 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 return PERMISSION_DENIED;
283 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700285 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700286}
287
288status_t AudioSystem::getFrameCount(audio_io_handle_t output,
Glenn Kastene33054e2012-11-14 12:54:39 -0800289 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700290{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800291 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
292 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700293
Eric Laurent73e26b62015-04-27 16:55:58 -0700294 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
295 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
296 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297 *frameCount = af->frameCount(output);
298 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700299 *frameCount = outputDesc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800301 if (*frameCount == 0) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700302 ALOGE("AudioSystem::getFrameCount failed for output %d", output);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800303 return BAD_VALUE;
304 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700305
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700306 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 return NO_ERROR;
309}
310
Glenn Kastenfff6d712012-01-12 16:38:12 -0800311status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314
Dima Zavinfce7a472011-04-19 22:30:36 -0700315 if (streamType == AUDIO_STREAM_DEFAULT) {
316 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700318
Glenn Kastenfff6d712012-01-12 16:38:12 -0800319 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700320 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321 return PERMISSION_DENIED;
322 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323
Glenn Kasten241618f2014-03-25 17:48:57 -0700324 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700325}
326
327status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700328 uint32_t* latency)
329{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800330 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
331 if (af == 0) return PERMISSION_DENIED;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700332
Eric Laurent73e26b62015-04-27 16:55:58 -0700333 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
334 sp<AudioIoDescriptor> outputDesc = gAudioFlingerClient->getIoDescriptor(output);
335 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336 *latency = af->latency(output);
337 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700338 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 }
340
Glenn Kasten241618f2014-03-25 17:48:57 -0700341 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700342
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 return NO_ERROR;
344}
345
Glenn Kastendd8104c2012-07-02 12:42:44 -0700346status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
347 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurent73e26b62015-04-27 16:55:58 -0700350 if (af == 0) return PERMISSION_DENIED;
351 LOG_ALWAYS_FATAL_IF(gAudioFlingerClient == 0);
352 return gAudioFlingerClient->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353}
354
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700355status_t AudioSystem::setVoiceVolume(float value)
356{
357 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
358 if (af == 0) return PERMISSION_DENIED;
359 return af->setVoiceVolume(value);
360}
361
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000362status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700363 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800364{
365 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
366 if (af == 0) return PERMISSION_DENIED;
367
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000368 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800369}
370
Glenn Kasten4944acb2013-08-19 08:39:20 -0700371uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
372{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800373 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800374 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800375 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700376 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800377
378 result = af->getInputFramesLost(ioHandle);
379 return result;
380}
381
Eric Laurentde3f8392014-07-27 18:38:22 -0700382audio_unique_id_t AudioSystem::newAudioUniqueId()
Glenn Kasten4944acb2013-08-19 08:39:20 -0700383{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700384 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700385 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
386 return af->newAudioUniqueId();
Eric Laurentbe916aa2010-06-01 23:49:17 -0700387}
388
Marco Nelissend457c972014-02-11 08:47:07 -0800389void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700390{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700391 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
392 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800393 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700394 }
395}
396
Marco Nelissend457c972014-02-11 08:47:07 -0800397void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700398{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700399 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
400 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800401 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700402 }
403}
404
Eric Laurent93c3d412014-08-01 14:48:35 -0700405audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
406{
407 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
408 if (af == 0) return AUDIO_HW_SYNC_INVALID;
409 return af->getAudioHwSyncForSession(sessionId);
410}
411
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412// ---------------------------------------------------------------------------
413
Eric Laurent73e26b62015-04-27 16:55:58 -0700414
415void AudioSystem::AudioFlingerClient::clearIoCache()
416{
417 Mutex::Autolock _l(mLock);
418 mIoDescriptors.clear();
419 mInBuffSize = 0;
420 mInSamplingRate = 0;
421 mInFormat = AUDIO_FORMAT_DEFAULT;
422 mInChannelMask = AUDIO_CHANNEL_NONE;
423}
424
Glenn Kasten4944acb2013-08-19 08:39:20 -0700425void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
426{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800427 audio_error_callback cb = NULL;
428 {
429 Mutex::Autolock _l(AudioSystem::gLock);
430 AudioSystem::gAudioFlinger.clear();
431 cb = gAudioErrorCallback;
432 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433
Eric Laurent73e26b62015-04-27 16:55:58 -0700434 // clear output handles and stream to output map caches
435 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436
Eric Laurentf6778fd2014-11-18 17:26:58 -0800437 if (cb) {
438 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000440 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441}
442
Eric Laurent73e26b62015-04-27 16:55:58 -0700443void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
444 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100445 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700446
Eric Laurent73e26b62015-04-27 16:55:58 -0700447 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700448
Eric Laurent73e26b62015-04-27 16:55:58 -0700449 Mutex::Autolock _l(mLock);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700450
451 switch (event) {
Eric Laurent73e26b62015-04-27 16:55:58 -0700452 case AUDIO_OUTPUT_OPENED:
453 case AUDIO_INPUT_OPENED: {
454 if (getIoDescriptor(ioDesc->mIoHandle) != 0) {
455 ALOGV("ioConfigChanged() opening already existing output! %d", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700456 break;
457 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700458 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
459 ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x "
460 "frameCount %zu", event == AUDIO_OUTPUT_OPENED ? "output" : "input",
461 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
462 ioDesc->mFrameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700463 } break;
Eric Laurent73e26b62015-04-27 16:55:58 -0700464 case AUDIO_OUTPUT_CLOSED:
465 case AUDIO_INPUT_CLOSED: {
466 if (getIoDescriptor(ioDesc->mIoHandle) == 0) {
467 ALOGW("ioConfigChanged() closing unknown %s %d",
468 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700469 break;
470 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700471 ALOGV("ioConfigChanged() %s %d closed", event == AUDIO_OUTPUT_CLOSED ? "output" : "input",
472 ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700473
Eric Laurent73e26b62015-04-27 16:55:58 -0700474 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700475 } break;
476
Eric Laurent73e26b62015-04-27 16:55:58 -0700477 case AUDIO_OUTPUT_CONFIG_CHANGED:
478 case AUDIO_INPUT_CONFIG_CHANGED: {
479 if (getIoDescriptor(ioDesc->mIoHandle) == 0) {
480 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700481 break;
482 }
Eric Laurent73e26b62015-04-27 16:55:58 -0700483 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
484 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
485 "channel mask %#x frameCount %zu",
486 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
487 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
488 ioDesc->mChannelMask, ioDesc->mFrameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700489 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700490 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491}
492
Eric Laurent73e26b62015-04-27 16:55:58 -0700493status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
494 uint32_t sampleRate, audio_format_t format,
495 audio_channel_mask_t channelMask, size_t* buffSize)
496{
497 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
498 if (af == 0) {
499 return PERMISSION_DENIED;
500 }
501 Mutex::Autolock _l(mLock);
502 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
503 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
504 || (channelMask != mInChannelMask)) {
505 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
506 if (inBuffSize == 0) {
507 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
508 sampleRate, format, channelMask);
509 return BAD_VALUE;
510 }
511 // A benign race is possible here: we could overwrite a fresher cache entry
512 // save the request params
513 mInSamplingRate = sampleRate;
514 mInFormat = format;
515 mInChannelMask = channelMask;
516
517 mInBuffSize = inBuffSize;
518 }
519
520 *buffSize = mInBuffSize;
521
522 return NO_ERROR;
523}
524
525sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
526{
527 sp<AudioIoDescriptor> desc;
528 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
529 if (index >= 0) {
530 desc = mIoDescriptors.valueAt(index);
531 }
532 return desc;
533}
534
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700535/*static*/ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700536{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700537 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 gAudioErrorCallback = cb;
539}
540
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700541/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
542{
543 Mutex::Autolock _l(gLock);
544 gDynPolicyCallback = cb;
545}
546
Eric Laurentc2f1f072009-07-17 12:17:14 -0700547// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800548// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700549sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
550sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
551
552
Glenn Kasten18a6d902012-09-24 11:27:56 -0700553// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800554const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700555{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800556 sp<IAudioPolicyService> ap;
557 sp<AudioPolicyServiceClient> apc;
558 {
559 Mutex::Autolock _l(gLockAPS);
560 if (gAudioPolicyService == 0) {
561 sp<IServiceManager> sm = defaultServiceManager();
562 sp<IBinder> binder;
563 do {
564 binder = sm->getService(String16("media.audio_policy"));
565 if (binder != 0)
566 break;
567 ALOGW("AudioPolicyService not published, waiting...");
568 usleep(500000); // 0.5 s
569 } while (true);
570 if (gAudioPolicyServiceClient == NULL) {
571 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
572 }
573 binder->linkToDeath(gAudioPolicyServiceClient);
574 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
575 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
576 apc = gAudioPolicyServiceClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700577 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800578 ap = gAudioPolicyService;
579 }
580 if (apc != 0) {
581 ap->registerClient(apc);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700582 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800583
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800584 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700585}
586
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700587// ---------------------------------------------------------------------------
588
Dima Zavinfce7a472011-04-19 22:30:36 -0700589status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
590 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800591 const char *device_address,
592 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700593{
594 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700595 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800596 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700597
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598 if (aps == 0) return PERMISSION_DENIED;
599
Eric Laurent71b63e32011-09-02 14:20:56 -0700600 if (device_address != NULL) {
601 address = device_address;
602 }
Paul McLeane743a472015-01-28 11:07:31 -0800603 if (device_name != NULL) {
604 name = device_name;
605 }
606 return aps->setDeviceConnectionState(device, state, address, name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700607}
608
Dima Zavinfce7a472011-04-19 22:30:36 -0700609audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700610 const char *device_address)
611{
612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700613 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700614
615 return aps->getDeviceConnectionState(device, device_address);
616}
617
Glenn Kastenf78aee72012-01-04 11:00:47 -0800618status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700619{
Glenn Kasten347966c2012-01-18 14:58:32 -0800620 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700621 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
622 if (aps == 0) return PERMISSION_DENIED;
623
624 return aps->setPhoneState(state);
625}
626
Dima Zavinfce7a472011-04-19 22:30:36 -0700627status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628{
629 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
630 if (aps == 0) return PERMISSION_DENIED;
631 return aps->setForceUse(usage, config);
632}
633
Dima Zavinfce7a472011-04-19 22:30:36 -0700634audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700635{
636 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700637 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700638 return aps->getForceUse(usage);
639}
640
641
Dima Zavinfce7a472011-04-19 22:30:36 -0700642audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700643 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800644 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700645 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000646 audio_output_flags_t flags,
647 const audio_offload_info_t *offloadInfo)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700648{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700649 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
650 if (aps == 0) return 0;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000651 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700652}
653
Eric Laurente83b55d2014-11-14 10:06:21 -0800654status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
655 audio_io_handle_t *output,
656 audio_session_t session,
657 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700658 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800659 uint32_t samplingRate,
660 audio_format_t format,
661 audio_channel_mask_t channelMask,
662 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700663 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800664 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700665{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700666 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800667 if (aps == 0) return NO_INIT;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700668 return aps->getOutputForAttr(attr, output, session, stream, uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800669 samplingRate, format, channelMask,
Paul McLeanaa981192015-03-21 09:55:15 -0700670 flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700671}
672
Eric Laurentde070132010-07-13 04:45:46 -0700673status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700674 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800675 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676{
677 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
678 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700679 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700680}
681
Eric Laurentde070132010-07-13 04:45:46 -0700682status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700683 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800684 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700685{
686 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
687 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700688 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700689}
690
Eric Laurente83b55d2014-11-14 10:06:21 -0800691void AudioSystem::releaseOutput(audio_io_handle_t output,
692 audio_stream_type_t stream,
693 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694{
695 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
696 if (aps == 0) return;
Eric Laurente83b55d2014-11-14 10:06:21 -0800697 aps->releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700698}
699
Eric Laurentcaf7f482014-11-25 17:50:47 -0800700status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
701 audio_io_handle_t *input,
702 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700703 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800704 uint32_t samplingRate,
705 audio_format_t format,
706 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600707 audio_input_flags_t flags,
708 audio_port_handle_t selectedDeviceId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709{
710 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800711 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600712 return aps->getInputForAttr(
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700713 attr, input, session, uid, samplingRate, format, channelMask, flags, selectedDeviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700714}
715
Eric Laurent4dc68062014-07-28 17:26:49 -0700716status_t AudioSystem::startInput(audio_io_handle_t input,
717 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718{
719 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
720 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700721 return aps->startInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700722}
723
Eric Laurent4dc68062014-07-28 17:26:49 -0700724status_t AudioSystem::stopInput(audio_io_handle_t input,
725 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700726{
727 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
728 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4dc68062014-07-28 17:26:49 -0700729 return aps->stopInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700730}
731
Eric Laurent4dc68062014-07-28 17:26:49 -0700732void AudioSystem::releaseInput(audio_io_handle_t input,
733 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700734{
735 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
736 if (aps == 0) return;
Eric Laurent4dc68062014-07-28 17:26:49 -0700737 aps->releaseInput(input, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700738}
739
Dima Zavinfce7a472011-04-19 22:30:36 -0700740status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700741 int indexMin,
742 int indexMax)
743{
744 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
745 if (aps == 0) return PERMISSION_DENIED;
746 return aps->initStreamVolume(stream, indexMin, indexMax);
747}
748
Eric Laurent83844cc2011-11-18 16:43:31 -0800749status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
750 int index,
751 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700752{
753 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
754 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800755 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700756}
757
Eric Laurent83844cc2011-11-18 16:43:31 -0800758status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
759 int *index,
760 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700761{
762 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
763 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800764 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700765}
766
Dima Zavinfce7a472011-04-19 22:30:36 -0700767uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700768{
769 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
770 if (aps == 0) return 0;
771 return aps->getStrategyForStream(stream);
772}
773
Eric Laurent63742522012-03-08 13:42:42 -0800774audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800775{
776 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800777 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800778 return aps->getDevicesForStream(stream);
779}
780
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700781audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700782{
783 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800784 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -0700785 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700786 return aps->getOutputForEffect(desc);
787}
788
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700789status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700790 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700791 uint32_t strategy,
792 int session,
793 int id)
794{
795 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
796 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700797 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700798}
799
800status_t AudioSystem::unregisterEffect(int id)
801{
802 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
803 if (aps == 0) return PERMISSION_DENIED;
804 return aps->unregisterEffect(id);
805}
806
Eric Laurentdb7c0792011-08-10 10:37:50 -0700807status_t AudioSystem::setEffectEnabled(int id, bool enabled)
808{
809 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
810 if (aps == 0) return PERMISSION_DENIED;
811 return aps->setEffectEnabled(id, enabled);
812}
813
Glenn Kastenfff6d712012-01-12 16:38:12 -0800814status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700815{
Eric Laurenteda6c362011-02-02 09:33:30 -0800816 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
817 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700818 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800819 *state = aps->isStreamActive(stream, inPastMs);
820 return NO_ERROR;
821}
822
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800823status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
824 uint32_t inPastMs)
825{
826 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
827 if (aps == 0) return PERMISSION_DENIED;
828 if (state == NULL) return BAD_VALUE;
829 *state = aps->isStreamActiveRemotely(stream, inPastMs);
830 return NO_ERROR;
831}
832
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700833status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
834{
835 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
836 if (aps == 0) return PERMISSION_DENIED;
837 if (state == NULL) return BAD_VALUE;
838 *state = aps->isSourceActive(stream);
839 return NO_ERROR;
840}
841
Glenn Kasten3b16c762012-11-14 08:44:39 -0800842uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700843{
844 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
845 if (af == 0) return 0;
846 return af->getPrimaryOutputSamplingRate();
847}
848
Glenn Kastene33054e2012-11-14 12:54:39 -0800849size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700850{
851 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
852 if (af == 0) return 0;
853 return af->getPrimaryOutputFrameCount();
854}
Eric Laurenteda6c362011-02-02 09:33:30 -0800855
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700856status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
857{
858 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
859 if (af == 0) return PERMISSION_DENIED;
860 return af->setLowRamDevice(isLowRamDevice);
861}
862
Eric Laurent9f6530f2011-08-30 10:18:54 -0700863void AudioSystem::clearAudioConfigCache()
864{
Glenn Kastend2d089f2014-11-05 11:48:12 -0800865 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +0100866 ALOGV("clearAudioConfigCache()");
Eric Laurent73e26b62015-04-27 16:55:58 -0700867 if (gAudioFlingerClient != 0) {
868 gAudioFlingerClient->clearIoCache();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800869 }
870 {
871 Mutex::Autolock _l(gLock);
Glenn Kastend2d089f2014-11-05 11:48:12 -0800872 gAudioFlinger.clear();
873 }
874 {
875 Mutex::Autolock _l(gLockAPS);
876 gAudioPolicyService.clear();
877 }
Eric Laurent9f6530f2011-08-30 10:18:54 -0700878}
879
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000880bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
881{
882 ALOGV("isOffloadSupported()");
883 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
884 if (aps == 0) return false;
885 return aps->isOffloadSupported(info);
886}
887
Eric Laurent203b1a12014-04-01 10:34:16 -0700888status_t AudioSystem::listAudioPorts(audio_port_role_t role,
889 audio_port_type_t type,
890 unsigned int *num_ports,
891 struct audio_port *ports,
892 unsigned int *generation)
893{
894 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
895 if (aps == 0) return PERMISSION_DENIED;
896 return aps->listAudioPorts(role, type, num_ports, ports, generation);
897}
898
899status_t AudioSystem::getAudioPort(struct audio_port *port)
900{
901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
902 if (aps == 0) return PERMISSION_DENIED;
903 return aps->getAudioPort(port);
904}
905
906status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
907 audio_patch_handle_t *handle)
908{
909 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
910 if (aps == 0) return PERMISSION_DENIED;
911 return aps->createAudioPatch(patch, handle);
912}
913
914status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
915{
916 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
917 if (aps == 0) return PERMISSION_DENIED;
918 return aps->releaseAudioPatch(handle);
919}
920
921status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
922 struct audio_patch *patches,
923 unsigned int *generation)
924{
925 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
926 if (aps == 0) return PERMISSION_DENIED;
927 return aps->listAudioPatches(num_patches, patches, generation);
928}
929
930status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
931{
932 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
933 if (aps == 0) return PERMISSION_DENIED;
934 return aps->setAudioPortConfig(config);
935}
936
Eric Laurentb28753e2015-04-01 13:06:28 -0700937status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callBack)
Eric Laurentb52c1522014-05-20 11:27:36 -0700938{
Eric Laurentb28753e2015-04-01 13:06:28 -0700939 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
940 if (aps == 0) return PERMISSION_DENIED;
941
942 Mutex::Autolock _l(gLockAPS);
943 if (gAudioPolicyServiceClient == 0) {
944 return NO_INIT;
945 }
946 return gAudioPolicyServiceClient->addAudioPortCallback(callBack);
Eric Laurentb52c1522014-05-20 11:27:36 -0700947}
948
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700949/*static*/
Eric Laurentb28753e2015-04-01 13:06:28 -0700950status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callBack)
951{
952 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
953 if (aps == 0) return PERMISSION_DENIED;
954
955 Mutex::Autolock _l(gLockAPS);
956 if (gAudioPolicyServiceClient == 0) {
957 return NO_INIT;
958 }
959 return gAudioPolicyServiceClient->removeAudioPortCallback(callBack);
960}
961
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700962status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
963 audio_io_handle_t *ioHandle,
964 audio_devices_t *device)
965{
966 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
967 if (aps == 0) return PERMISSION_DENIED;
968 return aps->acquireSoundTriggerSession(session, ioHandle, device);
969}
970
971status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
972{
973 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
974 if (aps == 0) return PERMISSION_DENIED;
975 return aps->releaseSoundTriggerSession(session);
976}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700977
978audio_mode_t AudioSystem::getPhoneState()
979{
980 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
981 if (aps == 0) return AUDIO_MODE_INVALID;
982 return aps->getPhoneState();
983}
984
Eric Laurentbaac1832014-12-01 17:52:59 -0800985status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
986{
987 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
988 if (aps == 0) return PERMISSION_DENIED;
989 return aps->registerPolicyMixes(mixes, registration);
990}
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700991
Eric Laurent554a2772015-04-10 11:29:24 -0700992status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
993 const audio_attributes_t *attributes,
994 audio_io_handle_t *handle)
995{
996 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
997 if (aps == 0) return PERMISSION_DENIED;
998 return aps->startAudioSource(source, attributes, handle);
999}
1000
1001status_t AudioSystem::stopAudioSource(audio_io_handle_t handle)
1002{
1003 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1004 if (aps == 0) return PERMISSION_DENIED;
1005 return aps->stopAudioSource(handle);
1006}
1007
Eric Laurentc2f1f072009-07-17 12:17:14 -07001008// ---------------------------------------------------------------------------
1009
Eric Laurentb28753e2015-04-01 13:06:28 -07001010status_t AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1011 const sp<AudioPortCallback>& callBack)
1012{
1013 Mutex::Autolock _l(mLock);
1014 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1015 if (mAudioPortCallbacks[i] == callBack) {
1016 return INVALID_OPERATION;
1017 }
1018 }
1019 mAudioPortCallbacks.add(callBack);
1020 return NO_ERROR;
1021}
1022
1023status_t AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1024 const sp<AudioPortCallback>& callBack)
1025{
1026 Mutex::Autolock _l(mLock);
1027 size_t i;
1028 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1029 if (mAudioPortCallbacks[i] == callBack) {
1030 break;
1031 }
1032 }
1033 if (i == mAudioPortCallbacks.size()) {
1034 return INVALID_OPERATION;
1035 }
1036 mAudioPortCallbacks.removeAt(i);
1037 return NO_ERROR;
1038}
1039
1040void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1041{
1042 Mutex::Autolock _l(mLock);
1043 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1044 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1045 }
1046}
1047
1048void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1049{
1050 Mutex::Autolock _l(mLock);
1051 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1052 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1053 }
1054}
1055
Jean-Michel Trivide801052015-04-14 19:10:14 -07001056void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1057 String8 regId, int32_t state)
1058{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001059 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1060 dynamic_policy_callback cb = NULL;
1061 {
1062 Mutex::Autolock _l(AudioSystem::gLock);
1063 cb = gDynPolicyCallback;
1064 }
1065
1066 if (cb != NULL) {
1067 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1068 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001069}
1070
Glenn Kasten4944acb2013-08-19 08:39:20 -07001071void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1072{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001073 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001074 Mutex::Autolock _l(mLock);
1075 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1076 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001077 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001078 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001079 {
1080 Mutex::Autolock _l(gLockAPS);
1081 AudioSystem::gAudioPolicyService.clear();
1082 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001083
Steve Block5ff1dd52012-01-05 23:22:43 +00001084 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001085}
1086
Glenn Kasten40bc9062015-03-20 09:09:33 -07001087} // namespace android