Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 20 | #include "Configuration.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 21 | #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 Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 34 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | #include <hardware_legacy/power.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 36 | #include <media/AudioEffect.h> |
| 37 | #include <media/EffectsFactoryApi.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 39 | #include <hardware/hardware.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 40 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 41 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 42 | #include <hardware/audio_policy.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 43 | #include <audio_effects/audio_effects_conf.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 44 | #include <media/AudioParameter.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | namespace android { |
| 47 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 48 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 49 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | |
| 51 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 52 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | |
Glenn Kasten | 1b8ae3d | 2013-07-19 10:32:41 -0700 | [diff] [blame] | 54 | static const nsecs_t kAudioCommandTimeout = 3000000000LL; // 3 seconds |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 55 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 56 | namespace { |
| 57 | extern struct audio_policy_service_ops aps_ops; |
| 58 | }; |
| 59 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | AudioPolicyService::AudioPolicyService() |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 63 | : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL), |
| 64 | mAudioPolicyManager(NULL), mAudioPolicyClient(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 65 | { |
| 66 | char value[PROPERTY_VALUE_MAX]; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 67 | const struct hw_module_t *module; |
| 68 | int forced_val; |
| 69 | int rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 71 | Mutex::Autolock _l(mLock); |
| 72 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 73 | // start tone playback thread |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 74 | mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | // start audio commands thread |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 76 | mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); |
| 77 | // start output activity command thread |
| 78 | mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 79 | |
| 80 | #ifdef USE_LEGACY_AUDIO_POLICY |
| 81 | ALOGI("AudioPolicyService CSTOR in legacy mode"); |
| 82 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 83 | /* instantiate the audio policy manager */ |
| 84 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 85 | if (rc) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 86 | return; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 87 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 88 | rc = audio_policy_dev_open(module, &mpAudioPolicyDev); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 89 | ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 90 | if (rc) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 91 | return; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 92 | } |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 93 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 94 | rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, |
| 95 | &mpAudioPolicy); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 96 | ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 97 | if (rc) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 98 | return; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 99 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 100 | |
| 101 | rc = mpAudioPolicy->init_check(mpAudioPolicy); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 102 | ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 103 | if (rc) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 104 | return; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 105 | } |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 106 | ALOGI("Loaded audio policy from %s (%s)", module->name, module->id); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 107 | #else |
| 108 | ALOGI("AudioPolicyService CSTOR in new mode"); |
| 109 | |
| 110 | mAudioPolicyClient = new AudioPolicyClient(this); |
| 111 | mAudioPolicyManager = new AudioPolicyManager(mAudioPolicyClient); |
| 112 | #endif |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 113 | |
| 114 | // load audio pre processing modules |
| 115 | if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { |
| 116 | loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); |
| 117 | } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { |
| 118 | loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); |
| 119 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | AudioPolicyService::~AudioPolicyService() |
| 123 | { |
| 124 | mTonePlaybackThread->exit(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | mAudioCommandThread->exit(); |
Eric Laurent | 657ff61 | 2014-05-07 11:58:24 -0700 | [diff] [blame^] | 126 | mOutputCommandThread->exit(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 127 | |
| 128 | // release audio pre processing resources |
| 129 | for (size_t i = 0; i < mInputSources.size(); i++) { |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 130 | delete mInputSources.valueAt(i); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 131 | } |
| 132 | mInputSources.clear(); |
| 133 | |
| 134 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 135 | mInputs.valueAt(i)->mEffects.clear(); |
| 136 | delete mInputs.valueAt(i); |
| 137 | } |
| 138 | mInputs.clear(); |
| 139 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 140 | #ifdef USE_LEGACY_AUDIO_POLICY |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 141 | if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 142 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 143 | } |
| 144 | if (mpAudioPolicyDev != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | audio_policy_dev_close(mpAudioPolicyDev); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 146 | } |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 147 | #else |
| 148 | delete mAudioPolicyManager; |
| 149 | delete mAudioPolicyClient; |
| 150 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 153 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 154 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 155 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 156 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static bool tryLock(Mutex& mutex) |
| 160 | { |
| 161 | bool locked = false; |
| 162 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 163 | if (mutex.tryLock() == NO_ERROR) { |
| 164 | locked = true; |
| 165 | break; |
| 166 | } |
Glenn Kasten | 22ecc91 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 167 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | } |
| 169 | return locked; |
| 170 | } |
| 171 | |
| 172 | status_t AudioPolicyService::dumpInternals(int fd) |
| 173 | { |
| 174 | const size_t SIZE = 256; |
| 175 | char buffer[SIZE]; |
| 176 | String8 result; |
| 177 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 178 | #ifdef USE_LEGACY_AUDIO_POLICY |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 179 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 180 | #else |
| 181 | snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager); |
| 182 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 183 | result.append(buffer); |
| 184 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 185 | result.append(buffer); |
| 186 | snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); |
| 187 | result.append(buffer); |
| 188 | |
| 189 | write(fd, result.string(), result.size()); |
| 190 | return NO_ERROR; |
| 191 | } |
| 192 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 193 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 194 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 195 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | dumpPermissionDenial(fd); |
| 197 | } else { |
| 198 | bool locked = tryLock(mLock); |
| 199 | if (!locked) { |
| 200 | String8 result(kDeadlockedString); |
| 201 | write(fd, result.string(), result.size()); |
| 202 | } |
| 203 | |
| 204 | dumpInternals(fd); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 205 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 206 | mAudioCommandThread->dump(fd); |
| 207 | } |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 208 | if (mTonePlaybackThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 209 | mTonePlaybackThread->dump(fd); |
| 210 | } |
| 211 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 212 | #ifdef USE_LEGACY_AUDIO_POLICY |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 213 | if (mpAudioPolicy) { |
| 214 | mpAudioPolicy->dump(mpAudioPolicy, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 215 | } |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 216 | #else |
| 217 | if (mAudioPolicyManager) { |
| 218 | mAudioPolicyManager->dump(fd); |
| 219 | } |
| 220 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 221 | |
| 222 | if (locked) mLock.unlock(); |
| 223 | } |
| 224 | return NO_ERROR; |
| 225 | } |
| 226 | |
| 227 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 228 | { |
| 229 | const size_t SIZE = 256; |
| 230 | char buffer[SIZE]; |
| 231 | String8 result; |
| 232 | snprintf(buffer, SIZE, "Permission Denial: " |
| 233 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 234 | IPCThreadState::self()->getCallingPid(), |
| 235 | IPCThreadState::self()->getCallingUid()); |
| 236 | result.append(buffer); |
| 237 | write(fd, result.string(), result.size()); |
| 238 | return NO_ERROR; |
| 239 | } |
| 240 | |
Glenn Kasten | 81872a2 | 2012-03-07 16:49:22 -0800 | [diff] [blame] | 241 | void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 242 | { |
Glenn Kasten | 81872a2 | 2012-03-07 16:49:22 -0800 | [diff] [blame] | 243 | const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 244 | for (size_t i = 0; i < fxVector.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 245 | fxVector.itemAt(i)->setEnabled(enabled); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | status_t AudioPolicyService::onTransact( |
| 250 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 251 | { |
| 252 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 253 | } |
| 254 | |
| 255 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 256 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 257 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 258 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, |
| 259 | const wp<AudioPolicyService>& service) |
| 260 | : Thread(false), mName(name), mService(service) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 261 | { |
| 262 | mpToneGenerator = NULL; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 267 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 268 | if (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 269 | release_wake_lock(mName.string()); |
| 270 | } |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 271 | for (size_t k=0; k < mAudioCommands.size(); k++) { |
| 272 | delete mAudioCommands[k]->mParam; |
| 273 | delete mAudioCommands[k]; |
| 274 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 275 | mAudioCommands.clear(); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 276 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 280 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 281 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 285 | { |
| 286 | nsecs_t waitTime = INT64_MAX; |
| 287 | |
| 288 | mLock.lock(); |
| 289 | while (!exitPending()) |
| 290 | { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 291 | while (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 292 | nsecs_t curTime = systemTime(); |
| 293 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 294 | if (mAudioCommands[0]->mTime <= curTime) { |
| 295 | AudioCommand *command = mAudioCommands[0]; |
| 296 | mAudioCommands.removeAt(0); |
| 297 | mLastCommand = *command; |
| 298 | |
| 299 | switch (command->mCommand) { |
| 300 | case START_TONE: { |
| 301 | mLock.unlock(); |
| 302 | ToneData *data = (ToneData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 303 | ALOGV("AudioCommandThread() processing start tone %d on stream %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 304 | data->mType, data->mStream); |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 305 | delete mpToneGenerator; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 306 | mpToneGenerator = new ToneGenerator(data->mStream, 1.0); |
| 307 | mpToneGenerator->startTone(data->mType); |
| 308 | delete data; |
| 309 | mLock.lock(); |
| 310 | }break; |
| 311 | case STOP_TONE: { |
| 312 | mLock.unlock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 313 | ALOGV("AudioCommandThread() processing stop tone"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 314 | if (mpToneGenerator != NULL) { |
| 315 | mpToneGenerator->stopTone(); |
| 316 | delete mpToneGenerator; |
| 317 | mpToneGenerator = NULL; |
| 318 | } |
| 319 | mLock.lock(); |
| 320 | }break; |
| 321 | case SET_VOLUME: { |
| 322 | VolumeData *data = (VolumeData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 323 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 324 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
| 325 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 326 | data->mVolume, |
| 327 | data->mIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 328 | if (command->mWaitStatus) { |
| 329 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 330 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 331 | } |
| 332 | delete data; |
| 333 | }break; |
| 334 | case SET_PARAMETERS: { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 335 | ParametersData *data = (ParametersData *)command->mParam; |
| 336 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 337 | data->mKeyValuePairs.string(), data->mIO); |
| 338 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
| 339 | if (command->mWaitStatus) { |
| 340 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 341 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 342 | } |
| 343 | delete data; |
| 344 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | case SET_VOICE_VOLUME: { |
| 346 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 347 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 348 | data->mVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 349 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
| 350 | if (command->mWaitStatus) { |
| 351 | command->mCond.signal(); |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 352 | command->mCond.waitRelative(mLock, kAudioCommandTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | } |
| 354 | delete data; |
| 355 | }break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 356 | case STOP_OUTPUT: { |
| 357 | StopOutputData *data = (StopOutputData *)command->mParam; |
| 358 | ALOGV("AudioCommandThread() processing stop output %d", |
| 359 | data->mIO); |
| 360 | sp<AudioPolicyService> svc = mService.promote(); |
| 361 | if (svc == 0) { |
| 362 | break; |
| 363 | } |
| 364 | mLock.unlock(); |
| 365 | svc->doStopOutput(data->mIO, data->mStream, data->mSession); |
| 366 | mLock.lock(); |
| 367 | delete data; |
| 368 | }break; |
| 369 | case RELEASE_OUTPUT: { |
| 370 | ReleaseOutputData *data = (ReleaseOutputData *)command->mParam; |
| 371 | ALOGV("AudioCommandThread() processing release output %d", |
| 372 | data->mIO); |
| 373 | sp<AudioPolicyService> svc = mService.promote(); |
| 374 | if (svc == 0) { |
| 375 | break; |
| 376 | } |
| 377 | mLock.unlock(); |
| 378 | svc->doReleaseOutput(data->mIO); |
| 379 | mLock.lock(); |
| 380 | delete data; |
| 381 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 382 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 383 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 384 | } |
| 385 | delete command; |
| 386 | waitTime = INT64_MAX; |
| 387 | } else { |
| 388 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | // release delayed commands wake lock |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 393 | if (mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 394 | release_wake_lock(mName.string()); |
| 395 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 396 | ALOGV("AudioCommandThread() going to sleep"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 397 | mWaitWorkCV.waitRelative(mLock, waitTime); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 398 | ALOGV("AudioCommandThread() waking up"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 399 | } |
| 400 | mLock.unlock(); |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 405 | { |
| 406 | const size_t SIZE = 256; |
| 407 | char buffer[SIZE]; |
| 408 | String8 result; |
| 409 | |
| 410 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 411 | result.append(buffer); |
| 412 | write(fd, result.string(), result.size()); |
| 413 | |
| 414 | bool locked = tryLock(mLock); |
| 415 | if (!locked) { |
| 416 | String8 result2(kCmdDeadlockedString); |
| 417 | write(fd, result2.string(), result2.size()); |
| 418 | } |
| 419 | |
| 420 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 421 | result = String8(buffer); |
| 422 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 423 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 424 | mAudioCommands[i]->dump(buffer, SIZE); |
| 425 | result.append(buffer); |
| 426 | } |
| 427 | result.append(" Last Command\n"); |
| 428 | mLastCommand.dump(buffer, SIZE); |
| 429 | result.append(buffer); |
| 430 | |
| 431 | write(fd, result.string(), result.size()); |
| 432 | |
| 433 | if (locked) mLock.unlock(); |
| 434 | |
| 435 | return NO_ERROR; |
| 436 | } |
| 437 | |
Glenn Kasten | 3d2f877 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 438 | void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type, |
| 439 | audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 440 | { |
| 441 | AudioCommand *command = new AudioCommand(); |
| 442 | command->mCommand = START_TONE; |
| 443 | ToneData *data = new ToneData(); |
| 444 | data->mType = type; |
| 445 | data->mStream = stream; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 446 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 447 | Mutex::Autolock _l(mLock); |
| 448 | insertCommand_l(command); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 449 | ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 450 | mWaitWorkCV.signal(); |
| 451 | } |
| 452 | |
| 453 | void AudioPolicyService::AudioCommandThread::stopToneCommand() |
| 454 | { |
| 455 | AudioCommand *command = new AudioCommand(); |
| 456 | command->mCommand = STOP_TONE; |
| 457 | command->mParam = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 458 | Mutex::Autolock _l(mLock); |
| 459 | insertCommand_l(command); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 460 | ALOGV("AudioCommandThread() adding tone stop"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 | mWaitWorkCV.signal(); |
| 462 | } |
| 463 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 464 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 465 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 466 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 467 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 468 | { |
| 469 | status_t status = NO_ERROR; |
| 470 | |
| 471 | AudioCommand *command = new AudioCommand(); |
| 472 | command->mCommand = SET_VOLUME; |
| 473 | VolumeData *data = new VolumeData(); |
| 474 | data->mStream = stream; |
| 475 | data->mVolume = volume; |
| 476 | data->mIO = output; |
| 477 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 478 | Mutex::Autolock _l(mLock); |
| 479 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 480 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 481 | stream, volume, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 482 | mWaitWorkCV.signal(); |
| 483 | if (command->mWaitStatus) { |
| 484 | command->mCond.wait(mLock); |
| 485 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 486 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 487 | } |
| 488 | return status; |
| 489 | } |
| 490 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 491 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 492 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 493 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 494 | { |
| 495 | status_t status = NO_ERROR; |
| 496 | |
| 497 | AudioCommand *command = new AudioCommand(); |
| 498 | command->mCommand = SET_PARAMETERS; |
| 499 | ParametersData *data = new ParametersData(); |
| 500 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 501 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 502 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 503 | Mutex::Autolock _l(mLock); |
| 504 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 505 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 506 | keyValuePairs, ioHandle, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 507 | mWaitWorkCV.signal(); |
| 508 | if (command->mWaitStatus) { |
| 509 | command->mCond.wait(mLock); |
| 510 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 511 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 512 | } |
| 513 | return status; |
| 514 | } |
| 515 | |
| 516 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 517 | { |
| 518 | status_t status = NO_ERROR; |
| 519 | |
| 520 | AudioCommand *command = new AudioCommand(); |
| 521 | command->mCommand = SET_VOICE_VOLUME; |
| 522 | VoiceVolumeData *data = new VoiceVolumeData(); |
| 523 | data->mVolume = volume; |
| 524 | command->mParam = data; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 525 | Mutex::Autolock _l(mLock); |
| 526 | insertCommand_l(command, delayMs); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 527 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 528 | mWaitWorkCV.signal(); |
| 529 | if (command->mWaitStatus) { |
| 530 | command->mCond.wait(mLock); |
| 531 | status = command->mStatus; |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 532 | command->mCond.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 533 | } |
| 534 | return status; |
| 535 | } |
| 536 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 537 | void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output, |
| 538 | audio_stream_type_t stream, |
| 539 | int session) |
| 540 | { |
| 541 | AudioCommand *command = new AudioCommand(); |
| 542 | command->mCommand = STOP_OUTPUT; |
| 543 | StopOutputData *data = new StopOutputData(); |
| 544 | data->mIO = output; |
| 545 | data->mStream = stream; |
| 546 | data->mSession = session; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 547 | command->mParam = data; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 548 | Mutex::Autolock _l(mLock); |
| 549 | insertCommand_l(command); |
| 550 | ALOGV("AudioCommandThread() adding stop output %d", output); |
| 551 | mWaitWorkCV.signal(); |
| 552 | } |
| 553 | |
| 554 | void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output) |
| 555 | { |
| 556 | AudioCommand *command = new AudioCommand(); |
| 557 | command->mCommand = RELEASE_OUTPUT; |
| 558 | ReleaseOutputData *data = new ReleaseOutputData(); |
| 559 | data->mIO = output; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 560 | command->mParam = data; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 561 | Mutex::Autolock _l(mLock); |
| 562 | insertCommand_l(command); |
| 563 | ALOGV("AudioCommandThread() adding release output %d", output); |
| 564 | mWaitWorkCV.signal(); |
| 565 | } |
| 566 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 567 | // insertCommand_l() must be called with mLock held |
| 568 | void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) |
| 569 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 570 | ssize_t i; // not size_t because i will count down to -1 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 571 | Vector <AudioCommand *> removedCommands; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 572 | command->mTime = systemTime() + milliseconds(delayMs); |
| 573 | |
| 574 | // acquire wake lock to make sure delayed commands are processed |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 575 | if (mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 576 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 577 | } |
| 578 | |
| 579 | // check same pending commands with later time stamps and eliminate them |
| 580 | for (i = mAudioCommands.size()-1; i >= 0; i--) { |
| 581 | AudioCommand *command2 = mAudioCommands[i]; |
| 582 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 583 | if (command2->mTime <= command->mTime) break; |
| 584 | if (command2->mCommand != command->mCommand) continue; |
| 585 | |
| 586 | switch (command->mCommand) { |
| 587 | case SET_PARAMETERS: { |
| 588 | ParametersData *data = (ParametersData *)command->mParam; |
| 589 | ParametersData *data2 = (ParametersData *)command2->mParam; |
| 590 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 591 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 592 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 593 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 594 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 595 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 596 | String8 key; |
| 597 | String8 value; |
| 598 | param.getAt(j, key, value); |
| 599 | for (size_t k = 0; k < param2.size(); k++) { |
| 600 | String8 key2; |
| 601 | String8 value2; |
| 602 | param2.getAt(k, key2, value2); |
| 603 | if (key2 == key) { |
| 604 | param2.remove(key2); |
| 605 | ALOGV("Filtering out parameter %s", key2.string()); |
| 606 | break; |
| 607 | } |
| 608 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 609 | } |
| 610 | // if all keys have been filtered out, remove the command. |
| 611 | // otherwise, update the key value pairs |
| 612 | if (param2.size() == 0) { |
| 613 | removedCommands.add(command2); |
| 614 | } else { |
| 615 | data2->mKeyValuePairs = param2.toString(); |
| 616 | } |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 617 | command->mTime = command2->mTime; |
| 618 | // force delayMs to non 0 so that code below does not request to wait for |
| 619 | // command status as the command is now delayed |
| 620 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 621 | } break; |
| 622 | |
| 623 | case SET_VOLUME: { |
| 624 | VolumeData *data = (VolumeData *)command->mParam; |
| 625 | VolumeData *data2 = (VolumeData *)command2->mParam; |
| 626 | if (data->mIO != data2->mIO) break; |
| 627 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 628 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 629 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 630 | removedCommands.add(command2); |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 631 | command->mTime = command2->mTime; |
| 632 | // force delayMs to non 0 so that code below does not request to wait for |
| 633 | // command status as the command is now delayed |
| 634 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 635 | } break; |
| 636 | case START_TONE: |
| 637 | case STOP_TONE: |
| 638 | default: |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | // remove filtered commands |
| 644 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 645 | // removed commands always have time stamps greater than current command |
| 646 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
| 647 | if (mAudioCommands[k] == removedCommands[j]) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 648 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 649 | // for commands that are not filtered, |
| 650 | // command->mParam is deleted in threadLoop |
| 651 | delete mAudioCommands[k]->mParam; |
| 652 | delete mAudioCommands[k]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 653 | mAudioCommands.removeAt(k); |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | removedCommands.clear(); |
| 659 | |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 660 | // wait for status only if delay is 0 |
| 661 | if (delayMs == 0) { |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 662 | command->mWaitStatus = true; |
| 663 | } else { |
| 664 | command->mWaitStatus = false; |
| 665 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 666 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 667 | // insert command at the right place according to its time stamp |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 668 | ALOGV("inserting command: %d at index %d, num commands %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 669 | command->mCommand, (int)i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 670 | mAudioCommands.insertAt(command, i + 1); |
| 671 | } |
| 672 | |
| 673 | void AudioPolicyService::AudioCommandThread::exit() |
| 674 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 675 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 676 | { |
| 677 | AutoMutex _l(mLock); |
| 678 | requestExit(); |
| 679 | mWaitWorkCV.signal(); |
| 680 | } |
| 681 | requestExitAndWait(); |
| 682 | } |
| 683 | |
| 684 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 685 | { |
| 686 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 687 | mCommand, |
| 688 | (int)ns2s(mTime), |
| 689 | (int)ns2ms(mTime)%1000, |
| 690 | mWaitStatus, |
| 691 | mParam); |
| 692 | } |
| 693 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 694 | /******* helpers for the service_ops callbacks defined below *********/ |
| 695 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 696 | const char *keyValuePairs, |
| 697 | int delayMs) |
| 698 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 699 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 700 | delayMs); |
| 701 | } |
| 702 | |
| 703 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 704 | float volume, |
| 705 | audio_io_handle_t output, |
| 706 | int delayMs) |
| 707 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 708 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 709 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | int AudioPolicyService::startTone(audio_policy_tone_t tone, |
| 713 | audio_stream_type_t stream) |
| 714 | { |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 715 | if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 716 | ALOGE("startTone: illegal tone requested (%d)", tone); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 717 | } |
| 718 | if (stream != AUDIO_STREAM_VOICE_CALL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 719 | ALOGE("startTone: illegal stream (%d) requested for tone %d", stream, |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 720 | tone); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 721 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 722 | mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, |
| 723 | AUDIO_STREAM_VOICE_CALL); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | int AudioPolicyService::stopTone() |
| 728 | { |
| 729 | mTonePlaybackThread->stopToneCommand(); |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 734 | { |
| 735 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 736 | } |
| 737 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 738 | // ---------------------------------------------------------------------------- |
| 739 | // Audio pre-processing configuration |
| 740 | // ---------------------------------------------------------------------------- |
| 741 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 742 | /*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 743 | MIC_SRC_TAG, |
| 744 | VOICE_UL_SRC_TAG, |
| 745 | VOICE_DL_SRC_TAG, |
| 746 | VOICE_CALL_SRC_TAG, |
| 747 | CAMCORDER_SRC_TAG, |
| 748 | VOICE_REC_SRC_TAG, |
| 749 | VOICE_COMM_SRC_TAG |
| 750 | }; |
| 751 | |
| 752 | // returns the audio_source_t enum corresponding to the input source name or |
| 753 | // AUDIO_SOURCE_CNT is no match found |
| 754 | audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) |
| 755 | { |
| 756 | int i; |
| 757 | for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { |
| 758 | if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 759 | ALOGV("inputSourceNameToEnum found source %s %d", name, i); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | } |
| 763 | return (audio_source_t)i; |
| 764 | } |
| 765 | |
| 766 | size_t AudioPolicyService::growParamSize(char *param, |
| 767 | size_t size, |
| 768 | size_t *curSize, |
| 769 | size_t *totSize) |
| 770 | { |
| 771 | // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) |
| 772 | size_t pos = ((*curSize - 1 ) / size + 1) * size; |
| 773 | |
| 774 | if (pos + size > *totSize) { |
| 775 | while (pos + size > *totSize) { |
| 776 | *totSize += ((*totSize + 7) / 8) * 4; |
| 777 | } |
| 778 | param = (char *)realloc(param, *totSize); |
| 779 | } |
| 780 | *curSize = pos + size; |
| 781 | return pos; |
| 782 | } |
| 783 | |
| 784 | size_t AudioPolicyService::readParamValue(cnode *node, |
| 785 | char *param, |
| 786 | size_t *curSize, |
| 787 | size_t *totSize) |
| 788 | { |
| 789 | if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { |
| 790 | size_t pos = growParamSize(param, sizeof(short), curSize, totSize); |
| 791 | *(short *)((char *)param + pos) = (short)atoi(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 792 | ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 793 | return sizeof(short); |
| 794 | } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { |
| 795 | size_t pos = growParamSize(param, sizeof(int), curSize, totSize); |
| 796 | *(int *)((char *)param + pos) = atoi(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 797 | ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 798 | return sizeof(int); |
| 799 | } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { |
| 800 | size_t pos = growParamSize(param, sizeof(float), curSize, totSize); |
| 801 | *(float *)((char *)param + pos) = (float)atof(node->value); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 802 | ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 803 | return sizeof(float); |
| 804 | } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { |
| 805 | size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); |
| 806 | if (strncmp(node->value, "false", strlen("false") + 1) == 0) { |
| 807 | *(bool *)((char *)param + pos) = false; |
| 808 | } else { |
| 809 | *(bool *)((char *)param + pos) = true; |
| 810 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 811 | ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 812 | return sizeof(bool); |
| 813 | } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { |
| 814 | size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); |
| 815 | if (*curSize + len + 1 > *totSize) { |
| 816 | *totSize = *curSize + len + 1; |
| 817 | param = (char *)realloc(param, *totSize); |
| 818 | } |
| 819 | strncpy(param + *curSize, node->value, len); |
| 820 | *curSize += len; |
| 821 | param[*curSize] = '\0'; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 822 | ALOGV("readParamValue() reading string %s", param + *curSize - len); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 823 | return len; |
| 824 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 825 | ALOGW("readParamValue() unknown param type %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) |
| 830 | { |
| 831 | cnode *param; |
| 832 | cnode *value; |
| 833 | size_t curSize = sizeof(effect_param_t); |
| 834 | size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); |
| 835 | effect_param_t *fx_param = (effect_param_t *)malloc(totSize); |
| 836 | |
| 837 | param = config_find(root, PARAM_TAG); |
| 838 | value = config_find(root, VALUE_TAG); |
| 839 | if (param == NULL && value == NULL) { |
| 840 | // try to parse simple parameter form {int int} |
| 841 | param = root->first_child; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 842 | if (param != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 843 | // Note: that a pair of random strings is read as 0 0 |
| 844 | int *ptr = (int *)fx_param->data; |
| 845 | int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 846 | ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 847 | *ptr++ = atoi(param->name); |
| 848 | *ptr = atoi(param->value); |
| 849 | fx_param->psize = sizeof(int); |
| 850 | fx_param->vsize = sizeof(int); |
| 851 | return fx_param; |
| 852 | } |
| 853 | } |
| 854 | if (param == NULL || value == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 855 | ALOGW("loadEffectParameter() invalid parameter description %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 856 | goto error; |
| 857 | } |
| 858 | |
| 859 | fx_param->psize = 0; |
| 860 | param = param->first_child; |
| 861 | while (param) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 862 | ALOGV("loadEffectParameter() reading param of type %s", param->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 863 | size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); |
| 864 | if (size == 0) { |
| 865 | goto error; |
| 866 | } |
| 867 | fx_param->psize += size; |
| 868 | param = param->next; |
| 869 | } |
| 870 | |
| 871 | // align start of value field on 32 bit boundary |
| 872 | curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); |
| 873 | |
| 874 | fx_param->vsize = 0; |
| 875 | value = value->first_child; |
| 876 | while (value) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 877 | ALOGV("loadEffectParameter() reading value of type %s", value->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 878 | size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); |
| 879 | if (size == 0) { |
| 880 | goto error; |
| 881 | } |
| 882 | fx_param->vsize += size; |
| 883 | value = value->next; |
| 884 | } |
| 885 | |
| 886 | return fx_param; |
| 887 | |
| 888 | error: |
You Kim | db358af | 2013-09-12 20:48:08 +0900 | [diff] [blame] | 889 | free(fx_param); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 890 | return NULL; |
| 891 | } |
| 892 | |
| 893 | void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) |
| 894 | { |
| 895 | cnode *node = root->first_child; |
| 896 | while (node) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 897 | ALOGV("loadEffectParameters() loading param %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 898 | effect_param_t *param = loadEffectParameter(node); |
| 899 | if (param == NULL) { |
| 900 | node = node->next; |
| 901 | continue; |
| 902 | } |
| 903 | params.add(param); |
| 904 | node = node->next; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( |
| 909 | cnode *root, |
| 910 | const Vector <EffectDesc *>& effects) |
| 911 | { |
| 912 | cnode *node = root->first_child; |
| 913 | if (node == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 914 | ALOGW("loadInputSource() empty element %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 915 | return NULL; |
| 916 | } |
| 917 | InputSourceDesc *source = new InputSourceDesc(); |
| 918 | while (node) { |
| 919 | size_t i; |
| 920 | for (i = 0; i < effects.size(); i++) { |
| 921 | if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 922 | ALOGV("loadInputSource() found effect %s in list", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | } |
| 926 | if (i == effects.size()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 927 | ALOGV("loadInputSource() effect %s not in list", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 928 | node = node->next; |
| 929 | continue; |
| 930 | } |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 931 | EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 932 | loadEffectParameters(node, effect->mParams); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 933 | ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 934 | source->mEffects.add(effect); |
| 935 | node = node->next; |
| 936 | } |
| 937 | if (source->mEffects.size() == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 938 | ALOGW("loadInputSource() no valid effects found in source %s", root->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 939 | delete source; |
| 940 | return NULL; |
| 941 | } |
| 942 | return source; |
| 943 | } |
| 944 | |
| 945 | status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) |
| 946 | { |
| 947 | cnode *node = config_find(root, PREPROCESSING_TAG); |
| 948 | if (node == NULL) { |
| 949 | return -ENOENT; |
| 950 | } |
| 951 | node = node->first_child; |
| 952 | while (node) { |
| 953 | audio_source_t source = inputSourceNameToEnum(node->name); |
| 954 | if (source == AUDIO_SOURCE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 955 | ALOGW("loadInputSources() invalid input source %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 956 | node = node->next; |
| 957 | continue; |
| 958 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 959 | ALOGV("loadInputSources() loading input source %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 960 | InputSourceDesc *desc = loadInputSource(node, effects); |
| 961 | if (desc == NULL) { |
| 962 | node = node->next; |
| 963 | continue; |
| 964 | } |
| 965 | mInputSources.add(source, desc); |
| 966 | node = node->next; |
| 967 | } |
| 968 | return NO_ERROR; |
| 969 | } |
| 970 | |
| 971 | AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) |
| 972 | { |
| 973 | cnode *node = config_find(root, UUID_TAG); |
| 974 | if (node == NULL) { |
| 975 | return NULL; |
| 976 | } |
| 977 | effect_uuid_t uuid; |
| 978 | if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 979 | ALOGW("loadEffect() invalid uuid %s", node->value); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 980 | return NULL; |
| 981 | } |
Glenn Kasten | 9fda4b8 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 982 | return new EffectDesc(root->name, uuid); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) |
| 986 | { |
| 987 | cnode *node = config_find(root, EFFECTS_TAG); |
| 988 | if (node == NULL) { |
| 989 | return -ENOENT; |
| 990 | } |
| 991 | node = node->first_child; |
| 992 | while (node) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 993 | ALOGV("loadEffects() loading effect %s", node->name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 994 | EffectDesc *effect = loadEffect(node); |
| 995 | if (effect == NULL) { |
| 996 | node = node->next; |
| 997 | continue; |
| 998 | } |
| 999 | effects.add(effect); |
| 1000 | node = node->next; |
| 1001 | } |
| 1002 | return NO_ERROR; |
| 1003 | } |
| 1004 | |
| 1005 | status_t AudioPolicyService::loadPreProcessorConfig(const char *path) |
| 1006 | { |
| 1007 | cnode *root; |
| 1008 | char *data; |
| 1009 | |
| 1010 | data = (char *)load_file(path, NULL); |
| 1011 | if (data == NULL) { |
| 1012 | return -ENODEV; |
| 1013 | } |
| 1014 | root = config_node("", ""); |
| 1015 | config_load(root, data); |
| 1016 | |
| 1017 | Vector <EffectDesc *> effects; |
| 1018 | loadEffects(root, effects); |
| 1019 | loadInputSources(root, effects); |
| 1020 | |
Yu Yezhong | e6056ba | 2013-10-15 14:32:34 +0800 | [diff] [blame] | 1021 | // delete effects to fix memory leak. |
| 1022 | // as effects is local var and valgrind would treat this as memory leak |
| 1023 | // and although it only did in mediaserver init, but free it in case mediaserver reboot |
| 1024 | size_t i; |
| 1025 | for (i = 0; i < effects.size(); i++) { |
| 1026 | delete effects[i]; |
| 1027 | } |
| 1028 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1029 | config_free(root); |
| 1030 | free(root); |
| 1031 | free(data); |
| 1032 | |
| 1033 | return NO_ERROR; |
| 1034 | } |
| 1035 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1036 | extern "C" { |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1037 | audio_module_handle_t aps_load_hw_module(void *service __unused, |
| 1038 | const char *name); |
| 1039 | audio_io_handle_t aps_open_output(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1040 | audio_devices_t *pDevices, |
| 1041 | uint32_t *pSamplingRate, |
| 1042 | audio_format_t *pFormat, |
| 1043 | audio_channel_mask_t *pChannelMask, |
| 1044 | uint32_t *pLatencyMs, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1045 | audio_output_flags_t flags); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1046 | |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1047 | audio_io_handle_t aps_open_output_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1048 | audio_module_handle_t module, |
| 1049 | audio_devices_t *pDevices, |
| 1050 | uint32_t *pSamplingRate, |
| 1051 | audio_format_t *pFormat, |
| 1052 | audio_channel_mask_t *pChannelMask, |
| 1053 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1054 | audio_output_flags_t flags, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1055 | const audio_offload_info_t *offloadInfo); |
| 1056 | audio_io_handle_t aps_open_dup_output(void *service __unused, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1057 | audio_io_handle_t output1, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1058 | audio_io_handle_t output2); |
| 1059 | int aps_close_output(void *service __unused, audio_io_handle_t output); |
| 1060 | int aps_suspend_output(void *service __unused, audio_io_handle_t output); |
| 1061 | int aps_restore_output(void *service __unused, audio_io_handle_t output); |
| 1062 | audio_io_handle_t aps_open_input(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1063 | audio_devices_t *pDevices, |
| 1064 | uint32_t *pSamplingRate, |
| 1065 | audio_format_t *pFormat, |
| 1066 | audio_channel_mask_t *pChannelMask, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1067 | audio_in_acoustics_t acoustics __unused); |
| 1068 | audio_io_handle_t aps_open_input_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1069 | audio_module_handle_t module, |
| 1070 | audio_devices_t *pDevices, |
| 1071 | uint32_t *pSamplingRate, |
| 1072 | audio_format_t *pFormat, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1073 | audio_channel_mask_t *pChannelMask); |
| 1074 | int aps_close_input(void *service __unused, audio_io_handle_t input); |
| 1075 | int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); |
| 1076 | int aps_move_effects(void *service __unused, int session, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1077 | audio_io_handle_t src_output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1078 | audio_io_handle_t dst_output); |
| 1079 | char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, |
| 1080 | const char *keys); |
| 1081 | void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 1082 | const char *kv_pairs, int delay_ms); |
| 1083 | int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1084 | float volume, audio_io_handle_t output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1085 | int delay_ms); |
| 1086 | int aps_start_tone(void *service, audio_policy_tone_t tone, |
| 1087 | audio_stream_type_t stream); |
| 1088 | int aps_stop_tone(void *service); |
| 1089 | int aps_set_voice_volume(void *service, float volume, int delay_ms); |
| 1090 | }; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | namespace { |
| 1093 | struct audio_policy_service_ops aps_ops = { |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1094 | .open_output = aps_open_output, |
| 1095 | .open_duplicate_output = aps_open_dup_output, |
| 1096 | .close_output = aps_close_output, |
| 1097 | .suspend_output = aps_suspend_output, |
| 1098 | .restore_output = aps_restore_output, |
| 1099 | .open_input = aps_open_input, |
| 1100 | .close_input = aps_close_input, |
| 1101 | .set_stream_volume = aps_set_stream_volume, |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 1102 | .invalidate_stream = aps_invalidate_stream, |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1103 | .set_parameters = aps_set_parameters, |
| 1104 | .get_parameters = aps_get_parameters, |
| 1105 | .start_tone = aps_start_tone, |
| 1106 | .stop_tone = aps_stop_tone, |
| 1107 | .set_voice_volume = aps_set_voice_volume, |
| 1108 | .move_effects = aps_move_effects, |
| 1109 | .load_hw_module = aps_load_hw_module, |
| 1110 | .open_output_on_module = aps_open_output_on_module, |
| 1111 | .open_input_on_module = aps_open_input_on_module, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1112 | }; |
| 1113 | }; // namespace <unnamed> |
| 1114 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1115 | }; // namespace android |