blob: 7f14960ff6666170d74a26a052e37988867cd0c6 [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>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Mathias Agopian65ab4712010-07-14 17:59:35 -070044namespace android {
45
Glenn Kasten8dad0e32012-01-09 08:41:22 -080046static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
47static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080050static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Eric Laurent0ede8922014-05-09 18:04:42 -070052static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010053
Dima Zavinfce7a472011-04-19 22:30:36 -070054namespace {
55 extern struct audio_policy_service_ops aps_ops;
56};
57
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070061 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
62 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
64 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070065 const struct hw_module_t *module;
66 int forced_val;
67 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
Eric Laurent93575202011-01-18 18:39:02 -080069 Mutex::Autolock _l(mLock);
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080072 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
75 // start output activity command thread
76 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070077
78#ifdef USE_LEGACY_AUDIO_POLICY
79 ALOGI("AudioPolicyService CSTOR in legacy mode");
80
Dima Zavinfce7a472011-04-19 22:30:36 -070081 /* instantiate the audio policy manager */
82 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070083 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070084 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070085 }
Dima Zavinfce7a472011-04-19 22:30:36 -070086 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000087 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070088 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070089 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070090 }
Eric Laurent93575202011-01-18 18:39:02 -080091
Dima Zavinfce7a472011-04-19 22:30:36 -070092 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
93 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070095 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070096 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070097 }
Dima Zavinfce7a472011-04-19 22:30:36 -070098
99 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +0000100 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700101 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700102 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700103 }
Steve Blockdf64d152012-01-04 20:05:49 +0000104 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700105#else
106 ALOGI("AudioPolicyService CSTOR in new mode");
107
108 mAudioPolicyClient = new AudioPolicyClient(this);
Eric Laurentf269b8e2014-06-09 20:01:29 -0700109 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700110#endif
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700111
bryant_liuba2b4392014-06-11 16:49:30 +0800112 // load audio processing modules
113 mAudioPolicyEffects = new AudioPolicyEffects();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114}
115
116AudioPolicyService::~AudioPolicyService()
117{
118 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700120 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700121
Eric Laurentdce54a12014-03-10 12:19:46 -0700122#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700123 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700124 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700125 }
126 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700127 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700128 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700129#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700130 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700131 delete mAudioPolicyClient;
132#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700133
134 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800135 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700136}
137
138// A notification client is always registered by AudioSystem when the client process
139// connects to AudioPolicyService.
140void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
141{
142
143 Mutex::Autolock _l(mLock);
144
145 uid_t uid = IPCThreadState::self()->getCallingUid();
146 if (mNotificationClients.indexOfKey(uid) < 0) {
147 sp<NotificationClient> notificationClient = new NotificationClient(this,
148 client,
149 uid);
150 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
151
152 mNotificationClients.add(uid, notificationClient);
153
154 sp<IBinder> binder = client->asBinder();
155 binder->linkToDeath(notificationClient);
156 }
157}
158
159// removeNotificationClient() is called when the client process dies.
160void AudioPolicyService::removeNotificationClient(uid_t uid)
161{
162 Mutex::Autolock _l(mLock);
163
164 mNotificationClients.removeItem(uid);
165
166#ifndef USE_LEGACY_AUDIO_POLICY
167 if (mAudioPolicyManager) {
168 mAudioPolicyManager->clearAudioPatches(uid);
169 }
170#endif
171}
172
173void AudioPolicyService::onAudioPortListUpdate()
174{
175 mOutputCommandThread->updateAudioPortListCommand();
176}
177
178void AudioPolicyService::doOnAudioPortListUpdate()
179{
180 Mutex::Autolock _l(mLock);
181 for (size_t i = 0; i < mNotificationClients.size(); i++) {
182 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
183 }
184}
185
186void AudioPolicyService::onAudioPatchListUpdate()
187{
188 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189}
190
Eric Laurent951f4552014-05-20 10:48:17 -0700191status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
192 audio_patch_handle_t *handle,
193 int delayMs)
194{
195 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
196}
197
198status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
199 int delayMs)
200{
201 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
202}
203
Eric Laurentb52c1522014-05-20 11:27:36 -0700204void AudioPolicyService::doOnAudioPatchListUpdate()
205{
206 Mutex::Autolock _l(mLock);
207 for (size_t i = 0; i < mNotificationClients.size(); i++) {
208 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
209 }
210}
211
Eric Laurente1715a42014-05-20 11:30:42 -0700212status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
213 int delayMs)
214{
215 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
216}
217
Eric Laurentb52c1522014-05-20 11:27:36 -0700218AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
219 const sp<IAudioPolicyServiceClient>& client,
220 uid_t uid)
221 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
222{
223}
224
225AudioPolicyService::NotificationClient::~NotificationClient()
226{
227}
228
229void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
230{
231 sp<NotificationClient> keep(this);
232 sp<AudioPolicyService> service = mService.promote();
233 if (service != 0) {
234 service->removeNotificationClient(mUid);
235 }
236}
237
238void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
239{
240 if (mAudioPolicyServiceClient != 0) {
241 mAudioPolicyServiceClient->onAudioPortListUpdate();
242 }
243}
244
245void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
246{
247 if (mAudioPolicyServiceClient != 0) {
248 mAudioPolicyServiceClient->onAudioPatchListUpdate();
249 }
250}
Eric Laurent57dae992011-07-24 13:36:09 -0700251
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700253 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700254 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255}
256
257static bool tryLock(Mutex& mutex)
258{
259 bool locked = false;
260 for (int i = 0; i < kDumpLockRetries; ++i) {
261 if (mutex.tryLock() == NO_ERROR) {
262 locked = true;
263 break;
264 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800265 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 }
267 return locked;
268}
269
270status_t AudioPolicyService::dumpInternals(int fd)
271{
272 const size_t SIZE = 256;
273 char buffer[SIZE];
274 String8 result;
275
Eric Laurentdce54a12014-03-10 12:19:46 -0700276#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700278#else
279 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
280#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 result.append(buffer);
282 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
283 result.append(buffer);
284 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
285 result.append(buffer);
286
287 write(fd, result.string(), result.size());
288 return NO_ERROR;
289}
290
Glenn Kasten0f11b512014-01-31 16:18:54 -0800291status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292{
Glenn Kasten44deb052012-02-05 18:09:08 -0800293 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 dumpPermissionDenial(fd);
295 } else {
296 bool locked = tryLock(mLock);
297 if (!locked) {
298 String8 result(kDeadlockedString);
299 write(fd, result.string(), result.size());
300 }
301
302 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800303 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 mAudioCommandThread->dump(fd);
305 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800306 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 mTonePlaybackThread->dump(fd);
308 }
309
Eric Laurentdce54a12014-03-10 12:19:46 -0700310#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700311 if (mpAudioPolicy) {
312 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700314#else
315 if (mAudioPolicyManager) {
316 mAudioPolicyManager->dump(fd);
317 }
318#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319
320 if (locked) mLock.unlock();
321 }
322 return NO_ERROR;
323}
324
325status_t AudioPolicyService::dumpPermissionDenial(int fd)
326{
327 const size_t SIZE = 256;
328 char buffer[SIZE];
329 String8 result;
330 snprintf(buffer, SIZE, "Permission Denial: "
331 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
332 IPCThreadState::self()->getCallingPid(),
333 IPCThreadState::self()->getCallingUid());
334 result.append(buffer);
335 write(fd, result.string(), result.size());
336 return NO_ERROR;
337}
338
339status_t AudioPolicyService::onTransact(
340 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
341{
342 return BnAudioPolicyService::onTransact(code, data, reply, flags);
343}
344
345
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346// ----------- AudioPolicyService::AudioCommandThread implementation ----------
347
Eric Laurentbfb1b832013-01-07 09:53:42 -0800348AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
349 const wp<AudioPolicyService>& service)
350 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
352 mpToneGenerator = NULL;
353}
354
355
356AudioPolicyService::AudioCommandThread::~AudioCommandThread()
357{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800358 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 release_wake_lock(mName.string());
360 }
361 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800362 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363}
364
365void AudioPolicyService::AudioCommandThread::onFirstRef()
366{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800367 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368}
369
370bool AudioPolicyService::AudioCommandThread::threadLoop()
371{
372 nsecs_t waitTime = INT64_MAX;
373
374 mLock.lock();
375 while (!exitPending())
376 {
Eric Laurent59a89232014-06-08 14:14:17 -0700377 sp<AudioPolicyService> svc;
378 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 nsecs_t curTime = systemTime();
380 // commands are sorted by increasing time stamp: execute them from index 0 and up
381 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700382 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700384 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
386 switch (command->mCommand) {
387 case START_TONE: {
388 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700389 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800392 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
394 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 mLock.lock();
396 }break;
397 case STOP_TONE: {
398 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 if (mpToneGenerator != NULL) {
401 mpToneGenerator->stopTone();
402 delete mpToneGenerator;
403 mpToneGenerator = NULL;
404 }
405 mLock.lock();
406 }break;
407 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700408 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100409 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700410 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
411 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
412 data->mVolume,
413 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 }break;
415 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700416 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700417 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
418 data->mKeyValuePairs.string(), data->mIO);
419 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700420 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700422 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700424 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800427 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700428 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800429 ALOGV("AudioCommandThread() processing stop output %d",
430 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700431 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800432 if (svc == 0) {
433 break;
434 }
435 mLock.unlock();
436 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
437 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 }break;
439 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700440 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800441 ALOGV("AudioCommandThread() processing release output %d",
442 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700443 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800444 if (svc == 0) {
445 break;
446 }
447 mLock.unlock();
448 svc->doReleaseOutput(data->mIO);
449 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800450 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700451 case CREATE_AUDIO_PATCH: {
452 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
453 ALOGV("AudioCommandThread() processing create audio patch");
454 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
455 if (af == 0) {
456 command->mStatus = PERMISSION_DENIED;
457 } else {
458 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
459 }
460 } break;
461 case RELEASE_AUDIO_PATCH: {
462 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
463 ALOGV("AudioCommandThread() processing release audio patch");
464 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
465 if (af == 0) {
466 command->mStatus = PERMISSION_DENIED;
467 } else {
468 command->mStatus = af->releaseAudioPatch(data->mHandle);
469 }
470 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700471 case UPDATE_AUDIOPORT_LIST: {
472 ALOGV("AudioCommandThread() processing update audio port list");
Eric Laurent59a89232014-06-08 14:14:17 -0700473 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700474 if (svc == 0) {
475 break;
476 }
477 mLock.unlock();
478 svc->doOnAudioPortListUpdate();
479 mLock.lock();
480 }break;
481 case UPDATE_AUDIOPATCH_LIST: {
482 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700483 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700484 if (svc == 0) {
485 break;
486 }
487 mLock.unlock();
488 svc->doOnAudioPatchListUpdate();
489 mLock.lock();
490 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700491 case SET_AUDIOPORT_CONFIG: {
492 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
493 ALOGV("AudioCommandThread() processing set port config");
494 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
495 if (af == 0) {
496 command->mStatus = PERMISSION_DENIED;
497 } else {
498 command->mStatus = af->setAudioPortConfig(&data->mConfig);
499 }
500 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000502 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700504 {
505 Mutex::Autolock _l(command->mLock);
506 if (command->mWaitStatus) {
507 command->mWaitStatus = false;
508 command->mCond.signal();
509 }
510 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 waitTime = INT64_MAX;
512 } else {
513 waitTime = mAudioCommands[0]->mTime - curTime;
514 break;
515 }
516 }
Eric Laurent59a89232014-06-08 14:14:17 -0700517 // release mLock before releasing strong reference on the service as
518 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
519 mLock.unlock();
520 svc.clear();
521 mLock.lock();
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700522 if (!exitPending() && mAudioCommands.isEmpty()) {
523 // release delayed commands wake lock
524 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700525 ALOGV("AudioCommandThread() going to sleep");
526 mWaitWorkCV.waitRelative(mLock, waitTime);
527 ALOGV("AudioCommandThread() waking up");
528 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700530 // release delayed commands wake lock before quitting
531 if (!mAudioCommands.isEmpty()) {
532 release_wake_lock(mName.string());
533 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 mLock.unlock();
535 return false;
536}
537
538status_t AudioPolicyService::AudioCommandThread::dump(int fd)
539{
540 const size_t SIZE = 256;
541 char buffer[SIZE];
542 String8 result;
543
544 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
545 result.append(buffer);
546 write(fd, result.string(), result.size());
547
548 bool locked = tryLock(mLock);
549 if (!locked) {
550 String8 result2(kCmdDeadlockedString);
551 write(fd, result2.string(), result2.size());
552 }
553
554 snprintf(buffer, SIZE, "- Commands:\n");
555 result = String8(buffer);
556 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800557 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 mAudioCommands[i]->dump(buffer, SIZE);
559 result.append(buffer);
560 }
561 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700562 if (mLastCommand != 0) {
563 mLastCommand->dump(buffer, SIZE);
564 result.append(buffer);
565 } else {
566 result.append(" none\n");
567 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568
569 write(fd, result.string(), result.size());
570
571 if (locked) mLock.unlock();
572
573 return NO_ERROR;
574}
575
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800576void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
577 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578{
Eric Laurent0ede8922014-05-09 18:04:42 -0700579 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700581 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 data->mType = type;
583 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100584 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100585 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700586 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587}
588
589void AudioPolicyService::AudioCommandThread::stopToneCommand()
590{
Eric Laurent0ede8922014-05-09 18:04:42 -0700591 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700594 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595}
596
Glenn Kastenfff6d712012-01-12 16:38:12 -0800597status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700598 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800599 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700600 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601{
Eric Laurent0ede8922014-05-09 18:04:42 -0700602 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700604 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 data->mStream = stream;
606 data->mVolume = volume;
607 data->mIO = output;
608 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700609 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100610 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700611 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700612 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613}
614
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800615status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700616 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700617 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618{
Eric Laurent0ede8922014-05-09 18:04:42 -0700619 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700621 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700623 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700625 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100626 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700627 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700628 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629}
630
631status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
632{
Eric Laurent0ede8922014-05-09 18:04:42 -0700633 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700635 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 data->mVolume = volume;
637 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700638 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700640 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641}
642
Eric Laurentbfb1b832013-01-07 09:53:42 -0800643void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
644 audio_stream_type_t stream,
645 int session)
646{
Eric Laurent0ede8922014-05-09 18:04:42 -0700647 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800648 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700649 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800650 data->mIO = output;
651 data->mStream = stream;
652 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100653 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800654 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700655 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800656}
657
658void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
659{
Eric Laurent0ede8922014-05-09 18:04:42 -0700660 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800661 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700662 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800663 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100664 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800665 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700666 sendCommand(command);
667}
668
Eric Laurent951f4552014-05-20 10:48:17 -0700669status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
670 const struct audio_patch *patch,
671 audio_patch_handle_t *handle,
672 int delayMs)
673{
674 status_t status = NO_ERROR;
675
676 sp<AudioCommand> command = new AudioCommand();
677 command->mCommand = CREATE_AUDIO_PATCH;
678 CreateAudioPatchData *data = new CreateAudioPatchData();
679 data->mPatch = *patch;
680 data->mHandle = *handle;
681 command->mParam = data;
682 command->mWaitStatus = true;
683 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
684 status = sendCommand(command, delayMs);
685 if (status == NO_ERROR) {
686 *handle = data->mHandle;
687 }
688 return status;
689}
690
691status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
692 int delayMs)
693{
694 sp<AudioCommand> command = new AudioCommand();
695 command->mCommand = RELEASE_AUDIO_PATCH;
696 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
697 data->mHandle = handle;
698 command->mParam = data;
699 command->mWaitStatus = true;
700 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
701 return sendCommand(command, delayMs);
702}
703
Eric Laurentb52c1522014-05-20 11:27:36 -0700704void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
705{
706 sp<AudioCommand> command = new AudioCommand();
707 command->mCommand = UPDATE_AUDIOPORT_LIST;
708 ALOGV("AudioCommandThread() adding update audio port list");
709 sendCommand(command);
710}
711
712void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
713{
714 sp<AudioCommand>command = new AudioCommand();
715 command->mCommand = UPDATE_AUDIOPATCH_LIST;
716 ALOGV("AudioCommandThread() adding update audio patch list");
717 sendCommand(command);
718}
719
Eric Laurente1715a42014-05-20 11:30:42 -0700720status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
721 const struct audio_port_config *config, int delayMs)
722{
723 sp<AudioCommand> command = new AudioCommand();
724 command->mCommand = SET_AUDIOPORT_CONFIG;
725 SetAudioPortConfigData *data = new SetAudioPortConfigData();
726 data->mConfig = *config;
727 command->mParam = data;
728 command->mWaitStatus = true;
729 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
730 return sendCommand(command, delayMs);
731}
732
Eric Laurent0ede8922014-05-09 18:04:42 -0700733status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
734{
735 {
736 Mutex::Autolock _l(mLock);
737 insertCommand_l(command, delayMs);
738 mWaitWorkCV.signal();
739 }
740 Mutex::Autolock _l(command->mLock);
741 while (command->mWaitStatus) {
742 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
743 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
744 command->mStatus = TIMED_OUT;
745 command->mWaitStatus = false;
746 }
747 }
748 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800749}
750
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700752void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800754 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700755 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756 command->mTime = systemTime() + milliseconds(delayMs);
757
758 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800759 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
761 }
762
763 // check same pending commands with later time stamps and eliminate them
764 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700765 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
767 if (command2->mTime <= command->mTime) break;
768 if (command2->mCommand != command->mCommand) continue;
769
770 switch (command->mCommand) {
771 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700772 ParametersData *data = (ParametersData *)command->mParam.get();
773 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700774 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100775 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700776 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 AudioParameter param = AudioParameter(data->mKeyValuePairs);
778 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
779 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700780 String8 key;
781 String8 value;
782 param.getAt(j, key, value);
783 for (size_t k = 0; k < param2.size(); k++) {
784 String8 key2;
785 String8 value2;
786 param2.getAt(k, key2, value2);
787 if (key2 == key) {
788 param2.remove(key2);
789 ALOGV("Filtering out parameter %s", key2.string());
790 break;
791 }
792 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 }
794 // if all keys have been filtered out, remove the command.
795 // otherwise, update the key value pairs
796 if (param2.size() == 0) {
797 removedCommands.add(command2);
798 } else {
799 data2->mKeyValuePairs = param2.toString();
800 }
Eric Laurent21e54562013-09-23 12:08:05 -0700801 command->mTime = command2->mTime;
802 // force delayMs to non 0 so that code below does not request to wait for
803 // command status as the command is now delayed
804 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 } break;
806
807 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700808 VolumeData *data = (VolumeData *)command->mParam.get();
809 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 if (data->mIO != data2->mIO) break;
811 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100812 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700813 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700815 command->mTime = command2->mTime;
816 // force delayMs to non 0 so that code below does not request to wait for
817 // command status as the command is now delayed
818 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 } break;
820 case START_TONE:
821 case STOP_TONE:
822 default:
823 break;
824 }
825 }
826
827 // remove filtered commands
828 for (size_t j = 0; j < removedCommands.size(); j++) {
829 // removed commands always have time stamps greater than current command
830 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700831 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100832 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mAudioCommands.removeAt(k);
834 break;
835 }
836 }
837 }
838 removedCommands.clear();
839
Eric Laurent0ede8922014-05-09 18:04:42 -0700840 // Disable wait for status if delay is not 0
841 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700842 command->mWaitStatus = false;
843 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700844
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700846 ALOGV("inserting command: %d at index %zd, num commands %zu",
847 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 mAudioCommands.insertAt(command, i + 1);
849}
850
851void AudioPolicyService::AudioCommandThread::exit()
852{
Steve Block3856b092011-10-20 11:56:00 +0100853 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 {
855 AutoMutex _l(mLock);
856 requestExit();
857 mWaitWorkCV.signal();
858 }
859 requestExitAndWait();
860}
861
862void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
863{
864 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
865 mCommand,
866 (int)ns2s(mTime),
867 (int)ns2ms(mTime)%1000,
868 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700869 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870}
871
Dima Zavinfce7a472011-04-19 22:30:36 -0700872/******* helpers for the service_ops callbacks defined below *********/
873void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
874 const char *keyValuePairs,
875 int delayMs)
876{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800877 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700878 delayMs);
879}
880
881int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
882 float volume,
883 audio_io_handle_t output,
884 int delayMs)
885{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800886 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800887 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700888}
889
890int AudioPolicyService::startTone(audio_policy_tone_t tone,
891 audio_stream_type_t stream)
892{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700893 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000894 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700895 }
896 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000897 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700898 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700899 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700900 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
901 AUDIO_STREAM_VOICE_CALL);
902 return 0;
903}
904
905int AudioPolicyService::stopTone()
906{
907 mTonePlaybackThread->stopToneCommand();
908 return 0;
909}
910
911int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
912{
913 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
914}
915
Dima Zavinfce7a472011-04-19 22:30:36 -0700916extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800917audio_module_handle_t aps_load_hw_module(void *service __unused,
918 const char *name);
919audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700920 audio_devices_t *pDevices,
921 uint32_t *pSamplingRate,
922 audio_format_t *pFormat,
923 audio_channel_mask_t *pChannelMask,
924 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800925 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700926
Eric Laurent2d388ec2014-03-07 13:25:54 -0800927audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700928 audio_module_handle_t module,
929 audio_devices_t *pDevices,
930 uint32_t *pSamplingRate,
931 audio_format_t *pFormat,
932 audio_channel_mask_t *pChannelMask,
933 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000934 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800935 const audio_offload_info_t *offloadInfo);
936audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -0700937 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800938 audio_io_handle_t output2);
939int aps_close_output(void *service __unused, audio_io_handle_t output);
940int aps_suspend_output(void *service __unused, audio_io_handle_t output);
941int aps_restore_output(void *service __unused, audio_io_handle_t output);
942audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700943 audio_devices_t *pDevices,
944 uint32_t *pSamplingRate,
945 audio_format_t *pFormat,
946 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800947 audio_in_acoustics_t acoustics __unused);
948audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700949 audio_module_handle_t module,
950 audio_devices_t *pDevices,
951 uint32_t *pSamplingRate,
952 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800953 audio_channel_mask_t *pChannelMask);
954int aps_close_input(void *service __unused, audio_io_handle_t input);
955int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
956int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -0700957 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800958 audio_io_handle_t dst_output);
959char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
960 const char *keys);
961void aps_set_parameters(void *service, audio_io_handle_t io_handle,
962 const char *kv_pairs, int delay_ms);
963int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800965 int delay_ms);
966int aps_start_tone(void *service, audio_policy_tone_t tone,
967 audio_stream_type_t stream);
968int aps_stop_tone(void *service);
969int aps_set_voice_volume(void *service, float volume, int delay_ms);
970};
Dima Zavinfce7a472011-04-19 22:30:36 -0700971
972namespace {
973 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800974 .open_output = aps_open_output,
975 .open_duplicate_output = aps_open_dup_output,
976 .close_output = aps_close_output,
977 .suspend_output = aps_suspend_output,
978 .restore_output = aps_restore_output,
979 .open_input = aps_open_input,
980 .close_input = aps_close_input,
981 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -0800982 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800983 .set_parameters = aps_set_parameters,
984 .get_parameters = aps_get_parameters,
985 .start_tone = aps_start_tone,
986 .stop_tone = aps_stop_tone,
987 .set_voice_volume = aps_set_voice_volume,
988 .move_effects = aps_move_effects,
989 .load_hw_module = aps_load_hw_module,
990 .open_output_on_module = aps_open_output_on_module,
991 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -0700992 };
993}; // namespace <unnamed>
994
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995}; // namespace android