blob: af0c823543c209afc60340f931d94106c4e1b8aa [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>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080037#include <media/AudioParameter.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavin64760242011-05-11 14:15:23 -070039#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070040#include <system/audio_policy.h>
Mikhail Naganov61a4fac2016-10-13 14:44:18 -070041
Mathias Agopian65ab4712010-07-14 17:59:35 -070042namespace android {
43
Glenn Kasten8dad0e32012-01-09 08:41:22 -080044static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
45static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080048static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Eric Laurent0ede8922014-05-09 18:04:42 -070050static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010051
Dima Zavinfce7a472011-04-19 22:30:36 -070052
Mathias Agopian65ab4712010-07-14 17:59:35 -070053// ----------------------------------------------------------------------------
54
55AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070056 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070057 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070058{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070059}
60
61void AudioPolicyService::onFirstRef()
62{
Eric Laurent8b1e80b2014-10-07 09:08:47 -070063 {
64 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080065
Eric Laurent8b1e80b2014-10-07 09:08:47 -070066 // start tone playback thread
67 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
68 // start audio commands thread
69 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
70 // start output activity command thread
71 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070072
Eric Laurent8b1e80b2014-10-07 09:08:47 -070073 mAudioPolicyClient = new AudioPolicyClient(this);
74 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurent8b1e80b2014-10-07 09:08:47 -070075 }
bryant_liuba2b4392014-06-11 16:49:30 +080076 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
78 {
79 Mutex::Autolock _l(mLock);
80 mAudioPolicyEffects = audioPolicyEffects;
81 }
Mathias Agopian65ab4712010-07-14 17:59:35 -070082}
83
84AudioPolicyService::~AudioPolicyService()
85{
86 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -070087 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -070088 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -070089
Eric Laurentf269b8e2014-06-09 20:01:29 -070090 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -070091 delete mAudioPolicyClient;
Eric Laurentb52c1522014-05-20 11:27:36 -070092
93 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +080094 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -070095}
96
97// A notification client is always registered by AudioSystem when the client process
98// connects to AudioPolicyService.
99void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
100{
Eric Laurent12590252015-08-21 18:40:20 -0700101 if (client == 0) {
102 ALOGW("%s got NULL client", __FUNCTION__);
103 return;
104 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800105 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700106
107 uid_t uid = IPCThreadState::self()->getCallingUid();
108 if (mNotificationClients.indexOfKey(uid) < 0) {
109 sp<NotificationClient> notificationClient = new NotificationClient(this,
110 client,
111 uid);
112 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
113
114 mNotificationClients.add(uid, notificationClient);
115
Marco Nelissenf8880202014-11-14 07:58:25 -0800116 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurentb52c1522014-05-20 11:27:36 -0700117 binder->linkToDeath(notificationClient);
118 }
119}
120
Eric Laurente8726fe2015-06-26 09:39:24 -0700121void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
122{
123 Mutex::Autolock _l(mNotificationClientsLock);
124
125 uid_t uid = IPCThreadState::self()->getCallingUid();
126 if (mNotificationClients.indexOfKey(uid) < 0) {
127 return;
128 }
129 mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
130}
131
Eric Laurentb52c1522014-05-20 11:27:36 -0700132// removeNotificationClient() is called when the client process dies.
133void AudioPolicyService::removeNotificationClient(uid_t uid)
134{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800135 {
136 Mutex::Autolock _l(mNotificationClientsLock);
137 mNotificationClients.removeItem(uid);
138 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800139 {
140 Mutex::Autolock _l(mLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700141 if (mAudioPolicyManager) {
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700142 mAudioPolicyManager->releaseResourcesForUid(uid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700143 }
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800144 }
Eric Laurentb52c1522014-05-20 11:27:36 -0700145}
146
147void AudioPolicyService::onAudioPortListUpdate()
148{
149 mOutputCommandThread->updateAudioPortListCommand();
150}
151
152void AudioPolicyService::doOnAudioPortListUpdate()
153{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800154 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700155 for (size_t i = 0; i < mNotificationClients.size(); i++) {
156 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
157 }
158}
159
160void AudioPolicyService::onAudioPatchListUpdate()
161{
162 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163}
164
Eric Laurentb52c1522014-05-20 11:27:36 -0700165void AudioPolicyService::doOnAudioPatchListUpdate()
166{
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800167 Mutex::Autolock _l(mNotificationClientsLock);
Eric Laurentb52c1522014-05-20 11:27:36 -0700168 for (size_t i = 0; i < mNotificationClients.size(); i++) {
169 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
170 }
171}
172
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700173void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700174{
175 ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
176 regId.string(), state);
177 mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
178}
179
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700180void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700181{
182 Mutex::Autolock _l(mNotificationClientsLock);
183 for (size_t i = 0; i < mNotificationClients.size(); i++) {
184 mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
185 }
186}
187
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800188void AudioPolicyService::onRecordingConfigurationUpdate(int event,
189 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800190 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800191{
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800192 mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800193 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800194}
195
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800196void AudioPolicyService::doOnRecordingConfigurationUpdate(int event,
197 const record_client_info_t *clientInfo, const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800198 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800199{
200 Mutex::Autolock _l(mNotificationClientsLock);
201 for (size_t i = 0; i < mNotificationClients.size(); i++) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800202 mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800203 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800204 }
205}
206
207status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
208 audio_patch_handle_t *handle,
209 int delayMs)
210{
211 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
212}
213
214status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
215 int delayMs)
216{
217 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
218}
219
Eric Laurente1715a42014-05-20 11:30:42 -0700220status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
221 int delayMs)
222{
223 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
224}
225
Eric Laurentb52c1522014-05-20 11:27:36 -0700226AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
227 const sp<IAudioPolicyServiceClient>& client,
228 uid_t uid)
Eric Laurente8726fe2015-06-26 09:39:24 -0700229 : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
230 mAudioPortCallbacksEnabled(false)
Eric Laurentb52c1522014-05-20 11:27:36 -0700231{
232}
233
234AudioPolicyService::NotificationClient::~NotificationClient()
235{
236}
237
238void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
239{
240 sp<NotificationClient> keep(this);
241 sp<AudioPolicyService> service = mService.promote();
242 if (service != 0) {
243 service->removeNotificationClient(mUid);
244 }
245}
246
247void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
248{
Eric Laurente8726fe2015-06-26 09:39:24 -0700249 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700250 mAudioPolicyServiceClient->onAudioPortListUpdate();
251 }
252}
253
254void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
255{
Eric Laurente8726fe2015-06-26 09:39:24 -0700256 if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
Eric Laurentb52c1522014-05-20 11:27:36 -0700257 mAudioPolicyServiceClient->onAudioPatchListUpdate();
258 }
259}
Eric Laurent57dae992011-07-24 13:36:09 -0700260
Jean-Michel Trivide801052015-04-14 19:10:14 -0700261void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700262 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700263{
264 if (mAudioPolicyServiceClient != 0) {
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800265 mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
266 }
267}
268
269void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800270 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800271 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
272 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800273{
274 if (mAudioPolicyServiceClient != 0) {
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800275 mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800276 clientConfig, deviceConfig, patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700277 }
278}
279
Eric Laurente8726fe2015-06-26 09:39:24 -0700280void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
281{
282 mAudioPortCallbacksEnabled = enabled;
283}
284
285
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700287 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700288 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289}
290
291static bool tryLock(Mutex& mutex)
292{
293 bool locked = false;
294 for (int i = 0; i < kDumpLockRetries; ++i) {
295 if (mutex.tryLock() == NO_ERROR) {
296 locked = true;
297 break;
298 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800299 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 }
301 return locked;
302}
303
304status_t AudioPolicyService::dumpInternals(int fd)
305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
Eric Laurentdce54a12014-03-10 12:19:46 -0700310 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 result.append(buffer);
312 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
313 result.append(buffer);
314 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
315 result.append(buffer);
316
317 write(fd, result.string(), result.size());
318 return NO_ERROR;
319}
320
Glenn Kasten0f11b512014-01-31 16:18:54 -0800321status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322{
Glenn Kasten44deb052012-02-05 18:09:08 -0800323 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 dumpPermissionDenial(fd);
325 } else {
326 bool locked = tryLock(mLock);
327 if (!locked) {
328 String8 result(kDeadlockedString);
329 write(fd, result.string(), result.size());
330 }
331
332 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800333 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 mAudioCommandThread->dump(fd);
335 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800336 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337 mTonePlaybackThread->dump(fd);
338 }
339
Eric Laurentdce54a12014-03-10 12:19:46 -0700340 if (mAudioPolicyManager) {
341 mAudioPolicyManager->dump(fd);
342 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343
344 if (locked) mLock.unlock();
345 }
346 return NO_ERROR;
347}
348
349status_t AudioPolicyService::dumpPermissionDenial(int fd)
350{
351 const size_t SIZE = 256;
352 char buffer[SIZE];
353 String8 result;
354 snprintf(buffer, SIZE, "Permission Denial: "
355 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
356 IPCThreadState::self()->getCallingPid(),
357 IPCThreadState::self()->getCallingUid());
358 result.append(buffer);
359 write(fd, result.string(), result.size());
360 return NO_ERROR;
361}
362
363status_t AudioPolicyService::onTransact(
364 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
365{
366 return BnAudioPolicyService::onTransact(code, data, reply, flags);
367}
368
369
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370// ----------- AudioPolicyService::AudioCommandThread implementation ----------
371
Eric Laurentbfb1b832013-01-07 09:53:42 -0800372AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
373 const wp<AudioPolicyService>& service)
374 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375{
376 mpToneGenerator = NULL;
377}
378
379
380AudioPolicyService::AudioCommandThread::~AudioCommandThread()
381{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800382 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 release_wake_lock(mName.string());
384 }
385 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800386 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387}
388
389void AudioPolicyService::AudioCommandThread::onFirstRef()
390{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800391 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392}
393
394bool AudioPolicyService::AudioCommandThread::threadLoop()
395{
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800396 nsecs_t waitTime = -1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397
398 mLock.lock();
399 while (!exitPending())
400 {
Eric Laurent59a89232014-06-08 14:14:17 -0700401 sp<AudioPolicyService> svc;
402 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 nsecs_t curTime = systemTime();
404 // commands are sorted by increasing time stamp: execute them from index 0 and up
405 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700406 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700408 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409
410 switch (command->mCommand) {
411 case START_TONE: {
412 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700413 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800416 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
418 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 mLock.lock();
420 }break;
421 case STOP_TONE: {
422 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 if (mpToneGenerator != NULL) {
425 mpToneGenerator->stopTone();
426 delete mpToneGenerator;
427 mpToneGenerator = NULL;
428 }
429 mLock.lock();
430 }break;
431 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700432 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700434 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
435 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
436 data->mVolume,
437 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }break;
439 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700441 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
442 data->mKeyValuePairs.string(), data->mIO);
443 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700444 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700446 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700448 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800451 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700452 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800453 ALOGV("AudioCommandThread() processing stop output %d",
454 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700455 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800456 if (svc == 0) {
457 break;
458 }
459 mLock.unlock();
460 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
461 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800462 }break;
463 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700464 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800465 ALOGV("AudioCommandThread() processing release output %d",
466 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700467 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800468 if (svc == 0) {
469 break;
470 }
471 mLock.unlock();
Eric Laurente83b55d2014-11-14 10:06:21 -0800472 svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800473 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800474 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700475 case CREATE_AUDIO_PATCH: {
476 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
477 ALOGV("AudioCommandThread() processing create audio patch");
478 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
479 if (af == 0) {
480 command->mStatus = PERMISSION_DENIED;
481 } else {
482 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
483 }
484 } break;
485 case RELEASE_AUDIO_PATCH: {
486 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
487 ALOGV("AudioCommandThread() processing release audio patch");
488 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
489 if (af == 0) {
490 command->mStatus = PERMISSION_DENIED;
491 } else {
492 command->mStatus = af->releaseAudioPatch(data->mHandle);
493 }
494 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700495 case UPDATE_AUDIOPORT_LIST: {
496 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700497 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700498 if (svc == 0) {
499 break;
500 }
501 mLock.unlock();
502 svc->doOnAudioPortListUpdate();
503 mLock.lock();
504 }break;
505 case UPDATE_AUDIOPATCH_LIST: {
506 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700507 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700508 if (svc == 0) {
509 break;
510 }
511 mLock.unlock();
512 svc->doOnAudioPatchListUpdate();
513 mLock.lock();
514 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700515 case SET_AUDIOPORT_CONFIG: {
516 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
517 ALOGV("AudioCommandThread() processing set port config");
518 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
519 if (af == 0) {
520 command->mStatus = PERMISSION_DENIED;
521 } else {
522 command->mStatus = af->setAudioPortConfig(&data->mConfig);
523 }
524 } break;
Jean-Michel Trivide801052015-04-14 19:10:14 -0700525 case DYN_POLICY_MIX_STATE_UPDATE: {
526 DynPolicyMixStateUpdateData *data =
527 (DynPolicyMixStateUpdateData *)command->mParam.get();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700528 ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
529 data->mRegId.string(), data->mState);
530 svc = mService.promote();
531 if (svc == 0) {
532 break;
533 }
534 mLock.unlock();
535 svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
536 mLock.lock();
537 } break;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800538 case RECORDING_CONFIGURATION_UPDATE: {
539 RecordingConfigurationUpdateData *data =
540 (RecordingConfigurationUpdateData *)command->mParam.get();
541 ALOGV("AudioCommandThread() processing recording configuration update");
542 svc = mService.promote();
543 if (svc == 0) {
544 break;
545 }
546 mLock.unlock();
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800547 svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo,
548 &data->mClientConfig, &data->mDeviceConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800549 data->mPatchHandle);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800550 mLock.lock();
551 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000553 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700555 {
556 Mutex::Autolock _l(command->mLock);
557 if (command->mWaitStatus) {
558 command->mWaitStatus = false;
559 command->mCond.signal();
560 }
561 }
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800562 waitTime = -1;
Zach Janga754b4f2015-10-27 01:29:34 +0000563 // release mLock before releasing strong reference on the service as
564 // AudioPolicyService destructor calls AudioCommandThread::exit() which
565 // acquires mLock.
566 mLock.unlock();
567 svc.clear();
568 mLock.lock();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 } else {
570 waitTime = mAudioCommands[0]->mTime - curTime;
571 break;
572 }
573 }
Zach Janga754b4f2015-10-27 01:29:34 +0000574
575 // release delayed commands wake lock if the queue is empty
576 if (mAudioCommands.isEmpty()) {
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700577 release_wake_lock(mName.string());
Zach Janga754b4f2015-10-27 01:29:34 +0000578 }
579
580 // At this stage we have either an empty command queue or the first command in the queue
581 // has a finite delay. So unless we are exiting it is safe to wait.
582 if (!exitPending()) {
Eric Laurent59a89232014-06-08 14:14:17 -0700583 ALOGV("AudioCommandThread() going to sleep");
Eric Laurentd7eda8d2016-02-02 17:18:39 -0800584 if (waitTime == -1) {
585 mWaitWorkCV.wait(mLock);
586 } else {
587 mWaitWorkCV.waitRelative(mLock, waitTime);
588 }
Eric Laurent59a89232014-06-08 14:14:17 -0700589 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700591 // release delayed commands wake lock before quitting
592 if (!mAudioCommands.isEmpty()) {
593 release_wake_lock(mName.string());
594 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 mLock.unlock();
596 return false;
597}
598
599status_t AudioPolicyService::AudioCommandThread::dump(int fd)
600{
601 const size_t SIZE = 256;
602 char buffer[SIZE];
603 String8 result;
604
605 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
606 result.append(buffer);
607 write(fd, result.string(), result.size());
608
609 bool locked = tryLock(mLock);
610 if (!locked) {
611 String8 result2(kCmdDeadlockedString);
612 write(fd, result2.string(), result2.size());
613 }
614
615 snprintf(buffer, SIZE, "- Commands:\n");
616 result = String8(buffer);
617 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800618 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 mAudioCommands[i]->dump(buffer, SIZE);
620 result.append(buffer);
621 }
622 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700623 if (mLastCommand != 0) {
624 mLastCommand->dump(buffer, SIZE);
625 result.append(buffer);
626 } else {
627 result.append(" none\n");
628 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629
630 write(fd, result.string(), result.size());
631
632 if (locked) mLock.unlock();
633
634 return NO_ERROR;
635}
636
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800637void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
638 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639{
Eric Laurent0ede8922014-05-09 18:04:42 -0700640 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700642 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 data->mType = type;
644 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100645 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100646 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648}
649
650void AudioPolicyService::AudioCommandThread::stopToneCommand()
651{
Eric Laurent0ede8922014-05-09 18:04:42 -0700652 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700655 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656}
657
Glenn Kastenfff6d712012-01-12 16:38:12 -0800658status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700659 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800660 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700661 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662{
Eric Laurent0ede8922014-05-09 18:04:42 -0700663 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 data->mStream = stream;
667 data->mVolume = volume;
668 data->mIO = output;
669 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700670 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700672 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700673 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674}
675
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800676status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700677 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700678 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679{
Eric Laurent0ede8922014-05-09 18:04:42 -0700680 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700682 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700684 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700686 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700688 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700689 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690}
691
692status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
693{
Eric Laurent0ede8922014-05-09 18:04:42 -0700694 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700696 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 data->mVolume = volume;
698 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700699 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700701 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702}
703
Eric Laurentbfb1b832013-01-07 09:53:42 -0800704void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
705 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800706 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800707{
Eric Laurent0ede8922014-05-09 18:04:42 -0700708 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800709 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700710 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800711 data->mIO = output;
712 data->mStream = stream;
713 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100714 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800715 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700716 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800717}
718
Eric Laurente83b55d2014-11-14 10:06:21 -0800719void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
720 audio_stream_type_t stream,
721 audio_session_t session)
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722{
Eric Laurent0ede8922014-05-09 18:04:42 -0700723 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700725 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800726 data->mIO = output;
Eric Laurente83b55d2014-11-14 10:06:21 -0800727 data->mStream = stream;
728 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100729 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800730 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700731 sendCommand(command);
732}
733
Eric Laurent951f4552014-05-20 10:48:17 -0700734status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
735 const struct audio_patch *patch,
736 audio_patch_handle_t *handle,
737 int delayMs)
738{
739 status_t status = NO_ERROR;
740
741 sp<AudioCommand> command = new AudioCommand();
742 command->mCommand = CREATE_AUDIO_PATCH;
743 CreateAudioPatchData *data = new CreateAudioPatchData();
744 data->mPatch = *patch;
745 data->mHandle = *handle;
746 command->mParam = data;
747 command->mWaitStatus = true;
748 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
749 status = sendCommand(command, delayMs);
750 if (status == NO_ERROR) {
751 *handle = data->mHandle;
752 }
753 return status;
754}
755
756status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
757 int delayMs)
758{
759 sp<AudioCommand> command = new AudioCommand();
760 command->mCommand = RELEASE_AUDIO_PATCH;
761 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
762 data->mHandle = handle;
763 command->mParam = data;
764 command->mWaitStatus = true;
765 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
766 return sendCommand(command, delayMs);
767}
768
Eric Laurentb52c1522014-05-20 11:27:36 -0700769void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
770{
771 sp<AudioCommand> command = new AudioCommand();
772 command->mCommand = UPDATE_AUDIOPORT_LIST;
773 ALOGV("AudioCommandThread() adding update audio port list");
774 sendCommand(command);
775}
776
777void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
778{
779 sp<AudioCommand>command = new AudioCommand();
780 command->mCommand = UPDATE_AUDIOPATCH_LIST;
781 ALOGV("AudioCommandThread() adding update audio patch list");
782 sendCommand(command);
783}
784
Eric Laurente1715a42014-05-20 11:30:42 -0700785status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
786 const struct audio_port_config *config, int delayMs)
787{
788 sp<AudioCommand> command = new AudioCommand();
789 command->mCommand = SET_AUDIOPORT_CONFIG;
790 SetAudioPortConfigData *data = new SetAudioPortConfigData();
791 data->mConfig = *config;
792 command->mParam = data;
793 command->mWaitStatus = true;
794 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
795 return sendCommand(command, delayMs);
796}
797
Jean-Michel Trivide801052015-04-14 19:10:14 -0700798void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700799 const String8& regId, int32_t state)
Jean-Michel Trivide801052015-04-14 19:10:14 -0700800{
801 sp<AudioCommand> command = new AudioCommand();
802 command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
803 DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
804 data->mRegId = regId;
805 data->mState = state;
806 command->mParam = data;
807 ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
808 regId.string(), state);
809 sendCommand(command);
810}
811
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800812void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800813 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800814 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
815 audio_patch_handle_t patchHandle)
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800816{
817 sp<AudioCommand>command = new AudioCommand();
818 command->mCommand = RECORDING_CONFIGURATION_UPDATE;
819 RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData();
820 data->mEvent = event;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800821 data->mClientInfo = *clientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800822 data->mClientConfig = *clientConfig;
823 data->mDeviceConfig = *deviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800824 data->mPatchHandle = patchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800825 command->mParam = data;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800826 ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u",
827 event, clientInfo->source, clientInfo->uid);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800828 sendCommand(command);
829}
830
Eric Laurent0ede8922014-05-09 18:04:42 -0700831status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
832{
833 {
834 Mutex::Autolock _l(mLock);
835 insertCommand_l(command, delayMs);
836 mWaitWorkCV.signal();
837 }
838 Mutex::Autolock _l(command->mLock);
839 while (command->mWaitStatus) {
840 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
841 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
842 command->mStatus = TIMED_OUT;
843 command->mWaitStatus = false;
844 }
845 }
846 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800847}
848
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700850void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800852 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700853 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 command->mTime = systemTime() + milliseconds(delayMs);
855
856 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800857 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
859 }
860
861 // check same pending commands with later time stamps and eliminate them
Ivan Lozano5ff158f2017-10-30 09:06:24 -0700862 for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700863 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
865 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700866
867 // create audio patch or release audio patch commands are equivalent
868 // with regard to filtering
869 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
870 (command->mCommand == RELEASE_AUDIO_PATCH)) {
871 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
872 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
873 continue;
874 }
875 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876
877 switch (command->mCommand) {
878 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700879 ParametersData *data = (ParametersData *)command->mParam.get();
880 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700883 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 AudioParameter param = AudioParameter(data->mKeyValuePairs);
885 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
886 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700887 String8 key;
888 String8 value;
889 param.getAt(j, key, value);
890 for (size_t k = 0; k < param2.size(); k++) {
891 String8 key2;
892 String8 value2;
893 param2.getAt(k, key2, value2);
894 if (key2 == key) {
895 param2.remove(key2);
896 ALOGV("Filtering out parameter %s", key2.string());
897 break;
898 }
899 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 }
901 // if all keys have been filtered out, remove the command.
902 // otherwise, update the key value pairs
903 if (param2.size() == 0) {
904 removedCommands.add(command2);
905 } else {
906 data2->mKeyValuePairs = param2.toString();
907 }
Eric Laurent21e54562013-09-23 12:08:05 -0700908 command->mTime = command2->mTime;
909 // force delayMs to non 0 so that code below does not request to wait for
910 // command status as the command is now delayed
911 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 } break;
913
914 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700915 VolumeData *data = (VolumeData *)command->mParam.get();
916 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 if (data->mIO != data2->mIO) break;
918 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700920 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700922 command->mTime = command2->mTime;
923 // force delayMs to non 0 so that code below does not request to wait for
924 // command status as the command is now delayed
925 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700927
Eric Laurentbaf35fe2016-07-27 15:36:53 -0700928 case SET_VOICE_VOLUME: {
929 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
930 VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get();
931 ALOGV("Filtering out voice volume command value %f replaced by %f",
932 data2->mVolume, data->mVolume);
933 removedCommands.add(command2);
934 command->mTime = command2->mTime;
935 // force delayMs to non 0 so that code below does not request to wait for
936 // command status as the command is now delayed
937 delayMs = 1;
938 } break;
939
Eric Laurente45b48a2014-09-04 16:40:57 -0700940 case CREATE_AUDIO_PATCH:
941 case RELEASE_AUDIO_PATCH: {
942 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700943 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700944 if (command->mCommand == CREATE_AUDIO_PATCH) {
945 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700946 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700947 } else {
948 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
949 }
950 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700951 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700952 if (command2->mCommand == CREATE_AUDIO_PATCH) {
953 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700954 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700955 } else {
956 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
Glenn Kastenf60b6b62015-07-06 10:53:26 -0700957 memset(&patch2, 0, sizeof(patch2));
Eric Laurente45b48a2014-09-04 16:40:57 -0700958 }
959 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700960 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
961 same output. */
962 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
963 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
964 bool isOutputDiff = false;
965 if (patch.num_sources == patch2.num_sources) {
966 for (unsigned count = 0; count < patch.num_sources; count++) {
967 if (patch.sources[count].id != patch2.sources[count].id) {
968 isOutputDiff = true;
969 break;
970 }
971 }
972 if (isOutputDiff)
973 break;
974 }
975 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700976 ALOGV("Filtering out %s audio patch command for handle %d",
977 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
978 removedCommands.add(command2);
979 command->mTime = command2->mTime;
980 // force delayMs to non 0 so that code below does not request to wait for
981 // command status as the command is now delayed
982 delayMs = 1;
983 } break;
984
Jean-Michel Trivide801052015-04-14 19:10:14 -0700985 case DYN_POLICY_MIX_STATE_UPDATE: {
986
987 } break;
988
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800989 case RECORDING_CONFIGURATION_UPDATE: {
990
991 } break;
992
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 case START_TONE:
994 case STOP_TONE:
995 default:
996 break;
997 }
998 }
999
1000 // remove filtered commands
1001 for (size_t j = 0; j < removedCommands.size(); j++) {
1002 // removed commands always have time stamps greater than current command
1003 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -07001004 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +01001005 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 mAudioCommands.removeAt(k);
1007 break;
1008 }
1009 }
1010 }
1011 removedCommands.clear();
1012
Eric Laurentaa79bef2015-01-15 14:33:51 -08001013 // Disable wait for status if delay is not 0.
1014 // Except for create audio patch command because the returned patch handle
1015 // is needed by audio policy manager
1016 if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001017 command->mWaitStatus = false;
1018 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001019
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -07001021 ALOGV("inserting command: %d at index %zd, num commands %zu",
1022 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 mAudioCommands.insertAt(command, i + 1);
1024}
1025
1026void AudioPolicyService::AudioCommandThread::exit()
1027{
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029 {
1030 AutoMutex _l(mLock);
1031 requestExit();
1032 mWaitWorkCV.signal();
1033 }
Zach Janga754b4f2015-10-27 01:29:34 +00001034 // Note that we can call it from the thread loop if all other references have been released
1035 // but it will safely return WOULD_BLOCK in this case
Mathias Agopian65ab4712010-07-14 17:59:35 -07001036 requestExitAndWait();
1037}
1038
1039void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1040{
1041 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1042 mCommand,
1043 (int)ns2s(mTime),
1044 (int)ns2ms(mTime)%1000,
1045 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -07001046 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047}
1048
Dima Zavinfce7a472011-04-19 22:30:36 -07001049/******* helpers for the service_ops callbacks defined below *********/
1050void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1051 const char *keyValuePairs,
1052 int delayMs)
1053{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001054 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001055 delayMs);
1056}
1057
1058int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1059 float volume,
1060 audio_io_handle_t output,
1061 int delayMs)
1062{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001063 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001064 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001065}
1066
1067int AudioPolicyService::startTone(audio_policy_tone_t tone,
1068 audio_stream_type_t stream)
1069{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001070 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001071 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001072 }
1073 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001074 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001075 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001076 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001077 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1078 AUDIO_STREAM_VOICE_CALL);
1079 return 0;
1080}
1081
1082int AudioPolicyService::stopTone()
1083{
1084 mTonePlaybackThread->stopToneCommand();
1085 return 0;
1086}
1087
1088int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1089{
1090 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1091}
1092
Dima Zavinfce7a472011-04-19 22:30:36 -07001093extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001094audio_module_handle_t aps_load_hw_module(void *service __unused,
1095 const char *name);
1096audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001097 audio_devices_t *pDevices,
1098 uint32_t *pSamplingRate,
1099 audio_format_t *pFormat,
1100 audio_channel_mask_t *pChannelMask,
1101 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001102 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001103
Eric Laurent2d388ec2014-03-07 13:25:54 -08001104audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001105 audio_module_handle_t module,
1106 audio_devices_t *pDevices,
1107 uint32_t *pSamplingRate,
1108 audio_format_t *pFormat,
1109 audio_channel_mask_t *pChannelMask,
1110 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001111 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001112 const audio_offload_info_t *offloadInfo);
1113audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001114 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001115 audio_io_handle_t output2);
1116int aps_close_output(void *service __unused, audio_io_handle_t output);
1117int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1118int aps_restore_output(void *service __unused, audio_io_handle_t output);
1119audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001120 audio_devices_t *pDevices,
1121 uint32_t *pSamplingRate,
1122 audio_format_t *pFormat,
1123 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001124 audio_in_acoustics_t acoustics __unused);
1125audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001126 audio_module_handle_t module,
1127 audio_devices_t *pDevices,
1128 uint32_t *pSamplingRate,
1129 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001130 audio_channel_mask_t *pChannelMask);
1131int aps_close_input(void *service __unused, audio_io_handle_t input);
1132int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
Glenn Kastend848eb42016-03-08 13:42:11 -08001133int aps_move_effects(void *service __unused, audio_session_t session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001134 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001135 audio_io_handle_t dst_output);
1136char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1137 const char *keys);
1138void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1139 const char *kv_pairs, int delay_ms);
1140int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001141 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001142 int delay_ms);
1143int aps_start_tone(void *service, audio_policy_tone_t tone,
1144 audio_stream_type_t stream);
1145int aps_stop_tone(void *service);
1146int aps_set_voice_volume(void *service, float volume, int delay_ms);
1147};
Dima Zavinfce7a472011-04-19 22:30:36 -07001148
Mikhail Naganov1b2a7942017-12-08 10:18:09 -08001149} // namespace android