blob: 3a5626d7c50b87b00a288b9f9133e4d77f529569 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080038#include <media/AudioParameter.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043#include <hardware/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Eric Laurent0ede8922014-05-09 18:04:42 -070053static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Dima Zavinfce7a472011-04-19 22:30:36 -070055namespace {
56 extern struct audio_policy_service_ops aps_ops;
57};
58
Mathias Agopian65ab4712010-07-14 17:59:35 -070059// ----------------------------------------------------------------------------
60
61AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070062 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
Eric Laurentbb6c9a02014-09-25 14:11:47 -070063 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
Eric Laurentf5ada6e2014-10-09 17:49:00 -070065}
66
67void AudioPolicyService::onFirstRef()
68{
Mathias Agopian65ab4712010-07-14 17:59:35 -070069 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070070 const struct hw_module_t *module;
71 int forced_val;
72 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
Eric Laurent8b1e80b2014-10-07 09:08:47 -070074 {
75 Mutex::Autolock _l(mLock);
Eric Laurent93575202011-01-18 18:39:02 -080076
Eric Laurent8b1e80b2014-10-07 09:08:47 -070077 // start tone playback thread
78 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
79 // start audio commands thread
80 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
81 // start output activity command thread
82 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070083
84#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurent8b1e80b2014-10-07 09:08:47 -070085 ALOGI("AudioPolicyService CSTOR in legacy mode");
Eric Laurentdce54a12014-03-10 12:19:46 -070086
Eric Laurent8b1e80b2014-10-07 09:08:47 -070087 /* instantiate the audio policy manager */
88 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
89 if (rc) {
90 return;
91 }
92 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
93 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
94 if (rc) {
95 return;
96 }
Eric Laurent93575202011-01-18 18:39:02 -080097
Eric Laurent8b1e80b2014-10-07 09:08:47 -070098 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
99 &mpAudioPolicy);
100 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
101 if (rc) {
102 return;
103 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700104
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700105 rc = mpAudioPolicy->init_check(mpAudioPolicy);
106 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
107 if (rc) {
108 return;
109 }
110 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700111#else
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700112 ALOGI("AudioPolicyService CSTOR in new mode");
Eric Laurentdce54a12014-03-10 12:19:46 -0700113
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700114 mAudioPolicyClient = new AudioPolicyClient(this);
115 mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
Eric Laurentdce54a12014-03-10 12:19:46 -0700116#endif
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700117 }
bryant_liuba2b4392014-06-11 16:49:30 +0800118 // load audio processing modules
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700119 sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
120 {
121 Mutex::Autolock _l(mLock);
122 mAudioPolicyEffects = audioPolicyEffects;
123 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124}
125
126AudioPolicyService::~AudioPolicyService()
127{
128 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700130 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131
Eric Laurentdce54a12014-03-10 12:19:46 -0700132#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700139#else
Eric Laurentf269b8e2014-06-09 20:01:29 -0700140 destroyAudioPolicyManager(mAudioPolicyManager);
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 delete mAudioPolicyClient;
142#endif
Eric Laurentb52c1522014-05-20 11:27:36 -0700143
144 mNotificationClients.clear();
bryant_liuba2b4392014-06-11 16:49:30 +0800145 mAudioPolicyEffects.clear();
Eric Laurentb52c1522014-05-20 11:27:36 -0700146}
147
148// A notification client is always registered by AudioSystem when the client process
149// connects to AudioPolicyService.
150void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
151{
152
153 Mutex::Autolock _l(mLock);
154
155 uid_t uid = IPCThreadState::self()->getCallingUid();
156 if (mNotificationClients.indexOfKey(uid) < 0) {
157 sp<NotificationClient> notificationClient = new NotificationClient(this,
158 client,
159 uid);
160 ALOGV("registerClient() client %p, uid %d", client.get(), uid);
161
162 mNotificationClients.add(uid, notificationClient);
163
164 sp<IBinder> binder = client->asBinder();
165 binder->linkToDeath(notificationClient);
166 }
167}
168
169// removeNotificationClient() is called when the client process dies.
170void AudioPolicyService::removeNotificationClient(uid_t uid)
171{
172 Mutex::Autolock _l(mLock);
173
174 mNotificationClients.removeItem(uid);
175
176#ifndef USE_LEGACY_AUDIO_POLICY
177 if (mAudioPolicyManager) {
178 mAudioPolicyManager->clearAudioPatches(uid);
179 }
180#endif
181}
182
183void AudioPolicyService::onAudioPortListUpdate()
184{
185 mOutputCommandThread->updateAudioPortListCommand();
186}
187
188void AudioPolicyService::doOnAudioPortListUpdate()
189{
190 Mutex::Autolock _l(mLock);
191 for (size_t i = 0; i < mNotificationClients.size(); i++) {
192 mNotificationClients.valueAt(i)->onAudioPortListUpdate();
193 }
194}
195
196void AudioPolicyService::onAudioPatchListUpdate()
197{
198 mOutputCommandThread->updateAudioPatchListCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199}
200
Eric Laurent951f4552014-05-20 10:48:17 -0700201status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
202 audio_patch_handle_t *handle,
203 int delayMs)
204{
205 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
206}
207
208status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
209 int delayMs)
210{
211 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
212}
213
Eric Laurentb52c1522014-05-20 11:27:36 -0700214void AudioPolicyService::doOnAudioPatchListUpdate()
215{
216 Mutex::Autolock _l(mLock);
217 for (size_t i = 0; i < mNotificationClients.size(); i++) {
218 mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
219 }
220}
221
Eric Laurente1715a42014-05-20 11:30:42 -0700222status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
223 int delayMs)
224{
225 return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
226}
227
Eric Laurentb52c1522014-05-20 11:27:36 -0700228AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
229 const sp<IAudioPolicyServiceClient>& client,
230 uid_t uid)
231 : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
232{
233}
234
235AudioPolicyService::NotificationClient::~NotificationClient()
236{
237}
238
239void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
240{
241 sp<NotificationClient> keep(this);
242 sp<AudioPolicyService> service = mService.promote();
243 if (service != 0) {
244 service->removeNotificationClient(mUid);
245 }
246}
247
248void AudioPolicyService::NotificationClient::onAudioPortListUpdate()
249{
250 if (mAudioPolicyServiceClient != 0) {
251 mAudioPolicyServiceClient->onAudioPortListUpdate();
252 }
253}
254
255void AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
256{
257 if (mAudioPolicyServiceClient != 0) {
258 mAudioPolicyServiceClient->onAudioPatchListUpdate();
259 }
260}
Eric Laurent57dae992011-07-24 13:36:09 -0700261
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700263 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700264 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
267static bool tryLock(Mutex& mutex)
268{
269 bool locked = false;
270 for (int i = 0; i < kDumpLockRetries; ++i) {
271 if (mutex.tryLock() == NO_ERROR) {
272 locked = true;
273 break;
274 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800275 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 }
277 return locked;
278}
279
280status_t AudioPolicyService::dumpInternals(int fd)
281{
282 const size_t SIZE = 256;
283 char buffer[SIZE];
284 String8 result;
285
Eric Laurentdce54a12014-03-10 12:19:46 -0700286#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700287 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700288#else
289 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
290#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 result.append(buffer);
292 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
293 result.append(buffer);
294 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
295 result.append(buffer);
296
297 write(fd, result.string(), result.size());
298 return NO_ERROR;
299}
300
Glenn Kasten0f11b512014-01-31 16:18:54 -0800301status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302{
Glenn Kasten44deb052012-02-05 18:09:08 -0800303 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 dumpPermissionDenial(fd);
305 } else {
306 bool locked = tryLock(mLock);
307 if (!locked) {
308 String8 result(kDeadlockedString);
309 write(fd, result.string(), result.size());
310 }
311
312 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800313 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314 mAudioCommandThread->dump(fd);
315 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800316 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 mTonePlaybackThread->dump(fd);
318 }
319
Eric Laurentdce54a12014-03-10 12:19:46 -0700320#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700321 if (mpAudioPolicy) {
322 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700324#else
325 if (mAudioPolicyManager) {
326 mAudioPolicyManager->dump(fd);
327 }
328#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
330 if (locked) mLock.unlock();
331 }
332 return NO_ERROR;
333}
334
335status_t AudioPolicyService::dumpPermissionDenial(int fd)
336{
337 const size_t SIZE = 256;
338 char buffer[SIZE];
339 String8 result;
340 snprintf(buffer, SIZE, "Permission Denial: "
341 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
342 IPCThreadState::self()->getCallingPid(),
343 IPCThreadState::self()->getCallingUid());
344 result.append(buffer);
345 write(fd, result.string(), result.size());
346 return NO_ERROR;
347}
348
349status_t AudioPolicyService::onTransact(
350 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
351{
352 return BnAudioPolicyService::onTransact(code, data, reply, flags);
353}
354
355
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356// ----------- AudioPolicyService::AudioCommandThread implementation ----------
357
Eric Laurentbfb1b832013-01-07 09:53:42 -0800358AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
359 const wp<AudioPolicyService>& service)
360 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361{
362 mpToneGenerator = NULL;
363}
364
365
366AudioPolicyService::AudioCommandThread::~AudioCommandThread()
367{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800368 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 release_wake_lock(mName.string());
370 }
371 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800372 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373}
374
375void AudioPolicyService::AudioCommandThread::onFirstRef()
376{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800377 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378}
379
380bool AudioPolicyService::AudioCommandThread::threadLoop()
381{
382 nsecs_t waitTime = INT64_MAX;
383
384 mLock.lock();
385 while (!exitPending())
386 {
Eric Laurent59a89232014-06-08 14:14:17 -0700387 sp<AudioPolicyService> svc;
388 while (!mAudioCommands.isEmpty() && !exitPending()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 nsecs_t curTime = systemTime();
390 // commands are sorted by increasing time stamp: execute them from index 0 and up
391 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700392 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700394 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
396 switch (command->mCommand) {
397 case START_TONE: {
398 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700399 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800402 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
404 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 mLock.lock();
406 }break;
407 case STOP_TONE: {
408 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100409 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 if (mpToneGenerator != NULL) {
411 mpToneGenerator->stopTone();
412 delete mpToneGenerator;
413 mpToneGenerator = NULL;
414 }
415 mLock.lock();
416 }break;
417 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700418 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700420 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
421 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
422 data->mVolume,
423 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 }break;
425 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700426 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700427 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
428 data->mKeyValuePairs.string(), data->mIO);
429 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700430 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700432 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700434 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800437 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700438 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800439 ALOGV("AudioCommandThread() processing stop output %d",
440 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700441 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800442 if (svc == 0) {
443 break;
444 }
445 mLock.unlock();
446 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
447 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800448 }break;
449 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700450 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800451 ALOGV("AudioCommandThread() processing release output %d",
452 data->mIO);
Eric Laurent59a89232014-06-08 14:14:17 -0700453 svc = mService.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800454 if (svc == 0) {
455 break;
456 }
457 mLock.unlock();
458 svc->doReleaseOutput(data->mIO);
459 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800460 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700461 case CREATE_AUDIO_PATCH: {
462 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
463 ALOGV("AudioCommandThread() processing create 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->createAudioPatch(&data->mPatch, &data->mHandle);
469 }
470 } break;
471 case RELEASE_AUDIO_PATCH: {
472 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
473 ALOGV("AudioCommandThread() processing release audio patch");
474 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
475 if (af == 0) {
476 command->mStatus = PERMISSION_DENIED;
477 } else {
478 command->mStatus = af->releaseAudioPatch(data->mHandle);
479 }
480 } break;
Eric Laurentb52c1522014-05-20 11:27:36 -0700481 case UPDATE_AUDIOPORT_LIST: {
482 ALOGV("AudioCommandThread() processing update audio port 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->doOnAudioPortListUpdate();
489 mLock.lock();
490 }break;
491 case UPDATE_AUDIOPATCH_LIST: {
492 ALOGV("AudioCommandThread() processing update audio patch list");
Eric Laurent59a89232014-06-08 14:14:17 -0700493 svc = mService.promote();
Eric Laurentb52c1522014-05-20 11:27:36 -0700494 if (svc == 0) {
495 break;
496 }
497 mLock.unlock();
498 svc->doOnAudioPatchListUpdate();
499 mLock.lock();
500 }break;
Eric Laurente1715a42014-05-20 11:30:42 -0700501 case SET_AUDIOPORT_CONFIG: {
502 SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
503 ALOGV("AudioCommandThread() processing set port config");
504 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
505 if (af == 0) {
506 command->mStatus = PERMISSION_DENIED;
507 } else {
508 command->mStatus = af->setAudioPortConfig(&data->mConfig);
509 }
510 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000512 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700514 {
515 Mutex::Autolock _l(command->mLock);
516 if (command->mWaitStatus) {
517 command->mWaitStatus = false;
518 command->mCond.signal();
519 }
520 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700521 waitTime = INT64_MAX;
522 } else {
523 waitTime = mAudioCommands[0]->mTime - curTime;
524 break;
525 }
526 }
Eric Laurent59a89232014-06-08 14:14:17 -0700527 // release mLock before releasing strong reference on the service as
528 // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
529 mLock.unlock();
530 svc.clear();
531 mLock.lock();
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700532 if (!exitPending() && mAudioCommands.isEmpty()) {
533 // release delayed commands wake lock
534 release_wake_lock(mName.string());
Eric Laurent59a89232014-06-08 14:14:17 -0700535 ALOGV("AudioCommandThread() going to sleep");
536 mWaitWorkCV.waitRelative(mLock, waitTime);
537 ALOGV("AudioCommandThread() waking up");
538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 }
Ricardo Garcia05f2fdc2014-07-24 15:48:24 -0700540 // release delayed commands wake lock before quitting
541 if (!mAudioCommands.isEmpty()) {
542 release_wake_lock(mName.string());
543 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 mLock.unlock();
545 return false;
546}
547
548status_t AudioPolicyService::AudioCommandThread::dump(int fd)
549{
550 const size_t SIZE = 256;
551 char buffer[SIZE];
552 String8 result;
553
554 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
555 result.append(buffer);
556 write(fd, result.string(), result.size());
557
558 bool locked = tryLock(mLock);
559 if (!locked) {
560 String8 result2(kCmdDeadlockedString);
561 write(fd, result2.string(), result2.size());
562 }
563
564 snprintf(buffer, SIZE, "- Commands:\n");
565 result = String8(buffer);
566 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800567 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 mAudioCommands[i]->dump(buffer, SIZE);
569 result.append(buffer);
570 }
571 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700572 if (mLastCommand != 0) {
573 mLastCommand->dump(buffer, SIZE);
574 result.append(buffer);
575 } else {
576 result.append(" none\n");
577 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578
579 write(fd, result.string(), result.size());
580
581 if (locked) mLock.unlock();
582
583 return NO_ERROR;
584}
585
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800586void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
587 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588{
Eric Laurent0ede8922014-05-09 18:04:42 -0700589 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700591 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 data->mType = type;
593 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100594 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100595 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700596 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597}
598
599void AudioPolicyService::AudioCommandThread::stopToneCommand()
600{
Eric Laurent0ede8922014-05-09 18:04:42 -0700601 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100603 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700604 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605}
606
Glenn Kastenfff6d712012-01-12 16:38:12 -0800607status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700608 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800609 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700610 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611{
Eric Laurent0ede8922014-05-09 18:04:42 -0700612 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700614 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 data->mStream = stream;
616 data->mVolume = volume;
617 data->mIO = output;
618 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700619 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100620 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700621 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700622 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623}
624
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800625status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700626 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700627 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628{
Eric Laurent0ede8922014-05-09 18:04:42 -0700629 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700631 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700633 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700635 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100636 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700637 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700638 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639}
640
641status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
642{
Eric Laurent0ede8922014-05-09 18:04:42 -0700643 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700645 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 data->mVolume = volume;
647 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700648 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100649 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700650 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651}
652
Eric Laurentbfb1b832013-01-07 09:53:42 -0800653void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
654 audio_stream_type_t stream,
655 int session)
656{
Eric Laurent0ede8922014-05-09 18:04:42 -0700657 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800658 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700659 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800660 data->mIO = output;
661 data->mStream = stream;
662 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100663 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800664 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700665 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800666}
667
668void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
669{
Eric Laurent0ede8922014-05-09 18:04:42 -0700670 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800671 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700672 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800673 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100674 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800675 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700676 sendCommand(command);
677}
678
Eric Laurent951f4552014-05-20 10:48:17 -0700679status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
680 const struct audio_patch *patch,
681 audio_patch_handle_t *handle,
682 int delayMs)
683{
684 status_t status = NO_ERROR;
685
686 sp<AudioCommand> command = new AudioCommand();
687 command->mCommand = CREATE_AUDIO_PATCH;
688 CreateAudioPatchData *data = new CreateAudioPatchData();
689 data->mPatch = *patch;
690 data->mHandle = *handle;
691 command->mParam = data;
692 command->mWaitStatus = true;
693 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
694 status = sendCommand(command, delayMs);
695 if (status == NO_ERROR) {
696 *handle = data->mHandle;
697 }
698 return status;
699}
700
701status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
702 int delayMs)
703{
704 sp<AudioCommand> command = new AudioCommand();
705 command->mCommand = RELEASE_AUDIO_PATCH;
706 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
707 data->mHandle = handle;
708 command->mParam = data;
709 command->mWaitStatus = true;
710 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
711 return sendCommand(command, delayMs);
712}
713
Eric Laurentb52c1522014-05-20 11:27:36 -0700714void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
715{
716 sp<AudioCommand> command = new AudioCommand();
717 command->mCommand = UPDATE_AUDIOPORT_LIST;
718 ALOGV("AudioCommandThread() adding update audio port list");
719 sendCommand(command);
720}
721
722void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
723{
724 sp<AudioCommand>command = new AudioCommand();
725 command->mCommand = UPDATE_AUDIOPATCH_LIST;
726 ALOGV("AudioCommandThread() adding update audio patch list");
727 sendCommand(command);
728}
729
Eric Laurente1715a42014-05-20 11:30:42 -0700730status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
731 const struct audio_port_config *config, int delayMs)
732{
733 sp<AudioCommand> command = new AudioCommand();
734 command->mCommand = SET_AUDIOPORT_CONFIG;
735 SetAudioPortConfigData *data = new SetAudioPortConfigData();
736 data->mConfig = *config;
737 command->mParam = data;
738 command->mWaitStatus = true;
739 ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
740 return sendCommand(command, delayMs);
741}
742
Eric Laurent0ede8922014-05-09 18:04:42 -0700743status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
744{
745 {
746 Mutex::Autolock _l(mLock);
747 insertCommand_l(command, delayMs);
748 mWaitWorkCV.signal();
749 }
750 Mutex::Autolock _l(command->mLock);
751 while (command->mWaitStatus) {
752 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
753 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
754 command->mStatus = TIMED_OUT;
755 command->mWaitStatus = false;
756 }
757 }
758 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800759}
760
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700762void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800764 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700765 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 command->mTime = systemTime() + milliseconds(delayMs);
767
768 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800769 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
771 }
772
773 // check same pending commands with later time stamps and eliminate them
774 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700775 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
777 if (command2->mTime <= command->mTime) break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700778
779 // create audio patch or release audio patch commands are equivalent
780 // with regard to filtering
781 if ((command->mCommand == CREATE_AUDIO_PATCH) ||
782 (command->mCommand == RELEASE_AUDIO_PATCH)) {
783 if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
784 (command2->mCommand != RELEASE_AUDIO_PATCH)) {
785 continue;
786 }
787 } else if (command2->mCommand != command->mCommand) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788
789 switch (command->mCommand) {
790 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700791 ParametersData *data = (ParametersData *)command->mParam.get();
792 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700795 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 AudioParameter param = AudioParameter(data->mKeyValuePairs);
797 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
798 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700799 String8 key;
800 String8 value;
801 param.getAt(j, key, value);
802 for (size_t k = 0; k < param2.size(); k++) {
803 String8 key2;
804 String8 value2;
805 param2.getAt(k, key2, value2);
806 if (key2 == key) {
807 param2.remove(key2);
808 ALOGV("Filtering out parameter %s", key2.string());
809 break;
810 }
811 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 }
813 // if all keys have been filtered out, remove the command.
814 // otherwise, update the key value pairs
815 if (param2.size() == 0) {
816 removedCommands.add(command2);
817 } else {
818 data2->mKeyValuePairs = param2.toString();
819 }
Eric Laurent21e54562013-09-23 12:08:05 -0700820 command->mTime = command2->mTime;
821 // force delayMs to non 0 so that code below does not request to wait for
822 // command status as the command is now delayed
823 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 } break;
825
826 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700827 VolumeData *data = (VolumeData *)command->mParam.get();
828 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 if (data->mIO != data2->mIO) break;
830 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100831 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700832 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700834 command->mTime = command2->mTime;
835 // force delayMs to non 0 so that code below does not request to wait for
836 // command status as the command is now delayed
837 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 } break;
Eric Laurente45b48a2014-09-04 16:40:57 -0700839
840 case CREATE_AUDIO_PATCH:
841 case RELEASE_AUDIO_PATCH: {
842 audio_patch_handle_t handle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700843 struct audio_patch patch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700844 if (command->mCommand == CREATE_AUDIO_PATCH) {
845 handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700846 patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700847 } else {
848 handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
849 }
850 audio_patch_handle_t handle2;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700851 struct audio_patch patch2;
Eric Laurente45b48a2014-09-04 16:40:57 -0700852 if (command2->mCommand == CREATE_AUDIO_PATCH) {
853 handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700854 patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
Eric Laurente45b48a2014-09-04 16:40:57 -0700855 } else {
856 handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
857 }
858 if (handle != handle2) break;
Haynes Mathew Georgea2d4a6d2014-10-13 13:05:22 -0700859 /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
860 same output. */
861 if( (command->mCommand == CREATE_AUDIO_PATCH) &&
862 (command2->mCommand == CREATE_AUDIO_PATCH) ) {
863 bool isOutputDiff = false;
864 if (patch.num_sources == patch2.num_sources) {
865 for (unsigned count = 0; count < patch.num_sources; count++) {
866 if (patch.sources[count].id != patch2.sources[count].id) {
867 isOutputDiff = true;
868 break;
869 }
870 }
871 if (isOutputDiff)
872 break;
873 }
874 }
Eric Laurente45b48a2014-09-04 16:40:57 -0700875 ALOGV("Filtering out %s audio patch command for handle %d",
876 (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
877 removedCommands.add(command2);
878 command->mTime = command2->mTime;
879 // force delayMs to non 0 so that code below does not request to wait for
880 // command status as the command is now delayed
881 delayMs = 1;
882 } break;
883
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 case START_TONE:
885 case STOP_TONE:
886 default:
887 break;
888 }
889 }
890
891 // remove filtered commands
892 for (size_t j = 0; j < removedCommands.size(); j++) {
893 // removed commands always have time stamps greater than current command
894 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700895 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100896 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 mAudioCommands.removeAt(k);
898 break;
899 }
900 }
901 }
902 removedCommands.clear();
903
Eric Laurent0ede8922014-05-09 18:04:42 -0700904 // Disable wait for status if delay is not 0
905 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700906 command->mWaitStatus = false;
907 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700908
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 // insert command at the right place according to its time stamp
Eric Laurent1e693b52014-07-09 15:03:28 -0700910 ALOGV("inserting command: %d at index %zd, num commands %zu",
911 command->mCommand, i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 mAudioCommands.insertAt(command, i + 1);
913}
914
915void AudioPolicyService::AudioCommandThread::exit()
916{
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 {
919 AutoMutex _l(mLock);
920 requestExit();
921 mWaitWorkCV.signal();
922 }
923 requestExitAndWait();
924}
925
926void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
927{
928 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
929 mCommand,
930 (int)ns2s(mTime),
931 (int)ns2ms(mTime)%1000,
932 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700933 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934}
935
Dima Zavinfce7a472011-04-19 22:30:36 -0700936/******* helpers for the service_ops callbacks defined below *********/
937void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
938 const char *keyValuePairs,
939 int delayMs)
940{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800941 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700942 delayMs);
943}
944
945int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
946 float volume,
947 audio_io_handle_t output,
948 int delayMs)
949{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800950 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800951 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700952}
953
954int AudioPolicyService::startTone(audio_policy_tone_t tone,
955 audio_stream_type_t stream)
956{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700957 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000958 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700959 }
960 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000961 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700962 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700963 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
965 AUDIO_STREAM_VOICE_CALL);
966 return 0;
967}
968
969int AudioPolicyService::stopTone()
970{
971 mTonePlaybackThread->stopToneCommand();
972 return 0;
973}
974
975int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
976{
977 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
978}
979
Dima Zavinfce7a472011-04-19 22:30:36 -0700980extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800981audio_module_handle_t aps_load_hw_module(void *service __unused,
982 const char *name);
983audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700984 audio_devices_t *pDevices,
985 uint32_t *pSamplingRate,
986 audio_format_t *pFormat,
987 audio_channel_mask_t *pChannelMask,
988 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800989 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -0700990
Eric Laurent2d388ec2014-03-07 13:25:54 -0800991audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -0700992 audio_module_handle_t module,
993 audio_devices_t *pDevices,
994 uint32_t *pSamplingRate,
995 audio_format_t *pFormat,
996 audio_channel_mask_t *pChannelMask,
997 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000998 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800999 const audio_offload_info_t *offloadInfo);
1000audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001001 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001002 audio_io_handle_t output2);
1003int aps_close_output(void *service __unused, audio_io_handle_t output);
1004int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1005int aps_restore_output(void *service __unused, audio_io_handle_t output);
1006audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001007 audio_devices_t *pDevices,
1008 uint32_t *pSamplingRate,
1009 audio_format_t *pFormat,
1010 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001011 audio_in_acoustics_t acoustics __unused);
1012audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001013 audio_module_handle_t module,
1014 audio_devices_t *pDevices,
1015 uint32_t *pSamplingRate,
1016 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001017 audio_channel_mask_t *pChannelMask);
1018int aps_close_input(void *service __unused, audio_io_handle_t input);
1019int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1020int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001021 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001022 audio_io_handle_t dst_output);
1023char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1024 const char *keys);
1025void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1026 const char *kv_pairs, int delay_ms);
1027int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001028 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001029 int delay_ms);
1030int aps_start_tone(void *service, audio_policy_tone_t tone,
1031 audio_stream_type_t stream);
1032int aps_stop_tone(void *service);
1033int aps_set_voice_volume(void *service, float volume, int delay_ms);
1034};
Dima Zavinfce7a472011-04-19 22:30:36 -07001035
1036namespace {
1037 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001038 .open_output = aps_open_output,
1039 .open_duplicate_output = aps_open_dup_output,
1040 .close_output = aps_close_output,
1041 .suspend_output = aps_suspend_output,
1042 .restore_output = aps_restore_output,
1043 .open_input = aps_open_input,
1044 .close_input = aps_close_input,
1045 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001046 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001047 .set_parameters = aps_set_parameters,
1048 .get_parameters = aps_get_parameters,
1049 .start_tone = aps_start_tone,
1050 .stop_tone = aps_stop_tone,
1051 .set_voice_volume = aps_set_voice_volume,
1052 .move_effects = aps_move_effects,
1053 .load_hw_module = aps_load_hw_module,
1054 .open_output_on_module = aps_open_output_on_module,
1055 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001056 };
1057}; // namespace <unnamed>
1058
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059}; // namespace android