blob: fbe4f1869a428f2f2cb12aa1957f25344d4730db [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{
152
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800153 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700154
155 uid_t uid = IPCThreadState::self()->getCallingUid();
156 if (mNotificationClients.indexOfKey(uid) < 0) {
157 sp<NotificationClient> notificationClient = new NotificationClient(this,
158 client,
159 uid);
160 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
161
162 mNotificationClients.add(uid, notificationClient);
163
Marco Nelissenf8880202014-11-14 07:58:25 -0800164 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700165 binder->linkToDeath(notificationClient);
166 }
167}
168
Eric Laurente8726fe2015-06-26 09:39:24 -0700169void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
170{
171 Mutex::Autolock _l(mNotificationClientsLock);
172
173 uid_t uid = IPCThreadState::self()->getCallingUid();
174 if (mNotificationClients.indexOfKey(uid) < 0) {
175 return;
176 }
177 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
178}
179
Eric Laurentb52c1522014-05-20 11:27:36 -0700180// removeNotificationClient() is called when the client process dies.
181void AudioPolicyService::removeNotificationClient(uid_t uid)
182{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800183 {
184 Mutex::Autolock _l(mNotificationClientsLock);
185 mNotificationClients.removeItem(uid);
186 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700187#ifndef USE_LEGACY_AUDIO_POLICY
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800188 {
189 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700190 if (mAudioPolicyManager) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700191 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700192 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800193 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700194#endif
195}
196
197void AudioPolicyService::onAudioPortListUpdate()
198{
199 mOutputCommandThread->updateAudioPortListCommand();
200}
201
202void AudioPolicyService::doOnAudioPortListUpdate()
203{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800204 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700205 for (size_t i = 0; i < mNotificationClients.size(); i++) {
206 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
207 }
208}
209
210void AudioPolicyService::onAudioPatchListUpdate()
211{
212 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213}
214
Eric Laurent951f4552014-05-20 10:48:17 -0700215status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
216 audio_patch_handle_t *handle,
217 int delayMs)
218{
219 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
220}
221
222status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
223 int delayMs)
224{
225 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
226}
227
Eric Laurentb52c1522014-05-20 11:27:36 -0700228void AudioPolicyService::doOnAudioPatchListUpdate()
229{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800230 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700231 for (size_t i = 0; i < mNotificationClients.size(); i++) {
232 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
233 }
234}
235
Jean-Michel Trivide801052015-04-14 19:10:14 -0700236void AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
237{
238 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
239 regId.string(), state);
240 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
241}
242
243void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
244{
245 Mutex::Autolock _l(mNotificationClientsLock);
246 for (size_t i = 0; i < mNotificationClients.size(); i++) {
247 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
248 }
249}
250
Eric Laurente1715a42014-05-20 11:30:42 -0700251status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
252 int delayMs)
253{
254 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
255}
256
Eric Laurentb52c1522014-05-20 11:27:36 -0700257AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
258 const sp<IAudioPolicyServiceClient>& client,
259 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700260 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
261 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700262{
263}
264
265AudioPolicyService::NotificationClient::~NotificationClient()
266{
267}
268
269void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
270{
271 sp<NotificationClient> keep(this);
272 sp<AudioPolicyService> service = mService.promote();
273 if (service != 0) {
274 service->removeNotificationClient(mUid);
275 }
276}
277
278void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
279{
Eric Laurente8726fe2015-06-26 09:39:24 -0700280 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700281 mAudioPolicyServiceClient->onAudioPortListUpdate();
282 }
283}
284
285void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
286{
Eric Laurente8726fe2015-06-26 09:39:24 -0700287 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700288 mAudioPolicyServiceClient->onAudioPatchListUpdate();
289 }
290}
Eric Laurent57dae992011-07-24 13:36:09 -0700291
Jean-Michel Trivide801052015-04-14 19:10:14 -0700292void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
293 String8 regId, int32_t state)
294{
295 if (mAudioPolicyServiceClient != 0) {
296 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
297 }
298}
299
Eric Laurente8726fe2015-06-26 09:39:24 -0700300void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
301{
302 mAudioPortCallbacksEnabled = enabled;
303}
304
305
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700307 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700308 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309}
310
311static bool tryLock(Mutex& mutex)
312{
313 bool locked = false;
314 for (int i = 0; i < kDumpLockRetries; ++i) {
315 if (mutex.tryLock() == NO_ERROR) {
316 locked = true;
317 break;
318 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800319 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 }
321 return locked;
322}
323
324status_t AudioPolicyService::dumpInternals(int fd)
325{
326 const size_t SIZE = 256;
327 char buffer[SIZE];
328 String8 result;
329
Eric Laurentdce54a12014-03-10 12:19:46 -0700330#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700332#else
333 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
334#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 result.append(buffer);
336 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
337 result.append(buffer);
338 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
339 result.append(buffer);
340
341 write(fd, result.string(), result.size());
342 return NO_ERROR;
343}
344
Glenn Kasten0f11b512014-01-31 16:18:54 -0800345status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346{
Glenn Kasten44deb052012-02-05 18:09:08 -0800347 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 dumpPermissionDenial(fd);
349 } else {
350 bool locked = tryLock(mLock);
351 if (!locked) {
352 String8 result(kDeadlockedString);
353 write(fd, result.string(), result.size());
354 }
355
356 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800357 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358 mAudioCommandThread->dump(fd);
359 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800360 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 mTonePlaybackThread->dump(fd);
362 }
363
Eric Laurentdce54a12014-03-10 12:19:46 -0700364#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700365 if (mpAudioPolicy) {
366 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700368#else
369 if (mAudioPolicyManager) {
370 mAudioPolicyManager->dump(fd);
371 }
372#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373
374 if (locked) mLock.unlock();
375 }
376 return NO_ERROR;
377}
378
379status_t AudioPolicyService::dumpPermissionDenial(int fd)
380{
381 const size_t SIZE = 256;
382 char buffer[SIZE];
383 String8 result;
384 snprintf(buffer, SIZE, "Permission Denial: "
385 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
386 IPCThreadState::self()->getCallingPid(),
387 IPCThreadState::self()->getCallingUid());
388 result.append(buffer);
389 write(fd, result.string(), result.size());
390 return NO_ERROR;
391}
392
393status_t AudioPolicyService::onTransact(
394 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
395{
396 return BnAudioPolicyService::onTransact(code, data, reply, flags);
397}
398
399
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400// ----------- AudioPolicyService::AudioCommandThread implementation ----------
401
Eric Laurentbfb1b832013-01-07 09:53:42 -0800402AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
403 const wp<AudioPolicyService>& service)
404 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405{
406 mpToneGenerator = NULL;
407}
408
409
410AudioPolicyService::AudioCommandThread::~AudioCommandThread()
411{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800412 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 release_wake_lock(mName.string());
414 }
415 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800416 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417}
418
419void AudioPolicyService::AudioCommandThread::onFirstRef()
420{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800421 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422}
423
424bool AudioPolicyService::AudioCommandThread::threadLoop()
425{
426 nsecs_t waitTime = INT64_MAX;
427
428 mLock.lock();
429 while (!exitPending())
430 {
Eric Laurent59a89232014-06-08 14:14:17 -0700431 sp<AudioPolicyService> svc;
432 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 nsecs_t curTime = systemTime();
434 // commands are sorted by increasing time stamp: execute them from index 0 and up
435 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700436 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700438 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439
440 switch (command->mCommand) {
441 case START_TONE: {
442 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700443 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100444 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800446 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
448 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 mLock.lock();
450 }break;
451 case STOP_TONE: {
452 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100453 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454 if (mpToneGenerator != NULL) {
455 mpToneGenerator->stopTone();
456 delete mpToneGenerator;
457 mpToneGenerator = NULL;
458 }
459 mLock.lock();
460 }break;
461 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700462 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700464 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
465 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
466 data->mVolume,
467 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 }break;
469 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700470 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700471 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
472 data->mKeyValuePairs.string(), data->mIO);
473 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700474 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700476 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700478 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800481 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700482 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800483 ALOGV("AudioCommandThread() processing stop output %d",
484 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700485 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800486 if (svc == 0) {
487 break;
488 }
489 mLock.unlock();
490 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
491 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800492 }break;
493 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700494 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800495 ALOGV("AudioCommandThread() processing release output %d",
496 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700497 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800498 if (svc == 0) {
499 break;
500 }
501 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800502 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800503 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800504 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700505 case CREATE_AUDIO_PATCH: {
506 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
507 ALOGV("AudioCommandThread() processing create audio patch");
508 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
509 if (af == 0) {
510 command->mStatus = PERMISSION_DENIED;
511 } else {
512 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
513 }
514 } break;
515 case RELEASE_AUDIO_PATCH: {
516 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
517 ALOGV("AudioCommandThread() processing release audio patch");
518 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
519 if (af == 0) {
520 command->mStatus = PERMISSION_DENIED;
521 } else {
522 command->mStatus = af->releaseAudioPatch(data->mHandle);
523 }
524 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700525 case UPDATE_AUDIOPORT_LIST: {
526 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700527 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700528 if (svc == 0) {
529 break;
530 }
531 mLock.unlock();
532 svc->doOnAudioPortListUpdate();
533 mLock.lock();
534 }break;
535 case UPDATE_AUDIOPATCH_LIST: {
536 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700537 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700538 if (svc == 0) {
539 break;
540 }
541 mLock.unlock();
542 svc->doOnAudioPatchListUpdate();
543 mLock.lock();
544 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700545 case SET_AUDIOPORT_CONFIG: {
546 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
547 ALOGV("AudioCommandThread() processing set port config");
548 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
549 if (af == 0) {
550 command->mStatus = PERMISSION_DENIED;
551 } else {
552 command->mStatus = af->setAudioPortConfig(&data->mConfig);
553 }
554 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700555 case DYN_POLICY_MIX_STATE_UPDATE: {
556 DynPolicyMixStateUpdateData *data =
557 (DynPolicyMixStateUpdateData *)command->mParam.get();
558 //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
559 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
560 data->mRegId.string(), data->mState);
561 svc = mService.promote();
562 if (svc == 0) {
563 break;
564 }
565 mLock.unlock();
566 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
567 mLock.lock();
568 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000570 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700572 {
573 Mutex::Autolock _l(command->mLock);
574 if (command->mWaitStatus) {
575 command->mWaitStatus = false;
576 command->mCond.signal();
577 }
578 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 waitTime = INT64_MAX;
580 } else {
581 waitTime = mAudioCommands[0]->mTime - curTime;
582 break;
583 }
584 }
Eric Laurent59a89232014-06-08 14:14:17 -0700585 // release mLock before releasing strong reference on the service as
586 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
587 mLock.unlock();
588 svc.clear();
589 mLock.lock();
Wally Yau884de212015-03-24 09:10:58 -0700590 if (!exitPending() && (mAudioCommands.isEmpty() || waitTime != INT64_MAX)) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700591 // release delayed commands wake lock
592 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700593 ALOGV("AudioCommandThread() going to sleep");
594 mWaitWorkCV.waitRelative(mLock, waitTime);
595 ALOGV("AudioCommandThread() waking up");
596 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700598 // release delayed commands wake lock before quitting
599 if (!mAudioCommands.isEmpty()) {
600 release_wake_lock(mName.string());
601 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 mLock.unlock();
603 return false;
604}
605
606status_t AudioPolicyService::AudioCommandThread::dump(int fd)
607{
608 const size_t SIZE = 256;
609 char buffer[SIZE];
610 String8 result;
611
612 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
613 result.append(buffer);
614 write(fd, result.string(), result.size());
615
616 bool locked = tryLock(mLock);
617 if (!locked) {
618 String8 result2(kCmdDeadlockedString);
619 write(fd, result2.string(), result2.size());
620 }
621
622 snprintf(buffer, SIZE, "- Commands:\n");
623 result = String8(buffer);
624 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800625 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 mAudioCommands[i]->dump(buffer, SIZE);
627 result.append(buffer);
628 }
629 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700630 if (mLastCommand != 0) {
631 mLastCommand->dump(buffer, SIZE);
632 result.append(buffer);
633 } else {
634 result.append(" none\n");
635 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636
637 write(fd, result.string(), result.size());
638
639 if (locked) mLock.unlock();
640
641 return NO_ERROR;
642}
643
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800644void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
645 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646{
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700649 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 data->mType = type;
651 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100652 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100653 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700654 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655}
656
657void AudioPolicyService::AudioCommandThread::stopToneCommand()
658{
Eric Laurent0ede8922014-05-09 18:04:42 -0700659 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100661 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700662 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663}
664
Glenn Kastenfff6d712012-01-12 16:38:12 -0800665status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700666 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800667 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700668 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669{
Eric Laurent0ede8922014-05-09 18:04:42 -0700670 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700672 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 data->mStream = stream;
674 data->mVolume = volume;
675 data->mIO = output;
676 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700677 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100678 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700679 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700680 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681}
682
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800683status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700684 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700685 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686{
Eric Laurent0ede8922014-05-09 18:04:42 -0700687 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700689 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700691 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700693 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700695 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700696 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697}
698
699status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
700{
Eric Laurent0ede8922014-05-09 18:04:42 -0700701 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700703 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 data->mVolume = volume;
705 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700706 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700708 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709}
710
Eric Laurentbfb1b832013-01-07 09:53:42 -0800711void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
712 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800713 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800714{
Eric Laurent0ede8922014-05-09 18:04:42 -0700715 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800716 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700717 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800718 data->mIO = output;
719 data->mStream = stream;
720 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100721 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700723 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724}
725
Eric Laurente83b55d2014-11-14 10:06:21 -0800726void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
727 audio_stream_type_t stream,
728 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800729{
Eric Laurent0ede8922014-05-09 18:04:42 -0700730 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800731 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700732 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800733 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800734 data->mStream = stream;
735 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100736 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800737 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700738 sendCommand(command);
739}
740
Eric Laurent951f4552014-05-20 10:48:17 -0700741status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
742 const struct audio_patch *patch,
743 audio_patch_handle_t *handle,
744 int delayMs)
745{
746 status_t status = NO_ERROR;
747
748 sp<AudioCommand> command = new AudioCommand();
749 command->mCommand = CREATE_AUDIO_PATCH;
750 CreateAudioPatchData *data = new CreateAudioPatchData();
751 data->mPatch = *patch;
752 data->mHandle = *handle;
753 command->mParam = data;
754 command->mWaitStatus = true;
755 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
756 status = sendCommand(command, delayMs);
757 if (status == NO_ERROR) {
758 *handle = data->mHandle;
759 }
760 return status;
761}
762
763status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
764 int delayMs)
765{
766 sp<AudioCommand> command = new AudioCommand();
767 command->mCommand = RELEASE_AUDIO_PATCH;
768 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
769 data->mHandle = handle;
770 command->mParam = data;
771 command->mWaitStatus = true;
772 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
773 return sendCommand(command, delayMs);
774}
775
Eric Laurentb52c1522014-05-20 11:27:36 -0700776void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
777{
778 sp<AudioCommand> command = new AudioCommand();
779 command->mCommand = UPDATE_AUDIOPORT_LIST;
780 ALOGV("AudioCommandThread() adding update audio port list");
781 sendCommand(command);
782}
783
784void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
785{
786 sp<AudioCommand>command = new AudioCommand();
787 command->mCommand = UPDATE_AUDIOPATCH_LIST;
788 ALOGV("AudioCommandThread() adding update audio patch list");
789 sendCommand(command);
790}
791
Eric Laurente1715a42014-05-20 11:30:42 -0700792status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
793 const struct audio_port_config *config, int delayMs)
794{
795 sp<AudioCommand> command = new AudioCommand();
796 command->mCommand = SET_AUDIOPORT_CONFIG;
797 SetAudioPortConfigData *data = new SetAudioPortConfigData();
798 data->mConfig = *config;
799 command->mParam = data;
800 command->mWaitStatus = true;
801 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
802 return sendCommand(command, delayMs);
803}
804
Jean-Michel Trivide801052015-04-14 19:10:14 -0700805void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
806 String8 regId, int32_t state)
807{
808 sp<AudioCommand> command = new AudioCommand();
809 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
810 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
811 data->mRegId = regId;
812 data->mState = state;
813 command->mParam = data;
814 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
815 regId.string(), state);
816 sendCommand(command);
817}
818
Eric Laurent0ede8922014-05-09 18:04:42 -0700819status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
820{
821 {
822 Mutex::Autolock _l(mLock);
823 insertCommand_l(command, delayMs);
824 mWaitWorkCV.signal();
825 }
826 Mutex::Autolock _l(command->mLock);
827 while (command->mWaitStatus) {
828 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
829 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
830 command->mStatus = TIMED_OUT;
831 command->mWaitStatus = false;
832 }
833 }
834 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800835}
836
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700838void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800840 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700841 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 command->mTime = systemTime() + milliseconds(delayMs);
843
844 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800845 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
847 }
848
849 // check same pending commands with later time stamps and eliminate them
850 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700851 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
853 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700854
855 // create audio patch or release audio patch commands are equivalent
856 // with regard to filtering
857 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
858 (command->mCommand == RELEASE_AUDIO_PATCH)) {
859 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
860 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
861 continue;
862 }
863 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864
865 switch (command->mCommand) {
866 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700867 ParametersData *data = (ParametersData *)command->mParam.get();
868 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100870 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700871 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 AudioParameter param = AudioParameter(data->mKeyValuePairs);
873 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
874 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700875 String8 key;
876 String8 value;
877 param.getAt(j, key, value);
878 for (size_t k = 0; k < param2.size(); k++) {
879 String8 key2;
880 String8 value2;
881 param2.getAt(k, key2, value2);
882 if (key2 == key) {
883 param2.remove(key2);
884 ALOGV("Filtering out parameter %s", key2.string());
885 break;
886 }
887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 }
889 // if all keys have been filtered out, remove the command.
890 // otherwise, update the key value pairs
891 if (param2.size() == 0) {
892 removedCommands.add(command2);
893 } else {
894 data2->mKeyValuePairs = param2.toString();
895 }
Eric Laurent21e54562013-09-23 12:08:05 -0700896 command->mTime = command2->mTime;
897 // force delayMs to non 0 so that code below does not request to wait for
898 // command status as the command is now delayed
899 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 } break;
901
902 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700903 VolumeData *data = (VolumeData *)command->mParam.get();
904 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 if (data->mIO != data2->mIO) break;
906 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100907 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700908 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700910 command->mTime = command2->mTime;
911 // force delayMs to non 0 so that code below does not request to wait for
912 // command status as the command is now delayed
913 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700915
916 case CREATE_AUDIO_PATCH:
917 case RELEASE_AUDIO_PATCH: {
918 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700919 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700920 if (command->mCommand == CREATE_AUDIO_PATCH) {
921 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700922 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700923 } else {
924 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
925 }
926 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700927 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700928 if (command2->mCommand == CREATE_AUDIO_PATCH) {
929 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700930 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700931 } else {
932 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
933 }
934 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700935 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
936 same output. */
937 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
938 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
939 bool isOutputDiff = false;
940 if (patch.num_sources == patch2.num_sources) {
941 for (unsigned count = 0; count < patch.num_sources; count++) {
942 if (patch.sources[count].id != patch2.sources[count].id) {
943 isOutputDiff = true;
944 break;
945 }
946 }
947 if (isOutputDiff)
948 break;
949 }
950 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700951 ALOGV("Filtering out %s audio patch command for handle %d",
952 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
953 removedCommands.add(command2);
954 command->mTime = command2->mTime;
955 // force delayMs to non 0 so that code below does not request to wait for
956 // command status as the command is now delayed
957 delayMs = 1;
958 } break;
959
Jean-Michel Trivide801052015-04-14 19:10:14 -0700960 case DYN_POLICY_MIX_STATE_UPDATE: {
961
962 } break;
963
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 case START_TONE:
965 case STOP_TONE:
966 default:
967 break;
968 }
969 }
970
971 // remove filtered commands
972 for (size_t j = 0; j < removedCommands.size(); j++) {
973 // removed commands always have time stamps greater than current command
974 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700975 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 mAudioCommands.removeAt(k);
978 break;
979 }
980 }
981 }
982 removedCommands.clear();
983
Eric Laurentaa79bef2015-01-15 14:33:51 -0800984 // Disable wait for status if delay is not 0.
985 // Except for create audio patch command because the returned patch handle
986 // is needed by audio policy manager
987 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700988 command->mWaitStatus = false;
989 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700990
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700992 ALOGV("inserting command: %d at index %zd, num commands %zu",
993 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 mAudioCommands.insertAt(command, i + 1);
995}
996
997void AudioPolicyService::AudioCommandThread::exit()
998{
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 {
1001 AutoMutex _l(mLock);
1002 requestExit();
1003 mWaitWorkCV.signal();
1004 }
1005 requestExitAndWait();
1006}
1007
1008void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1009{
1010 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1011 mCommand,
1012 (int)ns2s(mTime),
1013 (int)ns2ms(mTime)%1000,
1014 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001015 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016}
1017
Dima Zavinfce7a472011-04-19 22:30:36 -07001018/******* helpers for the service_ops callbacks defined below *********/
1019void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1020 const char *keyValuePairs,
1021 int delayMs)
1022{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001023 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001024 delayMs);
1025}
1026
1027int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1028 float volume,
1029 audio_io_handle_t output,
1030 int delayMs)
1031{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001032 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001033 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001034}
1035
1036int AudioPolicyService::startTone(audio_policy_tone_t tone,
1037 audio_stream_type_t stream)
1038{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001039 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001040 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001041 }
1042 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001043 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001044 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001045 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001046 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1047 AUDIO_STREAM_VOICE_CALL);
1048 return 0;
1049}
1050
1051int AudioPolicyService::stopTone()
1052{
1053 mTonePlaybackThread->stopToneCommand();
1054 return 0;
1055}
1056
1057int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1058{
1059 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1060}
1061
Dima Zavinfce7a472011-04-19 22:30:36 -07001062extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001063audio_module_handle_t aps_load_hw_module(void *service __unused,
1064 const char *name);
1065audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001066 audio_devices_t *pDevices,
1067 uint32_t *pSamplingRate,
1068 audio_format_t *pFormat,
1069 audio_channel_mask_t *pChannelMask,
1070 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001071 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001072
Eric Laurent2d388ec2014-03-07 13:25:54 -08001073audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001074 audio_module_handle_t module,
1075 audio_devices_t *pDevices,
1076 uint32_t *pSamplingRate,
1077 audio_format_t *pFormat,
1078 audio_channel_mask_t *pChannelMask,
1079 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001080 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001081 const audio_offload_info_t *offloadInfo);
1082audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001083 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001084 audio_io_handle_t output2);
1085int aps_close_output(void *service __unused, audio_io_handle_t output);
1086int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1087int aps_restore_output(void *service __unused, audio_io_handle_t output);
1088audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001089 audio_devices_t *pDevices,
1090 uint32_t *pSamplingRate,
1091 audio_format_t *pFormat,
1092 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001093 audio_in_acoustics_t acoustics __unused);
1094audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001095 audio_module_handle_t module,
1096 audio_devices_t *pDevices,
1097 uint32_t *pSamplingRate,
1098 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001099 audio_channel_mask_t *pChannelMask);
1100int aps_close_input(void *service __unused, audio_io_handle_t input);
1101int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1102int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001103 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001104 audio_io_handle_t dst_output);
1105char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1106 const char *keys);
1107void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1108 const char *kv_pairs, int delay_ms);
1109int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001110 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001111 int delay_ms);
1112int aps_start_tone(void *service, audio_policy_tone_t tone,
1113 audio_stream_type_t stream);
1114int aps_stop_tone(void *service);
1115int aps_set_voice_volume(void *service, float volume, int delay_ms);
1116};
Dima Zavinfce7a472011-04-19 22:30:36 -07001117
1118namespace {
1119 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001120 .open_output = aps_open_output,
1121 .open_duplicate_output = aps_open_dup_output,
1122 .close_output = aps_close_output,
1123 .suspend_output = aps_suspend_output,
1124 .restore_output = aps_restore_output,
1125 .open_input = aps_open_input,
1126 .close_input = aps_close_input,
1127 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001128 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001129 .set_parameters = aps_set_parameters,
1130 .get_parameters = aps_get_parameters,
1131 .start_tone = aps_start_tone,
1132 .stop_tone = aps_stop_tone,
1133 .set_voice_volume = aps_set_voice_volume,
1134 .move_effects = aps_move_effects,
1135 .load_hw_module = aps_load_hw_module,
1136 .open_output_on_module = aps_open_output_on_module,
1137 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001138 };
1139}; // namespace <unnamed>
1140
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141}; // namespace android