blob: 45f260afb1facfa838c81b6d8fef62e52786e717 [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,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800242 audio_source_t source, const audio_config_base_t *clientConfig,
243 const audio_config_base_t *deviceConfig)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800244{
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800245 mOutputCommandThread->recordingConfigurationUpdateCommand(event, session, source,
246 clientConfig, deviceConfig);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800247}
248
249void AudioPolicyService::doOnRecordingConfigurationUpdate(int event, audio_session_t session,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800250 audio_source_t source, const audio_config_base_t *clientConfig,
251 const audio_config_base_t *deviceConfig)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800252{
253 Mutex::Autolock _l(mNotificationClientsLock);
254 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800255 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, session, source,
256 clientConfig, deviceConfig);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800257 }
258}
259
260status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
261 audio_patch_handle_t *handle,
262 int delayMs)
263{
264 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
265}
266
267status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
268 int delayMs)
269{
270 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
271}
272
Eric Laurente1715a42014-05-20 11:30:42 -0700273status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
274 int delayMs)
275{
276 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
277}
278
Eric Laurentb52c1522014-05-20 11:27:36 -0700279AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
280 const sp<IAudioPolicyServiceClient>& client,
281 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700282 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
283 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700284{
285}
286
287AudioPolicyService::NotificationClient::~NotificationClient()
288{
289}
290
291void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
292{
293 sp<NotificationClient> keep(this);
294 sp<AudioPolicyService> service = mService.promote();
295 if (service != 0) {
296 service->removeNotificationClient(mUid);
297 }
298}
299
300void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
301{
Eric Laurente8726fe2015-06-26 09:39:24 -0700302 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700303 mAudioPolicyServiceClient->onAudioPortListUpdate();
304 }
305}
306
307void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
308{
Eric Laurente8726fe2015-06-26 09:39:24 -0700309 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700310 mAudioPolicyServiceClient->onAudioPatchListUpdate();
311 }
312}
Eric Laurent57dae992011-07-24 13:36:09 -0700313
Jean-Michel Trivide801052015-04-14 19:10:14 -0700314void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
315 String8 regId, int32_t state)
316{
317 if (mAudioPolicyServiceClient != 0) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800318 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
319 }
320}
321
322void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800323 int event, audio_session_t session, audio_source_t source,
324 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800325{
326 if (mAudioPolicyServiceClient != 0) {
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800327 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, session, source,
328 clientConfig, deviceConfig);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700329 }
330}
331
Eric Laurente8726fe2015-06-26 09:39:24 -0700332void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
333{
334 mAudioPortCallbacksEnabled = enabled;
335}
336
337
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700339 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700340 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341}
342
343static bool tryLock(Mutex& mutex)
344{
345 bool locked = false;
346 for (int i = 0; i < kDumpLockRetries; ++i) {
347 if (mutex.tryLock() == NO_ERROR) {
348 locked = true;
349 break;
350 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800351 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 }
353 return locked;
354}
355
356status_t AudioPolicyService::dumpInternals(int fd)
357{
358 const size_t SIZE = 256;
359 char buffer[SIZE];
360 String8 result;
361
Eric Laurentdce54a12014-03-10 12:19:46 -0700362#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700364#else
365 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
366#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 result.append(buffer);
368 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
369 result.append(buffer);
370 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
371 result.append(buffer);
372
373 write(fd, result.string(), result.size());
374 return NO_ERROR;
375}
376
Glenn Kasten0f11b512014-01-31 16:18:54 -0800377status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd);
381 } else {
382 bool locked = tryLock(mLock);
383 if (!locked) {
384 String8 result(kDeadlockedString);
385 write(fd, result.string(), result.size());
386 }
387
388 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800389 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 mAudioCommandThread->dump(fd);
391 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800392 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 mTonePlaybackThread->dump(fd);
394 }
395
Eric Laurentdce54a12014-03-10 12:19:46 -0700396#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700397 if (mpAudioPolicy) {
398 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700400#else
401 if (mAudioPolicyManager) {
402 mAudioPolicyManager->dump(fd);
403 }
404#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405
406 if (locked) mLock.unlock();
407 }
408 return NO_ERROR;
409}
410
411status_t AudioPolicyService::dumpPermissionDenial(int fd)
412{
413 const size_t SIZE = 256;
414 char buffer[SIZE];
415 String8 result;
416 snprintf(buffer, SIZE, "Permission Denial: "
417 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
418 IPCThreadState::self()->getCallingPid(),
419 IPCThreadState::self()->getCallingUid());
420 result.append(buffer);
421 write(fd, result.string(), result.size());
422 return NO_ERROR;
423}
424
425status_t AudioPolicyService::onTransact(
426 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
427{
428 return BnAudioPolicyService::onTransact(code, data, reply, flags);
429}
430
431
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432// ----------- AudioPolicyService::AudioCommandThread implementation ----------
433
Eric Laurentbfb1b832013-01-07 09:53:42 -0800434AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
435 const wp<AudioPolicyService>& service)
436 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437{
438 mpToneGenerator = NULL;
439}
440
441
442AudioPolicyService::AudioCommandThread::~AudioCommandThread()
443{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800444 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 release_wake_lock(mName.string());
446 }
447 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800448 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449}
450
451void AudioPolicyService::AudioCommandThread::onFirstRef()
452{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800453 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454}
455
456bool AudioPolicyService::AudioCommandThread::threadLoop()
457{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800458 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459
460 mLock.lock();
461 while (!exitPending())
462 {
Eric Laurent59a89232014-06-08 14:14:17 -0700463 sp<AudioPolicyService> svc;
464 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 nsecs_t curTime = systemTime();
466 // commands are sorted by increasing time stamp: execute them from index 0 and up
467 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700468 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700470 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471
472 switch (command->mCommand) {
473 case START_TONE: {
474 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700475 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800478 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
480 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 mLock.lock();
482 }break;
483 case STOP_TONE: {
484 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 if (mpToneGenerator != NULL) {
487 mpToneGenerator->stopTone();
488 delete mpToneGenerator;
489 mpToneGenerator = NULL;
490 }
491 mLock.lock();
492 }break;
493 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700494 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700496 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
497 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
498 data->mVolume,
499 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 }break;
501 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700502 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700503 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
504 data->mKeyValuePairs.string(), data->mIO);
505 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700506 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700508 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700510 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800513 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700514 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515 ALOGV("AudioCommandThread() processing stop output %d",
516 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700517 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800518 if (svc == 0) {
519 break;
520 }
521 mLock.unlock();
522 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
523 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800524 }break;
525 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700526 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800527 ALOGV("AudioCommandThread() processing release output %d",
528 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700529 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800530 if (svc == 0) {
531 break;
532 }
533 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800534 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800535 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800536 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700537 case CREATE_AUDIO_PATCH: {
538 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
539 ALOGV("AudioCommandThread() processing create audio patch");
540 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
541 if (af == 0) {
542 command->mStatus = PERMISSION_DENIED;
543 } else {
544 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
545 }
546 } break;
547 case RELEASE_AUDIO_PATCH: {
548 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
549 ALOGV("AudioCommandThread() processing release audio patch");
550 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
551 if (af == 0) {
552 command->mStatus = PERMISSION_DENIED;
553 } else {
554 command->mStatus = af->releaseAudioPatch(data->mHandle);
555 }
556 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700557 case UPDATE_AUDIOPORT_LIST: {
558 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700559 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700560 if (svc == 0) {
561 break;
562 }
563 mLock.unlock();
564 svc->doOnAudioPortListUpdate();
565 mLock.lock();
566 }break;
567 case UPDATE_AUDIOPATCH_LIST: {
568 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700569 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700570 if (svc == 0) {
571 break;
572 }
573 mLock.unlock();
574 svc->doOnAudioPatchListUpdate();
575 mLock.lock();
576 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700577 case SET_AUDIOPORT_CONFIG: {
578 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
579 ALOGV("AudioCommandThread() processing set port config");
580 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
581 if (af == 0) {
582 command->mStatus = PERMISSION_DENIED;
583 } else {
584 command->mStatus = af->setAudioPortConfig(&data->mConfig);
585 }
586 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700587 case DYN_POLICY_MIX_STATE_UPDATE: {
588 DynPolicyMixStateUpdateData *data =
589 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700590 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
591 data->mRegId.string(), data->mState);
592 svc = mService.promote();
593 if (svc == 0) {
594 break;
595 }
596 mLock.unlock();
597 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
598 mLock.lock();
599 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800600 case RECORDING_CONFIGURATION_UPDATE: {
601 RecordingConfigurationUpdateData *data =
602 (RecordingConfigurationUpdateData *)command->mParam.get();
603 ALOGV("AudioCommandThread() processing recording configuration update");
604 svc = mService.promote();
605 if (svc == 0) {
606 break;
607 }
608 mLock.unlock();
609 svc->doOnRecordingConfigurationUpdate(data->mEvent, data->mSession,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800610 data->mSource, &data->mClientConfig, &data->mDeviceConfig);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800611 mLock.lock();
612 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000614 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700616 {
617 Mutex::Autolock _l(command->mLock);
618 if (command->mWaitStatus) {
619 command->mWaitStatus = false;
620 command->mCond.signal();
621 }
622 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800623 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000624 // release mLock before releasing strong reference on the service as
625 // AudioPolicyService destructor calls AudioCommandThread::exit() which
626 // acquires mLock.
627 mLock.unlock();
628 svc.clear();
629 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 } else {
631 waitTime = mAudioCommands[0]->mTime - curTime;
632 break;
633 }
634 }
Zach Janga754b4f2015-10-27 01:29:34 +0000635
636 // release delayed commands wake lock if the queue is empty
637 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700638 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000639 }
640
641 // At this stage we have either an empty command queue or the first command in the queue
642 // has a finite delay. So unless we are exiting it is safe to wait.
643 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700644 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800645 if (waitTime == -1) {
646 mWaitWorkCV.wait(mLock);
647 } else {
648 mWaitWorkCV.waitRelative(mLock, waitTime);
649 }
Eric Laurent59a89232014-06-08 14:14:17 -0700650 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700652 // release delayed commands wake lock before quitting
653 if (!mAudioCommands.isEmpty()) {
654 release_wake_lock(mName.string());
655 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 mLock.unlock();
657 return false;
658}
659
660status_t AudioPolicyService::AudioCommandThread::dump(int fd)
661{
662 const size_t SIZE = 256;
663 char buffer[SIZE];
664 String8 result;
665
666 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
667 result.append(buffer);
668 write(fd, result.string(), result.size());
669
670 bool locked = tryLock(mLock);
671 if (!locked) {
672 String8 result2(kCmdDeadlockedString);
673 write(fd, result2.string(), result2.size());
674 }
675
676 snprintf(buffer, SIZE, "- Commands:\n");
677 result = String8(buffer);
678 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800679 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 mAudioCommands[i]->dump(buffer, SIZE);
681 result.append(buffer);
682 }
683 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700684 if (mLastCommand != 0) {
685 mLastCommand->dump(buffer, SIZE);
686 result.append(buffer);
687 } else {
688 result.append(" none\n");
689 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690
691 write(fd, result.string(), result.size());
692
693 if (locked) mLock.unlock();
694
695 return NO_ERROR;
696}
697
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800698void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
699 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700{
Eric Laurent0ede8922014-05-09 18:04:42 -0700701 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700703 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 data->mType = type;
705 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100706 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700708 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709}
710
711void AudioPolicyService::AudioCommandThread::stopToneCommand()
712{
Eric Laurent0ede8922014-05-09 18:04:42 -0700713 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700716 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717}
718
Glenn Kastenfff6d712012-01-12 16:38:12 -0800719status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700720 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800721 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700722 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723{
Eric Laurent0ede8922014-05-09 18:04:42 -0700724 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700726 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 data->mStream = stream;
728 data->mVolume = volume;
729 data->mIO = output;
730 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700731 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700733 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700734 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735}
736
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800737status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700738 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700739 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740{
Eric Laurent0ede8922014-05-09 18:04:42 -0700741 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700743 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700745 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700747 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700749 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700750 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751}
752
753status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
754{
Eric Laurent0ede8922014-05-09 18:04:42 -0700755 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700757 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 data->mVolume = volume;
759 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700760 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700762 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763}
764
Eric Laurentbfb1b832013-01-07 09:53:42 -0800765void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
766 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800767 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800768{
Eric Laurent0ede8922014-05-09 18:04:42 -0700769 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800770 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700771 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800772 data->mIO = output;
773 data->mStream = stream;
774 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100775 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800776 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700777 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800778}
779
Eric Laurente83b55d2014-11-14 10:06:21 -0800780void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
781 audio_stream_type_t stream,
782 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800783{
Eric Laurent0ede8922014-05-09 18:04:42 -0700784 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800785 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700786 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800787 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800788 data->mStream = stream;
789 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100790 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800791 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700792 sendCommand(command);
793}
794
Eric Laurent951f4552014-05-20 10:48:17 -0700795status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
796 const struct audio_patch *patch,
797 audio_patch_handle_t *handle,
798 int delayMs)
799{
800 status_t status = NO_ERROR;
801
802 sp<AudioCommand> command = new AudioCommand();
803 command->mCommand = CREATE_AUDIO_PATCH;
804 CreateAudioPatchData *data = new CreateAudioPatchData();
805 data->mPatch = *patch;
806 data->mHandle = *handle;
807 command->mParam = data;
808 command->mWaitStatus = true;
809 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
810 status = sendCommand(command, delayMs);
811 if (status == NO_ERROR) {
812 *handle = data->mHandle;
813 }
814 return status;
815}
816
817status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
818 int delayMs)
819{
820 sp<AudioCommand> command = new AudioCommand();
821 command->mCommand = RELEASE_AUDIO_PATCH;
822 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
823 data->mHandle = handle;
824 command->mParam = data;
825 command->mWaitStatus = true;
826 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
827 return sendCommand(command, delayMs);
828}
829
Eric Laurentb52c1522014-05-20 11:27:36 -0700830void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
831{
832 sp<AudioCommand> command = new AudioCommand();
833 command->mCommand = UPDATE_AUDIOPORT_LIST;
834 ALOGV("AudioCommandThread() adding update audio port list");
835 sendCommand(command);
836}
837
838void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
839{
840 sp<AudioCommand>command = new AudioCommand();
841 command->mCommand = UPDATE_AUDIOPATCH_LIST;
842 ALOGV("AudioCommandThread() adding update audio patch list");
843 sendCommand(command);
844}
845
Eric Laurente1715a42014-05-20 11:30:42 -0700846status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
847 const struct audio_port_config *config, int delayMs)
848{
849 sp<AudioCommand> command = new AudioCommand();
850 command->mCommand = SET_AUDIOPORT_CONFIG;
851 SetAudioPortConfigData *data = new SetAudioPortConfigData();
852 data->mConfig = *config;
853 command->mParam = data;
854 command->mWaitStatus = true;
855 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
856 return sendCommand(command, delayMs);
857}
858
Jean-Michel Trivide801052015-04-14 19:10:14 -0700859void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
860 String8 regId, int32_t state)
861{
862 sp<AudioCommand> command = new AudioCommand();
863 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
864 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
865 data->mRegId = regId;
866 data->mState = state;
867 command->mParam = data;
868 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
869 regId.string(), state);
870 sendCommand(command);
871}
872
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800873void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800874 int event, audio_session_t session, audio_source_t source,
875 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800876{
877 sp<AudioCommand>command = new AudioCommand();
878 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
879 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
880 data->mEvent = event;
881 data->mSession = session;
882 data->mSource = source;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800883 data->mClientConfig = *clientConfig;
884 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800885 command->mParam = data;
886 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d",
887 event, source);
888 sendCommand(command);
889}
890
Eric Laurent0ede8922014-05-09 18:04:42 -0700891status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
892{
893 {
894 Mutex::Autolock _l(mLock);
895 insertCommand_l(command, delayMs);
896 mWaitWorkCV.signal();
897 }
898 Mutex::Autolock _l(command->mLock);
899 while (command->mWaitStatus) {
900 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
901 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
902 command->mStatus = TIMED_OUT;
903 command->mWaitStatus = false;
904 }
905 }
906 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800907}
908
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700910void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800912 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700913 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 command->mTime = systemTime() + milliseconds(delayMs);
915
916 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800917 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
919 }
920
921 // check same pending commands with later time stamps and eliminate them
922 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700923 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
925 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700926
927 // create audio patch or release audio patch commands are equivalent
928 // with regard to filtering
929 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
930 (command->mCommand == RELEASE_AUDIO_PATCH)) {
931 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
932 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
933 continue;
934 }
935 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936
937 switch (command->mCommand) {
938 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700939 ParametersData *data = (ParametersData *)command->mParam.get();
940 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100942 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700943 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 AudioParameter param = AudioParameter(data->mKeyValuePairs);
945 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
946 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700947 String8 key;
948 String8 value;
949 param.getAt(j, key, value);
950 for (size_t k = 0; k < param2.size(); k++) {
951 String8 key2;
952 String8 value2;
953 param2.getAt(k, key2, value2);
954 if (key2 == key) {
955 param2.remove(key2);
956 ALOGV("Filtering out parameter %s", key2.string());
957 break;
958 }
959 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 }
961 // if all keys have been filtered out, remove the command.
962 // otherwise, update the key value pairs
963 if (param2.size() == 0) {
964 removedCommands.add(command2);
965 } else {
966 data2->mKeyValuePairs = param2.toString();
967 }
Eric Laurent21e54562013-09-23 12:08:05 -0700968 command->mTime = command2->mTime;
969 // force delayMs to non 0 so that code below does not request to wait for
970 // command status as the command is now delayed
971 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 } break;
973
974 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700975 VolumeData *data = (VolumeData *)command->mParam.get();
976 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 if (data->mIO != data2->mIO) break;
978 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700980 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700982 command->mTime = command2->mTime;
983 // force delayMs to non 0 so that code below does not request to wait for
984 // command status as the command is now delayed
985 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700987
988 case CREATE_AUDIO_PATCH:
989 case RELEASE_AUDIO_PATCH: {
990 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700991 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700992 if (command->mCommand == CREATE_AUDIO_PATCH) {
993 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700994 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700995 } else {
996 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
997 }
998 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700999 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -07001000 if (command2->mCommand == CREATE_AUDIO_PATCH) {
1001 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001002 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -07001003 } else {
1004 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -07001005 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -07001006 }
1007 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -07001008 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
1009 same output. */
1010 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
1011 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
1012 bool isOutputDiff = false;
1013 if (patch.num_sources == patch2.num_sources) {
1014 for (unsigned count = 0; count < patch.num_sources; count++) {
1015 if (patch.sources[count].id != patch2.sources[count].id) {
1016 isOutputDiff = true;
1017 break;
1018 }
1019 }
1020 if (isOutputDiff)
1021 break;
1022 }
1023 }
Eric Laurente45b48a2014-09-04 16:40:57 -07001024 ALOGV("Filtering out %s audio patch command for handle %d",
1025 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
1026 removedCommands.add(command2);
1027 command->mTime = command2->mTime;
1028 // force delayMs to non 0 so that code below does not request to wait for
1029 // command status as the command is now delayed
1030 delayMs = 1;
1031 } break;
1032
Jean-Michel Trivide801052015-04-14 19:10:14 -07001033 case DYN_POLICY_MIX_STATE_UPDATE: {
1034
1035 } break;
1036
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -08001037 case RECORDING_CONFIGURATION_UPDATE: {
1038
1039 } break;
1040
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 case START_TONE:
1042 case STOP_TONE:
1043 default:
1044 break;
1045 }
1046 }
1047
1048 // remove filtered commands
1049 for (size_t j = 0; j < removedCommands.size(); j++) {
1050 // removed commands always have time stamps greater than current command
1051 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001052 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001053 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 mAudioCommands.removeAt(k);
1055 break;
1056 }
1057 }
1058 }
1059 removedCommands.clear();
1060
Eric Laurentaa79bef2015-01-15 14:33:51 -08001061 // Disable wait for status if delay is not 0.
1062 // Except for create audio patch command because the returned patch handle
1063 // is needed by audio policy manager
1064 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001065 command->mWaitStatus = false;
1066 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001067
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001069 ALOGV("inserting command: %d at index %zd, num commands %zu",
1070 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 mAudioCommands.insertAt(command, i + 1);
1072}
1073
1074void AudioPolicyService::AudioCommandThread::exit()
1075{
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 {
1078 AutoMutex _l(mLock);
1079 requestExit();
1080 mWaitWorkCV.signal();
1081 }
Zach Janga754b4f2015-10-27 01:29:34 +00001082 // Note that we can call it from the thread loop if all other references have been released
1083 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 requestExitAndWait();
1085}
1086
1087void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1088{
1089 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1090 mCommand,
1091 (int)ns2s(mTime),
1092 (int)ns2ms(mTime)%1000,
1093 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001094 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095}
1096
Dima Zavinfce7a472011-04-19 22:30:36 -07001097/******* helpers for the service_ops callbacks defined below *********/
1098void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1099 const char *keyValuePairs,
1100 int delayMs)
1101{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001102 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001103 delayMs);
1104}
1105
1106int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1107 float volume,
1108 audio_io_handle_t output,
1109 int delayMs)
1110{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001111 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001112 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001113}
1114
1115int AudioPolicyService::startTone(audio_policy_tone_t tone,
1116 audio_stream_type_t stream)
1117{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001118 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001119 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001120 }
1121 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001122 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001123 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001124 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001125 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1126 AUDIO_STREAM_VOICE_CALL);
1127 return 0;
1128}
1129
1130int AudioPolicyService::stopTone()
1131{
1132 mTonePlaybackThread->stopToneCommand();
1133 return 0;
1134}
1135
1136int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1137{
1138 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1139}
1140
Dima Zavinfce7a472011-04-19 22:30:36 -07001141extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001142audio_module_handle_t aps_load_hw_module(void *service __unused,
1143 const char *name);
1144audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001145 audio_devices_t *pDevices,
1146 uint32_t *pSamplingRate,
1147 audio_format_t *pFormat,
1148 audio_channel_mask_t *pChannelMask,
1149 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001150 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001151
Eric Laurent2d388ec2014-03-07 13:25:54 -08001152audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001153 audio_module_handle_t module,
1154 audio_devices_t *pDevices,
1155 uint32_t *pSamplingRate,
1156 audio_format_t *pFormat,
1157 audio_channel_mask_t *pChannelMask,
1158 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001159 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001160 const audio_offload_info_t *offloadInfo);
1161audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001162 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001163 audio_io_handle_t output2);
1164int aps_close_output(void *service __unused, audio_io_handle_t output);
1165int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1166int aps_restore_output(void *service __unused, audio_io_handle_t output);
1167audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001168 audio_devices_t *pDevices,
1169 uint32_t *pSamplingRate,
1170 audio_format_t *pFormat,
1171 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001172 audio_in_acoustics_t acoustics __unused);
1173audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001174 audio_module_handle_t module,
1175 audio_devices_t *pDevices,
1176 uint32_t *pSamplingRate,
1177 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001178 audio_channel_mask_t *pChannelMask);
1179int aps_close_input(void *service __unused, audio_io_handle_t input);
1180int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1181int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001182 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001183 audio_io_handle_t dst_output);
1184char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1185 const char *keys);
1186void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1187 const char *kv_pairs, int delay_ms);
1188int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001189 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001190 int delay_ms);
1191int aps_start_tone(void *service, audio_policy_tone_t tone,
1192 audio_stream_type_t stream);
1193int aps_stop_tone(void *service);
1194int aps_set_voice_volume(void *service, float volume, int delay_ms);
1195};
Dima Zavinfce7a472011-04-19 22:30:36 -07001196
1197namespace {
1198 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001199 .open_output = aps_open_output,
1200 .open_duplicate_output = aps_open_dup_output,
1201 .close_output = aps_close_output,
1202 .suspend_output = aps_suspend_output,
1203 .restore_output = aps_restore_output,
1204 .open_input = aps_open_input,
1205 .close_input = aps_close_input,
1206 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001207 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001208 .set_parameters = aps_set_parameters,
1209 .get_parameters = aps_get_parameters,
1210 .start_tone = aps_start_tone,
1211 .stop_tone = aps_stop_tone,
1212 .set_voice_volume = aps_set_voice_volume,
1213 .move_effects = aps_move_effects,
1214 .load_hw_module = aps_load_hw_module,
1215 .open_output_on_module = aps_open_output_on_module,
1216 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001217 };
1218}; // namespace <unnamed>
1219
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220}; // namespace android