blob: 80022c8ba8e659b4d9414d36457ae7864a840fe3 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -080038#include <media/AidlConversion.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070041#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080042#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
Dima Zavin64760242011-05-11 14:15:23 -070044#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070045#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070046
Mathias Agopian65ab4712010-07-14 17:59:35 -070047namespace android {
48
Glenn Kasten8dad0e32012-01-09 08:41:22 -080049static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
50static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Mikhail Naganov959e2d02019-03-28 11:08:19 -070052static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Svet Ganovf4ddfef2018-01-16 07:37:58 -080056static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070057
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070061 : BnAudioPolicyService(),
Ytai Ben-Tsvi85093d52020-03-26 09:41:15 -070062 mAudioPolicyManager(NULL),
63 mAudioPolicyClient(NULL),
64 mPhoneState(AUDIO_MODE_INVALID),
65 mCaptureStateNotifier(false) {
Eric Laurentf5ada6e2014-10-09 17:49:00 -070066}
67
68void AudioPolicyService::onFirstRef()
69{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 {
71 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080072
Eric Laurent8b1e80b2014-10-07 09:08:47 -070073 // start audio commands thread
74 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
75 // start output activity command thread
76 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070077
Eric Laurent8b1e80b2014-10-07 09:08:47 -070078 mAudioPolicyClient = new AudioPolicyClient(this);
79 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 }
bryant_liuba2b4392014-06-11 16:49:30 +080081 // load audio processing modules
Mikhail Naganov12b716c2020-04-30 22:37:43 +000082 sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects();
83 sp<UidPolicy> uidPolicy = new UidPolicy(this);
84 sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 {
86 Mutex::Autolock _l(mLock);
87 mAudioPolicyEffects = audioPolicyEffects;
Mikhail Naganov12b716c2020-04-30 22:37:43 +000088 mUidPolicy = uidPolicy;
89 mSensorPrivacyPolicy = sensorPrivacyPolicy;
Eric Laurent8b1e80b2014-10-07 09:08:47 -070090 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +000091 uidPolicy->registerSelf();
92 sensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070093}
94
95AudioPolicyService::~AudioPolicyService()
96{
Mathias Agopian65ab4712010-07-14 17:59:35 -070097 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070098 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099
Eric Laurentf269b8e2014-06-09 20:01:29 -0700100 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700101 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700102
103 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800104 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800105
106 mUidPolicy->unregisterSelf();
Michael Groovercfd28302018-12-11 19:16:46 -0800107 mSensorPrivacyPolicy->unregisterSelf();
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000108
109 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800110 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700111}
112
113// A notification client is always registered by AudioSystem when the client process
114// connects to AudioPolicyService.
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800115void AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client)
Eric Laurentb52c1522014-05-20 11:27:36 -0700116{
Eric Laurent12590252015-08-21 18:40:20 -0700117 if (client == 0) {
118 ALOGW("%s got NULL client", __FUNCTION__);
119 return;
120 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800121 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700122
123 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800124 pid_t pid = IPCThreadState::self()->getCallingPid();
125 int64_t token = ((int64_t)uid<<32) | pid;
126
127 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700128 sp<NotificationClient> notificationClient = new NotificationClient(this,
129 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800130 uid,
131 pid);
132 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
luochaojiang908c7d72018-06-21 14:58:04 +0800134 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700135
Marco Nelissenf8880202014-11-14 07:58:25 -0800136 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700137 binder->linkToDeath(notificationClient);
138 }
139}
140
Eric Laurente8726fe2015-06-26 09:39:24 -0700141void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
142{
143 Mutex::Autolock _l(mNotificationClientsLock);
144
145 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800146 pid_t pid = IPCThreadState::self()->getCallingPid();
147 int64_t token = ((int64_t)uid<<32) | pid;
148
149 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700150 return;
151 }
luochaojiang908c7d72018-06-21 14:58:04 +0800152 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700153}
154
François Gaffiecfe17322018-11-07 13:41:29 +0100155void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
156{
157 Mutex::Autolock _l(mNotificationClientsLock);
158
159 uid_t uid = IPCThreadState::self()->getCallingUid();
160 pid_t pid = IPCThreadState::self()->getCallingPid();
161 int64_t token = ((int64_t)uid<<32) | pid;
162
163 if (mNotificationClients.indexOfKey(token) < 0) {
164 return;
165 }
166 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
167}
168
Eric Laurentb52c1522014-05-20 11:27:36 -0700169// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800170void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700171{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000172 bool hasSameUid = false;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800173 {
174 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800175 int64_t token = ((int64_t)uid<<32) | pid;
176 mNotificationClients.removeItem(token);
luochaojiang908c7d72018-06-21 14:58:04 +0800177 for (size_t i = 0; i < mNotificationClients.size(); i++) {
178 if (mNotificationClients.valueAt(i)->uid() == uid) {
179 hasSameUid = true;
180 break;
181 }
182 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000183 }
184 {
185 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800186 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700187 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700188 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700189 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800190 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700191}
192
193void AudioPolicyService::onAudioPortListUpdate()
194{
195 mOutputCommandThread->updateAudioPortListCommand();
196}
197
198void AudioPolicyService::doOnAudioPortListUpdate()
199{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800200 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700201 for (size_t i = 0; i < mNotificationClients.size(); i++) {
202 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
203 }
204}
205
206void AudioPolicyService::onAudioPatchListUpdate()
207{
208 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209}
210
Eric Laurentb52c1522014-05-20 11:27:36 -0700211void AudioPolicyService::doOnAudioPatchListUpdate()
212{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800213 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700214 for (size_t i = 0; i < mNotificationClients.size(); i++) {
215 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
216 }
217}
218
François Gaffiecfe17322018-11-07 13:41:29 +0100219void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
220{
221 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
222}
223
224void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
225{
226 Mutex::Autolock _l(mNotificationClientsLock);
227 for (size_t i = 0; i < mNotificationClients.size(); i++) {
228 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
229 }
230}
231
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700232void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700233{
234 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
235 regId.string(), state);
236 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
237}
238
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700239void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700240{
241 Mutex::Autolock _l(mNotificationClientsLock);
242 for (size_t i = 0; i < mNotificationClients.size(); i++) {
243 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
244 }
245}
246
Eric Laurenta9f86652018-11-28 17:23:11 -0800247void AudioPolicyService::onRecordingConfigurationUpdate(
248 int event,
249 const record_client_info_t *clientInfo,
250 const audio_config_base_t *clientConfig,
251 std::vector<effect_descriptor_t> clientEffects,
252 const audio_config_base_t *deviceConfig,
253 std::vector<effect_descriptor_t> effects,
254 audio_patch_handle_t patchHandle,
255 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800256{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800257 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800258 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800259}
260
Eric Laurenta9f86652018-11-28 17:23:11 -0800261void AudioPolicyService::doOnRecordingConfigurationUpdate(
262 int event,
263 const record_client_info_t *clientInfo,
264 const audio_config_base_t *clientConfig,
265 std::vector<effect_descriptor_t> clientEffects,
266 const audio_config_base_t *deviceConfig,
267 std::vector<effect_descriptor_t> effects,
268 audio_patch_handle_t patchHandle,
269 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800270{
271 Mutex::Autolock _l(mNotificationClientsLock);
272 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800273 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800274 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800275 }
276}
277
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700278void AudioPolicyService::onRoutingUpdated()
279{
280 mOutputCommandThread->routingChangedCommand();
281}
282
283void AudioPolicyService::doOnRoutingUpdated()
284{
285 Mutex::Autolock _l(mNotificationClientsLock);
286 for (size_t i = 0; i < mNotificationClients.size(); i++) {
287 mNotificationClients.valueAt(i)->onRoutingUpdated();
288 }
289}
290
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800291status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
292 audio_patch_handle_t *handle,
293 int delayMs)
294{
295 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
296}
297
298status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
299 int delayMs)
300{
301 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
302}
303
Eric Laurente1715a42014-05-20 11:30:42 -0700304status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
305 int delayMs)
306{
307 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
308}
309
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800310AudioPolicyService::NotificationClient::NotificationClient(
311 const sp<AudioPolicyService>& service,
312 const sp<media::IAudioPolicyServiceClient>& client,
313 uid_t uid,
314 pid_t pid)
luochaojiang908c7d72018-06-21 14:58:04 +0800315 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100316 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700317{
318}
319
320AudioPolicyService::NotificationClient::~NotificationClient()
321{
322}
323
324void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
325{
326 sp<NotificationClient> keep(this);
327 sp<AudioPolicyService> service = mService.promote();
328 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800329 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700330 }
331}
332
333void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
334{
Eric Laurente8726fe2015-06-26 09:39:24 -0700335 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700336 mAudioPolicyServiceClient->onAudioPortListUpdate();
337 }
338}
339
340void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
341{
Eric Laurente8726fe2015-06-26 09:39:24 -0700342 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700343 mAudioPolicyServiceClient->onAudioPatchListUpdate();
344 }
345}
Eric Laurent57dae992011-07-24 13:36:09 -0700346
François Gaffiecfe17322018-11-07 13:41:29 +0100347void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
348 int flags)
349{
350 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
351 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
352 }
353}
354
355
Jean-Michel Trivide801052015-04-14 19:10:14 -0700356void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700357 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700358{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700359 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800360 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(
361 legacy2aidl_String8_string(regId).value(), state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800362 }
363}
364
365void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800366 int event,
367 const record_client_info_t *clientInfo,
368 const audio_config_base_t *clientConfig,
369 std::vector<effect_descriptor_t> clientEffects,
370 const audio_config_base_t *deviceConfig,
371 std::vector<effect_descriptor_t> effects,
372 audio_patch_handle_t patchHandle,
373 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800374{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700375 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Ytai Ben-Tsvi7e7a79d2020-12-15 16:48:16 -0800376 status_t status = [&]() -> status_t {
377 int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event));
378 media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS(
379 legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo));
380 media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS(
381 legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig));
382 std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS(
383 convertContainer<std::vector<media::EffectDescriptor>>(
384 clientEffects,
385 legacy2aidl_effect_descriptor_t_EffectDescriptor));
386 media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS(
387 legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig));
388 std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS(
389 convertContainer<std::vector<media::EffectDescriptor>>(
390 effects,
391 legacy2aidl_effect_descriptor_t_EffectDescriptor));
392 int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS(
393 legacy2aidl_audio_patch_handle_t_int32_t(patchHandle));
394 media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS(
395 legacy2aidl_audio_source_t_AudioSourceType(source));
396 return aidl_utils::statusTFromBinderStatus(
397 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl,
398 clientInfoAidl,
399 clientConfigAidl,
400 clientEffectsAidl,
401 deviceConfigAidl,
402 effectsAidl,
403 patchHandleAidl,
404 sourceAidl));
405 }();
406 ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700407 }
408}
409
Eric Laurente8726fe2015-06-26 09:39:24 -0700410void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
411{
412 mAudioPortCallbacksEnabled = enabled;
413}
414
François Gaffiecfe17322018-11-07 13:41:29 +0100415void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
416{
417 mAudioVolumeGroupCallbacksEnabled = enabled;
418}
Eric Laurente8726fe2015-06-26 09:39:24 -0700419
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700420void AudioPolicyService::NotificationClient::onRoutingUpdated()
421{
422 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
423 mAudioPolicyServiceClient->onRoutingUpdated();
424 }
425}
426
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700428 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700429 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430}
431
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000432static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433{
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000434 return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR;
435}
436
437static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
438{
439 if (locked) mutex.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440}
441
442status_t AudioPolicyService::dumpInternals(int fd)
443{
444 const size_t SIZE = 256;
445 char buffer[SIZE];
446 String8 result;
447
Eric Laurentdce54a12014-03-10 12:19:46 -0700448 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 result.append(buffer);
450 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
451 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452
Hayden Gomes524159d2019-12-23 14:41:47 -0800453 snprintf(buffer, SIZE, "Supported System Usages:\n");
454 result.append(buffer);
455 for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin();
456 it != mSupportedSystemUsages.end(); ++it) {
457 snprintf(buffer, SIZE, "\t%d\n", *it);
458 result.append(buffer);
459 }
460
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 write(fd, result.string(), result.size());
462 return NO_ERROR;
463}
464
Eric Laurente8c8b432018-10-17 10:08:02 -0700465void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800466{
Eric Laurente8c8b432018-10-17 10:08:02 -0700467 Mutex::Autolock _l(mLock);
468 updateUidStates_l();
469}
470
471void AudioPolicyService::updateUidStates_l()
472{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800473// Go over all active clients and allow capture (does not force silence) in the
474// following cases:
Eric Laurent8ed73c12019-08-30 17:25:19 -0700475// The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700476// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700477// AND the source is VOICE_RECOGNITION or HOTWORD
478// OR uses VOICE_RECOGNITION AND is on TOP
479// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700480// AND there is no active privacy sensitive capture or call
481// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800482// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700483// AND Is on TOP
484// AND the source is VOICE_RECOGNITION or HOTWORD
485// OR The assistant is not on TOP
486// AND there is no active privacy sensitive capture or call
487// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700488// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800489// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700490// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurent4e947da2019-10-17 15:24:06 -0700491// OR the client source is HOTWORD
492// AND is on TOP
493// OR all active clients are using HOTWORD source
494// AND no call is active
495// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700496// OR the client is the current InputMethodService
497// AND a RTT call is active AND the source is VOICE_RECOGNITION
Eric Laurenta171e352019-05-07 13:04:45 -0700498// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800499// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700500// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700501// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700502// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800503
Eric Laurent4e947da2019-10-17 15:24:06 -0700504
Eric Laurent4eb58f12018-12-07 16:41:02 -0800505 sp<AudioRecordClient> topActive;
506 sp<AudioRecordClient> latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800507 sp<AudioRecordClient> topSensitiveActive;
Eric Laurentb809a752020-06-29 09:53:13 -0700508 sp<AudioRecordClient> latestSensitiveActiveOrComm;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700509
Eric Laurenta46bedb2018-12-07 18:01:26 -0800510 nsecs_t topStartNs = 0;
511 nsecs_t latestStartNs = 0;
Eric Laurentc21d5692020-02-25 10:24:36 -0800512 nsecs_t topSensitiveStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800513 nsecs_t latestSensitiveStartNs = 0;
514 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
515 bool isAssistantOnTop = false;
516 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700517 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurentc21d5692020-02-25 10:24:36 -0800518 bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION;
519 bool rttCallActive = (isInCall || isInCommunication)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700520 && mUidPolicy->isRttEnabled();
Eric Laurent4e947da2019-10-17 15:24:06 -0700521 bool onlyHotwordActive = true;
Eric Laurentb809a752020-06-29 09:53:13 -0700522 bool isPhoneStateOwnerActive = false;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800523
Michael Groovercfd28302018-12-11 19:16:46 -0800524 // if Sensor Privacy is enabled then all recordings should be silenced.
525 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
526 silenceAllRecordings_l();
527 return;
528 }
529
Eric Laurente8c8b432018-10-17 10:08:02 -0700530 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
531 sp<AudioRecordClient> current = mAudioRecordClients[i];
Evan Severson241d9592021-01-08 12:16:02 -0800532 if (!isVirtualSource(current->attributes.source)
533 && isUserSensorPrivacyEnabledForUid(current->uid)) {
534 setAppState_l(current->portId, APP_STATE_IDLE);
535 continue;
536 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700537 if (!current->active) {
538 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800539 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700540
541 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
542 // clients which app is in IDLE state are not eligible for top active or
543 // latest active
544 if (appState == APP_STATE_IDLE) {
545 continue;
546 }
547
Eric Laurent589171c2019-07-25 18:04:29 -0700548 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurent14a88632020-07-16 12:28:30 -0700549 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800550 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700551 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800552 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
553 bool isPrivacySensitive =
554 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700555
Eric Laurentc21d5692020-02-25 10:24:36 -0800556 if (appState == APP_STATE_TOP) {
557 if (isPrivacySensitive) {
558 if (current->startTimeNs > topSensitiveStartNs) {
559 topSensitiveActive = current;
560 topSensitiveStartNs = current->startTimeNs;
561 }
562 } else {
563 if (current->startTimeNs > topStartNs) {
564 topActive = current;
565 topStartNs = current->startTimeNs;
566 }
567 }
568 if (isAssistant) {
569 isAssistantOnTop = true;
570 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800571 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800572 // Clients capturing for HOTWORD are not considered
573 // for latest active to avoid masking regular clients started before
574 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
575 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
576 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700577 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
578 // is marked latest sensitive active even if another app qualifies.
579 if (current->startTimeNs > latestSensitiveStartNs
580 || (isInCommunication && current->uid == mPhoneStateOwnerUid)) {
581 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
582 || latestSensitiveActiveOrComm->uid != mPhoneStateOwnerUid) {
583 latestSensitiveActiveOrComm = current;
584 latestSensitiveStartNs = current->startTimeNs;
585 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800586 }
587 isSensitiveActive = true;
588 } else {
589 if (current->startTimeNs > latestStartNs) {
590 latestActive = current;
591 latestStartNs = current->startTimeNs;
592 }
593 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800594 }
595 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700596 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
597 onlyHotwordActive = false;
598 }
Eric Laurentb809a752020-06-29 09:53:13 -0700599 if (current->uid == mPhoneStateOwnerUid) {
600 isPhoneStateOwnerActive = true;
601 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800602 }
603
Eric Laurent1ff16a72019-03-14 18:35:04 -0700604 // if no active client with UI on Top, consider latest active as top
605 if (topActive == nullptr) {
606 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800607 topStartNs = latestStartNs;
608 }
609 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700610 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800611 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700612 } else if (latestSensitiveActiveOrComm != nullptr) {
613 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
614 // foreground UI in case both are capturing with privacy sensitive flag.
615 if (isInCommunication && latestSensitiveActiveOrComm->uid == mPhoneStateOwnerUid) {
616 topSensitiveActive = latestSensitiveActiveOrComm;
617 topSensitiveStartNs = latestSensitiveStartNs;
618 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800619 }
620
621 // If both privacy sensitive and regular capture are active:
622 // if the regular capture is privileged
623 // allow concurrency
624 // else
625 // favor the privacy sensitive case
626 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700627 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800628 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800629 }
630
631 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
632 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700633 if (!current->active) {
634 continue;
635 }
636
Eric Laurent4eb58f12018-12-07 16:41:02 -0800637 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700638 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800639 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
640 false : current->uid == topSensitiveActive->uid;
641
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000642 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700643 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700644 bool canCaptureCommunication = recordClient->canCaptureOutput
645 || !isPhoneStateOwnerActive
646 || recordClient->uid == mPhoneStateOwnerUid;
647 return !(isInCall && !canCaptureCall)
648 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800649 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700650
651 // By default allow capture if:
652 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700653 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700654 // AND there is no active privacy sensitive capture or call
655 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
656 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800657 && (isTopOrLatestActive || isTopOrLatestSensitive)
658 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700659 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800660 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800661
662 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700663 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
664 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800665 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700666 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700667 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700668 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700669 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700670 // OR uses HOTWORD
671 // AND there is no active privacy sensitive capture or call
672 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700673 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800674 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700675 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800676 }
677 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700678 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800679 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700680 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800681 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700682 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800683 }
684 }
685 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700686 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700687 // The assistant is not on TOP
688 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700689 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700690 // OR
691 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
692 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700693 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800694 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700695 allowCapture = true;
696 }
Eric Laurent589171c2019-07-25 18:04:29 -0700697 if (isA11yOnTop) {
698 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
699 allowCapture = true;
700 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800701 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700702 } else if (source == AUDIO_SOURCE_HOTWORD) {
703 // For HOTWORD source allow capture when not on TOP if:
704 // All active clients are using HOTWORD source
705 // AND no call is active
706 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800707 if (onlyHotwordActive
708 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700709 allowCapture = true;
710 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700711 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
712 // For current InputMethodService allow capture if:
713 // A RTT call is active AND the source is VOICE_RECOGNITION
714 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
715 allowCapture = true;
716 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800717 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700718 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700719 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
720 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700721 }
722}
723
Michael Groovercfd28302018-12-11 19:16:46 -0800724void AudioPolicyService::silenceAllRecordings_l() {
725 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
726 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700727 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700728 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700729 }
Michael Groovercfd28302018-12-11 19:16:46 -0800730 }
731}
732
Eric Laurente8c8b432018-10-17 10:08:02 -0700733/* static */
734app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700735
736 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700737 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700738 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
739 // include persistent services
740 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700741 }
742 return APP_STATE_FOREGROUND;
743}
744
Eric Laurent4eb58f12018-12-07 16:41:02 -0800745/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800746bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800747{
748 switch (source) {
749 case AUDIO_SOURCE_VOICE_UPLINK:
750 case AUDIO_SOURCE_VOICE_DOWNLINK:
751 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800752 case AUDIO_SOURCE_REMOTE_SUBMIX:
753 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700754 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800755 return true;
756 default:
757 break;
758 }
759 return false;
760}
761
Eric Laurent5ada82e2019-08-29 17:53:54 -0700762void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700763{
764 AutoCallerClear acc;
765
766 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700767 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700768 }
769 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
770 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700771 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700772 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700773 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800774}
775
Glenn Kasten0f11b512014-01-31 16:18:54 -0800776status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777{
Glenn Kasten44deb052012-02-05 18:09:08 -0800778 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 dumpPermissionDenial(fd);
780 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000781 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 if (!locked) {
783 String8 result(kDeadlockedString);
784 write(fd, result.string(), result.size());
785 }
786
787 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800788 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 mAudioCommandThread->dump(fd);
790 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791
Eric Laurentdce54a12014-03-10 12:19:46 -0700792 if (mAudioPolicyManager) {
793 mAudioPolicyManager->dump(fd);
794 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795
Kevin Rocard8be94972019-02-22 13:26:25 -0800796 mPackageManager.dump(fd);
797
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000798 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799 }
800 return NO_ERROR;
801}
802
803status_t AudioPolicyService::dumpPermissionDenial(int fd)
804{
805 const size_t SIZE = 256;
806 char buffer[SIZE];
807 String8 result;
808 snprintf(buffer, SIZE, "Permission Denial: "
809 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
810 IPCThreadState::self()->getCallingPid(),
811 IPCThreadState::self()->getCallingUid());
812 result.append(buffer);
813 write(fd, result.string(), result.size());
814 return NO_ERROR;
815}
816
817status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800818 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
819 switch (code) {
820 case SHELL_COMMAND_TRANSACTION: {
821 int in = data.readFileDescriptor();
822 int out = data.readFileDescriptor();
823 int err = data.readFileDescriptor();
824 int argc = data.readInt32();
825 Vector<String16> args;
826 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
827 args.add(data.readString16());
828 }
829 sp<IBinder> unusedCallback;
830 sp<IResultReceiver> resultReceiver;
831 status_t status;
832 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
833 return status;
834 }
835 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
836 return status;
837 }
838 status = shellCommand(in, out, err, args);
839 if (resultReceiver != nullptr) {
840 resultReceiver->send(status);
841 }
842 return NO_ERROR;
843 }
844 }
845
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 return BnAudioPolicyService::onTransact(code, data, reply, flags);
847}
848
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800849// ------------------- Shell command implementation -------------------
850
851// NOTE: This is a remote API - make sure all args are validated
852status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
853 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
854 return PERMISSION_DENIED;
855 }
856 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
857 return BAD_VALUE;
858 }
jovanakbe066e12019-09-02 11:54:39 -0700859 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800860 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700861 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800862 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700863 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800864 return handleGetUidState(args, out, err);
865 } else if (args.size() == 1 && args[0] == String16("help")) {
866 printHelp(out);
867 return NO_ERROR;
868 }
869 printHelp(err);
870 return BAD_VALUE;
871}
872
jovanakbe066e12019-09-02 11:54:39 -0700873static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
874 if (userId < 0) {
875 ALOGE("Invalid user: %d", userId);
876 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800877 return BAD_VALUE;
878 }
jovanakbe066e12019-09-02 11:54:39 -0700879
880 PermissionController pc;
881 uid = pc.getPackageUid(packageName, 0);
882 if (uid <= 0) {
883 ALOGE("Unknown package: '%s'", String8(packageName).string());
884 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
885 return BAD_VALUE;
886 }
887
888 uid = multiuser_get_uid(userId, uid);
889 return NO_ERROR;
890}
891
892status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
893 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
894 if (!(args.size() == 3 || args.size() == 5)) {
895 printHelp(err);
896 return BAD_VALUE;
897 }
898
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800899 bool active = false;
900 if (args[2] == String16("active")) {
901 active = true;
902 } else if ((args[2] != String16("idle"))) {
903 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
904 return BAD_VALUE;
905 }
jovanakbe066e12019-09-02 11:54:39 -0700906
907 int userId = 0;
908 if (args.size() >= 5 && args[3] == String16("--user")) {
909 userId = atoi(String8(args[4]));
910 }
911
912 uid_t uid;
913 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
914 return BAD_VALUE;
915 }
916
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000917 sp<UidPolicy> uidPolicy;
918 {
919 Mutex::Autolock _l(mLock);
920 uidPolicy = mUidPolicy;
921 }
922 if (uidPolicy) {
923 uidPolicy->addOverrideUid(uid, active);
924 return NO_ERROR;
925 }
926 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800927}
928
929status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700930 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
931 if (!(args.size() == 2 || args.size() == 4)) {
932 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800933 return BAD_VALUE;
934 }
jovanakbe066e12019-09-02 11:54:39 -0700935
936 int userId = 0;
937 if (args.size() >= 4 && args[2] == String16("--user")) {
938 userId = atoi(String8(args[3]));
939 }
940
941 uid_t uid;
942 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
943 return BAD_VALUE;
944 }
945
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000946 sp<UidPolicy> uidPolicy;
947 {
948 Mutex::Autolock _l(mLock);
949 uidPolicy = mUidPolicy;
950 }
951 if (uidPolicy) {
952 uidPolicy->removeOverrideUid(uid);
953 return NO_ERROR;
954 }
955 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800956}
957
958status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700959 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
960 if (!(args.size() == 2 || args.size() == 4)) {
961 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800962 return BAD_VALUE;
963 }
jovanakbe066e12019-09-02 11:54:39 -0700964
965 int userId = 0;
966 if (args.size() >= 4 && args[2] == String16("--user")) {
967 userId = atoi(String8(args[3]));
968 }
969
970 uid_t uid;
971 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
972 return BAD_VALUE;
973 }
974
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000975 sp<UidPolicy> uidPolicy;
976 {
977 Mutex::Autolock _l(mLock);
978 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800979 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000980 if (uidPolicy) {
981 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
982 }
983 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800984}
985
Evan Severson241d9592021-01-08 12:16:02 -0800986bool AudioPolicyService::isUserSensorPrivacyEnabledForUid(uid_t uid) {
987 userid_t userId = multiuser_get_user_id(uid);
988 if (mMicrophoneSensorPrivacyPolicies.find(userId) == mMicrophoneSensorPrivacyPolicies.end()) {
989 sp<SensorPrivacyPolicy> userPolicy = new SensorPrivacyPolicy(this);
990 userPolicy->registerSelfForMicrophoneOnly(userId);
991 mMicrophoneSensorPrivacyPolicies[userId] = userPolicy;
992 }
993 return mMicrophoneSensorPrivacyPolicies[userId]->isSensorPrivacyEnabled();
994}
995
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800996status_t AudioPolicyService::printHelp(int out) {
997 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700998 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
999 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
1000 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001001 " help print this message\n");
1002}
1003
1004// ----------- AudioPolicyService::UidPolicy implementation ----------
1005
1006void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001007 status_t res = mAm.linkToDeath(this);
1008 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001009 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -07001010 | ActivityManager::UID_OBSERVER_ACTIVE
1011 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001012 ActivityManager::PROCESS_STATE_UNKNOWN,
1013 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001014 if (!res) {
1015 Mutex::Autolock _l(mLock);
1016 mObserverRegistered = true;
1017 } else {
1018 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001019
Steven Moreland2f348142019-07-02 15:59:07 -07001020 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001021 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001022}
1023
1024void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001025 mAm.unlinkToDeath(this);
1026 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001027 Mutex::Autolock _l(mLock);
1028 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001029}
1030
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001031void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1032 Mutex::Autolock _l(mLock);
1033 mCachedUids.clear();
1034 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001035}
1036
Eric Laurente8c8b432018-10-17 10:08:02 -07001037void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001038 bool needToReregister = false;
1039 {
1040 Mutex::Autolock _l(mLock);
1041 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001042 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001043 if (needToReregister) {
1044 // Looks like ActivityManager has died previously, attempt to re-register.
1045 registerSelf();
1046 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001047}
1048
1049bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1050 if (isServiceUid(uid)) return true;
1051 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001052 {
1053 Mutex::Autolock _l(mLock);
1054 auto overrideIter = mOverrideUids.find(uid);
1055 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001056 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001057 }
1058 // In an absense of the ActivityManager, assume everything to be active.
1059 if (!mObserverRegistered) return true;
1060 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001061 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001062 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001063 }
1064 }
1065 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001066 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001067 {
1068 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001069 mCachedUids.insert(std::pair<uid_t,
1070 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1071 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001072 }
1073 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001074}
1075
Eric Laurente8c8b432018-10-17 10:08:02 -07001076int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1077 if (isServiceUid(uid)) {
1078 return ActivityManager::PROCESS_STATE_TOP;
1079 }
1080 checkRegistered();
1081 {
1082 Mutex::Autolock _l(mLock);
1083 auto overrideIter = mOverrideUids.find(uid);
1084 if (overrideIter != mOverrideUids.end()) {
1085 if (overrideIter->second.first) {
1086 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1087 return overrideIter->second.second;
1088 } else {
1089 auto cacheIter = mCachedUids.find(uid);
1090 if (cacheIter != mCachedUids.end()) {
1091 return cacheIter->second.second;
1092 }
1093 }
1094 }
1095 return ActivityManager::PROCESS_STATE_UNKNOWN;
1096 }
1097 // In an absense of the ActivityManager, assume everything to be active.
1098 if (!mObserverRegistered) {
1099 return ActivityManager::PROCESS_STATE_TOP;
1100 }
1101 auto cacheIter = mCachedUids.find(uid);
1102 if (cacheIter != mCachedUids.end()) {
1103 if (cacheIter->second.first) {
1104 return cacheIter->second.second;
1105 } else {
1106 return ActivityManager::PROCESS_STATE_UNKNOWN;
1107 }
1108 }
1109 }
1110 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001111 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001112 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1113 if (active) {
1114 state = am.getUidProcessState(uid, String16("audioserver"));
1115 }
1116 {
1117 Mutex::Autolock _l(mLock);
1118 mCachedUids.insert(std::pair<uid_t,
1119 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1120 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001121
Eric Laurente8c8b432018-10-17 10:08:02 -07001122 return state;
1123}
1124
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001125void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001126 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001127}
1128
1129void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001130 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001131}
1132
1133void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001134 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001135}
1136
Eric Laurente8c8b432018-10-17 10:08:02 -07001137void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1138 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001139 int64_t procStateSeq __unused,
1140 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001141 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1142 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001143 }
1144}
1145
1146void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001147 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1148}
1149
1150void AudioPolicyService::UidPolicy::notifyService() {
1151 sp<AudioPolicyService> service = mService.promote();
1152 if (service != nullptr) {
1153 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001154 }
1155}
1156
Eric Laurente8c8b432018-10-17 10:08:02 -07001157void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1158 std::pair<bool, int>> *uids,
1159 uid_t uid,
1160 bool active,
1161 int state,
1162 bool insert) {
1163 if (isServiceUid(uid)) {
1164 return;
1165 }
1166 bool wasActive = isUidActive(uid);
1167 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001168 {
1169 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001170 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001171 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001172 if (wasActive != isUidActive(uid) || state != previousState) {
1173 notifyService();
1174 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001175}
1176
Eric Laurente8c8b432018-10-17 10:08:02 -07001177void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1178 std::pair<bool, int>> *uids,
1179 uid_t uid,
1180 bool active,
1181 int state,
1182 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001183 auto it = uids->find(uid);
1184 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001185 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001186 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1187 it->second.first = active;
1188 }
1189 if (it->second.first) {
1190 it->second.second = state;
1191 } else {
1192 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1193 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001194 } else {
1195 uids->erase(it);
1196 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001197 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1198 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1199 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001200 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001201}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202
Eric Laurent4eb58f12018-12-07 16:41:02 -08001203bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1204 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001205 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001206 continue;
1207 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001208 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1209 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001210 return true;
1211 }
1212 }
1213 return false;
1214}
1215
Eric Laurentb78763e2018-10-17 10:08:02 -07001216bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1217{
1218 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1219 return it != mA11yUids.end();
1220}
1221
Michael Groovercfd28302018-12-11 19:16:46 -08001222// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1223void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1224 SensorPrivacyManager spm;
1225 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1226 spm.addSensorPrivacyListener(this);
1227}
1228
Evan Severson241d9592021-01-08 12:16:02 -08001229void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) {
1230 SensorPrivacyManager spm;
1231 mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
1232 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);
1233 spm.addIndividualSensorPrivacyListener(userId,
1234 SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
1235}
1236
Michael Groovercfd28302018-12-11 19:16:46 -08001237void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1238 SensorPrivacyManager spm;
1239 spm.removeSensorPrivacyListener(this);
1240}
1241
1242bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1243 return mSensorPrivacyEnabled;
1244}
1245
1246binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1247 mSensorPrivacyEnabled = enabled;
1248 sp<AudioPolicyService> service = mService.promote();
1249 if (service != nullptr) {
1250 service->updateUidStates();
1251 }
1252 return binder::Status::ok();
1253}
1254
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1256
Eric Laurentbfb1b832013-01-07 09:53:42 -08001257AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1258 const wp<AudioPolicyService>& service)
1259 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261}
1262
1263
1264AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1265{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001266 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 release_wake_lock(mName.string());
1268 }
1269 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270}
1271
1272void AudioPolicyService::AudioCommandThread::onFirstRef()
1273{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001274 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275}
1276
1277bool AudioPolicyService::AudioCommandThread::threadLoop()
1278{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001279 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280
1281 mLock.lock();
1282 while (!exitPending())
1283 {
Eric Laurent59a89232014-06-08 14:14:17 -07001284 sp<AudioPolicyService> svc;
1285 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 nsecs_t curTime = systemTime();
1287 // commands are sorted by increasing time stamp: execute them from index 0 and up
1288 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001289 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001291 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292
1293 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001295 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001296 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001297 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001298 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001299 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1300 data->mVolume,
1301 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001302 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303 }break;
1304 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001305 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001306 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1307 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001308 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001309 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001310 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001311 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001313 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001314 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001315 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001316 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001318 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001320 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001321 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001322 ALOGV("AudioCommandThread() processing stop output portId %d",
1323 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001324 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001325 if (svc == 0) {
1326 break;
1327 }
1328 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001329 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001330 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001331 }break;
1332 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001333 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001334 ALOGV("AudioCommandThread() processing release output portId %d",
1335 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001336 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001337 if (svc == 0) {
1338 break;
1339 }
1340 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001341 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001342 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001343 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001344 case CREATE_AUDIO_PATCH: {
1345 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1346 ALOGV("AudioCommandThread() processing create audio patch");
1347 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1348 if (af == 0) {
1349 command->mStatus = PERMISSION_DENIED;
1350 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001351 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001352 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001353 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001354 }
1355 } break;
1356 case RELEASE_AUDIO_PATCH: {
1357 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1358 ALOGV("AudioCommandThread() processing release audio patch");
1359 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1360 if (af == 0) {
1361 command->mStatus = PERMISSION_DENIED;
1362 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001363 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001364 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001365 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001366 }
1367 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001368 case UPDATE_AUDIOPORT_LIST: {
1369 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001370 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001371 if (svc == 0) {
1372 break;
1373 }
1374 mLock.unlock();
1375 svc->doOnAudioPortListUpdate();
1376 mLock.lock();
1377 }break;
1378 case UPDATE_AUDIOPATCH_LIST: {
1379 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001380 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001381 if (svc == 0) {
1382 break;
1383 }
1384 mLock.unlock();
1385 svc->doOnAudioPatchListUpdate();
1386 mLock.lock();
1387 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001388 case CHANGED_AUDIOVOLUMEGROUP: {
1389 AudioVolumeGroupData *data =
1390 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1391 ALOGV("AudioCommandThread() processing update audio volume group");
1392 svc = mService.promote();
1393 if (svc == 0) {
1394 break;
1395 }
1396 mLock.unlock();
1397 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1398 mLock.lock();
1399 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001400 case SET_AUDIOPORT_CONFIG: {
1401 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1402 ALOGV("AudioCommandThread() processing set port config");
1403 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1404 if (af == 0) {
1405 command->mStatus = PERMISSION_DENIED;
1406 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001407 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001408 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001409 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001410 }
1411 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001412 case DYN_POLICY_MIX_STATE_UPDATE: {
1413 DynPolicyMixStateUpdateData *data =
1414 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001415 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1416 data->mRegId.string(), data->mState);
1417 svc = mService.promote();
1418 if (svc == 0) {
1419 break;
1420 }
1421 mLock.unlock();
1422 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1423 mLock.lock();
1424 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001425 case RECORDING_CONFIGURATION_UPDATE: {
1426 RecordingConfigurationUpdateData *data =
1427 (RecordingConfigurationUpdateData *)command->mParam.get();
1428 ALOGV("AudioCommandThread() processing recording configuration update");
1429 svc = mService.promote();
1430 if (svc == 0) {
1431 break;
1432 }
1433 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001434 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001435 &data->mClientConfig, data->mClientEffects,
1436 &data->mDeviceConfig, data->mEffects,
1437 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001438 mLock.lock();
1439 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001440 case SET_EFFECT_SUSPENDED: {
1441 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1442 ALOGV("AudioCommandThread() processing set effect suspended");
1443 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1444 if (af != 0) {
1445 mLock.unlock();
1446 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1447 mLock.lock();
1448 }
1449 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001450 case AUDIO_MODULES_UPDATE: {
1451 ALOGV("AudioCommandThread() processing audio modules update");
1452 svc = mService.promote();
1453 if (svc == 0) {
1454 break;
1455 }
1456 mLock.unlock();
1457 svc->doOnNewAudioModulesAvailable();
1458 mLock.lock();
1459 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001460 case ROUTING_UPDATED: {
1461 ALOGV("AudioCommandThread() processing routing update");
1462 svc = mService.promote();
1463 if (svc == 0) {
1464 break;
1465 }
1466 mLock.unlock();
1467 svc->doOnRoutingUpdated();
1468 mLock.lock();
1469 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001470
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001472 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001474 {
1475 Mutex::Autolock _l(command->mLock);
1476 if (command->mWaitStatus) {
1477 command->mWaitStatus = false;
1478 command->mCond.signal();
1479 }
1480 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001481 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001482 // release mLock before releasing strong reference on the service as
1483 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1484 // acquires mLock.
1485 mLock.unlock();
1486 svc.clear();
1487 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 } else {
1489 waitTime = mAudioCommands[0]->mTime - curTime;
1490 break;
1491 }
1492 }
Zach Janga754b4f2015-10-27 01:29:34 +00001493
1494 // release delayed commands wake lock if the queue is empty
1495 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001496 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001497 }
1498
1499 // At this stage we have either an empty command queue or the first command in the queue
1500 // has a finite delay. So unless we are exiting it is safe to wait.
1501 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001502 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001503 if (waitTime == -1) {
1504 mWaitWorkCV.wait(mLock);
1505 } else {
1506 mWaitWorkCV.waitRelative(mLock, waitTime);
1507 }
Eric Laurent59a89232014-06-08 14:14:17 -07001508 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001510 // release delayed commands wake lock before quitting
1511 if (!mAudioCommands.isEmpty()) {
1512 release_wake_lock(mName.string());
1513 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 mLock.unlock();
1515 return false;
1516}
1517
1518status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1519{
1520 const size_t SIZE = 256;
1521 char buffer[SIZE];
1522 String8 result;
1523
1524 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1525 result.append(buffer);
1526 write(fd, result.string(), result.size());
1527
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001528 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529 if (!locked) {
1530 String8 result2(kCmdDeadlockedString);
1531 write(fd, result2.string(), result2.size());
1532 }
1533
1534 snprintf(buffer, SIZE, "- Commands:\n");
1535 result = String8(buffer);
1536 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001537 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001538 mAudioCommands[i]->dump(buffer, SIZE);
1539 result.append(buffer);
1540 }
1541 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001542 if (mLastCommand != 0) {
1543 mLastCommand->dump(buffer, SIZE);
1544 result.append(buffer);
1545 } else {
1546 result.append(" none\n");
1547 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548
1549 write(fd, result.string(), result.size());
1550
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001551 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552
1553 return NO_ERROR;
1554}
1555
Glenn Kastenfff6d712012-01-12 16:38:12 -08001556status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001557 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001558 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001559 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560{
Eric Laurent0ede8922014-05-09 18:04:42 -07001561 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001563 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001564 data->mStream = stream;
1565 data->mVolume = volume;
1566 data->mIO = output;
1567 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001568 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001569 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001570 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001571 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572}
1573
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001574status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001575 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001576 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577{
Eric Laurent0ede8922014-05-09 18:04:42 -07001578 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001580 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001582 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001583 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001584 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001585 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001586 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001587 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588}
1589
1590status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1591{
Eric Laurent0ede8922014-05-09 18:04:42 -07001592 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001594 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 data->mVolume = volume;
1596 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001597 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001598 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001599 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600}
1601
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001602void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1603 audio_session_t sessionId,
1604 bool suspended)
1605{
1606 sp<AudioCommand> command = new AudioCommand();
1607 command->mCommand = SET_EFFECT_SUSPENDED;
1608 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1609 data->mEffectId = effectId;
1610 data->mSessionId = sessionId;
1611 data->mSuspended = suspended;
1612 command->mParam = data;
1613 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1614 effectId, sessionId, suspended);
1615 sendCommand(command);
1616}
1617
1618
Eric Laurentd7fe0862018-07-14 16:48:01 -07001619void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001620{
Eric Laurent0ede8922014-05-09 18:04:42 -07001621 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001622 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001623 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001624 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001625 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001626 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001627 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001628}
1629
Eric Laurentd7fe0862018-07-14 16:48:01 -07001630void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001631{
Eric Laurent0ede8922014-05-09 18:04:42 -07001632 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001633 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001634 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001635 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001636 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001637 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001638 sendCommand(command);
1639}
1640
Eric Laurent951f4552014-05-20 10:48:17 -07001641status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1642 const struct audio_patch *patch,
1643 audio_patch_handle_t *handle,
1644 int delayMs)
1645{
1646 status_t status = NO_ERROR;
1647
1648 sp<AudioCommand> command = new AudioCommand();
1649 command->mCommand = CREATE_AUDIO_PATCH;
1650 CreateAudioPatchData *data = new CreateAudioPatchData();
1651 data->mPatch = *patch;
1652 data->mHandle = *handle;
1653 command->mParam = data;
1654 command->mWaitStatus = true;
1655 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1656 status = sendCommand(command, delayMs);
1657 if (status == NO_ERROR) {
1658 *handle = data->mHandle;
1659 }
1660 return status;
1661}
1662
1663status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1664 int delayMs)
1665{
1666 sp<AudioCommand> command = new AudioCommand();
1667 command->mCommand = RELEASE_AUDIO_PATCH;
1668 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1669 data->mHandle = handle;
1670 command->mParam = data;
1671 command->mWaitStatus = true;
1672 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1673 return sendCommand(command, delayMs);
1674}
1675
Eric Laurentb52c1522014-05-20 11:27:36 -07001676void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1677{
1678 sp<AudioCommand> command = new AudioCommand();
1679 command->mCommand = UPDATE_AUDIOPORT_LIST;
1680 ALOGV("AudioCommandThread() adding update audio port list");
1681 sendCommand(command);
1682}
1683
1684void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1685{
1686 sp<AudioCommand>command = new AudioCommand();
1687 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1688 ALOGV("AudioCommandThread() adding update audio patch list");
1689 sendCommand(command);
1690}
1691
François Gaffiecfe17322018-11-07 13:41:29 +01001692void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1693 int flags)
1694{
1695 sp<AudioCommand>command = new AudioCommand();
1696 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1697 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1698 data->mGroup = group;
1699 data->mFlags = flags;
1700 command->mParam = data;
1701 ALOGV("AudioCommandThread() adding audio volume group changed");
1702 sendCommand(command);
1703}
1704
Eric Laurente1715a42014-05-20 11:30:42 -07001705status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1706 const struct audio_port_config *config, int delayMs)
1707{
1708 sp<AudioCommand> command = new AudioCommand();
1709 command->mCommand = SET_AUDIOPORT_CONFIG;
1710 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1711 data->mConfig = *config;
1712 command->mParam = data;
1713 command->mWaitStatus = true;
1714 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1715 return sendCommand(command, delayMs);
1716}
1717
Jean-Michel Trivide801052015-04-14 19:10:14 -07001718void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001719 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001720{
1721 sp<AudioCommand> command = new AudioCommand();
1722 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1723 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1724 data->mRegId = regId;
1725 data->mState = state;
1726 command->mParam = data;
1727 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1728 regId.string(), state);
1729 sendCommand(command);
1730}
1731
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001732void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001733 int event,
1734 const record_client_info_t *clientInfo,
1735 const audio_config_base_t *clientConfig,
1736 std::vector<effect_descriptor_t> clientEffects,
1737 const audio_config_base_t *deviceConfig,
1738 std::vector<effect_descriptor_t> effects,
1739 audio_patch_handle_t patchHandle,
1740 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001741{
1742 sp<AudioCommand>command = new AudioCommand();
1743 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1744 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1745 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001746 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001747 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001748 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001749 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001750 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001751 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001752 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001753 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001754 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1755 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001756 sendCommand(command);
1757}
1758
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001759void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1760{
1761 sp<AudioCommand> command = new AudioCommand();
1762 command->mCommand = AUDIO_MODULES_UPDATE;
1763 sendCommand(command);
1764}
1765
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001766void AudioPolicyService::AudioCommandThread::routingChangedCommand()
1767{
1768 sp<AudioCommand>command = new AudioCommand();
1769 command->mCommand = ROUTING_UPDATED;
1770 ALOGV("AudioCommandThread() adding routing update");
1771 sendCommand(command);
1772}
1773
Eric Laurent0ede8922014-05-09 18:04:42 -07001774status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1775{
1776 {
1777 Mutex::Autolock _l(mLock);
1778 insertCommand_l(command, delayMs);
1779 mWaitWorkCV.signal();
1780 }
1781 Mutex::Autolock _l(command->mLock);
1782 while (command->mWaitStatus) {
1783 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1784 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1785 command->mStatus = TIMED_OUT;
1786 command->mWaitStatus = false;
1787 }
1788 }
1789 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001790}
1791
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001793void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001795 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001796 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 command->mTime = systemTime() + milliseconds(delayMs);
1798
1799 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001800 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1802 }
1803
1804 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001805 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001806 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001807 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1808 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001809
1810 // create audio patch or release audio patch commands are equivalent
1811 // with regard to filtering
1812 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1813 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1814 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1815 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1816 continue;
1817 }
1818 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001819
1820 switch (command->mCommand) {
1821 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001822 ParametersData *data = (ParametersData *)command->mParam.get();
1823 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001825 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001826 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1828 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1829 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001830 String8 key;
1831 String8 value;
1832 param.getAt(j, key, value);
1833 for (size_t k = 0; k < param2.size(); k++) {
1834 String8 key2;
1835 String8 value2;
1836 param2.getAt(k, key2, value2);
1837 if (key2 == key) {
1838 param2.remove(key2);
1839 ALOGV("Filtering out parameter %s", key2.string());
1840 break;
1841 }
1842 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843 }
1844 // if all keys have been filtered out, remove the command.
1845 // otherwise, update the key value pairs
1846 if (param2.size() == 0) {
1847 removedCommands.add(command2);
1848 } else {
1849 data2->mKeyValuePairs = param2.toString();
1850 }
Eric Laurent21e54562013-09-23 12:08:05 -07001851 command->mTime = command2->mTime;
1852 // force delayMs to non 0 so that code below does not request to wait for
1853 // command status as the command is now delayed
1854 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855 } break;
1856
1857 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001858 VolumeData *data = (VolumeData *)command->mParam.get();
1859 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860 if (data->mIO != data2->mIO) break;
1861 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001862 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001863 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001865 command->mTime = command2->mTime;
1866 // force delayMs to non 0 so that code below does not request to wait for
1867 // command status as the command is now delayed
1868 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001870
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001871 case SET_VOICE_VOLUME: {
1872 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1873 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1874 ALOGV("Filtering out voice volume command value %f replaced by %f",
1875 data2->mVolume, data->mVolume);
1876 removedCommands.add(command2);
1877 command->mTime = command2->mTime;
1878 // force delayMs to non 0 so that code below does not request to wait for
1879 // command status as the command is now delayed
1880 delayMs = 1;
1881 } break;
1882
Eric Laurente45b48a2014-09-04 16:40:57 -07001883 case CREATE_AUDIO_PATCH:
1884 case RELEASE_AUDIO_PATCH: {
1885 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001886 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001887 if (command->mCommand == CREATE_AUDIO_PATCH) {
1888 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001889 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001890 } else {
1891 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001892 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001893 }
1894 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001895 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001896 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1897 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001898 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001899 } else {
1900 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001901 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001902 }
1903 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001904 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1905 same output. */
1906 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1907 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1908 bool isOutputDiff = false;
1909 if (patch.num_sources == patch2.num_sources) {
1910 for (unsigned count = 0; count < patch.num_sources; count++) {
1911 if (patch.sources[count].id != patch2.sources[count].id) {
1912 isOutputDiff = true;
1913 break;
1914 }
1915 }
1916 if (isOutputDiff)
1917 break;
1918 }
1919 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001920 ALOGV("Filtering out %s audio patch command for handle %d",
1921 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1922 removedCommands.add(command2);
1923 command->mTime = command2->mTime;
1924 // force delayMs to non 0 so that code below does not request to wait for
1925 // command status as the command is now delayed
1926 delayMs = 1;
1927 } break;
1928
Jean-Michel Trivide801052015-04-14 19:10:14 -07001929 case DYN_POLICY_MIX_STATE_UPDATE: {
1930
1931 } break;
1932
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001933 case RECORDING_CONFIGURATION_UPDATE: {
1934
1935 } break;
1936
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001937 case ROUTING_UPDATED: {
1938
1939 } break;
1940
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 default:
1942 break;
1943 }
1944 }
1945
1946 // remove filtered commands
1947 for (size_t j = 0; j < removedCommands.size(); j++) {
1948 // removed commands always have time stamps greater than current command
1949 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001950 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001951 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 mAudioCommands.removeAt(k);
1953 break;
1954 }
1955 }
1956 }
1957 removedCommands.clear();
1958
Eric Laurentaa79bef2015-01-15 14:33:51 -08001959 // Disable wait for status if delay is not 0.
1960 // Except for create audio patch command because the returned patch handle
1961 // is needed by audio policy manager
1962 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001963 command->mWaitStatus = false;
1964 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001965
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001967 ALOGV("inserting command: %d at index %zd, num commands %zu",
1968 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969 mAudioCommands.insertAt(command, i + 1);
1970}
1971
1972void AudioPolicyService::AudioCommandThread::exit()
1973{
Steve Block3856b092011-10-20 11:56:00 +01001974 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975 {
1976 AutoMutex _l(mLock);
1977 requestExit();
1978 mWaitWorkCV.signal();
1979 }
Zach Janga754b4f2015-10-27 01:29:34 +00001980 // Note that we can call it from the thread loop if all other references have been released
1981 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982 requestExitAndWait();
1983}
1984
1985void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1986{
1987 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1988 mCommand,
1989 (int)ns2s(mTime),
1990 (int)ns2ms(mTime)%1000,
1991 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001992 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001993}
1994
Dima Zavinfce7a472011-04-19 22:30:36 -07001995/******* helpers for the service_ops callbacks defined below *********/
1996void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1997 const char *keyValuePairs,
1998 int delayMs)
1999{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002000 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07002001 delayMs);
2002}
2003
2004int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
2005 float volume,
2006 audio_io_handle_t output,
2007 int delayMs)
2008{
Glenn Kastenfff6d712012-01-12 16:38:12 -08002009 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002010 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07002011}
2012
Dima Zavinfce7a472011-04-19 22:30:36 -07002013int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
2014{
2015 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
2016}
2017
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002018void AudioPolicyService::setEffectSuspended(int effectId,
2019 audio_session_t sessionId,
2020 bool suspended)
2021{
2022 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2023}
2024
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002025void AudioPolicyService::onNewAudioModulesAvailable()
2026{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002027 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002028}
2029
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002030
Dima Zavinfce7a472011-04-19 22:30:36 -07002031extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002032audio_module_handle_t aps_load_hw_module(void *service __unused,
2033 const char *name);
2034audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002035 audio_devices_t *pDevices,
2036 uint32_t *pSamplingRate,
2037 audio_format_t *pFormat,
2038 audio_channel_mask_t *pChannelMask,
2039 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002040 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002041
Eric Laurent2d388ec2014-03-07 13:25:54 -08002042audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002043 audio_module_handle_t module,
2044 audio_devices_t *pDevices,
2045 uint32_t *pSamplingRate,
2046 audio_format_t *pFormat,
2047 audio_channel_mask_t *pChannelMask,
2048 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002049 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002050 const audio_offload_info_t *offloadInfo);
2051audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002052 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002053 audio_io_handle_t output2);
2054int aps_close_output(void *service __unused, audio_io_handle_t output);
2055int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2056int aps_restore_output(void *service __unused, audio_io_handle_t output);
2057audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002058 audio_devices_t *pDevices,
2059 uint32_t *pSamplingRate,
2060 audio_format_t *pFormat,
2061 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002062 audio_in_acoustics_t acoustics __unused);
2063audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002064 audio_module_handle_t module,
2065 audio_devices_t *pDevices,
2066 uint32_t *pSamplingRate,
2067 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002068 audio_channel_mask_t *pChannelMask);
2069int aps_close_input(void *service __unused, audio_io_handle_t input);
2070int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002071int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002072 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002073 audio_io_handle_t dst_output);
2074char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2075 const char *keys);
2076void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2077 const char *kv_pairs, int delay_ms);
2078int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002079 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002080 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002081int aps_set_voice_volume(void *service, float volume, int delay_ms);
2082};
Dima Zavinfce7a472011-04-19 22:30:36 -07002083
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002084} // namespace android