blob: 65b84957d48fc589514069856f66b1f047280983 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080031#include <binder/ActivityManager.h>
32#include <binder/PermissionController.h>
33#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070038#include <media/AudioEffect.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080039#include <media/AudioParameter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Svet Ganovf4ddfef2018-01-16 07:37:58 -080055static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY");
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Mathias Agopian65ab4712010-07-14 17:59:35 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070060 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070061 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070063}
64
65void AudioPolicyService::onFirstRef()
66{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070067 {
68 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080069
Eric Laurent8b1e80b2014-10-07 09:08:47 -070070 // start tone playback thread
71 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
72 // start audio commands thread
73 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
74 // start output activity command thread
75 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 mAudioPolicyClient = new AudioPolicyClient(this);
78 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070079 }
bryant_liuba2b4392014-06-11 16:49:30 +080080 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070081 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
82 {
83 Mutex::Autolock _l(mLock);
84 mAudioPolicyEffects = audioPolicyEffects;
85 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -080086
87 mUidPolicy = new UidPolicy(this);
88 mUidPolicy->registerSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -070089}
90
91AudioPolicyService::~AudioPolicyService()
92{
93 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070094 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070095 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070096
Eric Laurentf269b8e2014-06-09 20:01:29 -070097 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070098 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070099
100 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800101 mAudioPolicyEffects.clear();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800102
103 mUidPolicy->unregisterSelf();
104 mUidPolicy.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700105}
106
107// A notification client is always registered by AudioSystem when the client process
108// connects to AudioPolicyService.
109void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
110{
Eric Laurent12590252015-08-21 18:40:20 -0700111 if (client == 0) {
112 ALOGW("%s got NULL client", __FUNCTION__);
113 return;
114 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800115 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700116
117 uid_t uid = IPCThreadState::self()->getCallingUid();
118 if (mNotificationClients.indexOfKey(uid) < 0) {
119 sp<NotificationClient> notificationClient = new NotificationClient(this,
120 client,
121 uid);
122 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
123
124 mNotificationClients.add(uid, notificationClient);
125
Marco Nelissenf8880202014-11-14 07:58:25 -0800126 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700127 binder->linkToDeath(notificationClient);
128 }
129}
130
Eric Laurente8726fe2015-06-26 09:39:24 -0700131void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
132{
133 Mutex::Autolock _l(mNotificationClientsLock);
134
135 uid_t uid = IPCThreadState::self()->getCallingUid();
136 if (mNotificationClients.indexOfKey(uid) < 0) {
137 return;
138 }
139 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
140}
141
Eric Laurentb52c1522014-05-20 11:27:36 -0700142// removeNotificationClient() is called when the client process dies.
143void AudioPolicyService::removeNotificationClient(uid_t uid)
144{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800145 {
146 Mutex::Autolock _l(mNotificationClientsLock);
147 mNotificationClients.removeItem(uid);
148 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800149 {
150 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700151 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700152 // called from binder death notification: no need to clear caller identity
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700153 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700154 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800155 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700156}
157
158void AudioPolicyService::onAudioPortListUpdate()
159{
160 mOutputCommandThread->updateAudioPortListCommand();
161}
162
163void AudioPolicyService::doOnAudioPortListUpdate()
164{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800165 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700166 for (size_t i = 0; i < mNotificationClients.size(); i++) {
167 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
168 }
169}
170
171void AudioPolicyService::onAudioPatchListUpdate()
172{
173 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174}
175
Eric Laurentb52c1522014-05-20 11:27:36 -0700176void AudioPolicyService::doOnAudioPatchListUpdate()
177{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800178 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700179 for (size_t i = 0; i < mNotificationClients.size(); i++) {
180 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
181 }
182}
183
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700184void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700185{
186 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
187 regId.string(), state);
188 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
189}
190
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700191void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700192{
193 Mutex::Autolock _l(mNotificationClientsLock);
194 for (size_t i = 0; i < mNotificationClients.size(); i++) {
195 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
196 }
197}
198
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800199void AudioPolicyService::onRecordingConfigurationUpdate(int event,
200 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800201 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800202{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800203 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800204 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800205}
206
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800207void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
208 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800209 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800210{
211 Mutex::Autolock _l(mNotificationClientsLock);
212 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800213 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800214 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800215 }
216}
217
218status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
219 audio_patch_handle_t *handle,
220 int delayMs)
221{
222 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
223}
224
225status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
226 int delayMs)
227{
228 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
229}
230
Eric Laurente1715a42014-05-20 11:30:42 -0700231status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
232 int delayMs)
233{
234 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
235}
236
Eric Laurentb52c1522014-05-20 11:27:36 -0700237AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
238 const sp<IAudioPolicyServiceClient>& client,
239 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700240 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
241 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700242{
243}
244
245AudioPolicyService::NotificationClient::~NotificationClient()
246{
247}
248
249void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
250{
251 sp<NotificationClient> keep(this);
252 sp<AudioPolicyService> service = mService.promote();
253 if (service != 0) {
254 service->removeNotificationClient(mUid);
255 }
256}
257
258void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
259{
Eric Laurente8726fe2015-06-26 09:39:24 -0700260 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700261 mAudioPolicyServiceClient->onAudioPortListUpdate();
262 }
263}
264
265void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
266{
Eric Laurente8726fe2015-06-26 09:39:24 -0700267 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700268 mAudioPolicyServiceClient->onAudioPatchListUpdate();
269 }
270}
Eric Laurent57dae992011-07-24 13:36:09 -0700271
Jean-Michel Trivide801052015-04-14 19:10:14 -0700272void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700273 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700274{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700275 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800276 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
277 }
278}
279
280void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800281 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800282 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
283 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800284{
Andy Hung4ef19fa2018-05-15 19:35:29 -0700285 if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800286 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800287 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700288 }
289}
290
Eric Laurente8726fe2015-06-26 09:39:24 -0700291void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
292{
293 mAudioPortCallbacksEnabled = enabled;
294}
295
296
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700298 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700299 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300}
301
302static bool tryLock(Mutex& mutex)
303{
304 bool locked = false;
305 for (int i = 0; i < kDumpLockRetries; ++i) {
306 if (mutex.tryLock() == NO_ERROR) {
307 locked = true;
308 break;
309 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800310 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 }
312 return locked;
313}
314
315status_t AudioPolicyService::dumpInternals(int fd)
316{
317 const size_t SIZE = 256;
318 char buffer[SIZE];
319 String8 result;
320
Eric Laurentdce54a12014-03-10 12:19:46 -0700321 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 result.append(buffer);
323 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
324 result.append(buffer);
325 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
326 result.append(buffer);
327
328 write(fd, result.string(), result.size());
329 return NO_ERROR;
330}
331
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800332void AudioPolicyService::setRecordSilenced(uid_t uid, bool silenced)
333{
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700334 {
335 Mutex::Autolock _l(mLock);
336 if (mAudioPolicyManager) {
Eric Laurent10b71232018-04-13 18:14:44 -0700337 AutoCallerClear acc;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700338 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
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700577void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
578 sp<AudioPolicyService> service = mService.promote();
579 if (service != nullptr) {
580 service->setRecordSilenced(uid, !active);
581 }
582}
583
584void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) {
585 if (isServiceUid(uid)) return;
586 bool wasOverridden = false, wasActive = false;
587 {
588 Mutex::Autolock _l(mLock);
589 updateUidLocked(&mOverrideUids, uid, active, insert, &wasOverridden, &wasActive);
590 }
591 if (!wasOverridden && insert) {
592 notifyService(uid, active); // Started to override.
593 } else if (wasOverridden && !insert) {
594 notifyService(uid, isUidActive(uid)); // Override ceased, notify with ground truth.
595 } else if (wasActive != active) {
596 notifyService(uid, active); // Override updated.
597 }
598}
599
600void AudioPolicyService::UidPolicy::updateUidCache(uid_t uid, bool active, bool insert) {
601 if (isServiceUid(uid)) return;
602 bool wasActive = false;
603 {
604 Mutex::Autolock _l(mLock);
605 updateUidLocked(&mCachedUids, uid, active, insert, nullptr, &wasActive);
606 // Do not notify service if currently overridden.
607 if (mOverrideUids.find(uid) != mOverrideUids.end()) return;
608 }
609 bool nowActive = active && insert;
610 if (wasActive != nowActive) notifyService(uid, nowActive);
611}
612
613void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, bool> *uids,
614 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive) {
615 auto it = uids->find(uid);
616 if (it != uids->end()) {
617 if (wasThere != nullptr) *wasThere = true;
618 if (wasActive != nullptr) *wasActive = it->second;
619 if (insert) {
620 it->second = active;
621 } else {
622 uids->erase(it);
623 }
624 } else if (insert) {
625 uids->insert(std::pair<uid_t, bool>(uid, active));
626 }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800627}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629// ----------- AudioPolicyService::AudioCommandThread implementation ----------
630
Eric Laurentbfb1b832013-01-07 09:53:42 -0800631AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
632 const wp<AudioPolicyService>& service)
633 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634{
635 mpToneGenerator = NULL;
636}
637
638
639AudioPolicyService::AudioCommandThread::~AudioCommandThread()
640{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800641 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 release_wake_lock(mName.string());
643 }
644 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800645 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646}
647
648void AudioPolicyService::AudioCommandThread::onFirstRef()
649{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800650 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651}
652
653bool AudioPolicyService::AudioCommandThread::threadLoop()
654{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800655 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656
657 mLock.lock();
658 while (!exitPending())
659 {
Eric Laurent59a89232014-06-08 14:14:17 -0700660 sp<AudioPolicyService> svc;
661 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662 nsecs_t curTime = systemTime();
663 // commands are sorted by increasing time stamp: execute them from index 0 and up
664 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700667 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668
669 switch (command->mCommand) {
670 case START_TONE: {
671 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700672 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800675 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
677 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 mLock.lock();
679 }break;
680 case STOP_TONE: {
681 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100682 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 if (mpToneGenerator != NULL) {
684 mpToneGenerator->stopTone();
685 delete mpToneGenerator;
686 mpToneGenerator = NULL;
687 }
688 mLock.lock();
689 }break;
690 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700691 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700693 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
694 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
695 data->mVolume,
696 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 }break;
698 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700699 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700700 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
701 data->mKeyValuePairs.string(), data->mIO);
702 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700703 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700705 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100706 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700707 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800710 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700711 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800712 ALOGV("AudioCommandThread() processing stop output %d",
713 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700714 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800715 if (svc == 0) {
716 break;
717 }
718 mLock.unlock();
719 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
720 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 }break;
722 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700723 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 ALOGV("AudioCommandThread() processing release output %d",
725 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700726 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800727 if (svc == 0) {
728 break;
729 }
730 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800731 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800732 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800733 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700734 case CREATE_AUDIO_PATCH: {
735 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
736 ALOGV("AudioCommandThread() processing create audio patch");
737 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
738 if (af == 0) {
739 command->mStatus = PERMISSION_DENIED;
740 } else {
741 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
742 }
743 } break;
744 case RELEASE_AUDIO_PATCH: {
745 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
746 ALOGV("AudioCommandThread() processing release audio patch");
747 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
748 if (af == 0) {
749 command->mStatus = PERMISSION_DENIED;
750 } else {
751 command->mStatus = af->releaseAudioPatch(data->mHandle);
752 }
753 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700754 case UPDATE_AUDIOPORT_LIST: {
755 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700756 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700757 if (svc == 0) {
758 break;
759 }
760 mLock.unlock();
761 svc->doOnAudioPortListUpdate();
762 mLock.lock();
763 }break;
764 case UPDATE_AUDIOPATCH_LIST: {
765 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700766 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700767 if (svc == 0) {
768 break;
769 }
770 mLock.unlock();
771 svc->doOnAudioPatchListUpdate();
772 mLock.lock();
773 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700774 case SET_AUDIOPORT_CONFIG: {
775 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
776 ALOGV("AudioCommandThread() processing set port config");
777 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
778 if (af == 0) {
779 command->mStatus = PERMISSION_DENIED;
780 } else {
781 command->mStatus = af->setAudioPortConfig(&data->mConfig);
782 }
783 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700784 case DYN_POLICY_MIX_STATE_UPDATE: {
785 DynPolicyMixStateUpdateData *data =
786 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700787 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
788 data->mRegId.string(), data->mState);
789 svc = mService.promote();
790 if (svc == 0) {
791 break;
792 }
793 mLock.unlock();
794 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
795 mLock.lock();
796 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800797 case RECORDING_CONFIGURATION_UPDATE: {
798 RecordingConfigurationUpdateData *data =
799 (RecordingConfigurationUpdateData *)command->mParam.get();
800 ALOGV("AudioCommandThread() processing recording configuration update");
801 svc = mService.promote();
802 if (svc == 0) {
803 break;
804 }
805 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800806 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
807 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800808 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800809 mLock.lock();
810 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000812 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700814 {
815 Mutex::Autolock _l(command->mLock);
816 if (command->mWaitStatus) {
817 command->mWaitStatus = false;
818 command->mCond.signal();
819 }
820 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800821 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000822 // release mLock before releasing strong reference on the service as
823 // AudioPolicyService destructor calls AudioCommandThread::exit() which
824 // acquires mLock.
825 mLock.unlock();
826 svc.clear();
827 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 } else {
829 waitTime = mAudioCommands[0]->mTime - curTime;
830 break;
831 }
832 }
Zach Janga754b4f2015-10-27 01:29:34 +0000833
834 // release delayed commands wake lock if the queue is empty
835 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700836 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000837 }
838
839 // At this stage we have either an empty command queue or the first command in the queue
840 // has a finite delay. So unless we are exiting it is safe to wait.
841 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700842 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800843 if (waitTime == -1) {
844 mWaitWorkCV.wait(mLock);
845 } else {
846 mWaitWorkCV.waitRelative(mLock, waitTime);
847 }
Eric Laurent59a89232014-06-08 14:14:17 -0700848 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700850 // release delayed commands wake lock before quitting
851 if (!mAudioCommands.isEmpty()) {
852 release_wake_lock(mName.string());
853 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 mLock.unlock();
855 return false;
856}
857
858status_t AudioPolicyService::AudioCommandThread::dump(int fd)
859{
860 const size_t SIZE = 256;
861 char buffer[SIZE];
862 String8 result;
863
864 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
865 result.append(buffer);
866 write(fd, result.string(), result.size());
867
868 bool locked = tryLock(mLock);
869 if (!locked) {
870 String8 result2(kCmdDeadlockedString);
871 write(fd, result2.string(), result2.size());
872 }
873
874 snprintf(buffer, SIZE, "- Commands:\n");
875 result = String8(buffer);
876 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800877 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 mAudioCommands[i]->dump(buffer, SIZE);
879 result.append(buffer);
880 }
881 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700882 if (mLastCommand != 0) {
883 mLastCommand->dump(buffer, SIZE);
884 result.append(buffer);
885 } else {
886 result.append(" none\n");
887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888
889 write(fd, result.string(), result.size());
890
891 if (locked) mLock.unlock();
892
893 return NO_ERROR;
894}
895
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800896void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
897 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898{
Eric Laurent0ede8922014-05-09 18:04:42 -0700899 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700901 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 data->mType = type;
903 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100904 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700906 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907}
908
909void AudioPolicyService::AudioCommandThread::stopToneCommand()
910{
Eric Laurent0ede8922014-05-09 18:04:42 -0700911 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700914 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915}
916
Glenn Kastenfff6d712012-01-12 16:38:12 -0800917status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700918 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800919 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700920 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921{
Eric Laurent0ede8922014-05-09 18:04:42 -0700922 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700924 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 data->mStream = stream;
926 data->mVolume = volume;
927 data->mIO = output;
928 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700929 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700931 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700932 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933}
934
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800935status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700936 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700937 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938{
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700941 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700943 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700945 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700947 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700948 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949}
950
951status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
952{
Eric Laurent0ede8922014-05-09 18:04:42 -0700953 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700955 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 data->mVolume = volume;
957 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700958 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100959 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700960 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961}
962
Eric Laurentbfb1b832013-01-07 09:53:42 -0800963void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
964 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800965 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800966{
Eric Laurent0ede8922014-05-09 18:04:42 -0700967 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800968 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700969 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800970 data->mIO = output;
971 data->mStream = stream;
972 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100973 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800974 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700975 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800976}
977
Eric Laurente83b55d2014-11-14 10:06:21 -0800978void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
979 audio_stream_type_t stream,
980 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800981{
Eric Laurent0ede8922014-05-09 18:04:42 -0700982 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800983 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700984 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800985 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800986 data->mStream = stream;
987 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100988 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800989 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700990 sendCommand(command);
991}
992
Eric Laurent951f4552014-05-20 10:48:17 -0700993status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
994 const struct audio_patch *patch,
995 audio_patch_handle_t *handle,
996 int delayMs)
997{
998 status_t status = NO_ERROR;
999
1000 sp<AudioCommand> command = new AudioCommand();
1001 command->mCommand = CREATE_AUDIO_PATCH;
1002 CreateAudioPatchData *data = new CreateAudioPatchData();
1003 data->mPatch = *patch;
1004 data->mHandle = *handle;
1005 command->mParam = data;
1006 command->mWaitStatus = true;
1007 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
1008 status = sendCommand(command, delayMs);
1009 if (status == NO_ERROR) {
1010 *handle = data->mHandle;
1011 }
1012 return status;
1013}
1014
1015status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
1016 int delayMs)
1017{
1018 sp<AudioCommand> command = new AudioCommand();
1019 command->mCommand = RELEASE_AUDIO_PATCH;
1020 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
1021 data->mHandle = handle;
1022 command->mParam = data;
1023 command->mWaitStatus = true;
1024 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
1025 return sendCommand(command, delayMs);
1026}
1027
Eric Laurentb52c1522014-05-20 11:27:36 -07001028void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
1029{
1030 sp<AudioCommand> command = new AudioCommand();
1031 command->mCommand = UPDATE_AUDIOPORT_LIST;
1032 ALOGV("AudioCommandThread() adding update audio port list");
1033 sendCommand(command);
1034}
1035
1036void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
1037{
1038 sp<AudioCommand>command = new AudioCommand();
1039 command->mCommand = UPDATE_AUDIOPATCH_LIST;
1040 ALOGV("AudioCommandThread() adding update audio patch list");
1041 sendCommand(command);
1042}
1043
Eric Laurente1715a42014-05-20 11:30:42 -07001044status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
1045 const struct audio_port_config *config, int delayMs)
1046{
1047 sp<AudioCommand> command = new AudioCommand();
1048 command->mCommand = SET_AUDIOPORT_CONFIG;
1049 SetAudioPortConfigData *data = new SetAudioPortConfigData();
1050 data->mConfig = *config;
1051 command->mParam = data;
1052 command->mWaitStatus = true;
1053 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
1054 return sendCommand(command, delayMs);
1055}
1056
Jean-Michel Trivide801052015-04-14 19:10:14 -07001057void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -07001058 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -07001059{
1060 sp<AudioCommand> command = new AudioCommand();
1061 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
1062 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
1063 data->mRegId = regId;
1064 data->mState = state;
1065 command->mParam = data;
1066 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
1067 regId.string(), state);
1068 sendCommand(command);
1069}
1070
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001071void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001072 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001073 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1074 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001075{
1076 sp<AudioCommand>command = new AudioCommand();
1077 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
1078 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
1079 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001080 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -08001081 data->mClientConfig = *clientConfig;
1082 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -08001083 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001084 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -08001085 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
1086 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001087 sendCommand(command);
1088}
1089
Eric Laurent0ede8922014-05-09 18:04:42 -07001090status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
1091{
1092 {
1093 Mutex::Autolock _l(mLock);
1094 insertCommand_l(command, delayMs);
1095 mWaitWorkCV.signal();
1096 }
1097 Mutex::Autolock _l(command->mLock);
1098 while (command->mWaitStatus) {
1099 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
1100 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
1101 command->mStatus = TIMED_OUT;
1102 command->mWaitStatus = false;
1103 }
1104 }
1105 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001106}
1107
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -07001109void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110{
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001111 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -07001112 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 command->mTime = systemTime() + milliseconds(delayMs);
1114
1115 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -08001116 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
1118 }
1119
1120 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -07001121 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001122 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1124 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001125
1126 // create audio patch or release audio patch commands are equivalent
1127 // with regard to filtering
1128 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
1129 (command->mCommand == RELEASE_AUDIO_PATCH)) {
1130 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
1131 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
1132 continue;
1133 }
1134 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135
1136 switch (command->mCommand) {
1137 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001138 ParametersData *data = (ParametersData *)command->mParam.get();
1139 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001141 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001142 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1144 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1145 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001146 String8 key;
1147 String8 value;
1148 param.getAt(j, key, value);
1149 for (size_t k = 0; k < param2.size(); k++) {
1150 String8 key2;
1151 String8 value2;
1152 param2.getAt(k, key2, value2);
1153 if (key2 == key) {
1154 param2.remove(key2);
1155 ALOGV("Filtering out parameter %s", key2.string());
1156 break;
1157 }
1158 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159 }
1160 // if all keys have been filtered out, remove the command.
1161 // otherwise, update the key value pairs
1162 if (param2.size() == 0) {
1163 removedCommands.add(command2);
1164 } else {
1165 data2->mKeyValuePairs = param2.toString();
1166 }
Eric Laurent21e54562013-09-23 12:08:05 -07001167 command->mTime = command2->mTime;
1168 // force delayMs to non 0 so that code below does not request to wait for
1169 // command status as the command is now delayed
1170 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 } break;
1172
1173 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -07001174 VolumeData *data = (VolumeData *)command->mParam.get();
1175 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 if (data->mIO != data2->mIO) break;
1177 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001178 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001179 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001181 command->mTime = command2->mTime;
1182 // force delayMs to non 0 so that code below does not request to wait for
1183 // command status as the command is now delayed
1184 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -07001186
Eric Laurentbaf35fe2016-07-27 15:36:53 -07001187 case SET_VOICE_VOLUME: {
1188 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
1189 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
1190 ALOGV("Filtering out voice volume command value %f replaced by %f",
1191 data2->mVolume, data->mVolume);
1192 removedCommands.add(command2);
1193 command->mTime = command2->mTime;
1194 // force delayMs to non 0 so that code below does not request to wait for
1195 // command status as the command is now delayed
1196 delayMs = 1;
1197 } break;
1198
Eric Laurente45b48a2014-09-04 16:40:57 -07001199 case CREATE_AUDIO_PATCH:
1200 case RELEASE_AUDIO_PATCH: {
1201 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001202 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001203 if (command->mCommand == CREATE_AUDIO_PATCH) {
1204 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001205 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001206 } else {
1207 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
1208 }
1209 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001210 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001211 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1212 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001213 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001214 } else {
1215 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001216 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001217 }
1218 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001219 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1220 same output. */
1221 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1222 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1223 bool isOutputDiff = false;
1224 if (patch.num_sources == patch2.num_sources) {
1225 for (unsigned count = 0; count < patch.num_sources; count++) {
1226 if (patch.sources[count].id != patch2.sources[count].id) {
1227 isOutputDiff = true;
1228 break;
1229 }
1230 }
1231 if (isOutputDiff)
1232 break;
1233 }
1234 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001235 ALOGV("Filtering out %s audio patch command for handle %d",
1236 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1237 removedCommands.add(command2);
1238 command->mTime = command2->mTime;
1239 // force delayMs to non 0 so that code below does not request to wait for
1240 // command status as the command is now delayed
1241 delayMs = 1;
1242 } break;
1243
Jean-Michel Trivide801052015-04-14 19:10:14 -07001244 case DYN_POLICY_MIX_STATE_UPDATE: {
1245
1246 } break;
1247
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001248 case RECORDING_CONFIGURATION_UPDATE: {
1249
1250 } break;
1251
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 case START_TONE:
1253 case STOP_TONE:
1254 default:
1255 break;
1256 }
1257 }
1258
1259 // remove filtered commands
1260 for (size_t j = 0; j < removedCommands.size(); j++) {
1261 // removed commands always have time stamps greater than current command
1262 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001263 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001264 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 mAudioCommands.removeAt(k);
1266 break;
1267 }
1268 }
1269 }
1270 removedCommands.clear();
1271
Eric Laurentaa79bef2015-01-15 14:33:51 -08001272 // Disable wait for status if delay is not 0.
1273 // Except for create audio patch command because the returned patch handle
1274 // is needed by audio policy manager
1275 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001276 command->mWaitStatus = false;
1277 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001278
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001280 ALOGV("inserting command: %d at index %zd, num commands %zu",
1281 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 mAudioCommands.insertAt(command, i + 1);
1283}
1284
1285void AudioPolicyService::AudioCommandThread::exit()
1286{
Steve Block3856b092011-10-20 11:56:00 +01001287 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 {
1289 AutoMutex _l(mLock);
1290 requestExit();
1291 mWaitWorkCV.signal();
1292 }
Zach Janga754b4f2015-10-27 01:29:34 +00001293 // Note that we can call it from the thread loop if all other references have been released
1294 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295 requestExitAndWait();
1296}
1297
1298void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1299{
1300 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1301 mCommand,
1302 (int)ns2s(mTime),
1303 (int)ns2ms(mTime)%1000,
1304 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001305 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306}
1307
Dima Zavinfce7a472011-04-19 22:30:36 -07001308/******* helpers for the service_ops callbacks defined below *********/
1309void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1310 const char *keyValuePairs,
1311 int delayMs)
1312{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001313 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001314 delayMs);
1315}
1316
1317int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1318 float volume,
1319 audio_io_handle_t output,
1320 int delayMs)
1321{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001322 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001323 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001324}
1325
1326int AudioPolicyService::startTone(audio_policy_tone_t tone,
1327 audio_stream_type_t stream)
1328{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001329 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001330 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001331 }
1332 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001333 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001334 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001335 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001336 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1337 AUDIO_STREAM_VOICE_CALL);
1338 return 0;
1339}
1340
1341int AudioPolicyService::stopTone()
1342{
1343 mTonePlaybackThread->stopToneCommand();
1344 return 0;
1345}
1346
1347int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1348{
1349 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1350}
1351
Dima Zavinfce7a472011-04-19 22:30:36 -07001352extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001353audio_module_handle_t aps_load_hw_module(void *service __unused,
1354 const char *name);
1355audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001356 audio_devices_t *pDevices,
1357 uint32_t *pSamplingRate,
1358 audio_format_t *pFormat,
1359 audio_channel_mask_t *pChannelMask,
1360 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001361 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001362
Eric Laurent2d388ec2014-03-07 13:25:54 -08001363audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001364 audio_module_handle_t module,
1365 audio_devices_t *pDevices,
1366 uint32_t *pSamplingRate,
1367 audio_format_t *pFormat,
1368 audio_channel_mask_t *pChannelMask,
1369 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001370 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001371 const audio_offload_info_t *offloadInfo);
1372audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001373 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001374 audio_io_handle_t output2);
1375int aps_close_output(void *service __unused, audio_io_handle_t output);
1376int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1377int aps_restore_output(void *service __unused, audio_io_handle_t output);
1378audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001379 audio_devices_t *pDevices,
1380 uint32_t *pSamplingRate,
1381 audio_format_t *pFormat,
1382 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001383 audio_in_acoustics_t acoustics __unused);
1384audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001385 audio_module_handle_t module,
1386 audio_devices_t *pDevices,
1387 uint32_t *pSamplingRate,
1388 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001389 audio_channel_mask_t *pChannelMask);
1390int aps_close_input(void *service __unused, audio_io_handle_t input);
1391int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001392int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001393 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001394 audio_io_handle_t dst_output);
1395char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1396 const char *keys);
1397void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1398 const char *kv_pairs, int delay_ms);
1399int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001400 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001401 int delay_ms);
1402int aps_start_tone(void *service, audio_policy_tone_t tone,
1403 audio_stream_type_t stream);
1404int aps_stop_tone(void *service);
1405int aps_set_voice_volume(void *service, float volume, int delay_ms);
1406};
Dima Zavinfce7a472011-04-19 22:30:36 -07001407
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001408} // namespace android