blob: a898dffdc68697c5cd97e757982206bad7b8bc09 [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];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700532 if (!current->active) {
533 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800534 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700535
536 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
537 // clients which app is in IDLE state are not eligible for top active or
538 // latest active
539 if (appState == APP_STATE_IDLE) {
540 continue;
541 }
542
Eric Laurent589171c2019-07-25 18:04:29 -0700543 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
Eric Laurent14a88632020-07-16 12:28:30 -0700544 // Clients capturing for Accessibility services or virtual sources are not considered
Eric Laurentc21d5692020-02-25 10:24:36 -0800545 // for top or latest active to avoid masking regular clients started before
Eric Laurent14a88632020-07-16 12:28:30 -0700546 if (!isAccessibility && !isVirtualSource(current->attributes.source)) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800547 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
548 bool isPrivacySensitive =
549 (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0;
Eric Laurentb809a752020-06-29 09:53:13 -0700550
Eric Laurentc21d5692020-02-25 10:24:36 -0800551 if (appState == APP_STATE_TOP) {
552 if (isPrivacySensitive) {
553 if (current->startTimeNs > topSensitiveStartNs) {
554 topSensitiveActive = current;
555 topSensitiveStartNs = current->startTimeNs;
556 }
557 } else {
558 if (current->startTimeNs > topStartNs) {
559 topActive = current;
560 topStartNs = current->startTimeNs;
561 }
562 }
563 if (isAssistant) {
564 isAssistantOnTop = true;
565 }
Eric Laurenta46bedb2018-12-07 18:01:26 -0800566 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800567 // Clients capturing for HOTWORD are not considered
568 // for latest active to avoid masking regular clients started before
569 if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD
570 || ((isA11yOnTop || rttCallActive) && isAssistant))) {
571 if (isPrivacySensitive) {
Eric Laurentb809a752020-06-29 09:53:13 -0700572 // if audio mode is IN_COMMUNICATION, make sure the audio mode owner
573 // is marked latest sensitive active even if another app qualifies.
574 if (current->startTimeNs > latestSensitiveStartNs
575 || (isInCommunication && current->uid == mPhoneStateOwnerUid)) {
576 if (!isInCommunication || latestSensitiveActiveOrComm == nullptr
577 || latestSensitiveActiveOrComm->uid != mPhoneStateOwnerUid) {
578 latestSensitiveActiveOrComm = current;
579 latestSensitiveStartNs = current->startTimeNs;
580 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800581 }
582 isSensitiveActive = true;
583 } else {
584 if (current->startTimeNs > latestStartNs) {
585 latestActive = current;
586 latestStartNs = current->startTimeNs;
587 }
588 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800589 }
590 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700591 if (current->attributes.source != AUDIO_SOURCE_HOTWORD) {
592 onlyHotwordActive = false;
593 }
Eric Laurentb809a752020-06-29 09:53:13 -0700594 if (current->uid == mPhoneStateOwnerUid) {
595 isPhoneStateOwnerActive = true;
596 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800597 }
598
Eric Laurent1ff16a72019-03-14 18:35:04 -0700599 // if no active client with UI on Top, consider latest active as top
600 if (topActive == nullptr) {
601 topActive = latestActive;
Eric Laurentc21d5692020-02-25 10:24:36 -0800602 topStartNs = latestStartNs;
603 }
604 if (topSensitiveActive == nullptr) {
Eric Laurentb809a752020-06-29 09:53:13 -0700605 topSensitiveActive = latestSensitiveActiveOrComm;
Eric Laurentc21d5692020-02-25 10:24:36 -0800606 topSensitiveStartNs = latestSensitiveStartNs;
Eric Laurentb809a752020-06-29 09:53:13 -0700607 } else if (latestSensitiveActiveOrComm != nullptr) {
608 // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with
609 // foreground UI in case both are capturing with privacy sensitive flag.
610 if (isInCommunication && latestSensitiveActiveOrComm->uid == mPhoneStateOwnerUid) {
611 topSensitiveActive = latestSensitiveActiveOrComm;
612 topSensitiveStartNs = latestSensitiveStartNs;
613 }
Eric Laurentc21d5692020-02-25 10:24:36 -0800614 }
615
616 // If both privacy sensitive and regular capture are active:
617 // if the regular capture is privileged
618 // allow concurrency
619 // else
620 // favor the privacy sensitive case
621 if (topActive != nullptr && topSensitiveActive != nullptr
Ricardo Correa57a37692020-03-23 17:27:25 -0700622 && !topActive->canCaptureOutput) {
Eric Laurentc21d5692020-02-25 10:24:36 -0800623 topActive = nullptr;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800624 }
625
626 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
627 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700628 if (!current->active) {
629 continue;
630 }
631
Eric Laurent4eb58f12018-12-07 16:41:02 -0800632 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700633 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
Eric Laurentc21d5692020-02-25 10:24:36 -0800634 bool isTopOrLatestSensitive = topSensitiveActive == nullptr ?
635 false : current->uid == topSensitiveActive->uid;
636
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000637 auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) {
Ricardo Correa57a37692020-03-23 17:27:25 -0700638 bool canCaptureCall = recordClient->canCaptureOutput;
Eric Laurentb809a752020-06-29 09:53:13 -0700639 bool canCaptureCommunication = recordClient->canCaptureOutput
640 || !isPhoneStateOwnerActive
641 || recordClient->uid == mPhoneStateOwnerUid;
642 return !(isInCall && !canCaptureCall)
643 && !(isInCommunication && !canCaptureCommunication);
Eric Laurentc21d5692020-02-25 10:24:36 -0800644 };
Eric Laurent1ff16a72019-03-14 18:35:04 -0700645
646 // By default allow capture if:
647 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700648 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700649 // AND there is no active privacy sensitive capture or call
650 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
651 bool allowCapture = !isAssistantOnTop
Eric Laurentc21d5692020-02-25 10:24:36 -0800652 && (isTopOrLatestActive || isTopOrLatestSensitive)
653 && !(isSensitiveActive
Ricardo Correa57a37692020-03-23 17:27:25 -0700654 && !(isTopOrLatestSensitive || current->canCaptureOutput))
Eric Laurentc21d5692020-02-25 10:24:36 -0800655 && canCaptureIfInCallOrCommunication(current);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800656
657 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700658 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
659 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800660 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700661 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700662 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700663 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700664 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700665 // OR uses HOTWORD
666 // AND there is no active privacy sensitive capture or call
667 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700668 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800669 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700670 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800671 }
672 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700673 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurentc21d5692020-02-25 10:24:36 -0800674 source == AUDIO_SOURCE_HOTWORD)
Ricardo Correa57a37692020-03-23 17:27:25 -0700675 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800676 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700677 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800678 }
679 }
680 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700681 // For accessibility service allow capture if:
Eric Laurent47670c92019-08-28 16:59:05 -0700682 // The assistant is not on TOP
683 // AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700684 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent47670c92019-08-28 16:59:05 -0700685 // OR
686 // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD
687 if (!isAssistantOnTop
Ricardo Correa57a37692020-03-23 17:27:25 -0700688 && !(isSensitiveActive && !current->canCaptureOutput)
Eric Laurentc21d5692020-02-25 10:24:36 -0800689 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent47670c92019-08-28 16:59:05 -0700690 allowCapture = true;
691 }
Eric Laurent589171c2019-07-25 18:04:29 -0700692 if (isA11yOnTop) {
693 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
694 allowCapture = true;
695 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800696 }
Eric Laurent4e947da2019-10-17 15:24:06 -0700697 } else if (source == AUDIO_SOURCE_HOTWORD) {
698 // For HOTWORD source allow capture when not on TOP if:
699 // All active clients are using HOTWORD source
700 // AND no call is active
701 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurentc21d5692020-02-25 10:24:36 -0800702 if (onlyHotwordActive
703 && canCaptureIfInCallOrCommunication(current)) {
Eric Laurent4e947da2019-10-17 15:24:06 -0700704 allowCapture = true;
705 }
Kohsuke Yatoha623a132020-03-24 20:10:26 -0700706 } else if (mUidPolicy->isCurrentImeUid(current->uid)) {
707 // For current InputMethodService allow capture if:
708 // A RTT call is active AND the source is VOICE_RECOGNITION
709 if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) {
710 allowCapture = true;
711 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800712 }
Eric Laurent5ada82e2019-08-29 17:53:54 -0700713 setAppState_l(current->portId,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700714 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
715 APP_STATE_IDLE);
Eric Laurente8c8b432018-10-17 10:08:02 -0700716 }
717}
718
Michael Groovercfd28302018-12-11 19:16:46 -0800719void AudioPolicyService::silenceAllRecordings_l() {
720 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
721 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700722 if (!isVirtualSource(current->attributes.source)) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700723 setAppState_l(current->portId, APP_STATE_IDLE);
Eric Laurent1ff16a72019-03-14 18:35:04 -0700724 }
Michael Groovercfd28302018-12-11 19:16:46 -0800725 }
726}
727
Eric Laurente8c8b432018-10-17 10:08:02 -0700728/* static */
729app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700730
731 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700732 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700733 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
734 // include persistent services
735 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700736 }
737 return APP_STATE_FOREGROUND;
738}
739
Eric Laurent4eb58f12018-12-07 16:41:02 -0800740/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800741bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800742{
743 switch (source) {
744 case AUDIO_SOURCE_VOICE_UPLINK:
745 case AUDIO_SOURCE_VOICE_DOWNLINK:
746 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800747 case AUDIO_SOURCE_REMOTE_SUBMIX:
748 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent68eb2122020-04-30 17:40:57 -0700749 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800750 return true;
751 default:
752 break;
753 }
754 return false;
755}
756
Eric Laurent5ada82e2019-08-29 17:53:54 -0700757void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state)
Eric Laurente8c8b432018-10-17 10:08:02 -0700758{
759 AutoCallerClear acc;
760
761 if (mAudioPolicyManager) {
Eric Laurent5ada82e2019-08-29 17:53:54 -0700762 mAudioPolicyManager->setAppState(portId, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700763 }
764 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
765 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700766 bool silenced = state == APP_STATE_IDLE;
Eric Laurent5ada82e2019-08-29 17:53:54 -0700767 af->setRecordSilenced(portId, silenced);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700768 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800769}
770
Glenn Kasten0f11b512014-01-31 16:18:54 -0800771status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772{
Glenn Kasten44deb052012-02-05 18:09:08 -0800773 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 dumpPermissionDenial(fd);
775 } else {
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000776 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 if (!locked) {
778 String8 result(kDeadlockedString);
779 write(fd, result.string(), result.size());
780 }
781
782 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800783 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 mAudioCommandThread->dump(fd);
785 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786
Eric Laurentdce54a12014-03-10 12:19:46 -0700787 if (mAudioPolicyManager) {
788 mAudioPolicyManager->dump(fd);
789 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790
Kevin Rocard8be94972019-02-22 13:26:25 -0800791 mPackageManager.dump(fd);
792
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000793 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 }
795 return NO_ERROR;
796}
797
798status_t AudioPolicyService::dumpPermissionDenial(int fd)
799{
800 const size_t SIZE = 256;
801 char buffer[SIZE];
802 String8 result;
803 snprintf(buffer, SIZE, "Permission Denial: "
804 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
805 IPCThreadState::self()->getCallingPid(),
806 IPCThreadState::self()->getCallingUid());
807 result.append(buffer);
808 write(fd, result.string(), result.size());
809 return NO_ERROR;
810}
811
812status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800813 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
814 switch (code) {
815 case SHELL_COMMAND_TRANSACTION: {
816 int in = data.readFileDescriptor();
817 int out = data.readFileDescriptor();
818 int err = data.readFileDescriptor();
819 int argc = data.readInt32();
820 Vector<String16> args;
821 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
822 args.add(data.readString16());
823 }
824 sp<IBinder> unusedCallback;
825 sp<IResultReceiver> resultReceiver;
826 status_t status;
827 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
828 return status;
829 }
830 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
831 return status;
832 }
833 status = shellCommand(in, out, err, args);
834 if (resultReceiver != nullptr) {
835 resultReceiver->send(status);
836 }
837 return NO_ERROR;
838 }
839 }
840
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 return BnAudioPolicyService::onTransact(code, data, reply, flags);
842}
843
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800844// ------------------- Shell command implementation -------------------
845
846// NOTE: This is a remote API - make sure all args are validated
847status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
848 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
849 return PERMISSION_DENIED;
850 }
851 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
852 return BAD_VALUE;
853 }
jovanakbe066e12019-09-02 11:54:39 -0700854 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800855 return handleSetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700856 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800857 return handleResetUidState(args, err);
jovanakbe066e12019-09-02 11:54:39 -0700858 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800859 return handleGetUidState(args, out, err);
860 } else if (args.size() == 1 && args[0] == String16("help")) {
861 printHelp(out);
862 return NO_ERROR;
863 }
864 printHelp(err);
865 return BAD_VALUE;
866}
867
jovanakbe066e12019-09-02 11:54:39 -0700868static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
869 if (userId < 0) {
870 ALOGE("Invalid user: %d", userId);
871 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800872 return BAD_VALUE;
873 }
jovanakbe066e12019-09-02 11:54:39 -0700874
875 PermissionController pc;
876 uid = pc.getPackageUid(packageName, 0);
877 if (uid <= 0) {
878 ALOGE("Unknown package: '%s'", String8(packageName).string());
879 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
880 return BAD_VALUE;
881 }
882
883 uid = multiuser_get_uid(userId, uid);
884 return NO_ERROR;
885}
886
887status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
888 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
889 if (!(args.size() == 3 || args.size() == 5)) {
890 printHelp(err);
891 return BAD_VALUE;
892 }
893
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800894 bool active = false;
895 if (args[2] == String16("active")) {
896 active = true;
897 } else if ((args[2] != String16("idle"))) {
898 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
899 return BAD_VALUE;
900 }
jovanakbe066e12019-09-02 11:54:39 -0700901
902 int userId = 0;
903 if (args.size() >= 5 && args[3] == String16("--user")) {
904 userId = atoi(String8(args[4]));
905 }
906
907 uid_t uid;
908 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
909 return BAD_VALUE;
910 }
911
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000912 sp<UidPolicy> uidPolicy;
913 {
914 Mutex::Autolock _l(mLock);
915 uidPolicy = mUidPolicy;
916 }
917 if (uidPolicy) {
918 uidPolicy->addOverrideUid(uid, active);
919 return NO_ERROR;
920 }
921 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800922}
923
924status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700925 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
926 if (!(args.size() == 2 || args.size() == 4)) {
927 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800928 return BAD_VALUE;
929 }
jovanakbe066e12019-09-02 11:54:39 -0700930
931 int userId = 0;
932 if (args.size() >= 4 && args[2] == String16("--user")) {
933 userId = atoi(String8(args[3]));
934 }
935
936 uid_t uid;
937 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
938 return BAD_VALUE;
939 }
940
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000941 sp<UidPolicy> uidPolicy;
942 {
943 Mutex::Autolock _l(mLock);
944 uidPolicy = mUidPolicy;
945 }
946 if (uidPolicy) {
947 uidPolicy->removeOverrideUid(uid);
948 return NO_ERROR;
949 }
950 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800951}
952
953status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanakbe066e12019-09-02 11:54:39 -0700954 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
955 if (!(args.size() == 2 || args.size() == 4)) {
956 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800957 return BAD_VALUE;
958 }
jovanakbe066e12019-09-02 11:54:39 -0700959
960 int userId = 0;
961 if (args.size() >= 4 && args[2] == String16("--user")) {
962 userId = atoi(String8(args[3]));
963 }
964
965 uid_t uid;
966 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
967 return BAD_VALUE;
968 }
969
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000970 sp<UidPolicy> uidPolicy;
971 {
972 Mutex::Autolock _l(mLock);
973 uidPolicy = mUidPolicy;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800974 }
Mikhail Naganov12b716c2020-04-30 22:37:43 +0000975 if (uidPolicy) {
976 return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n");
977 }
978 return NO_INIT;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800979}
980
981status_t AudioPolicyService::printHelp(int out) {
982 return dprintf(out, "Audio policy service commands:\n"
jovanakbe066e12019-09-02 11:54:39 -0700983 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
984 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
985 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800986 " help print this message\n");
987}
988
989// ----------- AudioPolicyService::UidPolicy implementation ----------
990
991void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700992 status_t res = mAm.linkToDeath(this);
993 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800994 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700995 | ActivityManager::UID_OBSERVER_ACTIVE
996 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800997 ActivityManager::PROCESS_STATE_UNKNOWN,
998 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700999 if (!res) {
1000 Mutex::Autolock _l(mLock);
1001 mObserverRegistered = true;
1002 } else {
1003 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -08001004
Steven Moreland2f348142019-07-02 15:59:07 -07001005 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001006 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001007}
1008
1009void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -07001010 mAm.unlinkToDeath(this);
1011 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001012 Mutex::Autolock _l(mLock);
1013 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001014}
1015
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001016void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
1017 Mutex::Autolock _l(mLock);
1018 mCachedUids.clear();
1019 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001020}
1021
Eric Laurente8c8b432018-10-17 10:08:02 -07001022void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001023 bool needToReregister = false;
1024 {
1025 Mutex::Autolock _l(mLock);
1026 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001027 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001028 if (needToReregister) {
1029 // Looks like ActivityManager has died previously, attempt to re-register.
1030 registerSelf();
1031 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001032}
1033
1034bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
1035 if (isServiceUid(uid)) return true;
1036 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001037 {
1038 Mutex::Autolock _l(mLock);
1039 auto overrideIter = mOverrideUids.find(uid);
1040 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001041 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001042 }
1043 // In an absense of the ActivityManager, assume everything to be active.
1044 if (!mObserverRegistered) return true;
1045 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -07001046 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001047 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001048 }
1049 }
1050 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001051 bool active = am.isUidActive(uid, String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001052 {
1053 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001054 mCachedUids.insert(std::pair<uid_t,
1055 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
1056 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001057 }
1058 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001059}
1060
Eric Laurente8c8b432018-10-17 10:08:02 -07001061int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
1062 if (isServiceUid(uid)) {
1063 return ActivityManager::PROCESS_STATE_TOP;
1064 }
1065 checkRegistered();
1066 {
1067 Mutex::Autolock _l(mLock);
1068 auto overrideIter = mOverrideUids.find(uid);
1069 if (overrideIter != mOverrideUids.end()) {
1070 if (overrideIter->second.first) {
1071 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
1072 return overrideIter->second.second;
1073 } else {
1074 auto cacheIter = mCachedUids.find(uid);
1075 if (cacheIter != mCachedUids.end()) {
1076 return cacheIter->second.second;
1077 }
1078 }
1079 }
1080 return ActivityManager::PROCESS_STATE_UNKNOWN;
1081 }
1082 // In an absense of the ActivityManager, assume everything to be active.
1083 if (!mObserverRegistered) {
1084 return ActivityManager::PROCESS_STATE_TOP;
1085 }
1086 auto cacheIter = mCachedUids.find(uid);
1087 if (cacheIter != mCachedUids.end()) {
1088 if (cacheIter->second.first) {
1089 return cacheIter->second.second;
1090 } else {
1091 return ActivityManager::PROCESS_STATE_UNKNOWN;
1092 }
1093 }
1094 }
1095 ActivityManager am;
Hui Yu12c7ec72020-05-04 17:40:52 +00001096 bool active = am.isUidActive(uid, String16("audioserver"));
Eric Laurente8c8b432018-10-17 10:08:02 -07001097 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
1098 if (active) {
1099 state = am.getUidProcessState(uid, String16("audioserver"));
1100 }
1101 {
1102 Mutex::Autolock _l(mLock);
1103 mCachedUids.insert(std::pair<uid_t,
1104 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
1105 }
Eric Laurent4eb58f12018-12-07 16:41:02 -08001106
Eric Laurente8c8b432018-10-17 10:08:02 -07001107 return state;
1108}
1109
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001110void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001111 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001112}
1113
1114void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001115 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001116}
1117
1118void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001119 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001120}
1121
Eric Laurente8c8b432018-10-17 10:08:02 -07001122void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
1123 int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07001124 int64_t procStateSeq __unused,
1125 int32_t capability __unused) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001126 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
1127 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001128 }
1129}
1130
1131void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001132 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
1133}
1134
1135void AudioPolicyService::UidPolicy::notifyService() {
1136 sp<AudioPolicyService> service = mService.promote();
1137 if (service != nullptr) {
1138 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001139 }
1140}
1141
Eric Laurente8c8b432018-10-17 10:08:02 -07001142void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
1143 std::pair<bool, int>> *uids,
1144 uid_t uid,
1145 bool active,
1146 int state,
1147 bool insert) {
1148 if (isServiceUid(uid)) {
1149 return;
1150 }
1151 bool wasActive = isUidActive(uid);
1152 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001153 {
1154 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001155 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001156 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001157 if (wasActive != isUidActive(uid) || state != previousState) {
1158 notifyService();
1159 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001160}
1161
Eric Laurente8c8b432018-10-17 10:08:02 -07001162void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1163 std::pair<bool, int>> *uids,
1164 uid_t uid,
1165 bool active,
1166 int state,
1167 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001168 auto it = uids->find(uid);
1169 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001170 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001171 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1172 it->second.first = active;
1173 }
1174 if (it->second.first) {
1175 it->second.second = state;
1176 } else {
1177 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1178 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001179 } else {
1180 uids->erase(it);
1181 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001182 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1183 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1184 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001185 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001186}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187
Eric Laurent4eb58f12018-12-07 16:41:02 -08001188bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1189 for (const auto &uid : mCachedUids) {
Eric Laurent47670c92019-08-28 16:59:05 -07001190 if (!isA11yUid(uid.first)) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001191 continue;
1192 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001193 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1194 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001195 return true;
1196 }
1197 }
1198 return false;
1199}
1200
Eric Laurentb78763e2018-10-17 10:08:02 -07001201bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1202{
1203 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1204 return it != mA11yUids.end();
1205}
1206
Michael Groovercfd28302018-12-11 19:16:46 -08001207// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1208void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1209 SensorPrivacyManager spm;
1210 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1211 spm.addSensorPrivacyListener(this);
1212}
1213
1214void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1215 SensorPrivacyManager spm;
1216 spm.removeSensorPrivacyListener(this);
1217}
1218
1219bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1220 return mSensorPrivacyEnabled;
1221}
1222
1223binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1224 mSensorPrivacyEnabled = enabled;
1225 sp<AudioPolicyService> service = mService.promote();
1226 if (service != nullptr) {
1227 service->updateUidStates();
1228 }
1229 return binder::Status::ok();
1230}
1231
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1233
Eric Laurentbfb1b832013-01-07 09:53:42 -08001234AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1235 const wp<AudioPolicyService>& service)
1236 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238}
1239
1240
1241AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1242{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001243 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 release_wake_lock(mName.string());
1245 }
1246 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247}
1248
1249void AudioPolicyService::AudioCommandThread::onFirstRef()
1250{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001251 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252}
1253
1254bool AudioPolicyService::AudioCommandThread::threadLoop()
1255{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001256 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257
1258 mLock.lock();
1259 while (!exitPending())
1260 {
Eric Laurent59a89232014-06-08 14:14:17 -07001261 sp<AudioPolicyService> svc;
1262 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 nsecs_t curTime = systemTime();
1264 // commands are sorted by increasing time stamp: execute them from index 0 and up
1265 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001266 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001268 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269
1270 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001272 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001273 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001274 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001275 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001276 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1277 data->mVolume,
1278 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001279 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 }break;
1281 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001282 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001283 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1284 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001285 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001286 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001287 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001288 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001290 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001292 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001293 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001295 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001297 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001298 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001299 ALOGV("AudioCommandThread() processing stop output portId %d",
1300 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001301 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001302 if (svc == 0) {
1303 break;
1304 }
1305 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001306 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001307 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001308 }break;
1309 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001310 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001311 ALOGV("AudioCommandThread() processing release output portId %d",
1312 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001313 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001314 if (svc == 0) {
1315 break;
1316 }
1317 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001318 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001319 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001320 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001321 case CREATE_AUDIO_PATCH: {
1322 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1323 ALOGV("AudioCommandThread() processing create audio patch");
1324 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1325 if (af == 0) {
1326 command->mStatus = PERMISSION_DENIED;
1327 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001328 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001329 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001330 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001331 }
1332 } break;
1333 case RELEASE_AUDIO_PATCH: {
1334 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1335 ALOGV("AudioCommandThread() processing release audio patch");
1336 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1337 if (af == 0) {
1338 command->mStatus = PERMISSION_DENIED;
1339 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001340 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001341 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001342 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001343 }
1344 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001345 case UPDATE_AUDIOPORT_LIST: {
1346 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001347 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001348 if (svc == 0) {
1349 break;
1350 }
1351 mLock.unlock();
1352 svc->doOnAudioPortListUpdate();
1353 mLock.lock();
1354 }break;
1355 case UPDATE_AUDIOPATCH_LIST: {
1356 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001357 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001358 if (svc == 0) {
1359 break;
1360 }
1361 mLock.unlock();
1362 svc->doOnAudioPatchListUpdate();
1363 mLock.lock();
1364 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001365 case CHANGED_AUDIOVOLUMEGROUP: {
1366 AudioVolumeGroupData *data =
1367 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1368 ALOGV("AudioCommandThread() processing update audio volume group");
1369 svc = mService.promote();
1370 if (svc == 0) {
1371 break;
1372 }
1373 mLock.unlock();
1374 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1375 mLock.lock();
1376 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001377 case SET_AUDIOPORT_CONFIG: {
1378 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1379 ALOGV("AudioCommandThread() processing set port config");
1380 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1381 if (af == 0) {
1382 command->mStatus = PERMISSION_DENIED;
1383 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001384 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001385 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001386 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001387 }
1388 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001389 case DYN_POLICY_MIX_STATE_UPDATE: {
1390 DynPolicyMixStateUpdateData *data =
1391 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001392 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1393 data->mRegId.string(), data->mState);
1394 svc = mService.promote();
1395 if (svc == 0) {
1396 break;
1397 }
1398 mLock.unlock();
1399 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1400 mLock.lock();
1401 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001402 case RECORDING_CONFIGURATION_UPDATE: {
1403 RecordingConfigurationUpdateData *data =
1404 (RecordingConfigurationUpdateData *)command->mParam.get();
1405 ALOGV("AudioCommandThread() processing recording configuration update");
1406 svc = mService.promote();
1407 if (svc == 0) {
1408 break;
1409 }
1410 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001411 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001412 &data->mClientConfig, data->mClientEffects,
1413 &data->mDeviceConfig, data->mEffects,
1414 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001415 mLock.lock();
1416 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001417 case SET_EFFECT_SUSPENDED: {
1418 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1419 ALOGV("AudioCommandThread() processing set effect suspended");
1420 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1421 if (af != 0) {
1422 mLock.unlock();
1423 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1424 mLock.lock();
1425 }
1426 } break;
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001427 case AUDIO_MODULES_UPDATE: {
1428 ALOGV("AudioCommandThread() processing audio modules update");
1429 svc = mService.promote();
1430 if (svc == 0) {
1431 break;
1432 }
1433 mLock.unlock();
1434 svc->doOnNewAudioModulesAvailable();
1435 mLock.lock();
1436 } break;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001437 case ROUTING_UPDATED: {
1438 ALOGV("AudioCommandThread() processing routing update");
1439 svc = mService.promote();
1440 if (svc == 0) {
1441 break;
1442 }
1443 mLock.unlock();
1444 svc->doOnRoutingUpdated();
1445 mLock.lock();
1446 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001447
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001449 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001451 {
1452 Mutex::Autolock _l(command->mLock);
1453 if (command->mWaitStatus) {
1454 command->mWaitStatus = false;
1455 command->mCond.signal();
1456 }
1457 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001458 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001459 // release mLock before releasing strong reference on the service as
1460 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1461 // acquires mLock.
1462 mLock.unlock();
1463 svc.clear();
1464 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001465 } else {
1466 waitTime = mAudioCommands[0]->mTime - curTime;
1467 break;
1468 }
1469 }
Zach Janga754b4f2015-10-27 01:29:34 +00001470
1471 // release delayed commands wake lock if the queue is empty
1472 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001473 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001474 }
1475
1476 // At this stage we have either an empty command queue or the first command in the queue
1477 // has a finite delay. So unless we are exiting it is safe to wait.
1478 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001479 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001480 if (waitTime == -1) {
1481 mWaitWorkCV.wait(mLock);
1482 } else {
1483 mWaitWorkCV.waitRelative(mLock, waitTime);
1484 }
Eric Laurent59a89232014-06-08 14:14:17 -07001485 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001487 // release delayed commands wake lock before quitting
1488 if (!mAudioCommands.isEmpty()) {
1489 release_wake_lock(mName.string());
1490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001491 mLock.unlock();
1492 return false;
1493}
1494
1495status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1496{
1497 const size_t SIZE = 256;
1498 char buffer[SIZE];
1499 String8 result;
1500
1501 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1502 result.append(buffer);
1503 write(fd, result.string(), result.size());
1504
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001505 const bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001506 if (!locked) {
1507 String8 result2(kCmdDeadlockedString);
1508 write(fd, result2.string(), result2.size());
1509 }
1510
1511 snprintf(buffer, SIZE, "- Commands:\n");
1512 result = String8(buffer);
1513 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001514 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515 mAudioCommands[i]->dump(buffer, SIZE);
1516 result.append(buffer);
1517 }
1518 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001519 if (mLastCommand != 0) {
1520 mLastCommand->dump(buffer, SIZE);
1521 result.append(buffer);
1522 } else {
1523 result.append(" none\n");
1524 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525
1526 write(fd, result.string(), result.size());
1527
Mikhail Naganov12b716c2020-04-30 22:37:43 +00001528 dumpReleaseLock(mLock, locked);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529
1530 return NO_ERROR;
1531}
1532
Glenn Kastenfff6d712012-01-12 16:38:12 -08001533status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001534 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001535 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001536 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537{
Eric Laurent0ede8922014-05-09 18:04:42 -07001538 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001540 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541 data->mStream = stream;
1542 data->mVolume = volume;
1543 data->mIO = output;
1544 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001545 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001546 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001547 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001548 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549}
1550
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001551status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001552 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001553 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554{
Eric Laurent0ede8922014-05-09 18:04:42 -07001555 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001557 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001559 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001561 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001562 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001563 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001564 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565}
1566
1567status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1568{
Eric Laurent0ede8922014-05-09 18:04:42 -07001569 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001571 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572 data->mVolume = volume;
1573 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001574 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001575 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001576 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577}
1578
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001579void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1580 audio_session_t sessionId,
1581 bool suspended)
1582{
1583 sp<AudioCommand> command = new AudioCommand();
1584 command->mCommand = SET_EFFECT_SUSPENDED;
1585 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1586 data->mEffectId = effectId;
1587 data->mSessionId = sessionId;
1588 data->mSuspended = suspended;
1589 command->mParam = data;
1590 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1591 effectId, sessionId, suspended);
1592 sendCommand(command);
1593}
1594
1595
Eric Laurentd7fe0862018-07-14 16:48:01 -07001596void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001597{
Eric Laurent0ede8922014-05-09 18:04:42 -07001598 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001599 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001600 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001601 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001602 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001603 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001604 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001605}
1606
Eric Laurentd7fe0862018-07-14 16:48:01 -07001607void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001608{
Eric Laurent0ede8922014-05-09 18:04:42 -07001609 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001610 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001611 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001612 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001613 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001614 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001615 sendCommand(command);
1616}
1617
Eric Laurent951f4552014-05-20 10:48:17 -07001618status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1619 const struct audio_patch *patch,
1620 audio_patch_handle_t *handle,
1621 int delayMs)
1622{
1623 status_t status = NO_ERROR;
1624
1625 sp<AudioCommand> command = new AudioCommand();
1626 command->mCommand = CREATE_AUDIO_PATCH;
1627 CreateAudioPatchData *data = new CreateAudioPatchData();
1628 data->mPatch = *patch;
1629 data->mHandle = *handle;
1630 command->mParam = data;
1631 command->mWaitStatus = true;
1632 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1633 status = sendCommand(command, delayMs);
1634 if (status == NO_ERROR) {
1635 *handle = data->mHandle;
1636 }
1637 return status;
1638}
1639
1640status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1641 int delayMs)
1642{
1643 sp<AudioCommand> command = new AudioCommand();
1644 command->mCommand = RELEASE_AUDIO_PATCH;
1645 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1646 data->mHandle = handle;
1647 command->mParam = data;
1648 command->mWaitStatus = true;
1649 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1650 return sendCommand(command, delayMs);
1651}
1652
Eric Laurentb52c1522014-05-20 11:27:36 -07001653void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1654{
1655 sp<AudioCommand> command = new AudioCommand();
1656 command->mCommand = UPDATE_AUDIOPORT_LIST;
1657 ALOGV("AudioCommandThread() adding update audio port list");
1658 sendCommand(command);
1659}
1660
1661void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1662{
1663 sp<AudioCommand>command = new AudioCommand();
1664 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1665 ALOGV("AudioCommandThread() adding update audio patch list");
1666 sendCommand(command);
1667}
1668
François Gaffiecfe17322018-11-07 13:41:29 +01001669void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1670 int flags)
1671{
1672 sp<AudioCommand>command = new AudioCommand();
1673 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1674 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1675 data->mGroup = group;
1676 data->mFlags = flags;
1677 command->mParam = data;
1678 ALOGV("AudioCommandThread() adding audio volume group changed");
1679 sendCommand(command);
1680}
1681
Eric Laurente1715a42014-05-20 11:30:42 -07001682status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1683 const struct audio_port_config *config, int delayMs)
1684{
1685 sp<AudioCommand> command = new AudioCommand();
1686 command->mCommand = SET_AUDIOPORT_CONFIG;
1687 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1688 data->mConfig = *config;
1689 command->mParam = data;
1690 command->mWaitStatus = true;
1691 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1692 return sendCommand(command, delayMs);
1693}
1694
Jean-Michel Trivide801052015-04-14 19:10:14 -07001695void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001696 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001697{
1698 sp<AudioCommand> command = new AudioCommand();
1699 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1700 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1701 data->mRegId = regId;
1702 data->mState = state;
1703 command->mParam = data;
1704 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1705 regId.string(), state);
1706 sendCommand(command);
1707}
1708
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001709void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001710 int event,
1711 const record_client_info_t *clientInfo,
1712 const audio_config_base_t *clientConfig,
1713 std::vector<effect_descriptor_t> clientEffects,
1714 const audio_config_base_t *deviceConfig,
1715 std::vector<effect_descriptor_t> effects,
1716 audio_patch_handle_t patchHandle,
1717 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001718{
1719 sp<AudioCommand>command = new AudioCommand();
1720 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1721 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1722 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001723 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001724 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001725 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001726 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001727 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001728 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001729 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001730 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001731 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1732 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001733 sendCommand(command);
1734}
1735
Mikhail Naganov88b30d22020-03-09 19:43:13 +00001736void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1737{
1738 sp<AudioCommand> command = new AudioCommand();
1739 command->mCommand = AUDIO_MODULES_UPDATE;
1740 sendCommand(command);
1741}
1742
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001743void AudioPolicyService::AudioCommandThread::routingChangedCommand()
1744{
1745 sp<AudioCommand>command = new AudioCommand();
1746 command->mCommand = ROUTING_UPDATED;
1747 ALOGV("AudioCommandThread() adding routing update");
1748 sendCommand(command);
1749}
1750
Eric Laurent0ede8922014-05-09 18:04:42 -07001751status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1752{
1753 {
1754 Mutex::Autolock _l(mLock);
1755 insertCommand_l(command, delayMs);
1756 mWaitWorkCV.signal();
1757 }
1758 Mutex::Autolock _l(command->mLock);
1759 while (command->mWaitStatus) {
1760 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1761 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1762 command->mStatus = TIMED_OUT;
1763 command->mWaitStatus = false;
1764 }
1765 }
1766 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001767}
1768
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001770void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001771{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001772 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001773 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 command->mTime = systemTime() + milliseconds(delayMs);
1775
1776 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001777 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1779 }
1780
1781 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001782 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001783 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1785 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001786
1787 // create audio patch or release audio patch commands are equivalent
1788 // with regard to filtering
1789 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1790 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1791 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1792 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1793 continue;
1794 }
1795 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796
1797 switch (command->mCommand) {
1798 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001799 ParametersData *data = (ParametersData *)command->mParam.get();
1800 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001802 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001803 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1805 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1806 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001807 String8 key;
1808 String8 value;
1809 param.getAt(j, key, value);
1810 for (size_t k = 0; k < param2.size(); k++) {
1811 String8 key2;
1812 String8 value2;
1813 param2.getAt(k, key2, value2);
1814 if (key2 == key) {
1815 param2.remove(key2);
1816 ALOGV("Filtering out parameter %s", key2.string());
1817 break;
1818 }
1819 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820 }
1821 // if all keys have been filtered out, remove the command.
1822 // otherwise, update the key value pairs
1823 if (param2.size() == 0) {
1824 removedCommands.add(command2);
1825 } else {
1826 data2->mKeyValuePairs = param2.toString();
1827 }
Eric Laurent21e54562013-09-23 12:08:05 -07001828 command->mTime = command2->mTime;
1829 // force delayMs to non 0 so that code below does not request to wait for
1830 // command status as the command is now delayed
1831 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001832 } break;
1833
1834 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001835 VolumeData *data = (VolumeData *)command->mParam.get();
1836 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837 if (data->mIO != data2->mIO) break;
1838 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001840 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001841 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001842 command->mTime = command2->mTime;
1843 // force delayMs to non 0 so that code below does not request to wait for
1844 // command status as the command is now delayed
1845 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001847
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001848 case SET_VOICE_VOLUME: {
1849 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1850 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1851 ALOGV("Filtering out voice volume command value %f replaced by %f",
1852 data2->mVolume, data->mVolume);
1853 removedCommands.add(command2);
1854 command->mTime = command2->mTime;
1855 // force delayMs to non 0 so that code below does not request to wait for
1856 // command status as the command is now delayed
1857 delayMs = 1;
1858 } break;
1859
Eric Laurente45b48a2014-09-04 16:40:57 -07001860 case CREATE_AUDIO_PATCH:
1861 case RELEASE_AUDIO_PATCH: {
1862 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001863 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001864 if (command->mCommand == CREATE_AUDIO_PATCH) {
1865 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001866 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001867 } else {
1868 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001869 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001870 }
1871 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001872 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001873 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1874 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001875 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001876 } else {
1877 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001878 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001879 }
1880 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001881 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1882 same output. */
1883 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1884 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1885 bool isOutputDiff = false;
1886 if (patch.num_sources == patch2.num_sources) {
1887 for (unsigned count = 0; count < patch.num_sources; count++) {
1888 if (patch.sources[count].id != patch2.sources[count].id) {
1889 isOutputDiff = true;
1890 break;
1891 }
1892 }
1893 if (isOutputDiff)
1894 break;
1895 }
1896 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001897 ALOGV("Filtering out %s audio patch command for handle %d",
1898 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1899 removedCommands.add(command2);
1900 command->mTime = command2->mTime;
1901 // force delayMs to non 0 so that code below does not request to wait for
1902 // command status as the command is now delayed
1903 delayMs = 1;
1904 } break;
1905
Jean-Michel Trivide801052015-04-14 19:10:14 -07001906 case DYN_POLICY_MIX_STATE_UPDATE: {
1907
1908 } break;
1909
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001910 case RECORDING_CONFIGURATION_UPDATE: {
1911
1912 } break;
1913
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -07001914 case ROUTING_UPDATED: {
1915
1916 } break;
1917
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918 default:
1919 break;
1920 }
1921 }
1922
1923 // remove filtered commands
1924 for (size_t j = 0; j < removedCommands.size(); j++) {
1925 // removed commands always have time stamps greater than current command
1926 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001927 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001928 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 mAudioCommands.removeAt(k);
1930 break;
1931 }
1932 }
1933 }
1934 removedCommands.clear();
1935
Eric Laurentaa79bef2015-01-15 14:33:51 -08001936 // Disable wait for status if delay is not 0.
1937 // Except for create audio patch command because the returned patch handle
1938 // is needed by audio policy manager
1939 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001940 command->mWaitStatus = false;
1941 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001942
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001944 ALOGV("inserting command: %d at index %zd, num commands %zu",
1945 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 mAudioCommands.insertAt(command, i + 1);
1947}
1948
1949void AudioPolicyService::AudioCommandThread::exit()
1950{
Steve Block3856b092011-10-20 11:56:00 +01001951 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 {
1953 AutoMutex _l(mLock);
1954 requestExit();
1955 mWaitWorkCV.signal();
1956 }
Zach Janga754b4f2015-10-27 01:29:34 +00001957 // Note that we can call it from the thread loop if all other references have been released
1958 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 requestExitAndWait();
1960}
1961
1962void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1963{
1964 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1965 mCommand,
1966 (int)ns2s(mTime),
1967 (int)ns2ms(mTime)%1000,
1968 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001969 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970}
1971
Dima Zavinfce7a472011-04-19 22:30:36 -07001972/******* helpers for the service_ops callbacks defined below *********/
1973void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1974 const char *keyValuePairs,
1975 int delayMs)
1976{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001977 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001978 delayMs);
1979}
1980
1981int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1982 float volume,
1983 audio_io_handle_t output,
1984 int delayMs)
1985{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001986 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001987 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001988}
1989
Dima Zavinfce7a472011-04-19 22:30:36 -07001990int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1991{
1992 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1993}
1994
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001995void AudioPolicyService::setEffectSuspended(int effectId,
1996 audio_session_t sessionId,
1997 bool suspended)
1998{
1999 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
2000}
2001
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002002void AudioPolicyService::onNewAudioModulesAvailable()
2003{
Mikhail Naganovd0e2c742020-03-25 15:59:59 -07002004 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov88b30d22020-03-09 19:43:13 +00002005}
2006
Eric Laurentb20cf7d2019-04-05 19:37:34 -07002007
Dima Zavinfce7a472011-04-19 22:30:36 -07002008extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08002009audio_module_handle_t aps_load_hw_module(void *service __unused,
2010 const char *name);
2011audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002012 audio_devices_t *pDevices,
2013 uint32_t *pSamplingRate,
2014 audio_format_t *pFormat,
2015 audio_channel_mask_t *pChannelMask,
2016 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002017 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07002018
Eric Laurent2d388ec2014-03-07 13:25:54 -08002019audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002020 audio_module_handle_t module,
2021 audio_devices_t *pDevices,
2022 uint32_t *pSamplingRate,
2023 audio_format_t *pFormat,
2024 audio_channel_mask_t *pChannelMask,
2025 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002026 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002027 const audio_offload_info_t *offloadInfo);
2028audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07002029 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002030 audio_io_handle_t output2);
2031int aps_close_output(void *service __unused, audio_io_handle_t output);
2032int aps_suspend_output(void *service __unused, audio_io_handle_t output);
2033int aps_restore_output(void *service __unused, audio_io_handle_t output);
2034audio_io_handle_t aps_open_input(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,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002039 audio_in_acoustics_t acoustics __unused);
2040audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07002041 audio_module_handle_t module,
2042 audio_devices_t *pDevices,
2043 uint32_t *pSamplingRate,
2044 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002045 audio_channel_mask_t *pChannelMask);
2046int aps_close_input(void *service __unused, audio_io_handle_t input);
2047int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08002048int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07002049 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002050 audio_io_handle_t dst_output);
2051char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
2052 const char *keys);
2053void aps_set_parameters(void *service, audio_io_handle_t io_handle,
2054 const char *kv_pairs, int delay_ms);
2055int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07002056 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08002057 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08002058int aps_set_voice_volume(void *service, float volume, int delay_ms);
2059};
Dima Zavinfce7a472011-04-19 22:30:36 -07002060
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08002061} // namespace android