blob: 082923ad6e98a77abdf62f9e245eb891c816d691 [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 Laurent8c7e6da2015-04-21 17:37:00 -0700154 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700155 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700157}
158
159void AudioPolicyService::onAudioPortListUpdate()
160{
161 mOutputCommandThread->updateAudioPortListCommand();
162}
163
164void AudioPolicyService::doOnAudioPortListUpdate()
165{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800166 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700167 for (size_t i = 0; i < mNotificationClients.size(); i++) {
168 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
169 }
170}
171
172void AudioPolicyService::onAudioPatchListUpdate()
173{
174 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175}
176
Eric Laurentb52c1522014-05-20 11:27:36 -0700177void AudioPolicyService::doOnAudioPatchListUpdate()
178{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800179 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700180 for (size_t i = 0; i < mNotificationClients.size(); i++) {
181 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
182 }
183}
184
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700185void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700186{
187 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
188 regId.string(), state);
189 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
190}
191
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700192void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700193{
194 Mutex::Autolock _l(mNotificationClientsLock);
195 for (size_t i = 0; i < mNotificationClients.size(); i++) {
196 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
197 }
198}
199
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800200void AudioPolicyService::onRecordingConfigurationUpdate(int event,
201 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800202 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800203{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800204 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800205 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800206}
207
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800208void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
209 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800210 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800211{
212 Mutex::Autolock _l(mNotificationClientsLock);
213 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800214 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800215 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800216 }
217}
218
219status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
220 audio_patch_handle_t *handle,
221 int delayMs)
222{
223 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
224}
225
226status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
227 int delayMs)
228{
229 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
230}
231
Eric Laurente1715a42014-05-20 11:30:42 -0700232status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
233 int delayMs)
234{
235 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
236}
237
Eric Laurentb52c1522014-05-20 11:27:36 -0700238AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
239 const sp<IAudioPolicyServiceClient>& client,
240 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700241 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
242 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700243{
244}
245
246AudioPolicyService::NotificationClient::~NotificationClient()
247{
248}
249
250void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
251{
252 sp<NotificationClient> keep(this);
253 sp<AudioPolicyService> service = mService.promote();
254 if (service != 0) {
255 service->removeNotificationClient(mUid);
256 }
257}
258
259void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
260{
Eric Laurente8726fe2015-06-26 09:39:24 -0700261 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700262 mAudioPolicyServiceClient->onAudioPortListUpdate();
263 }
264}
265
266void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
267{
Eric Laurente8726fe2015-06-26 09:39:24 -0700268 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700269 mAudioPolicyServiceClient->onAudioPatchListUpdate();
270 }
271}
Eric Laurent57dae992011-07-24 13:36:09 -0700272
Jean-Michel Trivide801052015-04-14 19:10:14 -0700273void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700274 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700275{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700276 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800277 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
278 }
279}
280
281void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800282 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800283 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
284 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800285{
Eric Laurent96c7eed2018-03-26 17:57:01 -0700286 if (mAudioPolicyServiceClient != 0 && (mUid % AID_USER_OFFSET) < AID_APP_START) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800287 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800288 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700289 }
290}
291
Eric Laurente8726fe2015-06-26 09:39:24 -0700292void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
293{
294 mAudioPortCallbacksEnabled = enabled;
295}
296
297
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700299 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700300 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301}
302
303static bool tryLock(Mutex& mutex)
304{
305 bool locked = false;
306 for (int i = 0; i < kDumpLockRetries; ++i) {
307 if (mutex.tryLock() == NO_ERROR) {
308 locked = true;
309 break;
310 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800311 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 }
313 return locked;
314}
315
316status_t AudioPolicyService::dumpInternals(int fd)
317{
318 const size_t SIZE = 256;
319 char buffer[SIZE];
320 String8 result;
321
Eric Laurentdce54a12014-03-10 12:19:46 -0700322 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 result.append(buffer);
324 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
325 result.append(buffer);
326 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
327 result.append(buffer);
328
329 write(fd, result.string(), result.size());
330 return NO_ERROR;
331}
332
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800333void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
334{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700335 {
336 Mutex::Autolock _l(mLock);
337 if (mAudioPolicyManager) {
338 mAudioPolicyManager->setRecordSilenced(uid, silenced);
339 }
340 }
341 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
342 if (af) {
343 af->setRecordSilenced(uid, silenced);
344 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800345}
346
Glenn Kasten0f11b512014-01-31 16:18:54 -0800347status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348{
Glenn Kasten44deb052012-02-05 18:09:08 -0800349 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 dumpPermissionDenial(fd);
351 } else {
352 bool locked = tryLock(mLock);
353 if (!locked) {
354 String8 result(kDeadlockedString);
355 write(fd, result.string(), result.size());
356 }
357
358 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800359 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 mAudioCommandThread->dump(fd);
361 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800362 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 mTonePlaybackThread->dump(fd);
364 }
365
Eric Laurentdce54a12014-03-10 12:19:46 -0700366 if (mAudioPolicyManager) {
367 mAudioPolicyManager->dump(fd);
368 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369
370 if (locked) mLock.unlock();
371 }
372 return NO_ERROR;
373}
374
375status_t AudioPolicyService::dumpPermissionDenial(int fd)
376{
377 const size_t SIZE = 256;
378 char buffer[SIZE];
379 String8 result;
380 snprintf(buffer, SIZE, "Permission Denial: "
381 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
382 IPCThreadState::self()->getCallingPid(),
383 IPCThreadState::self()->getCallingUid());
384 result.append(buffer);
385 write(fd, result.string(), result.size());
386 return NO_ERROR;
387}
388
389status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800390 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
391 switch (code) {
392 case SHELL_COMMAND_TRANSACTION: {
393 int in = data.readFileDescriptor();
394 int out = data.readFileDescriptor();
395 int err = data.readFileDescriptor();
396 int argc = data.readInt32();
397 Vector<String16> args;
398 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
399 args.add(data.readString16());
400 }
401 sp<IBinder> unusedCallback;
402 sp<IResultReceiver> resultReceiver;
403 status_t status;
404 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
405 return status;
406 }
407 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
408 return status;
409 }
410 status = shellCommand(in, out, err, args);
411 if (resultReceiver != nullptr) {
412 resultReceiver->send(status);
413 }
414 return NO_ERROR;
415 }
416 }
417
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 return BnAudioPolicyService::onTransact(code, data, reply, flags);
419}
420
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800421// ------------------- Shell command implementation -------------------
422
423// NOTE: This is a remote API - make sure all args are validated
424status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
425 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
426 return PERMISSION_DENIED;
427 }
428 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
429 return BAD_VALUE;
430 }
431 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
432 return handleSetUidState(args, err);
433 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
434 return handleResetUidState(args, err);
435 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
436 return handleGetUidState(args, out, err);
437 } else if (args.size() == 1 && args[0] == String16("help")) {
438 printHelp(out);
439 return NO_ERROR;
440 }
441 printHelp(err);
442 return BAD_VALUE;
443}
444
445status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
446 PermissionController pc;
447 int uid = pc.getPackageUid(args[1], 0);
448 if (uid <= 0) {
449 ALOGE("Unknown package: '%s'", String8(args[1]).string());
450 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
451 return BAD_VALUE;
452 }
453 bool active = false;
454 if (args[2] == String16("active")) {
455 active = true;
456 } else if ((args[2] != String16("idle"))) {
457 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
458 return BAD_VALUE;
459 }
460 mUidPolicy->addOverrideUid(uid, active);
461 return NO_ERROR;
462}
463
464status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
465 PermissionController pc;
466 int uid = pc.getPackageUid(args[1], 0);
467 if (uid < 0) {
468 ALOGE("Unknown package: '%s'", String8(args[1]).string());
469 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
470 return BAD_VALUE;
471 }
472 mUidPolicy->removeOverrideUid(uid);
473 return NO_ERROR;
474}
475
476status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
477 PermissionController pc;
478 int uid = pc.getPackageUid(args[1], 0);
479 if (uid < 0) {
480 ALOGE("Unknown package: '%s'", String8(args[1]).string());
481 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
482 return BAD_VALUE;
483 }
484 if (mUidPolicy->isUidActive(uid)) {
485 return dprintf(out, "active\n");
486 } else {
487 return dprintf(out, "idle\n");
488 }
489}
490
491status_t AudioPolicyService::printHelp(int out) {
492 return dprintf(out, "Audio policy service commands:\n"
493 " get-uid-state <PACKAGE> gets the uid state\n"
494 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
495 " reset-uid-state <PACKAGE> clears the uid state override\n"
496 " help print this message\n");
497}
498
499// ----------- AudioPolicyService::UidPolicy implementation ----------
500
501void AudioPolicyService::UidPolicy::registerSelf() {
502 ActivityManager am;
503 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
504 | ActivityManager::UID_OBSERVER_IDLE
505 | ActivityManager::UID_OBSERVER_ACTIVE,
506 ActivityManager::PROCESS_STATE_UNKNOWN,
507 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700508 status_t res = am.linkToDeath(this);
509 if (!res) {
510 Mutex::Autolock _l(mLock);
511 mObserverRegistered = true;
512 } else {
513 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
514 am.unregisterUidObserver(this);
515 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800516}
517
518void AudioPolicyService::UidPolicy::unregisterSelf() {
519 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700520 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800521 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700522 Mutex::Autolock _l(mLock);
523 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800524}
525
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700526void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
527 Mutex::Autolock _l(mLock);
528 mCachedUids.clear();
529 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800530}
531
532bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700533 if (isServiceUid(uid)) return true;
534 bool needToReregister = false;
535 {
536 Mutex::Autolock _l(mLock);
537 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800538 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700539 if (needToReregister) {
540 // Looks like ActivityManager has died previously, attempt to re-register.
541 registerSelf();
542 }
543 {
544 Mutex::Autolock _l(mLock);
545 auto overrideIter = mOverrideUids.find(uid);
546 if (overrideIter != mOverrideUids.end()) {
547 return overrideIter->second;
548 }
549 // In an absense of the ActivityManager, assume everything to be active.
550 if (!mObserverRegistered) return true;
551 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700552 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700553 return cacheIter->second;
554 }
555 }
556 ActivityManager am;
557 bool active = am.isUidActive(uid, String16("audioserver"));
558 {
559 Mutex::Autolock _l(mLock);
560 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
561 }
562 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800563}
564
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700565void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
566 updateUidCache(uid, true, true);
567}
568
569void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
570 updateUidCache(uid, false, false);
571}
572
573void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
574 updateUidCache(uid, false, true);
575}
576
577bool AudioPolicyService::UidPolicy::isServiceUid(uid_t uid) const {
578 return uid % AID_USER_OFFSET < AID_APP_START;
579}
580
581void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
582 sp<AudioPolicyService> service = mService.promote();
583 if (service != nullptr) {
584 service->setRecordSilenced(uid, !active);
585 }
586}
587
588void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
589 if (isServiceUid(uid)) return;
590 bool wasOverridden = false, wasActive = false;
591 {
592 Mutex::Autolock _l(mLock);
593 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
594 }
595 if (!wasOverridden && insert) {
596 notifyService(uid, active); // Started to override.
597 } else if (wasOverridden && !insert) {
598 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
599 } else if (wasActive != active) {
600 notifyService(uid, active); // Override updated.
601 }
602}
603
604void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
605 if (isServiceUid(uid)) return;
606 bool wasActive = false;
607 {
608 Mutex::Autolock _l(mLock);
609 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
610 // Do not notify service if currently overridden.
611 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
612 }
613 bool nowActive = active && insert;
614 if (wasActive != nowActive) notifyService(uid, nowActive);
615}
616
617void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
618 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
619 auto it = uids->find(uid);
620 if (it != uids->end()) {
621 if (wasThere != nullptr) *wasThere = true;
622 if (wasActive != nullptr) *wasActive = it->second;
623 if (insert) {
624 it->second = active;
625 } else {
626 uids->erase(it);
627 }
628 } else if (insert) {
629 uids->insert(std::pair<uid_t, bool>(uid, active));
630 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800631}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633// ----------- AudioPolicyService::AudioCommandThread implementation ----------
634
Eric Laurentbfb1b832013-01-07 09:53:42 -0800635AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
636 const wp<AudioPolicyService>& service)
637 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638{
639 mpToneGenerator = NULL;
640}
641
642
643AudioPolicyService::AudioCommandThread::~AudioCommandThread()
644{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800645 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 release_wake_lock(mName.string());
647 }
648 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800649 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650}
651
652void AudioPolicyService::AudioCommandThread::onFirstRef()
653{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800654 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655}
656
657bool AudioPolicyService::AudioCommandThread::threadLoop()
658{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800659 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660
661 mLock.lock();
662 while (!exitPending())
663 {
Eric Laurent59a89232014-06-08 14:14:17 -0700664 sp<AudioPolicyService> svc;
665 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 nsecs_t curTime = systemTime();
667 // commands are sorted by increasing time stamp: execute them from index 0 and up
668 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700669 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672
673 switch (command->mCommand) {
674 case START_TONE: {
675 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700676 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800679 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
681 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 mLock.lock();
683 }break;
684 case STOP_TONE: {
685 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 if (mpToneGenerator != NULL) {
688 mpToneGenerator->stopTone();
689 delete mpToneGenerator;
690 mpToneGenerator = NULL;
691 }
692 mLock.lock();
693 }break;
694 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700695 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700697 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
698 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
699 data->mVolume,
700 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 }break;
702 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700703 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700704 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
705 data->mKeyValuePairs.string(), data->mIO);
706 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700707 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700709 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100710 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700711 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800714 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700715 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800716 ALOGV("AudioCommandThread() processing stop output %d",
717 data->mIO);
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();
723 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
724 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800725 }break;
726 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700727 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800728 ALOGV("AudioCommandThread() processing release output %d",
729 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700730 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800731 if (svc == 0) {
732 break;
733 }
734 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800735 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800736 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800737 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700738 case CREATE_AUDIO_PATCH: {
739 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
740 ALOGV("AudioCommandThread() processing create audio patch");
741 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
742 if (af == 0) {
743 command->mStatus = PERMISSION_DENIED;
744 } else {
745 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
746 }
747 } break;
748 case RELEASE_AUDIO_PATCH: {
749 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
750 ALOGV("AudioCommandThread() processing release audio patch");
751 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
752 if (af == 0) {
753 command->mStatus = PERMISSION_DENIED;
754 } else {
755 command->mStatus = af->releaseAudioPatch(data->mHandle);
756 }
757 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700758 case UPDATE_AUDIOPORT_LIST: {
759 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700760 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700761 if (svc == 0) {
762 break;
763 }
764 mLock.unlock();
765 svc->doOnAudioPortListUpdate();
766 mLock.lock();
767 }break;
768 case UPDATE_AUDIOPATCH_LIST: {
769 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700770 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700771 if (svc == 0) {
772 break;
773 }
774 mLock.unlock();
775 svc->doOnAudioPatchListUpdate();
776 mLock.lock();
777 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700778 case SET_AUDIOPORT_CONFIG: {
779 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
780 ALOGV("AudioCommandThread() processing set port config");
781 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
782 if (af == 0) {
783 command->mStatus = PERMISSION_DENIED;
784 } else {
785 command->mStatus = af->setAudioPortConfig(&data->mConfig);
786 }
787 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700788 case DYN_POLICY_MIX_STATE_UPDATE: {
789 DynPolicyMixStateUpdateData *data =
790 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700791 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
792 data->mRegId.string(), data->mState);
793 svc = mService.promote();
794 if (svc == 0) {
795 break;
796 }
797 mLock.unlock();
798 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
799 mLock.lock();
800 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800801 case RECORDING_CONFIGURATION_UPDATE: {
802 RecordingConfigurationUpdateData *data =
803 (RecordingConfigurationUpdateData *)command->mParam.get();
804 ALOGV("AudioCommandThread() processing recording configuration update");
805 svc = mService.promote();
806 if (svc == 0) {
807 break;
808 }
809 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800810 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
811 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800812 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800813 mLock.lock();
814 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000816 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700818 {
819 Mutex::Autolock _l(command->mLock);
820 if (command->mWaitStatus) {
821 command->mWaitStatus = false;
822 command->mCond.signal();
823 }
824 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800825 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000826 // release mLock before releasing strong reference on the service as
827 // AudioPolicyService destructor calls AudioCommandThread::exit() which
828 // acquires mLock.
829 mLock.unlock();
830 svc.clear();
831 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 } else {
833 waitTime = mAudioCommands[0]->mTime - curTime;
834 break;
835 }
836 }
Zach Janga754b4f2015-10-27 01:29:34 +0000837
838 // release delayed commands wake lock if the queue is empty
839 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700840 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000841 }
842
843 // At this stage we have either an empty command queue or the first command in the queue
844 // has a finite delay. So unless we are exiting it is safe to wait.
845 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700846 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800847 if (waitTime == -1) {
848 mWaitWorkCV.wait(mLock);
849 } else {
850 mWaitWorkCV.waitRelative(mLock, waitTime);
851 }
Eric Laurent59a89232014-06-08 14:14:17 -0700852 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700854 // release delayed commands wake lock before quitting
855 if (!mAudioCommands.isEmpty()) {
856 release_wake_lock(mName.string());
857 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 mLock.unlock();
859 return false;
860}
861
862status_t AudioPolicyService::AudioCommandThread::dump(int fd)
863{
864 const size_t SIZE = 256;
865 char buffer[SIZE];
866 String8 result;
867
868 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
869 result.append(buffer);
870 write(fd, result.string(), result.size());
871
872 bool locked = tryLock(mLock);
873 if (!locked) {
874 String8 result2(kCmdDeadlockedString);
875 write(fd, result2.string(), result2.size());
876 }
877
878 snprintf(buffer, SIZE, "- Commands:\n");
879 result = String8(buffer);
880 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800881 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 mAudioCommands[i]->dump(buffer, SIZE);
883 result.append(buffer);
884 }
885 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700886 if (mLastCommand != 0) {
887 mLastCommand->dump(buffer, SIZE);
888 result.append(buffer);
889 } else {
890 result.append(" none\n");
891 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892
893 write(fd, result.string(), result.size());
894
895 if (locked) mLock.unlock();
896
897 return NO_ERROR;
898}
899
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800900void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
901 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902{
Eric Laurent0ede8922014-05-09 18:04:42 -0700903 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700905 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 data->mType = type;
907 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100908 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100909 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700910 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911}
912
913void AudioPolicyService::AudioCommandThread::stopToneCommand()
914{
Eric Laurent0ede8922014-05-09 18:04:42 -0700915 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700918 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919}
920
Glenn Kastenfff6d712012-01-12 16:38:12 -0800921status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700922 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800923 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700924 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925{
Eric Laurent0ede8922014-05-09 18:04:42 -0700926 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700928 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 data->mStream = stream;
930 data->mVolume = volume;
931 data->mIO = output;
932 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700933 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100934 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700935 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700936 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937}
938
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800939status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700940 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700941 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942{
Eric Laurent0ede8922014-05-09 18:04:42 -0700943 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700945 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700947 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700949 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700951 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700952 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953}
954
955status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
956{
Eric Laurent0ede8922014-05-09 18:04:42 -0700957 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700959 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 data->mVolume = volume;
961 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700964 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965}
966
Eric Laurentbfb1b832013-01-07 09:53:42 -0800967void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
968 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800969 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800970{
Eric Laurent0ede8922014-05-09 18:04:42 -0700971 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800972 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700973 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800974 data->mIO = output;
975 data->mStream = stream;
976 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100977 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800978 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700979 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800980}
981
Eric Laurente83b55d2014-11-14 10:06:21 -0800982void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
983 audio_stream_type_t stream,
984 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800985{
Eric Laurent0ede8922014-05-09 18:04:42 -0700986 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800987 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700988 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800989 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800990 data->mStream = stream;
991 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100992 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800993 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700994 sendCommand(command);
995}
996
Eric Laurent951f4552014-05-20 10:48:17 -0700997status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
998 const struct audio_patch *patch,
999 audio_patch_handle_t *handle,
1000 int delayMs)
1001{
1002 status_t status = NO_ERROR;
1003
1004 sp<AudioCommand> command = new AudioCommand();
1005 command->mCommand = CREATE_AUDIO_PATCH;
1006 CreateAudioPatchData *data = new CreateAudioPatchData();
1007 data->mPatch = *patch;
1008 data->mHandle = *handle;
1009 command->mParam = data;
1010 command->mWaitStatus = true;
1011 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1012 status = sendCommand(command, delayMs);
1013 if (status == NO_ERROR) {
1014 *handle = data->mHandle;
1015 }
1016 return status;
1017}
1018
1019status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1020 int delayMs)
1021{
1022 sp<AudioCommand> command = new AudioCommand();
1023 command->mCommand = RELEASE_AUDIO_PATCH;
1024 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1025 data->mHandle = handle;
1026 command->mParam = data;
1027 command->mWaitStatus = true;
1028 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1029 return sendCommand(command, delayMs);
1030}
1031
Eric Laurentb52c1522014-05-20 11:27:36 -07001032void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1033{
1034 sp<AudioCommand> command = new AudioCommand();
1035 command->mCommand = UPDATE_AUDIOPORT_LIST;
1036 ALOGV("AudioCommandThread() adding update audio port list");
1037 sendCommand(command);
1038}
1039
1040void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1041{
1042 sp<AudioCommand>command = new AudioCommand();
1043 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1044 ALOGV("AudioCommandThread() adding update audio patch list");
1045 sendCommand(command);
1046}
1047
Eric Laurente1715a42014-05-20 11:30:42 -07001048status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1049 const struct audio_port_config *config, int delayMs)
1050{
1051 sp<AudioCommand> command = new AudioCommand();
1052 command->mCommand = SET_AUDIOPORT_CONFIG;
1053 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1054 data->mConfig = *config;
1055 command->mParam = data;
1056 command->mWaitStatus = true;
1057 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1058 return sendCommand(command, delayMs);
1059}
1060
Jean-Michel Trivide801052015-04-14 19:10:14 -07001061void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001062 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001063{
1064 sp<AudioCommand> command = new AudioCommand();
1065 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1066 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1067 data->mRegId = regId;
1068 data->mState = state;
1069 command->mParam = data;
1070 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1071 regId.string(), state);
1072 sendCommand(command);
1073}
1074
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001075void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001076 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001077 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1078 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001079{
1080 sp<AudioCommand>command = new AudioCommand();
1081 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1082 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1083 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001084 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001085 data->mClientConfig = *clientConfig;
1086 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001087 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001088 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001089 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1090 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001091 sendCommand(command);
1092}
1093
Eric Laurent0ede8922014-05-09 18:04:42 -07001094status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1095{
1096 {
1097 Mutex::Autolock _l(mLock);
1098 insertCommand_l(command, delayMs);
1099 mWaitWorkCV.signal();
1100 }
1101 Mutex::Autolock _l(command->mLock);
1102 while (command->mWaitStatus) {
1103 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1104 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1105 command->mStatus = TIMED_OUT;
1106 command->mWaitStatus = false;
1107 }
1108 }
1109 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001110}
1111
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001113void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001115 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001116 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 command->mTime = systemTime() + milliseconds(delayMs);
1118
1119 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001120 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1122 }
1123
1124 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001125 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001126 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1128 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001129
1130 // create audio patch or release audio patch commands are equivalent
1131 // with regard to filtering
1132 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1133 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1134 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1135 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1136 continue;
1137 }
1138 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139
1140 switch (command->mCommand) {
1141 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001142 ParametersData *data = (ParametersData *)command->mParam.get();
1143 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001145 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001146 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1148 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1149 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001150 String8 key;
1151 String8 value;
1152 param.getAt(j, key, value);
1153 for (size_t k = 0; k < param2.size(); k++) {
1154 String8 key2;
1155 String8 value2;
1156 param2.getAt(k, key2, value2);
1157 if (key2 == key) {
1158 param2.remove(key2);
1159 ALOGV("Filtering out parameter %s", key2.string());
1160 break;
1161 }
1162 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 }
1164 // if all keys have been filtered out, remove the command.
1165 // otherwise, update the key value pairs
1166 if (param2.size() == 0) {
1167 removedCommands.add(command2);
1168 } else {
1169 data2->mKeyValuePairs = param2.toString();
1170 }
Eric Laurent21e54562013-09-23 12:08:05 -07001171 command->mTime = command2->mTime;
1172 // force delayMs to non 0 so that code below does not request to wait for
1173 // command status as the command is now delayed
1174 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 } break;
1176
1177 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001178 VolumeData *data = (VolumeData *)command->mParam.get();
1179 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180 if (data->mIO != data2->mIO) break;
1181 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001182 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001183 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001185 command->mTime = command2->mTime;
1186 // force delayMs to non 0 so that code below does not request to wait for
1187 // command status as the command is now delayed
1188 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001190
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001191 case SET_VOICE_VOLUME: {
1192 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1193 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1194 ALOGV("Filtering out voice volume command value %f replaced by %f",
1195 data2->mVolume, data->mVolume);
1196 removedCommands.add(command2);
1197 command->mTime = command2->mTime;
1198 // force delayMs to non 0 so that code below does not request to wait for
1199 // command status as the command is now delayed
1200 delayMs = 1;
1201 } break;
1202
Eric Laurente45b48a2014-09-04 16:40:57 -07001203 case CREATE_AUDIO_PATCH:
1204 case RELEASE_AUDIO_PATCH: {
1205 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001206 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001207 if (command->mCommand == CREATE_AUDIO_PATCH) {
1208 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001209 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001210 } else {
1211 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1212 }
1213 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001214 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001215 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1216 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001217 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001218 } else {
1219 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001220 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001221 }
1222 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001223 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1224 same output. */
1225 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1226 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1227 bool isOutputDiff = false;
1228 if (patch.num_sources == patch2.num_sources) {
1229 for (unsigned count = 0; count < patch.num_sources; count++) {
1230 if (patch.sources[count].id != patch2.sources[count].id) {
1231 isOutputDiff = true;
1232 break;
1233 }
1234 }
1235 if (isOutputDiff)
1236 break;
1237 }
1238 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001239 ALOGV("Filtering out %s audio patch command for handle %d",
1240 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1241 removedCommands.add(command2);
1242 command->mTime = command2->mTime;
1243 // force delayMs to non 0 so that code below does not request to wait for
1244 // command status as the command is now delayed
1245 delayMs = 1;
1246 } break;
1247
Jean-Michel Trivide801052015-04-14 19:10:14 -07001248 case DYN_POLICY_MIX_STATE_UPDATE: {
1249
1250 } break;
1251
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001252 case RECORDING_CONFIGURATION_UPDATE: {
1253
1254 } break;
1255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 case START_TONE:
1257 case STOP_TONE:
1258 default:
1259 break;
1260 }
1261 }
1262
1263 // remove filtered commands
1264 for (size_t j = 0; j < removedCommands.size(); j++) {
1265 // removed commands always have time stamps greater than current command
1266 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001267 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 mAudioCommands.removeAt(k);
1270 break;
1271 }
1272 }
1273 }
1274 removedCommands.clear();
1275
Eric Laurentaa79bef2015-01-15 14:33:51 -08001276 // Disable wait for status if delay is not 0.
1277 // Except for create audio patch command because the returned patch handle
1278 // is needed by audio policy manager
1279 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001280 command->mWaitStatus = false;
1281 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001282
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001284 ALOGV("inserting command: %d at index %zd, num commands %zu",
1285 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 mAudioCommands.insertAt(command, i + 1);
1287}
1288
1289void AudioPolicyService::AudioCommandThread::exit()
1290{
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292 {
1293 AutoMutex _l(mLock);
1294 requestExit();
1295 mWaitWorkCV.signal();
1296 }
Zach Janga754b4f2015-10-27 01:29:34 +00001297 // Note that we can call it from the thread loop if all other references have been released
1298 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299 requestExitAndWait();
1300}
1301
1302void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1303{
1304 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1305 mCommand,
1306 (int)ns2s(mTime),
1307 (int)ns2ms(mTime)%1000,
1308 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001309 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310}
1311
Dima Zavinfce7a472011-04-19 22:30:36 -07001312/******* helpers for the service_ops callbacks defined below *********/
1313void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1314 const char *keyValuePairs,
1315 int delayMs)
1316{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001317 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001318 delayMs);
1319}
1320
1321int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1322 float volume,
1323 audio_io_handle_t output,
1324 int delayMs)
1325{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001326 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001327 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001328}
1329
1330int AudioPolicyService::startTone(audio_policy_tone_t tone,
1331 audio_stream_type_t stream)
1332{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001333 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001334 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001335 }
1336 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001337 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001338 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001339 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001340 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1341 AUDIO_STREAM_VOICE_CALL);
1342 return 0;
1343}
1344
1345int AudioPolicyService::stopTone()
1346{
1347 mTonePlaybackThread->stopToneCommand();
1348 return 0;
1349}
1350
1351int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1352{
1353 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1354}
1355
Dima Zavinfce7a472011-04-19 22:30:36 -07001356extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001357audio_module_handle_t aps_load_hw_module(void *service __unused,
1358 const char *name);
1359audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001360 audio_devices_t *pDevices,
1361 uint32_t *pSamplingRate,
1362 audio_format_t *pFormat,
1363 audio_channel_mask_t *pChannelMask,
1364 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001365 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001366
Eric Laurent2d388ec2014-03-07 13:25:54 -08001367audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368 audio_module_handle_t module,
1369 audio_devices_t *pDevices,
1370 uint32_t *pSamplingRate,
1371 audio_format_t *pFormat,
1372 audio_channel_mask_t *pChannelMask,
1373 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001374 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001375 const audio_offload_info_t *offloadInfo);
1376audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001377 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001378 audio_io_handle_t output2);
1379int aps_close_output(void *service __unused, audio_io_handle_t output);
1380int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1381int aps_restore_output(void *service __unused, audio_io_handle_t output);
1382audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001383 audio_devices_t *pDevices,
1384 uint32_t *pSamplingRate,
1385 audio_format_t *pFormat,
1386 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001387 audio_in_acoustics_t acoustics __unused);
1388audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001389 audio_module_handle_t module,
1390 audio_devices_t *pDevices,
1391 uint32_t *pSamplingRate,
1392 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001393 audio_channel_mask_t *pChannelMask);
1394int aps_close_input(void *service __unused, audio_io_handle_t input);
1395int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001396int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001397 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001398 audio_io_handle_t dst_output);
1399char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1400 const char *keys);
1401void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1402 const char *kv_pairs, int delay_ms);
1403int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001404 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001405 int delay_ms);
1406int aps_start_tone(void *service, audio_policy_tone_t tone,
1407 audio_stream_type_t stream);
1408int aps_stop_tone(void *service);
1409int aps_set_voice_volume(void *service, float volume, int delay_ms);
1410};
Dima Zavinfce7a472011-04-19 22:30:36 -07001411
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001412} // namespace android