blob: cdde60580d90ae717ac9cddb2467da5b17c1cf7c [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>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080038#include <media/AudioParameter.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043#include <hardware/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -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
Dima Zavinfce7a472011-04-19 22:30:36 -070055namespace {
56 extern struct audio_policy_service_ops aps_ops;
57};
58
Mathias Agopian65ab4712010-07-14 17:59:35 -070059// ----------------------------------------------------------------------------
60
61AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070062 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070063 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Mathias Agopian65ab4712010-07-14 17:59:35 -070069 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070070 const struct hw_module_t *module;
71 int forced_val;
72 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
Eric Laurent8b1e80b2014-10-07 09:08:47 -070074 {
75 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 // start tone playback thread
78 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
79 // start audio commands thread
80 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
81 // start output activity command thread
82 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070083
84#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 ALOGI("AudioPolicyService CSTOR in legacy mode");
Eric Laurentdce54a12014-03-10 12:19:46 -070086
Eric Laurent8b1e80b2014-10-07 09:08:47 -070087 /* instantiate the audio policy manager */
88 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
89 if (rc) {
90 return;
91 }
92 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
93 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
94 if (rc) {
95 return;
96 }
Eric Laurent93575202011-01-18 18:39:02 -080097
Eric Laurent8b1e80b2014-10-07 09:08:47 -070098 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
99 &mpAudioPolicy);
100 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
101 if (rc) {
102 return;
103 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700104
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700105 rc = mpAudioPolicy->init_check(mpAudioPolicy);
106 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
107 if (rc) {
108 return;
109 }
110 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700111#else
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700112 ALOGI("AudioPolicyService CSTOR in new mode");
Eric Laurentdce54a12014-03-10 12:19:46 -0700113
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700114 mAudioPolicyClient = new AudioPolicyClient(this);
115 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700116#endif
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 }
bryant_liuba2b4392014-06-11 16:49:30 +0800118 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700119 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
120 {
121 Mutex::Autolock _l(mLock);
122 mAudioPolicyEffects = audioPolicyEffects;
123 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124}
125
126AudioPolicyService::~AudioPolicyService()
127{
128 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700130 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131
Eric Laurentdce54a12014-03-10 12:19:46 -0700132#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700139#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700140 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 delete mAudioPolicyClient;
142#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700143
144 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800145 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700146}
147
148// A notification client is always registered by AudioSystem when the client process
149// connects to AudioPolicyService.
150void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
151{
Eric Laurent12590252015-08-21 18:40:20 -0700152 if (client == 0) {
153 ALOGW("%s got NULL client", __FUNCTION__);
154 return;
155 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800156 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700157
158 uid_t uid = IPCThreadState::self()->getCallingUid();
159 if (mNotificationClients.indexOfKey(uid) < 0) {
160 sp<NotificationClient> notificationClient = new NotificationClient(this,
161 client,
162 uid);
163 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
164
165 mNotificationClients.add(uid, notificationClient);
166
Marco Nelissenf8880202014-11-14 07:58:25 -0800167 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700168 binder->linkToDeath(notificationClient);
169 }
170}
171
Eric Laurente8726fe2015-06-26 09:39:24 -0700172void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
173{
174 Mutex::Autolock _l(mNotificationClientsLock);
175
176 uid_t uid = IPCThreadState::self()->getCallingUid();
177 if (mNotificationClients.indexOfKey(uid) < 0) {
178 return;
179 }
180 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
181}
182
Eric Laurentb52c1522014-05-20 11:27:36 -0700183// removeNotificationClient() is called when the client process dies.
184void AudioPolicyService::removeNotificationClient(uid_t uid)
185{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800186 {
187 Mutex::Autolock _l(mNotificationClientsLock);
188 mNotificationClients.removeItem(uid);
189 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700190#ifndef USE_LEGACY_AUDIO_POLICY
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800191 {
192 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700193 if (mAudioPolicyManager) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700194 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700195 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800196 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700197#endif
198}
199
200void AudioPolicyService::onAudioPortListUpdate()
201{
202 mOutputCommandThread->updateAudioPortListCommand();
203}
204
205void AudioPolicyService::doOnAudioPortListUpdate()
206{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800207 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700208 for (size_t i = 0; i < mNotificationClients.size(); i++) {
209 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
210 }
211}
212
213void AudioPolicyService::onAudioPatchListUpdate()
214{
215 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216}
217
Eric Laurent951f4552014-05-20 10:48:17 -0700218status_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 Laurentb52c1522014-05-20 11:27:36 -0700231void AudioPolicyService::doOnAudioPatchListUpdate()
232{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800233 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700234 for (size_t i = 0; i < mNotificationClients.size(); i++) {
235 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
236 }
237}
238
Jean-Michel Trivide801052015-04-14 19:10:14 -0700239void AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
240{
241 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
242 regId.string(), state);
243 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
244}
245
246void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
247{
248 Mutex::Autolock _l(mNotificationClientsLock);
249 for (size_t i = 0; i < mNotificationClients.size(); i++) {
250 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
251 }
252}
253
Eric Laurente1715a42014-05-20 11:30:42 -0700254status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
255 int delayMs)
256{
257 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
258}
259
Eric Laurentb52c1522014-05-20 11:27:36 -0700260AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
261 const sp<IAudioPolicyServiceClient>& client,
262 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700263 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
264 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700265{
266}
267
268AudioPolicyService::NotificationClient::~NotificationClient()
269{
270}
271
272void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
273{
274 sp<NotificationClient> keep(this);
275 sp<AudioPolicyService> service = mService.promote();
276 if (service != 0) {
277 service->removeNotificationClient(mUid);
278 }
279}
280
281void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
282{
Eric Laurente8726fe2015-06-26 09:39:24 -0700283 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700284 mAudioPolicyServiceClient->onAudioPortListUpdate();
285 }
286}
287
288void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
289{
Eric Laurente8726fe2015-06-26 09:39:24 -0700290 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700291 mAudioPolicyServiceClient->onAudioPatchListUpdate();
292 }
293}
Eric Laurent57dae992011-07-24 13:36:09 -0700294
Jean-Michel Trivide801052015-04-14 19:10:14 -0700295void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
296 String8 regId, int32_t state)
297{
298 if (mAudioPolicyServiceClient != 0) {
299 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
300 }
301}
302
Eric Laurente8726fe2015-06-26 09:39:24 -0700303void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
304{
305 mAudioPortCallbacksEnabled = enabled;
306}
307
308
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700310 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700311 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312}
313
314static bool tryLock(Mutex& mutex)
315{
316 bool locked = false;
317 for (int i = 0; i < kDumpLockRetries; ++i) {
318 if (mutex.tryLock() == NO_ERROR) {
319 locked = true;
320 break;
321 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800322 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
324 return locked;
325}
326
327status_t AudioPolicyService::dumpInternals(int fd)
328{
329 const size_t SIZE = 256;
330 char buffer[SIZE];
331 String8 result;
332
Eric Laurentdce54a12014-03-10 12:19:46 -0700333#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700335#else
336 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
337#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 result.append(buffer);
339 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
340 result.append(buffer);
341 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
342 result.append(buffer);
343
344 write(fd, result.string(), result.size());
345 return NO_ERROR;
346}
347
Glenn Kasten0f11b512014-01-31 16:18:54 -0800348status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349{
Glenn Kasten44deb052012-02-05 18:09:08 -0800350 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 dumpPermissionDenial(fd);
352 } else {
353 bool locked = tryLock(mLock);
354 if (!locked) {
355 String8 result(kDeadlockedString);
356 write(fd, result.string(), result.size());
357 }
358
359 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800360 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 mAudioCommandThread->dump(fd);
362 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800363 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 mTonePlaybackThread->dump(fd);
365 }
366
Eric Laurentdce54a12014-03-10 12:19:46 -0700367#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 if (mpAudioPolicy) {
369 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700371#else
372 if (mAudioPolicyManager) {
373 mAudioPolicyManager->dump(fd);
374 }
375#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376
377 if (locked) mLock.unlock();
378 }
379 return NO_ERROR;
380}
381
382status_t AudioPolicyService::dumpPermissionDenial(int fd)
383{
384 const size_t SIZE = 256;
385 char buffer[SIZE];
386 String8 result;
387 snprintf(buffer, SIZE, "Permission Denial: "
388 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
389 IPCThreadState::self()->getCallingPid(),
390 IPCThreadState::self()->getCallingUid());
391 result.append(buffer);
392 write(fd, result.string(), result.size());
393 return NO_ERROR;
394}
395
396status_t AudioPolicyService::onTransact(
397 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
398{
399 return BnAudioPolicyService::onTransact(code, data, reply, flags);
400}
401
402
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403// ----------- AudioPolicyService::AudioCommandThread implementation ----------
404
Eric Laurentbfb1b832013-01-07 09:53:42 -0800405AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
406 const wp<AudioPolicyService>& service)
407 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
409 mpToneGenerator = NULL;
410}
411
412
413AudioPolicyService::AudioCommandThread::~AudioCommandThread()
414{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800415 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 release_wake_lock(mName.string());
417 }
418 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800419 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420}
421
422void AudioPolicyService::AudioCommandThread::onFirstRef()
423{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800424 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425}
426
427bool AudioPolicyService::AudioCommandThread::threadLoop()
428{
429 nsecs_t waitTime = INT64_MAX;
430
431 mLock.lock();
432 while (!exitPending())
433 {
Eric Laurent59a89232014-06-08 14:14:17 -0700434 sp<AudioPolicyService> svc;
435 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 nsecs_t curTime = systemTime();
437 // commands are sorted by increasing time stamp: execute them from index 0 and up
438 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700439 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700441 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442
443 switch (command->mCommand) {
444 case START_TONE: {
445 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700446 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800449 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
451 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 mLock.lock();
453 }break;
454 case STOP_TONE: {
455 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100456 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 if (mpToneGenerator != NULL) {
458 mpToneGenerator->stopTone();
459 delete mpToneGenerator;
460 mpToneGenerator = NULL;
461 }
462 mLock.lock();
463 }break;
464 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700465 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100466 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700467 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
468 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
469 data->mVolume,
470 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 }break;
472 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700473 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700474 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
475 data->mKeyValuePairs.string(), data->mIO);
476 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700477 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700479 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700481 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800484 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700485 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800486 ALOGV("AudioCommandThread() processing stop output %d",
487 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700488 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800489 if (svc == 0) {
490 break;
491 }
492 mLock.unlock();
493 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
494 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800495 }break;
496 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700497 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800498 ALOGV("AudioCommandThread() processing release output %d",
499 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700500 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800501 if (svc == 0) {
502 break;
503 }
504 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800505 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800506 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800507 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700508 case CREATE_AUDIO_PATCH: {
509 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
510 ALOGV("AudioCommandThread() processing create audio patch");
511 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
512 if (af == 0) {
513 command->mStatus = PERMISSION_DENIED;
514 } else {
515 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
516 }
517 } break;
518 case RELEASE_AUDIO_PATCH: {
519 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
520 ALOGV("AudioCommandThread() processing release audio patch");
521 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
522 if (af == 0) {
523 command->mStatus = PERMISSION_DENIED;
524 } else {
525 command->mStatus = af->releaseAudioPatch(data->mHandle);
526 }
527 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700528 case UPDATE_AUDIOPORT_LIST: {
529 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700530 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700531 if (svc == 0) {
532 break;
533 }
534 mLock.unlock();
535 svc->doOnAudioPortListUpdate();
536 mLock.lock();
537 }break;
538 case UPDATE_AUDIOPATCH_LIST: {
539 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700540 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700541 if (svc == 0) {
542 break;
543 }
544 mLock.unlock();
545 svc->doOnAudioPatchListUpdate();
546 mLock.lock();
547 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700548 case SET_AUDIOPORT_CONFIG: {
549 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
550 ALOGV("AudioCommandThread() processing set port config");
551 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
552 if (af == 0) {
553 command->mStatus = PERMISSION_DENIED;
554 } else {
555 command->mStatus = af->setAudioPortConfig(&data->mConfig);
556 }
557 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700558 case DYN_POLICY_MIX_STATE_UPDATE: {
559 DynPolicyMixStateUpdateData *data =
560 (DynPolicyMixStateUpdateData *)command->mParam.get();
561 //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
562 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
563 data->mRegId.string(), data->mState);
564 svc = mService.promote();
565 if (svc == 0) {
566 break;
567 }
568 mLock.unlock();
569 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
570 mLock.lock();
571 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000573 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700575 {
576 Mutex::Autolock _l(command->mLock);
577 if (command->mWaitStatus) {
578 command->mWaitStatus = false;
579 command->mCond.signal();
580 }
581 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 waitTime = INT64_MAX;
Zach Janga754b4f2015-10-27 01:29:34 +0000583 // release mLock before releasing strong reference on the service as
584 // AudioPolicyService destructor calls AudioCommandThread::exit() which
585 // acquires mLock.
586 mLock.unlock();
587 svc.clear();
588 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 } else {
590 waitTime = mAudioCommands[0]->mTime - curTime;
591 break;
592 }
593 }
Zach Janga754b4f2015-10-27 01:29:34 +0000594
595 // release delayed commands wake lock if the queue is empty
596 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700597 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000598 }
599
600 // At this stage we have either an empty command queue or the first command in the queue
601 // has a finite delay. So unless we are exiting it is safe to wait.
602 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700603 ALOGV("AudioCommandThread() going to sleep");
604 mWaitWorkCV.waitRelative(mLock, waitTime);
Eric Laurent59a89232014-06-08 14:14:17 -0700605 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700607 // release delayed commands wake lock before quitting
608 if (!mAudioCommands.isEmpty()) {
609 release_wake_lock(mName.string());
610 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611 mLock.unlock();
612 return false;
613}
614
615status_t AudioPolicyService::AudioCommandThread::dump(int fd)
616{
617 const size_t SIZE = 256;
618 char buffer[SIZE];
619 String8 result;
620
621 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
622 result.append(buffer);
623 write(fd, result.string(), result.size());
624
625 bool locked = tryLock(mLock);
626 if (!locked) {
627 String8 result2(kCmdDeadlockedString);
628 write(fd, result2.string(), result2.size());
629 }
630
631 snprintf(buffer, SIZE, "- Commands:\n");
632 result = String8(buffer);
633 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800634 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 mAudioCommands[i]->dump(buffer, SIZE);
636 result.append(buffer);
637 }
638 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700639 if (mLastCommand != 0) {
640 mLastCommand->dump(buffer, SIZE);
641 result.append(buffer);
642 } else {
643 result.append(" none\n");
644 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645
646 write(fd, result.string(), result.size());
647
648 if (locked) mLock.unlock();
649
650 return NO_ERROR;
651}
652
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800653void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
654 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655{
Eric Laurent0ede8922014-05-09 18:04:42 -0700656 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700658 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 data->mType = type;
660 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100661 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700663 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664}
665
666void AudioPolicyService::AudioCommandThread::stopToneCommand()
667{
Eric Laurent0ede8922014-05-09 18:04:42 -0700668 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100670 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700671 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672}
673
Glenn Kastenfff6d712012-01-12 16:38:12 -0800674status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700675 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800676 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700677 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678{
Eric Laurent0ede8922014-05-09 18:04:42 -0700679 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700681 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 data->mStream = stream;
683 data->mVolume = volume;
684 data->mIO = output;
685 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700686 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700688 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700689 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690}
691
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800692status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700693 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700694 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695{
Eric Laurent0ede8922014-05-09 18:04:42 -0700696 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700698 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700700 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700702 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700704 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700705 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706}
707
708status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
709{
Eric Laurent0ede8922014-05-09 18:04:42 -0700710 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700712 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 data->mVolume = volume;
714 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700715 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100716 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700717 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718}
719
Eric Laurentbfb1b832013-01-07 09:53:42 -0800720void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
721 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800722 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800723{
Eric Laurent0ede8922014-05-09 18:04:42 -0700724 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800725 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700726 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800727 data->mIO = output;
728 data->mStream = stream;
729 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100730 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800731 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700732 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800733}
734
Eric Laurente83b55d2014-11-14 10:06:21 -0800735void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
736 audio_stream_type_t stream,
737 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800738{
Eric Laurent0ede8922014-05-09 18:04:42 -0700739 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800740 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700741 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800742 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800743 data->mStream = stream;
744 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100745 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800746 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700747 sendCommand(command);
748}
749
Eric Laurent951f4552014-05-20 10:48:17 -0700750status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
751 const struct audio_patch *patch,
752 audio_patch_handle_t *handle,
753 int delayMs)
754{
755 status_t status = NO_ERROR;
756
757 sp<AudioCommand> command = new AudioCommand();
758 command->mCommand = CREATE_AUDIO_PATCH;
759 CreateAudioPatchData *data = new CreateAudioPatchData();
760 data->mPatch = *patch;
761 data->mHandle = *handle;
762 command->mParam = data;
763 command->mWaitStatus = true;
764 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
765 status = sendCommand(command, delayMs);
766 if (status == NO_ERROR) {
767 *handle = data->mHandle;
768 }
769 return status;
770}
771
772status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
773 int delayMs)
774{
775 sp<AudioCommand> command = new AudioCommand();
776 command->mCommand = RELEASE_AUDIO_PATCH;
777 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
778 data->mHandle = handle;
779 command->mParam = data;
780 command->mWaitStatus = true;
781 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
782 return sendCommand(command, delayMs);
783}
784
Eric Laurentb52c1522014-05-20 11:27:36 -0700785void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
786{
787 sp<AudioCommand> command = new AudioCommand();
788 command->mCommand = UPDATE_AUDIOPORT_LIST;
789 ALOGV("AudioCommandThread() adding update audio port list");
790 sendCommand(command);
791}
792
793void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
794{
795 sp<AudioCommand>command = new AudioCommand();
796 command->mCommand = UPDATE_AUDIOPATCH_LIST;
797 ALOGV("AudioCommandThread() adding update audio patch list");
798 sendCommand(command);
799}
800
Eric Laurente1715a42014-05-20 11:30:42 -0700801status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
802 const struct audio_port_config *config, int delayMs)
803{
804 sp<AudioCommand> command = new AudioCommand();
805 command->mCommand = SET_AUDIOPORT_CONFIG;
806 SetAudioPortConfigData *data = new SetAudioPortConfigData();
807 data->mConfig = *config;
808 command->mParam = data;
809 command->mWaitStatus = true;
810 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
811 return sendCommand(command, delayMs);
812}
813
Jean-Michel Trivide801052015-04-14 19:10:14 -0700814void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
815 String8 regId, int32_t state)
816{
817 sp<AudioCommand> command = new AudioCommand();
818 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
819 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
820 data->mRegId = regId;
821 data->mState = state;
822 command->mParam = data;
823 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
824 regId.string(), state);
825 sendCommand(command);
826}
827
Eric Laurent0ede8922014-05-09 18:04:42 -0700828status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
829{
830 {
831 Mutex::Autolock _l(mLock);
832 insertCommand_l(command, delayMs);
833 mWaitWorkCV.signal();
834 }
835 Mutex::Autolock _l(command->mLock);
836 while (command->mWaitStatus) {
837 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
838 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
839 command->mStatus = TIMED_OUT;
840 command->mWaitStatus = false;
841 }
842 }
843 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800844}
845
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700847void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800849 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700850 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 command->mTime = systemTime() + milliseconds(delayMs);
852
853 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800854 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
856 }
857
858 // check same pending commands with later time stamps and eliminate them
859 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700860 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
862 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700863
864 // create audio patch or release audio patch commands are equivalent
865 // with regard to filtering
866 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
867 (command->mCommand == RELEASE_AUDIO_PATCH)) {
868 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
869 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
870 continue;
871 }
872 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873
874 switch (command->mCommand) {
875 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700876 ParametersData *data = (ParametersData *)command->mParam.get();
877 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700880 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 AudioParameter param = AudioParameter(data->mKeyValuePairs);
882 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
883 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700884 String8 key;
885 String8 value;
886 param.getAt(j, key, value);
887 for (size_t k = 0; k < param2.size(); k++) {
888 String8 key2;
889 String8 value2;
890 param2.getAt(k, key2, value2);
891 if (key2 == key) {
892 param2.remove(key2);
893 ALOGV("Filtering out parameter %s", key2.string());
894 break;
895 }
896 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 }
898 // if all keys have been filtered out, remove the command.
899 // otherwise, update the key value pairs
900 if (param2.size() == 0) {
901 removedCommands.add(command2);
902 } else {
903 data2->mKeyValuePairs = param2.toString();
904 }
Eric Laurent21e54562013-09-23 12:08:05 -0700905 command->mTime = command2->mTime;
906 // force delayMs to non 0 so that code below does not request to wait for
907 // command status as the command is now delayed
908 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 } break;
910
911 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700912 VolumeData *data = (VolumeData *)command->mParam.get();
913 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 if (data->mIO != data2->mIO) break;
915 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700917 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700919 command->mTime = command2->mTime;
920 // force delayMs to non 0 so that code below does not request to wait for
921 // command status as the command is now delayed
922 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700924
925 case CREATE_AUDIO_PATCH:
926 case RELEASE_AUDIO_PATCH: {
927 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700928 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700929 if (command->mCommand == CREATE_AUDIO_PATCH) {
930 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700931 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700932 } else {
933 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
934 }
935 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700936 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700937 if (command2->mCommand == CREATE_AUDIO_PATCH) {
938 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700939 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700940 } else {
941 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -0700942 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -0700943 }
944 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700945 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
946 same output. */
947 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
948 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
949 bool isOutputDiff = false;
950 if (patch.num_sources == patch2.num_sources) {
951 for (unsigned count = 0; count < patch.num_sources; count++) {
952 if (patch.sources[count].id != patch2.sources[count].id) {
953 isOutputDiff = true;
954 break;
955 }
956 }
957 if (isOutputDiff)
958 break;
959 }
960 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700961 ALOGV("Filtering out %s audio patch command for handle %d",
962 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
963 removedCommands.add(command2);
964 command->mTime = command2->mTime;
965 // force delayMs to non 0 so that code below does not request to wait for
966 // command status as the command is now delayed
967 delayMs = 1;
968 } break;
969
Jean-Michel Trivide801052015-04-14 19:10:14 -0700970 case DYN_POLICY_MIX_STATE_UPDATE: {
971
972 } break;
973
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974 case START_TONE:
975 case STOP_TONE:
976 default:
977 break;
978 }
979 }
980
981 // remove filtered commands
982 for (size_t j = 0; j < removedCommands.size(); j++) {
983 // removed commands always have time stamps greater than current command
984 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700985 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 mAudioCommands.removeAt(k);
988 break;
989 }
990 }
991 }
992 removedCommands.clear();
993
Eric Laurentaa79bef2015-01-15 14:33:51 -0800994 // Disable wait for status if delay is not 0.
995 // Except for create audio patch command because the returned patch handle
996 // is needed by audio policy manager
997 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700998 command->mWaitStatus = false;
999 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001000
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001002 ALOGV("inserting command: %d at index %zd, num commands %zu",
1003 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004 mAudioCommands.insertAt(command, i + 1);
1005}
1006
1007void AudioPolicyService::AudioCommandThread::exit()
1008{
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010 {
1011 AutoMutex _l(mLock);
1012 requestExit();
1013 mWaitWorkCV.signal();
1014 }
Zach Janga754b4f2015-10-27 01:29:34 +00001015 // Note that we can call it from the thread loop if all other references have been released
1016 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 requestExitAndWait();
1018}
1019
1020void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1021{
1022 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1023 mCommand,
1024 (int)ns2s(mTime),
1025 (int)ns2ms(mTime)%1000,
1026 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001027 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028}
1029
Dima Zavinfce7a472011-04-19 22:30:36 -07001030/******* helpers for the service_ops callbacks defined below *********/
1031void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1032 const char *keyValuePairs,
1033 int delayMs)
1034{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001035 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001036 delayMs);
1037}
1038
1039int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1040 float volume,
1041 audio_io_handle_t output,
1042 int delayMs)
1043{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001044 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001045 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001046}
1047
1048int AudioPolicyService::startTone(audio_policy_tone_t tone,
1049 audio_stream_type_t stream)
1050{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001051 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001052 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001053 }
1054 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001055 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001056 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001057 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001058 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1059 AUDIO_STREAM_VOICE_CALL);
1060 return 0;
1061}
1062
1063int AudioPolicyService::stopTone()
1064{
1065 mTonePlaybackThread->stopToneCommand();
1066 return 0;
1067}
1068
1069int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1070{
1071 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1072}
1073
Dima Zavinfce7a472011-04-19 22:30:36 -07001074extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001075audio_module_handle_t aps_load_hw_module(void *service __unused,
1076 const char *name);
1077audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001078 audio_devices_t *pDevices,
1079 uint32_t *pSamplingRate,
1080 audio_format_t *pFormat,
1081 audio_channel_mask_t *pChannelMask,
1082 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001083 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001084
Eric Laurent2d388ec2014-03-07 13:25:54 -08001085audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001086 audio_module_handle_t module,
1087 audio_devices_t *pDevices,
1088 uint32_t *pSamplingRate,
1089 audio_format_t *pFormat,
1090 audio_channel_mask_t *pChannelMask,
1091 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001092 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001093 const audio_offload_info_t *offloadInfo);
1094audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001095 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001096 audio_io_handle_t output2);
1097int aps_close_output(void *service __unused, audio_io_handle_t output);
1098int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1099int aps_restore_output(void *service __unused, audio_io_handle_t output);
1100audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001101 audio_devices_t *pDevices,
1102 uint32_t *pSamplingRate,
1103 audio_format_t *pFormat,
1104 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001105 audio_in_acoustics_t acoustics __unused);
1106audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001107 audio_module_handle_t module,
1108 audio_devices_t *pDevices,
1109 uint32_t *pSamplingRate,
1110 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001111 audio_channel_mask_t *pChannelMask);
1112int aps_close_input(void *service __unused, audio_io_handle_t input);
1113int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1114int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001115 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001116 audio_io_handle_t dst_output);
1117char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1118 const char *keys);
1119void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1120 const char *kv_pairs, int delay_ms);
1121int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001122 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001123 int delay_ms);
1124int aps_start_tone(void *service, audio_policy_tone_t tone,
1125 audio_stream_type_t stream);
1126int aps_stop_tone(void *service);
1127int aps_set_voice_volume(void *service, float volume, int delay_ms);
1128};
Dima Zavinfce7a472011-04-19 22:30:36 -07001129
1130namespace {
1131 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001132 .open_output = aps_open_output,
1133 .open_duplicate_output = aps_open_dup_output,
1134 .close_output = aps_close_output,
1135 .suspend_output = aps_suspend_output,
1136 .restore_output = aps_restore_output,
1137 .open_input = aps_open_input,
1138 .close_input = aps_close_input,
1139 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001140 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001141 .set_parameters = aps_set_parameters,
1142 .get_parameters = aps_get_parameters,
1143 .start_tone = aps_start_tone,
1144 .stop_tone = aps_stop_tone,
1145 .set_voice_volume = aps_set_voice_volume,
1146 .move_effects = aps_move_effects,
1147 .load_hw_module = aps_load_hw_module,
1148 .open_output_on_module = aps_open_output_on_module,
1149 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001150 };
1151}; // namespace <unnamed>
1152
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153}; // namespace android