blob: f8cecbf6fc793234184cb5b8e7b96cbd340fcaaa [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 Laurent6ede98f2019-06-11 14:50:30 -0700412// AND an accessibility service is on TOP or a RTT call is active
Eric Laurent589171c2019-07-25 18:04:29 -0700413// AND the source is VOICE_RECOGNITION or HOTWORD
414// OR uses VOICE_RECOGNITION AND is on TOP
415// 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 Laurent589171c2019-07-25 18:04:29 -0700419// AND Is on TOP
420// AND the source is VOICE_RECOGNITION or HOTWORD
421// OR The assistant is not on TOP
422// AND there is no active privacy sensitive capture or call
423// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta171e352019-05-07 13:04:45 -0700424// AND is on TOP
Eric Laurenta46bedb2018-12-07 18:01:26 -0800425// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700426// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurenta171e352019-05-07 13:04:45 -0700427// OR Any client
Eric Laurenta46bedb2018-12-07 18:01:26 -0800428// AND The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700429// AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700430// AND there is no active privacy sensitive capture or call
Eric Laurent589171c2019-07-25 18:04:29 -0700431// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800432
433 sp<AudioRecordClient> topActive;
434 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800435 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700436
Eric Laurenta46bedb2018-12-07 18:01:26 -0800437 nsecs_t topStartNs = 0;
438 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800439 nsecs_t latestSensitiveStartNs = 0;
440 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
441 bool isAssistantOnTop = false;
442 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700443 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700444 bool rttCallActive =
445 (mPhoneState == AUDIO_MODE_IN_CALL || mPhoneState == AUDIO_MODE_IN_COMMUNICATION)
446 && mUidPolicy->isRttEnabled();
Eric Laurent4eb58f12018-12-07 16:41:02 -0800447
Michael Groovercfd28302018-12-11 19:16:46 -0800448 // if Sensor Privacy is enabled then all recordings should be silenced.
449 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
450 silenceAllRecordings_l();
451 return;
452 }
453
Eric Laurente8c8b432018-10-17 10:08:02 -0700454 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
455 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700456 if (!current->active) {
457 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800458 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700459
460 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
461 // clients which app is in IDLE state are not eligible for top active or
462 // latest active
463 if (appState == APP_STATE_IDLE) {
464 continue;
465 }
466
Eric Laurentc6af90d2019-07-19 10:31:39 -0700467 bool isAssistant = mUidPolicy->isAssistantUid(current->uid);
Eric Laurent589171c2019-07-25 18:04:29 -0700468 bool isAccessibility = mUidPolicy->isA11yUid(current->uid);
469 if (appState == APP_STATE_TOP && !isAccessibility) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800470 if (current->startTimeNs > topStartNs) {
471 topActive = current;
472 topStartNs = current->startTimeNs;
473 }
Eric Laurentc6af90d2019-07-19 10:31:39 -0700474 if (isAssistant) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800475 isAssistantOnTop = true;
476 }
477 }
Eric Laurent589171c2019-07-25 18:04:29 -0700478 // Assistant capturing for HOTWORD or Accessibility services not considered
479 // for latest active to avoid masking regular clients started before
480 if (current->startTimeNs > latestStartNs
481 && !((current->attributes.source == AUDIO_SOURCE_HOTWORD
482 || isA11yOnTop || rttCallActive)
483 && isAssistant)
484 && !isAccessibility) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800485 latestActive = current;
486 latestStartNs = current->startTimeNs;
487 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700488 if (isPrivacySensitiveSource(current->attributes.source)) {
489 if (current->startTimeNs > latestSensitiveStartNs) {
490 latestSensitiveActive = current;
491 latestSensitiveStartNs = current->startTimeNs;
492 }
493 isSensitiveActive = true;
494 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800495 }
496
Eric Laurent1ff16a72019-03-14 18:35:04 -0700497 // if no active client with UI on Top, consider latest active as top
498 if (topActive == nullptr) {
499 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800500 }
501
Eric Laurent1ff16a72019-03-14 18:35:04 -0700502 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800503
Eric Laurent4eb58f12018-12-07 16:41:02 -0800504 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
505 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700506 if (!current->active) {
507 continue;
508 }
509
510 // keep capture allowed if another client with the same UID has already
511 // been allowed to capture
512 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
513 != enabledUids.end()) {
514 continue;
515 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800516
517 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700518 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
519 bool isLatestSensitive = latestSensitiveActive == nullptr ?
520 false : current->uid == latestSensitiveActive->uid;
521
522 // By default allow capture if:
523 // The assistant is not on TOP
Eric Laurenta171e352019-05-07 13:04:45 -0700524 // AND is on TOP or latest started
Eric Laurent1ff16a72019-03-14 18:35:04 -0700525 // AND there is no active privacy sensitive capture or call
526 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
527 bool allowCapture = !isAssistantOnTop
Eric Laurenta171e352019-05-07 13:04:45 -0700528 && ((isTopOrLatestActive && !isLatestSensitive) || isLatestSensitive)
Eric Laurent1ff16a72019-03-14 18:35:04 -0700529 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
530 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800531
532 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700533 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
534 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800535 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700536 // For assistant allow capture if:
Eric Laurent6ede98f2019-06-11 14:50:30 -0700537 // An accessibility service is on TOP or a RTT call is active
Eric Laurent1ff16a72019-03-14 18:35:04 -0700538 // AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurenta171e352019-05-07 13:04:45 -0700539 // OR is on TOP AND uses VOICE_RECOGNITION
Eric Laurent1ff16a72019-03-14 18:35:04 -0700540 // OR uses HOTWORD
541 // AND there is no active privacy sensitive capture or call
542 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent6ede98f2019-06-11 14:50:30 -0700543 if (isA11yOnTop || rttCallActive) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800544 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700545 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800546 }
547 } else {
Eric Laurenta171e352019-05-07 13:04:45 -0700548 if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
Eric Laurent1ff16a72019-03-14 18:35:04 -0700549 source == AUDIO_SOURCE_HOTWORD) &&
550 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
551 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800552 }
553 }
554 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700555 // For accessibility service allow capture if:
Eric Laurenta171e352019-05-07 13:04:45 -0700556 // Is on TOP
Eric Laurent589171c2019-07-25 18:04:29 -0700557 // AND the source is VOICE_RECOGNITION or HOTWORD
558 // Or
559 // The assistant is not on TOP
560 // AND there is no active privacy sensitive capture or call
561 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
562 if (isA11yOnTop) {
563 if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) {
564 allowCapture = true;
565 }
566 } else {
567 if (!isAssistantOnTop
568 && (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
569 allowCapture = true;
570 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800571 }
572 }
573 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700574 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
575 APP_STATE_IDLE);
576 if (allowCapture) {
577 enabledUids.push_back(current->uid);
578 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700579 }
580}
581
Michael Groovercfd28302018-12-11 19:16:46 -0800582void AudioPolicyService::silenceAllRecordings_l() {
583 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
584 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700585 if (!isVirtualSource(current->attributes.source)) {
586 setAppState_l(current->uid, APP_STATE_IDLE);
587 }
Michael Groovercfd28302018-12-11 19:16:46 -0800588 }
589}
590
Eric Laurente8c8b432018-10-17 10:08:02 -0700591/* static */
592app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700593
594 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700595 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700596 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
597 // include persistent services
598 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700599 }
600 return APP_STATE_FOREGROUND;
601}
602
Eric Laurent4eb58f12018-12-07 16:41:02 -0800603/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800604bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
605{
606 switch (source) {
607 case AUDIO_SOURCE_CAMCORDER:
608 case AUDIO_SOURCE_VOICE_COMMUNICATION:
609 return true;
610 default:
611 break;
612 }
613 return false;
614}
615
616/* static */
617bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800618{
619 switch (source) {
620 case AUDIO_SOURCE_VOICE_UPLINK:
621 case AUDIO_SOURCE_VOICE_DOWNLINK:
622 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800623 case AUDIO_SOURCE_REMOTE_SUBMIX:
624 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800625 return true;
626 default:
627 break;
628 }
629 return false;
630}
631
Eric Laurente8c8b432018-10-17 10:08:02 -0700632void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
633{
634 AutoCallerClear acc;
635
636 if (mAudioPolicyManager) {
637 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700638 }
639 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
640 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700641 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700642 af->setRecordSilenced(uid, silenced);
643 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800644}
645
Glenn Kasten0f11b512014-01-31 16:18:54 -0800646status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647{
Glenn Kasten44deb052012-02-05 18:09:08 -0800648 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 dumpPermissionDenial(fd);
650 } else {
Mikhail Naganov959e2d02019-03-28 11:08:19 -0700651 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 if (!locked) {
653 String8 result(kDeadlockedString);
654 write(fd, result.string(), result.size());
655 }
656
657 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800658 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 mAudioCommandThread->dump(fd);
660 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661
Eric Laurentdce54a12014-03-10 12:19:46 -0700662 if (mAudioPolicyManager) {
663 mAudioPolicyManager->dump(fd);
664 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665
Kevin Rocard8be94972019-02-22 13:26:25 -0800666 mPackageManager.dump(fd);
667
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 if (locked) mLock.unlock();
669 }
670 return NO_ERROR;
671}
672
673status_t AudioPolicyService::dumpPermissionDenial(int fd)
674{
675 const size_t SIZE = 256;
676 char buffer[SIZE];
677 String8 result;
678 snprintf(buffer, SIZE, "Permission Denial: "
679 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
680 IPCThreadState::self()->getCallingPid(),
681 IPCThreadState::self()->getCallingUid());
682 result.append(buffer);
683 write(fd, result.string(), result.size());
684 return NO_ERROR;
685}
686
687status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800688 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
689 switch (code) {
690 case SHELL_COMMAND_TRANSACTION: {
691 int in = data.readFileDescriptor();
692 int out = data.readFileDescriptor();
693 int err = data.readFileDescriptor();
694 int argc = data.readInt32();
695 Vector<String16> args;
696 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
697 args.add(data.readString16());
698 }
699 sp<IBinder> unusedCallback;
700 sp<IResultReceiver> resultReceiver;
701 status_t status;
702 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
703 return status;
704 }
705 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
706 return status;
707 }
708 status = shellCommand(in, out, err, args);
709 if (resultReceiver != nullptr) {
710 resultReceiver->send(status);
711 }
712 return NO_ERROR;
713 }
714 }
715
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 return BnAudioPolicyService::onTransact(code, data, reply, flags);
717}
718
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800719// ------------------- Shell command implementation -------------------
720
721// NOTE: This is a remote API - make sure all args are validated
722status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
723 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
724 return PERMISSION_DENIED;
725 }
726 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
727 return BAD_VALUE;
728 }
729 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
730 return handleSetUidState(args, err);
731 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
732 return handleResetUidState(args, err);
733 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
734 return handleGetUidState(args, out, err);
735 } else if (args.size() == 1 && args[0] == String16("help")) {
736 printHelp(out);
737 return NO_ERROR;
738 }
739 printHelp(err);
740 return BAD_VALUE;
741}
742
743status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
744 PermissionController pc;
745 int uid = pc.getPackageUid(args[1], 0);
746 if (uid <= 0) {
747 ALOGE("Unknown package: '%s'", String8(args[1]).string());
748 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
749 return BAD_VALUE;
750 }
751 bool active = false;
752 if (args[2] == String16("active")) {
753 active = true;
754 } else if ((args[2] != String16("idle"))) {
755 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
756 return BAD_VALUE;
757 }
758 mUidPolicy->addOverrideUid(uid, active);
759 return NO_ERROR;
760}
761
762status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
763 PermissionController pc;
764 int uid = pc.getPackageUid(args[1], 0);
765 if (uid < 0) {
766 ALOGE("Unknown package: '%s'", String8(args[1]).string());
767 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
768 return BAD_VALUE;
769 }
770 mUidPolicy->removeOverrideUid(uid);
771 return NO_ERROR;
772}
773
774status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
775 PermissionController pc;
776 int uid = pc.getPackageUid(args[1], 0);
777 if (uid < 0) {
778 ALOGE("Unknown package: '%s'", String8(args[1]).string());
779 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
780 return BAD_VALUE;
781 }
782 if (mUidPolicy->isUidActive(uid)) {
783 return dprintf(out, "active\n");
784 } else {
785 return dprintf(out, "idle\n");
786 }
787}
788
789status_t AudioPolicyService::printHelp(int out) {
790 return dprintf(out, "Audio policy service commands:\n"
791 " get-uid-state <PACKAGE> gets the uid state\n"
792 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
793 " reset-uid-state <PACKAGE> clears the uid state override\n"
794 " help print this message\n");
795}
796
797// ----------- AudioPolicyService::UidPolicy implementation ----------
798
799void AudioPolicyService::UidPolicy::registerSelf() {
800 ActivityManager am;
801 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
802 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700803 | ActivityManager::UID_OBSERVER_ACTIVE
804 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800805 ActivityManager::PROCESS_STATE_UNKNOWN,
806 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700807 status_t res = am.linkToDeath(this);
808 if (!res) {
809 Mutex::Autolock _l(mLock);
810 mObserverRegistered = true;
811 } else {
812 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800813
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700814 am.unregisterUidObserver(this);
815 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800816}
817
818void AudioPolicyService::UidPolicy::unregisterSelf() {
819 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700820 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800821 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700822 Mutex::Autolock _l(mLock);
823 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800824}
825
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700826void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
827 Mutex::Autolock _l(mLock);
828 mCachedUids.clear();
829 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800830}
831
Eric Laurente8c8b432018-10-17 10:08:02 -0700832void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700833 bool needToReregister = false;
834 {
835 Mutex::Autolock _l(mLock);
836 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800837 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700838 if (needToReregister) {
839 // Looks like ActivityManager has died previously, attempt to re-register.
840 registerSelf();
841 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700842}
843
844bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
845 if (isServiceUid(uid)) return true;
846 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700847 {
848 Mutex::Autolock _l(mLock);
849 auto overrideIter = mOverrideUids.find(uid);
850 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700851 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700852 }
853 // In an absense of the ActivityManager, assume everything to be active.
854 if (!mObserverRegistered) return true;
855 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700856 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700857 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700858 }
859 }
860 ActivityManager am;
861 bool active = am.isUidActive(uid, String16("audioserver"));
862 {
863 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700864 mCachedUids.insert(std::pair<uid_t,
865 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
866 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700867 }
868 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800869}
870
Eric Laurente8c8b432018-10-17 10:08:02 -0700871int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
872 if (isServiceUid(uid)) {
873 return ActivityManager::PROCESS_STATE_TOP;
874 }
875 checkRegistered();
876 {
877 Mutex::Autolock _l(mLock);
878 auto overrideIter = mOverrideUids.find(uid);
879 if (overrideIter != mOverrideUids.end()) {
880 if (overrideIter->second.first) {
881 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
882 return overrideIter->second.second;
883 } else {
884 auto cacheIter = mCachedUids.find(uid);
885 if (cacheIter != mCachedUids.end()) {
886 return cacheIter->second.second;
887 }
888 }
889 }
890 return ActivityManager::PROCESS_STATE_UNKNOWN;
891 }
892 // In an absense of the ActivityManager, assume everything to be active.
893 if (!mObserverRegistered) {
894 return ActivityManager::PROCESS_STATE_TOP;
895 }
896 auto cacheIter = mCachedUids.find(uid);
897 if (cacheIter != mCachedUids.end()) {
898 if (cacheIter->second.first) {
899 return cacheIter->second.second;
900 } else {
901 return ActivityManager::PROCESS_STATE_UNKNOWN;
902 }
903 }
904 }
905 ActivityManager am;
906 bool active = am.isUidActive(uid, String16("audioserver"));
907 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
908 if (active) {
909 state = am.getUidProcessState(uid, String16("audioserver"));
910 }
911 {
912 Mutex::Autolock _l(mLock);
913 mCachedUids.insert(std::pair<uid_t,
914 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
915 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800916
Eric Laurente8c8b432018-10-17 10:08:02 -0700917 return state;
918}
919
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700920void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700921 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700922}
923
924void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700925 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700926}
927
928void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700929 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700930}
931
Eric Laurente8c8b432018-10-17 10:08:02 -0700932void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
933 int32_t procState,
934 int64_t procStateSeq __unused) {
935 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
936 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700937 }
938}
939
940void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700941 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
942}
943
944void AudioPolicyService::UidPolicy::notifyService() {
945 sp<AudioPolicyService> service = mService.promote();
946 if (service != nullptr) {
947 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700948 }
949}
950
Eric Laurente8c8b432018-10-17 10:08:02 -0700951void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
952 std::pair<bool, int>> *uids,
953 uid_t uid,
954 bool active,
955 int state,
956 bool insert) {
957 if (isServiceUid(uid)) {
958 return;
959 }
960 bool wasActive = isUidActive(uid);
961 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700962 {
963 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700964 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700965 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700966 if (wasActive != isUidActive(uid) || state != previousState) {
967 notifyService();
968 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700969}
970
Eric Laurente8c8b432018-10-17 10:08:02 -0700971void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
972 std::pair<bool, int>> *uids,
973 uid_t uid,
974 bool active,
975 int state,
976 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700977 auto it = uids->find(uid);
978 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700979 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700980 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
981 it->second.first = active;
982 }
983 if (it->second.first) {
984 it->second.second = state;
985 } else {
986 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
987 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700988 } else {
989 uids->erase(it);
990 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700991 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
992 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
993 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700994 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800995}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996
Eric Laurent4eb58f12018-12-07 16:41:02 -0800997bool AudioPolicyService::UidPolicy::isA11yOnTop() {
998 for (const auto &uid : mCachedUids) {
999 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
1000 if (it == mA11yUids.end()) {
1001 continue;
1002 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -08001003 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
1004 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -08001005 return true;
1006 }
1007 }
1008 return false;
1009}
1010
Eric Laurentb78763e2018-10-17 10:08:02 -07001011bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
1012{
1013 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
1014 return it != mA11yUids.end();
1015}
1016
Michael Groovercfd28302018-12-11 19:16:46 -08001017// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
1018void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
1019 SensorPrivacyManager spm;
1020 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1021 spm.addSensorPrivacyListener(this);
1022}
1023
1024void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1025 SensorPrivacyManager spm;
1026 spm.removeSensorPrivacyListener(this);
1027}
1028
1029bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1030 return mSensorPrivacyEnabled;
1031}
1032
1033binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1034 mSensorPrivacyEnabled = enabled;
1035 sp<AudioPolicyService> service = mService.promote();
1036 if (service != nullptr) {
1037 service->updateUidStates();
1038 }
1039 return binder::Status::ok();
1040}
1041
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1043
Eric Laurentbfb1b832013-01-07 09:53:42 -08001044AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1045 const wp<AudioPolicyService>& service)
1046 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048}
1049
1050
1051AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1052{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001053 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 release_wake_lock(mName.string());
1055 }
1056 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057}
1058
1059void AudioPolicyService::AudioCommandThread::onFirstRef()
1060{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001061 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062}
1063
1064bool AudioPolicyService::AudioCommandThread::threadLoop()
1065{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001066 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001067
1068 mLock.lock();
1069 while (!exitPending())
1070 {
Eric Laurent59a89232014-06-08 14:14:17 -07001071 sp<AudioPolicyService> svc;
1072 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 nsecs_t curTime = systemTime();
1074 // commands are sorted by increasing time stamp: execute them from index 0 and up
1075 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001076 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001078 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079
1080 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001082 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001084 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001085 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001086 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1087 data->mVolume,
1088 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001089 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 }break;
1091 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001092 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001093 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1094 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001095 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001096 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001097 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001098 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001100 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001101 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001102 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001103 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001105 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001107 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001108 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001109 ALOGV("AudioCommandThread() processing stop output portId %d",
1110 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001111 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001112 if (svc == 0) {
1113 break;
1114 }
1115 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001116 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001117 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001118 }break;
1119 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001120 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001121 ALOGV("AudioCommandThread() processing release output portId %d",
1122 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001123 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001124 if (svc == 0) {
1125 break;
1126 }
1127 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001128 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001129 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001130 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001131 case CREATE_AUDIO_PATCH: {
1132 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1133 ALOGV("AudioCommandThread() processing create audio patch");
1134 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1135 if (af == 0) {
1136 command->mStatus = PERMISSION_DENIED;
1137 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001138 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001139 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001140 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001141 }
1142 } break;
1143 case RELEASE_AUDIO_PATCH: {
1144 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1145 ALOGV("AudioCommandThread() processing release audio patch");
1146 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1147 if (af == 0) {
1148 command->mStatus = PERMISSION_DENIED;
1149 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001150 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001151 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001152 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001153 }
1154 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001155 case UPDATE_AUDIOPORT_LIST: {
1156 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001157 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001158 if (svc == 0) {
1159 break;
1160 }
1161 mLock.unlock();
1162 svc->doOnAudioPortListUpdate();
1163 mLock.lock();
1164 }break;
1165 case UPDATE_AUDIOPATCH_LIST: {
1166 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001167 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001168 if (svc == 0) {
1169 break;
1170 }
1171 mLock.unlock();
1172 svc->doOnAudioPatchListUpdate();
1173 mLock.lock();
1174 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001175 case CHANGED_AUDIOVOLUMEGROUP: {
1176 AudioVolumeGroupData *data =
1177 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1178 ALOGV("AudioCommandThread() processing update audio volume group");
1179 svc = mService.promote();
1180 if (svc == 0) {
1181 break;
1182 }
1183 mLock.unlock();
1184 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1185 mLock.lock();
1186 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001187 case SET_AUDIOPORT_CONFIG: {
1188 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1189 ALOGV("AudioCommandThread() processing set port config");
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 Laurente1715a42014-05-20 11:30:42 -07001195 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001196 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001197 }
1198 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001199 case DYN_POLICY_MIX_STATE_UPDATE: {
1200 DynPolicyMixStateUpdateData *data =
1201 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001202 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1203 data->mRegId.string(), data->mState);
1204 svc = mService.promote();
1205 if (svc == 0) {
1206 break;
1207 }
1208 mLock.unlock();
1209 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1210 mLock.lock();
1211 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001212 case RECORDING_CONFIGURATION_UPDATE: {
1213 RecordingConfigurationUpdateData *data =
1214 (RecordingConfigurationUpdateData *)command->mParam.get();
1215 ALOGV("AudioCommandThread() processing recording configuration update");
1216 svc = mService.promote();
1217 if (svc == 0) {
1218 break;
1219 }
1220 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001221 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001222 &data->mClientConfig, data->mClientEffects,
1223 &data->mDeviceConfig, data->mEffects,
1224 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001225 mLock.lock();
1226 } break;
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001227 case SET_EFFECT_SUSPENDED: {
1228 SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get();
1229 ALOGV("AudioCommandThread() processing set effect suspended");
1230 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1231 if (af != 0) {
1232 mLock.unlock();
1233 af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended);
1234 mLock.lock();
1235 }
1236 } break;
1237
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001239 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001241 {
1242 Mutex::Autolock _l(command->mLock);
1243 if (command->mWaitStatus) {
1244 command->mWaitStatus = false;
1245 command->mCond.signal();
1246 }
1247 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001248 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001249 // release mLock before releasing strong reference on the service as
1250 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1251 // acquires mLock.
1252 mLock.unlock();
1253 svc.clear();
1254 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 } else {
1256 waitTime = mAudioCommands[0]->mTime - curTime;
1257 break;
1258 }
1259 }
Zach Janga754b4f2015-10-27 01:29:34 +00001260
1261 // release delayed commands wake lock if the queue is empty
1262 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001263 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001264 }
1265
1266 // At this stage we have either an empty command queue or the first command in the queue
1267 // has a finite delay. So unless we are exiting it is safe to wait.
1268 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001269 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001270 if (waitTime == -1) {
1271 mWaitWorkCV.wait(mLock);
1272 } else {
1273 mWaitWorkCV.waitRelative(mLock, waitTime);
1274 }
Eric Laurent59a89232014-06-08 14:14:17 -07001275 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001277 // release delayed commands wake lock before quitting
1278 if (!mAudioCommands.isEmpty()) {
1279 release_wake_lock(mName.string());
1280 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 mLock.unlock();
1282 return false;
1283}
1284
1285status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1286{
1287 const size_t SIZE = 256;
1288 char buffer[SIZE];
1289 String8 result;
1290
1291 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1292 result.append(buffer);
1293 write(fd, result.string(), result.size());
1294
Mikhail Naganov959e2d02019-03-28 11:08:19 -07001295 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296 if (!locked) {
1297 String8 result2(kCmdDeadlockedString);
1298 write(fd, result2.string(), result2.size());
1299 }
1300
1301 snprintf(buffer, SIZE, "- Commands:\n");
1302 result = String8(buffer);
1303 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001304 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305 mAudioCommands[i]->dump(buffer, SIZE);
1306 result.append(buffer);
1307 }
1308 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001309 if (mLastCommand != 0) {
1310 mLastCommand->dump(buffer, SIZE);
1311 result.append(buffer);
1312 } else {
1313 result.append(" none\n");
1314 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315
1316 write(fd, result.string(), result.size());
1317
1318 if (locked) mLock.unlock();
1319
1320 return NO_ERROR;
1321}
1322
Glenn Kastenfff6d712012-01-12 16:38:12 -08001323status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001324 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001325 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001326 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327{
Eric Laurent0ede8922014-05-09 18:04:42 -07001328 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001330 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 data->mStream = stream;
1332 data->mVolume = volume;
1333 data->mIO = output;
1334 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001335 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001336 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001337 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001338 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339}
1340
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001341status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001342 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001343 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344{
Eric Laurent0ede8922014-05-09 18:04:42 -07001345 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001347 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001349 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001351 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001352 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001353 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001354 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355}
1356
1357status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1358{
Eric Laurent0ede8922014-05-09 18:04:42 -07001359 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001361 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362 data->mVolume = volume;
1363 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001364 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001365 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001366 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367}
1368
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001369void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId,
1370 audio_session_t sessionId,
1371 bool suspended)
1372{
1373 sp<AudioCommand> command = new AudioCommand();
1374 command->mCommand = SET_EFFECT_SUSPENDED;
1375 sp<SetEffectSuspendedData> data = new SetEffectSuspendedData();
1376 data->mEffectId = effectId;
1377 data->mSessionId = sessionId;
1378 data->mSuspended = suspended;
1379 command->mParam = data;
1380 ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d",
1381 effectId, sessionId, suspended);
1382 sendCommand(command);
1383}
1384
1385
Eric Laurentd7fe0862018-07-14 16:48:01 -07001386void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001387{
Eric Laurent0ede8922014-05-09 18:04:42 -07001388 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001389 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001390 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001391 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001392 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001393 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001394 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001395}
1396
Eric Laurentd7fe0862018-07-14 16:48:01 -07001397void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001398{
Eric Laurent0ede8922014-05-09 18:04:42 -07001399 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001400 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001401 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001402 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001403 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001404 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001405 sendCommand(command);
1406}
1407
Eric Laurent951f4552014-05-20 10:48:17 -07001408status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1409 const struct audio_patch *patch,
1410 audio_patch_handle_t *handle,
1411 int delayMs)
1412{
1413 status_t status = NO_ERROR;
1414
1415 sp<AudioCommand> command = new AudioCommand();
1416 command->mCommand = CREATE_AUDIO_PATCH;
1417 CreateAudioPatchData *data = new CreateAudioPatchData();
1418 data->mPatch = *patch;
1419 data->mHandle = *handle;
1420 command->mParam = data;
1421 command->mWaitStatus = true;
1422 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1423 status = sendCommand(command, delayMs);
1424 if (status == NO_ERROR) {
1425 *handle = data->mHandle;
1426 }
1427 return status;
1428}
1429
1430status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1431 int delayMs)
1432{
1433 sp<AudioCommand> command = new AudioCommand();
1434 command->mCommand = RELEASE_AUDIO_PATCH;
1435 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1436 data->mHandle = handle;
1437 command->mParam = data;
1438 command->mWaitStatus = true;
1439 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1440 return sendCommand(command, delayMs);
1441}
1442
Eric Laurentb52c1522014-05-20 11:27:36 -07001443void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1444{
1445 sp<AudioCommand> command = new AudioCommand();
1446 command->mCommand = UPDATE_AUDIOPORT_LIST;
1447 ALOGV("AudioCommandThread() adding update audio port list");
1448 sendCommand(command);
1449}
1450
1451void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1452{
1453 sp<AudioCommand>command = new AudioCommand();
1454 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1455 ALOGV("AudioCommandThread() adding update audio patch list");
1456 sendCommand(command);
1457}
1458
François Gaffiecfe17322018-11-07 13:41:29 +01001459void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1460 int flags)
1461{
1462 sp<AudioCommand>command = new AudioCommand();
1463 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1464 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1465 data->mGroup = group;
1466 data->mFlags = flags;
1467 command->mParam = data;
1468 ALOGV("AudioCommandThread() adding audio volume group changed");
1469 sendCommand(command);
1470}
1471
Eric Laurente1715a42014-05-20 11:30:42 -07001472status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1473 const struct audio_port_config *config, int delayMs)
1474{
1475 sp<AudioCommand> command = new AudioCommand();
1476 command->mCommand = SET_AUDIOPORT_CONFIG;
1477 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1478 data->mConfig = *config;
1479 command->mParam = data;
1480 command->mWaitStatus = true;
1481 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1482 return sendCommand(command, delayMs);
1483}
1484
Jean-Michel Trivide801052015-04-14 19:10:14 -07001485void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001486 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001487{
1488 sp<AudioCommand> command = new AudioCommand();
1489 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1490 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1491 data->mRegId = regId;
1492 data->mState = state;
1493 command->mParam = data;
1494 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1495 regId.string(), state);
1496 sendCommand(command);
1497}
1498
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001499void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001500 int event,
1501 const record_client_info_t *clientInfo,
1502 const audio_config_base_t *clientConfig,
1503 std::vector<effect_descriptor_t> clientEffects,
1504 const audio_config_base_t *deviceConfig,
1505 std::vector<effect_descriptor_t> effects,
1506 audio_patch_handle_t patchHandle,
1507 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001508{
1509 sp<AudioCommand>command = new AudioCommand();
1510 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1511 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1512 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001513 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001514 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001515 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001516 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001517 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001518 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001519 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001520 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001521 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1522 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001523 sendCommand(command);
1524}
1525
Eric Laurent0ede8922014-05-09 18:04:42 -07001526status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1527{
1528 {
1529 Mutex::Autolock _l(mLock);
1530 insertCommand_l(command, delayMs);
1531 mWaitWorkCV.signal();
1532 }
1533 Mutex::Autolock _l(command->mLock);
1534 while (command->mWaitStatus) {
1535 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1536 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1537 command->mStatus = TIMED_OUT;
1538 command->mWaitStatus = false;
1539 }
1540 }
1541 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001542}
1543
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001545void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001547 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001548 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 command->mTime = systemTime() + milliseconds(delayMs);
1550
1551 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001552 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001553 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1554 }
1555
1556 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001557 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001558 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1560 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001561
1562 // create audio patch or release audio patch commands are equivalent
1563 // with regard to filtering
1564 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1565 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1566 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1567 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1568 continue;
1569 }
1570 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571
1572 switch (command->mCommand) {
1573 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001574 ParametersData *data = (ParametersData *)command->mParam.get();
1575 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001577 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001578 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001579 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1580 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1581 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001582 String8 key;
1583 String8 value;
1584 param.getAt(j, key, value);
1585 for (size_t k = 0; k < param2.size(); k++) {
1586 String8 key2;
1587 String8 value2;
1588 param2.getAt(k, key2, value2);
1589 if (key2 == key) {
1590 param2.remove(key2);
1591 ALOGV("Filtering out parameter %s", key2.string());
1592 break;
1593 }
1594 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 }
1596 // if all keys have been filtered out, remove the command.
1597 // otherwise, update the key value pairs
1598 if (param2.size() == 0) {
1599 removedCommands.add(command2);
1600 } else {
1601 data2->mKeyValuePairs = param2.toString();
1602 }
Eric Laurent21e54562013-09-23 12:08:05 -07001603 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;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 } break;
1608
1609 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001610 VolumeData *data = (VolumeData *)command->mParam.get();
1611 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001612 if (data->mIO != data2->mIO) break;
1613 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001614 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001615 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001617 command->mTime = command2->mTime;
1618 // force delayMs to non 0 so that code below does not request to wait for
1619 // command status as the command is now delayed
1620 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001621 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001622
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001623 case SET_VOICE_VOLUME: {
1624 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1625 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1626 ALOGV("Filtering out voice volume command value %f replaced by %f",
1627 data2->mVolume, data->mVolume);
1628 removedCommands.add(command2);
1629 command->mTime = command2->mTime;
1630 // force delayMs to non 0 so that code below does not request to wait for
1631 // command status as the command is now delayed
1632 delayMs = 1;
1633 } break;
1634
Eric Laurente45b48a2014-09-04 16:40:57 -07001635 case CREATE_AUDIO_PATCH:
1636 case RELEASE_AUDIO_PATCH: {
1637 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001638 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001639 if (command->mCommand == CREATE_AUDIO_PATCH) {
1640 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001641 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001642 } else {
1643 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001644 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001645 }
1646 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001647 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001648 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1649 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001650 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001651 } else {
1652 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001653 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001654 }
1655 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001656 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1657 same output. */
1658 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1659 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1660 bool isOutputDiff = false;
1661 if (patch.num_sources == patch2.num_sources) {
1662 for (unsigned count = 0; count < patch.num_sources; count++) {
1663 if (patch.sources[count].id != patch2.sources[count].id) {
1664 isOutputDiff = true;
1665 break;
1666 }
1667 }
1668 if (isOutputDiff)
1669 break;
1670 }
1671 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001672 ALOGV("Filtering out %s audio patch command for handle %d",
1673 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1674 removedCommands.add(command2);
1675 command->mTime = command2->mTime;
1676 // force delayMs to non 0 so that code below does not request to wait for
1677 // command status as the command is now delayed
1678 delayMs = 1;
1679 } break;
1680
Jean-Michel Trivide801052015-04-14 19:10:14 -07001681 case DYN_POLICY_MIX_STATE_UPDATE: {
1682
1683 } break;
1684
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001685 case RECORDING_CONFIGURATION_UPDATE: {
1686
1687 } break;
1688
Mathias Agopian65ab4712010-07-14 17:59:35 -07001689 default:
1690 break;
1691 }
1692 }
1693
1694 // remove filtered commands
1695 for (size_t j = 0; j < removedCommands.size(); j++) {
1696 // removed commands always have time stamps greater than current command
1697 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001698 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001699 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 mAudioCommands.removeAt(k);
1701 break;
1702 }
1703 }
1704 }
1705 removedCommands.clear();
1706
Eric Laurentaa79bef2015-01-15 14:33:51 -08001707 // Disable wait for status if delay is not 0.
1708 // Except for create audio patch command because the returned patch handle
1709 // is needed by audio policy manager
1710 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001711 command->mWaitStatus = false;
1712 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001713
Mathias Agopian65ab4712010-07-14 17:59:35 -07001714 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001715 ALOGV("inserting command: %d at index %zd, num commands %zu",
1716 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001717 mAudioCommands.insertAt(command, i + 1);
1718}
1719
1720void AudioPolicyService::AudioCommandThread::exit()
1721{
Steve Block3856b092011-10-20 11:56:00 +01001722 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723 {
1724 AutoMutex _l(mLock);
1725 requestExit();
1726 mWaitWorkCV.signal();
1727 }
Zach Janga754b4f2015-10-27 01:29:34 +00001728 // Note that we can call it from the thread loop if all other references have been released
1729 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001730 requestExitAndWait();
1731}
1732
1733void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1734{
1735 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1736 mCommand,
1737 (int)ns2s(mTime),
1738 (int)ns2ms(mTime)%1000,
1739 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001740 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001741}
1742
Dima Zavinfce7a472011-04-19 22:30:36 -07001743/******* helpers for the service_ops callbacks defined below *********/
1744void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1745 const char *keyValuePairs,
1746 int delayMs)
1747{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001748 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001749 delayMs);
1750}
1751
1752int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1753 float volume,
1754 audio_io_handle_t output,
1755 int delayMs)
1756{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001757 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001758 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001759}
1760
Dima Zavinfce7a472011-04-19 22:30:36 -07001761int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1762{
1763 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1764}
1765
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001766void AudioPolicyService::setEffectSuspended(int effectId,
1767 audio_session_t sessionId,
1768 bool suspended)
1769{
1770 mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended);
1771}
1772
1773
Dima Zavinfce7a472011-04-19 22:30:36 -07001774extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001775audio_module_handle_t aps_load_hw_module(void *service __unused,
1776 const char *name);
1777audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001778 audio_devices_t *pDevices,
1779 uint32_t *pSamplingRate,
1780 audio_format_t *pFormat,
1781 audio_channel_mask_t *pChannelMask,
1782 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001783 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001784
Eric Laurent2d388ec2014-03-07 13:25:54 -08001785audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001786 audio_module_handle_t module,
1787 audio_devices_t *pDevices,
1788 uint32_t *pSamplingRate,
1789 audio_format_t *pFormat,
1790 audio_channel_mask_t *pChannelMask,
1791 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001792 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001793 const audio_offload_info_t *offloadInfo);
1794audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001795 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001796 audio_io_handle_t output2);
1797int aps_close_output(void *service __unused, audio_io_handle_t output);
1798int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1799int aps_restore_output(void *service __unused, audio_io_handle_t output);
1800audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001801 audio_devices_t *pDevices,
1802 uint32_t *pSamplingRate,
1803 audio_format_t *pFormat,
1804 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001805 audio_in_acoustics_t acoustics __unused);
1806audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001807 audio_module_handle_t module,
1808 audio_devices_t *pDevices,
1809 uint32_t *pSamplingRate,
1810 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001811 audio_channel_mask_t *pChannelMask);
1812int aps_close_input(void *service __unused, audio_io_handle_t input);
1813int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001814int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001815 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001816 audio_io_handle_t dst_output);
1817char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1818 const char *keys);
1819void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1820 const char *kv_pairs, int delay_ms);
1821int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001822 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001823 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001824int aps_set_voice_volume(void *service, float volume, int delay_ms);
1825};
Dima Zavinfce7a472011-04-19 22:30:36 -07001826
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001827} // namespace android