Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 19 | |
| 20 | #undef __STRICT_ANSI__ |
| 21 | #define __STDINT_LIMITS |
| 22 | #define __STDC_LIMIT_MACROS |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <sys/time.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 26 | #include <binder/IServiceManager.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | #include <binder/IPCThreadState.h> |
| 30 | #include <utils/String16.h> |
| 31 | #include <utils/threads.h> |
| 32 | #include "AudioPolicyService.h" |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 33 | #include "ServiceUtilities.h" |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 35 | #include <hardware_legacy/power.h> |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 36 | #include <media/AudioEffect.h> |
| 37 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 38 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 39 | #include <hardware/hardware.h> |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 40 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 41 | #include <system/audio_policy.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 42 | #include <hardware/audio_policy.h> |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 43 | #include <audio_effects/audio_effects_conf.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 44 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Glenn Kasten | 05d3310 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 47 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 48 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 49 | |
| 50 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 22c4241 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 51 | static const int kDumpLockSleepUs = 20000; |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 52 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 53 | namespace { |
| 54 | extern struct audio_policy_service_ops aps_ops; |
| 55 | }; |
| 56 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | AudioPolicyService::AudioPolicyService() |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 60 | : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 61 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 62 | char value[PROPERTY_VALUE_MAX]; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 63 | const struct hw_module_t *module; |
| 64 | int forced_val; |
| 65 | int rc; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 66 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 67 | Mutex::Autolock _l(mLock); |
| 68 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 69 | // start tone playback thread |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 70 | mTonePlaybackThread = new AudioCommandThread(String8("")); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 71 | // start audio commands thread |
Glenn Kasten | 86e3362 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 72 | mAudioCommandThread = new AudioCommandThread(String8("ApmCommand")); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 73 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 74 | /* instantiate the audio policy manager */ |
| 75 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); |
| 76 | if (rc) |
| 77 | return; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 78 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 79 | rc = audio_policy_dev_open(module, &mpAudioPolicyDev); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 80 | ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc)); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 81 | if (rc) |
| 82 | return; |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 83 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 84 | rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this, |
| 85 | &mpAudioPolicy); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 86 | ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc)); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 87 | if (rc) |
| 88 | return; |
| 89 | |
| 90 | rc = mpAudioPolicy->init_check(mpAudioPolicy); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 91 | ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc)); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 92 | if (rc) |
| 93 | return; |
| 94 | |
| 95 | property_get("ro.camera.sound.forced", value, "0"); |
| 96 | forced_val = strtol(value, NULL, 0); |
| 97 | mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val); |
| 98 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 99 | ALOGI("Loaded audio policy from %s (%s)", module->name, module->id); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 100 | |
| 101 | // load audio pre processing modules |
| 102 | if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) { |
| 103 | loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE); |
| 104 | } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) { |
| 105 | loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE); |
| 106 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | AudioPolicyService::~AudioPolicyService() |
| 110 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 111 | mTonePlaybackThread->exit(); |
| 112 | mTonePlaybackThread.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 113 | mAudioCommandThread->exit(); |
| 114 | mAudioCommandThread.clear(); |
| 115 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 116 | |
| 117 | // release audio pre processing resources |
| 118 | for (size_t i = 0; i < mInputSources.size(); i++) { |
Glenn Kasten | afe9833 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 119 | delete mInputSources.valueAt(i); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 120 | } |
| 121 | mInputSources.clear(); |
| 122 | |
| 123 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 124 | mInputs.valueAt(i)->mEffects.clear(); |
| 125 | delete mInputs.valueAt(i); |
| 126 | } |
| 127 | mInputs.clear(); |
| 128 | |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 129 | if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 130 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 131 | if (mpAudioPolicyDev != NULL) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 132 | audio_policy_dev_close(mpAudioPolicyDev); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 135 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, |
| 136 | audio_policy_dev_state_t state, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 137 | const char *device_address) |
| 138 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 139 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 140 | return NO_INIT; |
| 141 | } |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 142 | if (!settingsAllowed()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 143 | return PERMISSION_DENIED; |
| 144 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 146 | return BAD_VALUE; |
| 147 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 148 | if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && |
| 149 | state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 150 | return BAD_VALUE; |
| 151 | } |
| 152 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 153 | ALOGV("setDeviceConnectionState() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 154 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 155 | return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, |
| 156 | state, device_address); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 159 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( |
| 160 | audio_devices_t device, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 161 | const char *device_address) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 162 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 163 | if (mpAudioPolicy == NULL) { |
| 164 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 165 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 166 | return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, |
| 167 | device_address); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Glenn Kasten | accb114 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 170 | status_t AudioPolicyService::setPhoneState(audio_mode_t state) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 171 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 172 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 173 | return NO_INIT; |
| 174 | } |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 175 | if (!settingsAllowed()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 176 | return PERMISSION_DENIED; |
| 177 | } |
Glenn Kasten | 01aaf2c | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 178 | if (uint32_t(state) >= AUDIO_MODE_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 179 | return BAD_VALUE; |
| 180 | } |
| 181 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 182 | ALOGV("setPhoneState() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 183 | |
| 184 | // TODO: check if it is more appropriate to do it in platform specific policy manager |
| 185 | AudioSystem::setMode(state); |
| 186 | |
| 187 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 188 | mpAudioPolicy->set_phone_state(mpAudioPolicy, state); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 189 | return NO_ERROR; |
| 190 | } |
| 191 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 192 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, |
| 193 | audio_policy_forced_cfg_t config) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 194 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 195 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 196 | return NO_INIT; |
| 197 | } |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 198 | if (!settingsAllowed()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 199 | return PERMISSION_DENIED; |
| 200 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 201 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 202 | return BAD_VALUE; |
| 203 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 204 | if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 205 | return BAD_VALUE; |
| 206 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 207 | ALOGV("setForceUse() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 208 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 209 | mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 210 | return NO_ERROR; |
| 211 | } |
| 212 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 213 | audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 214 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 215 | if (mpAudioPolicy == NULL) { |
| 216 | return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 217 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 218 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
| 219 | return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 220 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 221 | return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 224 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 225 | uint32_t samplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 226 | audio_format_t format, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 227 | uint32_t channels, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 228 | audio_policy_output_flags_t flags) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 229 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 230 | if (mpAudioPolicy == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 231 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 232 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 233 | ALOGV("getOutput() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 234 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 235 | return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 238 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 239 | audio_stream_type_t stream, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 240 | int session) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 241 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 242 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 243 | return NO_INIT; |
| 244 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 245 | ALOGV("startOutput() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 247 | return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 250 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 251 | audio_stream_type_t stream, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 252 | int session) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 253 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 254 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 255 | return NO_INIT; |
| 256 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 257 | ALOGV("stopOutput() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 258 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 259 | return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void AudioPolicyService::releaseOutput(audio_io_handle_t output) |
| 263 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 264 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 265 | return; |
| 266 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 267 | ALOGV("releaseOutput() tid %d", gettid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 268 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 269 | mpAudioPolicy->release_output(mpAudioPolicy, output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 272 | audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 273 | uint32_t samplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 274 | audio_format_t format, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 275 | uint32_t channels, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 276 | audio_in_acoustics_t acoustics, |
| 277 | int audioSession) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 278 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 279 | if (mpAudioPolicy == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 280 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 281 | } |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 282 | // already checked by client, but double-check in case the client wrapper is bypassed |
| 283 | if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) { |
| 284 | return 0; |
| 285 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 286 | Mutex::Autolock _l(mLock); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 287 | audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, |
| 288 | format, channels, acoustics); |
| 289 | |
| 290 | if (input == 0) { |
| 291 | return input; |
| 292 | } |
| 293 | // create audio pre processors according to input source |
Glenn Kasten | 0f0fbd9 | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 294 | ssize_t index = mInputSources.indexOfKey(inputSource); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 295 | if (index < 0) { |
| 296 | return input; |
| 297 | } |
| 298 | ssize_t idx = mInputs.indexOfKey(input); |
| 299 | InputDesc *inputDesc; |
| 300 | if (idx < 0) { |
| 301 | inputDesc = new InputDesc(); |
| 302 | inputDesc->mSessionId = audioSession; |
| 303 | mInputs.add(input, inputDesc); |
| 304 | } else { |
| 305 | inputDesc = mInputs.valueAt(idx); |
| 306 | } |
| 307 | |
| 308 | Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects; |
| 309 | for (size_t i = 0; i < effects.size(); i++) { |
| 310 | EffectDesc *effect = effects[i]; |
| 311 | sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input); |
| 312 | status_t status = fx->initCheck(); |
| 313 | if (status != NO_ERROR && status != ALREADY_EXISTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 314 | ALOGW("Failed to create Fx %s on input %d", effect->mName, input); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 315 | // fx goes out of scope and strong ref on AudioEffect is released |
| 316 | continue; |
| 317 | } |
| 318 | for (size_t j = 0; j < effect->mParams.size(); j++) { |
| 319 | fx->setParameter(effect->mParams[j]); |
| 320 | } |
| 321 | inputDesc->mEffects.add(fx); |
| 322 | } |
| 323 | setPreProcessorEnabled(inputDesc, true); |
| 324 | return input; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | status_t AudioPolicyService::startInput(audio_io_handle_t input) |
| 328 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 329 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 330 | return NO_INIT; |
| 331 | } |
| 332 | Mutex::Autolock _l(mLock); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 333 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 334 | return mpAudioPolicy->start_input(mpAudioPolicy, input); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | status_t AudioPolicyService::stopInput(audio_io_handle_t input) |
| 338 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 339 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 340 | return NO_INIT; |
| 341 | } |
| 342 | Mutex::Autolock _l(mLock); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 343 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 344 | return mpAudioPolicy->stop_input(mpAudioPolicy, input); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void AudioPolicyService::releaseInput(audio_io_handle_t input) |
| 348 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 349 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 353 | mpAudioPolicy->release_input(mpAudioPolicy, input); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 354 | |
| 355 | ssize_t index = mInputs.indexOfKey(input); |
| 356 | if (index < 0) { |
| 357 | return; |
| 358 | } |
| 359 | InputDesc *inputDesc = mInputs.valueAt(index); |
| 360 | setPreProcessorEnabled(inputDesc, false); |
| 361 | inputDesc->mEffects.clear(); |
| 362 | delete inputDesc; |
| 363 | mInputs.removeItemsAt(index); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 366 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 367 | int indexMin, |
| 368 | int indexMax) |
| 369 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 370 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 371 | return NO_INIT; |
| 372 | } |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 373 | if (!settingsAllowed()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 374 | return PERMISSION_DENIED; |
| 375 | } |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 376 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 377 | return BAD_VALUE; |
| 378 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 379 | mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 380 | return NO_ERROR; |
| 381 | } |
| 382 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 383 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, |
| 384 | int index, |
| 385 | audio_devices_t device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 386 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 387 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 388 | return NO_INIT; |
| 389 | } |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 390 | if (!settingsAllowed()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 391 | return PERMISSION_DENIED; |
| 392 | } |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 393 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 394 | return BAD_VALUE; |
| 395 | } |
| 396 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 397 | if (mpAudioPolicy->set_stream_volume_index_for_device) { |
| 398 | return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy, |
| 399 | stream, |
| 400 | index, |
| 401 | device); |
| 402 | } else { |
| 403 | return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); |
| 404 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 407 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, |
| 408 | int *index, |
| 409 | audio_devices_t device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 410 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 411 | if (mpAudioPolicy == NULL) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 412 | return NO_INIT; |
| 413 | } |
Glenn Kasten | 6e987a4 | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 414 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 415 | return BAD_VALUE; |
| 416 | } |
Eric Laurent | 9bc8358d | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 417 | if (mpAudioPolicy->get_stream_volume_index_for_device) { |
| 418 | return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy, |
| 419 | stream, |
| 420 | index, |
| 421 | device); |
| 422 | } else { |
| 423 | return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); |
| 424 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 427 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 428 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 429 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 432 | return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Eric Laurent | c9ab9f5 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 435 | //audio policy: use audio_device_t appropriately |
| 436 | |
| 437 | audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 438 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 439 | if (mpAudioPolicy == NULL) { |
Eric Laurent | c9ab9f5 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 440 | return (audio_devices_t)0; |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 441 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 442 | return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 445 | audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) |
| 446 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 447 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 448 | return NO_INIT; |
| 449 | } |
| 450 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 451 | return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 455 | audio_io_handle_t io, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 456 | uint32_t strategy, |
| 457 | int session, |
| 458 | int id) |
| 459 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 460 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 461 | return NO_INIT; |
| 462 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 463 | return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | status_t AudioPolicyService::unregisterEffect(int id) |
| 467 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 468 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 469 | return NO_INIT; |
| 470 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 471 | return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 474 | status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) |
| 475 | { |
| 476 | if (mpAudioPolicy == NULL) { |
| 477 | return NO_INIT; |
| 478 | } |
| 479 | return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled); |
| 480 | } |
| 481 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 482 | bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 483 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 484 | if (mpAudioPolicy == NULL) { |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | Mutex::Autolock _l(mLock); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 488 | return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Eric Laurent | 0f7f4ec | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 491 | status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, |
| 492 | effect_descriptor_t *descriptors, |
| 493 | uint32_t *count) |
| 494 | { |
| 495 | |
| 496 | if (mpAudioPolicy == NULL) { |
| 497 | *count = 0; |
| 498 | return NO_INIT; |
| 499 | } |
| 500 | Mutex::Autolock _l(mLock); |
| 501 | status_t status = NO_ERROR; |
| 502 | |
| 503 | size_t index; |
| 504 | for (index = 0; index < mInputs.size(); index++) { |
| 505 | if (mInputs.valueAt(index)->mSessionId == audioSession) { |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | if (index == mInputs.size()) { |
| 510 | *count = 0; |
| 511 | return BAD_VALUE; |
| 512 | } |
| 513 | Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects; |
| 514 | |
| 515 | for (size_t i = 0; i < effects.size(); i++) { |
| 516 | effect_descriptor_t desc = effects[i]->descriptor(); |
| 517 | if (i < *count) { |
| 518 | memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t)); |
| 519 | } |
| 520 | } |
| 521 | if (effects.size() > *count) { |
| 522 | status = NO_MEMORY; |
| 523 | } |
| 524 | *count = effects.size(); |
| 525 | return status; |
| 526 | } |
| 527 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 528 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 7ed4f0c | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 529 | ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(), |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 530 | IPCThreadState::self()->getCallingPid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 533 | static bool tryLock(Mutex& mutex) |
| 534 | { |
| 535 | bool locked = false; |
| 536 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 537 | if (mutex.tryLock() == NO_ERROR) { |
| 538 | locked = true; |
| 539 | break; |
| 540 | } |
Glenn Kasten | 22c4241 | 2012-01-09 08:33:38 -0800 | [diff] [blame] | 541 | usleep(kDumpLockSleepUs); |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 542 | } |
| 543 | return locked; |
| 544 | } |
| 545 | |
| 546 | status_t AudioPolicyService::dumpInternals(int fd) |
| 547 | { |
| 548 | const size_t SIZE = 256; |
| 549 | char buffer[SIZE]; |
| 550 | String8 result; |
| 551 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 552 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 553 | result.append(buffer); |
| 554 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 555 | result.append(buffer); |
| 556 | snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); |
| 557 | result.append(buffer); |
| 558 | |
| 559 | write(fd, result.string(), result.size()); |
| 560 | return NO_ERROR; |
| 561 | } |
| 562 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 563 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) |
| 564 | { |
Glenn Kasten | 8121114 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 565 | if (!dumpAllowed()) { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 566 | dumpPermissionDenial(fd); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 567 | } else { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 568 | bool locked = tryLock(mLock); |
| 569 | if (!locked) { |
| 570 | String8 result(kDeadlockedString); |
| 571 | write(fd, result.string(), result.size()); |
| 572 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 573 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 574 | dumpInternals(fd); |
Glenn Kasten | 1137be1 | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 575 | if (mAudioCommandThread != 0) { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 576 | mAudioCommandThread->dump(fd); |
| 577 | } |
Glenn Kasten | 1137be1 | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 578 | if (mTonePlaybackThread != 0) { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 579 | mTonePlaybackThread->dump(fd); |
| 580 | } |
| 581 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 582 | if (mpAudioPolicy) { |
| 583 | mpAudioPolicy->dump(mpAudioPolicy, fd); |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | if (locked) mLock.unlock(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 587 | } |
| 588 | return NO_ERROR; |
| 589 | } |
| 590 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 591 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 592 | { |
| 593 | const size_t SIZE = 256; |
| 594 | char buffer[SIZE]; |
| 595 | String8 result; |
| 596 | snprintf(buffer, SIZE, "Permission Denial: " |
| 597 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 598 | IPCThreadState::self()->getCallingPid(), |
| 599 | IPCThreadState::self()->getCallingUid()); |
| 600 | result.append(buffer); |
| 601 | write(fd, result.string(), result.size()); |
| 602 | return NO_ERROR; |
| 603 | } |
| 604 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 605 | void AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled) |
| 606 | { |
| 607 | Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects; |
| 608 | for (size_t i = 0; i < fxVector.size(); i++) { |
Glenn Kasten | 7d3be3a | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 609 | fxVector.itemAt(i)->setEnabled(enabled); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 613 | status_t AudioPolicyService::onTransact( |
| 614 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 615 | { |
| 616 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 617 | } |
| 618 | |
| 619 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 620 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 621 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 622 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) |
| 623 | : Thread(false), mName(name) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 624 | { |
| 625 | mpToneGenerator = NULL; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 630 | { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 631 | if (mName != "" && !mAudioCommands.isEmpty()) { |
| 632 | release_wake_lock(mName.string()); |
| 633 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 634 | mAudioCommands.clear(); |
Glenn Kasten | 2589cf9 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 635 | delete mpToneGenerator; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 639 | { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 640 | if (mName != "") { |
| 641 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
| 642 | } else { |
Glenn Kasten | 86e3362 | 2012-02-28 12:30:08 -0800 | [diff] [blame] | 643 | run("AudioCommand", ANDROID_PRIORITY_AUDIO); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 644 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 648 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 649 | nsecs_t waitTime = INT64_MAX; |
| 650 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 651 | mLock.lock(); |
| 652 | while (!exitPending()) |
| 653 | { |
| 654 | while(!mAudioCommands.isEmpty()) { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 655 | nsecs_t curTime = systemTime(); |
| 656 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 657 | if (mAudioCommands[0]->mTime <= curTime) { |
| 658 | AudioCommand *command = mAudioCommands[0]; |
| 659 | mAudioCommands.removeAt(0); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 660 | mLastCommand = *command; |
| 661 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 662 | switch (command->mCommand) { |
| 663 | case START_TONE: { |
| 664 | mLock.unlock(); |
| 665 | ToneData *data = (ToneData *)command->mParam; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 666 | ALOGV("AudioCommandThread() processing start tone %d on stream %d", |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 667 | data->mType, data->mStream); |
Glenn Kasten | 2589cf9 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 668 | delete mpToneGenerator; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 669 | mpToneGenerator = new ToneGenerator(data->mStream, 1.0); |
| 670 | mpToneGenerator->startTone(data->mType); |
| 671 | delete data; |
| 672 | mLock.lock(); |
| 673 | }break; |
| 674 | case STOP_TONE: { |
| 675 | mLock.unlock(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 676 | ALOGV("AudioCommandThread() processing stop tone"); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 677 | if (mpToneGenerator != NULL) { |
| 678 | mpToneGenerator->stopTone(); |
| 679 | delete mpToneGenerator; |
| 680 | mpToneGenerator = NULL; |
| 681 | } |
| 682 | mLock.lock(); |
| 683 | }break; |
| 684 | case SET_VOLUME: { |
| 685 | VolumeData *data = (VolumeData *)command->mParam; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 686 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 687 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
| 688 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 689 | data->mVolume, |
| 690 | data->mIO); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 691 | if (command->mWaitStatus) { |
| 692 | command->mCond.signal(); |
| 693 | mWaitWorkCV.wait(mLock); |
| 694 | } |
| 695 | delete data; |
| 696 | }break; |
| 697 | case SET_PARAMETERS: { |
| 698 | ParametersData *data = (ParametersData *)command->mParam; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 699 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 700 | data->mKeyValuePairs.string(), data->mIO); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 701 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
| 702 | if (command->mWaitStatus) { |
| 703 | command->mCond.signal(); |
| 704 | mWaitWorkCV.wait(mLock); |
| 705 | } |
| 706 | delete data; |
| 707 | }break; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 708 | case SET_VOICE_VOLUME: { |
| 709 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 710 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 711 | data->mVolume); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 712 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
| 713 | if (command->mWaitStatus) { |
| 714 | command->mCond.signal(); |
| 715 | mWaitWorkCV.wait(mLock); |
| 716 | } |
| 717 | delete data; |
| 718 | }break; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 719 | default: |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 720 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | } |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 722 | delete command; |
| 723 | waitTime = INT64_MAX; |
| 724 | } else { |
| 725 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 726 | break; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 727 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 728 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 729 | // release delayed commands wake lock |
| 730 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 731 | release_wake_lock(mName.string()); |
| 732 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 733 | ALOGV("AudioCommandThread() going to sleep"); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 734 | mWaitWorkCV.waitRelative(mLock, waitTime); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 735 | ALOGV("AudioCommandThread() waking up"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 736 | } |
| 737 | mLock.unlock(); |
| 738 | return false; |
| 739 | } |
| 740 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 741 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 742 | { |
| 743 | const size_t SIZE = 256; |
| 744 | char buffer[SIZE]; |
| 745 | String8 result; |
| 746 | |
| 747 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 748 | result.append(buffer); |
| 749 | write(fd, result.string(), result.size()); |
| 750 | |
| 751 | bool locked = tryLock(mLock); |
| 752 | if (!locked) { |
| 753 | String8 result2(kCmdDeadlockedString); |
| 754 | write(fd, result2.string(), result2.size()); |
| 755 | } |
| 756 | |
| 757 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 758 | result = String8(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 759 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 150d238 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 760 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 761 | mAudioCommands[i]->dump(buffer, SIZE); |
| 762 | result.append(buffer); |
| 763 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 764 | result.append(" Last Command\n"); |
| 765 | mLastCommand.dump(buffer, SIZE); |
| 766 | result.append(buffer); |
| 767 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 768 | write(fd, result.string(), result.size()); |
| 769 | |
| 770 | if (locked) mLock.unlock(); |
| 771 | |
| 772 | return NO_ERROR; |
| 773 | } |
| 774 | |
Glenn Kasten | 23f7ad3 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 775 | void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type, |
| 776 | audio_stream_type_t stream) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 777 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 778 | AudioCommand *command = new AudioCommand(); |
| 779 | command->mCommand = START_TONE; |
| 780 | ToneData *data = new ToneData(); |
| 781 | data->mType = type; |
| 782 | data->mStream = stream; |
| 783 | command->mParam = (void *)data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 784 | command->mWaitStatus = false; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 785 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 786 | insertCommand_l(command); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 787 | ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 788 | mWaitWorkCV.signal(); |
| 789 | } |
| 790 | |
| 791 | void AudioPolicyService::AudioCommandThread::stopToneCommand() |
| 792 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 793 | AudioCommand *command = new AudioCommand(); |
| 794 | command->mCommand = STOP_TONE; |
| 795 | command->mParam = NULL; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 796 | command->mWaitStatus = false; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 797 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 798 | insertCommand_l(command); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 799 | ALOGV("AudioCommandThread() adding tone stop"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 800 | mWaitWorkCV.signal(); |
| 801 | } |
| 802 | |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 803 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 804 | float volume, |
Glenn Kasten | 39d00cb | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 805 | audio_io_handle_t output, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 806 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 807 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 808 | status_t status = NO_ERROR; |
| 809 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 810 | AudioCommand *command = new AudioCommand(); |
| 811 | command->mCommand = SET_VOLUME; |
| 812 | VolumeData *data = new VolumeData(); |
| 813 | data->mStream = stream; |
| 814 | data->mVolume = volume; |
| 815 | data->mIO = output; |
| 816 | command->mParam = data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 817 | if (delayMs == 0) { |
| 818 | command->mWaitStatus = true; |
| 819 | } else { |
| 820 | command->mWaitStatus = false; |
| 821 | } |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 822 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 823 | insertCommand_l(command, delayMs); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 824 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 825 | stream, volume, output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 826 | mWaitWorkCV.signal(); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 827 | if (command->mWaitStatus) { |
| 828 | command->mCond.wait(mLock); |
| 829 | status = command->mStatus; |
| 830 | mWaitWorkCV.signal(); |
| 831 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 832 | return status; |
| 833 | } |
| 834 | |
Glenn Kasten | 39d00cb | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 835 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 836 | const char *keyValuePairs, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 837 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 838 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 839 | status_t status = NO_ERROR; |
| 840 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 841 | AudioCommand *command = new AudioCommand(); |
| 842 | command->mCommand = SET_PARAMETERS; |
| 843 | ParametersData *data = new ParametersData(); |
| 844 | data->mIO = ioHandle; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 845 | data->mKeyValuePairs = String8(keyValuePairs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 846 | command->mParam = data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 847 | if (delayMs == 0) { |
| 848 | command->mWaitStatus = true; |
| 849 | } else { |
| 850 | command->mWaitStatus = false; |
| 851 | } |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 852 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 853 | insertCommand_l(command, delayMs); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 854 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 855 | keyValuePairs, ioHandle, delayMs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 856 | mWaitWorkCV.signal(); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 857 | if (command->mWaitStatus) { |
| 858 | command->mCond.wait(mLock); |
| 859 | status = command->mStatus; |
| 860 | mWaitWorkCV.signal(); |
| 861 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 862 | return status; |
| 863 | } |
| 864 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 865 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 866 | { |
| 867 | status_t status = NO_ERROR; |
| 868 | |
| 869 | AudioCommand *command = new AudioCommand(); |
| 870 | command->mCommand = SET_VOICE_VOLUME; |
| 871 | VoiceVolumeData *data = new VoiceVolumeData(); |
| 872 | data->mVolume = volume; |
| 873 | command->mParam = data; |
| 874 | if (delayMs == 0) { |
| 875 | command->mWaitStatus = true; |
| 876 | } else { |
| 877 | command->mWaitStatus = false; |
| 878 | } |
| 879 | Mutex::Autolock _l(mLock); |
| 880 | insertCommand_l(command, delayMs); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 881 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 882 | mWaitWorkCV.signal(); |
| 883 | if (command->mWaitStatus) { |
| 884 | command->mCond.wait(mLock); |
| 885 | status = command->mStatus; |
| 886 | mWaitWorkCV.signal(); |
| 887 | } |
| 888 | return status; |
| 889 | } |
| 890 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 891 | // insertCommand_l() must be called with mLock held |
| 892 | void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) |
| 893 | { |
Glenn Kasten | 150d238 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 894 | ssize_t i; // not size_t because i will count down to -1 |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 895 | Vector <AudioCommand *> removedCommands; |
| 896 | |
| 897 | command->mTime = systemTime() + milliseconds(delayMs); |
| 898 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 899 | // acquire wake lock to make sure delayed commands are processed |
| 900 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 901 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 902 | } |
| 903 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 904 | // check same pending commands with later time stamps and eliminate them |
| 905 | for (i = mAudioCommands.size()-1; i >= 0; i--) { |
| 906 | AudioCommand *command2 = mAudioCommands[i]; |
| 907 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 908 | if (command2->mTime <= command->mTime) break; |
| 909 | if (command2->mCommand != command->mCommand) continue; |
| 910 | |
| 911 | switch (command->mCommand) { |
| 912 | case SET_PARAMETERS: { |
| 913 | ParametersData *data = (ParametersData *)command->mParam; |
| 914 | ParametersData *data2 = (ParametersData *)command2->mParam; |
| 915 | if (data->mIO != data2->mIO) break; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 916 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 917 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 918 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 919 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 920 | for (size_t j = 0; j < param.size(); j++) { |
| 921 | String8 key; |
| 922 | String8 value; |
| 923 | param.getAt(j, key, value); |
| 924 | for (size_t k = 0; k < param2.size(); k++) { |
| 925 | String8 key2; |
| 926 | String8 value2; |
| 927 | param2.getAt(k, key2, value2); |
| 928 | if (key2 == key) { |
| 929 | param2.remove(key2); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 930 | ALOGV("Filtering out parameter %s", key2.string()); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 931 | break; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | // if all keys have been filtered out, remove the command. |
| 936 | // otherwise, update the key value pairs |
| 937 | if (param2.size() == 0) { |
| 938 | removedCommands.add(command2); |
| 939 | } else { |
| 940 | data2->mKeyValuePairs = param2.toString(); |
| 941 | } |
| 942 | } break; |
| 943 | |
| 944 | case SET_VOLUME: { |
| 945 | VolumeData *data = (VolumeData *)command->mParam; |
| 946 | VolumeData *data2 = (VolumeData *)command2->mParam; |
| 947 | if (data->mIO != data2->mIO) break; |
| 948 | if (data->mStream != data2->mStream) break; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 949 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 950 | data->mIO, data->mStream); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 951 | removedCommands.add(command2); |
| 952 | } break; |
| 953 | case START_TONE: |
| 954 | case STOP_TONE: |
| 955 | default: |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // remove filtered commands |
| 961 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 962 | // removed commands always have time stamps greater than current command |
| 963 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
| 964 | if (mAudioCommands[k] == removedCommands[j]) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 965 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 966 | mAudioCommands.removeAt(k); |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | removedCommands.clear(); |
| 972 | |
| 973 | // insert command at the right place according to its time stamp |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 974 | ALOGV("inserting command: %d at index %d, num commands %d", |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 975 | command->mCommand, (int)i+1, mAudioCommands.size()); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 976 | mAudioCommands.insertAt(command, i + 1); |
| 977 | } |
| 978 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 979 | void AudioPolicyService::AudioCommandThread::exit() |
| 980 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 981 | ALOGV("AudioCommandThread::exit"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 982 | { |
| 983 | AutoMutex _l(mLock); |
| 984 | requestExit(); |
| 985 | mWaitWorkCV.signal(); |
| 986 | } |
| 987 | requestExitAndWait(); |
| 988 | } |
| 989 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 990 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 991 | { |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 992 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 993 | mCommand, |
| 994 | (int)ns2s(mTime), |
| 995 | (int)ns2ms(mTime)%1000, |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 996 | mWaitStatus, |
| 997 | mParam); |
| 998 | } |
| 999 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1000 | /******* helpers for the service_ops callbacks defined below *********/ |
| 1001 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 1002 | const char *keyValuePairs, |
| 1003 | int delayMs) |
| 1004 | { |
Glenn Kasten | 39d00cb | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1005 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1006 | delayMs); |
| 1007 | } |
| 1008 | |
| 1009 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 1010 | float volume, |
| 1011 | audio_io_handle_t output, |
| 1012 | int delayMs) |
| 1013 | { |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1014 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 39d00cb | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1015 | output, delayMs); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | int AudioPolicyService::startTone(audio_policy_tone_t tone, |
| 1019 | audio_stream_type_t stream) |
| 1020 | { |
| 1021 | if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1022 | ALOGE("startTone: illegal tone requested (%d)", tone); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1023 | if (stream != AUDIO_STREAM_VOICE_CALL) |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1024 | ALOGE("startTone: illegal stream (%d) requested for tone %d", stream, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1025 | tone); |
| 1026 | mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING, |
| 1027 | AUDIO_STREAM_VOICE_CALL); |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | int AudioPolicyService::stopTone() |
| 1032 | { |
| 1033 | mTonePlaybackThread->stopToneCommand(); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 1038 | { |
| 1039 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 1040 | } |
| 1041 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1042 | // ---------------------------------------------------------------------------- |
| 1043 | // Audio pre-processing configuration |
| 1044 | // ---------------------------------------------------------------------------- |
| 1045 | |
Glenn Kasten | 05d3310 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 1046 | /*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1047 | MIC_SRC_TAG, |
| 1048 | VOICE_UL_SRC_TAG, |
| 1049 | VOICE_DL_SRC_TAG, |
| 1050 | VOICE_CALL_SRC_TAG, |
| 1051 | CAMCORDER_SRC_TAG, |
| 1052 | VOICE_REC_SRC_TAG, |
| 1053 | VOICE_COMM_SRC_TAG |
| 1054 | }; |
| 1055 | |
| 1056 | // returns the audio_source_t enum corresponding to the input source name or |
| 1057 | // AUDIO_SOURCE_CNT is no match found |
| 1058 | audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name) |
| 1059 | { |
| 1060 | int i; |
| 1061 | for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) { |
| 1062 | if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1063 | ALOGV("inputSourceNameToEnum found source %s %d", name, i); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | } |
| 1067 | return (audio_source_t)i; |
| 1068 | } |
| 1069 | |
| 1070 | size_t AudioPolicyService::growParamSize(char *param, |
| 1071 | size_t size, |
| 1072 | size_t *curSize, |
| 1073 | size_t *totSize) |
| 1074 | { |
| 1075 | // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int) |
| 1076 | size_t pos = ((*curSize - 1 ) / size + 1) * size; |
| 1077 | |
| 1078 | if (pos + size > *totSize) { |
| 1079 | while (pos + size > *totSize) { |
| 1080 | *totSize += ((*totSize + 7) / 8) * 4; |
| 1081 | } |
| 1082 | param = (char *)realloc(param, *totSize); |
| 1083 | } |
| 1084 | *curSize = pos + size; |
| 1085 | return pos; |
| 1086 | } |
| 1087 | |
| 1088 | size_t AudioPolicyService::readParamValue(cnode *node, |
| 1089 | char *param, |
| 1090 | size_t *curSize, |
| 1091 | size_t *totSize) |
| 1092 | { |
| 1093 | if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) { |
| 1094 | size_t pos = growParamSize(param, sizeof(short), curSize, totSize); |
| 1095 | *(short *)((char *)param + pos) = (short)atoi(node->value); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1096 | ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos)); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1097 | return sizeof(short); |
| 1098 | } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) { |
| 1099 | size_t pos = growParamSize(param, sizeof(int), curSize, totSize); |
| 1100 | *(int *)((char *)param + pos) = atoi(node->value); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1101 | ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos)); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1102 | return sizeof(int); |
| 1103 | } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) { |
| 1104 | size_t pos = growParamSize(param, sizeof(float), curSize, totSize); |
| 1105 | *(float *)((char *)param + pos) = (float)atof(node->value); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1106 | ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos)); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1107 | return sizeof(float); |
| 1108 | } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) { |
| 1109 | size_t pos = growParamSize(param, sizeof(bool), curSize, totSize); |
| 1110 | if (strncmp(node->value, "false", strlen("false") + 1) == 0) { |
| 1111 | *(bool *)((char *)param + pos) = false; |
| 1112 | } else { |
| 1113 | *(bool *)((char *)param + pos) = true; |
| 1114 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1115 | ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false"); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1116 | return sizeof(bool); |
| 1117 | } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) { |
| 1118 | size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX); |
| 1119 | if (*curSize + len + 1 > *totSize) { |
| 1120 | *totSize = *curSize + len + 1; |
| 1121 | param = (char *)realloc(param, *totSize); |
| 1122 | } |
| 1123 | strncpy(param + *curSize, node->value, len); |
| 1124 | *curSize += len; |
| 1125 | param[*curSize] = '\0'; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1126 | ALOGV("readParamValue() reading string %s", param + *curSize - len); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1127 | return len; |
| 1128 | } |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1129 | ALOGW("readParamValue() unknown param type %s", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root) |
| 1134 | { |
| 1135 | cnode *param; |
| 1136 | cnode *value; |
| 1137 | size_t curSize = sizeof(effect_param_t); |
| 1138 | size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int); |
| 1139 | effect_param_t *fx_param = (effect_param_t *)malloc(totSize); |
| 1140 | |
| 1141 | param = config_find(root, PARAM_TAG); |
| 1142 | value = config_find(root, VALUE_TAG); |
| 1143 | if (param == NULL && value == NULL) { |
| 1144 | // try to parse simple parameter form {int int} |
| 1145 | param = root->first_child; |
Glenn Kasten | 3694ec1 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1146 | if (param != NULL) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1147 | // Note: that a pair of random strings is read as 0 0 |
| 1148 | int *ptr = (int *)fx_param->data; |
| 1149 | int *ptr2 = (int *)((char *)param + sizeof(effect_param_t)); |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1150 | ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1151 | *ptr++ = atoi(param->name); |
| 1152 | *ptr = atoi(param->value); |
| 1153 | fx_param->psize = sizeof(int); |
| 1154 | fx_param->vsize = sizeof(int); |
| 1155 | return fx_param; |
| 1156 | } |
| 1157 | } |
| 1158 | if (param == NULL || value == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1159 | ALOGW("loadEffectParameter() invalid parameter description %s", root->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1160 | goto error; |
| 1161 | } |
| 1162 | |
| 1163 | fx_param->psize = 0; |
| 1164 | param = param->first_child; |
| 1165 | while (param) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1166 | ALOGV("loadEffectParameter() reading param of type %s", param->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1167 | size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize); |
| 1168 | if (size == 0) { |
| 1169 | goto error; |
| 1170 | } |
| 1171 | fx_param->psize += size; |
| 1172 | param = param->next; |
| 1173 | } |
| 1174 | |
| 1175 | // align start of value field on 32 bit boundary |
| 1176 | curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int); |
| 1177 | |
| 1178 | fx_param->vsize = 0; |
| 1179 | value = value->first_child; |
| 1180 | while (value) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1181 | ALOGV("loadEffectParameter() reading value of type %s", value->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1182 | size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize); |
| 1183 | if (size == 0) { |
| 1184 | goto error; |
| 1185 | } |
| 1186 | fx_param->vsize += size; |
| 1187 | value = value->next; |
| 1188 | } |
| 1189 | |
| 1190 | return fx_param; |
| 1191 | |
| 1192 | error: |
| 1193 | delete fx_param; |
| 1194 | return NULL; |
| 1195 | } |
| 1196 | |
| 1197 | void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params) |
| 1198 | { |
| 1199 | cnode *node = root->first_child; |
| 1200 | while (node) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1201 | ALOGV("loadEffectParameters() loading param %s", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1202 | effect_param_t *param = loadEffectParameter(node); |
| 1203 | if (param == NULL) { |
| 1204 | node = node->next; |
| 1205 | continue; |
| 1206 | } |
| 1207 | params.add(param); |
| 1208 | node = node->next; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource( |
| 1213 | cnode *root, |
| 1214 | const Vector <EffectDesc *>& effects) |
| 1215 | { |
| 1216 | cnode *node = root->first_child; |
| 1217 | if (node == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1218 | ALOGW("loadInputSource() empty element %s", root->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1219 | return NULL; |
| 1220 | } |
| 1221 | InputSourceDesc *source = new InputSourceDesc(); |
| 1222 | while (node) { |
| 1223 | size_t i; |
| 1224 | for (i = 0; i < effects.size(); i++) { |
| 1225 | if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1226 | ALOGV("loadInputSource() found effect %s in list", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | if (i == effects.size()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1231 | ALOGV("loadInputSource() effect %s not in list", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1232 | node = node->next; |
| 1233 | continue; |
| 1234 | } |
Glenn Kasten | afe9833 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 1235 | EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1236 | loadEffectParameters(node, effect->mParams); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1237 | ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1238 | source->mEffects.add(effect); |
| 1239 | node = node->next; |
| 1240 | } |
| 1241 | if (source->mEffects.size() == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1242 | ALOGW("loadInputSource() no valid effects found in source %s", root->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1243 | delete source; |
| 1244 | return NULL; |
| 1245 | } |
| 1246 | return source; |
| 1247 | } |
| 1248 | |
| 1249 | status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects) |
| 1250 | { |
| 1251 | cnode *node = config_find(root, PREPROCESSING_TAG); |
| 1252 | if (node == NULL) { |
| 1253 | return -ENOENT; |
| 1254 | } |
| 1255 | node = node->first_child; |
| 1256 | while (node) { |
| 1257 | audio_source_t source = inputSourceNameToEnum(node->name); |
| 1258 | if (source == AUDIO_SOURCE_CNT) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1259 | ALOGW("loadInputSources() invalid input source %s", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1260 | node = node->next; |
| 1261 | continue; |
| 1262 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1263 | ALOGV("loadInputSources() loading input source %s", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1264 | InputSourceDesc *desc = loadInputSource(node, effects); |
| 1265 | if (desc == NULL) { |
| 1266 | node = node->next; |
| 1267 | continue; |
| 1268 | } |
| 1269 | mInputSources.add(source, desc); |
| 1270 | node = node->next; |
| 1271 | } |
| 1272 | return NO_ERROR; |
| 1273 | } |
| 1274 | |
| 1275 | AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root) |
| 1276 | { |
| 1277 | cnode *node = config_find(root, UUID_TAG); |
| 1278 | if (node == NULL) { |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | effect_uuid_t uuid; |
| 1282 | if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1283 | ALOGW("loadEffect() invalid uuid %s", node->value); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1284 | return NULL; |
| 1285 | } |
Glenn Kasten | afe9833 | 2012-02-02 14:04:37 -0800 | [diff] [blame] | 1286 | return new EffectDesc(root->name, uuid); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects) |
| 1290 | { |
| 1291 | cnode *node = config_find(root, EFFECTS_TAG); |
| 1292 | if (node == NULL) { |
| 1293 | return -ENOENT; |
| 1294 | } |
| 1295 | node = node->first_child; |
| 1296 | while (node) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1297 | ALOGV("loadEffects() loading effect %s", node->name); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1298 | EffectDesc *effect = loadEffect(node); |
| 1299 | if (effect == NULL) { |
| 1300 | node = node->next; |
| 1301 | continue; |
| 1302 | } |
| 1303 | effects.add(effect); |
| 1304 | node = node->next; |
| 1305 | } |
| 1306 | return NO_ERROR; |
| 1307 | } |
| 1308 | |
| 1309 | status_t AudioPolicyService::loadPreProcessorConfig(const char *path) |
| 1310 | { |
| 1311 | cnode *root; |
| 1312 | char *data; |
| 1313 | |
| 1314 | data = (char *)load_file(path, NULL); |
| 1315 | if (data == NULL) { |
| 1316 | return -ENODEV; |
| 1317 | } |
| 1318 | root = config_node("", ""); |
| 1319 | config_load(root, data); |
| 1320 | |
| 1321 | Vector <EffectDesc *> effects; |
| 1322 | loadEffects(root, effects); |
| 1323 | loadInputSources(root, effects); |
| 1324 | |
| 1325 | config_free(root); |
| 1326 | free(root); |
| 1327 | free(data); |
| 1328 | |
| 1329 | return NO_ERROR; |
| 1330 | } |
| 1331 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1332 | /* implementation of the interface to the policy manager */ |
| 1333 | extern "C" { |
| 1334 | |
| 1335 | static audio_io_handle_t aps_open_output(void *service, |
| 1336 | uint32_t *pDevices, |
| 1337 | uint32_t *pSamplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1338 | audio_format_t *pFormat, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1339 | uint32_t *pChannels, |
| 1340 | uint32_t *pLatencyMs, |
| 1341 | audio_policy_output_flags_t flags) |
| 1342 | { |
| 1343 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1344 | if (af == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1345 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels, |
| 1350 | pLatencyMs, flags); |
| 1351 | } |
| 1352 | |
| 1353 | static audio_io_handle_t aps_open_dup_output(void *service, |
| 1354 | audio_io_handle_t output1, |
| 1355 | audio_io_handle_t output2) |
| 1356 | { |
| 1357 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1358 | if (af == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1359 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | return af->openDuplicateOutput(output1, output2); |
| 1363 | } |
| 1364 | |
| 1365 | static int aps_close_output(void *service, audio_io_handle_t output) |
| 1366 | { |
| 1367 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1368 | if (af == 0) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1369 | return PERMISSION_DENIED; |
| 1370 | |
| 1371 | return af->closeOutput(output); |
| 1372 | } |
| 1373 | |
| 1374 | static int aps_suspend_output(void *service, audio_io_handle_t output) |
| 1375 | { |
| 1376 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1377 | if (af == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1378 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1379 | return PERMISSION_DENIED; |
| 1380 | } |
| 1381 | |
| 1382 | return af->suspendOutput(output); |
| 1383 | } |
| 1384 | |
| 1385 | static int aps_restore_output(void *service, audio_io_handle_t output) |
| 1386 | { |
| 1387 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1388 | if (af == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1389 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1390 | return PERMISSION_DENIED; |
| 1391 | } |
| 1392 | |
| 1393 | return af->restoreOutput(output); |
| 1394 | } |
| 1395 | |
| 1396 | static audio_io_handle_t aps_open_input(void *service, |
| 1397 | uint32_t *pDevices, |
| 1398 | uint32_t *pSamplingRate, |
Glenn Kasten | 0a204ed | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1399 | audio_format_t *pFormat, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1400 | uint32_t *pChannels, |
Glenn Kasten | 882c0a2 | 2012-01-27 12:32:34 -0800 | [diff] [blame] | 1401 | audio_in_acoustics_t acoustics) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1402 | { |
| 1403 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1404 | if (af == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1405 | ALOGW("%s: could not get AudioFlinger", __func__); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | return af->openInput(pDevices, pSamplingRate, pFormat, pChannels, |
| 1410 | acoustics); |
| 1411 | } |
| 1412 | |
| 1413 | static int aps_close_input(void *service, audio_io_handle_t input) |
| 1414 | { |
| 1415 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1416 | if (af == 0) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1417 | return PERMISSION_DENIED; |
| 1418 | |
| 1419 | return af->closeInput(input); |
| 1420 | } |
| 1421 | |
| 1422 | static int aps_set_stream_output(void *service, audio_stream_type_t stream, |
| 1423 | audio_io_handle_t output) |
| 1424 | { |
| 1425 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1426 | if (af == 0) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1427 | return PERMISSION_DENIED; |
| 1428 | |
| 1429 | return af->setStreamOutput(stream, output); |
| 1430 | } |
| 1431 | |
| 1432 | static int aps_move_effects(void *service, int session, |
| 1433 | audio_io_handle_t src_output, |
| 1434 | audio_io_handle_t dst_output) |
| 1435 | { |
| 1436 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 706b618 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1437 | if (af == 0) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1438 | return PERMISSION_DENIED; |
| 1439 | |
Glenn Kasten | 39d00cb | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1440 | return af->moveEffects(session, src_output, dst_output); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static char * aps_get_parameters(void *service, audio_io_handle_t io_handle, |
| 1444 | const char *keys) |
| 1445 | { |
| 1446 | String8 result = AudioSystem::getParameters(io_handle, String8(keys)); |
| 1447 | return strdup(result.string()); |
| 1448 | } |
| 1449 | |
| 1450 | static void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 1451 | const char *kv_pairs, int delay_ms) |
| 1452 | { |
| 1453 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1454 | |
| 1455 | audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms); |
| 1456 | } |
| 1457 | |
| 1458 | static int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
| 1459 | float volume, audio_io_handle_t output, |
| 1460 | int delay_ms) |
| 1461 | { |
| 1462 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1463 | |
| 1464 | return audioPolicyService->setStreamVolume(stream, volume, output, |
| 1465 | delay_ms); |
| 1466 | } |
| 1467 | |
| 1468 | static int aps_start_tone(void *service, audio_policy_tone_t tone, |
| 1469 | audio_stream_type_t stream) |
| 1470 | { |
| 1471 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1472 | |
| 1473 | return audioPolicyService->startTone(tone, stream); |
| 1474 | } |
| 1475 | |
| 1476 | static int aps_stop_tone(void *service) |
| 1477 | { |
| 1478 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1479 | |
| 1480 | return audioPolicyService->stopTone(); |
| 1481 | } |
| 1482 | |
| 1483 | static int aps_set_voice_volume(void *service, float volume, int delay_ms) |
| 1484 | { |
| 1485 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1486 | |
| 1487 | return audioPolicyService->setVoiceVolume(volume, delay_ms); |
| 1488 | } |
| 1489 | |
| 1490 | }; // extern "C" |
| 1491 | |
| 1492 | namespace { |
| 1493 | struct audio_policy_service_ops aps_ops = { |
| 1494 | open_output : aps_open_output, |
| 1495 | open_duplicate_output : aps_open_dup_output, |
| 1496 | close_output : aps_close_output, |
| 1497 | suspend_output : aps_suspend_output, |
| 1498 | restore_output : aps_restore_output, |
| 1499 | open_input : aps_open_input, |
| 1500 | close_input : aps_close_input, |
| 1501 | set_stream_volume : aps_set_stream_volume, |
| 1502 | set_stream_output : aps_set_stream_output, |
| 1503 | set_parameters : aps_set_parameters, |
| 1504 | get_parameters : aps_get_parameters, |
| 1505 | start_tone : aps_start_tone, |
| 1506 | stop_tone : aps_stop_tone, |
| 1507 | set_voice_volume : aps_set_voice_volume, |
| 1508 | move_effects : aps_move_effects, |
| 1509 | }; |
| 1510 | }; // namespace <unnamed> |
| 1511 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1512 | }; // namespace android |