Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <utils/Log.h> |
| 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <utils/String16.h> |
| 33 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 38 | #include <cutils/compiler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 40 | #include <media/IMediaPlayerService.h> |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 41 | #include <media/IMediaDeathNotifier.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
| 43 | #include <private/media/AudioTrackShared.h> |
| 44 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 47 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | #include "AudioMixer.h" |
| 50 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 51 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 54 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 55 | #include <audio_effects/effect_ns.h> |
| 56 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 58 | #include <audio_utils/primitives.h> |
| 59 | |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 60 | #include <cpustats/ThreadCpuUsage.h> |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 62 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
| 63 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 64 | #include <common_time/cc_helper.h> |
| 65 | #include <common_time/local_clock.h> |
| 66 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 69 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | namespace android { |
| 71 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 72 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 73 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 74 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | static const float MAX_GAIN = 4096.0f; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 76 | static const uint32_t MAX_GAIN_INT = 0x1000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 77 | |
| 78 | // retry counts for buffer fill timeout |
| 79 | // 50 * ~20msecs = 1 second |
| 80 | static const int8_t kMaxTrackRetries = 50; |
| 81 | static const int8_t kMaxTrackStartupRetries = 50; |
| 82 | // allow less retry attempts on direct output thread. |
| 83 | // direct outputs can be a scarce resource in audio hardware and should |
| 84 | // be released as quickly as possible. |
| 85 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 86 | |
| 87 | static const int kDumpLockRetries = 50; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 88 | static const int kDumpLockSleepUs = 20000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 90 | // don't warn about blocked writes or record buffer overflows more often than this |
| 91 | static const nsecs_t kWarningThrottleNs = seconds(5); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 93 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 94 | static const int kRecordThreadSleepUs = 5000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 95 | |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 96 | // maximum time to wait for setParameters to complete |
| 97 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 98 | |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 99 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 100 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 101 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 102 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 103 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 104 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 105 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | // ---------------------------------------------------------------------------- |
| 107 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 108 | // To collect the amplifier usage |
| 109 | static void addBatteryData(uint32_t params) { |
Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 110 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 111 | if (service == NULL) { |
| 112 | // it already logged |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | service->addBatteryData(params); |
| 117 | } |
| 118 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 119 | static int load_audio_interface(const char *if_name, const hw_module_t **mod, |
| 120 | audio_hw_device_t **dev) |
| 121 | { |
| 122 | int rc; |
| 123 | |
| 124 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod); |
| 125 | if (rc) |
| 126 | goto out; |
| 127 | |
| 128 | rc = audio_hw_device_open(*mod, dev); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 129 | ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 130 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 131 | if (rc) |
| 132 | goto out; |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | out: |
| 137 | *mod = NULL; |
| 138 | *dev = NULL; |
| 139 | return rc; |
| 140 | } |
| 141 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 142 | static const char * const audio_interfaces[] = { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 143 | "primary", |
| 144 | "a2dp", |
| 145 | "usb", |
| 146 | }; |
| 147 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 148 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 149 | // ---------------------------------------------------------------------------- |
| 150 | |
| 151 | AudioFlinger::AudioFlinger() |
| 152 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 153 | mPrimaryHardwareDev(NULL), |
| 154 | mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef() |
| 155 | mMasterVolume(1.0f), |
| 156 | mMasterVolumeSupportLvl(MVS_NONE), |
| 157 | mMasterMute(false), |
| 158 | mNextUniqueId(1), |
| 159 | mMode(AUDIO_MODE_INVALID), |
| 160 | mBtNrecIsOff(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 161 | { |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void AudioFlinger::onFirstRef() |
| 165 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 166 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 167 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 168 | Mutex::Autolock _l(mLock); |
| 169 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 170 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 171 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 172 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 173 | uint32_t int_val; |
| 174 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 175 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 176 | ALOGI("Using %u mSec as standby time.", int_val); |
| 177 | } else { |
| 178 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 179 | ALOGI("Using default %u mSec as standby time.", |
| 180 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 181 | } |
| 182 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 183 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 184 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 185 | const hw_module_t *mod; |
| 186 | audio_hw_device_t *dev; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 187 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 188 | rc = load_audio_interface(audio_interfaces[i], &mod, &dev); |
| 189 | if (rc) |
| 190 | continue; |
| 191 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 192 | ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i], |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 193 | mod->name, mod->id); |
| 194 | mAudioHwDevs.push(dev); |
| 195 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 196 | if (mPrimaryHardwareDev == NULL) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 197 | mPrimaryHardwareDev = dev; |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 198 | ALOGI("Using '%s' (%s.%s) as the primary audio interface", |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 199 | mod->name, mod->id, audio_interfaces[i]); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 203 | if (mPrimaryHardwareDev == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 204 | ALOGE("Primary audio interface not found"); |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 205 | // proceed, all later accesses to mPrimaryHardwareDev verify it's safe with initCheck() |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 208 | // Currently (mPrimaryHardwareDev == NULL) == (mAudioHwDevs.size() == 0), but the way the |
| 209 | // primary HW dev is selected can change so these conditions might not always be equivalent. |
| 210 | // When that happens, re-visit all the code that assumes this. |
| 211 | |
| 212 | AutoMutex lock(mHardwareLock); |
| 213 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 214 | // Determine the level of master volume support the primary audio HAL has, |
| 215 | // and set the initial master volume at the same time. |
| 216 | float initialVolume = 1.0; |
| 217 | mMasterVolumeSupportLvl = MVS_NONE; |
| 218 | if (0 == mPrimaryHardwareDev->init_check(mPrimaryHardwareDev)) { |
| 219 | audio_hw_device_t *dev = mPrimaryHardwareDev; |
| 220 | |
| 221 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 222 | if ((NULL != dev->get_master_volume) && |
| 223 | (NO_ERROR == dev->get_master_volume(dev, &initialVolume))) { |
| 224 | mMasterVolumeSupportLvl = MVS_FULL; |
| 225 | } else { |
| 226 | mMasterVolumeSupportLvl = MVS_SETONLY; |
| 227 | initialVolume = 1.0; |
| 228 | } |
| 229 | |
| 230 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 231 | if ((NULL == dev->set_master_volume) || |
| 232 | (NO_ERROR != dev->set_master_volume(dev, initialVolume))) { |
| 233 | mMasterVolumeSupportLvl = MVS_NONE; |
| 234 | } |
| 235 | mHardwareStatus = AUDIO_HW_INIT; |
| 236 | } |
| 237 | |
| 238 | // Set the mode for each audio HAL, and try to set the initial volume (if |
| 239 | // supported) for all of the non-primary audio HALs. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 240 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 241 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 242 | |
| 243 | mHardwareStatus = AUDIO_HW_INIT; |
| 244 | rc = dev->init_check(dev); |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 245 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 246 | if (rc == 0) { |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 247 | mMode = AUDIO_MODE_NORMAL; // assigned multiple times with same value |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 248 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 249 | dev->set_mode(dev, mMode); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 250 | |
| 251 | if ((dev != mPrimaryHardwareDev) && |
| 252 | (NULL != dev->set_master_volume)) { |
| 253 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 254 | dev->set_master_volume(dev, initialVolume); |
| 255 | } |
| 256 | |
| 257 | mHardwareStatus = AUDIO_HW_INIT; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 258 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 259 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 260 | |
| 261 | mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl) |
| 262 | ? initialVolume |
| 263 | : 1.0; |
| 264 | mMasterVolume = initialVolume; |
| 265 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | AudioFlinger::~AudioFlinger() |
| 269 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 270 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 271 | while (!mRecordThreads.isEmpty()) { |
| 272 | // closeInput() will remove first entry from mRecordThreads |
| 273 | closeInput(mRecordThreads.keyAt(0)); |
| 274 | } |
| 275 | while (!mPlaybackThreads.isEmpty()) { |
| 276 | // closeOutput() will remove first entry from mPlaybackThreads |
| 277 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 278 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 279 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 280 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 281 | // no mHardwareLock needed, as there are no other references to this |
| 282 | audio_hw_device_close(mAudioHwDevs[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 286 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices) |
| 287 | { |
| 288 | /* first matching HW device is returned */ |
| 289 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 290 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 291 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 292 | return dev; |
| 293 | } |
| 294 | return NULL; |
| 295 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 296 | |
| 297 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 298 | { |
| 299 | const size_t SIZE = 256; |
| 300 | char buffer[SIZE]; |
| 301 | String8 result; |
| 302 | |
| 303 | result.append("Clients:\n"); |
| 304 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 305 | sp<Client> client = mClients.valueAt(i).promote(); |
| 306 | if (client != 0) { |
| 307 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 308 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 311 | |
| 312 | result.append("Global session refs:\n"); |
| 313 | result.append(" session pid cnt\n"); |
| 314 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 315 | AudioSessionRef *r = mAudioSessionRefs[i]; |
| 316 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt); |
| 317 | result.append(buffer); |
| 318 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 319 | write(fd, result.string(), result.size()); |
| 320 | return NO_ERROR; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 325 | { |
| 326 | const size_t SIZE = 256; |
| 327 | char buffer[SIZE]; |
| 328 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 329 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 330 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 331 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 332 | "Standby Time mSec: %u\n", |
| 333 | hardwareStatus, |
| 334 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | result.append(buffer); |
| 336 | write(fd, result.string(), result.size()); |
| 337 | return NO_ERROR; |
| 338 | } |
| 339 | |
| 340 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 341 | { |
| 342 | const size_t SIZE = 256; |
| 343 | char buffer[SIZE]; |
| 344 | String8 result; |
| 345 | snprintf(buffer, SIZE, "Permission Denial: " |
| 346 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 347 | IPCThreadState::self()->getCallingPid(), |
| 348 | IPCThreadState::self()->getCallingUid()); |
| 349 | result.append(buffer); |
| 350 | write(fd, result.string(), result.size()); |
| 351 | return NO_ERROR; |
| 352 | } |
| 353 | |
| 354 | static bool tryLock(Mutex& mutex) |
| 355 | { |
| 356 | bool locked = false; |
| 357 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 358 | if (mutex.tryLock() == NO_ERROR) { |
| 359 | locked = true; |
| 360 | break; |
| 361 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 362 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 363 | } |
| 364 | return locked; |
| 365 | } |
| 366 | |
| 367 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 368 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 369 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 370 | dumpPermissionDenial(fd, args); |
| 371 | } else { |
| 372 | // get state of hardware lock |
| 373 | bool hardwareLocked = tryLock(mHardwareLock); |
| 374 | if (!hardwareLocked) { |
| 375 | String8 result(kHardwareLockedString); |
| 376 | write(fd, result.string(), result.size()); |
| 377 | } else { |
| 378 | mHardwareLock.unlock(); |
| 379 | } |
| 380 | |
| 381 | bool locked = tryLock(mLock); |
| 382 | |
| 383 | // failed to lock - AudioFlinger is probably deadlocked |
| 384 | if (!locked) { |
| 385 | String8 result(kDeadlockedString); |
| 386 | write(fd, result.string(), result.size()); |
| 387 | } |
| 388 | |
| 389 | dumpClients(fd, args); |
| 390 | dumpInternals(fd, args); |
| 391 | |
| 392 | // dump playback threads |
| 393 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 394 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 395 | } |
| 396 | |
| 397 | // dump record threads |
| 398 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 399 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 400 | } |
| 401 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 402 | // dump all hardware devs |
| 403 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 404 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 405 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 406 | } |
| 407 | if (locked) mLock.unlock(); |
| 408 | } |
| 409 | return NO_ERROR; |
| 410 | } |
| 411 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 412 | sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) |
| 413 | { |
| 414 | // If pid is already in the mClients wp<> map, then use that entry |
| 415 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 416 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 417 | if (client == 0) { |
| 418 | client = new Client(this, pid); |
| 419 | mClients.add(pid, client); |
| 420 | } |
| 421 | |
| 422 | return client; |
| 423 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 424 | |
| 425 | // IAudioFlinger interface |
| 426 | |
| 427 | |
| 428 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 429 | pid_t pid, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 430 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 431 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 432 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 433 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 434 | int frameCount, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 435 | // FIXME dead, remove from IAudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 436 | uint32_t flags, |
| 437 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 438 | audio_io_handle_t output, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 439 | bool isTimed, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 440 | int *sessionId, |
| 441 | status_t *status) |
| 442 | { |
| 443 | sp<PlaybackThread::Track> track; |
| 444 | sp<TrackHandle> trackHandle; |
| 445 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 446 | status_t lStatus; |
| 447 | int lSessionId; |
| 448 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 449 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 450 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 451 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 452 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 453 | lStatus = BAD_VALUE; |
| 454 | goto Exit; |
| 455 | } |
| 456 | |
| 457 | { |
| 458 | Mutex::Autolock _l(mLock); |
| 459 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 460 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 462 | ALOGE("unknown output thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 463 | lStatus = BAD_VALUE; |
| 464 | goto Exit; |
| 465 | } |
| 466 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 467 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 468 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 469 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 470 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 471 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 472 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 473 | if (mPlaybackThreads.keyAt(i) != output) { |
| 474 | // prevent same audio session on different output threads |
| 475 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 476 | if (sessions & PlaybackThread::TRACK_SESSION) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 477 | ALOGE("createTrack() session ID %d already in use", *sessionId); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 478 | lStatus = BAD_VALUE; |
| 479 | goto Exit; |
| 480 | } |
| 481 | // check if an effect with same session ID is waiting for a track to be created |
| 482 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 483 | effectThread = t.get(); |
| 484 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 487 | lSessionId = *sessionId; |
| 488 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 489 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 490 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 491 | if (sessionId != NULL) { |
| 492 | *sessionId = lSessionId; |
| 493 | } |
| 494 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 495 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 496 | |
| 497 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 498 | channelMask, frameCount, sharedBuffer, lSessionId, isTimed, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 499 | |
| 500 | // move effect chain to this output thread if an effect on same session was waiting |
| 501 | // for a track to be created |
| 502 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 503 | Mutex::Autolock _dl(thread->mLock); |
| 504 | Mutex::Autolock _sl(effectThread->mLock); |
| 505 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 506 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 507 | } |
| 508 | if (lStatus == NO_ERROR) { |
| 509 | trackHandle = new TrackHandle(track); |
| 510 | } else { |
| 511 | // remove local strong reference to Client before deleting the Track so that the Client |
| 512 | // destructor is called by the TrackBase destructor with mLock held |
| 513 | client.clear(); |
| 514 | track.clear(); |
| 515 | } |
| 516 | |
| 517 | Exit: |
| 518 | if(status) { |
| 519 | *status = lStatus; |
| 520 | } |
| 521 | return trackHandle; |
| 522 | } |
| 523 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 524 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 525 | { |
| 526 | Mutex::Autolock _l(mLock); |
| 527 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 528 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 529 | ALOGW("sampleRate() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | return thread->sampleRate(); |
| 533 | } |
| 534 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 535 | int AudioFlinger::channelCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | { |
| 537 | Mutex::Autolock _l(mLock); |
| 538 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 539 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 540 | ALOGW("channelCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | return thread->channelCount(); |
| 544 | } |
| 545 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 546 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 547 | { |
| 548 | Mutex::Autolock _l(mLock); |
| 549 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 550 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 551 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 552 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | } |
| 554 | return thread->format(); |
| 555 | } |
| 556 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 557 | size_t AudioFlinger::frameCount(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 558 | { |
| 559 | Mutex::Autolock _l(mLock); |
| 560 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 561 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 562 | ALOGW("frameCount() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | return thread->frameCount(); |
| 566 | } |
| 567 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 568 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 569 | { |
| 570 | Mutex::Autolock _l(mLock); |
| 571 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 572 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 573 | ALOGW("latency() unknown thread %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | return thread->latency(); |
| 577 | } |
| 578 | |
| 579 | status_t AudioFlinger::setMasterVolume(float value) |
| 580 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 581 | status_t ret = initCheck(); |
| 582 | if (ret != NO_ERROR) { |
| 583 | return ret; |
| 584 | } |
| 585 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | // check calling permissions |
| 587 | if (!settingsAllowed()) { |
| 588 | return PERMISSION_DENIED; |
| 589 | } |
| 590 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 591 | float swmv = value; |
| 592 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 593 | // when hw supports master volume, don't scale in sw mixer |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 594 | if (MVS_NONE != mMasterVolumeSupportLvl) { |
| 595 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 596 | AutoMutex lock(mHardwareLock); |
| 597 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 598 | |
| 599 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 600 | if (NULL != dev->set_master_volume) { |
| 601 | dev->set_master_volume(dev, value); |
| 602 | } |
| 603 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 604 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 605 | |
| 606 | swmv = 1.0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 607 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 608 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 609 | Mutex::Autolock _l(mLock); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 610 | mMasterVolume = value; |
| 611 | mMasterVolumeSW = swmv; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 612 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 613 | mPlaybackThreads.valueAt(i)->setMasterVolume(swmv); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | |
| 615 | return NO_ERROR; |
| 616 | } |
| 617 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 618 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 619 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 620 | status_t ret = initCheck(); |
| 621 | if (ret != NO_ERROR) { |
| 622 | return ret; |
| 623 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 624 | |
| 625 | // check calling permissions |
| 626 | if (!settingsAllowed()) { |
| 627 | return PERMISSION_DENIED; |
| 628 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 629 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 630 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 631 | return BAD_VALUE; |
| 632 | } |
| 633 | |
| 634 | { // scope for the lock |
| 635 | AutoMutex lock(mHardwareLock); |
| 636 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 637 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 638 | mHardwareStatus = AUDIO_HW_IDLE; |
| 639 | } |
| 640 | |
| 641 | if (NO_ERROR == ret) { |
| 642 | Mutex::Autolock _l(mLock); |
| 643 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 644 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 645 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | status_t AudioFlinger::setMicMute(bool state) |
| 652 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 653 | status_t ret = initCheck(); |
| 654 | if (ret != NO_ERROR) { |
| 655 | return ret; |
| 656 | } |
| 657 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 658 | // check calling permissions |
| 659 | if (!settingsAllowed()) { |
| 660 | return PERMISSION_DENIED; |
| 661 | } |
| 662 | |
| 663 | AutoMutex lock(mHardwareLock); |
| 664 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 665 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 666 | mHardwareStatus = AUDIO_HW_IDLE; |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | bool AudioFlinger::getMicMute() const |
| 671 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 672 | status_t ret = initCheck(); |
| 673 | if (ret != NO_ERROR) { |
| 674 | return false; |
| 675 | } |
| 676 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 677 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 678 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 679 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 680 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 681 | mHardwareStatus = AUDIO_HW_IDLE; |
| 682 | return state; |
| 683 | } |
| 684 | |
| 685 | status_t AudioFlinger::setMasterMute(bool muted) |
| 686 | { |
| 687 | // check calling permissions |
| 688 | if (!settingsAllowed()) { |
| 689 | return PERMISSION_DENIED; |
| 690 | } |
| 691 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 692 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 693 | // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 694 | mMasterMute = muted; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 695 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 696 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
| 697 | |
| 698 | return NO_ERROR; |
| 699 | } |
| 700 | |
| 701 | float AudioFlinger::masterVolume() const |
| 702 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 703 | Mutex::Autolock _l(mLock); |
| 704 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 705 | } |
| 706 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 707 | float AudioFlinger::masterVolumeSW() const |
| 708 | { |
| 709 | Mutex::Autolock _l(mLock); |
| 710 | return masterVolumeSW_l(); |
| 711 | } |
| 712 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 713 | bool AudioFlinger::masterMute() const |
| 714 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 715 | Mutex::Autolock _l(mLock); |
| 716 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 717 | } |
| 718 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 719 | float AudioFlinger::masterVolume_l() const |
| 720 | { |
| 721 | if (MVS_FULL == mMasterVolumeSupportLvl) { |
| 722 | float ret_val; |
| 723 | AutoMutex lock(mHardwareLock); |
| 724 | |
| 725 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 726 | assert(NULL != mPrimaryHardwareDev); |
| 727 | assert(NULL != mPrimaryHardwareDev->get_master_volume); |
| 728 | |
| 729 | mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val); |
| 730 | mHardwareStatus = AUDIO_HW_IDLE; |
| 731 | return ret_val; |
| 732 | } |
| 733 | |
| 734 | return mMasterVolume; |
| 735 | } |
| 736 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 737 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 738 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 739 | { |
| 740 | // check calling permissions |
| 741 | if (!settingsAllowed()) { |
| 742 | return PERMISSION_DENIED; |
| 743 | } |
| 744 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 745 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 746 | ALOGE("setStreamVolume() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 747 | return BAD_VALUE; |
| 748 | } |
| 749 | |
| 750 | AutoMutex lock(mLock); |
| 751 | PlaybackThread *thread = NULL; |
| 752 | if (output) { |
| 753 | thread = checkPlaybackThread_l(output); |
| 754 | if (thread == NULL) { |
| 755 | return BAD_VALUE; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | mStreamTypes[stream].volume = value; |
| 760 | |
| 761 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 762 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 763 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
| 764 | } |
| 765 | } else { |
| 766 | thread->setStreamVolume(stream, value); |
| 767 | } |
| 768 | |
| 769 | return NO_ERROR; |
| 770 | } |
| 771 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 772 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 773 | { |
| 774 | // check calling permissions |
| 775 | if (!settingsAllowed()) { |
| 776 | return PERMISSION_DENIED; |
| 777 | } |
| 778 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 779 | if (uint32_t(stream) >= AUDIO_STREAM_CNT || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 780 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 781 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 782 | return BAD_VALUE; |
| 783 | } |
| 784 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 785 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 786 | mStreamTypes[stream].mute = muted; |
| 787 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 788 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
| 789 | |
| 790 | return NO_ERROR; |
| 791 | } |
| 792 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 793 | float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 794 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 795 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 796 | return 0.0f; |
| 797 | } |
| 798 | |
| 799 | AutoMutex lock(mLock); |
| 800 | float volume; |
| 801 | if (output) { |
| 802 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 803 | if (thread == NULL) { |
| 804 | return 0.0f; |
| 805 | } |
| 806 | volume = thread->streamVolume(stream); |
| 807 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 808 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return volume; |
| 812 | } |
| 813 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 814 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 815 | { |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 816 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 817 | return true; |
| 818 | } |
| 819 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 820 | AutoMutex lock(mLock); |
| 821 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 824 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 825 | { |
| 826 | status_t result; |
| 827 | |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 828 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 829 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 830 | // check calling permissions |
| 831 | if (!settingsAllowed()) { |
| 832 | return PERMISSION_DENIED; |
| 833 | } |
| 834 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 835 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 836 | if (ioHandle == 0) { |
| 837 | AutoMutex lock(mHardwareLock); |
| 838 | mHardwareStatus = AUDIO_SET_PARAMETER; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 839 | status_t final_result = NO_ERROR; |
| 840 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 841 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 842 | result = dev->set_parameters(dev, keyValuePairs.string()); |
| 843 | final_result = result ?: final_result; |
| 844 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 845 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 846 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 847 | AudioParameter param = AudioParameter(keyValuePairs); |
| 848 | String8 value; |
| 849 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
| 850 | Mutex::Autolock _l(mLock); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 851 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 852 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 853 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 854 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 855 | RecordThread::RecordTrack *track = thread->track(); |
| 856 | if (track != NULL) { |
| 857 | audio_devices_t device = (audio_devices_t)( |
| 858 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 859 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 860 | thread->setEffectSuspended(FX_IID_AEC, |
| 861 | suspend, |
| 862 | track->sessionId()); |
| 863 | thread->setEffectSuspended(FX_IID_NS, |
| 864 | suspend, |
| 865 | track->sessionId()); |
| 866 | } |
| 867 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 868 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 871 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 875 | // and the thread is exited once the lock is released |
| 876 | sp<ThreadBase> thread; |
| 877 | { |
| 878 | Mutex::Autolock _l(mLock); |
| 879 | thread = checkPlaybackThread_l(ioHandle); |
| 880 | if (thread == NULL) { |
| 881 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 882 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 883 | // indicate output device change to all input threads for pre processing |
| 884 | AudioParameter param = AudioParameter(keyValuePairs); |
| 885 | int value; |
| 886 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 887 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 888 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 889 | } |
| 890 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 893 | if (thread != 0) { |
| 894 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 895 | } |
| 896 | return BAD_VALUE; |
| 897 | } |
| 898 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 899 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 900 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 901 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 902 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 903 | |
| 904 | if (ioHandle == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 905 | String8 out_s8; |
| 906 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 907 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 908 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 909 | char *s = dev->get_parameters(dev, keys.string()); |
John Grossman | ef7740b | 2012-02-09 11:28:36 -0800 | [diff] [blame] | 910 | out_s8 += String8(s ? s : ""); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 911 | free(s); |
| 912 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 913 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | Mutex::Autolock _l(mLock); |
| 917 | |
| 918 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 919 | if (playbackThread != NULL) { |
| 920 | return playbackThread->getParameters(keys); |
| 921 | } |
| 922 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 923 | if (recordThread != NULL) { |
| 924 | return recordThread->getParameters(keys); |
| 925 | } |
| 926 | return String8(""); |
| 927 | } |
| 928 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 929 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 930 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 931 | status_t ret = initCheck(); |
| 932 | if (ret != NO_ERROR) { |
| 933 | return 0; |
| 934 | } |
| 935 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 936 | AutoMutex lock(mHardwareLock); |
| 937 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
| 938 | size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); |
| 939 | mHardwareStatus = AUDIO_HW_IDLE; |
| 940 | return size; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 943 | unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 944 | { |
| 945 | if (ioHandle == 0) { |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | Mutex::Autolock _l(mLock); |
| 950 | |
| 951 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 952 | if (recordThread != NULL) { |
| 953 | return recordThread->getInputFramesLost(); |
| 954 | } |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | status_t AudioFlinger::setVoiceVolume(float value) |
| 959 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 960 | status_t ret = initCheck(); |
| 961 | if (ret != NO_ERROR) { |
| 962 | return ret; |
| 963 | } |
| 964 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 965 | // check calling permissions |
| 966 | if (!settingsAllowed()) { |
| 967 | return PERMISSION_DENIED; |
| 968 | } |
| 969 | |
| 970 | AutoMutex lock(mHardwareLock); |
| 971 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 972 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 973 | mHardwareStatus = AUDIO_HW_IDLE; |
| 974 | |
| 975 | return ret; |
| 976 | } |
| 977 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 978 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
| 979 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 980 | { |
| 981 | status_t status; |
| 982 | |
| 983 | Mutex::Autolock _l(mLock); |
| 984 | |
| 985 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 986 | if (playbackThread != NULL) { |
| 987 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 988 | } |
| 989 | |
| 990 | return BAD_VALUE; |
| 991 | } |
| 992 | |
| 993 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 994 | { |
| 995 | |
| 996 | Mutex::Autolock _l(mLock); |
| 997 | |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 998 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 999 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1000 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1001 | client, |
| 1002 | pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1003 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1004 | |
| 1005 | mNotificationClients.add(pid, notificationClient); |
| 1006 | |
| 1007 | sp<IBinder> binder = client->asBinder(); |
| 1008 | binder->linkToDeath(notificationClient); |
| 1009 | |
| 1010 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1011 | // with AudioSystem::gLock |
| 1012 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1013 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 1014 | } |
| 1015 | |
| 1016 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1017 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1023 | { |
| 1024 | Mutex::Autolock _l(mLock); |
| 1025 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1026 | ssize_t index = mNotificationClients.indexOfKey(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1027 | if (index >= 0) { |
| 1028 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1029 | ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1030 | mNotificationClients.removeItem(pid); |
| 1031 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1032 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1033 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1034 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1035 | bool removed = false; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1036 | for (size_t i = 0; i< num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1037 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1038 | ALOGV(" pid %d @ %d", ref->pid, i); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1039 | if (ref->pid == pid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1040 | ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1041 | mAudioSessionRefs.removeAt(i); |
| 1042 | delete ref; |
| 1043 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1044 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1045 | } else { |
| 1046 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | if (removed) { |
| 1050 | purgeStaleEffects_l(); |
| 1051 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1055 | void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, void *param2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1056 | { |
| 1057 | size_t size = mNotificationClients.size(); |
| 1058 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1059 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, |
| 1060 | param2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 1065 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1066 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1067 | ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1068 | mClients.removeItem(pid); |
| 1069 | } |
| 1070 | |
| 1071 | |
| 1072 | // ---------------------------------------------------------------------------- |
| 1073 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1074 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 1075 | uint32_t device, type_t type) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1076 | : Thread(false), |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1077 | mType(type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1078 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), |
| 1079 | // mChannelMask |
| 1080 | mChannelCount(0), |
| 1081 | mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), |
| 1082 | mParamStatus(NO_ERROR), |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1083 | mStandby(false), mId(id), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1084 | mDevice(device), |
| 1085 | mDeathRecipient(new PMDeathRecipient(this)) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1086 | { |
| 1087 | } |
| 1088 | |
| 1089 | AudioFlinger::ThreadBase::~ThreadBase() |
| 1090 | { |
| 1091 | mParamCond.broadcast(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1092 | // do not lock the mutex in destructor |
| 1093 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1094 | if (mPowerManager != 0) { |
| 1095 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 1096 | binder->unlinkToDeath(mDeathRecipient); |
| 1097 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | void AudioFlinger::ThreadBase::exit() |
| 1101 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1102 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1103 | { |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1104 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 1105 | // if (!exitPending()) { |
| 1106 | // // context switch from here to exit() |
| 1107 | // // exit() calls requestExit(), what exitPending() observes |
| 1108 | // // exit() calls signal(), which is dropped since no waiters |
| 1109 | // // context switch back from exit() to here |
| 1110 | // mWaitWorkCV.wait(...); |
| 1111 | // // now thread is hung |
| 1112 | // } |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1113 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1114 | requestExit(); |
| 1115 | mWaitWorkCV.signal(); |
| 1116 | } |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1117 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 1118 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1119 | requestExitAndWait(); |
| 1120 | } |
| 1121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1122 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1123 | { |
| 1124 | status_t status; |
| 1125 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1126 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1127 | Mutex::Autolock _l(mLock); |
| 1128 | |
| 1129 | mNewParameters.add(keyValuePairs); |
| 1130 | mWaitWorkCV.signal(); |
| 1131 | // wait condition with timeout in case the thread loop has exited |
| 1132 | // before the request could be processed |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1133 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1134 | status = mParamStatus; |
| 1135 | mWaitWorkCV.signal(); |
| 1136 | } else { |
| 1137 | status = TIMED_OUT; |
| 1138 | } |
| 1139 | return status; |
| 1140 | } |
| 1141 | |
| 1142 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1143 | { |
| 1144 | Mutex::Autolock _l(mLock); |
| 1145 | sendConfigEvent_l(event, param); |
| 1146 | } |
| 1147 | |
| 1148 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1149 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1150 | { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1151 | ConfigEvent configEvent; |
| 1152 | configEvent.mEvent = event; |
| 1153 | configEvent.mParam = param; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1154 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1155 | ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1156 | mWaitWorkCV.signal(); |
| 1157 | } |
| 1158 | |
| 1159 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1160 | { |
| 1161 | mLock.lock(); |
| 1162 | while(!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1163 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1164 | ConfigEvent configEvent = mConfigEvents[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1165 | mConfigEvents.removeAt(0); |
| 1166 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1167 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1168 | mLock.unlock(); |
| 1169 | mAudioFlinger->mLock.lock(); |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1170 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1171 | mAudioFlinger->mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1172 | mLock.lock(); |
| 1173 | } |
| 1174 | mLock.unlock(); |
| 1175 | } |
| 1176 | |
| 1177 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1178 | { |
| 1179 | const size_t SIZE = 256; |
| 1180 | char buffer[SIZE]; |
| 1181 | String8 result; |
| 1182 | |
| 1183 | bool locked = tryLock(mLock); |
| 1184 | if (!locked) { |
| 1185 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1186 | write(fd, buffer, strlen(buffer)); |
| 1187 | } |
| 1188 | |
| 1189 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1190 | result.append(buffer); |
| 1191 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1192 | result.append(buffer); |
| 1193 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); |
| 1194 | result.append(buffer); |
| 1195 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1196 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1197 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1198 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1199 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1200 | result.append(buffer); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1201 | snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1202 | result.append(buffer); |
| 1203 | |
| 1204 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1205 | result.append(buffer); |
| 1206 | result.append(" Index Command"); |
| 1207 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1208 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1209 | result.append(buffer); |
| 1210 | result.append(mNewParameters[i]); |
| 1211 | } |
| 1212 | |
| 1213 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1214 | result.append(buffer); |
| 1215 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1216 | result.append(buffer); |
| 1217 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1218 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1219 | result.append(buffer); |
| 1220 | } |
| 1221 | result.append("\n"); |
| 1222 | |
| 1223 | write(fd, result.string(), result.size()); |
| 1224 | |
| 1225 | if (locked) { |
| 1226 | mLock.unlock(); |
| 1227 | } |
| 1228 | return NO_ERROR; |
| 1229 | } |
| 1230 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1231 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1232 | { |
| 1233 | const size_t SIZE = 256; |
| 1234 | char buffer[SIZE]; |
| 1235 | String8 result; |
| 1236 | |
| 1237 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1238 | write(fd, buffer, strlen(buffer)); |
| 1239 | |
| 1240 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1241 | sp<EffectChain> chain = mEffectChains[i]; |
| 1242 | if (chain != 0) { |
| 1243 | chain->dump(fd, args); |
| 1244 | } |
| 1245 | } |
| 1246 | return NO_ERROR; |
| 1247 | } |
| 1248 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1249 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1250 | { |
| 1251 | Mutex::Autolock _l(mLock); |
| 1252 | acquireWakeLock_l(); |
| 1253 | } |
| 1254 | |
| 1255 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1256 | { |
| 1257 | if (mPowerManager == 0) { |
| 1258 | // use checkService() to avoid blocking if power service is not up yet |
| 1259 | sp<IBinder> binder = |
| 1260 | defaultServiceManager()->checkService(String16("power")); |
| 1261 | if (binder == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1262 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1263 | } else { |
| 1264 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1265 | binder->linkToDeath(mDeathRecipient); |
| 1266 | } |
| 1267 | } |
| 1268 | if (mPowerManager != 0) { |
| 1269 | sp<IBinder> binder = new BBinder(); |
| 1270 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1271 | binder, |
| 1272 | String16(mName)); |
| 1273 | if (status == NO_ERROR) { |
| 1274 | mWakeLockToken = binder; |
| 1275 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1276 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1281 | { |
| 1282 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1283 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1287 | { |
| 1288 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1289 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1290 | if (mPowerManager != 0) { |
| 1291 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1292 | } |
| 1293 | mWakeLockToken.clear(); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1298 | { |
| 1299 | Mutex::Autolock _l(mLock); |
| 1300 | releaseWakeLock_l(); |
| 1301 | mPowerManager.clear(); |
| 1302 | } |
| 1303 | |
| 1304 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1305 | { |
| 1306 | sp<ThreadBase> thread = mThread.promote(); |
| 1307 | if (thread != 0) { |
| 1308 | thread->clearPowerManager(); |
| 1309 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1310 | ALOGW("power manager service died !!!"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1311 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1312 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1313 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1314 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1315 | { |
| 1316 | Mutex::Autolock _l(mLock); |
| 1317 | setEffectSuspended_l(type, suspend, sessionId); |
| 1318 | } |
| 1319 | |
| 1320 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1321 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1322 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 1323 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1324 | if (chain != 0) { |
| 1325 | if (type != NULL) { |
| 1326 | chain->setEffectSuspended_l(type, suspend); |
| 1327 | } else { |
| 1328 | chain->setEffectSuspendedAll_l(suspend); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1333 | } |
| 1334 | |
| 1335 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1336 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1337 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1338 | if (index < 0) { |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1343 | mSuspendedSessions.editValueAt(index); |
| 1344 | |
| 1345 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1346 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1347 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1348 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1349 | chain->setEffectSuspendedAll_l(true); |
| 1350 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1351 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1352 | desc->mType.timeLow); |
| 1353 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1359 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1360 | bool suspend, |
| 1361 | int sessionId) |
| 1362 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1363 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1364 | |
| 1365 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1366 | |
| 1367 | if (suspend) { |
| 1368 | if (index >= 0) { |
| 1369 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1370 | } else { |
| 1371 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1372 | } |
| 1373 | } else { |
| 1374 | if (index < 0) { |
| 1375 | return; |
| 1376 | } |
| 1377 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | int key = EffectChain::kKeyForSuspendAll; |
| 1382 | if (type != NULL) { |
| 1383 | key = type->timeLow; |
| 1384 | } |
| 1385 | index = sessionEffects.indexOfKey(key); |
| 1386 | |
| 1387 | sp <SuspendedSessionDesc> desc; |
| 1388 | if (suspend) { |
| 1389 | if (index >= 0) { |
| 1390 | desc = sessionEffects.valueAt(index); |
| 1391 | } else { |
| 1392 | desc = new SuspendedSessionDesc(); |
| 1393 | if (type != NULL) { |
| 1394 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1395 | } |
| 1396 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1397 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1398 | } |
| 1399 | desc->mRefCount++; |
| 1400 | } else { |
| 1401 | if (index < 0) { |
| 1402 | return; |
| 1403 | } |
| 1404 | desc = sessionEffects.valueAt(index); |
| 1405 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1406 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1407 | sessionEffects.removeItemsAt(index); |
| 1408 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1409 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1410 | sessionId); |
| 1411 | mSuspendedSessions.removeItem(sessionId); |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | if (!sessionEffects.isEmpty()) { |
| 1416 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1421 | bool enabled, |
| 1422 | int sessionId) |
| 1423 | { |
| 1424 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1425 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1426 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1427 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1428 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1429 | bool enabled, |
| 1430 | int sessionId) |
| 1431 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1432 | if (mType != RECORD) { |
| 1433 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1434 | // another session. This gives the priority to well behaved effect control panels |
| 1435 | // and applications not using global effects. |
| 1436 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1437 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1438 | } |
| 1439 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1440 | |
| 1441 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1442 | if (chain != 0) { |
| 1443 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1447 | // ---------------------------------------------------------------------------- |
| 1448 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1449 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1450 | AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1451 | audio_io_handle_t id, |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1452 | uint32_t device, |
| 1453 | type_t type) |
| 1454 | : ThreadBase(audioFlinger, id, device, type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1455 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), |
| 1456 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1457 | // but it would be safer to explicitly pass initial masterMute as parameter |
| 1458 | mMasterMute(audioFlinger->masterMute_l()), |
| 1459 | // mStreamTypes[] initialized in constructor body |
| 1460 | mOutput(output), |
| 1461 | // Assumes constructor is called by AudioFlinger with it's mLock held, |
| 1462 | // but it would be safer to explicitly pass initial masterVolume as parameter |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1463 | mMasterVolume(audioFlinger->masterVolumeSW_l()), |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1464 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1465 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1466 | snprintf(mName, kNameLength, "AudioOut_%d", id); |
| 1467 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1468 | readOutputParameters(); |
| 1469 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1470 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1471 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
| 1472 | for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; |
| 1473 | stream = (audio_stream_type_t) (stream + 1)) { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1474 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1475 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 1476 | // initialized by stream_type_t default constructor |
| 1477 | // mStreamTypes[stream].valid = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1478 | } |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1479 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1480 | // because mAudioFlinger doesn't have one to copy from |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1484 | { |
| 1485 | delete [] mMixBuffer; |
| 1486 | } |
| 1487 | |
| 1488 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1489 | { |
| 1490 | dumpInternals(fd, args); |
| 1491 | dumpTracks(fd, args); |
| 1492 | dumpEffectChains(fd, args); |
| 1493 | return NO_ERROR; |
| 1494 | } |
| 1495 | |
| 1496 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1497 | { |
| 1498 | const size_t SIZE = 256; |
| 1499 | char buffer[SIZE]; |
| 1500 | String8 result; |
| 1501 | |
| 1502 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1503 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1504 | result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1505 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1506 | sp<Track> track = mTracks[i]; |
| 1507 | if (track != 0) { |
| 1508 | track->dump(buffer, SIZE); |
| 1509 | result.append(buffer); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1514 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1515 | result.append(" Name Clien Typ Fmt Chn mask Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1516 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 1517 | sp<Track> track = mActiveTracks[i].promote(); |
| 1518 | if (track != 0) { |
| 1519 | track->dump(buffer, SIZE); |
| 1520 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | write(fd, result.string(), result.size()); |
| 1524 | return NO_ERROR; |
| 1525 | } |
| 1526 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1527 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1528 | { |
| 1529 | const size_t SIZE = 256; |
| 1530 | char buffer[SIZE]; |
| 1531 | String8 result; |
| 1532 | |
| 1533 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1534 | result.append(buffer); |
| 1535 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1536 | result.append(buffer); |
| 1537 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1538 | result.append(buffer); |
| 1539 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1540 | result.append(buffer); |
| 1541 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1542 | result.append(buffer); |
| 1543 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1544 | result.append(buffer); |
| 1545 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1546 | result.append(buffer); |
| 1547 | write(fd, result.string(), result.size()); |
| 1548 | |
| 1549 | dumpBase(fd, args); |
| 1550 | |
| 1551 | return NO_ERROR; |
| 1552 | } |
| 1553 | |
| 1554 | // Thread virtuals |
| 1555 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1556 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1557 | status_t status = initCheck(); |
| 1558 | if (status == NO_ERROR) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1559 | ALOGI("AudioFlinger's thread %p ready to run", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1560 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1561 | ALOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1562 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1563 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1567 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1568 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1572 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1573 | const sp<AudioFlinger::Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1574 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1575 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1576 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1577 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1578 | int frameCount, |
| 1579 | const sp<IMemory>& sharedBuffer, |
| 1580 | int sessionId, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1581 | bool isTimed, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1582 | status_t *status) |
| 1583 | { |
| 1584 | sp<Track> track; |
| 1585 | status_t lStatus; |
| 1586 | |
| 1587 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1588 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1589 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1590 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1591 | "for output %p with format %d", |
| 1592 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1593 | lStatus = BAD_VALUE; |
| 1594 | goto Exit; |
| 1595 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1596 | } |
| 1597 | } else { |
| 1598 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1599 | if (sampleRate > mSampleRate*2) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1600 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1601 | lStatus = BAD_VALUE; |
| 1602 | goto Exit; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1606 | lStatus = initCheck(); |
| 1607 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1608 | ALOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1609 | goto Exit; |
| 1610 | } |
| 1611 | |
| 1612 | { // scope for mLock |
| 1613 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1614 | |
| 1615 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1616 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1617 | // manager |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1618 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1619 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1620 | sp<Track> t = mTracks[i]; |
| 1621 | if (t != 0) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1622 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1623 | if (sessionId == t->sessionId() && strategy != actual) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1624 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1625 | strategy, actual); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1626 | lStatus = BAD_VALUE; |
| 1627 | goto Exit; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1632 | if (!isTimed) { |
| 1633 | track = new Track(this, client, streamType, sampleRate, format, |
| 1634 | channelMask, frameCount, sharedBuffer, sessionId); |
| 1635 | } else { |
| 1636 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
| 1637 | channelMask, frameCount, sharedBuffer, sessionId); |
| 1638 | } |
| 1639 | if (track == NULL || track->getCblk() == NULL || track->name() < 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1640 | lStatus = NO_MEMORY; |
| 1641 | goto Exit; |
| 1642 | } |
| 1643 | mTracks.add(track); |
| 1644 | |
| 1645 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1646 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1647 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1648 | track->setMainBuffer(chain->inBuffer()); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1649 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1650 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1651 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1652 | |
| 1653 | // invalidate track immediately if the stream type was moved to another thread since |
| 1654 | // createTrack() was called by the client process. |
| 1655 | if (!mStreamTypes[streamType].valid) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1656 | ALOGW("createTrack_l() on thread %p: invalidating track on stream %d", |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1657 | this, streamType); |
| 1658 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); |
| 1659 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1660 | } |
| 1661 | lStatus = NO_ERROR; |
| 1662 | |
| 1663 | Exit: |
| 1664 | if(status) { |
| 1665 | *status = lStatus; |
| 1666 | } |
| 1667 | return track; |
| 1668 | } |
| 1669 | |
| 1670 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1671 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1672 | Mutex::Autolock _l(mLock); |
| 1673 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1674 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1675 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | } |
| 1679 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1680 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1681 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1682 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1683 | mMasterVolume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1686 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1687 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1688 | Mutex::Autolock _l(mLock); |
| 1689 | setMasterMute_l(muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1692 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1693 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1694 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1695 | mStreamTypes[stream].volume = value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1698 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1699 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1700 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1701 | mStreamTypes[stream].mute = muted; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1704 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1705 | { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1706 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1707 | return mStreamTypes[stream].volume; |
| 1708 | } |
| 1709 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1710 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1711 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1712 | { |
| 1713 | status_t status = ALREADY_EXISTS; |
| 1714 | |
| 1715 | // set retry count for buffer fill |
| 1716 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1717 | if (mActiveTracks.indexOf(track) < 0) { |
| 1718 | // the track is newly added, make sure it fills up all its |
| 1719 | // buffers before playing. This is to ensure the client will |
| 1720 | // effectively get the latency it requested. |
| 1721 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1722 | track->mResetDone = false; |
| 1723 | mActiveTracks.add(track); |
| 1724 | if (track->mainBuffer() != mMixBuffer) { |
| 1725 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1726 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1727 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1728 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | status = NO_ERROR; |
| 1733 | } |
| 1734 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1735 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1736 | mWaitWorkCV.broadcast(); |
| 1737 | |
| 1738 | return status; |
| 1739 | } |
| 1740 | |
| 1741 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1742 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1743 | { |
| 1744 | track->mState = TrackBase::TERMINATED; |
| 1745 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1746 | removeTrack_l(track); |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1751 | { |
| 1752 | mTracks.remove(track); |
| 1753 | deleteTrackName_l(track->name()); |
| 1754 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1755 | if (chain != 0) { |
| 1756 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1761 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1762 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1763 | char *s; |
| 1764 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1765 | Mutex::Autolock _l(mLock); |
| 1766 | if (initCheck() != NO_ERROR) { |
| 1767 | return out_s8; |
| 1768 | } |
| 1769 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1770 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1771 | out_s8 = String8(s); |
| 1772 | free(s); |
| 1773 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1776 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1777 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1778 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1779 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1780 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1781 | ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1782 | |
| 1783 | switch (event) { |
| 1784 | case AudioSystem::OUTPUT_OPENED: |
| 1785 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1786 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1787 | desc.samplingRate = mSampleRate; |
| 1788 | desc.format = mFormat; |
| 1789 | desc.frameCount = mFrameCount; |
| 1790 | desc.latency = latency(); |
| 1791 | param2 = &desc; |
| 1792 | break; |
| 1793 | |
| 1794 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1795 | param2 = ¶m; |
| 1796 | case AudioSystem::OUTPUT_CLOSED: |
| 1797 | default: |
| 1798 | break; |
| 1799 | } |
| 1800 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1801 | } |
| 1802 | |
| 1803 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1804 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1805 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1806 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1807 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1808 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 1809 | mFrameSize = audio_stream_frame_size(&mOutput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1810 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1811 | |
| 1812 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1813 | // Allocate a stereo buffer even if HW output is mono. |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 1814 | delete[] mMixBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1815 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1816 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
| 1817 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1818 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1819 | // parameters into account |
| 1820 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1821 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1822 | // matter. |
| 1823 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1824 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1825 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1826 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1827 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1831 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 1832 | if (halFrames == NULL || dspFrames == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1833 | return BAD_VALUE; |
| 1834 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1835 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1836 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1837 | return INVALID_OPERATION; |
| 1838 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1839 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1840 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1841 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1844 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1845 | { |
| 1846 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1847 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1848 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1849 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1853 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1854 | if (sessionId == track->sessionId() && |
| 1855 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1856 | result |= TRACK_SESSION; |
| 1857 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1861 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1864 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1865 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1866 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1867 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1868 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1869 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1870 | } |
| 1871 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1872 | sp<Track> track = mTracks[i]; |
| 1873 | if (sessionId == track->sessionId() && |
| 1874 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 1875 | return AudioSystem::getStrategyForStream(track->streamType()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1878 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1881 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 1882 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1883 | { |
| 1884 | Mutex::Autolock _l(mLock); |
| 1885 | return mOutput; |
| 1886 | } |
| 1887 | |
| 1888 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 1889 | { |
| 1890 | Mutex::Autolock _l(mLock); |
| 1891 | AudioStreamOut *output = mOutput; |
| 1892 | mOutput = NULL; |
| 1893 | return output; |
| 1894 | } |
| 1895 | |
| 1896 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 1897 | audio_stream_t* AudioFlinger::PlaybackThread::stream() |
| 1898 | { |
| 1899 | if (mOutput == NULL) { |
| 1900 | return NULL; |
| 1901 | } |
| 1902 | return &mOutput->stream->common; |
| 1903 | } |
| 1904 | |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1905 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() |
| 1906 | { |
| 1907 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 1908 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 1909 | // underruns |
| 1910 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
| 1911 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); |
| 1912 | } else { |
| 1913 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 1914 | } |
| 1915 | } |
| 1916 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1917 | // ---------------------------------------------------------------------------- |
| 1918 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1919 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1920 | audio_io_handle_t id, uint32_t device, type_t type) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 1921 | : PlaybackThread(audioFlinger, output, id, device, type), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1922 | mAudioMixer(new AudioMixer(mFrameCount, mSampleRate)), |
| 1923 | mPrevMixerStatus(MIXER_IDLE) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1924 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1925 | // FIXME - Current mixer implementation only supports stereo output |
| 1926 | if (mChannelCount == 1) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1927 | ALOGE("Invalid audio hardware channel count"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | AudioFlinger::MixerThread::~MixerThread() |
| 1932 | { |
| 1933 | delete mAudioMixer; |
| 1934 | } |
| 1935 | |
| 1936 | bool AudioFlinger::MixerThread::threadLoop() |
| 1937 | { |
| 1938 | Vector< sp<Track> > tracksToRemove; |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 1939 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1940 | nsecs_t standbyTime = systemTime(); |
| 1941 | size_t mixBufferSize = mFrameCount * mFrameSize; |
| 1942 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1943 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1944 | // increase threshold again due to low power audio mode. The way this warning threshold is |
| 1945 | // calculated and its usefulness should be reconsidered anyway. |
| 1946 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1947 | nsecs_t lastWarning = 0; |
| 1948 | bool longStandbyExit = false; |
| 1949 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1950 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1951 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1952 | uint32_t sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1953 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1954 | #ifdef DEBUG_CPU_USAGE |
| 1955 | ThreadCpuUsage cpu; |
| 1956 | const CentralTendencyStatistics& stats = cpu.statistics(); |
| 1957 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1958 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1959 | acquireWakeLock(); |
| 1960 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1961 | while (!exitPending()) |
| 1962 | { |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1963 | #ifdef DEBUG_CPU_USAGE |
| 1964 | cpu.sampleAndEnable(); |
| 1965 | unsigned n = stats.n(); |
| 1966 | // cpu.elapsed() is expensive, so don't call it every loop |
| 1967 | if ((n & 127) == 1) { |
| 1968 | long long elapsed = cpu.elapsed(); |
| 1969 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 1970 | double perLoop = elapsed / (double) n; |
| 1971 | double perLoop100 = perLoop * 0.01; |
| 1972 | double mean = stats.mean(); |
| 1973 | double stddev = stats.stddev(); |
| 1974 | double minimum = stats.minimum(); |
| 1975 | double maximum = stats.maximum(); |
| 1976 | cpu.resetStatistics(); |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1977 | ALOGI("CPU usage over past %.1f secs (%u mixer loops at %.1f mean ms per loop):\n us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f", |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1978 | elapsed * .000000001, n, perLoop * .000001, |
| 1979 | mean * .001, |
| 1980 | stddev * .001, |
| 1981 | minimum * .001, |
| 1982 | maximum * .001, |
| 1983 | mean / perLoop100, |
| 1984 | stddev / perLoop100, |
| 1985 | minimum / perLoop100, |
| 1986 | maximum / perLoop100); |
| 1987 | } |
| 1988 | } |
| 1989 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 | processConfigEvents(); |
| 1991 | |
| 1992 | mixerStatus = MIXER_IDLE; |
| 1993 | { // scope for mLock |
| 1994 | |
| 1995 | Mutex::Autolock _l(mLock); |
| 1996 | |
| 1997 | if (checkForNewParameters_l()) { |
| 1998 | mixBufferSize = mFrameCount * mFrameSize; |
| 1999 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 2000 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2001 | // increase threshold again due to low power audio mode. The way this warning |
| 2002 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 2003 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2004 | activeSleepTime = activeSleepTimeUs(); |
| 2005 | idleSleepTime = idleSleepTimeUs(); |
| 2006 | } |
| 2007 | |
| 2008 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 2009 | |
| 2010 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2011 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 2012 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2013 | if (!mStandby) { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2014 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d", this, mSuspended); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2015 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2016 | mStandby = true; |
| 2017 | mBytesWritten = 0; |
| 2018 | } |
| 2019 | |
| 2020 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 2021 | // we're about to wait, flush the binder command buffer |
| 2022 | IPCThreadState::self()->flushCommands(); |
| 2023 | |
| 2024 | if (exitPending()) break; |
| 2025 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2026 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2027 | // wait until we have something to do... |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2028 | ALOGV("MixerThread %p TID %d going to sleep", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2029 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2030 | ALOGV("MixerThread %p TID %d waking up", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2031 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2032 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2033 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 2034 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2035 | char value[PROPERTY_VALUE_MAX]; |
| 2036 | property_get("ro.audio.silent", value, "0"); |
| 2037 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2038 | ALOGD("Silence is golden"); |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 2039 | setMasterMute_l(true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2043 | standbyTime = systemTime() + mStandbyTimeInNsecs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2044 | sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2045 | sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2046 | continue; |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 2051 | |
| 2052 | // prevent any changes in effect chain list and in each effect chain |
| 2053 | // during mixing and effect process as the audio buffers could be deleted |
| 2054 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2055 | lockEffectChains_l(effectChains); |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2056 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2057 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2058 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2059 | // obtain the presentation timestamp of the next output buffer |
| 2060 | int64_t pts; |
| 2061 | status_t status = INVALID_OPERATION; |
| 2062 | |
| 2063 | if (NULL != mOutput->stream->get_next_write_timestamp) { |
| 2064 | status = mOutput->stream->get_next_write_timestamp( |
| 2065 | mOutput->stream, &pts); |
| 2066 | } |
| 2067 | |
| 2068 | if (status != NO_ERROR) { |
| 2069 | pts = AudioBufferProvider::kInvalidPTS; |
| 2070 | } |
| 2071 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2072 | // mix buffers... |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2073 | mAudioMixer->process(pts); |
Eric Laurent | 21e4b6e | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 2074 | // increase sleep time progressively when application underrun condition clears. |
| 2075 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 2076 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 2077 | // such that we would underrun the audio HAL. |
| 2078 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2079 | sleepTimeShift--; |
| 2080 | } |
Eric Laurent | 21e4b6e | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 2081 | sleepTime = 0; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2082 | standbyTime = systemTime() + mStandbyTimeInNsecs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2083 | //TODO: delay standby when effects have a tail |
| 2084 | } else { |
| 2085 | // If no tracks are ready, sleep once for the duration of an output |
| 2086 | // buffer size, then write 0s to the output |
| 2087 | if (sleepTime == 0) { |
| 2088 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 2089 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 2090 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 2091 | sleepTime = kMinThreadSleepTimeUs; |
| 2092 | } |
| 2093 | // reduce sleep time in case of consecutive application underruns to avoid |
| 2094 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 2095 | // duration we would end up writing less data than needed by the audio HAL if |
| 2096 | // the condition persists. |
| 2097 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2098 | sleepTimeShift++; |
| 2099 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2100 | } else { |
| 2101 | sleepTime = idleSleepTime; |
| 2102 | } |
| 2103 | } else if (mBytesWritten != 0 || |
| 2104 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
| 2105 | memset (mMixBuffer, 0, mixBufferSize); |
| 2106 | sleepTime = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2107 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2108 | } |
| 2109 | // TODO add standby time extension fct of effect tail |
| 2110 | } |
| 2111 | |
| 2112 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2113 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2114 | } |
| 2115 | // sleepTime == 0 means we must write to audio hardware |
| 2116 | if (sleepTime == 0) { |
Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2117 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2118 | effectChains[i]->process_l(); |
| 2119 | } |
| 2120 | // enable changes in effect chain |
| 2121 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2122 | mLastWriteTime = systemTime(); |
| 2123 | mInWrite = true; |
| 2124 | mBytesWritten += mixBufferSize; |
| 2125 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2126 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2127 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2128 | mNumWrites++; |
| 2129 | mInWrite = false; |
| 2130 | nsecs_t now = systemTime(); |
| 2131 | nsecs_t delta = now - mLastWriteTime; |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2132 | if (!mStandby && delta > maxPeriod) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2133 | mNumDelayedWrites++; |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2134 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2135 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2136 | ns2ms(delta), mNumDelayedWrites, this); |
| 2137 | lastWarning = now; |
| 2138 | } |
| 2139 | if (mStandby) { |
| 2140 | longStandbyExit = true; |
| 2141 | } |
| 2142 | } |
| 2143 | mStandby = false; |
| 2144 | } else { |
| 2145 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2146 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2147 | usleep(sleepTime); |
| 2148 | } |
| 2149 | |
| 2150 | // finally let go of all our tracks, without the lock held |
| 2151 | // since we can't guarantee the destructors won't acquire that |
| 2152 | // same lock. |
| 2153 | tracksToRemove.clear(); |
| 2154 | |
| 2155 | // Effect chains will be actually deleted here if they were removed from |
| 2156 | // mEffectChains list during mixing or effects processing |
| 2157 | effectChains.clear(); |
| 2158 | } |
| 2159 | |
| 2160 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2161 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2164 | releaseWakeLock(); |
| 2165 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2166 | ALOGV("MixerThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2167 | return false; |
| 2168 | } |
| 2169 | |
| 2170 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2171 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 2172 | const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2173 | { |
| 2174 | |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2175 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2176 | // find out which tracks need to be processed |
| 2177 | size_t count = activeTracks.size(); |
| 2178 | size_t mixedTracks = 0; |
| 2179 | size_t tracksWithEffect = 0; |
| 2180 | |
| 2181 | float masterVolume = mMasterVolume; |
| 2182 | bool masterMute = mMasterMute; |
| 2183 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2184 | if (masterMute) { |
| 2185 | masterVolume = 0; |
| 2186 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2187 | // Delegate master volume control to effect in output mix effect chain if needed |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2188 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2189 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2190 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2191 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2192 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2193 | chain.clear(); |
| 2194 | } |
| 2195 | |
| 2196 | for (size_t i=0 ; i<count ; i++) { |
| 2197 | sp<Track> t = activeTracks[i].promote(); |
| 2198 | if (t == 0) continue; |
| 2199 | |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2200 | // this const just means the local variable doesn't change |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2201 | Track* const track = t.get(); |
| 2202 | audio_track_cblk_t* cblk = track->cblk(); |
| 2203 | |
| 2204 | // The first time a track is added we wait |
| 2205 | // for all its buffers to be filled before processing it |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2206 | int name = track->name(); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2207 | // make sure that we have enough frames to mix one full buffer. |
| 2208 | // enforce this condition only once to enable draining the buffer in case the client |
| 2209 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2210 | // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2211 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2212 | uint32_t minFrames = 1; |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2213 | if (!track->isStopped() && !track->isPausing() && |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2214 | (mPrevMixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2215 | if (t->sampleRate() == (int)mSampleRate) { |
| 2216 | minFrames = mFrameCount; |
| 2217 | } else { |
Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2218 | // +1 for rounding and +1 for additional sample needed for interpolation |
| 2219 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
| 2220 | // add frames already consumed but not yet released by the resampler |
| 2221 | // because cblk->framesReady() will include these frames |
| 2222 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2223 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2224 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2225 | // of unreleased frames after each pass, but just in case... |
Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2226 | ALOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2227 | } |
| 2228 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2229 | if ((track->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2230 | !track->isPaused() && !track->isTerminated()) |
| 2231 | { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2232 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2233 | |
| 2234 | mixedTracks++; |
| 2235 | |
| 2236 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2237 | // connected to the track |
| 2238 | chain.clear(); |
| 2239 | if (track->mainBuffer() != mMixBuffer) { |
| 2240 | chain = getEffectChain_l(track->sessionId()); |
| 2241 | // Delegate volume control to effect in track effect chain if needed |
| 2242 | if (chain != 0) { |
| 2243 | tracksWithEffect++; |
| 2244 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2245 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d", |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2246 | name, track->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | int param = AudioMixer::VOLUME; |
| 2252 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2253 | // no ramp for the first volume setting |
| 2254 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2255 | if (track->mState == TrackBase::RESUMING) { |
| 2256 | track->mState = TrackBase::ACTIVE; |
| 2257 | param = AudioMixer::RAMP_VOLUME; |
| 2258 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2259 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2260 | } else if (cblk->server != 0) { |
| 2261 | // If the track is stopped before the first frame was mixed, |
| 2262 | // do not apply ramp |
| 2263 | param = AudioMixer::RAMP_VOLUME; |
| 2264 | } |
| 2265 | |
| 2266 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2267 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2268 | if (track->isMuted() || track->isPausing() || |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2269 | mStreamTypes[track->streamType()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2270 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2271 | if (track->isPausing()) { |
| 2272 | track->setPaused(); |
| 2273 | } |
| 2274 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2275 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2276 | // read original volumes with volume control |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2277 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2278 | float v = masterVolume * typeVolume; |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 2279 | uint32_t vlr = cblk->getVolumeLR(); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2280 | vl = vlr & 0xFFFF; |
| 2281 | vr = vlr >> 16; |
| 2282 | // track volumes come from shared memory, so can't be trusted and must be clamped |
| 2283 | if (vl > MAX_GAIN_INT) { |
| 2284 | ALOGV("Track left volume out of range: %04X", vl); |
| 2285 | vl = MAX_GAIN_INT; |
| 2286 | } |
| 2287 | if (vr > MAX_GAIN_INT) { |
| 2288 | ALOGV("Track right volume out of range: %04X", vr); |
| 2289 | vr = MAX_GAIN_INT; |
| 2290 | } |
| 2291 | // now apply the master volume and stream type volume |
| 2292 | vl = (uint32_t)(v * vl) << 12; |
| 2293 | vr = (uint32_t)(v * vr) << 12; |
| 2294 | // assuming master volume and stream type volume each go up to 1.0, |
| 2295 | // vl and vr are now in 8.24 format |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2296 | |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2297 | uint16_t sendLevel = cblk->getSendLevel_U4_12(); |
| 2298 | // send level comes from shared memory and so may be corrupt |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2299 | if (sendLevel > MAX_GAIN_INT) { |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2300 | ALOGV("Track send level out of range: %04X", sendLevel); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2301 | sendLevel = MAX_GAIN_INT; |
Glenn Kasten | 05632a5 | 2012-01-03 14:22:33 -0800 | [diff] [blame] | 2302 | } |
| 2303 | va = (uint32_t)(v * sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2304 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2305 | // Delegate volume control to effect in track effect chain if needed |
| 2306 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2307 | // Do not ramp volume if volume is controlled by effect |
| 2308 | param = AudioMixer::VOLUME; |
| 2309 | track->mHasVolumeController = true; |
| 2310 | } else { |
| 2311 | // force no volume ramp when volume controller was just disabled or removed |
| 2312 | // from effect chain to avoid volume spike |
| 2313 | if (track->mHasVolumeController) { |
| 2314 | param = AudioMixer::VOLUME; |
| 2315 | } |
| 2316 | track->mHasVolumeController = false; |
| 2317 | } |
| 2318 | |
| 2319 | // Convert volumes from 8.24 to 4.12 format |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2320 | // This additional clamping is needed in case chain->setVolume_l() overshot |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2321 | vl = (vl + (1 << 11)) >> 12; |
| 2322 | if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT; |
| 2323 | vr = (vr + (1 << 11)) >> 12; |
| 2324 | if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT; |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2325 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2326 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; // va is uint32_t, so no need to check for - |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2327 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2328 | // XXX: these things DON'T need to be done each time |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2329 | mAudioMixer->setBufferProvider(name, track); |
| 2330 | mAudioMixer->enable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2331 | |
Glenn Kasten | 3b81aca | 2012-01-27 15:26:23 -0800 | [diff] [blame] | 2332 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); |
| 2333 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); |
| 2334 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2335 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2336 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2337 | AudioMixer::TRACK, |
| 2338 | AudioMixer::FORMAT, (void *)track->format()); |
| 2339 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2340 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2341 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2342 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2343 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2344 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2345 | AudioMixer::RESAMPLE, |
| 2346 | AudioMixer::SAMPLE_RATE, |
| 2347 | (void *)(cblk->sampleRate)); |
| 2348 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2349 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2350 | AudioMixer::TRACK, |
| 2351 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2352 | mAudioMixer->setParameter( |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2353 | name, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2354 | AudioMixer::TRACK, |
| 2355 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 2356 | |
| 2357 | // reset retry count |
| 2358 | track->mRetryCount = kMaxTrackRetries; |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2359 | // If one track is ready, set the mixer ready if: |
| 2360 | // - the mixer was not ready during previous round OR |
| 2361 | // - no other track is not ready |
| 2362 | if (mPrevMixerStatus != MIXER_TRACKS_READY || |
| 2363 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 2364 | mixerStatus = MIXER_TRACKS_READY; |
| 2365 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2366 | } else { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2367 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2368 | if (track->isStopped()) { |
| 2369 | track->reset(); |
| 2370 | } |
| 2371 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2372 | // We have consumed all the buffers of this track. |
| 2373 | // Remove it from the list of active tracks. |
| 2374 | tracksToRemove->add(track); |
| 2375 | } else { |
| 2376 | // No buffers for this track. Give it a few chances to |
| 2377 | // fill a buffer, then remove it from active list. |
| 2378 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2379 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2380 | tracksToRemove->add(track); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2381 | // indicate to client process that the track was disabled because of underrun |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2382 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2383 | // If one track is not ready, mark the mixer also not ready if: |
| 2384 | // - the mixer was ready during previous round OR |
| 2385 | // - no other track is ready |
| 2386 | } else if (mPrevMixerStatus == MIXER_TRACKS_READY || |
| 2387 | mixerStatus != MIXER_TRACKS_READY) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2388 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2389 | } |
| 2390 | } |
Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2391 | mAudioMixer->disable(name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | // remove all the tracks that need to be... |
| 2396 | count = tracksToRemove->size(); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2397 | if (CC_UNLIKELY(count)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2398 | for (size_t i=0 ; i<count ; i++) { |
| 2399 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 2400 | mActiveTracks.remove(track); |
| 2401 | if (track->mainBuffer() != mMixBuffer) { |
| 2402 | chain = getEffectChain_l(track->sessionId()); |
| 2403 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2404 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2405 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2409 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | // mix buffer must be cleared if all tracks are connected to an |
| 2415 | // effect chain as in this case the mixer will not write to |
| 2416 | // mix buffer and track effects will accumulate into it |
| 2417 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 2418 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 2419 | } |
| 2420 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 2421 | mPrevMixerStatus = mixerStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2422 | return mixerStatus; |
| 2423 | } |
| 2424 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2425 | void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2426 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2427 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2428 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2429 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2430 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2431 | size_t size = mTracks.size(); |
| 2432 | for (size_t i = 0; i < size; i++) { |
| 2433 | sp<Track> t = mTracks[i]; |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2434 | if (t->streamType() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2435 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2436 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2441 | void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid) |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2442 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2443 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2444 | this, streamType, valid); |
| 2445 | Mutex::Autolock _l(mLock); |
| 2446 | |
| 2447 | mStreamTypes[streamType].valid = valid; |
| 2448 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2449 | |
| 2450 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2451 | int AudioFlinger::MixerThread::getTrackName_l() |
| 2452 | { |
| 2453 | return mAudioMixer->getTrackName(); |
| 2454 | } |
| 2455 | |
| 2456 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2457 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 2458 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2459 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2460 | mAudioMixer->deleteTrackName(name); |
| 2461 | } |
| 2462 | |
| 2463 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2464 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 2465 | { |
| 2466 | bool reconfig = false; |
| 2467 | |
| 2468 | while (!mNewParameters.isEmpty()) { |
| 2469 | status_t status = NO_ERROR; |
| 2470 | String8 keyValuePair = mNewParameters[0]; |
| 2471 | AudioParameter param = AudioParameter(keyValuePair); |
| 2472 | int value; |
| 2473 | |
| 2474 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 2475 | reconfig = true; |
| 2476 | } |
| 2477 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 2478 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2479 | status = BAD_VALUE; |
| 2480 | } else { |
| 2481 | reconfig = true; |
| 2482 | } |
| 2483 | } |
| 2484 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2485 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2486 | status = BAD_VALUE; |
| 2487 | } else { |
| 2488 | reconfig = true; |
| 2489 | } |
| 2490 | } |
| 2491 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2492 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 2493 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2494 | // if frame count is changed after track creation |
| 2495 | if (!mTracks.isEmpty()) { |
| 2496 | status = INVALID_OPERATION; |
| 2497 | } else { |
| 2498 | reconfig = true; |
| 2499 | } |
| 2500 | } |
| 2501 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2502 | // when changing the audio output device, call addBatteryData to notify |
| 2503 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2504 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2505 | uint32_t params = 0; |
| 2506 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2507 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2508 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 2509 | } |
| 2510 | |
| 2511 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2512 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2513 | // check if any other device (except speaker) is on |
| 2514 | if (value & deviceWithoutSpeaker ) { |
| 2515 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 2516 | } |
| 2517 | |
| 2518 | if (params != 0) { |
| 2519 | addBatteryData(params); |
| 2520 | } |
| 2521 | } |
| 2522 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2523 | // forward device change to effects that have requested to be |
| 2524 | // aware of attached audio device. |
| 2525 | mDevice = (uint32_t)value; |
| 2526 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2527 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2532 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2533 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2534 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2535 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2536 | mStandby = true; |
| 2537 | mBytesWritten = 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2538 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2539 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2540 | } |
| 2541 | if (status == NO_ERROR && reconfig) { |
| 2542 | delete mAudioMixer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 2543 | // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) |
| 2544 | mAudioMixer = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2545 | readOutputParameters(); |
| 2546 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 2547 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 2548 | int name = getTrackName_l(); |
| 2549 | if (name < 0) break; |
| 2550 | mTracks[i]->mName = name; |
| 2551 | // limit track sample rate to 2 x new output sample rate |
| 2552 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 2553 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 2554 | } |
| 2555 | } |
| 2556 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | mNewParameters.removeAt(0); |
| 2561 | |
| 2562 | mParamStatus = status; |
| 2563 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2564 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2565 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2566 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2567 | } |
| 2568 | return reconfig; |
| 2569 | } |
| 2570 | |
| 2571 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2572 | { |
| 2573 | const size_t SIZE = 256; |
| 2574 | char buffer[SIZE]; |
| 2575 | String8 result; |
| 2576 | |
| 2577 | PlaybackThread::dumpInternals(fd, args); |
| 2578 | |
| 2579 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2580 | result.append(buffer); |
| 2581 | write(fd, result.string(), result.size()); |
| 2582 | return NO_ERROR; |
| 2583 | } |
| 2584 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2585 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2586 | { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2587 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2590 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() |
| 2591 | { |
| 2592 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2593 | } |
| 2594 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2595 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2596 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 2597 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 2598 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 2599 | // mLeftVolFloat, mRightVolFloat |
| 2600 | // mLeftVolShort, mRightVolShort |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2601 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2605 | { |
| 2606 | } |
| 2607 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2608 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2609 | { |
| 2610 | // Do not apply volume on compressed audio |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2611 | if (!audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2612 | return; |
| 2613 | } |
| 2614 | |
| 2615 | // convert to signed 16 bit before volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2616 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2617 | size_t count = mFrameCount * mChannelCount; |
| 2618 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2619 | int16_t *dst = mMixBuffer + count-1; |
| 2620 | while(count--) { |
| 2621 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | size_t frameCount = mFrameCount; |
| 2626 | int16_t *out = mMixBuffer; |
| 2627 | if (ramp) { |
| 2628 | if (mChannelCount == 1) { |
| 2629 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2630 | int32_t vlInc = d / (int32_t)frameCount; |
| 2631 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2632 | do { |
| 2633 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2634 | out++; |
| 2635 | vl += vlInc; |
| 2636 | } while (--frameCount); |
| 2637 | |
| 2638 | } else { |
| 2639 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2640 | int32_t vlInc = d / (int32_t)frameCount; |
| 2641 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2642 | int32_t vrInc = d / (int32_t)frameCount; |
| 2643 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2644 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2645 | do { |
| 2646 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2647 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2648 | out += 2; |
| 2649 | vl += vlInc; |
| 2650 | vr += vrInc; |
| 2651 | } while (--frameCount); |
| 2652 | } |
| 2653 | } else { |
| 2654 | if (mChannelCount == 1) { |
| 2655 | do { |
| 2656 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2657 | out++; |
| 2658 | } while (--frameCount); |
| 2659 | } else { |
| 2660 | do { |
| 2661 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2662 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2663 | out += 2; |
| 2664 | } while (--frameCount); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | // convert back to unsigned 8 bit after volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2669 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2670 | size_t count = mFrameCount * mChannelCount; |
| 2671 | int16_t *src = mMixBuffer; |
| 2672 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2673 | while(count--) { |
| 2674 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | mLeftVolShort = leftVol; |
| 2679 | mRightVolShort = rightVol; |
| 2680 | } |
| 2681 | |
| 2682 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2683 | { |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 2684 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2685 | sp<Track> trackToRemove; |
| 2686 | sp<Track> activeTrack; |
| 2687 | nsecs_t standbyTime = systemTime(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2688 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2689 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2690 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2691 | uint32_t sleepTime = idleSleepTime; |
| 2692 | // use shorter standby delay as on normal output to release |
| 2693 | // hardware resources as soon as possible |
| 2694 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
| 2695 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2696 | acquireWakeLock(); |
| 2697 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2698 | while (!exitPending()) |
| 2699 | { |
| 2700 | bool rampVolume; |
| 2701 | uint16_t leftVol; |
| 2702 | uint16_t rightVol; |
| 2703 | Vector< sp<EffectChain> > effectChains; |
| 2704 | |
| 2705 | processConfigEvents(); |
| 2706 | |
| 2707 | mixerStatus = MIXER_IDLE; |
| 2708 | |
| 2709 | { // scope for the mLock |
| 2710 | |
| 2711 | Mutex::Autolock _l(mLock); |
| 2712 | |
| 2713 | if (checkForNewParameters_l()) { |
| 2714 | mixBufferSize = mFrameCount*mFrameSize; |
| 2715 | activeSleepTime = activeSleepTimeUs(); |
| 2716 | idleSleepTime = idleSleepTimeUs(); |
| 2717 | standbyDelay = microseconds(activeSleepTime*2); |
| 2718 | } |
| 2719 | |
| 2720 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2721 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2722 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2723 | // wait until we have something to do... |
| 2724 | if (!mStandby) { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2725 | ALOGV("Audio hardware entering standby, mixer %p", this); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2726 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2727 | mStandby = true; |
| 2728 | mBytesWritten = 0; |
| 2729 | } |
| 2730 | |
| 2731 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2732 | // we're about to wait, flush the binder command buffer |
| 2733 | IPCThreadState::self()->flushCommands(); |
| 2734 | |
| 2735 | if (exitPending()) break; |
| 2736 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2737 | releaseWakeLock_l(); |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2738 | ALOGV("DirectOutputThread %p TID %d going to sleep", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2739 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 2740 | ALOGV("DirectOutputThread %p TID %d waking up in active mode", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2741 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2742 | |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 2743 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2744 | char value[PROPERTY_VALUE_MAX]; |
| 2745 | property_get("ro.audio.silent", value, "0"); |
| 2746 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2747 | ALOGD("Silence is golden"); |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 2748 | setMasterMute_l(true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | standbyTime = systemTime() + standbyDelay; |
| 2753 | sleepTime = idleSleepTime; |
| 2754 | continue; |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | effectChains = mEffectChains; |
| 2759 | |
| 2760 | // find out which tracks need to be processed |
| 2761 | if (mActiveTracks.size() != 0) { |
| 2762 | sp<Track> t = mActiveTracks[0].promote(); |
| 2763 | if (t == 0) continue; |
| 2764 | |
| 2765 | Track* const track = t.get(); |
| 2766 | audio_track_cblk_t* cblk = track->cblk(); |
| 2767 | |
| 2768 | // The first time a track is added we wait |
| 2769 | // for all its buffers to be filled before processing it |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2770 | if (cblk->framesReady() && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2771 | !track->isPaused() && !track->isTerminated()) |
| 2772 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2773 | //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2774 | |
| 2775 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2776 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2777 | mLeftVolFloat = mRightVolFloat = 0; |
| 2778 | mLeftVolShort = mRightVolShort = 0; |
| 2779 | if (track->mState == TrackBase::RESUMING) { |
| 2780 | track->mState = TrackBase::ACTIVE; |
| 2781 | rampVolume = true; |
| 2782 | } |
| 2783 | } else if (cblk->server != 0) { |
| 2784 | // If the track is stopped before the first frame was mixed, |
| 2785 | // do not apply ramp |
| 2786 | rampVolume = true; |
| 2787 | } |
| 2788 | // compute volume for this track |
| 2789 | float left, right; |
| 2790 | if (track->isMuted() || mMasterMute || track->isPausing() || |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2791 | mStreamTypes[track->streamType()].mute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2792 | left = right = 0; |
| 2793 | if (track->isPausing()) { |
| 2794 | track->setPaused(); |
| 2795 | } |
| 2796 | } else { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 2797 | float typeVolume = mStreamTypes[track->streamType()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2798 | float v = mMasterVolume * typeVolume; |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 2799 | uint32_t vlr = cblk->getVolumeLR(); |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2800 | float v_clamped = v * (vlr & 0xFFFF); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2801 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2802 | left = v_clamped/MAX_GAIN; |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 2803 | v_clamped = v * (vlr >> 16); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2804 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2805 | right = v_clamped/MAX_GAIN; |
| 2806 | } |
| 2807 | |
| 2808 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2809 | mLeftVolFloat = left; |
| 2810 | mRightVolFloat = right; |
| 2811 | |
| 2812 | // If audio HAL implements volume control, |
| 2813 | // force software volume to nominal value |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2814 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2815 | left = 1.0f; |
| 2816 | right = 1.0f; |
| 2817 | } |
| 2818 | |
| 2819 | // Convert volumes from float to 8.24 |
| 2820 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2821 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2822 | |
| 2823 | // Delegate volume control to effect in track effect chain if needed |
| 2824 | // only one effect chain can be present on DirectOutputThread, so if |
| 2825 | // there is one, the track is connected to it |
| 2826 | if (!effectChains.isEmpty()) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2827 | // Do not ramp volume if volume is controlled by effect |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2828 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2829 | rampVolume = false; |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | // Convert volumes from 8.24 to 4.12 format |
| 2834 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2835 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2836 | leftVol = (uint16_t)v_clamped; |
| 2837 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2838 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2839 | rightVol = (uint16_t)v_clamped; |
| 2840 | } else { |
| 2841 | leftVol = mLeftVolShort; |
| 2842 | rightVol = mRightVolShort; |
| 2843 | rampVolume = false; |
| 2844 | } |
| 2845 | |
| 2846 | // reset retry count |
| 2847 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 2848 | activeTrack = t; |
| 2849 | mixerStatus = MIXER_TRACKS_READY; |
| 2850 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2851 | //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2852 | if (track->isStopped()) { |
| 2853 | track->reset(); |
| 2854 | } |
| 2855 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2856 | // We have consumed all the buffers of this track. |
| 2857 | // Remove it from the list of active tracks. |
| 2858 | trackToRemove = track; |
| 2859 | } else { |
| 2860 | // No buffers for this track. Give it a few chances to |
| 2861 | // fill a buffer, then remove it from active list. |
| 2862 | if (--(track->mRetryCount) <= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2863 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2864 | trackToRemove = track; |
| 2865 | } else { |
| 2866 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | // remove all the tracks that need to be... |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2873 | if (CC_UNLIKELY(trackToRemove != 0)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2874 | mActiveTracks.remove(trackToRemove); |
| 2875 | if (!effectChains.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2876 | ALOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2877 | trackToRemove->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2878 | effectChains[0]->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2879 | } |
| 2880 | if (trackToRemove->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2881 | removeTrack_l(trackToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2885 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2888 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2889 | AudioBufferProvider::Buffer buffer; |
| 2890 | size_t frameCount = mFrameCount; |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2891 | int8_t *curBuf = (int8_t *)mMixBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2892 | // output audio to hardware |
| 2893 | while (frameCount) { |
| 2894 | buffer.frameCount = frameCount; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 2895 | activeTrack->getNextBuffer(&buffer, |
| 2896 | AudioBufferProvider::kInvalidPTS); |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2897 | if (CC_UNLIKELY(buffer.raw == NULL)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2898 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2899 | break; |
| 2900 | } |
| 2901 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2902 | frameCount -= buffer.frameCount; |
| 2903 | curBuf += buffer.frameCount * mFrameSize; |
| 2904 | activeTrack->releaseBuffer(&buffer); |
| 2905 | } |
| 2906 | sleepTime = 0; |
| 2907 | standbyTime = systemTime() + standbyDelay; |
| 2908 | } else { |
| 2909 | if (sleepTime == 0) { |
| 2910 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2911 | sleepTime = activeSleepTime; |
| 2912 | } else { |
| 2913 | sleepTime = idleSleepTime; |
| 2914 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2915 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2916 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
| 2917 | sleepTime = 0; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2922 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2923 | } |
| 2924 | // sleepTime == 0 means we must write to audio hardware |
| 2925 | if (sleepTime == 0) { |
| 2926 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2927 | applyVolume(leftVol, rightVol, rampVolume); |
| 2928 | } |
| 2929 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2930 | effectChains[i]->process_l(); |
| 2931 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2932 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2933 | |
| 2934 | mLastWriteTime = systemTime(); |
| 2935 | mInWrite = true; |
| 2936 | mBytesWritten += mixBufferSize; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2937 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2938 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2939 | mNumWrites++; |
| 2940 | mInWrite = false; |
| 2941 | mStandby = false; |
| 2942 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2943 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2944 | usleep(sleepTime); |
| 2945 | } |
| 2946 | |
| 2947 | // finally let go of removed track, without the lock held |
| 2948 | // since we can't guarantee the destructors won't acquire that |
| 2949 | // same lock. |
| 2950 | trackToRemove.clear(); |
| 2951 | activeTrack.clear(); |
| 2952 | |
| 2953 | // Effect chains will be actually deleted here if they were removed from |
| 2954 | // mEffectChains list during mixing or effects processing |
| 2955 | effectChains.clear(); |
| 2956 | } |
| 2957 | |
| 2958 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2959 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2962 | releaseWakeLock(); |
| 2963 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2964 | ALOGV("DirectOutputThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2965 | return false; |
| 2966 | } |
| 2967 | |
| 2968 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2969 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2970 | { |
| 2971 | return 0; |
| 2972 | } |
| 2973 | |
| 2974 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2975 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2976 | { |
| 2977 | } |
| 2978 | |
| 2979 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2980 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2981 | { |
| 2982 | bool reconfig = false; |
| 2983 | |
| 2984 | while (!mNewParameters.isEmpty()) { |
| 2985 | status_t status = NO_ERROR; |
| 2986 | String8 keyValuePair = mNewParameters[0]; |
| 2987 | AudioParameter param = AudioParameter(keyValuePair); |
| 2988 | int value; |
| 2989 | |
| 2990 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2991 | // do not accept frame count changes if tracks are open as the track buffer |
| 2992 | // size depends on frame count and correct behavior would not be garantied |
| 2993 | // if frame count is changed after track creation |
| 2994 | if (!mTracks.isEmpty()) { |
| 2995 | status = INVALID_OPERATION; |
| 2996 | } else { |
| 2997 | reconfig = true; |
| 2998 | } |
| 2999 | } |
| 3000 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3001 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3002 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3003 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3004 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3005 | mStandby = true; |
| 3006 | mBytesWritten = 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 3007 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3008 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3009 | } |
| 3010 | if (status == NO_ERROR && reconfig) { |
| 3011 | readOutputParameters(); |
| 3012 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | mNewParameters.removeAt(0); |
| 3017 | |
| 3018 | mParamStatus = status; |
| 3019 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 3020 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 3021 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 3022 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3023 | } |
| 3024 | return reconfig; |
| 3025 | } |
| 3026 | |
| 3027 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
| 3028 | { |
| 3029 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3030 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 3031 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3032 | } else { |
| 3033 | time = 10000; |
| 3034 | } |
| 3035 | return time; |
| 3036 | } |
| 3037 | |
| 3038 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 3039 | { |
| 3040 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3041 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 3042 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3043 | } else { |
| 3044 | time = 10000; |
| 3045 | } |
| 3046 | return time; |
| 3047 | } |
| 3048 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3049 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() |
| 3050 | { |
| 3051 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3052 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3053 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 3054 | } else { |
| 3055 | time = 10000; |
| 3056 | } |
| 3057 | return time; |
| 3058 | } |
| 3059 | |
| 3060 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3061 | // ---------------------------------------------------------------------------- |
| 3062 | |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3063 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3064 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 3065 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING), |
| 3066 | mWaitTimeMs(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3067 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3068 | addOutputTrack(mainThread); |
| 3069 | } |
| 3070 | |
| 3071 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 3072 | { |
| 3073 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3074 | mOutputTracks[i]->destroy(); |
| 3075 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 3079 | { |
| 3080 | Vector< sp<Track> > tracksToRemove; |
Glenn Kasten | 29c23c3 | 2012-01-26 13:37:52 -0800 | [diff] [blame] | 3081 | mixer_state mixerStatus = MIXER_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3082 | nsecs_t standbyTime = systemTime(); |
| 3083 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 3084 | SortedVector< sp<OutputTrack> > outputTracks; |
| 3085 | uint32_t writeFrames = 0; |
| 3086 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 3087 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 3088 | uint32_t sleepTime = idleSleepTime; |
| 3089 | Vector< sp<EffectChain> > effectChains; |
| 3090 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3091 | acquireWakeLock(); |
| 3092 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3093 | while (!exitPending()) |
| 3094 | { |
| 3095 | processConfigEvents(); |
| 3096 | |
| 3097 | mixerStatus = MIXER_IDLE; |
| 3098 | { // scope for the mLock |
| 3099 | |
| 3100 | Mutex::Autolock _l(mLock); |
| 3101 | |
| 3102 | if (checkForNewParameters_l()) { |
| 3103 | mixBufferSize = mFrameCount*mFrameSize; |
| 3104 | updateWaitTime(); |
| 3105 | activeSleepTime = activeSleepTimeUs(); |
| 3106 | idleSleepTime = idleSleepTimeUs(); |
| 3107 | } |
| 3108 | |
| 3109 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 3110 | |
| 3111 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3112 | outputTracks.add(mOutputTracks[i]); |
| 3113 | } |
| 3114 | |
| 3115 | // put audio hardware into standby after short delay |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3116 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 3117 | mSuspended)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3118 | if (!mStandby) { |
| 3119 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3120 | outputTracks[i]->stop(); |
| 3121 | } |
| 3122 | mStandby = true; |
| 3123 | mBytesWritten = 0; |
| 3124 | } |
| 3125 | |
| 3126 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 3127 | // we're about to wait, flush the binder command buffer |
| 3128 | IPCThreadState::self()->flushCommands(); |
| 3129 | outputTracks.clear(); |
| 3130 | |
| 3131 | if (exitPending()) break; |
| 3132 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3133 | releaseWakeLock_l(); |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 3134 | ALOGV("DuplicatingThread %p TID %d going to sleep", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3135 | mWaitWorkCV.wait(mLock); |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 3136 | ALOGV("DuplicatingThread %p TID %d waking up", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3137 | acquireWakeLock_l(); |
| 3138 | |
Eric Laurent | 2774144 | 2012-01-17 19:20:12 -0800 | [diff] [blame] | 3139 | mPrevMixerStatus = MIXER_IDLE; |
Glenn Kasten | f1d4592 | 2012-01-13 15:54:24 -0800 | [diff] [blame] | 3140 | if (!mMasterMute) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3141 | char value[PROPERTY_VALUE_MAX]; |
| 3142 | property_get("ro.audio.silent", value, "0"); |
| 3143 | if (atoi(value)) { |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3144 | ALOGD("Silence is golden"); |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 3145 | setMasterMute_l(true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3149 | standbyTime = systemTime() + mStandbyTimeInNsecs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3150 | sleepTime = idleSleepTime; |
| 3151 | continue; |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 3156 | |
| 3157 | // prevent any changes in effect chain list and in each effect chain |
| 3158 | // during mixing and effect process as the audio buffers could be deleted |
| 3159 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3160 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3163 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3164 | // mix buffers... |
| 3165 | if (outputsReady(outputTracks)) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3166 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3167 | } else { |
| 3168 | memset(mMixBuffer, 0, mixBufferSize); |
| 3169 | } |
| 3170 | sleepTime = 0; |
| 3171 | writeFrames = mFrameCount; |
| 3172 | } else { |
| 3173 | if (sleepTime == 0) { |
| 3174 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 3175 | sleepTime = activeSleepTime; |
| 3176 | } else { |
| 3177 | sleepTime = idleSleepTime; |
| 3178 | } |
| 3179 | } else if (mBytesWritten != 0) { |
| 3180 | // flush remaining overflow buffers in output tracks |
| 3181 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3182 | if (outputTracks[i]->isActive()) { |
| 3183 | sleepTime = 0; |
| 3184 | writeFrames = 0; |
| 3185 | memset(mMixBuffer, 0, mixBufferSize); |
| 3186 | break; |
| 3187 | } |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3193 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3194 | } |
| 3195 | // sleepTime == 0 means we must write to audio hardware |
| 3196 | if (sleepTime == 0) { |
| 3197 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3198 | effectChains[i]->process_l(); |
| 3199 | } |
| 3200 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3201 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3202 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3203 | standbyTime = systemTime() + mStandbyTimeInNsecs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3204 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3205 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3206 | } |
| 3207 | mStandby = false; |
| 3208 | mBytesWritten += mixBufferSize; |
| 3209 | } else { |
| 3210 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3211 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3212 | usleep(sleepTime); |
| 3213 | } |
| 3214 | |
| 3215 | // finally let go of all our tracks, without the lock held |
| 3216 | // since we can't guarantee the destructors won't acquire that |
| 3217 | // same lock. |
| 3218 | tracksToRemove.clear(); |
| 3219 | outputTracks.clear(); |
| 3220 | |
| 3221 | // Effect chains will be actually deleted here if they were removed from |
| 3222 | // mEffectChains list during mixing or effects processing |
| 3223 | effectChains.clear(); |
| 3224 | } |
| 3225 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3226 | releaseWakeLock(); |
| 3227 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3228 | return false; |
| 3229 | } |
| 3230 | |
| 3231 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3232 | { |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 3233 | // FIXME explain this formula |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3234 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3235 | OutputTrack *outputTrack = new OutputTrack(thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3236 | this, |
| 3237 | mSampleRate, |
| 3238 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3239 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3240 | frameCount); |
| 3241 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3242 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3243 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3244 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3245 | updateWaitTime(); |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3250 | { |
| 3251 | Mutex::Autolock _l(mLock); |
| 3252 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3253 | if (mOutputTracks[i]->thread() == thread) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3254 | mOutputTracks[i]->destroy(); |
| 3255 | mOutputTracks.removeAt(i); |
| 3256 | updateWaitTime(); |
| 3257 | return; |
| 3258 | } |
| 3259 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3260 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 3264 | { |
| 3265 | mWaitTimeMs = UINT_MAX; |
| 3266 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3267 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3268 | if (strong != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3269 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3270 | if (waitTimeMs < mWaitTimeMs) { |
| 3271 | mWaitTimeMs = waitTimeMs; |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | |
| 3278 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 3279 | { |
| 3280 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3281 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 3282 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3283 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3284 | return false; |
| 3285 | } |
| 3286 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3287 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3288 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3289 | return false; |
| 3290 | } |
| 3291 | } |
| 3292 | return true; |
| 3293 | } |
| 3294 | |
| 3295 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 3296 | { |
| 3297 | return (mWaitTimeMs * 1000) / 2; |
| 3298 | } |
| 3299 | |
| 3300 | // ---------------------------------------------------------------------------- |
| 3301 | |
| 3302 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 3303 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3304 | ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3305 | const sp<Client>& client, |
| 3306 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3307 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3308 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3309 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3310 | const sp<IMemory>& sharedBuffer, |
| 3311 | int sessionId) |
| 3312 | : RefBase(), |
| 3313 | mThread(thread), |
| 3314 | mClient(client), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3315 | mCblk(NULL), |
| 3316 | // mBuffer |
| 3317 | // mBufferEnd |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3318 | mFrameCount(0), |
| 3319 | mState(IDLE), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3320 | mFormat(format), |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3321 | mStepServerFailed(false), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3322 | mSessionId(sessionId) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3323 | // mChannelCount |
| 3324 | // mChannelMask |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3325 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3326 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3327 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3328 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3329 | size_t size = sizeof(audio_track_cblk_t); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3330 | uint8_t channelCount = popcount(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3331 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3332 | if (sharedBuffer == 0) { |
| 3333 | size += bufferSize; |
| 3334 | } |
| 3335 | |
| 3336 | if (client != NULL) { |
| 3337 | mCblkMemory = client->heap()->allocate(size); |
| 3338 | if (mCblkMemory != 0) { |
| 3339 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3340 | if (mCblk != NULL) { // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3341 | new(mCblk) audio_track_cblk_t(); |
| 3342 | // clear all buffers |
| 3343 | mCblk->frameCount = frameCount; |
| 3344 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3345 | mChannelCount = channelCount; |
| 3346 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3347 | if (sharedBuffer == 0) { |
| 3348 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3349 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3350 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3351 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3352 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3353 | } else { |
| 3354 | mBuffer = sharedBuffer->pointer(); |
| 3355 | } |
| 3356 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3357 | } |
| 3358 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3359 | ALOGE("not enough memory for AudioTrack size=%u", size); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3360 | client->heap()->dump("AudioTrack"); |
| 3361 | return; |
| 3362 | } |
| 3363 | } else { |
| 3364 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
Glenn Kasten | 4a6f028 | 2012-01-06 15:03:11 -0800 | [diff] [blame] | 3365 | // construct the shared structure in-place. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3366 | new(mCblk) audio_track_cblk_t(); |
| 3367 | // clear all buffers |
| 3368 | mCblk->frameCount = frameCount; |
| 3369 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3370 | mChannelCount = channelCount; |
| 3371 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3372 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3373 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3374 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3375 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3376 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3377 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 3382 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3383 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3384 | if (mClient == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3385 | delete mCblk; |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 3386 | } else { |
| 3387 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3388 | } |
| 3389 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 3390 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3391 | if (mClient != 0) { |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3392 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3393 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 3394 | // If the client's reference count drops to zero, the associated destructor |
| 3395 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 3396 | // relying on the automatic clear() at end of scope. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3397 | mClient.clear(); |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 3402 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3403 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3404 | mFrameCount = buffer->frameCount; |
| 3405 | step(); |
| 3406 | buffer->frameCount = 0; |
| 3407 | } |
| 3408 | |
| 3409 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 3410 | bool result; |
| 3411 | audio_track_cblk_t* cblk = this->cblk(); |
| 3412 | |
| 3413 | result = cblk->stepServer(mFrameCount); |
| 3414 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3415 | ALOGV("stepServer failed acquiring cblk mutex"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3416 | mStepServerFailed = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3417 | } |
| 3418 | return result; |
| 3419 | } |
| 3420 | |
| 3421 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 3422 | audio_track_cblk_t* cblk = this->cblk(); |
| 3423 | |
| 3424 | cblk->user = 0; |
| 3425 | cblk->server = 0; |
| 3426 | cblk->userBase = 0; |
| 3427 | cblk->serverBase = 0; |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3428 | mStepServerFailed = false; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3429 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3432 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 3433 | return (int)mCblk->sampleRate; |
| 3434 | } |
| 3435 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3436 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 3437 | audio_track_cblk_t* cblk = this->cblk(); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3438 | size_t frameSize = cblk->frameSize; |
| 3439 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize; |
| 3440 | int8_t *bufferEnd = bufferStart + frames * frameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3441 | |
| 3442 | // Check validity of returned pointer in case the track control block would have been corrupted. |
| 3443 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 3444 | ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3445 | ALOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3446 | server %d, serverBase %d, user %d, userBase %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3447 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3448 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 3449 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | return bufferStart; |
| 3453 | } |
| 3454 | |
| 3455 | // ---------------------------------------------------------------------------- |
| 3456 | |
| 3457 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 3458 | AudioFlinger::PlaybackThread::Track::Track( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3459 | PlaybackThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3460 | const sp<Client>& client, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 3461 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3462 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 3463 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3464 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3465 | int frameCount, |
| 3466 | const sp<IMemory>& sharedBuffer, |
| 3467 | int sessionId) |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3468 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId), |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3469 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), |
| 3470 | mAuxEffectId(0), mHasVolumeController(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3471 | { |
| 3472 | if (mCblk != NULL) { |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3473 | if (thread != NULL) { |
| 3474 | mName = thread->getTrackName_l(); |
| 3475 | mMainBuffer = thread->mixBuffer(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3476 | } |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 3477 | ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3478 | if (mName < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3479 | ALOGE("no more track names available"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3481 | mStreamType = streamType; |
| 3482 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 3483 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
Eric Laurent | edc15ad | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 3484 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | AudioFlinger::PlaybackThread::Track::~Track() |
| 3489 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3490 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3491 | sp<ThreadBase> thread = mThread.promote(); |
| 3492 | if (thread != 0) { |
| 3493 | Mutex::Autolock _l(thread->mLock); |
| 3494 | mState = TERMINATED; |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 3499 | { |
| 3500 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 3501 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 3502 | // destructor is called. As the destructor needs to lock mLock, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3503 | // we must acquire a strong reference on this Track before locking mLock |
| 3504 | // here so that the destructor is called only when exiting this function. |
| 3505 | // On the other hand, as long as Track::destroy() is only called by |
| 3506 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 3507 | // this Track with its member mTrack. |
| 3508 | sp<Track> keep(this); |
| 3509 | { // scope for mLock |
| 3510 | sp<ThreadBase> thread = mThread.promote(); |
| 3511 | if (thread != 0) { |
| 3512 | if (!isOutputTrack()) { |
| 3513 | if (mState == ACTIVE || mState == RESUMING) { |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3514 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3515 | |
| 3516 | // to track the speaker usage |
| 3517 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3518 | } |
| 3519 | AudioSystem::releaseOutput(thread->id()); |
| 3520 | } |
| 3521 | Mutex::Autolock _l(thread->mLock); |
| 3522 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3523 | playbackThread->destroyTrack_l(this); |
| 3524 | } |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 3529 | { |
Glenn Kasten | 83d8653 | 2012-01-17 14:39:34 -0800 | [diff] [blame] | 3530 | uint32_t vlr = mCblk->getVolumeLR(); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3531 | snprintf(buffer, size, " %05d %05d %03u %03u 0x%08x %05u %04u %1d %1d %1d %05u %05u %05u 0x%08x 0x%08x 0x%08x 0x%08x\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3532 | mName - AudioMixer::TRACK0, |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 3533 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3534 | mStreamType, |
| 3535 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3536 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3537 | mSessionId, |
| 3538 | mFrameCount, |
| 3539 | mState, |
| 3540 | mMute, |
| 3541 | mFillingUpStatus, |
| 3542 | mCblk->sampleRate, |
Glenn Kasten | b1cf75c | 2012-01-17 12:20:54 -0800 | [diff] [blame] | 3543 | vlr & 0xFFFF, |
| 3544 | vlr >> 16, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3545 | mCblk->server, |
| 3546 | mCblk->user, |
| 3547 | (int)mMainBuffer, |
| 3548 | (int)mAuxBuffer); |
| 3549 | } |
| 3550 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3551 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer( |
| 3552 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3553 | { |
| 3554 | audio_track_cblk_t* cblk = this->cblk(); |
| 3555 | uint32_t framesReady; |
| 3556 | uint32_t framesReq = buffer->frameCount; |
| 3557 | |
| 3558 | // Check if last stepServer failed, try to step now |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3559 | if (mStepServerFailed) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3560 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3561 | ALOGV("stepServer recovered"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 3562 | mStepServerFailed = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | framesReady = cblk->framesReady(); |
| 3566 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3567 | if (CC_LIKELY(framesReady)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3568 | uint32_t s = cblk->server; |
| 3569 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3570 | |
| 3571 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 3572 | if (framesReq > framesReady) { |
| 3573 | framesReq = framesReady; |
| 3574 | } |
| 3575 | if (s + framesReq > bufferEnd) { |
| 3576 | framesReq = bufferEnd - s; |
| 3577 | } |
| 3578 | |
| 3579 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3580 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3581 | |
| 3582 | buffer->frameCount = framesReq; |
| 3583 | return NO_ERROR; |
| 3584 | } |
| 3585 | |
| 3586 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3587 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3588 | buffer->frameCount = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3589 | ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3590 | return NOT_ENOUGH_DATA; |
| 3591 | } |
| 3592 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3593 | uint32_t AudioFlinger::PlaybackThread::Track::framesReady() const{ |
| 3594 | return mCblk->framesReady(); |
| 3595 | } |
| 3596 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3597 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3598 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3599 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3600 | if (framesReady() >= mCblk->frameCount || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3601 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 3602 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3603 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3604 | return true; |
| 3605 | } |
| 3606 | return false; |
| 3607 | } |
| 3608 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 3609 | status_t AudioFlinger::PlaybackThread::Track::start(pid_t tid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3610 | { |
| 3611 | status_t status = NO_ERROR; |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 3612 | ALOGV("start(%d), calling pid %d session %d tid %d", |
| 3613 | mName, IPCThreadState::self()->getCallingPid(), mSessionId, tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3614 | sp<ThreadBase> thread = mThread.promote(); |
| 3615 | if (thread != 0) { |
| 3616 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 3617 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3618 | // here the track could be either new, or restarted |
| 3619 | // in both cases "unstop" the track |
| 3620 | if (mState == PAUSED) { |
| 3621 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3622 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3623 | } else { |
| 3624 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3625 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3629 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3630 | status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3631 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3632 | |
| 3633 | // to track the speaker usage |
| 3634 | if (status == NO_ERROR) { |
| 3635 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 3636 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3637 | } |
| 3638 | if (status == NO_ERROR) { |
| 3639 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3640 | playbackThread->addTrack_l(this); |
| 3641 | } else { |
| 3642 | mState = state; |
| 3643 | } |
| 3644 | } else { |
| 3645 | status = BAD_VALUE; |
| 3646 | } |
| 3647 | return status; |
| 3648 | } |
| 3649 | |
| 3650 | void AudioFlinger::PlaybackThread::Track::stop() |
| 3651 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 3652 | ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3653 | sp<ThreadBase> thread = mThread.promote(); |
| 3654 | if (thread != 0) { |
| 3655 | Mutex::Autolock _l(thread->mLock); |
Glenn Kasten | b853e98 | 2012-01-26 13:39:18 -0800 | [diff] [blame] | 3656 | track_state state = mState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3657 | if (mState > STOPPED) { |
| 3658 | mState = STOPPED; |
| 3659 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3660 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3661 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3662 | reset(); |
| 3663 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3664 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3665 | } |
| 3666 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3667 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3668 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3669 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3670 | |
| 3671 | // to track the speaker usage |
| 3672 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | void AudioFlinger::PlaybackThread::Track::pause() |
| 3678 | { |
Glenn Kasten | 23d82a9 | 2012-02-03 11:10:00 -0800 | [diff] [blame] | 3679 | ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3680 | sp<ThreadBase> thread = mThread.promote(); |
| 3681 | if (thread != 0) { |
| 3682 | Mutex::Autolock _l(thread->mLock); |
| 3683 | if (mState == ACTIVE || mState == RESUMING) { |
| 3684 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3685 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3686 | if (!isOutputTrack()) { |
| 3687 | thread->mLock.unlock(); |
Glenn Kasten | 02bbd20 | 2012-02-08 12:35:35 -0800 | [diff] [blame] | 3688 | AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3689 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3690 | |
| 3691 | // to track the speaker usage |
| 3692 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | } |
| 3696 | } |
| 3697 | |
| 3698 | void AudioFlinger::PlaybackThread::Track::flush() |
| 3699 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3700 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3701 | sp<ThreadBase> thread = mThread.promote(); |
| 3702 | if (thread != 0) { |
| 3703 | Mutex::Autolock _l(thread->mLock); |
| 3704 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3705 | return; |
| 3706 | } |
| 3707 | // No point remaining in PAUSED state after a flush => go to |
| 3708 | // STOPPED state |
| 3709 | mState = STOPPED; |
| 3710 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3711 | // do not reset the track if it is still in the process of being stopped or paused. |
| 3712 | // this will be done by prepareTracks_l() when the track is stopped. |
| 3713 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3714 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3715 | reset(); |
| 3716 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | |
| 3720 | void AudioFlinger::PlaybackThread::Track::reset() |
| 3721 | { |
| 3722 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3723 | // the audioflinger thread detects the track is stopped. |
| 3724 | if (!mResetDone) { |
| 3725 | TrackBase::reset(); |
| 3726 | // Force underrun condition to avoid false underrun callback until first data is |
| 3727 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3728 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 3729 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3730 | mFillingUpStatus = FS_FILLING; |
| 3731 | mResetDone = true; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 3736 | { |
| 3737 | mMute = muted; |
| 3738 | } |
| 3739 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3740 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3741 | { |
| 3742 | status_t status = DEAD_OBJECT; |
| 3743 | sp<ThreadBase> thread = mThread.promote(); |
| 3744 | if (thread != 0) { |
| 3745 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3746 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3747 | } |
| 3748 | return status; |
| 3749 | } |
| 3750 | |
| 3751 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3752 | { |
| 3753 | mAuxEffectId = EffectId; |
| 3754 | mAuxBuffer = buffer; |
| 3755 | } |
| 3756 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3757 | // timed audio tracks |
| 3758 | |
| 3759 | sp<AudioFlinger::PlaybackThread::TimedTrack> |
| 3760 | AudioFlinger::PlaybackThread::TimedTrack::create( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3761 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3762 | const sp<Client>& client, |
| 3763 | audio_stream_type_t streamType, |
| 3764 | uint32_t sampleRate, |
| 3765 | audio_format_t format, |
| 3766 | uint32_t channelMask, |
| 3767 | int frameCount, |
| 3768 | const sp<IMemory>& sharedBuffer, |
| 3769 | int sessionId) { |
| 3770 | if (!client->reserveTimedTrack()) |
| 3771 | return NULL; |
| 3772 | |
| 3773 | sp<TimedTrack> track = new TimedTrack( |
| 3774 | thread, client, streamType, sampleRate, format, channelMask, frameCount, |
| 3775 | sharedBuffer, sessionId); |
| 3776 | |
| 3777 | if (track == NULL) { |
| 3778 | client->releaseTimedTrack(); |
| 3779 | return NULL; |
| 3780 | } |
| 3781 | |
| 3782 | return track; |
| 3783 | } |
| 3784 | |
| 3785 | AudioFlinger::PlaybackThread::TimedTrack::TimedTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 3786 | PlaybackThread *thread, |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3787 | const sp<Client>& client, |
| 3788 | audio_stream_type_t streamType, |
| 3789 | uint32_t sampleRate, |
| 3790 | audio_format_t format, |
| 3791 | uint32_t channelMask, |
| 3792 | int frameCount, |
| 3793 | const sp<IMemory>& sharedBuffer, |
| 3794 | int sessionId) |
| 3795 | : Track(thread, client, streamType, sampleRate, format, channelMask, |
| 3796 | frameCount, sharedBuffer, sessionId), |
| 3797 | mTimedSilenceBuffer(NULL), |
| 3798 | mTimedSilenceBufferSize(0), |
| 3799 | mTimedAudioOutputOnTime(false), |
| 3800 | mMediaTimeTransformValid(false) |
| 3801 | { |
| 3802 | LocalClock lc; |
| 3803 | mLocalTimeFreq = lc.getLocalFreq(); |
| 3804 | |
| 3805 | mLocalTimeToSampleTransform.a_zero = 0; |
| 3806 | mLocalTimeToSampleTransform.b_zero = 0; |
| 3807 | mLocalTimeToSampleTransform.a_to_b_numer = sampleRate; |
| 3808 | mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq; |
| 3809 | LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer, |
| 3810 | &mLocalTimeToSampleTransform.a_to_b_denom); |
| 3811 | } |
| 3812 | |
| 3813 | AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() { |
| 3814 | mClient->releaseTimedTrack(); |
| 3815 | delete [] mTimedSilenceBuffer; |
| 3816 | } |
| 3817 | |
| 3818 | status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer( |
| 3819 | size_t size, sp<IMemory>* buffer) { |
| 3820 | |
| 3821 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 3822 | |
| 3823 | trimTimedBufferQueue_l(); |
| 3824 | |
| 3825 | // lazily initialize the shared memory heap for timed buffers |
| 3826 | if (mTimedMemoryDealer == NULL) { |
| 3827 | const int kTimedBufferHeapSize = 512 << 10; |
| 3828 | |
| 3829 | mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize, |
| 3830 | "AudioFlingerTimed"); |
| 3831 | if (mTimedMemoryDealer == NULL) |
| 3832 | return NO_MEMORY; |
| 3833 | } |
| 3834 | |
| 3835 | sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size); |
| 3836 | if (newBuffer == NULL) { |
| 3837 | newBuffer = mTimedMemoryDealer->allocate(size); |
| 3838 | if (newBuffer == NULL) |
| 3839 | return NO_MEMORY; |
| 3840 | } |
| 3841 | |
| 3842 | *buffer = newBuffer; |
| 3843 | return NO_ERROR; |
| 3844 | } |
| 3845 | |
| 3846 | // caller must hold mTimedBufferQueueLock |
| 3847 | void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() { |
| 3848 | int64_t mediaTimeNow; |
| 3849 | { |
| 3850 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 3851 | if (!mMediaTimeTransformValid) |
| 3852 | return; |
| 3853 | |
| 3854 | int64_t targetTimeNow; |
| 3855 | status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) |
| 3856 | ? mCCHelper.getCommonTime(&targetTimeNow) |
| 3857 | : mCCHelper.getLocalTime(&targetTimeNow); |
| 3858 | |
| 3859 | if (OK != res) |
| 3860 | return; |
| 3861 | |
| 3862 | if (!mMediaTimeTransform.doReverseTransform(targetTimeNow, |
| 3863 | &mediaTimeNow)) { |
| 3864 | return; |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | size_t trimIndex; |
| 3869 | for (trimIndex = 0; trimIndex < mTimedBufferQueue.size(); trimIndex++) { |
| 3870 | if (mTimedBufferQueue[trimIndex].pts() > mediaTimeNow) |
| 3871 | break; |
| 3872 | } |
| 3873 | |
| 3874 | if (trimIndex) { |
| 3875 | mTimedBufferQueue.removeItemsAt(0, trimIndex); |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer( |
| 3880 | const sp<IMemory>& buffer, int64_t pts) { |
| 3881 | |
| 3882 | { |
| 3883 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 3884 | if (!mMediaTimeTransformValid) |
| 3885 | return INVALID_OPERATION; |
| 3886 | } |
| 3887 | |
| 3888 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 3889 | |
| 3890 | mTimedBufferQueue.add(TimedBuffer(buffer, pts)); |
| 3891 | |
| 3892 | return NO_ERROR; |
| 3893 | } |
| 3894 | |
| 3895 | status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform( |
| 3896 | const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) { |
| 3897 | |
| 3898 | ALOGV("%s az=%lld bz=%lld n=%d d=%u tgt=%d", __PRETTY_FUNCTION__, |
| 3899 | xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom, |
| 3900 | target); |
| 3901 | |
| 3902 | if (!(target == TimedAudioTrack::LOCAL_TIME || |
| 3903 | target == TimedAudioTrack::COMMON_TIME)) { |
| 3904 | return BAD_VALUE; |
| 3905 | } |
| 3906 | |
| 3907 | Mutex::Autolock lock(mMediaTimeTransformLock); |
| 3908 | mMediaTimeTransform = xform; |
| 3909 | mMediaTimeTransformTarget = target; |
| 3910 | mMediaTimeTransformValid = true; |
| 3911 | |
| 3912 | return NO_ERROR; |
| 3913 | } |
| 3914 | |
| 3915 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 3916 | |
| 3917 | // implementation of getNextBuffer for tracks whose buffers have timestamps |
| 3918 | status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer( |
| 3919 | AudioBufferProvider::Buffer* buffer, int64_t pts) |
| 3920 | { |
| 3921 | if (pts == AudioBufferProvider::kInvalidPTS) { |
| 3922 | buffer->raw = 0; |
| 3923 | buffer->frameCount = 0; |
| 3924 | return INVALID_OPERATION; |
| 3925 | } |
| 3926 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 3927 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 3928 | |
| 3929 | while (true) { |
| 3930 | |
| 3931 | // if we have no timed buffers, then fail |
| 3932 | if (mTimedBufferQueue.isEmpty()) { |
| 3933 | buffer->raw = 0; |
| 3934 | buffer->frameCount = 0; |
| 3935 | return NOT_ENOUGH_DATA; |
| 3936 | } |
| 3937 | |
| 3938 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
| 3939 | |
| 3940 | // calculate the PTS of the head of the timed buffer queue expressed in |
| 3941 | // local time |
| 3942 | int64_t headLocalPTS; |
| 3943 | { |
| 3944 | Mutex::Autolock mttLock(mMediaTimeTransformLock); |
| 3945 | |
| 3946 | assert(mMediaTimeTransformValid); |
| 3947 | |
| 3948 | if (mMediaTimeTransform.a_to_b_denom == 0) { |
| 3949 | // the transform represents a pause, so yield silence |
| 3950 | timedYieldSilence(buffer->frameCount, buffer); |
| 3951 | return NO_ERROR; |
| 3952 | } |
| 3953 | |
| 3954 | int64_t transformedPTS; |
| 3955 | if (!mMediaTimeTransform.doForwardTransform(head.pts(), |
| 3956 | &transformedPTS)) { |
| 3957 | // the transform failed. this shouldn't happen, but if it does |
| 3958 | // then just drop this buffer |
| 3959 | ALOGW("timedGetNextBuffer transform failed"); |
| 3960 | buffer->raw = 0; |
| 3961 | buffer->frameCount = 0; |
| 3962 | mTimedBufferQueue.removeAt(0); |
| 3963 | return NO_ERROR; |
| 3964 | } |
| 3965 | |
| 3966 | if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) { |
| 3967 | if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS, |
| 3968 | &headLocalPTS)) { |
| 3969 | buffer->raw = 0; |
| 3970 | buffer->frameCount = 0; |
| 3971 | return INVALID_OPERATION; |
| 3972 | } |
| 3973 | } else { |
| 3974 | headLocalPTS = transformedPTS; |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | // adjust the head buffer's PTS to reflect the portion of the head buffer |
| 3979 | // that has already been consumed |
| 3980 | int64_t effectivePTS = headLocalPTS + |
| 3981 | ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate()); |
| 3982 | |
| 3983 | // Calculate the delta in samples between the head of the input buffer |
| 3984 | // queue and the start of the next output buffer that will be written. |
| 3985 | // If the transformation fails because of over or underflow, it means |
| 3986 | // that the sample's position in the output stream is so far out of |
| 3987 | // whack that it should just be dropped. |
| 3988 | int64_t sampleDelta; |
| 3989 | if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) { |
| 3990 | ALOGV("*** head buffer is too far from PTS: dropped buffer"); |
| 3991 | mTimedBufferQueue.removeAt(0); |
| 3992 | continue; |
| 3993 | } |
| 3994 | if (!mLocalTimeToSampleTransform.doForwardTransform( |
| 3995 | (effectivePTS - pts) << 32, &sampleDelta)) { |
| 3996 | ALOGV("*** too late during sample rate transform: dropped buffer"); |
| 3997 | mTimedBufferQueue.removeAt(0); |
| 3998 | continue; |
| 3999 | } |
| 4000 | |
| 4001 | ALOGV("*** %s head.pts=%lld head.pos=%d pts=%lld sampleDelta=[%d.%08x]", |
| 4002 | __PRETTY_FUNCTION__, head.pts(), head.position(), pts, |
| 4003 | static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) + (sampleDelta >> 32)), |
| 4004 | static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF)); |
| 4005 | |
| 4006 | // if the delta between the ideal placement for the next input sample and |
| 4007 | // the current output position is within this threshold, then we will |
| 4008 | // concatenate the next input samples to the previous output |
| 4009 | const int64_t kSampleContinuityThreshold = |
| 4010 | (static_cast<int64_t>(sampleRate()) << 32) / 10; |
| 4011 | |
| 4012 | // if this is the first buffer of audio that we're emitting from this track |
| 4013 | // then it should be almost exactly on time. |
| 4014 | const int64_t kSampleStartupThreshold = 1LL << 32; |
| 4015 | |
| 4016 | if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) || |
| 4017 | (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) { |
| 4018 | // the next input is close enough to being on time, so concatenate it |
| 4019 | // with the last output |
| 4020 | timedYieldSamples(buffer); |
| 4021 | |
| 4022 | ALOGV("*** on time: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 4023 | return NO_ERROR; |
| 4024 | } else if (sampleDelta > 0) { |
| 4025 | // the gap between the current output position and the proper start of |
| 4026 | // the next input sample is too big, so fill it with silence |
| 4027 | uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32; |
| 4028 | |
| 4029 | timedYieldSilence(framesUntilNextInput, buffer); |
| 4030 | ALOGV("*** silence: frameCount=%u", buffer->frameCount); |
| 4031 | return NO_ERROR; |
| 4032 | } else { |
| 4033 | // the next input sample is late |
| 4034 | uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32)); |
| 4035 | size_t onTimeSamplePosition = |
| 4036 | head.position() + lateFrames * mCblk->frameSize; |
| 4037 | |
| 4038 | if (onTimeSamplePosition > head.buffer()->size()) { |
| 4039 | // all the remaining samples in the head are too late, so |
| 4040 | // drop it and move on |
| 4041 | ALOGV("*** too late: dropped buffer"); |
| 4042 | mTimedBufferQueue.removeAt(0); |
| 4043 | continue; |
| 4044 | } else { |
| 4045 | // skip over the late samples |
| 4046 | head.setPosition(onTimeSamplePosition); |
| 4047 | |
| 4048 | // yield the available samples |
| 4049 | timedYieldSamples(buffer); |
| 4050 | |
| 4051 | ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount); |
| 4052 | return NO_ERROR; |
| 4053 | } |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | |
| 4058 | // Yield samples from the timed buffer queue head up to the given output |
| 4059 | // buffer's capacity. |
| 4060 | // |
| 4061 | // Caller must hold mTimedBufferQueueLock |
| 4062 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples( |
| 4063 | AudioBufferProvider::Buffer* buffer) { |
| 4064 | |
| 4065 | const TimedBuffer& head = mTimedBufferQueue[0]; |
| 4066 | |
| 4067 | buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) + |
| 4068 | head.position()); |
| 4069 | |
| 4070 | uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) / |
| 4071 | mCblk->frameSize); |
| 4072 | size_t framesRequested = buffer->frameCount; |
| 4073 | buffer->frameCount = min(framesLeftInHead, framesRequested); |
| 4074 | |
| 4075 | mTimedAudioOutputOnTime = true; |
| 4076 | } |
| 4077 | |
| 4078 | // Yield samples of silence up to the given output buffer's capacity |
| 4079 | // |
| 4080 | // Caller must hold mTimedBufferQueueLock |
| 4081 | void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence( |
| 4082 | uint32_t numFrames, AudioBufferProvider::Buffer* buffer) { |
| 4083 | |
| 4084 | // lazily allocate a buffer filled with silence |
| 4085 | if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) { |
| 4086 | delete [] mTimedSilenceBuffer; |
| 4087 | mTimedSilenceBufferSize = numFrames * mCblk->frameSize; |
| 4088 | mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize]; |
| 4089 | memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize); |
| 4090 | } |
| 4091 | |
| 4092 | buffer->raw = mTimedSilenceBuffer; |
| 4093 | size_t framesRequested = buffer->frameCount; |
| 4094 | buffer->frameCount = min(numFrames, framesRequested); |
| 4095 | |
| 4096 | mTimedAudioOutputOnTime = false; |
| 4097 | } |
| 4098 | |
| 4099 | void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer( |
| 4100 | AudioBufferProvider::Buffer* buffer) { |
| 4101 | |
| 4102 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4103 | |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4104 | // If the buffer which was just released is part of the buffer at the head |
| 4105 | // of the queue, be sure to update the amt of the buffer which has been |
| 4106 | // consumed. If the buffer being returned is not part of the head of the |
| 4107 | // queue, its either because the buffer is part of the silence buffer, or |
| 4108 | // because the head of the timed queue was trimmed after the mixer called |
| 4109 | // getNextBuffer but before the mixer called releaseBuffer. |
| 4110 | if ((buffer->raw != mTimedSilenceBuffer) && mTimedBufferQueue.size()) { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4111 | TimedBuffer& head = mTimedBufferQueue.editItemAt(0); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4112 | |
| 4113 | void* start = head.buffer()->pointer(); |
Glenn Kasten | f063b49 | 2012-02-17 16:24:10 -0800 | [diff] [blame] | 4114 | void* end = (char *) head.buffer()->pointer() + head.buffer()->size(); |
John Grossman | fe5b3ba | 2012-02-12 17:51:21 -0800 | [diff] [blame] | 4115 | |
| 4116 | if ((buffer->raw >= start) && (buffer->raw <= end)) { |
| 4117 | head.setPosition(head.position() + |
| 4118 | (buffer->frameCount * mCblk->frameSize)); |
| 4119 | if (static_cast<size_t>(head.position()) >= head.buffer()->size()) { |
| 4120 | mTimedBufferQueue.removeAt(0); |
| 4121 | } |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | buffer->raw = 0; |
| 4126 | buffer->frameCount = 0; |
| 4127 | } |
| 4128 | |
| 4129 | uint32_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const { |
| 4130 | Mutex::Autolock _l(mTimedBufferQueueLock); |
| 4131 | |
| 4132 | uint32_t frames = 0; |
| 4133 | for (size_t i = 0; i < mTimedBufferQueue.size(); i++) { |
| 4134 | const TimedBuffer& tb = mTimedBufferQueue[i]; |
| 4135 | frames += (tb.buffer()->size() - tb.position()) / mCblk->frameSize; |
| 4136 | } |
| 4137 | |
| 4138 | return frames; |
| 4139 | } |
| 4140 | |
| 4141 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer() |
| 4142 | : mPTS(0), mPosition(0) {} |
| 4143 | |
| 4144 | AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer( |
| 4145 | const sp<IMemory>& buffer, int64_t pts) |
| 4146 | : mBuffer(buffer), mPTS(pts), mPosition(0) {} |
| 4147 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4148 | // ---------------------------------------------------------------------------- |
| 4149 | |
| 4150 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 4151 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4152 | RecordThread *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4153 | const sp<Client>& client, |
| 4154 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4155 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4156 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4157 | int frameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4158 | int sessionId) |
| 4159 | : TrackBase(thread, client, sampleRate, format, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 4160 | channelMask, frameCount, 0 /*sharedBuffer*/, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4161 | mOverflow(false) |
| 4162 | { |
| 4163 | if (mCblk != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4164 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4165 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4166 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4167 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4168 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4169 | } else { |
| 4170 | mCblk->frameSize = sizeof(int8_t); |
| 4171 | } |
| 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 4176 | { |
| 4177 | sp<ThreadBase> thread = mThread.promote(); |
| 4178 | if (thread != 0) { |
| 4179 | AudioSystem::releaseInput(thread->id()); |
| 4180 | } |
| 4181 | } |
| 4182 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4183 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4184 | { |
| 4185 | audio_track_cblk_t* cblk = this->cblk(); |
| 4186 | uint32_t framesAvail; |
| 4187 | uint32_t framesReq = buffer->frameCount; |
| 4188 | |
| 4189 | // Check if last stepServer failed, try to step now |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 4190 | if (mStepServerFailed) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4191 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4192 | ALOGV("stepServer recovered"); |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 4193 | mStepServerFailed = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | framesAvail = cblk->framesAvailable_l(); |
| 4197 | |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4198 | if (CC_LIKELY(framesAvail)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4199 | uint32_t s = cblk->server; |
| 4200 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 4201 | |
| 4202 | if (framesReq > framesAvail) { |
| 4203 | framesReq = framesAvail; |
| 4204 | } |
| 4205 | if (s + framesReq > bufferEnd) { |
| 4206 | framesReq = bufferEnd - s; |
| 4207 | } |
| 4208 | |
| 4209 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4210 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4211 | |
| 4212 | buffer->frameCount = framesReq; |
| 4213 | return NO_ERROR; |
| 4214 | } |
| 4215 | |
| 4216 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4217 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4218 | buffer->frameCount = 0; |
| 4219 | return NOT_ENOUGH_DATA; |
| 4220 | } |
| 4221 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4222 | status_t AudioFlinger::RecordThread::RecordTrack::start(pid_t tid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4223 | { |
| 4224 | sp<ThreadBase> thread = mThread.promote(); |
| 4225 | if (thread != 0) { |
| 4226 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4227 | return recordThread->start(this, tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4228 | } else { |
| 4229 | return BAD_VALUE; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 4234 | { |
| 4235 | sp<ThreadBase> thread = mThread.promote(); |
| 4236 | if (thread != 0) { |
| 4237 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 4238 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 4239 | TrackBase::reset(); |
| 4240 | // Force overerrun condition to avoid false overrun callback until first data is |
| 4241 | // read from buffer |
| 4242 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 4247 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4248 | snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 4249 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4250 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4251 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4252 | mSessionId, |
| 4253 | mFrameCount, |
| 4254 | mState, |
| 4255 | mCblk->sampleRate, |
| 4256 | mCblk->server, |
| 4257 | mCblk->user); |
| 4258 | } |
| 4259 | |
| 4260 | |
| 4261 | // ---------------------------------------------------------------------------- |
| 4262 | |
| 4263 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4264 | PlaybackThread *playbackThread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4265 | DuplicatingThread *sourceThread, |
| 4266 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4267 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4268 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4269 | int frameCount) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 4270 | : Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4271 | mActive(false), mSourceThread(sourceThread) |
| 4272 | { |
| 4273 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4274 | if (mCblk != NULL) { |
| 4275 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 4276 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4277 | mOutBuffer.frameCount = 0; |
| 4278 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4279 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4280 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 4281 | mCblk, mBuffer, mCblk->buffers, |
| 4282 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4283 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4284 | ALOGW("Error creating output track on thread %p", playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4285 | } |
| 4286 | } |
| 4287 | |
| 4288 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 4289 | { |
| 4290 | clearBufferQueue(); |
| 4291 | } |
| 4292 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4293 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(pid_t tid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4294 | { |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4295 | status_t status = Track::start(tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4296 | if (status != NO_ERROR) { |
| 4297 | return status; |
| 4298 | } |
| 4299 | |
| 4300 | mActive = true; |
| 4301 | mRetryCount = 127; |
| 4302 | return status; |
| 4303 | } |
| 4304 | |
| 4305 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 4306 | { |
| 4307 | Track::stop(); |
| 4308 | clearBufferQueue(); |
| 4309 | mOutBuffer.frameCount = 0; |
| 4310 | mActive = false; |
| 4311 | } |
| 4312 | |
| 4313 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 4314 | { |
| 4315 | Buffer *pInBuffer; |
| 4316 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4317 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4318 | bool outputBufferFull = false; |
| 4319 | inBuffer.frameCount = frames; |
| 4320 | inBuffer.i16 = data; |
| 4321 | |
| 4322 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 4323 | |
| 4324 | if (!mActive && frames != 0) { |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4325 | start(0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4326 | sp<ThreadBase> thread = mThread.promote(); |
| 4327 | if (thread != 0) { |
| 4328 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 4329 | if (mCblk->frameCount > frames){ |
| 4330 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 4331 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 4332 | pInBuffer = new Buffer; |
| 4333 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 4334 | pInBuffer->frameCount = startFrames; |
| 4335 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 4336 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 4337 | mBufferQueue.add(pInBuffer); |
| 4338 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4339 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | while (waitTimeLeftMs) { |
| 4346 | // First write pending buffers, then new data |
| 4347 | if (mBufferQueue.size()) { |
| 4348 | pInBuffer = mBufferQueue.itemAt(0); |
| 4349 | } else { |
| 4350 | pInBuffer = &inBuffer; |
| 4351 | } |
| 4352 | |
| 4353 | if (pInBuffer->frameCount == 0) { |
| 4354 | break; |
| 4355 | } |
| 4356 | |
| 4357 | if (mOutBuffer.frameCount == 0) { |
| 4358 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 4359 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4360 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4361 | ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4362 | outputBufferFull = true; |
| 4363 | break; |
| 4364 | } |
| 4365 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 4366 | if (waitTimeLeftMs >= waitTimeMs) { |
| 4367 | waitTimeLeftMs -= waitTimeMs; |
| 4368 | } else { |
| 4369 | waitTimeLeftMs = 0; |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 4374 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 4375 | mCblk->stepUser(outFrames); |
| 4376 | pInBuffer->frameCount -= outFrames; |
| 4377 | pInBuffer->i16 += outFrames * channelCount; |
| 4378 | mOutBuffer.frameCount -= outFrames; |
| 4379 | mOutBuffer.i16 += outFrames * channelCount; |
| 4380 | |
| 4381 | if (pInBuffer->frameCount == 0) { |
| 4382 | if (mBufferQueue.size()) { |
| 4383 | mBufferQueue.removeAt(0); |
| 4384 | delete [] pInBuffer->mBuffer; |
| 4385 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4386 | ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4387 | } else { |
| 4388 | break; |
| 4389 | } |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 4394 | if (inBuffer.frameCount) { |
| 4395 | sp<ThreadBase> thread = mThread.promote(); |
| 4396 | if (thread != 0 && !thread->standby()) { |
| 4397 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 4398 | pInBuffer = new Buffer; |
| 4399 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 4400 | pInBuffer->frameCount = inBuffer.frameCount; |
| 4401 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 4402 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 4403 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4404 | ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4405 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4406 | ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4407 | } |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 4412 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 4413 | // by output mixer. |
| 4414 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 4415 | if (mCblk->user < mCblk->frameCount) { |
| 4416 | frames = mCblk->frameCount - mCblk->user; |
| 4417 | pInBuffer = new Buffer; |
| 4418 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 4419 | pInBuffer->frameCount = frames; |
| 4420 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 4421 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 4422 | mBufferQueue.add(pInBuffer); |
| 4423 | } else if (mActive) { |
| 4424 | stop(); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | return outputBufferFull; |
| 4429 | } |
| 4430 | |
| 4431 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 4432 | { |
| 4433 | int active; |
| 4434 | status_t result; |
| 4435 | audio_track_cblk_t* cblk = mCblk; |
| 4436 | uint32_t framesReq = buffer->frameCount; |
| 4437 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4438 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4439 | buffer->frameCount = 0; |
| 4440 | |
| 4441 | uint32_t framesAvail = cblk->framesAvailable(); |
| 4442 | |
| 4443 | |
| 4444 | if (framesAvail == 0) { |
| 4445 | Mutex::Autolock _l(cblk->lock); |
| 4446 | goto start_loop_here; |
| 4447 | while (framesAvail == 0) { |
| 4448 | active = mActive; |
Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4449 | if (CC_UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4450 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4451 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4452 | } |
| 4453 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 4454 | if (result != NO_ERROR) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4455 | return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4456 | } |
| 4457 | // read the server count again |
| 4458 | start_loop_here: |
| 4459 | framesAvail = cblk->framesAvailable_l(); |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | // if (framesAvail < framesReq) { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 4464 | // return NO_MORE_BUFFERS; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4465 | // } |
| 4466 | |
| 4467 | if (framesReq > framesAvail) { |
| 4468 | framesReq = framesAvail; |
| 4469 | } |
| 4470 | |
| 4471 | uint32_t u = cblk->user; |
| 4472 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 4473 | |
| 4474 | if (u + framesReq > bufferEnd) { |
| 4475 | framesReq = bufferEnd - u; |
| 4476 | } |
| 4477 | |
| 4478 | buffer->frameCount = framesReq; |
| 4479 | buffer->raw = (void *)cblk->buffer(u); |
| 4480 | return NO_ERROR; |
| 4481 | } |
| 4482 | |
| 4483 | |
| 4484 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 4485 | { |
| 4486 | size_t size = mBufferQueue.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4487 | |
| 4488 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 4489 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4490 | delete [] pBuffer->mBuffer; |
| 4491 | delete pBuffer; |
| 4492 | } |
| 4493 | mBufferQueue.clear(); |
| 4494 | } |
| 4495 | |
| 4496 | // ---------------------------------------------------------------------------- |
| 4497 | |
| 4498 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 4499 | : RefBase(), |
| 4500 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 4501 | // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4502 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4503 | mPid(pid), |
| 4504 | mTimedTrackCount(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4505 | { |
| 4506 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 4507 | } |
| 4508 | |
| 4509 | // Client destructor must be called with AudioFlinger::mLock held |
| 4510 | AudioFlinger::Client::~Client() |
| 4511 | { |
| 4512 | mAudioFlinger->removeClient_l(mPid); |
| 4513 | } |
| 4514 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 4515 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4516 | { |
| 4517 | return mMemoryDealer; |
| 4518 | } |
| 4519 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4520 | // Reserve one of the limited slots for a timed audio track associated |
| 4521 | // with this client |
| 4522 | bool AudioFlinger::Client::reserveTimedTrack() |
| 4523 | { |
| 4524 | const int kMaxTimedTracksPerClient = 4; |
| 4525 | |
| 4526 | Mutex::Autolock _l(mTimedTrackLock); |
| 4527 | |
| 4528 | if (mTimedTrackCount >= kMaxTimedTracksPerClient) { |
| 4529 | ALOGW("can not create timed track - pid %d has exceeded the limit", |
| 4530 | mPid); |
| 4531 | return false; |
| 4532 | } |
| 4533 | |
| 4534 | mTimedTrackCount++; |
| 4535 | return true; |
| 4536 | } |
| 4537 | |
| 4538 | // Release a slot for a timed audio track |
| 4539 | void AudioFlinger::Client::releaseTimedTrack() |
| 4540 | { |
| 4541 | Mutex::Autolock _l(mTimedTrackLock); |
| 4542 | mTimedTrackCount--; |
| 4543 | } |
| 4544 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4545 | // ---------------------------------------------------------------------------- |
| 4546 | |
| 4547 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 4548 | const sp<IAudioFlingerClient>& client, |
| 4549 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 4550 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4551 | { |
| 4552 | } |
| 4553 | |
| 4554 | AudioFlinger::NotificationClient::~NotificationClient() |
| 4555 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 4559 | { |
| 4560 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 4561 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | // ---------------------------------------------------------------------------- |
| 4565 | |
| 4566 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 4567 | : BnAudioTrack(), |
| 4568 | mTrack(track) |
| 4569 | { |
| 4570 | } |
| 4571 | |
| 4572 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 4573 | // just stop the track on deletion, associated resources |
| 4574 | // will be freed from the main thread once all pending buffers have |
| 4575 | // been played. Unless it's not in the active track list, in which |
| 4576 | // case we free everything now... |
| 4577 | mTrack->destroy(); |
| 4578 | } |
| 4579 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 4580 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 4581 | return mTrack->getCblk(); |
| 4582 | } |
| 4583 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4584 | status_t AudioFlinger::TrackHandle::start(pid_t tid) { |
| 4585 | return mTrack->start(tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | void AudioFlinger::TrackHandle::stop() { |
| 4589 | mTrack->stop(); |
| 4590 | } |
| 4591 | |
| 4592 | void AudioFlinger::TrackHandle::flush() { |
| 4593 | mTrack->flush(); |
| 4594 | } |
| 4595 | |
| 4596 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 4597 | mTrack->mute(e); |
| 4598 | } |
| 4599 | |
| 4600 | void AudioFlinger::TrackHandle::pause() { |
| 4601 | mTrack->pause(); |
| 4602 | } |
| 4603 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4604 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 4605 | { |
| 4606 | return mTrack->attachAuxEffect(EffectId); |
| 4607 | } |
| 4608 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4609 | status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size, |
| 4610 | sp<IMemory>* buffer) { |
| 4611 | if (!mTrack->isTimedTrack()) |
| 4612 | return INVALID_OPERATION; |
| 4613 | |
| 4614 | PlaybackThread::TimedTrack* tt = |
| 4615 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 4616 | return tt->allocateTimedBuffer(size, buffer); |
| 4617 | } |
| 4618 | |
| 4619 | status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer, |
| 4620 | int64_t pts) { |
| 4621 | if (!mTrack->isTimedTrack()) |
| 4622 | return INVALID_OPERATION; |
| 4623 | |
| 4624 | PlaybackThread::TimedTrack* tt = |
| 4625 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 4626 | return tt->queueTimedBuffer(buffer, pts); |
| 4627 | } |
| 4628 | |
| 4629 | status_t AudioFlinger::TrackHandle::setMediaTimeTransform( |
| 4630 | const LinearTransform& xform, int target) { |
| 4631 | |
| 4632 | if (!mTrack->isTimedTrack()) |
| 4633 | return INVALID_OPERATION; |
| 4634 | |
| 4635 | PlaybackThread::TimedTrack* tt = |
| 4636 | reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get()); |
| 4637 | return tt->setMediaTimeTransform( |
| 4638 | xform, static_cast<TimedAudioTrack::TargetTimeline>(target)); |
| 4639 | } |
| 4640 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4641 | status_t AudioFlinger::TrackHandle::onTransact( |
| 4642 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4643 | { |
| 4644 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 4645 | } |
| 4646 | |
| 4647 | // ---------------------------------------------------------------------------- |
| 4648 | |
| 4649 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 4650 | pid_t pid, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 4651 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4652 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4653 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4654 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4655 | int frameCount, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 4656 | // FIXME dead, remove from IAudioFlinger |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4657 | uint32_t flags, |
| 4658 | int *sessionId, |
| 4659 | status_t *status) |
| 4660 | { |
| 4661 | sp<RecordThread::RecordTrack> recordTrack; |
| 4662 | sp<RecordHandle> recordHandle; |
| 4663 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4664 | status_t lStatus; |
| 4665 | RecordThread *thread; |
| 4666 | size_t inFrameCount; |
| 4667 | int lSessionId; |
| 4668 | |
| 4669 | // check calling permissions |
| 4670 | if (!recordingAllowed()) { |
| 4671 | lStatus = PERMISSION_DENIED; |
| 4672 | goto Exit; |
| 4673 | } |
| 4674 | |
| 4675 | // add client to list |
| 4676 | { // scope for mLock |
| 4677 | Mutex::Autolock _l(mLock); |
| 4678 | thread = checkRecordThread_l(input); |
| 4679 | if (thread == NULL) { |
| 4680 | lStatus = BAD_VALUE; |
| 4681 | goto Exit; |
| 4682 | } |
| 4683 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 4684 | client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4685 | |
| 4686 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4687 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4688 | lSessionId = *sessionId; |
| 4689 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4690 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4691 | if (sessionId != NULL) { |
| 4692 | *sessionId = lSessionId; |
| 4693 | } |
| 4694 | } |
| 4695 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4696 | recordTrack = thread->createRecordTrack_l(client, |
| 4697 | sampleRate, |
| 4698 | format, |
| 4699 | channelMask, |
| 4700 | frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4701 | lSessionId, |
| 4702 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4703 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4704 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4705 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 4706 | // destructor is called by the TrackBase destructor with mLock held |
| 4707 | client.clear(); |
| 4708 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4709 | goto Exit; |
| 4710 | } |
| 4711 | |
| 4712 | // return to handle to client |
| 4713 | recordHandle = new RecordHandle(recordTrack); |
| 4714 | lStatus = NO_ERROR; |
| 4715 | |
| 4716 | Exit: |
| 4717 | if (status) { |
| 4718 | *status = lStatus; |
| 4719 | } |
| 4720 | return recordHandle; |
| 4721 | } |
| 4722 | |
| 4723 | // ---------------------------------------------------------------------------- |
| 4724 | |
| 4725 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 4726 | : BnAudioRecord(), |
| 4727 | mRecordTrack(recordTrack) |
| 4728 | { |
| 4729 | } |
| 4730 | |
| 4731 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 4732 | stop(); |
| 4733 | } |
| 4734 | |
Glenn Kasten | 90716c5 | 2012-01-26 13:40:12 -0800 | [diff] [blame] | 4735 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 4736 | return mRecordTrack->getCblk(); |
| 4737 | } |
| 4738 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4739 | status_t AudioFlinger::RecordHandle::start(pid_t tid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4740 | ALOGV("RecordHandle::start()"); |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 4741 | return mRecordTrack->start(tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4745 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4746 | mRecordTrack->stop(); |
| 4747 | } |
| 4748 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4749 | status_t AudioFlinger::RecordHandle::onTransact( |
| 4750 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4751 | { |
| 4752 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 4753 | } |
| 4754 | |
| 4755 | // ---------------------------------------------------------------------------- |
| 4756 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4757 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4758 | AudioStreamIn *input, |
| 4759 | uint32_t sampleRate, |
| 4760 | uint32_t channels, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 4761 | audio_io_handle_t id, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4762 | uint32_t device) : |
Glenn Kasten | 23bb8be | 2012-01-26 10:38:26 -0800 | [diff] [blame] | 4763 | ThreadBase(audioFlinger, id, device, RECORD), |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 4764 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), |
| 4765 | // mRsmpInIndex and mInputBytes set by readInputParameters() |
| 4766 | mReqChannelCount(popcount(channels)), |
| 4767 | mReqSampleRate(sampleRate) |
| 4768 | // mBytesRead is only meaningful while active, and so is cleared in start() |
| 4769 | // (but might be better to also clear here for dump?) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4770 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4771 | snprintf(mName, kNameLength, "AudioIn_%d", id); |
| 4772 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4773 | readInputParameters(); |
| 4774 | } |
| 4775 | |
| 4776 | |
| 4777 | AudioFlinger::RecordThread::~RecordThread() |
| 4778 | { |
| 4779 | delete[] mRsmpInBuffer; |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 4780 | delete mResampler; |
| 4781 | delete[] mRsmpOutBuffer; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | void AudioFlinger::RecordThread::onFirstRef() |
| 4785 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4786 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4787 | } |
| 4788 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4789 | status_t AudioFlinger::RecordThread::readyToRun() |
| 4790 | { |
| 4791 | status_t status = initCheck(); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4792 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4793 | return status; |
| 4794 | } |
| 4795 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4796 | bool AudioFlinger::RecordThread::threadLoop() |
| 4797 | { |
| 4798 | AudioBufferProvider::Buffer buffer; |
| 4799 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4800 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4801 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4802 | nsecs_t lastWarning = 0; |
| 4803 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4804 | acquireWakeLock(); |
| 4805 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4806 | // start recording |
| 4807 | while (!exitPending()) { |
| 4808 | |
| 4809 | processConfigEvents(); |
| 4810 | |
| 4811 | { // scope for mLock |
| 4812 | Mutex::Autolock _l(mLock); |
| 4813 | checkForNewParameters_l(); |
| 4814 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 4815 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4816 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4817 | mStandby = true; |
| 4818 | } |
| 4819 | |
| 4820 | if (exitPending()) break; |
| 4821 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4822 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4823 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4824 | // go to sleep |
| 4825 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4826 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4827 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4828 | continue; |
| 4829 | } |
| 4830 | if (mActiveTrack != 0) { |
| 4831 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 4832 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4833 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4834 | mStandby = true; |
| 4835 | } |
| 4836 | mActiveTrack.clear(); |
| 4837 | mStartStopCond.broadcast(); |
| 4838 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 4839 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 4840 | mActiveTrack.clear(); |
| 4841 | mStartStopCond.broadcast(); |
| 4842 | } else if (mBytesRead != 0) { |
| 4843 | // record start succeeds only if first read from audio input |
| 4844 | // succeeds |
| 4845 | if (mBytesRead > 0) { |
| 4846 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4847 | } else { |
| 4848 | mActiveTrack.clear(); |
| 4849 | } |
| 4850 | mStartStopCond.broadcast(); |
| 4851 | } |
| 4852 | mStandby = false; |
| 4853 | } |
| 4854 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4855 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | if (mActiveTrack != 0) { |
| 4859 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 4860 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4861 | unlockEffectChains(effectChains); |
| 4862 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4863 | continue; |
| 4864 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4865 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4866 | effectChains[i]->process_l(); |
| 4867 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4868 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4869 | buffer.frameCount = mFrameCount; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 4870 | if (CC_LIKELY(mActiveTrack->getNextBuffer( |
| 4871 | &buffer, AudioBufferProvider::kInvalidPTS) == NO_ERROR)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4872 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4873 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4874 | // no resampling |
| 4875 | while (framesOut) { |
| 4876 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 4877 | if (framesIn) { |
| 4878 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 4879 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 4880 | if (framesIn > framesOut) |
| 4881 | framesIn = framesOut; |
| 4882 | mRsmpInIndex += framesIn; |
| 4883 | framesOut -= framesIn; |
| 4884 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4885 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4886 | memcpy(dst, src, framesIn * mFrameSize); |
| 4887 | } else { |
| 4888 | int16_t *src16 = (int16_t *)src; |
| 4889 | int16_t *dst16 = (int16_t *)dst; |
| 4890 | if (mChannelCount == 1) { |
| 4891 | while (framesIn--) { |
| 4892 | *dst16++ = *src16; |
| 4893 | *dst16++ = *src16++; |
| 4894 | } |
| 4895 | } else { |
| 4896 | while (framesIn--) { |
| 4897 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 4898 | src16 += 2; |
| 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | } |
| 4903 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 4904 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4905 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4906 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4907 | framesOut = 0; |
| 4908 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4909 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4910 | mRsmpInIndex = 0; |
| 4911 | } |
| 4912 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4913 | ALOGE("Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4914 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4915 | // Force input into standby so that it tries to |
| 4916 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4917 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4918 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4919 | } |
| 4920 | mRsmpInIndex = mFrameCount; |
| 4921 | framesOut = 0; |
| 4922 | buffer.frameCount = 0; |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | } else { |
| 4927 | // resampling |
| 4928 | |
| 4929 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 4930 | // alter output frame count as if we were expecting stereo samples |
| 4931 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4932 | framesOut >>= 1; |
| 4933 | } |
| 4934 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 4935 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 4936 | // are 32 bit aligned which should be always true. |
| 4937 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4938 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4939 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 4940 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 4941 | int16_t *dst = buffer.i16; |
| 4942 | while (framesOut--) { |
| 4943 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 4944 | src += 2; |
| 4945 | } |
| 4946 | } else { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4947 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | } |
| 4951 | mActiveTrack->releaseBuffer(&buffer); |
| 4952 | mActiveTrack->overflow(); |
| 4953 | } |
| 4954 | // client isn't retrieving buffers fast enough |
| 4955 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4956 | if (!mActiveTrack->setOverflow()) { |
| 4957 | nsecs_t now = systemTime(); |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4958 | if ((now - lastWarning) > kWarningThrottleNs) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4959 | ALOGW("RecordThread: buffer overflow"); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4960 | lastWarning = now; |
| 4961 | } |
| 4962 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4963 | // Release the processor for a while before asking for a new buffer. |
| 4964 | // This will give the application more chance to read from the buffer and |
| 4965 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4966 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4967 | } |
| 4968 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4969 | // enable changes in effect chain |
| 4970 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4971 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4975 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4976 | } |
| 4977 | mActiveTrack.clear(); |
| 4978 | |
| 4979 | mStartStopCond.broadcast(); |
| 4980 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4981 | releaseWakeLock(); |
| 4982 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4983 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4984 | return false; |
| 4985 | } |
| 4986 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4987 | |
| 4988 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 4989 | const sp<AudioFlinger::Client>& client, |
| 4990 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 4991 | audio_format_t format, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4992 | int channelMask, |
| 4993 | int frameCount, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4994 | int sessionId, |
| 4995 | status_t *status) |
| 4996 | { |
| 4997 | sp<RecordTrack> track; |
| 4998 | status_t lStatus; |
| 4999 | |
| 5000 | lStatus = initCheck(); |
| 5001 | if (lStatus != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5002 | ALOGE("Audio driver not initialized."); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5003 | goto Exit; |
| 5004 | } |
| 5005 | |
| 5006 | { // scope for mLock |
| 5007 | Mutex::Autolock _l(mLock); |
| 5008 | |
| 5009 | track = new RecordTrack(this, client, sampleRate, |
Glenn Kasten | 5cf034d | 2012-02-21 10:35:56 -0800 | [diff] [blame^] | 5010 | format, channelMask, frameCount, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5011 | |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 5012 | if (track->getCblk() == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5013 | lStatus = NO_MEMORY; |
| 5014 | goto Exit; |
| 5015 | } |
| 5016 | |
| 5017 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5018 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5019 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 5020 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5021 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5022 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5023 | } |
| 5024 | lStatus = NO_ERROR; |
| 5025 | |
| 5026 | Exit: |
| 5027 | if (status) { |
| 5028 | *status = lStatus; |
| 5029 | } |
| 5030 | return track; |
| 5031 | } |
| 5032 | |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 5033 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, pid_t tid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5034 | { |
Glenn Kasten | 6dbc135 | 2012-02-02 10:56:47 -0800 | [diff] [blame] | 5035 | ALOGV("RecordThread::start tid=%d", tid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5036 | sp <ThreadBase> strongMe = this; |
| 5037 | status_t status = NO_ERROR; |
| 5038 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 5039 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5040 | if (mActiveTrack != 0) { |
| 5041 | if (recordTrack != mActiveTrack.get()) { |
| 5042 | status = -EBUSY; |
| 5043 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 5044 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 5045 | } |
| 5046 | return status; |
| 5047 | } |
| 5048 | |
| 5049 | recordTrack->mState = TrackBase::IDLE; |
| 5050 | mActiveTrack = recordTrack; |
| 5051 | mLock.unlock(); |
| 5052 | status_t status = AudioSystem::startInput(mId); |
| 5053 | mLock.lock(); |
| 5054 | if (status != NO_ERROR) { |
| 5055 | mActiveTrack.clear(); |
| 5056 | return status; |
| 5057 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5058 | mRsmpInIndex = mFrameCount; |
| 5059 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 5060 | if (mResampler != NULL) { |
| 5061 | mResampler->reset(); |
| 5062 | } |
| 5063 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5064 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5065 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5066 | mWaitWorkCV.signal(); |
| 5067 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5068 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5069 | mActiveTrack.clear(); |
| 5070 | status = INVALID_OPERATION; |
| 5071 | goto startError; |
| 5072 | } |
| 5073 | mStartStopCond.wait(mLock); |
| 5074 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5075 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5076 | status = BAD_VALUE; |
| 5077 | goto startError; |
| 5078 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5079 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5080 | return status; |
| 5081 | } |
| 5082 | startError: |
| 5083 | AudioSystem::stopInput(mId); |
| 5084 | return status; |
| 5085 | } |
| 5086 | |
| 5087 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5088 | ALOGV("RecordThread::stop"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5089 | sp <ThreadBase> strongMe = this; |
| 5090 | { |
Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 5091 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5092 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 5093 | mActiveTrack->mState = TrackBase::PAUSING; |
| 5094 | // do not wait for mStartStopCond if exiting |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5095 | if (exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5096 | return; |
| 5097 | } |
| 5098 | mStartStopCond.wait(mLock); |
| 5099 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 5100 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 5101 | mLock.unlock(); |
| 5102 | AudioSystem::stopInput(mId); |
| 5103 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5104 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | } |
| 5109 | |
| 5110 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 5111 | { |
| 5112 | const size_t SIZE = 256; |
| 5113 | char buffer[SIZE]; |
| 5114 | String8 result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5115 | |
| 5116 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 5117 | result.append(buffer); |
| 5118 | |
| 5119 | if (mActiveTrack != 0) { |
| 5120 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5121 | result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5122 | mActiveTrack->dump(buffer, SIZE); |
| 5123 | result.append(buffer); |
| 5124 | |
| 5125 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 5126 | result.append(buffer); |
| 5127 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 5128 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5129 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5130 | result.append(buffer); |
| 5131 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 5132 | result.append(buffer); |
| 5133 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 5134 | result.append(buffer); |
| 5135 | |
| 5136 | |
| 5137 | } else { |
| 5138 | result.append("No record client\n"); |
| 5139 | } |
| 5140 | write(fd, result.string(), result.size()); |
| 5141 | |
| 5142 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 5143 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5144 | |
| 5145 | return NO_ERROR; |
| 5146 | } |
| 5147 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 5148 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5149 | { |
| 5150 | size_t framesReq = buffer->frameCount; |
| 5151 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 5152 | int channelCount; |
| 5153 | |
| 5154 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5155 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5156 | if (mBytesRead < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5157 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5158 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 5159 | // Force input into standby so that it tries to |
| 5160 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5161 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5162 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5163 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5164 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5165 | buffer->frameCount = 0; |
| 5166 | return NOT_ENOUGH_DATA; |
| 5167 | } |
| 5168 | mRsmpInIndex = 0; |
| 5169 | framesReady = mFrameCount; |
| 5170 | } |
| 5171 | |
| 5172 | if (framesReq > framesReady) { |
| 5173 | framesReq = framesReady; |
| 5174 | } |
| 5175 | |
| 5176 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 5177 | channelCount = 1; |
| 5178 | } else { |
| 5179 | channelCount = 2; |
| 5180 | } |
| 5181 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 5182 | buffer->frameCount = framesReq; |
| 5183 | return NO_ERROR; |
| 5184 | } |
| 5185 | |
| 5186 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 5187 | { |
| 5188 | mRsmpInIndex += buffer->frameCount; |
| 5189 | buffer->frameCount = 0; |
| 5190 | } |
| 5191 | |
| 5192 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 5193 | { |
| 5194 | bool reconfig = false; |
| 5195 | |
| 5196 | while (!mNewParameters.isEmpty()) { |
| 5197 | status_t status = NO_ERROR; |
| 5198 | String8 keyValuePair = mNewParameters[0]; |
| 5199 | AudioParameter param = AudioParameter(keyValuePair); |
| 5200 | int value; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5201 | audio_format_t reqFormat = mFormat; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5202 | int reqSamplingRate = mReqSampleRate; |
| 5203 | int reqChannelCount = mReqChannelCount; |
| 5204 | |
| 5205 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 5206 | reqSamplingRate = value; |
| 5207 | reconfig = true; |
| 5208 | } |
| 5209 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5210 | reqFormat = (audio_format_t) value; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5211 | reconfig = true; |
| 5212 | } |
| 5213 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5214 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5215 | reconfig = true; |
| 5216 | } |
| 5217 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5218 | // do not accept frame count changes if tracks are open as the track buffer |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 5219 | // size depends on frame count and correct behavior would not be guaranteed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5220 | // if frame count is changed after track creation |
| 5221 | if (mActiveTrack != 0) { |
| 5222 | status = INVALID_OPERATION; |
| 5223 | } else { |
| 5224 | reconfig = true; |
| 5225 | } |
| 5226 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5227 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5228 | // forward device change to effects that have requested to be |
| 5229 | // aware of attached audio device. |
| 5230 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5231 | mEffectChains[i]->setDevice_l(value); |
| 5232 | } |
| 5233 | // store input device and output device but do not forward output device to audio HAL. |
| 5234 | // Note that status is ignored by the caller for output device |
| 5235 | // (see AudioFlinger::setParameters() |
| 5236 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 5237 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 5238 | status = BAD_VALUE; |
| 5239 | } else { |
| 5240 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5241 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5242 | if (mTrack != NULL) { |
| 5243 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 5244 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5245 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 5246 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 5247 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5248 | } |
| 5249 | mDevice |= (uint32_t)value; |
| 5250 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5251 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5252 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5253 | if (status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5254 | mInput->stream->common.standby(&mInput->stream->common); |
| 5255 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5256 | } |
| 5257 | if (reconfig) { |
| 5258 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5259 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5260 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5261 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
| 5262 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5263 | (reqChannelCount < 3)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5264 | status = NO_ERROR; |
| 5265 | } |
| 5266 | if (status == NO_ERROR) { |
| 5267 | readInputParameters(); |
| 5268 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 5269 | } |
| 5270 | } |
| 5271 | } |
| 5272 | |
| 5273 | mNewParameters.removeAt(0); |
| 5274 | |
| 5275 | mParamStatus = status; |
| 5276 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 5277 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 5278 | // already timed out waiting for the status and will never signal the condition. |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 5279 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5280 | } |
| 5281 | return reconfig; |
| 5282 | } |
| 5283 | |
| 5284 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 5285 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5286 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5287 | String8 out_s8 = String8(); |
| 5288 | |
| 5289 | Mutex::Autolock _l(mLock); |
| 5290 | if (initCheck() != NO_ERROR) { |
| 5291 | return out_s8; |
| 5292 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5293 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5294 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5295 | out_s8 = String8(s); |
| 5296 | free(s); |
| 5297 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 5301 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 5302 | void *param2 = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5303 | |
| 5304 | switch (event) { |
| 5305 | case AudioSystem::INPUT_OPENED: |
| 5306 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5307 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5308 | desc.samplingRate = mSampleRate; |
| 5309 | desc.format = mFormat; |
| 5310 | desc.frameCount = mFrameCount; |
| 5311 | desc.latency = 0; |
| 5312 | param2 = &desc; |
| 5313 | break; |
| 5314 | |
| 5315 | case AudioSystem::INPUT_CLOSED: |
| 5316 | default: |
| 5317 | break; |
| 5318 | } |
| 5319 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 5320 | } |
| 5321 | |
| 5322 | void AudioFlinger::RecordThread::readInputParameters() |
| 5323 | { |
Glenn Kasten | e9dd017 | 2012-01-27 18:08:45 -0800 | [diff] [blame] | 5324 | delete mRsmpInBuffer; |
| 5325 | // mRsmpInBuffer is always assigned a new[] below |
| 5326 | delete mRsmpOutBuffer; |
| 5327 | mRsmpOutBuffer = NULL; |
| 5328 | delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 5329 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5330 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5331 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 5332 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 5333 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5334 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 5335 | mFrameSize = audio_stream_frame_size(&mInput->stream->common); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5336 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5337 | mFrameCount = mInputBytes / mFrameSize; |
| 5338 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 5339 | |
| 5340 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 5341 | { |
| 5342 | int channelCount; |
| 5343 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 5344 | // stereo to mono post process as the resampler always outputs stereo. |
| 5345 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 5346 | channelCount = 1; |
| 5347 | } else { |
| 5348 | channelCount = 2; |
| 5349 | } |
| 5350 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 5351 | mResampler->setSampleRate(mSampleRate); |
| 5352 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 5353 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 5354 | |
| 5355 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 5356 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 5357 | mFrameCount >>= 1; |
| 5358 | } |
| 5359 | |
| 5360 | } |
| 5361 | mRsmpInIndex = mFrameCount; |
| 5362 | } |
| 5363 | |
| 5364 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 5365 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5366 | Mutex::Autolock _l(mLock); |
| 5367 | if (initCheck() != NO_ERROR) { |
| 5368 | return 0; |
| 5369 | } |
| 5370 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5371 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5372 | } |
| 5373 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5374 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 5375 | { |
| 5376 | Mutex::Autolock _l(mLock); |
| 5377 | uint32_t result = 0; |
| 5378 | if (getEffectChain_l(sessionId) != 0) { |
| 5379 | result = EFFECT_SESSION; |
| 5380 | } |
| 5381 | |
| 5382 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 5383 | result |= TRACK_SESSION; |
| 5384 | } |
| 5385 | |
| 5386 | return result; |
| 5387 | } |
| 5388 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 5389 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 5390 | { |
| 5391 | Mutex::Autolock _l(mLock); |
| 5392 | return mTrack; |
| 5393 | } |
| 5394 | |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 5395 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5396 | { |
| 5397 | Mutex::Autolock _l(mLock); |
| 5398 | return mInput; |
| 5399 | } |
| 5400 | |
| 5401 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 5402 | { |
| 5403 | Mutex::Autolock _l(mLock); |
| 5404 | AudioStreamIn *input = mInput; |
| 5405 | mInput = NULL; |
| 5406 | return input; |
| 5407 | } |
| 5408 | |
| 5409 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 5410 | audio_stream_t* AudioFlinger::RecordThread::stream() |
| 5411 | { |
| 5412 | if (mInput == NULL) { |
| 5413 | return NULL; |
| 5414 | } |
| 5415 | return &mInput->stream->common; |
| 5416 | } |
| 5417 | |
| 5418 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5419 | // ---------------------------------------------------------------------------- |
| 5420 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5421 | audio_io_handle_t AudioFlinger::openOutput(uint32_t *pDevices, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5422 | uint32_t *pSamplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5423 | audio_format_t *pFormat, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5424 | uint32_t *pChannels, |
| 5425 | uint32_t *pLatencyMs, |
| 5426 | uint32_t flags) |
| 5427 | { |
| 5428 | status_t status; |
| 5429 | PlaybackThread *thread = NULL; |
| 5430 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 5431 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5432 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5433 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5434 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5435 | audio_stream_out_t *outStream; |
| 5436 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5437 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5438 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5439 | pDevices ? *pDevices : 0, |
| 5440 | samplingRate, |
| 5441 | format, |
| 5442 | channels, |
| 5443 | flags); |
| 5444 | |
| 5445 | if (pDevices == NULL || *pDevices == 0) { |
| 5446 | return 0; |
| 5447 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5448 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5449 | Mutex::Autolock _l(mLock); |
| 5450 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5451 | outHwDev = findSuitableHwDev_l(*pDevices); |
| 5452 | if (outHwDev == NULL) |
| 5453 | return 0; |
| 5454 | |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5455 | status = outHwDev->open_output_stream(outHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5456 | &channels, &samplingRate, &outStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5457 | ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5458 | outStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5459 | samplingRate, |
| 5460 | format, |
| 5461 | channels, |
| 5462 | status); |
| 5463 | |
| 5464 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5465 | if (outStream != NULL) { |
| 5466 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5467 | audio_io_handle_t id = nextUniqueId(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5468 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5469 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || |
| 5470 | (format != AUDIO_FORMAT_PCM_16_BIT) || |
| 5471 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5472 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5473 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5474 | } else { |
| 5475 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5476 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5477 | } |
| 5478 | mPlaybackThreads.add(id, thread); |
| 5479 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 5480 | if (pSamplingRate != NULL) *pSamplingRate = samplingRate; |
| 5481 | if (pFormat != NULL) *pFormat = format; |
| 5482 | if (pChannels != NULL) *pChannels = channels; |
| 5483 | if (pLatencyMs != NULL) *pLatencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5484 | |
| 5485 | // notify client processes of the new output creation |
| 5486 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 5487 | return id; |
| 5488 | } |
| 5489 | |
| 5490 | return 0; |
| 5491 | } |
| 5492 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5493 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 5494 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5495 | { |
| 5496 | Mutex::Autolock _l(mLock); |
| 5497 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 5498 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 5499 | |
| 5500 | if (thread1 == NULL || thread2 == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5501 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5502 | return 0; |
| 5503 | } |
| 5504 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5505 | audio_io_handle_t id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5506 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 5507 | thread->addOutputTrack(thread2); |
| 5508 | mPlaybackThreads.add(id, thread); |
| 5509 | // notify client processes of the new output creation |
| 5510 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 5511 | return id; |
| 5512 | } |
| 5513 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5514 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5515 | { |
| 5516 | // keep strong reference on the playback thread so that |
| 5517 | // it is not destroyed while exit() is executed |
| 5518 | sp <PlaybackThread> thread; |
| 5519 | { |
| 5520 | Mutex::Autolock _l(mLock); |
| 5521 | thread = checkPlaybackThread_l(output); |
| 5522 | if (thread == NULL) { |
| 5523 | return BAD_VALUE; |
| 5524 | } |
| 5525 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5526 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5527 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5528 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5529 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5530 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5531 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 5532 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 5533 | } |
| 5534 | } |
| 5535 | } |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5536 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5537 | mPlaybackThreads.removeItem(output); |
| 5538 | } |
| 5539 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5540 | // The thread entity (active unit of execution) is no longer running here, |
| 5541 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5542 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5543 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5544 | AudioStreamOut *out = thread->clearOutput(); |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 5545 | assert(out != NULL); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5546 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5547 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 5548 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5549 | } |
| 5550 | return NO_ERROR; |
| 5551 | } |
| 5552 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5553 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5554 | { |
| 5555 | Mutex::Autolock _l(mLock); |
| 5556 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5557 | |
| 5558 | if (thread == NULL) { |
| 5559 | return BAD_VALUE; |
| 5560 | } |
| 5561 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5562 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5563 | thread->suspend(); |
| 5564 | |
| 5565 | return NO_ERROR; |
| 5566 | } |
| 5567 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5568 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5569 | { |
| 5570 | Mutex::Autolock _l(mLock); |
| 5571 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5572 | |
| 5573 | if (thread == NULL) { |
| 5574 | return BAD_VALUE; |
| 5575 | } |
| 5576 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5577 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5578 | |
| 5579 | thread->restore(); |
| 5580 | |
| 5581 | return NO_ERROR; |
| 5582 | } |
| 5583 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5584 | audio_io_handle_t AudioFlinger::openInput(uint32_t *pDevices, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5585 | uint32_t *pSamplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5586 | audio_format_t *pFormat, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5587 | uint32_t *pChannels, |
Glenn Kasten | de9719b | 2012-01-27 12:32:34 -0800 | [diff] [blame] | 5588 | audio_in_acoustics_t acoustics) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5589 | { |
| 5590 | status_t status; |
| 5591 | RecordThread *thread = NULL; |
| 5592 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5593 | audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5594 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5595 | uint32_t reqSamplingRate = samplingRate; |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5596 | audio_format_t reqFormat = format; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5597 | uint32_t reqChannels = channels; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5598 | audio_stream_in_t *inStream; |
| 5599 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5600 | |
| 5601 | if (pDevices == NULL || *pDevices == 0) { |
| 5602 | return 0; |
| 5603 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5604 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5605 | Mutex::Autolock _l(mLock); |
| 5606 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5607 | inHwDev = findSuitableHwDev_l(*pDevices); |
| 5608 | if (inHwDev == NULL) |
| 5609 | return 0; |
| 5610 | |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5611 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5612 | &channels, &samplingRate, |
Glenn Kasten | de9719b | 2012-01-27 12:32:34 -0800 | [diff] [blame] | 5613 | acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5614 | &inStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5615 | ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d", |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5616 | inStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5617 | samplingRate, |
| 5618 | format, |
| 5619 | channels, |
| 5620 | acoustics, |
| 5621 | status); |
| 5622 | |
| 5623 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 5624 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 5625 | // or stereo to mono conversions on 16 bit PCM inputs. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5626 | if (inStream == NULL && status == BAD_VALUE && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5627 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5628 | (samplingRate <= 2 * reqSamplingRate) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5629 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5630 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 5631 | status = inHwDev->open_input_stream(inHwDev, *pDevices, &format, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5632 | &channels, &samplingRate, |
Glenn Kasten | de9719b | 2012-01-27 12:32:34 -0800 | [diff] [blame] | 5633 | acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5634 | &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5635 | } |
| 5636 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5637 | if (inStream != NULL) { |
| 5638 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 5639 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5640 | audio_io_handle_t id = nextUniqueId(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5641 | // Start record thread |
| 5642 | // RecorThread require both input and output device indication to forward to audio |
| 5643 | // pre processing modules |
| 5644 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 5645 | thread = new RecordThread(this, |
| 5646 | input, |
| 5647 | reqSamplingRate, |
| 5648 | reqChannels, |
| 5649 | id, |
| 5650 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5651 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5652 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 5653 | if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate; |
| 5654 | if (pFormat != NULL) *pFormat = format; |
| 5655 | if (pChannels != NULL) *pChannels = reqChannels; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5656 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5657 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5658 | |
| 5659 | // notify client processes of the new input creation |
| 5660 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 5661 | return id; |
| 5662 | } |
| 5663 | |
| 5664 | return 0; |
| 5665 | } |
| 5666 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5667 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5668 | { |
| 5669 | // keep strong reference on the record thread so that |
| 5670 | // it is not destroyed while exit() is executed |
| 5671 | sp <RecordThread> thread; |
| 5672 | { |
| 5673 | Mutex::Autolock _l(mLock); |
| 5674 | thread = checkRecordThread_l(input); |
| 5675 | if (thread == NULL) { |
| 5676 | return BAD_VALUE; |
| 5677 | } |
| 5678 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5679 | ALOGV("closeInput() %d", input); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5680 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5681 | mRecordThreads.removeItem(input); |
| 5682 | } |
| 5683 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 5684 | // The thread entity (active unit of execution) is no longer running here, |
| 5685 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5686 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5687 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | aed850d | 2012-01-26 09:46:34 -0800 | [diff] [blame] | 5688 | assert(in != NULL); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5689 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5690 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 5691 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5692 | |
| 5693 | return NO_ERROR; |
| 5694 | } |
| 5695 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5696 | status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5697 | { |
| 5698 | Mutex::Autolock _l(mLock); |
| 5699 | MixerThread *dstThread = checkMixerThread_l(output); |
| 5700 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5701 | ALOGW("setStreamOutput() bad output id %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5702 | return BAD_VALUE; |
| 5703 | } |
| 5704 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5705 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5706 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 5707 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5708 | dstThread->setStreamValid(stream, true); |
| 5709 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5710 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5711 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5712 | if (thread != dstThread && thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5713 | MixerThread *srcThread = (MixerThread *)thread; |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5714 | srcThread->setStreamValid(stream, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5715 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5716 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5717 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5718 | |
| 5719 | return NO_ERROR; |
| 5720 | } |
| 5721 | |
| 5722 | |
| 5723 | int AudioFlinger::newAudioSessionId() |
| 5724 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5725 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5726 | } |
| 5727 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5728 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 5729 | { |
| 5730 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 5731 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5732 | ALOGV("acquiring %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 5733 | size_t num = mAudioSessionRefs.size(); |
| 5734 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5735 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
| 5736 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5737 | ref->cnt++; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5738 | ALOGV(" incremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5739 | return; |
| 5740 | } |
| 5741 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 5742 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 5743 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 5747 | { |
| 5748 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 5749 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5750 | ALOGV("releasing %d from %d", audioSession, caller); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 5751 | size_t num = mAudioSessionRefs.size(); |
| 5752 | for (size_t i = 0; i< num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5753 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 5754 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5755 | ref->cnt--; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5756 | ALOGV(" decremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5757 | if (ref->cnt == 0) { |
| 5758 | mAudioSessionRefs.removeAt(i); |
| 5759 | delete ref; |
| 5760 | purgeStaleEffects_l(); |
| 5761 | } |
| 5762 | return; |
| 5763 | } |
| 5764 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5765 | ALOGW("session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | void AudioFlinger::purgeStaleEffects_l() { |
| 5769 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5770 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5771 | |
| 5772 | Vector< sp<EffectChain> > chains; |
| 5773 | |
| 5774 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5775 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 5776 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5777 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5778 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 5779 | chains.push(ec); |
| 5780 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5781 | } |
| 5782 | } |
| 5783 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5784 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 5785 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5786 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 5787 | chains.push(ec); |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | for (size_t i = 0; i < chains.size(); i++) { |
| 5792 | sp<EffectChain> ec = chains[i]; |
| 5793 | int sessionid = ec->sessionId(); |
| 5794 | sp<ThreadBase> t = ec->mThread.promote(); |
| 5795 | if (t == 0) { |
| 5796 | continue; |
| 5797 | } |
| 5798 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 5799 | bool found = false; |
| 5800 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 5801 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
| 5802 | if (ref->sessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5803 | ALOGV(" session %d still exists for %d with %d refs", |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5804 | sessionid, ref->pid, ref->cnt); |
| 5805 | found = true; |
| 5806 | break; |
| 5807 | } |
| 5808 | } |
| 5809 | if (!found) { |
| 5810 | // remove all effects from the chain |
| 5811 | while (ec->mEffects.size()) { |
| 5812 | sp<EffectModule> effect = ec->mEffects[0]; |
| 5813 | effect->unPin(); |
| 5814 | Mutex::Autolock _l (t->mLock); |
| 5815 | t->removeEffect_l(effect); |
| 5816 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 5817 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 5818 | if (handle != 0) { |
| 5819 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5820 | if (handle->mHasControl && handle->mEnabled) { |
| 5821 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 5822 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5823 | } |
| 5824 | } |
| 5825 | AudioSystem::unregisterEffect(effect->id()); |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | return; |
| 5830 | } |
| 5831 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5832 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5833 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5834 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5835 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5839 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5840 | { |
| 5841 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5842 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5846 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5847 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 5848 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5849 | } |
| 5850 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5851 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5852 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5853 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5854 | } |
| 5855 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5856 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() |
| 5857 | { |
| 5858 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5859 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5860 | AudioStreamOut *output = thread->getOutput(); |
| 5861 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5862 | return thread; |
| 5863 | } |
| 5864 | } |
| 5865 | return NULL; |
| 5866 | } |
| 5867 | |
| 5868 | uint32_t AudioFlinger::primaryOutputDevice_l() |
| 5869 | { |
| 5870 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 5871 | |
| 5872 | if (thread == NULL) { |
| 5873 | return 0; |
| 5874 | } |
| 5875 | |
| 5876 | return thread->device(); |
| 5877 | } |
| 5878 | |
| 5879 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5880 | // ---------------------------------------------------------------------------- |
| 5881 | // Effect management |
| 5882 | // ---------------------------------------------------------------------------- |
| 5883 | |
| 5884 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 5885 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5886 | { |
| 5887 | Mutex::Autolock _l(mLock); |
| 5888 | return EffectQueryNumberEffects(numEffects); |
| 5889 | } |
| 5890 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 5891 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5892 | { |
| 5893 | Mutex::Autolock _l(mLock); |
| 5894 | return EffectQueryEffect(index, descriptor); |
| 5895 | } |
| 5896 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 5897 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 5898 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5899 | { |
| 5900 | Mutex::Autolock _l(mLock); |
| 5901 | return EffectGetDescriptor(pUuid, descriptor); |
| 5902 | } |
| 5903 | |
| 5904 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5905 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 5906 | effect_descriptor_t *pDesc, |
| 5907 | const sp<IEffectClient>& effectClient, |
| 5908 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 5909 | audio_io_handle_t io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5910 | int sessionId, |
| 5911 | status_t *status, |
| 5912 | int *id, |
| 5913 | int *enabled) |
| 5914 | { |
| 5915 | status_t lStatus = NO_ERROR; |
| 5916 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5917 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5918 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 5919 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5920 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5921 | |
| 5922 | if (pDesc == NULL) { |
| 5923 | lStatus = BAD_VALUE; |
| 5924 | goto Exit; |
| 5925 | } |
| 5926 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5927 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5928 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5929 | lStatus = PERMISSION_DENIED; |
| 5930 | goto Exit; |
| 5931 | } |
| 5932 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5933 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5934 | // that can only be created by audio policy manager (running in same process) |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 5935 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5936 | lStatus = PERMISSION_DENIED; |
| 5937 | goto Exit; |
| 5938 | } |
| 5939 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5940 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5941 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5942 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5943 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5944 | lStatus = BAD_VALUE; |
| 5945 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5946 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5947 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5948 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5949 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5950 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5951 | } |
| 5952 | } |
| 5953 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5954 | { |
| 5955 | Mutex::Autolock _l(mLock); |
| 5956 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5957 | |
| 5958 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 5959 | // if uuid is specified, request effect descriptor |
| 5960 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 5961 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5962 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5963 | goto Exit; |
| 5964 | } |
| 5965 | } else { |
| 5966 | // if uuid is not specified, look for an available implementation |
| 5967 | // of the required type in effect factory |
| 5968 | if (EffectIsNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5969 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5970 | lStatus = BAD_VALUE; |
| 5971 | goto Exit; |
| 5972 | } |
| 5973 | uint32_t numEffects = 0; |
| 5974 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5975 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5976 | bool found = false; |
| 5977 | |
| 5978 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 5979 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5980 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5981 | goto Exit; |
| 5982 | } |
| 5983 | for (uint32_t i = 0; i < numEffects; i++) { |
| 5984 | lStatus = EffectQueryEffect(i, &desc); |
| 5985 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5986 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5987 | continue; |
| 5988 | } |
| 5989 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 5990 | // If matching type found save effect descriptor. If the session is |
| 5991 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 5992 | // an auxiliary version of this effect type is available |
| 5993 | found = true; |
| 5994 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5995 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5996 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5997 | break; |
| 5998 | } |
| 5999 | } |
| 6000 | } |
| 6001 | if (!found) { |
| 6002 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6003 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6004 | goto Exit; |
| 6005 | } |
| 6006 | // For same effect type, chose auxiliary version over insert version if |
| 6007 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6008 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6009 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6010 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6015 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6016 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6017 | lStatus = INVALID_OPERATION; |
| 6018 | goto Exit; |
| 6019 | } |
| 6020 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6021 | // check recording permission for visualizer |
| 6022 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 6023 | !recordingAllowed()) { |
| 6024 | lStatus = PERMISSION_DENIED; |
| 6025 | goto Exit; |
| 6026 | } |
| 6027 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6028 | // return effect descriptor |
| 6029 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 6030 | |
| 6031 | // If output is not specified try to find a matching audio session ID in one of the |
| 6032 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6033 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 6034 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6035 | // Note: io is never 0 when creating an effect on an input |
| 6036 | if (io == 0) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6037 | // look for the thread where the specified audio session is present |
| 6038 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 6039 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6040 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6041 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6042 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6043 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6044 | if (io == 0) { |
| 6045 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 6046 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 6047 | io = mRecordThreads.keyAt(i); |
| 6048 | break; |
| 6049 | } |
| 6050 | } |
| 6051 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6052 | // If no output thread contains the requested session ID, default to |
| 6053 | // first output. The effect chain will be moved to the correct output |
| 6054 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6055 | if (io == 0 && mPlaybackThreads.size()) { |
| 6056 | io = mPlaybackThreads.keyAt(0); |
| 6057 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6058 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6059 | } |
| 6060 | ThreadBase *thread = checkRecordThread_l(io); |
| 6061 | if (thread == NULL) { |
| 6062 | thread = checkPlaybackThread_l(io); |
| 6063 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6064 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6065 | lStatus = BAD_VALUE; |
| 6066 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6067 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6068 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 6069 | |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 6070 | sp<Client> client = registerPid_l(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6071 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6072 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6073 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 6074 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6075 | if (handle != 0 && id != NULL) { |
| 6076 | *id = handle->id(); |
| 6077 | } |
| 6078 | } |
| 6079 | |
| 6080 | Exit: |
| 6081 | if(status) { |
| 6082 | *status = lStatus; |
| 6083 | } |
| 6084 | return handle; |
| 6085 | } |
| 6086 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6087 | status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, |
| 6088 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6089 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6090 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6091 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6092 | Mutex::Autolock _l(mLock); |
| 6093 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6094 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6095 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6096 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6097 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 6098 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6099 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6100 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6101 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6102 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 6103 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6104 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6105 | return BAD_VALUE; |
| 6106 | } |
| 6107 | |
| 6108 | Mutex::Autolock _dl(dstThread->mLock); |
| 6109 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6110 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6111 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6112 | return NO_ERROR; |
| 6113 | } |
| 6114 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6115 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6116 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6117 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6118 | AudioFlinger::PlaybackThread *dstThread, |
| 6119 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6120 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6121 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6122 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6123 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6124 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6125 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6126 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6127 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6128 | return INVALID_OPERATION; |
| 6129 | } |
| 6130 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6131 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6132 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6133 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6134 | // removed. |
| 6135 | srcThread->removeEffectChain_l(chain); |
| 6136 | |
| 6137 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 6138 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 6139 | audio_io_handle_t dstOutput = dstThread->id(); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6140 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6141 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6142 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 6143 | while (effect != 0) { |
| 6144 | srcThread->removeEffect_l(effect); |
| 6145 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6146 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 6147 | if (effect->state() == EffectModule::ACTIVE || |
| 6148 | effect->state() == EffectModule::STOPPING) { |
| 6149 | effect->start(); |
| 6150 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6151 | // if the move request is not received from audio policy manager, the effect must be |
| 6152 | // re-registered with the new strategy and output |
| 6153 | if (dstChain == 0) { |
| 6154 | dstChain = effect->chain().promote(); |
| 6155 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6156 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6157 | srcThread->addEffect_l(effect); |
| 6158 | return NO_INIT; |
| 6159 | } |
| 6160 | strategy = dstChain->strategy(); |
| 6161 | } |
| 6162 | if (reRegister) { |
| 6163 | AudioSystem::unregisterEffect(effect->id()); |
| 6164 | AudioSystem::registerEffect(&effect->desc(), |
| 6165 | dstOutput, |
| 6166 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6167 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 6168 | effect->id()); |
| 6169 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6170 | effect = chain->getEffectFromId_l(0); |
| 6171 | } |
| 6172 | |
| 6173 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6174 | } |
| 6175 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6176 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6177 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6178 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6179 | const sp<AudioFlinger::Client>& client, |
| 6180 | const sp<IEffectClient>& effectClient, |
| 6181 | int32_t priority, |
| 6182 | int sessionId, |
| 6183 | effect_descriptor_t *desc, |
| 6184 | int *enabled, |
| 6185 | status_t *status |
| 6186 | ) |
| 6187 | { |
| 6188 | sp<EffectModule> effect; |
| 6189 | sp<EffectHandle> handle; |
| 6190 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6191 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6192 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6193 | bool effectCreated = false; |
| 6194 | bool effectRegistered = false; |
| 6195 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6196 | lStatus = initCheck(); |
| 6197 | if (lStatus != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6198 | ALOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6199 | goto Exit; |
| 6200 | } |
| 6201 | |
| 6202 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 6203 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6204 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6205 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6206 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6207 | lStatus = BAD_VALUE; |
| 6208 | goto Exit; |
| 6209 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6210 | // Only Pre processor effects are allowed on input threads and only on input threads |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 6211 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6212 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6213 | desc->name, desc->flags, mType); |
| 6214 | lStatus = BAD_VALUE; |
| 6215 | goto Exit; |
| 6216 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6217 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6218 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6219 | |
| 6220 | { // scope for mLock |
| 6221 | Mutex::Autolock _l(mLock); |
| 6222 | |
| 6223 | // check for existing effect chain with the requested audio session |
| 6224 | chain = getEffectChain_l(sessionId); |
| 6225 | if (chain == 0) { |
| 6226 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6227 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6228 | chain = new EffectChain(this, sessionId); |
| 6229 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6230 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 6231 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6232 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6233 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6234 | } |
| 6235 | |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 6236 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6237 | |
| 6238 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6239 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6240 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6241 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6242 | if (lStatus != NO_ERROR) { |
| 6243 | goto Exit; |
| 6244 | } |
| 6245 | effectRegistered = true; |
| 6246 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6247 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6248 | lStatus = effect->status(); |
| 6249 | if (lStatus != NO_ERROR) { |
| 6250 | goto Exit; |
| 6251 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6252 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6253 | if (lStatus != NO_ERROR) { |
| 6254 | goto Exit; |
| 6255 | } |
| 6256 | effectCreated = true; |
| 6257 | |
| 6258 | effect->setDevice(mDevice); |
| 6259 | effect->setMode(mAudioFlinger->getMode()); |
| 6260 | } |
| 6261 | // create effect handle and connect it to effect module |
| 6262 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 6263 | lStatus = effect->addHandle(handle); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6264 | if (enabled != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6265 | *enabled = (int)effect->isEnabled(); |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | Exit: |
| 6270 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6271 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6272 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6273 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6274 | } |
| 6275 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6276 | AudioSystem::unregisterEffect(effect->id()); |
| 6277 | } |
| 6278 | if (chainCreated) { |
| 6279 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6280 | } |
| 6281 | handle.clear(); |
| 6282 | } |
| 6283 | |
| 6284 | if(status) { |
| 6285 | *status = lStatus; |
| 6286 | } |
| 6287 | return handle; |
| 6288 | } |
| 6289 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6290 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 6291 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6292 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 6293 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6294 | } |
| 6295 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6296 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 6297 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6298 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6299 | { |
| 6300 | // check for existing effect chain with the requested audio session |
| 6301 | int sessionId = effect->sessionId(); |
| 6302 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6303 | bool chainCreated = false; |
| 6304 | |
| 6305 | if (chain == 0) { |
| 6306 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6307 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6308 | chain = new EffectChain(this, sessionId); |
| 6309 | addEffectChain_l(chain); |
| 6310 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 6311 | chainCreated = true; |
| 6312 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6313 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6314 | |
| 6315 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6316 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6317 | this, effect->desc().name, chain.get()); |
| 6318 | return BAD_VALUE; |
| 6319 | } |
| 6320 | |
| 6321 | status_t status = chain->addEffect_l(effect); |
| 6322 | if (status != NO_ERROR) { |
| 6323 | if (chainCreated) { |
| 6324 | removeEffectChain_l(chain); |
| 6325 | } |
| 6326 | return status; |
| 6327 | } |
| 6328 | |
| 6329 | effect->setDevice(mDevice); |
| 6330 | effect->setMode(mAudioFlinger->getMode()); |
| 6331 | return NO_ERROR; |
| 6332 | } |
| 6333 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6334 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6335 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6336 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6337 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6338 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6339 | detachAuxEffect_l(effect->id()); |
| 6340 | } |
| 6341 | |
| 6342 | sp<EffectChain> chain = effect->chain().promote(); |
| 6343 | if (chain != 0) { |
| 6344 | // remove effect chain if removing last effect |
| 6345 | if (chain->removeEffect_l(effect) == 0) { |
| 6346 | removeEffectChain_l(chain); |
| 6347 | } |
| 6348 | } else { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6349 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6350 | } |
| 6351 | } |
| 6352 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6353 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 6354 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 6355 | { |
| 6356 | effectChains = mEffectChains; |
| 6357 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6358 | mEffectChains[i]->lock(); |
| 6359 | } |
| 6360 | } |
| 6361 | |
| 6362 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 6363 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 6364 | { |
| 6365 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 6366 | effectChains[i]->unlock(); |
| 6367 | } |
| 6368 | } |
| 6369 | |
| 6370 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 6371 | { |
| 6372 | Mutex::Autolock _l(mLock); |
| 6373 | return getEffectChain_l(sessionId); |
| 6374 | } |
| 6375 | |
| 6376 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 6377 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6378 | size_t size = mEffectChains.size(); |
| 6379 | for (size_t i = 0; i < size; i++) { |
| 6380 | if (mEffectChains[i]->sessionId() == sessionId) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 6381 | return mEffectChains[i]; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6382 | } |
| 6383 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 6384 | return 0; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6385 | } |
| 6386 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 6387 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6388 | { |
| 6389 | Mutex::Autolock _l(mLock); |
| 6390 | size_t size = mEffectChains.size(); |
| 6391 | for (size_t i = 0; i < size; i++) { |
| 6392 | mEffectChains[i]->setMode_l(mode); |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6397 | const wp<EffectHandle>& handle, |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 6398 | bool unpinIfLast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6399 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6400 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6401 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6402 | // delete the effect module if removing last handle on it |
| 6403 | if (effect->removeHandle(handle) == 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 6404 | if (!effect->isPinned() || unpinIfLast) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6405 | removeEffect_l(effect); |
| 6406 | AudioSystem::unregisterEffect(effect->id()); |
| 6407 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6408 | } |
| 6409 | } |
| 6410 | |
| 6411 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6412 | { |
| 6413 | int session = chain->sessionId(); |
| 6414 | int16_t *buffer = mMixBuffer; |
| 6415 | bool ownsBuffer = false; |
| 6416 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6417 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6418 | if (session > 0) { |
| 6419 | // Only one effect chain can be present in direct output thread and it uses |
| 6420 | // the mix buffer as input |
| 6421 | if (mType != DIRECT) { |
| 6422 | size_t numSamples = mFrameCount * mChannelCount; |
| 6423 | buffer = new int16_t[numSamples]; |
| 6424 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6425 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6426 | ownsBuffer = true; |
| 6427 | } |
| 6428 | |
| 6429 | // Attach all tracks with same session ID to this chain. |
| 6430 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6431 | sp<Track> track = mTracks[i]; |
| 6432 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6433 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6434 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6435 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6436 | } |
| 6437 | } |
| 6438 | |
| 6439 | // indicate all active tracks in the chain |
| 6440 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 6441 | sp<Track> track = mActiveTracks[i].promote(); |
| 6442 | if (track == 0) continue; |
| 6443 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6444 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6445 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6446 | } |
| 6447 | } |
| 6448 | } |
| 6449 | |
| 6450 | chain->setInBuffer(buffer, ownsBuffer); |
| 6451 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6452 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6453 | // chains list in order to be processed last as it contains output stage effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6454 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 6455 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6456 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6457 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 6458 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6459 | // Effect chain for other sessions are inserted at beginning of effect |
| 6460 | // chains list to be processed before output mix effects. Relative order between other |
| 6461 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6462 | size_t size = mEffectChains.size(); |
| 6463 | size_t i = 0; |
| 6464 | for (i = 0; i < size; i++) { |
| 6465 | if (mEffectChains[i]->sessionId() < session) break; |
| 6466 | } |
| 6467 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6468 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6469 | |
| 6470 | return NO_ERROR; |
| 6471 | } |
| 6472 | |
| 6473 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6474 | { |
| 6475 | int session = chain->sessionId(); |
| 6476 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6477 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6478 | |
| 6479 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6480 | if (chain == mEffectChains[i]) { |
| 6481 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6482 | // detach all active tracks from the chain |
| 6483 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 6484 | sp<Track> track = mActiveTracks[i].promote(); |
| 6485 | if (track == 0) continue; |
| 6486 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6487 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6488 | chain.get(), session); |
| 6489 | chain->decActiveTrackCnt(); |
| 6490 | } |
| 6491 | } |
| 6492 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6493 | // detach all tracks with same session ID from this chain |
| 6494 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6495 | sp<Track> track = mTracks[i]; |
| 6496 | if (session == track->sessionId()) { |
| 6497 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6498 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6499 | } |
| 6500 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6501 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6502 | } |
| 6503 | } |
| 6504 | return mEffectChains.size(); |
| 6505 | } |
| 6506 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6507 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 6508 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6509 | { |
| 6510 | Mutex::Autolock _l(mLock); |
| 6511 | return attachAuxEffect_l(track, EffectId); |
| 6512 | } |
| 6513 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6514 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 6515 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6516 | { |
| 6517 | status_t status = NO_ERROR; |
| 6518 | |
| 6519 | if (EffectId == 0) { |
| 6520 | track->setAuxBuffer(0, NULL); |
| 6521 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6522 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 6523 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6524 | if (effect != 0) { |
| 6525 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6526 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 6527 | } else { |
| 6528 | status = INVALID_OPERATION; |
| 6529 | } |
| 6530 | } else { |
| 6531 | status = BAD_VALUE; |
| 6532 | } |
| 6533 | } |
| 6534 | return status; |
| 6535 | } |
| 6536 | |
| 6537 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 6538 | { |
| 6539 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6540 | sp<Track> track = mTracks[i]; |
| 6541 | if (track->auxEffectId() == effectId) { |
| 6542 | attachAuxEffect_l(track, 0); |
| 6543 | } |
| 6544 | } |
| 6545 | } |
| 6546 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6547 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6548 | { |
| 6549 | // only one chain per input thread |
| 6550 | if (mEffectChains.size() != 0) { |
| 6551 | return INVALID_OPERATION; |
| 6552 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6553 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6554 | |
| 6555 | chain->setInBuffer(NULL); |
| 6556 | chain->setOutBuffer(NULL); |
| 6557 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6558 | checkSuspendOnAddEffectChain_l(chain); |
| 6559 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6560 | mEffectChains.add(chain); |
| 6561 | |
| 6562 | return NO_ERROR; |
| 6563 | } |
| 6564 | |
| 6565 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6566 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6567 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6568 | ALOGW_IF(mEffectChains.size() != 1, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6569 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6570 | chain.get(), mEffectChains.size(), this); |
| 6571 | if (mEffectChains.size() == 1) { |
| 6572 | mEffectChains.removeAt(0); |
| 6573 | } |
| 6574 | return 0; |
| 6575 | } |
| 6576 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6577 | // ---------------------------------------------------------------------------- |
| 6578 | // EffectModule implementation |
| 6579 | // ---------------------------------------------------------------------------- |
| 6580 | |
| 6581 | #undef LOG_TAG |
| 6582 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 6583 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 6584 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6585 | const wp<AudioFlinger::EffectChain>& chain, |
| 6586 | effect_descriptor_t *desc, |
| 6587 | int id, |
| 6588 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 6589 | : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6590 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6591 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6592 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6593 | int lStatus; |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 6594 | if (thread == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6595 | return; |
| 6596 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6597 | |
| 6598 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 6599 | |
| 6600 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6601 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6602 | |
| 6603 | if (mStatus != NO_ERROR) { |
| 6604 | return; |
| 6605 | } |
| 6606 | lStatus = init(); |
| 6607 | if (lStatus < 0) { |
| 6608 | mStatus = lStatus; |
| 6609 | goto Error; |
| 6610 | } |
| 6611 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6612 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 6613 | mPinned = true; |
| 6614 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6615 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6616 | return; |
| 6617 | Error: |
| 6618 | EffectRelease(mEffectInterface); |
| 6619 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6620 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6621 | } |
| 6622 | |
| 6623 | AudioFlinger::EffectModule::~EffectModule() |
| 6624 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6625 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6626 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6627 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6628 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 6629 | sp<ThreadBase> thread = mThread.promote(); |
| 6630 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6631 | audio_stream_t *stream = thread->stream(); |
| 6632 | if (stream != NULL) { |
| 6633 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6634 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6635 | } |
| 6636 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6637 | // release effect engine |
| 6638 | EffectRelease(mEffectInterface); |
| 6639 | } |
| 6640 | } |
| 6641 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 6642 | status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6643 | { |
| 6644 | status_t status; |
| 6645 | |
| 6646 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6647 | int priority = handle->priority(); |
| 6648 | size_t size = mHandles.size(); |
| 6649 | sp<EffectHandle> h; |
| 6650 | size_t i; |
| 6651 | for (i = 0; i < size; i++) { |
| 6652 | h = mHandles[i].promote(); |
| 6653 | if (h == 0) continue; |
| 6654 | if (h->priority() <= priority) break; |
| 6655 | } |
| 6656 | // if inserted in first place, move effect control from previous owner to this handle |
| 6657 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6658 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6659 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6660 | enabled = h->enabled(); |
| 6661 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6662 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6663 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6664 | status = NO_ERROR; |
| 6665 | } else { |
| 6666 | status = ALREADY_EXISTS; |
| 6667 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6668 | ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6669 | mHandles.insertAt(handle, i); |
| 6670 | return status; |
| 6671 | } |
| 6672 | |
| 6673 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 6674 | { |
| 6675 | Mutex::Autolock _l(mLock); |
| 6676 | size_t size = mHandles.size(); |
| 6677 | size_t i; |
| 6678 | for (i = 0; i < size; i++) { |
| 6679 | if (mHandles[i] == handle) break; |
| 6680 | } |
| 6681 | if (i == size) { |
| 6682 | return size; |
| 6683 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6684 | ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6685 | |
| 6686 | bool enabled = false; |
| 6687 | EffectHandle *hdl = handle.unsafe_get(); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 6688 | if (hdl != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6689 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6690 | enabled = hdl->enabled(); |
| 6691 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6692 | mHandles.removeAt(i); |
| 6693 | size = mHandles.size(); |
| 6694 | // if removed from first place, move effect control from this handle to next in line |
| 6695 | if (i == 0 && size != 0) { |
| 6696 | sp<EffectHandle> h = mHandles[0].promote(); |
| 6697 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6698 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6699 | } |
| 6700 | } |
| 6701 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6702 | // Prevent calls to process() and other functions on effect interface from now on. |
| 6703 | // The effect engine will be released by the destructor when the last strong reference on |
| 6704 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6705 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6706 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6707 | } |
| 6708 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6709 | return size; |
| 6710 | } |
| 6711 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6712 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 6713 | { |
| 6714 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 6715 | return mHandles.size() != 0 ? mHandles[0].promote() : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6716 | } |
| 6717 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 6718 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6719 | { |
Glenn Kasten | 90bebef | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 6720 | ALOGV("disconnect() %p handle %p", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6721 | // keep a strong reference on this EffectModule to avoid calling the |
| 6722 | // destructor before we exit |
| 6723 | sp<EffectModule> keep(this); |
| 6724 | { |
| 6725 | sp<ThreadBase> thread = mThread.promote(); |
| 6726 | if (thread != 0) { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 6727 | thread->disconnectEffect(keep, handle, unpinIfLast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6728 | } |
| 6729 | } |
| 6730 | } |
| 6731 | |
| 6732 | void AudioFlinger::EffectModule::updateState() { |
| 6733 | Mutex::Autolock _l(mLock); |
| 6734 | |
| 6735 | switch (mState) { |
| 6736 | case RESTART: |
| 6737 | reset_l(); |
| 6738 | // FALL THROUGH |
| 6739 | |
| 6740 | case STARTING: |
| 6741 | // clear auxiliary effect input buffer for next accumulation |
| 6742 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6743 | memset(mConfig.inputCfg.buffer.raw, |
| 6744 | 0, |
| 6745 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 6746 | } |
| 6747 | start_l(); |
| 6748 | mState = ACTIVE; |
| 6749 | break; |
| 6750 | case STOPPING: |
| 6751 | stop_l(); |
| 6752 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 6753 | mState = STOPPED; |
| 6754 | break; |
| 6755 | case STOPPED: |
| 6756 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 6757 | // turn off sequence. |
| 6758 | if (--mDisableWaitCnt == 0) { |
| 6759 | reset_l(); |
| 6760 | mState = IDLE; |
| 6761 | } |
| 6762 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6763 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6764 | break; |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | void AudioFlinger::EffectModule::process() |
| 6769 | { |
| 6770 | Mutex::Autolock _l(mLock); |
| 6771 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6772 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6773 | mConfig.inputCfg.buffer.raw == NULL || |
| 6774 | mConfig.outputCfg.buffer.raw == NULL) { |
| 6775 | return; |
| 6776 | } |
| 6777 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6778 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6779 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 6780 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6781 | ditherAndClamp(mConfig.inputCfg.buffer.s32, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6782 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6783 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6784 | } |
| 6785 | |
| 6786 | // do the actual processing in the effect engine |
| 6787 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 6788 | &mConfig.inputCfg.buffer, |
| 6789 | &mConfig.outputCfg.buffer); |
| 6790 | |
| 6791 | // force transition to IDLE state when engine is ready |
| 6792 | if (mState == STOPPED && ret == -ENODATA) { |
| 6793 | mDisableWaitCnt = 1; |
| 6794 | } |
| 6795 | |
| 6796 | // clear auxiliary effect input buffer for next accumulation |
| 6797 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6798 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 6799 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6800 | } |
| 6801 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6802 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6803 | // If an insert effect is idle and input buffer is different from output buffer, |
| 6804 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6805 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6806 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6807 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 6808 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 6809 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 6810 | for (size_t i = 0; i < frameCnt; i++) { |
| 6811 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6812 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6813 | } |
| 6814 | } |
| 6815 | } |
| 6816 | |
| 6817 | void AudioFlinger::EffectModule::reset_l() |
| 6818 | { |
| 6819 | if (mEffectInterface == NULL) { |
| 6820 | return; |
| 6821 | } |
| 6822 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 6823 | } |
| 6824 | |
| 6825 | status_t AudioFlinger::EffectModule::configure() |
| 6826 | { |
| 6827 | uint32_t channels; |
| 6828 | if (mEffectInterface == NULL) { |
| 6829 | return NO_INIT; |
| 6830 | } |
| 6831 | |
| 6832 | sp<ThreadBase> thread = mThread.promote(); |
| 6833 | if (thread == 0) { |
| 6834 | return DEAD_OBJECT; |
| 6835 | } |
| 6836 | |
| 6837 | // TODO: handle configuration of effects replacing track process |
| 6838 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6839 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6840 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6841 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6845 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6846 | } else { |
| 6847 | mConfig.inputCfg.channels = channels; |
| 6848 | } |
| 6849 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6850 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 6851 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6852 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 6853 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 6854 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 6855 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 6856 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 6857 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 6858 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 6859 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 6860 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 6861 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6862 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6863 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6864 | // - in other sessions: |
| 6865 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 6866 | // other effect: overwrites output buffer: input buffer == output buffer |
| 6867 | // Auxiliary effect: |
| 6868 | // accumulates in output buffer: input buffer != output buffer |
| 6869 | // Therefore: accumulate <=> input buffer != output buffer |
| 6870 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6871 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 6872 | } else { |
| 6873 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 6874 | } |
| 6875 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 6876 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 6877 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 6878 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 6879 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6880 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6881 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 6882 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6883 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6884 | uint32_t size = sizeof(int); |
| 6885 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 6886 | EFFECT_CMD_SET_CONFIG, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6887 | sizeof(effect_config_t), |
| 6888 | &mConfig, |
| 6889 | &size, |
| 6890 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6891 | if (status == 0) { |
| 6892 | status = cmdStatus; |
| 6893 | } |
| 6894 | |
| 6895 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 6896 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 6897 | |
| 6898 | return status; |
| 6899 | } |
| 6900 | |
| 6901 | status_t AudioFlinger::EffectModule::init() |
| 6902 | { |
| 6903 | Mutex::Autolock _l(mLock); |
| 6904 | if (mEffectInterface == NULL) { |
| 6905 | return NO_INIT; |
| 6906 | } |
| 6907 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6908 | uint32_t size = sizeof(status_t); |
| 6909 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6910 | EFFECT_CMD_INIT, |
| 6911 | 0, |
| 6912 | NULL, |
| 6913 | &size, |
| 6914 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6915 | if (status == 0) { |
| 6916 | status = cmdStatus; |
| 6917 | } |
| 6918 | return status; |
| 6919 | } |
| 6920 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6921 | status_t AudioFlinger::EffectModule::start() |
| 6922 | { |
| 6923 | Mutex::Autolock _l(mLock); |
| 6924 | return start_l(); |
| 6925 | } |
| 6926 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6927 | status_t AudioFlinger::EffectModule::start_l() |
| 6928 | { |
| 6929 | if (mEffectInterface == NULL) { |
| 6930 | return NO_INIT; |
| 6931 | } |
| 6932 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6933 | uint32_t size = sizeof(status_t); |
| 6934 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6935 | EFFECT_CMD_ENABLE, |
| 6936 | 0, |
| 6937 | NULL, |
| 6938 | &size, |
| 6939 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6940 | if (status == 0) { |
| 6941 | status = cmdStatus; |
| 6942 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6943 | if (status == 0 && |
| 6944 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6945 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6946 | sp<ThreadBase> thread = mThread.promote(); |
| 6947 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6948 | audio_stream_t *stream = thread->stream(); |
| 6949 | if (stream != NULL) { |
| 6950 | stream->add_audio_effect(stream, mEffectInterface); |
| 6951 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6952 | } |
| 6953 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6954 | return status; |
| 6955 | } |
| 6956 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6957 | status_t AudioFlinger::EffectModule::stop() |
| 6958 | { |
| 6959 | Mutex::Autolock _l(mLock); |
| 6960 | return stop_l(); |
| 6961 | } |
| 6962 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6963 | status_t AudioFlinger::EffectModule::stop_l() |
| 6964 | { |
| 6965 | if (mEffectInterface == NULL) { |
| 6966 | return NO_INIT; |
| 6967 | } |
| 6968 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6969 | uint32_t size = sizeof(status_t); |
| 6970 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6971 | EFFECT_CMD_DISABLE, |
| 6972 | 0, |
| 6973 | NULL, |
| 6974 | &size, |
| 6975 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6976 | if (status == 0) { |
| 6977 | status = cmdStatus; |
| 6978 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6979 | if (status == 0 && |
| 6980 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6981 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6982 | sp<ThreadBase> thread = mThread.promote(); |
| 6983 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6984 | audio_stream_t *stream = thread->stream(); |
| 6985 | if (stream != NULL) { |
| 6986 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6987 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6988 | } |
| 6989 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6990 | return status; |
| 6991 | } |
| 6992 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6993 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 6994 | uint32_t cmdSize, |
| 6995 | void *pCmdData, |
| 6996 | uint32_t *replySize, |
| 6997 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6998 | { |
| 6999 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7000 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7001 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7002 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7003 | return NO_INIT; |
| 7004 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7005 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 7006 | cmdCode, |
| 7007 | cmdSize, |
| 7008 | pCmdData, |
| 7009 | replySize, |
| 7010 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7011 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7012 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7013 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 7014 | sp<EffectHandle> h = mHandles[i].promote(); |
| 7015 | if (h != 0) { |
| 7016 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 7017 | } |
| 7018 | } |
| 7019 | } |
| 7020 | return status; |
| 7021 | } |
| 7022 | |
| 7023 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 7024 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7025 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7026 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7027 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7028 | |
| 7029 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7030 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 7031 | if (enabled && status != NO_ERROR) { |
| 7032 | return status; |
| 7033 | } |
| 7034 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7035 | switch (mState) { |
| 7036 | // going from disabled to enabled |
| 7037 | case IDLE: |
| 7038 | mState = STARTING; |
| 7039 | break; |
| 7040 | case STOPPED: |
| 7041 | mState = RESTART; |
| 7042 | break; |
| 7043 | case STOPPING: |
| 7044 | mState = ACTIVE; |
| 7045 | break; |
| 7046 | |
| 7047 | // going from enabled to disabled |
| 7048 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7049 | mState = STOPPED; |
| 7050 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7051 | case STARTING: |
| 7052 | mState = IDLE; |
| 7053 | break; |
| 7054 | case ACTIVE: |
| 7055 | mState = STOPPING; |
| 7056 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7057 | case DESTROYED: |
| 7058 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7059 | } |
| 7060 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 7061 | sp<EffectHandle> h = mHandles[i].promote(); |
| 7062 | if (h != 0) { |
| 7063 | h->setEnabled(enabled); |
| 7064 | } |
| 7065 | } |
| 7066 | } |
| 7067 | return NO_ERROR; |
| 7068 | } |
| 7069 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 7070 | bool AudioFlinger::EffectModule::isEnabled() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7071 | { |
| 7072 | switch (mState) { |
| 7073 | case RESTART: |
| 7074 | case STARTING: |
| 7075 | case ACTIVE: |
| 7076 | return true; |
| 7077 | case IDLE: |
| 7078 | case STOPPING: |
| 7079 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7080 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7081 | default: |
| 7082 | return false; |
| 7083 | } |
| 7084 | } |
| 7085 | |
Glenn Kasten | c59c004 | 2012-02-02 14:06:11 -0800 | [diff] [blame] | 7086 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7087 | { |
| 7088 | switch (mState) { |
| 7089 | case RESTART: |
| 7090 | case ACTIVE: |
| 7091 | case STOPPING: |
| 7092 | case STOPPED: |
| 7093 | return true; |
| 7094 | case IDLE: |
| 7095 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7096 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7097 | default: |
| 7098 | return false; |
| 7099 | } |
| 7100 | } |
| 7101 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7102 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 7103 | { |
| 7104 | Mutex::Autolock _l(mLock); |
| 7105 | status_t status = NO_ERROR; |
| 7106 | |
| 7107 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 7108 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7109 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7110 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 7111 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7112 | status_t cmdStatus; |
| 7113 | uint32_t volume[2]; |
| 7114 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7115 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7116 | volume[0] = *left; |
| 7117 | volume[1] = *right; |
| 7118 | if (controller) { |
| 7119 | pVolume = volume; |
| 7120 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7121 | status = (*mEffectInterface)->command(mEffectInterface, |
| 7122 | EFFECT_CMD_SET_VOLUME, |
| 7123 | size, |
| 7124 | volume, |
| 7125 | &size, |
| 7126 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7127 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 7128 | *left = volume[0]; |
| 7129 | *right = volume[1]; |
| 7130 | } |
| 7131 | } |
| 7132 | return status; |
| 7133 | } |
| 7134 | |
| 7135 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 7136 | { |
| 7137 | Mutex::Autolock _l(mLock); |
| 7138 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 7139 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 7140 | // audio pre processing modules on RecordThread can receive both output and |
| 7141 | // input device indication in the same call |
| 7142 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 7143 | if (dev) { |
| 7144 | status_t cmdStatus; |
| 7145 | uint32_t size = sizeof(status_t); |
| 7146 | |
| 7147 | status = (*mEffectInterface)->command(mEffectInterface, |
| 7148 | EFFECT_CMD_SET_DEVICE, |
| 7149 | sizeof(uint32_t), |
| 7150 | &dev, |
| 7151 | &size, |
| 7152 | &cmdStatus); |
| 7153 | if (status == NO_ERROR) { |
| 7154 | status = cmdStatus; |
| 7155 | } |
| 7156 | } |
| 7157 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 7158 | if (dev) { |
| 7159 | status_t cmdStatus; |
| 7160 | uint32_t size = sizeof(status_t); |
| 7161 | |
| 7162 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 7163 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 7164 | sizeof(uint32_t), |
| 7165 | &dev, |
| 7166 | &size, |
| 7167 | &cmdStatus); |
| 7168 | if (status2 == NO_ERROR) { |
| 7169 | status2 = cmdStatus; |
| 7170 | } |
| 7171 | if (status == NO_ERROR) { |
| 7172 | status = status2; |
| 7173 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7174 | } |
| 7175 | } |
| 7176 | return status; |
| 7177 | } |
| 7178 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7179 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7180 | { |
| 7181 | Mutex::Autolock _l(mLock); |
| 7182 | status_t status = NO_ERROR; |
| 7183 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7184 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7185 | uint32_t size = sizeof(status_t); |
| 7186 | status = (*mEffectInterface)->command(mEffectInterface, |
| 7187 | EFFECT_CMD_SET_AUDIO_MODE, |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7188 | sizeof(audio_mode_t), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7189 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7190 | &size, |
| 7191 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7192 | if (status == NO_ERROR) { |
| 7193 | status = cmdStatus; |
| 7194 | } |
| 7195 | } |
| 7196 | return status; |
| 7197 | } |
| 7198 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7199 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 7200 | { |
| 7201 | Mutex::Autolock _l(mLock); |
| 7202 | mSuspended = suspended; |
| 7203 | } |
Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 7204 | |
| 7205 | bool AudioFlinger::EffectModule::suspended() const |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7206 | { |
| 7207 | Mutex::Autolock _l(mLock); |
| 7208 | return mSuspended; |
| 7209 | } |
| 7210 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7211 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 7212 | { |
| 7213 | const size_t SIZE = 256; |
| 7214 | char buffer[SIZE]; |
| 7215 | String8 result; |
| 7216 | |
| 7217 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 7218 | result.append(buffer); |
| 7219 | |
| 7220 | bool locked = tryLock(mLock); |
| 7221 | // failed to lock - AudioFlinger is probably deadlocked |
| 7222 | if (!locked) { |
| 7223 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 7224 | } |
| 7225 | |
| 7226 | result.append("\t\tSession Status State Engine:\n"); |
| 7227 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 7228 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 7229 | result.append(buffer); |
| 7230 | |
| 7231 | result.append("\t\tDescriptor:\n"); |
| 7232 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 7233 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 7234 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 7235 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 7236 | result.append(buffer); |
| 7237 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 7238 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 7239 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 7240 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 7241 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 7242 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7243 | mDescriptor.apiVersion, |
| 7244 | mDescriptor.flags); |
| 7245 | result.append(buffer); |
| 7246 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 7247 | mDescriptor.name); |
| 7248 | result.append(buffer); |
| 7249 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 7250 | mDescriptor.implementor); |
| 7251 | result.append(buffer); |
| 7252 | |
| 7253 | result.append("\t\t- Input configuration:\n"); |
| 7254 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 7255 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 7256 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 7257 | mConfig.inputCfg.buffer.frameCount, |
| 7258 | mConfig.inputCfg.samplingRate, |
| 7259 | mConfig.inputCfg.channels, |
| 7260 | mConfig.inputCfg.format); |
| 7261 | result.append(buffer); |
| 7262 | |
| 7263 | result.append("\t\t- Output configuration:\n"); |
| 7264 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 7265 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 7266 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 7267 | mConfig.outputCfg.buffer.frameCount, |
| 7268 | mConfig.outputCfg.samplingRate, |
| 7269 | mConfig.outputCfg.channels, |
| 7270 | mConfig.outputCfg.format); |
| 7271 | result.append(buffer); |
| 7272 | |
| 7273 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 7274 | result.append(buffer); |
| 7275 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 7276 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 7277 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 7278 | if (handle != 0) { |
| 7279 | handle->dump(buffer, SIZE); |
| 7280 | result.append(buffer); |
| 7281 | } |
| 7282 | } |
| 7283 | |
| 7284 | result.append("\n"); |
| 7285 | |
| 7286 | write(fd, result.string(), result.length()); |
| 7287 | |
| 7288 | if (locked) { |
| 7289 | mLock.unlock(); |
| 7290 | } |
| 7291 | |
| 7292 | return NO_ERROR; |
| 7293 | } |
| 7294 | |
| 7295 | // ---------------------------------------------------------------------------- |
| 7296 | // EffectHandle implementation |
| 7297 | // ---------------------------------------------------------------------------- |
| 7298 | |
| 7299 | #undef LOG_TAG |
| 7300 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 7301 | |
| 7302 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 7303 | const sp<AudioFlinger::Client>& client, |
| 7304 | const sp<IEffectClient>& effectClient, |
| 7305 | int32_t priority) |
| 7306 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7307 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7308 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7309 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7310 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7311 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7312 | if (client == 0) { |
| 7313 | return; |
| 7314 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7315 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 7316 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 7317 | if (mCblkMemory != 0) { |
| 7318 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 7319 | |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7320 | if (mCblk != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7321 | new(mCblk) effect_param_cblk_t(); |
| 7322 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 7323 | } |
| 7324 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 7325 | ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7326 | return; |
| 7327 | } |
| 7328 | } |
| 7329 | |
| 7330 | AudioFlinger::EffectHandle::~EffectHandle() |
| 7331 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7332 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7333 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7334 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7335 | } |
| 7336 | |
| 7337 | status_t AudioFlinger::EffectHandle::enable() |
| 7338 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7339 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7340 | if (!mHasControl) return INVALID_OPERATION; |
| 7341 | if (mEffect == 0) return DEAD_OBJECT; |
| 7342 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7343 | if (mEnabled) { |
| 7344 | return NO_ERROR; |
| 7345 | } |
| 7346 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7347 | mEnabled = true; |
| 7348 | |
| 7349 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 7350 | if (thread != 0) { |
| 7351 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 7352 | } |
| 7353 | |
| 7354 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 7355 | if (mEffect->suspended()) { |
| 7356 | return NO_ERROR; |
| 7357 | } |
| 7358 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7359 | status_t status = mEffect->setEnabled(true); |
| 7360 | if (status != NO_ERROR) { |
| 7361 | if (thread != 0) { |
| 7362 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 7363 | } |
| 7364 | mEnabled = false; |
| 7365 | } |
| 7366 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7367 | } |
| 7368 | |
| 7369 | status_t AudioFlinger::EffectHandle::disable() |
| 7370 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7371 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7372 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7373 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7374 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7375 | if (!mEnabled) { |
| 7376 | return NO_ERROR; |
| 7377 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7378 | mEnabled = false; |
| 7379 | |
| 7380 | if (mEffect->suspended()) { |
| 7381 | return NO_ERROR; |
| 7382 | } |
| 7383 | |
| 7384 | status_t status = mEffect->setEnabled(false); |
| 7385 | |
| 7386 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 7387 | if (thread != 0) { |
| 7388 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 7389 | } |
| 7390 | |
| 7391 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7392 | } |
| 7393 | |
| 7394 | void AudioFlinger::EffectHandle::disconnect() |
| 7395 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7396 | disconnect(true); |
| 7397 | } |
| 7398 | |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7399 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7400 | { |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7401 | ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7402 | if (mEffect == 0) { |
| 7403 | return; |
| 7404 | } |
Glenn Kasten | 58123c3 | 2012-02-03 10:32:24 -0800 | [diff] [blame] | 7405 | mEffect->disconnect(this, unpinIfLast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7406 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 7407 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7408 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 7409 | if (thread != 0) { |
| 7410 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 7411 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7412 | } |
| 7413 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7414 | // release sp on module => module destructor can be called now |
| 7415 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7416 | if (mClient != 0) { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7417 | if (mCblk != NULL) { |
Glenn Kasten | 1a0ae5b | 2012-02-03 10:24:48 -0800 | [diff] [blame] | 7418 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7419 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 7420 | } |
Glenn Kasten | dbfafaf | 2012-01-25 15:27:15 -0800 | [diff] [blame] | 7421 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 7422 | // Client destructor must run with AudioFlinger mutex locked |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7423 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 7424 | mClient.clear(); |
| 7425 | } |
| 7426 | } |
| 7427 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7428 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 7429 | uint32_t cmdSize, |
| 7430 | void *pCmdData, |
| 7431 | uint32_t *replySize, |
| 7432 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7433 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7434 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7435 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7436 | |
| 7437 | // only get parameter command is permitted for applications not controlling the effect |
| 7438 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 7439 | return INVALID_OPERATION; |
| 7440 | } |
| 7441 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7442 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7443 | |
| 7444 | // handle commands that are not forwarded transparently to effect engine |
| 7445 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 7446 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 7447 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 7448 | Mutex::Autolock _l(mCblk->lock); |
| 7449 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 7450 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 7451 | mCblk->serverIndex = 0; |
| 7452 | mCblk->clientIndex = 0; |
| 7453 | return BAD_VALUE; |
| 7454 | } |
| 7455 | status_t status = NO_ERROR; |
| 7456 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 7457 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7458 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7459 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 7460 | int size = *p++; |
| 7461 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7462 | ALOGW("command(): invalid parameter block size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7463 | break; |
| 7464 | } |
| 7465 | effect_param_t *param = (effect_param_t *)p; |
| 7466 | if (param->psize == 0 || param->vsize == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7467 | ALOGW("command(): null parameter or value size"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7468 | mCblk->serverIndex += size; |
| 7469 | continue; |
| 7470 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7471 | uint32_t psize = sizeof(effect_param_t) + |
| 7472 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 7473 | param->vsize; |
| 7474 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 7475 | psize, |
| 7476 | p, |
| 7477 | &rsize, |
| 7478 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7479 | // stop at first error encountered |
| 7480 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7481 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7482 | *(int *)pReplyData = reply; |
| 7483 | break; |
| 7484 | } else if (reply != NO_ERROR) { |
| 7485 | *(int *)pReplyData = reply; |
| 7486 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7487 | } |
| 7488 | mCblk->serverIndex += size; |
| 7489 | } |
| 7490 | mCblk->serverIndex = 0; |
| 7491 | mCblk->clientIndex = 0; |
| 7492 | return status; |
| 7493 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7494 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7495 | return enable(); |
| 7496 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7497 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7498 | return disable(); |
| 7499 | } |
| 7500 | |
| 7501 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7502 | } |
| 7503 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7504 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7505 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7506 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7507 | |
| 7508 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7509 | mEnabled = enabled; |
| 7510 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7511 | if (signal && mEffectClient != 0) { |
| 7512 | mEffectClient->controlStatusChanged(hasControl); |
| 7513 | } |
| 7514 | } |
| 7515 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7516 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 7517 | uint32_t cmdSize, |
| 7518 | void *pCmdData, |
| 7519 | uint32_t replySize, |
| 7520 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7521 | { |
| 7522 | if (mEffectClient != 0) { |
| 7523 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7524 | } |
| 7525 | } |
| 7526 | |
| 7527 | |
| 7528 | |
| 7529 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 7530 | { |
| 7531 | if (mEffectClient != 0) { |
| 7532 | mEffectClient->enableStatusChanged(enabled); |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | status_t AudioFlinger::EffectHandle::onTransact( |
| 7537 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7538 | { |
| 7539 | return BnEffect::onTransact(code, data, reply, flags); |
| 7540 | } |
| 7541 | |
| 7542 | |
| 7543 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 7544 | { |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 7545 | bool locked = mCblk != NULL && tryLock(mCblk->lock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7546 | |
| 7547 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 7548 | (mClient == 0) ? getpid_cached : mClient->pid(), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7549 | mPriority, |
| 7550 | mHasControl, |
| 7551 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7552 | mCblk ? mCblk->clientIndex : 0, |
| 7553 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7554 | ); |
| 7555 | |
| 7556 | if (locked) { |
| 7557 | mCblk->lock.unlock(); |
| 7558 | } |
| 7559 | } |
| 7560 | |
| 7561 | #undef LOG_TAG |
| 7562 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 7563 | |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7564 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7565 | int sessionId) |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7566 | : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7567 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 7568 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7569 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7570 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Glenn Kasten | 9eaa557 | 2012-01-20 13:32:16 -0800 | [diff] [blame] | 7571 | if (thread == NULL) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7572 | return; |
| 7573 | } |
| 7574 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 7575 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7576 | } |
| 7577 | |
| 7578 | AudioFlinger::EffectChain::~EffectChain() |
| 7579 | { |
| 7580 | if (mOwnInBuffer) { |
| 7581 | delete mInBuffer; |
| 7582 | } |
| 7583 | |
| 7584 | } |
| 7585 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7586 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7587 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7588 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7589 | size_t size = mEffects.size(); |
| 7590 | |
| 7591 | for (size_t i = 0; i < size; i++) { |
| 7592 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7593 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7594 | } |
| 7595 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7596 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7597 | } |
| 7598 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7599 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7600 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7601 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7602 | size_t size = mEffects.size(); |
| 7603 | |
| 7604 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7605 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 7606 | if (id == 0 || mEffects[i]->id() == id) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7607 | return mEffects[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7608 | } |
| 7609 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7610 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7611 | } |
| 7612 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7613 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 7614 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 7615 | const effect_uuid_t *type) |
| 7616 | { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7617 | size_t size = mEffects.size(); |
| 7618 | |
| 7619 | for (size_t i = 0; i < size; i++) { |
| 7620 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7621 | return mEffects[i]; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7622 | } |
| 7623 | } |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 7624 | return 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7625 | } |
| 7626 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7627 | // Must be called with EffectChain::mLock locked |
| 7628 | void AudioFlinger::EffectChain::process_l() |
| 7629 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7630 | sp<ThreadBase> thread = mThread.promote(); |
| 7631 | if (thread == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7632 | ALOGW("process_l(): cannot promote mixer thread"); |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7633 | return; |
| 7634 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7635 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 7636 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7637 | // always process effects unless no more tracks are on the session and the effect tail |
| 7638 | // has been rendered |
| 7639 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7640 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7641 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7642 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7643 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 7644 | doProcess = false; |
| 7645 | } |
| 7646 | |
| 7647 | if (activeTrackCnt() == 0) { |
| 7648 | // if no track is active and the effect tail has not been rendered, |
| 7649 | // the input buffer must be cleared here as the mixer process will not do it |
| 7650 | if (tracksOnSession || mTailBufferCount > 0) { |
| 7651 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 7652 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 7653 | if (mTailBufferCount > 0) { |
| 7654 | mTailBufferCount--; |
| 7655 | } |
| 7656 | } |
| 7657 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7658 | } |
| 7659 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7660 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7661 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7662 | for (size_t i = 0; i < size; i++) { |
| 7663 | mEffects[i]->process(); |
| 7664 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7665 | } |
| 7666 | for (size_t i = 0; i < size; i++) { |
| 7667 | mEffects[i]->updateState(); |
| 7668 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7669 | } |
| 7670 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7671 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7672 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7673 | { |
| 7674 | effect_descriptor_t desc = effect->desc(); |
| 7675 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 7676 | |
| 7677 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7678 | effect->setChain(this); |
| 7679 | sp<ThreadBase> thread = mThread.promote(); |
| 7680 | if (thread == 0) { |
| 7681 | return NO_INIT; |
| 7682 | } |
| 7683 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7684 | |
| 7685 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7686 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 7687 | // they are processed first and accumulated in chain input buffer |
| 7688 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7689 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7690 | // the input buffer for auxiliary effect contains mono samples in |
| 7691 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 7692 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 7693 | // calling the process in effect engine |
| 7694 | size_t numSamples = thread->frameCount(); |
| 7695 | int32_t *buffer = new int32_t[numSamples]; |
| 7696 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 7697 | effect->setInBuffer((int16_t *)buffer); |
| 7698 | // auxiliary effects output samples to chain input buffer for further processing |
| 7699 | // by insert effects |
| 7700 | effect->setOutBuffer(mInBuffer); |
| 7701 | } else { |
| 7702 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 7703 | // after track and auxiliary effects. |
| 7704 | // Insert effect order as a function of indicated preference: |
| 7705 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 7706 | // another effect is present |
| 7707 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 7708 | // last effect claiming first position |
| 7709 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 7710 | // first effect claiming last position |
| 7711 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 7712 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 7713 | // already present |
| 7714 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7715 | size_t size = mEffects.size(); |
| 7716 | size_t idx_insert = size; |
| 7717 | ssize_t idx_insert_first = -1; |
| 7718 | ssize_t idx_insert_last = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7719 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7720 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7721 | effect_descriptor_t d = mEffects[i]->desc(); |
| 7722 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 7723 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 7724 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 7725 | // check invalid effect chaining combinations |
| 7726 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 7727 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7728 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7729 | return INVALID_OPERATION; |
| 7730 | } |
| 7731 | // remember position of first insert effect and by default |
| 7732 | // select this as insert position for new effect |
| 7733 | if (idx_insert == size) { |
| 7734 | idx_insert = i; |
| 7735 | } |
| 7736 | // remember position of last insert effect claiming |
| 7737 | // first position |
| 7738 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 7739 | idx_insert_first = i; |
| 7740 | } |
| 7741 | // remember position of first insert effect claiming |
| 7742 | // last position |
| 7743 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 7744 | idx_insert_last == -1) { |
| 7745 | idx_insert_last = i; |
| 7746 | } |
| 7747 | } |
| 7748 | } |
| 7749 | |
| 7750 | // modify idx_insert from first position if needed |
| 7751 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 7752 | if (idx_insert_last != -1) { |
| 7753 | idx_insert = idx_insert_last; |
| 7754 | } else { |
| 7755 | idx_insert = size; |
| 7756 | } |
| 7757 | } else { |
| 7758 | if (idx_insert_first != -1) { |
| 7759 | idx_insert = idx_insert_first + 1; |
| 7760 | } |
| 7761 | } |
| 7762 | |
| 7763 | // always read samples from chain input buffer |
| 7764 | effect->setInBuffer(mInBuffer); |
| 7765 | |
| 7766 | // if last effect in the chain, output samples to chain |
| 7767 | // output buffer, otherwise to chain input buffer |
| 7768 | if (idx_insert == size) { |
| 7769 | if (idx_insert != 0) { |
| 7770 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 7771 | mEffects[idx_insert-1]->configure(); |
| 7772 | } |
| 7773 | effect->setOutBuffer(mOutBuffer); |
| 7774 | } else { |
| 7775 | effect->setOutBuffer(mInBuffer); |
| 7776 | } |
| 7777 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7778 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7779 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7780 | } |
| 7781 | effect->configure(); |
| 7782 | return NO_ERROR; |
| 7783 | } |
| 7784 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7785 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 7786 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7787 | { |
| 7788 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7789 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7790 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 7791 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7792 | for (size_t i = 0; i < size; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7793 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7794 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 7795 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 7796 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7797 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 7798 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 7799 | mEffects[i]->stop(); |
| 7800 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7801 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7802 | delete[] effect->inBuffer(); |
| 7803 | } else { |
| 7804 | if (i == size - 1 && i != 0) { |
| 7805 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 7806 | mEffects[i - 1]->configure(); |
| 7807 | } |
| 7808 | } |
| 7809 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7810 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7811 | break; |
| 7812 | } |
| 7813 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7814 | |
| 7815 | return mEffects.size(); |
| 7816 | } |
| 7817 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7818 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 7819 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7820 | { |
| 7821 | size_t size = mEffects.size(); |
| 7822 | for (size_t i = 0; i < size; i++) { |
| 7823 | mEffects[i]->setDevice(device); |
| 7824 | } |
| 7825 | } |
| 7826 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7827 | // setMode_l() must be called with PlaybackThread::mLock held |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 7828 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7829 | { |
| 7830 | size_t size = mEffects.size(); |
| 7831 | for (size_t i = 0; i < size; i++) { |
| 7832 | mEffects[i]->setMode(mode); |
| 7833 | } |
| 7834 | } |
| 7835 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7836 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 7837 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7838 | { |
| 7839 | uint32_t newLeft = *left; |
| 7840 | uint32_t newRight = *right; |
| 7841 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7842 | int ctrlIdx = -1; |
| 7843 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7844 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7845 | // first update volume controller |
| 7846 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7847 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7848 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 7849 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7850 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7851 | break; |
| 7852 | } |
| 7853 | } |
| 7854 | |
| 7855 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7856 | if (hasControl) { |
| 7857 | *left = mNewLeftVolume; |
| 7858 | *right = mNewRightVolume; |
| 7859 | } |
| 7860 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7861 | } |
| 7862 | |
| 7863 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7864 | mLeftVolume = newLeft; |
| 7865 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7866 | |
| 7867 | // second get volume update from volume controller |
| 7868 | if (ctrlIdx >= 0) { |
| 7869 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7870 | mNewLeftVolume = newLeft; |
| 7871 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7872 | } |
| 7873 | // then indicate volume to all other effects in chain. |
| 7874 | // Pass altered volume to effects before volume controller |
| 7875 | // and requested volume to effects after controller |
| 7876 | uint32_t lVol = newLeft; |
| 7877 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7878 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7879 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7880 | if ((int)i == ctrlIdx) continue; |
| 7881 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 7882 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7883 | lVol = *left; |
| 7884 | rVol = *right; |
| 7885 | } |
| 7886 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 7887 | } |
| 7888 | *left = newLeft; |
| 7889 | *right = newRight; |
| 7890 | |
| 7891 | return hasControl; |
| 7892 | } |
| 7893 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7894 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 7895 | { |
| 7896 | const size_t SIZE = 256; |
| 7897 | char buffer[SIZE]; |
| 7898 | String8 result; |
| 7899 | |
| 7900 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 7901 | result.append(buffer); |
| 7902 | |
| 7903 | bool locked = tryLock(mLock); |
| 7904 | // failed to lock - AudioFlinger is probably deadlocked |
| 7905 | if (!locked) { |
| 7906 | result.append("\tCould not lock mutex:\n"); |
| 7907 | } |
| 7908 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7909 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 7910 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7911 | mEffects.size(), |
| 7912 | (uint32_t)mInBuffer, |
| 7913 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7914 | mActiveTrackCnt); |
| 7915 | result.append(buffer); |
| 7916 | write(fd, result.string(), result.size()); |
| 7917 | |
| 7918 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 7919 | sp<EffectModule> effect = mEffects[i]; |
| 7920 | if (effect != 0) { |
| 7921 | effect->dump(fd, args); |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | if (locked) { |
| 7926 | mLock.unlock(); |
| 7927 | } |
| 7928 | |
| 7929 | return NO_ERROR; |
| 7930 | } |
| 7931 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7932 | // must be called with ThreadBase::mLock held |
| 7933 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 7934 | const effect_uuid_t *type, bool suspend) |
| 7935 | { |
| 7936 | sp<SuspendedEffectDesc> desc; |
| 7937 | // use effect type UUID timelow as key as there is no real risk of identical |
| 7938 | // timeLow fields among effect type UUIDs. |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7939 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7940 | if (suspend) { |
| 7941 | if (index >= 0) { |
| 7942 | desc = mSuspendedEffects.valueAt(index); |
| 7943 | } else { |
| 7944 | desc = new SuspendedEffectDesc(); |
| 7945 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 7946 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7947 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7948 | } |
| 7949 | if (desc->mRefCount++ == 0) { |
| 7950 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 7951 | if (effect != 0) { |
| 7952 | desc->mEffect = effect; |
| 7953 | effect->setSuspended(true); |
| 7954 | effect->setEnabled(false); |
| 7955 | } |
| 7956 | } |
| 7957 | } else { |
| 7958 | if (index < 0) { |
| 7959 | return; |
| 7960 | } |
| 7961 | desc = mSuspendedEffects.valueAt(index); |
| 7962 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7963 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7964 | desc->mRefCount = 1; |
| 7965 | } |
| 7966 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7967 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7968 | if (desc->mEffect != 0) { |
| 7969 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 7970 | if (effect != 0) { |
| 7971 | effect->setSuspended(false); |
| 7972 | sp<EffectHandle> handle = effect->controlHandle(); |
| 7973 | if (handle != 0) { |
| 7974 | effect->setEnabled(handle->enabled()); |
| 7975 | } |
| 7976 | } |
| 7977 | desc->mEffect.clear(); |
| 7978 | } |
| 7979 | mSuspendedEffects.removeItemsAt(index); |
| 7980 | } |
| 7981 | } |
| 7982 | } |
| 7983 | |
| 7984 | // must be called with ThreadBase::mLock held |
| 7985 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 7986 | { |
| 7987 | sp<SuspendedEffectDesc> desc; |
| 7988 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 7989 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7990 | if (suspend) { |
| 7991 | if (index >= 0) { |
| 7992 | desc = mSuspendedEffects.valueAt(index); |
| 7993 | } else { |
| 7994 | desc = new SuspendedEffectDesc(); |
| 7995 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7996 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7997 | } |
| 7998 | if (desc->mRefCount++ == 0) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 7999 | Vector< sp<EffectModule> > effects; |
| 8000 | getSuspendEligibleEffects(effects); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8001 | for (size_t i = 0; i < effects.size(); i++) { |
| 8002 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 8003 | } |
| 8004 | } |
| 8005 | } else { |
| 8006 | if (index < 0) { |
| 8007 | return; |
| 8008 | } |
| 8009 | desc = mSuspendedEffects.valueAt(index); |
| 8010 | if (desc->mRefCount <= 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8011 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8012 | desc->mRefCount = 1; |
| 8013 | } |
| 8014 | if (--desc->mRefCount == 0) { |
| 8015 | Vector<const effect_uuid_t *> types; |
| 8016 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 8017 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 8018 | continue; |
| 8019 | } |
| 8020 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 8021 | } |
| 8022 | for (size_t i = 0; i < types.size(); i++) { |
| 8023 | setEffectSuspended_l(types[i], false); |
| 8024 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8025 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8026 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 8027 | } |
| 8028 | } |
| 8029 | } |
| 8030 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 8031 | |
| 8032 | // The volume effect is used for automated tests only |
| 8033 | #ifndef OPENSL_ES_H_ |
| 8034 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 8035 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 8036 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 8037 | #endif //OPENSL_ES_H_ |
| 8038 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8039 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 8040 | { |
| 8041 | // auxiliary effects and visualizer are never suspended on output mix |
| 8042 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 8043 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 8044 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 8045 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8046 | return false; |
| 8047 | } |
| 8048 | return true; |
| 8049 | } |
| 8050 | |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 8051 | void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects) |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8052 | { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 8053 | effects.clear(); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8054 | for (size_t i = 0; i < mEffects.size(); i++) { |
Glenn Kasten | d053971 | 2012-01-30 12:56:03 -0800 | [diff] [blame] | 8055 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 8056 | effects.add(mEffects[i]); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8057 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8058 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 8062 | const effect_uuid_t *type) |
| 8063 | { |
Glenn Kasten | 090f019 | 2012-01-30 13:00:02 -0800 | [diff] [blame] | 8064 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 8065 | return effect != 0 && effect->isEnabled() ? effect : 0; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8066 | } |
| 8067 | |
| 8068 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 8069 | bool enabled) |
| 8070 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 8071 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8072 | if (enabled) { |
| 8073 | if (index < 0) { |
| 8074 | // if the effect is not suspend check if all effects are suspended |
| 8075 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 8076 | if (index < 0) { |
| 8077 | return; |
| 8078 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8079 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 8080 | return; |
| 8081 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8082 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 8083 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8084 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 8085 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 8086 | return; |
| 8087 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8088 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8089 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8090 | effect->desc().type.timeLow); |
| 8091 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 8092 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 8093 | if (desc->mEffect == 0) { |
| 8094 | desc->mEffect = effect; |
| 8095 | effect->setEnabled(false); |
| 8096 | effect->setSuspended(true); |
| 8097 | } |
| 8098 | } else { |
| 8099 | if (index < 0) { |
| 8100 | return; |
| 8101 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 8102 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 8103 | effect->desc().type.timeLow); |
| 8104 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 8105 | desc->mEffect.clear(); |
| 8106 | effect->setSuspended(false); |
| 8107 | } |
| 8108 | } |
| 8109 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8110 | #undef LOG_TAG |
| 8111 | #define LOG_TAG "AudioFlinger" |
| 8112 | |
| 8113 | // ---------------------------------------------------------------------------- |
| 8114 | |
| 8115 | status_t AudioFlinger::onTransact( |
| 8116 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 8117 | { |
| 8118 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 8119 | } |
| 8120 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 8121 | }; // namespace android |