blob: e3c72aeb80cb663631a0156186424480ed7a6115 [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/ActivityManager.h>
33#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
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()
Eric Laurentdce54a12014-03-10 12:19:46 -070061 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070062 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070064}
65
66void AudioPolicyService::onFirstRef()
67{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070068 {
69 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080070
Eric Laurent8b1e80b2014-10-07 09:08:47 -070071 // start audio commands thread
72 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
73 // start output activity command thread
74 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070075
Eric Laurent8b1e80b2014-10-07 09:08:47 -070076 mAudioPolicyClient = new AudioPolicyClient(this);
77 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070078 }
bryant_liuba2b4392014-06-11 16:49:30 +080079 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
81 {
82 Mutex::Autolock _l(mLock);
83 mAudioPolicyEffects = audioPolicyEffects;
84 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080085
86 mUidPolicy = new UidPolicy(this);
87 mUidPolicy->registerSelf();
Michael Groovercfd28302018-12-11 19:16:46 -080088
89 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
90 mSensorPrivacyPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070091}
92
93AudioPolicyService::~AudioPolicyService()
94{
Mathias Agopian65ab4712010-07-14 17:59:35 -070095 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070096 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070097
Eric Laurentf269b8e2014-06-09 20:01:29 -070098 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070099 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700100
101 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800102 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800103
104 mUidPolicy->unregisterSelf();
105 mUidPolicy.clear();
Michael Groovercfd28302018-12-11 19:16:46 -0800106
107 mSensorPrivacyPolicy->unregisterSelf();
108 mSensorPrivacyPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700109}
110
111// A notification client is always registered by AudioSystem when the client process
112// connects to AudioPolicyService.
113void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
114{
Eric Laurent12590252015-08-21 18:40:20 -0700115 if (client == 0) {
116 ALOGW("%s got NULL client", __FUNCTION__);
117 return;
118 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800119 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700120
121 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800122 pid_t pid = IPCThreadState::self()->getCallingPid();
123 int64_t token = ((int64_t)uid<<32) | pid;
124
125 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700126 sp<NotificationClient> notificationClient = new NotificationClient(this,
127 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800128 uid,
129 pid);
130 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700131
luochaojiang908c7d72018-06-21 14:58:04 +0800132 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
Marco Nelissenf8880202014-11-14 07:58:25 -0800134 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700135 binder->linkToDeath(notificationClient);
136 }
137}
138
Eric Laurente8726fe2015-06-26 09:39:24 -0700139void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
140{
141 Mutex::Autolock _l(mNotificationClientsLock);
142
143 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800144 pid_t pid = IPCThreadState::self()->getCallingPid();
145 int64_t token = ((int64_t)uid<<32) | pid;
146
147 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700148 return;
149 }
luochaojiang908c7d72018-06-21 14:58:04 +0800150 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700151}
152
François Gaffiecfe17322018-11-07 13:41:29 +0100153void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled)
154{
155 Mutex::Autolock _l(mNotificationClientsLock);
156
157 uid_t uid = IPCThreadState::self()->getCallingUid();
158 pid_t pid = IPCThreadState::self()->getCallingPid();
159 int64_t token = ((int64_t)uid<<32) | pid;
160
161 if (mNotificationClients.indexOfKey(token) < 0) {
162 return;
163 }
164 mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled);
165}
166
Eric Laurentb52c1522014-05-20 11:27:36 -0700167// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800168void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700169{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800170 {
171 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800172 int64_t token = ((int64_t)uid<<32) | pid;
173 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800174 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800175 {
176 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800177 bool hasSameUid = false;
178 for (size_t i = 0; i < mNotificationClients.size(); i++) {
179 if (mNotificationClients.valueAt(i)->uid() == uid) {
180 hasSameUid = true;
181 break;
182 }
183 }
184 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700185 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700186 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700187 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800188 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700189}
190
191void AudioPolicyService::onAudioPortListUpdate()
192{
193 mOutputCommandThread->updateAudioPortListCommand();
194}
195
196void AudioPolicyService::doOnAudioPortListUpdate()
197{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800198 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700199 for (size_t i = 0; i < mNotificationClients.size(); i++) {
200 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
201 }
202}
203
204void AudioPolicyService::onAudioPatchListUpdate()
205{
206 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207}
208
Eric Laurentb52c1522014-05-20 11:27:36 -0700209void AudioPolicyService::doOnAudioPatchListUpdate()
210{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800211 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700212 for (size_t i = 0; i < mNotificationClients.size(); i++) {
213 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
214 }
215}
216
François Gaffiecfe17322018-11-07 13:41:29 +0100217void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags)
218{
219 mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags);
220}
221
222void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags)
223{
224 Mutex::Autolock _l(mNotificationClientsLock);
225 for (size_t i = 0; i < mNotificationClients.size(); i++) {
226 mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags);
227 }
228}
229
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700230void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700231{
232 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
233 regId.string(), state);
234 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
235}
236
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700237void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700238{
239 Mutex::Autolock _l(mNotificationClientsLock);
240 for (size_t i = 0; i < mNotificationClients.size(); i++) {
241 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
242 }
243}
244
Eric Laurenta9f86652018-11-28 17:23:11 -0800245void AudioPolicyService::onRecordingConfigurationUpdate(
246 int event,
247 const record_client_info_t *clientInfo,
248 const audio_config_base_t *clientConfig,
249 std::vector<effect_descriptor_t> clientEffects,
250 const audio_config_base_t *deviceConfig,
251 std::vector<effect_descriptor_t> effects,
252 audio_patch_handle_t patchHandle,
253 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800254{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800255 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800256 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800257}
258
Eric Laurenta9f86652018-11-28 17:23:11 -0800259void AudioPolicyService::doOnRecordingConfigurationUpdate(
260 int event,
261 const record_client_info_t *clientInfo,
262 const audio_config_base_t *clientConfig,
263 std::vector<effect_descriptor_t> clientEffects,
264 const audio_config_base_t *deviceConfig,
265 std::vector<effect_descriptor_t> effects,
266 audio_patch_handle_t patchHandle,
267 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800268{
269 Mutex::Autolock _l(mNotificationClientsLock);
270 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800271 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800272 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800273 }
274}
275
276status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
277 audio_patch_handle_t *handle,
278 int delayMs)
279{
280 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
281}
282
283status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
284 int delayMs)
285{
286 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
287}
288
Eric Laurente1715a42014-05-20 11:30:42 -0700289status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
290 int delayMs)
291{
292 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
293}
294
Eric Laurentb52c1522014-05-20 11:27:36 -0700295AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
296 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800297 uid_t uid,
298 pid_t pid)
299 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
François Gaffiecfe17322018-11-07 13:41:29 +0100300 mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700301{
302}
303
304AudioPolicyService::NotificationClient::~NotificationClient()
305{
306}
307
308void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
309{
310 sp<NotificationClient> keep(this);
311 sp<AudioPolicyService> service = mService.promote();
312 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800313 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700314 }
315}
316
317void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
318{
Eric Laurente8726fe2015-06-26 09:39:24 -0700319 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700320 mAudioPolicyServiceClient->onAudioPortListUpdate();
321 }
322}
323
324void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
325{
Eric Laurente8726fe2015-06-26 09:39:24 -0700326 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700327 mAudioPolicyServiceClient->onAudioPatchListUpdate();
328 }
329}
Eric Laurent57dae992011-07-24 13:36:09 -0700330
François Gaffiecfe17322018-11-07 13:41:29 +0100331void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group,
332 int flags)
333{
334 if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) {
335 mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags);
336 }
337}
338
339
Jean-Michel Trivide801052015-04-14 19:10:14 -0700340void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700341 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700342{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700343 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800344 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
345 }
346}
347
348void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800349 int event,
350 const record_client_info_t *clientInfo,
351 const audio_config_base_t *clientConfig,
352 std::vector<effect_descriptor_t> clientEffects,
353 const audio_config_base_t *deviceConfig,
354 std::vector<effect_descriptor_t> effects,
355 audio_patch_handle_t patchHandle,
356 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800357{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700358 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800359 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -0800360 clientConfig, clientEffects, deviceConfig, effects, patchHandle, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700361 }
362}
363
Eric Laurente8726fe2015-06-26 09:39:24 -0700364void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
365{
366 mAudioPortCallbacksEnabled = enabled;
367}
368
François Gaffiecfe17322018-11-07 13:41:29 +0100369void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled)
370{
371 mAudioVolumeGroupCallbacksEnabled = enabled;
372}
Eric Laurente8726fe2015-06-26 09:39:24 -0700373
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700375 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700376 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377}
378
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700379static bool dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380{
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700381 status_t err = mutex.timedLock(kDumpLockTimeoutNs);
382 return err == NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383}
384
385status_t AudioPolicyService::dumpInternals(int fd)
386{
387 const size_t SIZE = 256;
388 char buffer[SIZE];
389 String8 result;
390
Eric Laurentdce54a12014-03-10 12:19:46 -0700391 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 result.append(buffer);
393 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
394 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
396 write(fd, result.string(), result.size());
397 return NO_ERROR;
398}
399
Eric Laurente8c8b432018-10-17 10:08:02 -0700400void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800401{
Eric Laurente8c8b432018-10-17 10:08:02 -0700402 Mutex::Autolock _l(mLock);
403 updateUidStates_l();
404}
405
406void AudioPolicyService::updateUidStates_l()
407{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800408// Go over all active clients and allow capture (does not force silence) in the
409// following cases:
Eric Laurent1ff16a72019-03-14 18:35:04 -0700410// Another client in the same UID has already been allowed to capture
411// OR The client is the assistant
Eric Laurenta46bedb2018-12-07 18:01:26 -0800412// AND an accessibility service is on TOP
413// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700414// OR uses VOICE_RECOGNITION AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800415// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700416// AND there is no active privacy sensitive capture or call
417// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800418// OR The client is an accessibility service
Eric Laurenta171e352019-05-07 13:04:45 -0700419// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800420// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700421// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurenta171e352019-05-07 13:04:45 -0700422// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800423// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700424// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700425// AND there is no active privacy sensitive capture or call
426// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800427
428 sp<AudioRecordClient> topActive;
429 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800430 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700431
Eric Laurenta46bedb2018-12-07 18:01:26 -0800432 nsecs_t topStartNs = 0;
433 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800434 nsecs_t latestSensitiveStartNs = 0;
435 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
436 bool isAssistantOnTop = false;
437 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800439
Michael Groovercfd28302018-12-11 19:16:46 -0800440 // if Sensor Privacy is enabled then all recordings should be silenced.
441 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
442 silenceAllRecordings_l();
443 return;
444 }
445
Eric Laurente8c8b432018-10-17 10:08:02 -0700446 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
447 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700448 if (!current->active) {
449 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800450 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700451
452 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
453 // clients which app is in IDLE state are not eligible for top active or
454 // latest active
455 if (appState == APP_STATE_IDLE) {
456 continue;
457 }
458
459 if (appState == APP_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800460 if (current->startTimeNs > topStartNs) {
461 topActive = current;
462 topStartNs = current->startTimeNs;
463 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800464 if (mUidPolicy->isAssistantUid(current->uid)) {
465 isAssistantOnTop = true;
466 }
467 }
468 if (current->startTimeNs > latestStartNs) {
469 latestActive = current;
470 latestStartNs = current->startTimeNs;
471 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700472 if (isPrivacySensitiveSource(current->attributes.source)) {
473 if (current->startTimeNs > latestSensitiveStartNs) {
474 latestSensitiveActive = current;
475 latestSensitiveStartNs = current->startTimeNs;
476 }
477 isSensitiveActive = true;
478 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800479 }
480
Eric Laurent1ff16a72019-03-14 18:35:04 -0700481 // if no active client with UI on Top, consider latest active as top
482 if (topActive == nullptr) {
483 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800484 }
485
Eric Laurent1ff16a72019-03-14 18:35:04 -0700486 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800487
Eric Laurent4eb58f12018-12-07 16:41:02 -0800488 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
489 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700490 if (!current->active) {
491 continue;
492 }
493
494 // keep capture allowed if another client with the same UID has already
495 // been allowed to capture
496 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
497 != enabledUids.end()) {
498 continue;
499 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800500
501 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700502 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
503 bool isLatestSensitive = latestSensitiveActive == nullptr ?
504 false : current->uid == latestSensitiveActive->uid;
505
506 // By default allow capture if:
507 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700508 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700509 // AND there is no active privacy sensitive capture or call
510 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
511 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700512 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700513 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
514 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800515
516 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700517 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
518 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800519 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700520 // For assistant allow capture if:
521 // An accessibility service is on TOP
522 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700523 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700524 // OR uses HOTWORD
525 // AND there is no active privacy sensitive capture or call
526 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800527 if (isA11yOnTop) {
528 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700529 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800530 }
531 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700532 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700533 source == AUDIO_SOURCE_HOTWORD) &&
534 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
535 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800536 }
537 }
538 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700539 // For accessibility service allow capture if:
Eric Laurenta171e352019-05-07 13:04:45 -0700540 // Is on TOP
Eric Laurent1ff16a72019-03-14 18:35:04 -0700541 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700542 if (isA11yOnTop &&
Eric Laurent1ff16a72019-03-14 18:35:04 -0700543 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
544 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800545 }
546 }
547 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700548 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
549 APP_STATE_IDLE);
550 if (allowCapture) {
551 enabledUids.push_back(current->uid);
552 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700553 }
554}
555
Michael Groovercfd28302018-12-11 19:16:46 -0800556void AudioPolicyService::silenceAllRecordings_l() {
557 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
558 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700559 if (!isVirtualSource(current->attributes.source)) {
560 setAppState_l(current->uid, APP_STATE_IDLE);
561 }
Michael Groovercfd28302018-12-11 19:16:46 -0800562 }
563}
564
Eric Laurente8c8b432018-10-17 10:08:02 -0700565/* static */
566app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700567
568 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700569 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700570 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
571 // include persistent services
572 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700573 }
574 return APP_STATE_FOREGROUND;
575}
576
Eric Laurent4eb58f12018-12-07 16:41:02 -0800577/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800578bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
579{
580 switch (source) {
581 case AUDIO_SOURCE_CAMCORDER:
582 case AUDIO_SOURCE_VOICE_COMMUNICATION:
583 return true;
584 default:
585 break;
586 }
587 return false;
588}
589
590/* static */
591bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800592{
593 switch (source) {
594 case AUDIO_SOURCE_VOICE_UPLINK:
595 case AUDIO_SOURCE_VOICE_DOWNLINK:
596 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800597 case AUDIO_SOURCE_REMOTE_SUBMIX:
598 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800599 return true;
600 default:
601 break;
602 }
603 return false;
604}
605
Eric Laurente8c8b432018-10-17 10:08:02 -0700606void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
607{
608 AutoCallerClear acc;
609
610 if (mAudioPolicyManager) {
611 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700612 }
613 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
614 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700615 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700616 af->setRecordSilenced(uid, silenced);
617 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800618}
619
Glenn Kasten0f11b512014-01-31 16:18:54 -0800620status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621{
Glenn Kasten44deb052012-02-05 18:09:08 -0800622 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 dumpPermissionDenial(fd);
624 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700625 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 if (!locked) {
627 String8 result(kDeadlockedString);
628 write(fd, result.string(), result.size());
629 }
630
631 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800632 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 mAudioCommandThread->dump(fd);
634 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635
Eric Laurentdce54a12014-03-10 12:19:46 -0700636 if (mAudioPolicyManager) {
637 mAudioPolicyManager->dump(fd);
638 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639
Kevin Rocard8be94972019-02-22 13:26:25 -0800640 mPackageManager.dump(fd);
641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 if (locked) mLock.unlock();
643 }
644 return NO_ERROR;
645}
646
647status_t AudioPolicyService::dumpPermissionDenial(int fd)
648{
649 const size_t SIZE = 256;
650 char buffer[SIZE];
651 String8 result;
652 snprintf(buffer, SIZE, "Permission Denial: "
653 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
654 IPCThreadState::self()->getCallingPid(),
655 IPCThreadState::self()->getCallingUid());
656 result.append(buffer);
657 write(fd, result.string(), result.size());
658 return NO_ERROR;
659}
660
661status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800662 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
663 switch (code) {
664 case SHELL_COMMAND_TRANSACTION: {
665 int in = data.readFileDescriptor();
666 int out = data.readFileDescriptor();
667 int err = data.readFileDescriptor();
668 int argc = data.readInt32();
669 Vector<String16> args;
670 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
671 args.add(data.readString16());
672 }
673 sp<IBinder> unusedCallback;
674 sp<IResultReceiver> resultReceiver;
675 status_t status;
676 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
677 return status;
678 }
679 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
680 return status;
681 }
682 status = shellCommand(in, out, err, args);
683 if (resultReceiver != nullptr) {
684 resultReceiver->send(status);
685 }
686 return NO_ERROR;
687 }
688 }
689
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 return BnAudioPolicyService::onTransact(code, data, reply, flags);
691}
692
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800693// ------------------- Shell command implementation -------------------
694
695// NOTE: This is a remote API - make sure all args are validated
696status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
697 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
698 return PERMISSION_DENIED;
699 }
700 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
701 return BAD_VALUE;
702 }
703 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
704 return handleSetUidState(args, err);
705 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
706 return handleResetUidState(args, err);
707 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
708 return handleGetUidState(args, out, err);
709 } else if (args.size() == 1 && args[0] == String16("help")) {
710 printHelp(out);
711 return NO_ERROR;
712 }
713 printHelp(err);
714 return BAD_VALUE;
715}
716
717status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
718 PermissionController pc;
719 int uid = pc.getPackageUid(args[1], 0);
720 if (uid <= 0) {
721 ALOGE("Unknown package: '%s'", String8(args[1]).string());
722 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
723 return BAD_VALUE;
724 }
725 bool active = false;
726 if (args[2] == String16("active")) {
727 active = true;
728 } else if ((args[2] != String16("idle"))) {
729 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
730 return BAD_VALUE;
731 }
732 mUidPolicy->addOverrideUid(uid, active);
733 return NO_ERROR;
734}
735
736status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
737 PermissionController pc;
738 int uid = pc.getPackageUid(args[1], 0);
739 if (uid < 0) {
740 ALOGE("Unknown package: '%s'", String8(args[1]).string());
741 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
742 return BAD_VALUE;
743 }
744 mUidPolicy->removeOverrideUid(uid);
745 return NO_ERROR;
746}
747
748status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
749 PermissionController pc;
750 int uid = pc.getPackageUid(args[1], 0);
751 if (uid < 0) {
752 ALOGE("Unknown package: '%s'", String8(args[1]).string());
753 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
754 return BAD_VALUE;
755 }
756 if (mUidPolicy->isUidActive(uid)) {
757 return dprintf(out, "active\n");
758 } else {
759 return dprintf(out, "idle\n");
760 }
761}
762
763status_t AudioPolicyService::printHelp(int out) {
764 return dprintf(out, "Audio policy service commands:\n"
765 " get-uid-state <PACKAGE> gets the uid state\n"
766 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
767 " reset-uid-state <PACKAGE> clears the uid state override\n"
768 " help print this message\n");
769}
770
771// ----------- AudioPolicyService::UidPolicy implementation ----------
772
773void AudioPolicyService::UidPolicy::registerSelf() {
774 ActivityManager am;
775 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
776 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700777 | ActivityManager::UID_OBSERVER_ACTIVE
778 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800779 ActivityManager::PROCESS_STATE_UNKNOWN,
780 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700781 status_t res = am.linkToDeath(this);
782 if (!res) {
783 Mutex::Autolock _l(mLock);
784 mObserverRegistered = true;
785 } else {
786 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800787
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700788 am.unregisterUidObserver(this);
789 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800790}
791
792void AudioPolicyService::UidPolicy::unregisterSelf() {
793 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700794 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800795 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700796 Mutex::Autolock _l(mLock);
797 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800798}
799
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700800void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
801 Mutex::Autolock _l(mLock);
802 mCachedUids.clear();
803 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800804}
805
Eric Laurente8c8b432018-10-17 10:08:02 -0700806void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700807 bool needToReregister = false;
808 {
809 Mutex::Autolock _l(mLock);
810 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800811 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700812 if (needToReregister) {
813 // Looks like ActivityManager has died previously, attempt to re-register.
814 registerSelf();
815 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700816}
817
818bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
819 if (isServiceUid(uid)) return true;
820 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700821 {
822 Mutex::Autolock _l(mLock);
823 auto overrideIter = mOverrideUids.find(uid);
824 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700825 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700826 }
827 // In an absense of the ActivityManager, assume everything to be active.
828 if (!mObserverRegistered) return true;
829 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700830 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700831 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700832 }
833 }
834 ActivityManager am;
835 bool active = am.isUidActive(uid, String16("audioserver"));
836 {
837 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700838 mCachedUids.insert(std::pair<uid_t,
839 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
840 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700841 }
842 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800843}
844
Eric Laurente8c8b432018-10-17 10:08:02 -0700845int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
846 if (isServiceUid(uid)) {
847 return ActivityManager::PROCESS_STATE_TOP;
848 }
849 checkRegistered();
850 {
851 Mutex::Autolock _l(mLock);
852 auto overrideIter = mOverrideUids.find(uid);
853 if (overrideIter != mOverrideUids.end()) {
854 if (overrideIter->second.first) {
855 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
856 return overrideIter->second.second;
857 } else {
858 auto cacheIter = mCachedUids.find(uid);
859 if (cacheIter != mCachedUids.end()) {
860 return cacheIter->second.second;
861 }
862 }
863 }
864 return ActivityManager::PROCESS_STATE_UNKNOWN;
865 }
866 // In an absense of the ActivityManager, assume everything to be active.
867 if (!mObserverRegistered) {
868 return ActivityManager::PROCESS_STATE_TOP;
869 }
870 auto cacheIter = mCachedUids.find(uid);
871 if (cacheIter != mCachedUids.end()) {
872 if (cacheIter->second.first) {
873 return cacheIter->second.second;
874 } else {
875 return ActivityManager::PROCESS_STATE_UNKNOWN;
876 }
877 }
878 }
879 ActivityManager am;
880 bool active = am.isUidActive(uid, String16("audioserver"));
881 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
882 if (active) {
883 state = am.getUidProcessState(uid, String16("audioserver"));
884 }
885 {
886 Mutex::Autolock _l(mLock);
887 mCachedUids.insert(std::pair<uid_t,
888 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
889 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800890
Eric Laurente8c8b432018-10-17 10:08:02 -0700891 return state;
892}
893
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700894void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700895 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700896}
897
898void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700899 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700900}
901
902void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700903 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700904}
905
Eric Laurente8c8b432018-10-17 10:08:02 -0700906void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
907 int32_t procState,
908 int64_t procStateSeq __unused) {
909 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
910 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700911 }
912}
913
914void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700915 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
916}
917
918void AudioPolicyService::UidPolicy::notifyService() {
919 sp<AudioPolicyService> service = mService.promote();
920 if (service != nullptr) {
921 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700922 }
923}
924
Eric Laurente8c8b432018-10-17 10:08:02 -0700925void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
926 std::pair<bool, int>> *uids,
927 uid_t uid,
928 bool active,
929 int state,
930 bool insert) {
931 if (isServiceUid(uid)) {
932 return;
933 }
934 bool wasActive = isUidActive(uid);
935 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700936 {
937 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700938 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700939 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700940 if (wasActive != isUidActive(uid) || state != previousState) {
941 notifyService();
942 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700943}
944
Eric Laurente8c8b432018-10-17 10:08:02 -0700945void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
946 std::pair<bool, int>> *uids,
947 uid_t uid,
948 bool active,
949 int state,
950 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700951 auto it = uids->find(uid);
952 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700953 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700954 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
955 it->second.first = active;
956 }
957 if (it->second.first) {
958 it->second.second = state;
959 } else {
960 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
961 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700962 } else {
963 uids->erase(it);
964 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700965 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
966 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
967 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700968 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800969}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970
Eric Laurent4eb58f12018-12-07 16:41:02 -0800971bool AudioPolicyService::UidPolicy::isA11yOnTop() {
972 for (const auto &uid : mCachedUids) {
973 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
974 if (it == mA11yUids.end()) {
975 continue;
976 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800977 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
978 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800979 return true;
980 }
981 }
982 return false;
983}
984
Eric Laurentb78763e2018-10-17 10:08:02 -0700985bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
986{
987 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
988 return it != mA11yUids.end();
989}
990
Michael Groovercfd28302018-12-11 19:16:46 -0800991// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
992void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
993 SensorPrivacyManager spm;
994 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
995 spm.addSensorPrivacyListener(this);
996}
997
998void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
999 SensorPrivacyManager spm;
1000 spm.removeSensorPrivacyListener(this);
1001}
1002
1003bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1004 return mSensorPrivacyEnabled;
1005}
1006
1007binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1008 mSensorPrivacyEnabled = enabled;
1009 sp<AudioPolicyService> service = mService.promote();
1010 if (service != nullptr) {
1011 service->updateUidStates();
1012 }
1013 return binder::Status::ok();
1014}
1015
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1017
Eric Laurentbfb1b832013-01-07 09:53:42 -08001018AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1019 const wp<AudioPolicyService>& service)
1020 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022}
1023
1024
1025AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1026{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001027 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 release_wake_lock(mName.string());
1029 }
1030 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031}
1032
1033void AudioPolicyService::AudioCommandThread::onFirstRef()
1034{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001035 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036}
1037
1038bool AudioPolicyService::AudioCommandThread::threadLoop()
1039{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001040 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041
1042 mLock.lock();
1043 while (!exitPending())
1044 {
Eric Laurent59a89232014-06-08 14:14:17 -07001045 sp<AudioPolicyService> svc;
1046 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 nsecs_t curTime = systemTime();
1048 // commands are sorted by increasing time stamp: execute them from index 0 and up
1049 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001050 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001052 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053
1054 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001056 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001058 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001059 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001060 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1061 data->mVolume,
1062 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001063 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 }break;
1065 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001066 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001067 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1068 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001069 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001070 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001071 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001072 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001074 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001076 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001077 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001079 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001081 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001082 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001083 ALOGV("AudioCommandThread() processing stop output portId %d",
1084 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001085 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001086 if (svc == 0) {
1087 break;
1088 }
1089 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001090 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001091 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001092 }break;
1093 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001094 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001095 ALOGV("AudioCommandThread() processing release output portId %d",
1096 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001097 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001098 if (svc == 0) {
1099 break;
1100 }
1101 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001102 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001103 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001104 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001105 case CREATE_AUDIO_PATCH: {
1106 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1107 ALOGV("AudioCommandThread() processing create audio patch");
1108 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1109 if (af == 0) {
1110 command->mStatus = PERMISSION_DENIED;
1111 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001112 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001113 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001114 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001115 }
1116 } break;
1117 case RELEASE_AUDIO_PATCH: {
1118 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1119 ALOGV("AudioCommandThread() processing release audio patch");
1120 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1121 if (af == 0) {
1122 command->mStatus = PERMISSION_DENIED;
1123 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001124 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001125 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001126 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001127 }
1128 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001129 case UPDATE_AUDIOPORT_LIST: {
1130 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001131 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001132 if (svc == 0) {
1133 break;
1134 }
1135 mLock.unlock();
1136 svc->doOnAudioPortListUpdate();
1137 mLock.lock();
1138 }break;
1139 case UPDATE_AUDIOPATCH_LIST: {
1140 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001141 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001142 if (svc == 0) {
1143 break;
1144 }
1145 mLock.unlock();
1146 svc->doOnAudioPatchListUpdate();
1147 mLock.lock();
1148 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001149 case CHANGED_AUDIOVOLUMEGROUP: {
1150 AudioVolumeGroupData *data =
1151 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1152 ALOGV("AudioCommandThread() processing update audio volume group");
1153 svc = mService.promote();
1154 if (svc == 0) {
1155 break;
1156 }
1157 mLock.unlock();
1158 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1159 mLock.lock();
1160 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001161 case SET_AUDIOPORT_CONFIG: {
1162 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1163 ALOGV("AudioCommandThread() processing set port config");
1164 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1165 if (af == 0) {
1166 command->mStatus = PERMISSION_DENIED;
1167 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001168 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001169 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001170 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001171 }
1172 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001173 case DYN_POLICY_MIX_STATE_UPDATE: {
1174 DynPolicyMixStateUpdateData *data =
1175 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001176 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1177 data->mRegId.string(), data->mState);
1178 svc = mService.promote();
1179 if (svc == 0) {
1180 break;
1181 }
1182 mLock.unlock();
1183 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1184 mLock.lock();
1185 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001186 case RECORDING_CONFIGURATION_UPDATE: {
1187 RecordingConfigurationUpdateData *data =
1188 (RecordingConfigurationUpdateData *)command->mParam.get();
1189 ALOGV("AudioCommandThread() processing recording configuration update");
1190 svc = mService.promote();
1191 if (svc == 0) {
1192 break;
1193 }
1194 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001195 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001196 &data->mClientConfig, data->mClientEffects,
1197 &data->mDeviceConfig, data->mEffects,
1198 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001199 mLock.lock();
1200 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001201 case SET_EFFECT_SUSPENDED: {
1202 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1203 ALOGV("AudioCommandThread() processing set effect suspended");
1204 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1205 if (af != 0) {
1206 mLock.unlock();
1207 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1208 mLock.lock();
1209 }
1210 } break;
1211
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001213 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001215 {
1216 Mutex::Autolock _l(command->mLock);
1217 if (command->mWaitStatus) {
1218 command->mWaitStatus = false;
1219 command->mCond.signal();
1220 }
1221 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001222 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001223 // release mLock before releasing strong reference on the service as
1224 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1225 // acquires mLock.
1226 mLock.unlock();
1227 svc.clear();
1228 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229 } else {
1230 waitTime = mAudioCommands[0]->mTime - curTime;
1231 break;
1232 }
1233 }
Zach Janga754b4f2015-10-27 01:29:34 +00001234
1235 // release delayed commands wake lock if the queue is empty
1236 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001237 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001238 }
1239
1240 // At this stage we have either an empty command queue or the first command in the queue
1241 // has a finite delay. So unless we are exiting it is safe to wait.
1242 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001243 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001244 if (waitTime == -1) {
1245 mWaitWorkCV.wait(mLock);
1246 } else {
1247 mWaitWorkCV.waitRelative(mLock, waitTime);
1248 }
Eric Laurent59a89232014-06-08 14:14:17 -07001249 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001251 // release delayed commands wake lock before quitting
1252 if (!mAudioCommands.isEmpty()) {
1253 release_wake_lock(mName.string());
1254 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 mLock.unlock();
1256 return false;
1257}
1258
1259status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1260{
1261 const size_t SIZE = 256;
1262 char buffer[SIZE];
1263 String8 result;
1264
1265 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1266 result.append(buffer);
1267 write(fd, result.string(), result.size());
1268
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001269 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270 if (!locked) {
1271 String8 result2(kCmdDeadlockedString);
1272 write(fd, result2.string(), result2.size());
1273 }
1274
1275 snprintf(buffer, SIZE, "- Commands:\n");
1276 result = String8(buffer);
1277 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001278 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279 mAudioCommands[i]->dump(buffer, SIZE);
1280 result.append(buffer);
1281 }
1282 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001283 if (mLastCommand != 0) {
1284 mLastCommand->dump(buffer, SIZE);
1285 result.append(buffer);
1286 } else {
1287 result.append(" none\n");
1288 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289
1290 write(fd, result.string(), result.size());
1291
1292 if (locked) mLock.unlock();
1293
1294 return NO_ERROR;
1295}
1296
Glenn Kastenfff6d712012-01-12 16:38:12 -08001297status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001298 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001299 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001300 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001301{
Eric Laurent0ede8922014-05-09 18:04:42 -07001302 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001303 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001304 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 data->mStream = stream;
1306 data->mVolume = volume;
1307 data->mIO = output;
1308 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001309 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001310 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001311 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001312 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313}
1314
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001315status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001316 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001317 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318{
Eric Laurent0ede8922014-05-09 18:04:42 -07001319 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001321 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001323 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001325 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001327 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001328 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329}
1330
1331status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1332{
Eric Laurent0ede8922014-05-09 18:04:42 -07001333 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001335 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336 data->mVolume = volume;
1337 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001338 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001339 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001340 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341}
1342
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001343void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1344 audio_session_t sessionId,
1345 bool suspended)
1346{
1347 sp<AudioCommand> command = new AudioCommand();
1348 command->mCommand = SET_EFFECT_SUSPENDED;
1349 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1350 data->mEffectId = effectId;
1351 data->mSessionId = sessionId;
1352 data->mSuspended = suspended;
1353 command->mParam = data;
1354 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1355 effectId, sessionId, suspended);
1356 sendCommand(command);
1357}
1358
1359
Eric Laurentd7fe0862018-07-14 16:48:01 -07001360void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001361{
Eric Laurent0ede8922014-05-09 18:04:42 -07001362 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001363 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001364 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001365 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001366 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001367 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001368 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001369}
1370
Eric Laurentd7fe0862018-07-14 16:48:01 -07001371void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001372{
Eric Laurent0ede8922014-05-09 18:04:42 -07001373 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001374 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001375 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001376 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001377 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001378 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001379 sendCommand(command);
1380}
1381
Eric Laurent951f4552014-05-20 10:48:17 -07001382status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1383 const struct audio_patch *patch,
1384 audio_patch_handle_t *handle,
1385 int delayMs)
1386{
1387 status_t status = NO_ERROR;
1388
1389 sp<AudioCommand> command = new AudioCommand();
1390 command->mCommand = CREATE_AUDIO_PATCH;
1391 CreateAudioPatchData *data = new CreateAudioPatchData();
1392 data->mPatch = *patch;
1393 data->mHandle = *handle;
1394 command->mParam = data;
1395 command->mWaitStatus = true;
1396 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1397 status = sendCommand(command, delayMs);
1398 if (status == NO_ERROR) {
1399 *handle = data->mHandle;
1400 }
1401 return status;
1402}
1403
1404status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1405 int delayMs)
1406{
1407 sp<AudioCommand> command = new AudioCommand();
1408 command->mCommand = RELEASE_AUDIO_PATCH;
1409 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1410 data->mHandle = handle;
1411 command->mParam = data;
1412 command->mWaitStatus = true;
1413 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1414 return sendCommand(command, delayMs);
1415}
1416
Eric Laurentb52c1522014-05-20 11:27:36 -07001417void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1418{
1419 sp<AudioCommand> command = new AudioCommand();
1420 command->mCommand = UPDATE_AUDIOPORT_LIST;
1421 ALOGV("AudioCommandThread() adding update audio port list");
1422 sendCommand(command);
1423}
1424
1425void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1426{
1427 sp<AudioCommand>command = new AudioCommand();
1428 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1429 ALOGV("AudioCommandThread() adding update audio patch list");
1430 sendCommand(command);
1431}
1432
François Gaffiecfe17322018-11-07 13:41:29 +01001433void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1434 int flags)
1435{
1436 sp<AudioCommand>command = new AudioCommand();
1437 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1438 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1439 data->mGroup = group;
1440 data->mFlags = flags;
1441 command->mParam = data;
1442 ALOGV("AudioCommandThread() adding audio volume group changed");
1443 sendCommand(command);
1444}
1445
Eric Laurente1715a42014-05-20 11:30:42 -07001446status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1447 const struct audio_port_config *config, int delayMs)
1448{
1449 sp<AudioCommand> command = new AudioCommand();
1450 command->mCommand = SET_AUDIOPORT_CONFIG;
1451 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1452 data->mConfig = *config;
1453 command->mParam = data;
1454 command->mWaitStatus = true;
1455 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1456 return sendCommand(command, delayMs);
1457}
1458
Jean-Michel Trivide801052015-04-14 19:10:14 -07001459void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001460 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001461{
1462 sp<AudioCommand> command = new AudioCommand();
1463 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1464 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1465 data->mRegId = regId;
1466 data->mState = state;
1467 command->mParam = data;
1468 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1469 regId.string(), state);
1470 sendCommand(command);
1471}
1472
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001473void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001474 int event,
1475 const record_client_info_t *clientInfo,
1476 const audio_config_base_t *clientConfig,
1477 std::vector<effect_descriptor_t> clientEffects,
1478 const audio_config_base_t *deviceConfig,
1479 std::vector<effect_descriptor_t> effects,
1480 audio_patch_handle_t patchHandle,
1481 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001482{
1483 sp<AudioCommand>command = new AudioCommand();
1484 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1485 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1486 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001487 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001488 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001489 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001490 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001491 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001492 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001493 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001494 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001495 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1496 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001497 sendCommand(command);
1498}
1499
Eric Laurent0ede8922014-05-09 18:04:42 -07001500status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1501{
1502 {
1503 Mutex::Autolock _l(mLock);
1504 insertCommand_l(command, delayMs);
1505 mWaitWorkCV.signal();
1506 }
1507 Mutex::Autolock _l(command->mLock);
1508 while (command->mWaitStatus) {
1509 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1510 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1511 command->mStatus = TIMED_OUT;
1512 command->mWaitStatus = false;
1513 }
1514 }
1515 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001516}
1517
Mathias Agopian65ab4712010-07-14 17:59:35 -07001518// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001519void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001521 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001522 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001523 command->mTime = systemTime() + milliseconds(delayMs);
1524
1525 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001526 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1528 }
1529
1530 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001531 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001532 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1534 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001535
1536 // create audio patch or release audio patch commands are equivalent
1537 // with regard to filtering
1538 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1539 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1540 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1541 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1542 continue;
1543 }
1544 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545
1546 switch (command->mCommand) {
1547 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001548 ParametersData *data = (ParametersData *)command->mParam.get();
1549 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001551 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001552 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1554 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1555 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001556 String8 key;
1557 String8 value;
1558 param.getAt(j, key, value);
1559 for (size_t k = 0; k < param2.size(); k++) {
1560 String8 key2;
1561 String8 value2;
1562 param2.getAt(k, key2, value2);
1563 if (key2 == key) {
1564 param2.remove(key2);
1565 ALOGV("Filtering out parameter %s", key2.string());
1566 break;
1567 }
1568 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569 }
1570 // if all keys have been filtered out, remove the command.
1571 // otherwise, update the key value pairs
1572 if (param2.size() == 0) {
1573 removedCommands.add(command2);
1574 } else {
1575 data2->mKeyValuePairs = param2.toString();
1576 }
Eric Laurent21e54562013-09-23 12:08:05 -07001577 command->mTime = command2->mTime;
1578 // force delayMs to non 0 so that code below does not request to wait for
1579 // command status as the command is now delayed
1580 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001581 } break;
1582
1583 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001584 VolumeData *data = (VolumeData *)command->mParam.get();
1585 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586 if (data->mIO != data2->mIO) break;
1587 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001588 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001589 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001590 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001591 command->mTime = command2->mTime;
1592 // force delayMs to non 0 so that code below does not request to wait for
1593 // command status as the command is now delayed
1594 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001596
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001597 case SET_VOICE_VOLUME: {
1598 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1599 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1600 ALOGV("Filtering out voice volume command value %f replaced by %f",
1601 data2->mVolume, data->mVolume);
1602 removedCommands.add(command2);
1603 command->mTime = command2->mTime;
1604 // force delayMs to non 0 so that code below does not request to wait for
1605 // command status as the command is now delayed
1606 delayMs = 1;
1607 } break;
1608
Eric Laurente45b48a2014-09-04 16:40:57 -07001609 case CREATE_AUDIO_PATCH:
1610 case RELEASE_AUDIO_PATCH: {
1611 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001612 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001613 if (command->mCommand == CREATE_AUDIO_PATCH) {
1614 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001615 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001616 } else {
1617 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001618 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001619 }
1620 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001621 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001622 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1623 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001624 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001625 } else {
1626 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001627 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001628 }
1629 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001630 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1631 same output. */
1632 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1633 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1634 bool isOutputDiff = false;
1635 if (patch.num_sources == patch2.num_sources) {
1636 for (unsigned count = 0; count < patch.num_sources; count++) {
1637 if (patch.sources[count].id != patch2.sources[count].id) {
1638 isOutputDiff = true;
1639 break;
1640 }
1641 }
1642 if (isOutputDiff)
1643 break;
1644 }
1645 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001646 ALOGV("Filtering out %s audio patch command for handle %d",
1647 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1648 removedCommands.add(command2);
1649 command->mTime = command2->mTime;
1650 // force delayMs to non 0 so that code below does not request to wait for
1651 // command status as the command is now delayed
1652 delayMs = 1;
1653 } break;
1654
Jean-Michel Trivide801052015-04-14 19:10:14 -07001655 case DYN_POLICY_MIX_STATE_UPDATE: {
1656
1657 } break;
1658
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001659 case RECORDING_CONFIGURATION_UPDATE: {
1660
1661 } break;
1662
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663 default:
1664 break;
1665 }
1666 }
1667
1668 // remove filtered commands
1669 for (size_t j = 0; j < removedCommands.size(); j++) {
1670 // removed commands always have time stamps greater than current command
1671 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001672 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001673 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674 mAudioCommands.removeAt(k);
1675 break;
1676 }
1677 }
1678 }
1679 removedCommands.clear();
1680
Eric Laurentaa79bef2015-01-15 14:33:51 -08001681 // Disable wait for status if delay is not 0.
1682 // Except for create audio patch command because the returned patch handle
1683 // is needed by audio policy manager
1684 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001685 command->mWaitStatus = false;
1686 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001687
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001689 ALOGV("inserting command: %d at index %zd, num commands %zu",
1690 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691 mAudioCommands.insertAt(command, i + 1);
1692}
1693
1694void AudioPolicyService::AudioCommandThread::exit()
1695{
Steve Block3856b092011-10-20 11:56:00 +01001696 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697 {
1698 AutoMutex _l(mLock);
1699 requestExit();
1700 mWaitWorkCV.signal();
1701 }
Zach Janga754b4f2015-10-27 01:29:34 +00001702 // Note that we can call it from the thread loop if all other references have been released
1703 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 requestExitAndWait();
1705}
1706
1707void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1708{
1709 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1710 mCommand,
1711 (int)ns2s(mTime),
1712 (int)ns2ms(mTime)%1000,
1713 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001714 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715}
1716
Dima Zavinfce7a472011-04-19 22:30:36 -07001717/******* helpers for the service_ops callbacks defined below *********/
1718void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1719 const char *keyValuePairs,
1720 int delayMs)
1721{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001722 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001723 delayMs);
1724}
1725
1726int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1727 float volume,
1728 audio_io_handle_t output,
1729 int delayMs)
1730{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001731 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001732 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001733}
1734
Dima Zavinfce7a472011-04-19 22:30:36 -07001735int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1736{
1737 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1738}
1739
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001740void AudioPolicyService::setEffectSuspended(int effectId,
1741 audio_session_t sessionId,
1742 bool suspended)
1743{
1744 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1745}
1746
1747
Dima Zavinfce7a472011-04-19 22:30:36 -07001748extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001749audio_module_handle_t aps_load_hw_module(void *service __unused,
1750 const char *name);
1751audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001752 audio_devices_t *pDevices,
1753 uint32_t *pSamplingRate,
1754 audio_format_t *pFormat,
1755 audio_channel_mask_t *pChannelMask,
1756 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001757 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001758
Eric Laurent2d388ec2014-03-07 13:25:54 -08001759audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001760 audio_module_handle_t module,
1761 audio_devices_t *pDevices,
1762 uint32_t *pSamplingRate,
1763 audio_format_t *pFormat,
1764 audio_channel_mask_t *pChannelMask,
1765 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001766 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001767 const audio_offload_info_t *offloadInfo);
1768audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001769 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001770 audio_io_handle_t output2);
1771int aps_close_output(void *service __unused, audio_io_handle_t output);
1772int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1773int aps_restore_output(void *service __unused, audio_io_handle_t output);
1774audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001775 audio_devices_t *pDevices,
1776 uint32_t *pSamplingRate,
1777 audio_format_t *pFormat,
1778 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001779 audio_in_acoustics_t acoustics __unused);
1780audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001781 audio_module_handle_t module,
1782 audio_devices_t *pDevices,
1783 uint32_t *pSamplingRate,
1784 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001785 audio_channel_mask_t *pChannelMask);
1786int aps_close_input(void *service __unused, audio_io_handle_t input);
1787int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001788int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001789 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001790 audio_io_handle_t dst_output);
1791char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1792 const char *keys);
1793void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1794 const char *kv_pairs, int delay_ms);
1795int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001796 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001797 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001798int aps_set_voice_volume(void *service, float volume, int delay_ms);
1799};
Dima Zavinfce7a472011-04-19 22:30:36 -07001800
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001801} // namespace android