blob: b1ed522c7d53255eab2712cde3733297ca0efdfe [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start audio commands thread
71 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
72 // start output activity command thread
73 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070074
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 mAudioPolicyClient = new AudioPolicyClient(this);
76 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 }
bryant_liuba2b4392014-06-11 16:49:30 +080078 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
80 {
81 Mutex::Autolock _l(mLock);
82 mAudioPolicyEffects = audioPolicyEffects;
83 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080084
85 mUidPolicy = new UidPolicy(this);
86 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070087}
88
89AudioPolicyService::~AudioPolicyService()
90{
Mathias Agopian65ab4712010-07-14 17:59:35 -070091 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070092 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070093
Eric Laurentf269b8e2014-06-09 20:01:29 -070094 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070095 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070096
97 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +080098 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -080099
100 mUidPolicy->unregisterSelf();
101 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700102}
103
104// A notification client is always registered by AudioSystem when the client process
105// connects to AudioPolicyService.
106void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
107{
Eric Laurent12590252015-08-21 18:40:20 -0700108 if (client == 0) {
109 ALOGW("%s got NULL client", __FUNCTION__);
110 return;
111 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800112 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700113
114 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800115 pid_t pid = IPCThreadState::self()->getCallingPid();
116 int64_t token = ((int64_t)uid<<32) | pid;
117
118 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700119 sp<NotificationClient> notificationClient = new NotificationClient(this,
120 client,
luochaojiang908c7d72018-06-21 14:58:04 +0800121 uid,
122 pid);
123 ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700124
luochaojiang908c7d72018-06-21 14:58:04 +0800125 mNotificationClients.add(token, notificationClient);
Eric Laurentb52c1522014-05-20 11:27:36 -0700126
Marco Nelissenf8880202014-11-14 07:58:25 -0800127 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700128 binder->linkToDeath(notificationClient);
129 }
130}
131
Eric Laurente8726fe2015-06-26 09:39:24 -0700132void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
133{
134 Mutex::Autolock _l(mNotificationClientsLock);
135
136 uid_t uid = IPCThreadState::self()->getCallingUid();
luochaojiang908c7d72018-06-21 14:58:04 +0800137 pid_t pid = IPCThreadState::self()->getCallingPid();
138 int64_t token = ((int64_t)uid<<32) | pid;
139
140 if (mNotificationClients.indexOfKey(token) < 0) {
Eric Laurente8726fe2015-06-26 09:39:24 -0700141 return;
142 }
luochaojiang908c7d72018-06-21 14:58:04 +0800143 mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled);
Eric Laurente8726fe2015-06-26 09:39:24 -0700144}
145
Eric Laurentb52c1522014-05-20 11:27:36 -0700146// removeNotificationClient() is called when the client process dies.
luochaojiang908c7d72018-06-21 14:58:04 +0800147void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid)
Eric Laurentb52c1522014-05-20 11:27:36 -0700148{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800149 {
150 Mutex::Autolock _l(mNotificationClientsLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800151 int64_t token = ((int64_t)uid<<32) | pid;
152 mNotificationClients.removeItem(token);
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800153 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800154 {
155 Mutex::Autolock _l(mLock);
luochaojiang908c7d72018-06-21 14:58:04 +0800156 bool hasSameUid = false;
157 for (size_t i = 0; i < mNotificationClients.size(); i++) {
158 if (mNotificationClients.valueAt(i)->uid() == uid) {
159 hasSameUid = true;
160 break;
161 }
162 }
163 if (mAudioPolicyManager && !hasSameUid) {
Eric Laurent10b71232018-04-13 18:14:44 -0700164 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700165 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700166 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800167 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700168}
169
170void AudioPolicyService::onAudioPortListUpdate()
171{
172 mOutputCommandThread->updateAudioPortListCommand();
173}
174
175void AudioPolicyService::doOnAudioPortListUpdate()
176{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800177 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700178 for (size_t i = 0; i < mNotificationClients.size(); i++) {
179 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
180 }
181}
182
183void AudioPolicyService::onAudioPatchListUpdate()
184{
185 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186}
187
Eric Laurentb52c1522014-05-20 11:27:36 -0700188void AudioPolicyService::doOnAudioPatchListUpdate()
189{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800190 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700191 for (size_t i = 0; i < mNotificationClients.size(); i++) {
192 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
193 }
194}
195
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700196void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700197{
198 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
199 regId.string(), state);
200 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
201}
202
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700203void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700204{
205 Mutex::Autolock _l(mNotificationClientsLock);
206 for (size_t i = 0; i < mNotificationClients.size(); i++) {
207 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
208 }
209}
210
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800211void AudioPolicyService::onRecordingConfigurationUpdate(int event,
212 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800213 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800214{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800215 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800216 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800217}
218
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800219void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
220 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800221 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800222{
223 Mutex::Autolock _l(mNotificationClientsLock);
224 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800225 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800226 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800227 }
228}
229
230status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
231 audio_patch_handle_t *handle,
232 int delayMs)
233{
234 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
235}
236
237status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
238 int delayMs)
239{
240 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
241}
242
Eric Laurente1715a42014-05-20 11:30:42 -0700243status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
244 int delayMs)
245{
246 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
247}
248
Eric Laurentb52c1522014-05-20 11:27:36 -0700249AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
250 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800251 uid_t uid,
252 pid_t pid)
253 : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client),
Eric Laurente8726fe2015-06-26 09:39:24 -0700254 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700255{
256}
257
258AudioPolicyService::NotificationClient::~NotificationClient()
259{
260}
261
262void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
263{
264 sp<NotificationClient> keep(this);
265 sp<AudioPolicyService> service = mService.promote();
266 if (service != 0) {
luochaojiang908c7d72018-06-21 14:58:04 +0800267 service->removeNotificationClient(mUid, mPid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700268 }
269}
270
271void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
272{
Eric Laurente8726fe2015-06-26 09:39:24 -0700273 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700274 mAudioPolicyServiceClient->onAudioPortListUpdate();
275 }
276}
277
278void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
279{
Eric Laurente8726fe2015-06-26 09:39:24 -0700280 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700281 mAudioPolicyServiceClient->onAudioPatchListUpdate();
282 }
283}
Eric Laurent57dae992011-07-24 13:36:09 -0700284
Jean-Michel Trivide801052015-04-14 19:10:14 -0700285void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700286 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700287{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700288 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800289 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
290 }
291}
292
293void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800294 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800295 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
296 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800297{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700298 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800299 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800300 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700301 }
302}
303
Eric Laurente8726fe2015-06-26 09:39:24 -0700304void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
305{
306 mAudioPortCallbacksEnabled = enabled;
307}
308
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700311 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700312 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313}
314
315static bool tryLock(Mutex& mutex)
316{
317 bool locked = false;
318 for (int i = 0; i < kDumpLockRetries; ++i) {
319 if (mutex.tryLock() == NO_ERROR) {
320 locked = true;
321 break;
322 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800323 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 return locked;
326}
327
328status_t AudioPolicyService::dumpInternals(int fd)
329{
330 const size_t SIZE = 256;
331 char buffer[SIZE];
332 String8 result;
333
Eric Laurentdce54a12014-03-10 12:19:46 -0700334 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 result.append(buffer);
336 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
337 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338
339 write(fd, result.string(), result.size());
340 return NO_ERROR;
341}
342
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800343void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
344{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700345 {
346 Mutex::Autolock _l(mLock);
347 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700348 AutoCallerClear acc;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700349 mAudioPolicyManager->setRecordSilenced(uid, silenced);
350 }
351 }
352 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
353 if (af) {
354 af->setRecordSilenced(uid, silenced);
355 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800356}
357
Glenn Kasten0f11b512014-01-31 16:18:54 -0800358status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359{
Glenn Kasten44deb052012-02-05 18:09:08 -0800360 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 dumpPermissionDenial(fd);
362 } else {
363 bool locked = tryLock(mLock);
364 if (!locked) {
365 String8 result(kDeadlockedString);
366 write(fd, result.string(), result.size());
367 }
368
369 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800370 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 mAudioCommandThread->dump(fd);
372 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373
Eric Laurentdce54a12014-03-10 12:19:46 -0700374 if (mAudioPolicyManager) {
375 mAudioPolicyManager->dump(fd);
376 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377
378 if (locked) mLock.unlock();
379 }
380 return NO_ERROR;
381}
382
383status_t AudioPolicyService::dumpPermissionDenial(int fd)
384{
385 const size_t SIZE = 256;
386 char buffer[SIZE];
387 String8 result;
388 snprintf(buffer, SIZE, "Permission Denial: "
389 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
390 IPCThreadState::self()->getCallingPid(),
391 IPCThreadState::self()->getCallingUid());
392 result.append(buffer);
393 write(fd, result.string(), result.size());
394 return NO_ERROR;
395}
396
397status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800398 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
399 switch (code) {
400 case SHELL_COMMAND_TRANSACTION: {
401 int in = data.readFileDescriptor();
402 int out = data.readFileDescriptor();
403 int err = data.readFileDescriptor();
404 int argc = data.readInt32();
405 Vector<String16> args;
406 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
407 args.add(data.readString16());
408 }
409 sp<IBinder> unusedCallback;
410 sp<IResultReceiver> resultReceiver;
411 status_t status;
412 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
413 return status;
414 }
415 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
416 return status;
417 }
418 status = shellCommand(in, out, err, args);
419 if (resultReceiver != nullptr) {
420 resultReceiver->send(status);
421 }
422 return NO_ERROR;
423 }
424 }
425
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 return BnAudioPolicyService::onTransact(code, data, reply, flags);
427}
428
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800429// ------------------- Shell command implementation -------------------
430
431// NOTE: This is a remote API - make sure all args are validated
432status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
433 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
434 return PERMISSION_DENIED;
435 }
436 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
437 return BAD_VALUE;
438 }
439 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
440 return handleSetUidState(args, err);
441 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
442 return handleResetUidState(args, err);
443 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
444 return handleGetUidState(args, out, err);
445 } else if (args.size() == 1 && args[0] == String16("help")) {
446 printHelp(out);
447 return NO_ERROR;
448 }
449 printHelp(err);
450 return BAD_VALUE;
451}
452
453status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
454 PermissionController pc;
455 int uid = pc.getPackageUid(args[1], 0);
456 if (uid <= 0) {
457 ALOGE("Unknown package: '%s'", String8(args[1]).string());
458 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
459 return BAD_VALUE;
460 }
461 bool active = false;
462 if (args[2] == String16("active")) {
463 active = true;
464 } else if ((args[2] != String16("idle"))) {
465 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
466 return BAD_VALUE;
467 }
468 mUidPolicy->addOverrideUid(uid, active);
469 return NO_ERROR;
470}
471
472status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
473 PermissionController pc;
474 int uid = pc.getPackageUid(args[1], 0);
475 if (uid < 0) {
476 ALOGE("Unknown package: '%s'", String8(args[1]).string());
477 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
478 return BAD_VALUE;
479 }
480 mUidPolicy->removeOverrideUid(uid);
481 return NO_ERROR;
482}
483
484status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
485 PermissionController pc;
486 int uid = pc.getPackageUid(args[1], 0);
487 if (uid < 0) {
488 ALOGE("Unknown package: '%s'", String8(args[1]).string());
489 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
490 return BAD_VALUE;
491 }
492 if (mUidPolicy->isUidActive(uid)) {
493 return dprintf(out, "active\n");
494 } else {
495 return dprintf(out, "idle\n");
496 }
497}
498
499status_t AudioPolicyService::printHelp(int out) {
500 return dprintf(out, "Audio policy service commands:\n"
501 " get-uid-state <PACKAGE> gets the uid state\n"
502 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
503 " reset-uid-state <PACKAGE> clears the uid state override\n"
504 " help print this message\n");
505}
506
507// ----------- AudioPolicyService::UidPolicy implementation ----------
508
509void AudioPolicyService::UidPolicy::registerSelf() {
510 ActivityManager am;
511 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
512 | ActivityManager::UID_OBSERVER_IDLE
513 | ActivityManager::UID_OBSERVER_ACTIVE,
514 ActivityManager::PROCESS_STATE_UNKNOWN,
515 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700516 status_t res = am.linkToDeath(this);
517 if (!res) {
518 Mutex::Autolock _l(mLock);
519 mObserverRegistered = true;
520 } else {
521 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
522 am.unregisterUidObserver(this);
523 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800524}
525
526void AudioPolicyService::UidPolicy::unregisterSelf() {
527 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700528 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800529 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700530 Mutex::Autolock _l(mLock);
531 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800532}
533
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700534void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
535 Mutex::Autolock _l(mLock);
536 mCachedUids.clear();
537 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800538}
539
540bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700541 if (isServiceUid(uid)) return true;
542 bool needToReregister = false;
543 {
544 Mutex::Autolock _l(mLock);
545 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800546 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700547 if (needToReregister) {
548 // Looks like ActivityManager has died previously, attempt to re-register.
549 registerSelf();
550 }
551 {
552 Mutex::Autolock _l(mLock);
553 auto overrideIter = mOverrideUids.find(uid);
554 if (overrideIter != mOverrideUids.end()) {
555 return overrideIter->second;
556 }
557 // In an absense of the ActivityManager, assume everything to be active.
558 if (!mObserverRegistered) return true;
559 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700560 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700561 return cacheIter->second;
562 }
563 }
564 ActivityManager am;
565 bool active = am.isUidActive(uid, String16("audioserver"));
566 {
567 Mutex::Autolock _l(mLock);
568 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
569 }
570 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800571}
572
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700573void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
574 updateUidCache(uid, true, true);
575}
576
577void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
578 updateUidCache(uid, false, false);
579}
580
581void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
582 updateUidCache(uid, false, true);
583}
584
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700585void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
586 sp<AudioPolicyService> service = mService.promote();
587 if (service != nullptr) {
588 service->setRecordSilenced(uid, !active);
589 }
590}
591
592void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
593 if (isServiceUid(uid)) return;
594 bool wasOverridden = false, wasActive = false;
595 {
596 Mutex::Autolock _l(mLock);
597 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
598 }
599 if (!wasOverridden && insert) {
600 notifyService(uid, active); // Started to override.
601 } else if (wasOverridden && !insert) {
602 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
603 } else if (wasActive != active) {
604 notifyService(uid, active); // Override updated.
605 }
606}
607
608void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
609 if (isServiceUid(uid)) return;
610 bool wasActive = false;
611 {
612 Mutex::Autolock _l(mLock);
613 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
614 // Do not notify service if currently overridden.
615 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
616 }
617 bool nowActive = active && insert;
618 if (wasActive != nowActive) notifyService(uid, nowActive);
619}
620
621void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
622 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
623 auto it = uids->find(uid);
624 if (it != uids->end()) {
625 if (wasThere != nullptr) *wasThere = true;
626 if (wasActive != nullptr) *wasActive = it->second;
627 if (insert) {
628 it->second = active;
629 } else {
630 uids->erase(it);
631 }
632 } else if (insert) {
633 uids->insert(std::pair<uid_t, bool>(uid, active));
634 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800635}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637// ----------- AudioPolicyService::AudioCommandThread implementation ----------
638
Eric Laurentbfb1b832013-01-07 09:53:42 -0800639AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
640 const wp<AudioPolicyService>& service)
641 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643}
644
645
646AudioPolicyService::AudioCommandThread::~AudioCommandThread()
647{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 release_wake_lock(mName.string());
650 }
651 mAudioCommands.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652}
653
654void AudioPolicyService::AudioCommandThread::onFirstRef()
655{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800656 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657}
658
659bool AudioPolicyService::AudioCommandThread::threadLoop()
660{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800661 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662
663 mLock.lock();
664 while (!exitPending())
665 {
Eric Laurent59a89232014-06-08 14:14:17 -0700666 sp<AudioPolicyService> svc;
667 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 nsecs_t curTime = systemTime();
669 // commands are sorted by increasing time stamp: execute them from index 0 and up
670 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700673 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674
675 switch (command->mCommand) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700677 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700679 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700680 mLock.unlock();
Eric Laurentde070132010-07-13 04:45:46 -0700681 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
682 data->mVolume,
683 data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700684 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 }break;
686 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700687 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700688 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
689 data->mKeyValuePairs.string(), data->mIO);
Andy Hungfe726a62018-09-27 15:17:25 -0700690 mLock.unlock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700691 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Andy Hungfe726a62018-09-27 15:17:25 -0700692 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700693 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700695 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700697 data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700698 mLock.unlock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Andy Hungfe726a62018-09-27 15:17:25 -0700700 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800702 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700703 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700704 ALOGV("AudioCommandThread() processing stop output portId %d",
705 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700706 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800707 if (svc == 0) {
708 break;
709 }
710 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700711 svc->doStopOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800712 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800713 }break;
714 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700715 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700716 ALOGV("AudioCommandThread() processing release output portId %d",
717 data->mPortId);
Eric Laurent59a89232014-06-08 14:14:17 -0700718 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800719 if (svc == 0) {
720 break;
721 }
722 mLock.unlock();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700723 svc->doReleaseOutput(data->mPortId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800725 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700726 case CREATE_AUDIO_PATCH: {
727 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
728 ALOGV("AudioCommandThread() processing create audio patch");
729 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
730 if (af == 0) {
731 command->mStatus = PERMISSION_DENIED;
732 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700733 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700734 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700735 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700736 }
737 } break;
738 case RELEASE_AUDIO_PATCH: {
739 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
740 ALOGV("AudioCommandThread() processing release audio patch");
741 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
742 if (af == 0) {
743 command->mStatus = PERMISSION_DENIED;
744 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700745 mLock.unlock();
Eric Laurent951f4552014-05-20 10:48:17 -0700746 command->mStatus = af->releaseAudioPatch(data->mHandle);
Andy Hungfe726a62018-09-27 15:17:25 -0700747 mLock.lock();
Eric Laurent951f4552014-05-20 10:48:17 -0700748 }
749 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700750 case UPDATE_AUDIOPORT_LIST: {
751 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700752 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700753 if (svc == 0) {
754 break;
755 }
756 mLock.unlock();
757 svc->doOnAudioPortListUpdate();
758 mLock.lock();
759 }break;
760 case UPDATE_AUDIOPATCH_LIST: {
761 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700762 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700763 if (svc == 0) {
764 break;
765 }
766 mLock.unlock();
767 svc->doOnAudioPatchListUpdate();
768 mLock.lock();
769 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700770 case SET_AUDIOPORT_CONFIG: {
771 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
772 ALOGV("AudioCommandThread() processing set port config");
773 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
774 if (af == 0) {
775 command->mStatus = PERMISSION_DENIED;
776 } else {
Andy Hungfe726a62018-09-27 15:17:25 -0700777 mLock.unlock();
Eric Laurente1715a42014-05-20 11:30:42 -0700778 command->mStatus = af->setAudioPortConfig(&data->mConfig);
Andy Hungfe726a62018-09-27 15:17:25 -0700779 mLock.lock();
Eric Laurente1715a42014-05-20 11:30:42 -0700780 }
781 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700782 case DYN_POLICY_MIX_STATE_UPDATE: {
783 DynPolicyMixStateUpdateData *data =
784 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700785 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
786 data->mRegId.string(), data->mState);
787 svc = mService.promote();
788 if (svc == 0) {
789 break;
790 }
791 mLock.unlock();
792 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
793 mLock.lock();
794 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800795 case RECORDING_CONFIGURATION_UPDATE: {
796 RecordingConfigurationUpdateData *data =
797 (RecordingConfigurationUpdateData *)command->mParam.get();
798 ALOGV("AudioCommandThread() processing recording configuration update");
799 svc = mService.promote();
800 if (svc == 0) {
801 break;
802 }
803 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800804 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
805 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800806 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800807 mLock.lock();
808 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000810 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700812 {
813 Mutex::Autolock _l(command->mLock);
814 if (command->mWaitStatus) {
815 command->mWaitStatus = false;
816 command->mCond.signal();
817 }
818 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800819 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000820 // release mLock before releasing strong reference on the service as
821 // AudioPolicyService destructor calls AudioCommandThread::exit() which
822 // acquires mLock.
823 mLock.unlock();
824 svc.clear();
825 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 } else {
827 waitTime = mAudioCommands[0]->mTime - curTime;
828 break;
829 }
830 }
Zach Janga754b4f2015-10-27 01:29:34 +0000831
832 // release delayed commands wake lock if the queue is empty
833 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700834 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000835 }
836
837 // At this stage we have either an empty command queue or the first command in the queue
838 // has a finite delay. So unless we are exiting it is safe to wait.
839 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700840 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800841 if (waitTime == -1) {
842 mWaitWorkCV.wait(mLock);
843 } else {
844 mWaitWorkCV.waitRelative(mLock, waitTime);
845 }
Eric Laurent59a89232014-06-08 14:14:17 -0700846 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700848 // release delayed commands wake lock before quitting
849 if (!mAudioCommands.isEmpty()) {
850 release_wake_lock(mName.string());
851 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 mLock.unlock();
853 return false;
854}
855
856status_t AudioPolicyService::AudioCommandThread::dump(int fd)
857{
858 const size_t SIZE = 256;
859 char buffer[SIZE];
860 String8 result;
861
862 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
863 result.append(buffer);
864 write(fd, result.string(), result.size());
865
866 bool locked = tryLock(mLock);
867 if (!locked) {
868 String8 result2(kCmdDeadlockedString);
869 write(fd, result2.string(), result2.size());
870 }
871
872 snprintf(buffer, SIZE, "- Commands:\n");
873 result = String8(buffer);
874 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800875 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 mAudioCommands[i]->dump(buffer, SIZE);
877 result.append(buffer);
878 }
879 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700880 if (mLastCommand != 0) {
881 mLastCommand->dump(buffer, SIZE);
882 result.append(buffer);
883 } else {
884 result.append(" none\n");
885 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886
887 write(fd, result.string(), result.size());
888
889 if (locked) mLock.unlock();
890
891 return NO_ERROR;
892}
893
Glenn Kastenfff6d712012-01-12 16:38:12 -0800894status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700895 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800896 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700897 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898{
Eric Laurent0ede8922014-05-09 18:04:42 -0700899 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700901 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 data->mStream = stream;
903 data->mVolume = volume;
904 data->mIO = output;
905 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700906 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100907 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700908 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700909 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910}
911
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800912status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700913 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700914 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915{
Eric Laurent0ede8922014-05-09 18:04:42 -0700916 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700918 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700920 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700922 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700924 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700925 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926}
927
928status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
929{
Eric Laurent0ede8922014-05-09 18:04:42 -0700930 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700932 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 data->mVolume = volume;
934 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700935 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700937 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938}
939
Eric Laurentd7fe0862018-07-14 16:48:01 -0700940void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800941{
Eric Laurent0ede8922014-05-09 18:04:42 -0700942 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800943 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700944 sp<StopOutputData> data = new StopOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700945 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100946 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700947 ALOGV("AudioCommandThread() adding stop output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -0700948 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800949}
950
Eric Laurentd7fe0862018-07-14 16:48:01 -0700951void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800952{
Eric Laurent0ede8922014-05-09 18:04:42 -0700953 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800954 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700955 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentd7fe0862018-07-14 16:48:01 -0700956 data->mPortId = portId;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100957 command->mParam = data;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700958 ALOGV("AudioCommandThread() adding release output portId %d", portId);
Eric Laurent0ede8922014-05-09 18:04:42 -0700959 sendCommand(command);
960}
961
Eric Laurent951f4552014-05-20 10:48:17 -0700962status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
963 const struct audio_patch *patch,
964 audio_patch_handle_t *handle,
965 int delayMs)
966{
967 status_t status = NO_ERROR;
968
969 sp<AudioCommand> command = new AudioCommand();
970 command->mCommand = CREATE_AUDIO_PATCH;
971 CreateAudioPatchData *data = new CreateAudioPatchData();
972 data->mPatch = *patch;
973 data->mHandle = *handle;
974 command->mParam = data;
975 command->mWaitStatus = true;
976 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
977 status = sendCommand(command, delayMs);
978 if (status == NO_ERROR) {
979 *handle = data->mHandle;
980 }
981 return status;
982}
983
984status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
985 int delayMs)
986{
987 sp<AudioCommand> command = new AudioCommand();
988 command->mCommand = RELEASE_AUDIO_PATCH;
989 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
990 data->mHandle = handle;
991 command->mParam = data;
992 command->mWaitStatus = true;
993 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
994 return sendCommand(command, delayMs);
995}
996
Eric Laurentb52c1522014-05-20 11:27:36 -0700997void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
998{
999 sp<AudioCommand> command = new AudioCommand();
1000 command->mCommand = UPDATE_AUDIOPORT_LIST;
1001 ALOGV("AudioCommandThread() adding update audio port list");
1002 sendCommand(command);
1003}
1004
1005void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1006{
1007 sp<AudioCommand>command = new AudioCommand();
1008 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1009 ALOGV("AudioCommandThread() adding update audio patch list");
1010 sendCommand(command);
1011}
1012
Eric Laurente1715a42014-05-20 11:30:42 -07001013status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1014 const struct audio_port_config *config, int delayMs)
1015{
1016 sp<AudioCommand> command = new AudioCommand();
1017 command->mCommand = SET_AUDIOPORT_CONFIG;
1018 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1019 data->mConfig = *config;
1020 command->mParam = data;
1021 command->mWaitStatus = true;
1022 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1023 return sendCommand(command, delayMs);
1024}
1025
Jean-Michel Trivide801052015-04-14 19:10:14 -07001026void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001027 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001028{
1029 sp<AudioCommand> command = new AudioCommand();
1030 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1031 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1032 data->mRegId = regId;
1033 data->mState = state;
1034 command->mParam = data;
1035 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1036 regId.string(), state);
1037 sendCommand(command);
1038}
1039
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001040void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001041 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001042 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1043 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001044{
1045 sp<AudioCommand>command = new AudioCommand();
1046 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1047 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1048 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001049 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001050 data->mClientConfig = *clientConfig;
1051 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001052 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001053 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001054 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1055 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001056 sendCommand(command);
1057}
1058
Eric Laurent0ede8922014-05-09 18:04:42 -07001059status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1060{
1061 {
1062 Mutex::Autolock _l(mLock);
1063 insertCommand_l(command, delayMs);
1064 mWaitWorkCV.signal();
1065 }
1066 Mutex::Autolock _l(command->mLock);
1067 while (command->mWaitStatus) {
1068 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1069 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1070 command->mStatus = TIMED_OUT;
1071 command->mWaitStatus = false;
1072 }
1073 }
1074 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001075}
1076
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001078void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001080 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001081 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082 command->mTime = systemTime() + milliseconds(delayMs);
1083
1084 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001085 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1087 }
1088
1089 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001090 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001091 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1093 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001094
1095 // create audio patch or release audio patch commands are equivalent
1096 // with regard to filtering
1097 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1098 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1099 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1100 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1101 continue;
1102 }
1103 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104
1105 switch (command->mCommand) {
1106 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001107 ParametersData *data = (ParametersData *)command->mParam.get();
1108 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001111 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1113 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1114 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001115 String8 key;
1116 String8 value;
1117 param.getAt(j, key, value);
1118 for (size_t k = 0; k < param2.size(); k++) {
1119 String8 key2;
1120 String8 value2;
1121 param2.getAt(k, key2, value2);
1122 if (key2 == key) {
1123 param2.remove(key2);
1124 ALOGV("Filtering out parameter %s", key2.string());
1125 break;
1126 }
1127 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 }
1129 // if all keys have been filtered out, remove the command.
1130 // otherwise, update the key value pairs
1131 if (param2.size() == 0) {
1132 removedCommands.add(command2);
1133 } else {
1134 data2->mKeyValuePairs = param2.toString();
1135 }
Eric Laurent21e54562013-09-23 12:08:05 -07001136 command->mTime = command2->mTime;
1137 // force delayMs to non 0 so that code below does not request to wait for
1138 // command status as the command is now delayed
1139 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 } break;
1141
1142 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001143 VolumeData *data = (VolumeData *)command->mParam.get();
1144 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 if (data->mIO != data2->mIO) break;
1146 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001148 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001150 command->mTime = command2->mTime;
1151 // force delayMs to non 0 so that code below does not request to wait for
1152 // command status as the command is now delayed
1153 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001155
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001156 case SET_VOICE_VOLUME: {
1157 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1158 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1159 ALOGV("Filtering out voice volume command value %f replaced by %f",
1160 data2->mVolume, data->mVolume);
1161 removedCommands.add(command2);
1162 command->mTime = command2->mTime;
1163 // force delayMs to non 0 so that code below does not request to wait for
1164 // command status as the command is now delayed
1165 delayMs = 1;
1166 } break;
1167
Eric Laurente45b48a2014-09-04 16:40:57 -07001168 case CREATE_AUDIO_PATCH:
1169 case RELEASE_AUDIO_PATCH: {
1170 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001171 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001172 if (command->mCommand == CREATE_AUDIO_PATCH) {
1173 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001174 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001175 } else {
1176 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
Mikhail Naganov7be71d22018-05-23 16:51:46 -07001177 memset(&patch, 0, sizeof(patch));
Eric Laurente45b48a2014-09-04 16:40:57 -07001178 }
1179 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001180 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001181 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1182 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001183 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001184 } else {
1185 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001186 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001187 }
1188 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001189 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1190 same output. */
1191 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1192 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1193 bool isOutputDiff = false;
1194 if (patch.num_sources == patch2.num_sources) {
1195 for (unsigned count = 0; count < patch.num_sources; count++) {
1196 if (patch.sources[count].id != patch2.sources[count].id) {
1197 isOutputDiff = true;
1198 break;
1199 }
1200 }
1201 if (isOutputDiff)
1202 break;
1203 }
1204 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001205 ALOGV("Filtering out %s audio patch command for handle %d",
1206 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1207 removedCommands.add(command2);
1208 command->mTime = command2->mTime;
1209 // force delayMs to non 0 so that code below does not request to wait for
1210 // command status as the command is now delayed
1211 delayMs = 1;
1212 } break;
1213
Jean-Michel Trivide801052015-04-14 19:10:14 -07001214 case DYN_POLICY_MIX_STATE_UPDATE: {
1215
1216 } break;
1217
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001218 case RECORDING_CONFIGURATION_UPDATE: {
1219
1220 } break;
1221
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222 default:
1223 break;
1224 }
1225 }
1226
1227 // remove filtered commands
1228 for (size_t j = 0; j < removedCommands.size(); j++) {
1229 // removed commands always have time stamps greater than current command
1230 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001231 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001232 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 mAudioCommands.removeAt(k);
1234 break;
1235 }
1236 }
1237 }
1238 removedCommands.clear();
1239
Eric Laurentaa79bef2015-01-15 14:33:51 -08001240 // Disable wait for status if delay is not 0.
1241 // Except for create audio patch command because the returned patch handle
1242 // is needed by audio policy manager
1243 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001244 command->mWaitStatus = false;
1245 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001246
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001248 ALOGV("inserting command: %d at index %zd, num commands %zu",
1249 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001250 mAudioCommands.insertAt(command, i + 1);
1251}
1252
1253void AudioPolicyService::AudioCommandThread::exit()
1254{
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 {
1257 AutoMutex _l(mLock);
1258 requestExit();
1259 mWaitWorkCV.signal();
1260 }
Zach Janga754b4f2015-10-27 01:29:34 +00001261 // Note that we can call it from the thread loop if all other references have been released
1262 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 requestExitAndWait();
1264}
1265
1266void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1267{
1268 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1269 mCommand,
1270 (int)ns2s(mTime),
1271 (int)ns2ms(mTime)%1000,
1272 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001273 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274}
1275
Dima Zavinfce7a472011-04-19 22:30:36 -07001276/******* helpers for the service_ops callbacks defined below *********/
1277void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1278 const char *keyValuePairs,
1279 int delayMs)
1280{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001281 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001282 delayMs);
1283}
1284
1285int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1286 float volume,
1287 audio_io_handle_t output,
1288 int delayMs)
1289{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001290 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001291 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001292}
1293
Dima Zavinfce7a472011-04-19 22:30:36 -07001294int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1295{
1296 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1297}
1298
Dima Zavinfce7a472011-04-19 22:30:36 -07001299extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001300audio_module_handle_t aps_load_hw_module(void *service __unused,
1301 const char *name);
1302audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001303 audio_devices_t *pDevices,
1304 uint32_t *pSamplingRate,
1305 audio_format_t *pFormat,
1306 audio_channel_mask_t *pChannelMask,
1307 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001308 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001309
Eric Laurent2d388ec2014-03-07 13:25:54 -08001310audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001311 audio_module_handle_t module,
1312 audio_devices_t *pDevices,
1313 uint32_t *pSamplingRate,
1314 audio_format_t *pFormat,
1315 audio_channel_mask_t *pChannelMask,
1316 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001317 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001318 const audio_offload_info_t *offloadInfo);
1319audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001320 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001321 audio_io_handle_t output2);
1322int aps_close_output(void *service __unused, audio_io_handle_t output);
1323int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1324int aps_restore_output(void *service __unused, audio_io_handle_t output);
1325audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001326 audio_devices_t *pDevices,
1327 uint32_t *pSamplingRate,
1328 audio_format_t *pFormat,
1329 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001330 audio_in_acoustics_t acoustics __unused);
1331audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001332 audio_module_handle_t module,
1333 audio_devices_t *pDevices,
1334 uint32_t *pSamplingRate,
1335 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001336 audio_channel_mask_t *pChannelMask);
1337int aps_close_input(void *service __unused, audio_io_handle_t input);
1338int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001339int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001340 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001341 audio_io_handle_t dst_output);
1342char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1343 const char *keys);
1344void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1345 const char *kv_pairs, int delay_ms);
1346int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001347 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001348 int delay_ms);
Eric Laurent2d388ec2014-03-07 13:25:54 -08001349int aps_set_voice_volume(void *service, float volume, int delay_ms);
1350};
Dima Zavinfce7a472011-04-19 22:30:36 -07001351
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001352} // namespace android