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