blob: 05078796327cd97fa7ce376412c0ee591f992e35 [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>
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070021
22#include <android/media/BnCaptureStateListener.h>
Mathias Agopian75624082009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070024#include <binder/ProcessState.h>
François Gaffie24437602018-04-23 15:08:59 +020025#include <binder/IPCThreadState.h>
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070026#include <media/AidlConversion.h>
Eric Laurent21da6472017-11-09 16:29:26 -080027#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <media/AudioSystem.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070029#include <media/IAudioFlinger.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070030#include <media/IAudioPolicyService.h>
François Gaffied0ba9ed2018-11-05 11:50:42 +010031#include <media/TypeConverter.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <math.h>
33
Dima Zavin64760242011-05-11 14:15:23 -070034#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070035
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070036#define VALUE_OR_RETURN(x) \
37 ({ auto _tmp = (x); \
38 if (!_tmp.ok()) return Status::fromStatusT(_tmp.error()); \
39 _tmp.value(); })
40
Eric Laurentc2f1f072009-07-17 12:17:14 -070041// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070042
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043namespace android {
44
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070045using binder::Status;
46
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047// client singleton for AudioFlinger binder interface
48Mutex AudioSystem::gLock;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080049Mutex AudioSystem::gLockErrorCallbacks;
Glenn Kastend2d089f2014-11-05 11:48:12 -080050Mutex AudioSystem::gLockAPS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051sp<IAudioFlinger> AudioSystem::gAudioFlinger;
52sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -080053std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks;
Jean-Michel Trivif613d422015-04-23 18:41:29 -070054dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055record_config_callback AudioSystem::gRecordConfigCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070057// Required to be held while calling into gSoundTriggerCaptureStateListener.
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070058class CaptureStateListenerImpl;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070059Mutex gSoundTriggerCaptureStateListenerLock;
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070060sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070061
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080063const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080065 sp<IAudioFlinger> af;
66 sp<AudioFlingerClient> afc;
Mikhail Naganov69330d42020-04-08 19:29:50 +000067 bool reportNoError = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -080068 {
69 Mutex::Autolock _l(gLock);
70 if (gAudioFlinger == 0) {
71 sp<IServiceManager> sm = defaultServiceManager();
72 sp<IBinder> binder;
73 do {
74 binder = sm->getService(String16("media.audio_flinger"));
75 if (binder != 0)
76 break;
77 ALOGW("AudioFlinger not published, waiting...");
78 usleep(500000); // 0.5 s
79 } while (true);
80 if (gAudioFlingerClient == NULL) {
81 gAudioFlingerClient = new AudioFlingerClient();
82 } else {
Mikhail Naganov69330d42020-04-08 19:29:50 +000083 reportNoError = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080084 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080085 binder->linkToDeath(gAudioFlingerClient);
86 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
87 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
88 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070089 // Make sure callbacks can be received by gAudioFlingerClient
90 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070091 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080092 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080094 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020095 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080096 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020097 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -080098 }
Mikhail Naganov69330d42020-04-08 19:29:50 +000099 if (reportNoError) reportError(NO_ERROR);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800100 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101}
102
Eric Laurent296fb132015-05-01 11:38:42 -0700103const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
104{
105 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
106 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
107 if (af == 0) return 0;
108 Mutex::Autolock _l(gLock);
109 return gAudioFlingerClient;
110}
111
112sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
113{
114 sp<AudioIoDescriptor> desc;
115 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
116 if (afc != 0) {
117 desc = afc->getIoDescriptor(ioHandle);
118 }
119 return desc;
120}
121
Eric Laurent46291612013-07-18 14:38:44 -0700122/* static */ status_t AudioSystem::checkAudioFlinger()
123{
124 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
125 return NO_ERROR;
126 }
127 return DEAD_OBJECT;
128}
129
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700130// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
131
Glenn Kasten4944acb2013-08-19 08:39:20 -0700132status_t AudioSystem::muteMicrophone(bool state)
133{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
135 if (af == 0) return PERMISSION_DENIED;
136 return af->setMicMute(state);
137}
138
Glenn Kasten4944acb2013-08-19 08:39:20 -0700139status_t AudioSystem::isMicrophoneMuted(bool* state)
140{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
143 *state = af->getMicMute();
144 return NO_ERROR;
145}
146
147status_t AudioSystem::setMasterVolume(float value)
148{
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
151 af->setMasterVolume(value);
152 return NO_ERROR;
153}
154
155status_t AudioSystem::setMasterMute(bool mute)
156{
157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
159 af->setMasterMute(mute);
160 return NO_ERROR;
161}
162
163status_t AudioSystem::getMasterVolume(float* volume)
164{
165 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
166 if (af == 0) return PERMISSION_DENIED;
167 *volume = af->masterVolume();
168 return NO_ERROR;
169}
170
171status_t AudioSystem::getMasterMute(bool* mute)
172{
173 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
174 if (af == 0) return PERMISSION_DENIED;
175 *mute = af->masterMute();
176 return NO_ERROR;
177}
178
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800179status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
180 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181{
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
184 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700185 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 return NO_ERROR;
187}
188
Glenn Kastenfff6d712012-01-12 16:38:12 -0800189status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190{
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
193 if (af == 0) return PERMISSION_DENIED;
194 af->setStreamMute(stream, mute);
195 return NO_ERROR;
196}
197
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800198status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
199 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200{
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
203 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700204 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 return NO_ERROR;
206}
207
Glenn Kastenfff6d712012-01-12 16:38:12 -0800208status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209{
Dima Zavinfce7a472011-04-19 22:30:36 -0700210 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
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;
213 *mute = af->streamMute(stream);
214 return NO_ERROR;
215}
216
Glenn Kastenf78aee72012-01-04 11:00:47 -0800217status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800218{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800219 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
221 if (af == 0) return PERMISSION_DENIED;
222 return af->setMode(mode);
223}
224
Glenn Kasten4944acb2013-08-19 08:39:20 -0700225status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
226{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
228 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700229 return af->setParameters(ioHandle, keyValuePairs);
230}
231
Glenn Kasten4944acb2013-08-19 08:39:20 -0700232String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
233{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
235 String8 result = String8("");
236 if (af == 0) return result;
237
238 result = af->getParameters(ioHandle, keys);
239 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240}
241
Glenn Kastenc23885e2013-12-19 16:35:18 -0800242status_t AudioSystem::setParameters(const String8& keyValuePairs)
243{
Glenn Kasten142f5192014-03-25 17:44:59 -0700244 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800245}
246
247String8 AudioSystem::getParameters(const String8& keys)
248{
Glenn Kasten142f5192014-03-25 17:44:59 -0700249 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800250}
251
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252// convert volume steps to natural log scale
253
254// change this value to change volume scaling
255static const float dBPerStep = 0.5f;
256// shouldn't need to touch these
257static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
258static const float dBConvertInverse = 1.0f / dBConvert;
259
260float AudioSystem::linearToLog(int volume)
261{
262 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000263 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 // return v;
265 return volume ? exp(float(100 - volume) * dBConvert) : 0;
266}
267
268int AudioSystem::logToLinear(float volume)
269{
270 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000271 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 // return v;
273 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
274}
275
Eric Laurent21da6472017-11-09 16:29:26 -0800276/* static */ size_t AudioSystem::calculateMinFrameCount(
277 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
278 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
279{
280 // Ensure that buffer depth covers at least audio hardware latency
281 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
282 if (minBufCount < 2) {
283 minBufCount = 2;
284 }
285#if 0
286 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
287 // but keeping the code here to make it easier to add later.
288 if (minBufCount < notificationsPerBufferReq) {
289 minBufCount = notificationsPerBufferReq;
290 }
291#endif
292 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
293 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
294 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
295 /*, notificationsPerBufferReq*/);
296 return minBufCount * sourceFramesNeededWithTimestretch(
297 sampleRate, afFrameCount, afSampleRate, speed);
298}
299
300
Glenn Kasten3b16c762012-11-14 08:44:39 -0800301status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304
Dima Zavinfce7a472011-04-19 22:30:36 -0700305 if (streamType == AUDIO_STREAM_DEFAULT) {
306 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307 }
308
Glenn Kastenfff6d712012-01-12 16:38:12 -0800309 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310 if (output == 0) {
311 return PERMISSION_DENIED;
312 }
313
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700314 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700315}
316
Glenn Kasten2c073da2016-02-26 09:14:08 -0800317status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800318 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700319{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800320 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
321 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800322 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
323 if (desc == 0) {
324 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800326 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800328 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800329 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800330 return BAD_VALUE;
331 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332
Glenn Kasten2c073da2016-02-26 09:14:08 -0800333 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 return NO_ERROR;
336}
337
Glenn Kastene33054e2012-11-14 12:54:39 -0800338status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341
Dima Zavinfce7a472011-04-19 22:30:36 -0700342 if (streamType == AUDIO_STREAM_DEFAULT) {
343 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700345
Glenn Kastenfff6d712012-01-12 16:38:12 -0800346 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700347 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 return PERMISSION_DENIED;
349 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700351 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700352}
353
Glenn Kasten2c073da2016-02-26 09:14:08 -0800354status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800355 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700356{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800357 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
358 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800359 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
360 if (desc == 0) {
361 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800363 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800365 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800366 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800367 return BAD_VALUE;
368 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369
Glenn Kasten2c073da2016-02-26 09:14:08 -0800370 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700371
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 return NO_ERROR;
373}
374
Glenn Kastenfff6d712012-01-12 16:38:12 -0800375status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700377 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378
Dima Zavinfce7a472011-04-19 22:30:36 -0700379 if (streamType == AUDIO_STREAM_DEFAULT) {
380 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700381 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700382
Glenn Kastenfff6d712012-01-12 16:38:12 -0800383 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700384 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 return PERMISSION_DENIED;
386 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387
Glenn Kasten241618f2014-03-25 17:48:57 -0700388 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700389}
390
391status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700392 uint32_t* latency)
393{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800394 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
395 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700396 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700397 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700398 *latency = af->latency(output);
399 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700400 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700401 }
402
Glenn Kasten241618f2014-03-25 17:48:57 -0700403 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700404
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405 return NO_ERROR;
406}
407
Glenn Kastendd8104c2012-07-02 12:42:44 -0700408status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
409 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410{
Eric Laurent296fb132015-05-01 11:38:42 -0700411 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
412 if (afc == 0) {
413 return NO_INIT;
414 }
415 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416}
417
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700418status_t AudioSystem::setVoiceVolume(float value)
419{
420 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
421 if (af == 0) return PERMISSION_DENIED;
422 return af->setVoiceVolume(value);
423}
424
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000425status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700426 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800427{
428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
429 if (af == 0) return PERMISSION_DENIED;
430
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000431 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800432}
433
Glenn Kasten4944acb2013-08-19 08:39:20 -0700434uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
435{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800436 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800437 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800438 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700439 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800440
441 result = af->getInputFramesLost(ioHandle);
442 return result;
443}
444
Glenn Kasteneeecb982016-02-26 10:44:04 -0800445audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700446{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700447 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700448 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700449 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800450 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700451}
452
Andy Hung8b0bfd92019-12-23 13:11:11 -0800453void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700454{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700455 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800457 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700458 }
459}
460
Glenn Kastend848eb42016-03-08 13:42:11 -0800461void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700462{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700463 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
464 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800465 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700466 }
467}
468
Eric Laurent93c3d412014-08-01 14:48:35 -0700469audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
470{
471 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
472 if (af == 0) return AUDIO_HW_SYNC_INVALID;
473 return af->getAudioHwSyncForSession(sessionId);
474}
475
Eric Laurent72e3f392015-05-20 14:43:50 -0700476status_t AudioSystem::systemReady()
477{
478 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
479 if (af == 0) return NO_INIT;
480 return af->systemReady();
481}
482
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700483status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
484 size_t* frameCount)
485{
486 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
487 if (af == 0) return PERMISSION_DENIED;
488 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
489 if (desc == 0) {
490 *frameCount = af->frameCountHAL(ioHandle);
491 } else {
492 *frameCount = desc->mFrameCountHAL;
493 }
494 if (*frameCount == 0) {
495 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
496 return BAD_VALUE;
497 }
498
499 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
500
501 return NO_ERROR;
502}
503
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504// ---------------------------------------------------------------------------
505
Eric Laurent73e26b62015-04-27 16:55:58 -0700506
507void AudioSystem::AudioFlingerClient::clearIoCache()
508{
509 Mutex::Autolock _l(mLock);
510 mIoDescriptors.clear();
511 mInBuffSize = 0;
512 mInSamplingRate = 0;
513 mInFormat = AUDIO_FORMAT_DEFAULT;
514 mInChannelMask = AUDIO_CHANNEL_NONE;
515}
516
Glenn Kasten4944acb2013-08-19 08:39:20 -0700517void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
518{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800519 {
520 Mutex::Autolock _l(AudioSystem::gLock);
521 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800522 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523
Eric Laurent73e26b62015-04-27 16:55:58 -0700524 // clear output handles and stream to output map caches
525 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800527 reportError(DEAD_OBJECT);
528
Steve Block5ff1dd52012-01-05 23:22:43 +0000529 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530}
531
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700532Status AudioSystem::AudioFlingerClient::ioConfigChanged(
533 media::AudioIoConfigEvent _event,
534 const media::AudioIoDescriptor& _ioDesc) {
535 audio_io_config_event event = VALUE_OR_RETURN(
536 aidl2legacy_AudioIoConfigEvent_audio_io_config_event(_event));
537 sp<AudioIoDescriptor> ioDesc(
538 VALUE_OR_RETURN(aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
539
Steve Block3856b092011-10-20 11:56:00 +0100540 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700541
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700542 if (ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700543
Eric Laurent296fb132015-05-01 11:38:42 -0700544 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700545 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700546 {
547 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700548 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700549
550 switch (event) {
551 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700552 case AUDIO_OUTPUT_REGISTERED:
553 case AUDIO_INPUT_OPENED:
554 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700555 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700556 if (oldDesc == 0) {
557 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
558 } else {
559 deviceId = oldDesc->getDeviceId();
560 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700561 }
Eric Laurent296fb132015-05-01 11:38:42 -0700562
563 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
564 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700565 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700566 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
567 if (it != mAudioDeviceCallbacks.end()) {
568 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100569 }
570 }
Eric Laurent296fb132015-05-01 11:38:42 -0700571 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700572 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
573 "frameCount %zu deviceId %d",
574 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
575 "output" : "input",
576 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
577 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700578 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
579 ioDesc->mFrameCount, ioDesc->getDeviceId());
580 } break;
581 case AUDIO_OUTPUT_CLOSED:
582 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700583 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700584 ALOGW("ioConfigChanged() closing unknown %s %d",
585 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
586 break;
587 }
588 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700589 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700590
Eric Laurent296fb132015-05-01 11:38:42 -0700591 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700592 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700593 } break;
594
595 case AUDIO_OUTPUT_CONFIG_CHANGED:
596 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700597 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700598 if (oldDesc == 0) {
Dean Wheatley8f992a12020-05-08 20:33:51 +1000599 ALOGW("ioConfigChanged() modifying unknown %s! %d",
600 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700601 break;
602 }
603
604 deviceId = oldDesc->getDeviceId();
605 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
606
607 if (deviceId != ioDesc->getDeviceId()) {
608 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700609 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
610 if (it != mAudioDeviceCallbacks.end()) {
611 callbacks = it->second;
612 }
Eric Laurent296fb132015-05-01 11:38:42 -0700613 }
614 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700615 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700616 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
617 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800618 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
619 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700620
Eric Laurentc2f1f072009-07-17 12:17:14 -0700621 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700622 case AUDIO_CLIENT_STARTED: {
623 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
624 if (oldDesc == 0) {
625 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
626 break;
627 }
628 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
629 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
630 oldDesc->mPatch = ioDesc->mPatch;
631 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
632 if (it != mAudioDeviceCallbacks.end()) {
633 auto cbks = it->second;
634 auto it2 = cbks.find(ioDesc->mPortId);
635 if (it2 != cbks.end()) {
636 callbacks.emplace(ioDesc->mPortId, it2->second);
637 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100638 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100639 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700640 } break;
641 }
642
643 for (auto wpCbk : callbacks) {
644 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
645 if (spCbk != nullptr) {
646 callbacksToCall.push_back(spCbk);
647 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700648 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800649 }
650
651 // Callbacks must be called without mLock held. May lead to dead lock if calling for
652 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700653 for (auto cb : callbacksToCall) {
654 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
655 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700656 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700657
658 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659}
660
Eric Laurent73e26b62015-04-27 16:55:58 -0700661status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
662 uint32_t sampleRate, audio_format_t format,
663 audio_channel_mask_t channelMask, size_t* buffSize)
664{
665 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
666 if (af == 0) {
667 return PERMISSION_DENIED;
668 }
669 Mutex::Autolock _l(mLock);
670 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
671 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
672 || (channelMask != mInChannelMask)) {
673 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
674 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800675 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700676 sampleRate, format, channelMask);
677 return BAD_VALUE;
678 }
679 // A benign race is possible here: we could overwrite a fresher cache entry
680 // save the request params
681 mInSamplingRate = sampleRate;
682 mInFormat = format;
683 mInChannelMask = channelMask;
684
685 mInBuffSize = inBuffSize;
686 }
687
688 *buffSize = mInBuffSize;
689
690 return NO_ERROR;
691}
692
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700693sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700694{
695 sp<AudioIoDescriptor> desc;
696 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
697 if (index >= 0) {
698 desc = mIoDescriptors.valueAt(index);
699 }
700 return desc;
701}
702
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700703sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
704{
705 Mutex::Autolock _l(mLock);
706 return getIoDescriptor_l(ioHandle);
707}
708
Eric Laurent296fb132015-05-01 11:38:42 -0700709status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700710 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
711 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700712{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700713 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800714 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700715 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
716 auto result = callbacks.try_emplace(portId, callback);
717 if (!result.second) {
718 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700719 }
Eric Laurent296fb132015-05-01 11:38:42 -0700720 return NO_ERROR;
721}
722
723status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700724 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
725 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700726{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700727 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800728 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700729 auto it = mAudioDeviceCallbacks.find(audioIo);
730 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700731 return INVALID_OPERATION;
732 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700733 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700734 return INVALID_OPERATION;
735 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700736 if (it->second.size() == 0) {
737 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700738 }
739 return NO_ERROR;
740}
741
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800742/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700743{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800744 Mutex::Autolock _l(gLockErrorCallbacks);
745 gAudioErrorCallbacks.insert(cb);
746 return reinterpret_cast<uintptr_t>(cb);
747}
748
749/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
750 Mutex::Autolock _l(gLockErrorCallbacks);
751 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
752}
753
754/* static */ void AudioSystem::reportError(status_t err) {
755 Mutex::Autolock _l(gLockErrorCallbacks);
756 for (auto callback : gAudioErrorCallbacks) {
757 callback(err);
758 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800759}
760
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700761/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
762{
763 Mutex::Autolock _l(gLock);
764 gDynPolicyCallback = cb;
765}
766
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800767/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
768{
769 Mutex::Autolock _l(gLock);
770 gRecordConfigCallback = cb;
771}
772
Eric Laurentc2f1f072009-07-17 12:17:14 -0700773// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800774// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700775sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
776sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
777
778
Glenn Kasten18a6d902012-09-24 11:27:56 -0700779// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800780const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700781{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800782 sp<IAudioPolicyService> ap;
783 sp<AudioPolicyServiceClient> apc;
784 {
785 Mutex::Autolock _l(gLockAPS);
786 if (gAudioPolicyService == 0) {
787 sp<IServiceManager> sm = defaultServiceManager();
788 sp<IBinder> binder;
789 do {
790 binder = sm->getService(String16("media.audio_policy"));
791 if (binder != 0)
792 break;
793 ALOGW("AudioPolicyService not published, waiting...");
794 usleep(500000); // 0.5 s
795 } while (true);
796 if (gAudioPolicyServiceClient == NULL) {
797 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
798 }
799 binder->linkToDeath(gAudioPolicyServiceClient);
800 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
801 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
802 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700803 // Make sure callbacks can be received by gAudioPolicyServiceClient
804 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700805 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800806 ap = gAudioPolicyService;
807 }
808 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200809 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800810 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200811 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100812 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200813 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700814 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800815
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800816 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700817}
818
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700819// ---------------------------------------------------------------------------
820
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000821void AudioSystem::onNewAudioModulesAvailable()
822{
823 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
824 if (aps == 0) return;
825 aps->onNewAudioModulesAvailable();
826}
827
Dima Zavinfce7a472011-04-19 22:30:36 -0700828status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
829 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800830 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800831 const char *device_name,
832 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700833{
834 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700835 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800836 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700837
Eric Laurentc2f1f072009-07-17 12:17:14 -0700838 if (aps == 0) return PERMISSION_DENIED;
839
Eric Laurent71b63e32011-09-02 14:20:56 -0700840 if (device_address != NULL) {
841 address = device_address;
842 }
Paul McLeane743a472015-01-28 11:07:31 -0800843 if (device_name != NULL) {
844 name = device_name;
845 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800846 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700847}
848
Dima Zavinfce7a472011-04-19 22:30:36 -0700849audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700850 const char *device_address)
851{
852 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700853 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700854
855 return aps->getDeviceConnectionState(device, device_address);
856}
857
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800858status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
859 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800860 const char *device_name,
861 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800862{
863 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
864 const char *address = "";
865 const char *name = "";
866
867 if (aps == 0) return PERMISSION_DENIED;
868
869 if (device_address != NULL) {
870 address = device_address;
871 }
872 if (device_name != NULL) {
873 name = device_name;
874 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800875 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800876}
877
Eric Laurent00dba062020-02-11 15:52:09 -0800878status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700879{
Glenn Kasten347966c2012-01-18 14:58:32 -0800880 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
882 if (aps == 0) return PERMISSION_DENIED;
883
Eric Laurent00dba062020-02-11 15:52:09 -0800884 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700885}
886
Dima Zavinfce7a472011-04-19 22:30:36 -0700887status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700888{
889 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
890 if (aps == 0) return PERMISSION_DENIED;
891 return aps->setForceUse(usage, config);
892}
893
Dima Zavinfce7a472011-04-19 22:30:36 -0700894audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700895{
896 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700897 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700898 return aps->getForceUse(usage);
899}
900
901
Eric Laurentf4e63452017-11-06 19:31:46 +0000902audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700903{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700904 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
905 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000906 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907}
908
Eric Laurent42984412019-05-09 17:57:03 -0700909status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800910 audio_io_handle_t *output,
911 audio_session_t session,
912 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200913 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700914 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800915 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800916 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700917 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800918 audio_port_handle_t *portId,
919 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700920{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700921 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800922 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200923 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700924 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800925 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700926}
927
Eric Laurentd7fe0862018-07-14 16:48:01 -0700928status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700929{
930 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
931 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700932 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700933}
934
Eric Laurentd7fe0862018-07-14 16:48:01 -0700935status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700936{
937 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
938 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700939 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700940}
941
Eric Laurentd7fe0862018-07-14 16:48:01 -0700942void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700943{
944 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
945 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700946 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947}
948
Eric Laurentcaf7f482014-11-25 17:50:47 -0800949status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
950 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700951 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800952 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700953 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700954 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800955 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800956 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600957 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700958 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800959 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960{
961 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800962 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600963 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700964 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800965 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966}
967
Eric Laurent4eb58f12018-12-07 16:41:02 -0800968status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700969{
970 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
971 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800972 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973}
974
Eric Laurentfee19762018-01-29 18:44:13 -0800975status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700976{
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800979 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980}
981
Eric Laurentfee19762018-01-29 18:44:13 -0800982void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700983{
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
985 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800986 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700987}
988
Dima Zavinfce7a472011-04-19 22:30:36 -0700989status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700990 int indexMin,
991 int indexMax)
992{
993 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
994 if (aps == 0) return PERMISSION_DENIED;
995 return aps->initStreamVolume(stream, indexMin, indexMax);
996}
997
Eric Laurent83844cc2011-11-18 16:43:31 -0800998status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
999 int index,
1000 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001001{
1002 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1003 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001004 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001005}
1006
Eric Laurent83844cc2011-11-18 16:43:31 -08001007status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
1008 int *index,
1009 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010{
1011 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1012 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001013 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001014}
1015
François Gaffiecfe17322018-11-07 13:41:29 +01001016status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
1017 int index,
1018 audio_devices_t device)
1019{
1020 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1021 if (aps == 0) return PERMISSION_DENIED;
1022 return aps->setVolumeIndexForAttributes(attr, index, device);
1023}
1024
1025status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1026 int &index,
1027 audio_devices_t device)
1028{
1029 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1030 if (aps == 0) return PERMISSION_DENIED;
1031 return aps->getVolumeIndexForAttributes(attr, index, device);
1032}
1033
1034status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1035{
1036 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1037 if (aps == 0) return PERMISSION_DENIED;
1038 return aps->getMaxVolumeIndexForAttributes(attr, index);
1039}
1040
1041status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1042{
1043 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1044 if (aps == 0) return PERMISSION_DENIED;
1045 return aps->getMinVolumeIndexForAttributes(attr, index);
1046}
1047
Dima Zavinfce7a472011-04-19 22:30:36 -07001048uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001049{
1050 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001051 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001052 return aps->getStrategyForStream(stream);
1053}
1054
Eric Laurent63742522012-03-08 13:42:42 -08001055audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001056{
1057 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001058 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001059 return aps->getDevicesForStream(stream);
1060}
1061
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001062status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1063 AudioDeviceTypeAddrVector *devices) {
1064 if (devices == nullptr) {
1065 return BAD_VALUE;
1066 }
1067 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1068 if (aps == 0) return PERMISSION_DENIED;
1069 return aps->getDevicesForAttributes(aa, devices);
1070}
1071
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001072audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001073{
1074 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001075 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001076 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001077 return aps->getOutputForEffect(desc);
1078}
1079
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001080status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001081 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001082 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001083 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001084 int id)
1085{
1086 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1087 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001088 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001089}
1090
1091status_t AudioSystem::unregisterEffect(int id)
1092{
1093 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1094 if (aps == 0) return PERMISSION_DENIED;
1095 return aps->unregisterEffect(id);
1096}
1097
Eric Laurentdb7c0792011-08-10 10:37:50 -07001098status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1099{
1100 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1101 if (aps == 0) return PERMISSION_DENIED;
1102 return aps->setEffectEnabled(id, enabled);
1103}
1104
Eric Laurent6c796322019-04-09 14:13:17 -07001105status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1106{
1107 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1108 if (aps == 0) return PERMISSION_DENIED;
1109 return aps->moveEffectsToIo(ids, io);
1110}
1111
Glenn Kastenfff6d712012-01-12 16:38:12 -08001112status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001113{
Eric Laurenteda6c362011-02-02 09:33:30 -08001114 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1115 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001116 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001117 *state = aps->isStreamActive(stream, inPastMs);
1118 return NO_ERROR;
1119}
1120
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001121status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1122 uint32_t inPastMs)
1123{
1124 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1125 if (aps == 0) return PERMISSION_DENIED;
1126 if (state == NULL) return BAD_VALUE;
1127 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1128 return NO_ERROR;
1129}
1130
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001131status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1132{
1133 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1134 if (aps == 0) return PERMISSION_DENIED;
1135 if (state == NULL) return BAD_VALUE;
1136 *state = aps->isSourceActive(stream);
1137 return NO_ERROR;
1138}
1139
Glenn Kasten3b16c762012-11-14 08:44:39 -08001140uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001141{
1142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1143 if (af == 0) return 0;
1144 return af->getPrimaryOutputSamplingRate();
1145}
1146
Glenn Kastene33054e2012-11-14 12:54:39 -08001147size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001148{
1149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1150 if (af == 0) return 0;
1151 return af->getPrimaryOutputFrameCount();
1152}
Eric Laurenteda6c362011-02-02 09:33:30 -08001153
Andy Hung6f248bb2018-01-23 14:04:37 -08001154status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001155{
1156 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1157 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001158 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001159}
1160
Eric Laurent9f6530f2011-08-30 10:18:54 -07001161void AudioSystem::clearAudioConfigCache()
1162{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001163 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001164 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001165 {
1166 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001167 if (gAudioFlingerClient != 0) {
1168 gAudioFlingerClient->clearIoCache();
1169 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001170 gAudioFlinger.clear();
1171 }
1172 {
1173 Mutex::Autolock _l(gLockAPS);
1174 gAudioPolicyService.clear();
1175 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001176}
1177
Hayden Gomes524159d2019-12-23 14:41:47 -08001178status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1179 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1180 if (aps == nullptr) return PERMISSION_DENIED;
1181 return aps->setSupportedSystemUsages(systemUsages);
1182}
1183
Kevin Rocardb99cc752019-03-21 20:52:24 -07001184status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1185 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1186 if (aps == nullptr) return PERMISSION_DENIED;
1187 return aps->setAllowedCapturePolicy(uid, flags);
1188}
1189
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001190bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1191{
1192 ALOGV("isOffloadSupported()");
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == 0) return false;
1195 return aps->isOffloadSupported(info);
1196}
1197
Eric Laurent203b1a12014-04-01 10:34:16 -07001198status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1199 audio_port_type_t type,
1200 unsigned int *num_ports,
1201 struct audio_port *ports,
1202 unsigned int *generation)
1203{
1204 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1205 if (aps == 0) return PERMISSION_DENIED;
1206 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1207}
1208
1209status_t AudioSystem::getAudioPort(struct audio_port *port)
1210{
1211 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1212 if (aps == 0) return PERMISSION_DENIED;
1213 return aps->getAudioPort(port);
1214}
1215
1216status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1217 audio_patch_handle_t *handle)
1218{
1219 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1220 if (aps == 0) return PERMISSION_DENIED;
1221 return aps->createAudioPatch(patch, handle);
1222}
1223
1224status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1225{
1226 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1227 if (aps == 0) return PERMISSION_DENIED;
1228 return aps->releaseAudioPatch(handle);
1229}
1230
1231status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1232 struct audio_patch *patches,
1233 unsigned int *generation)
1234{
1235 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1236 if (aps == 0) return PERMISSION_DENIED;
1237 return aps->listAudioPatches(num_patches, patches, generation);
1238}
1239
1240status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1241{
1242 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1243 if (aps == 0) return PERMISSION_DENIED;
1244 return aps->setAudioPortConfig(config);
1245}
1246
Eric Laurent296fb132015-05-01 11:38:42 -07001247status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001248{
Eric Laurentb28753e2015-04-01 13:06:28 -07001249 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1250 if (aps == 0) return PERMISSION_DENIED;
1251
1252 Mutex::Autolock _l(gLockAPS);
1253 if (gAudioPolicyServiceClient == 0) {
1254 return NO_INIT;
1255 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001256 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1257 if (ret == 1) {
1258 aps->setAudioPortCallbacksEnabled(true);
1259 }
1260 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001261}
1262
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001263/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001264status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001265{
1266 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1267 if (aps == 0) return PERMISSION_DENIED;
1268
1269 Mutex::Autolock _l(gLockAPS);
1270 if (gAudioPolicyServiceClient == 0) {
1271 return NO_INIT;
1272 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001273 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1274 if (ret == 0) {
1275 aps->setAudioPortCallbacksEnabled(false);
1276 }
1277 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001278}
1279
François Gaffiecfe17322018-11-07 13:41:29 +01001280status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1281{
1282 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1283 if (aps == 0) return PERMISSION_DENIED;
1284
1285 Mutex::Autolock _l(gLockAPS);
1286 if (gAudioPolicyServiceClient == 0) {
1287 return NO_INIT;
1288 }
1289 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1290 if (ret == 1) {
1291 aps->setAudioVolumeGroupCallbacksEnabled(true);
1292 }
1293 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1294}
1295
1296status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1297{
1298 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1299 if (aps == 0) return PERMISSION_DENIED;
1300
1301 Mutex::Autolock _l(gLockAPS);
1302 if (gAudioPolicyServiceClient == 0) {
1303 return NO_INIT;
1304 }
1305 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1306 if (ret == 0) {
1307 aps->setAudioVolumeGroupCallbacksEnabled(false);
1308 }
1309 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1310}
1311
Eric Laurent296fb132015-05-01 11:38:42 -07001312status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001313 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1314 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001315{
1316 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1317 if (afc == 0) {
1318 return NO_INIT;
1319 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001320 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001321 if (status == NO_ERROR) {
1322 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1323 if (af != 0) {
1324 af->registerClient(afc);
1325 }
1326 }
1327 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001328}
1329
1330status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001331 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1332 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001333{
1334 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1335 if (afc == 0) {
1336 return NO_INIT;
1337 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001338 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001339}
1340
1341audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1342{
1343 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1344 if (af == 0) return PERMISSION_DENIED;
1345 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1346 if (desc == 0) {
1347 return AUDIO_PORT_HANDLE_NONE;
1348 }
1349 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001350}
1351
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001352status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1353 audio_io_handle_t *ioHandle,
1354 audio_devices_t *device)
1355{
1356 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1357 if (aps == 0) return PERMISSION_DENIED;
1358 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1359}
1360
1361status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1362{
1363 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1364 if (aps == 0) return PERMISSION_DENIED;
1365 return aps->releaseSoundTriggerSession(session);
1366}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001367
1368audio_mode_t AudioSystem::getPhoneState()
1369{
1370 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1371 if (aps == 0) return AUDIO_MODE_INVALID;
1372 return aps->getPhoneState();
1373}
1374
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001375status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001376{
1377 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1378 if (aps == 0) return PERMISSION_DENIED;
1379 return aps->registerPolicyMixes(mixes, registration);
1380}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001381
jiabin6a02d532020-08-07 11:56:38 -07001382status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices)
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001383{
1384 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1385 if (aps == 0) return PERMISSION_DENIED;
1386 return aps->setUidDeviceAffinities(uid, devices);
1387}
1388
1389status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1390 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1391 if (aps == 0) return PERMISSION_DENIED;
1392 return aps->removeUidDeviceAffinities(uid);
1393}
1394
Oscar Azucena90e77632019-11-27 17:12:28 -08001395status_t AudioSystem::setUserIdDeviceAffinities(int userId,
jiabin6a02d532020-08-07 11:56:38 -07001396 const AudioDeviceTypeAddrVector& devices)
Oscar Azucena90e77632019-11-27 17:12:28 -08001397{
1398 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1399 if (aps == 0) return PERMISSION_DENIED;
1400 return aps->setUserIdDeviceAffinities(userId, devices);
1401}
1402
1403status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1404{
1405 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1406 if (aps == 0) return PERMISSION_DENIED;
1407 return aps->removeUserIdDeviceAffinities(userId);
1408}
1409
Eric Laurent554a2772015-04-10 11:29:24 -07001410status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1411 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001412 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001413{
1414 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1415 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001416 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001417}
1418
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001419status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001420{
1421 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1422 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001423 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001424}
1425
Andy Hung2ddee192015-12-18 17:34:44 -08001426status_t AudioSystem::setMasterMono(bool mono)
1427{
1428 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1429 if (aps == 0) return PERMISSION_DENIED;
1430 return aps->setMasterMono(mono);
1431}
1432
1433status_t AudioSystem::getMasterMono(bool *mono)
1434{
1435 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1436 if (aps == 0) return PERMISSION_DENIED;
1437 return aps->getMasterMono(mono);
1438}
1439
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001440status_t AudioSystem::setMasterBalance(float balance)
1441{
1442 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1443 if (af == 0) return PERMISSION_DENIED;
1444 return af->setMasterBalance(balance);
1445}
1446
1447status_t AudioSystem::getMasterBalance(float *balance)
1448{
1449 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1450 if (af == 0) return PERMISSION_DENIED;
1451 return af->getMasterBalance(balance);
1452}
1453
Eric Laurentac9cef52017-06-09 15:46:26 -07001454float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1455{
1456 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1457 if (aps == 0) return NAN;
1458 return aps->getStreamVolumeDB(stream, index, device);
1459}
1460
jiabin46a76fa2018-01-05 10:18:21 -08001461status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1462{
1463 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1464 if (af == 0) return PERMISSION_DENIED;
1465 return af->getMicrophones(microphones);
1466}
1467
Eric Laurent42896a02019-09-27 15:40:33 -07001468status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1469 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1470 if (af == nullptr) return PERMISSION_DENIED;
1471 return af->setAudioHalPids(pids);
1472}
1473
jiabin81772902018-04-02 17:52:27 -07001474status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1475 audio_format_t *surroundFormats,
1476 bool *surroundFormatsEnabled,
1477 bool reported)
1478{
1479 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1480 if (aps == 0) return PERMISSION_DENIED;
1481 return aps->getSurroundFormats(
1482 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1483}
1484
1485status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1486{
1487 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1488 if (aps == 0) return PERMISSION_DENIED;
1489 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1490}
1491
Eric Laurentb78763e2018-10-17 10:08:02 -07001492status_t AudioSystem::setAssistantUid(uid_t uid)
1493{
1494 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1495 if (aps == 0) return PERMISSION_DENIED;
1496
1497 return aps->setAssistantUid(uid);
1498}
1499
1500status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1501{
1502 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1503 if (aps == 0) return PERMISSION_DENIED;
1504
1505 return aps->setA11yServicesUids(uids);
1506}
1507
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001508status_t AudioSystem::setCurrentImeUid(uid_t uid)
1509{
1510 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1511 if (aps == 0) return PERMISSION_DENIED;
1512
1513 return aps->setCurrentImeUid(uid);
1514}
1515
jiabin6012f912018-11-02 17:06:30 -07001516bool AudioSystem::isHapticPlaybackSupported()
1517{
1518 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1519 if (aps == 0) return false;
1520 return aps->isHapticPlaybackSupported();
1521}
1522
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001523status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001524 std::vector<audio_format_t> *formats) {
1525 const sp <IAudioPolicyService>
1526 & aps = AudioSystem::get_audio_policy_service();
1527 if (aps == 0) return PERMISSION_DENIED;
1528 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1529}
1530
1531status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001532{
1533 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1534 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001535 return aps->listAudioProductStrategies(strategies);
1536}
1537
1538audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1539{
1540 AudioProductStrategyVector strategies;
1541 listAudioProductStrategies(strategies);
1542 for (const auto &strategy : strategies) {
1543 auto attrVect = strategy.getAudioAttributes();
1544 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1545 return attributes.getStreamType() == stream; });
1546 if (iter != end(attrVect)) {
1547 return iter->getAttributes();
1548 }
1549 }
1550 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1551 return AUDIO_ATTRIBUTES_INITIALIZER;
1552}
1553
1554audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1555{
François Gaffie4b2018b2018-11-07 11:18:59 +01001556 product_strategy_t psId;
1557 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1558 if (ret != NO_ERROR) {
1559 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1560 return AUDIO_STREAM_MUSIC;
1561 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001562 AudioProductStrategyVector strategies;
1563 listAudioProductStrategies(strategies);
1564 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001565 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001566 auto attrVect = strategy.getAudioAttributes();
1567 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1568 return AudioProductStrategy::attributesMatches(
1569 refAttr.getAttributes(), attr); });
1570 if (iter != end(attrVect)) {
1571 return iter->getStreamType();
1572 }
1573 }
1574 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001575 switch (attr.usage) {
1576 case AUDIO_USAGE_VIRTUAL_SOURCE:
1577 // virtual source is not expected to have an associated product strategy
1578 break;
1579 default:
1580 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1581 break;
1582 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001583 return AUDIO_STREAM_MUSIC;
1584}
1585
François Gaffie4b2018b2018-11-07 11:18:59 +01001586status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1587 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001588{
1589 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001590 if (aps == 0) return PERMISSION_DENIED;
1591 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1592}
1593
1594status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1595{
1596 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1597 if (aps == 0) return PERMISSION_DENIED;
1598 return aps->listAudioVolumeGroups(groups);
1599}
1600
1601status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1602 volume_group_t &volumeGroup)
1603{
1604 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1605 if (aps == 0) return PERMISSION_DENIED;
1606 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001607}
Eric Laurentb78763e2018-10-17 10:08:02 -07001608
Eric Laurent6ede98f2019-06-11 14:50:30 -07001609status_t AudioSystem::setRttEnabled(bool enabled)
1610{
1611 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1612 if (aps == 0) return PERMISSION_DENIED;
1613 return aps->setRttEnabled(enabled);
1614}
1615
Eric Laurent8340e672019-11-06 11:01:08 -08001616bool AudioSystem::isCallScreenModeSupported()
1617{
1618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1619 if (aps == 0) return false;
1620 return aps->isCallScreenModeSupported();
1621}
1622
jiabin0a488932020-08-07 17:32:40 -07001623status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
1624 device_role_t role,
1625 const AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001626{
1627 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1628 if (aps == 0) {
1629 return PERMISSION_DENIED;
1630 }
jiabin0a488932020-08-07 17:32:40 -07001631 return aps->setDevicesRoleForStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001632}
1633
jiabin0a488932020-08-07 17:32:40 -07001634status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001635{
1636 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1637 if (aps == 0) {
1638 return PERMISSION_DENIED;
1639 }
jiabin0a488932020-08-07 17:32:40 -07001640 return aps->removeDevicesRoleForStrategy(strategy, role);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001641}
1642
jiabin0a488932020-08-07 17:32:40 -07001643status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
1644 device_role_t role,
1645 AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001646{
1647 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1648 if (aps == 0) {
1649 return PERMISSION_DENIED;
1650 }
jiabin0a488932020-08-07 17:32:40 -07001651 return aps->getDevicesForRoleAndStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001652}
1653
Jiabin Huang3b98d322020-09-03 17:54:16 +00001654status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
1655 device_role_t role,
1656 const AudioDeviceTypeAddrVector &devices)
1657{
1658 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1659 if (aps == 0) {
1660 return PERMISSION_DENIED;
1661 }
1662 return aps->setDevicesRoleForCapturePreset(audioSource, role, devices);
1663}
1664
1665status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
1666 device_role_t role,
1667 const AudioDeviceTypeAddrVector &devices)
1668{
1669 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1670 if (aps == 0) {
1671 return PERMISSION_DENIED;
1672 }
1673 return aps->addDevicesRoleForCapturePreset(audioSource, role, devices);
1674}
1675
1676status_t AudioSystem::removeDevicesRoleForCapturePreset(
1677 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices)
1678{
1679 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1680 if (aps == 0) {
1681 return PERMISSION_DENIED;
1682 }
1683 return aps->removeDevicesRoleForCapturePreset(audioSource, role, devices);
1684}
1685
1686status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
1687 device_role_t role)
1688{
1689 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1690 if (aps == 0) {
1691 return PERMISSION_DENIED;
1692 }
1693 return aps->clearDevicesRoleForCapturePreset(audioSource, role);
1694}
1695
1696status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
1697 device_role_t role,
1698 AudioDeviceTypeAddrVector &devices)
1699{
1700 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1701 if (aps == 0) {
1702 return PERMISSION_DENIED;
1703 }
1704 return aps->getDevicesForRoleAndCapturePreset(audioSource, role, devices);
1705}
1706
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001707class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1708 public IBinder::DeathRecipient {
1709public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001710 CaptureStateListenerImpl(
1711 const sp<IAudioPolicyService>& aps,
1712 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001713 : mAps(aps), mListener(listener) {}
1714
1715 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001716 bool active;
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001717 status_t status = mAps->registerSoundTriggerCaptureStateListener(this, &active);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001718 if (status != NO_ERROR) {
1719 mListener->onServiceDied();
1720 return;
1721 }
1722 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001723 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001724 }
1725
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001726 binder::Status setCaptureState(bool active) override {
1727 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001728 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001729 return binder::Status::ok();
1730 }
1731
1732 void binderDied(const wp<IBinder>&) override {
1733 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001734 mListener->onServiceDied();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001735 gSoundTriggerCaptureStateListener = nullptr;
1736 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001737
1738private:
1739 // Need this in order to keep the death receipent alive.
1740 sp<IAudioPolicyService> mAps;
1741 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001742};
1743
1744status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1745 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001746 LOG_ALWAYS_FATAL_IF(listener == nullptr);
1747
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001748 const sp<IAudioPolicyService>& aps =
1749 AudioSystem::get_audio_policy_service();
1750 if (aps == 0) {
1751 return PERMISSION_DENIED;
1752 }
1753
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001754 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001755 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001756 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001757
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001758 return NO_ERROR;
1759}
1760
Eric Laurentc2f1f072009-07-17 12:17:14 -07001761// ---------------------------------------------------------------------------
1762
Eric Laurente8726fe2015-06-26 09:39:24 -07001763int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001764 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001765{
1766 Mutex::Autolock _l(mLock);
1767 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001768 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001769 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001770 }
1771 }
Eric Laurent296fb132015-05-01 11:38:42 -07001772 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001773 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001774}
1775
Eric Laurente8726fe2015-06-26 09:39:24 -07001776int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001777 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001778{
1779 Mutex::Autolock _l(mLock);
1780 size_t i;
1781 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001782 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001783 break;
1784 }
1785 }
1786 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001787 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001788 }
1789 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001790 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001791}
1792
Eric Laurent296fb132015-05-01 11:38:42 -07001793
Eric Laurentb28753e2015-04-01 13:06:28 -07001794void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1795{
1796 Mutex::Autolock _l(mLock);
1797 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1798 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1799 }
1800}
1801
1802void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1803{
1804 Mutex::Autolock _l(mLock);
1805 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1806 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1807 }
1808}
1809
François Gaffiecfe17322018-11-07 13:41:29 +01001810// ----------------------------------------------------------------------------
1811int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1812 const sp<AudioVolumeGroupCallback>& callback)
1813{
1814 Mutex::Autolock _l(mLock);
1815 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1816 if (mAudioVolumeGroupCallback[i] == callback) {
1817 return -1;
1818 }
1819 }
1820 mAudioVolumeGroupCallback.add(callback);
1821 return mAudioVolumeGroupCallback.size();
1822}
1823
1824int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1825 const sp<AudioVolumeGroupCallback>& callback)
1826{
1827 Mutex::Autolock _l(mLock);
1828 size_t i;
1829 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1830 if (mAudioVolumeGroupCallback[i] == callback) {
1831 break;
1832 }
1833 }
1834 if (i == mAudioVolumeGroupCallback.size()) {
1835 return -1;
1836 }
1837 mAudioVolumeGroupCallback.removeAt(i);
1838 return mAudioVolumeGroupCallback.size();
1839}
1840
1841void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1842 int flags)
1843{
1844 Mutex::Autolock _l(mLock);
1845 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1846 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1847 }
1848}
1849// ----------------------------------------------------------------------------
1850
Jean-Michel Trivide801052015-04-14 19:10:14 -07001851void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1852 String8 regId, int32_t state)
1853{
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001854 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1855 dynamic_policy_callback cb = NULL;
1856 {
1857 Mutex::Autolock _l(AudioSystem::gLock);
1858 cb = gDynPolicyCallback;
1859 }
1860
1861 if (cb != NULL) {
1862 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1863 }
Jean-Michel Trivide801052015-04-14 19:10:14 -07001864}
1865
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001866void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -08001867 int event,
1868 const record_client_info_t *clientInfo,
1869 const audio_config_base_t *clientConfig,
1870 std::vector<effect_descriptor_t> clientEffects,
1871 const audio_config_base_t *deviceConfig,
1872 std::vector<effect_descriptor_t> effects,
1873 audio_patch_handle_t patchHandle,
1874 audio_source_t source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001875 record_config_callback cb = NULL;
1876 {
1877 Mutex::Autolock _l(AudioSystem::gLock);
1878 cb = gRecordConfigCallback;
1879 }
1880
1881 if (cb != NULL) {
Eric Laurenta9f86652018-11-28 17:23:11 -08001882 cb(event, clientInfo, clientConfig, clientEffects,
1883 deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001884 }
1885}
1886
Glenn Kasten4944acb2013-08-19 08:39:20 -07001887void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1888{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001889 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001890 Mutex::Autolock _l(mLock);
1891 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1892 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001893 }
François Gaffiecfe17322018-11-07 13:41:29 +01001894 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1895 mAudioVolumeGroupCallback[i]->onServiceDied();
1896 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001897 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001898 {
1899 Mutex::Autolock _l(gLockAPS);
1900 AudioSystem::gAudioPolicyService.clear();
1901 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001902
Steve Block5ff1dd52012-01-05 23:22:43 +00001903 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001904}
1905
Glenn Kasten40bc9062015-03-20 09:09:33 -07001906} // namespace android