blob: 0c5d2754fb31f7def8e39ccd4bf258d34ad06475 [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{
452 nsecs_t waitTime = INT64_MAX;
453
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 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617 waitTime = INT64_MAX;
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");
639 mWaitWorkCV.waitRelative(mLock, waitTime);
Eric Laurent59a89232014-06-08 14:14:17 -0700640 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700642 // release delayed commands wake lock before quitting
643 if (!mAudioCommands.isEmpty()) {
644 release_wake_lock(mName.string());
645 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 mLock.unlock();
647 return false;
648}
649
650status_t AudioPolicyService::AudioCommandThread::dump(int fd)
651{
652 const size_t SIZE = 256;
653 char buffer[SIZE];
654 String8 result;
655
656 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
657 result.append(buffer);
658 write(fd, result.string(), result.size());
659
660 bool locked = tryLock(mLock);
661 if (!locked) {
662 String8 result2(kCmdDeadlockedString);
663 write(fd, result2.string(), result2.size());
664 }
665
666 snprintf(buffer, SIZE, "- Commands:\n");
667 result = String8(buffer);
668 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800669 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 mAudioCommands[i]->dump(buffer, SIZE);
671 result.append(buffer);
672 }
673 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700674 if (mLastCommand != 0) {
675 mLastCommand->dump(buffer, SIZE);
676 result.append(buffer);
677 } else {
678 result.append(" none\n");
679 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680
681 write(fd, result.string(), result.size());
682
683 if (locked) mLock.unlock();
684
685 return NO_ERROR;
686}
687
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800688void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
689 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690{
Eric Laurent0ede8922014-05-09 18:04:42 -0700691 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700693 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 data->mType = type;
695 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100696 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700698 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699}
700
701void AudioPolicyService::AudioCommandThread::stopToneCommand()
702{
Eric Laurent0ede8922014-05-09 18:04:42 -0700703 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700706 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707}
708
Glenn Kastenfff6d712012-01-12 16:38:12 -0800709status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700710 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800711 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700712 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713{
Eric Laurent0ede8922014-05-09 18:04:42 -0700714 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700716 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 data->mStream = stream;
718 data->mVolume = volume;
719 data->mIO = output;
720 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700721 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700723 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700724 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725}
726
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800727status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700728 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700729 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730{
Eric Laurent0ede8922014-05-09 18:04:42 -0700731 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700733 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700735 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700737 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100738 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700739 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700740 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741}
742
743status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
744{
Eric Laurent0ede8922014-05-09 18:04:42 -0700745 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700747 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 data->mVolume = volume;
749 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700750 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700752 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753}
754
Eric Laurentbfb1b832013-01-07 09:53:42 -0800755void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
756 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800757 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800758{
Eric Laurent0ede8922014-05-09 18:04:42 -0700759 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800760 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700761 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800762 data->mIO = output;
763 data->mStream = stream;
764 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100765 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800766 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700767 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800768}
769
Eric Laurente83b55d2014-11-14 10:06:21 -0800770void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
771 audio_stream_type_t stream,
772 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800773{
Eric Laurent0ede8922014-05-09 18:04:42 -0700774 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800775 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700776 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800777 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800778 data->mStream = stream;
779 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100780 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800781 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700782 sendCommand(command);
783}
784
Eric Laurent951f4552014-05-20 10:48:17 -0700785status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
786 const struct audio_patch *patch,
787 audio_patch_handle_t *handle,
788 int delayMs)
789{
790 status_t status = NO_ERROR;
791
792 sp<AudioCommand> command = new AudioCommand();
793 command->mCommand = CREATE_AUDIO_PATCH;
794 CreateAudioPatchData *data = new CreateAudioPatchData();
795 data->mPatch = *patch;
796 data->mHandle = *handle;
797 command->mParam = data;
798 command->mWaitStatus = true;
799 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
800 status = sendCommand(command, delayMs);
801 if (status == NO_ERROR) {
802 *handle = data->mHandle;
803 }
804 return status;
805}
806
807status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
808 int delayMs)
809{
810 sp<AudioCommand> command = new AudioCommand();
811 command->mCommand = RELEASE_AUDIO_PATCH;
812 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
813 data->mHandle = handle;
814 command->mParam = data;
815 command->mWaitStatus = true;
816 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
817 return sendCommand(command, delayMs);
818}
819
Eric Laurentb52c1522014-05-20 11:27:36 -0700820void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
821{
822 sp<AudioCommand> command = new AudioCommand();
823 command->mCommand = UPDATE_AUDIOPORT_LIST;
824 ALOGV("AudioCommandThread() adding update audio port list");
825 sendCommand(command);
826}
827
828void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
829{
830 sp<AudioCommand>command = new AudioCommand();
831 command->mCommand = UPDATE_AUDIOPATCH_LIST;
832 ALOGV("AudioCommandThread() adding update audio patch list");
833 sendCommand(command);
834}
835
Eric Laurente1715a42014-05-20 11:30:42 -0700836status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
837 const struct audio_port_config *config, int delayMs)
838{
839 sp<AudioCommand> command = new AudioCommand();
840 command->mCommand = SET_AUDIOPORT_CONFIG;
841 SetAudioPortConfigData *data = new SetAudioPortConfigData();
842 data->mConfig = *config;
843 command->mParam = data;
844 command->mWaitStatus = true;
845 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
846 return sendCommand(command, delayMs);
847}
848
Jean-Michel Trivide801052015-04-14 19:10:14 -0700849void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
850 String8 regId, int32_t state)
851{
852 sp<AudioCommand> command = new AudioCommand();
853 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
854 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
855 data->mRegId = regId;
856 data->mState = state;
857 command->mParam = data;
858 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
859 regId.string(), state);
860 sendCommand(command);
861}
862
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800863void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
864 int event, audio_session_t session, audio_source_t source)
865{
866 sp<AudioCommand>command = new AudioCommand();
867 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
868 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
869 data->mEvent = event;
870 data->mSession = session;
871 data->mSource = source;
872 command->mParam = data;
873 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d",
874 event, source);
875 sendCommand(command);
876}
877
Eric Laurent0ede8922014-05-09 18:04:42 -0700878status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
879{
880 {
881 Mutex::Autolock _l(mLock);
882 insertCommand_l(command, delayMs);
883 mWaitWorkCV.signal();
884 }
885 Mutex::Autolock _l(command->mLock);
886 while (command->mWaitStatus) {
887 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
888 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
889 command->mStatus = TIMED_OUT;
890 command->mWaitStatus = false;
891 }
892 }
893 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800894}
895
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700897void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800899 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700900 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 command->mTime = systemTime() + milliseconds(delayMs);
902
903 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800904 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
906 }
907
908 // check same pending commands with later time stamps and eliminate them
909 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700910 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
912 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700913
914 // create audio patch or release audio patch commands are equivalent
915 // with regard to filtering
916 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
917 (command->mCommand == RELEASE_AUDIO_PATCH)) {
918 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
919 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
920 continue;
921 }
922 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923
924 switch (command->mCommand) {
925 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700926 ParametersData *data = (ParametersData *)command->mParam.get();
927 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700930 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 AudioParameter param = AudioParameter(data->mKeyValuePairs);
932 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
933 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700934 String8 key;
935 String8 value;
936 param.getAt(j, key, value);
937 for (size_t k = 0; k < param2.size(); k++) {
938 String8 key2;
939 String8 value2;
940 param2.getAt(k, key2, value2);
941 if (key2 == key) {
942 param2.remove(key2);
943 ALOGV("Filtering out parameter %s", key2.string());
944 break;
945 }
946 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 }
948 // if all keys have been filtered out, remove the command.
949 // otherwise, update the key value pairs
950 if (param2.size() == 0) {
951 removedCommands.add(command2);
952 } else {
953 data2->mKeyValuePairs = param2.toString();
954 }
Eric Laurent21e54562013-09-23 12:08:05 -0700955 command->mTime = command2->mTime;
956 // force delayMs to non 0 so that code below does not request to wait for
957 // command status as the command is now delayed
958 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 } break;
960
961 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700962 VolumeData *data = (VolumeData *)command->mParam.get();
963 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 if (data->mIO != data2->mIO) break;
965 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700967 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700969 command->mTime = command2->mTime;
970 // force delayMs to non 0 so that code below does not request to wait for
971 // command status as the command is now delayed
972 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700974
975 case CREATE_AUDIO_PATCH:
976 case RELEASE_AUDIO_PATCH: {
977 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700978 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700979 if (command->mCommand == CREATE_AUDIO_PATCH) {
980 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700981 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700982 } else {
983 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
984 }
985 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700986 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700987 if (command2->mCommand == CREATE_AUDIO_PATCH) {
988 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700989 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700990 } else {
991 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -0700992 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -0700993 }
994 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700995 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
996 same output. */
997 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
998 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
999 bool isOutputDiff = false;
1000 if (patch.num_sources == patch2.num_sources) {
1001 for (unsigned count = 0; count < patch.num_sources; count++) {
1002 if (patch.sources[count].id != patch2.sources[count].id) {
1003 isOutputDiff = true;
1004 break;
1005 }
1006 }
1007 if (isOutputDiff)
1008 break;
1009 }
1010 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001011 ALOGV("Filtering out %s audio patch command for handle %d",
1012 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1013 removedCommands.add(command2);
1014 command->mTime = command2->mTime;
1015 // force delayMs to non 0 so that code below does not request to wait for
1016 // command status as the command is now delayed
1017 delayMs = 1;
1018 } break;
1019
Jean-Michel Trivide801052015-04-14 19:10:14 -07001020 case DYN_POLICY_MIX_STATE_UPDATE: {
1021
1022 } break;
1023
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001024 case RECORDING_CONFIGURATION_UPDATE: {
1025
1026 } break;
1027
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 case START_TONE:
1029 case STOP_TONE:
1030 default:
1031 break;
1032 }
1033 }
1034
1035 // remove filtered commands
1036 for (size_t j = 0; j < removedCommands.size(); j++) {
1037 // removed commands always have time stamps greater than current command
1038 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001039 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 mAudioCommands.removeAt(k);
1042 break;
1043 }
1044 }
1045 }
1046 removedCommands.clear();
1047
Eric Laurentaa79bef2015-01-15 14:33:51 -08001048 // Disable wait for status if delay is not 0.
1049 // Except for create audio patch command because the returned patch handle
1050 // is needed by audio policy manager
1051 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001052 command->mWaitStatus = false;
1053 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001054
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001056 ALOGV("inserting command: %d at index %zd, num commands %zu",
1057 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 mAudioCommands.insertAt(command, i + 1);
1059}
1060
1061void AudioPolicyService::AudioCommandThread::exit()
1062{
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 {
1065 AutoMutex _l(mLock);
1066 requestExit();
1067 mWaitWorkCV.signal();
1068 }
Zach Janga754b4f2015-10-27 01:29:34 +00001069 // Note that we can call it from the thread loop if all other references have been released
1070 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 requestExitAndWait();
1072}
1073
1074void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1075{
1076 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1077 mCommand,
1078 (int)ns2s(mTime),
1079 (int)ns2ms(mTime)%1000,
1080 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001081 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082}
1083
Dima Zavinfce7a472011-04-19 22:30:36 -07001084/******* helpers for the service_ops callbacks defined below *********/
1085void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1086 const char *keyValuePairs,
1087 int delayMs)
1088{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001089 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001090 delayMs);
1091}
1092
1093int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1094 float volume,
1095 audio_io_handle_t output,
1096 int delayMs)
1097{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001098 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001099 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001100}
1101
1102int AudioPolicyService::startTone(audio_policy_tone_t tone,
1103 audio_stream_type_t stream)
1104{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001105 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001106 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001107 }
1108 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001109 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001110 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001111 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001112 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1113 AUDIO_STREAM_VOICE_CALL);
1114 return 0;
1115}
1116
1117int AudioPolicyService::stopTone()
1118{
1119 mTonePlaybackThread->stopToneCommand();
1120 return 0;
1121}
1122
1123int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1124{
1125 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1126}
1127
Dima Zavinfce7a472011-04-19 22:30:36 -07001128extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001129audio_module_handle_t aps_load_hw_module(void *service __unused,
1130 const char *name);
1131audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001132 audio_devices_t *pDevices,
1133 uint32_t *pSamplingRate,
1134 audio_format_t *pFormat,
1135 audio_channel_mask_t *pChannelMask,
1136 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001137 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001138
Eric Laurent2d388ec2014-03-07 13:25:54 -08001139audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001140 audio_module_handle_t module,
1141 audio_devices_t *pDevices,
1142 uint32_t *pSamplingRate,
1143 audio_format_t *pFormat,
1144 audio_channel_mask_t *pChannelMask,
1145 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001146 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001147 const audio_offload_info_t *offloadInfo);
1148audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001149 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001150 audio_io_handle_t output2);
1151int aps_close_output(void *service __unused, audio_io_handle_t output);
1152int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1153int aps_restore_output(void *service __unused, audio_io_handle_t output);
1154audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001155 audio_devices_t *pDevices,
1156 uint32_t *pSamplingRate,
1157 audio_format_t *pFormat,
1158 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001159 audio_in_acoustics_t acoustics __unused);
1160audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001161 audio_module_handle_t module,
1162 audio_devices_t *pDevices,
1163 uint32_t *pSamplingRate,
1164 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001165 audio_channel_mask_t *pChannelMask);
1166int aps_close_input(void *service __unused, audio_io_handle_t input);
1167int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1168int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001169 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001170 audio_io_handle_t dst_output);
1171char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1172 const char *keys);
1173void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1174 const char *kv_pairs, int delay_ms);
1175int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001176 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001177 int delay_ms);
1178int aps_start_tone(void *service, audio_policy_tone_t tone,
1179 audio_stream_type_t stream);
1180int aps_stop_tone(void *service);
1181int aps_set_voice_volume(void *service, float volume, int delay_ms);
1182};
Dima Zavinfce7a472011-04-19 22:30:36 -07001183
1184namespace {
1185 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001186 .open_output = aps_open_output,
1187 .open_duplicate_output = aps_open_dup_output,
1188 .close_output = aps_close_output,
1189 .suspend_output = aps_suspend_output,
1190 .restore_output = aps_restore_output,
1191 .open_input = aps_open_input,
1192 .close_input = aps_close_input,
1193 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001194 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001195 .set_parameters = aps_set_parameters,
1196 .get_parameters = aps_get_parameters,
1197 .start_tone = aps_start_tone,
1198 .stop_tone = aps_stop_tone,
1199 .set_voice_volume = aps_set_voice_volume,
1200 .move_effects = aps_move_effects,
1201 .load_hw_module = aps_load_hw_module,
1202 .open_output_on_module = aps_open_output_on_module,
1203 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001204 };
1205}; // namespace <unnamed>
1206
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207}; // namespace android