blob: 45194d4c548eb3c28e6d6c19947653de81f789c9 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 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 "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mikhail Naganov959e2d02019-03-28 11:08:19 -070051static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070060 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070061 mAudioPolicyManager(NULL),
62 mAudioPolicyClient(NULL),
63 mPhoneState(AUDIO_MODE_INVALID),
64 mCaptureStateNotifier(false) {
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070069 {
70 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080071
Eric Laurent8b1e80b2014-10-07 09:08:47 -070072 // start audio commands thread
73 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
74 // start output activity command thread
75 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 mAudioPolicyClient = new AudioPolicyClient(this);
78 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 }
bryant_liuba2b4392014-06-11 16:49:30 +080080 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +000081 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
82 sp<UidPolicy> uidPolicy = new UidPolicy(this);
83 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070084 {
85 Mutex::Autolock _l(mLock);
86 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +000087 mUidPolicy = uidPolicy;
88 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -070089 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +000090 uidPolicy->registerSelf();
91 sensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070092}
93
94AudioPolicyService::~AudioPolicyService()
95{
Mathias Agopian65ab4712010-07-14 17:59:35 -070096 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070097 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070098
Eric Laurentf269b8e2014-06-09 20:01:29 -070099 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700100 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700101
102 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800103 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800104
105 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800106 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000107
108 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800109 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700110}
111
112// A notification client is always registered by AudioSystem when the client process
113// connects to AudioPolicyService.
114void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
115{
Eric Laurent12590252015-08-21 18:40:20 -0700116 if (client == 0) {
117 ALOGW("%s got NULL client", __FUNCTION__);
118 return;
119 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800120 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700121
122 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800123 pid_t pid = IPCThreadState::self()->getCallingPid();
124 int64_t token = ((int64_t)uid<<32) | pid;
125
126 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700127 sp<NotificationClient> notificationClient = new NotificationClient(this,
128 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800129 uid,
130 pid);
131 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700132
luochaojiang908c7d72018-06-21 14:58:04 +0800133 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700134
Marco Nelissenf8880202014-11-14 07:58:25 -0800135 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700136 binder->linkToDeath(notificationClient);
137 }
138}
139
Eric Laurente8726fe2015-06-26 09:39:24 -0700140void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
141{
142 Mutex::Autolock _l(mNotificationClientsLock);
143
144 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800145 pid_t pid = IPCThreadState::self()->getCallingPid();
146 int64_t token = ((int64_t)uid<<32) | pid;
147
148 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700149 return;
150 }
luochaojiang908c7d72018-06-21 14:58:04 +0800151 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700152}
153
François Gaffiecfe17322018-11-07 13:41:29 +0100154void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
155{
156 Mutex::Autolock _l(mNotificationClientsLock);
157
158 uid_t uid = IPCThreadState::self()->getCallingUid();
159 pid_t pid = IPCThreadState::self()->getCallingPid();
160 int64_t token = ((int64_t)uid<<32) | pid;
161
162 if (mNotificationClients.indexOfKey(token) < 0) {
163 return;
164 }
165 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
166}
167
Eric Laurentb52c1522014-05-20 11:27:36 -0700168// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800169void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700170{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000171 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800172 {
173 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800174 int64_t token = ((int64_t)uid<<32) | pid;
175 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800176 for (size_t i = 0; i < mNotificationClients.size(); i++) {
177 if (mNotificationClients.valueAt(i)->uid() == uid) {
178 hasSameUid = true;
179 break;
180 }
181 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000182 }
183 {
184 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800185 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700186 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700187 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700188 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800189 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700190}
191
192void AudioPolicyService::onAudioPortListUpdate()
193{
194 mOutputCommandThread->updateAudioPortListCommand();
195}
196
197void AudioPolicyService::doOnAudioPortListUpdate()
198{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800199 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700200 for (size_t i = 0; i < mNotificationClients.size(); i++) {
201 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
202 }
203}
204
205void AudioPolicyService::onAudioPatchListUpdate()
206{
207 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208}
209
Eric Laurentb52c1522014-05-20 11:27:36 -0700210void AudioPolicyService::doOnAudioPatchListUpdate()
211{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800212 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700213 for (size_t i = 0; i < mNotificationClients.size(); i++) {
214 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
215 }
216}
217
François Gaffiecfe17322018-11-07 13:41:29 +0100218void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
219{
220 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
221}
222
223void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
224{
225 Mutex::Autolock _l(mNotificationClientsLock);
226 for (size_t i = 0; i < mNotificationClients.size(); i++) {
227 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
228 }
229}
230
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700231void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700232{
233 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
234 regId.string(), state);
235 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
236}
237
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700238void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700239{
240 Mutex::Autolock _l(mNotificationClientsLock);
241 for (size_t i = 0; i < mNotificationClients.size(); i++) {
242 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
243 }
244}
245
Eric Laurenta9f86652018-11-28 17:23:11 -0800246void AudioPolicyService::onRecordingConfigurationUpdate(
247 int event,
248 const record_client_info_t *clientInfo,
249 const audio_config_base_t *clientConfig,
250 std::vector<effect_descriptor_t> clientEffects,
251 const audio_config_base_t *deviceConfig,
252 std::vector<effect_descriptor_t> effects,
253 audio_patch_handle_t patchHandle,
254 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800255{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800256 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800257 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800258}
259
Eric Laurenta9f86652018-11-28 17:23:11 -0800260void AudioPolicyService::doOnRecordingConfigurationUpdate(
261 int event,
262 const record_client_info_t *clientInfo,
263 const audio_config_base_t *clientConfig,
264 std::vector<effect_descriptor_t> clientEffects,
265 const audio_config_base_t *deviceConfig,
266 std::vector<effect_descriptor_t> effects,
267 audio_patch_handle_t patchHandle,
268 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800269{
270 Mutex::Autolock _l(mNotificationClientsLock);
271 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800272 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800273 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800274 }
275}
276
277status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
278 audio_patch_handle_t *handle,
279 int delayMs)
280{
281 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
282}
283
284status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
285 int delayMs)
286{
287 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
288}
289
Eric Laurente1715a42014-05-20 11:30:42 -0700290status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
291 int delayMs)
292{
293 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
294}
295
Eric Laurentb52c1522014-05-20 11:27:36 -0700296AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
297 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800298 uid_t uid,
299 pid_t pid)
300 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100301 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700302{
303}
304
305AudioPolicyService::NotificationClient::~NotificationClient()
306{
307}
308
309void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
310{
311 sp<NotificationClient> keep(this);
312 sp<AudioPolicyService> service = mService.promote();
313 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800314 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700315 }
316}
317
318void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
319{
Eric Laurente8726fe2015-06-26 09:39:24 -0700320 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700321 mAudioPolicyServiceClient->onAudioPortListUpdate();
322 }
323}
324
325void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
326{
Eric Laurente8726fe2015-06-26 09:39:24 -0700327 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700328 mAudioPolicyServiceClient->onAudioPatchListUpdate();
329 }
330}
Eric Laurent57dae992011-07-24 13:36:09 -0700331
François Gaffiecfe17322018-11-07 13:41:29 +0100332void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
333 int flags)
334{
335 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
336 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
337 }
338}
339
340
Jean-Michel Trivide801052015-04-14 19:10:14 -0700341void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700342 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700343{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700344 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800345 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
346 }
347}
348
349void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800350 int event,
351 const record_client_info_t *clientInfo,
352 const audio_config_base_t *clientConfig,
353 std::vector<effect_descriptor_t> clientEffects,
354 const audio_config_base_t *deviceConfig,
355 std::vector<effect_descriptor_t> effects,
356 audio_patch_handle_t patchHandle,
357 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800358{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700359 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800360 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800361 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700362 }
363}
364
Eric Laurente8726fe2015-06-26 09:39:24 -0700365void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
366{
367 mAudioPortCallbacksEnabled = enabled;
368}
369
François Gaffiecfe17322018-11-07 13:41:29 +0100370void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
371{
372 mAudioVolumeGroupCallbacksEnabled = enabled;
373}
Eric Laurente8726fe2015-06-26 09:39:24 -0700374
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700376 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700377 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378}
379
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000380static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000382 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
383}
384
385static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
386{
387 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
390status_t AudioPolicyService::dumpInternals(int fd)
391{
392 const size_t SIZE = 256;
393 char buffer[SIZE];
394 String8 result;
395
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 result.append(buffer);
398 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
399 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
Hayden Gomes524159d2019-12-23 14:41:47 -0800401 snprintf(buffer, SIZE, "Supported System Usages:\n");
402 result.append(buffer);
403 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
404 it != mSupportedSystemUsages.end(); ++it) {
405 snprintf(buffer, SIZE, "\t%d\n", *it);
406 result.append(buffer);
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 write(fd, result.string(), result.size());
410 return NO_ERROR;
411}
412
Eric Laurente8c8b432018-10-17 10:08:02 -0700413void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800414{
Eric Laurente8c8b432018-10-17 10:08:02 -0700415 Mutex::Autolock _l(mLock);
416 updateUidStates_l();
417}
418
419void AudioPolicyService::updateUidStates_l()
420{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800421// Go over all active clients and allow capture (does not force silence) in the
422// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700423// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700424// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700425// AND the source is VOICE_RECOGNITION or HOTWORD
426// OR uses VOICE_RECOGNITION AND is on TOP
427// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700428// AND there is no active privacy sensitive capture or call
429// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800430// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700431// AND Is on TOP
432// AND the source is VOICE_RECOGNITION or HOTWORD
433// OR The assistant is not on TOP
434// AND there is no active privacy sensitive capture or call
435// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700436// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700439// OR the client source is HOTWORD
440// AND is on TOP
441// OR all active clients are using HOTWORD source
442// AND no call is active
443// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700444// OR the client is the current InputMethodService
445// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700446// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800447// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700448// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700449// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700450// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800451
Eric Laurent4e947da2019-10-17 15:24:06 -0700452
Eric Laurent4eb58f12018-12-07 16:41:02 -0800453 sp<AudioRecordClient> topActive;
454 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800455 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700456 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700457
Eric Laurenta46bedb2018-12-07 18:01:26 -0800458 nsecs_t topStartNs = 0;
459 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800460 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800461 nsecs_t latestSensitiveStartNs = 0;
462 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
463 bool isAssistantOnTop = false;
464 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700465 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800466 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
467 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700468 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700469 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700470 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800471
Michael Groovercfd28302018-12-11 19:16:46 -0800472 // if Sensor Privacy is enabled then all recordings should be silenced.
473 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
474 silenceAllRecordings_l();
475 return;
476 }
477
Eric Laurente8c8b432018-10-17 10:08:02 -0700478 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
479 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700480 if (!current->active) {
481 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800482 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700483
484 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
485 // clients which app is in IDLE state are not eligible for top active or
486 // latest active
487 if (appState == APP_STATE_IDLE) {
488 continue;
489 }
490
Eric Laurent589171c2019-07-25 18:04:29 -0700491 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurent14a88632020-07-16 12:28:30 -0700492 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800493 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700494 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800495 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
496 bool isPrivacySensitive =
497 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700498
Eric Laurentc21d5692020-02-25 10:24:36 -0800499 if (appState == APP_STATE_TOP) {
500 if (isPrivacySensitive) {
501 if (current->startTimeNs > topSensitiveStartNs) {
502 topSensitiveActive = current;
503 topSensitiveStartNs = current->startTimeNs;
504 }
505 } else {
506 if (current->startTimeNs > topStartNs) {
507 topActive = current;
508 topStartNs = current->startTimeNs;
509 }
510 }
511 if (isAssistant) {
512 isAssistantOnTop = true;
513 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800514 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800515 // Clients capturing for HOTWORD are not considered
516 // for latest active to avoid masking regular clients started before
517 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
518 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
519 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700520 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
521 // is marked latest sensitive active even if another app qualifies.
522 if (current->startTimeNs > latestSensitiveStartNs
523 || (isInCommunication && current->uid == mPhoneStateOwnerUid)) {
524 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
525 || latestSensitiveActiveOrComm->uid != mPhoneStateOwnerUid) {
526 latestSensitiveActiveOrComm = current;
527 latestSensitiveStartNs = current->startTimeNs;
528 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800529 }
530 isSensitiveActive = true;
531 } else {
532 if (current->startTimeNs > latestStartNs) {
533 latestActive = current;
534 latestStartNs = current->startTimeNs;
535 }
536 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800537 }
538 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700539 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
540 onlyHotwordActive = false;
541 }
Eric Laurentb809a752020-06-29 09:53:13 -0700542 if (current->uid == mPhoneStateOwnerUid) {
543 isPhoneStateOwnerActive = true;
544 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800545 }
546
Eric Laurent1ff16a72019-03-14 18:35:04 -0700547 // if no active client with UI on Top, consider latest active as top
548 if (topActive == nullptr) {
549 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800550 topStartNs = latestStartNs;
551 }
552 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700553 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800554 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700555 } else if (latestSensitiveActiveOrComm != nullptr) {
556 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
557 // foreground UI in case both are capturing with privacy sensitive flag.
558 if (isInCommunication && latestSensitiveActiveOrComm->uid == mPhoneStateOwnerUid) {
559 topSensitiveActive = latestSensitiveActiveOrComm;
560 topSensitiveStartNs = latestSensitiveStartNs;
561 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800562 }
563
564 // If both privacy sensitive and regular capture are active:
565 // if the regular capture is privileged
566 // allow concurrency
567 // else
568 // favor the privacy sensitive case
569 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700570 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800571 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800572 }
573
574 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
575 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700576 if (!current->active) {
577 continue;
578 }
579
Eric Laurent4eb58f12018-12-07 16:41:02 -0800580 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700581 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800582 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
583 false : current->uid == topSensitiveActive->uid;
584
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000585 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700586 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700587 bool canCaptureCommunication = recordClient->canCaptureOutput
588 || !isPhoneStateOwnerActive
589 || recordClient->uid == mPhoneStateOwnerUid;
590 return !(isInCall && !canCaptureCall)
591 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800592 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700593
594 // By default allow capture if:
595 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700596 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700597 // AND there is no active privacy sensitive capture or call
598 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
599 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800600 && (isTopOrLatestActive || isTopOrLatestSensitive)
601 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700602 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800603 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800604
605 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700606 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
607 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800608 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700609 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700610 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700611 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700612 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700613 // OR uses HOTWORD
614 // AND there is no active privacy sensitive capture or call
615 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700616 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800617 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700618 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800619 }
620 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700621 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800622 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700623 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800624 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700625 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800626 }
627 }
628 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700629 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700630 // The assistant is not on TOP
631 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700632 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700633 // OR
634 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
635 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700636 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800637 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700638 allowCapture = true;
639 }
Eric Laurent589171c2019-07-25 18:04:29 -0700640 if (isA11yOnTop) {
641 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
642 allowCapture = true;
643 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800644 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700645 } else if (source == AUDIO_SOURCE_HOTWORD) {
646 // For HOTWORD source allow capture when not on TOP if:
647 // All active clients are using HOTWORD source
648 // AND no call is active
649 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800650 if (onlyHotwordActive
651 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700652 allowCapture = true;
653 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700654 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
655 // For current InputMethodService allow capture if:
656 // A RTT call is active AND the source is VOICE_RECOGNITION
657 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
658 allowCapture = true;
659 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800660 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700661 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700662 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
663 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700664 }
665}
666
Michael Groovercfd28302018-12-11 19:16:46 -0800667void AudioPolicyService::silenceAllRecordings_l() {
668 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
669 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700670 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700671 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700672 }
Michael Groovercfd28302018-12-11 19:16:46 -0800673 }
674}
675
Eric Laurente8c8b432018-10-17 10:08:02 -0700676/* static */
677app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700678
679 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700680 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700681 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
682 // include persistent services
683 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700684 }
685 return APP_STATE_FOREGROUND;
686}
687
Eric Laurent4eb58f12018-12-07 16:41:02 -0800688/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800689bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800690{
691 switch (source) {
692 case AUDIO_SOURCE_VOICE_UPLINK:
693 case AUDIO_SOURCE_VOICE_DOWNLINK:
694 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800695 case AUDIO_SOURCE_REMOTE_SUBMIX:
696 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700697 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800698 return true;
699 default:
700 break;
701 }
702 return false;
703}
704
Eric Laurent5ada82e2019-08-29 17:53:54 -0700705void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700706{
707 AutoCallerClear acc;
708
709 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700710 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700711 }
712 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
713 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700714 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700715 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700716 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800717}
718
Glenn Kasten0f11b512014-01-31 16:18:54 -0800719status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720{
Glenn Kasten44deb052012-02-05 18:09:08 -0800721 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 dumpPermissionDenial(fd);
723 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000724 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 if (!locked) {
726 String8 result(kDeadlockedString);
727 write(fd, result.string(), result.size());
728 }
729
730 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800731 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 mAudioCommandThread->dump(fd);
733 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734
Eric Laurentdce54a12014-03-10 12:19:46 -0700735 if (mAudioPolicyManager) {
736 mAudioPolicyManager->dump(fd);
737 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738
Kevin Rocard8be94972019-02-22 13:26:25 -0800739 mPackageManager.dump(fd);
740
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000741 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 }
743 return NO_ERROR;
744}
745
746status_t AudioPolicyService::dumpPermissionDenial(int fd)
747{
748 const size_t SIZE = 256;
749 char buffer[SIZE];
750 String8 result;
751 snprintf(buffer, SIZE, "Permission Denial: "
752 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
753 IPCThreadState::self()->getCallingPid(),
754 IPCThreadState::self()->getCallingUid());
755 result.append(buffer);
756 write(fd, result.string(), result.size());
757 return NO_ERROR;
758}
759
760status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800761 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
762 switch (code) {
763 case SHELL_COMMAND_TRANSACTION: {
764 int in = data.readFileDescriptor();
765 int out = data.readFileDescriptor();
766 int err = data.readFileDescriptor();
767 int argc = data.readInt32();
768 Vector<String16> args;
769 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
770 args.add(data.readString16());
771 }
772 sp<IBinder> unusedCallback;
773 sp<IResultReceiver> resultReceiver;
774 status_t status;
775 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
776 return status;
777 }
778 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
779 return status;
780 }
781 status = shellCommand(in, out, err, args);
782 if (resultReceiver != nullptr) {
783 resultReceiver->send(status);
784 }
785 return NO_ERROR;
786 }
787 }
788
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 return BnAudioPolicyService::onTransact(code, data, reply, flags);
790}
791
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800792// ------------------- Shell command implementation -------------------
793
794// NOTE: This is a remote API - make sure all args are validated
795status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
796 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
797 return PERMISSION_DENIED;
798 }
799 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
800 return BAD_VALUE;
801 }
jovanakbe066e12019-09-02 11:54:39 -0700802 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800803 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700804 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800805 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700806 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800807 return handleGetUidState(args, out, err);
808 } else if (args.size() == 1 && args[0] == String16("help")) {
809 printHelp(out);
810 return NO_ERROR;
811 }
812 printHelp(err);
813 return BAD_VALUE;
814}
815
jovanakbe066e12019-09-02 11:54:39 -0700816static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
817 if (userId < 0) {
818 ALOGE("Invalid user: %d", userId);
819 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800820 return BAD_VALUE;
821 }
jovanakbe066e12019-09-02 11:54:39 -0700822
823 PermissionController pc;
824 uid = pc.getPackageUid(packageName, 0);
825 if (uid <= 0) {
826 ALOGE("Unknown package: '%s'", String8(packageName).string());
827 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
828 return BAD_VALUE;
829 }
830
831 uid = multiuser_get_uid(userId, uid);
832 return NO_ERROR;
833}
834
835status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
836 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
837 if (!(args.size() == 3 || args.size() == 5)) {
838 printHelp(err);
839 return BAD_VALUE;
840 }
841
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800842 bool active = false;
843 if (args[2] == String16("active")) {
844 active = true;
845 } else if ((args[2] != String16("idle"))) {
846 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
847 return BAD_VALUE;
848 }
jovanakbe066e12019-09-02 11:54:39 -0700849
850 int userId = 0;
851 if (args.size() >= 5 && args[3] == String16("--user")) {
852 userId = atoi(String8(args[4]));
853 }
854
855 uid_t uid;
856 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
857 return BAD_VALUE;
858 }
859
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000860 sp<UidPolicy> uidPolicy;
861 {
862 Mutex::Autolock _l(mLock);
863 uidPolicy = mUidPolicy;
864 }
865 if (uidPolicy) {
866 uidPolicy->addOverrideUid(uid, active);
867 return NO_ERROR;
868 }
869 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800870}
871
872status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700873 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
874 if (!(args.size() == 2 || args.size() == 4)) {
875 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800876 return BAD_VALUE;
877 }
jovanakbe066e12019-09-02 11:54:39 -0700878
879 int userId = 0;
880 if (args.size() >= 4 && args[2] == String16("--user")) {
881 userId = atoi(String8(args[3]));
882 }
883
884 uid_t uid;
885 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
886 return BAD_VALUE;
887 }
888
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000889 sp<UidPolicy> uidPolicy;
890 {
891 Mutex::Autolock _l(mLock);
892 uidPolicy = mUidPolicy;
893 }
894 if (uidPolicy) {
895 uidPolicy->removeOverrideUid(uid);
896 return NO_ERROR;
897 }
898 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800899}
900
901status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700902 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
903 if (!(args.size() == 2 || args.size() == 4)) {
904 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800905 return BAD_VALUE;
906 }
jovanakbe066e12019-09-02 11:54:39 -0700907
908 int userId = 0;
909 if (args.size() >= 4 && args[2] == String16("--user")) {
910 userId = atoi(String8(args[3]));
911 }
912
913 uid_t uid;
914 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
915 return BAD_VALUE;
916 }
917
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000918 sp<UidPolicy> uidPolicy;
919 {
920 Mutex::Autolock _l(mLock);
921 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800922 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000923 if (uidPolicy) {
924 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
925 }
926 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800927}
928
929status_t AudioPolicyService::printHelp(int out) {
930 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700931 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
932 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
933 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800934 " help print this message\n");
935}
936
937// ----------- AudioPolicyService::UidPolicy implementation ----------
938
939void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700940 status_t res = mAm.linkToDeath(this);
941 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800942 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700943 | ActivityManager::UID_OBSERVER_ACTIVE
944 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800945 ActivityManager::PROCESS_STATE_UNKNOWN,
946 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700947 if (!res) {
948 Mutex::Autolock _l(mLock);
949 mObserverRegistered = true;
950 } else {
951 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800952
Steven Moreland2f348142019-07-02 15:59:07 -0700953 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700954 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800955}
956
957void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700958 mAm.unlinkToDeath(this);
959 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700960 Mutex::Autolock _l(mLock);
961 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800962}
963
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700964void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
965 Mutex::Autolock _l(mLock);
966 mCachedUids.clear();
967 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800968}
969
Eric Laurente8c8b432018-10-17 10:08:02 -0700970void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700971 bool needToReregister = false;
972 {
973 Mutex::Autolock _l(mLock);
974 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800975 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700976 if (needToReregister) {
977 // Looks like ActivityManager has died previously, attempt to re-register.
978 registerSelf();
979 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700980}
981
982bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
983 if (isServiceUid(uid)) return true;
984 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700985 {
986 Mutex::Autolock _l(mLock);
987 auto overrideIter = mOverrideUids.find(uid);
988 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700989 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700990 }
991 // In an absense of the ActivityManager, assume everything to be active.
992 if (!mObserverRegistered) return true;
993 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700994 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700995 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700996 }
997 }
998 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +0000999 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001000 {
1001 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001002 mCachedUids.insert(std::pair<uid_t,
1003 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1004 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001005 }
1006 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001007}
1008
Eric Laurente8c8b432018-10-17 10:08:02 -07001009int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1010 if (isServiceUid(uid)) {
1011 return ActivityManager::PROCESS_STATE_TOP;
1012 }
1013 checkRegistered();
1014 {
1015 Mutex::Autolock _l(mLock);
1016 auto overrideIter = mOverrideUids.find(uid);
1017 if (overrideIter != mOverrideUids.end()) {
1018 if (overrideIter->second.first) {
1019 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1020 return overrideIter->second.second;
1021 } else {
1022 auto cacheIter = mCachedUids.find(uid);
1023 if (cacheIter != mCachedUids.end()) {
1024 return cacheIter->second.second;
1025 }
1026 }
1027 }
1028 return ActivityManager::PROCESS_STATE_UNKNOWN;
1029 }
1030 // In an absense of the ActivityManager, assume everything to be active.
1031 if (!mObserverRegistered) {
1032 return ActivityManager::PROCESS_STATE_TOP;
1033 }
1034 auto cacheIter = mCachedUids.find(uid);
1035 if (cacheIter != mCachedUids.end()) {
1036 if (cacheIter->second.first) {
1037 return cacheIter->second.second;
1038 } else {
1039 return ActivityManager::PROCESS_STATE_UNKNOWN;
1040 }
1041 }
1042 }
1043 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001044 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001045 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1046 if (active) {
1047 state = am.getUidProcessState(uid, String16("audioserver"));
1048 }
1049 {
1050 Mutex::Autolock _l(mLock);
1051 mCachedUids.insert(std::pair<uid_t,
1052 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1053 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001054
Eric Laurente8c8b432018-10-17 10:08:02 -07001055 return state;
1056}
1057
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001058void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001059 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001060}
1061
1062void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001063 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001064}
1065
1066void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001067 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001068}
1069
Eric Laurente8c8b432018-10-17 10:08:02 -07001070void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1071 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001072 int64_t procStateSeq __unused,
1073 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001074 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1075 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001076 }
1077}
1078
1079void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001080 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1081}
1082
1083void AudioPolicyService::UidPolicy::notifyService() {
1084 sp<AudioPolicyService> service = mService.promote();
1085 if (service != nullptr) {
1086 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001087 }
1088}
1089
Eric Laurente8c8b432018-10-17 10:08:02 -07001090void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1091 std::pair<bool, int>> *uids,
1092 uid_t uid,
1093 bool active,
1094 int state,
1095 bool insert) {
1096 if (isServiceUid(uid)) {
1097 return;
1098 }
1099 bool wasActive = isUidActive(uid);
1100 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001101 {
1102 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001103 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001104 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001105 if (wasActive != isUidActive(uid) || state != previousState) {
1106 notifyService();
1107 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001108}
1109
Eric Laurente8c8b432018-10-17 10:08:02 -07001110void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1111 std::pair<bool, int>> *uids,
1112 uid_t uid,
1113 bool active,
1114 int state,
1115 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001116 auto it = uids->find(uid);
1117 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001118 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001119 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1120 it->second.first = active;
1121 }
1122 if (it->second.first) {
1123 it->second.second = state;
1124 } else {
1125 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1126 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001127 } else {
1128 uids->erase(it);
1129 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001130 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1131 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1132 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001133 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001134}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135
Eric Laurent4eb58f12018-12-07 16:41:02 -08001136bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1137 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001138 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001139 continue;
1140 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001141 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1142 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001143 return true;
1144 }
1145 }
1146 return false;
1147}
1148
Eric Laurentb78763e2018-10-17 10:08:02 -07001149bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1150{
1151 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1152 return it != mA11yUids.end();
1153}
1154
Michael Groovercfd28302018-12-11 19:16:46 -08001155// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1156void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1157 SensorPrivacyManager spm;
1158 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1159 spm.addSensorPrivacyListener(this);
1160}
1161
1162void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1163 SensorPrivacyManager spm;
1164 spm.removeSensorPrivacyListener(this);
1165}
1166
1167bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1168 return mSensorPrivacyEnabled;
1169}
1170
1171binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1172 mSensorPrivacyEnabled = enabled;
1173 sp<AudioPolicyService> service = mService.promote();
1174 if (service != nullptr) {
1175 service->updateUidStates();
1176 }
1177 return binder::Status::ok();
1178}
1179
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1181
Eric Laurentbfb1b832013-01-07 09:53:42 -08001182AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1183 const wp<AudioPolicyService>& service)
1184 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186}
1187
1188
1189AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1190{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001191 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192 release_wake_lock(mName.string());
1193 }
1194 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195}
1196
1197void AudioPolicyService::AudioCommandThread::onFirstRef()
1198{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001199 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200}
1201
1202bool AudioPolicyService::AudioCommandThread::threadLoop()
1203{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001204 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205
1206 mLock.lock();
1207 while (!exitPending())
1208 {
Eric Laurent59a89232014-06-08 14:14:17 -07001209 sp<AudioPolicyService> svc;
1210 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 nsecs_t curTime = systemTime();
1212 // commands are sorted by increasing time stamp: execute them from index 0 and up
1213 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001214 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001216 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217
1218 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001220 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001222 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001223 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001224 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1225 data->mVolume,
1226 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001227 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 }break;
1229 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001230 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001231 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1232 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001233 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001234 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001235 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001236 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001238 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001239 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001240 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001241 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001243 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001245 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001246 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001247 ALOGV("AudioCommandThread() processing stop output portId %d",
1248 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001249 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001250 if (svc == 0) {
1251 break;
1252 }
1253 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001254 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001255 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001256 }break;
1257 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001258 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001259 ALOGV("AudioCommandThread() processing release output portId %d",
1260 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001261 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001262 if (svc == 0) {
1263 break;
1264 }
1265 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001266 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001267 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001268 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001269 case CREATE_AUDIO_PATCH: {
1270 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1271 ALOGV("AudioCommandThread() processing create audio patch");
1272 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1273 if (af == 0) {
1274 command->mStatus = PERMISSION_DENIED;
1275 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001276 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001277 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001278 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001279 }
1280 } break;
1281 case RELEASE_AUDIO_PATCH: {
1282 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1283 ALOGV("AudioCommandThread() processing release audio patch");
1284 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1285 if (af == 0) {
1286 command->mStatus = PERMISSION_DENIED;
1287 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001288 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001289 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001290 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001291 }
1292 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001293 case UPDATE_AUDIOPORT_LIST: {
1294 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001295 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001296 if (svc == 0) {
1297 break;
1298 }
1299 mLock.unlock();
1300 svc->doOnAudioPortListUpdate();
1301 mLock.lock();
1302 }break;
1303 case UPDATE_AUDIOPATCH_LIST: {
1304 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001305 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001306 if (svc == 0) {
1307 break;
1308 }
1309 mLock.unlock();
1310 svc->doOnAudioPatchListUpdate();
1311 mLock.lock();
1312 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001313 case CHANGED_AUDIOVOLUMEGROUP: {
1314 AudioVolumeGroupData *data =
1315 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1316 ALOGV("AudioCommandThread() processing update audio volume group");
1317 svc = mService.promote();
1318 if (svc == 0) {
1319 break;
1320 }
1321 mLock.unlock();
1322 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1323 mLock.lock();
1324 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001325 case SET_AUDIOPORT_CONFIG: {
1326 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1327 ALOGV("AudioCommandThread() processing set port config");
1328 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1329 if (af == 0) {
1330 command->mStatus = PERMISSION_DENIED;
1331 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001332 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001333 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001334 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001335 }
1336 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001337 case DYN_POLICY_MIX_STATE_UPDATE: {
1338 DynPolicyMixStateUpdateData *data =
1339 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001340 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1341 data->mRegId.string(), data->mState);
1342 svc = mService.promote();
1343 if (svc == 0) {
1344 break;
1345 }
1346 mLock.unlock();
1347 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1348 mLock.lock();
1349 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001350 case RECORDING_CONFIGURATION_UPDATE: {
1351 RecordingConfigurationUpdateData *data =
1352 (RecordingConfigurationUpdateData *)command->mParam.get();
1353 ALOGV("AudioCommandThread() processing recording configuration update");
1354 svc = mService.promote();
1355 if (svc == 0) {
1356 break;
1357 }
1358 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001359 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001360 &data->mClientConfig, data->mClientEffects,
1361 &data->mDeviceConfig, data->mEffects,
1362 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001363 mLock.lock();
1364 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001365 case SET_EFFECT_SUSPENDED: {
1366 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1367 ALOGV("AudioCommandThread() processing set effect suspended");
1368 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1369 if (af != 0) {
1370 mLock.unlock();
1371 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1372 mLock.lock();
1373 }
1374 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001375 case AUDIO_MODULES_UPDATE: {
1376 ALOGV("AudioCommandThread() processing audio modules update");
1377 svc = mService.promote();
1378 if (svc == 0) {
1379 break;
1380 }
1381 mLock.unlock();
1382 svc->doOnNewAudioModulesAvailable();
1383 mLock.lock();
1384 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001385
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001387 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001389 {
1390 Mutex::Autolock _l(command->mLock);
1391 if (command->mWaitStatus) {
1392 command->mWaitStatus = false;
1393 command->mCond.signal();
1394 }
1395 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001396 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001397 // release mLock before releasing strong reference on the service as
1398 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1399 // acquires mLock.
1400 mLock.unlock();
1401 svc.clear();
1402 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 } else {
1404 waitTime = mAudioCommands[0]->mTime - curTime;
1405 break;
1406 }
1407 }
Zach Janga754b4f2015-10-27 01:29:34 +00001408
1409 // release delayed commands wake lock if the queue is empty
1410 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001411 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001412 }
1413
1414 // At this stage we have either an empty command queue or the first command in the queue
1415 // has a finite delay. So unless we are exiting it is safe to wait.
1416 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001417 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001418 if (waitTime == -1) {
1419 mWaitWorkCV.wait(mLock);
1420 } else {
1421 mWaitWorkCV.waitRelative(mLock, waitTime);
1422 }
Eric Laurent59a89232014-06-08 14:14:17 -07001423 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001425 // release delayed commands wake lock before quitting
1426 if (!mAudioCommands.isEmpty()) {
1427 release_wake_lock(mName.string());
1428 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429 mLock.unlock();
1430 return false;
1431}
1432
1433status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1434{
1435 const size_t SIZE = 256;
1436 char buffer[SIZE];
1437 String8 result;
1438
1439 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1440 result.append(buffer);
1441 write(fd, result.string(), result.size());
1442
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001443 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 if (!locked) {
1445 String8 result2(kCmdDeadlockedString);
1446 write(fd, result2.string(), result2.size());
1447 }
1448
1449 snprintf(buffer, SIZE, "- Commands:\n");
1450 result = String8(buffer);
1451 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001452 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 mAudioCommands[i]->dump(buffer, SIZE);
1454 result.append(buffer);
1455 }
1456 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001457 if (mLastCommand != 0) {
1458 mLastCommand->dump(buffer, SIZE);
1459 result.append(buffer);
1460 } else {
1461 result.append(" none\n");
1462 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001463
1464 write(fd, result.string(), result.size());
1465
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001466 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001467
1468 return NO_ERROR;
1469}
1470
Glenn Kastenfff6d712012-01-12 16:38:12 -08001471status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001472 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001473 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001474 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475{
Eric Laurent0ede8922014-05-09 18:04:42 -07001476 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001478 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479 data->mStream = stream;
1480 data->mVolume = volume;
1481 data->mIO = output;
1482 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001483 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001484 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001485 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001486 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487}
1488
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001489status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001490 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001491 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492{
Eric Laurent0ede8922014-05-09 18:04:42 -07001493 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001495 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001497 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001499 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001500 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001501 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001502 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503}
1504
1505status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1506{
Eric Laurent0ede8922014-05-09 18:04:42 -07001507 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001508 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001509 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510 data->mVolume = volume;
1511 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001512 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001513 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001514 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515}
1516
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001517void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1518 audio_session_t sessionId,
1519 bool suspended)
1520{
1521 sp<AudioCommand> command = new AudioCommand();
1522 command->mCommand = SET_EFFECT_SUSPENDED;
1523 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1524 data->mEffectId = effectId;
1525 data->mSessionId = sessionId;
1526 data->mSuspended = suspended;
1527 command->mParam = data;
1528 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1529 effectId, sessionId, suspended);
1530 sendCommand(command);
1531}
1532
1533
Eric Laurentd7fe0862018-07-14 16:48:01 -07001534void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001535{
Eric Laurent0ede8922014-05-09 18:04:42 -07001536 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001537 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001538 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001539 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001540 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001541 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001542 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001543}
1544
Eric Laurentd7fe0862018-07-14 16:48:01 -07001545void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001546{
Eric Laurent0ede8922014-05-09 18:04:42 -07001547 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001548 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001549 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001550 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001551 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001552 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001553 sendCommand(command);
1554}
1555
Eric Laurent951f4552014-05-20 10:48:17 -07001556status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1557 const struct audio_patch *patch,
1558 audio_patch_handle_t *handle,
1559 int delayMs)
1560{
1561 status_t status = NO_ERROR;
1562
1563 sp<AudioCommand> command = new AudioCommand();
1564 command->mCommand = CREATE_AUDIO_PATCH;
1565 CreateAudioPatchData *data = new CreateAudioPatchData();
1566 data->mPatch = *patch;
1567 data->mHandle = *handle;
1568 command->mParam = data;
1569 command->mWaitStatus = true;
1570 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1571 status = sendCommand(command, delayMs);
1572 if (status == NO_ERROR) {
1573 *handle = data->mHandle;
1574 }
1575 return status;
1576}
1577
1578status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1579 int delayMs)
1580{
1581 sp<AudioCommand> command = new AudioCommand();
1582 command->mCommand = RELEASE_AUDIO_PATCH;
1583 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1584 data->mHandle = handle;
1585 command->mParam = data;
1586 command->mWaitStatus = true;
1587 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1588 return sendCommand(command, delayMs);
1589}
1590
Eric Laurentb52c1522014-05-20 11:27:36 -07001591void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1592{
1593 sp<AudioCommand> command = new AudioCommand();
1594 command->mCommand = UPDATE_AUDIOPORT_LIST;
1595 ALOGV("AudioCommandThread() adding update audio port list");
1596 sendCommand(command);
1597}
1598
1599void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1600{
1601 sp<AudioCommand>command = new AudioCommand();
1602 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1603 ALOGV("AudioCommandThread() adding update audio patch list");
1604 sendCommand(command);
1605}
1606
François Gaffiecfe17322018-11-07 13:41:29 +01001607void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1608 int flags)
1609{
1610 sp<AudioCommand>command = new AudioCommand();
1611 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1612 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1613 data->mGroup = group;
1614 data->mFlags = flags;
1615 command->mParam = data;
1616 ALOGV("AudioCommandThread() adding audio volume group changed");
1617 sendCommand(command);
1618}
1619
Eric Laurente1715a42014-05-20 11:30:42 -07001620status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1621 const struct audio_port_config *config, int delayMs)
1622{
1623 sp<AudioCommand> command = new AudioCommand();
1624 command->mCommand = SET_AUDIOPORT_CONFIG;
1625 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1626 data->mConfig = *config;
1627 command->mParam = data;
1628 command->mWaitStatus = true;
1629 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1630 return sendCommand(command, delayMs);
1631}
1632
Jean-Michel Trivide801052015-04-14 19:10:14 -07001633void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001634 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001635{
1636 sp<AudioCommand> command = new AudioCommand();
1637 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1638 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1639 data->mRegId = regId;
1640 data->mState = state;
1641 command->mParam = data;
1642 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1643 regId.string(), state);
1644 sendCommand(command);
1645}
1646
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001647void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001648 int event,
1649 const record_client_info_t *clientInfo,
1650 const audio_config_base_t *clientConfig,
1651 std::vector<effect_descriptor_t> clientEffects,
1652 const audio_config_base_t *deviceConfig,
1653 std::vector<effect_descriptor_t> effects,
1654 audio_patch_handle_t patchHandle,
1655 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001656{
1657 sp<AudioCommand>command = new AudioCommand();
1658 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1659 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1660 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001661 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001662 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001663 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001664 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001665 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001666 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001667 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001668 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001669 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1670 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001671 sendCommand(command);
1672}
1673
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001674void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1675{
1676 sp<AudioCommand> command = new AudioCommand();
1677 command->mCommand = AUDIO_MODULES_UPDATE;
1678 sendCommand(command);
1679}
1680
Eric Laurent0ede8922014-05-09 18:04:42 -07001681status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1682{
1683 {
1684 Mutex::Autolock _l(mLock);
1685 insertCommand_l(command, delayMs);
1686 mWaitWorkCV.signal();
1687 }
1688 Mutex::Autolock _l(command->mLock);
1689 while (command->mWaitStatus) {
1690 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1691 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1692 command->mStatus = TIMED_OUT;
1693 command->mWaitStatus = false;
1694 }
1695 }
1696 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001697}
1698
Mathias Agopian65ab4712010-07-14 17:59:35 -07001699// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001700void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001702 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001703 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 command->mTime = systemTime() + milliseconds(delayMs);
1705
1706 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001707 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1709 }
1710
1711 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001712 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001713 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001714 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1715 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001716
1717 // create audio patch or release audio patch commands are equivalent
1718 // with regard to filtering
1719 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1720 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1721 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1722 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1723 continue;
1724 }
1725 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726
1727 switch (command->mCommand) {
1728 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001729 ParametersData *data = (ParametersData *)command->mParam.get();
1730 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001732 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001733 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001734 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1735 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1736 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001737 String8 key;
1738 String8 value;
1739 param.getAt(j, key, value);
1740 for (size_t k = 0; k < param2.size(); k++) {
1741 String8 key2;
1742 String8 value2;
1743 param2.getAt(k, key2, value2);
1744 if (key2 == key) {
1745 param2.remove(key2);
1746 ALOGV("Filtering out parameter %s", key2.string());
1747 break;
1748 }
1749 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 }
1751 // if all keys have been filtered out, remove the command.
1752 // otherwise, update the key value pairs
1753 if (param2.size() == 0) {
1754 removedCommands.add(command2);
1755 } else {
1756 data2->mKeyValuePairs = param2.toString();
1757 }
Eric Laurent21e54562013-09-23 12:08:05 -07001758 command->mTime = command2->mTime;
1759 // force delayMs to non 0 so that code below does not request to wait for
1760 // command status as the command is now delayed
1761 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762 } break;
1763
1764 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001765 VolumeData *data = (VolumeData *)command->mParam.get();
1766 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001767 if (data->mIO != data2->mIO) break;
1768 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001769 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001770 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001771 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001772 command->mTime = command2->mTime;
1773 // force delayMs to non 0 so that code below does not request to wait for
1774 // command status as the command is now delayed
1775 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001777
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001778 case SET_VOICE_VOLUME: {
1779 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1780 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1781 ALOGV("Filtering out voice volume command value %f replaced by %f",
1782 data2->mVolume, data->mVolume);
1783 removedCommands.add(command2);
1784 command->mTime = command2->mTime;
1785 // force delayMs to non 0 so that code below does not request to wait for
1786 // command status as the command is now delayed
1787 delayMs = 1;
1788 } break;
1789
Eric Laurente45b48a2014-09-04 16:40:57 -07001790 case CREATE_AUDIO_PATCH:
1791 case RELEASE_AUDIO_PATCH: {
1792 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001793 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001794 if (command->mCommand == CREATE_AUDIO_PATCH) {
1795 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001796 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001797 } else {
1798 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001799 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001800 }
1801 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001802 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001803 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1804 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001805 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001806 } else {
1807 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001808 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001809 }
1810 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001811 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1812 same output. */
1813 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1814 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1815 bool isOutputDiff = false;
1816 if (patch.num_sources == patch2.num_sources) {
1817 for (unsigned count = 0; count < patch.num_sources; count++) {
1818 if (patch.sources[count].id != patch2.sources[count].id) {
1819 isOutputDiff = true;
1820 break;
1821 }
1822 }
1823 if (isOutputDiff)
1824 break;
1825 }
1826 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001827 ALOGV("Filtering out %s audio patch command for handle %d",
1828 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1829 removedCommands.add(command2);
1830 command->mTime = command2->mTime;
1831 // force delayMs to non 0 so that code below does not request to wait for
1832 // command status as the command is now delayed
1833 delayMs = 1;
1834 } break;
1835
Jean-Michel Trivide801052015-04-14 19:10:14 -07001836 case DYN_POLICY_MIX_STATE_UPDATE: {
1837
1838 } break;
1839
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001840 case RECORDING_CONFIGURATION_UPDATE: {
1841
1842 } break;
1843
Mathias Agopian65ab4712010-07-14 17:59:35 -07001844 default:
1845 break;
1846 }
1847 }
1848
1849 // remove filtered commands
1850 for (size_t j = 0; j < removedCommands.size(); j++) {
1851 // removed commands always have time stamps greater than current command
1852 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001853 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001854 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855 mAudioCommands.removeAt(k);
1856 break;
1857 }
1858 }
1859 }
1860 removedCommands.clear();
1861
Eric Laurentaa79bef2015-01-15 14:33:51 -08001862 // Disable wait for status if delay is not 0.
1863 // Except for create audio patch command because the returned patch handle
1864 // is needed by audio policy manager
1865 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001866 command->mWaitStatus = false;
1867 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001868
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001870 ALOGV("inserting command: %d at index %zd, num commands %zu",
1871 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872 mAudioCommands.insertAt(command, i + 1);
1873}
1874
1875void AudioPolicyService::AudioCommandThread::exit()
1876{
Steve Block3856b092011-10-20 11:56:00 +01001877 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 {
1879 AutoMutex _l(mLock);
1880 requestExit();
1881 mWaitWorkCV.signal();
1882 }
Zach Janga754b4f2015-10-27 01:29:34 +00001883 // Note that we can call it from the thread loop if all other references have been released
1884 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 requestExitAndWait();
1886}
1887
1888void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1889{
1890 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1891 mCommand,
1892 (int)ns2s(mTime),
1893 (int)ns2ms(mTime)%1000,
1894 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001895 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896}
1897
Dima Zavinfce7a472011-04-19 22:30:36 -07001898/******* helpers for the service_ops callbacks defined below *********/
1899void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1900 const char *keyValuePairs,
1901 int delayMs)
1902{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001903 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001904 delayMs);
1905}
1906
1907int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1908 float volume,
1909 audio_io_handle_t output,
1910 int delayMs)
1911{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001912 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001913 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001914}
1915
Dima Zavinfce7a472011-04-19 22:30:36 -07001916int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1917{
1918 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1919}
1920
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001921void AudioPolicyService::setEffectSuspended(int effectId,
1922 audio_session_t sessionId,
1923 bool suspended)
1924{
1925 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1926}
1927
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001928void AudioPolicyService::onNewAudioModulesAvailable()
1929{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07001930 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001931}
1932
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001933
Dima Zavinfce7a472011-04-19 22:30:36 -07001934extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001935audio_module_handle_t aps_load_hw_module(void *service __unused,
1936 const char *name);
1937audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001938 audio_devices_t *pDevices,
1939 uint32_t *pSamplingRate,
1940 audio_format_t *pFormat,
1941 audio_channel_mask_t *pChannelMask,
1942 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001943 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001944
Eric Laurent2d388ec2014-03-07 13:25:54 -08001945audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001946 audio_module_handle_t module,
1947 audio_devices_t *pDevices,
1948 uint32_t *pSamplingRate,
1949 audio_format_t *pFormat,
1950 audio_channel_mask_t *pChannelMask,
1951 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001952 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001953 const audio_offload_info_t *offloadInfo);
1954audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001955 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001956 audio_io_handle_t output2);
1957int aps_close_output(void *service __unused, audio_io_handle_t output);
1958int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1959int aps_restore_output(void *service __unused, audio_io_handle_t output);
1960audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001961 audio_devices_t *pDevices,
1962 uint32_t *pSamplingRate,
1963 audio_format_t *pFormat,
1964 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001965 audio_in_acoustics_t acoustics __unused);
1966audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001967 audio_module_handle_t module,
1968 audio_devices_t *pDevices,
1969 uint32_t *pSamplingRate,
1970 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001971 audio_channel_mask_t *pChannelMask);
1972int aps_close_input(void *service __unused, audio_io_handle_t input);
1973int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001974int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001975 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001976 audio_io_handle_t dst_output);
1977char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1978 const char *keys);
1979void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1980 const char *kv_pairs, int delay_ms);
1981int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001982 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001983 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001984int aps_set_voice_volume(void *service, float volume, int delay_ms);
1985};
Dima Zavinfce7a472011-04-19 22:30:36 -07001986
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001987} // namespace android