blob: 5d25ea837b1cb6fc78793196357e893a4328bc48 [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>
Eric Laurentf1047e82018-04-16 19:18:20 -070029#include <cutils/multiuser.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <cutils/properties.h>
31#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080032#include <binder/ActivityManager.h>
33#include <binder/PermissionController.h>
34#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/String16.h>
36#include <utils/threads.h>
37#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080040#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070041#include <mediautils/ServiceUtilities.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070045
Svet Ganovf4ddfef2018-01-16 07:37:58 -080046#include <private/android_filesystem_config.h>
47
Mathias Agopian65ab4712010-07-14 17:59:35 -070048namespace android {
49
Glenn Kasten8dad0e32012-01-09 08:41:22 -080050static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
51static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080054static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Eric Laurent0ede8922014-05-09 18:04:42 -070056static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010057
Svet Ganovf4ddfef2018-01-16 07:37:58 -080058static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070059
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070063 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070064 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070065{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070066}
67
68void AudioPolicyService::onFirstRef()
69{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 {
71 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080072
Eric Laurent8b1e80b2014-10-07 09:08:47 -070073 // start tone playback thread
74 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
75 // start audio commands thread
76 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
77 // start output activity command thread
78 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070079
Eric Laurent8b1e80b2014-10-07 09:08:47 -070080 mAudioPolicyClient = new AudioPolicyClient(this);
81 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070082 }
bryant_liuba2b4392014-06-11 16:49:30 +080083 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070084 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
85 {
86 Mutex::Autolock _l(mLock);
87 mAudioPolicyEffects = audioPolicyEffects;
88 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080089
90 mUidPolicy = new UidPolicy(this);
91 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070092}
93
94AudioPolicyService::~AudioPolicyService()
95{
96 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070097 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070098 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099
Eric Laurentf269b8e2014-06-09 20:01:29 -0700100 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700101 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -0700102
103 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800104 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800105
106 mUidPolicy->unregisterSelf();
107 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700108}
109
110// A notification client is always registered by AudioSystem when the client process
111// connects to AudioPolicyService.
112void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
113{
Eric Laurent12590252015-08-21 18:40:20 -0700114 if (client == 0) {
115 ALOGW("%s got NULL client", __FUNCTION__);
116 return;
117 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800118 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700119
120 uid_t uid = IPCThreadState::self()->getCallingUid();
121 if (mNotificationClients.indexOfKey(uid) < 0) {
122 sp<NotificationClient> notificationClient = new NotificationClient(this,
123 client,
124 uid);
125 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
126
127 mNotificationClients.add(uid, notificationClient);
128
Marco Nelissenf8880202014-11-14 07:58:25 -0800129 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700130 binder->linkToDeath(notificationClient);
131 }
132}
133
Eric Laurente8726fe2015-06-26 09:39:24 -0700134void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
135{
136 Mutex::Autolock _l(mNotificationClientsLock);
137
138 uid_t uid = IPCThreadState::self()->getCallingUid();
139 if (mNotificationClients.indexOfKey(uid) < 0) {
140 return;
141 }
142 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
143}
144
Eric Laurentb52c1522014-05-20 11:27:36 -0700145// removeNotificationClient() is called when the client process dies.
146void AudioPolicyService::removeNotificationClient(uid_t uid)
147{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800148 {
149 Mutex::Autolock _l(mNotificationClientsLock);
150 mNotificationClients.removeItem(uid);
151 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800152 {
153 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700154 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700155 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700156 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700157 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800158 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700159}
160
161void AudioPolicyService::onAudioPortListUpdate()
162{
163 mOutputCommandThread->updateAudioPortListCommand();
164}
165
166void AudioPolicyService::doOnAudioPortListUpdate()
167{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800168 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700169 for (size_t i = 0; i < mNotificationClients.size(); i++) {
170 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
171 }
172}
173
174void AudioPolicyService::onAudioPatchListUpdate()
175{
176 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177}
178
Eric Laurentb52c1522014-05-20 11:27:36 -0700179void AudioPolicyService::doOnAudioPatchListUpdate()
180{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800181 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700182 for (size_t i = 0; i < mNotificationClients.size(); i++) {
183 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
184 }
185}
186
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700187void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700188{
189 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
190 regId.string(), state);
191 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
192}
193
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700194void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700195{
196 Mutex::Autolock _l(mNotificationClientsLock);
197 for (size_t i = 0; i < mNotificationClients.size(); i++) {
198 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
199 }
200}
201
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800202void AudioPolicyService::onRecordingConfigurationUpdate(int event,
203 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800204 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800205{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800206 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800207 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800208}
209
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800210void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
211 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800212 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800213{
214 Mutex::Autolock _l(mNotificationClientsLock);
215 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800216 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800217 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800218 }
219}
220
221status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
222 audio_patch_handle_t *handle,
223 int delayMs)
224{
225 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
226}
227
228status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
229 int delayMs)
230{
231 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
232}
233
Eric Laurente1715a42014-05-20 11:30:42 -0700234status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
235 int delayMs)
236{
237 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
238}
239
Eric Laurentb52c1522014-05-20 11:27:36 -0700240AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
241 const sp<IAudioPolicyServiceClient>& client,
242 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700243 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
244 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700245{
246}
247
248AudioPolicyService::NotificationClient::~NotificationClient()
249{
250}
251
252void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
253{
254 sp<NotificationClient> keep(this);
255 sp<AudioPolicyService> service = mService.promote();
256 if (service != 0) {
257 service->removeNotificationClient(mUid);
258 }
259}
260
261void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
262{
Eric Laurente8726fe2015-06-26 09:39:24 -0700263 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700264 mAudioPolicyServiceClient->onAudioPortListUpdate();
265 }
266}
267
268void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
269{
Eric Laurente8726fe2015-06-26 09:39:24 -0700270 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700271 mAudioPolicyServiceClient->onAudioPatchListUpdate();
272 }
273}
Eric Laurent57dae992011-07-24 13:36:09 -0700274
Jean-Michel Trivide801052015-04-14 19:10:14 -0700275void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700276 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700277{
Eric Laurentf1047e82018-04-16 19:18:20 -0700278 if (mAudioPolicyServiceClient != 0 && multiuser_get_app_id(mUid) < AID_APP_START) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800279 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
280 }
281}
282
283void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800284 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800285 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
286 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800287{
Eric Laurentf1047e82018-04-16 19:18:20 -0700288 if (mAudioPolicyServiceClient != 0 && multiuser_get_app_id(mUid) < AID_APP_START) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800289 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800290 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700291 }
292}
293
Eric Laurente8726fe2015-06-26 09:39:24 -0700294void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
295{
296 mAudioPortCallbacksEnabled = enabled;
297}
298
299
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700301 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700302 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303}
304
305static bool tryLock(Mutex& mutex)
306{
307 bool locked = false;
308 for (int i = 0; i < kDumpLockRetries; ++i) {
309 if (mutex.tryLock() == NO_ERROR) {
310 locked = true;
311 break;
312 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800313 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314 }
315 return locked;
316}
317
318status_t AudioPolicyService::dumpInternals(int fd)
319{
320 const size_t SIZE = 256;
321 char buffer[SIZE];
322 String8 result;
323
Eric Laurentdce54a12014-03-10 12:19:46 -0700324 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 result.append(buffer);
326 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
327 result.append(buffer);
328 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
329 result.append(buffer);
330
331 write(fd, result.string(), result.size());
332 return NO_ERROR;
333}
334
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800335void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
336{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700337 {
338 Mutex::Autolock _l(mLock);
339 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700340 AutoCallerClear acc;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700341 mAudioPolicyManager->setRecordSilenced(uid, silenced);
342 }
343 }
344 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
345 if (af) {
346 af->setRecordSilenced(uid, silenced);
347 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800348}
349
Glenn Kasten0f11b512014-01-31 16:18:54 -0800350status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
Glenn Kasten44deb052012-02-05 18:09:08 -0800352 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 dumpPermissionDenial(fd);
354 } else {
355 bool locked = tryLock(mLock);
356 if (!locked) {
357 String8 result(kDeadlockedString);
358 write(fd, result.string(), result.size());
359 }
360
361 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800362 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 mAudioCommandThread->dump(fd);
364 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800365 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 mTonePlaybackThread->dump(fd);
367 }
368
Eric Laurentdce54a12014-03-10 12:19:46 -0700369 if (mAudioPolicyManager) {
370 mAudioPolicyManager->dump(fd);
371 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372
373 if (locked) mLock.unlock();
374 }
375 return NO_ERROR;
376}
377
378status_t AudioPolicyService::dumpPermissionDenial(int fd)
379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383 snprintf(buffer, SIZE, "Permission Denial: "
384 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
385 IPCThreadState::self()->getCallingPid(),
386 IPCThreadState::self()->getCallingUid());
387 result.append(buffer);
388 write(fd, result.string(), result.size());
389 return NO_ERROR;
390}
391
392status_t AudioPolicyService::onTransact(
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800393 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
394 switch (code) {
395 case SHELL_COMMAND_TRANSACTION: {
396 int in = data.readFileDescriptor();
397 int out = data.readFileDescriptor();
398 int err = data.readFileDescriptor();
399 int argc = data.readInt32();
400 Vector<String16> args;
401 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
402 args.add(data.readString16());
403 }
404 sp<IBinder> unusedCallback;
405 sp<IResultReceiver> resultReceiver;
406 status_t status;
407 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
408 return status;
409 }
410 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
411 return status;
412 }
413 status = shellCommand(in, out, err, args);
414 if (resultReceiver != nullptr) {
415 resultReceiver->send(status);
416 }
417 return NO_ERROR;
418 }
419 }
420
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 return BnAudioPolicyService::onTransact(code, data, reply, flags);
422}
423
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800424// ------------------- Shell command implementation -------------------
425
426// NOTE: This is a remote API - make sure all args are validated
427status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) {
428 if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) {
429 return PERMISSION_DENIED;
430 }
431 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
432 return BAD_VALUE;
433 }
434 if (args.size() == 3 && args[0] == String16("set-uid-state")) {
435 return handleSetUidState(args, err);
436 } else if (args.size() == 2 && args[0] == String16("reset-uid-state")) {
437 return handleResetUidState(args, err);
438 } else if (args.size() == 2 && args[0] == String16("get-uid-state")) {
439 return handleGetUidState(args, out, err);
440 } else if (args.size() == 1 && args[0] == String16("help")) {
441 printHelp(out);
442 return NO_ERROR;
443 }
444 printHelp(err);
445 return BAD_VALUE;
446}
447
448status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) {
449 PermissionController pc;
450 int uid = pc.getPackageUid(args[1], 0);
451 if (uid <= 0) {
452 ALOGE("Unknown package: '%s'", String8(args[1]).string());
453 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
454 return BAD_VALUE;
455 }
456 bool active = false;
457 if (args[2] == String16("active")) {
458 active = true;
459 } else if ((args[2] != String16("idle"))) {
460 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
461 return BAD_VALUE;
462 }
463 mUidPolicy->addOverrideUid(uid, active);
464 return NO_ERROR;
465}
466
467status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) {
468 PermissionController pc;
469 int uid = pc.getPackageUid(args[1], 0);
470 if (uid < 0) {
471 ALOGE("Unknown package: '%s'", String8(args[1]).string());
472 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
473 return BAD_VALUE;
474 }
475 mUidPolicy->removeOverrideUid(uid);
476 return NO_ERROR;
477}
478
479status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) {
480 PermissionController pc;
481 int uid = pc.getPackageUid(args[1], 0);
482 if (uid < 0) {
483 ALOGE("Unknown package: '%s'", String8(args[1]).string());
484 dprintf(err, "Unknown package: '%s'\n", String8(args[1]).string());
485 return BAD_VALUE;
486 }
487 if (mUidPolicy->isUidActive(uid)) {
488 return dprintf(out, "active\n");
489 } else {
490 return dprintf(out, "idle\n");
491 }
492}
493
494status_t AudioPolicyService::printHelp(int out) {
495 return dprintf(out, "Audio policy service commands:\n"
496 " get-uid-state <PACKAGE> gets the uid state\n"
497 " set-uid-state <PACKAGE> <active|idle> overrides the uid state\n"
498 " reset-uid-state <PACKAGE> clears the uid state override\n"
499 " help print this message\n");
500}
501
502// ----------- AudioPolicyService::UidPolicy implementation ----------
503
504void AudioPolicyService::UidPolicy::registerSelf() {
505 ActivityManager am;
506 am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
507 | ActivityManager::UID_OBSERVER_IDLE
508 | ActivityManager::UID_OBSERVER_ACTIVE,
509 ActivityManager::PROCESS_STATE_UNKNOWN,
510 String16("audioserver"));
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700511 status_t res = am.linkToDeath(this);
512 if (!res) {
513 Mutex::Autolock _l(mLock);
514 mObserverRegistered = true;
515 } else {
516 ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res);
517 am.unregisterUidObserver(this);
518 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800519}
520
521void AudioPolicyService::UidPolicy::unregisterSelf() {
522 ActivityManager am;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700523 am.unlinkToDeath(this);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800524 am.unregisterUidObserver(this);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700525 Mutex::Autolock _l(mLock);
526 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800527}
528
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700529void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) {
530 Mutex::Autolock _l(mLock);
531 mCachedUids.clear();
532 mObserverRegistered = false;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800533}
534
535bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700536 if (isServiceUid(uid)) return true;
537 bool needToReregister = false;
538 {
539 Mutex::Autolock _l(mLock);
540 needToReregister = !mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800541 }
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700542 if (needToReregister) {
543 // Looks like ActivityManager has died previously, attempt to re-register.
544 registerSelf();
545 }
546 {
547 Mutex::Autolock _l(mLock);
548 auto overrideIter = mOverrideUids.find(uid);
549 if (overrideIter != mOverrideUids.end()) {
550 return overrideIter->second;
551 }
552 // In an absense of the ActivityManager, assume everything to be active.
553 if (!mObserverRegistered) return true;
554 auto cacheIter = mCachedUids.find(uid);
Mikhail Naganoveba668a2018-04-05 08:13:15 -0700555 if (cacheIter != mCachedUids.end()) {
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700556 return cacheIter->second;
557 }
558 }
559 ActivityManager am;
560 bool active = am.isUidActive(uid, String16("audioserver"));
561 {
562 Mutex::Autolock _l(mLock);
563 mCachedUids.insert(std::pair<uid_t, bool>(uid, active));
564 }
565 return active;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800566}
567
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700568void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) {
569 updateUidCache(uid, true, true);
570}
571
572void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) {
573 updateUidCache(uid, false, false);
574}
575
576void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) {
577 updateUidCache(uid, false, true);
578}
579
580bool AudioPolicyService::UidPolicy::isServiceUid(uid_t uid) const {
Eric Laurentf1047e82018-04-16 19:18:20 -0700581 return multiuser_get_app_id(uid) < AID_APP_START;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700582}
583
584void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
585 sp<AudioPolicyService> service = mService.promote();
586 if (service != nullptr) {
587 service->setRecordSilenced(uid, !active);
588 }
589}
590
591void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
592 if (isServiceUid(uid)) return;
593 bool wasOverridden = false, wasActive = false;
594 {
595 Mutex::Autolock _l(mLock);
596 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
597 }
598 if (!wasOverridden && insert) {
599 notifyService(uid, active); // Started to override.
600 } else if (wasOverridden && !insert) {
601 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
602 } else if (wasActive != active) {
603 notifyService(uid, active); // Override updated.
604 }
605}
606
607void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
608 if (isServiceUid(uid)) return;
609 bool wasActive = false;
610 {
611 Mutex::Autolock _l(mLock);
612 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
613 // Do not notify service if currently overridden.
614 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
615 }
616 bool nowActive = active && insert;
617 if (wasActive != nowActive) notifyService(uid, nowActive);
618}
619
620void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
621 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
622 auto it = uids->find(uid);
623 if (it != uids->end()) {
624 if (wasThere != nullptr) *wasThere = true;
625 if (wasActive != nullptr) *wasActive = it->second;
626 if (insert) {
627 it->second = active;
628 } else {
629 uids->erase(it);
630 }
631 } else if (insert) {
632 uids->insert(std::pair<uid_t, bool>(uid, active));
633 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800634}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636// ----------- AudioPolicyService::AudioCommandThread implementation ----------
637
Eric Laurentbfb1b832013-01-07 09:53:42 -0800638AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
639 const wp<AudioPolicyService>& service)
640 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
642 mpToneGenerator = NULL;
643}
644
645
646AudioPolicyService::AudioCommandThread::~AudioCommandThread()
647{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 release_wake_lock(mName.string());
650 }
651 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800652 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653}
654
655void AudioPolicyService::AudioCommandThread::onFirstRef()
656{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800657 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658}
659
660bool AudioPolicyService::AudioCommandThread::threadLoop()
661{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800662 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663
664 mLock.lock();
665 while (!exitPending())
666 {
Eric Laurent59a89232014-06-08 14:14:17 -0700667 sp<AudioPolicyService> svc;
668 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 nsecs_t curTime = systemTime();
670 // commands are sorted by increasing time stamp: execute them from index 0 and up
671 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700672 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700674 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675
676 switch (command->mCommand) {
677 case START_TONE: {
678 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700679 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800682 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
684 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 mLock.lock();
686 }break;
687 case STOP_TONE: {
688 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100689 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 if (mpToneGenerator != NULL) {
691 mpToneGenerator->stopTone();
692 delete mpToneGenerator;
693 mpToneGenerator = NULL;
694 }
695 mLock.lock();
696 }break;
697 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700698 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700700 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
701 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
702 data->mVolume,
703 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 }break;
705 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700706 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700707 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
708 data->mKeyValuePairs.string(), data->mIO);
709 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700710 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700712 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100713 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700714 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800717 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700718 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800719 ALOGV("AudioCommandThread() processing stop output %d",
720 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700721 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 if (svc == 0) {
723 break;
724 }
725 mLock.unlock();
726 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
727 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800728 }break;
729 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700730 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800731 ALOGV("AudioCommandThread() processing release output %d",
732 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700733 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800734 if (svc == 0) {
735 break;
736 }
737 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800738 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800739 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800740 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700741 case CREATE_AUDIO_PATCH: {
742 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
743 ALOGV("AudioCommandThread() processing create audio patch");
744 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
745 if (af == 0) {
746 command->mStatus = PERMISSION_DENIED;
747 } else {
748 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
749 }
750 } break;
751 case RELEASE_AUDIO_PATCH: {
752 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
753 ALOGV("AudioCommandThread() processing release audio patch");
754 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
755 if (af == 0) {
756 command->mStatus = PERMISSION_DENIED;
757 } else {
758 command->mStatus = af->releaseAudioPatch(data->mHandle);
759 }
760 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700761 case UPDATE_AUDIOPORT_LIST: {
762 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700763 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700764 if (svc == 0) {
765 break;
766 }
767 mLock.unlock();
768 svc->doOnAudioPortListUpdate();
769 mLock.lock();
770 }break;
771 case UPDATE_AUDIOPATCH_LIST: {
772 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700773 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700774 if (svc == 0) {
775 break;
776 }
777 mLock.unlock();
778 svc->doOnAudioPatchListUpdate();
779 mLock.lock();
780 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700781 case SET_AUDIOPORT_CONFIG: {
782 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
783 ALOGV("AudioCommandThread() processing set port config");
784 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
785 if (af == 0) {
786 command->mStatus = PERMISSION_DENIED;
787 } else {
788 command->mStatus = af->setAudioPortConfig(&data->mConfig);
789 }
790 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700791 case DYN_POLICY_MIX_STATE_UPDATE: {
792 DynPolicyMixStateUpdateData *data =
793 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700794 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
795 data->mRegId.string(), data->mState);
796 svc = mService.promote();
797 if (svc == 0) {
798 break;
799 }
800 mLock.unlock();
801 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
802 mLock.lock();
803 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800804 case RECORDING_CONFIGURATION_UPDATE: {
805 RecordingConfigurationUpdateData *data =
806 (RecordingConfigurationUpdateData *)command->mParam.get();
807 ALOGV("AudioCommandThread() processing recording configuration update");
808 svc = mService.promote();
809 if (svc == 0) {
810 break;
811 }
812 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800813 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
814 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800815 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800816 mLock.lock();
817 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000819 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700821 {
822 Mutex::Autolock _l(command->mLock);
823 if (command->mWaitStatus) {
824 command->mWaitStatus = false;
825 command->mCond.signal();
826 }
827 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800828 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000829 // release mLock before releasing strong reference on the service as
830 // AudioPolicyService destructor calls AudioCommandThread::exit() which
831 // acquires mLock.
832 mLock.unlock();
833 svc.clear();
834 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 } else {
836 waitTime = mAudioCommands[0]->mTime - curTime;
837 break;
838 }
839 }
Zach Janga754b4f2015-10-27 01:29:34 +0000840
841 // release delayed commands wake lock if the queue is empty
842 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700843 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000844 }
845
846 // At this stage we have either an empty command queue or the first command in the queue
847 // has a finite delay. So unless we are exiting it is safe to wait.
848 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700849 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800850 if (waitTime == -1) {
851 mWaitWorkCV.wait(mLock);
852 } else {
853 mWaitWorkCV.waitRelative(mLock, waitTime);
854 }
Eric Laurent59a89232014-06-08 14:14:17 -0700855 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700857 // release delayed commands wake lock before quitting
858 if (!mAudioCommands.isEmpty()) {
859 release_wake_lock(mName.string());
860 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 mLock.unlock();
862 return false;
863}
864
865status_t AudioPolicyService::AudioCommandThread::dump(int fd)
866{
867 const size_t SIZE = 256;
868 char buffer[SIZE];
869 String8 result;
870
871 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
872 result.append(buffer);
873 write(fd, result.string(), result.size());
874
875 bool locked = tryLock(mLock);
876 if (!locked) {
877 String8 result2(kCmdDeadlockedString);
878 write(fd, result2.string(), result2.size());
879 }
880
881 snprintf(buffer, SIZE, "- Commands:\n");
882 result = String8(buffer);
883 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800884 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 mAudioCommands[i]->dump(buffer, SIZE);
886 result.append(buffer);
887 }
888 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700889 if (mLastCommand != 0) {
890 mLastCommand->dump(buffer, SIZE);
891 result.append(buffer);
892 } else {
893 result.append(" none\n");
894 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895
896 write(fd, result.string(), result.size());
897
898 if (locked) mLock.unlock();
899
900 return NO_ERROR;
901}
902
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800903void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
904 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905{
Eric Laurent0ede8922014-05-09 18:04:42 -0700906 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700908 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 data->mType = type;
910 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100911 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700913 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914}
915
916void AudioPolicyService::AudioCommandThread::stopToneCommand()
917{
Eric Laurent0ede8922014-05-09 18:04:42 -0700918 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100920 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700921 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922}
923
Glenn Kastenfff6d712012-01-12 16:38:12 -0800924status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700925 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800926 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700927 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928{
Eric Laurent0ede8922014-05-09 18:04:42 -0700929 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700931 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 data->mStream = stream;
933 data->mVolume = volume;
934 data->mIO = output;
935 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700936 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100937 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700938 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940}
941
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800942status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700943 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700944 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945{
Eric Laurent0ede8922014-05-09 18:04:42 -0700946 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700948 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700950 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700952 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700954 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700955 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956}
957
958status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
959{
Eric Laurent0ede8922014-05-09 18:04:42 -0700960 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 data->mVolume = volume;
964 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700965 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700967 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968}
969
Eric Laurentbfb1b832013-01-07 09:53:42 -0800970void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
971 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800972 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800973{
Eric Laurent0ede8922014-05-09 18:04:42 -0700974 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800975 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700976 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800977 data->mIO = output;
978 data->mStream = stream;
979 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100980 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800981 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700982 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800983}
984
Eric Laurente83b55d2014-11-14 10:06:21 -0800985void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
986 audio_stream_type_t stream,
987 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800988{
Eric Laurent0ede8922014-05-09 18:04:42 -0700989 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800990 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700991 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800992 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800993 data->mStream = stream;
994 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100995 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800996 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700997 sendCommand(command);
998}
999
Eric Laurent951f4552014-05-20 10:48:17 -07001000status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
1001 const struct audio_patch *patch,
1002 audio_patch_handle_t *handle,
1003 int delayMs)
1004{
1005 status_t status = NO_ERROR;
1006
1007 sp<AudioCommand> command = new AudioCommand();
1008 command->mCommand = CREATE_AUDIO_PATCH;
1009 CreateAudioPatchData *data = new CreateAudioPatchData();
1010 data->mPatch = *patch;
1011 data->mHandle = *handle;
1012 command->mParam = data;
1013 command->mWaitStatus = true;
1014 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1015 status = sendCommand(command, delayMs);
1016 if (status == NO_ERROR) {
1017 *handle = data->mHandle;
1018 }
1019 return status;
1020}
1021
1022status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1023 int delayMs)
1024{
1025 sp<AudioCommand> command = new AudioCommand();
1026 command->mCommand = RELEASE_AUDIO_PATCH;
1027 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1028 data->mHandle = handle;
1029 command->mParam = data;
1030 command->mWaitStatus = true;
1031 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1032 return sendCommand(command, delayMs);
1033}
1034
Eric Laurentb52c1522014-05-20 11:27:36 -07001035void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1036{
1037 sp<AudioCommand> command = new AudioCommand();
1038 command->mCommand = UPDATE_AUDIOPORT_LIST;
1039 ALOGV("AudioCommandThread() adding update audio port list");
1040 sendCommand(command);
1041}
1042
1043void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1044{
1045 sp<AudioCommand>command = new AudioCommand();
1046 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1047 ALOGV("AudioCommandThread() adding update audio patch list");
1048 sendCommand(command);
1049}
1050
Eric Laurente1715a42014-05-20 11:30:42 -07001051status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1052 const struct audio_port_config *config, int delayMs)
1053{
1054 sp<AudioCommand> command = new AudioCommand();
1055 command->mCommand = SET_AUDIOPORT_CONFIG;
1056 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1057 data->mConfig = *config;
1058 command->mParam = data;
1059 command->mWaitStatus = true;
1060 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1061 return sendCommand(command, delayMs);
1062}
1063
Jean-Michel Trivide801052015-04-14 19:10:14 -07001064void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001065 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001066{
1067 sp<AudioCommand> command = new AudioCommand();
1068 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1069 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1070 data->mRegId = regId;
1071 data->mState = state;
1072 command->mParam = data;
1073 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1074 regId.string(), state);
1075 sendCommand(command);
1076}
1077
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001078void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001079 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001080 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1081 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001082{
1083 sp<AudioCommand>command = new AudioCommand();
1084 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1085 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1086 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001087 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001088 data->mClientConfig = *clientConfig;
1089 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001090 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001091 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001092 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1093 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001094 sendCommand(command);
1095}
1096
Eric Laurent0ede8922014-05-09 18:04:42 -07001097status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1098{
1099 {
1100 Mutex::Autolock _l(mLock);
1101 insertCommand_l(command, delayMs);
1102 mWaitWorkCV.signal();
1103 }
1104 Mutex::Autolock _l(command->mLock);
1105 while (command->mWaitStatus) {
1106 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1107 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1108 command->mStatus = TIMED_OUT;
1109 command->mWaitStatus = false;
1110 }
1111 }
1112 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001113}
1114
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001116void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001118 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001119 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 command->mTime = systemTime() + milliseconds(delayMs);
1121
1122 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001123 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1125 }
1126
1127 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001128 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001129 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1131 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001132
1133 // create audio patch or release audio patch commands are equivalent
1134 // with regard to filtering
1135 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1136 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1137 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1138 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1139 continue;
1140 }
1141 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142
1143 switch (command->mCommand) {
1144 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001145 ParametersData *data = (ParametersData *)command->mParam.get();
1146 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001148 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001149 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1151 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1152 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001153 String8 key;
1154 String8 value;
1155 param.getAt(j, key, value);
1156 for (size_t k = 0; k < param2.size(); k++) {
1157 String8 key2;
1158 String8 value2;
1159 param2.getAt(k, key2, value2);
1160 if (key2 == key) {
1161 param2.remove(key2);
1162 ALOGV("Filtering out parameter %s", key2.string());
1163 break;
1164 }
1165 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 }
1167 // if all keys have been filtered out, remove the command.
1168 // otherwise, update the key value pairs
1169 if (param2.size() == 0) {
1170 removedCommands.add(command2);
1171 } else {
1172 data2->mKeyValuePairs = param2.toString();
1173 }
Eric Laurent21e54562013-09-23 12:08:05 -07001174 command->mTime = command2->mTime;
1175 // force delayMs to non 0 so that code below does not request to wait for
1176 // command status as the command is now delayed
1177 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 } break;
1179
1180 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001181 VolumeData *data = (VolumeData *)command->mParam.get();
1182 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183 if (data->mIO != data2->mIO) break;
1184 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001185 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001186 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001188 command->mTime = command2->mTime;
1189 // force delayMs to non 0 so that code below does not request to wait for
1190 // command status as the command is now delayed
1191 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001193
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001194 case SET_VOICE_VOLUME: {
1195 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1196 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1197 ALOGV("Filtering out voice volume command value %f replaced by %f",
1198 data2->mVolume, data->mVolume);
1199 removedCommands.add(command2);
1200 command->mTime = command2->mTime;
1201 // force delayMs to non 0 so that code below does not request to wait for
1202 // command status as the command is now delayed
1203 delayMs = 1;
1204 } break;
1205
Eric Laurente45b48a2014-09-04 16:40:57 -07001206 case CREATE_AUDIO_PATCH:
1207 case RELEASE_AUDIO_PATCH: {
1208 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001209 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001210 if (command->mCommand == CREATE_AUDIO_PATCH) {
1211 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001212 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001213 } else {
1214 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1215 }
1216 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001217 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001218 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1219 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001220 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001221 } else {
1222 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001223 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001224 }
1225 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001226 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1227 same output. */
1228 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1229 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1230 bool isOutputDiff = false;
1231 if (patch.num_sources == patch2.num_sources) {
1232 for (unsigned count = 0; count < patch.num_sources; count++) {
1233 if (patch.sources[count].id != patch2.sources[count].id) {
1234 isOutputDiff = true;
1235 break;
1236 }
1237 }
1238 if (isOutputDiff)
1239 break;
1240 }
1241 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001242 ALOGV("Filtering out %s audio patch command for handle %d",
1243 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1244 removedCommands.add(command2);
1245 command->mTime = command2->mTime;
1246 // force delayMs to non 0 so that code below does not request to wait for
1247 // command status as the command is now delayed
1248 delayMs = 1;
1249 } break;
1250
Jean-Michel Trivide801052015-04-14 19:10:14 -07001251 case DYN_POLICY_MIX_STATE_UPDATE: {
1252
1253 } break;
1254
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001255 case RECORDING_CONFIGURATION_UPDATE: {
1256
1257 } break;
1258
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 case START_TONE:
1260 case STOP_TONE:
1261 default:
1262 break;
1263 }
1264 }
1265
1266 // remove filtered commands
1267 for (size_t j = 0; j < removedCommands.size(); j++) {
1268 // removed commands always have time stamps greater than current command
1269 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001270 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001271 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 mAudioCommands.removeAt(k);
1273 break;
1274 }
1275 }
1276 }
1277 removedCommands.clear();
1278
Eric Laurentaa79bef2015-01-15 14:33:51 -08001279 // Disable wait for status if delay is not 0.
1280 // Except for create audio patch command because the returned patch handle
1281 // is needed by audio policy manager
1282 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001283 command->mWaitStatus = false;
1284 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001285
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001287 ALOGV("inserting command: %d at index %zd, num commands %zu",
1288 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 mAudioCommands.insertAt(command, i + 1);
1290}
1291
1292void AudioPolicyService::AudioCommandThread::exit()
1293{
Steve Block3856b092011-10-20 11:56:00 +01001294 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295 {
1296 AutoMutex _l(mLock);
1297 requestExit();
1298 mWaitWorkCV.signal();
1299 }
Zach Janga754b4f2015-10-27 01:29:34 +00001300 // Note that we can call it from the thread loop if all other references have been released
1301 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302 requestExitAndWait();
1303}
1304
1305void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1306{
1307 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1308 mCommand,
1309 (int)ns2s(mTime),
1310 (int)ns2ms(mTime)%1000,
1311 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001312 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313}
1314
Dima Zavinfce7a472011-04-19 22:30:36 -07001315/******* helpers for the service_ops callbacks defined below *********/
1316void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1317 const char *keyValuePairs,
1318 int delayMs)
1319{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001320 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001321 delayMs);
1322}
1323
1324int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1325 float volume,
1326 audio_io_handle_t output,
1327 int delayMs)
1328{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001329 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001330 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001331}
1332
1333int AudioPolicyService::startTone(audio_policy_tone_t tone,
1334 audio_stream_type_t stream)
1335{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001336 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001337 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001338 }
1339 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001340 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001341 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001342 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001343 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1344 AUDIO_STREAM_VOICE_CALL);
1345 return 0;
1346}
1347
1348int AudioPolicyService::stopTone()
1349{
1350 mTonePlaybackThread->stopToneCommand();
1351 return 0;
1352}
1353
1354int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1355{
1356 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1357}
1358
Dima Zavinfce7a472011-04-19 22:30:36 -07001359extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001360audio_module_handle_t aps_load_hw_module(void *service __unused,
1361 const char *name);
1362audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001363 audio_devices_t *pDevices,
1364 uint32_t *pSamplingRate,
1365 audio_format_t *pFormat,
1366 audio_channel_mask_t *pChannelMask,
1367 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001368 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001369
Eric Laurent2d388ec2014-03-07 13:25:54 -08001370audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001371 audio_module_handle_t module,
1372 audio_devices_t *pDevices,
1373 uint32_t *pSamplingRate,
1374 audio_format_t *pFormat,
1375 audio_channel_mask_t *pChannelMask,
1376 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001377 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001378 const audio_offload_info_t *offloadInfo);
1379audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001380 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001381 audio_io_handle_t output2);
1382int aps_close_output(void *service __unused, audio_io_handle_t output);
1383int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1384int aps_restore_output(void *service __unused, audio_io_handle_t output);
1385audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001386 audio_devices_t *pDevices,
1387 uint32_t *pSamplingRate,
1388 audio_format_t *pFormat,
1389 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001390 audio_in_acoustics_t acoustics __unused);
1391audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001392 audio_module_handle_t module,
1393 audio_devices_t *pDevices,
1394 uint32_t *pSamplingRate,
1395 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001396 audio_channel_mask_t *pChannelMask);
1397int aps_close_input(void *service __unused, audio_io_handle_t input);
1398int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001399int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001400 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001401 audio_io_handle_t dst_output);
1402char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1403 const char *keys);
1404void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1405 const char *kv_pairs, int delay_ms);
1406int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001408 int delay_ms);
1409int aps_start_tone(void *service, audio_policy_tone_t tone,
1410 audio_stream_type_t stream);
1411int aps_stop_tone(void *service);
1412int aps_set_voice_volume(void *service, float volume, int delay_ms);
1413};
Dima Zavinfce7a472011-04-19 22:30:36 -07001414
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001415} // namespace android