blob: 33c20084807f94785667dd42e211dc126646fc38 [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>
Eric Laurentfb00fc72017-05-25 18:17:12 -070022#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020023#include <binder/IPCThreadState.h>
Eric Laurent21da6472017-11-09 16:29:26 -080024#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070026#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070027#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010028#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029#include <math.h>
30
Dima Zavin64760242011-05-11 14:15:23 -070031#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Eric Laurentc2f1f072009-07-17 12:17:14 -070033// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035namespace android {
36
37// client singleton for AudioFlinger binder interface
38Mutex AudioSystem::gLock;
Glenn Kastend2d089f2014-11-05 11:48:12 -080039Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040sp<IAudioFlinger> AudioSystem::gAudioFlinger;
41sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
42audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070043dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080044record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080045
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080047const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080049 sp<IAudioFlinger> af;
50 sp<AudioFlingerClient> afc;
51 {
52 Mutex::Autolock _l(gLock);
53 if (gAudioFlinger == 0) {
54 sp<IServiceManager> sm = defaultServiceManager();
55 sp<IBinder> binder;
56 do {
57 binder = sm->getService(String16("media.audio_flinger"));
58 if (binder != 0)
59 break;
60 ALOGW("AudioFlinger not published, waiting...");
61 usleep(500000); // 0.5 s
62 } while (true);
63 if (gAudioFlingerClient == NULL) {
64 gAudioFlingerClient = new AudioFlingerClient();
65 } else {
66 if (gAudioErrorCallback) {
67 gAudioErrorCallback(NO_ERROR);
68 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080070 binder->linkToDeath(gAudioFlingerClient);
71 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
72 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
73 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070074 // Make sure callbacks can be received by gAudioFlingerClient
75 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080077 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080079 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020080 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080081 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020082 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080083 }
84 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085}
86
Eric Laurent296fb132015-05-01 11:38:42 -070087const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
88{
89 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
90 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
91 if (af == 0) return 0;
92 Mutex::Autolock _l(gLock);
93 return gAudioFlingerClient;
94}
95
96sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
97{
98 sp<AudioIoDescriptor> desc;
99 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
100 if (afc != 0) {
101 desc = afc->getIoDescriptor(ioHandle);
102 }
103 return desc;
104}
105
Eric Laurent46291612013-07-18 14:38:44 -0700106/* static */ status_t AudioSystem::checkAudioFlinger()
107{
108 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
109 return NO_ERROR;
110 }
111 return DEAD_OBJECT;
112}
113
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700114// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
115
Glenn Kasten4944acb2013-08-19 08:39:20 -0700116status_t AudioSystem::muteMicrophone(bool state)
117{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
119 if (af == 0) return PERMISSION_DENIED;
120 return af->setMicMute(state);
121}
122
Glenn Kasten4944acb2013-08-19 08:39:20 -0700123status_t AudioSystem::isMicrophoneMuted(bool* state)
124{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
126 if (af == 0) return PERMISSION_DENIED;
127 *state = af->getMicMute();
128 return NO_ERROR;
129}
130
131status_t AudioSystem::setMasterVolume(float value)
132{
133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
134 if (af == 0) return PERMISSION_DENIED;
135 af->setMasterVolume(value);
136 return NO_ERROR;
137}
138
139status_t AudioSystem::setMasterMute(bool mute)
140{
141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
143 af->setMasterMute(mute);
144 return NO_ERROR;
145}
146
147status_t AudioSystem::getMasterVolume(float* volume)
148{
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
151 *volume = af->masterVolume();
152 return NO_ERROR;
153}
154
155status_t AudioSystem::getMasterMute(bool* mute)
156{
157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
159 *mute = af->masterMute();
160 return NO_ERROR;
161}
162
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800163status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
164 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165{
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700169 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 return NO_ERROR;
171}
172
Glenn Kastenfff6d712012-01-12 16:38:12 -0800173status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174{
Dima Zavinfce7a472011-04-19 22:30:36 -0700175 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
177 if (af == 0) return PERMISSION_DENIED;
178 af->setStreamMute(stream, mute);
179 return NO_ERROR;
180}
181
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800182status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
183 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184{
Dima Zavinfce7a472011-04-19 22:30:36 -0700185 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
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 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189 return NO_ERROR;
190}
191
Glenn Kastenfff6d712012-01-12 16:38:12 -0800192status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193{
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
196 if (af == 0) return PERMISSION_DENIED;
197 *mute = af->streamMute(stream);
198 return NO_ERROR;
199}
200
Glenn Kastenf78aee72012-01-04 11:00:47 -0800201status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800203 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
205 if (af == 0) return PERMISSION_DENIED;
206 return af->setMode(mode);
207}
208
Glenn Kasten4944acb2013-08-19 08:39:20 -0700209status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
210{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
212 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700213 return af->setParameters(ioHandle, keyValuePairs);
214}
215
Glenn Kasten4944acb2013-08-19 08:39:20 -0700216String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
217{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700218 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
219 String8 result = String8("");
220 if (af == 0) return result;
221
222 result = af->getParameters(ioHandle, keys);
223 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224}
225
Glenn Kastenc23885e2013-12-19 16:35:18 -0800226status_t AudioSystem::setParameters(const String8& keyValuePairs)
227{
Glenn Kasten142f5192014-03-25 17:44:59 -0700228 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800229}
230
231String8 AudioSystem::getParameters(const String8& keys)
232{
Glenn Kasten142f5192014-03-25 17:44:59 -0700233 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800234}
235
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236// convert volume steps to natural log scale
237
238// change this value to change volume scaling
239static const float dBPerStep = 0.5f;
240// shouldn't need to touch these
241static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
242static const float dBConvertInverse = 1.0f / dBConvert;
243
244float AudioSystem::linearToLog(int volume)
245{
246 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000247 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 // return v;
249 return volume ? exp(float(100 - volume) * dBConvert) : 0;
250}
251
252int AudioSystem::logToLinear(float volume)
253{
254 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000255 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 // return v;
257 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
258}
259
Eric Laurent21da6472017-11-09 16:29:26 -0800260/* static */ size_t AudioSystem::calculateMinFrameCount(
261 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
262 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
263{
264 // Ensure that buffer depth covers at least audio hardware latency
265 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
266 if (minBufCount < 2) {
267 minBufCount = 2;
268 }
269#if 0
270 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
271 // but keeping the code here to make it easier to add later.
272 if (minBufCount < notificationsPerBufferReq) {
273 minBufCount = notificationsPerBufferReq;
274 }
275#endif
276 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
277 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
278 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
279 /*, notificationsPerBufferReq*/);
280 return minBufCount * sourceFramesNeededWithTimestretch(
281 sampleRate, afFrameCount, afSampleRate, speed);
282}
283
284
Glenn Kasten3b16c762012-11-14 08:44:39 -0800285status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288
Dima Zavinfce7a472011-04-19 22:30:36 -0700289 if (streamType == AUDIO_STREAM_DEFAULT) {
290 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 }
292
Glenn Kastenfff6d712012-01-12 16:38:12 -0800293 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 if (output == 0) {
295 return PERMISSION_DENIED;
296 }
297
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700298 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700299}
300
Glenn Kasten2c073da2016-02-26 09:14:08 -0800301status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800302 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700303{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800304 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
305 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800306 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
307 if (desc == 0) {
308 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800310 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800312 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800313 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800314 return BAD_VALUE;
315 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316
Glenn Kasten2c073da2016-02-26 09:14:08 -0800317 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 return NO_ERROR;
320}
321
Glenn Kastene33054e2012-11-14 12:54:39 -0800322status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325
Dima Zavinfce7a472011-04-19 22:30:36 -0700326 if (streamType == AUDIO_STREAM_DEFAULT) {
327 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700329
Glenn Kastenfff6d712012-01-12 16:38:12 -0800330 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700331 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 return PERMISSION_DENIED;
333 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700335 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700336}
337
Glenn Kasten2c073da2016-02-26 09:14:08 -0800338status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800339 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700340{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800341 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
342 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800343 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
344 if (desc == 0) {
345 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800347 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800349 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800350 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800351 return BAD_VALUE;
352 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353
Glenn Kasten2c073da2016-02-26 09:14:08 -0800354 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
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 Kastenfff6d712012-01-12 16:38:12 -0800359status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (streamType == AUDIO_STREAM_DEFAULT) {
364 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700366
Glenn Kastenfff6d712012-01-12 16:38:12 -0800367 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700368 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369 return PERMISSION_DENIED;
370 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371
Glenn Kasten241618f2014-03-25 17:48:57 -0700372 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700373}
374
375status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700376 uint32_t* latency)
377{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700380 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700381 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 *latency = af->latency(output);
383 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700384 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 }
386
Glenn Kasten241618f2014-03-25 17:48:57 -0700387 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 return NO_ERROR;
390}
391
Glenn Kastendd8104c2012-07-02 12:42:44 -0700392status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
393 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394{
Eric Laurent296fb132015-05-01 11:38:42 -0700395 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
396 if (afc == 0) {
397 return NO_INIT;
398 }
399 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400}
401
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700402status_t AudioSystem::setVoiceVolume(float value)
403{
404 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
405 if (af == 0) return PERMISSION_DENIED;
406 return af->setVoiceVolume(value);
407}
408
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000409status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700410 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800411{
412 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
413 if (af == 0) return PERMISSION_DENIED;
414
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000415 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800416}
417
Glenn Kasten4944acb2013-08-19 08:39:20 -0700418uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
419{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800420 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800421 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800422 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700423 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800424
425 result = af->getInputFramesLost(ioHandle);
426 return result;
427}
428
Glenn Kasteneeecb982016-02-26 10:44:04 -0800429audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700430{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700431 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700432 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800433 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700434}
435
Glenn Kastend848eb42016-03-08 13:42:11 -0800436void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700437{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700438 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
439 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800440 af->acquireAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700441 }
442}
443
Glenn Kastend848eb42016-03-08 13:42:11 -0800444void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700445{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700446 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
447 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800448 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700449 }
450}
451
Eric Laurent93c3d412014-08-01 14:48:35 -0700452audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
453{
454 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
455 if (af == 0) return AUDIO_HW_SYNC_INVALID;
456 return af->getAudioHwSyncForSession(sessionId);
457}
458
Eric Laurent72e3f392015-05-20 14:43:50 -0700459status_t AudioSystem::systemReady()
460{
461 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
462 if (af == 0) return NO_INIT;
463 return af->systemReady();
464}
465
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700466status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
467 size_t* frameCount)
468{
469 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
470 if (af == 0) return PERMISSION_DENIED;
471 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
472 if (desc == 0) {
473 *frameCount = af->frameCountHAL(ioHandle);
474 } else {
475 *frameCount = desc->mFrameCountHAL;
476 }
477 if (*frameCount == 0) {
478 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
479 return BAD_VALUE;
480 }
481
482 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
483
484 return NO_ERROR;
485}
486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487// ---------------------------------------------------------------------------
488
Eric Laurent73e26b62015-04-27 16:55:58 -0700489
490void AudioSystem::AudioFlingerClient::clearIoCache()
491{
492 Mutex::Autolock _l(mLock);
493 mIoDescriptors.clear();
494 mInBuffSize = 0;
495 mInSamplingRate = 0;
496 mInFormat = AUDIO_FORMAT_DEFAULT;
497 mInChannelMask = AUDIO_CHANNEL_NONE;
498}
499
Glenn Kasten4944acb2013-08-19 08:39:20 -0700500void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
501{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800502 audio_error_callback cb = NULL;
503 {
504 Mutex::Autolock _l(AudioSystem::gLock);
505 AudioSystem::gAudioFlinger.clear();
506 cb = gAudioErrorCallback;
507 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508
Eric Laurent73e26b62015-04-27 16:55:58 -0700509 // clear output handles and stream to output map caches
510 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511
Eric Laurentf6778fd2014-11-18 17:26:58 -0800512 if (cb) {
513 cb(DEAD_OBJECT);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000515 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516}
517
Eric Laurent73e26b62015-04-27 16:55:58 -0700518void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
519 const sp<AudioIoDescriptor>& ioDesc) {
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521
Eric Laurent73e26b62015-04-27 16:55:58 -0700522 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523
Eric Laurent296fb132015-05-01 11:38:42 -0700524 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100525 AudioDeviceCallbacks callbacks;
526 bool deviceValidOrChanged = false;
527 Mutex::Autolock _l(mCallbacksLock);
Eric Laurent296fb132015-05-01 11:38:42 -0700528 {
529 Mutex::Autolock _l(mLock);
530
531 switch (event) {
532 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700533 case AUDIO_OUTPUT_REGISTERED:
534 case AUDIO_INPUT_OPENED:
535 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700536 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700537 if (oldDesc == 0) {
538 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
539 } else {
540 deviceId = oldDesc->getDeviceId();
541 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700542 }
Eric Laurent296fb132015-05-01 11:38:42 -0700543
544 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
545 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700546 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
547 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
548 if (ioIndex >= 0) {
549 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100550 deviceValidOrChanged = true;
551 }
552 }
553 if (event == AUDIO_OUTPUT_REGISTERED || event == AUDIO_INPUT_REGISTERED) {
554 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
555 if ((ioIndex >= 0) && !mAudioDeviceCallbacks.valueAt(ioIndex).notifiedOnce()) {
556 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurentad2e7b92017-09-14 20:06:42 -0700557 }
Eric Laurent296fb132015-05-01 11:38:42 -0700558 }
559 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700560 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
561 "frameCount %zu deviceId %d",
562 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
563 "output" : "input",
564 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
565 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700566 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
567 ioDesc->mFrameCount, ioDesc->getDeviceId());
568 } break;
569 case AUDIO_OUTPUT_CLOSED:
570 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700571 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700572 ALOGW("ioConfigChanged() closing unknown %s %d",
573 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
574 break;
575 }
576 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700577 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700578
Eric Laurent296fb132015-05-01 11:38:42 -0700579 mIoDescriptors.removeItem(ioDesc->mIoHandle);
580 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
581 } break;
582
583 case AUDIO_OUTPUT_CONFIG_CHANGED:
584 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700585 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700586 if (oldDesc == 0) {
587 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
588 break;
589 }
590
591 deviceId = oldDesc->getDeviceId();
592 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
593
594 if (deviceId != ioDesc->getDeviceId()) {
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100595 deviceValidOrChanged = true;
Eric Laurent296fb132015-05-01 11:38:42 -0700596 deviceId = ioDesc->getDeviceId();
597 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
598 if (ioIndex >= 0) {
599 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
600 }
601 }
602 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700603 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700604 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
605 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800606 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
607 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700608
Eric Laurentc2f1f072009-07-17 12:17:14 -0700609 } break;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700610 }
Eric Laurent296fb132015-05-01 11:38:42 -0700611 }
612 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100613 if (callbacks.size() != 0) {
614 for (size_t i = 0; i < callbacks.size(); ) {
615 sp<AudioDeviceCallback> callback = callbacks[i].promote();
616 if (callback.get() != nullptr) {
617 // Call the callback only if the device actually changed, the input or output was
618 // opened or closed or the client was newly registered and the callback was never
619 // called
620 if (!callback->notifiedOnce() || deviceValidOrChanged) {
621 // Must be called without mLock held. May lead to dead lock if calling for
622 // example getRoutedDevice that updates the device and tries to acquire mLock.
623 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
624 callback->setNotifiedOnce();
625 }
626 i++;
627 } else {
628 callbacks.removeAt(i);
629 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700630 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100631 callbacks.setNotifiedOnce();
632 // clean up callback list while we are here if some clients have disappeared without
633 // unregistering their callback, or if cb was served for the first time since registered
Eric Laurentad2e7b92017-09-14 20:06:42 -0700634 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700635 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636}
637
Eric Laurent73e26b62015-04-27 16:55:58 -0700638status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
639 uint32_t sampleRate, audio_format_t format,
640 audio_channel_mask_t channelMask, size_t* buffSize)
641{
642 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
643 if (af == 0) {
644 return PERMISSION_DENIED;
645 }
646 Mutex::Autolock _l(mLock);
647 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
648 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
649 || (channelMask != mInChannelMask)) {
650 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
651 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800652 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700653 sampleRate, format, channelMask);
654 return BAD_VALUE;
655 }
656 // A benign race is possible here: we could overwrite a fresher cache entry
657 // save the request params
658 mInSamplingRate = sampleRate;
659 mInFormat = format;
660 mInChannelMask = channelMask;
661
662 mInBuffSize = inBuffSize;
663 }
664
665 *buffSize = mInBuffSize;
666
667 return NO_ERROR;
668}
669
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700670sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700671{
672 sp<AudioIoDescriptor> desc;
673 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
674 if (index >= 0) {
675 desc = mIoDescriptors.valueAt(index);
676 }
677 return desc;
678}
679
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700680sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
681{
682 Mutex::Autolock _l(mLock);
683 return getIoDescriptor_l(ioHandle);
684}
685
Eric Laurent296fb132015-05-01 11:38:42 -0700686status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700687 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700688{
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100689 Mutex::Autolock _l(mCallbacksLock);
690 AudioDeviceCallbacks callbacks;
Eric Laurent296fb132015-05-01 11:38:42 -0700691 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
692 if (ioIndex >= 0) {
693 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
694 }
695
696 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700697 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700698 return INVALID_OPERATION;
699 }
700 }
701 callbacks.add(callback);
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100702 callbacks.resetNotifiedOnce();
Eric Laurent296fb132015-05-01 11:38:42 -0700703 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
704 return NO_ERROR;
705}
706
707status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -0700708 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -0700709{
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100710 Mutex::Autolock _l(mCallbacksLock);
Eric Laurent296fb132015-05-01 11:38:42 -0700711 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
712 if (ioIndex < 0) {
713 return INVALID_OPERATION;
714 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100715 AudioDeviceCallbacks callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
Eric Laurent296fb132015-05-01 11:38:42 -0700716
717 size_t cbIndex;
718 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
Eric Laurentad2e7b92017-09-14 20:06:42 -0700719 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700720 break;
721 }
722 }
723 if (cbIndex == callbacks.size()) {
724 return INVALID_OPERATION;
725 }
726 callbacks.removeAt(cbIndex);
727 if (callbacks.size() != 0) {
728 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
729 } else {
730 mAudioDeviceCallbacks.removeItem(audioIo);
731 }
732 return NO_ERROR;
733}
734
735/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700736{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700737 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 gAudioErrorCallback = cb;
739}
740
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700741/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
742{
743 Mutex::Autolock _l(gLock);
744 gDynPolicyCallback = cb;
745}
746
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800747/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
748{
749 Mutex::Autolock _l(gLock);
750 gRecordConfigCallback = cb;
751}
752
Eric Laurentc2f1f072009-07-17 12:17:14 -0700753// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800754// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
756sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
757
758
Glenn Kasten18a6d902012-09-24 11:27:56 -0700759// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800760const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700761{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800762 sp<IAudioPolicyService> ap;
763 sp<AudioPolicyServiceClient> apc;
764 {
765 Mutex::Autolock _l(gLockAPS);
766 if (gAudioPolicyService == 0) {
767 sp<IServiceManager> sm = defaultServiceManager();
768 sp<IBinder> binder;
769 do {
770 binder = sm->getService(String16("media.audio_policy"));
771 if (binder != 0)
772 break;
773 ALOGW("AudioPolicyService not published, waiting...");
774 usleep(500000); // 0.5 s
775 } while (true);
776 if (gAudioPolicyServiceClient == NULL) {
777 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
778 }
779 binder->linkToDeath(gAudioPolicyServiceClient);
780 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
781 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
782 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700783 // Make sure callbacks can be received by gAudioPolicyServiceClient
784 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700785 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800786 ap = gAudioPolicyService;
787 }
788 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200789 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800790 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200791 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
792 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700793 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800794
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800795 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700796}
797
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700798// ---------------------------------------------------------------------------
799
Dima Zavinfce7a472011-04-19 22:30:36 -0700800status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
801 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800802 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800803 const char *device_name,
804 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700805{
806 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700807 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800808 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700809
Eric Laurentc2f1f072009-07-17 12:17:14 -0700810 if (aps == 0) return PERMISSION_DENIED;
811
Eric Laurent71b63e32011-09-02 14:20:56 -0700812 if (device_address != NULL) {
813 address = device_address;
814 }
Paul McLeane743a472015-01-28 11:07:31 -0800815 if (device_name != NULL) {
816 name = device_name;
817 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800818 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700819}
820
Dima Zavinfce7a472011-04-19 22:30:36 -0700821audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822 const char *device_address)
823{
824 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700825 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700826
827 return aps->getDeviceConnectionState(device, device_address);
828}
829
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800830status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
831 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800832 const char *device_name,
833 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800834{
835 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
836 const char *address = "";
837 const char *name = "";
838
839 if (aps == 0) return PERMISSION_DENIED;
840
841 if (device_address != NULL) {
842 address = device_address;
843 }
844 if (device_name != NULL) {
845 name = device_name;
846 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800847 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800848}
849
Glenn Kastenf78aee72012-01-04 11:00:47 -0800850status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700851{
Glenn Kasten347966c2012-01-18 14:58:32 -0800852 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700853 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
854 if (aps == 0) return PERMISSION_DENIED;
855
856 return aps->setPhoneState(state);
857}
858
Dima Zavinfce7a472011-04-19 22:30:36 -0700859status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700860{
861 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
862 if (aps == 0) return PERMISSION_DENIED;
863 return aps->setForceUse(usage, config);
864}
865
Dima Zavinfce7a472011-04-19 22:30:36 -0700866audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700867{
868 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700869 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700870 return aps->getForceUse(usage);
871}
872
873
Eric Laurentf4e63452017-11-06 19:31:46 +0000874audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700875{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700876 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
877 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000878 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700879}
880
Eric Laurente83b55d2014-11-14 10:06:21 -0800881status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
882 audio_io_handle_t *output,
883 audio_session_t session,
884 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200885 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700886 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800887 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800888 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700889 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800890 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700891{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700892 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800893 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200894 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800895 config,
896 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700897}
898
Eric Laurentd7fe0862018-07-14 16:48:01 -0700899status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700900{
901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
902 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700903 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904}
905
Eric Laurentd7fe0862018-07-14 16:48:01 -0700906status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907{
908 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
909 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700910 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911}
912
Eric Laurentd7fe0862018-07-14 16:48:01 -0700913void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700914{
915 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
916 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700917 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918}
919
Eric Laurentcaf7f482014-11-25 17:50:47 -0800920status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
921 audio_io_handle_t *input,
922 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700923 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700924 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800925 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800926 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600927 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700928 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800929 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700930{
931 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800932 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600933 return aps->getInputForAttr(
Eric Laurentfee19762018-01-29 18:44:13 -0800934 attr, input, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800935 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700936}
937
Eric Laurent4eb58f12018-12-07 16:41:02 -0800938status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700939{
940 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
941 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800942 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943}
944
Eric Laurentfee19762018-01-29 18:44:13 -0800945status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700946{
947 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
948 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800949 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700950}
951
Eric Laurentfee19762018-01-29 18:44:13 -0800952void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953{
954 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
955 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800956 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957}
958
Dima Zavinfce7a472011-04-19 22:30:36 -0700959status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960 int indexMin,
961 int indexMax)
962{
963 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
964 if (aps == 0) return PERMISSION_DENIED;
965 return aps->initStreamVolume(stream, indexMin, indexMax);
966}
967
Eric Laurent83844cc2011-11-18 16:43:31 -0800968status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
969 int index,
970 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700971{
972 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
973 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800974 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700975}
976
Eric Laurent83844cc2011-11-18 16:43:31 -0800977status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
978 int *index,
979 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980{
981 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
982 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800983 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984}
985
Dima Zavinfce7a472011-04-19 22:30:36 -0700986uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700987{
988 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +0100989 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -0700990 return aps->getStrategyForStream(stream);
991}
992
Eric Laurent63742522012-03-08 13:42:42 -0800993audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800994{
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -0800996 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800997 return aps->getDevicesForStream(stream);
998}
999
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001000audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001001{
1002 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001003 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001004 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001005 return aps->getOutputForEffect(desc);
1006}
1007
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001008status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001009 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001010 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001011 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001012 int id)
1013{
1014 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1015 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001016 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001017}
1018
1019status_t AudioSystem::unregisterEffect(int id)
1020{
1021 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1022 if (aps == 0) return PERMISSION_DENIED;
1023 return aps->unregisterEffect(id);
1024}
1025
Eric Laurentdb7c0792011-08-10 10:37:50 -07001026status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1027{
1028 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1029 if (aps == 0) return PERMISSION_DENIED;
1030 return aps->setEffectEnabled(id, enabled);
1031}
1032
Glenn Kastenfff6d712012-01-12 16:38:12 -08001033status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001034{
Eric Laurenteda6c362011-02-02 09:33:30 -08001035 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1036 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001037 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001038 *state = aps->isStreamActive(stream, inPastMs);
1039 return NO_ERROR;
1040}
1041
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001042status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1043 uint32_t inPastMs)
1044{
1045 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1046 if (aps == 0) return PERMISSION_DENIED;
1047 if (state == NULL) return BAD_VALUE;
1048 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1049 return NO_ERROR;
1050}
1051
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001052status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1053{
1054 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1055 if (aps == 0) return PERMISSION_DENIED;
1056 if (state == NULL) return BAD_VALUE;
1057 *state = aps->isSourceActive(stream);
1058 return NO_ERROR;
1059}
1060
Glenn Kasten3b16c762012-11-14 08:44:39 -08001061uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001062{
1063 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1064 if (af == 0) return 0;
1065 return af->getPrimaryOutputSamplingRate();
1066}
1067
Glenn Kastene33054e2012-11-14 12:54:39 -08001068size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001069{
1070 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1071 if (af == 0) return 0;
1072 return af->getPrimaryOutputFrameCount();
1073}
Eric Laurenteda6c362011-02-02 09:33:30 -08001074
Andy Hung6f248bb2018-01-23 14:04:37 -08001075status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001076{
1077 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1078 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001079 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001080}
1081
Eric Laurent9f6530f2011-08-30 10:18:54 -07001082void AudioSystem::clearAudioConfigCache()
1083{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001084 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001086 {
1087 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001088 if (gAudioFlingerClient != 0) {
1089 gAudioFlingerClient->clearIoCache();
1090 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001091 gAudioFlinger.clear();
1092 }
1093 {
1094 Mutex::Autolock _l(gLockAPS);
1095 gAudioPolicyService.clear();
1096 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001097}
1098
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001099bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1100{
1101 ALOGV("isOffloadSupported()");
1102 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1103 if (aps == 0) return false;
1104 return aps->isOffloadSupported(info);
1105}
1106
Eric Laurent203b1a12014-04-01 10:34:16 -07001107status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1108 audio_port_type_t type,
1109 unsigned int *num_ports,
1110 struct audio_port *ports,
1111 unsigned int *generation)
1112{
1113 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1114 if (aps == 0) return PERMISSION_DENIED;
1115 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1116}
1117
1118status_t AudioSystem::getAudioPort(struct audio_port *port)
1119{
1120 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1121 if (aps == 0) return PERMISSION_DENIED;
1122 return aps->getAudioPort(port);
1123}
1124
1125status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1126 audio_patch_handle_t *handle)
1127{
1128 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1129 if (aps == 0) return PERMISSION_DENIED;
1130 return aps->createAudioPatch(patch, handle);
1131}
1132
1133status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1134{
1135 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1136 if (aps == 0) return PERMISSION_DENIED;
1137 return aps->releaseAudioPatch(handle);
1138}
1139
1140status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1141 struct audio_patch *patches,
1142 unsigned int *generation)
1143{
1144 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1145 if (aps == 0) return PERMISSION_DENIED;
1146 return aps->listAudioPatches(num_patches, patches, generation);
1147}
1148
1149status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1150{
1151 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1152 if (aps == 0) return PERMISSION_DENIED;
1153 return aps->setAudioPortConfig(config);
1154}
1155
Eric Laurent296fb132015-05-01 11:38:42 -07001156status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001157{
Eric Laurentb28753e2015-04-01 13:06:28 -07001158 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1159 if (aps == 0) return PERMISSION_DENIED;
1160
1161 Mutex::Autolock _l(gLockAPS);
1162 if (gAudioPolicyServiceClient == 0) {
1163 return NO_INIT;
1164 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001165 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1166 if (ret == 1) {
1167 aps->setAudioPortCallbacksEnabled(true);
1168 }
1169 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001170}
1171
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001172/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001173status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001174{
1175 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1176 if (aps == 0) return PERMISSION_DENIED;
1177
1178 Mutex::Autolock _l(gLockAPS);
1179 if (gAudioPolicyServiceClient == 0) {
1180 return NO_INIT;
1181 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001182 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1183 if (ret == 0) {
1184 aps->setAudioPortCallbacksEnabled(false);
1185 }
1186 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001187}
1188
1189status_t AudioSystem::addAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001190 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001191{
1192 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1193 if (afc == 0) {
1194 return NO_INIT;
1195 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001196 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1197 if (status == NO_ERROR) {
1198 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1199 if (af != 0) {
1200 af->registerClient(afc);
1201 }
1202 }
1203 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001204}
1205
1206status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurentad2e7b92017-09-14 20:06:42 -07001207 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
Eric Laurent296fb132015-05-01 11:38:42 -07001208{
1209 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1210 if (afc == 0) {
1211 return NO_INIT;
1212 }
1213 return afc->removeAudioDeviceCallback(callback, audioIo);
1214}
1215
1216audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1217{
1218 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1219 if (af == 0) return PERMISSION_DENIED;
1220 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1221 if (desc == 0) {
1222 return AUDIO_PORT_HANDLE_NONE;
1223 }
1224 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001225}
1226
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001227status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1228 audio_io_handle_t *ioHandle,
1229 audio_devices_t *device)
1230{
1231 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1232 if (aps == 0) return PERMISSION_DENIED;
1233 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1234}
1235
1236status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1237{
1238 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1239 if (aps == 0) return PERMISSION_DENIED;
1240 return aps->releaseSoundTriggerSession(session);
1241}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001242
1243audio_mode_t AudioSystem::getPhoneState()
1244{
1245 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1246 if (aps == 0) return AUDIO_MODE_INVALID;
1247 return aps->getPhoneState();
1248}
1249
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001250status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001251{
1252 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1253 if (aps == 0) return PERMISSION_DENIED;
1254 return aps->registerPolicyMixes(mixes, registration);
1255}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001256
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001257status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1258{
1259 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1260 if (aps == 0) return PERMISSION_DENIED;
1261 return aps->setUidDeviceAffinities(uid, devices);
1262}
1263
1264status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1265 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1266 if (aps == 0) return PERMISSION_DENIED;
1267 return aps->removeUidDeviceAffinities(uid);
1268}
1269
Eric Laurent554a2772015-04-10 11:29:24 -07001270status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1271 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001272 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001273{
1274 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1275 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001276 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001277}
1278
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001279status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001280{
1281 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1282 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001283 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001284}
1285
Andy Hung2ddee192015-12-18 17:34:44 -08001286status_t AudioSystem::setMasterMono(bool mono)
1287{
1288 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1289 if (aps == 0) return PERMISSION_DENIED;
1290 return aps->setMasterMono(mono);
1291}
1292
1293status_t AudioSystem::getMasterMono(bool *mono)
1294{
1295 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1296 if (aps == 0) return PERMISSION_DENIED;
1297 return aps->getMasterMono(mono);
1298}
1299
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001300status_t AudioSystem::setMasterBalance(float balance)
1301{
1302 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1303 if (af == 0) return PERMISSION_DENIED;
1304 return af->setMasterBalance(balance);
1305}
1306
1307status_t AudioSystem::getMasterBalance(float *balance)
1308{
1309 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1310 if (af == 0) return PERMISSION_DENIED;
1311 return af->getMasterBalance(balance);
1312}
1313
Eric Laurentac9cef52017-06-09 15:46:26 -07001314float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1315{
1316 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1317 if (aps == 0) return NAN;
1318 return aps->getStreamVolumeDB(stream, index, device);
1319}
1320
jiabin46a76fa2018-01-05 10:18:21 -08001321status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1322{
1323 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1324 if (af == 0) return PERMISSION_DENIED;
1325 return af->getMicrophones(microphones);
1326}
1327
jiabin81772902018-04-02 17:52:27 -07001328status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1329 audio_format_t *surroundFormats,
1330 bool *surroundFormatsEnabled,
1331 bool reported)
1332{
1333 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1334 if (aps == 0) return PERMISSION_DENIED;
1335 return aps->getSurroundFormats(
1336 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1337}
1338
1339status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1340{
1341 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1342 if (aps == 0) return PERMISSION_DENIED;
1343 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1344}
1345
Eric Laurentb78763e2018-10-17 10:08:02 -07001346status_t AudioSystem::setAssistantUid(uid_t uid)
1347{
1348 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1349 if (aps == 0) return PERMISSION_DENIED;
1350
1351 return aps->setAssistantUid(uid);
1352}
1353
1354status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1355{
1356 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1357 if (aps == 0) return PERMISSION_DENIED;
1358
1359 return aps->setA11yServicesUids(uids);
1360}
1361
jiabin6012f912018-11-02 17:06:30 -07001362bool AudioSystem::isHapticPlaybackSupported()
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return false;
1366 return aps->isHapticPlaybackSupported();
1367}
1368
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001369status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001370 std::vector<audio_format_t> *formats) {
1371 const sp <IAudioPolicyService>
1372 & aps = AudioSystem::get_audio_policy_service();
1373 if (aps == 0) return PERMISSION_DENIED;
1374 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1375}
1376
1377status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001378{
1379 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1380 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001381 return aps->listAudioProductStrategies(strategies);
1382}
1383
1384audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1385{
1386 AudioProductStrategyVector strategies;
1387 listAudioProductStrategies(strategies);
1388 for (const auto &strategy : strategies) {
1389 auto attrVect = strategy.getAudioAttributes();
1390 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1391 return attributes.getStreamType() == stream; });
1392 if (iter != end(attrVect)) {
1393 return iter->getAttributes();
1394 }
1395 }
1396 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1397 return AUDIO_ATTRIBUTES_INITIALIZER;
1398}
1399
1400audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1401{
1402 product_strategy_t strategyId =
1403 AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr));
1404 AudioProductStrategyVector strategies;
1405 listAudioProductStrategies(strategies);
1406 for (const auto &strategy : strategies) {
1407 if (strategy.getId() == strategyId) {
1408 auto attrVect = strategy.getAudioAttributes();
1409 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1410 return AudioProductStrategy::attributesMatches(
1411 refAttr.getAttributes(), attr); });
1412 if (iter != end(attrVect)) {
1413 return iter->getStreamType();
1414 }
1415 }
1416 }
1417 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1418 return AUDIO_STREAM_MUSIC;
1419}
1420
1421product_strategy_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa)
1422{
1423 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1424 if (aps == 0) return PRODUCT_STRATEGY_NONE;
1425 return aps->getProductStrategyFromAudioAttributes(aa);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001426}
Eric Laurentb78763e2018-10-17 10:08:02 -07001427
Eric Laurentc2f1f072009-07-17 12:17:14 -07001428// ---------------------------------------------------------------------------
1429
Eric Laurente8726fe2015-06-26 09:39:24 -07001430int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001431 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001432{
1433 Mutex::Autolock _l(mLock);
1434 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001435 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001436 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001437 }
1438 }
Eric Laurent296fb132015-05-01 11:38:42 -07001439 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001440 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001441}
1442
Eric Laurente8726fe2015-06-26 09:39:24 -07001443int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001444 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001445{
1446 Mutex::Autolock _l(mLock);
1447 size_t i;
1448 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001449 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001450 break;
1451 }
1452 }
1453 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001454 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001455 }
1456 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001457 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001458}
1459
Eric Laurent296fb132015-05-01 11:38:42 -07001460
Eric Laurentb28753e2015-04-01 13:06:28 -07001461void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1462{
1463 Mutex::Autolock _l(mLock);
1464 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1465 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1466 }
1467}
1468
1469void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1470{
1471 Mutex::Autolock _l(mLock);
1472 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1473 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1474 }
1475}
1476
Jean-Michel Trivide801052015-04-14 19:10:14 -07001477void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1478 String8 regId, int32_t state)
1479{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001480 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1481 dynamic_policy_callback cb = NULL;
1482 {
1483 Mutex::Autolock _l(AudioSystem::gLock);
1484 cb = gDynPolicyCallback;
1485 }
1486
1487 if (cb != NULL) {
1488 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1489 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001490}
1491
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001492void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001493 int event,
1494 const record_client_info_t *clientInfo,
1495 const audio_config_base_t *clientConfig,
1496 std::vector<effect_descriptor_t> clientEffects,
1497 const audio_config_base_t *deviceConfig,
1498 std::vector<effect_descriptor_t> effects,
1499 audio_patch_handle_t patchHandle,
1500 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001501 record_config_callback cb = NULL;
1502 {
1503 Mutex::Autolock _l(AudioSystem::gLock);
1504 cb = gRecordConfigCallback;
1505 }
1506
1507 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001508 cb(event, clientInfo, clientConfig, clientEffects,
1509 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001510 }
1511}
1512
Glenn Kasten4944acb2013-08-19 08:39:20 -07001513void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1514{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001515 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001516 Mutex::Autolock _l(mLock);
1517 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1518 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001519 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001520 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001521 {
1522 Mutex::Autolock _l(gLockAPS);
1523 AudioSystem::gAudioPolicyService.clear();
1524 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001525
Steve Block5ff1dd52012-01-05 23:22:43 +00001526 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001527}
1528
Glenn Kasten40bc9062015-03-20 09:09:33 -07001529} // namespace android