blob: 363968cc5c742607f19530ae2f013e381c547a41 [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 Laurentb52c1522014-05-20 11:27:36 -0700218void AudioPolicyService::doOnAudioPatchListUpdate()
219{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800220 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700221 for (size_t i = 0; i < mNotificationClients.size(); i++) {
222 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
223 }
224}
225
Jean-Michel Trivide801052015-04-14 19:10:14 -0700226void AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
227{
228 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
229 regId.string(), state);
230 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
231}
232
233void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
234{
235 Mutex::Autolock _l(mNotificationClientsLock);
236 for (size_t i = 0; i < mNotificationClients.size(); i++) {
237 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
238 }
239}
240
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800241void AudioPolicyService::onRecordingConfigurationUpdate(int event, audio_session_t session,
242 audio_source_t source)
243{
244 mOutputCommandThread->recordingConfigurationUpdateCommand(event, session, source);
245}
246
247void AudioPolicyService::doOnRecordingConfigurationUpdate(int event, audio_session_t session,
248 audio_source_t source)
249{
250 Mutex::Autolock _l(mNotificationClientsLock);
251 for (size_t i = 0; i < mNotificationClients.size(); i++) {
252 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, session, source);
253 }
254}
255
256status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
257 audio_patch_handle_t *handle,
258 int delayMs)
259{
260 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
261}
262
263status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
264 int delayMs)
265{
266 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
267}
268
Eric Laurente1715a42014-05-20 11:30:42 -0700269status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
270 int delayMs)
271{
272 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
273}
274
Eric Laurentb52c1522014-05-20 11:27:36 -0700275AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
276 const sp<IAudioPolicyServiceClient>& client,
277 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700278 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
279 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700280{
281}
282
283AudioPolicyService::NotificationClient::~NotificationClient()
284{
285}
286
287void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
288{
289 sp<NotificationClient> keep(this);
290 sp<AudioPolicyService> service = mService.promote();
291 if (service != 0) {
292 service->removeNotificationClient(mUid);
293 }
294}
295
296void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
297{
Eric Laurente8726fe2015-06-26 09:39:24 -0700298 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700299 mAudioPolicyServiceClient->onAudioPortListUpdate();
300 }
301}
302
303void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
304{
Eric Laurente8726fe2015-06-26 09:39:24 -0700305 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700306 mAudioPolicyServiceClient->onAudioPatchListUpdate();
307 }
308}
Eric Laurent57dae992011-07-24 13:36:09 -0700309
Jean-Michel Trivide801052015-04-14 19:10:14 -0700310void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
311 String8 regId, int32_t state)
312{
313 if (mAudioPolicyServiceClient != 0) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800314 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
315 }
316}
317
318void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
319 int event, audio_session_t session, audio_source_t source)
320{
321 if (mAudioPolicyServiceClient != 0) {
322 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, session, source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700323 }
324}
325
Eric Laurente8726fe2015-06-26 09:39:24 -0700326void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
327{
328 mAudioPortCallbacksEnabled = enabled;
329}
330
331
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700333 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700334 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335}
336
337static bool tryLock(Mutex& mutex)
338{
339 bool locked = false;
340 for (int i = 0; i < kDumpLockRetries; ++i) {
341 if (mutex.tryLock() == NO_ERROR) {
342 locked = true;
343 break;
344 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800345 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 }
347 return locked;
348}
349
350status_t AudioPolicyService::dumpInternals(int fd)
351{
352 const size_t SIZE = 256;
353 char buffer[SIZE];
354 String8 result;
355
Eric Laurentdce54a12014-03-10 12:19:46 -0700356#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700357 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700358#else
359 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
360#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 result.append(buffer);
362 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
363 result.append(buffer);
364 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
365 result.append(buffer);
366
367 write(fd, result.string(), result.size());
368 return NO_ERROR;
369}
370
Glenn Kasten0f11b512014-01-31 16:18:54 -0800371status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372{
Glenn Kasten44deb052012-02-05 18:09:08 -0800373 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 dumpPermissionDenial(fd);
375 } else {
376 bool locked = tryLock(mLock);
377 if (!locked) {
378 String8 result(kDeadlockedString);
379 write(fd, result.string(), result.size());
380 }
381
382 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800383 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 mAudioCommandThread->dump(fd);
385 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800386 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 mTonePlaybackThread->dump(fd);
388 }
389
Eric Laurentdce54a12014-03-10 12:19:46 -0700390#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700391 if (mpAudioPolicy) {
392 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700394#else
395 if (mAudioPolicyManager) {
396 mAudioPolicyManager->dump(fd);
397 }
398#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399
400 if (locked) mLock.unlock();
401 }
402 return NO_ERROR;
403}
404
405status_t AudioPolicyService::dumpPermissionDenial(int fd)
406{
407 const size_t SIZE = 256;
408 char buffer[SIZE];
409 String8 result;
410 snprintf(buffer, SIZE, "Permission Denial: "
411 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
412 IPCThreadState::self()->getCallingPid(),
413 IPCThreadState::self()->getCallingUid());
414 result.append(buffer);
415 write(fd, result.string(), result.size());
416 return NO_ERROR;
417}
418
419status_t AudioPolicyService::onTransact(
420 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
421{
422 return BnAudioPolicyService::onTransact(code, data, reply, flags);
423}
424
425
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426// ----------- AudioPolicyService::AudioCommandThread implementation ----------
427
Eric Laurentbfb1b832013-01-07 09:53:42 -0800428AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
429 const wp<AudioPolicyService>& service)
430 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431{
432 mpToneGenerator = NULL;
433}
434
435
436AudioPolicyService::AudioCommandThread::~AudioCommandThread()
437{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 release_wake_lock(mName.string());
440 }
441 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800442 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443}
444
445void AudioPolicyService::AudioCommandThread::onFirstRef()
446{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800447 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448}
449
450bool AudioPolicyService::AudioCommandThread::threadLoop()
451{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800452 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700453
454 mLock.lock();
455 while (!exitPending())
456 {
Eric Laurent59a89232014-06-08 14:14:17 -0700457 sp<AudioPolicyService> svc;
458 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 nsecs_t curTime = systemTime();
460 // commands are sorted by increasing time stamp: execute them from index 0 and up
461 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700462 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700464 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465
466 switch (command->mCommand) {
467 case START_TONE: {
468 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700469 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100470 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800472 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
474 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 mLock.lock();
476 }break;
477 case STOP_TONE: {
478 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 if (mpToneGenerator != NULL) {
481 mpToneGenerator->stopTone();
482 delete mpToneGenerator;
483 mpToneGenerator = NULL;
484 }
485 mLock.lock();
486 }break;
487 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700488 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100489 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700490 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
491 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
492 data->mVolume,
493 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494 }break;
495 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700496 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700497 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
498 data->mKeyValuePairs.string(), data->mIO);
499 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700500 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700502 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100503 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700504 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800507 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700508 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800509 ALOGV("AudioCommandThread() processing stop output %d",
510 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700511 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800512 if (svc == 0) {
513 break;
514 }
515 mLock.unlock();
516 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
517 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800518 }break;
519 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700520 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800521 ALOGV("AudioCommandThread() processing release output %d",
522 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700523 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800524 if (svc == 0) {
525 break;
526 }
527 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800528 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800529 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800530 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700531 case CREATE_AUDIO_PATCH: {
532 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
533 ALOGV("AudioCommandThread() processing create audio patch");
534 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
535 if (af == 0) {
536 command->mStatus = PERMISSION_DENIED;
537 } else {
538 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
539 }
540 } break;
541 case RELEASE_AUDIO_PATCH: {
542 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
543 ALOGV("AudioCommandThread() processing release audio patch");
544 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
545 if (af == 0) {
546 command->mStatus = PERMISSION_DENIED;
547 } else {
548 command->mStatus = af->releaseAudioPatch(data->mHandle);
549 }
550 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700551 case UPDATE_AUDIOPORT_LIST: {
552 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700553 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700554 if (svc == 0) {
555 break;
556 }
557 mLock.unlock();
558 svc->doOnAudioPortListUpdate();
559 mLock.lock();
560 }break;
561 case UPDATE_AUDIOPATCH_LIST: {
562 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700563 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700564 if (svc == 0) {
565 break;
566 }
567 mLock.unlock();
568 svc->doOnAudioPatchListUpdate();
569 mLock.lock();
570 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700571 case SET_AUDIOPORT_CONFIG: {
572 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
573 ALOGV("AudioCommandThread() processing set port config");
574 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
575 if (af == 0) {
576 command->mStatus = PERMISSION_DENIED;
577 } else {
578 command->mStatus = af->setAudioPortConfig(&data->mConfig);
579 }
580 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700581 case DYN_POLICY_MIX_STATE_UPDATE: {
582 DynPolicyMixStateUpdateData *data =
583 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700584 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
585 data->mRegId.string(), data->mState);
586 svc = mService.promote();
587 if (svc == 0) {
588 break;
589 }
590 mLock.unlock();
591 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
592 mLock.lock();
593 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800594 case RECORDING_CONFIGURATION_UPDATE: {
595 RecordingConfigurationUpdateData *data =
596 (RecordingConfigurationUpdateData *)command->mParam.get();
597 ALOGV("AudioCommandThread() processing recording configuration update");
598 svc = mService.promote();
599 if (svc == 0) {
600 break;
601 }
602 mLock.unlock();
603 svc->doOnRecordingConfigurationUpdate(data->mEvent, data->mSession,
604 data->mSource);
605 mLock.lock();
606 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000608 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700610 {
611 Mutex::Autolock _l(command->mLock);
612 if (command->mWaitStatus) {
613 command->mWaitStatus = false;
614 command->mCond.signal();
615 }
616 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800617 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000618 // release mLock before releasing strong reference on the service as
619 // AudioPolicyService destructor calls AudioCommandThread::exit() which
620 // acquires mLock.
621 mLock.unlock();
622 svc.clear();
623 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 } else {
625 waitTime = mAudioCommands[0]->mTime - curTime;
626 break;
627 }
628 }
Zach Janga754b4f2015-10-27 01:29:34 +0000629
630 // release delayed commands wake lock if the queue is empty
631 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700632 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000633 }
634
635 // At this stage we have either an empty command queue or the first command in the queue
636 // has a finite delay. So unless we are exiting it is safe to wait.
637 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700638 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800639 if (waitTime == -1) {
640 mWaitWorkCV.wait(mLock);
641 } else {
642 mWaitWorkCV.waitRelative(mLock, waitTime);
643 }
Eric Laurent59a89232014-06-08 14:14:17 -0700644 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700646 // release delayed commands wake lock before quitting
647 if (!mAudioCommands.isEmpty()) {
648 release_wake_lock(mName.string());
649 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 mLock.unlock();
651 return false;
652}
653
654status_t AudioPolicyService::AudioCommandThread::dump(int fd)
655{
656 const size_t SIZE = 256;
657 char buffer[SIZE];
658 String8 result;
659
660 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
661 result.append(buffer);
662 write(fd, result.string(), result.size());
663
664 bool locked = tryLock(mLock);
665 if (!locked) {
666 String8 result2(kCmdDeadlockedString);
667 write(fd, result2.string(), result2.size());
668 }
669
670 snprintf(buffer, SIZE, "- Commands:\n");
671 result = String8(buffer);
672 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800673 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 mAudioCommands[i]->dump(buffer, SIZE);
675 result.append(buffer);
676 }
677 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700678 if (mLastCommand != 0) {
679 mLastCommand->dump(buffer, SIZE);
680 result.append(buffer);
681 } else {
682 result.append(" none\n");
683 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684
685 write(fd, result.string(), result.size());
686
687 if (locked) mLock.unlock();
688
689 return NO_ERROR;
690}
691
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800692void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
693 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694{
Eric Laurent0ede8922014-05-09 18:04:42 -0700695 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700697 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 data->mType = type;
699 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100700 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700702 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703}
704
705void AudioPolicyService::AudioCommandThread::stopToneCommand()
706{
Eric Laurent0ede8922014-05-09 18:04:42 -0700707 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700710 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711}
712
Glenn Kastenfff6d712012-01-12 16:38:12 -0800713status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700714 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800715 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700716 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717{
Eric Laurent0ede8922014-05-09 18:04:42 -0700718 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700720 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 data->mStream = stream;
722 data->mVolume = volume;
723 data->mIO = output;
724 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700725 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100726 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700727 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700728 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729}
730
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800731status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700732 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700733 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734{
Eric Laurent0ede8922014-05-09 18:04:42 -0700735 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700737 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700739 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700741 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100742 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700743 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700744 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745}
746
747status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
748{
Eric Laurent0ede8922014-05-09 18:04:42 -0700749 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700751 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 data->mVolume = volume;
753 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700754 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700756 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757}
758
Eric Laurentbfb1b832013-01-07 09:53:42 -0800759void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
760 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800761 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800762{
Eric Laurent0ede8922014-05-09 18:04:42 -0700763 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800764 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700765 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800766 data->mIO = output;
767 data->mStream = stream;
768 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100769 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800770 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700771 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800772}
773
Eric Laurente83b55d2014-11-14 10:06:21 -0800774void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
775 audio_stream_type_t stream,
776 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800777{
Eric Laurent0ede8922014-05-09 18:04:42 -0700778 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800779 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700780 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800781 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800782 data->mStream = stream;
783 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100784 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800785 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700786 sendCommand(command);
787}
788
Eric Laurent951f4552014-05-20 10:48:17 -0700789status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
790 const struct audio_patch *patch,
791 audio_patch_handle_t *handle,
792 int delayMs)
793{
794 status_t status = NO_ERROR;
795
796 sp<AudioCommand> command = new AudioCommand();
797 command->mCommand = CREATE_AUDIO_PATCH;
798 CreateAudioPatchData *data = new CreateAudioPatchData();
799 data->mPatch = *patch;
800 data->mHandle = *handle;
801 command->mParam = data;
802 command->mWaitStatus = true;
803 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
804 status = sendCommand(command, delayMs);
805 if (status == NO_ERROR) {
806 *handle = data->mHandle;
807 }
808 return status;
809}
810
811status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
812 int delayMs)
813{
814 sp<AudioCommand> command = new AudioCommand();
815 command->mCommand = RELEASE_AUDIO_PATCH;
816 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
817 data->mHandle = handle;
818 command->mParam = data;
819 command->mWaitStatus = true;
820 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
821 return sendCommand(command, delayMs);
822}
823
Eric Laurentb52c1522014-05-20 11:27:36 -0700824void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
825{
826 sp<AudioCommand> command = new AudioCommand();
827 command->mCommand = UPDATE_AUDIOPORT_LIST;
828 ALOGV("AudioCommandThread() adding update audio port list");
829 sendCommand(command);
830}
831
832void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
833{
834 sp<AudioCommand>command = new AudioCommand();
835 command->mCommand = UPDATE_AUDIOPATCH_LIST;
836 ALOGV("AudioCommandThread() adding update audio patch list");
837 sendCommand(command);
838}
839
Eric Laurente1715a42014-05-20 11:30:42 -0700840status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
841 const struct audio_port_config *config, int delayMs)
842{
843 sp<AudioCommand> command = new AudioCommand();
844 command->mCommand = SET_AUDIOPORT_CONFIG;
845 SetAudioPortConfigData *data = new SetAudioPortConfigData();
846 data->mConfig = *config;
847 command->mParam = data;
848 command->mWaitStatus = true;
849 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
850 return sendCommand(command, delayMs);
851}
852
Jean-Michel Trivide801052015-04-14 19:10:14 -0700853void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
854 String8 regId, int32_t state)
855{
856 sp<AudioCommand> command = new AudioCommand();
857 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
858 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
859 data->mRegId = regId;
860 data->mState = state;
861 command->mParam = data;
862 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
863 regId.string(), state);
864 sendCommand(command);
865}
866
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800867void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
868 int event, audio_session_t session, audio_source_t source)
869{
870 sp<AudioCommand>command = new AudioCommand();
871 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
872 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
873 data->mEvent = event;
874 data->mSession = session;
875 data->mSource = source;
876 command->mParam = data;
877 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d",
878 event, source);
879 sendCommand(command);
880}
881
Eric Laurent0ede8922014-05-09 18:04:42 -0700882status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
883{
884 {
885 Mutex::Autolock _l(mLock);
886 insertCommand_l(command, delayMs);
887 mWaitWorkCV.signal();
888 }
889 Mutex::Autolock _l(command->mLock);
890 while (command->mWaitStatus) {
891 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
892 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
893 command->mStatus = TIMED_OUT;
894 command->mWaitStatus = false;
895 }
896 }
897 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800898}
899
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700901void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800903 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700904 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 command->mTime = systemTime() + milliseconds(delayMs);
906
907 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800908 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
910 }
911
912 // check same pending commands with later time stamps and eliminate them
913 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700914 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
916 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700917
918 // create audio patch or release audio patch commands are equivalent
919 // with regard to filtering
920 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
921 (command->mCommand == RELEASE_AUDIO_PATCH)) {
922 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
923 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
924 continue;
925 }
926 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927
928 switch (command->mCommand) {
929 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700930 ParametersData *data = (ParametersData *)command->mParam.get();
931 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100933 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700934 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 AudioParameter param = AudioParameter(data->mKeyValuePairs);
936 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
937 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700938 String8 key;
939 String8 value;
940 param.getAt(j, key, value);
941 for (size_t k = 0; k < param2.size(); k++) {
942 String8 key2;
943 String8 value2;
944 param2.getAt(k, key2, value2);
945 if (key2 == key) {
946 param2.remove(key2);
947 ALOGV("Filtering out parameter %s", key2.string());
948 break;
949 }
950 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 }
952 // if all keys have been filtered out, remove the command.
953 // otherwise, update the key value pairs
954 if (param2.size() == 0) {
955 removedCommands.add(command2);
956 } else {
957 data2->mKeyValuePairs = param2.toString();
958 }
Eric Laurent21e54562013-09-23 12:08:05 -0700959 command->mTime = command2->mTime;
960 // force delayMs to non 0 so that code below does not request to wait for
961 // command status as the command is now delayed
962 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 } break;
964
965 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700966 VolumeData *data = (VolumeData *)command->mParam.get();
967 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 if (data->mIO != data2->mIO) break;
969 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700971 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700973 command->mTime = command2->mTime;
974 // force delayMs to non 0 so that code below does not request to wait for
975 // command status as the command is now delayed
976 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700978
979 case CREATE_AUDIO_PATCH:
980 case RELEASE_AUDIO_PATCH: {
981 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700982 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700983 if (command->mCommand == CREATE_AUDIO_PATCH) {
984 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700985 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700986 } else {
987 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
988 }
989 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700990 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700991 if (command2->mCommand == CREATE_AUDIO_PATCH) {
992 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700993 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700994 } else {
995 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -0700996 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -0700997 }
998 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700999 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1000 same output. */
1001 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1002 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1003 bool isOutputDiff = false;
1004 if (patch.num_sources == patch2.num_sources) {
1005 for (unsigned count = 0; count < patch.num_sources; count++) {
1006 if (patch.sources[count].id != patch2.sources[count].id) {
1007 isOutputDiff = true;
1008 break;
1009 }
1010 }
1011 if (isOutputDiff)
1012 break;
1013 }
1014 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001015 ALOGV("Filtering out %s audio patch command for handle %d",
1016 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1017 removedCommands.add(command2);
1018 command->mTime = command2->mTime;
1019 // force delayMs to non 0 so that code below does not request to wait for
1020 // command status as the command is now delayed
1021 delayMs = 1;
1022 } break;
1023
Jean-Michel Trivide801052015-04-14 19:10:14 -07001024 case DYN_POLICY_MIX_STATE_UPDATE: {
1025
1026 } break;
1027
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001028 case RECORDING_CONFIGURATION_UPDATE: {
1029
1030 } break;
1031
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 case START_TONE:
1033 case STOP_TONE:
1034 default:
1035 break;
1036 }
1037 }
1038
1039 // remove filtered commands
1040 for (size_t j = 0; j < removedCommands.size(); j++) {
1041 // removed commands always have time stamps greater than current command
1042 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001043 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001044 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045 mAudioCommands.removeAt(k);
1046 break;
1047 }
1048 }
1049 }
1050 removedCommands.clear();
1051
Eric Laurentaa79bef2015-01-15 14:33:51 -08001052 // Disable wait for status if delay is not 0.
1053 // Except for create audio patch command because the returned patch handle
1054 // is needed by audio policy manager
1055 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001056 command->mWaitStatus = false;
1057 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001058
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001060 ALOGV("inserting command: %d at index %zd, num commands %zu",
1061 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 mAudioCommands.insertAt(command, i + 1);
1063}
1064
1065void AudioPolicyService::AudioCommandThread::exit()
1066{
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 {
1069 AutoMutex _l(mLock);
1070 requestExit();
1071 mWaitWorkCV.signal();
1072 }
Zach Janga754b4f2015-10-27 01:29:34 +00001073 // Note that we can call it from the thread loop if all other references have been released
1074 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 requestExitAndWait();
1076}
1077
1078void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1079{
1080 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1081 mCommand,
1082 (int)ns2s(mTime),
1083 (int)ns2ms(mTime)%1000,
1084 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001085 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086}
1087
Dima Zavinfce7a472011-04-19 22:30:36 -07001088/******* helpers for the service_ops callbacks defined below *********/
1089void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1090 const char *keyValuePairs,
1091 int delayMs)
1092{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001093 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001094 delayMs);
1095}
1096
1097int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1098 float volume,
1099 audio_io_handle_t output,
1100 int delayMs)
1101{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001102 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001103 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001104}
1105
1106int AudioPolicyService::startTone(audio_policy_tone_t tone,
1107 audio_stream_type_t stream)
1108{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001109 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001110 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001111 }
1112 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001113 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001114 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001115 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001116 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1117 AUDIO_STREAM_VOICE_CALL);
1118 return 0;
1119}
1120
1121int AudioPolicyService::stopTone()
1122{
1123 mTonePlaybackThread->stopToneCommand();
1124 return 0;
1125}
1126
1127int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1128{
1129 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1130}
1131
Dima Zavinfce7a472011-04-19 22:30:36 -07001132extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001133audio_module_handle_t aps_load_hw_module(void *service __unused,
1134 const char *name);
1135audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001136 audio_devices_t *pDevices,
1137 uint32_t *pSamplingRate,
1138 audio_format_t *pFormat,
1139 audio_channel_mask_t *pChannelMask,
1140 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001141 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001142
Eric Laurent2d388ec2014-03-07 13:25:54 -08001143audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001144 audio_module_handle_t module,
1145 audio_devices_t *pDevices,
1146 uint32_t *pSamplingRate,
1147 audio_format_t *pFormat,
1148 audio_channel_mask_t *pChannelMask,
1149 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001150 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001151 const audio_offload_info_t *offloadInfo);
1152audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001153 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001154 audio_io_handle_t output2);
1155int aps_close_output(void *service __unused, audio_io_handle_t output);
1156int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1157int aps_restore_output(void *service __unused, audio_io_handle_t output);
1158audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001159 audio_devices_t *pDevices,
1160 uint32_t *pSamplingRate,
1161 audio_format_t *pFormat,
1162 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001163 audio_in_acoustics_t acoustics __unused);
1164audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001165 audio_module_handle_t module,
1166 audio_devices_t *pDevices,
1167 uint32_t *pSamplingRate,
1168 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001169 audio_channel_mask_t *pChannelMask);
1170int aps_close_input(void *service __unused, audio_io_handle_t input);
1171int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1172int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001173 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001174 audio_io_handle_t dst_output);
1175char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1176 const char *keys);
1177void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1178 const char *kv_pairs, int delay_ms);
1179int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001180 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001181 int delay_ms);
1182int aps_start_tone(void *service, audio_policy_tone_t tone,
1183 audio_stream_type_t stream);
1184int aps_stop_tone(void *service);
1185int aps_set_voice_volume(void *service, float volume, int delay_ms);
1186};
Dima Zavinfce7a472011-04-19 22:30:36 -07001187
1188namespace {
1189 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001190 .open_output = aps_open_output,
1191 .open_duplicate_output = aps_open_dup_output,
1192 .close_output = aps_close_output,
1193 .suspend_output = aps_suspend_output,
1194 .restore_output = aps_restore_output,
1195 .open_input = aps_open_input,
1196 .close_input = aps_close_input,
1197 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001198 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001199 .set_parameters = aps_set_parameters,
1200 .get_parameters = aps_get_parameters,
1201 .start_tone = aps_start_tone,
1202 .stop_tone = aps_stop_tone,
1203 .set_voice_volume = aps_set_voice_volume,
1204 .move_effects = aps_move_effects,
1205 .load_hw_module = aps_load_hw_module,
1206 .open_output_on_module = aps_open_output_on_module,
1207 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001208 };
1209}; // namespace <unnamed>
1210
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211}; // namespace android