blob: 73138932c9fa601ae0eedbc9e43c258b531cda88 [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>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053026#include <dlfcn.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070027
28#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
31#include <cutils/properties.h>
32#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080033#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080039#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070040#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080041#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080043#include <mediautils/TimeCheck.h>
Michael Groovercfd28302018-12-11 19:16:46 -080044#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
Dima Zavin64760242011-05-11 14:15:23 -070046#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070047#include <system/audio_policy.h>
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053048#include <AudioPolicyManager.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050namespace android {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080051using binder::Status;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten8dad0e32012-01-09 08:41:22 -080053static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
54static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053055static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so";
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
Mikhail Naganov959e2d02019-03-28 11:08:19 -070057static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
Eric Laurent0ede8922014-05-09 18:04:42 -070059static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010060
Svet Ganovf4ddfef2018-01-16 07:37:58 -080061static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063// ----------------------------------------------------------------------------
64
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053065static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
66{
67 AudioPolicyManager *apm = new AudioPolicyManager(clientInterface);
68 status_t status = apm->initialize();
69 if (status != NO_ERROR) {
70 delete apm;
71 apm = nullptr;
72 }
73 return apm;
74}
75
76static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
77{
78 delete interface;
79}
80// ----------------------------------------------------------------------------
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070083 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070084 mAudioPolicyManager(NULL),
85 mAudioPolicyClient(NULL),
86 mPhoneState(AUDIO_MODE_INVALID),
Jaideep Sharmaba9053b2021-01-25 21:24:26 +053087 mCaptureStateNotifier(false),
88 mCreateAudioPolicyManager(createAudioPolicyManager),
89 mDestroyAudioPolicyManager(destroyAudioPolicyManager) {
90}
91
92void AudioPolicyService::loadAudioPolicyManager()
93{
94 mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW);
95 if (mLibraryHandle != nullptr) {
96 ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath);
97 mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance>
98 (dlsym(mLibraryHandle, "createAudioPolicyManager"));
99 const char *lastError = dlerror();
100 ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s",
101 __func__, lastError != nullptr ? lastError : "no error");
102
103 mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>(
104 dlsym(mLibraryHandle, "destroyAudioPolicyManager"));
105 lastError = dlerror();
106 ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s",
107 __func__, lastError != nullptr ? lastError : "no error");
108 if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){
109 unloadAudioPolicyManager();
110 LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods");
111 }
112 }
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700113}
114
115void AudioPolicyService::onFirstRef()
116{
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 {
118 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -0800119
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700120 // start audio commands thread
121 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
122 // start output activity command thread
123 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -0700124
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700125 mAudioPolicyClient = new AudioPolicyClient(this);
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530126
127 loadAudioPolicyManager();
128 mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700129 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200130
bryant_liuba2b4392014-06-11 16:49:30 +0800131 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000132 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
133 sp<UidPolicy> uidPolicy = new UidPolicy(this);
134 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700135 {
136 Mutex::Autolock _l(mLock);
137 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000138 mUidPolicy = uidPolicy;
139 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700140 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000141 uidPolicy->registerSelf();
142 sensorPrivacyPolicy->registerSelf();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200143
144 AudioSystem::audioPolicyReady();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145}
146
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530147void AudioPolicyService::unloadAudioPolicyManager()
148{
149 ALOGV("%s ", __func__);
150 if (mLibraryHandle != nullptr) {
151 dlclose(mLibraryHandle);
152 }
153 mLibraryHandle = nullptr;
154 mCreateAudioPolicyManager = nullptr;
155 mDestroyAudioPolicyManager = nullptr;
156}
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158AudioPolicyService::~AudioPolicyService()
159{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700161 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700162
Jaideep Sharmaba9053b2021-01-25 21:24:26 +0530163 mDestroyAudioPolicyManager(mAudioPolicyManager);
164 unloadAudioPolicyManager();
165
Eric Laurentdce54a12014-03-10 12:19:46 -0700166 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700167
168 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800169 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800170
171 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800172 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000173
174 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800175 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700176}
177
178// A notification client is always registered by AudioSystem when the client process
179// connects to AudioPolicyService.
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800180Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700181{
Eric Laurent12590252015-08-21 18:40:20 -0700182 if (client == 0) {
183 ALOGW("%s got NULL client", __FUNCTION__);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800184 return Status::ok();
Eric Laurent12590252015-08-21 18:40:20 -0700185 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800186 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700187
188 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800189 pid_t pid = IPCThreadState::self()->getCallingPid();
190 int64_t token = ((int64_t)uid<<32) | pid;
191
192 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700193 sp<NotificationClient> notificationClient = new NotificationClient(this,
194 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800195 uid,
196 pid);
197 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700198
luochaojiang908c7d72018-06-21 14:58:04 +0800199 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700200
Marco Nelissenf8880202014-11-14 07:58:25 -0800201 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700202 binder->linkToDeath(notificationClient);
203 }
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800204 return Status::ok();
Eric Laurentb52c1522014-05-20 11:27:36 -0700205}
206
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800207Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
Eric Laurente8726fe2015-06-26 09:39:24 -0700208{
209 Mutex::Autolock _l(mNotificationClientsLock);
210
211 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800212 pid_t pid = IPCThreadState::self()->getCallingPid();
213 int64_t token = ((int64_t)uid<<32) | pid;
214
215 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800216 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700217 }
luochaojiang908c7d72018-06-21 14:58:04 +0800218 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800219 return Status::ok();
Eric Laurente8726fe2015-06-26 09:39:24 -0700220}
221
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800222Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
François Gaffiecfe17322018-11-07 13:41:29 +0100223{
224 Mutex::Autolock _l(mNotificationClientsLock);
225
226 uid_t uid = IPCThreadState::self()->getCallingUid();
227 pid_t pid = IPCThreadState::self()->getCallingPid();
228 int64_t token = ((int64_t)uid<<32) | pid;
229
230 if (mNotificationClients.indexOfKey(token) < 0) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800231 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100232 }
233 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800234 return Status::ok();
François Gaffiecfe17322018-11-07 13:41:29 +0100235}
236
Eric Laurentb52c1522014-05-20 11:27:36 -0700237// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800238void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700239{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000240 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800241 {
242 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800243 int64_t token = ((int64_t)uid<<32) | pid;
244 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800245 for (size_t i = 0; i < mNotificationClients.size(); i++) {
246 if (mNotificationClients.valueAt(i)->uid() == uid) {
247 hasSameUid = true;
248 break;
249 }
250 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000251 }
252 {
253 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800254 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700255 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700256 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700257 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800258 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700259}
260
261void AudioPolicyService::onAudioPortListUpdate()
262{
263 mOutputCommandThread->updateAudioPortListCommand();
264}
265
266void AudioPolicyService::doOnAudioPortListUpdate()
267{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800268 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 for (size_t i = 0; i < mNotificationClients.size(); i++) {
270 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
271 }
272}
273
274void AudioPolicyService::onAudioPatchListUpdate()
275{
276 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277}
278
Eric Laurentb52c1522014-05-20 11:27:36 -0700279void AudioPolicyService::doOnAudioPatchListUpdate()
280{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800281 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700282 for (size_t i = 0; i < mNotificationClients.size(); i++) {
283 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
284 }
285}
286
François Gaffiecfe17322018-11-07 13:41:29 +0100287void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
288{
289 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
290}
291
292void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
293{
294 Mutex::Autolock _l(mNotificationClientsLock);
295 for (size_t i = 0; i < mNotificationClients.size(); i++) {
296 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
297 }
298}
299
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700300void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700301{
302 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
303 regId.string(), state);
304 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
305}
306
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700307void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700308{
309 Mutex::Autolock _l(mNotificationClientsLock);
310 for (size_t i = 0; i < mNotificationClients.size(); i++) {
311 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
312 }
313}
314
Eric Laurenta9f86652018-11-28 17:23:11 -0800315void AudioPolicyService::onRecordingConfigurationUpdate(
316 int event,
317 const record_client_info_t *clientInfo,
318 const audio_config_base_t *clientConfig,
319 std::vector<effect_descriptor_t> clientEffects,
320 const audio_config_base_t *deviceConfig,
321 std::vector<effect_descriptor_t> effects,
322 audio_patch_handle_t patchHandle,
323 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800324{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800325 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800326 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800327}
328
Eric Laurenta9f86652018-11-28 17:23:11 -0800329void AudioPolicyService::doOnRecordingConfigurationUpdate(
330 int event,
331 const record_client_info_t *clientInfo,
332 const audio_config_base_t *clientConfig,
333 std::vector<effect_descriptor_t> clientEffects,
334 const audio_config_base_t *deviceConfig,
335 std::vector<effect_descriptor_t> effects,
336 audio_patch_handle_t patchHandle,
337 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800338{
339 Mutex::Autolock _l(mNotificationClientsLock);
340 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800341 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800342 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800343 }
344}
345
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700346void AudioPolicyService::onRoutingUpdated()
347{
348 mOutputCommandThread->routingChangedCommand();
349}
350
351void AudioPolicyService::doOnRoutingUpdated()
352{
353 Mutex::Autolock _l(mNotificationClientsLock);
354 for (size_t i = 0; i < mNotificationClients.size(); i++) {
355 mNotificationClients.valueAt(i)->onRoutingUpdated();
356 }
357}
358
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800359status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
360 audio_patch_handle_t *handle,
361 int delayMs)
362{
363 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
364}
365
366status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
367 int delayMs)
368{
369 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
370}
371
Eric Laurente1715a42014-05-20 11:30:42 -0700372status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
373 int delayMs)
374{
375 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
376}
377
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800378AudioPolicyService::NotificationClient::NotificationClient(
379 const sp<AudioPolicyService>& service,
380 const sp<media::IAudioPolicyServiceClient>& client,
381 uid_t uid,
382 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800383 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100384 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700385{
386}
387
388AudioPolicyService::NotificationClient::~NotificationClient()
389{
390}
391
392void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
393{
394 sp<NotificationClient> keep(this);
395 sp<AudioPolicyService> service = mService.promote();
396 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800397 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700398 }
399}
400
401void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
402{
Eric Laurente8726fe2015-06-26 09:39:24 -0700403 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700404 mAudioPolicyServiceClient->onAudioPortListUpdate();
405 }
406}
407
408void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
409{
Eric Laurente8726fe2015-06-26 09:39:24 -0700410 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700411 mAudioPolicyServiceClient->onAudioPatchListUpdate();
412 }
413}
Eric Laurent57dae992011-07-24 13:36:09 -0700414
François Gaffiecfe17322018-11-07 13:41:29 +0100415void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
416 int flags)
417{
418 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
419 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
420 }
421}
422
423
Jean-Michel Trivide801052015-04-14 19:10:14 -0700424void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700425 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700426{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700427 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800428 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
429 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800430 }
431}
432
433void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800434 int event,
435 const record_client_info_t *clientInfo,
436 const audio_config_base_t *clientConfig,
437 std::vector<effect_descriptor_t> clientEffects,
438 const audio_config_base_t *deviceConfig,
439 std::vector<effect_descriptor_t> effects,
440 audio_patch_handle_t patchHandle,
441 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800442{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700443 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800444 status_t status = [&]() -> status_t {
445 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
446 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
447 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
448 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700449 legacy2aidl_audio_config_base_t_AudioConfigBase(
450 *clientConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800451 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
452 convertContainer<std::vector<media::EffectDescriptor>>(
453 clientEffects,
454 legacy2aidl_effect_descriptor_t_EffectDescriptor));
455 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
Mikhail Naganovde3fa182021-07-30 15:06:42 -0700456 legacy2aidl_audio_config_base_t_AudioConfigBase(
457 *deviceConfig, true /*isInput*/));
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800458 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
459 convertContainer<std::vector<media::EffectDescriptor>>(
460 effects,
461 legacy2aidl_effect_descriptor_t_EffectDescriptor));
462 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
463 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
464 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
465 legacy2aidl_audio_source_t_AudioSourceType(source));
466 return aidl_utils::statusTFromBinderStatus(
467 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
468 clientInfoAidl,
469 clientConfigAidl,
470 clientEffectsAidl,
471 deviceConfigAidl,
472 effectsAidl,
473 patchHandleAidl,
474 sourceAidl));
475 }();
476 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700477 }
478}
479
Eric Laurente8726fe2015-06-26 09:39:24 -0700480void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
481{
482 mAudioPortCallbacksEnabled = enabled;
483}
484
François Gaffiecfe17322018-11-07 13:41:29 +0100485void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
486{
487 mAudioVolumeGroupCallbacksEnabled = enabled;
488}
Eric Laurente8726fe2015-06-26 09:39:24 -0700489
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700490void AudioPolicyService::NotificationClient::onRoutingUpdated()
491{
492 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
493 mAudioPolicyServiceClient->onRoutingUpdated();
494 }
495}
496
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700498 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700499 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500}
501
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000502static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000504 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
505}
506
507static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
508{
509 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510}
511
512status_t AudioPolicyService::dumpInternals(int fd)
513{
514 const size_t SIZE = 256;
515 char buffer[SIZE];
516 String8 result;
517
Eric Laurentdce54a12014-03-10 12:19:46 -0700518 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 result.append(buffer);
520 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
521 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522
Hayden Gomes524159d2019-12-23 14:41:47 -0800523 snprintf(buffer, SIZE, "Supported System Usages:\n");
524 result.append(buffer);
525 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
526 it != mSupportedSystemUsages.end(); ++it) {
527 snprintf(buffer, SIZE, "\t%d\n", *it);
528 result.append(buffer);
529 }
530
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 write(fd, result.string(), result.size());
532 return NO_ERROR;
533}
534
Eric Laurente8c8b432018-10-17 10:08:02 -0700535void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800536{
Eric Laurente8c8b432018-10-17 10:08:02 -0700537 Mutex::Autolock _l(mLock);
538 updateUidStates_l();
539}
540
541void AudioPolicyService::updateUidStates_l()
542{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800543// Go over all active clients and allow capture (does not force silence) in the
544// following cases:
Evan Severson1f700cd2021-02-10 13:10:37 -0800545// The client is the assistant
546// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700547// AND the source is VOICE_RECOGNITION or HOTWORD
Evan Severson1f700cd2021-02-10 13:10:37 -0800548// OR uses VOICE_RECOGNITION AND is on TOP
549// OR uses HOTWORD
550// AND there is no active privacy sensitive capture or call
551// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
552// OR The client is an accessibility service
553// AND Is on TOP
554// AND the source is VOICE_RECOGNITION or HOTWORD
555// OR The assistant is not on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700556// AND there is no active privacy sensitive capture or call
557// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Evan Severson1f700cd2021-02-10 13:10:37 -0800558// AND is on TOP
559// AND the source is VOICE_RECOGNITION or HOTWORD
560// OR the client source is virtual (remote submix, call audio TX or RX...)
561// OR the client source is HOTWORD
562// AND is on TOP
563// OR all active clients are using HOTWORD source
564// AND no call is active
565// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
566// OR the client is the current InputMethodService
567// AND a RTT call is active AND the source is VOICE_RECOGNITION
568// OR Any client
569// AND The assistant is not on TOP
570// AND is on TOP or latest started
571// AND there is no active privacy sensitive capture or call
572// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800573
Eric Laurent4e947da2019-10-17 15:24:06 -0700574
Eric Laurent4eb58f12018-12-07 16:41:02 -0800575 sp<AudioRecordClient> topActive;
576 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800577 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700578 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700579
Eric Laurenta46bedb2018-12-07 18:01:26 -0800580 nsecs_t topStartNs = 0;
581 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800582 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800583 nsecs_t latestSensitiveStartNs = 0;
584 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
585 bool isAssistantOnTop = false;
586 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700587 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800588 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
589 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700590 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700591 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700592 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800593
Michael Groovercfd28302018-12-11 19:16:46 -0800594 // if Sensor Privacy is enabled then all recordings should be silenced.
595 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
596 silenceAllRecordings_l();
597 return;
598 }
599
Eric Laurente8c8b432018-10-17 10:08:02 -0700600 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
601 sp<AudioRecordClient> current = mAudioRecordClients[i];
Svet Ganov33761132021-05-13 22:51:08 +0000602 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
603 current->attributionSource.uid));
Evan Severson1f700cd2021-02-10 13:10:37 -0800604 if (!current->active) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700605 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800606 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700607
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700608 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700609 // clients which app is in IDLE state are not eligible for top active or
610 // latest active
611 if (appState == APP_STATE_IDLE) {
612 continue;
613 }
614
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700615 bool isAccessibility = mUidPolicy->isA11yUid(currentUid);
Eric Laurent14a88632020-07-16 12:28:30 -0700616 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800617 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700618 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700619 bool isAssistant = mUidPolicy->isAssistantUid(currentUid);
Eric Laurentc21d5692020-02-25 10:24:36 -0800620 bool isPrivacySensitive =
621 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700622
Eric Laurentc21d5692020-02-25 10:24:36 -0800623 if (appState == APP_STATE_TOP) {
624 if (isPrivacySensitive) {
625 if (current->startTimeNs > topSensitiveStartNs) {
626 topSensitiveActive = current;
627 topSensitiveStartNs = current->startTimeNs;
628 }
629 } else {
630 if (current->startTimeNs > topStartNs) {
631 topActive = current;
632 topStartNs = current->startTimeNs;
633 }
634 }
635 if (isAssistant) {
636 isAssistantOnTop = true;
637 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800638 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800639 // Clients capturing for HOTWORD are not considered
640 // for latest active to avoid masking regular clients started before
641 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
642 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
643 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700644 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
645 // is marked latest sensitive active even if another app qualifies.
646 if (current->startTimeNs > latestSensitiveStartNs
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700647 || (isInCommunication && currentUid == mPhoneStateOwnerUid)) {
Eric Laurentb809a752020-06-29 09:53:13 -0700648 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700649 || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000650 latestSensitiveActiveOrComm->attributionSource.uid))
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700651 != mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700652 latestSensitiveActiveOrComm = current;
653 latestSensitiveStartNs = current->startTimeNs;
654 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800655 }
656 isSensitiveActive = true;
657 } else {
658 if (current->startTimeNs > latestStartNs) {
659 latestActive = current;
660 latestStartNs = current->startTimeNs;
661 }
662 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800663 }
664 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700665 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
666 onlyHotwordActive = false;
667 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700668 if (currentUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700669 isPhoneStateOwnerActive = true;
670 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800671 }
672
Eric Laurent1ff16a72019-03-14 18:35:04 -0700673 // if no active client with UI on Top, consider latest active as top
674 if (topActive == nullptr) {
675 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800676 topStartNs = latestStartNs;
677 }
678 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700679 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800680 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700681 } else if (latestSensitiveActiveOrComm != nullptr) {
682 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
683 // foreground UI in case both are capturing with privacy sensitive flag.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700684 uid_t latestActiveUid = VALUE_OR_FATAL(
Svet Ganov33761132021-05-13 22:51:08 +0000685 aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700686 if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) {
Eric Laurentb809a752020-06-29 09:53:13 -0700687 topSensitiveActive = latestSensitiveActiveOrComm;
688 topSensitiveStartNs = latestSensitiveStartNs;
689 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800690 }
691
692 // If both privacy sensitive and regular capture are active:
693 // if the regular capture is privileged
694 // allow concurrency
695 // else
696 // favor the privacy sensitive case
697 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700698 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800699 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800700 }
701
702 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
703 sp<AudioRecordClient> current = mAudioRecordClients[i];
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700704 uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000705 current->attributionSource.uid));
Eric Laurent1ff16a72019-03-14 18:35:04 -0700706 if (!current->active) {
707 continue;
708 }
709
Eric Laurent4eb58f12018-12-07 16:41:02 -0800710 audio_source_t source = current->attributes.source;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700711 bool isTopOrLatestActive = topActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000712 current->attributionSource.uid == topActive->attributionSource.uid;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700713 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false :
Svet Ganov33761132021-05-13 22:51:08 +0000714 current->attributionSource.uid == topSensitiveActive->attributionSource.uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800715
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000716 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700717 uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(
Svet Ganov33761132021-05-13 22:51:08 +0000718 recordClient->attributionSource.uid));
Ricardo Correa57a37692020-03-23 17:27:25 -0700719 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700720 bool canCaptureCommunication = recordClient->canCaptureOutput
721 || !isPhoneStateOwnerActive
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700722 || recordUid == mPhoneStateOwnerUid;
Eric Laurentb809a752020-06-29 09:53:13 -0700723 return !(isInCall && !canCaptureCall)
724 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800725 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700726
727 // By default allow capture if:
728 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700729 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700730 // AND there is no active privacy sensitive capture or call
731 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
732 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800733 && (isTopOrLatestActive || isTopOrLatestSensitive)
734 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700735 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800736 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800737
Eric Laurented726cc2021-07-01 14:26:41 +0200738 if (!current->hasOp()) {
739 // Never allow capture if app op is denied
740 allowCapture = false;
741 } else if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700742 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
743 allowCapture = true;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700744 } else if (mUidPolicy->isAssistantUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700745 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700746 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700747 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700748 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700749 // OR uses HOTWORD
750 // AND there is no active privacy sensitive capture or call
751 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700752 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800753 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700754 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800755 }
756 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700757 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800758 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700759 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800760 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700761 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800762 }
763 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700764 } else if (mUidPolicy->isA11yUid(currentUid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700765 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700766 // The assistant is not on TOP
767 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700768 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700769 // OR
770 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
771 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700772 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800773 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700774 allowCapture = true;
775 }
Eric Laurent589171c2019-07-25 18:04:29 -0700776 if (isA11yOnTop) {
777 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
778 allowCapture = true;
779 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800780 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700781 } else if (source == AUDIO_SOURCE_HOTWORD) {
782 // For HOTWORD source allow capture when not on TOP if:
783 // All active clients are using HOTWORD source
784 // AND no call is active
785 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800786 if (onlyHotwordActive
787 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700788 allowCapture = true;
789 }
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700790 } else if (mUidPolicy->isCurrentImeUid(currentUid)) {
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700791 // For current InputMethodService allow capture if:
792 // A RTT call is active AND the source is VOICE_RECOGNITION
793 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
794 allowCapture = true;
795 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800796 }
Eric Laurent8c7ef892021-06-10 13:32:16 +0200797 setAppState_l(current,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700798 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) :
Eric Laurent1ff16a72019-03-14 18:35:04 -0700799 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700800 }
801}
802
Michael Groovercfd28302018-12-11 19:16:46 -0800803void AudioPolicyService::silenceAllRecordings_l() {
804 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
805 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700806 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200807 setAppState_l(current, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700808 }
Michael Groovercfd28302018-12-11 19:16:46 -0800809 }
810}
811
Eric Laurente8c8b432018-10-17 10:08:02 -0700812/* static */
813app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700814
815 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700816 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700817 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
818 // include persistent services
819 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700820 }
821 return APP_STATE_FOREGROUND;
822}
823
Eric Laurent4eb58f12018-12-07 16:41:02 -0800824/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800825bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800826{
827 switch (source) {
828 case AUDIO_SOURCE_VOICE_UPLINK:
829 case AUDIO_SOURCE_VOICE_DOWNLINK:
830 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800831 case AUDIO_SOURCE_REMOTE_SUBMIX:
832 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700833 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800834 return true;
835 default:
836 break;
837 }
838 return false;
839}
840
Eric Laurented726cc2021-07-01 14:26:41 +0200841/* static */
842bool AudioPolicyService::isAppOpSource(audio_source_t source)
843{
844 switch (source) {
845 case AUDIO_SOURCE_FM_TUNER:
846 case AUDIO_SOURCE_ECHO_REFERENCE:
847 return false;
848 default:
849 break;
850 }
851 return true;
852}
853
Eric Laurent8c7ef892021-06-10 13:32:16 +0200854void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700855{
856 AutoCallerClear acc;
857
858 if (mAudioPolicyManager) {
Eric Laurent8c7ef892021-06-10 13:32:16 +0200859 mAudioPolicyManager->setAppState(client->portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700860 }
861 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
862 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700863 bool silenced = state == APP_STATE_IDLE;
Eric Laurent8c7ef892021-06-10 13:32:16 +0200864 if (client->silenced != silenced) {
865 if (client->active) {
866 if (silenced) {
867 finishRecording(client->attributionSource, client->attributes.source);
868 } else {
869 std::stringstream msg;
870 msg << "Audio recording un-silenced on session " << client->session;
871 if (!startRecording(client->attributionSource, String16(msg.str().c_str()),
872 client->attributes.source)) {
873 silenced = true;
874 }
875 }
876 }
877 af->setRecordSilenced(client->portId, silenced);
878 client->silenced = silenced;
879 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700880 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800881}
882
Glenn Kasten0f11b512014-01-31 16:18:54 -0800883status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884{
Glenn Kasten44deb052012-02-05 18:09:08 -0800885 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 dumpPermissionDenial(fd);
887 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000888 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 if (!locked) {
890 String8 result(kDeadlockedString);
891 write(fd, result.string(), result.size());
892 }
893
894 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800895 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 mAudioCommandThread->dump(fd);
897 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898
Eric Laurentdce54a12014-03-10 12:19:46 -0700899 if (mAudioPolicyManager) {
900 mAudioPolicyManager->dump(fd);
901 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902
Kevin Rocard8be94972019-02-22 13:26:25 -0800903 mPackageManager.dump(fd);
904
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000905 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 }
907 return NO_ERROR;
908}
909
910status_t AudioPolicyService::dumpPermissionDenial(int fd)
911{
912 const size_t SIZE = 256;
913 char buffer[SIZE];
914 String8 result;
915 snprintf(buffer, SIZE, "Permission Denial: "
916 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
917 IPCThreadState::self()->getCallingPid(),
918 IPCThreadState::self()->getCallingUid());
919 result.append(buffer);
920 write(fd, result.string(), result.size());
921 return NO_ERROR;
922}
923
924status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800925 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800926 // make sure transactions reserved to AudioFlinger do not come from other processes
927 switch (code) {
928 case TRANSACTION_startOutput:
929 case TRANSACTION_stopOutput:
930 case TRANSACTION_releaseOutput:
931 case TRANSACTION_getInputForAttr:
932 case TRANSACTION_startInput:
933 case TRANSACTION_stopInput:
934 case TRANSACTION_releaseInput:
935 case TRANSACTION_getOutputForEffect:
936 case TRANSACTION_registerEffect:
937 case TRANSACTION_unregisterEffect:
938 case TRANSACTION_setEffectEnabled:
939 case TRANSACTION_getStrategyForStream:
940 case TRANSACTION_getOutputForAttr:
941 case TRANSACTION_moveEffectsToIo:
942 ALOGW("%s: transaction %d received from PID %d",
943 __func__, code, IPCThreadState::self()->getCallingPid());
944 return INVALID_OPERATION;
945 default:
946 break;
947 }
948
949 // make sure the following transactions come from system components
950 switch (code) {
951 case TRANSACTION_setDeviceConnectionState:
952 case TRANSACTION_handleDeviceConfigChange:
953 case TRANSACTION_setPhoneState:
954//FIXME: Allow setForceUse calls from system apps until a better use case routing API is available
955// case TRANSACTION_setForceUse:
956 case TRANSACTION_initStreamVolume:
957 case TRANSACTION_setStreamVolumeIndex:
958 case TRANSACTION_setVolumeIndexForAttributes:
959 case TRANSACTION_getStreamVolumeIndex:
960 case TRANSACTION_getVolumeIndexForAttributes:
961 case TRANSACTION_getMinVolumeIndexForAttributes:
962 case TRANSACTION_getMaxVolumeIndexForAttributes:
963 case TRANSACTION_isStreamActive:
964 case TRANSACTION_isStreamActiveRemotely:
965 case TRANSACTION_isSourceActive:
966 case TRANSACTION_getDevicesForStream:
967 case TRANSACTION_registerPolicyMixes:
968 case TRANSACTION_setMasterMono:
969 case TRANSACTION_getSurroundFormats:
Kriti Dang6537def2021-03-02 13:46:59 +0100970 case TRANSACTION_getReportedSurroundFormats:
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800971 case TRANSACTION_setSurroundFormatEnabled:
972 case TRANSACTION_setAssistantUid:
973 case TRANSACTION_setA11yServicesUids:
974 case TRANSACTION_setUidDeviceAffinities:
975 case TRANSACTION_removeUidDeviceAffinities:
976 case TRANSACTION_setUserIdDeviceAffinities:
977 case TRANSACTION_removeUserIdDeviceAffinities:
978 case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP:
979 case TRANSACTION_listAudioVolumeGroups:
980 case TRANSACTION_getVolumeGroupFromAudioAttributes:
981 case TRANSACTION_acquireSoundTriggerSession:
982 case TRANSACTION_releaseSoundTriggerSession:
983 case TRANSACTION_setRttEnabled:
984 case TRANSACTION_isCallScreenModeSupported:
985 case TRANSACTION_setDevicesRoleForStrategy:
986 case TRANSACTION_setSupportedSystemUsages:
987 case TRANSACTION_removeDevicesRoleForStrategy:
988 case TRANSACTION_getDevicesForRoleAndStrategy:
989 case TRANSACTION_getDevicesForAttributes:
990 case TRANSACTION_setAllowedCapturePolicy:
991 case TRANSACTION_onNewAudioModulesAvailable:
992 case TRANSACTION_setCurrentImeUid:
993 case TRANSACTION_registerSoundTriggerCaptureStateListener:
994 case TRANSACTION_setDevicesRoleForCapturePreset:
995 case TRANSACTION_addDevicesRoleForCapturePreset:
996 case TRANSACTION_removeDevicesRoleForCapturePreset:
997 case TRANSACTION_clearDevicesRoleForCapturePreset:
998 case TRANSACTION_getDevicesForRoleAndCapturePreset: {
999 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1000 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1001 __func__, code, IPCThreadState::self()->getCallingPid(),
1002 IPCThreadState::self()->getCallingUid());
1003 return INVALID_OPERATION;
1004 }
1005 } break;
1006 default:
1007 break;
1008 }
1009
1010 std::string tag("IAudioPolicyService command " + std::to_string(code));
1011 TimeCheck check(tag.c_str());
1012
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001013 switch (code) {
1014 case SHELL_COMMAND_TRANSACTION: {
1015 int in = data.readFileDescriptor();
1016 int out = data.readFileDescriptor();
1017 int err = data.readFileDescriptor();
1018 int argc = data.readInt32();
1019 Vector<String16> args;
1020 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
1021 args.add(data.readString16());
1022 }
1023 sp<IBinder> unusedCallback;
1024 sp<IResultReceiver> resultReceiver;
1025 status_t status;
1026 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
1027 return status;
1028 }
1029 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
1030 return status;
1031 }
1032 status = shellCommand(in, out, err, args);
1033 if (resultReceiver != nullptr) {
1034 resultReceiver->send(status);
1035 }
1036 return NO_ERROR;
1037 }
1038 }
1039
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 return BnAudioPolicyService::onTransact(code, data, reply, flags);
1041}
1042
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001043// ------------------- Shell command implementation -------------------
1044
1045// NOTE: This is a remote API - make sure all args are validated
1046status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
1047 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
1048 return PERMISSION_DENIED;
1049 }
1050 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
1051 return BAD_VALUE;
1052 }
jovanakbe066e12019-09-02 11:54:39 -07001053 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001054 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001055 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001056 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -07001057 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001058 return handleGetUidState(args, out, err);
Eric Laurent269acb42021-04-23 16:53:22 +02001059 } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) {
1060 purgePermissionCache();
1061 return NO_ERROR;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001062 } else if (args.size() == 1 && args[0] == String16("help")) {
1063 printHelp(out);
1064 return NO_ERROR;
1065 }
1066 printHelp(err);
1067 return BAD_VALUE;
1068}
1069
jovanakbe066e12019-09-02 11:54:39 -07001070static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
1071 if (userId < 0) {
1072 ALOGE("Invalid user: %d", userId);
1073 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001074 return BAD_VALUE;
1075 }
jovanakbe066e12019-09-02 11:54:39 -07001076
1077 PermissionController pc;
1078 uid = pc.getPackageUid(packageName, 0);
1079 if (uid <= 0) {
1080 ALOGE("Unknown package: '%s'", String8(packageName).string());
1081 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
1082 return BAD_VALUE;
1083 }
1084
1085 uid = multiuser_get_uid(userId, uid);
1086 return NO_ERROR;
1087}
1088
1089status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
1090 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
1091 if (!(args.size() == 3 || args.size() == 5)) {
1092 printHelp(err);
1093 return BAD_VALUE;
1094 }
1095
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001096 bool active = false;
1097 if (args[2] == String16("active")) {
1098 active = true;
1099 } else if ((args[2] != String16("idle"))) {
1100 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
1101 return BAD_VALUE;
1102 }
jovanakbe066e12019-09-02 11:54:39 -07001103
1104 int userId = 0;
1105 if (args.size() >= 5 && args[3] == String16("--user")) {
1106 userId = atoi(String8(args[4]));
1107 }
1108
1109 uid_t uid;
1110 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1111 return BAD_VALUE;
1112 }
1113
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001114 sp<UidPolicy> uidPolicy;
1115 {
1116 Mutex::Autolock _l(mLock);
1117 uidPolicy = mUidPolicy;
1118 }
1119 if (uidPolicy) {
1120 uidPolicy->addOverrideUid(uid, active);
1121 return NO_ERROR;
1122 }
1123 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001124}
1125
1126status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001127 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1128 if (!(args.size() == 2 || args.size() == 4)) {
1129 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001130 return BAD_VALUE;
1131 }
jovanakbe066e12019-09-02 11:54:39 -07001132
1133 int userId = 0;
1134 if (args.size() >= 4 && args[2] == String16("--user")) {
1135 userId = atoi(String8(args[3]));
1136 }
1137
1138 uid_t uid;
1139 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1140 return BAD_VALUE;
1141 }
1142
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001143 sp<UidPolicy> uidPolicy;
1144 {
1145 Mutex::Autolock _l(mLock);
1146 uidPolicy = mUidPolicy;
1147 }
1148 if (uidPolicy) {
1149 uidPolicy->removeOverrideUid(uid);
1150 return NO_ERROR;
1151 }
1152 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001153}
1154
1155status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -07001156 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
1157 if (!(args.size() == 2 || args.size() == 4)) {
1158 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001159 return BAD_VALUE;
1160 }
jovanakbe066e12019-09-02 11:54:39 -07001161
1162 int userId = 0;
1163 if (args.size() >= 4 && args[2] == String16("--user")) {
1164 userId = atoi(String8(args[3]));
1165 }
1166
1167 uid_t uid;
1168 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
1169 return BAD_VALUE;
1170 }
1171
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001172 sp<UidPolicy> uidPolicy;
1173 {
1174 Mutex::Autolock _l(mLock);
1175 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001176 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001177 if (uidPolicy) {
1178 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
1179 }
1180 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001181}
1182
1183status_t AudioPolicyService::printHelp(int out) {
1184 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -07001185 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
1186 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1187 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001188 " help print this message\n");
1189}
1190
1191// ----------- AudioPolicyService::UidPolicy implementation ----------
1192
1193void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001194 status_t res = mAm.linkToDeath(this);
1195 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001196 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001197 | ActivityManager::UID_OBSERVER_ACTIVE
1198 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001199 ActivityManager::PROCESS_STATE_UNKNOWN,
1200 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001201 if (!res) {
1202 Mutex::Autolock _l(mLock);
1203 mObserverRegistered = true;
1204 } else {
1205 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001206
Steven Moreland2f348142019-07-02 15:59:07 -07001207 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001208 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001209}
1210
1211void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001212 mAm.unlinkToDeath(this);
1213 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001214 Mutex::Autolock _l(mLock);
1215 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001216}
1217
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001218void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1219 Mutex::Autolock _l(mLock);
1220 mCachedUids.clear();
1221 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001222}
1223
Eric Laurente8c8b432018-10-17 10:08:02 -07001224void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001225 bool needToReregister = false;
1226 {
1227 Mutex::Autolock _l(mLock);
1228 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001229 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001230 if (needToReregister) {
1231 // Looks like ActivityManager has died previously, attempt to re-register.
1232 registerSelf();
1233 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001234}
1235
1236bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1237 if (isServiceUid(uid)) return true;
1238 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001239 {
1240 Mutex::Autolock _l(mLock);
1241 auto overrideIter = mOverrideUids.find(uid);
1242 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001243 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001244 }
1245 // In an absense of the ActivityManager, assume everything to be active.
1246 if (!mObserverRegistered) return true;
1247 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001248 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001249 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001250 }
1251 }
1252 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001253 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001254 {
1255 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001256 mCachedUids.insert(std::pair<uid_t,
1257 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1258 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001259 }
1260 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001261}
1262
Eric Laurente8c8b432018-10-17 10:08:02 -07001263int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1264 if (isServiceUid(uid)) {
1265 return ActivityManager::PROCESS_STATE_TOP;
1266 }
1267 checkRegistered();
1268 {
1269 Mutex::Autolock _l(mLock);
1270 auto overrideIter = mOverrideUids.find(uid);
1271 if (overrideIter != mOverrideUids.end()) {
1272 if (overrideIter->second.first) {
1273 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1274 return overrideIter->second.second;
1275 } else {
1276 auto cacheIter = mCachedUids.find(uid);
1277 if (cacheIter != mCachedUids.end()) {
1278 return cacheIter->second.second;
1279 }
1280 }
1281 }
1282 return ActivityManager::PROCESS_STATE_UNKNOWN;
1283 }
1284 // In an absense of the ActivityManager, assume everything to be active.
1285 if (!mObserverRegistered) {
1286 return ActivityManager::PROCESS_STATE_TOP;
1287 }
1288 auto cacheIter = mCachedUids.find(uid);
1289 if (cacheIter != mCachedUids.end()) {
1290 if (cacheIter->second.first) {
1291 return cacheIter->second.second;
1292 } else {
1293 return ActivityManager::PROCESS_STATE_UNKNOWN;
1294 }
1295 }
1296 }
1297 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001298 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001299 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1300 if (active) {
1301 state = am.getUidProcessState(uid, String16("audioserver"));
1302 }
1303 {
1304 Mutex::Autolock _l(mLock);
1305 mCachedUids.insert(std::pair<uid_t,
1306 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1307 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001308
Eric Laurente8c8b432018-10-17 10:08:02 -07001309 return state;
1310}
1311
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001312void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001313 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001314}
1315
1316void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001317 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001318}
1319
1320void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001321 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001322}
1323
Eric Laurente8c8b432018-10-17 10:08:02 -07001324void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1325 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001326 int64_t procStateSeq __unused,
1327 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001328 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1329 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001330 }
1331}
1332
1333void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001334 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1335}
1336
1337void AudioPolicyService::UidPolicy::notifyService() {
1338 sp<AudioPolicyService> service = mService.promote();
1339 if (service != nullptr) {
1340 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001341 }
1342}
1343
Eric Laurente8c8b432018-10-17 10:08:02 -07001344void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1345 std::pair<bool, int>> *uids,
1346 uid_t uid,
1347 bool active,
1348 int state,
1349 bool insert) {
1350 if (isServiceUid(uid)) {
1351 return;
1352 }
1353 bool wasActive = isUidActive(uid);
1354 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001355 {
1356 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001357 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001358 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001359 if (wasActive != isUidActive(uid) || state != previousState) {
1360 notifyService();
1361 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001362}
1363
Eric Laurente8c8b432018-10-17 10:08:02 -07001364void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1365 std::pair<bool, int>> *uids,
1366 uid_t uid,
1367 bool active,
1368 int state,
1369 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001370 auto it = uids->find(uid);
1371 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001372 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001373 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1374 it->second.first = active;
1375 }
1376 if (it->second.first) {
1377 it->second.second = state;
1378 } else {
1379 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1380 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001381 } else {
1382 uids->erase(it);
1383 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001384 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1385 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1386 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001387 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001388}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001389
Eric Laurent4eb58f12018-12-07 16:41:02 -08001390bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1391 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001392 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001393 continue;
1394 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001395 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1396 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001397 return true;
1398 }
1399 }
1400 return false;
1401}
1402
Eric Laurentb78763e2018-10-17 10:08:02 -07001403bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1404{
1405 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1406 return it != mA11yUids.end();
1407}
1408
Michael Groovercfd28302018-12-11 19:16:46 -08001409// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1410void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1411 SensorPrivacyManager spm;
1412 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1413 spm.addSensorPrivacyListener(this);
1414}
1415
Evan Severson241d9592021-01-08 12:16:02 -08001416void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1417 SensorPrivacyManager spm;
1418 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1419 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1420 spm.addIndividualSensorPrivacyListener(userId,
1421 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1422}
1423
Michael Groovercfd28302018-12-11 19:16:46 -08001424void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1425 SensorPrivacyManager spm;
1426 spm.removeSensorPrivacyListener(this);
1427}
1428
1429bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1430 return mSensorPrivacyEnabled;
1431}
1432
1433binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1434 mSensorPrivacyEnabled = enabled;
1435 sp<AudioPolicyService> service = mService.promote();
1436 if (service != nullptr) {
1437 service->updateUidStates();
1438 }
1439 return binder::Status::ok();
1440}
1441
Eric Laurented726cc2021-07-01 14:26:41 +02001442// ----------- AudioPolicyService::OpRecordAudioMonitor implementation ----------
1443
1444// static
1445sp<AudioPolicyService::OpRecordAudioMonitor>
1446AudioPolicyService::OpRecordAudioMonitor::createIfNeeded(
1447 const AttributionSourceState& attributionSource, const audio_attributes_t& attr,
1448 wp<AudioCommandThread> commandThread)
1449{
Eric Laurent987ce102021-07-05 12:11:51 +02001450 if (isAudioServerOrRootUid(attributionSource.uid)) {
1451 ALOGV("not silencing record for audio or root source %s",
Eric Laurented726cc2021-07-01 14:26:41 +02001452 attributionSource.toString().c_str());
1453 return nullptr;
1454 }
1455
1456 if (!AudioPolicyService::isAppOpSource(attr.source)) {
1457 ALOGD("not monitoring app op for uid %d and source %d",
1458 attributionSource.uid, attr.source);
1459 return nullptr;
1460 }
1461
1462 if (!attributionSource.packageName.has_value()
1463 || attributionSource.packageName.value().size() == 0) {
1464 return nullptr;
1465 }
1466 return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread);
1467}
1468
1469AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor(
1470 const AttributionSourceState& attributionSource, int32_t appOp,
1471 wp<AudioCommandThread> commandThread) :
1472 mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp),
1473 mCommandThread(commandThread)
1474{
1475}
1476
1477AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor()
1478{
1479 if (mOpCallback != 0) {
1480 mAppOpsManager.stopWatchingMode(mOpCallback);
1481 }
1482 mOpCallback.clear();
1483}
1484
1485void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
1486{
1487 checkOp();
1488 mOpCallback = new RecordAudioOpCallback(this);
1489 ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
1490 // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
1491 // since it controls the mic permission for legacy apps.
1492 mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1493 mAttributionSource.packageName.value_or(""))),
1494 mOpCallback);
1495}
1496
1497bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const {
1498 return mHasOp.load();
1499}
1500
1501// Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor
1502// is updated in AppOp callback and in onFirstRef()
1503// Note this method is never called (and never to be) for audio server / root track
1504// due to the UID in createIfNeeded(). As a result for those record track, it's:
1505// - not called from constructor,
1506// - not called from RecordAudioOpCallback because the callback is not installed in this case
1507void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates)
1508{
1509 // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too
1510 // since it controls the mic permission for legacy apps.
1511 const int32_t mode = mAppOpsManager.checkOp(mAppOp,
1512 mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
1513 mAttributionSource.packageName.value_or(""))));
1514 const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED);
1515 // verbose logging only log when appOp changed
1516 ALOGI_IF(hasIt != mHasOp.load(),
1517 "App op %d missing, %ssilencing record %s",
1518 mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str());
1519 mHasOp.store(hasIt);
1520
1521 if (updateUidStates) {
1522 sp<AudioCommandThread> commandThread = mCommandThread.promote();
1523 if (commandThread != nullptr) {
1524 commandThread->updateUidStatesCommand();
1525 }
1526 }
1527}
1528
1529AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback(
1530 const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor)
1531{ }
1532
1533void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op,
1534 const String16& packageName __unused) {
1535 sp<OpRecordAudioMonitor> monitor = mMonitor.promote();
1536 if (monitor != NULL) {
1537 if (op != monitor->getOp()) {
1538 return;
1539 }
1540 monitor->checkOp(true);
1541 }
1542}
1543
1544
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1546
Eric Laurentbfb1b832013-01-07 09:53:42 -08001547AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1548 const wp<AudioPolicyService>& service)
1549 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001551}
1552
1553
1554AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1555{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001556 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 release_wake_lock(mName.string());
1558 }
1559 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560}
1561
1562void AudioPolicyService::AudioCommandThread::onFirstRef()
1563{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001564 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565}
1566
1567bool AudioPolicyService::AudioCommandThread::threadLoop()
1568{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001569 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570
1571 mLock.lock();
1572 while (!exitPending())
1573 {
Eric Laurent59a89232014-06-08 14:14:17 -07001574 sp<AudioPolicyService> svc;
1575 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576 nsecs_t curTime = systemTime();
1577 // commands are sorted by increasing time stamp: execute them from index 0 and up
1578 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001579 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001580 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001581 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001582
1583 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001584 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001585 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001586 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001587 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001588 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001589 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1590 data->mVolume,
1591 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001592 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593 }break;
1594 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001595 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001596 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1597 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001598 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001599 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001600 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001601 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001603 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001604 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001605 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001606 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001608 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001610 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001611 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001612 ALOGV("AudioCommandThread() processing stop output portId %d",
1613 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001614 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001615 if (svc == 0) {
1616 break;
1617 }
1618 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001619 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001620 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001621 }break;
1622 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001623 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001624 ALOGV("AudioCommandThread() processing release output portId %d",
1625 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001626 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001627 if (svc == 0) {
1628 break;
1629 }
1630 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001631 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001632 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001633 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001634 case CREATE_AUDIO_PATCH: {
1635 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1636 ALOGV("AudioCommandThread() processing create audio patch");
1637 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1638 if (af == 0) {
1639 command->mStatus = PERMISSION_DENIED;
1640 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001641 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001642 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001643 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001644 }
1645 } break;
1646 case RELEASE_AUDIO_PATCH: {
1647 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1648 ALOGV("AudioCommandThread() processing release audio patch");
1649 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1650 if (af == 0) {
1651 command->mStatus = PERMISSION_DENIED;
1652 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001653 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001654 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001655 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001656 }
1657 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001658 case UPDATE_AUDIOPORT_LIST: {
1659 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001660 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001661 if (svc == 0) {
1662 break;
1663 }
1664 mLock.unlock();
1665 svc->doOnAudioPortListUpdate();
1666 mLock.lock();
1667 }break;
1668 case UPDATE_AUDIOPATCH_LIST: {
1669 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001670 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001671 if (svc == 0) {
1672 break;
1673 }
1674 mLock.unlock();
1675 svc->doOnAudioPatchListUpdate();
1676 mLock.lock();
1677 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001678 case CHANGED_AUDIOVOLUMEGROUP: {
1679 AudioVolumeGroupData *data =
1680 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1681 ALOGV("AudioCommandThread() processing update audio volume group");
1682 svc = mService.promote();
1683 if (svc == 0) {
1684 break;
1685 }
1686 mLock.unlock();
1687 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1688 mLock.lock();
1689 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001690 case SET_AUDIOPORT_CONFIG: {
1691 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1692 ALOGV("AudioCommandThread() processing set port config");
1693 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1694 if (af == 0) {
1695 command->mStatus = PERMISSION_DENIED;
1696 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001697 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001698 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001699 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001700 }
1701 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001702 case DYN_POLICY_MIX_STATE_UPDATE: {
1703 DynPolicyMixStateUpdateData *data =
1704 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001705 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1706 data->mRegId.string(), data->mState);
1707 svc = mService.promote();
1708 if (svc == 0) {
1709 break;
1710 }
1711 mLock.unlock();
1712 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1713 mLock.lock();
1714 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001715 case RECORDING_CONFIGURATION_UPDATE: {
1716 RecordingConfigurationUpdateData *data =
1717 (RecordingConfigurationUpdateData *)command->mParam.get();
1718 ALOGV("AudioCommandThread() processing recording configuration update");
1719 svc = mService.promote();
1720 if (svc == 0) {
1721 break;
1722 }
1723 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001724 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001725 &data->mClientConfig, data->mClientEffects,
1726 &data->mDeviceConfig, data->mEffects,
1727 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001728 mLock.lock();
1729 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001730 case SET_EFFECT_SUSPENDED: {
1731 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1732 ALOGV("AudioCommandThread() processing set effect suspended");
1733 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1734 if (af != 0) {
1735 mLock.unlock();
1736 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1737 mLock.lock();
1738 }
1739 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001740 case AUDIO_MODULES_UPDATE: {
1741 ALOGV("AudioCommandThread() processing audio modules update");
1742 svc = mService.promote();
1743 if (svc == 0) {
1744 break;
1745 }
1746 mLock.unlock();
1747 svc->doOnNewAudioModulesAvailable();
1748 mLock.lock();
1749 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001750 case ROUTING_UPDATED: {
1751 ALOGV("AudioCommandThread() processing routing update");
1752 svc = mService.promote();
1753 if (svc == 0) {
1754 break;
1755 }
1756 mLock.unlock();
1757 svc->doOnRoutingUpdated();
1758 mLock.lock();
1759 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001760
Eric Laurented726cc2021-07-01 14:26:41 +02001761 case UPDATE_UID_STATES: {
1762 ALOGV("AudioCommandThread() processing updateUID states");
1763 svc = mService.promote();
1764 if (svc == 0) {
1765 break;
1766 }
1767 mLock.unlock();
1768 svc->updateUidStates();
1769 mLock.lock();
1770 } break;
1771
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001773 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001775 {
1776 Mutex::Autolock _l(command->mLock);
1777 if (command->mWaitStatus) {
1778 command->mWaitStatus = false;
1779 command->mCond.signal();
1780 }
1781 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001782 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001783 // release mLock before releasing strong reference on the service as
1784 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1785 // acquires mLock.
1786 mLock.unlock();
1787 svc.clear();
1788 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789 } else {
1790 waitTime = mAudioCommands[0]->mTime - curTime;
1791 break;
1792 }
1793 }
Zach Janga754b4f2015-10-27 01:29:34 +00001794
1795 // release delayed commands wake lock if the queue is empty
1796 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001797 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001798 }
1799
1800 // At this stage we have either an empty command queue or the first command in the queue
1801 // has a finite delay. So unless we are exiting it is safe to wait.
1802 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001803 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001804 if (waitTime == -1) {
1805 mWaitWorkCV.wait(mLock);
1806 } else {
1807 mWaitWorkCV.waitRelative(mLock, waitTime);
1808 }
Eric Laurent59a89232014-06-08 14:14:17 -07001809 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001810 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001811 // release delayed commands wake lock before quitting
1812 if (!mAudioCommands.isEmpty()) {
1813 release_wake_lock(mName.string());
1814 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815 mLock.unlock();
1816 return false;
1817}
1818
1819status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1820{
1821 const size_t SIZE = 256;
1822 char buffer[SIZE];
1823 String8 result;
1824
1825 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1826 result.append(buffer);
1827 write(fd, result.string(), result.size());
1828
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001829 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001830 if (!locked) {
1831 String8 result2(kCmdDeadlockedString);
1832 write(fd, result2.string(), result2.size());
1833 }
1834
1835 snprintf(buffer, SIZE, "- Commands:\n");
1836 result = String8(buffer);
1837 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001838 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839 mAudioCommands[i]->dump(buffer, SIZE);
1840 result.append(buffer);
1841 }
1842 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001843 if (mLastCommand != 0) {
1844 mLastCommand->dump(buffer, SIZE);
1845 result.append(buffer);
1846 } else {
1847 result.append(" none\n");
1848 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849
1850 write(fd, result.string(), result.size());
1851
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001852 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853
1854 return NO_ERROR;
1855}
1856
Glenn Kastenfff6d712012-01-12 16:38:12 -08001857status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001858 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001859 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001860 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861{
Eric Laurent0ede8922014-05-09 18:04:42 -07001862 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001864 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865 data->mStream = stream;
1866 data->mVolume = volume;
1867 data->mIO = output;
1868 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001869 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001871 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001872 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873}
1874
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001875status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001876 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001877 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878{
Eric Laurent0ede8922014-05-09 18:04:42 -07001879 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001881 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001883 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001885 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001886 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001887 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001888 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889}
1890
1891status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1892{
Eric Laurent0ede8922014-05-09 18:04:42 -07001893 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001895 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 data->mVolume = volume;
1897 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001898 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001899 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001900 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901}
1902
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001903void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1904 audio_session_t sessionId,
1905 bool suspended)
1906{
1907 sp<AudioCommand> command = new AudioCommand();
1908 command->mCommand = SET_EFFECT_SUSPENDED;
1909 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1910 data->mEffectId = effectId;
1911 data->mSessionId = sessionId;
1912 data->mSuspended = suspended;
1913 command->mParam = data;
1914 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1915 effectId, sessionId, suspended);
1916 sendCommand(command);
1917}
1918
1919
Eric Laurentd7fe0862018-07-14 16:48:01 -07001920void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001921{
Eric Laurent0ede8922014-05-09 18:04:42 -07001922 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001923 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001924 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001925 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001926 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001927 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001928 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001929}
1930
Eric Laurentd7fe0862018-07-14 16:48:01 -07001931void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001932{
Eric Laurent0ede8922014-05-09 18:04:42 -07001933 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001934 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001935 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001936 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001937 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001938 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001939 sendCommand(command);
1940}
1941
Eric Laurent951f4552014-05-20 10:48:17 -07001942status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1943 const struct audio_patch *patch,
1944 audio_patch_handle_t *handle,
1945 int delayMs)
1946{
1947 status_t status = NO_ERROR;
1948
1949 sp<AudioCommand> command = new AudioCommand();
1950 command->mCommand = CREATE_AUDIO_PATCH;
1951 CreateAudioPatchData *data = new CreateAudioPatchData();
1952 data->mPatch = *patch;
1953 data->mHandle = *handle;
1954 command->mParam = data;
1955 command->mWaitStatus = true;
1956 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1957 status = sendCommand(command, delayMs);
1958 if (status == NO_ERROR) {
1959 *handle = data->mHandle;
1960 }
1961 return status;
1962}
1963
1964status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1965 int delayMs)
1966{
1967 sp<AudioCommand> command = new AudioCommand();
1968 command->mCommand = RELEASE_AUDIO_PATCH;
1969 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1970 data->mHandle = handle;
1971 command->mParam = data;
1972 command->mWaitStatus = true;
1973 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1974 return sendCommand(command, delayMs);
1975}
1976
Eric Laurentb52c1522014-05-20 11:27:36 -07001977void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1978{
1979 sp<AudioCommand> command = new AudioCommand();
1980 command->mCommand = UPDATE_AUDIOPORT_LIST;
1981 ALOGV("AudioCommandThread() adding update audio port list");
1982 sendCommand(command);
1983}
1984
Eric Laurented726cc2021-07-01 14:26:41 +02001985void AudioPolicyService::AudioCommandThread::updateUidStatesCommand()
1986{
1987 sp<AudioCommand> command = new AudioCommand();
1988 command->mCommand = UPDATE_UID_STATES;
1989 ALOGV("AudioCommandThread() adding update UID states");
1990 sendCommand(command);
1991}
1992
Eric Laurentb52c1522014-05-20 11:27:36 -07001993void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1994{
1995 sp<AudioCommand>command = new AudioCommand();
1996 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1997 ALOGV("AudioCommandThread() adding update audio patch list");
1998 sendCommand(command);
1999}
2000
François Gaffiecfe17322018-11-07 13:41:29 +01002001void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
2002 int flags)
2003{
2004 sp<AudioCommand>command = new AudioCommand();
2005 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
2006 AudioVolumeGroupData *data= new AudioVolumeGroupData();
2007 data->mGroup = group;
2008 data->mFlags = flags;
2009 command->mParam = data;
2010 ALOGV("AudioCommandThread() adding audio volume group changed");
2011 sendCommand(command);
2012}
2013
Eric Laurente1715a42014-05-20 11:30:42 -07002014status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
2015 const struct audio_port_config *config, int delayMs)
2016{
2017 sp<AudioCommand> command = new AudioCommand();
2018 command->mCommand = SET_AUDIOPORT_CONFIG;
2019 SetAudioPortConfigData *data = new SetAudioPortConfigData();
2020 data->mConfig = *config;
2021 command->mParam = data;
2022 command->mWaitStatus = true;
2023 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
2024 return sendCommand(command, delayMs);
2025}
2026
Jean-Michel Trivide801052015-04-14 19:10:14 -07002027void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07002028 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07002029{
2030 sp<AudioCommand> command = new AudioCommand();
2031 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
2032 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
2033 data->mRegId = regId;
2034 data->mState = state;
2035 command->mParam = data;
2036 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
2037 regId.string(), state);
2038 sendCommand(command);
2039}
2040
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002041void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08002042 int event,
2043 const record_client_info_t *clientInfo,
2044 const audio_config_base_t *clientConfig,
2045 std::vector<effect_descriptor_t> clientEffects,
2046 const audio_config_base_t *deviceConfig,
2047 std::vector<effect_descriptor_t> effects,
2048 audio_patch_handle_t patchHandle,
2049 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002050{
2051 sp<AudioCommand>command = new AudioCommand();
2052 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
2053 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
2054 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002055 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002056 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002057 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08002058 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08002059 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08002060 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08002061 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002062 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08002063 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
2064 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002065 sendCommand(command);
2066}
2067
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002068void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
2069{
2070 sp<AudioCommand> command = new AudioCommand();
2071 command->mCommand = AUDIO_MODULES_UPDATE;
2072 sendCommand(command);
2073}
2074
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002075void AudioPolicyService::AudioCommandThread::routingChangedCommand()
2076{
2077 sp<AudioCommand>command = new AudioCommand();
2078 command->mCommand = ROUTING_UPDATED;
2079 ALOGV("AudioCommandThread() adding routing update");
2080 sendCommand(command);
2081}
2082
Eric Laurent0ede8922014-05-09 18:04:42 -07002083status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
2084{
2085 {
2086 Mutex::Autolock _l(mLock);
2087 insertCommand_l(command, delayMs);
2088 mWaitWorkCV.signal();
2089 }
2090 Mutex::Autolock _l(command->mLock);
2091 while (command->mWaitStatus) {
2092 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
2093 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
2094 command->mStatus = TIMED_OUT;
2095 command->mWaitStatus = false;
2096 }
2097 }
2098 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08002099}
2100
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07002102void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002104 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07002105 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106 command->mTime = systemTime() + milliseconds(delayMs);
2107
2108 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08002109 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
2111 }
2112
2113 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07002114 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002115 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07002116 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
2117 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002118
2119 // create audio patch or release audio patch commands are equivalent
2120 // with regard to filtering
2121 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
2122 (command->mCommand == RELEASE_AUDIO_PATCH)) {
2123 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
2124 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
2125 continue;
2126 }
2127 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002128
2129 switch (command->mCommand) {
2130 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002131 ParametersData *data = (ParametersData *)command->mParam.get();
2132 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002133 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01002134 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07002135 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136 AudioParameter param = AudioParameter(data->mKeyValuePairs);
2137 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
2138 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002139 String8 key;
2140 String8 value;
2141 param.getAt(j, key, value);
2142 for (size_t k = 0; k < param2.size(); k++) {
2143 String8 key2;
2144 String8 value2;
2145 param2.getAt(k, key2, value2);
2146 if (key2 == key) {
2147 param2.remove(key2);
2148 ALOGV("Filtering out parameter %s", key2.string());
2149 break;
2150 }
2151 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152 }
2153 // if all keys have been filtered out, remove the command.
2154 // otherwise, update the key value pairs
2155 if (param2.size() == 0) {
2156 removedCommands.add(command2);
2157 } else {
2158 data2->mKeyValuePairs = param2.toString();
2159 }
Eric Laurent21e54562013-09-23 12:08:05 -07002160 command->mTime = command2->mTime;
2161 // force delayMs to non 0 so that code below does not request to wait for
2162 // command status as the command is now delayed
2163 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 } break;
2165
2166 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07002167 VolumeData *data = (VolumeData *)command->mParam.get();
2168 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 if (data->mIO != data2->mIO) break;
2170 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01002171 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07002172 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07002174 command->mTime = command2->mTime;
2175 // force delayMs to non 0 so that code below does not request to wait for
2176 // command status as the command is now delayed
2177 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07002179
Eric Laurentbaf35fe2016-07-27 15:36:53 -07002180 case SET_VOICE_VOLUME: {
2181 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
2182 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
2183 ALOGV("Filtering out voice volume command value %f replaced by %f",
2184 data2->mVolume, data->mVolume);
2185 removedCommands.add(command2);
2186 command->mTime = command2->mTime;
2187 // force delayMs to non 0 so that code below does not request to wait for
2188 // command status as the command is now delayed
2189 delayMs = 1;
2190 } break;
2191
Eric Laurente45b48a2014-09-04 16:40:57 -07002192 case CREATE_AUDIO_PATCH:
2193 case RELEASE_AUDIO_PATCH: {
2194 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002195 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002196 if (command->mCommand == CREATE_AUDIO_PATCH) {
2197 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002198 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002199 } else {
2200 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07002201 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07002202 }
2203 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002204 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07002205 if (command2->mCommand == CREATE_AUDIO_PATCH) {
2206 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002207 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07002208 } else {
2209 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07002210 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07002211 }
2212 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07002213 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
2214 same output. */
2215 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
2216 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
2217 bool isOutputDiff = false;
2218 if (patch.num_sources == patch2.num_sources) {
2219 for (unsigned count = 0; count < patch.num_sources; count++) {
2220 if (patch.sources[count].id != patch2.sources[count].id) {
2221 isOutputDiff = true;
2222 break;
2223 }
2224 }
2225 if (isOutputDiff)
2226 break;
2227 }
2228 }
Eric Laurente45b48a2014-09-04 16:40:57 -07002229 ALOGV("Filtering out %s audio patch command for handle %d",
2230 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
2231 removedCommands.add(command2);
2232 command->mTime = command2->mTime;
2233 // force delayMs to non 0 so that code below does not request to wait for
2234 // command status as the command is now delayed
2235 delayMs = 1;
2236 } break;
2237
Jean-Michel Trivide801052015-04-14 19:10:14 -07002238 case DYN_POLICY_MIX_STATE_UPDATE: {
2239
2240 } break;
2241
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08002242 case RECORDING_CONFIGURATION_UPDATE: {
2243
2244 } break;
2245
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07002246 case ROUTING_UPDATED: {
2247
2248 } break;
2249
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 default:
2251 break;
2252 }
2253 }
2254
2255 // remove filtered commands
2256 for (size_t j = 0; j < removedCommands.size(); j++) {
2257 // removed commands always have time stamps greater than current command
2258 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07002259 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01002260 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 mAudioCommands.removeAt(k);
2262 break;
2263 }
2264 }
2265 }
2266 removedCommands.clear();
2267
Eric Laurentaa79bef2015-01-15 14:33:51 -08002268 // Disable wait for status if delay is not 0.
2269 // Except for create audio patch command because the returned patch handle
2270 // is needed by audio policy manager
2271 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07002272 command->mWaitStatus = false;
2273 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07002274
Mathias Agopian65ab4712010-07-14 17:59:35 -07002275 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07002276 ALOGV("inserting command: %d at index %zd, num commands %zu",
2277 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002278 mAudioCommands.insertAt(command, i + 1);
2279}
2280
2281void AudioPolicyService::AudioCommandThread::exit()
2282{
Steve Block3856b092011-10-20 11:56:00 +01002283 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002284 {
2285 AutoMutex _l(mLock);
2286 requestExit();
2287 mWaitWorkCV.signal();
2288 }
Zach Janga754b4f2015-10-27 01:29:34 +00002289 // Note that we can call it from the thread loop if all other references have been released
2290 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07002291 requestExitAndWait();
2292}
2293
2294void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
2295{
2296 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
2297 mCommand,
2298 (int)ns2s(mTime),
2299 (int)ns2ms(mTime)%1000,
2300 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07002301 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002302}
2303
Dima Zavinfce7a472011-04-19 22:30:36 -07002304/******* helpers for the service_ops callbacks defined below *********/
2305void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
2306 const char *keyValuePairs,
2307 int delayMs)
2308{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002309 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002310 delayMs);
2311}
2312
2313int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2314 float volume,
2315 audio_io_handle_t output,
2316 int delayMs)
2317{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002318 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002319 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002320}
2321
Dima Zavinfce7a472011-04-19 22:30:36 -07002322int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2323{
2324 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2325}
2326
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002327void AudioPolicyService::setEffectSuspended(int effectId,
2328 audio_session_t sessionId,
2329 bool suspended)
2330{
2331 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2332}
2333
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002334Status AudioPolicyService::onNewAudioModulesAvailable()
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002335{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002336 mOutputCommandThread->audioModulesUpdateCommand();
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002337 return Status::ok();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002338}
2339
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002340
Dima Zavinfce7a472011-04-19 22:30:36 -07002341extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002342audio_module_handle_t aps_load_hw_module(void *service __unused,
2343 const char *name);
2344audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002345 audio_devices_t *pDevices,
2346 uint32_t *pSamplingRate,
2347 audio_format_t *pFormat,
2348 audio_channel_mask_t *pChannelMask,
2349 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002350 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002351
Eric Laurent2d388ec2014-03-07 13:25:54 -08002352audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002353 audio_module_handle_t module,
2354 audio_devices_t *pDevices,
2355 uint32_t *pSamplingRate,
2356 audio_format_t *pFormat,
2357 audio_channel_mask_t *pChannelMask,
2358 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002359 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002360 const audio_offload_info_t *offloadInfo);
2361audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002362 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002363 audio_io_handle_t output2);
2364int aps_close_output(void *service __unused, audio_io_handle_t output);
2365int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2366int aps_restore_output(void *service __unused, audio_io_handle_t output);
2367audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002368 audio_devices_t *pDevices,
2369 uint32_t *pSamplingRate,
2370 audio_format_t *pFormat,
2371 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002372 audio_in_acoustics_t acoustics __unused);
2373audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002374 audio_module_handle_t module,
2375 audio_devices_t *pDevices,
2376 uint32_t *pSamplingRate,
2377 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002378 audio_channel_mask_t *pChannelMask);
2379int aps_close_input(void *service __unused, audio_io_handle_t input);
2380int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002381int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002382 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002383 audio_io_handle_t dst_output);
2384char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2385 const char *keys);
2386void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2387 const char *kv_pairs, int delay_ms);
2388int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002389 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002390 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002391int aps_set_voice_volume(void *service, float volume, int delay_ms);
2392};
Dima Zavinfce7a472011-04-19 22:30:36 -07002393
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002394} // namespace android