blob: e858e8d4d01436a3f978530b9b85e7a47aa29f8f [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>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080031#include <binder/ActivityManager.h>
32#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Michael Groovercfd28302018-12-11 19:16:46 -080041#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
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
379static bool tryLock(Mutex& mutex)
380{
381 bool locked = false;
382 for (int i = 0; i < kDumpLockRetries; ++i) {
383 if (mutex.tryLock() == NO_ERROR) {
384 locked = true;
385 break;
386 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800387 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 }
389 return locked;
390}
391
392status_t AudioPolicyService::dumpInternals(int fd)
393{
394 const size_t SIZE = 256;
395 char buffer[SIZE];
396 String8 result;
397
Eric Laurentdce54a12014-03-10 12:19:46 -0700398 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 result.append(buffer);
400 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
401 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402
403 write(fd, result.string(), result.size());
404 return NO_ERROR;
405}
406
Eric Laurente8c8b432018-10-17 10:08:02 -0700407void AudioPolicyService::updateUidStates()
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800408{
Eric Laurente8c8b432018-10-17 10:08:02 -0700409 Mutex::Autolock _l(mLock);
410 updateUidStates_l();
411}
412
413void AudioPolicyService::updateUidStates_l()
414{
Eric Laurent4eb58f12018-12-07 16:41:02 -0800415// Go over all active clients and allow capture (does not force silence) in the
416// following cases:
Eric Laurent1ff16a72019-03-14 18:35:04 -0700417// Another client in the same UID has already been allowed to capture
418// OR The client is the assistant
Eric Laurenta46bedb2018-12-07 18:01:26 -0800419// AND an accessibility service is on TOP
420// AND the source is VOICE_RECOGNITION or HOTWORD
421// OR uses VOICE_RECOGNITION AND is on TOP OR latest started
422// OR uses HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700423// AND there is no active privacy sensitive capture or call
424// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurenta46bedb2018-12-07 18:01:26 -0800425// OR The client is an accessibility service
426// AND is on TOP OR latest started
427// AND the source is VOICE_RECOGNITION or HOTWORD
Eric Laurent1ff16a72019-03-14 18:35:04 -0700428// OR the client source is virtual (remote submix, call audio TX or RX...)
Eric Laurenta46bedb2018-12-07 18:01:26 -0800429// OR Any other client
430// AND The assistant is not on TOP
Eric Laurent1ff16a72019-03-14 18:35:04 -0700431// AND there is no active privacy sensitive capture or call
432// OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800433//TODO: mamanage pre processing effects according to use case priority
434
435 sp<AudioRecordClient> topActive;
436 sp<AudioRecordClient> latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800437 sp<AudioRecordClient> latestSensitiveActive;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700438
Eric Laurenta46bedb2018-12-07 18:01:26 -0800439 nsecs_t topStartNs = 0;
440 nsecs_t latestStartNs = 0;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800441 nsecs_t latestSensitiveStartNs = 0;
442 bool isA11yOnTop = mUidPolicy->isA11yOnTop();
443 bool isAssistantOnTop = false;
444 bool isSensitiveActive = false;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700445 bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800446
Michael Groovercfd28302018-12-11 19:16:46 -0800447 // if Sensor Privacy is enabled then all recordings should be silenced.
448 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
449 silenceAllRecordings_l();
450 return;
451 }
452
Eric Laurente8c8b432018-10-17 10:08:02 -0700453 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
454 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700455 if (!current->active) {
456 continue;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800457 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700458
459 app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid));
460 // clients which app is in IDLE state are not eligible for top active or
461 // latest active
462 if (appState == APP_STATE_IDLE) {
463 continue;
464 }
465
466 if (appState == APP_STATE_TOP) {
Eric Laurenta46bedb2018-12-07 18:01:26 -0800467 if (current->startTimeNs > topStartNs) {
468 topActive = current;
469 topStartNs = current->startTimeNs;
470 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800471 if (mUidPolicy->isAssistantUid(current->uid)) {
472 isAssistantOnTop = true;
473 }
474 }
475 if (current->startTimeNs > latestStartNs) {
476 latestActive = current;
477 latestStartNs = current->startTimeNs;
478 }
Eric Laurent1ff16a72019-03-14 18:35:04 -0700479 if (isPrivacySensitiveSource(current->attributes.source)) {
480 if (current->startTimeNs > latestSensitiveStartNs) {
481 latestSensitiveActive = current;
482 latestSensitiveStartNs = current->startTimeNs;
483 }
484 isSensitiveActive = true;
485 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800486 }
487
Eric Laurent1ff16a72019-03-14 18:35:04 -0700488 // if no active client with UI on Top, consider latest active as top
489 if (topActive == nullptr) {
490 topActive = latestActive;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800491 }
492
Eric Laurent1ff16a72019-03-14 18:35:04 -0700493 std::vector<uid_t> enabledUids;
Eric Laurenta46bedb2018-12-07 18:01:26 -0800494
Eric Laurent4eb58f12018-12-07 16:41:02 -0800495 for (size_t i =0; i < mAudioRecordClients.size(); i++) {
496 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700497 if (!current->active) {
498 continue;
499 }
500
501 // keep capture allowed if another client with the same UID has already
502 // been allowed to capture
503 if (std::find(enabledUids.begin(), enabledUids.end(), current->uid)
504 != enabledUids.end()) {
505 continue;
506 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800507
508 audio_source_t source = current->attributes.source;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700509 bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid;
510 bool isLatestSensitive = latestSensitiveActive == nullptr ?
511 false : current->uid == latestSensitiveActive->uid;
512
513 // By default allow capture if:
514 // The assistant is not on TOP
515 // AND there is no active privacy sensitive capture or call
516 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
517 bool allowCapture = !isAssistantOnTop
518 && !(isSensitiveActive && !(isLatestSensitive || current->canCaptureOutput))
519 && !(isInCall && !current->canCaptureOutput);
Eric Laurent2dc962b2019-03-01 08:25:25 -0800520
521 if (isVirtualSource(source)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700522 // Allow capture for virtual (remote submix, call audio TX or RX...) sources
523 allowCapture = true;
Eric Laurent2dc962b2019-03-01 08:25:25 -0800524 } else if (mUidPolicy->isAssistantUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700525 // For assistant allow capture if:
526 // An accessibility service is on TOP
527 // AND the source is VOICE_RECOGNITION or HOTWORD
528 // OR is on TOP OR latest started AND uses VOICE_RECOGNITION
529 // OR uses HOTWORD
530 // AND there is no active privacy sensitive capture or call
531 // OR client has CAPTURE_AUDIO_OUTPUT privileged permission
Eric Laurent4eb58f12018-12-07 16:41:02 -0800532 if (isA11yOnTop) {
533 if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700534 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800535 }
536 } else {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700537 if (((isTopOrLatestActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) ||
538 source == AUDIO_SOURCE_HOTWORD) &&
539 (!(isSensitiveActive || isInCall) || current->canCaptureOutput)) {
540 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800541 }
542 }
543 } else if (mUidPolicy->isA11yUid(current->uid)) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700544 // For accessibility service allow capture if:
545 // Is on TOP OR latest started
546 // AND the source is VOICE_RECOGNITION or HOTWORD
547 if (isTopOrLatestActive &&
548 (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD)) {
549 allowCapture = true;
Eric Laurent4eb58f12018-12-07 16:41:02 -0800550 }
551 }
552 setAppState_l(current->uid,
Eric Laurent1ff16a72019-03-14 18:35:04 -0700553 allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) :
554 APP_STATE_IDLE);
555 if (allowCapture) {
556 enabledUids.push_back(current->uid);
557 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700558 }
559}
560
Michael Groovercfd28302018-12-11 19:16:46 -0800561void AudioPolicyService::silenceAllRecordings_l() {
562 for (size_t i = 0; i < mAudioRecordClients.size(); i++) {
563 sp<AudioRecordClient> current = mAudioRecordClients[i];
Eric Laurent1ff16a72019-03-14 18:35:04 -0700564 if (!isVirtualSource(current->attributes.source)) {
565 setAppState_l(current->uid, APP_STATE_IDLE);
566 }
Michael Groovercfd28302018-12-11 19:16:46 -0800567 }
568}
569
Eric Laurente8c8b432018-10-17 10:08:02 -0700570/* static */
571app_state_t AudioPolicyService::apmStatFromAmState(int amState) {
Eric Laurent1ff16a72019-03-14 18:35:04 -0700572
573 if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700574 return APP_STATE_IDLE;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700575 } else if (amState <= ActivityManager::PROCESS_STATE_TOP) {
576 // include persistent services
577 return APP_STATE_TOP;
Eric Laurente8c8b432018-10-17 10:08:02 -0700578 }
579 return APP_STATE_FOREGROUND;
580}
581
Eric Laurent4eb58f12018-12-07 16:41:02 -0800582/* static */
Eric Laurent2dc962b2019-03-01 08:25:25 -0800583bool AudioPolicyService::isPrivacySensitiveSource(audio_source_t source)
584{
585 switch (source) {
586 case AUDIO_SOURCE_CAMCORDER:
587 case AUDIO_SOURCE_VOICE_COMMUNICATION:
588 return true;
589 default:
590 break;
591 }
592 return false;
593}
594
595/* static */
596bool AudioPolicyService::isVirtualSource(audio_source_t source)
Eric Laurent4eb58f12018-12-07 16:41:02 -0800597{
598 switch (source) {
599 case AUDIO_SOURCE_VOICE_UPLINK:
600 case AUDIO_SOURCE_VOICE_DOWNLINK:
601 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent2dc962b2019-03-01 08:25:25 -0800602 case AUDIO_SOURCE_REMOTE_SUBMIX:
603 case AUDIO_SOURCE_FM_TUNER:
Eric Laurent4eb58f12018-12-07 16:41:02 -0800604 return true;
605 default:
606 break;
607 }
608 return false;
609}
610
Eric Laurente8c8b432018-10-17 10:08:02 -0700611void AudioPolicyService::setAppState_l(uid_t uid, app_state_t state)
612{
613 AutoCallerClear acc;
614
615 if (mAudioPolicyManager) {
616 mAudioPolicyManager->setAppState(uid, state);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700617 }
618 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
619 if (af) {
Eric Laurentf32108e2018-10-04 17:22:04 -0700620 bool silenced = state == APP_STATE_IDLE;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700621 af->setRecordSilenced(uid, silenced);
622 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800623}
624
Glenn Kasten0f11b512014-01-31 16:18:54 -0800625status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626{
Glenn Kasten44deb052012-02-05 18:09:08 -0800627 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 dumpPermissionDenial(fd);
629 } else {
630 bool locked = tryLock(mLock);
631 if (!locked) {
632 String8 result(kDeadlockedString);
633 write(fd, result.string(), result.size());
634 }
635
636 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800637 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 mAudioCommandThread->dump(fd);
639 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640
Eric Laurentdce54a12014-03-10 12:19:46 -0700641 if (mAudioPolicyManager) {
642 mAudioPolicyManager->dump(fd);
643 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644
Kevin Rocard8be94972019-02-22 13:26:25 -0800645 mPackageManager.dump(fd);
646
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 if (locked) mLock.unlock();
648 }
649 return NO_ERROR;
650}
651
652status_t AudioPolicyService::dumpPermissionDenial(int fd)
653{
654 const size_t SIZE = 256;
655 char buffer[SIZE];
656 String8 result;
657 snprintf(buffer, SIZE, "Permission Denial: "
658 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
659 IPCThreadState::self()->getCallingPid(),
660 IPCThreadState::self()->getCallingUid());
661 result.append(buffer);
662 write(fd, result.string(), result.size());
663 return NO_ERROR;
664}
665
666status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800667 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
668 switch (code) {
669 case SHELL_COMMAND_TRANSACTION: {
670 int in = data.readFileDescriptor();
671 int out = data.readFileDescriptor();
672 int err = data.readFileDescriptor();
673 int argc = data.readInt32();
674 Vector<String16> args;
675 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
676 args.add(data.readString16());
677 }
678 sp<IBinder> unusedCallback;
679 sp<IResultReceiver> resultReceiver;
680 status_t status;
681 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
682 return status;
683 }
684 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
685 return status;
686 }
687 status = shellCommand(in, out, err, args);
688 if (resultReceiver != nullptr) {
689 resultReceiver->send(status);
690 }
691 return NO_ERROR;
692 }
693 }
694
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 return BnAudioPolicyService::onTransact(code, data, reply, flags);
696}
697
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800698// ------------------- Shell command implementation -------------------
699
700// NOTE: This is a remote API - make sure all args are validated
701status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
702 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
703 return PERMISSION_DENIED;
704 }
705 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
706 return BAD_VALUE;
707 }
708 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
709 return handleSetUidState(args, err);
710 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
711 return handleResetUidState(args, err);
712 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
713 return handleGetUidState(args, out, err);
714 } else if (args.size() == 1 && args[0] == String16("help")) {
715 printHelp(out);
716 return NO_ERROR;
717 }
718 printHelp(err);
719 return BAD_VALUE;
720}
721
722status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
723 PermissionController pc;
724 int uid = pc.getPackageUid(args[1], 0);
725 if (uid <= 0) {
726 ALOGE("Unknown package: '%s'", String8(args[1]).string());
727 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
728 return BAD_VALUE;
729 }
730 bool active = false;
731 if (args[2] == String16("active")) {
732 active = true;
733 } else if ((args[2] != String16("idle"))) {
734 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
735 return BAD_VALUE;
736 }
737 mUidPolicy->addOverrideUid(uid, active);
738 return NO_ERROR;
739}
740
741status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
742 PermissionController pc;
743 int uid = pc.getPackageUid(args[1], 0);
744 if (uid < 0) {
745 ALOGE("Unknown package: '%s'", String8(args[1]).string());
746 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
747 return BAD_VALUE;
748 }
749 mUidPolicy->removeOverrideUid(uid);
750 return NO_ERROR;
751}
752
753status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
754 PermissionController pc;
755 int uid = pc.getPackageUid(args[1], 0);
756 if (uid < 0) {
757 ALOGE("Unknown package: '%s'", String8(args[1]).string());
758 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
759 return BAD_VALUE;
760 }
761 if (mUidPolicy->isUidActive(uid)) {
762 return dprintf(out, "active\n");
763 } else {
764 return dprintf(out, "idle\n");
765 }
766}
767
768status_t AudioPolicyService::printHelp(int out) {
769 return dprintf(out, "Audio policy service commands:\n"
770 " get-uid-state <PACKAGE> gets the uid state\n"
771 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
772 " reset-uid-state <PACKAGE> clears the uid state override\n"
773 " help print this message\n");
774}
775
776// ----------- AudioPolicyService::UidPolicy implementation ----------
777
778void AudioPolicyService::UidPolicy::registerSelf() {
779 ActivityManager am;
780 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
781 | ActivityManager::UID_OBSERVER_IDLE
Eric Laurente8c8b432018-10-17 10:08:02 -0700782 | ActivityManager::UID_OBSERVER_ACTIVE
783 | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800784 ActivityManager::PROCESS_STATE_UNKNOWN,
785 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700786 status_t res = am.linkToDeath(this);
787 if (!res) {
788 Mutex::Autolock _l(mLock);
789 mObserverRegistered = true;
790 } else {
791 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800792
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700793 am.unregisterUidObserver(this);
794 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800795}
796
797void AudioPolicyService::UidPolicy::unregisterSelf() {
798 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700799 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800800 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700801 Mutex::Autolock _l(mLock);
802 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800803}
804
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700805void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
806 Mutex::Autolock _l(mLock);
807 mCachedUids.clear();
808 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800809}
810
Eric Laurente8c8b432018-10-17 10:08:02 -0700811void AudioPolicyService::UidPolicy::checkRegistered() {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700812 bool needToReregister = false;
813 {
814 Mutex::Autolock _l(mLock);
815 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800816 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700817 if (needToReregister) {
818 // Looks like ActivityManager has died previously, attempt to re-register.
819 registerSelf();
820 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700821}
822
823bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
824 if (isServiceUid(uid)) return true;
825 checkRegistered();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700826 {
827 Mutex::Autolock _l(mLock);
828 auto overrideIter = mOverrideUids.find(uid);
829 if (overrideIter != mOverrideUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700830 return overrideIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700831 }
832 // In an absense of the ActivityManager, assume everything to be active.
833 if (!mObserverRegistered) return true;
834 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700835 if (cacheIter != mCachedUids.end()) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700836 return cacheIter->second.first;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700837 }
838 }
839 ActivityManager am;
840 bool active = am.isUidActive(uid, String16("audioserver"));
841 {
842 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700843 mCachedUids.insert(std::pair<uid_t,
844 std::pair<bool, int>>(uid, std::pair<bool, int>(active,
845 ActivityManager::PROCESS_STATE_UNKNOWN)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700846 }
847 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800848}
849
Eric Laurente8c8b432018-10-17 10:08:02 -0700850int AudioPolicyService::UidPolicy::getUidState(uid_t uid) {
851 if (isServiceUid(uid)) {
852 return ActivityManager::PROCESS_STATE_TOP;
853 }
854 checkRegistered();
855 {
856 Mutex::Autolock _l(mLock);
857 auto overrideIter = mOverrideUids.find(uid);
858 if (overrideIter != mOverrideUids.end()) {
859 if (overrideIter->second.first) {
860 if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) {
861 return overrideIter->second.second;
862 } else {
863 auto cacheIter = mCachedUids.find(uid);
864 if (cacheIter != mCachedUids.end()) {
865 return cacheIter->second.second;
866 }
867 }
868 }
869 return ActivityManager::PROCESS_STATE_UNKNOWN;
870 }
871 // In an absense of the ActivityManager, assume everything to be active.
872 if (!mObserverRegistered) {
873 return ActivityManager::PROCESS_STATE_TOP;
874 }
875 auto cacheIter = mCachedUids.find(uid);
876 if (cacheIter != mCachedUids.end()) {
877 if (cacheIter->second.first) {
878 return cacheIter->second.second;
879 } else {
880 return ActivityManager::PROCESS_STATE_UNKNOWN;
881 }
882 }
883 }
884 ActivityManager am;
885 bool active = am.isUidActive(uid, String16("audioserver"));
886 int state = ActivityManager::PROCESS_STATE_UNKNOWN;
887 if (active) {
888 state = am.getUidProcessState(uid, String16("audioserver"));
889 }
890 {
891 Mutex::Autolock _l(mLock);
892 mCachedUids.insert(std::pair<uid_t,
893 std::pair<bool, int>>(uid, std::pair<bool, int>(active, state)));
894 }
Eric Laurent4eb58f12018-12-07 16:41:02 -0800895
Eric Laurente8c8b432018-10-17 10:08:02 -0700896 return state;
897}
898
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700899void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700900 updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700901}
902
903void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700904 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700905}
906
907void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700908 updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700909}
910
Eric Laurente8c8b432018-10-17 10:08:02 -0700911void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid,
912 int32_t procState,
913 int64_t procStateSeq __unused) {
914 if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) {
915 updateUid(&mCachedUids, uid, true, procState, true);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700916 }
917}
918
919void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700920 updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert);
921}
922
923void AudioPolicyService::UidPolicy::notifyService() {
924 sp<AudioPolicyService> service = mService.promote();
925 if (service != nullptr) {
926 service->updateUidStates();
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700927 }
928}
929
Eric Laurente8c8b432018-10-17 10:08:02 -0700930void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t,
931 std::pair<bool, int>> *uids,
932 uid_t uid,
933 bool active,
934 int state,
935 bool insert) {
936 if (isServiceUid(uid)) {
937 return;
938 }
939 bool wasActive = isUidActive(uid);
940 int previousState = getUidState(uid);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700941 {
942 Mutex::Autolock _l(mLock);
Eric Laurente8c8b432018-10-17 10:08:02 -0700943 updateUidLocked(uids, uid, active, state, insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700944 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700945 if (wasActive != isUidActive(uid) || state != previousState) {
946 notifyService();
947 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700948}
949
Eric Laurente8c8b432018-10-17 10:08:02 -0700950void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t,
951 std::pair<bool, int>> *uids,
952 uid_t uid,
953 bool active,
954 int state,
955 bool insert) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700956 auto it = uids->find(uid);
957 if (it != uids->end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700958 if (insert) {
Eric Laurente8c8b432018-10-17 10:08:02 -0700959 if (state == ActivityManager::PROCESS_STATE_UNKNOWN) {
960 it->second.first = active;
961 }
962 if (it->second.first) {
963 it->second.second = state;
964 } else {
965 it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN;
966 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700967 } else {
968 uids->erase(it);
969 }
Eric Laurente8c8b432018-10-17 10:08:02 -0700970 } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) {
971 uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid,
972 std::pair<bool, int>(active, state)));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700973 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800974}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975
Eric Laurent4eb58f12018-12-07 16:41:02 -0800976bool AudioPolicyService::UidPolicy::isA11yOnTop() {
977 for (const auto &uid : mCachedUids) {
978 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid.first);
979 if (it == mA11yUids.end()) {
980 continue;
981 }
Amith Yamasanibcbb3002019-01-23 13:53:33 -0800982 if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP
983 && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
Eric Laurent4eb58f12018-12-07 16:41:02 -0800984 return true;
985 }
986 }
987 return false;
988}
989
Eric Laurentb78763e2018-10-17 10:08:02 -0700990bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid)
991{
992 std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid);
993 return it != mA11yUids.end();
994}
995
Michael Groovercfd28302018-12-11 19:16:46 -0800996// ----------- AudioPolicyService::SensorPrivacyService implementation ----------
997void AudioPolicyService::SensorPrivacyPolicy::registerSelf() {
998 SensorPrivacyManager spm;
999 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
1000 spm.addSensorPrivacyListener(this);
1001}
1002
1003void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() {
1004 SensorPrivacyManager spm;
1005 spm.removeSensorPrivacyListener(this);
1006}
1007
1008bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
1009 return mSensorPrivacyEnabled;
1010}
1011
1012binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
1013 mSensorPrivacyEnabled = enabled;
1014 sp<AudioPolicyService> service = mService.promote();
1015 if (service != nullptr) {
1016 service->updateUidStates();
1017 }
1018 return binder::Status::ok();
1019}
1020
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021// ----------- AudioPolicyService::AudioCommandThread implementation ----------
1022
Eric Laurentbfb1b832013-01-07 09:53:42 -08001023AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
1024 const wp<AudioPolicyService>& service)
1025 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027}
1028
1029
1030AudioPolicyService::AudioCommandThread::~AudioCommandThread()
1031{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001032 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 release_wake_lock(mName.string());
1034 }
1035 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036}
1037
1038void AudioPolicyService::AudioCommandThread::onFirstRef()
1039{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001040 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041}
1042
1043bool AudioPolicyService::AudioCommandThread::threadLoop()
1044{
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001045 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046
1047 mLock.lock();
1048 while (!exitPending())
1049 {
Eric Laurent59a89232014-06-08 14:14:17 -07001050 sp<AudioPolicyService> svc;
1051 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 nsecs_t curTime = systemTime();
1053 // commands are sorted by increasing time stamp: execute them from index 0 and up
1054 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001055 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -07001057 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058
1059 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001061 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001062 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -07001063 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001064 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -07001065 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
1066 data->mVolume,
1067 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001068 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 }break;
1070 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001071 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001072 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
1073 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -07001074 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001075 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -07001076 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001077 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001079 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -07001081 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001082 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001083 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -07001084 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001086 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001087 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001088 ALOGV("AudioCommandThread() processing stop output portId %d",
1089 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001090 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001091 if (svc == 0) {
1092 break;
1093 }
1094 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001095 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001096 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001097 }break;
1098 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001099 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001100 ALOGV("AudioCommandThread() processing release output portId %d",
1101 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -07001102 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001103 if (svc == 0) {
1104 break;
1105 }
1106 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001107 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001108 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001109 }break;
Eric Laurent951f4552014-05-20 10:48:17 -07001110 case CREATE_AUDIO_PATCH: {
1111 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
1112 ALOGV("AudioCommandThread() processing create audio patch");
1113 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1114 if (af == 0) {
1115 command->mStatus = PERMISSION_DENIED;
1116 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001117 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001118 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001119 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001120 }
1121 } break;
1122 case RELEASE_AUDIO_PATCH: {
1123 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
1124 ALOGV("AudioCommandThread() processing release audio patch");
1125 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1126 if (af == 0) {
1127 command->mStatus = PERMISSION_DENIED;
1128 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001129 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -07001130 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -07001131 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -07001132 }
1133 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -07001134 case UPDATE_AUDIOPORT_LIST: {
1135 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -07001136 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001137 if (svc == 0) {
1138 break;
1139 }
1140 mLock.unlock();
1141 svc->doOnAudioPortListUpdate();
1142 mLock.lock();
1143 }break;
1144 case UPDATE_AUDIOPATCH_LIST: {
1145 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -07001146 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -07001147 if (svc == 0) {
1148 break;
1149 }
1150 mLock.unlock();
1151 svc->doOnAudioPatchListUpdate();
1152 mLock.lock();
1153 }break;
François Gaffiecfe17322018-11-07 13:41:29 +01001154 case CHANGED_AUDIOVOLUMEGROUP: {
1155 AudioVolumeGroupData *data =
1156 static_cast<AudioVolumeGroupData *>(command->mParam.get());
1157 ALOGV("AudioCommandThread() processing update audio volume group");
1158 svc = mService.promote();
1159 if (svc == 0) {
1160 break;
1161 }
1162 mLock.unlock();
1163 svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags);
1164 mLock.lock();
1165 }break;
Eric Laurente1715a42014-05-20 11:30:42 -07001166 case SET_AUDIOPORT_CONFIG: {
1167 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
1168 ALOGV("AudioCommandThread() processing set port config");
1169 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1170 if (af == 0) {
1171 command->mStatus = PERMISSION_DENIED;
1172 } else {
Andy Hungfe726a62018-09-27 15:17:25 -07001173 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -07001174 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -07001175 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -07001176 }
1177 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -07001178 case DYN_POLICY_MIX_STATE_UPDATE: {
1179 DynPolicyMixStateUpdateData *data =
1180 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -07001181 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
1182 data->mRegId.string(), data->mState);
1183 svc = mService.promote();
1184 if (svc == 0) {
1185 break;
1186 }
1187 mLock.unlock();
1188 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
1189 mLock.lock();
1190 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001191 case RECORDING_CONFIGURATION_UPDATE: {
1192 RecordingConfigurationUpdateData *data =
1193 (RecordingConfigurationUpdateData *)command->mParam.get();
1194 ALOGV("AudioCommandThread() processing recording configuration update");
1195 svc = mService.promote();
1196 if (svc == 0) {
1197 break;
1198 }
1199 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001200 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
Eric Laurenta9f86652018-11-28 17:23:11 -08001201 &data->mClientConfig, data->mClientEffects,
1202 &data->mDeviceConfig, data->mEffects,
1203 data->mPatchHandle, data->mSource);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001204 mLock.lock();
1205 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206 default:
Steve Block5ff1dd52012-01-05 23:22:43 +00001207 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 }
Eric Laurent0ede8922014-05-09 18:04:42 -07001209 {
1210 Mutex::Autolock _l(command->mLock);
1211 if (command->mWaitStatus) {
1212 command->mWaitStatus = false;
1213 command->mCond.signal();
1214 }
1215 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001216 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +00001217 // release mLock before releasing strong reference on the service as
1218 // AudioPolicyService destructor calls AudioCommandThread::exit() which
1219 // acquires mLock.
1220 mLock.unlock();
1221 svc.clear();
1222 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 } else {
1224 waitTime = mAudioCommands[0]->mTime - curTime;
1225 break;
1226 }
1227 }
Zach Janga754b4f2015-10-27 01:29:34 +00001228
1229 // release delayed commands wake lock if the queue is empty
1230 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001231 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +00001232 }
1233
1234 // At this stage we have either an empty command queue or the first command in the queue
1235 // has a finite delay. So unless we are exiting it is safe to wait.
1236 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -07001237 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -08001238 if (waitTime == -1) {
1239 mWaitWorkCV.wait(mLock);
1240 } else {
1241 mWaitWorkCV.waitRelative(mLock, waitTime);
1242 }
Eric Laurent59a89232014-06-08 14:14:17 -07001243 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -07001245 // release delayed commands wake lock before quitting
1246 if (!mAudioCommands.isEmpty()) {
1247 release_wake_lock(mName.string());
1248 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249 mLock.unlock();
1250 return false;
1251}
1252
1253status_t AudioPolicyService::AudioCommandThread::dump(int fd)
1254{
1255 const size_t SIZE = 256;
1256 char buffer[SIZE];
1257 String8 result;
1258
1259 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
1260 result.append(buffer);
1261 write(fd, result.string(), result.size());
1262
1263 bool locked = tryLock(mLock);
1264 if (!locked) {
1265 String8 result2(kCmdDeadlockedString);
1266 write(fd, result2.string(), result2.size());
1267 }
1268
1269 snprintf(buffer, SIZE, "- Commands:\n");
1270 result = String8(buffer);
1271 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001272 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273 mAudioCommands[i]->dump(buffer, SIZE);
1274 result.append(buffer);
1275 }
1276 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -07001277 if (mLastCommand != 0) {
1278 mLastCommand->dump(buffer, SIZE);
1279 result.append(buffer);
1280 } else {
1281 result.append(" none\n");
1282 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283
1284 write(fd, result.string(), result.size());
1285
1286 if (locked) mLock.unlock();
1287
1288 return NO_ERROR;
1289}
1290
Glenn Kastenfff6d712012-01-12 16:38:12 -08001291status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -07001292 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001293 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -07001294 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295{
Eric Laurent0ede8922014-05-09 18:04:42 -07001296 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001298 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299 data->mStream = stream;
1300 data->mVolume = volume;
1301 data->mIO = output;
1302 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001303 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -07001305 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -07001306 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307}
1308
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001309status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001310 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -07001311 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312{
Eric Laurent0ede8922014-05-09 18:04:42 -07001313 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -07001315 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -07001317 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001319 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -07001321 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -07001322 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323}
1324
1325status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
1326{
Eric Laurent0ede8922014-05-09 18:04:42 -07001327 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -07001329 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330 data->mVolume = volume;
1331 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -07001332 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +01001333 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -07001334 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335}
1336
Eric Laurentd7fe0862018-07-14 16:48:01 -07001337void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001338{
Eric Laurent0ede8922014-05-09 18:04:42 -07001339 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001340 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001341 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001342 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001343 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001344 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001345 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001346}
1347
Eric Laurentd7fe0862018-07-14 16:48:01 -07001348void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001349{
Eric Laurent0ede8922014-05-09 18:04:42 -07001350 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001351 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -07001352 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -07001353 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +01001354 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -07001355 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -07001356 sendCommand(command);
1357}
1358
Eric Laurent951f4552014-05-20 10:48:17 -07001359status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1360 const struct audio_patch *patch,
1361 audio_patch_handle_t *handle,
1362 int delayMs)
1363{
1364 status_t status = NO_ERROR;
1365
1366 sp<AudioCommand> command = new AudioCommand();
1367 command->mCommand = CREATE_AUDIO_PATCH;
1368 CreateAudioPatchData *data = new CreateAudioPatchData();
1369 data->mPatch = *patch;
1370 data->mHandle = *handle;
1371 command->mParam = data;
1372 command->mWaitStatus = true;
1373 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1374 status = sendCommand(command, delayMs);
1375 if (status == NO_ERROR) {
1376 *handle = data->mHandle;
1377 }
1378 return status;
1379}
1380
1381status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1382 int delayMs)
1383{
1384 sp<AudioCommand> command = new AudioCommand();
1385 command->mCommand = RELEASE_AUDIO_PATCH;
1386 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1387 data->mHandle = handle;
1388 command->mParam = data;
1389 command->mWaitStatus = true;
1390 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1391 return sendCommand(command, delayMs);
1392}
1393
Eric Laurentb52c1522014-05-20 11:27:36 -07001394void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1395{
1396 sp<AudioCommand> command = new AudioCommand();
1397 command->mCommand = UPDATE_AUDIOPORT_LIST;
1398 ALOGV("AudioCommandThread() adding update audio port list");
1399 sendCommand(command);
1400}
1401
1402void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1403{
1404 sp<AudioCommand>command = new AudioCommand();
1405 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1406 ALOGV("AudioCommandThread() adding update audio patch list");
1407 sendCommand(command);
1408}
1409
François Gaffiecfe17322018-11-07 13:41:29 +01001410void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group,
1411 int flags)
1412{
1413 sp<AudioCommand>command = new AudioCommand();
1414 command->mCommand = CHANGED_AUDIOVOLUMEGROUP;
1415 AudioVolumeGroupData *data= new AudioVolumeGroupData();
1416 data->mGroup = group;
1417 data->mFlags = flags;
1418 command->mParam = data;
1419 ALOGV("AudioCommandThread() adding audio volume group changed");
1420 sendCommand(command);
1421}
1422
Eric Laurente1715a42014-05-20 11:30:42 -07001423status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1424 const struct audio_port_config *config, int delayMs)
1425{
1426 sp<AudioCommand> command = new AudioCommand();
1427 command->mCommand = SET_AUDIOPORT_CONFIG;
1428 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1429 data->mConfig = *config;
1430 command->mParam = data;
1431 command->mWaitStatus = true;
1432 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1433 return sendCommand(command, delayMs);
1434}
1435
Jean-Michel Trivide801052015-04-14 19:10:14 -07001436void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001437 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001438{
1439 sp<AudioCommand> command = new AudioCommand();
1440 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1441 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1442 data->mRegId = regId;
1443 data->mState = state;
1444 command->mParam = data;
1445 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1446 regId.string(), state);
1447 sendCommand(command);
1448}
1449
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001450void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -08001451 int event,
1452 const record_client_info_t *clientInfo,
1453 const audio_config_base_t *clientConfig,
1454 std::vector<effect_descriptor_t> clientEffects,
1455 const audio_config_base_t *deviceConfig,
1456 std::vector<effect_descriptor_t> effects,
1457 audio_patch_handle_t patchHandle,
1458 audio_source_t source)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001459{
1460 sp<AudioCommand>command = new AudioCommand();
1461 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1462 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1463 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001464 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001465 data->mClientConfig = *clientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001466 data->mClientEffects = clientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001467 data->mDeviceConfig = *deviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -08001468 data->mEffects = effects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001469 data->mPatchHandle = patchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -08001470 data->mSource = source;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001471 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001472 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1473 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001474 sendCommand(command);
1475}
1476
Eric Laurent0ede8922014-05-09 18:04:42 -07001477status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1478{
1479 {
1480 Mutex::Autolock _l(mLock);
1481 insertCommand_l(command, delayMs);
1482 mWaitWorkCV.signal();
1483 }
1484 Mutex::Autolock _l(command->mLock);
1485 while (command->mWaitStatus) {
1486 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1487 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1488 command->mStatus = TIMED_OUT;
1489 command->mWaitStatus = false;
1490 }
1491 }
1492 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001493}
1494
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001496void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001498 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001499 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500 command->mTime = systemTime() + milliseconds(delayMs);
1501
1502 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001503 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1505 }
1506
1507 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001508 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001509 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001510 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1511 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001512
1513 // create audio patch or release audio patch commands are equivalent
1514 // with regard to filtering
1515 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1516 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1517 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1518 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1519 continue;
1520 }
1521 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522
1523 switch (command->mCommand) {
1524 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001525 ParametersData *data = (ParametersData *)command->mParam.get();
1526 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001528 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001529 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1531 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1532 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001533 String8 key;
1534 String8 value;
1535 param.getAt(j, key, value);
1536 for (size_t k = 0; k < param2.size(); k++) {
1537 String8 key2;
1538 String8 value2;
1539 param2.getAt(k, key2, value2);
1540 if (key2 == key) {
1541 param2.remove(key2);
1542 ALOGV("Filtering out parameter %s", key2.string());
1543 break;
1544 }
1545 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001546 }
1547 // if all keys have been filtered out, remove the command.
1548 // otherwise, update the key value pairs
1549 if (param2.size() == 0) {
1550 removedCommands.add(command2);
1551 } else {
1552 data2->mKeyValuePairs = param2.toString();
1553 }
Eric Laurent21e54562013-09-23 12:08:05 -07001554 command->mTime = command2->mTime;
1555 // force delayMs to non 0 so that code below does not request to wait for
1556 // command status as the command is now delayed
1557 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001558 } break;
1559
1560 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001561 VolumeData *data = (VolumeData *)command->mParam.get();
1562 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001563 if (data->mIO != data2->mIO) break;
1564 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001565 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001566 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001568 command->mTime = command2->mTime;
1569 // force delayMs to non 0 so that code below does not request to wait for
1570 // command status as the command is now delayed
1571 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001572 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001573
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001574 case SET_VOICE_VOLUME: {
1575 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1576 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1577 ALOGV("Filtering out voice volume command value %f replaced by %f",
1578 data2->mVolume, data->mVolume);
1579 removedCommands.add(command2);
1580 command->mTime = command2->mTime;
1581 // force delayMs to non 0 so that code below does not request to wait for
1582 // command status as the command is now delayed
1583 delayMs = 1;
1584 } break;
1585
Eric Laurente45b48a2014-09-04 16:40:57 -07001586 case CREATE_AUDIO_PATCH:
1587 case RELEASE_AUDIO_PATCH: {
1588 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001589 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001590 if (command->mCommand == CREATE_AUDIO_PATCH) {
1591 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001592 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001593 } else {
1594 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001595 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001596 }
1597 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001598 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001599 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1600 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001601 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001602 } else {
1603 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001604 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001605 }
1606 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001607 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1608 same output. */
1609 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1610 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1611 bool isOutputDiff = false;
1612 if (patch.num_sources == patch2.num_sources) {
1613 for (unsigned count = 0; count < patch.num_sources; count++) {
1614 if (patch.sources[count].id != patch2.sources[count].id) {
1615 isOutputDiff = true;
1616 break;
1617 }
1618 }
1619 if (isOutputDiff)
1620 break;
1621 }
1622 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001623 ALOGV("Filtering out %s audio patch command for handle %d",
1624 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1625 removedCommands.add(command2);
1626 command->mTime = command2->mTime;
1627 // force delayMs to non 0 so that code below does not request to wait for
1628 // command status as the command is now delayed
1629 delayMs = 1;
1630 } break;
1631
Jean-Michel Trivide801052015-04-14 19:10:14 -07001632 case DYN_POLICY_MIX_STATE_UPDATE: {
1633
1634 } break;
1635
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001636 case RECORDING_CONFIGURATION_UPDATE: {
1637
1638 } break;
1639
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 default:
1641 break;
1642 }
1643 }
1644
1645 // remove filtered commands
1646 for (size_t j = 0; j < removedCommands.size(); j++) {
1647 // removed commands always have time stamps greater than current command
1648 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001649 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001650 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 mAudioCommands.removeAt(k);
1652 break;
1653 }
1654 }
1655 }
1656 removedCommands.clear();
1657
Eric Laurentaa79bef2015-01-15 14:33:51 -08001658 // Disable wait for status if delay is not 0.
1659 // Except for create audio patch command because the returned patch handle
1660 // is needed by audio policy manager
1661 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001662 command->mWaitStatus = false;
1663 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001664
Mathias Agopian65ab4712010-07-14 17:59:35 -07001665 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001666 ALOGV("inserting command: %d at index %zd, num commands %zu",
1667 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668 mAudioCommands.insertAt(command, i + 1);
1669}
1670
1671void AudioPolicyService::AudioCommandThread::exit()
1672{
Steve Block3856b092011-10-20 11:56:00 +01001673 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001674 {
1675 AutoMutex _l(mLock);
1676 requestExit();
1677 mWaitWorkCV.signal();
1678 }
Zach Janga754b4f2015-10-27 01:29:34 +00001679 // Note that we can call it from the thread loop if all other references have been released
1680 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 requestExitAndWait();
1682}
1683
1684void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1685{
1686 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1687 mCommand,
1688 (int)ns2s(mTime),
1689 (int)ns2ms(mTime)%1000,
1690 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001691 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692}
1693
Dima Zavinfce7a472011-04-19 22:30:36 -07001694/******* helpers for the service_ops callbacks defined below *********/
1695void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1696 const char *keyValuePairs,
1697 int delayMs)
1698{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001699 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001700 delayMs);
1701}
1702
1703int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1704 float volume,
1705 audio_io_handle_t output,
1706 int delayMs)
1707{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001708 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001709 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001710}
1711
Dima Zavinfce7a472011-04-19 22:30:36 -07001712int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1713{
1714 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1715}
1716
Dima Zavinfce7a472011-04-19 22:30:36 -07001717extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001718audio_module_handle_t aps_load_hw_module(void *service __unused,
1719 const char *name);
1720audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001721 audio_devices_t *pDevices,
1722 uint32_t *pSamplingRate,
1723 audio_format_t *pFormat,
1724 audio_channel_mask_t *pChannelMask,
1725 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001726 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001727
Eric Laurent2d388ec2014-03-07 13:25:54 -08001728audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001729 audio_module_handle_t module,
1730 audio_devices_t *pDevices,
1731 uint32_t *pSamplingRate,
1732 audio_format_t *pFormat,
1733 audio_channel_mask_t *pChannelMask,
1734 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001735 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001736 const audio_offload_info_t *offloadInfo);
1737audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001738 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001739 audio_io_handle_t output2);
1740int aps_close_output(void *service __unused, audio_io_handle_t output);
1741int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1742int aps_restore_output(void *service __unused, audio_io_handle_t output);
1743audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001744 audio_devices_t *pDevices,
1745 uint32_t *pSamplingRate,
1746 audio_format_t *pFormat,
1747 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001748 audio_in_acoustics_t acoustics __unused);
1749audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001750 audio_module_handle_t module,
1751 audio_devices_t *pDevices,
1752 uint32_t *pSamplingRate,
1753 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001754 audio_channel_mask_t *pChannelMask);
1755int aps_close_input(void *service __unused, audio_io_handle_t input);
1756int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001757int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001758 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001759 audio_io_handle_t dst_output);
1760char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1761 const char *keys);
1762void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1763 const char *kv_pairs, int delay_ms);
1764int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001765 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001766 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001767int aps_set_voice_volume(void *service, float volume, int delay_ms);
1768};
Dima Zavinfce7a472011-04-19 22:30:36 -07001769
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001770} // namespace android