blob: c4139d304203099ca1bb21f9df1ac8bc84a4fa8e [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <sys/time.h>
Mikhail Naganov959e2d02019-03-28 11:08:19 -070026
27#include <audio_utils/clock.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mikhail Naganov959e2d02019-03-28 11:08:19 -070051static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start audio commands thread
71 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
72 // start output activity command thread
73 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070074
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 mAudioPolicyClient = new AudioPolicyClient(this);
76 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 }
bryant_liuba2b4392014-06-11 16:49:30 +080078 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
80 {
81 Mutex::Autolock _l(mLock);
82 mAudioPolicyEffects = audioPolicyEffects;
83 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080084
85 mUidPolicy = new UidPolicy(this);
86 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080087
88 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
89 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070090}
91
92AudioPolicyService::~AudioPolicyService()
93{
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070095 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070096
Eric Laurentf269b8e2014-06-09 20:01:29 -070097 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070098 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070099
100 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800101 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800102
103 mUidPolicy->unregisterSelf();
104 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800105
106 mSensorPrivacyPolicy->unregisterSelf();
107 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700108}
109
110// A notification client is always registered by AudioSystem when the client process
111// connects to AudioPolicyService.
112void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
113{
Eric Laurent12590252015-08-21 18:40:20 -0700114 if (client == 0) {
115 ALOGW("%s got NULL client", __FUNCTION__);
116 return;
117 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800118 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700119
120 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800121 pid_t pid = IPCThreadState::self()->getCallingPid();
122 int64_t token = ((int64_t)uid<<32) | pid;
123
124 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700125 sp<NotificationClient> notificationClient = new NotificationClient(this,
126 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800127 uid,
128 pid);
129 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700130
luochaojiang908c7d72018-06-21 14:58:04 +0800131 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700132
Marco Nelissenf8880202014-11-14 07:58:25 -0800133 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700134 binder->linkToDeath(notificationClient);
135 }
136}
137
Eric Laurente8726fe2015-06-26 09:39:24 -0700138void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
139{
140 Mutex::Autolock _l(mNotificationClientsLock);
141
142 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800143 pid_t pid = IPCThreadState::self()->getCallingPid();
144 int64_t token = ((int64_t)uid<<32) | pid;
145
146 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700147 return;
148 }
luochaojiang908c7d72018-06-21 14:58:04 +0800149 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700150}
151
François Gaffiecfe17322018-11-07 13:41:29 +0100152void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
153{
154 Mutex::Autolock _l(mNotificationClientsLock);
155
156 uid_t uid = IPCThreadState::self()->getCallingUid();
157 pid_t pid = IPCThreadState::self()->getCallingPid();
158 int64_t token = ((int64_t)uid<<32) | pid;
159
160 if (mNotificationClients.indexOfKey(token) < 0) {
161 return;
162 }
163 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
164}
165
Eric Laurentb52c1522014-05-20 11:27:36 -0700166// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800167void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700168{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800169 {
170 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800171 int64_t token = ((int64_t)uid<<32) | pid;
172 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800173 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800174 {
175 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800176 bool hasSameUid = false;
177 for (size_t i = 0; i < mNotificationClients.size(); i++) {
178 if (mNotificationClients.valueAt(i)->uid() == uid) {
179 hasSameUid = true;
180 break;
181 }
182 }
183 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700184 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700185 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700186 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800187 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700188}
189
190void AudioPolicyService::onAudioPortListUpdate()
191{
192 mOutputCommandThread->updateAudioPortListCommand();
193}
194
195void AudioPolicyService::doOnAudioPortListUpdate()
196{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800197 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700198 for (size_t i = 0; i < mNotificationClients.size(); i++) {
199 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
200 }
201}
202
203void AudioPolicyService::onAudioPatchListUpdate()
204{
205 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206}
207
Eric Laurentb52c1522014-05-20 11:27:36 -0700208void AudioPolicyService::doOnAudioPatchListUpdate()
209{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800210 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700211 for (size_t i = 0; i < mNotificationClients.size(); i++) {
212 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
213 }
214}
215
François Gaffiecfe17322018-11-07 13:41:29 +0100216void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
217{
218 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
219}
220
221void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
222{
223 Mutex::Autolock _l(mNotificationClientsLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
225 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
226 }
227}
228
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700229void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700230{
231 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
232 regId.string(), state);
233 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
234}
235
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700236void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700237{
238 Mutex::Autolock _l(mNotificationClientsLock);
239 for (size_t i = 0; i < mNotificationClients.size(); i++) {
240 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
241 }
242}
243
Eric Laurenta9f86652018-11-28 17:23:11 -0800244void AudioPolicyService::onRecordingConfigurationUpdate(
245 int event,
246 const record_client_info_t *clientInfo,
247 const audio_config_base_t *clientConfig,
248 std::vector<effect_descriptor_t> clientEffects,
249 const audio_config_base_t *deviceConfig,
250 std::vector<effect_descriptor_t> effects,
251 audio_patch_handle_t patchHandle,
252 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800253{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800254 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800255 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800256}
257
Eric Laurenta9f86652018-11-28 17:23:11 -0800258void AudioPolicyService::doOnRecordingConfigurationUpdate(
259 int event,
260 const record_client_info_t *clientInfo,
261 const audio_config_base_t *clientConfig,
262 std::vector<effect_descriptor_t> clientEffects,
263 const audio_config_base_t *deviceConfig,
264 std::vector<effect_descriptor_t> effects,
265 audio_patch_handle_t patchHandle,
266 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800267{
268 Mutex::Autolock _l(mNotificationClientsLock);
269 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800270 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800271 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800272 }
273}
274
275status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
276 audio_patch_handle_t *handle,
277 int delayMs)
278{
279 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
280}
281
282status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
283 int delayMs)
284{
285 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
286}
287
Eric Laurente1715a42014-05-20 11:30:42 -0700288status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
289 int delayMs)
290{
291 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
292}
293
Eric Laurentb52c1522014-05-20 11:27:36 -0700294AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
295 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800296 uid_t uid,
297 pid_t pid)
298 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100299 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700300{
301}
302
303AudioPolicyService::NotificationClient::~NotificationClient()
304{
305}
306
307void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
308{
309 sp<NotificationClient> keep(this);
310 sp<AudioPolicyService> service = mService.promote();
311 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800312 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700313 }
314}
315
316void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
317{
Eric Laurente8726fe2015-06-26 09:39:24 -0700318 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700319 mAudioPolicyServiceClient->onAudioPortListUpdate();
320 }
321}
322
323void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
324{
Eric Laurente8726fe2015-06-26 09:39:24 -0700325 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700326 mAudioPolicyServiceClient->onAudioPatchListUpdate();
327 }
328}
Eric Laurent57dae992011-07-24 13:36:09 -0700329
François Gaffiecfe17322018-11-07 13:41:29 +0100330void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
331 int flags)
332{
333 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
334 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
335 }
336}
337
338
Jean-Michel Trivide801052015-04-14 19:10:14 -0700339void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700340 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700341{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700342 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800343 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
344 }
345}
346
347void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800348 int event,
349 const record_client_info_t *clientInfo,
350 const audio_config_base_t *clientConfig,
351 std::vector<effect_descriptor_t> clientEffects,
352 const audio_config_base_t *deviceConfig,
353 std::vector<effect_descriptor_t> effects,
354 audio_patch_handle_t patchHandle,
355 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800356{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700357 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800358 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800359 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700360 }
361}
362
Eric Laurente8726fe2015-06-26 09:39:24 -0700363void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
364{
365 mAudioPortCallbacksEnabled = enabled;
366}
367
François Gaffiecfe17322018-11-07 13:41:29 +0100368void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
369{
370 mAudioVolumeGroupCallbacksEnabled = enabled;
371}
Eric Laurente8726fe2015-06-26 09:39:24 -0700372
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700374 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700375 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376}
377
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700378static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700380 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
381 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382}
383
384status_t AudioPolicyService::dumpInternals(int fd)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389
Eric Laurentdce54a12014-03-10 12:19:46 -0700390 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 result.append(buffer);
392 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
393 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394
395 write(fd, result.string(), result.size());
396 return NO_ERROR;
397}
398
Eric Laurente8c8b432018-10-17 10:08:02 -0700399void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800400{
Eric Laurente8c8b432018-10-17 10:08:02 -0700401 Mutex::Autolock _l(mLock);
402 updateUidStates_l();
403}
404
405void AudioPolicyService::updateUidStates_l()
406{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800407// Go over all active clients and allow capture (does not force silence) in the
408// following cases:
Eric Laurent1ff16a72019-03-14 18:35:04 -0700409// Another client in the same UID has already been allowed to capture
410// OR The client is the assistant
Eric Laurent6ede98f2019-06-11 14:50:30 -0700411// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700412// AND the source is VOICE_RECOGNITION or HOTWORD
413// OR uses VOICE_RECOGNITION AND is on TOP
414// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700415// AND there is no active privacy sensitive capture or call
416// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800417// OR The client is an accessibility service
Eric Laurent589171c2019-07-25 18:04:29 -0700418// AND Is on TOP
419// AND the source is VOICE_RECOGNITION or HOTWORD
420// OR The assistant is not on TOP
421// AND there is no active privacy sensitive capture or call
422// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700423// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800424// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700425// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurenta171e352019-05-07 13:04:45 -0700426// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800427// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700428// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700429// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700430// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800431
432 sp<AudioRecordClient> topActive;
433 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800434 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700435
Eric Laurenta46bedb2018-12-07 18:01:26 -0800436 nsecs_t topStartNs = 0;
437 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800438 nsecs_t latestSensitiveStartNs = 0;
439 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
440 bool isAssistantOnTop = false;
441 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700442 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700443 bool rttCallActive =
444 (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
445 && mUidPolicy->isRttEnabled();
Eric Laurent4eb58f12018-12-07 16:41:02 -0800446
Michael Groovercfd28302018-12-11 19:16:46 -0800447 // if Sensor Privacy is enabled then all recordings should be silenced.
448 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
449 silenceAllRecordings_l();
450 return;
451 }
452
Eric Laurente8c8b432018-10-17 10:08:02 -0700453 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
454 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700455 if (!current->active) {
456 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800457 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700458
459 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
460 // clients which app is in IDLE state are not eligible for top active or
461 // latest active
462 if (appState == APP_STATE_IDLE) {
463 continue;
464 }
465
Eric Laurentc6af90d2019-07-19 10:31:39 -0700466 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
Eric Laurent589171c2019-07-25 18:04:29 -0700467 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
468 if (appState == APP_STATE_TOP && !isAccessibility) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800469 if (current->startTimeNs > topStartNs) {
470 topActive = current;
471 topStartNs = current->startTimeNs;
472 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700473 if (isAssistant) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800474 isAssistantOnTop = true;
475 }
476 }
Eric Laurent589171c2019-07-25 18:04:29 -0700477 // Assistant capturing for HOTWORD or Accessibility services not considered
478 // for latest active to avoid masking regular clients started before
479 if (current->startTimeNs > latestStartNs
480 && !((current->attributes.source == AUDIO_SOURCE_HOTWORD
481 || isA11yOnTop || rttCallActive)
482 && isAssistant)
483 && !isAccessibility) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800484 latestActive = current;
485 latestStartNs = current->startTimeNs;
486 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700487 if (isPrivacySensitiveSource(current->attributes.source)) {
488 if (current->startTimeNs > latestSensitiveStartNs) {
489 latestSensitiveActive = current;
490 latestSensitiveStartNs = current->startTimeNs;
491 }
492 isSensitiveActive = true;
493 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800494 }
495
Eric Laurent1ff16a72019-03-14 18:35:04 -0700496 // if no active client with UI on Top, consider latest active as top
497 if (topActive == nullptr) {
498 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800499 }
500
Eric Laurent1ff16a72019-03-14 18:35:04 -0700501 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800502
Eric Laurent4eb58f12018-12-07 16:41:02 -0800503 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
504 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700505 if (!current->active) {
506 continue;
507 }
508
509 // keep capture allowed if another client with the same UID has already
510 // been allowed to capture
511 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
512 != enabledUids.end()) {
513 continue;
514 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800515
516 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700517 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
518 bool isLatestSensitive = latestSensitiveActive == nullptr ?
519 false : current->uid == latestSensitiveActive->uid;
520
521 // By default allow capture if:
522 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700523 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700524 // AND there is no active privacy sensitive capture or call
525 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
526 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700527 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700528 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
529 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800530
531 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700532 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
533 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800534 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700535 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700536 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700537 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700538 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700539 // OR uses HOTWORD
540 // AND there is no active privacy sensitive capture or call
541 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700542 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800543 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700544 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800545 }
546 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700547 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700548 source == AUDIO_SOURCE_HOTWORD) &&
549 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
550 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800551 }
552 }
553 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700554 // For accessibility service allow capture if:
Eric Laurenta171e352019-05-07 13:04:45 -0700555 // Is on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700556 // AND the source is VOICE_RECOGNITION or HOTWORD
557 // Or
558 // The assistant is not on TOP
559 // AND there is no active privacy sensitive capture or call
560 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
561 if (isA11yOnTop) {
562 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
563 allowCapture = true;
564 }
565 } else {
566 if (!isAssistantOnTop
567 && (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
568 allowCapture = true;
569 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800570 }
571 }
572 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700573 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
574 APP_STATE_IDLE);
575 if (allowCapture) {
576 enabledUids.push_back(current->uid);
577 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700578 }
579}
580
Michael Groovercfd28302018-12-11 19:16:46 -0800581void AudioPolicyService::silenceAllRecordings_l() {
582 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
583 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700584 if (!isVirtualSource(current->attributes.source)) {
585 setAppState_l(current->uid, APP_STATE_IDLE);
586 }
Michael Groovercfd28302018-12-11 19:16:46 -0800587 }
588}
589
Eric Laurente8c8b432018-10-17 10:08:02 -0700590/* static */
591app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700592
593 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700594 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700595 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
596 // include persistent services
597 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700598 }
599 return APP_STATE_FOREGROUND;
600}
601
Eric Laurent4eb58f12018-12-07 16:41:02 -0800602/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800603bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
604{
605 switch (source) {
606 case AUDIO_SOURCE_CAMCORDER:
607 case AUDIO_SOURCE_VOICE_COMMUNICATION:
608 return true;
609 default:
610 break;
611 }
612 return false;
613}
614
615/* static */
616bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800617{
618 switch (source) {
619 case AUDIO_SOURCE_VOICE_UPLINK:
620 case AUDIO_SOURCE_VOICE_DOWNLINK:
621 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800622 case AUDIO_SOURCE_REMOTE_SUBMIX:
623 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800624 return true;
625 default:
626 break;
627 }
628 return false;
629}
630
Eric Laurente8c8b432018-10-17 10:08:02 -0700631void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
632{
633 AutoCallerClear acc;
634
635 if (mAudioPolicyManager) {
636 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700637 }
638 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
639 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700640 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700641 af->setRecordSilenced(uid, silenced);
642 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800643}
644
Glenn Kasten0f11b512014-01-31 16:18:54 -0800645status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646{
Glenn Kasten44deb052012-02-05 18:09:08 -0800647 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 dumpPermissionDenial(fd);
649 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700650 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 if (!locked) {
652 String8 result(kDeadlockedString);
653 write(fd, result.string(), result.size());
654 }
655
656 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800657 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 mAudioCommandThread->dump(fd);
659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660
Eric Laurentdce54a12014-03-10 12:19:46 -0700661 if (mAudioPolicyManager) {
662 mAudioPolicyManager->dump(fd);
663 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664
Kevin Rocard8be94972019-02-22 13:26:25 -0800665 mPackageManager.dump(fd);
666
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 if (locked) mLock.unlock();
668 }
669 return NO_ERROR;
670}
671
672status_t AudioPolicyService::dumpPermissionDenial(int fd)
673{
674 const size_t SIZE = 256;
675 char buffer[SIZE];
676 String8 result;
677 snprintf(buffer, SIZE, "Permission Denial: "
678 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
679 IPCThreadState::self()->getCallingPid(),
680 IPCThreadState::self()->getCallingUid());
681 result.append(buffer);
682 write(fd, result.string(), result.size());
683 return NO_ERROR;
684}
685
686status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800687 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
688 switch (code) {
689 case SHELL_COMMAND_TRANSACTION: {
690 int in = data.readFileDescriptor();
691 int out = data.readFileDescriptor();
692 int err = data.readFileDescriptor();
693 int argc = data.readInt32();
694 Vector<String16> args;
695 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
696 args.add(data.readString16());
697 }
698 sp<IBinder> unusedCallback;
699 sp<IResultReceiver> resultReceiver;
700 status_t status;
701 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
702 return status;
703 }
704 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
705 return status;
706 }
707 status = shellCommand(in, out, err, args);
708 if (resultReceiver != nullptr) {
709 resultReceiver->send(status);
710 }
711 return NO_ERROR;
712 }
713 }
714
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 return BnAudioPolicyService::onTransact(code, data, reply, flags);
716}
717
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800718// ------------------- Shell command implementation -------------------
719
720// NOTE: This is a remote API - make sure all args are validated
721status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
722 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
723 return PERMISSION_DENIED;
724 }
725 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
726 return BAD_VALUE;
727 }
jovanak87f731b2019-09-02 11:54:39 -0700728 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800729 return handleSetUidState(args, err);
jovanak87f731b2019-09-02 11:54:39 -0700730 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800731 return handleResetUidState(args, err);
jovanak87f731b2019-09-02 11:54:39 -0700732 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800733 return handleGetUidState(args, out, err);
734 } else if (args.size() == 1 && args[0] == String16("help")) {
735 printHelp(out);
736 return NO_ERROR;
737 }
738 printHelp(err);
739 return BAD_VALUE;
740}
741
jovanak87f731b2019-09-02 11:54:39 -0700742static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
743 if (userId < 0) {
744 ALOGE("Invalid user: %d", userId);
745 dprintf(err, "Invalid user: %d\n", userId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800746 return BAD_VALUE;
747 }
jovanak87f731b2019-09-02 11:54:39 -0700748
749 PermissionController pc;
750 uid = pc.getPackageUid(packageName, 0);
751 if (uid <= 0) {
752 ALOGE("Unknown package: '%s'", String8(packageName).string());
753 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
754 return BAD_VALUE;
755 }
756
757 uid = multiuser_get_uid(userId, uid);
758 return NO_ERROR;
759}
760
761status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
762 // Valid arg.size() is 3 or 5, args.size() is 5 with --user option.
763 if (!(args.size() == 3 || args.size() == 5)) {
764 printHelp(err);
765 return BAD_VALUE;
766 }
767
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800768 bool active = false;
769 if (args[2] == String16("active")) {
770 active = true;
771 } else if ((args[2] != String16("idle"))) {
772 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
773 return BAD_VALUE;
774 }
jovanak87f731b2019-09-02 11:54:39 -0700775
776 int userId = 0;
777 if (args.size() >= 5 && args[3] == String16("--user")) {
778 userId = atoi(String8(args[4]));
779 }
780
781 uid_t uid;
782 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
783 return BAD_VALUE;
784 }
785
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800786 mUidPolicy->addOverrideUid(uid, active);
787 return NO_ERROR;
788}
789
790status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
jovanak87f731b2019-09-02 11:54:39 -0700791 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
792 if (!(args.size() == 2 || args.size() == 4)) {
793 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800794 return BAD_VALUE;
795 }
jovanak87f731b2019-09-02 11:54:39 -0700796
797 int userId = 0;
798 if (args.size() >= 4 && args[2] == String16("--user")) {
799 userId = atoi(String8(args[3]));
800 }
801
802 uid_t uid;
803 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
804 return BAD_VALUE;
805 }
806
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800807 mUidPolicy->removeOverrideUid(uid);
808 return NO_ERROR;
809}
810
811status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
jovanak87f731b2019-09-02 11:54:39 -0700812 // Valid arg.size() is 2 or 4, args.size() is 4 with --user option.
813 if (!(args.size() == 2 || args.size() == 4)) {
814 printHelp(err);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800815 return BAD_VALUE;
816 }
jovanak87f731b2019-09-02 11:54:39 -0700817
818 int userId = 0;
819 if (args.size() >= 4 && args[2] == String16("--user")) {
820 userId = atoi(String8(args[3]));
821 }
822
823 uid_t uid;
824 if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) {
825 return BAD_VALUE;
826 }
827
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800828 if (mUidPolicy->isUidActive(uid)) {
829 return dprintf(out, "active\n");
830 } else {
831 return dprintf(out, "idle\n");
832 }
833}
834
835status_t AudioPolicyService::printHelp(int out) {
836 return dprintf(out, "Audio policy service commands:\n"
jovanak87f731b2019-09-02 11:54:39 -0700837 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
838 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
839 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800840 " help print this message\n");
841}
842
843// ----------- AudioPolicyService::UidPolicy implementation ----------
844
845void AudioPolicyService::UidPolicy::registerSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700846 status_t res = mAm.linkToDeath(this);
847 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800848 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700849 | ActivityManager::UID_OBSERVER_ACTIVE
850 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800851 ActivityManager::PROCESS_STATE_UNKNOWN,
852 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700853 if (!res) {
854 Mutex::Autolock _l(mLock);
855 mObserverRegistered = true;
856 } else {
857 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800858
Steven Moreland2f348142019-07-02 15:59:07 -0700859 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700860 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800861}
862
863void AudioPolicyService::UidPolicy::unregisterSelf() {
Steven Moreland2f348142019-07-02 15:59:07 -0700864 mAm.unlinkToDeath(this);
865 mAm.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700866 Mutex::Autolock _l(mLock);
867 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800868}
869
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700870void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
871 Mutex::Autolock _l(mLock);
872 mCachedUids.clear();
873 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800874}
875
Eric Laurente8c8b432018-10-17 10:08:02 -0700876void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700877 bool needToReregister = false;
878 {
879 Mutex::Autolock _l(mLock);
880 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800881 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700882 if (needToReregister) {
883 // Looks like ActivityManager has died previously, attempt to re-register.
884 registerSelf();
885 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700886}
887
888bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
889 if (isServiceUid(uid)) return true;
890 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700891 {
892 Mutex::Autolock _l(mLock);
893 auto overrideIter = mOverrideUids.find(uid);
894 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700895 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700896 }
897 // In an absense of the ActivityManager, assume everything to be active.
898 if (!mObserverRegistered) return true;
899 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700900 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700901 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700902 }
903 }
904 ActivityManager am;
905 bool active = am.isUidActive(uid, String16("audioserver"));
906 {
907 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700908 mCachedUids.insert(std::pair<uid_t,
909 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
910 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700911 }
912 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800913}
914
Eric Laurente8c8b432018-10-17 10:08:02 -0700915int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
916 if (isServiceUid(uid)) {
917 return ActivityManager::PROCESS_STATE_TOP;
918 }
919 checkRegistered();
920 {
921 Mutex::Autolock _l(mLock);
922 auto overrideIter = mOverrideUids.find(uid);
923 if (overrideIter != mOverrideUids.end()) {
924 if (overrideIter->second.first) {
925 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
926 return overrideIter->second.second;
927 } else {
928 auto cacheIter = mCachedUids.find(uid);
929 if (cacheIter != mCachedUids.end()) {
930 return cacheIter->second.second;
931 }
932 }
933 }
934 return ActivityManager::PROCESS_STATE_UNKNOWN;
935 }
936 // In an absense of the ActivityManager, assume everything to be active.
937 if (!mObserverRegistered) {
938 return ActivityManager::PROCESS_STATE_TOP;
939 }
940 auto cacheIter = mCachedUids.find(uid);
941 if (cacheIter != mCachedUids.end()) {
942 if (cacheIter->second.first) {
943 return cacheIter->second.second;
944 } else {
945 return ActivityManager::PROCESS_STATE_UNKNOWN;
946 }
947 }
948 }
949 ActivityManager am;
950 bool active = am.isUidActive(uid, String16("audioserver"));
951 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
952 if (active) {
953 state = am.getUidProcessState(uid, String16("audioserver"));
954 }
955 {
956 Mutex::Autolock _l(mLock);
957 mCachedUids.insert(std::pair<uid_t,
958 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
959 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800960
Eric Laurente8c8b432018-10-17 10:08:02 -0700961 return state;
962}
963
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700964void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700965 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700966}
967
968void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700969 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700970}
971
972void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700973 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700974}
975
Eric Laurente8c8b432018-10-17 10:08:02 -0700976void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
977 int32_t procState,
978 int64_t procStateSeq __unused) {
979 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
980 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700981 }
982}
983
984void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700985 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
986}
987
988void AudioPolicyService::UidPolicy::notifyService() {
989 sp<AudioPolicyService> service = mService.promote();
990 if (service != nullptr) {
991 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700992 }
993}
994
Eric Laurente8c8b432018-10-17 10:08:02 -0700995void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
996 std::pair<bool, int>> *uids,
997 uid_t uid,
998 bool active,
999 int state,
1000 bool insert) {
1001 if (isServiceUid(uid)) {
1002 return;
1003 }
1004 bool wasActive = isUidActive(uid);
1005 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001006 {
1007 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -07001008 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001009 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001010 if (wasActive != isUidActive(uid) || state != previousState) {
1011 notifyService();
1012 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001013}
1014
Eric Laurente8c8b432018-10-17 10:08:02 -07001015void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
1016 std::pair<bool, int>> *uids,
1017 uid_t uid,
1018 bool active,
1019 int state,
1020 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001021 auto it = uids->find(uid);
1022 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001023 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -07001024 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
1025 it->second.first = active;
1026 }
1027 if (it->second.first) {
1028 it->second.second = state;
1029 } else {
1030 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
1031 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001032 } else {
1033 uids->erase(it);
1034 }
Eric Laurente8c8b432018-10-17 10:08:02 -07001035 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
1036 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
1037 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -07001038 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001039}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040
Eric Laurent4eb58f12018-12-07 16:41:02 -08001041bool AudioPolicyService::UidPolicy::isA11yOnTop() {
1042 for (const auto &uid : mCachedUids) {
1043 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
1044 if (it == mA11yUids.end()) {
1045 continue;
1046 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001047 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1048 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001049 return true;
1050 }
1051 }
1052 return false;
1053}
1054
Eric Laurentb78763e2018-10-17 10:08:02 -07001055bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1056{
1057 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1058 return it != mA11yUids.end();
1059}
1060
Michael Groovercfd28302018-12-11 19:16:46 -08001061// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1062void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1063 SensorPrivacyManager spm;
1064 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1065 spm.addSensorPrivacyListener(this);
1066}
1067
1068void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1069 SensorPrivacyManager spm;
1070 spm.removeSensorPrivacyListener(this);
1071}
1072
1073bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1074 return mSensorPrivacyEnabled;
1075}
1076
1077binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1078 mSensorPrivacyEnabled = enabled;
1079 sp<AudioPolicyService> service = mService.promote();
1080 if (service != nullptr) {
1081 service->updateUidStates();
1082 }
1083 return binder::Status::ok();
1084}
1085
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1087
Eric Laurentbfb1b832013-01-07 09:53:42 -08001088AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1089 const wp<AudioPolicyService>& service)
1090 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092}
1093
1094
1095AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1096{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001097 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098 release_wake_lock(mName.string());
1099 }
1100 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101}
1102
1103void AudioPolicyService::AudioCommandThread::onFirstRef()
1104{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001105 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106}
1107
1108bool AudioPolicyService::AudioCommandThread::threadLoop()
1109{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001110 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111
1112 mLock.lock();
1113 while (!exitPending())
1114 {
Eric Laurent59a89232014-06-08 14:14:17 -07001115 sp<AudioPolicyService> svc;
1116 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 nsecs_t curTime = systemTime();
1118 // commands are sorted by increasing time stamp: execute them from index 0 and up
1119 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001120 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001122 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123
1124 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001126 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001127 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001128 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001129 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001130 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1131 data->mVolume,
1132 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001133 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134 }break;
1135 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001136 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001137 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1138 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001139 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001140 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001141 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001142 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001144 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001145 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001146 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001147 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001149 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001151 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001152 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001153 ALOGV("AudioCommandThread() processing stop output portId %d",
1154 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001155 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001156 if (svc == 0) {
1157 break;
1158 }
1159 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001160 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001161 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001162 }break;
1163 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001164 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001165 ALOGV("AudioCommandThread() processing release output portId %d",
1166 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001167 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001168 if (svc == 0) {
1169 break;
1170 }
1171 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001172 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001174 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001175 case CREATE_AUDIO_PATCH: {
1176 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1177 ALOGV("AudioCommandThread() processing create audio patch");
1178 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1179 if (af == 0) {
1180 command->mStatus = PERMISSION_DENIED;
1181 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001182 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001183 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001184 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001185 }
1186 } break;
1187 case RELEASE_AUDIO_PATCH: {
1188 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1189 ALOGV("AudioCommandThread() processing release audio patch");
1190 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1191 if (af == 0) {
1192 command->mStatus = PERMISSION_DENIED;
1193 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001194 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001195 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001196 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001197 }
1198 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001199 case UPDATE_AUDIOPORT_LIST: {
1200 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001201 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001202 if (svc == 0) {
1203 break;
1204 }
1205 mLock.unlock();
1206 svc->doOnAudioPortListUpdate();
1207 mLock.lock();
1208 }break;
1209 case UPDATE_AUDIOPATCH_LIST: {
1210 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001211 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001212 if (svc == 0) {
1213 break;
1214 }
1215 mLock.unlock();
1216 svc->doOnAudioPatchListUpdate();
1217 mLock.lock();
1218 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001219 case CHANGED_AUDIOVOLUMEGROUP: {
1220 AudioVolumeGroupData *data =
1221 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1222 ALOGV("AudioCommandThread() processing update audio volume group");
1223 svc = mService.promote();
1224 if (svc == 0) {
1225 break;
1226 }
1227 mLock.unlock();
1228 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1229 mLock.lock();
1230 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001231 case SET_AUDIOPORT_CONFIG: {
1232 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1233 ALOGV("AudioCommandThread() processing set port config");
1234 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1235 if (af == 0) {
1236 command->mStatus = PERMISSION_DENIED;
1237 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001238 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001239 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001240 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001241 }
1242 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001243 case DYN_POLICY_MIX_STATE_UPDATE: {
1244 DynPolicyMixStateUpdateData *data =
1245 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001246 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1247 data->mRegId.string(), data->mState);
1248 svc = mService.promote();
1249 if (svc == 0) {
1250 break;
1251 }
1252 mLock.unlock();
1253 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1254 mLock.lock();
1255 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001256 case RECORDING_CONFIGURATION_UPDATE: {
1257 RecordingConfigurationUpdateData *data =
1258 (RecordingConfigurationUpdateData *)command->mParam.get();
1259 ALOGV("AudioCommandThread() processing recording configuration update");
1260 svc = mService.promote();
1261 if (svc == 0) {
1262 break;
1263 }
1264 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001265 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001266 &data->mClientConfig, data->mClientEffects,
1267 &data->mDeviceConfig, data->mEffects,
1268 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001269 mLock.lock();
1270 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001271 case SET_EFFECT_SUSPENDED: {
1272 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1273 ALOGV("AudioCommandThread() processing set effect suspended");
1274 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1275 if (af != 0) {
1276 mLock.unlock();
1277 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1278 mLock.lock();
1279 }
1280 } break;
Mikhail Naganov3de4f262020-03-06 17:02:19 -08001281 case AUDIO_MODULES_UPDATE: {
1282 ALOGV("AudioCommandThread() processing audio modules update");
1283 svc = mService.promote();
1284 if (svc == 0) {
1285 break;
1286 }
1287 mLock.unlock();
1288 svc->doOnNewAudioModulesAvailable();
1289 mLock.lock();
1290 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001291
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001293 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001295 {
1296 Mutex::Autolock _l(command->mLock);
1297 if (command->mWaitStatus) {
1298 command->mWaitStatus = false;
1299 command->mCond.signal();
1300 }
1301 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001302 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001303 // release mLock before releasing strong reference on the service as
1304 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1305 // acquires mLock.
1306 mLock.unlock();
1307 svc.clear();
1308 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 } else {
1310 waitTime = mAudioCommands[0]->mTime - curTime;
1311 break;
1312 }
1313 }
Zach Janga754b4f2015-10-27 01:29:34 +00001314
1315 // release delayed commands wake lock if the queue is empty
1316 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001317 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001318 }
1319
1320 // At this stage we have either an empty command queue or the first command in the queue
1321 // has a finite delay. So unless we are exiting it is safe to wait.
1322 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001323 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001324 if (waitTime == -1) {
1325 mWaitWorkCV.wait(mLock);
1326 } else {
1327 mWaitWorkCV.waitRelative(mLock, waitTime);
1328 }
Eric Laurent59a89232014-06-08 14:14:17 -07001329 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001331 // release delayed commands wake lock before quitting
1332 if (!mAudioCommands.isEmpty()) {
1333 release_wake_lock(mName.string());
1334 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335 mLock.unlock();
1336 return false;
1337}
1338
1339status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1340{
1341 const size_t SIZE = 256;
1342 char buffer[SIZE];
1343 String8 result;
1344
1345 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1346 result.append(buffer);
1347 write(fd, result.string(), result.size());
1348
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001349 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 if (!locked) {
1351 String8 result2(kCmdDeadlockedString);
1352 write(fd, result2.string(), result2.size());
1353 }
1354
1355 snprintf(buffer, SIZE, "- Commands:\n");
1356 result = String8(buffer);
1357 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001358 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 mAudioCommands[i]->dump(buffer, SIZE);
1360 result.append(buffer);
1361 }
1362 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001363 if (mLastCommand != 0) {
1364 mLastCommand->dump(buffer, SIZE);
1365 result.append(buffer);
1366 } else {
1367 result.append(" none\n");
1368 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369
1370 write(fd, result.string(), result.size());
1371
1372 if (locked) mLock.unlock();
1373
1374 return NO_ERROR;
1375}
1376
Glenn Kastenfff6d712012-01-12 16:38:12 -08001377status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001378 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001379 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001380 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381{
Eric Laurent0ede8922014-05-09 18:04:42 -07001382 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001384 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385 data->mStream = stream;
1386 data->mVolume = volume;
1387 data->mIO = output;
1388 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001389 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001390 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001391 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001392 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393}
1394
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001395status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001396 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001397 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398{
Eric Laurent0ede8922014-05-09 18:04:42 -07001399 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001401 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001403 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001405 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001406 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001408 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409}
1410
1411status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1412{
Eric Laurent0ede8922014-05-09 18:04:42 -07001413 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001415 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416 data->mVolume = volume;
1417 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001418 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001419 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001420 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421}
1422
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001423void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1424 audio_session_t sessionId,
1425 bool suspended)
1426{
1427 sp<AudioCommand> command = new AudioCommand();
1428 command->mCommand = SET_EFFECT_SUSPENDED;
1429 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1430 data->mEffectId = effectId;
1431 data->mSessionId = sessionId;
1432 data->mSuspended = suspended;
1433 command->mParam = data;
1434 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1435 effectId, sessionId, suspended);
1436 sendCommand(command);
1437}
1438
1439
Eric Laurentd7fe0862018-07-14 16:48:01 -07001440void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001441{
Eric Laurent0ede8922014-05-09 18:04:42 -07001442 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001443 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001444 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001445 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001446 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001447 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001448 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001449}
1450
Eric Laurentd7fe0862018-07-14 16:48:01 -07001451void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001452{
Eric Laurent0ede8922014-05-09 18:04:42 -07001453 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001454 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001455 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001456 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001457 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001458 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001459 sendCommand(command);
1460}
1461
Eric Laurent951f4552014-05-20 10:48:17 -07001462status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1463 const struct audio_patch *patch,
1464 audio_patch_handle_t *handle,
1465 int delayMs)
1466{
1467 status_t status = NO_ERROR;
1468
1469 sp<AudioCommand> command = new AudioCommand();
1470 command->mCommand = CREATE_AUDIO_PATCH;
1471 CreateAudioPatchData *data = new CreateAudioPatchData();
1472 data->mPatch = *patch;
1473 data->mHandle = *handle;
1474 command->mParam = data;
1475 command->mWaitStatus = true;
1476 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1477 status = sendCommand(command, delayMs);
1478 if (status == NO_ERROR) {
1479 *handle = data->mHandle;
1480 }
1481 return status;
1482}
1483
1484status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1485 int delayMs)
1486{
1487 sp<AudioCommand> command = new AudioCommand();
1488 command->mCommand = RELEASE_AUDIO_PATCH;
1489 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1490 data->mHandle = handle;
1491 command->mParam = data;
1492 command->mWaitStatus = true;
1493 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1494 return sendCommand(command, delayMs);
1495}
1496
Eric Laurentb52c1522014-05-20 11:27:36 -07001497void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1498{
1499 sp<AudioCommand> command = new AudioCommand();
1500 command->mCommand = UPDATE_AUDIOPORT_LIST;
1501 ALOGV("AudioCommandThread() adding update audio port list");
1502 sendCommand(command);
1503}
1504
1505void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1506{
1507 sp<AudioCommand>command = new AudioCommand();
1508 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1509 ALOGV("AudioCommandThread() adding update audio patch list");
1510 sendCommand(command);
1511}
1512
François Gaffiecfe17322018-11-07 13:41:29 +01001513void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1514 int flags)
1515{
1516 sp<AudioCommand>command = new AudioCommand();
1517 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1518 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1519 data->mGroup = group;
1520 data->mFlags = flags;
1521 command->mParam = data;
1522 ALOGV("AudioCommandThread() adding audio volume group changed");
1523 sendCommand(command);
1524}
1525
Eric Laurente1715a42014-05-20 11:30:42 -07001526status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1527 const struct audio_port_config *config, int delayMs)
1528{
1529 sp<AudioCommand> command = new AudioCommand();
1530 command->mCommand = SET_AUDIOPORT_CONFIG;
1531 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1532 data->mConfig = *config;
1533 command->mParam = data;
1534 command->mWaitStatus = true;
1535 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1536 return sendCommand(command, delayMs);
1537}
1538
Jean-Michel Trivide801052015-04-14 19:10:14 -07001539void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001540 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001541{
1542 sp<AudioCommand> command = new AudioCommand();
1543 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1544 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1545 data->mRegId = regId;
1546 data->mState = state;
1547 command->mParam = data;
1548 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1549 regId.string(), state);
1550 sendCommand(command);
1551}
1552
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001553void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001554 int event,
1555 const record_client_info_t *clientInfo,
1556 const audio_config_base_t *clientConfig,
1557 std::vector<effect_descriptor_t> clientEffects,
1558 const audio_config_base_t *deviceConfig,
1559 std::vector<effect_descriptor_t> effects,
1560 audio_patch_handle_t patchHandle,
1561 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001562{
1563 sp<AudioCommand>command = new AudioCommand();
1564 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1565 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1566 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001567 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001568 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001569 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001570 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001571 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001572 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001573 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001574 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001575 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1576 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001577 sendCommand(command);
1578}
1579
Mikhail Naganov3de4f262020-03-06 17:02:19 -08001580void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand()
1581{
1582 sp<AudioCommand> command = new AudioCommand();
1583 command->mCommand = AUDIO_MODULES_UPDATE;
1584 sendCommand(command);
1585}
1586
Eric Laurent0ede8922014-05-09 18:04:42 -07001587status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1588{
1589 {
1590 Mutex::Autolock _l(mLock);
1591 insertCommand_l(command, delayMs);
1592 mWaitWorkCV.signal();
1593 }
1594 Mutex::Autolock _l(command->mLock);
1595 while (command->mWaitStatus) {
1596 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1597 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1598 command->mStatus = TIMED_OUT;
1599 command->mWaitStatus = false;
1600 }
1601 }
1602 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001603}
1604
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001606void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001608 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001609 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610 command->mTime = systemTime() + milliseconds(delayMs);
1611
1612 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001613 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1615 }
1616
1617 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001618 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001619 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1621 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001622
1623 // create audio patch or release audio patch commands are equivalent
1624 // with regard to filtering
1625 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1626 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1627 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1628 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1629 continue;
1630 }
1631 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632
1633 switch (command->mCommand) {
1634 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001635 ParametersData *data = (ParametersData *)command->mParam.get();
1636 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001637 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001638 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001639 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1641 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1642 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001643 String8 key;
1644 String8 value;
1645 param.getAt(j, key, value);
1646 for (size_t k = 0; k < param2.size(); k++) {
1647 String8 key2;
1648 String8 value2;
1649 param2.getAt(k, key2, value2);
1650 if (key2 == key) {
1651 param2.remove(key2);
1652 ALOGV("Filtering out parameter %s", key2.string());
1653 break;
1654 }
1655 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001656 }
1657 // if all keys have been filtered out, remove the command.
1658 // otherwise, update the key value pairs
1659 if (param2.size() == 0) {
1660 removedCommands.add(command2);
1661 } else {
1662 data2->mKeyValuePairs = param2.toString();
1663 }
Eric Laurent21e54562013-09-23 12:08:05 -07001664 command->mTime = command2->mTime;
1665 // force delayMs to non 0 so that code below does not request to wait for
1666 // command status as the command is now delayed
1667 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 } break;
1669
1670 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001671 VolumeData *data = (VolumeData *)command->mParam.get();
1672 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001673 if (data->mIO != data2->mIO) break;
1674 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001675 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001676 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001677 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001678 command->mTime = command2->mTime;
1679 // force delayMs to non 0 so that code below does not request to wait for
1680 // command status as the command is now delayed
1681 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001683
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001684 case SET_VOICE_VOLUME: {
1685 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1686 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1687 ALOGV("Filtering out voice volume command value %f replaced by %f",
1688 data2->mVolume, data->mVolume);
1689 removedCommands.add(command2);
1690 command->mTime = command2->mTime;
1691 // force delayMs to non 0 so that code below does not request to wait for
1692 // command status as the command is now delayed
1693 delayMs = 1;
1694 } break;
1695
Eric Laurente45b48a2014-09-04 16:40:57 -07001696 case CREATE_AUDIO_PATCH:
1697 case RELEASE_AUDIO_PATCH: {
1698 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001699 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001700 if (command->mCommand == CREATE_AUDIO_PATCH) {
1701 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001702 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001703 } else {
1704 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001705 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001706 }
1707 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001708 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001709 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1710 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001711 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001712 } else {
1713 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001714 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001715 }
1716 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001717 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1718 same output. */
1719 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1720 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1721 bool isOutputDiff = false;
1722 if (patch.num_sources == patch2.num_sources) {
1723 for (unsigned count = 0; count < patch.num_sources; count++) {
1724 if (patch.sources[count].id != patch2.sources[count].id) {
1725 isOutputDiff = true;
1726 break;
1727 }
1728 }
1729 if (isOutputDiff)
1730 break;
1731 }
1732 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001733 ALOGV("Filtering out %s audio patch command for handle %d",
1734 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1735 removedCommands.add(command2);
1736 command->mTime = command2->mTime;
1737 // force delayMs to non 0 so that code below does not request to wait for
1738 // command status as the command is now delayed
1739 delayMs = 1;
1740 } break;
1741
Jean-Michel Trivide801052015-04-14 19:10:14 -07001742 case DYN_POLICY_MIX_STATE_UPDATE: {
1743
1744 } break;
1745
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001746 case RECORDING_CONFIGURATION_UPDATE: {
1747
1748 } break;
1749
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 default:
1751 break;
1752 }
1753 }
1754
1755 // remove filtered commands
1756 for (size_t j = 0; j < removedCommands.size(); j++) {
1757 // removed commands always have time stamps greater than current command
1758 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001759 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001760 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001761 mAudioCommands.removeAt(k);
1762 break;
1763 }
1764 }
1765 }
1766 removedCommands.clear();
1767
Eric Laurentaa79bef2015-01-15 14:33:51 -08001768 // Disable wait for status if delay is not 0.
1769 // Except for create audio patch command because the returned patch handle
1770 // is needed by audio policy manager
1771 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001772 command->mWaitStatus = false;
1773 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001774
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001776 ALOGV("inserting command: %d at index %zd, num commands %zu",
1777 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778 mAudioCommands.insertAt(command, i + 1);
1779}
1780
1781void AudioPolicyService::AudioCommandThread::exit()
1782{
Steve Block3856b092011-10-20 11:56:00 +01001783 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784 {
1785 AutoMutex _l(mLock);
1786 requestExit();
1787 mWaitWorkCV.signal();
1788 }
Zach Janga754b4f2015-10-27 01:29:34 +00001789 // Note that we can call it from the thread loop if all other references have been released
1790 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001791 requestExitAndWait();
1792}
1793
1794void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1795{
1796 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1797 mCommand,
1798 (int)ns2s(mTime),
1799 (int)ns2ms(mTime)%1000,
1800 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001801 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001802}
1803
Dima Zavinfce7a472011-04-19 22:30:36 -07001804/******* helpers for the service_ops callbacks defined below *********/
1805void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1806 const char *keyValuePairs,
1807 int delayMs)
1808{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001809 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001810 delayMs);
1811}
1812
1813int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1814 float volume,
1815 audio_io_handle_t output,
1816 int delayMs)
1817{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001818 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001819 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001820}
1821
Dima Zavinfce7a472011-04-19 22:30:36 -07001822int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1823{
1824 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1825}
1826
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001827void AudioPolicyService::setEffectSuspended(int effectId,
1828 audio_session_t sessionId,
1829 bool suspended)
1830{
1831 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1832}
1833
Mikhail Naganov3de4f262020-03-06 17:02:19 -08001834void AudioPolicyService::onNewAudioModulesAvailable()
1835{
Mikhail Naganova30ec142020-03-24 09:32:34 -07001836 mOutputCommandThread->audioModulesUpdateCommand();
Mikhail Naganov3de4f262020-03-06 17:02:19 -08001837}
1838
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001839
Dima Zavinfce7a472011-04-19 22:30:36 -07001840extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001841audio_module_handle_t aps_load_hw_module(void *service __unused,
1842 const char *name);
1843audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001844 audio_devices_t *pDevices,
1845 uint32_t *pSamplingRate,
1846 audio_format_t *pFormat,
1847 audio_channel_mask_t *pChannelMask,
1848 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001849 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850
Eric Laurent2d388ec2014-03-07 13:25:54 -08001851audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001852 audio_module_handle_t module,
1853 audio_devices_t *pDevices,
1854 uint32_t *pSamplingRate,
1855 audio_format_t *pFormat,
1856 audio_channel_mask_t *pChannelMask,
1857 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001858 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001859 const audio_offload_info_t *offloadInfo);
1860audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001861 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001862 audio_io_handle_t output2);
1863int aps_close_output(void *service __unused, audio_io_handle_t output);
1864int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1865int aps_restore_output(void *service __unused, audio_io_handle_t output);
1866audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001867 audio_devices_t *pDevices,
1868 uint32_t *pSamplingRate,
1869 audio_format_t *pFormat,
1870 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001871 audio_in_acoustics_t acoustics __unused);
1872audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001873 audio_module_handle_t module,
1874 audio_devices_t *pDevices,
1875 uint32_t *pSamplingRate,
1876 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001877 audio_channel_mask_t *pChannelMask);
1878int aps_close_input(void *service __unused, audio_io_handle_t input);
1879int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001880int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001881 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001882 audio_io_handle_t dst_output);
1883char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1884 const char *keys);
1885void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1886 const char *kv_pairs, int delay_ms);
1887int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001888 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001889 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001890int aps_set_voice_volume(void *service, float volume, int delay_ms);
1891};
Dima Zavinfce7a472011-04-19 22:30:36 -07001892
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001893} // namespace android