blob: 2187635d4a1b43f7a6186280d21db700be3d6c39 [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-Tsvi1ff75692020-11-06 12:16:12 -080036#define VALUE_OR_RETURN_BINDER_STATUS(x) \
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070037 ({ auto _tmp = (x); \
Andy Hung1131b6e2020-12-08 20:47:45 -080038 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
Ytai Ben-Tsvia3815202020-10-28 14:58:08 -070039 std::move(_tmp.value()); })
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -070040
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;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -070056routing_callback AudioSystem::gRoutingCallback = NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070058// Required to be held while calling into gSoundTriggerCaptureStateListener.
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070059class CaptureStateListenerImpl;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070060Mutex gSoundTriggerCaptureStateListenerLock;
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -070061sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -070062
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063// establish binder interface to AudioFlinger service
Eric Laurent0ebd5f92014-11-19 19:04:52 -080064const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065{
Eric Laurent0ebd5f92014-11-19 19:04:52 -080066 sp<IAudioFlinger> af;
67 sp<AudioFlingerClient> afc;
Mikhail Naganov69330d42020-04-08 19:29:50 +000068 bool reportNoError = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -080069 {
70 Mutex::Autolock _l(gLock);
71 if (gAudioFlinger == 0) {
72 sp<IServiceManager> sm = defaultServiceManager();
73 sp<IBinder> binder;
74 do {
Ytai Ben-Tsvi50b8ccb2020-11-24 13:47:54 -080075 binder = sm->getService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME));
Eric Laurent0ebd5f92014-11-19 19:04:52 -080076 if (binder != 0)
77 break;
78 ALOGW("AudioFlinger not published, waiting...");
79 usleep(500000); // 0.5 s
80 } while (true);
81 if (gAudioFlingerClient == NULL) {
82 gAudioFlingerClient = new AudioFlingerClient();
83 } else {
Mikhail Naganov69330d42020-04-08 19:29:50 +000084 reportNoError = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080086 binder->linkToDeath(gAudioFlingerClient);
Ytai Ben-Tsvi50b8ccb2020-11-24 13:47:54 -080087 gAudioFlinger = new AudioFlingerClientAdapter(
88 interface_cast<media::IAudioFlingerService>(binder));
Eric Laurent0ebd5f92014-11-19 19:04:52 -080089 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
90 afc = gAudioFlingerClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -070091 // Make sure callbacks can be received by gAudioFlingerClient
92 ProcessState::self()->startThreadPool();
Glenn Kastene53b9ea2012-03-12 16:29:55 -070093 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080094 af = gAudioFlinger;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -080096 if (afc != 0) {
François Gaffie24437602018-04-23 15:08:59 +020097 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -080098 af->registerClient(afc);
François Gaffie24437602018-04-23 15:08:59 +020099 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800100 }
Mikhail Naganov69330d42020-04-08 19:29:50 +0000101 if (reportNoError) reportError(NO_ERROR);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800102 return af;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103}
104
Eric Laurent296fb132015-05-01 11:38:42 -0700105const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
106{
107 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
108 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
109 if (af == 0) return 0;
110 Mutex::Autolock _l(gLock);
111 return gAudioFlingerClient;
112}
113
114sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
115{
116 sp<AudioIoDescriptor> desc;
117 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
118 if (afc != 0) {
119 desc = afc->getIoDescriptor(ioHandle);
120 }
121 return desc;
122}
123
Eric Laurent46291612013-07-18 14:38:44 -0700124/* static */ status_t AudioSystem::checkAudioFlinger()
125{
126 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
127 return NO_ERROR;
128 }
129 return DEAD_OBJECT;
130}
131
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700132// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
133
Glenn Kasten4944acb2013-08-19 08:39:20 -0700134status_t AudioSystem::muteMicrophone(bool state)
135{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
137 if (af == 0) return PERMISSION_DENIED;
138 return af->setMicMute(state);
139}
140
Glenn Kasten4944acb2013-08-19 08:39:20 -0700141status_t AudioSystem::isMicrophoneMuted(bool* state)
142{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
144 if (af == 0) return PERMISSION_DENIED;
145 *state = af->getMicMute();
146 return NO_ERROR;
147}
148
149status_t AudioSystem::setMasterVolume(float value)
150{
151 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
152 if (af == 0) return PERMISSION_DENIED;
153 af->setMasterVolume(value);
154 return NO_ERROR;
155}
156
157status_t AudioSystem::setMasterMute(bool mute)
158{
159 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
160 if (af == 0) return PERMISSION_DENIED;
161 af->setMasterMute(mute);
162 return NO_ERROR;
163}
164
165status_t AudioSystem::getMasterVolume(float* volume)
166{
167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
169 *volume = af->masterVolume();
170 return NO_ERROR;
171}
172
173status_t AudioSystem::getMasterMute(bool* mute)
174{
175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
177 *mute = af->masterMute();
178 return NO_ERROR;
179}
180
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800181status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
182 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183{
Dima Zavinfce7a472011-04-19 22:30:36 -0700184 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
186 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700187 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 return NO_ERROR;
189}
190
Glenn Kastenfff6d712012-01-12 16:38:12 -0800191status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192{
Dima Zavinfce7a472011-04-19 22:30:36 -0700193 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
195 if (af == 0) return PERMISSION_DENIED;
196 af->setStreamMute(stream, mute);
197 return NO_ERROR;
198}
199
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800200status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
201 audio_io_handle_t output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202{
Dima Zavinfce7a472011-04-19 22:30:36 -0700203 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
205 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700206 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 return NO_ERROR;
208}
209
Glenn Kastenfff6d712012-01-12 16:38:12 -0800210status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211{
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
214 if (af == 0) return PERMISSION_DENIED;
215 *mute = af->streamMute(stream);
216 return NO_ERROR;
217}
218
Glenn Kastenf78aee72012-01-04 11:00:47 -0800219status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800221 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
223 if (af == 0) return PERMISSION_DENIED;
224 return af->setMode(mode);
225}
226
Glenn Kasten4944acb2013-08-19 08:39:20 -0700227status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
228{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
230 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700231 return af->setParameters(ioHandle, keyValuePairs);
232}
233
Glenn Kasten4944acb2013-08-19 08:39:20 -0700234String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
235{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
237 String8 result = String8("");
238 if (af == 0) return result;
239
240 result = af->getParameters(ioHandle, keys);
241 return result;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800242}
243
Glenn Kastenc23885e2013-12-19 16:35:18 -0800244status_t AudioSystem::setParameters(const String8& keyValuePairs)
245{
Glenn Kasten142f5192014-03-25 17:44:59 -0700246 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800247}
248
249String8 AudioSystem::getParameters(const String8& keys)
250{
Glenn Kasten142f5192014-03-25 17:44:59 -0700251 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
Glenn Kastenc23885e2013-12-19 16:35:18 -0800252}
253
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254// convert volume steps to natural log scale
255
256// change this value to change volume scaling
257static const float dBPerStep = 0.5f;
258// shouldn't need to touch these
259static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
260static const float dBConvertInverse = 1.0f / dBConvert;
261
262float AudioSystem::linearToLog(int volume)
263{
264 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000265 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 // return v;
267 return volume ? exp(float(100 - volume) * dBConvert) : 0;
268}
269
270int AudioSystem::logToLinear(float volume)
271{
272 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Blockb8a80522011-12-20 16:23:08 +0000273 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 // return v;
275 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
276}
277
Eric Laurent21da6472017-11-09 16:29:26 -0800278/* static */ size_t AudioSystem::calculateMinFrameCount(
279 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
280 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
281{
282 // Ensure that buffer depth covers at least audio hardware latency
283 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
284 if (minBufCount < 2) {
285 minBufCount = 2;
286 }
287#if 0
288 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
289 // but keeping the code here to make it easier to add later.
290 if (minBufCount < notificationsPerBufferReq) {
291 minBufCount = notificationsPerBufferReq;
292 }
293#endif
294 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
295 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
296 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
297 /*, notificationsPerBufferReq*/);
298 return minBufCount * sourceFramesNeededWithTimestretch(
299 sampleRate, afFrameCount, afSampleRate, speed);
300}
301
302
Glenn Kasten3b16c762012-11-14 08:44:39 -0800303status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700305 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306
Dima Zavinfce7a472011-04-19 22:30:36 -0700307 if (streamType == AUDIO_STREAM_DEFAULT) {
308 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309 }
310
Glenn Kastenfff6d712012-01-12 16:38:12 -0800311 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 if (output == 0) {
313 return PERMISSION_DENIED;
314 }
315
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700316 return getSamplingRate(output, samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700317}
318
Glenn Kasten2c073da2016-02-26 09:14:08 -0800319status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800320 uint32_t* samplingRate)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700321{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800322 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
323 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800324 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
325 if (desc == 0) {
326 *samplingRate = af->sampleRate(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800328 *samplingRate = desc->mSamplingRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700329 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800330 if (*samplingRate == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800331 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800332 return BAD_VALUE;
333 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334
Glenn Kasten2c073da2016-02-26 09:14:08 -0800335 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 return NO_ERROR;
338}
339
Glenn Kastene33054e2012-11-14 12:54:39 -0800340status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700342 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343
Dima Zavinfce7a472011-04-19 22:30:36 -0700344 if (streamType == AUDIO_STREAM_DEFAULT) {
345 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700347
Glenn Kastenfff6d712012-01-12 16:38:12 -0800348 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700349 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 return PERMISSION_DENIED;
351 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700353 return getFrameCount(output, frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700354}
355
Glenn Kasten2c073da2016-02-26 09:14:08 -0800356status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
Glenn Kastene33054e2012-11-14 12:54:39 -0800357 size_t* frameCount)
Eric Laurent1a9ed112012-03-20 18:36:01 -0700358{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800359 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
360 if (af == 0) return PERMISSION_DENIED;
Glenn Kasten2c073da2016-02-26 09:14:08 -0800361 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
362 if (desc == 0) {
363 *frameCount = af->frameCount(ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 } else {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800365 *frameCount = desc->mFrameCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700366 }
Glenn Kastenf94006c2014-01-08 08:56:06 -0800367 if (*frameCount == 0) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800368 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
Glenn Kastenf94006c2014-01-08 08:56:06 -0800369 return BAD_VALUE;
370 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700371
Glenn Kasten2c073da2016-02-26 09:14:08 -0800372 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700373
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 return NO_ERROR;
375}
376
Glenn Kastenfff6d712012-01-12 16:38:12 -0800377status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700379 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380
Dima Zavinfce7a472011-04-19 22:30:36 -0700381 if (streamType == AUDIO_STREAM_DEFAULT) {
382 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700383 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700384
Glenn Kastenfff6d712012-01-12 16:38:12 -0800385 output = getOutput(streamType);
Glenn Kasten142f5192014-03-25 17:44:59 -0700386 if (output == AUDIO_IO_HANDLE_NONE) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387 return PERMISSION_DENIED;
388 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389
Glenn Kasten241618f2014-03-25 17:48:57 -0700390 return getLatency(output, latency);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700391}
392
393status_t AudioSystem::getLatency(audio_io_handle_t output,
Eric Laurent1a9ed112012-03-20 18:36:01 -0700394 uint32_t* latency)
395{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800396 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
397 if (af == 0) return PERMISSION_DENIED;
Eric Laurent296fb132015-05-01 11:38:42 -0700398 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
Eric Laurent73e26b62015-04-27 16:55:58 -0700399 if (outputDesc == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700400 *latency = af->latency(output);
401 } else {
Eric Laurent73e26b62015-04-27 16:55:58 -0700402 *latency = outputDesc->mLatency;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700403 }
404
Glenn Kasten241618f2014-03-25 17:48:57 -0700405 ALOGV("getLatency() output %d, latency %d", output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 return NO_ERROR;
408}
409
Glenn Kastendd8104c2012-07-02 12:42:44 -0700410status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
411 audio_channel_mask_t channelMask, size_t* buffSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412{
Eric Laurent296fb132015-05-01 11:38:42 -0700413 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
414 if (afc == 0) {
415 return NO_INIT;
416 }
417 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418}
419
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700420status_t AudioSystem::setVoiceVolume(float value)
421{
422 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
423 if (af == 0) return PERMISSION_DENIED;
424 return af->setVoiceVolume(value);
425}
426
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000427status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
Glenn Kasten0ed19592014-03-26 07:50:05 -0700428 uint32_t *dspFrames)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800429{
430 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
431 if (af == 0) return PERMISSION_DENIED;
432
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000433 return af->getRenderPosition(halFrames, dspFrames, output);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800434}
435
Glenn Kasten4944acb2013-08-19 08:39:20 -0700436uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
437{
Eric Laurent05bca2f2010-02-26 02:47:27 -0800438 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Glenn Kasten5f972c02014-01-13 09:59:31 -0800439 uint32_t result = 0;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800440 if (af == 0) return result;
Glenn Kasten142f5192014-03-25 17:44:59 -0700441 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800442
443 result = af->getInputFramesLost(ioHandle);
444 return result;
445}
446
Glenn Kasteneeecb982016-02-26 10:44:04 -0800447audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700448{
Mikhail Naganov2996f672019-04-18 12:29:59 -0700449 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700450 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
Eric Laurentde3f8392014-07-27 18:38:22 -0700451 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
Glenn Kasteneeecb982016-02-26 10:44:04 -0800452 return af->newAudioUniqueId(use);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700453}
454
Andy Hung8b0bfd92019-12-23 13:11:11 -0800455void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700456{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700457 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
458 if (af != 0) {
Andy Hung8b0bfd92019-12-23 13:11:11 -0800459 af->acquireAudioSessionId(audioSession, pid, uid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700460 }
461}
462
Glenn Kastend848eb42016-03-08 13:42:11 -0800463void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700464{
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700465 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
466 if (af != 0) {
Marco Nelissend457c972014-02-11 08:47:07 -0800467 af->releaseAudioSessionId(audioSession, pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700468 }
469}
470
Eric Laurent93c3d412014-08-01 14:48:35 -0700471audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
472{
473 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
474 if (af == 0) return AUDIO_HW_SYNC_INVALID;
475 return af->getAudioHwSyncForSession(sessionId);
476}
477
Eric Laurent72e3f392015-05-20 14:43:50 -0700478status_t AudioSystem::systemReady()
479{
480 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
481 if (af == 0) return NO_INIT;
482 return af->systemReady();
483}
484
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700485status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
486 size_t* frameCount)
487{
488 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
489 if (af == 0) return PERMISSION_DENIED;
490 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
491 if (desc == 0) {
492 *frameCount = af->frameCountHAL(ioHandle);
493 } else {
494 *frameCount = desc->mFrameCountHAL;
495 }
496 if (*frameCount == 0) {
497 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
498 return BAD_VALUE;
499 }
500
501 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
502
503 return NO_ERROR;
504}
505
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506// ---------------------------------------------------------------------------
507
Eric Laurent73e26b62015-04-27 16:55:58 -0700508
509void AudioSystem::AudioFlingerClient::clearIoCache()
510{
511 Mutex::Autolock _l(mLock);
512 mIoDescriptors.clear();
513 mInBuffSize = 0;
514 mInSamplingRate = 0;
515 mInFormat = AUDIO_FORMAT_DEFAULT;
516 mInChannelMask = AUDIO_CHANNEL_NONE;
517}
518
Glenn Kasten4944acb2013-08-19 08:39:20 -0700519void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
520{
Eric Laurentf6778fd2014-11-18 17:26:58 -0800521 {
522 Mutex::Autolock _l(AudioSystem::gLock);
523 AudioSystem::gAudioFlinger.clear();
Eric Laurentf6778fd2014-11-18 17:26:58 -0800524 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525
Eric Laurent73e26b62015-04-27 16:55:58 -0700526 // clear output handles and stream to output map caches
527 clearIoCache();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800529 reportError(DEAD_OBJECT);
530
Steve Block5ff1dd52012-01-05 23:22:43 +0000531 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532}
533
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700534Status AudioSystem::AudioFlingerClient::ioConfigChanged(
535 media::AudioIoConfigEvent _event,
536 const media::AudioIoDescriptor& _ioDesc) {
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -0800537 audio_io_config_event event = VALUE_OR_RETURN_BINDER_STATUS(
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700538 aidl2legacy_AudioIoConfigEvent_audio_io_config_event(_event));
539 sp<AudioIoDescriptor> ioDesc(
Ytai Ben-Tsvi1ff75692020-11-06 12:16:12 -0800540 VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc)));
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700541
Steve Block3856b092011-10-20 11:56:00 +0100542 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700543
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700544 if (ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return Status::ok();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700545
Eric Laurent296fb132015-05-01 11:38:42 -0700546 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700547 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
Eric Laurent296fb132015-05-01 11:38:42 -0700548 {
549 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700550 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
Eric Laurent296fb132015-05-01 11:38:42 -0700551
552 switch (event) {
553 case AUDIO_OUTPUT_OPENED:
Eric Laurentad2e7b92017-09-14 20:06:42 -0700554 case AUDIO_OUTPUT_REGISTERED:
555 case AUDIO_INPUT_OPENED:
556 case AUDIO_INPUT_REGISTERED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700557 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -0700558 if (oldDesc == 0) {
559 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
560 } else {
561 deviceId = oldDesc->getDeviceId();
562 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
Eric Laurent296fb132015-05-01 11:38:42 -0700563 }
Eric Laurent296fb132015-05-01 11:38:42 -0700564
565 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
566 deviceId = ioDesc->getDeviceId();
Eric Laurentad2e7b92017-09-14 20:06:42 -0700567 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
Eric Laurent09f1ed22019-04-24 17:45:17 -0700568 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
569 if (it != mAudioDeviceCallbacks.end()) {
570 callbacks = it->second;
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100571 }
572 }
Eric Laurent296fb132015-05-01 11:38:42 -0700573 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700574 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
575 "frameCount %zu deviceId %d",
576 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
577 "output" : "input",
578 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
579 "opened" : "registered",
Eric Laurent296fb132015-05-01 11:38:42 -0700580 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
581 ioDesc->mFrameCount, ioDesc->getDeviceId());
582 } break;
583 case AUDIO_OUTPUT_CLOSED:
584 case AUDIO_INPUT_CLOSED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700585 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700586 ALOGW("ioConfigChanged() closing unknown %s %d",
587 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
588 break;
589 }
590 ALOGV("ioConfigChanged() %s %d closed",
Eric Laurent73e26b62015-04-27 16:55:58 -0700591 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700592
Eric Laurent296fb132015-05-01 11:38:42 -0700593 mIoDescriptors.removeItem(ioDesc->mIoHandle);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700594 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700595 } break;
596
597 case AUDIO_OUTPUT_CONFIG_CHANGED:
598 case AUDIO_INPUT_CONFIG_CHANGED: {
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700599 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700600 if (oldDesc == 0) {
Dean Wheatley8f992a12020-05-08 20:33:51 +1000601 ALOGW("ioConfigChanged() modifying unknown %s! %d",
602 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
Eric Laurent296fb132015-05-01 11:38:42 -0700603 break;
604 }
605
606 deviceId = oldDesc->getDeviceId();
607 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
608
609 if (deviceId != ioDesc->getDeviceId()) {
610 deviceId = ioDesc->getDeviceId();
Eric Laurent09f1ed22019-04-24 17:45:17 -0700611 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
612 if (it != mAudioDeviceCallbacks.end()) {
613 callbacks = it->second;
614 }
Eric Laurent296fb132015-05-01 11:38:42 -0700615 }
616 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
Glenn Kasten4a8308b2016-04-18 14:10:01 -0700617 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
Eric Laurent296fb132015-05-01 11:38:42 -0700618 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
619 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
Glenn Kastend3bb6452016-12-05 18:14:37 -0800620 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
621 ioDesc->getDeviceId());
Eric Laurent296fb132015-05-01 11:38:42 -0700622
Eric Laurentc2f1f072009-07-17 12:17:14 -0700623 } break;
Eric Laurent09f1ed22019-04-24 17:45:17 -0700624 case AUDIO_CLIENT_STARTED: {
625 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
626 if (oldDesc == 0) {
627 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
628 break;
629 }
630 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
631 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
632 oldDesc->mPatch = ioDesc->mPatch;
633 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
634 if (it != mAudioDeviceCallbacks.end()) {
635 auto cbks = it->second;
636 auto it2 = cbks.find(ioDesc->mPortId);
637 if (it2 != cbks.end()) {
638 callbacks.emplace(ioDesc->mPortId, it2->second);
639 deviceId = oldDesc->getDeviceId();
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100640 }
Francois Gaffie24a9fb02019-01-18 17:51:34 +0100641 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700642 } break;
643 }
644
645 for (auto wpCbk : callbacks) {
646 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
647 if (spCbk != nullptr) {
648 callbacksToCall.push_back(spCbk);
649 }
Eric Laurentad2e7b92017-09-14 20:06:42 -0700650 }
Eric Laurent4463ff52019-02-07 13:56:09 -0800651 }
652
653 // Callbacks must be called without mLock held. May lead to dead lock if calling for
654 // example getRoutedDevice that updates the device and tries to acquire mLock.
Eric Laurent09f1ed22019-04-24 17:45:17 -0700655 for (auto cb : callbacksToCall) {
656 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
657 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700658 }
Ytai Ben-Tsvi10dc0a62020-09-18 11:31:55 -0700659
660 return Status::ok();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661}
662
Eric Laurent73e26b62015-04-27 16:55:58 -0700663status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
664 uint32_t sampleRate, audio_format_t format,
665 audio_channel_mask_t channelMask, size_t* buffSize)
666{
667 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
668 if (af == 0) {
669 return PERMISSION_DENIED;
670 }
671 Mutex::Autolock _l(mLock);
672 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
673 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
674 || (channelMask != mInChannelMask)) {
675 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
676 if (inBuffSize == 0) {
Glenn Kasten49f36ba2017-12-06 13:02:02 -0800677 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
Eric Laurent73e26b62015-04-27 16:55:58 -0700678 sampleRate, format, channelMask);
679 return BAD_VALUE;
680 }
681 // A benign race is possible here: we could overwrite a fresher cache entry
682 // save the request params
683 mInSamplingRate = sampleRate;
684 mInFormat = format;
685 mInChannelMask = channelMask;
686
687 mInBuffSize = inBuffSize;
688 }
689
690 *buffSize = mInBuffSize;
691
692 return NO_ERROR;
693}
694
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700695sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
Eric Laurent73e26b62015-04-27 16:55:58 -0700696{
697 sp<AudioIoDescriptor> desc;
698 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
699 if (index >= 0) {
700 desc = mIoDescriptors.valueAt(index);
701 }
702 return desc;
703}
704
Praveen Chavan49fdeaf2015-09-29 02:25:47 -0700705sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
706{
707 Mutex::Autolock _l(mLock);
708 return getIoDescriptor_l(ioHandle);
709}
710
Eric Laurent296fb132015-05-01 11:38:42 -0700711status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700712 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
713 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700714{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700715 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800716 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700717 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
718 auto result = callbacks.try_emplace(portId, callback);
719 if (!result.second) {
720 return INVALID_OPERATION;
Eric Laurent296fb132015-05-01 11:38:42 -0700721 }
Eric Laurent296fb132015-05-01 11:38:42 -0700722 return NO_ERROR;
723}
724
725status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -0700726 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
727 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -0700728{
Eric Laurent09f1ed22019-04-24 17:45:17 -0700729 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
Eric Laurent4463ff52019-02-07 13:56:09 -0800730 Mutex::Autolock _l(mLock);
Eric Laurent09f1ed22019-04-24 17:45:17 -0700731 auto it = mAudioDeviceCallbacks.find(audioIo);
732 if (it == mAudioDeviceCallbacks.end()) {
Eric Laurent296fb132015-05-01 11:38:42 -0700733 return INVALID_OPERATION;
734 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700735 if (it->second.erase(portId) == 0) {
Eric Laurent296fb132015-05-01 11:38:42 -0700736 return INVALID_OPERATION;
737 }
Eric Laurent09f1ed22019-04-24 17:45:17 -0700738 if (it->second.size() == 0) {
739 mAudioDeviceCallbacks.erase(audioIo);
Eric Laurent296fb132015-05-01 11:38:42 -0700740 }
741 return NO_ERROR;
742}
743
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800744/* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb)
Glenn Kasten4944acb2013-08-19 08:39:20 -0700745{
Ytai Ben-Tsvi000c3e42020-01-09 15:26:40 -0800746 Mutex::Autolock _l(gLockErrorCallbacks);
747 gAudioErrorCallbacks.insert(cb);
748 return reinterpret_cast<uintptr_t>(cb);
749}
750
751/* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) {
752 Mutex::Autolock _l(gLockErrorCallbacks);
753 gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb));
754}
755
756/* static */ void AudioSystem::reportError(status_t err) {
757 Mutex::Autolock _l(gLockErrorCallbacks);
758 for (auto callback : gAudioErrorCallbacks) {
759 callback(err);
760 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761}
762
Jean-Michel Trivif613d422015-04-23 18:41:29 -0700763/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
764{
765 Mutex::Autolock _l(gLock);
766 gDynPolicyCallback = cb;
767}
768
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800769/*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
770{
771 Mutex::Autolock _l(gLock);
772 gRecordConfigCallback = cb;
773}
774
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700775/*static*/ void AudioSystem::setRoutingCallback(routing_callback cb)
776{
777 Mutex::Autolock _l(gLock);
778 gRoutingCallback = cb;
779}
780
Eric Laurentc2f1f072009-07-17 12:17:14 -0700781// client singleton for AudioPolicyService binder interface
Glenn Kastend2d089f2014-11-05 11:48:12 -0800782// protected by gLockAPS
Eric Laurentc2f1f072009-07-17 12:17:14 -0700783sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
784sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
785
786
Glenn Kasten18a6d902012-09-24 11:27:56 -0700787// establish binder interface to AudioPolicy service
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800788const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
Eric Laurentc2f1f072009-07-17 12:17:14 -0700789{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800790 sp<IAudioPolicyService> ap;
791 sp<AudioPolicyServiceClient> apc;
792 {
793 Mutex::Autolock _l(gLockAPS);
794 if (gAudioPolicyService == 0) {
795 sp<IServiceManager> sm = defaultServiceManager();
796 sp<IBinder> binder;
797 do {
798 binder = sm->getService(String16("media.audio_policy"));
799 if (binder != 0)
800 break;
801 ALOGW("AudioPolicyService not published, waiting...");
802 usleep(500000); // 0.5 s
803 } while (true);
804 if (gAudioPolicyServiceClient == NULL) {
805 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
806 }
807 binder->linkToDeath(gAudioPolicyServiceClient);
808 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
809 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
810 apc = gAudioPolicyServiceClient;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700811 // Make sure callbacks can be received by gAudioPolicyServiceClient
812 ProcessState::self()->startThreadPool();
Eric Laurentc2f1f072009-07-17 12:17:14 -0700813 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800814 ap = gAudioPolicyService;
815 }
816 if (apc != 0) {
François Gaffie24437602018-04-23 15:08:59 +0200817 int64_t token = IPCThreadState::self()->clearCallingIdentity();
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800818 ap->registerClient(apc);
François Gaffie24437602018-04-23 15:08:59 +0200819 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
François Gaffiecfe17322018-11-07 13:41:29 +0100820 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
François Gaffie24437602018-04-23 15:08:59 +0200821 IPCThreadState::self()->restoreCallingIdentity(token);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822 }
Glenn Kastend2d089f2014-11-05 11:48:12 -0800823
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800824 return ap;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700825}
826
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700827// ---------------------------------------------------------------------------
828
Mikhail Naganov88b30d22020-03-09 19:43:13 +0000829void AudioSystem::onNewAudioModulesAvailable()
830{
831 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
832 if (aps == 0) return;
833 aps->onNewAudioModulesAvailable();
834}
835
Dima Zavinfce7a472011-04-19 22:30:36 -0700836status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
837 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800838 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800839 const char *device_name,
840 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700841{
842 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700843 const char *address = "";
Paul McLeane743a472015-01-28 11:07:31 -0800844 const char *name = "";
Eric Laurent71b63e32011-09-02 14:20:56 -0700845
Eric Laurentc2f1f072009-07-17 12:17:14 -0700846 if (aps == 0) return PERMISSION_DENIED;
847
Eric Laurent71b63e32011-09-02 14:20:56 -0700848 if (device_address != NULL) {
849 address = device_address;
850 }
Paul McLeane743a472015-01-28 11:07:31 -0800851 if (device_name != NULL) {
852 name = device_name;
853 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800854 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700855}
856
Dima Zavinfce7a472011-04-19 22:30:36 -0700857audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700858 const char *device_address)
859{
860 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700861 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862
863 return aps->getDeviceConnectionState(device, device_address);
864}
865
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800866status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
867 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800868 const char *device_name,
869 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800870{
871 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
872 const char *address = "";
873 const char *name = "";
874
875 if (aps == 0) return PERMISSION_DENIED;
876
877 if (device_address != NULL) {
878 address = device_address;
879 }
880 if (device_name != NULL) {
881 name = device_name;
882 }
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800883 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800884}
885
Eric Laurent00dba062020-02-11 15:52:09 -0800886status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887{
Glenn Kasten347966c2012-01-18 14:58:32 -0800888 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700889 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
890 if (aps == 0) return PERMISSION_DENIED;
891
Eric Laurent00dba062020-02-11 15:52:09 -0800892 return aps->setPhoneState(state, uid);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700893}
894
Dima Zavinfce7a472011-04-19 22:30:36 -0700895status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700896{
897 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
898 if (aps == 0) return PERMISSION_DENIED;
899 return aps->setForceUse(usage, config);
900}
901
Dima Zavinfce7a472011-04-19 22:30:36 -0700902audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700903{
904 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700905 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700906 return aps->getForceUse(usage);
907}
908
909
Eric Laurentf4e63452017-11-06 19:31:46 +0000910audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700911{
Eric Laurent1a9ed112012-03-20 18:36:01 -0700912 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
913 if (aps == 0) return 0;
Eric Laurentf4e63452017-11-06 19:31:46 +0000914 return aps->getOutput(stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700915}
916
Eric Laurent42984412019-05-09 17:57:03 -0700917status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
Eric Laurente83b55d2014-11-14 10:06:21 -0800918 audio_io_handle_t *output,
919 audio_session_t session,
920 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200921 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700922 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800923 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800924 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700925 audio_port_handle_t *selectedDeviceId,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800926 audio_port_handle_t *portId,
927 std::vector<audio_io_handle_t> *secondaryOutputs)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700928{
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700929 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurente83b55d2014-11-14 10:06:21 -0800930 if (aps == 0) return NO_INIT;
Nadav Bar766fb022018-01-07 12:18:03 +0200931 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
Ricardo Correa57a37692020-03-23 17:27:25 -0700932 config,
Kevin Rocard153f92d2018-12-18 18:33:28 -0800933 flags, selectedDeviceId, portId, secondaryOutputs);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700934}
935
Eric Laurentd7fe0862018-07-14 16:48:01 -0700936status_t AudioSystem::startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700937{
938 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
939 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700940 return aps->startOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941}
942
Eric Laurentd7fe0862018-07-14 16:48:01 -0700943status_t AudioSystem::stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944{
945 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
946 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700947 return aps->stopOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948}
949
Eric Laurentd7fe0862018-07-14 16:48:01 -0700950void AudioSystem::releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700951{
952 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
953 if (aps == 0) return;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700954 aps->releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700955}
956
Eric Laurentcaf7f482014-11-25 17:50:47 -0800957status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
958 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -0700959 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800960 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700961 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700962 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800963 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800964 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600965 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700966 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800967 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700968{
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800970 if (aps == 0) return NO_INIT;
Paul McLean466dc8e2015-04-17 13:15:36 -0600971 return aps->getInputForAttr(
Mikhail Naganov2996f672019-04-18 12:29:59 -0700972 attr, input, riid, session, pid, uid, opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800973 config, flags, selectedDeviceId, portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974}
975
Eric Laurent4eb58f12018-12-07 16:41:02 -0800976status_t AudioSystem::startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700977{
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800980 return aps->startInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981}
982
Eric Laurentfee19762018-01-29 18:44:13 -0800983status_t AudioSystem::stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984{
985 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
986 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentfee19762018-01-29 18:44:13 -0800987 return aps->stopInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988}
989
Eric Laurentfee19762018-01-29 18:44:13 -0800990void AudioSystem::releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700991{
992 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
993 if (aps == 0) return;
Eric Laurentfee19762018-01-29 18:44:13 -0800994 aps->releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700995}
996
Dima Zavinfce7a472011-04-19 22:30:36 -0700997status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998 int indexMin,
999 int indexMax)
1000{
1001 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1002 if (aps == 0) return PERMISSION_DENIED;
1003 return aps->initStreamVolume(stream, indexMin, indexMax);
1004}
1005
Eric Laurent83844cc2011-11-18 16:43:31 -08001006status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
1007 int index,
1008 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001009{
1010 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1011 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001012 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001013}
1014
Eric Laurent83844cc2011-11-18 16:43:31 -08001015status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
1016 int *index,
1017 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -07001018{
1019 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1020 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -08001021 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001022}
1023
François Gaffiecfe17322018-11-07 13:41:29 +01001024status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
1025 int index,
1026 audio_devices_t device)
1027{
1028 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1029 if (aps == 0) return PERMISSION_DENIED;
1030 return aps->setVolumeIndexForAttributes(attr, index, device);
1031}
1032
1033status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
1034 int &index,
1035 audio_devices_t device)
1036{
1037 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1038 if (aps == 0) return PERMISSION_DENIED;
1039 return aps->getVolumeIndexForAttributes(attr, index, device);
1040}
1041
1042status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1043{
1044 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1045 if (aps == 0) return PERMISSION_DENIED;
1046 return aps->getMaxVolumeIndexForAttributes(attr, index);
1047}
1048
1049status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1050{
1051 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1052 if (aps == 0) return PERMISSION_DENIED;
1053 return aps->getMinVolumeIndexForAttributes(attr, index);
1054}
1055
Dima Zavinfce7a472011-04-19 22:30:36 -07001056uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -07001057{
1058 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffied0ba9ed2018-11-05 11:50:42 +01001059 if (aps == 0) return PRODUCT_STRATEGY_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001060 return aps->getStrategyForStream(stream);
1061}
1062
Eric Laurent63742522012-03-08 13:42:42 -08001063audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001064{
1065 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001066 if (aps == 0) return AUDIO_DEVICE_NONE;
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001067 return aps->getDevicesForStream(stream);
1068}
1069
Jean-Michel Trivif41599b2020-01-07 14:22:08 -08001070status_t AudioSystem::getDevicesForAttributes(const AudioAttributes &aa,
1071 AudioDeviceTypeAddrVector *devices) {
1072 if (devices == nullptr) {
1073 return BAD_VALUE;
1074 }
1075 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1076 if (aps == 0) return PERMISSION_DENIED;
1077 return aps->getDevicesForAttributes(aa, devices);
1078}
1079
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001080audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -07001081{
1082 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Glenn Kastenefa6ea92014-01-08 09:10:43 -08001083 // FIXME change return type to status_t, and return PERMISSION_DENIED here
Glenn Kasten142f5192014-03-25 17:44:59 -07001084 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
Eric Laurentde070132010-07-13 04:45:46 -07001085 return aps->getOutputForEffect(desc);
1086}
1087
Glenn Kasten58e5aa32012-06-20 14:08:14 -07001088status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001089 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -07001090 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -08001091 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -07001092 int id)
1093{
1094 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1095 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001096 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -07001097}
1098
1099status_t AudioSystem::unregisterEffect(int id)
1100{
1101 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1102 if (aps == 0) return PERMISSION_DENIED;
1103 return aps->unregisterEffect(id);
1104}
1105
Eric Laurentdb7c0792011-08-10 10:37:50 -07001106status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1107{
1108 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1109 if (aps == 0) return PERMISSION_DENIED;
1110 return aps->setEffectEnabled(id, enabled);
1111}
1112
Eric Laurent6c796322019-04-09 14:13:17 -07001113status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1114{
1115 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1116 if (aps == 0) return PERMISSION_DENIED;
1117 return aps->moveEffectsToIo(ids, io);
1118}
1119
Glenn Kastenfff6d712012-01-12 16:38:12 -08001120status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001121{
Eric Laurenteda6c362011-02-02 09:33:30 -08001122 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1123 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001124 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -08001125 *state = aps->isStreamActive(stream, inPastMs);
1126 return NO_ERROR;
1127}
1128
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001129status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1130 uint32_t inPastMs)
1131{
1132 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1133 if (aps == 0) return PERMISSION_DENIED;
1134 if (state == NULL) return BAD_VALUE;
1135 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1136 return NO_ERROR;
1137}
1138
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001139status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1140{
1141 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1142 if (aps == 0) return PERMISSION_DENIED;
1143 if (state == NULL) return BAD_VALUE;
1144 *state = aps->isSourceActive(stream);
1145 return NO_ERROR;
1146}
1147
Glenn Kasten3b16c762012-11-14 08:44:39 -08001148uint32_t AudioSystem::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001149{
1150 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1151 if (af == 0) return 0;
1152 return af->getPrimaryOutputSamplingRate();
1153}
1154
Glenn Kastene33054e2012-11-14 12:54:39 -08001155size_t AudioSystem::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001156{
1157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1158 if (af == 0) return 0;
1159 return af->getPrimaryOutputFrameCount();
1160}
Eric Laurenteda6c362011-02-02 09:33:30 -08001161
Andy Hung6f248bb2018-01-23 14:04:37 -08001162status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001163{
1164 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1165 if (af == 0) return PERMISSION_DENIED;
Andy Hung6f248bb2018-01-23 14:04:37 -08001166 return af->setLowRamDevice(isLowRamDevice, totalMemory);
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001167}
1168
Eric Laurent9f6530f2011-08-30 10:18:54 -07001169void AudioSystem::clearAudioConfigCache()
1170{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001171 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
Steve Block3856b092011-10-20 11:56:00 +01001172 ALOGV("clearAudioConfigCache()");
Eric Laurentf6778fd2014-11-18 17:26:58 -08001173 {
1174 Mutex::Autolock _l(gLock);
Eric Laurent296fb132015-05-01 11:38:42 -07001175 if (gAudioFlingerClient != 0) {
1176 gAudioFlingerClient->clearIoCache();
1177 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001178 gAudioFlinger.clear();
1179 }
1180 {
1181 Mutex::Autolock _l(gLockAPS);
1182 gAudioPolicyService.clear();
1183 }
Eric Laurent9f6530f2011-08-30 10:18:54 -07001184}
1185
Hayden Gomes524159d2019-12-23 14:41:47 -08001186status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) {
1187 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1188 if (aps == nullptr) return PERMISSION_DENIED;
1189 return aps->setSupportedSystemUsages(systemUsages);
1190}
1191
Kevin Rocardb99cc752019-03-21 20:52:24 -07001192status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == nullptr) return PERMISSION_DENIED;
1195 return aps->setAllowedCapturePolicy(uid, flags);
1196}
1197
Eric Laurent90fe31c2020-11-26 20:06:35 +01001198audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info)
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001199{
Eric Laurent90fe31c2020-11-26 20:06:35 +01001200 ALOGV("%s", __func__);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent90fe31c2020-11-26 20:06:35 +01001202 if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED;
1203 return aps->getOffloadSupport(info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001204}
1205
Eric Laurent203b1a12014-04-01 10:34:16 -07001206status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1207 audio_port_type_t type,
1208 unsigned int *num_ports,
jiabin19cdba52020-11-24 11:28:58 -08001209 struct audio_port_v7 *ports,
Eric Laurent203b1a12014-04-01 10:34:16 -07001210 unsigned int *generation)
1211{
1212 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1213 if (aps == 0) return PERMISSION_DENIED;
1214 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1215}
1216
jiabin19cdba52020-11-24 11:28:58 -08001217status_t AudioSystem::getAudioPort(struct audio_port_v7 *port)
Eric Laurent203b1a12014-04-01 10:34:16 -07001218{
1219 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1220 if (aps == 0) return PERMISSION_DENIED;
1221 return aps->getAudioPort(port);
1222}
1223
1224status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1225 audio_patch_handle_t *handle)
1226{
1227 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1228 if (aps == 0) return PERMISSION_DENIED;
1229 return aps->createAudioPatch(patch, handle);
1230}
1231
1232status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1233{
1234 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1235 if (aps == 0) return PERMISSION_DENIED;
1236 return aps->releaseAudioPatch(handle);
1237}
1238
1239status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1240 struct audio_patch *patches,
1241 unsigned int *generation)
1242{
1243 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1244 if (aps == 0) return PERMISSION_DENIED;
1245 return aps->listAudioPatches(num_patches, patches, generation);
1246}
1247
1248status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1249{
1250 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1251 if (aps == 0) return PERMISSION_DENIED;
1252 return aps->setAudioPortConfig(config);
1253}
1254
Eric Laurent296fb132015-05-01 11:38:42 -07001255status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb52c1522014-05-20 11:27:36 -07001256{
Eric Laurentb28753e2015-04-01 13:06:28 -07001257 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1258 if (aps == 0) return PERMISSION_DENIED;
1259
1260 Mutex::Autolock _l(gLockAPS);
1261 if (gAudioPolicyServiceClient == 0) {
1262 return NO_INIT;
1263 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001264 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1265 if (ret == 1) {
1266 aps->setAudioPortCallbacksEnabled(true);
1267 }
1268 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurentb52c1522014-05-20 11:27:36 -07001269}
1270
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001271/*static*/
Eric Laurent296fb132015-05-01 11:38:42 -07001272status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001273{
1274 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1275 if (aps == 0) return PERMISSION_DENIED;
1276
1277 Mutex::Autolock _l(gLockAPS);
1278 if (gAudioPolicyServiceClient == 0) {
1279 return NO_INIT;
1280 }
Eric Laurente8726fe2015-06-26 09:39:24 -07001281 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1282 if (ret == 0) {
1283 aps->setAudioPortCallbacksEnabled(false);
1284 }
1285 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
Eric Laurent296fb132015-05-01 11:38:42 -07001286}
1287
François Gaffiecfe17322018-11-07 13:41:29 +01001288status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1289{
1290 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1291 if (aps == 0) return PERMISSION_DENIED;
1292
1293 Mutex::Autolock _l(gLockAPS);
1294 if (gAudioPolicyServiceClient == 0) {
1295 return NO_INIT;
1296 }
1297 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1298 if (ret == 1) {
1299 aps->setAudioVolumeGroupCallbacksEnabled(true);
1300 }
1301 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1302}
1303
1304status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1305{
1306 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1307 if (aps == 0) return PERMISSION_DENIED;
1308
1309 Mutex::Autolock _l(gLockAPS);
1310 if (gAudioPolicyServiceClient == 0) {
1311 return NO_INIT;
1312 }
1313 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1314 if (ret == 0) {
1315 aps->setAudioVolumeGroupCallbacksEnabled(false);
1316 }
1317 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1318}
1319
Eric Laurent296fb132015-05-01 11:38:42 -07001320status_t AudioSystem::addAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001321 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1322 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001323{
1324 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1325 if (afc == 0) {
1326 return NO_INIT;
1327 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001328 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001329 if (status == NO_ERROR) {
1330 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1331 if (af != 0) {
1332 af->registerClient(afc);
1333 }
1334 }
1335 return status;
Eric Laurent296fb132015-05-01 11:38:42 -07001336}
1337
1338status_t AudioSystem::removeAudioDeviceCallback(
Eric Laurent09f1ed22019-04-24 17:45:17 -07001339 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1340 audio_port_handle_t portId)
Eric Laurent296fb132015-05-01 11:38:42 -07001341{
1342 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1343 if (afc == 0) {
1344 return NO_INIT;
1345 }
Eric Laurent09f1ed22019-04-24 17:45:17 -07001346 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
Eric Laurent296fb132015-05-01 11:38:42 -07001347}
1348
1349audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1350{
1351 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1352 if (af == 0) return PERMISSION_DENIED;
1353 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1354 if (desc == 0) {
1355 return AUDIO_PORT_HANDLE_NONE;
1356 }
1357 return desc->getDeviceId();
Eric Laurentb28753e2015-04-01 13:06:28 -07001358}
1359
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001360status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1361 audio_io_handle_t *ioHandle,
1362 audio_devices_t *device)
1363{
1364 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1365 if (aps == 0) return PERMISSION_DENIED;
1366 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1367}
1368
1369status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1370{
1371 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1372 if (aps == 0) return PERMISSION_DENIED;
1373 return aps->releaseSoundTriggerSession(session);
1374}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001375
1376audio_mode_t AudioSystem::getPhoneState()
1377{
1378 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1379 if (aps == 0) return AUDIO_MODE_INVALID;
1380 return aps->getPhoneState();
1381}
1382
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001383status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -08001384{
1385 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1386 if (aps == 0) return PERMISSION_DENIED;
1387 return aps->registerPolicyMixes(mixes, registration);
1388}
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001389
jiabin6a02d532020-08-07 11:56:38 -07001390status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices)
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001391{
1392 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1393 if (aps == 0) return PERMISSION_DENIED;
1394 return aps->setUidDeviceAffinities(uid, devices);
1395}
1396
1397status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1398 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1399 if (aps == 0) return PERMISSION_DENIED;
1400 return aps->removeUidDeviceAffinities(uid);
1401}
1402
Oscar Azucena90e77632019-11-27 17:12:28 -08001403status_t AudioSystem::setUserIdDeviceAffinities(int userId,
jiabin6a02d532020-08-07 11:56:38 -07001404 const AudioDeviceTypeAddrVector& devices)
Oscar Azucena90e77632019-11-27 17:12:28 -08001405{
1406 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1407 if (aps == 0) return PERMISSION_DENIED;
1408 return aps->setUserIdDeviceAffinities(userId, devices);
1409}
1410
1411status_t AudioSystem::removeUserIdDeviceAffinities(int userId)
1412{
1413 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1414 if (aps == 0) return PERMISSION_DENIED;
1415 return aps->removeUserIdDeviceAffinities(userId);
1416}
1417
Eric Laurent554a2772015-04-10 11:29:24 -07001418status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1419 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001420 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001421{
1422 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1423 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001424 return aps->startAudioSource(source, attributes, portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001425}
1426
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001427status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -07001428{
1429 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1430 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001431 return aps->stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001432}
1433
Andy Hung2ddee192015-12-18 17:34:44 -08001434status_t AudioSystem::setMasterMono(bool mono)
1435{
1436 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1437 if (aps == 0) return PERMISSION_DENIED;
1438 return aps->setMasterMono(mono);
1439}
1440
1441status_t AudioSystem::getMasterMono(bool *mono)
1442{
1443 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1444 if (aps == 0) return PERMISSION_DENIED;
1445 return aps->getMasterMono(mono);
1446}
1447
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +01001448status_t AudioSystem::setMasterBalance(float balance)
1449{
1450 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1451 if (af == 0) return PERMISSION_DENIED;
1452 return af->setMasterBalance(balance);
1453}
1454
1455status_t AudioSystem::getMasterBalance(float *balance)
1456{
1457 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1458 if (af == 0) return PERMISSION_DENIED;
1459 return af->getMasterBalance(balance);
1460}
1461
Eric Laurentac9cef52017-06-09 15:46:26 -07001462float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1463{
1464 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1465 if (aps == 0) return NAN;
1466 return aps->getStreamVolumeDB(stream, index, device);
1467}
1468
jiabin46a76fa2018-01-05 10:18:21 -08001469status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1470{
1471 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1472 if (af == 0) return PERMISSION_DENIED;
1473 return af->getMicrophones(microphones);
1474}
1475
Eric Laurent42896a02019-09-27 15:40:33 -07001476status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1477 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1478 if (af == nullptr) return PERMISSION_DENIED;
1479 return af->setAudioHalPids(pids);
1480}
1481
jiabin81772902018-04-02 17:52:27 -07001482status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1483 audio_format_t *surroundFormats,
1484 bool *surroundFormatsEnabled,
1485 bool reported)
1486{
1487 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1488 if (aps == 0) return PERMISSION_DENIED;
1489 return aps->getSurroundFormats(
1490 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1491}
1492
1493status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1494{
1495 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1496 if (aps == 0) return PERMISSION_DENIED;
1497 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1498}
1499
Eric Laurentb78763e2018-10-17 10:08:02 -07001500status_t AudioSystem::setAssistantUid(uid_t uid)
1501{
1502 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1503 if (aps == 0) return PERMISSION_DENIED;
1504
1505 return aps->setAssistantUid(uid);
1506}
1507
1508status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1509{
1510 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1511 if (aps == 0) return PERMISSION_DENIED;
1512
1513 return aps->setA11yServicesUids(uids);
1514}
1515
Kohsuke Yatoha623a132020-03-24 20:10:26 -07001516status_t AudioSystem::setCurrentImeUid(uid_t uid)
1517{
1518 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1519 if (aps == 0) return PERMISSION_DENIED;
1520
1521 return aps->setCurrentImeUid(uid);
1522}
1523
jiabin6012f912018-11-02 17:06:30 -07001524bool AudioSystem::isHapticPlaybackSupported()
1525{
1526 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1527 if (aps == 0) return false;
1528 return aps->isHapticPlaybackSupported();
1529}
1530
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001531status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
François Gaffied0ba9ed2018-11-05 11:50:42 +01001532 std::vector<audio_format_t> *formats) {
1533 const sp <IAudioPolicyService>
1534 & aps = AudioSystem::get_audio_policy_service();
1535 if (aps == 0) return PERMISSION_DENIED;
1536 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1537}
1538
1539status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001540{
1541 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1542 if (aps == 0) return PERMISSION_DENIED;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001543 return aps->listAudioProductStrategies(strategies);
1544}
1545
1546audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1547{
1548 AudioProductStrategyVector strategies;
1549 listAudioProductStrategies(strategies);
1550 for (const auto &strategy : strategies) {
1551 auto attrVect = strategy.getAudioAttributes();
1552 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1553 return attributes.getStreamType() == stream; });
1554 if (iter != end(attrVect)) {
1555 return iter->getAttributes();
1556 }
1557 }
1558 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1559 return AUDIO_ATTRIBUTES_INITIALIZER;
1560}
1561
1562audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1563{
François Gaffie4b2018b2018-11-07 11:18:59 +01001564 product_strategy_t psId;
1565 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1566 if (ret != NO_ERROR) {
1567 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1568 return AUDIO_STREAM_MUSIC;
1569 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001570 AudioProductStrategyVector strategies;
1571 listAudioProductStrategies(strategies);
1572 for (const auto &strategy : strategies) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001573 if (strategy.getId() == psId) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001574 auto attrVect = strategy.getAudioAttributes();
1575 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1576 return AudioProductStrategy::attributesMatches(
1577 refAttr.getAttributes(), attr); });
1578 if (iter != end(attrVect)) {
1579 return iter->getStreamType();
1580 }
1581 }
1582 }
Jean-Michel Trivied678652019-12-19 13:39:30 -08001583 switch (attr.usage) {
1584 case AUDIO_USAGE_VIRTUAL_SOURCE:
1585 // virtual source is not expected to have an associated product strategy
1586 break;
1587 default:
1588 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1589 break;
1590 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001591 return AUDIO_STREAM_MUSIC;
1592}
1593
François Gaffie4b2018b2018-11-07 11:18:59 +01001594status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1595 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001596{
1597 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
François Gaffie4b2018b2018-11-07 11:18:59 +01001598 if (aps == 0) return PERMISSION_DENIED;
1599 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1600}
1601
1602status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1603{
1604 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1605 if (aps == 0) return PERMISSION_DENIED;
1606 return aps->listAudioVolumeGroups(groups);
1607}
1608
1609status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1610 volume_group_t &volumeGroup)
1611{
1612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1613 if (aps == 0) return PERMISSION_DENIED;
1614 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001615}
Eric Laurentb78763e2018-10-17 10:08:02 -07001616
Eric Laurent6ede98f2019-06-11 14:50:30 -07001617status_t AudioSystem::setRttEnabled(bool enabled)
1618{
1619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1620 if (aps == 0) return PERMISSION_DENIED;
1621 return aps->setRttEnabled(enabled);
1622}
1623
Eric Laurent8340e672019-11-06 11:01:08 -08001624bool AudioSystem::isCallScreenModeSupported()
1625{
1626 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1627 if (aps == 0) return false;
1628 return aps->isCallScreenModeSupported();
1629}
1630
jiabin0a488932020-08-07 17:32:40 -07001631status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy,
1632 device_role_t role,
1633 const AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001634{
1635 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1636 if (aps == 0) {
1637 return PERMISSION_DENIED;
1638 }
jiabin0a488932020-08-07 17:32:40 -07001639 return aps->setDevicesRoleForStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001640}
1641
jiabin0a488932020-08-07 17:32:40 -07001642status_t AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001643{
1644 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1645 if (aps == 0) {
1646 return PERMISSION_DENIED;
1647 }
jiabin0a488932020-08-07 17:32:40 -07001648 return aps->removeDevicesRoleForStrategy(strategy, role);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001649}
1650
jiabin0a488932020-08-07 17:32:40 -07001651status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy,
1652 device_role_t role,
1653 AudioDeviceTypeAddrVector &devices)
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001654{
1655 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1656 if (aps == 0) {
1657 return PERMISSION_DENIED;
1658 }
jiabin0a488932020-08-07 17:32:40 -07001659 return aps->getDevicesForRoleAndStrategy(strategy, role, devices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -07001660}
1661
Jiabin Huang3b98d322020-09-03 17:54:16 +00001662status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource,
1663 device_role_t role,
1664 const AudioDeviceTypeAddrVector &devices)
1665{
1666 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1667 if (aps == 0) {
1668 return PERMISSION_DENIED;
1669 }
1670 return aps->setDevicesRoleForCapturePreset(audioSource, role, devices);
1671}
1672
1673status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource,
1674 device_role_t role,
1675 const AudioDeviceTypeAddrVector &devices)
1676{
1677 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1678 if (aps == 0) {
1679 return PERMISSION_DENIED;
1680 }
1681 return aps->addDevicesRoleForCapturePreset(audioSource, role, devices);
1682}
1683
1684status_t AudioSystem::removeDevicesRoleForCapturePreset(
1685 audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices)
1686{
1687 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1688 if (aps == 0) {
1689 return PERMISSION_DENIED;
1690 }
1691 return aps->removeDevicesRoleForCapturePreset(audioSource, role, devices);
1692}
1693
1694status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource,
1695 device_role_t role)
1696{
1697 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1698 if (aps == 0) {
1699 return PERMISSION_DENIED;
1700 }
1701 return aps->clearDevicesRoleForCapturePreset(audioSource, role);
1702}
1703
1704status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource,
1705 device_role_t role,
1706 AudioDeviceTypeAddrVector &devices)
1707{
1708 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1709 if (aps == 0) {
1710 return PERMISSION_DENIED;
1711 }
1712 return aps->getDevicesForRoleAndCapturePreset(audioSource, role, devices);
1713}
1714
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001715class CaptureStateListenerImpl : public media::BnCaptureStateListener,
1716 public IBinder::DeathRecipient {
1717public:
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001718 CaptureStateListenerImpl(
1719 const sp<IAudioPolicyService>& aps,
1720 const sp<AudioSystem::CaptureStateListener>& listener)
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001721 : mAps(aps), mListener(listener) {}
1722
1723 void init() {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001724 bool active;
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001725 status_t status = mAps->registerSoundTriggerCaptureStateListener(this, &active);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001726 if (status != NO_ERROR) {
1727 mListener->onServiceDied();
1728 return;
1729 }
1730 mListener->onStateChanged(active);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001731 IInterface::asBinder(mAps)->linkToDeath(this);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001732 }
1733
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001734 binder::Status setCaptureState(bool active) override {
1735 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001736 mListener->onStateChanged(active);
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001737 return binder::Status::ok();
1738 }
1739
1740 void binderDied(const wp<IBinder>&) override {
1741 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001742 mListener->onServiceDied();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001743 gSoundTriggerCaptureStateListener = nullptr;
1744 }
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001745
1746private:
1747 // Need this in order to keep the death receipent alive.
1748 sp<IAudioPolicyService> mAps;
1749 sp<AudioSystem::CaptureStateListener> mListener;
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001750};
1751
1752status_t AudioSystem::registerSoundTriggerCaptureStateListener(
1753 const sp<CaptureStateListener>& listener) {
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001754 LOG_ALWAYS_FATAL_IF(listener == nullptr);
1755
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001756 const sp<IAudioPolicyService>& aps =
1757 AudioSystem::get_audio_policy_service();
1758 if (aps == 0) {
1759 return PERMISSION_DENIED;
1760 }
1761
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001762 Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock);
Ytai Ben-Tsvid9f82832020-08-27 09:31:10 -07001763 gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener);
Ytai Ben-Tsvia46b6d32020-08-31 13:29:11 -07001764 gSoundTriggerCaptureStateListener->init();
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001765
Ytai Ben-Tsvi5120eda2020-03-26 09:41:15 -07001766 return NO_ERROR;
1767}
1768
Eric Laurentc2f1f072009-07-17 12:17:14 -07001769// ---------------------------------------------------------------------------
1770
Eric Laurente8726fe2015-06-26 09:39:24 -07001771int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001772 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001773{
1774 Mutex::Autolock _l(mLock);
1775 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001776 if (mAudioPortCallbacks[i] == callback) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001777 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001778 }
1779 }
Eric Laurent296fb132015-05-01 11:38:42 -07001780 mAudioPortCallbacks.add(callback);
Eric Laurente8726fe2015-06-26 09:39:24 -07001781 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001782}
1783
Eric Laurente8726fe2015-06-26 09:39:24 -07001784int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
Eric Laurent296fb132015-05-01 11:38:42 -07001785 const sp<AudioPortCallback>& callback)
Eric Laurentb28753e2015-04-01 13:06:28 -07001786{
1787 Mutex::Autolock _l(mLock);
1788 size_t i;
1789 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
Eric Laurent296fb132015-05-01 11:38:42 -07001790 if (mAudioPortCallbacks[i] == callback) {
Eric Laurentb28753e2015-04-01 13:06:28 -07001791 break;
1792 }
1793 }
1794 if (i == mAudioPortCallbacks.size()) {
Eric Laurente8726fe2015-06-26 09:39:24 -07001795 return -1;
Eric Laurentb28753e2015-04-01 13:06:28 -07001796 }
1797 mAudioPortCallbacks.removeAt(i);
Eric Laurente8726fe2015-06-26 09:39:24 -07001798 return mAudioPortCallbacks.size();
Eric Laurentb28753e2015-04-01 13:06:28 -07001799}
1800
Eric Laurent296fb132015-05-01 11:38:42 -07001801
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001802Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
Eric Laurentb28753e2015-04-01 13:06:28 -07001803{
1804 Mutex::Autolock _l(mLock);
1805 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1806 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1807 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001808 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07001809}
1810
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001811Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
Eric Laurentb28753e2015-04-01 13:06:28 -07001812{
1813 Mutex::Autolock _l(mLock);
1814 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1815 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1816 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001817 return Status::ok();
Eric Laurentb28753e2015-04-01 13:06:28 -07001818}
1819
François Gaffiecfe17322018-11-07 13:41:29 +01001820// ----------------------------------------------------------------------------
1821int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1822 const sp<AudioVolumeGroupCallback>& callback)
1823{
1824 Mutex::Autolock _l(mLock);
1825 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1826 if (mAudioVolumeGroupCallback[i] == callback) {
1827 return -1;
1828 }
1829 }
1830 mAudioVolumeGroupCallback.add(callback);
1831 return mAudioVolumeGroupCallback.size();
1832}
1833
1834int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1835 const sp<AudioVolumeGroupCallback>& callback)
1836{
1837 Mutex::Autolock _l(mLock);
1838 size_t i;
1839 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1840 if (mAudioVolumeGroupCallback[i] == callback) {
1841 break;
1842 }
1843 }
1844 if (i == mAudioVolumeGroupCallback.size()) {
1845 return -1;
1846 }
1847 mAudioVolumeGroupCallback.removeAt(i);
1848 return mAudioVolumeGroupCallback.size();
1849}
1850
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001851Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group,
1852 int32_t flags) {
1853 volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1854 aidl2legacy_int32_t_volume_group_t(group));
1855 int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags));
1856
François Gaffiecfe17322018-11-07 13:41:29 +01001857 Mutex::Autolock _l(mLock);
1858 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001859 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy);
François Gaffiecfe17322018-11-07 13:41:29 +01001860 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001861 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +01001862}
1863// ----------------------------------------------------------------------------
1864
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001865Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1866 const ::std::string& regId, int32_t state) {
1867 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state);
1868
1869 String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId));
1870 int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state));
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001871 dynamic_policy_callback cb = NULL;
1872 {
1873 Mutex::Autolock _l(AudioSystem::gLock);
1874 cb = gDynPolicyCallback;
1875 }
1876
1877 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001878 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy);
Jean-Michel Trivif613d422015-04-23 18:41:29 -07001879 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001880 return Status::ok();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001881}
1882
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001883Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
1884 int32_t event,
1885 const media::RecordClientInfo& clientInfo,
1886 const media::AudioConfigBase& clientConfig,
1887 const std::vector<media::EffectDescriptor>& clientEffects,
1888 const media::AudioConfigBase& deviceConfig,
1889 const std::vector<media::EffectDescriptor>& effects,
1890 int32_t patchHandle,
1891 media::AudioSourceType source) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001892 record_config_callback cb = NULL;
1893 {
1894 Mutex::Autolock _l(AudioSystem::gLock);
1895 cb = gRecordConfigCallback;
1896 }
1897
1898 if (cb != NULL) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001899 int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event));
1900 record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1901 aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo));
1902 audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1903 aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig));
1904 std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1905 convertContainer<std::vector<effect_descriptor_t>>(
1906 clientEffects,
1907 aidl2legacy_EffectDescriptor_effect_descriptor_t));
1908 audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1909 aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig));
1910 std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1911 convertContainer<std::vector<effect_descriptor_t>>(
1912 effects,
1913 aidl2legacy_EffectDescriptor_effect_descriptor_t));
1914 audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1915 aidl2legacy_int32_t_audio_patch_handle_t(patchHandle));
1916 audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS(
1917 aidl2legacy_AudioSourceType_audio_source_t(source));
1918 cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy,
1919 &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001920 }
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001921 return Status::ok();
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001922}
1923
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001924Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() {
1925 routing_callback cb = NULL;
1926 {
1927 Mutex::Autolock _l(AudioSystem::gLock);
1928 cb = gRoutingCallback;
1929 }
1930
1931 if (cb != NULL) {
1932 cb();
1933 }
1934 return Status::ok();
1935}
1936
Glenn Kasten4944acb2013-08-19 08:39:20 -07001937void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1938{
Glenn Kastend2d089f2014-11-05 11:48:12 -08001939 {
Eric Laurentb28753e2015-04-01 13:06:28 -07001940 Mutex::Autolock _l(mLock);
1941 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1942 mAudioPortCallbacks[i]->onServiceDied();
Glenn Kastend2d089f2014-11-05 11:48:12 -08001943 }
François Gaffiecfe17322018-11-07 13:41:29 +01001944 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1945 mAudioVolumeGroupCallback[i]->onServiceDied();
1946 }
Eric Laurentb52c1522014-05-20 11:27:36 -07001947 }
Glenn Kastend2d089f2014-11-05 11:48:12 -08001948 {
1949 Mutex::Autolock _l(gLockAPS);
1950 AudioSystem::gAudioPolicyService.clear();
1951 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001952
Steve Block5ff1dd52012-01-05 23:22:43 +00001953 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -07001954}
1955
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -08001956ConversionResult<record_client_info_t>
1957aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) {
1958 record_client_info_t legacy;
1959 legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid));
1960 legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid));
1961 legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session));
1962 legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSourceType_audio_source_t(aidl.source));
1963 legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId));
1964 legacy.silenced = aidl.silenced;
1965 return legacy;
1966}
1967
1968ConversionResult<media::RecordClientInfo>
1969legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) {
1970 media::RecordClientInfo aidl;
1971 aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid));
1972 aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid));
1973 aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session));
1974 aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSourceType(legacy.source));
1975 aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id));
1976 aidl.silenced = legacy.silenced;
1977 return aidl;
1978}
1979
Glenn Kasten40bc9062015-03-20 09:09:33 -07001980} // namespace android