blob: 13bf60589ac14d4ca3b4e8a4a5e3cf4090e5fa49 [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"
Glenn Kasten44deb052012-02-05 18:09:08 -080037#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
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
Svet Ganovf4ddfef2018-01-16 07:37:58 -080045#include <private/android_filesystem_config.h>
46
Mathias Agopian65ab4712010-07-14 17:59:35 -070047namespace android {
48
Glenn Kasten8dad0e32012-01-09 08:41:22 -080049static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
50static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080053static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Eric Laurent0ede8922014-05-09 18:04:42 -070055static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010056
Svet Ganovf4ddfef2018-01-16 07:37:58 -080057static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070058
Mathias Agopian65ab4712010-07-14 17:59:35 -070059// ----------------------------------------------------------------------------
60
61AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070062 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070063 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070069 {
70 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080071
Eric Laurent8b1e80b2014-10-07 09:08:47 -070072 // start tone playback thread
73 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
74 // start audio commands thread
75 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070078
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 mAudioPolicyClient = new AudioPolicyClient(this);
80 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070081 }
bryant_liuba2b4392014-06-11 16:49:30 +080082 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070083 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
84 {
85 Mutex::Autolock _l(mLock);
86 mAudioPolicyEffects = audioPolicyEffects;
87 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080088
89 mUidPolicy = new UidPolicy(this);
90 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070091}
92
93AudioPolicyService::~AudioPolicyService()
94{
95 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070096 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070097 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070098
Eric Laurentf269b8e2014-06-09 20:01:29 -070099 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700100 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700101
102 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800103 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800104
105 mUidPolicy->unregisterSelf();
106 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700107}
108
109// A notification client is always registered by AudioSystem when the client process
110// connects to AudioPolicyService.
111void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
112{
Eric Laurent12590252015-08-21 18:40:20 -0700113 if (client == 0) {
114 ALOGW("%s got NULL client", __FUNCTION__);
115 return;
116 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800117 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700118
119 uid_t uid = IPCThreadState::self()->getCallingUid();
120 if (mNotificationClients.indexOfKey(uid) < 0) {
121 sp<NotificationClient> notificationClient = new NotificationClient(this,
122 client,
123 uid);
124 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
125
126 mNotificationClients.add(uid, notificationClient);
127
Marco Nelissenf8880202014-11-14 07:58:25 -0800128 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700129 binder->linkToDeath(notificationClient);
130 }
131}
132
Eric Laurente8726fe2015-06-26 09:39:24 -0700133void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
134{
135 Mutex::Autolock _l(mNotificationClientsLock);
136
137 uid_t uid = IPCThreadState::self()->getCallingUid();
138 if (mNotificationClients.indexOfKey(uid) < 0) {
139 return;
140 }
141 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
142}
143
Eric Laurentb52c1522014-05-20 11:27:36 -0700144// removeNotificationClient() is called when the client process dies.
145void AudioPolicyService::removeNotificationClient(uid_t uid)
146{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800147 {
148 Mutex::Autolock _l(mNotificationClientsLock);
149 mNotificationClients.removeItem(uid);
150 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800151 {
152 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700153 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700154 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700155 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700156 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800157 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700158}
159
160void AudioPolicyService::onAudioPortListUpdate()
161{
162 mOutputCommandThread->updateAudioPortListCommand();
163}
164
165void AudioPolicyService::doOnAudioPortListUpdate()
166{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800167 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700168 for (size_t i = 0; i < mNotificationClients.size(); i++) {
169 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
170 }
171}
172
173void AudioPolicyService::onAudioPatchListUpdate()
174{
175 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176}
177
Eric Laurentb52c1522014-05-20 11:27:36 -0700178void AudioPolicyService::doOnAudioPatchListUpdate()
179{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800180 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700181 for (size_t i = 0; i < mNotificationClients.size(); i++) {
182 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
183 }
184}
185
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700186void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700187{
188 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
189 regId.string(), state);
190 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
191}
192
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700193void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700194{
195 Mutex::Autolock _l(mNotificationClientsLock);
196 for (size_t i = 0; i < mNotificationClients.size(); i++) {
197 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
198 }
199}
200
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800201void AudioPolicyService::onRecordingConfigurationUpdate(int event,
202 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800203 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800204{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800205 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800206 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800207}
208
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800209void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
210 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800211 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800212{
213 Mutex::Autolock _l(mNotificationClientsLock);
214 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800215 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800216 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800217 }
218}
219
220status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
221 audio_patch_handle_t *handle,
222 int delayMs)
223{
224 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
225}
226
227status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
228 int delayMs)
229{
230 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
231}
232
Eric Laurente1715a42014-05-20 11:30:42 -0700233status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
234 int delayMs)
235{
236 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
237}
238
Eric Laurentb52c1522014-05-20 11:27:36 -0700239AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
240 const sp<IAudioPolicyServiceClient>& client,
241 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700242 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
243 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700244{
245}
246
247AudioPolicyService::NotificationClient::~NotificationClient()
248{
249}
250
251void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
252{
253 sp<NotificationClient> keep(this);
254 sp<AudioPolicyService> service = mService.promote();
255 if (service != 0) {
256 service->removeNotificationClient(mUid);
257 }
258}
259
260void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
261{
Eric Laurente8726fe2015-06-26 09:39:24 -0700262 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700263 mAudioPolicyServiceClient->onAudioPortListUpdate();
264 }
265}
266
267void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
268{
Eric Laurente8726fe2015-06-26 09:39:24 -0700269 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700270 mAudioPolicyServiceClient->onAudioPatchListUpdate();
271 }
272}
Eric Laurent57dae992011-07-24 13:36:09 -0700273
Jean-Michel Trivide801052015-04-14 19:10:14 -0700274void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700275 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700276{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700277 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800278 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
279 }
280}
281
282void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800283 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800284 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
285 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800286{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700287 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800288 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800289 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700290 }
291}
292
Eric Laurente8726fe2015-06-26 09:39:24 -0700293void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
294{
295 mAudioPortCallbacksEnabled = enabled;
296}
297
298
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700300 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700301 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302}
303
304static bool tryLock(Mutex& mutex)
305{
306 bool locked = false;
307 for (int i = 0; i < kDumpLockRetries; ++i) {
308 if (mutex.tryLock() == NO_ERROR) {
309 locked = true;
310 break;
311 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800312 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 }
314 return locked;
315}
316
317status_t AudioPolicyService::dumpInternals(int fd)
318{
319 const size_t SIZE = 256;
320 char buffer[SIZE];
321 String8 result;
322
Eric Laurentdce54a12014-03-10 12:19:46 -0700323 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 result.append(buffer);
325 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
326 result.append(buffer);
327 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
328 result.append(buffer);
329
330 write(fd, result.string(), result.size());
331 return NO_ERROR;
332}
333
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800334void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
335{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700336 {
337 Mutex::Autolock _l(mLock);
338 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700339 AutoCallerClear acc;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700340 mAudioPolicyManager->setRecordSilenced(uid, silenced);
341 }
342 }
343 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
344 if (af) {
345 af->setRecordSilenced(uid, silenced);
346 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800347}
348
Glenn Kasten0f11b512014-01-31 16:18:54 -0800349status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350{
Glenn Kasten44deb052012-02-05 18:09:08 -0800351 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 dumpPermissionDenial(fd);
353 } else {
354 bool locked = tryLock(mLock);
355 if (!locked) {
356 String8 result(kDeadlockedString);
357 write(fd, result.string(), result.size());
358 }
359
360 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800361 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 mAudioCommandThread->dump(fd);
363 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800364 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 mTonePlaybackThread->dump(fd);
366 }
367
Eric Laurentdce54a12014-03-10 12:19:46 -0700368 if (mAudioPolicyManager) {
369 mAudioPolicyManager->dump(fd);
370 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371
372 if (locked) mLock.unlock();
373 }
374 return NO_ERROR;
375}
376
377status_t AudioPolicyService::dumpPermissionDenial(int fd)
378{
379 const size_t SIZE = 256;
380 char buffer[SIZE];
381 String8 result;
382 snprintf(buffer, SIZE, "Permission Denial: "
383 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
384 IPCThreadState::self()->getCallingPid(),
385 IPCThreadState::self()->getCallingUid());
386 result.append(buffer);
387 write(fd, result.string(), result.size());
388 return NO_ERROR;
389}
390
391status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800392 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
393 switch (code) {
394 case SHELL_COMMAND_TRANSACTION: {
395 int in = data.readFileDescriptor();
396 int out = data.readFileDescriptor();
397 int err = data.readFileDescriptor();
398 int argc = data.readInt32();
399 Vector<String16> args;
400 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
401 args.add(data.readString16());
402 }
403 sp<IBinder> unusedCallback;
404 sp<IResultReceiver> resultReceiver;
405 status_t status;
406 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
407 return status;
408 }
409 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
410 return status;
411 }
412 status = shellCommand(in, out, err, args);
413 if (resultReceiver != nullptr) {
414 resultReceiver->send(status);
415 }
416 return NO_ERROR;
417 }
418 }
419
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 return BnAudioPolicyService::onTransact(code, data, reply, flags);
421}
422
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800423// ------------------- Shell command implementation -------------------
424
425// NOTE: This is a remote API - make sure all args are validated
426status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
427 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
428 return PERMISSION_DENIED;
429 }
430 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
431 return BAD_VALUE;
432 }
433 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
434 return handleSetUidState(args, err);
435 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
436 return handleResetUidState(args, err);
437 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
438 return handleGetUidState(args, out, err);
439 } else if (args.size() == 1 && args[0] == String16("help")) {
440 printHelp(out);
441 return NO_ERROR;
442 }
443 printHelp(err);
444 return BAD_VALUE;
445}
446
447status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
448 PermissionController pc;
449 int uid = pc.getPackageUid(args[1], 0);
450 if (uid <= 0) {
451 ALOGE("Unknown package: '%s'", String8(args[1]).string());
452 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
453 return BAD_VALUE;
454 }
455 bool active = false;
456 if (args[2] == String16("active")) {
457 active = true;
458 } else if ((args[2] != String16("idle"))) {
459 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
460 return BAD_VALUE;
461 }
462 mUidPolicy->addOverrideUid(uid, active);
463 return NO_ERROR;
464}
465
466status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
467 PermissionController pc;
468 int uid = pc.getPackageUid(args[1], 0);
469 if (uid < 0) {
470 ALOGE("Unknown package: '%s'", String8(args[1]).string());
471 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
472 return BAD_VALUE;
473 }
474 mUidPolicy->removeOverrideUid(uid);
475 return NO_ERROR;
476}
477
478status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
479 PermissionController pc;
480 int uid = pc.getPackageUid(args[1], 0);
481 if (uid < 0) {
482 ALOGE("Unknown package: '%s'", String8(args[1]).string());
483 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
484 return BAD_VALUE;
485 }
486 if (mUidPolicy->isUidActive(uid)) {
487 return dprintf(out, "active\n");
488 } else {
489 return dprintf(out, "idle\n");
490 }
491}
492
493status_t AudioPolicyService::printHelp(int out) {
494 return dprintf(out, "Audio policy service commands:\n"
495 " get-uid-state <PACKAGE> gets the uid state\n"
496 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
497 " reset-uid-state <PACKAGE> clears the uid state override\n"
498 " help print this message\n");
499}
500
501// ----------- AudioPolicyService::UidPolicy implementation ----------
502
503void AudioPolicyService::UidPolicy::registerSelf() {
504 ActivityManager am;
505 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
506 | ActivityManager::UID_OBSERVER_IDLE
507 | ActivityManager::UID_OBSERVER_ACTIVE,
508 ActivityManager::PROCESS_STATE_UNKNOWN,
509 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700510 status_t res = am.linkToDeath(this);
511 if (!res) {
512 Mutex::Autolock _l(mLock);
513 mObserverRegistered = true;
514 } else {
515 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
516 am.unregisterUidObserver(this);
517 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800518}
519
520void AudioPolicyService::UidPolicy::unregisterSelf() {
521 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700522 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800523 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700524 Mutex::Autolock _l(mLock);
525 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800526}
527
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700528void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
529 Mutex::Autolock _l(mLock);
530 mCachedUids.clear();
531 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800532}
533
534bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700535 if (isServiceUid(uid)) return true;
536 bool needToReregister = false;
537 {
538 Mutex::Autolock _l(mLock);
539 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800540 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700541 if (needToReregister) {
542 // Looks like ActivityManager has died previously, attempt to re-register.
543 registerSelf();
544 }
545 {
546 Mutex::Autolock _l(mLock);
547 auto overrideIter = mOverrideUids.find(uid);
548 if (overrideIter != mOverrideUids.end()) {
549 return overrideIter->second;
550 }
551 // In an absense of the ActivityManager, assume everything to be active.
552 if (!mObserverRegistered) return true;
553 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700554 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700555 return cacheIter->second;
556 }
557 }
558 ActivityManager am;
559 bool active = am.isUidActive(uid, String16("audioserver"));
560 {
561 Mutex::Autolock _l(mLock);
562 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
563 }
564 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800565}
566
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700567void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
568 updateUidCache(uid, true, true);
569}
570
571void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
572 updateUidCache(uid, false, false);
573}
574
575void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
576 updateUidCache(uid, false, true);
577}
578
579bool AudioPolicyService::UidPolicy::isServiceUid(uid_t uid) const {
580 return uid % AID_USER_OFFSET < AID_APP_START;
581}
582
583void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
584 sp<AudioPolicyService> service = mService.promote();
585 if (service != nullptr) {
586 service->setRecordSilenced(uid, !active);
587 }
588}
589
590void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
591 if (isServiceUid(uid)) return;
592 bool wasOverridden = false, wasActive = false;
593 {
594 Mutex::Autolock _l(mLock);
595 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
596 }
597 if (!wasOverridden && insert) {
598 notifyService(uid, active); // Started to override.
599 } else if (wasOverridden && !insert) {
600 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
601 } else if (wasActive != active) {
602 notifyService(uid, active); // Override updated.
603 }
604}
605
606void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
607 if (isServiceUid(uid)) return;
608 bool wasActive = false;
609 {
610 Mutex::Autolock _l(mLock);
611 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
612 // Do not notify service if currently overridden.
613 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
614 }
615 bool nowActive = active && insert;
616 if (wasActive != nowActive) notifyService(uid, nowActive);
617}
618
619void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
620 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
621 auto it = uids->find(uid);
622 if (it != uids->end()) {
623 if (wasThere != nullptr) *wasThere = true;
624 if (wasActive != nullptr) *wasActive = it->second;
625 if (insert) {
626 it->second = active;
627 } else {
628 uids->erase(it);
629 }
630 } else if (insert) {
631 uids->insert(std::pair<uid_t, bool>(uid, active));
632 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800633}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635// ----------- AudioPolicyService::AudioCommandThread implementation ----------
636
Eric Laurentbfb1b832013-01-07 09:53:42 -0800637AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
638 const wp<AudioPolicyService>& service)
639 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640{
641 mpToneGenerator = NULL;
642}
643
644
645AudioPolicyService::AudioCommandThread::~AudioCommandThread()
646{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800647 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 release_wake_lock(mName.string());
649 }
650 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800651 delete mpToneGenerator;
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) {
676 case START_TONE: {
677 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700678 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800681 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
683 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 mLock.lock();
685 }break;
686 case STOP_TONE: {
687 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 if (mpToneGenerator != NULL) {
690 mpToneGenerator->stopTone();
691 delete mpToneGenerator;
692 mpToneGenerator = NULL;
693 }
694 mLock.lock();
695 }break;
696 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700697 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700699 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
700 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
701 data->mVolume,
702 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 }break;
704 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700705 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700706 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
707 data->mKeyValuePairs.string(), data->mIO);
708 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700709 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700711 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100712 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700713 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800716 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700717 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800718 ALOGV("AudioCommandThread() processing stop output %d",
719 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700720 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 if (svc == 0) {
722 break;
723 }
724 mLock.unlock();
725 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
726 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800727 }break;
728 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700729 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800730 ALOGV("AudioCommandThread() processing release output %d",
731 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700732 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800733 if (svc == 0) {
734 break;
735 }
736 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800737 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800738 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800739 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700740 case CREATE_AUDIO_PATCH: {
741 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
742 ALOGV("AudioCommandThread() processing create audio patch");
743 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
744 if (af == 0) {
745 command->mStatus = PERMISSION_DENIED;
746 } else {
747 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
748 }
749 } break;
750 case RELEASE_AUDIO_PATCH: {
751 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
752 ALOGV("AudioCommandThread() processing release audio patch");
753 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
754 if (af == 0) {
755 command->mStatus = PERMISSION_DENIED;
756 } else {
757 command->mStatus = af->releaseAudioPatch(data->mHandle);
758 }
759 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700760 case UPDATE_AUDIOPORT_LIST: {
761 ALOGV("AudioCommandThread() processing update audio port 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->doOnAudioPortListUpdate();
768 mLock.lock();
769 }break;
770 case UPDATE_AUDIOPATCH_LIST: {
771 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700772 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700773 if (svc == 0) {
774 break;
775 }
776 mLock.unlock();
777 svc->doOnAudioPatchListUpdate();
778 mLock.lock();
779 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700780 case SET_AUDIOPORT_CONFIG: {
781 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
782 ALOGV("AudioCommandThread() processing set port config");
783 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
784 if (af == 0) {
785 command->mStatus = PERMISSION_DENIED;
786 } else {
787 command->mStatus = af->setAudioPortConfig(&data->mConfig);
788 }
789 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700790 case DYN_POLICY_MIX_STATE_UPDATE: {
791 DynPolicyMixStateUpdateData *data =
792 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700793 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
794 data->mRegId.string(), data->mState);
795 svc = mService.promote();
796 if (svc == 0) {
797 break;
798 }
799 mLock.unlock();
800 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
801 mLock.lock();
802 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800803 case RECORDING_CONFIGURATION_UPDATE: {
804 RecordingConfigurationUpdateData *data =
805 (RecordingConfigurationUpdateData *)command->mParam.get();
806 ALOGV("AudioCommandThread() processing recording configuration update");
807 svc = mService.promote();
808 if (svc == 0) {
809 break;
810 }
811 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800812 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
813 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800814 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800815 mLock.lock();
816 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000818 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700820 {
821 Mutex::Autolock _l(command->mLock);
822 if (command->mWaitStatus) {
823 command->mWaitStatus = false;
824 command->mCond.signal();
825 }
826 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800827 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000828 // release mLock before releasing strong reference on the service as
829 // AudioPolicyService destructor calls AudioCommandThread::exit() which
830 // acquires mLock.
831 mLock.unlock();
832 svc.clear();
833 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 } else {
835 waitTime = mAudioCommands[0]->mTime - curTime;
836 break;
837 }
838 }
Zach Janga754b4f2015-10-27 01:29:34 +0000839
840 // release delayed commands wake lock if the queue is empty
841 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700842 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000843 }
844
845 // At this stage we have either an empty command queue or the first command in the queue
846 // has a finite delay. So unless we are exiting it is safe to wait.
847 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700848 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800849 if (waitTime == -1) {
850 mWaitWorkCV.wait(mLock);
851 } else {
852 mWaitWorkCV.waitRelative(mLock, waitTime);
853 }
Eric Laurent59a89232014-06-08 14:14:17 -0700854 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700856 // release delayed commands wake lock before quitting
857 if (!mAudioCommands.isEmpty()) {
858 release_wake_lock(mName.string());
859 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 mLock.unlock();
861 return false;
862}
863
864status_t AudioPolicyService::AudioCommandThread::dump(int fd)
865{
866 const size_t SIZE = 256;
867 char buffer[SIZE];
868 String8 result;
869
870 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
871 result.append(buffer);
872 write(fd, result.string(), result.size());
873
874 bool locked = tryLock(mLock);
875 if (!locked) {
876 String8 result2(kCmdDeadlockedString);
877 write(fd, result2.string(), result2.size());
878 }
879
880 snprintf(buffer, SIZE, "- Commands:\n");
881 result = String8(buffer);
882 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800883 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 mAudioCommands[i]->dump(buffer, SIZE);
885 result.append(buffer);
886 }
887 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700888 if (mLastCommand != 0) {
889 mLastCommand->dump(buffer, SIZE);
890 result.append(buffer);
891 } else {
892 result.append(" none\n");
893 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894
895 write(fd, result.string(), result.size());
896
897 if (locked) mLock.unlock();
898
899 return NO_ERROR;
900}
901
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800902void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
903 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904{
Eric Laurent0ede8922014-05-09 18:04:42 -0700905 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700907 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 data->mType = type;
909 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100910 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100911 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700912 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913}
914
915void AudioPolicyService::AudioCommandThread::stopToneCommand()
916{
Eric Laurent0ede8922014-05-09 18:04:42 -0700917 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700920 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921}
922
Glenn Kastenfff6d712012-01-12 16:38:12 -0800923status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700924 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800925 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700926 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927{
Eric Laurent0ede8922014-05-09 18:04:42 -0700928 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700930 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 data->mStream = stream;
932 data->mVolume = volume;
933 data->mIO = output;
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 volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700937 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700938 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939}
940
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800941status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700942 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700943 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944{
Eric Laurent0ede8922014-05-09 18:04:42 -0700945 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700947 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700949 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700951 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700953 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700954 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955}
956
957status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
958{
Eric Laurent0ede8922014-05-09 18:04:42 -0700959 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700961 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 data->mVolume = volume;
963 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700966 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967}
968
Eric Laurentbfb1b832013-01-07 09:53:42 -0800969void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
970 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800971 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800972{
Eric Laurent0ede8922014-05-09 18:04:42 -0700973 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800974 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700975 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800976 data->mIO = output;
977 data->mStream = stream;
978 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100979 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800980 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700981 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800982}
983
Eric Laurente83b55d2014-11-14 10:06:21 -0800984void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
985 audio_stream_type_t stream,
986 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800987{
Eric Laurent0ede8922014-05-09 18:04:42 -0700988 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800989 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700990 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800991 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800992 data->mStream = stream;
993 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100994 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800995 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700996 sendCommand(command);
997}
998
Eric Laurent951f4552014-05-20 10:48:17 -0700999status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1000 const struct audio_patch *patch,
1001 audio_patch_handle_t *handle,
1002 int delayMs)
1003{
1004 status_t status = NO_ERROR;
1005
1006 sp<AudioCommand> command = new AudioCommand();
1007 command->mCommand = CREATE_AUDIO_PATCH;
1008 CreateAudioPatchData *data = new CreateAudioPatchData();
1009 data->mPatch = *patch;
1010 data->mHandle = *handle;
1011 command->mParam = data;
1012 command->mWaitStatus = true;
1013 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1014 status = sendCommand(command, delayMs);
1015 if (status == NO_ERROR) {
1016 *handle = data->mHandle;
1017 }
1018 return status;
1019}
1020
1021status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1022 int delayMs)
1023{
1024 sp<AudioCommand> command = new AudioCommand();
1025 command->mCommand = RELEASE_AUDIO_PATCH;
1026 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1027 data->mHandle = handle;
1028 command->mParam = data;
1029 command->mWaitStatus = true;
1030 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1031 return sendCommand(command, delayMs);
1032}
1033
Eric Laurentb52c1522014-05-20 11:27:36 -07001034void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1035{
1036 sp<AudioCommand> command = new AudioCommand();
1037 command->mCommand = UPDATE_AUDIOPORT_LIST;
1038 ALOGV("AudioCommandThread() adding update audio port list");
1039 sendCommand(command);
1040}
1041
1042void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1043{
1044 sp<AudioCommand>command = new AudioCommand();
1045 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1046 ALOGV("AudioCommandThread() adding update audio patch list");
1047 sendCommand(command);
1048}
1049
Eric Laurente1715a42014-05-20 11:30:42 -07001050status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1051 const struct audio_port_config *config, int delayMs)
1052{
1053 sp<AudioCommand> command = new AudioCommand();
1054 command->mCommand = SET_AUDIOPORT_CONFIG;
1055 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1056 data->mConfig = *config;
1057 command->mParam = data;
1058 command->mWaitStatus = true;
1059 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1060 return sendCommand(command, delayMs);
1061}
1062
Jean-Michel Trivide801052015-04-14 19:10:14 -07001063void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001064 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001065{
1066 sp<AudioCommand> command = new AudioCommand();
1067 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1068 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1069 data->mRegId = regId;
1070 data->mState = state;
1071 command->mParam = data;
1072 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1073 regId.string(), state);
1074 sendCommand(command);
1075}
1076
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001077void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001078 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001079 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1080 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001081{
1082 sp<AudioCommand>command = new AudioCommand();
1083 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1084 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1085 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001086 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001087 data->mClientConfig = *clientConfig;
1088 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001089 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001090 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001091 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1092 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001093 sendCommand(command);
1094}
1095
Eric Laurent0ede8922014-05-09 18:04:42 -07001096status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1097{
1098 {
1099 Mutex::Autolock _l(mLock);
1100 insertCommand_l(command, delayMs);
1101 mWaitWorkCV.signal();
1102 }
1103 Mutex::Autolock _l(command->mLock);
1104 while (command->mWaitStatus) {
1105 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1106 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1107 command->mStatus = TIMED_OUT;
1108 command->mWaitStatus = false;
1109 }
1110 }
1111 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001112}
1113
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001115void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001117 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001118 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 command->mTime = systemTime() + milliseconds(delayMs);
1120
1121 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001122 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1124 }
1125
1126 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001127 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001128 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1130 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001131
1132 // create audio patch or release audio patch commands are equivalent
1133 // with regard to filtering
1134 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1135 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1136 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1137 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1138 continue;
1139 }
1140 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141
1142 switch (command->mCommand) {
1143 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001144 ParametersData *data = (ParametersData *)command->mParam.get();
1145 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001147 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001148 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1150 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1151 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001152 String8 key;
1153 String8 value;
1154 param.getAt(j, key, value);
1155 for (size_t k = 0; k < param2.size(); k++) {
1156 String8 key2;
1157 String8 value2;
1158 param2.getAt(k, key2, value2);
1159 if (key2 == key) {
1160 param2.remove(key2);
1161 ALOGV("Filtering out parameter %s", key2.string());
1162 break;
1163 }
1164 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 }
1166 // if all keys have been filtered out, remove the command.
1167 // otherwise, update the key value pairs
1168 if (param2.size() == 0) {
1169 removedCommands.add(command2);
1170 } else {
1171 data2->mKeyValuePairs = param2.toString();
1172 }
Eric Laurent21e54562013-09-23 12:08:05 -07001173 command->mTime = command2->mTime;
1174 // force delayMs to non 0 so that code below does not request to wait for
1175 // command status as the command is now delayed
1176 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 } break;
1178
1179 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001180 VolumeData *data = (VolumeData *)command->mParam.get();
1181 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182 if (data->mIO != data2->mIO) break;
1183 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001184 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001185 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001187 command->mTime = command2->mTime;
1188 // force delayMs to non 0 so that code below does not request to wait for
1189 // command status as the command is now delayed
1190 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001192
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001193 case SET_VOICE_VOLUME: {
1194 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1195 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1196 ALOGV("Filtering out voice volume command value %f replaced by %f",
1197 data2->mVolume, data->mVolume);
1198 removedCommands.add(command2);
1199 command->mTime = command2->mTime;
1200 // force delayMs to non 0 so that code below does not request to wait for
1201 // command status as the command is now delayed
1202 delayMs = 1;
1203 } break;
1204
Eric Laurente45b48a2014-09-04 16:40:57 -07001205 case CREATE_AUDIO_PATCH:
1206 case RELEASE_AUDIO_PATCH: {
1207 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001208 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001209 if (command->mCommand == CREATE_AUDIO_PATCH) {
1210 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001211 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001212 } else {
1213 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1214 }
1215 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001216 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001217 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1218 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001219 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001220 } else {
1221 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001222 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001223 }
1224 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001225 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1226 same output. */
1227 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1228 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1229 bool isOutputDiff = false;
1230 if (patch.num_sources == patch2.num_sources) {
1231 for (unsigned count = 0; count < patch.num_sources; count++) {
1232 if (patch.sources[count].id != patch2.sources[count].id) {
1233 isOutputDiff = true;
1234 break;
1235 }
1236 }
1237 if (isOutputDiff)
1238 break;
1239 }
1240 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001241 ALOGV("Filtering out %s audio patch command for handle %d",
1242 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1243 removedCommands.add(command2);
1244 command->mTime = command2->mTime;
1245 // force delayMs to non 0 so that code below does not request to wait for
1246 // command status as the command is now delayed
1247 delayMs = 1;
1248 } break;
1249
Jean-Michel Trivide801052015-04-14 19:10:14 -07001250 case DYN_POLICY_MIX_STATE_UPDATE: {
1251
1252 } break;
1253
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001254 case RECORDING_CONFIGURATION_UPDATE: {
1255
1256 } break;
1257
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258 case START_TONE:
1259 case STOP_TONE:
1260 default:
1261 break;
1262 }
1263 }
1264
1265 // remove filtered commands
1266 for (size_t j = 0; j < removedCommands.size(); j++) {
1267 // removed commands always have time stamps greater than current command
1268 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001269 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 mAudioCommands.removeAt(k);
1272 break;
1273 }
1274 }
1275 }
1276 removedCommands.clear();
1277
Eric Laurentaa79bef2015-01-15 14:33:51 -08001278 // Disable wait for status if delay is not 0.
1279 // Except for create audio patch command because the returned patch handle
1280 // is needed by audio policy manager
1281 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001282 command->mWaitStatus = false;
1283 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001284
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001286 ALOGV("inserting command: %d at index %zd, num commands %zu",
1287 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 mAudioCommands.insertAt(command, i + 1);
1289}
1290
1291void AudioPolicyService::AudioCommandThread::exit()
1292{
Steve Block3856b092011-10-20 11:56:00 +01001293 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294 {
1295 AutoMutex _l(mLock);
1296 requestExit();
1297 mWaitWorkCV.signal();
1298 }
Zach Janga754b4f2015-10-27 01:29:34 +00001299 // Note that we can call it from the thread loop if all other references have been released
1300 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001301 requestExitAndWait();
1302}
1303
1304void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1305{
1306 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1307 mCommand,
1308 (int)ns2s(mTime),
1309 (int)ns2ms(mTime)%1000,
1310 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001311 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312}
1313
Dima Zavinfce7a472011-04-19 22:30:36 -07001314/******* helpers for the service_ops callbacks defined below *********/
1315void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1316 const char *keyValuePairs,
1317 int delayMs)
1318{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001319 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001320 delayMs);
1321}
1322
1323int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1324 float volume,
1325 audio_io_handle_t output,
1326 int delayMs)
1327{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001328 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001329 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001330}
1331
1332int AudioPolicyService::startTone(audio_policy_tone_t tone,
1333 audio_stream_type_t stream)
1334{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001335 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001336 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001337 }
1338 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001339 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001340 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001341 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001342 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1343 AUDIO_STREAM_VOICE_CALL);
1344 return 0;
1345}
1346
1347int AudioPolicyService::stopTone()
1348{
1349 mTonePlaybackThread->stopToneCommand();
1350 return 0;
1351}
1352
1353int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1354{
1355 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1356}
1357
Dima Zavinfce7a472011-04-19 22:30:36 -07001358extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001359audio_module_handle_t aps_load_hw_module(void *service __unused,
1360 const char *name);
1361audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001362 audio_devices_t *pDevices,
1363 uint32_t *pSamplingRate,
1364 audio_format_t *pFormat,
1365 audio_channel_mask_t *pChannelMask,
1366 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001367 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368
Eric Laurent2d388ec2014-03-07 13:25:54 -08001369audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001370 audio_module_handle_t module,
1371 audio_devices_t *pDevices,
1372 uint32_t *pSamplingRate,
1373 audio_format_t *pFormat,
1374 audio_channel_mask_t *pChannelMask,
1375 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001376 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001377 const audio_offload_info_t *offloadInfo);
1378audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001379 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001380 audio_io_handle_t output2);
1381int aps_close_output(void *service __unused, audio_io_handle_t output);
1382int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1383int aps_restore_output(void *service __unused, audio_io_handle_t output);
1384audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001385 audio_devices_t *pDevices,
1386 uint32_t *pSamplingRate,
1387 audio_format_t *pFormat,
1388 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001389 audio_in_acoustics_t acoustics __unused);
1390audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001391 audio_module_handle_t module,
1392 audio_devices_t *pDevices,
1393 uint32_t *pSamplingRate,
1394 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001395 audio_channel_mask_t *pChannelMask);
1396int aps_close_input(void *service __unused, audio_io_handle_t input);
1397int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001398int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001399 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001400 audio_io_handle_t dst_output);
1401char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1402 const char *keys);
1403void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1404 const char *kv_pairs, int delay_ms);
1405int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001407 int delay_ms);
1408int aps_start_tone(void *service, audio_policy_tone_t tone,
1409 audio_stream_type_t stream);
1410int aps_stop_tone(void *service);
1411int aps_set_voice_volume(void *service, float volume, int delay_ms);
1412};
Dima Zavinfce7a472011-04-19 22:30:36 -07001413
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001414} // namespace android