| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* //device/include/server/AudioFlinger/AudioFlinger.cpp | 
|  | 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 |  | 
|  | 40 | #include <media/AudioTrack.h> | 
|  | 41 | #include <media/AudioRecord.h> | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 42 | #include <media/IMediaPlayerService.h> | 
| Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 43 | #include <media/IMediaDeathNotifier.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | #include <private/media/AudioTrackShared.h> | 
|  | 46 | #include <private/media/AudioEffectShared.h> | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 47 |  | 
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 48 | #include <system/audio.h> | 
| Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 49 | #include <hardware/audio.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | #include "AudioMixer.h" | 
|  | 52 | #include "AudioFlinger.h" | 
|  | 53 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | #include <media/EffectsFactoryApi.h> | 
| Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 55 | #include <audio_effects/effect_visualizer.h> | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 56 | #include <audio_effects/effect_ns.h> | 
|  | 57 | #include <audio_effects/effect_aec.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 |  | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 59 | #include <audio_utils/primitives.h> | 
|  | 60 |  | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 61 | #include <cpustats/ThreadCpuUsage.h> | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 62 | #include <powermanager/PowerManager.h> | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 63 | // #define DEBUG_CPU_USAGE 10  // log statistics every n wall clock seconds | 
|  | 64 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 65 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 67 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | namespace android { | 
|  | 69 |  | 
| Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 70 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; | 
|  | 71 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); | 
|  | 74 | static const float MAX_GAIN = 4096.0f; | 
|  | 75 | static const float MAX_GAIN_INT = 0x1000; | 
|  | 76 |  | 
|  | 77 | // retry counts for buffer fill timeout | 
|  | 78 | // 50 * ~20msecs = 1 second | 
|  | 79 | static const int8_t kMaxTrackRetries = 50; | 
|  | 80 | static const int8_t kMaxTrackStartupRetries = 50; | 
|  | 81 | // allow less retry attempts on direct output thread. | 
|  | 82 | // direct outputs can be a scarce resource in audio hardware and should | 
|  | 83 | // be released as quickly as possible. | 
|  | 84 | static const int8_t kMaxTrackRetriesDirect = 2; | 
|  | 85 |  | 
|  | 86 | static const int kDumpLockRetries = 50; | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 87 | static const int kDumpLockSleepUs = 20000; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 88 |  | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 89 | // don't warn about blocked writes or record buffer overflows more often than this | 
|  | 90 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 92 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
|  | 93 | static const int kRecordThreadSleepUs = 5000; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 94 |  | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 95 | // maximum time to wait for setParameters to complete | 
|  | 96 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 97 |  | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 98 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
|  | 99 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
|  | 100 | // maximum divider applied to the active sleep time in the mixer thread loop | 
|  | 101 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
|  | 102 |  | 
|  | 103 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | // ---------------------------------------------------------------------------- | 
|  | 105 |  | 
|  | 106 | static bool recordingAllowed() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 107 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; | 
|  | 108 | bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO")); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 109 | if (!ok) ALOGE("Request requires android.permission.RECORD_AUDIO"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | return ok; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | static bool settingsAllowed() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; | 
|  | 115 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 116 | if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 117 | return ok; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 120 | // To collect the amplifier usage | 
|  | 121 | static void addBatteryData(uint32_t params) { | 
| Glenn Kasten | 25b248e | 2012-01-03 15:28:29 -0800 | [diff] [blame] | 122 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
|  | 123 | if (service == NULL) { | 
|  | 124 | // it already logged | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 125 | return; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | service->addBatteryData(params); | 
|  | 129 | } | 
|  | 130 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 131 | static int load_audio_interface(const char *if_name, const hw_module_t **mod, | 
|  | 132 | audio_hw_device_t **dev) | 
|  | 133 | { | 
|  | 134 | int rc; | 
|  | 135 |  | 
|  | 136 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod); | 
|  | 137 | if (rc) | 
|  | 138 | goto out; | 
|  | 139 |  | 
|  | 140 | rc = audio_hw_device_open(*mod, dev); | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 141 | 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] | 142 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); | 
|  | 143 | if (rc) | 
|  | 144 | goto out; | 
|  | 145 |  | 
|  | 146 | return 0; | 
|  | 147 |  | 
|  | 148 | out: | 
|  | 149 | *mod = NULL; | 
|  | 150 | *dev = NULL; | 
|  | 151 | return rc; | 
|  | 152 | } | 
|  | 153 |  | 
| Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 154 | static const char * const audio_interfaces[] = { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 155 | "primary", | 
|  | 156 | "a2dp", | 
|  | 157 | "usb", | 
|  | 158 | }; | 
|  | 159 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) | 
|  | 160 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 161 | // ---------------------------------------------------------------------------- | 
|  | 162 |  | 
|  | 163 | AudioFlinger::AudioFlinger() | 
|  | 164 | : BnAudioFlinger(), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 165 | mPrimaryHardwareDev(NULL), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1), | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 166 | mBtNrecIsOff(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 167 | { | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
|  | 170 | void AudioFlinger::onFirstRef() | 
|  | 171 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 172 | int rc = 0; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 173 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 174 | Mutex::Autolock _l(mLock); | 
|  | 175 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 176 | /* TODO: move all this work into an Init() function */ | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 177 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 178 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 179 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { | 
|  | 180 | const hw_module_t *mod; | 
|  | 181 | audio_hw_device_t *dev; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 182 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 183 | rc = load_audio_interface(audio_interfaces[i], &mod, &dev); | 
|  | 184 | if (rc) | 
|  | 185 | continue; | 
|  | 186 |  | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 187 | ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i], | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 188 | mod->name, mod->id); | 
|  | 189 | mAudioHwDevs.push(dev); | 
|  | 190 |  | 
|  | 191 | if (!mPrimaryHardwareDev) { | 
|  | 192 | mPrimaryHardwareDev = dev; | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 193 | ALOGI("Using '%s' (%s.%s) as the primary audio interface", | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 194 | mod->name, mod->id, audio_interfaces[i]); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 197 |  | 
|  | 198 | mHardwareStatus = AUDIO_HW_INIT; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 199 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 200 | if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 201 | ALOGE("Primary audio interface not found"); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 202 | return; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 206 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 207 |  | 
|  | 208 | mHardwareStatus = AUDIO_HW_INIT; | 
|  | 209 | rc = dev->init_check(dev); | 
|  | 210 | if (rc == 0) { | 
|  | 211 | AutoMutex lock(mHardwareLock); | 
|  | 212 |  | 
|  | 213 | mMode = AUDIO_MODE_NORMAL; | 
|  | 214 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
|  | 215 | dev->set_mode(dev, mMode); | 
|  | 216 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
|  | 217 | dev->set_master_volume(dev, 1.0f); | 
|  | 218 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 219 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 220 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 223 | status_t AudioFlinger::initCheck() const | 
|  | 224 | { | 
|  | 225 | Mutex::Autolock _l(mLock); | 
|  | 226 | if (mPrimaryHardwareDev == NULL || mAudioHwDevs.size() == 0) | 
|  | 227 | return NO_INIT; | 
|  | 228 | return NO_ERROR; | 
|  | 229 | } | 
|  | 230 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 231 | AudioFlinger::~AudioFlinger() | 
|  | 232 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 233 | int num_devs = mAudioHwDevs.size(); | 
|  | 234 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 235 | while (!mRecordThreads.isEmpty()) { | 
|  | 236 | // closeInput() will remove first entry from mRecordThreads | 
|  | 237 | closeInput(mRecordThreads.keyAt(0)); | 
|  | 238 | } | 
|  | 239 | while (!mPlaybackThreads.isEmpty()) { | 
|  | 240 | // closeOutput() will remove first entry from mPlaybackThreads | 
|  | 241 | closeOutput(mPlaybackThreads.keyAt(0)); | 
|  | 242 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | for (int i = 0; i < num_devs; i++) { | 
|  | 245 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 246 | audio_hw_device_close(dev); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 247 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 248 | mAudioHwDevs.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 251 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices) | 
|  | 252 | { | 
|  | 253 | /* first matching HW device is returned */ | 
|  | 254 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 255 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 256 | if ((dev->get_supported_devices(dev) & devices) == devices) | 
|  | 257 | return dev; | 
|  | 258 | } | 
|  | 259 | return NULL; | 
|  | 260 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 261 |  | 
|  | 262 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) | 
|  | 263 | { | 
|  | 264 | const size_t SIZE = 256; | 
|  | 265 | char buffer[SIZE]; | 
|  | 266 | String8 result; | 
|  | 267 |  | 
|  | 268 | result.append("Clients:\n"); | 
|  | 269 | for (size_t i = 0; i < mClients.size(); ++i) { | 
|  | 270 | wp<Client> wClient = mClients.valueAt(i); | 
|  | 271 | if (wClient != 0) { | 
|  | 272 | sp<Client> client = wClient.promote(); | 
|  | 273 | if (client != 0) { | 
|  | 274 | snprintf(buffer, SIZE, "  pid: %d\n", client->pid()); | 
|  | 275 | result.append(buffer); | 
|  | 276 | } | 
|  | 277 | } | 
|  | 278 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | result.append("Global session refs:\n"); | 
|  | 281 | result.append(" session pid cnt\n"); | 
|  | 282 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { | 
|  | 283 | AudioSessionRef *r = mAudioSessionRefs[i]; | 
|  | 284 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt); | 
|  | 285 | result.append(buffer); | 
|  | 286 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 287 | write(fd, result.string(), result.size()); | 
|  | 288 | return NO_ERROR; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 |  | 
|  | 292 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 293 | { | 
|  | 294 | const size_t SIZE = 256; | 
|  | 295 | char buffer[SIZE]; | 
|  | 296 | String8 result; | 
| Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 297 | hardware_call_state hardwareStatus = mHardwareStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 298 |  | 
|  | 299 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); | 
|  | 300 | result.append(buffer); | 
|  | 301 | write(fd, result.string(), result.size()); | 
|  | 302 | return NO_ERROR; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) | 
|  | 306 | { | 
|  | 307 | const size_t SIZE = 256; | 
|  | 308 | char buffer[SIZE]; | 
|  | 309 | String8 result; | 
|  | 310 | snprintf(buffer, SIZE, "Permission Denial: " | 
|  | 311 | "can't dump AudioFlinger from pid=%d, uid=%d\n", | 
|  | 312 | IPCThreadState::self()->getCallingPid(), | 
|  | 313 | IPCThreadState::self()->getCallingUid()); | 
|  | 314 | result.append(buffer); | 
|  | 315 | write(fd, result.string(), result.size()); | 
|  | 316 | return NO_ERROR; | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | static bool tryLock(Mutex& mutex) | 
|  | 320 | { | 
|  | 321 | bool locked = false; | 
|  | 322 | for (int i = 0; i < kDumpLockRetries; ++i) { | 
|  | 323 | if (mutex.tryLock() == NO_ERROR) { | 
|  | 324 | locked = true; | 
|  | 325 | break; | 
|  | 326 | } | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 327 | usleep(kDumpLockSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 328 | } | 
|  | 329 | return locked; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) | 
|  | 333 | { | 
|  | 334 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { | 
|  | 335 | dumpPermissionDenial(fd, args); | 
|  | 336 | } else { | 
|  | 337 | // get state of hardware lock | 
|  | 338 | bool hardwareLocked = tryLock(mHardwareLock); | 
|  | 339 | if (!hardwareLocked) { | 
|  | 340 | String8 result(kHardwareLockedString); | 
|  | 341 | write(fd, result.string(), result.size()); | 
|  | 342 | } else { | 
|  | 343 | mHardwareLock.unlock(); | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | bool locked = tryLock(mLock); | 
|  | 347 |  | 
|  | 348 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 349 | if (!locked) { | 
|  | 350 | String8 result(kDeadlockedString); | 
|  | 351 | write(fd, result.string(), result.size()); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | dumpClients(fd, args); | 
|  | 355 | dumpInternals(fd, args); | 
|  | 356 |  | 
|  | 357 | // dump playback threads | 
|  | 358 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 359 | mPlaybackThreads.valueAt(i)->dump(fd, args); | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | // dump record threads | 
|  | 363 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 364 | mRecordThreads.valueAt(i)->dump(fd, args); | 
|  | 365 | } | 
|  | 366 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 367 | // dump all hardware devs | 
|  | 368 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 369 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 370 | dev->dump(dev, fd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 371 | } | 
|  | 372 | if (locked) mLock.unlock(); | 
|  | 373 | } | 
|  | 374 | return NO_ERROR; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 |  | 
|  | 378 | // IAudioFlinger interface | 
|  | 379 |  | 
|  | 380 |  | 
|  | 381 | sp<IAudioTrack> AudioFlinger::createTrack( | 
|  | 382 | pid_t pid, | 
|  | 383 | int streamType, | 
|  | 384 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 385 | uint32_t format, | 
|  | 386 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 387 | int frameCount, | 
|  | 388 | uint32_t flags, | 
|  | 389 | const sp<IMemory>& sharedBuffer, | 
|  | 390 | int output, | 
|  | 391 | int *sessionId, | 
|  | 392 | status_t *status) | 
|  | 393 | { | 
|  | 394 | sp<PlaybackThread::Track> track; | 
|  | 395 | sp<TrackHandle> trackHandle; | 
|  | 396 | sp<Client> client; | 
|  | 397 | wp<Client> wclient; | 
|  | 398 | status_t lStatus; | 
|  | 399 | int lSessionId; | 
|  | 400 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 401 | if (streamType >= AUDIO_STREAM_CNT) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 402 | ALOGE("createTrack() invalid stream type %d", streamType); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 403 | lStatus = BAD_VALUE; | 
|  | 404 | goto Exit; | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | { | 
|  | 408 | Mutex::Autolock _l(mLock); | 
|  | 409 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 410 | PlaybackThread *effectThread = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 411 | if (thread == NULL) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 412 | ALOGE("unknown output thread"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 413 | lStatus = BAD_VALUE; | 
|  | 414 | goto Exit; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | wclient = mClients.valueFor(pid); | 
|  | 418 |  | 
|  | 419 | if (wclient != NULL) { | 
|  | 420 | client = wclient.promote(); | 
|  | 421 | } else { | 
|  | 422 | client = new Client(this, pid); | 
|  | 423 | mClients.add(pid, client); | 
|  | 424 | } | 
|  | 425 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 426 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 427 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 428 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 429 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 430 | if (mPlaybackThreads.keyAt(i) != output) { | 
|  | 431 | // prevent same audio session on different output threads | 
|  | 432 | uint32_t sessions = t->hasAudioSession(*sessionId); | 
|  | 433 | if (sessions & PlaybackThread::TRACK_SESSION) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 434 | ALOGE("createTrack() session ID %d already in use", *sessionId); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 435 | lStatus = BAD_VALUE; | 
|  | 436 | goto Exit; | 
|  | 437 | } | 
|  | 438 | // check if an effect with same session ID is waiting for a track to be created | 
|  | 439 | if (sessions & PlaybackThread::EFFECT_SESSION) { | 
|  | 440 | effectThread = t.get(); | 
|  | 441 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 442 | } | 
|  | 443 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 444 | lSessionId = *sessionId; | 
|  | 445 | } else { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 446 | // if no audio session id is provided, create one here | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 447 | lSessionId = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 448 | if (sessionId != NULL) { | 
|  | 449 | *sessionId = lSessionId; | 
|  | 450 | } | 
|  | 451 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 452 | ALOGV("createTrack() lSessionId: %d", lSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 453 |  | 
|  | 454 | track = thread->createTrack_l(client, streamType, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 455 | channelMask, frameCount, sharedBuffer, lSessionId, &lStatus); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 456 |  | 
|  | 457 | // move effect chain to this output thread if an effect on same session was waiting | 
|  | 458 | // for a track to be created | 
|  | 459 | if (lStatus == NO_ERROR && effectThread != NULL) { | 
|  | 460 | Mutex::Autolock _dl(thread->mLock); | 
|  | 461 | Mutex::Autolock _sl(effectThread->mLock); | 
|  | 462 | moveEffectChain_l(lSessionId, effectThread, thread, true); | 
|  | 463 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 464 | } | 
|  | 465 | if (lStatus == NO_ERROR) { | 
|  | 466 | trackHandle = new TrackHandle(track); | 
|  | 467 | } else { | 
|  | 468 | // remove local strong reference to Client before deleting the Track so that the Client | 
|  | 469 | // destructor is called by the TrackBase destructor with mLock held | 
|  | 470 | client.clear(); | 
|  | 471 | track.clear(); | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | Exit: | 
|  | 475 | if(status) { | 
|  | 476 | *status = lStatus; | 
|  | 477 | } | 
|  | 478 | return trackHandle; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | uint32_t AudioFlinger::sampleRate(int output) const | 
|  | 482 | { | 
|  | 483 | Mutex::Autolock _l(mLock); | 
|  | 484 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 485 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 486 | ALOGW("sampleRate() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 487 | return 0; | 
|  | 488 | } | 
|  | 489 | return thread->sampleRate(); | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | int AudioFlinger::channelCount(int output) const | 
|  | 493 | { | 
|  | 494 | Mutex::Autolock _l(mLock); | 
|  | 495 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 496 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 497 | ALOGW("channelCount() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 498 | return 0; | 
|  | 499 | } | 
|  | 500 | return thread->channelCount(); | 
|  | 501 | } | 
|  | 502 |  | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 503 | uint32_t AudioFlinger::format(int output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 504 | { | 
|  | 505 | Mutex::Autolock _l(mLock); | 
|  | 506 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 507 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 508 | ALOGW("format() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 509 | return 0; | 
|  | 510 | } | 
|  | 511 | return thread->format(); | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | size_t AudioFlinger::frameCount(int output) const | 
|  | 515 | { | 
|  | 516 | Mutex::Autolock _l(mLock); | 
|  | 517 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 518 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 519 | ALOGW("frameCount() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 520 | return 0; | 
|  | 521 | } | 
|  | 522 | return thread->frameCount(); | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | uint32_t AudioFlinger::latency(int output) const | 
|  | 526 | { | 
|  | 527 | Mutex::Autolock _l(mLock); | 
|  | 528 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 529 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 530 | ALOGW("latency() unknown thread %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | return 0; | 
|  | 532 | } | 
|  | 533 | return thread->latency(); | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | status_t AudioFlinger::setMasterVolume(float value) | 
|  | 537 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 538 | status_t ret = initCheck(); | 
|  | 539 | if (ret != NO_ERROR) { | 
|  | 540 | return ret; | 
|  | 541 | } | 
|  | 542 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 543 | // check calling permissions | 
|  | 544 | if (!settingsAllowed()) { | 
|  | 545 | return PERMISSION_DENIED; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | // when hw supports master volume, don't scale in sw mixer | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 549 | { // scope for the lock | 
|  | 550 | AutoMutex lock(mHardwareLock); | 
|  | 551 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 552 | if (mPrimaryHardwareDev->set_master_volume(mPrimaryHardwareDev, value) == NO_ERROR) { | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 553 | value = 1.0f; | 
|  | 554 | } | 
|  | 555 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 556 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 557 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 558 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | mMasterVolume = value; | 
|  | 560 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 561 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); | 
|  | 562 |  | 
|  | 563 | return NO_ERROR; | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | status_t AudioFlinger::setMode(int mode) | 
|  | 567 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 568 | status_t ret = initCheck(); | 
|  | 569 | if (ret != NO_ERROR) { | 
|  | 570 | return ret; | 
|  | 571 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 572 |  | 
|  | 573 | // check calling permissions | 
|  | 574 | if (!settingsAllowed()) { | 
|  | 575 | return PERMISSION_DENIED; | 
|  | 576 | } | 
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 577 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 578 | ALOGW("Illegal value: setMode(%d)", mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 579 | return BAD_VALUE; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | { // scope for the lock | 
|  | 583 | AutoMutex lock(mHardwareLock); | 
|  | 584 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 585 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | if (NO_ERROR == ret) { | 
|  | 590 | Mutex::Autolock _l(mLock); | 
|  | 591 | mMode = mode; | 
|  | 592 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 593 | mPlaybackThreads.valueAt(i)->setMode(mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 594 | } | 
|  | 595 |  | 
|  | 596 | return ret; | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | status_t AudioFlinger::setMicMute(bool state) | 
|  | 600 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 601 | status_t ret = initCheck(); | 
|  | 602 | if (ret != NO_ERROR) { | 
|  | 603 | return ret; | 
|  | 604 | } | 
|  | 605 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 606 | // check calling permissions | 
|  | 607 | if (!settingsAllowed()) { | 
|  | 608 | return PERMISSION_DENIED; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | AutoMutex lock(mHardwareLock); | 
|  | 612 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 613 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 614 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 615 | return ret; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | bool AudioFlinger::getMicMute() const | 
|  | 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 false; | 
|  | 623 | } | 
|  | 624 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 625 | bool state = AUDIO_MODE_INVALID; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 626 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 627 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 628 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 629 | return state; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | status_t AudioFlinger::setMasterMute(bool muted) | 
|  | 633 | { | 
|  | 634 | // check calling permissions | 
|  | 635 | if (!settingsAllowed()) { | 
|  | 636 | return PERMISSION_DENIED; | 
|  | 637 | } | 
|  | 638 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 639 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 640 | mMasterMute = muted; | 
|  | 641 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 642 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); | 
|  | 643 |  | 
|  | 644 | return NO_ERROR; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | float AudioFlinger::masterVolume() const | 
|  | 648 | { | 
|  | 649 | return mMasterVolume; | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | bool AudioFlinger::masterMute() const | 
|  | 653 | { | 
|  | 654 | return mMasterMute; | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | status_t AudioFlinger::setStreamVolume(int stream, float value, int output) | 
|  | 658 | { | 
|  | 659 | // check calling permissions | 
|  | 660 | if (!settingsAllowed()) { | 
|  | 661 | return PERMISSION_DENIED; | 
|  | 662 | } | 
|  | 663 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 664 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 665 | ALOGE("setStreamVolume() invalid stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 666 | return BAD_VALUE; | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | AutoMutex lock(mLock); | 
|  | 670 | PlaybackThread *thread = NULL; | 
|  | 671 | if (output) { | 
|  | 672 | thread = checkPlaybackThread_l(output); | 
|  | 673 | if (thread == NULL) { | 
|  | 674 | return BAD_VALUE; | 
|  | 675 | } | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | mStreamTypes[stream].volume = value; | 
|  | 679 |  | 
|  | 680 | if (thread == NULL) { | 
|  | 681 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 682 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); | 
|  | 683 | } | 
|  | 684 | } else { | 
|  | 685 | thread->setStreamVolume(stream, value); | 
|  | 686 | } | 
|  | 687 |  | 
|  | 688 | return NO_ERROR; | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 | status_t AudioFlinger::setStreamMute(int stream, bool muted) | 
|  | 692 | { | 
|  | 693 | // check calling permissions | 
|  | 694 | if (!settingsAllowed()) { | 
|  | 695 | return PERMISSION_DENIED; | 
|  | 696 | } | 
|  | 697 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 698 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT || | 
|  | 699 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 700 | ALOGE("setStreamMute() invalid stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 701 | return BAD_VALUE; | 
|  | 702 | } | 
|  | 703 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 704 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 705 | mStreamTypes[stream].mute = muted; | 
|  | 706 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) | 
|  | 707 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); | 
|  | 708 |  | 
|  | 709 | return NO_ERROR; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | float AudioFlinger::streamVolume(int stream, int output) const | 
|  | 713 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 714 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | return 0.0f; | 
|  | 716 | } | 
|  | 717 |  | 
|  | 718 | AutoMutex lock(mLock); | 
|  | 719 | float volume; | 
|  | 720 | if (output) { | 
|  | 721 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 722 | if (thread == NULL) { | 
|  | 723 | return 0.0f; | 
|  | 724 | } | 
|  | 725 | volume = thread->streamVolume(stream); | 
|  | 726 | } else { | 
|  | 727 | volume = mStreamTypes[stream].volume; | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | return volume; | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | bool AudioFlinger::streamMute(int stream) const | 
|  | 734 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 735 | if (stream < 0 || stream >= (int)AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | return true; | 
|  | 737 | } | 
|  | 738 |  | 
|  | 739 | return mStreamTypes[stream].mute; | 
|  | 740 | } | 
|  | 741 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 742 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) | 
|  | 743 | { | 
|  | 744 | status_t result; | 
|  | 745 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 746 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 747 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); | 
|  | 748 | // check calling permissions | 
|  | 749 | if (!settingsAllowed()) { | 
|  | 750 | return PERMISSION_DENIED; | 
|  | 751 | } | 
|  | 752 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 753 | // ioHandle == 0 means the parameters are global to the audio hardware interface | 
|  | 754 | if (ioHandle == 0) { | 
|  | 755 | AutoMutex lock(mHardwareLock); | 
|  | 756 | mHardwareStatus = AUDIO_SET_PARAMETER; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 757 | status_t final_result = NO_ERROR; | 
|  | 758 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 759 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 760 | result = dev->set_parameters(dev, keyValuePairs.string()); | 
|  | 761 | final_result = result ?: final_result; | 
|  | 762 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 763 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 764 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 765 | AudioParameter param = AudioParameter(keyValuePairs); | 
|  | 766 | String8 value; | 
|  | 767 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { | 
|  | 768 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 769 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); | 
|  | 770 | if (mBtNrecIsOff != btNrecIsOff) { | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 771 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 772 | sp<RecordThread> thread = mRecordThreads.valueAt(i); | 
|  | 773 | RecordThread::RecordTrack *track = thread->track(); | 
|  | 774 | if (track != NULL) { | 
|  | 775 | audio_devices_t device = (audio_devices_t)( | 
|  | 776 | thread->device() & AUDIO_DEVICE_IN_ALL); | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 777 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 778 | thread->setEffectSuspended(FX_IID_AEC, | 
|  | 779 | suspend, | 
|  | 780 | track->sessionId()); | 
|  | 781 | thread->setEffectSuspended(FX_IID_NS, | 
|  | 782 | suspend, | 
|  | 783 | track->sessionId()); | 
|  | 784 | } | 
|  | 785 | } | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 786 | mBtNrecIsOff = btNrecIsOff; | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 787 | } | 
|  | 788 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 789 | return final_result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 790 | } | 
|  | 791 |  | 
|  | 792 | // hold a strong ref on thread in case closeOutput() or closeInput() is called | 
|  | 793 | // and the thread is exited once the lock is released | 
|  | 794 | sp<ThreadBase> thread; | 
|  | 795 | { | 
|  | 796 | Mutex::Autolock _l(mLock); | 
|  | 797 | thread = checkPlaybackThread_l(ioHandle); | 
|  | 798 | if (thread == NULL) { | 
|  | 799 | thread = checkRecordThread_l(ioHandle); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 800 | } else if (thread.get() == primaryPlaybackThread_l()) { | 
|  | 801 | // indicate output device change to all input threads for pre processing | 
|  | 802 | AudioParameter param = AudioParameter(keyValuePairs); | 
|  | 803 | int value; | 
|  | 804 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 805 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 806 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); | 
|  | 807 | } | 
|  | 808 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 809 | } | 
|  | 810 | } | 
|  | 811 | if (thread != NULL) { | 
|  | 812 | result = thread->setParameters(keyValuePairs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 813 | return result; | 
|  | 814 | } | 
|  | 815 | return BAD_VALUE; | 
|  | 816 | } | 
|  | 817 |  | 
|  | 818 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) | 
|  | 819 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 820 | //    ALOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 821 | //            ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); | 
|  | 822 |  | 
|  | 823 | if (ioHandle == 0) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 824 | String8 out_s8; | 
|  | 825 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 826 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 827 | audio_hw_device_t *dev = mAudioHwDevs[i]; | 
|  | 828 | char *s = dev->get_parameters(dev, keys.string()); | 
|  | 829 | out_s8 += String8(s); | 
|  | 830 | free(s); | 
|  | 831 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 832 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 833 | } | 
|  | 834 |  | 
|  | 835 | Mutex::Autolock _l(mLock); | 
|  | 836 |  | 
|  | 837 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); | 
|  | 838 | if (playbackThread != NULL) { | 
|  | 839 | return playbackThread->getParameters(keys); | 
|  | 840 | } | 
|  | 841 | RecordThread *recordThread = checkRecordThread_l(ioHandle); | 
|  | 842 | if (recordThread != NULL) { | 
|  | 843 | return recordThread->getParameters(keys); | 
|  | 844 | } | 
|  | 845 | return String8(""); | 
|  | 846 | } | 
|  | 847 |  | 
|  | 848 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) | 
|  | 849 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 850 | status_t ret = initCheck(); | 
|  | 851 | if (ret != NO_ERROR) { | 
|  | 852 | return 0; | 
|  | 853 | } | 
|  | 854 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 855 | return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 856 | } | 
|  | 857 |  | 
|  | 858 | unsigned int AudioFlinger::getInputFramesLost(int ioHandle) | 
|  | 859 | { | 
|  | 860 | if (ioHandle == 0) { | 
|  | 861 | return 0; | 
|  | 862 | } | 
|  | 863 |  | 
|  | 864 | Mutex::Autolock _l(mLock); | 
|  | 865 |  | 
|  | 866 | RecordThread *recordThread = checkRecordThread_l(ioHandle); | 
|  | 867 | if (recordThread != NULL) { | 
|  | 868 | return recordThread->getInputFramesLost(); | 
|  | 869 | } | 
|  | 870 | return 0; | 
|  | 871 | } | 
|  | 872 |  | 
|  | 873 | status_t AudioFlinger::setVoiceVolume(float value) | 
|  | 874 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 875 | status_t ret = initCheck(); | 
|  | 876 | if (ret != NO_ERROR) { | 
|  | 877 | return ret; | 
|  | 878 | } | 
|  | 879 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 880 | // check calling permissions | 
|  | 881 | if (!settingsAllowed()) { | 
|  | 882 | return PERMISSION_DENIED; | 
|  | 883 | } | 
|  | 884 |  | 
|  | 885 | AutoMutex lock(mHardwareLock); | 
|  | 886 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 887 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 888 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 889 |  | 
|  | 890 | return ret; | 
|  | 891 | } | 
|  | 892 |  | 
|  | 893 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output) | 
|  | 894 | { | 
|  | 895 | status_t status; | 
|  | 896 |  | 
|  | 897 | Mutex::Autolock _l(mLock); | 
|  | 898 |  | 
|  | 899 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); | 
|  | 900 | if (playbackThread != NULL) { | 
|  | 901 | return playbackThread->getRenderPosition(halFrames, dspFrames); | 
|  | 902 | } | 
|  | 903 |  | 
|  | 904 | return BAD_VALUE; | 
|  | 905 | } | 
|  | 906 |  | 
|  | 907 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) | 
|  | 908 | { | 
|  | 909 |  | 
|  | 910 | Mutex::Autolock _l(mLock); | 
|  | 911 |  | 
|  | 912 | int pid = IPCThreadState::self()->getCallingPid(); | 
|  | 913 | if (mNotificationClients.indexOfKey(pid) < 0) { | 
|  | 914 | sp<NotificationClient> notificationClient = new NotificationClient(this, | 
|  | 915 | client, | 
|  | 916 | pid); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 917 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 918 |  | 
|  | 919 | mNotificationClients.add(pid, notificationClient); | 
|  | 920 |  | 
|  | 921 | sp<IBinder> binder = client->asBinder(); | 
|  | 922 | binder->linkToDeath(notificationClient); | 
|  | 923 |  | 
|  | 924 | // the config change is always sent from playback or record threads to avoid deadlock | 
|  | 925 | // with AudioSystem::gLock | 
|  | 926 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 927 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); | 
|  | 928 | } | 
|  | 929 |  | 
|  | 930 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 931 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); | 
|  | 932 | } | 
|  | 933 | } | 
|  | 934 | } | 
|  | 935 |  | 
|  | 936 | void AudioFlinger::removeNotificationClient(pid_t pid) | 
|  | 937 | { | 
|  | 938 | Mutex::Autolock _l(mLock); | 
|  | 939 |  | 
|  | 940 | int index = mNotificationClients.indexOfKey(pid); | 
|  | 941 | if (index >= 0) { | 
|  | 942 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 943 | ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 944 | mNotificationClients.removeItem(pid); | 
|  | 945 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 946 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 947 | ALOGV("%d died, releasing its sessions", pid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 948 | int num = mAudioSessionRefs.size(); | 
|  | 949 | bool removed = false; | 
|  | 950 | for (int i = 0; i< num; i++) { | 
|  | 951 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 952 | ALOGV(" pid %d @ %d", ref->pid, i); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 953 | if (ref->pid == pid) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 954 | ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 955 | mAudioSessionRefs.removeAt(i); | 
|  | 956 | delete ref; | 
|  | 957 | removed = true; | 
|  | 958 | i--; | 
|  | 959 | num--; | 
|  | 960 | } | 
|  | 961 | } | 
|  | 962 | if (removed) { | 
|  | 963 | purgeStaleEffects_l(); | 
|  | 964 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 965 | } | 
|  | 966 |  | 
|  | 967 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held | 
|  | 968 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) | 
|  | 969 | { | 
|  | 970 | size_t size = mNotificationClients.size(); | 
|  | 971 | for (size_t i = 0; i < size; i++) { | 
|  | 972 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); | 
|  | 973 | } | 
|  | 974 | } | 
|  | 975 |  | 
|  | 976 | // removeClient_l() must be called with AudioFlinger::mLock held | 
|  | 977 | void AudioFlinger::removeClient_l(pid_t pid) | 
|  | 978 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 979 | 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] | 980 | mClients.removeItem(pid); | 
|  | 981 | } | 
|  | 982 |  | 
|  | 983 |  | 
|  | 984 | // ---------------------------------------------------------------------------- | 
|  | 985 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 986 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 987 | :   Thread(false), | 
|  | 988 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 989 | mFrameSize(1), mFormat(0), mStandby(false), mId(id), mExiting(false), | 
|  | 990 | mDevice(device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 991 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 992 | mDeathRecipient = new PMDeathRecipient(this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | } | 
|  | 994 |  | 
|  | 995 | AudioFlinger::ThreadBase::~ThreadBase() | 
|  | 996 | { | 
|  | 997 | mParamCond.broadcast(); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 998 | // do not lock the mutex in destructor | 
|  | 999 | releaseWakeLock_l(); | 
| Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 1000 | if (mPowerManager != 0) { | 
|  | 1001 | sp<IBinder> binder = mPowerManager->asBinder(); | 
|  | 1002 | binder->unlinkToDeath(mDeathRecipient); | 
|  | 1003 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1004 | } | 
|  | 1005 |  | 
|  | 1006 | void AudioFlinger::ThreadBase::exit() | 
|  | 1007 | { | 
| Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 1008 | // keep a strong ref on ourself so that we won't get | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1009 | // destroyed in the middle of requestExitAndWait() | 
|  | 1010 | sp <ThreadBase> strongMe = this; | 
|  | 1011 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1012 | ALOGV("ThreadBase::exit"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1013 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 1014 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1015 | mExiting = true; | 
|  | 1016 | requestExit(); | 
|  | 1017 | mWaitWorkCV.signal(); | 
|  | 1018 | } | 
|  | 1019 | requestExitAndWait(); | 
|  | 1020 | } | 
|  | 1021 |  | 
|  | 1022 | uint32_t AudioFlinger::ThreadBase::sampleRate() const | 
|  | 1023 | { | 
|  | 1024 | return mSampleRate; | 
|  | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | int AudioFlinger::ThreadBase::channelCount() const | 
|  | 1028 | { | 
|  | 1029 | return (int)mChannelCount; | 
|  | 1030 | } | 
|  | 1031 |  | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1032 | uint32_t AudioFlinger::ThreadBase::format() const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1033 | { | 
|  | 1034 | return mFormat; | 
|  | 1035 | } | 
|  | 1036 |  | 
|  | 1037 | size_t AudioFlinger::ThreadBase::frameCount() const | 
|  | 1038 | { | 
|  | 1039 | return mFrameCount; | 
|  | 1040 | } | 
|  | 1041 |  | 
|  | 1042 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
|  | 1043 | { | 
|  | 1044 | status_t status; | 
|  | 1045 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1046 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1047 | Mutex::Autolock _l(mLock); | 
|  | 1048 |  | 
|  | 1049 | mNewParameters.add(keyValuePairs); | 
|  | 1050 | mWaitWorkCV.signal(); | 
|  | 1051 | // wait condition with timeout in case the thread loop has exited | 
|  | 1052 | // before the request could be processed | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 1053 | if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1054 | status = mParamStatus; | 
|  | 1055 | mWaitWorkCV.signal(); | 
|  | 1056 | } else { | 
|  | 1057 | status = TIMED_OUT; | 
|  | 1058 | } | 
|  | 1059 | return status; | 
|  | 1060 | } | 
|  | 1061 |  | 
|  | 1062 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) | 
|  | 1063 | { | 
|  | 1064 | Mutex::Autolock _l(mLock); | 
|  | 1065 | sendConfigEvent_l(event, param); | 
|  | 1066 | } | 
|  | 1067 |  | 
|  | 1068 | // sendConfigEvent_l() must be called with ThreadBase::mLock held | 
|  | 1069 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) | 
|  | 1070 | { | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1071 | ConfigEvent configEvent; | 
|  | 1072 | configEvent.mEvent = event; | 
|  | 1073 | configEvent.mParam = param; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1074 | mConfigEvents.add(configEvent); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1075 | 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] | 1076 | mWaitWorkCV.signal(); | 
|  | 1077 | } | 
|  | 1078 |  | 
|  | 1079 | void AudioFlinger::ThreadBase::processConfigEvents() | 
|  | 1080 | { | 
|  | 1081 | mLock.lock(); | 
|  | 1082 | while(!mConfigEvents.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1083 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1084 | ConfigEvent configEvent = mConfigEvents[0]; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1085 | mConfigEvents.removeAt(0); | 
|  | 1086 | // release mLock before locking AudioFlinger mLock: lock order is always | 
|  | 1087 | // AudioFlinger then ThreadBase to avoid cross deadlock | 
|  | 1088 | mLock.unlock(); | 
|  | 1089 | mAudioFlinger->mLock.lock(); | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1090 | audioConfigChanged_l(configEvent.mEvent, configEvent.mParam); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1091 | mAudioFlinger->mLock.unlock(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1092 | mLock.lock(); | 
|  | 1093 | } | 
|  | 1094 | mLock.unlock(); | 
|  | 1095 | } | 
|  | 1096 |  | 
|  | 1097 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) | 
|  | 1098 | { | 
|  | 1099 | const size_t SIZE = 256; | 
|  | 1100 | char buffer[SIZE]; | 
|  | 1101 | String8 result; | 
|  | 1102 |  | 
|  | 1103 | bool locked = tryLock(mLock); | 
|  | 1104 | if (!locked) { | 
|  | 1105 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); | 
|  | 1106 | write(fd, buffer, strlen(buffer)); | 
|  | 1107 | } | 
|  | 1108 |  | 
|  | 1109 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); | 
|  | 1110 | result.append(buffer); | 
|  | 1111 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); | 
|  | 1112 | result.append(buffer); | 
|  | 1113 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); | 
|  | 1114 | result.append(buffer); | 
|  | 1115 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); | 
|  | 1116 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1117 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); | 
|  | 1118 | result.append(buffer); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1119 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); | 
|  | 1120 | result.append(buffer); | 
|  | 1121 | snprintf(buffer, SIZE, "Frame size: %d\n", mFrameSize); | 
|  | 1122 | result.append(buffer); | 
|  | 1123 |  | 
|  | 1124 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); | 
|  | 1125 | result.append(buffer); | 
|  | 1126 | result.append(" Index Command"); | 
|  | 1127 | for (size_t i = 0; i < mNewParameters.size(); ++i) { | 
|  | 1128 | snprintf(buffer, SIZE, "\n %02d    ", i); | 
|  | 1129 | result.append(buffer); | 
|  | 1130 | result.append(mNewParameters[i]); | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); | 
|  | 1134 | result.append(buffer); | 
|  | 1135 | snprintf(buffer, SIZE, " Index event param\n"); | 
|  | 1136 | result.append(buffer); | 
|  | 1137 | for (size_t i = 0; i < mConfigEvents.size(); i++) { | 
| Glenn Kasten | f3990f2 | 2011-12-13 11:50:00 -0800 | [diff] [blame] | 1138 | 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] | 1139 | result.append(buffer); | 
|  | 1140 | } | 
|  | 1141 | result.append("\n"); | 
|  | 1142 |  | 
|  | 1143 | write(fd, result.string(), result.size()); | 
|  | 1144 |  | 
|  | 1145 | if (locked) { | 
|  | 1146 | mLock.unlock(); | 
|  | 1147 | } | 
|  | 1148 | return NO_ERROR; | 
|  | 1149 | } | 
|  | 1150 |  | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1151 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
|  | 1152 | { | 
|  | 1153 | const size_t SIZE = 256; | 
|  | 1154 | char buffer[SIZE]; | 
|  | 1155 | String8 result; | 
|  | 1156 |  | 
|  | 1157 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); | 
|  | 1158 | write(fd, buffer, strlen(buffer)); | 
|  | 1159 |  | 
|  | 1160 | for (size_t i = 0; i < mEffectChains.size(); ++i) { | 
|  | 1161 | sp<EffectChain> chain = mEffectChains[i]; | 
|  | 1162 | if (chain != 0) { | 
|  | 1163 | chain->dump(fd, args); | 
|  | 1164 | } | 
|  | 1165 | } | 
|  | 1166 | return NO_ERROR; | 
|  | 1167 | } | 
|  | 1168 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1169 | void AudioFlinger::ThreadBase::acquireWakeLock() | 
|  | 1170 | { | 
|  | 1171 | Mutex::Autolock _l(mLock); | 
|  | 1172 | acquireWakeLock_l(); | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | void AudioFlinger::ThreadBase::acquireWakeLock_l() | 
|  | 1176 | { | 
|  | 1177 | if (mPowerManager == 0) { | 
|  | 1178 | // use checkService() to avoid blocking if power service is not up yet | 
|  | 1179 | sp<IBinder> binder = | 
|  | 1180 | defaultServiceManager()->checkService(String16("power")); | 
|  | 1181 | if (binder == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1182 | ALOGW("Thread %s cannot connect to the power manager service", mName); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1183 | } else { | 
|  | 1184 | mPowerManager = interface_cast<IPowerManager>(binder); | 
|  | 1185 | binder->linkToDeath(mDeathRecipient); | 
|  | 1186 | } | 
|  | 1187 | } | 
|  | 1188 | if (mPowerManager != 0) { | 
|  | 1189 | sp<IBinder> binder = new BBinder(); | 
|  | 1190 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
|  | 1191 | binder, | 
|  | 1192 | String16(mName)); | 
|  | 1193 | if (status == NO_ERROR) { | 
|  | 1194 | mWakeLockToken = binder; | 
|  | 1195 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1196 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1197 | } | 
|  | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
|  | 1201 | { | 
|  | 1202 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1203 | releaseWakeLock_l(); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
|  | 1207 | { | 
|  | 1208 | if (mWakeLockToken != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1209 | ALOGV("releaseWakeLock_l() %s", mName); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1210 | if (mPowerManager != 0) { | 
|  | 1211 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); | 
|  | 1212 | } | 
|  | 1213 | mWakeLockToken.clear(); | 
|  | 1214 | } | 
|  | 1215 | } | 
|  | 1216 |  | 
|  | 1217 | void AudioFlinger::ThreadBase::clearPowerManager() | 
|  | 1218 | { | 
|  | 1219 | Mutex::Autolock _l(mLock); | 
|  | 1220 | releaseWakeLock_l(); | 
|  | 1221 | mPowerManager.clear(); | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) | 
|  | 1225 | { | 
|  | 1226 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 1227 | if (thread != 0) { | 
|  | 1228 | thread->clearPowerManager(); | 
|  | 1229 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1230 | ALOGW("power manager service died !!!"); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1231 | } | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1232 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1233 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
|  | 1234 | const effect_uuid_t *type, bool suspend, int sessionId) | 
|  | 1235 | { | 
|  | 1236 | Mutex::Autolock _l(mLock); | 
|  | 1237 | setEffectSuspended_l(type, suspend, sessionId); | 
|  | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
|  | 1241 | const effect_uuid_t *type, bool suspend, int sessionId) | 
|  | 1242 | { | 
|  | 1243 | sp<EffectChain> chain; | 
|  | 1244 | chain = getEffectChain_l(sessionId); | 
|  | 1245 | if (chain != 0) { | 
|  | 1246 | if (type != NULL) { | 
|  | 1247 | chain->setEffectSuspended_l(type, suspend); | 
|  | 1248 | } else { | 
|  | 1249 | chain->setEffectSuspendedAll_l(suspend); | 
|  | 1250 | } | 
|  | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | updateSuspendedSessions_l(type, suspend, sessionId); | 
|  | 1254 | } | 
|  | 1255 |  | 
|  | 1256 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
|  | 1257 | { | 
|  | 1258 | int index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
|  | 1259 | if (index < 0) { | 
|  | 1260 | return; | 
|  | 1261 | } | 
|  | 1262 |  | 
|  | 1263 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = | 
|  | 1264 | mSuspendedSessions.editValueAt(index); | 
|  | 1265 |  | 
|  | 1266 | for (size_t i = 0; i < sessionEffects.size(); i++) { | 
|  | 1267 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); | 
|  | 1268 | for (int j = 0; j < desc->mRefCount; j++) { | 
|  | 1269 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
|  | 1270 | chain->setEffectSuspendedAll_l(true); | 
|  | 1271 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1272 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1273 | desc->mType.timeLow); | 
|  | 1274 | chain->setEffectSuspended_l(&desc->mType, true); | 
|  | 1275 | } | 
|  | 1276 | } | 
|  | 1277 | } | 
|  | 1278 | } | 
|  | 1279 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1280 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
|  | 1281 | bool suspend, | 
|  | 1282 | int sessionId) | 
|  | 1283 | { | 
|  | 1284 | int index = mSuspendedSessions.indexOfKey(sessionId); | 
|  | 1285 |  | 
|  | 1286 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
|  | 1287 |  | 
|  | 1288 | if (suspend) { | 
|  | 1289 | if (index >= 0) { | 
|  | 1290 | sessionEffects = mSuspendedSessions.editValueAt(index); | 
|  | 1291 | } else { | 
|  | 1292 | mSuspendedSessions.add(sessionId, sessionEffects); | 
|  | 1293 | } | 
|  | 1294 | } else { | 
|  | 1295 | if (index < 0) { | 
|  | 1296 | return; | 
|  | 1297 | } | 
|  | 1298 | sessionEffects = mSuspendedSessions.editValueAt(index); | 
|  | 1299 | } | 
|  | 1300 |  | 
|  | 1301 |  | 
|  | 1302 | int key = EffectChain::kKeyForSuspendAll; | 
|  | 1303 | if (type != NULL) { | 
|  | 1304 | key = type->timeLow; | 
|  | 1305 | } | 
|  | 1306 | index = sessionEffects.indexOfKey(key); | 
|  | 1307 |  | 
|  | 1308 | sp <SuspendedSessionDesc> desc; | 
|  | 1309 | if (suspend) { | 
|  | 1310 | if (index >= 0) { | 
|  | 1311 | desc = sessionEffects.valueAt(index); | 
|  | 1312 | } else { | 
|  | 1313 | desc = new SuspendedSessionDesc(); | 
|  | 1314 | if (type != NULL) { | 
|  | 1315 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); | 
|  | 1316 | } | 
|  | 1317 | sessionEffects.add(key, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1318 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1319 | } | 
|  | 1320 | desc->mRefCount++; | 
|  | 1321 | } else { | 
|  | 1322 | if (index < 0) { | 
|  | 1323 | return; | 
|  | 1324 | } | 
|  | 1325 | desc = sessionEffects.valueAt(index); | 
|  | 1326 | if (--desc->mRefCount == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1327 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1328 | sessionEffects.removeItemsAt(index); | 
|  | 1329 | if (sessionEffects.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1330 | ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1331 | sessionId); | 
|  | 1332 | mSuspendedSessions.removeItem(sessionId); | 
|  | 1333 | } | 
|  | 1334 | } | 
|  | 1335 | } | 
|  | 1336 | if (!sessionEffects.isEmpty()) { | 
|  | 1337 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
|  | 1338 | } | 
|  | 1339 | } | 
|  | 1340 |  | 
|  | 1341 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
|  | 1342 | bool enabled, | 
|  | 1343 | int sessionId) | 
|  | 1344 | { | 
|  | 1345 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1346 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
|  | 1347 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1348 |  | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1349 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
|  | 1350 | bool enabled, | 
|  | 1351 | int sessionId) | 
|  | 1352 | { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1353 | if (mType != RECORD) { | 
|  | 1354 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
|  | 1355 | // another session. This gives the priority to well behaved effect control panels | 
|  | 1356 | // and applications not using global effects. | 
|  | 1357 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1358 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
|  | 1359 | } | 
|  | 1360 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1361 |  | 
|  | 1362 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1363 | if (chain != 0) { | 
|  | 1364 | chain->checkSuspendOnEffectEnabled(effect, enabled); | 
|  | 1365 | } | 
|  | 1366 | } | 
|  | 1367 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1368 | // ---------------------------------------------------------------------------- | 
|  | 1369 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1370 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 1371 | AudioStreamOut* output, | 
|  | 1372 | int id, | 
|  | 1373 | uint32_t device) | 
|  | 1374 | :   ThreadBase(audioFlinger, id, device), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 1375 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), mOutput(output), | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1376 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1377 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1378 | snprintf(mName, kNameLength, "AudioOut_%d", id); | 
|  | 1379 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1380 | readOutputParameters(); | 
|  | 1381 |  | 
|  | 1382 | mMasterVolume = mAudioFlinger->masterVolume(); | 
|  | 1383 | mMasterMute = mAudioFlinger->masterMute(); | 
|  | 1384 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1385 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1386 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); | 
|  | 1387 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1388 | mStreamTypes[stream].valid = true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1389 | } | 
|  | 1390 | } | 
|  | 1391 |  | 
|  | 1392 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
|  | 1393 | { | 
|  | 1394 | delete [] mMixBuffer; | 
|  | 1395 | } | 
|  | 1396 |  | 
|  | 1397 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
|  | 1398 | { | 
|  | 1399 | dumpInternals(fd, args); | 
|  | 1400 | dumpTracks(fd, args); | 
|  | 1401 | dumpEffectChains(fd, args); | 
|  | 1402 | return NO_ERROR; | 
|  | 1403 | } | 
|  | 1404 |  | 
|  | 1405 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) | 
|  | 1406 | { | 
|  | 1407 | const size_t SIZE = 256; | 
|  | 1408 | char buffer[SIZE]; | 
|  | 1409 | String8 result; | 
|  | 1410 |  | 
|  | 1411 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); | 
|  | 1412 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1413 | 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] | 1414 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1415 | sp<Track> track = mTracks[i]; | 
|  | 1416 | if (track != 0) { | 
|  | 1417 | track->dump(buffer, SIZE); | 
|  | 1418 | result.append(buffer); | 
|  | 1419 | } | 
|  | 1420 | } | 
|  | 1421 |  | 
|  | 1422 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); | 
|  | 1423 | result.append(buffer); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1424 | 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] | 1425 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { | 
|  | 1426 | wp<Track> wTrack = mActiveTracks[i]; | 
|  | 1427 | if (wTrack != 0) { | 
|  | 1428 | sp<Track> track = wTrack.promote(); | 
|  | 1429 | if (track != 0) { | 
|  | 1430 | track->dump(buffer, SIZE); | 
|  | 1431 | result.append(buffer); | 
|  | 1432 | } | 
|  | 1433 | } | 
|  | 1434 | } | 
|  | 1435 | write(fd, result.string(), result.size()); | 
|  | 1436 | return NO_ERROR; | 
|  | 1437 | } | 
|  | 1438 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1439 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 1440 | { | 
|  | 1441 | const size_t SIZE = 256; | 
|  | 1442 | char buffer[SIZE]; | 
|  | 1443 | String8 result; | 
|  | 1444 |  | 
|  | 1445 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); | 
|  | 1446 | result.append(buffer); | 
|  | 1447 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); | 
|  | 1448 | result.append(buffer); | 
|  | 1449 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); | 
|  | 1450 | result.append(buffer); | 
|  | 1451 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); | 
|  | 1452 | result.append(buffer); | 
|  | 1453 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); | 
|  | 1454 | result.append(buffer); | 
|  | 1455 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); | 
|  | 1456 | result.append(buffer); | 
|  | 1457 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); | 
|  | 1458 | result.append(buffer); | 
|  | 1459 | write(fd, result.string(), result.size()); | 
|  | 1460 |  | 
|  | 1461 | dumpBase(fd, args); | 
|  | 1462 |  | 
|  | 1463 | return NO_ERROR; | 
|  | 1464 | } | 
|  | 1465 |  | 
|  | 1466 | // Thread virtuals | 
|  | 1467 | status_t AudioFlinger::PlaybackThread::readyToRun() | 
|  | 1468 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1469 | status_t status = initCheck(); | 
|  | 1470 | if (status == NO_ERROR) { | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1471 | ALOGI("AudioFlinger's thread %p ready to run", this); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1472 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1473 | ALOGE("No working audio driver found."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1474 | } | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1475 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | } | 
|  | 1477 |  | 
|  | 1478 | void AudioFlinger::PlaybackThread::onFirstRef() | 
|  | 1479 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1480 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1481 | } | 
|  | 1482 |  | 
|  | 1483 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
|  | 1484 | sp<AudioFlinger::PlaybackThread::Track>  AudioFlinger::PlaybackThread::createTrack_l( | 
|  | 1485 | const sp<AudioFlinger::Client>& client, | 
|  | 1486 | int streamType, | 
|  | 1487 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1488 | uint32_t format, | 
|  | 1489 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1490 | int frameCount, | 
|  | 1491 | const sp<IMemory>& sharedBuffer, | 
|  | 1492 | int sessionId, | 
|  | 1493 | status_t *status) | 
|  | 1494 | { | 
|  | 1495 | sp<Track> track; | 
|  | 1496 | status_t lStatus; | 
|  | 1497 |  | 
|  | 1498 | if (mType == DIRECT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1499 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { | 
|  | 1500 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1501 | 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] | 1502 | "for output %p with format %d", | 
|  | 1503 | sampleRate, format, channelMask, mOutput, mFormat); | 
|  | 1504 | lStatus = BAD_VALUE; | 
|  | 1505 | goto Exit; | 
|  | 1506 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1507 | } | 
|  | 1508 | } else { | 
|  | 1509 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. | 
|  | 1510 | if (sampleRate > mSampleRate*2) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1511 | ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1512 | lStatus = BAD_VALUE; | 
|  | 1513 | goto Exit; | 
|  | 1514 | } | 
|  | 1515 | } | 
|  | 1516 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1517 | lStatus = initCheck(); | 
|  | 1518 | if (lStatus != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1519 | ALOGE("Audio driver not initialized."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | goto Exit; | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | { // scope for mLock | 
|  | 1524 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1525 |  | 
|  | 1526 | // all tracks in same audio session must share the same routing strategy otherwise | 
|  | 1527 | // conflicts will happen when tracks are moved from one output to another by audio policy | 
|  | 1528 | // manager | 
|  | 1529 | uint32_t strategy = | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1530 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1531 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1532 | sp<Track> t = mTracks[i]; | 
|  | 1533 | if (t != 0) { | 
| Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1534 | uint32_t actual = AudioSystem::getStrategyForStream((audio_stream_type_t)t->type()); | 
|  | 1535 | if (sessionId == t->sessionId() && strategy != actual) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1536 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
| Glenn Kasten | d879601 | 2011-10-28 10:31:42 -0700 | [diff] [blame] | 1537 | strategy, actual); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1538 | lStatus = BAD_VALUE; | 
|  | 1539 | goto Exit; | 
|  | 1540 | } | 
|  | 1541 | } | 
|  | 1542 | } | 
|  | 1543 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1544 | track = new Track(this, client, streamType, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1545 | channelMask, frameCount, sharedBuffer, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1546 | if (track->getCblk() == NULL || track->name() < 0) { | 
|  | 1547 | lStatus = NO_MEMORY; | 
|  | 1548 | goto Exit; | 
|  | 1549 | } | 
|  | 1550 | mTracks.add(track); | 
|  | 1551 |  | 
|  | 1552 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 1553 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1554 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1555 | track->setMainBuffer(chain->inBuffer()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1556 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1557 | chain->incTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1558 | } | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1559 |  | 
|  | 1560 | // invalidate track immediately if the stream type was moved to another thread since | 
|  | 1561 | // createTrack() was called by the client process. | 
|  | 1562 | if (!mStreamTypes[streamType].valid) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1563 | ALOGW("createTrack_l() on thread %p: invalidating track on stream %d", | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1564 | this, streamType); | 
|  | 1565 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); | 
|  | 1566 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1567 | } | 
|  | 1568 | lStatus = NO_ERROR; | 
|  | 1569 |  | 
|  | 1570 | Exit: | 
|  | 1571 | if(status) { | 
|  | 1572 | *status = lStatus; | 
|  | 1573 | } | 
|  | 1574 | return track; | 
|  | 1575 | } | 
|  | 1576 |  | 
|  | 1577 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
|  | 1578 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1579 | Mutex::Autolock _l(mLock); | 
|  | 1580 | if (initCheck() == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1581 | return mOutput->stream->get_latency(mOutput->stream); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1582 | } else { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1583 | return 0; | 
|  | 1584 | } | 
|  | 1585 | } | 
|  | 1586 |  | 
|  | 1587 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
|  | 1588 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1589 | mMasterVolume = value; | 
|  | 1590 | return NO_ERROR; | 
|  | 1591 | } | 
|  | 1592 |  | 
|  | 1593 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
|  | 1594 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1595 | mMasterMute = muted; | 
|  | 1596 | return NO_ERROR; | 
|  | 1597 | } | 
|  | 1598 |  | 
|  | 1599 | float AudioFlinger::PlaybackThread::masterVolume() const | 
|  | 1600 | { | 
|  | 1601 | return mMasterVolume; | 
|  | 1602 | } | 
|  | 1603 |  | 
|  | 1604 | bool AudioFlinger::PlaybackThread::masterMute() const | 
|  | 1605 | { | 
|  | 1606 | return mMasterMute; | 
|  | 1607 | } | 
|  | 1608 |  | 
|  | 1609 | status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value) | 
|  | 1610 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1611 | mStreamTypes[stream].volume = value; | 
|  | 1612 | return NO_ERROR; | 
|  | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted) | 
|  | 1616 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1617 | mStreamTypes[stream].mute = muted; | 
|  | 1618 | return NO_ERROR; | 
|  | 1619 | } | 
|  | 1620 |  | 
|  | 1621 | float AudioFlinger::PlaybackThread::streamVolume(int stream) const | 
|  | 1622 | { | 
|  | 1623 | return mStreamTypes[stream].volume; | 
|  | 1624 | } | 
|  | 1625 |  | 
|  | 1626 | bool AudioFlinger::PlaybackThread::streamMute(int stream) const | 
|  | 1627 | { | 
|  | 1628 | return mStreamTypes[stream].mute; | 
|  | 1629 | } | 
|  | 1630 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1631 | // addTrack_l() must be called with ThreadBase::mLock held | 
|  | 1632 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
|  | 1633 | { | 
|  | 1634 | status_t status = ALREADY_EXISTS; | 
|  | 1635 |  | 
|  | 1636 | // set retry count for buffer fill | 
|  | 1637 | track->mRetryCount = kMaxTrackStartupRetries; | 
|  | 1638 | if (mActiveTracks.indexOf(track) < 0) { | 
|  | 1639 | // the track is newly added, make sure it fills up all its | 
|  | 1640 | // buffers before playing. This is to ensure the client will | 
|  | 1641 | // effectively get the latency it requested. | 
|  | 1642 | track->mFillingUpStatus = Track::FS_FILLING; | 
|  | 1643 | track->mResetDone = false; | 
|  | 1644 | mActiveTracks.add(track); | 
|  | 1645 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 1646 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 1647 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1648 | 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] | 1649 | chain->incActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1650 | } | 
|  | 1651 | } | 
|  | 1652 |  | 
|  | 1653 | status = NO_ERROR; | 
|  | 1654 | } | 
|  | 1655 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1656 | ALOGV("mWaitWorkCV.broadcast"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1657 | mWaitWorkCV.broadcast(); | 
|  | 1658 |  | 
|  | 1659 | return status; | 
|  | 1660 | } | 
|  | 1661 |  | 
|  | 1662 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
|  | 1663 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
|  | 1664 | { | 
|  | 1665 | track->mState = TrackBase::TERMINATED; | 
|  | 1666 | if (mActiveTracks.indexOf(track) < 0) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1667 | removeTrack_l(track); | 
|  | 1668 | } | 
|  | 1669 | } | 
|  | 1670 |  | 
|  | 1671 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
|  | 1672 | { | 
|  | 1673 | mTracks.remove(track); | 
|  | 1674 | deleteTrackName_l(track->name()); | 
|  | 1675 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
|  | 1676 | if (chain != 0) { | 
|  | 1677 | chain->decTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1678 | } | 
|  | 1679 | } | 
|  | 1680 |  | 
|  | 1681 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
|  | 1682 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1683 | String8 out_s8 = String8(""); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1684 | char *s; | 
|  | 1685 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1686 | Mutex::Autolock _l(mLock); | 
|  | 1687 | if (initCheck() != NO_ERROR) { | 
|  | 1688 | return out_s8; | 
|  | 1689 | } | 
|  | 1690 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1691 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1692 | out_s8 = String8(s); | 
|  | 1693 | free(s); | 
|  | 1694 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1695 | } | 
|  | 1696 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1697 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1698 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { | 
|  | 1699 | AudioSystem::OutputDescriptor desc; | 
|  | 1700 | void *param2 = 0; | 
|  | 1701 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1702 | 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] | 1703 |  | 
|  | 1704 | switch (event) { | 
|  | 1705 | case AudioSystem::OUTPUT_OPENED: | 
|  | 1706 | case AudioSystem::OUTPUT_CONFIG_CHANGED: | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1707 | desc.channels = mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1708 | desc.samplingRate = mSampleRate; | 
|  | 1709 | desc.format = mFormat; | 
|  | 1710 | desc.frameCount = mFrameCount; | 
|  | 1711 | desc.latency = latency(); | 
|  | 1712 | param2 = &desc; | 
|  | 1713 | break; | 
|  | 1714 |  | 
|  | 1715 | case AudioSystem::STREAM_CONFIG_CHANGED: | 
|  | 1716 | param2 = ¶m; | 
|  | 1717 | case AudioSystem::OUTPUT_CLOSED: | 
|  | 1718 | default: | 
|  | 1719 | break; | 
|  | 1720 | } | 
|  | 1721 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
|  | 1722 | } | 
|  | 1723 |  | 
|  | 1724 | void AudioFlinger::PlaybackThread::readOutputParameters() | 
|  | 1725 | { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1726 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1727 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); | 
|  | 1728 | mChannelCount = (uint16_t)popcount(mChannelMask); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1729 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
|  | 1730 | mFrameSize = (uint16_t)audio_stream_frame_size(&mOutput->stream->common); | 
|  | 1731 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1732 |  | 
|  | 1733 | // FIXME - Current mixer implementation only supports stereo output: Always | 
|  | 1734 | // Allocate a stereo buffer even if HW output is mono. | 
|  | 1735 | if (mMixBuffer != NULL) delete[] mMixBuffer; | 
|  | 1736 | mMixBuffer = new int16_t[mFrameCount * 2]; | 
|  | 1737 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); | 
|  | 1738 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1739 | // force reconfiguration of effect chains and engines to take new buffer size and audio | 
|  | 1740 | // parameters into account | 
|  | 1741 | // Note that mLock is not held when readOutputParameters() is called from the constructor | 
|  | 1742 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
|  | 1743 | // matter. | 
|  | 1744 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
|  | 1745 | Vector< sp<EffectChain> > effectChains = mEffectChains; | 
|  | 1746 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1747 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1748 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1749 | } | 
|  | 1750 |  | 
|  | 1751 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) | 
|  | 1752 | { | 
|  | 1753 | if (halFrames == 0 || dspFrames == 0) { | 
|  | 1754 | return BAD_VALUE; | 
|  | 1755 | } | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1756 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1757 | if (initCheck() != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1758 | return INVALID_OPERATION; | 
|  | 1759 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1760 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1761 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1762 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1763 | } | 
|  | 1764 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1765 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1766 | { | 
|  | 1767 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1768 | uint32_t result = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1769 | if (getEffectChain_l(sessionId) != 0) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1770 | result = EFFECT_SESSION; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1771 | } | 
|  | 1772 |  | 
|  | 1773 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 1774 | sp<Track> track = mTracks[i]; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1775 | if (sessionId == track->sessionId() && | 
|  | 1776 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1777 | result |= TRACK_SESSION; | 
|  | 1778 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1779 | } | 
|  | 1780 | } | 
|  | 1781 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1782 | return result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1783 | } | 
|  | 1784 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1785 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) | 
|  | 1786 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1787 | // 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] | 1788 | // 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] | 1789 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 1790 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1791 | } | 
|  | 1792 | for (size_t i = 0; i < mTracks.size(); i++) { | 
|  | 1793 | sp<Track> track = mTracks[i]; | 
|  | 1794 | if (sessionId == track->sessionId() && | 
|  | 1795 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1796 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1797 | } | 
|  | 1798 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1799 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1800 | } | 
|  | 1801 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1802 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1803 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() | 
|  | 1804 | { | 
|  | 1805 | Mutex::Autolock _l(mLock); | 
|  | 1806 | return mOutput; | 
|  | 1807 | } | 
|  | 1808 |  | 
|  | 1809 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
|  | 1810 | { | 
|  | 1811 | Mutex::Autolock _l(mLock); | 
|  | 1812 | AudioStreamOut *output = mOutput; | 
|  | 1813 | mOutput = NULL; | 
|  | 1814 | return output; | 
|  | 1815 | } | 
|  | 1816 |  | 
|  | 1817 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 1818 | audio_stream_t* AudioFlinger::PlaybackThread::stream() | 
|  | 1819 | { | 
|  | 1820 | if (mOutput == NULL) { | 
|  | 1821 | return NULL; | 
|  | 1822 | } | 
|  | 1823 | return &mOutput->stream->common; | 
|  | 1824 | } | 
|  | 1825 |  | 
| Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1826 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() | 
|  | 1827 | { | 
|  | 1828 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, | 
|  | 1829 | // decoding and transfer time. So sleeping for half of the latency would likely cause | 
|  | 1830 | // underruns | 
|  | 1831 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { | 
|  | 1832 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 1833 | } else { | 
|  | 1834 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; | 
|  | 1835 | } | 
|  | 1836 | } | 
|  | 1837 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1838 | // ---------------------------------------------------------------------------- | 
|  | 1839 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1840 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1841 | :   PlaybackThread(audioFlinger, output, id, device), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 1842 | mAudioMixer(NULL) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1843 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1844 | mType = ThreadBase::MIXER; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1845 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); | 
|  | 1846 |  | 
|  | 1847 | // FIXME - Current mixer implementation only supports stereo output | 
|  | 1848 | if (mChannelCount == 1) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1849 | ALOGE("Invalid audio hardware channel count"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1850 | } | 
|  | 1851 | } | 
|  | 1852 |  | 
|  | 1853 | AudioFlinger::MixerThread::~MixerThread() | 
|  | 1854 | { | 
|  | 1855 | delete mAudioMixer; | 
|  | 1856 | } | 
|  | 1857 |  | 
|  | 1858 | bool AudioFlinger::MixerThread::threadLoop() | 
|  | 1859 | { | 
|  | 1860 | Vector< sp<Track> > tracksToRemove; | 
|  | 1861 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 1862 | nsecs_t standbyTime = systemTime(); | 
|  | 1863 | size_t mixBufferSize = mFrameCount * mFrameSize; | 
|  | 1864 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 1865 | // Should be reduced to 2x after the vendor fixes the driver issue | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1866 | // increase threshold again due to low power audio mode. The way this warning threshold is | 
|  | 1867 | // calculated and its usefulness should be reconsidered anyway. | 
|  | 1868 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1869 | nsecs_t lastWarning = 0; | 
|  | 1870 | bool longStandbyExit = false; | 
|  | 1871 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 1872 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 1873 | uint32_t sleepTime = idleSleepTime; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1874 | uint32_t sleepTimeShift = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1875 | Vector< sp<EffectChain> > effectChains; | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1876 | #ifdef DEBUG_CPU_USAGE | 
|  | 1877 | ThreadCpuUsage cpu; | 
|  | 1878 | const CentralTendencyStatistics& stats = cpu.statistics(); | 
|  | 1879 | #endif | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1880 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1881 | acquireWakeLock(); | 
|  | 1882 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1883 | while (!exitPending()) | 
|  | 1884 | { | 
| Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1885 | #ifdef DEBUG_CPU_USAGE | 
|  | 1886 | cpu.sampleAndEnable(); | 
|  | 1887 | unsigned n = stats.n(); | 
|  | 1888 | // cpu.elapsed() is expensive, so don't call it every loop | 
|  | 1889 | if ((n & 127) == 1) { | 
|  | 1890 | long long elapsed = cpu.elapsed(); | 
|  | 1891 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
|  | 1892 | double perLoop = elapsed / (double) n; | 
|  | 1893 | double perLoop100 = perLoop * 0.01; | 
|  | 1894 | double mean = stats.mean(); | 
|  | 1895 | double stddev = stats.stddev(); | 
|  | 1896 | double minimum = stats.minimum(); | 
|  | 1897 | double maximum = stats.maximum(); | 
|  | 1898 | cpu.resetStatistics(); | 
| Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1899 | 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] | 1900 | elapsed * .000000001, n, perLoop * .000001, | 
|  | 1901 | mean * .001, | 
|  | 1902 | stddev * .001, | 
|  | 1903 | minimum * .001, | 
|  | 1904 | maximum * .001, | 
|  | 1905 | mean / perLoop100, | 
|  | 1906 | stddev / perLoop100, | 
|  | 1907 | minimum / perLoop100, | 
|  | 1908 | maximum / perLoop100); | 
|  | 1909 | } | 
|  | 1910 | } | 
|  | 1911 | #endif | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1912 | processConfigEvents(); | 
|  | 1913 |  | 
|  | 1914 | mixerStatus = MIXER_IDLE; | 
|  | 1915 | { // scope for mLock | 
|  | 1916 |  | 
|  | 1917 | Mutex::Autolock _l(mLock); | 
|  | 1918 |  | 
|  | 1919 | if (checkForNewParameters_l()) { | 
|  | 1920 | mixBufferSize = mFrameCount * mFrameSize; | 
|  | 1921 | // FIXME: Relaxed timing because of a certain device that can't meet latency | 
|  | 1922 | // Should be reduced to 2x after the vendor fixes the driver issue | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1923 | // increase threshold again due to low power audio mode. The way this warning | 
|  | 1924 | // threshold is calculated and its usefulness should be reconsidered anyway. | 
|  | 1925 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1926 | activeSleepTime = activeSleepTimeUs(); | 
|  | 1927 | idleSleepTime = idleSleepTimeUs(); | 
|  | 1928 | } | 
|  | 1929 |  | 
|  | 1930 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; | 
|  | 1931 |  | 
|  | 1932 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1933 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || | 
|  | 1934 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1935 | if (!mStandby) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1936 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1937 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1938 | mStandby = true; | 
|  | 1939 | mBytesWritten = 0; | 
|  | 1940 | } | 
|  | 1941 |  | 
|  | 1942 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 1943 | // we're about to wait, flush the binder command buffer | 
|  | 1944 | IPCThreadState::self()->flushCommands(); | 
|  | 1945 |  | 
|  | 1946 | if (exitPending()) break; | 
|  | 1947 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1948 | releaseWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1949 | // wait until we have something to do... | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1950 | ALOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1951 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1952 | ALOGV("MixerThread %p TID %d waking up\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1953 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1954 |  | 
|  | 1955 | if (mMasterMute == false) { | 
|  | 1956 | char value[PROPERTY_VALUE_MAX]; | 
|  | 1957 | property_get("ro.audio.silent", value, "0"); | 
|  | 1958 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1959 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1960 | setMasterMute(true); | 
|  | 1961 | } | 
|  | 1962 | } | 
|  | 1963 |  | 
|  | 1964 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 1965 | sleepTime = idleSleepTime; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1966 | sleepTimeShift = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1967 | continue; | 
|  | 1968 | } | 
|  | 1969 | } | 
|  | 1970 |  | 
|  | 1971 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); | 
|  | 1972 |  | 
|  | 1973 | // prevent any changes in effect chain list and in each effect chain | 
|  | 1974 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 1975 | // or modified if an effect is created or deleted | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1976 | lockEffectChains_l(effectChains); | 
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 1977 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1978 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1979 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1980 | // mix buffers... | 
|  | 1981 | mAudioMixer->process(); | 
|  | 1982 | sleepTime = 0; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1983 | // increase sleep time progressively when application underrun condition clears | 
|  | 1984 | if (sleepTimeShift > 0) { | 
|  | 1985 | sleepTimeShift--; | 
|  | 1986 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1987 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 1988 | //TODO: delay standby when effects have a tail | 
|  | 1989 | } else { | 
|  | 1990 | // If no tracks are ready, sleep once for the duration of an output | 
|  | 1991 | // buffer size, then write 0s to the output | 
|  | 1992 | if (sleepTime == 0) { | 
|  | 1993 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1994 | sleepTime = activeSleepTime >> sleepTimeShift; | 
|  | 1995 | if (sleepTime < kMinThreadSleepTimeUs) { | 
|  | 1996 | sleepTime = kMinThreadSleepTimeUs; | 
|  | 1997 | } | 
|  | 1998 | // reduce sleep time in case of consecutive application underruns to avoid | 
|  | 1999 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
|  | 2000 | // duration we would end up writing less data than needed by the audio HAL if | 
|  | 2001 | // the condition persists. | 
|  | 2002 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
|  | 2003 | sleepTimeShift++; | 
|  | 2004 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2005 | } else { | 
|  | 2006 | sleepTime = idleSleepTime; | 
|  | 2007 | } | 
|  | 2008 | } else if (mBytesWritten != 0 || | 
|  | 2009 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { | 
|  | 2010 | memset (mMixBuffer, 0, mixBufferSize); | 
|  | 2011 | sleepTime = 0; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2012 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2013 | } | 
|  | 2014 | // TODO add standby time extension fct of effect tail | 
|  | 2015 | } | 
|  | 2016 |  | 
|  | 2017 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2018 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2019 | } | 
|  | 2020 | // sleepTime == 0 means we must write to audio hardware | 
|  | 2021 | if (sleepTime == 0) { | 
| Glenn Kasten | c5ac4cb | 2011-12-12 09:05:55 -0800 | [diff] [blame] | 2022 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2023 | effectChains[i]->process_l(); | 
|  | 2024 | } | 
|  | 2025 | // enable changes in effect chain | 
|  | 2026 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2027 | mLastWriteTime = systemTime(); | 
|  | 2028 | mInWrite = true; | 
|  | 2029 | mBytesWritten += mixBufferSize; | 
|  | 2030 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2031 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2032 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; | 
|  | 2033 | mNumWrites++; | 
|  | 2034 | mInWrite = false; | 
|  | 2035 | nsecs_t now = systemTime(); | 
|  | 2036 | nsecs_t delta = now - mLastWriteTime; | 
| Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2037 | if (!mStandby && delta > maxPeriod) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2038 | mNumDelayedWrites++; | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 2039 | if ((now - lastWarning) > kWarningThrottleNs) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2040 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2041 | ns2ms(delta), mNumDelayedWrites, this); | 
|  | 2042 | lastWarning = now; | 
|  | 2043 | } | 
|  | 2044 | if (mStandby) { | 
|  | 2045 | longStandbyExit = true; | 
|  | 2046 | } | 
|  | 2047 | } | 
|  | 2048 | mStandby = false; | 
|  | 2049 | } else { | 
|  | 2050 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2051 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2052 | usleep(sleepTime); | 
|  | 2053 | } | 
|  | 2054 |  | 
|  | 2055 | // finally let go of all our tracks, without the lock held | 
|  | 2056 | // since we can't guarantee the destructors won't acquire that | 
|  | 2057 | // same lock. | 
|  | 2058 | tracksToRemove.clear(); | 
|  | 2059 |  | 
|  | 2060 | // Effect chains will be actually deleted here if they were removed from | 
|  | 2061 | // mEffectChains list during mixing or effects processing | 
|  | 2062 | effectChains.clear(); | 
|  | 2063 | } | 
|  | 2064 |  | 
|  | 2065 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2066 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2067 | } | 
|  | 2068 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2069 | releaseWakeLock(); | 
|  | 2070 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2071 | ALOGV("MixerThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2072 | return false; | 
|  | 2073 | } | 
|  | 2074 |  | 
|  | 2075 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
|  | 2076 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) | 
|  | 2077 | { | 
|  | 2078 |  | 
|  | 2079 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 2080 | // find out which tracks need to be processed | 
|  | 2081 | size_t count = activeTracks.size(); | 
|  | 2082 | size_t mixedTracks = 0; | 
|  | 2083 | size_t tracksWithEffect = 0; | 
|  | 2084 |  | 
|  | 2085 | float masterVolume = mMasterVolume; | 
|  | 2086 | bool  masterMute = mMasterMute; | 
|  | 2087 |  | 
| Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2088 | if (masterMute) { | 
|  | 2089 | masterVolume = 0; | 
|  | 2090 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2091 | // 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] | 2092 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2093 | if (chain != 0) { | 
| Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2094 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2095 | chain->setVolume_l(&v, &v); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2096 | masterVolume = (float)((v + (1 << 23)) >> 24); | 
|  | 2097 | chain.clear(); | 
|  | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2101 | sp<Track> t = activeTracks[i].promote(); | 
|  | 2102 | if (t == 0) continue; | 
|  | 2103 |  | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2104 | // this const just means the local variable doesn't change | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2105 | Track* const track = t.get(); | 
|  | 2106 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 2107 |  | 
|  | 2108 | // The first time a track is added we wait | 
|  | 2109 | // for all its buffers to be filled before processing it | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2110 | int name = track->name(); | 
| Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2111 | // make sure that we have enough frames to mix one full buffer. | 
|  | 2112 | // enforce this condition only once to enable draining the buffer in case the client | 
|  | 2113 | // app does not call stop() and relies on underrun to stop: | 
|  | 2114 | // hence the test on (track->mRetryCount >= kMaxTrackRetries) meaning the track was mixed | 
|  | 2115 | // during last round | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2116 | uint32_t minFrames = 1; | 
| Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2117 | if (!track->isStopped() && !track->isPausing() && | 
|  | 2118 | (track->mRetryCount >= kMaxTrackRetries)) { | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2119 | if (t->sampleRate() == (int)mSampleRate) { | 
|  | 2120 | minFrames = mFrameCount; | 
|  | 2121 | } else { | 
| Eric Laurent | 071ccd5 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2122 | // +1 for rounding and +1 for additional sample needed for interpolation | 
|  | 2123 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; | 
|  | 2124 | // add frames already consumed but not yet released by the resampler | 
|  | 2125 | // because cblk->framesReady() will  include these frames | 
|  | 2126 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
|  | 2127 | // the minimum track buffer size is normally twice the number of frames necessary | 
|  | 2128 | // to fill one buffer and the resampler should not leave more than one buffer worth | 
|  | 2129 | // of unreleased frames after each pass, but just in case... | 
| Steve Block | c1dc1cb | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2130 | ALOG_ASSERT(minFrames <= cblk->frameCount); | 
| Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2131 | } | 
|  | 2132 | } | 
|  | 2133 | if ((cblk->framesReady() >= minFrames) && track->isReady() && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2134 | !track->isPaused() && !track->isTerminated()) | 
|  | 2135 | { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2136 | //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] | 2137 |  | 
|  | 2138 | mixedTracks++; | 
|  | 2139 |  | 
|  | 2140 | // track->mainBuffer() != mMixBuffer means there is an effect chain | 
|  | 2141 | // connected to the track | 
|  | 2142 | chain.clear(); | 
|  | 2143 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 2144 | chain = getEffectChain_l(track->sessionId()); | 
|  | 2145 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2146 | if (chain != 0) { | 
|  | 2147 | tracksWithEffect++; | 
|  | 2148 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2149 | 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] | 2150 | name, track->sessionId()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2151 | } | 
|  | 2152 | } | 
|  | 2153 |  | 
|  | 2154 |  | 
|  | 2155 | int param = AudioMixer::VOLUME; | 
|  | 2156 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 2157 | // no ramp for the first volume setting | 
|  | 2158 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 2159 | if (track->mState == TrackBase::RESUMING) { | 
|  | 2160 | track->mState = TrackBase::ACTIVE; | 
|  | 2161 | param = AudioMixer::RAMP_VOLUME; | 
|  | 2162 | } | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2163 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2164 | } else if (cblk->server != 0) { | 
|  | 2165 | // If the track is stopped before the first frame was mixed, | 
|  | 2166 | // do not apply ramp | 
|  | 2167 | param = AudioMixer::RAMP_VOLUME; | 
|  | 2168 | } | 
|  | 2169 |  | 
|  | 2170 | // compute volume for this track | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2171 | uint32_t vl, vr, va; | 
| Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2172 | if (track->isMuted() || track->isPausing() || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2173 | mStreamTypes[track->type()].mute) { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2174 | vl = vr = va = 0; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2175 | if (track->isPausing()) { | 
|  | 2176 | track->setPaused(); | 
|  | 2177 | } | 
|  | 2178 | } else { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2179 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2180 | // read original volumes with volume control | 
|  | 2181 | float typeVolume = mStreamTypes[track->type()].volume; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2182 | float v = masterVolume * typeVolume; | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2183 | vl = (uint32_t)(v * cblk->volume[0]) << 12; | 
|  | 2184 | vr = (uint32_t)(v * cblk->volume[1]) << 12; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2185 |  | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2186 | va = (uint32_t)(v * cblk->sendLevel); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2187 | } | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2188 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2189 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
|  | 2190 | // Do not ramp volume if volume is controlled by effect | 
|  | 2191 | param = AudioMixer::VOLUME; | 
|  | 2192 | track->mHasVolumeController = true; | 
|  | 2193 | } else { | 
|  | 2194 | // force no volume ramp when volume controller was just disabled or removed | 
|  | 2195 | // from effect chain to avoid volume spike | 
|  | 2196 | if (track->mHasVolumeController) { | 
|  | 2197 | param = AudioMixer::VOLUME; | 
|  | 2198 | } | 
|  | 2199 | track->mHasVolumeController = false; | 
|  | 2200 | } | 
|  | 2201 |  | 
|  | 2202 | // Convert volumes from 8.24 to 4.12 format | 
|  | 2203 | int16_t left, right, aux; | 
|  | 2204 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; | 
|  | 2205 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2206 | left = int16_t(v_clamped); | 
|  | 2207 | v_clamped = (vr + (1 << 11)) >> 12; | 
|  | 2208 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2209 | right = int16_t(v_clamped); | 
|  | 2210 |  | 
|  | 2211 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; | 
|  | 2212 | aux = int16_t(va); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2213 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2214 | // XXX: these things DON'T need to be done each time | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2215 | mAudioMixer->setBufferProvider(name, track); | 
|  | 2216 | mAudioMixer->enable(name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2217 |  | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2218 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)left); | 
|  | 2219 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)right); | 
|  | 2220 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)aux); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2221 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2222 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2223 | AudioMixer::TRACK, | 
|  | 2224 | AudioMixer::FORMAT, (void *)track->format()); | 
|  | 2225 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2226 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2227 | AudioMixer::TRACK, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2228 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2229 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2230 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2231 | AudioMixer::RESAMPLE, | 
|  | 2232 | AudioMixer::SAMPLE_RATE, | 
|  | 2233 | (void *)(cblk->sampleRate)); | 
|  | 2234 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2235 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2236 | AudioMixer::TRACK, | 
|  | 2237 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
|  | 2238 | mAudioMixer->setParameter( | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2239 | name, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2240 | AudioMixer::TRACK, | 
|  | 2241 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
|  | 2242 |  | 
|  | 2243 | // reset retry count | 
|  | 2244 | track->mRetryCount = kMaxTrackRetries; | 
|  | 2245 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 2246 | } else { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2247 | //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] | 2248 | if (track->isStopped()) { | 
|  | 2249 | track->reset(); | 
|  | 2250 | } | 
|  | 2251 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { | 
|  | 2252 | // We have consumed all the buffers of this track. | 
|  | 2253 | // Remove it from the list of active tracks. | 
|  | 2254 | tracksToRemove->add(track); | 
|  | 2255 | } else { | 
|  | 2256 | // No buffers for this track. Give it a few chances to | 
|  | 2257 | // fill a buffer, then remove it from active list. | 
|  | 2258 | if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2259 | 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] | 2260 | tracksToRemove->add(track); | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2261 | // indicate to client process that the track was disabled because of underrun | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2262 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2263 | } else if (mixerStatus != MIXER_TRACKS_READY) { | 
|  | 2264 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 2265 | } | 
|  | 2266 | } | 
| Glenn Kasten | 9c56d4a | 2011-12-19 15:06:39 -0800 | [diff] [blame] | 2267 | mAudioMixer->disable(name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2268 | } | 
|  | 2269 | } | 
|  | 2270 |  | 
|  | 2271 | // remove all the tracks that need to be... | 
|  | 2272 | count = tracksToRemove->size(); | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2273 | if (CC_UNLIKELY(count)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2274 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2275 | const sp<Track>& track = tracksToRemove->itemAt(i); | 
|  | 2276 | mActiveTracks.remove(track); | 
|  | 2277 | if (track->mainBuffer() != mMixBuffer) { | 
|  | 2278 | chain = getEffectChain_l(track->sessionId()); | 
|  | 2279 | if (chain != 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2280 | 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] | 2281 | chain->decActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2282 | } | 
|  | 2283 | } | 
|  | 2284 | if (track->isTerminated()) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2285 | removeTrack_l(track); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2286 | } | 
|  | 2287 | } | 
|  | 2288 | } | 
|  | 2289 |  | 
|  | 2290 | // mix buffer must be cleared if all tracks are connected to an | 
|  | 2291 | // effect chain as in this case the mixer will not write to | 
|  | 2292 | // mix buffer and track effects will accumulate into it | 
|  | 2293 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { | 
|  | 2294 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); | 
|  | 2295 | } | 
|  | 2296 |  | 
|  | 2297 | return mixerStatus; | 
|  | 2298 | } | 
|  | 2299 |  | 
|  | 2300 | void AudioFlinger::MixerThread::invalidateTracks(int streamType) | 
|  | 2301 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2302 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2303 | this,  streamType, mTracks.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2304 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2305 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2306 | size_t size = mTracks.size(); | 
|  | 2307 | for (size_t i = 0; i < size; i++) { | 
|  | 2308 | sp<Track> t = mTracks[i]; | 
|  | 2309 | if (t->type() == streamType) { | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2310 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2311 | t->mCblk->cv.signal(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2312 | } | 
|  | 2313 | } | 
|  | 2314 | } | 
|  | 2315 |  | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2316 | void AudioFlinger::PlaybackThread::setStreamValid(int streamType, bool valid) | 
|  | 2317 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2318 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2319 | this,  streamType, valid); | 
|  | 2320 | Mutex::Autolock _l(mLock); | 
|  | 2321 |  | 
|  | 2322 | mStreamTypes[streamType].valid = valid; | 
|  | 2323 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2324 |  | 
|  | 2325 | // getTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2326 | int AudioFlinger::MixerThread::getTrackName_l() | 
|  | 2327 | { | 
|  | 2328 | return mAudioMixer->getTrackName(); | 
|  | 2329 | } | 
|  | 2330 |  | 
|  | 2331 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2332 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
|  | 2333 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2334 | ALOGV("remove track (%d) and delete from mixer", name); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2335 | mAudioMixer->deleteTrackName(name); | 
|  | 2336 | } | 
|  | 2337 |  | 
|  | 2338 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
|  | 2339 | bool AudioFlinger::MixerThread::checkForNewParameters_l() | 
|  | 2340 | { | 
|  | 2341 | bool reconfig = false; | 
|  | 2342 |  | 
|  | 2343 | while (!mNewParameters.isEmpty()) { | 
|  | 2344 | status_t status = NO_ERROR; | 
|  | 2345 | String8 keyValuePair = mNewParameters[0]; | 
|  | 2346 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 2347 | int value; | 
|  | 2348 |  | 
|  | 2349 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 2350 | reconfig = true; | 
|  | 2351 | } | 
|  | 2352 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2353 | if (value != AUDIO_FORMAT_PCM_16_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2354 | status = BAD_VALUE; | 
|  | 2355 | } else { | 
|  | 2356 | reconfig = true; | 
|  | 2357 | } | 
|  | 2358 | } | 
|  | 2359 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2360 | if (value != AUDIO_CHANNEL_OUT_STEREO) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2361 | status = BAD_VALUE; | 
|  | 2362 | } else { | 
|  | 2363 | reconfig = true; | 
|  | 2364 | } | 
|  | 2365 | } | 
|  | 2366 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 2367 | // 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] | 2368 | // size depends on frame count and correct behavior would not be guaranteed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2369 | // if frame count is changed after track creation | 
|  | 2370 | if (!mTracks.isEmpty()) { | 
|  | 2371 | status = INVALID_OPERATION; | 
|  | 2372 | } else { | 
|  | 2373 | reconfig = true; | 
|  | 2374 | } | 
|  | 2375 | } | 
|  | 2376 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2377 | // when changing the audio output device, call addBatteryData to notify | 
|  | 2378 | // the change | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2379 | if ((int)mDevice != value) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2380 | uint32_t params = 0; | 
|  | 2381 | // check whether speaker is on | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2382 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2383 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
|  | 2384 | } | 
|  | 2385 |  | 
|  | 2386 | int deviceWithoutSpeaker | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2387 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2388 | // check if any other device (except speaker) is on | 
|  | 2389 | if (value & deviceWithoutSpeaker ) { | 
|  | 2390 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
|  | 2391 | } | 
|  | 2392 |  | 
|  | 2393 | if (params != 0) { | 
|  | 2394 | addBatteryData(params); | 
|  | 2395 | } | 
|  | 2396 | } | 
|  | 2397 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2398 | // forward device change to effects that have requested to be | 
|  | 2399 | // aware of attached audio device. | 
|  | 2400 | mDevice = (uint32_t)value; | 
|  | 2401 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2402 | mEffectChains[i]->setDevice_l(mDevice); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2403 | } | 
|  | 2404 | } | 
|  | 2405 |  | 
|  | 2406 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2407 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2408 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2409 | if (!mStandby && status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2410 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2411 | mStandby = true; | 
|  | 2412 | mBytesWritten = 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2413 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2414 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2415 | } | 
|  | 2416 | if (status == NO_ERROR && reconfig) { | 
|  | 2417 | delete mAudioMixer; | 
|  | 2418 | readOutputParameters(); | 
|  | 2419 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); | 
|  | 2420 | for (size_t i = 0; i < mTracks.size() ; i++) { | 
|  | 2421 | int name = getTrackName_l(); | 
|  | 2422 | if (name < 0) break; | 
|  | 2423 | mTracks[i]->mName = name; | 
|  | 2424 | // limit track sample rate to 2 x new output sample rate | 
|  | 2425 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { | 
|  | 2426 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); | 
|  | 2427 | } | 
|  | 2428 | } | 
|  | 2429 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
|  | 2430 | } | 
|  | 2431 | } | 
|  | 2432 |  | 
|  | 2433 | mNewParameters.removeAt(0); | 
|  | 2434 |  | 
|  | 2435 | mParamStatus = status; | 
|  | 2436 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2437 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 2438 | // 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] | 2439 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2440 | } | 
|  | 2441 | return reconfig; | 
|  | 2442 | } | 
|  | 2443 |  | 
|  | 2444 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
|  | 2445 | { | 
|  | 2446 | const size_t SIZE = 256; | 
|  | 2447 | char buffer[SIZE]; | 
|  | 2448 | String8 result; | 
|  | 2449 |  | 
|  | 2450 | PlaybackThread::dumpInternals(fd, args); | 
|  | 2451 |  | 
|  | 2452 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); | 
|  | 2453 | result.append(buffer); | 
|  | 2454 | write(fd, result.string(), result.size()); | 
|  | 2455 | return NO_ERROR; | 
|  | 2456 | } | 
|  | 2457 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2458 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() | 
|  | 2459 | { | 
| Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2460 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2461 | } | 
|  | 2462 |  | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2463 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() | 
|  | 2464 | { | 
|  | 2465 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2466 | } | 
|  | 2467 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2468 | // ---------------------------------------------------------------------------- | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2469 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2470 | :   PlaybackThread(audioFlinger, output, id, device) | 
|  | 2471 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2472 | mType = ThreadBase::DIRECT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2473 | } | 
|  | 2474 |  | 
|  | 2475 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
|  | 2476 | { | 
|  | 2477 | } | 
|  | 2478 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2479 | static inline | 
|  | 2480 | int32_t mul(int16_t in, int16_t v) | 
|  | 2481 | { | 
|  | 2482 | #if defined(__arm__) && !defined(__thumb__) | 
|  | 2483 | int32_t out; | 
|  | 2484 | asm( "smulbb %[out], %[in], %[v] \n" | 
|  | 2485 | : [out]"=r"(out) | 
|  | 2486 | : [in]"%r"(in), [v]"r"(v) | 
|  | 2487 | : ); | 
|  | 2488 | return out; | 
|  | 2489 | #else | 
|  | 2490 | return in * int32_t(v); | 
|  | 2491 | #endif | 
|  | 2492 | } | 
|  | 2493 |  | 
|  | 2494 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) | 
|  | 2495 | { | 
|  | 2496 | // Do not apply volume on compressed audio | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2497 | if (!audio_is_linear_pcm(mFormat)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2498 | return; | 
|  | 2499 | } | 
|  | 2500 |  | 
|  | 2501 | // convert to signed 16 bit before volume calculation | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2502 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2503 | size_t count = mFrameCount * mChannelCount; | 
|  | 2504 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; | 
|  | 2505 | int16_t *dst = mMixBuffer + count-1; | 
|  | 2506 | while(count--) { | 
|  | 2507 | *dst-- = (int16_t)(*src--^0x80) << 8; | 
|  | 2508 | } | 
|  | 2509 | } | 
|  | 2510 |  | 
|  | 2511 | size_t frameCount = mFrameCount; | 
|  | 2512 | int16_t *out = mMixBuffer; | 
|  | 2513 | if (ramp) { | 
|  | 2514 | if (mChannelCount == 1) { | 
|  | 2515 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; | 
|  | 2516 | int32_t vlInc = d / (int32_t)frameCount; | 
|  | 2517 | int32_t vl = ((int32_t)mLeftVolShort << 16); | 
|  | 2518 | do { | 
|  | 2519 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); | 
|  | 2520 | out++; | 
|  | 2521 | vl += vlInc; | 
|  | 2522 | } while (--frameCount); | 
|  | 2523 |  | 
|  | 2524 | } else { | 
|  | 2525 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; | 
|  | 2526 | int32_t vlInc = d / (int32_t)frameCount; | 
|  | 2527 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; | 
|  | 2528 | int32_t vrInc = d / (int32_t)frameCount; | 
|  | 2529 | int32_t vl = ((int32_t)mLeftVolShort << 16); | 
|  | 2530 | int32_t vr = ((int32_t)mRightVolShort << 16); | 
|  | 2531 | do { | 
|  | 2532 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); | 
|  | 2533 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); | 
|  | 2534 | out += 2; | 
|  | 2535 | vl += vlInc; | 
|  | 2536 | vr += vrInc; | 
|  | 2537 | } while (--frameCount); | 
|  | 2538 | } | 
|  | 2539 | } else { | 
|  | 2540 | if (mChannelCount == 1) { | 
|  | 2541 | do { | 
|  | 2542 | out[0] = clamp16(mul(out[0], leftVol) >> 12); | 
|  | 2543 | out++; | 
|  | 2544 | } while (--frameCount); | 
|  | 2545 | } else { | 
|  | 2546 | do { | 
|  | 2547 | out[0] = clamp16(mul(out[0], leftVol) >> 12); | 
|  | 2548 | out[1] = clamp16(mul(out[1], rightVol) >> 12); | 
|  | 2549 | out += 2; | 
|  | 2550 | } while (--frameCount); | 
|  | 2551 | } | 
|  | 2552 | } | 
|  | 2553 |  | 
|  | 2554 | // convert back to unsigned 8 bit after volume calculation | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2555 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2556 | size_t count = mFrameCount * mChannelCount; | 
|  | 2557 | int16_t *src = mMixBuffer; | 
|  | 2558 | uint8_t *dst = (uint8_t *)mMixBuffer; | 
|  | 2559 | while(count--) { | 
|  | 2560 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; | 
|  | 2561 | } | 
|  | 2562 | } | 
|  | 2563 |  | 
|  | 2564 | mLeftVolShort = leftVol; | 
|  | 2565 | mRightVolShort = rightVol; | 
|  | 2566 | } | 
|  | 2567 |  | 
|  | 2568 | bool AudioFlinger::DirectOutputThread::threadLoop() | 
|  | 2569 | { | 
|  | 2570 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 2571 | sp<Track> trackToRemove; | 
|  | 2572 | sp<Track> activeTrack; | 
|  | 2573 | nsecs_t standbyTime = systemTime(); | 
|  | 2574 | int8_t *curBuf; | 
|  | 2575 | size_t mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2576 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 2577 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 2578 | uint32_t sleepTime = idleSleepTime; | 
|  | 2579 | // use shorter standby delay as on normal output to release | 
|  | 2580 | // hardware resources as soon as possible | 
|  | 2581 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); | 
|  | 2582 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2583 | acquireWakeLock(); | 
|  | 2584 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2585 | while (!exitPending()) | 
|  | 2586 | { | 
|  | 2587 | bool rampVolume; | 
|  | 2588 | uint16_t leftVol; | 
|  | 2589 | uint16_t rightVol; | 
|  | 2590 | Vector< sp<EffectChain> > effectChains; | 
|  | 2591 |  | 
|  | 2592 | processConfigEvents(); | 
|  | 2593 |  | 
|  | 2594 | mixerStatus = MIXER_IDLE; | 
|  | 2595 |  | 
|  | 2596 | { // scope for the mLock | 
|  | 2597 |  | 
|  | 2598 | Mutex::Autolock _l(mLock); | 
|  | 2599 |  | 
|  | 2600 | if (checkForNewParameters_l()) { | 
|  | 2601 | mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2602 | activeSleepTime = activeSleepTimeUs(); | 
|  | 2603 | idleSleepTime = idleSleepTimeUs(); | 
|  | 2604 | standbyDelay = microseconds(activeSleepTime*2); | 
|  | 2605 | } | 
|  | 2606 |  | 
|  | 2607 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2608 | if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || | 
|  | 2609 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2610 | // wait until we have something to do... | 
|  | 2611 | if (!mStandby) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2612 | ALOGV("Audio hardware entering standby, mixer %p\n", this); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2613 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2614 | mStandby = true; | 
|  | 2615 | mBytesWritten = 0; | 
|  | 2616 | } | 
|  | 2617 |  | 
|  | 2618 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 2619 | // we're about to wait, flush the binder command buffer | 
|  | 2620 | IPCThreadState::self()->flushCommands(); | 
|  | 2621 |  | 
|  | 2622 | if (exitPending()) break; | 
|  | 2623 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2624 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2625 | ALOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2626 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2627 | ALOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2628 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2629 |  | 
|  | 2630 | if (mMasterMute == false) { | 
|  | 2631 | char value[PROPERTY_VALUE_MAX]; | 
|  | 2632 | property_get("ro.audio.silent", value, "0"); | 
|  | 2633 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2634 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2635 | setMasterMute(true); | 
|  | 2636 | } | 
|  | 2637 | } | 
|  | 2638 |  | 
|  | 2639 | standbyTime = systemTime() + standbyDelay; | 
|  | 2640 | sleepTime = idleSleepTime; | 
|  | 2641 | continue; | 
|  | 2642 | } | 
|  | 2643 | } | 
|  | 2644 |  | 
|  | 2645 | effectChains = mEffectChains; | 
|  | 2646 |  | 
|  | 2647 | // find out which tracks need to be processed | 
|  | 2648 | if (mActiveTracks.size() != 0) { | 
|  | 2649 | sp<Track> t = mActiveTracks[0].promote(); | 
|  | 2650 | if (t == 0) continue; | 
|  | 2651 |  | 
|  | 2652 | Track* const track = t.get(); | 
|  | 2653 | audio_track_cblk_t* cblk = track->cblk(); | 
|  | 2654 |  | 
|  | 2655 | // The first time a track is added we wait | 
|  | 2656 | // for all its buffers to be filled before processing it | 
| Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2657 | if (cblk->framesReady() && track->isReady() && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2658 | !track->isPaused() && !track->isTerminated()) | 
|  | 2659 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2660 | //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] | 2661 |  | 
|  | 2662 | if (track->mFillingUpStatus == Track::FS_FILLED) { | 
|  | 2663 | track->mFillingUpStatus = Track::FS_ACTIVE; | 
|  | 2664 | mLeftVolFloat = mRightVolFloat = 0; | 
|  | 2665 | mLeftVolShort = mRightVolShort = 0; | 
|  | 2666 | if (track->mState == TrackBase::RESUMING) { | 
|  | 2667 | track->mState = TrackBase::ACTIVE; | 
|  | 2668 | rampVolume = true; | 
|  | 2669 | } | 
|  | 2670 | } else if (cblk->server != 0) { | 
|  | 2671 | // If the track is stopped before the first frame was mixed, | 
|  | 2672 | // do not apply ramp | 
|  | 2673 | rampVolume = true; | 
|  | 2674 | } | 
|  | 2675 | // compute volume for this track | 
|  | 2676 | float left, right; | 
|  | 2677 | if (track->isMuted() || mMasterMute || track->isPausing() || | 
|  | 2678 | mStreamTypes[track->type()].mute) { | 
|  | 2679 | left = right = 0; | 
|  | 2680 | if (track->isPausing()) { | 
|  | 2681 | track->setPaused(); | 
|  | 2682 | } | 
|  | 2683 | } else { | 
|  | 2684 | float typeVolume = mStreamTypes[track->type()].volume; | 
|  | 2685 | float v = mMasterVolume * typeVolume; | 
|  | 2686 | float v_clamped = v * cblk->volume[0]; | 
|  | 2687 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; | 
|  | 2688 | left = v_clamped/MAX_GAIN; | 
|  | 2689 | v_clamped = v * cblk->volume[1]; | 
|  | 2690 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; | 
|  | 2691 | right = v_clamped/MAX_GAIN; | 
|  | 2692 | } | 
|  | 2693 |  | 
|  | 2694 | if (left != mLeftVolFloat || right != mRightVolFloat) { | 
|  | 2695 | mLeftVolFloat = left; | 
|  | 2696 | mRightVolFloat = right; | 
|  | 2697 |  | 
|  | 2698 | // If audio HAL implements volume control, | 
|  | 2699 | // force software volume to nominal value | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2700 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2701 | left = 1.0f; | 
|  | 2702 | right = 1.0f; | 
|  | 2703 | } | 
|  | 2704 |  | 
|  | 2705 | // Convert volumes from float to 8.24 | 
|  | 2706 | uint32_t vl = (uint32_t)(left * (1 << 24)); | 
|  | 2707 | uint32_t vr = (uint32_t)(right * (1 << 24)); | 
|  | 2708 |  | 
|  | 2709 | // Delegate volume control to effect in track effect chain if needed | 
|  | 2710 | // only one effect chain can be present on DirectOutputThread, so if | 
|  | 2711 | // there is one, the track is connected to it | 
|  | 2712 | if (!effectChains.isEmpty()) { | 
| Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2713 | // Do not ramp volume if volume is controlled by effect | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2714 | if(effectChains[0]->setVolume_l(&vl, &vr)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2715 | rampVolume = false; | 
|  | 2716 | } | 
|  | 2717 | } | 
|  | 2718 |  | 
|  | 2719 | // Convert volumes from 8.24 to 4.12 format | 
|  | 2720 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; | 
|  | 2721 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2722 | leftVol = (uint16_t)v_clamped; | 
|  | 2723 | v_clamped = (vr + (1 << 11)) >> 12; | 
|  | 2724 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; | 
|  | 2725 | rightVol = (uint16_t)v_clamped; | 
|  | 2726 | } else { | 
|  | 2727 | leftVol = mLeftVolShort; | 
|  | 2728 | rightVol = mRightVolShort; | 
|  | 2729 | rampVolume = false; | 
|  | 2730 | } | 
|  | 2731 |  | 
|  | 2732 | // reset retry count | 
|  | 2733 | track->mRetryCount = kMaxTrackRetriesDirect; | 
|  | 2734 | activeTrack = t; | 
|  | 2735 | mixerStatus = MIXER_TRACKS_READY; | 
|  | 2736 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2737 | //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] | 2738 | if (track->isStopped()) { | 
|  | 2739 | track->reset(); | 
|  | 2740 | } | 
|  | 2741 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { | 
|  | 2742 | // We have consumed all the buffers of this track. | 
|  | 2743 | // Remove it from the list of active tracks. | 
|  | 2744 | trackToRemove = track; | 
|  | 2745 | } else { | 
|  | 2746 | // No buffers for this track. Give it a few chances to | 
|  | 2747 | // fill a buffer, then remove it from active list. | 
|  | 2748 | if (--(track->mRetryCount) <= 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2749 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2750 | trackToRemove = track; | 
|  | 2751 | } else { | 
|  | 2752 | mixerStatus = MIXER_TRACKS_ENABLED; | 
|  | 2753 | } | 
|  | 2754 | } | 
|  | 2755 | } | 
|  | 2756 | } | 
|  | 2757 |  | 
|  | 2758 | // remove all the tracks that need to be... | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2759 | if (CC_UNLIKELY(trackToRemove != 0)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2760 | mActiveTracks.remove(trackToRemove); | 
|  | 2761 | if (!effectChains.isEmpty()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2762 | 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] | 2763 | trackToRemove->sessionId()); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2764 | effectChains[0]->decActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2765 | } | 
|  | 2766 | if (trackToRemove->isTerminated()) { | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2767 | removeTrack_l(trackToRemove); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2768 | } | 
|  | 2769 | } | 
|  | 2770 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2771 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2772 | } | 
|  | 2773 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2774 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2775 | AudioBufferProvider::Buffer buffer; | 
|  | 2776 | size_t frameCount = mFrameCount; | 
|  | 2777 | curBuf = (int8_t *)mMixBuffer; | 
|  | 2778 | // output audio to hardware | 
|  | 2779 | while (frameCount) { | 
|  | 2780 | buffer.frameCount = frameCount; | 
|  | 2781 | activeTrack->getNextBuffer(&buffer); | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2782 | if (CC_UNLIKELY(buffer.raw == NULL)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2783 | memset(curBuf, 0, frameCount * mFrameSize); | 
|  | 2784 | break; | 
|  | 2785 | } | 
|  | 2786 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
|  | 2787 | frameCount -= buffer.frameCount; | 
|  | 2788 | curBuf += buffer.frameCount * mFrameSize; | 
|  | 2789 | activeTrack->releaseBuffer(&buffer); | 
|  | 2790 | } | 
|  | 2791 | sleepTime = 0; | 
|  | 2792 | standbyTime = systemTime() + standbyDelay; | 
|  | 2793 | } else { | 
|  | 2794 | if (sleepTime == 0) { | 
|  | 2795 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 2796 | sleepTime = activeSleepTime; | 
|  | 2797 | } else { | 
|  | 2798 | sleepTime = idleSleepTime; | 
|  | 2799 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2800 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2801 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); | 
|  | 2802 | sleepTime = 0; | 
|  | 2803 | } | 
|  | 2804 | } | 
|  | 2805 |  | 
|  | 2806 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2807 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2808 | } | 
|  | 2809 | // sleepTime == 0 means we must write to audio hardware | 
|  | 2810 | if (sleepTime == 0) { | 
|  | 2811 | if (mixerStatus == MIXER_TRACKS_READY) { | 
|  | 2812 | applyVolume(leftVol, rightVol, rampVolume); | 
|  | 2813 | } | 
|  | 2814 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 2815 | effectChains[i]->process_l(); | 
|  | 2816 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2817 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2818 |  | 
|  | 2819 | mLastWriteTime = systemTime(); | 
|  | 2820 | mInWrite = true; | 
|  | 2821 | mBytesWritten += mixBufferSize; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2822 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2823 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; | 
|  | 2824 | mNumWrites++; | 
|  | 2825 | mInWrite = false; | 
|  | 2826 | mStandby = false; | 
|  | 2827 | } else { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2828 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2829 | usleep(sleepTime); | 
|  | 2830 | } | 
|  | 2831 |  | 
|  | 2832 | // finally let go of removed track, without the lock held | 
|  | 2833 | // since we can't guarantee the destructors won't acquire that | 
|  | 2834 | // same lock. | 
|  | 2835 | trackToRemove.clear(); | 
|  | 2836 | activeTrack.clear(); | 
|  | 2837 |  | 
|  | 2838 | // Effect chains will be actually deleted here if they were removed from | 
|  | 2839 | // mEffectChains list during mixing or effects processing | 
|  | 2840 | effectChains.clear(); | 
|  | 2841 | } | 
|  | 2842 |  | 
|  | 2843 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2844 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2845 | } | 
|  | 2846 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2847 | releaseWakeLock(); | 
|  | 2848 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2849 | ALOGV("DirectOutputThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2850 | return false; | 
|  | 2851 | } | 
|  | 2852 |  | 
|  | 2853 | // getTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2854 | int AudioFlinger::DirectOutputThread::getTrackName_l() | 
|  | 2855 | { | 
|  | 2856 | return 0; | 
|  | 2857 | } | 
|  | 2858 |  | 
|  | 2859 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
|  | 2860 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) | 
|  | 2861 | { | 
|  | 2862 | } | 
|  | 2863 |  | 
|  | 2864 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
|  | 2865 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() | 
|  | 2866 | { | 
|  | 2867 | bool reconfig = false; | 
|  | 2868 |  | 
|  | 2869 | while (!mNewParameters.isEmpty()) { | 
|  | 2870 | status_t status = NO_ERROR; | 
|  | 2871 | String8 keyValuePair = mNewParameters[0]; | 
|  | 2872 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 2873 | int value; | 
|  | 2874 |  | 
|  | 2875 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 2876 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 2877 | // size depends on frame count and correct behavior would not be garantied | 
|  | 2878 | // if frame count is changed after track creation | 
|  | 2879 | if (!mTracks.isEmpty()) { | 
|  | 2880 | status = INVALID_OPERATION; | 
|  | 2881 | } else { | 
|  | 2882 | reconfig = true; | 
|  | 2883 | } | 
|  | 2884 | } | 
|  | 2885 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2886 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2887 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2888 | if (!mStandby && status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2889 | mOutput->stream->common.standby(&mOutput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2890 | mStandby = true; | 
|  | 2891 | mBytesWritten = 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2892 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2893 | keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2894 | } | 
|  | 2895 | if (status == NO_ERROR && reconfig) { | 
|  | 2896 | readOutputParameters(); | 
|  | 2897 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
|  | 2898 | } | 
|  | 2899 | } | 
|  | 2900 |  | 
|  | 2901 | mNewParameters.removeAt(0); | 
|  | 2902 |  | 
|  | 2903 | mParamStatus = status; | 
|  | 2904 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2905 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 2906 | // 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] | 2907 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2908 | } | 
|  | 2909 | return reconfig; | 
|  | 2910 | } | 
|  | 2911 |  | 
|  | 2912 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() | 
|  | 2913 | { | 
|  | 2914 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2915 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2916 | time = PlaybackThread::activeSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2917 | } else { | 
|  | 2918 | time = 10000; | 
|  | 2919 | } | 
|  | 2920 | return time; | 
|  | 2921 | } | 
|  | 2922 |  | 
|  | 2923 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() | 
|  | 2924 | { | 
|  | 2925 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2926 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2927 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2928 | } else { | 
|  | 2929 | time = 10000; | 
|  | 2930 | } | 
|  | 2931 | return time; | 
|  | 2932 | } | 
|  | 2933 |  | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2934 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() | 
|  | 2935 | { | 
|  | 2936 | uint32_t time; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2937 | if (audio_is_linear_pcm(mFormat)) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2938 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
|  | 2939 | } else { | 
|  | 2940 | time = 10000; | 
|  | 2941 | } | 
|  | 2942 | return time; | 
|  | 2943 | } | 
|  | 2944 |  | 
|  | 2945 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2946 | // ---------------------------------------------------------------------------- | 
|  | 2947 |  | 
|  | 2948 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) | 
|  | 2949 | :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) | 
|  | 2950 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2951 | mType = ThreadBase::DUPLICATING; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2952 | addOutputTrack(mainThread); | 
|  | 2953 | } | 
|  | 2954 |  | 
|  | 2955 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
|  | 2956 | { | 
|  | 2957 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 2958 | mOutputTracks[i]->destroy(); | 
|  | 2959 | } | 
|  | 2960 | mOutputTracks.clear(); | 
|  | 2961 | } | 
|  | 2962 |  | 
|  | 2963 | bool AudioFlinger::DuplicatingThread::threadLoop() | 
|  | 2964 | { | 
|  | 2965 | Vector< sp<Track> > tracksToRemove; | 
|  | 2966 | uint32_t mixerStatus = MIXER_IDLE; | 
|  | 2967 | nsecs_t standbyTime = systemTime(); | 
|  | 2968 | size_t mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2969 | SortedVector< sp<OutputTrack> > outputTracks; | 
|  | 2970 | uint32_t writeFrames = 0; | 
|  | 2971 | uint32_t activeSleepTime = activeSleepTimeUs(); | 
|  | 2972 | uint32_t idleSleepTime = idleSleepTimeUs(); | 
|  | 2973 | uint32_t sleepTime = idleSleepTime; | 
|  | 2974 | Vector< sp<EffectChain> > effectChains; | 
|  | 2975 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2976 | acquireWakeLock(); | 
|  | 2977 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2978 | while (!exitPending()) | 
|  | 2979 | { | 
|  | 2980 | processConfigEvents(); | 
|  | 2981 |  | 
|  | 2982 | mixerStatus = MIXER_IDLE; | 
|  | 2983 | { // scope for the mLock | 
|  | 2984 |  | 
|  | 2985 | Mutex::Autolock _l(mLock); | 
|  | 2986 |  | 
|  | 2987 | if (checkForNewParameters_l()) { | 
|  | 2988 | mixBufferSize = mFrameCount*mFrameSize; | 
|  | 2989 | updateWaitTime(); | 
|  | 2990 | activeSleepTime = activeSleepTimeUs(); | 
|  | 2991 | idleSleepTime = idleSleepTimeUs(); | 
|  | 2992 | } | 
|  | 2993 |  | 
|  | 2994 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; | 
|  | 2995 |  | 
|  | 2996 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 2997 | outputTracks.add(mOutputTracks[i]); | 
|  | 2998 | } | 
|  | 2999 |  | 
|  | 3000 | // put audio hardware into standby after short delay | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3001 | if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || | 
|  | 3002 | mSuspended)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3003 | if (!mStandby) { | 
|  | 3004 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3005 | outputTracks[i]->stop(); | 
|  | 3006 | } | 
|  | 3007 | mStandby = true; | 
|  | 3008 | mBytesWritten = 0; | 
|  | 3009 | } | 
|  | 3010 |  | 
|  | 3011 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { | 
|  | 3012 | // we're about to wait, flush the binder command buffer | 
|  | 3013 | IPCThreadState::self()->flushCommands(); | 
|  | 3014 | outputTracks.clear(); | 
|  | 3015 |  | 
|  | 3016 | if (exitPending()) break; | 
|  | 3017 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3018 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3019 | ALOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3020 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3021 | ALOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3022 | acquireWakeLock_l(); | 
|  | 3023 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3024 | if (mMasterMute == false) { | 
|  | 3025 | char value[PROPERTY_VALUE_MAX]; | 
|  | 3026 | property_get("ro.audio.silent", value, "0"); | 
|  | 3027 | if (atoi(value)) { | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3028 | ALOGD("Silence is golden"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3029 | setMasterMute(true); | 
|  | 3030 | } | 
|  | 3031 | } | 
|  | 3032 |  | 
|  | 3033 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 3034 | sleepTime = idleSleepTime; | 
|  | 3035 | continue; | 
|  | 3036 | } | 
|  | 3037 | } | 
|  | 3038 |  | 
|  | 3039 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); | 
|  | 3040 |  | 
|  | 3041 | // prevent any changes in effect chain list and in each effect chain | 
|  | 3042 | // during mixing and effect process as the audio buffers could be deleted | 
|  | 3043 | // or modified if an effect is created or deleted | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3044 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3045 | } | 
|  | 3046 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3047 | if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3048 | // mix buffers... | 
|  | 3049 | if (outputsReady(outputTracks)) { | 
|  | 3050 | mAudioMixer->process(); | 
|  | 3051 | } else { | 
|  | 3052 | memset(mMixBuffer, 0, mixBufferSize); | 
|  | 3053 | } | 
|  | 3054 | sleepTime = 0; | 
|  | 3055 | writeFrames = mFrameCount; | 
|  | 3056 | } else { | 
|  | 3057 | if (sleepTime == 0) { | 
|  | 3058 | if (mixerStatus == MIXER_TRACKS_ENABLED) { | 
|  | 3059 | sleepTime = activeSleepTime; | 
|  | 3060 | } else { | 
|  | 3061 | sleepTime = idleSleepTime; | 
|  | 3062 | } | 
|  | 3063 | } else if (mBytesWritten != 0) { | 
|  | 3064 | // flush remaining overflow buffers in output tracks | 
|  | 3065 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3066 | if (outputTracks[i]->isActive()) { | 
|  | 3067 | sleepTime = 0; | 
|  | 3068 | writeFrames = 0; | 
|  | 3069 | memset(mMixBuffer, 0, mixBufferSize); | 
|  | 3070 | break; | 
|  | 3071 | } | 
|  | 3072 | } | 
|  | 3073 | } | 
|  | 3074 | } | 
|  | 3075 |  | 
|  | 3076 | if (mSuspended) { | 
| Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3077 | sleepTime = suspendSleepTimeUs(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3078 | } | 
|  | 3079 | // sleepTime == 0 means we must write to audio hardware | 
|  | 3080 | if (sleepTime == 0) { | 
|  | 3081 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 3082 | effectChains[i]->process_l(); | 
|  | 3083 | } | 
|  | 3084 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3085 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3086 |  | 
|  | 3087 | standbyTime = systemTime() + kStandbyTimeInNsecs; | 
|  | 3088 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3089 | outputTracks[i]->write(mMixBuffer, writeFrames); | 
|  | 3090 | } | 
|  | 3091 | mStandby = false; | 
|  | 3092 | mBytesWritten += mixBufferSize; | 
|  | 3093 | } else { | 
|  | 3094 | // enable changes in effect chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3095 | unlockEffectChains(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3096 | usleep(sleepTime); | 
|  | 3097 | } | 
|  | 3098 |  | 
|  | 3099 | // finally let go of all our tracks, without the lock held | 
|  | 3100 | // since we can't guarantee the destructors won't acquire that | 
|  | 3101 | // same lock. | 
|  | 3102 | tracksToRemove.clear(); | 
|  | 3103 | outputTracks.clear(); | 
|  | 3104 |  | 
|  | 3105 | // Effect chains will be actually deleted here if they were removed from | 
|  | 3106 | // mEffectChains list during mixing or effects processing | 
|  | 3107 | effectChains.clear(); | 
|  | 3108 | } | 
|  | 3109 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3110 | releaseWakeLock(); | 
|  | 3111 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3112 | return false; | 
|  | 3113 | } | 
|  | 3114 |  | 
|  | 3115 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
|  | 3116 | { | 
|  | 3117 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); | 
|  | 3118 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, | 
|  | 3119 | this, | 
|  | 3120 | mSampleRate, | 
|  | 3121 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3122 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3123 | frameCount); | 
|  | 3124 | if (outputTrack->cblk() != NULL) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3125 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3126 | mOutputTracks.add(outputTrack); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3127 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3128 | updateWaitTime(); | 
|  | 3129 | } | 
|  | 3130 | } | 
|  | 3131 |  | 
|  | 3132 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
|  | 3133 | { | 
|  | 3134 | Mutex::Autolock _l(mLock); | 
|  | 3135 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3136 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { | 
|  | 3137 | mOutputTracks[i]->destroy(); | 
|  | 3138 | mOutputTracks.removeAt(i); | 
|  | 3139 | updateWaitTime(); | 
|  | 3140 | return; | 
|  | 3141 | } | 
|  | 3142 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3143 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3144 | } | 
|  | 3145 |  | 
|  | 3146 | void AudioFlinger::DuplicatingThread::updateWaitTime() | 
|  | 3147 | { | 
|  | 3148 | mWaitTimeMs = UINT_MAX; | 
|  | 3149 | for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
|  | 3150 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
|  | 3151 | if (strong != NULL) { | 
|  | 3152 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
|  | 3153 | if (waitTimeMs < mWaitTimeMs) { | 
|  | 3154 | mWaitTimeMs = waitTimeMs; | 
|  | 3155 | } | 
|  | 3156 | } | 
|  | 3157 | } | 
|  | 3158 | } | 
|  | 3159 |  | 
|  | 3160 |  | 
|  | 3161 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) | 
|  | 3162 | { | 
|  | 3163 | for (size_t i = 0; i < outputTracks.size(); i++) { | 
|  | 3164 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); | 
|  | 3165 | if (thread == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3166 | 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] | 3167 | return false; | 
|  | 3168 | } | 
|  | 3169 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3170 | if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3171 | 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] | 3172 | return false; | 
|  | 3173 | } | 
|  | 3174 | } | 
|  | 3175 | return true; | 
|  | 3176 | } | 
|  | 3177 |  | 
|  | 3178 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() | 
|  | 3179 | { | 
|  | 3180 | return (mWaitTimeMs * 1000) / 2; | 
|  | 3181 | } | 
|  | 3182 |  | 
|  | 3183 | // ---------------------------------------------------------------------------- | 
|  | 3184 |  | 
|  | 3185 | // TrackBase constructor must be called with AudioFlinger::mLock held | 
|  | 3186 | AudioFlinger::ThreadBase::TrackBase::TrackBase( | 
|  | 3187 | const wp<ThreadBase>& thread, | 
|  | 3188 | const sp<Client>& client, | 
|  | 3189 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3190 | uint32_t format, | 
|  | 3191 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3192 | int frameCount, | 
|  | 3193 | uint32_t flags, | 
|  | 3194 | const sp<IMemory>& sharedBuffer, | 
|  | 3195 | int sessionId) | 
|  | 3196 | :   RefBase(), | 
|  | 3197 | mThread(thread), | 
|  | 3198 | mClient(client), | 
|  | 3199 | mCblk(0), | 
|  | 3200 | mFrameCount(0), | 
|  | 3201 | mState(IDLE), | 
|  | 3202 | mClientTid(-1), | 
|  | 3203 | mFormat(format), | 
|  | 3204 | mFlags(flags & ~SYSTEM_FLAGS_MASK), | 
|  | 3205 | mSessionId(sessionId) | 
|  | 3206 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3207 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3208 |  | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3209 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3210 | size_t size = sizeof(audio_track_cblk_t); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3211 | uint8_t channelCount = popcount(channelMask); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3212 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); | 
|  | 3213 | if (sharedBuffer == 0) { | 
|  | 3214 | size += bufferSize; | 
|  | 3215 | } | 
|  | 3216 |  | 
|  | 3217 | if (client != NULL) { | 
|  | 3218 | mCblkMemory = client->heap()->allocate(size); | 
|  | 3219 | if (mCblkMemory != 0) { | 
|  | 3220 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); | 
|  | 3221 | if (mCblk) { // construct the shared structure in-place. | 
|  | 3222 | new(mCblk) audio_track_cblk_t(); | 
|  | 3223 | // clear all buffers | 
|  | 3224 | mCblk->frameCount = frameCount; | 
|  | 3225 | mCblk->sampleRate = sampleRate; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3226 | mChannelCount = channelCount; | 
|  | 3227 | mChannelMask = channelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3228 | if (sharedBuffer == 0) { | 
|  | 3229 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 3230 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); | 
|  | 3231 | // Force underrun condition to avoid false underrun callback until first data is | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3232 | // written to buffer (other flags are cleared) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3233 | mCblk->flags = CBLK_UNDERRUN_ON; | 
|  | 3234 | } else { | 
|  | 3235 | mBuffer = sharedBuffer->pointer(); | 
|  | 3236 | } | 
|  | 3237 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; | 
|  | 3238 | } | 
|  | 3239 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3240 | ALOGE("not enough memory for AudioTrack size=%u", size); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3241 | client->heap()->dump("AudioTrack"); | 
|  | 3242 | return; | 
|  | 3243 | } | 
|  | 3244 | } else { | 
|  | 3245 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); | 
| Glenn Kasten | 4a6f028 | 2012-01-06 15:03:11 -0800 | [diff] [blame] | 3246 | // construct the shared structure in-place. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3247 | new(mCblk) audio_track_cblk_t(); | 
|  | 3248 | // clear all buffers | 
|  | 3249 | mCblk->frameCount = frameCount; | 
|  | 3250 | mCblk->sampleRate = sampleRate; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3251 | mChannelCount = channelCount; | 
|  | 3252 | mChannelMask = channelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3253 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 3254 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); | 
|  | 3255 | // Force underrun condition to avoid false underrun callback until first data is | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3256 | // written to buffer (other flags are cleared) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3257 | mCblk->flags = CBLK_UNDERRUN_ON; | 
|  | 3258 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3259 | } | 
|  | 3260 | } | 
|  | 3261 |  | 
|  | 3262 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() | 
|  | 3263 | { | 
|  | 3264 | if (mCblk) { | 
|  | 3265 | mCblk->~audio_track_cblk_t();   // destroy our shared-structure. | 
|  | 3266 | if (mClient == NULL) { | 
|  | 3267 | delete mCblk; | 
|  | 3268 | } | 
|  | 3269 | } | 
|  | 3270 | mCblkMemory.clear();            // and free the shared memory | 
|  | 3271 | if (mClient != NULL) { | 
|  | 3272 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); | 
|  | 3273 | mClient.clear(); | 
|  | 3274 | } | 
|  | 3275 | } | 
|  | 3276 |  | 
|  | 3277 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3278 | { | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3279 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3280 | mFrameCount = buffer->frameCount; | 
|  | 3281 | step(); | 
|  | 3282 | buffer->frameCount = 0; | 
|  | 3283 | } | 
|  | 3284 |  | 
|  | 3285 | bool AudioFlinger::ThreadBase::TrackBase::step() { | 
|  | 3286 | bool result; | 
|  | 3287 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3288 |  | 
|  | 3289 | result = cblk->stepServer(mFrameCount); | 
|  | 3290 | if (!result) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3291 | ALOGV("stepServer failed acquiring cblk mutex"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3292 | mFlags |= STEPSERVER_FAILED; | 
|  | 3293 | } | 
|  | 3294 | return result; | 
|  | 3295 | } | 
|  | 3296 |  | 
|  | 3297 | void AudioFlinger::ThreadBase::TrackBase::reset() { | 
|  | 3298 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3299 |  | 
|  | 3300 | cblk->user = 0; | 
|  | 3301 | cblk->server = 0; | 
|  | 3302 | cblk->userBase = 0; | 
|  | 3303 | cblk->serverBase = 0; | 
|  | 3304 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3305 | ALOGV("TrackBase::reset"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3306 | } | 
|  | 3307 |  | 
|  | 3308 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const | 
|  | 3309 | { | 
|  | 3310 | return mCblkMemory; | 
|  | 3311 | } | 
|  | 3312 |  | 
|  | 3313 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { | 
|  | 3314 | return (int)mCblk->sampleRate; | 
|  | 3315 | } | 
|  | 3316 |  | 
|  | 3317 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3318 | return (const int)mChannelCount; | 
|  | 3319 | } | 
|  | 3320 |  | 
|  | 3321 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { | 
|  | 3322 | return mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3323 | } | 
|  | 3324 |  | 
|  | 3325 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { | 
|  | 3326 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3327 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize; | 
|  | 3328 | int8_t *bufferEnd = bufferStart + frames * cblk->frameSize; | 
|  | 3329 |  | 
|  | 3330 | // Check validity of returned pointer in case the track control block would have been corrupted. | 
|  | 3331 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || | 
|  | 3332 | ((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3333 | 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] | 3334 | server %d, serverBase %d, user %d, userBase %d", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3335 | bufferStart, bufferEnd, mBuffer, mBufferEnd, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3336 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3337 | return 0; | 
|  | 3338 | } | 
|  | 3339 |  | 
|  | 3340 | return bufferStart; | 
|  | 3341 | } | 
|  | 3342 |  | 
|  | 3343 | // ---------------------------------------------------------------------------- | 
|  | 3344 |  | 
|  | 3345 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held | 
|  | 3346 | AudioFlinger::PlaybackThread::Track::Track( | 
|  | 3347 | const wp<ThreadBase>& thread, | 
|  | 3348 | const sp<Client>& client, | 
|  | 3349 | int streamType, | 
|  | 3350 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3351 | uint32_t format, | 
|  | 3352 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3353 | int frameCount, | 
|  | 3354 | const sp<IMemory>& sharedBuffer, | 
|  | 3355 | int sessionId) | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3356 | :   TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3357 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), | 
|  | 3358 | mAuxEffectId(0), mHasVolumeController(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3359 | { | 
|  | 3360 | if (mCblk != NULL) { | 
|  | 3361 | sp<ThreadBase> baseThread = thread.promote(); | 
|  | 3362 | if (baseThread != 0) { | 
|  | 3363 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); | 
|  | 3364 | mName = playbackThread->getTrackName_l(); | 
|  | 3365 | mMainBuffer = playbackThread->mixBuffer(); | 
|  | 3366 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3367 | ALOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3368 | if (mName < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 3369 | ALOGE("no more track names available"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3370 | } | 
|  | 3371 | mVolume[0] = 1.0f; | 
|  | 3372 | mVolume[1] = 1.0f; | 
|  | 3373 | mStreamType = streamType; | 
|  | 3374 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of | 
|  | 3375 | // 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] | 3376 | 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] | 3377 | } | 
|  | 3378 | } | 
|  | 3379 |  | 
|  | 3380 | AudioFlinger::PlaybackThread::Track::~Track() | 
|  | 3381 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3382 | ALOGV("PlaybackThread::Track destructor"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3383 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3384 | if (thread != 0) { | 
|  | 3385 | Mutex::Autolock _l(thread->mLock); | 
|  | 3386 | mState = TERMINATED; | 
|  | 3387 | } | 
|  | 3388 | } | 
|  | 3389 |  | 
|  | 3390 | void AudioFlinger::PlaybackThread::Track::destroy() | 
|  | 3391 | { | 
|  | 3392 | // NOTE: destroyTrack_l() can remove a strong reference to this Track | 
|  | 3393 | // by removing it from mTracks vector, so there is a risk that this Tracks's | 
|  | 3394 | // desctructor is called. As the destructor needs to lock mLock, | 
|  | 3395 | // we must acquire a strong reference on this Track before locking mLock | 
|  | 3396 | // here so that the destructor is called only when exiting this function. | 
|  | 3397 | // On the other hand, as long as Track::destroy() is only called by | 
|  | 3398 | // TrackHandle destructor, the TrackHandle still holds a strong ref on | 
|  | 3399 | // this Track with its member mTrack. | 
|  | 3400 | sp<Track> keep(this); | 
|  | 3401 | { // scope for mLock | 
|  | 3402 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3403 | if (thread != 0) { | 
|  | 3404 | if (!isOutputTrack()) { | 
|  | 3405 | if (mState == ACTIVE || mState == RESUMING) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3406 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3407 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3408 | mSessionId); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3409 |  | 
|  | 3410 | // to track the speaker usage | 
|  | 3411 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3412 | } | 
|  | 3413 | AudioSystem::releaseOutput(thread->id()); | 
|  | 3414 | } | 
|  | 3415 | Mutex::Autolock _l(thread->mLock); | 
|  | 3416 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3417 | playbackThread->destroyTrack_l(this); | 
|  | 3418 | } | 
|  | 3419 | } | 
|  | 3420 | } | 
|  | 3421 |  | 
|  | 3422 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) | 
|  | 3423 | { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3424 | 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] | 3425 | mName - AudioMixer::TRACK0, | 
|  | 3426 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 3427 | mStreamType, | 
|  | 3428 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3429 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3430 | mSessionId, | 
|  | 3431 | mFrameCount, | 
|  | 3432 | mState, | 
|  | 3433 | mMute, | 
|  | 3434 | mFillingUpStatus, | 
|  | 3435 | mCblk->sampleRate, | 
|  | 3436 | mCblk->volume[0], | 
|  | 3437 | mCblk->volume[1], | 
|  | 3438 | mCblk->server, | 
|  | 3439 | mCblk->user, | 
|  | 3440 | (int)mMainBuffer, | 
|  | 3441 | (int)mAuxBuffer); | 
|  | 3442 | } | 
|  | 3443 |  | 
|  | 3444 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3445 | { | 
|  | 3446 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3447 | uint32_t framesReady; | 
|  | 3448 | uint32_t framesReq = buffer->frameCount; | 
|  | 3449 |  | 
|  | 3450 | // Check if last stepServer failed, try to step now | 
|  | 3451 | if (mFlags & TrackBase::STEPSERVER_FAILED) { | 
|  | 3452 | if (!step())  goto getNextBuffer_exit; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3453 | ALOGV("stepServer recovered"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3454 | mFlags &= ~TrackBase::STEPSERVER_FAILED; | 
|  | 3455 | } | 
|  | 3456 |  | 
|  | 3457 | framesReady = cblk->framesReady(); | 
|  | 3458 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3459 | if (CC_LIKELY(framesReady)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3460 | uint32_t s = cblk->server; | 
|  | 3461 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; | 
|  | 3462 |  | 
|  | 3463 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; | 
|  | 3464 | if (framesReq > framesReady) { | 
|  | 3465 | framesReq = framesReady; | 
|  | 3466 | } | 
|  | 3467 | if (s + framesReq > bufferEnd) { | 
|  | 3468 | framesReq = bufferEnd - s; | 
|  | 3469 | } | 
|  | 3470 |  | 
|  | 3471 | buffer->raw = getBuffer(s, framesReq); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3472 | if (buffer->raw == NULL) goto getNextBuffer_exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3473 |  | 
|  | 3474 | buffer->frameCount = framesReq; | 
|  | 3475 | return NO_ERROR; | 
|  | 3476 | } | 
|  | 3477 |  | 
|  | 3478 | getNextBuffer_exit: | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3479 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | buffer->frameCount = 0; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3481 | 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] | 3482 | return NOT_ENOUGH_DATA; | 
|  | 3483 | } | 
|  | 3484 |  | 
|  | 3485 | bool AudioFlinger::PlaybackThread::Track::isReady() const { | 
| Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3486 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3487 |  | 
|  | 3488 | if (mCblk->framesReady() >= mCblk->frameCount || | 
|  | 3489 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { | 
|  | 3490 | mFillingUpStatus = FS_FILLED; | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3491 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3492 | return true; | 
|  | 3493 | } | 
|  | 3494 | return false; | 
|  | 3495 | } | 
|  | 3496 |  | 
|  | 3497 | status_t AudioFlinger::PlaybackThread::Track::start() | 
|  | 3498 | { | 
|  | 3499 | status_t status = NO_ERROR; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3500 | ALOGV("start(%d), calling thread %d session %d", | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3501 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3502 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3503 | if (thread != 0) { | 
|  | 3504 | Mutex::Autolock _l(thread->mLock); | 
|  | 3505 | int state = mState; | 
|  | 3506 | // here the track could be either new, or restarted | 
|  | 3507 | // in both cases "unstop" the track | 
|  | 3508 | if (mState == PAUSED) { | 
|  | 3509 | mState = TrackBase::RESUMING; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3510 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3511 | } else { | 
|  | 3512 | mState = TrackBase::ACTIVE; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3513 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3514 | } | 
|  | 3515 |  | 
|  | 3516 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { | 
|  | 3517 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3518 | status = AudioSystem::startOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3519 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3520 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3521 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3522 |  | 
|  | 3523 | // to track the speaker usage | 
|  | 3524 | if (status == NO_ERROR) { | 
|  | 3525 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); | 
|  | 3526 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3527 | } | 
|  | 3528 | if (status == NO_ERROR) { | 
|  | 3529 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3530 | playbackThread->addTrack_l(this); | 
|  | 3531 | } else { | 
|  | 3532 | mState = state; | 
|  | 3533 | } | 
|  | 3534 | } else { | 
|  | 3535 | status = BAD_VALUE; | 
|  | 3536 | } | 
|  | 3537 | return status; | 
|  | 3538 | } | 
|  | 3539 |  | 
|  | 3540 | void AudioFlinger::PlaybackThread::Track::stop() | 
|  | 3541 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3542 | ALOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3543 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3544 | if (thread != 0) { | 
|  | 3545 | Mutex::Autolock _l(thread->mLock); | 
|  | 3546 | int state = mState; | 
|  | 3547 | if (mState > STOPPED) { | 
|  | 3548 | mState = STOPPED; | 
|  | 3549 | // If the track is not active (PAUSED and buffers full), flush buffers | 
|  | 3550 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3551 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 3552 | reset(); | 
|  | 3553 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3554 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3555 | } | 
|  | 3556 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { | 
|  | 3557 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3558 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3559 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3560 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3561 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3562 |  | 
|  | 3563 | // to track the speaker usage | 
|  | 3564 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3565 | } | 
|  | 3566 | } | 
|  | 3567 | } | 
|  | 3568 |  | 
|  | 3569 | void AudioFlinger::PlaybackThread::Track::pause() | 
|  | 3570 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3571 | ALOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3572 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3573 | if (thread != 0) { | 
|  | 3574 | Mutex::Autolock _l(thread->mLock); | 
|  | 3575 | if (mState == ACTIVE || mState == RESUMING) { | 
|  | 3576 | mState = PAUSING; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3577 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3578 | if (!isOutputTrack()) { | 
|  | 3579 | thread->mLock.unlock(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3580 | AudioSystem::stopOutput(thread->id(), | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3581 | (audio_stream_type_t)mStreamType, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3582 | mSessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3583 | thread->mLock.lock(); | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3584 |  | 
|  | 3585 | // to track the speaker usage | 
|  | 3586 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3587 | } | 
|  | 3588 | } | 
|  | 3589 | } | 
|  | 3590 | } | 
|  | 3591 |  | 
|  | 3592 | void AudioFlinger::PlaybackThread::Track::flush() | 
|  | 3593 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3594 | ALOGV("flush(%d)", mName); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3595 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3596 | if (thread != 0) { | 
|  | 3597 | Mutex::Autolock _l(thread->mLock); | 
|  | 3598 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { | 
|  | 3599 | return; | 
|  | 3600 | } | 
|  | 3601 | // No point remaining in PAUSED state after a flush => go to | 
|  | 3602 | // STOPPED state | 
|  | 3603 | mState = STOPPED; | 
|  | 3604 |  | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3605 | // do not reset the track if it is still in the process of being stopped or paused. | 
|  | 3606 | // this will be done by prepareTracks_l() when the track is stopped. | 
|  | 3607 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3608 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { | 
|  | 3609 | reset(); | 
|  | 3610 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3611 | } | 
|  | 3612 | } | 
|  | 3613 |  | 
|  | 3614 | void AudioFlinger::PlaybackThread::Track::reset() | 
|  | 3615 | { | 
|  | 3616 | // Do not reset twice to avoid discarding data written just after a flush and before | 
|  | 3617 | // the audioflinger thread detects the track is stopped. | 
|  | 3618 | if (!mResetDone) { | 
|  | 3619 | TrackBase::reset(); | 
|  | 3620 | // Force underrun condition to avoid false underrun callback until first data is | 
|  | 3621 | // written to buffer | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3622 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); | 
|  | 3623 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3624 | mFillingUpStatus = FS_FILLING; | 
|  | 3625 | mResetDone = true; | 
|  | 3626 | } | 
|  | 3627 | } | 
|  | 3628 |  | 
|  | 3629 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) | 
|  | 3630 | { | 
|  | 3631 | mMute = muted; | 
|  | 3632 | } | 
|  | 3633 |  | 
|  | 3634 | void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right) | 
|  | 3635 | { | 
|  | 3636 | mVolume[0] = left; | 
|  | 3637 | mVolume[1] = right; | 
|  | 3638 | } | 
|  | 3639 |  | 
|  | 3640 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) | 
|  | 3641 | { | 
|  | 3642 | status_t status = DEAD_OBJECT; | 
|  | 3643 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3644 | if (thread != 0) { | 
|  | 3645 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 3646 | status = playbackThread->attachAuxEffect(this, EffectId); | 
|  | 3647 | } | 
|  | 3648 | return status; | 
|  | 3649 | } | 
|  | 3650 |  | 
|  | 3651 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) | 
|  | 3652 | { | 
|  | 3653 | mAuxEffectId = EffectId; | 
|  | 3654 | mAuxBuffer = buffer; | 
|  | 3655 | } | 
|  | 3656 |  | 
|  | 3657 | // ---------------------------------------------------------------------------- | 
|  | 3658 |  | 
|  | 3659 | // RecordTrack constructor must be called with AudioFlinger::mLock held | 
|  | 3660 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( | 
|  | 3661 | const wp<ThreadBase>& thread, | 
|  | 3662 | const sp<Client>& client, | 
|  | 3663 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3664 | uint32_t format, | 
|  | 3665 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3666 | int frameCount, | 
|  | 3667 | uint32_t flags, | 
|  | 3668 | int sessionId) | 
|  | 3669 | :   TrackBase(thread, client, sampleRate, format, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3670 | channelMask, frameCount, flags, 0, sessionId), | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3671 | mOverflow(false) | 
|  | 3672 | { | 
|  | 3673 | if (mCblk != NULL) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3674 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3675 | if (format == AUDIO_FORMAT_PCM_16_BIT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3676 | mCblk->frameSize = mChannelCount * sizeof(int16_t); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3677 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3678 | mCblk->frameSize = mChannelCount * sizeof(int8_t); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3679 | } else { | 
|  | 3680 | mCblk->frameSize = sizeof(int8_t); | 
|  | 3681 | } | 
|  | 3682 | } | 
|  | 3683 | } | 
|  | 3684 |  | 
|  | 3685 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() | 
|  | 3686 | { | 
|  | 3687 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3688 | if (thread != 0) { | 
|  | 3689 | AudioSystem::releaseInput(thread->id()); | 
|  | 3690 | } | 
|  | 3691 | } | 
|  | 3692 |  | 
|  | 3693 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 3694 | { | 
|  | 3695 | audio_track_cblk_t* cblk = this->cblk(); | 
|  | 3696 | uint32_t framesAvail; | 
|  | 3697 | uint32_t framesReq = buffer->frameCount; | 
|  | 3698 |  | 
|  | 3699 | // Check if last stepServer failed, try to step now | 
|  | 3700 | if (mFlags & TrackBase::STEPSERVER_FAILED) { | 
|  | 3701 | if (!step()) goto getNextBuffer_exit; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3702 | ALOGV("stepServer recovered"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3703 | mFlags &= ~TrackBase::STEPSERVER_FAILED; | 
|  | 3704 | } | 
|  | 3705 |  | 
|  | 3706 | framesAvail = cblk->framesAvailable_l(); | 
|  | 3707 |  | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3708 | if (CC_LIKELY(framesAvail)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3709 | uint32_t s = cblk->server; | 
|  | 3710 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; | 
|  | 3711 |  | 
|  | 3712 | if (framesReq > framesAvail) { | 
|  | 3713 | framesReq = framesAvail; | 
|  | 3714 | } | 
|  | 3715 | if (s + framesReq > bufferEnd) { | 
|  | 3716 | framesReq = bufferEnd - s; | 
|  | 3717 | } | 
|  | 3718 |  | 
|  | 3719 | buffer->raw = getBuffer(s, framesReq); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3720 | if (buffer->raw == NULL) goto getNextBuffer_exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3721 |  | 
|  | 3722 | buffer->frameCount = framesReq; | 
|  | 3723 | return NO_ERROR; | 
|  | 3724 | } | 
|  | 3725 |  | 
|  | 3726 | getNextBuffer_exit: | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 3727 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3728 | buffer->frameCount = 0; | 
|  | 3729 | return NOT_ENOUGH_DATA; | 
|  | 3730 | } | 
|  | 3731 |  | 
|  | 3732 | status_t AudioFlinger::RecordThread::RecordTrack::start() | 
|  | 3733 | { | 
|  | 3734 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3735 | if (thread != 0) { | 
|  | 3736 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
|  | 3737 | return recordThread->start(this); | 
|  | 3738 | } else { | 
|  | 3739 | return BAD_VALUE; | 
|  | 3740 | } | 
|  | 3741 | } | 
|  | 3742 |  | 
|  | 3743 | void AudioFlinger::RecordThread::RecordTrack::stop() | 
|  | 3744 | { | 
|  | 3745 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3746 | if (thread != 0) { | 
|  | 3747 | RecordThread *recordThread = (RecordThread *)thread.get(); | 
|  | 3748 | recordThread->stop(this); | 
| Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3749 | TrackBase::reset(); | 
|  | 3750 | // Force overerrun condition to avoid false overrun callback until first data is | 
|  | 3751 | // read from buffer | 
|  | 3752 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3753 | } | 
|  | 3754 | } | 
|  | 3755 |  | 
|  | 3756 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) | 
|  | 3757 | { | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3758 | snprintf(buffer, size, "   %05d %03u 0x%08x %05d   %04u %01d %05u  %08x %08x\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3759 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 3760 | mFormat, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3761 | mChannelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3762 | mSessionId, | 
|  | 3763 | mFrameCount, | 
|  | 3764 | mState, | 
|  | 3765 | mCblk->sampleRate, | 
|  | 3766 | mCblk->server, | 
|  | 3767 | mCblk->user); | 
|  | 3768 | } | 
|  | 3769 |  | 
|  | 3770 |  | 
|  | 3771 | // ---------------------------------------------------------------------------- | 
|  | 3772 |  | 
|  | 3773 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( | 
|  | 3774 | const wp<ThreadBase>& thread, | 
|  | 3775 | DuplicatingThread *sourceThread, | 
|  | 3776 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3777 | uint32_t format, | 
|  | 3778 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3779 | int frameCount) | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3780 | :   Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3781 | mActive(false), mSourceThread(sourceThread) | 
|  | 3782 | { | 
|  | 3783 |  | 
|  | 3784 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); | 
|  | 3785 | if (mCblk != NULL) { | 
|  | 3786 | mCblk->flags |= CBLK_DIRECTION_OUT; | 
|  | 3787 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); | 
|  | 3788 | mCblk->volume[0] = mCblk->volume[1] = 0x1000; | 
|  | 3789 | mOutBuffer.frameCount = 0; | 
|  | 3790 | playbackThread->mTracks.add(this); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3791 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3792 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", | 
|  | 3793 | mCblk, mBuffer, mCblk->buffers, | 
|  | 3794 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3795 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3796 | ALOGW("Error creating output track on thread %p", playbackThread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3797 | } | 
|  | 3798 | } | 
|  | 3799 |  | 
|  | 3800 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() | 
|  | 3801 | { | 
|  | 3802 | clearBufferQueue(); | 
|  | 3803 | } | 
|  | 3804 |  | 
|  | 3805 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() | 
|  | 3806 | { | 
|  | 3807 | status_t status = Track::start(); | 
|  | 3808 | if (status != NO_ERROR) { | 
|  | 3809 | return status; | 
|  | 3810 | } | 
|  | 3811 |  | 
|  | 3812 | mActive = true; | 
|  | 3813 | mRetryCount = 127; | 
|  | 3814 | return status; | 
|  | 3815 | } | 
|  | 3816 |  | 
|  | 3817 | void AudioFlinger::PlaybackThread::OutputTrack::stop() | 
|  | 3818 | { | 
|  | 3819 | Track::stop(); | 
|  | 3820 | clearBufferQueue(); | 
|  | 3821 | mOutBuffer.frameCount = 0; | 
|  | 3822 | mActive = false; | 
|  | 3823 | } | 
|  | 3824 |  | 
|  | 3825 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) | 
|  | 3826 | { | 
|  | 3827 | Buffer *pInBuffer; | 
|  | 3828 | Buffer inBuffer; | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3829 | uint32_t channelCount = mChannelCount; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3830 | bool outputBufferFull = false; | 
|  | 3831 | inBuffer.frameCount = frames; | 
|  | 3832 | inBuffer.i16 = data; | 
|  | 3833 |  | 
|  | 3834 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); | 
|  | 3835 |  | 
|  | 3836 | if (!mActive && frames != 0) { | 
|  | 3837 | start(); | 
|  | 3838 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3839 | if (thread != 0) { | 
|  | 3840 | MixerThread *mixerThread = (MixerThread *)thread.get(); | 
|  | 3841 | if (mCblk->frameCount > frames){ | 
|  | 3842 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { | 
|  | 3843 | uint32_t startFrames = (mCblk->frameCount - frames); | 
|  | 3844 | pInBuffer = new Buffer; | 
|  | 3845 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; | 
|  | 3846 | pInBuffer->frameCount = startFrames; | 
|  | 3847 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3848 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); | 
|  | 3849 | mBufferQueue.add(pInBuffer); | 
|  | 3850 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3851 | ALOGW ("OutputTrack::write() %p no more buffers in queue", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3852 | } | 
|  | 3853 | } | 
|  | 3854 | } | 
|  | 3855 | } | 
|  | 3856 |  | 
|  | 3857 | while (waitTimeLeftMs) { | 
|  | 3858 | // First write pending buffers, then new data | 
|  | 3859 | if (mBufferQueue.size()) { | 
|  | 3860 | pInBuffer = mBufferQueue.itemAt(0); | 
|  | 3861 | } else { | 
|  | 3862 | pInBuffer = &inBuffer; | 
|  | 3863 | } | 
|  | 3864 |  | 
|  | 3865 | if (pInBuffer->frameCount == 0) { | 
|  | 3866 | break; | 
|  | 3867 | } | 
|  | 3868 |  | 
|  | 3869 | if (mOutBuffer.frameCount == 0) { | 
|  | 3870 | mOutBuffer.frameCount = pInBuffer->frameCount; | 
|  | 3871 | nsecs_t startTime = systemTime(); | 
|  | 3872 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3873 | 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] | 3874 | outputBufferFull = true; | 
|  | 3875 | break; | 
|  | 3876 | } | 
|  | 3877 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); | 
|  | 3878 | if (waitTimeLeftMs >= waitTimeMs) { | 
|  | 3879 | waitTimeLeftMs -= waitTimeMs; | 
|  | 3880 | } else { | 
|  | 3881 | waitTimeLeftMs = 0; | 
|  | 3882 | } | 
|  | 3883 | } | 
|  | 3884 |  | 
|  | 3885 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; | 
|  | 3886 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); | 
|  | 3887 | mCblk->stepUser(outFrames); | 
|  | 3888 | pInBuffer->frameCount -= outFrames; | 
|  | 3889 | pInBuffer->i16 += outFrames * channelCount; | 
|  | 3890 | mOutBuffer.frameCount -= outFrames; | 
|  | 3891 | mOutBuffer.i16 += outFrames * channelCount; | 
|  | 3892 |  | 
|  | 3893 | if (pInBuffer->frameCount == 0) { | 
|  | 3894 | if (mBufferQueue.size()) { | 
|  | 3895 | mBufferQueue.removeAt(0); | 
|  | 3896 | delete [] pInBuffer->mBuffer; | 
|  | 3897 | delete pInBuffer; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3898 | 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] | 3899 | } else { | 
|  | 3900 | break; | 
|  | 3901 | } | 
|  | 3902 | } | 
|  | 3903 | } | 
|  | 3904 |  | 
|  | 3905 | // If we could not write all frames, allocate a buffer and queue it for next time. | 
|  | 3906 | if (inBuffer.frameCount) { | 
|  | 3907 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 3908 | if (thread != 0 && !thread->standby()) { | 
|  | 3909 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { | 
|  | 3910 | pInBuffer = new Buffer; | 
|  | 3911 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; | 
|  | 3912 | pInBuffer->frameCount = inBuffer.frameCount; | 
|  | 3913 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3914 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); | 
|  | 3915 | mBufferQueue.add(pInBuffer); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3916 | 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] | 3917 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3918 | 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] | 3919 | } | 
|  | 3920 | } | 
|  | 3921 | } | 
|  | 3922 |  | 
|  | 3923 | // Calling write() with a 0 length buffer, means that no more data will be written: | 
|  | 3924 | // If no more buffers are pending, fill output track buffer to make sure it is started | 
|  | 3925 | // by output mixer. | 
|  | 3926 | if (frames == 0 && mBufferQueue.size() == 0) { | 
|  | 3927 | if (mCblk->user < mCblk->frameCount) { | 
|  | 3928 | frames = mCblk->frameCount - mCblk->user; | 
|  | 3929 | pInBuffer = new Buffer; | 
|  | 3930 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; | 
|  | 3931 | pInBuffer->frameCount = frames; | 
|  | 3932 | pInBuffer->i16 = pInBuffer->mBuffer; | 
|  | 3933 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); | 
|  | 3934 | mBufferQueue.add(pInBuffer); | 
|  | 3935 | } else if (mActive) { | 
|  | 3936 | stop(); | 
|  | 3937 | } | 
|  | 3938 | } | 
|  | 3939 |  | 
|  | 3940 | return outputBufferFull; | 
|  | 3941 | } | 
|  | 3942 |  | 
|  | 3943 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) | 
|  | 3944 | { | 
|  | 3945 | int active; | 
|  | 3946 | status_t result; | 
|  | 3947 | audio_track_cblk_t* cblk = mCblk; | 
|  | 3948 | uint32_t framesReq = buffer->frameCount; | 
|  | 3949 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3950 | //    ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3951 | buffer->frameCount  = 0; | 
|  | 3952 |  | 
|  | 3953 | uint32_t framesAvail = cblk->framesAvailable(); | 
|  | 3954 |  | 
|  | 3955 |  | 
|  | 3956 | if (framesAvail == 0) { | 
|  | 3957 | Mutex::Autolock _l(cblk->lock); | 
|  | 3958 | goto start_loop_here; | 
|  | 3959 | while (framesAvail == 0) { | 
|  | 3960 | active = mActive; | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 3961 | if (CC_UNLIKELY(!active)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3962 | ALOGV("Not active and NO_MORE_BUFFERS"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3963 | return AudioTrack::NO_MORE_BUFFERS; | 
|  | 3964 | } | 
|  | 3965 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); | 
|  | 3966 | if (result != NO_ERROR) { | 
|  | 3967 | return AudioTrack::NO_MORE_BUFFERS; | 
|  | 3968 | } | 
|  | 3969 | // read the server count again | 
|  | 3970 | start_loop_here: | 
|  | 3971 | framesAvail = cblk->framesAvailable_l(); | 
|  | 3972 | } | 
|  | 3973 | } | 
|  | 3974 |  | 
|  | 3975 | //    if (framesAvail < framesReq) { | 
|  | 3976 | //        return AudioTrack::NO_MORE_BUFFERS; | 
|  | 3977 | //    } | 
|  | 3978 |  | 
|  | 3979 | if (framesReq > framesAvail) { | 
|  | 3980 | framesReq = framesAvail; | 
|  | 3981 | } | 
|  | 3982 |  | 
|  | 3983 | uint32_t u = cblk->user; | 
|  | 3984 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; | 
|  | 3985 |  | 
|  | 3986 | if (u + framesReq > bufferEnd) { | 
|  | 3987 | framesReq = bufferEnd - u; | 
|  | 3988 | } | 
|  | 3989 |  | 
|  | 3990 | buffer->frameCount  = framesReq; | 
|  | 3991 | buffer->raw         = (void *)cblk->buffer(u); | 
|  | 3992 | return NO_ERROR; | 
|  | 3993 | } | 
|  | 3994 |  | 
|  | 3995 |  | 
|  | 3996 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() | 
|  | 3997 | { | 
|  | 3998 | size_t size = mBufferQueue.size(); | 
|  | 3999 | Buffer *pBuffer; | 
|  | 4000 |  | 
|  | 4001 | for (size_t i = 0; i < size; i++) { | 
|  | 4002 | pBuffer = mBufferQueue.itemAt(i); | 
|  | 4003 | delete [] pBuffer->mBuffer; | 
|  | 4004 | delete pBuffer; | 
|  | 4005 | } | 
|  | 4006 | mBufferQueue.clear(); | 
|  | 4007 | } | 
|  | 4008 |  | 
|  | 4009 | // ---------------------------------------------------------------------------- | 
|  | 4010 |  | 
|  | 4011 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) | 
|  | 4012 | :   RefBase(), | 
|  | 4013 | mAudioFlinger(audioFlinger), | 
|  | 4014 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), | 
|  | 4015 | mPid(pid) | 
|  | 4016 | { | 
|  | 4017 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer | 
|  | 4018 | } | 
|  | 4019 |  | 
|  | 4020 | // Client destructor must be called with AudioFlinger::mLock held | 
|  | 4021 | AudioFlinger::Client::~Client() | 
|  | 4022 | { | 
|  | 4023 | mAudioFlinger->removeClient_l(mPid); | 
|  | 4024 | } | 
|  | 4025 |  | 
|  | 4026 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const | 
|  | 4027 | { | 
|  | 4028 | return mMemoryDealer; | 
|  | 4029 | } | 
|  | 4030 |  | 
|  | 4031 | // ---------------------------------------------------------------------------- | 
|  | 4032 |  | 
|  | 4033 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, | 
|  | 4034 | const sp<IAudioFlingerClient>& client, | 
|  | 4035 | pid_t pid) | 
|  | 4036 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) | 
|  | 4037 | { | 
|  | 4038 | } | 
|  | 4039 |  | 
|  | 4040 | AudioFlinger::NotificationClient::~NotificationClient() | 
|  | 4041 | { | 
|  | 4042 | mClient.clear(); | 
|  | 4043 | } | 
|  | 4044 |  | 
|  | 4045 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) | 
|  | 4046 | { | 
|  | 4047 | sp<NotificationClient> keep(this); | 
|  | 4048 | { | 
|  | 4049 | mAudioFlinger->removeNotificationClient(mPid); | 
|  | 4050 | } | 
|  | 4051 | } | 
|  | 4052 |  | 
|  | 4053 | // ---------------------------------------------------------------------------- | 
|  | 4054 |  | 
|  | 4055 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) | 
|  | 4056 | : BnAudioTrack(), | 
|  | 4057 | mTrack(track) | 
|  | 4058 | { | 
|  | 4059 | } | 
|  | 4060 |  | 
|  | 4061 | AudioFlinger::TrackHandle::~TrackHandle() { | 
|  | 4062 | // just stop the track on deletion, associated resources | 
|  | 4063 | // will be freed from the main thread once all pending buffers have | 
|  | 4064 | // been played. Unless it's not in the active track list, in which | 
|  | 4065 | // case we free everything now... | 
|  | 4066 | mTrack->destroy(); | 
|  | 4067 | } | 
|  | 4068 |  | 
|  | 4069 | status_t AudioFlinger::TrackHandle::start() { | 
|  | 4070 | return mTrack->start(); | 
|  | 4071 | } | 
|  | 4072 |  | 
|  | 4073 | void AudioFlinger::TrackHandle::stop() { | 
|  | 4074 | mTrack->stop(); | 
|  | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | void AudioFlinger::TrackHandle::flush() { | 
|  | 4078 | mTrack->flush(); | 
|  | 4079 | } | 
|  | 4080 |  | 
|  | 4081 | void AudioFlinger::TrackHandle::mute(bool e) { | 
|  | 4082 | mTrack->mute(e); | 
|  | 4083 | } | 
|  | 4084 |  | 
|  | 4085 | void AudioFlinger::TrackHandle::pause() { | 
|  | 4086 | mTrack->pause(); | 
|  | 4087 | } | 
|  | 4088 |  | 
|  | 4089 | void AudioFlinger::TrackHandle::setVolume(float left, float right) { | 
|  | 4090 | mTrack->setVolume(left, right); | 
|  | 4091 | } | 
|  | 4092 |  | 
|  | 4093 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { | 
|  | 4094 | return mTrack->getCblk(); | 
|  | 4095 | } | 
|  | 4096 |  | 
|  | 4097 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) | 
|  | 4098 | { | 
|  | 4099 | return mTrack->attachAuxEffect(EffectId); | 
|  | 4100 | } | 
|  | 4101 |  | 
|  | 4102 | status_t AudioFlinger::TrackHandle::onTransact( | 
|  | 4103 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 4104 | { | 
|  | 4105 | return BnAudioTrack::onTransact(code, data, reply, flags); | 
|  | 4106 | } | 
|  | 4107 |  | 
|  | 4108 | // ---------------------------------------------------------------------------- | 
|  | 4109 |  | 
|  | 4110 | sp<IAudioRecord> AudioFlinger::openRecord( | 
|  | 4111 | pid_t pid, | 
|  | 4112 | int input, | 
|  | 4113 | uint32_t sampleRate, | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4114 | uint32_t format, | 
|  | 4115 | uint32_t channelMask, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4116 | int frameCount, | 
|  | 4117 | uint32_t flags, | 
|  | 4118 | int *sessionId, | 
|  | 4119 | status_t *status) | 
|  | 4120 | { | 
|  | 4121 | sp<RecordThread::RecordTrack> recordTrack; | 
|  | 4122 | sp<RecordHandle> recordHandle; | 
|  | 4123 | sp<Client> client; | 
|  | 4124 | wp<Client> wclient; | 
|  | 4125 | status_t lStatus; | 
|  | 4126 | RecordThread *thread; | 
|  | 4127 | size_t inFrameCount; | 
|  | 4128 | int lSessionId; | 
|  | 4129 |  | 
|  | 4130 | // check calling permissions | 
|  | 4131 | if (!recordingAllowed()) { | 
|  | 4132 | lStatus = PERMISSION_DENIED; | 
|  | 4133 | goto Exit; | 
|  | 4134 | } | 
|  | 4135 |  | 
|  | 4136 | // add client to list | 
|  | 4137 | { // scope for mLock | 
|  | 4138 | Mutex::Autolock _l(mLock); | 
|  | 4139 | thread = checkRecordThread_l(input); | 
|  | 4140 | if (thread == NULL) { | 
|  | 4141 | lStatus = BAD_VALUE; | 
|  | 4142 | goto Exit; | 
|  | 4143 | } | 
|  | 4144 |  | 
|  | 4145 | wclient = mClients.valueFor(pid); | 
|  | 4146 | if (wclient != NULL) { | 
|  | 4147 | client = wclient.promote(); | 
|  | 4148 | } else { | 
|  | 4149 | client = new Client(this, pid); | 
|  | 4150 | mClients.add(pid, client); | 
|  | 4151 | } | 
|  | 4152 |  | 
|  | 4153 | // If no audio session id is provided, create one here | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4154 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4155 | lSessionId = *sessionId; | 
|  | 4156 | } else { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4157 | lSessionId = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4158 | if (sessionId != NULL) { | 
|  | 4159 | *sessionId = lSessionId; | 
|  | 4160 | } | 
|  | 4161 | } | 
|  | 4162 | // 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] | 4163 | recordTrack = thread->createRecordTrack_l(client, | 
|  | 4164 | sampleRate, | 
|  | 4165 | format, | 
|  | 4166 | channelMask, | 
|  | 4167 | frameCount, | 
|  | 4168 | flags, | 
|  | 4169 | lSessionId, | 
|  | 4170 | &lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4171 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4172 | if (lStatus != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4173 | // remove local strong reference to Client before deleting the RecordTrack so that the Client | 
|  | 4174 | // destructor is called by the TrackBase destructor with mLock held | 
|  | 4175 | client.clear(); | 
|  | 4176 | recordTrack.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4177 | goto Exit; | 
|  | 4178 | } | 
|  | 4179 |  | 
|  | 4180 | // return to handle to client | 
|  | 4181 | recordHandle = new RecordHandle(recordTrack); | 
|  | 4182 | lStatus = NO_ERROR; | 
|  | 4183 |  | 
|  | 4184 | Exit: | 
|  | 4185 | if (status) { | 
|  | 4186 | *status = lStatus; | 
|  | 4187 | } | 
|  | 4188 | return recordHandle; | 
|  | 4189 | } | 
|  | 4190 |  | 
|  | 4191 | // ---------------------------------------------------------------------------- | 
|  | 4192 |  | 
|  | 4193 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) | 
|  | 4194 | : BnAudioRecord(), | 
|  | 4195 | mRecordTrack(recordTrack) | 
|  | 4196 | { | 
|  | 4197 | } | 
|  | 4198 |  | 
|  | 4199 | AudioFlinger::RecordHandle::~RecordHandle() { | 
|  | 4200 | stop(); | 
|  | 4201 | } | 
|  | 4202 |  | 
|  | 4203 | status_t AudioFlinger::RecordHandle::start() { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4204 | ALOGV("RecordHandle::start()"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4205 | return mRecordTrack->start(); | 
|  | 4206 | } | 
|  | 4207 |  | 
|  | 4208 | void AudioFlinger::RecordHandle::stop() { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4209 | ALOGV("RecordHandle::stop()"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4210 | mRecordTrack->stop(); | 
|  | 4211 | } | 
|  | 4212 |  | 
|  | 4213 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { | 
|  | 4214 | return mRecordTrack->getCblk(); | 
|  | 4215 | } | 
|  | 4216 |  | 
|  | 4217 | status_t AudioFlinger::RecordHandle::onTransact( | 
|  | 4218 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 4219 | { | 
|  | 4220 | return BnAudioRecord::onTransact(code, data, reply, flags); | 
|  | 4221 | } | 
|  | 4222 |  | 
|  | 4223 | // ---------------------------------------------------------------------------- | 
|  | 4224 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4225 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
|  | 4226 | AudioStreamIn *input, | 
|  | 4227 | uint32_t sampleRate, | 
|  | 4228 | uint32_t channels, | 
|  | 4229 | int id, | 
|  | 4230 | uint32_t device) : | 
|  | 4231 | ThreadBase(audioFlinger, id, device), | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4232 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4233 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4234 | mType = ThreadBase::RECORD; | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4235 |  | 
|  | 4236 | snprintf(mName, kNameLength, "AudioIn_%d", id); | 
|  | 4237 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4238 | mReqChannelCount = popcount(channels); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4239 | mReqSampleRate = sampleRate; | 
|  | 4240 | readInputParameters(); | 
|  | 4241 | } | 
|  | 4242 |  | 
|  | 4243 |  | 
|  | 4244 | AudioFlinger::RecordThread::~RecordThread() | 
|  | 4245 | { | 
|  | 4246 | delete[] mRsmpInBuffer; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4247 | if (mResampler != NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4248 | delete mResampler; | 
|  | 4249 | delete[] mRsmpOutBuffer; | 
|  | 4250 | } | 
|  | 4251 | } | 
|  | 4252 |  | 
|  | 4253 | void AudioFlinger::RecordThread::onFirstRef() | 
|  | 4254 | { | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4255 | run(mName, PRIORITY_URGENT_AUDIO); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4256 | } | 
|  | 4257 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4258 | status_t AudioFlinger::RecordThread::readyToRun() | 
|  | 4259 | { | 
|  | 4260 | status_t status = initCheck(); | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4261 | ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4262 | return status; | 
|  | 4263 | } | 
|  | 4264 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4265 | bool AudioFlinger::RecordThread::threadLoop() | 
|  | 4266 | { | 
|  | 4267 | AudioBufferProvider::Buffer buffer; | 
|  | 4268 | sp<RecordTrack> activeTrack; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4269 | Vector< sp<EffectChain> > effectChains; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4270 |  | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4271 | nsecs_t lastWarning = 0; | 
|  | 4272 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4273 | acquireWakeLock(); | 
|  | 4274 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4275 | // start recording | 
|  | 4276 | while (!exitPending()) { | 
|  | 4277 |  | 
|  | 4278 | processConfigEvents(); | 
|  | 4279 |  | 
|  | 4280 | { // scope for mLock | 
|  | 4281 | Mutex::Autolock _l(mLock); | 
|  | 4282 | checkForNewParameters_l(); | 
|  | 4283 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { | 
|  | 4284 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4285 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4286 | mStandby = true; | 
|  | 4287 | } | 
|  | 4288 |  | 
|  | 4289 | if (exitPending()) break; | 
|  | 4290 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4291 | releaseWakeLock_l(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4292 | ALOGV("RecordThread: loop stopping"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4293 | // go to sleep | 
|  | 4294 | mWaitWorkCV.wait(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4295 | ALOGV("RecordThread: loop starting"); | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4296 | acquireWakeLock_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4297 | continue; | 
|  | 4298 | } | 
|  | 4299 | if (mActiveTrack != 0) { | 
|  | 4300 | if (mActiveTrack->mState == TrackBase::PAUSING) { | 
|  | 4301 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4302 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4303 | mStandby = true; | 
|  | 4304 | } | 
|  | 4305 | mActiveTrack.clear(); | 
|  | 4306 | mStartStopCond.broadcast(); | 
|  | 4307 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { | 
|  | 4308 | if (mReqChannelCount != mActiveTrack->channelCount()) { | 
|  | 4309 | mActiveTrack.clear(); | 
|  | 4310 | mStartStopCond.broadcast(); | 
|  | 4311 | } else if (mBytesRead != 0) { | 
|  | 4312 | // record start succeeds only if first read from audio input | 
|  | 4313 | // succeeds | 
|  | 4314 | if (mBytesRead > 0) { | 
|  | 4315 | mActiveTrack->mState = TrackBase::ACTIVE; | 
|  | 4316 | } else { | 
|  | 4317 | mActiveTrack.clear(); | 
|  | 4318 | } | 
|  | 4319 | mStartStopCond.broadcast(); | 
|  | 4320 | } | 
|  | 4321 | mStandby = false; | 
|  | 4322 | } | 
|  | 4323 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4324 | lockEffectChains_l(effectChains); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4325 | } | 
|  | 4326 |  | 
|  | 4327 | if (mActiveTrack != 0) { | 
|  | 4328 | if (mActiveTrack->mState != TrackBase::ACTIVE && | 
|  | 4329 | mActiveTrack->mState != TrackBase::RESUMING) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4330 | unlockEffectChains(effectChains); | 
|  | 4331 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4332 | continue; | 
|  | 4333 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4334 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
|  | 4335 | effectChains[i]->process_l(); | 
|  | 4336 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4337 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4338 | buffer.frameCount = mFrameCount; | 
| Glenn Kasten | f6b1678 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 4339 | if (CC_LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4340 | size_t framesOut = buffer.frameCount; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4341 | if (mResampler == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4342 | // no resampling | 
|  | 4343 | while (framesOut) { | 
|  | 4344 | size_t framesIn = mFrameCount - mRsmpInIndex; | 
|  | 4345 | if (framesIn) { | 
|  | 4346 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; | 
|  | 4347 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; | 
|  | 4348 | if (framesIn > framesOut) | 
|  | 4349 | framesIn = framesOut; | 
|  | 4350 | mRsmpInIndex += framesIn; | 
|  | 4351 | framesOut -= framesIn; | 
|  | 4352 | if ((int)mChannelCount == mReqChannelCount || | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4353 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4354 | memcpy(dst, src, framesIn * mFrameSize); | 
|  | 4355 | } else { | 
|  | 4356 | int16_t *src16 = (int16_t *)src; | 
|  | 4357 | int16_t *dst16 = (int16_t *)dst; | 
|  | 4358 | if (mChannelCount == 1) { | 
|  | 4359 | while (framesIn--) { | 
|  | 4360 | *dst16++ = *src16; | 
|  | 4361 | *dst16++ = *src16++; | 
|  | 4362 | } | 
|  | 4363 | } else { | 
|  | 4364 | while (framesIn--) { | 
|  | 4365 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); | 
|  | 4366 | src16 += 2; | 
|  | 4367 | } | 
|  | 4368 | } | 
|  | 4369 | } | 
|  | 4370 | } | 
|  | 4371 | if (framesOut && mFrameCount == mRsmpInIndex) { | 
|  | 4372 | if (framesOut == mFrameCount && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4373 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4374 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4375 | framesOut = 0; | 
|  | 4376 | } else { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4377 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4378 | mRsmpInIndex = 0; | 
|  | 4379 | } | 
|  | 4380 | if (mBytesRead < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4381 | ALOGE("Error reading audio input"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4382 | if (mActiveTrack->mState == TrackBase::ACTIVE) { | 
|  | 4383 | // Force input into standby so that it tries to | 
|  | 4384 | // recover at next read attempt | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4385 | mInput->stream->common.standby(&mInput->stream->common); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4386 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4387 | } | 
|  | 4388 | mRsmpInIndex = mFrameCount; | 
|  | 4389 | framesOut = 0; | 
|  | 4390 | buffer.frameCount = 0; | 
|  | 4391 | } | 
|  | 4392 | } | 
|  | 4393 | } | 
|  | 4394 | } else { | 
|  | 4395 | // resampling | 
|  | 4396 |  | 
|  | 4397 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); | 
|  | 4398 | // alter output frame count as if we were expecting stereo samples | 
|  | 4399 | if (mChannelCount == 1 && mReqChannelCount == 1) { | 
|  | 4400 | framesOut >>= 1; | 
|  | 4401 | } | 
|  | 4402 | mResampler->resample(mRsmpOutBuffer, framesOut, this); | 
|  | 4403 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() | 
|  | 4404 | // are 32 bit aligned which should be always true. | 
|  | 4405 | if (mChannelCount == 2 && mReqChannelCount == 1) { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4406 | ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4407 | // the resampler always outputs stereo samples: do post stereo to mono conversion | 
|  | 4408 | int16_t *src = (int16_t *)mRsmpOutBuffer; | 
|  | 4409 | int16_t *dst = buffer.i16; | 
|  | 4410 | while (framesOut--) { | 
|  | 4411 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); | 
|  | 4412 | src += 2; | 
|  | 4413 | } | 
|  | 4414 | } else { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 4415 | ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4416 | } | 
|  | 4417 |  | 
|  | 4418 | } | 
|  | 4419 | mActiveTrack->releaseBuffer(&buffer); | 
|  | 4420 | mActiveTrack->overflow(); | 
|  | 4421 | } | 
|  | 4422 | // client isn't retrieving buffers fast enough | 
|  | 4423 | else { | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4424 | if (!mActiveTrack->setOverflow()) { | 
|  | 4425 | nsecs_t now = systemTime(); | 
| Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 4426 | if ((now - lastWarning) > kWarningThrottleNs) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4427 | ALOGW("RecordThread: buffer overflow"); | 
| Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4428 | lastWarning = now; | 
|  | 4429 | } | 
|  | 4430 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4431 | // Release the processor for a while before asking for a new buffer. | 
|  | 4432 | // This will give the application more chance to read from the buffer and | 
|  | 4433 | // clear the overflow. | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4434 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4435 | } | 
|  | 4436 | } | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4437 | // enable changes in effect chain | 
|  | 4438 | unlockEffectChains(effectChains); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4439 | effectChains.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4440 | } | 
|  | 4441 |  | 
|  | 4442 | if (!mStandby) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4443 | mInput->stream->common.standby(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4444 | } | 
|  | 4445 | mActiveTrack.clear(); | 
|  | 4446 |  | 
|  | 4447 | mStartStopCond.broadcast(); | 
|  | 4448 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4449 | releaseWakeLock(); | 
|  | 4450 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4451 | ALOGV("RecordThread %p exiting", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4452 | return false; | 
|  | 4453 | } | 
|  | 4454 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4455 |  | 
|  | 4456 | sp<AudioFlinger::RecordThread::RecordTrack>  AudioFlinger::RecordThread::createRecordTrack_l( | 
|  | 4457 | const sp<AudioFlinger::Client>& client, | 
|  | 4458 | uint32_t sampleRate, | 
|  | 4459 | int format, | 
|  | 4460 | int channelMask, | 
|  | 4461 | int frameCount, | 
|  | 4462 | uint32_t flags, | 
|  | 4463 | int sessionId, | 
|  | 4464 | status_t *status) | 
|  | 4465 | { | 
|  | 4466 | sp<RecordTrack> track; | 
|  | 4467 | status_t lStatus; | 
|  | 4468 |  | 
|  | 4469 | lStatus = initCheck(); | 
|  | 4470 | if (lStatus != NO_ERROR) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4471 | ALOGE("Audio driver not initialized."); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4472 | goto Exit; | 
|  | 4473 | } | 
|  | 4474 |  | 
|  | 4475 | { // scope for mLock | 
|  | 4476 | Mutex::Autolock _l(mLock); | 
|  | 4477 |  | 
|  | 4478 | track = new RecordTrack(this, client, sampleRate, | 
|  | 4479 | format, channelMask, frameCount, flags, sessionId); | 
|  | 4480 |  | 
|  | 4481 | if (track->getCblk() == NULL) { | 
|  | 4482 | lStatus = NO_MEMORY; | 
|  | 4483 | goto Exit; | 
|  | 4484 | } | 
|  | 4485 |  | 
|  | 4486 | mTrack = track.get(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4487 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 4488 | bool suspend = audio_is_bluetooth_sco_device( | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4489 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4490 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
|  | 4491 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4492 | } | 
|  | 4493 | lStatus = NO_ERROR; | 
|  | 4494 |  | 
|  | 4495 | Exit: | 
|  | 4496 | if (status) { | 
|  | 4497 | *status = lStatus; | 
|  | 4498 | } | 
|  | 4499 | return track; | 
|  | 4500 | } | 
|  | 4501 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4502 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) | 
|  | 4503 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4504 | ALOGV("RecordThread::start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4505 | sp <ThreadBase> strongMe = this; | 
|  | 4506 | status_t status = NO_ERROR; | 
|  | 4507 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4508 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4509 | if (mActiveTrack != 0) { | 
|  | 4510 | if (recordTrack != mActiveTrack.get()) { | 
|  | 4511 | status = -EBUSY; | 
|  | 4512 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { | 
|  | 4513 | mActiveTrack->mState = TrackBase::ACTIVE; | 
|  | 4514 | } | 
|  | 4515 | return status; | 
|  | 4516 | } | 
|  | 4517 |  | 
|  | 4518 | recordTrack->mState = TrackBase::IDLE; | 
|  | 4519 | mActiveTrack = recordTrack; | 
|  | 4520 | mLock.unlock(); | 
|  | 4521 | status_t status = AudioSystem::startInput(mId); | 
|  | 4522 | mLock.lock(); | 
|  | 4523 | if (status != NO_ERROR) { | 
|  | 4524 | mActiveTrack.clear(); | 
|  | 4525 | return status; | 
|  | 4526 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4527 | mRsmpInIndex = mFrameCount; | 
|  | 4528 | mBytesRead = 0; | 
| Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4529 | if (mResampler != NULL) { | 
|  | 4530 | mResampler->reset(); | 
|  | 4531 | } | 
|  | 4532 | mActiveTrack->mState = TrackBase::RESUMING; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4533 | // signal thread to start | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4534 | ALOGV("Signal record thread"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4535 | mWaitWorkCV.signal(); | 
|  | 4536 | // do not wait for mStartStopCond if exiting | 
|  | 4537 | if (mExiting) { | 
|  | 4538 | mActiveTrack.clear(); | 
|  | 4539 | status = INVALID_OPERATION; | 
|  | 4540 | goto startError; | 
|  | 4541 | } | 
|  | 4542 | mStartStopCond.wait(mLock); | 
|  | 4543 | if (mActiveTrack == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4544 | ALOGV("Record failed to start"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4545 | status = BAD_VALUE; | 
|  | 4546 | goto startError; | 
|  | 4547 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4548 | ALOGV("Record started OK"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4549 | return status; | 
|  | 4550 | } | 
|  | 4551 | startError: | 
|  | 4552 | AudioSystem::stopInput(mId); | 
|  | 4553 | return status; | 
|  | 4554 | } | 
|  | 4555 |  | 
|  | 4556 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4557 | ALOGV("RecordThread::stop"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4558 | sp <ThreadBase> strongMe = this; | 
|  | 4559 | { | 
| Glenn Kasten | a7d8d6f | 2012-01-05 15:41:56 -0800 | [diff] [blame] | 4560 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4561 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { | 
|  | 4562 | mActiveTrack->mState = TrackBase::PAUSING; | 
|  | 4563 | // do not wait for mStartStopCond if exiting | 
|  | 4564 | if (mExiting) { | 
|  | 4565 | return; | 
|  | 4566 | } | 
|  | 4567 | mStartStopCond.wait(mLock); | 
|  | 4568 | // if we have been restarted, recordTrack == mActiveTrack.get() here | 
|  | 4569 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { | 
|  | 4570 | mLock.unlock(); | 
|  | 4571 | AudioSystem::stopInput(mId); | 
|  | 4572 | mLock.lock(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4573 | ALOGV("Record stopped OK"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4574 | } | 
|  | 4575 | } | 
|  | 4576 | } | 
|  | 4577 | } | 
|  | 4578 |  | 
|  | 4579 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
|  | 4580 | { | 
|  | 4581 | const size_t SIZE = 256; | 
|  | 4582 | char buffer[SIZE]; | 
|  | 4583 | String8 result; | 
|  | 4584 | pid_t pid = 0; | 
|  | 4585 |  | 
|  | 4586 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); | 
|  | 4587 | result.append(buffer); | 
|  | 4588 |  | 
|  | 4589 | if (mActiveTrack != 0) { | 
|  | 4590 | result.append("Active Track:\n"); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4591 | 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] | 4592 | mActiveTrack->dump(buffer, SIZE); | 
|  | 4593 | result.append(buffer); | 
|  | 4594 |  | 
|  | 4595 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); | 
|  | 4596 | result.append(buffer); | 
|  | 4597 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); | 
|  | 4598 | result.append(buffer); | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4599 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4600 | result.append(buffer); | 
|  | 4601 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); | 
|  | 4602 | result.append(buffer); | 
|  | 4603 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); | 
|  | 4604 | result.append(buffer); | 
|  | 4605 |  | 
|  | 4606 |  | 
|  | 4607 | } else { | 
|  | 4608 | result.append("No record client\n"); | 
|  | 4609 | } | 
|  | 4610 | write(fd, result.string(), result.size()); | 
|  | 4611 |  | 
|  | 4612 | dumpBase(fd, args); | 
| Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4613 | dumpEffectChains(fd, args); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4614 |  | 
|  | 4615 | return NO_ERROR; | 
|  | 4616 | } | 
|  | 4617 |  | 
|  | 4618 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 4619 | { | 
|  | 4620 | size_t framesReq = buffer->frameCount; | 
|  | 4621 | size_t framesReady = mFrameCount - mRsmpInIndex; | 
|  | 4622 | int channelCount; | 
|  | 4623 |  | 
|  | 4624 | if (framesReady == 0) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4625 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4626 | if (mBytesRead < 0) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4627 | ALOGE("RecordThread::getNextBuffer() Error reading audio input"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4628 | if (mActiveTrack->mState == TrackBase::ACTIVE) { | 
|  | 4629 | // Force input into standby so that it tries to | 
|  | 4630 | // recover at next read attempt | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4631 | mInput->stream->common.standby(&mInput->stream->common); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4632 | usleep(kRecordThreadSleepUs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4633 | } | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4634 | buffer->raw = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4635 | buffer->frameCount = 0; | 
|  | 4636 | return NOT_ENOUGH_DATA; | 
|  | 4637 | } | 
|  | 4638 | mRsmpInIndex = 0; | 
|  | 4639 | framesReady = mFrameCount; | 
|  | 4640 | } | 
|  | 4641 |  | 
|  | 4642 | if (framesReq > framesReady) { | 
|  | 4643 | framesReq = framesReady; | 
|  | 4644 | } | 
|  | 4645 |  | 
|  | 4646 | if (mChannelCount == 1 && mReqChannelCount == 2) { | 
|  | 4647 | channelCount = 1; | 
|  | 4648 | } else { | 
|  | 4649 | channelCount = 2; | 
|  | 4650 | } | 
|  | 4651 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; | 
|  | 4652 | buffer->frameCount = framesReq; | 
|  | 4653 | return NO_ERROR; | 
|  | 4654 | } | 
|  | 4655 |  | 
|  | 4656 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
|  | 4657 | { | 
|  | 4658 | mRsmpInIndex += buffer->frameCount; | 
|  | 4659 | buffer->frameCount = 0; | 
|  | 4660 | } | 
|  | 4661 |  | 
|  | 4662 | bool AudioFlinger::RecordThread::checkForNewParameters_l() | 
|  | 4663 | { | 
|  | 4664 | bool reconfig = false; | 
|  | 4665 |  | 
|  | 4666 | while (!mNewParameters.isEmpty()) { | 
|  | 4667 | status_t status = NO_ERROR; | 
|  | 4668 | String8 keyValuePair = mNewParameters[0]; | 
|  | 4669 | AudioParameter param = AudioParameter(keyValuePair); | 
|  | 4670 | int value; | 
|  | 4671 | int reqFormat = mFormat; | 
|  | 4672 | int reqSamplingRate = mReqSampleRate; | 
|  | 4673 | int reqChannelCount = mReqChannelCount; | 
|  | 4674 |  | 
|  | 4675 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
|  | 4676 | reqSamplingRate = value; | 
|  | 4677 | reconfig = true; | 
|  | 4678 | } | 
|  | 4679 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
|  | 4680 | reqFormat = value; | 
|  | 4681 | reconfig = true; | 
|  | 4682 | } | 
|  | 4683 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4684 | reqChannelCount = popcount(value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4685 | reconfig = true; | 
|  | 4686 | } | 
|  | 4687 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
|  | 4688 | // do not accept frame count changes if tracks are open as the track buffer | 
|  | 4689 | // size depends on frame count and correct behavior would not be garantied | 
|  | 4690 | // if frame count is changed after track creation | 
|  | 4691 | if (mActiveTrack != 0) { | 
|  | 4692 | status = INVALID_OPERATION; | 
|  | 4693 | } else { | 
|  | 4694 | reconfig = true; | 
|  | 4695 | } | 
|  | 4696 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4697 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
|  | 4698 | // forward device change to effects that have requested to be | 
|  | 4699 | // aware of attached audio device. | 
|  | 4700 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 4701 | mEffectChains[i]->setDevice_l(value); | 
|  | 4702 | } | 
|  | 4703 | // store input device and output device but do not forward output device to audio HAL. | 
|  | 4704 | // Note that status is ignored by the caller for output device | 
|  | 4705 | // (see AudioFlinger::setParameters() | 
|  | 4706 | if (value & AUDIO_DEVICE_OUT_ALL) { | 
|  | 4707 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); | 
|  | 4708 | status = BAD_VALUE; | 
|  | 4709 | } else { | 
|  | 4710 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4711 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
|  | 4712 | if (mTrack != NULL) { | 
|  | 4713 | bool suspend = audio_is_bluetooth_sco_device( | 
| Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4714 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4715 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); | 
|  | 4716 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); | 
|  | 4717 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4718 | } | 
|  | 4719 | mDevice |= (uint32_t)value; | 
|  | 4720 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4721 | if (status == NO_ERROR) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4722 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4723 | if (status == INVALID_OPERATION) { | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4724 | mInput->stream->common.standby(&mInput->stream->common); | 
|  | 4725 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4726 | } | 
|  | 4727 | if (reconfig) { | 
|  | 4728 | if (status == BAD_VALUE && | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4729 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4730 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4731 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && | 
|  | 4732 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4733 | (reqChannelCount < 3)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4734 | status = NO_ERROR; | 
|  | 4735 | } | 
|  | 4736 | if (status == NO_ERROR) { | 
|  | 4737 | readInputParameters(); | 
|  | 4738 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); | 
|  | 4739 | } | 
|  | 4740 | } | 
|  | 4741 | } | 
|  | 4742 |  | 
|  | 4743 | mNewParameters.removeAt(0); | 
|  | 4744 |  | 
|  | 4745 | mParamStatus = status; | 
|  | 4746 | mParamCond.signal(); | 
| Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4747 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
|  | 4748 | // 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] | 4749 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4750 | } | 
|  | 4751 | return reconfig; | 
|  | 4752 | } | 
|  | 4753 |  | 
|  | 4754 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
|  | 4755 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4756 | char *s; | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4757 | String8 out_s8 = String8(); | 
|  | 4758 |  | 
|  | 4759 | Mutex::Autolock _l(mLock); | 
|  | 4760 | if (initCheck() != NO_ERROR) { | 
|  | 4761 | return out_s8; | 
|  | 4762 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4763 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4764 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4765 | out_s8 = String8(s); | 
|  | 4766 | free(s); | 
|  | 4767 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4768 | } | 
|  | 4769 |  | 
|  | 4770 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { | 
|  | 4771 | AudioSystem::OutputDescriptor desc; | 
|  | 4772 | void *param2 = 0; | 
|  | 4773 |  | 
|  | 4774 | switch (event) { | 
|  | 4775 | case AudioSystem::INPUT_OPENED: | 
|  | 4776 | case AudioSystem::INPUT_CONFIG_CHANGED: | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4777 | desc.channels = mChannelMask; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4778 | desc.samplingRate = mSampleRate; | 
|  | 4779 | desc.format = mFormat; | 
|  | 4780 | desc.frameCount = mFrameCount; | 
|  | 4781 | desc.latency = 0; | 
|  | 4782 | param2 = &desc; | 
|  | 4783 | break; | 
|  | 4784 |  | 
|  | 4785 | case AudioSystem::INPUT_CLOSED: | 
|  | 4786 | default: | 
|  | 4787 | break; | 
|  | 4788 | } | 
|  | 4789 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
|  | 4790 | } | 
|  | 4791 |  | 
|  | 4792 | void AudioFlinger::RecordThread::readInputParameters() | 
|  | 4793 | { | 
|  | 4794 | if (mRsmpInBuffer) delete mRsmpInBuffer; | 
|  | 4795 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; | 
|  | 4796 | if (mResampler) delete mResampler; | 
| Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame] | 4797 | mResampler = NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4798 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4799 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
| Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4800 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
|  | 4801 | mChannelCount = (uint16_t)popcount(mChannelMask); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4802 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
|  | 4803 | mFrameSize = (uint16_t)audio_stream_frame_size(&mInput->stream->common); | 
|  | 4804 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4805 | mFrameCount = mInputBytes / mFrameSize; | 
|  | 4806 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; | 
|  | 4807 |  | 
|  | 4808 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) | 
|  | 4809 | { | 
|  | 4810 | int channelCount; | 
|  | 4811 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid | 
|  | 4812 | // stereo to mono post process as the resampler always outputs stereo. | 
|  | 4813 | if (mChannelCount == 1 && mReqChannelCount == 2) { | 
|  | 4814 | channelCount = 1; | 
|  | 4815 | } else { | 
|  | 4816 | channelCount = 2; | 
|  | 4817 | } | 
|  | 4818 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); | 
|  | 4819 | mResampler->setSampleRate(mSampleRate); | 
|  | 4820 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); | 
|  | 4821 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; | 
|  | 4822 |  | 
|  | 4823 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples | 
|  | 4824 | if (mChannelCount == 1 && mReqChannelCount == 1) { | 
|  | 4825 | mFrameCount >>= 1; | 
|  | 4826 | } | 
|  | 4827 |  | 
|  | 4828 | } | 
|  | 4829 | mRsmpInIndex = mFrameCount; | 
|  | 4830 | } | 
|  | 4831 |  | 
|  | 4832 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() | 
|  | 4833 | { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4834 | Mutex::Autolock _l(mLock); | 
|  | 4835 | if (initCheck() != NO_ERROR) { | 
|  | 4836 | return 0; | 
|  | 4837 | } | 
|  | 4838 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4839 | return mInput->stream->get_input_frames_lost(mInput->stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4840 | } | 
|  | 4841 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4842 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) | 
|  | 4843 | { | 
|  | 4844 | Mutex::Autolock _l(mLock); | 
|  | 4845 | uint32_t result = 0; | 
|  | 4846 | if (getEffectChain_l(sessionId) != 0) { | 
|  | 4847 | result = EFFECT_SESSION; | 
|  | 4848 | } | 
|  | 4849 |  | 
|  | 4850 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { | 
|  | 4851 | result |= TRACK_SESSION; | 
|  | 4852 | } | 
|  | 4853 |  | 
|  | 4854 | return result; | 
|  | 4855 | } | 
|  | 4856 |  | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4857 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() | 
|  | 4858 | { | 
|  | 4859 | Mutex::Autolock _l(mLock); | 
|  | 4860 | return mTrack; | 
|  | 4861 | } | 
|  | 4862 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4863 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() | 
|  | 4864 | { | 
|  | 4865 | Mutex::Autolock _l(mLock); | 
|  | 4866 | return mInput; | 
|  | 4867 | } | 
|  | 4868 |  | 
|  | 4869 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
|  | 4870 | { | 
|  | 4871 | Mutex::Autolock _l(mLock); | 
|  | 4872 | AudioStreamIn *input = mInput; | 
|  | 4873 | mInput = NULL; | 
|  | 4874 | return input; | 
|  | 4875 | } | 
|  | 4876 |  | 
|  | 4877 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
|  | 4878 | audio_stream_t* AudioFlinger::RecordThread::stream() | 
|  | 4879 | { | 
|  | 4880 | if (mInput == NULL) { | 
|  | 4881 | return NULL; | 
|  | 4882 | } | 
|  | 4883 | return &mInput->stream->common; | 
|  | 4884 | } | 
|  | 4885 |  | 
|  | 4886 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4887 | // ---------------------------------------------------------------------------- | 
|  | 4888 |  | 
|  | 4889 | int AudioFlinger::openOutput(uint32_t *pDevices, | 
|  | 4890 | uint32_t *pSamplingRate, | 
|  | 4891 | uint32_t *pFormat, | 
|  | 4892 | uint32_t *pChannels, | 
|  | 4893 | uint32_t *pLatencyMs, | 
|  | 4894 | uint32_t flags) | 
|  | 4895 | { | 
|  | 4896 | status_t status; | 
|  | 4897 | PlaybackThread *thread = NULL; | 
|  | 4898 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; | 
|  | 4899 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; | 
|  | 4900 | uint32_t format = pFormat ? *pFormat : 0; | 
|  | 4901 | uint32_t channels = pChannels ? *pChannels : 0; | 
|  | 4902 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4903 | audio_stream_out_t *outStream; | 
|  | 4904 | audio_hw_device_t *outHwDev; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4905 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4906 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4907 | pDevices ? *pDevices : 0, | 
|  | 4908 | samplingRate, | 
|  | 4909 | format, | 
|  | 4910 | channels, | 
|  | 4911 | flags); | 
|  | 4912 |  | 
|  | 4913 | if (pDevices == NULL || *pDevices == 0) { | 
|  | 4914 | return 0; | 
|  | 4915 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4916 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4917 | Mutex::Autolock _l(mLock); | 
|  | 4918 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4919 | outHwDev = findSuitableHwDev_l(*pDevices); | 
|  | 4920 | if (outHwDev == NULL) | 
|  | 4921 | return 0; | 
|  | 4922 |  | 
|  | 4923 | status = outHwDev->open_output_stream(outHwDev, *pDevices, (int *)&format, | 
|  | 4924 | &channels, &samplingRate, &outStream); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4925 | 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] | 4926 | outStream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4927 | samplingRate, | 
|  | 4928 | format, | 
|  | 4929 | channels, | 
|  | 4930 | status); | 
|  | 4931 |  | 
|  | 4932 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4933 | if (outStream != NULL) { | 
|  | 4934 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4935 | int id = nextUniqueId(); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4936 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4937 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || | 
|  | 4938 | (format != AUDIO_FORMAT_PCM_16_BIT) || | 
|  | 4939 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4940 | thread = new DirectOutputThread(this, output, id, *pDevices); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4941 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4942 | } else { | 
|  | 4943 | thread = new MixerThread(this, output, id, *pDevices); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4944 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4945 | } | 
|  | 4946 | mPlaybackThreads.add(id, thread); | 
|  | 4947 |  | 
|  | 4948 | if (pSamplingRate) *pSamplingRate = samplingRate; | 
|  | 4949 | if (pFormat) *pFormat = format; | 
|  | 4950 | if (pChannels) *pChannels = channels; | 
|  | 4951 | if (pLatencyMs) *pLatencyMs = thread->latency(); | 
|  | 4952 |  | 
|  | 4953 | // notify client processes of the new output creation | 
|  | 4954 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); | 
|  | 4955 | return id; | 
|  | 4956 | } | 
|  | 4957 |  | 
|  | 4958 | return 0; | 
|  | 4959 | } | 
|  | 4960 |  | 
|  | 4961 | int AudioFlinger::openDuplicateOutput(int output1, int output2) | 
|  | 4962 | { | 
|  | 4963 | Mutex::Autolock _l(mLock); | 
|  | 4964 | MixerThread *thread1 = checkMixerThread_l(output1); | 
|  | 4965 | MixerThread *thread2 = checkMixerThread_l(output2); | 
|  | 4966 |  | 
|  | 4967 | if (thread1 == NULL || thread2 == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 4968 | 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] | 4969 | return 0; | 
|  | 4970 | } | 
|  | 4971 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4972 | int id = nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4973 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); | 
|  | 4974 | thread->addOutputTrack(thread2); | 
|  | 4975 | mPlaybackThreads.add(id, thread); | 
|  | 4976 | // notify client processes of the new output creation | 
|  | 4977 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); | 
|  | 4978 | return id; | 
|  | 4979 | } | 
|  | 4980 |  | 
|  | 4981 | status_t AudioFlinger::closeOutput(int output) | 
|  | 4982 | { | 
|  | 4983 | // keep strong reference on the playback thread so that | 
|  | 4984 | // it is not destroyed while exit() is executed | 
|  | 4985 | sp <PlaybackThread> thread; | 
|  | 4986 | { | 
|  | 4987 | Mutex::Autolock _l(mLock); | 
|  | 4988 | thread = checkPlaybackThread_l(output); | 
|  | 4989 | if (thread == NULL) { | 
|  | 4990 | return BAD_VALUE; | 
|  | 4991 | } | 
|  | 4992 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4993 | ALOGV("closeOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4994 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4995 | if (thread->type() == ThreadBase::MIXER) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4996 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4997 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4998 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); | 
|  | 4999 | dupThread->removeOutputTrack((MixerThread *)thread.get()); | 
|  | 5000 | } | 
|  | 5001 | } | 
|  | 5002 | } | 
|  | 5003 | void *param2 = 0; | 
|  | 5004 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); | 
|  | 5005 | mPlaybackThreads.removeItem(output); | 
|  | 5006 | } | 
|  | 5007 | thread->exit(); | 
|  | 5008 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5009 | if (thread->type() != ThreadBase::DUPLICATING) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5010 | AudioStreamOut *out = thread->clearOutput(); | 
|  | 5011 | // from now on thread->mOutput is NULL | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5012 | out->hwDev->close_output_stream(out->hwDev, out->stream); | 
|  | 5013 | delete out; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5014 | } | 
|  | 5015 | return NO_ERROR; | 
|  | 5016 | } | 
|  | 5017 |  | 
|  | 5018 | status_t AudioFlinger::suspendOutput(int output) | 
|  | 5019 | { | 
|  | 5020 | Mutex::Autolock _l(mLock); | 
|  | 5021 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5022 |  | 
|  | 5023 | if (thread == NULL) { | 
|  | 5024 | return BAD_VALUE; | 
|  | 5025 | } | 
|  | 5026 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5027 | ALOGV("suspendOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5028 | thread->suspend(); | 
|  | 5029 |  | 
|  | 5030 | return NO_ERROR; | 
|  | 5031 | } | 
|  | 5032 |  | 
|  | 5033 | status_t AudioFlinger::restoreOutput(int output) | 
|  | 5034 | { | 
|  | 5035 | Mutex::Autolock _l(mLock); | 
|  | 5036 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5037 |  | 
|  | 5038 | if (thread == NULL) { | 
|  | 5039 | return BAD_VALUE; | 
|  | 5040 | } | 
|  | 5041 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5042 | ALOGV("restoreOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5043 |  | 
|  | 5044 | thread->restore(); | 
|  | 5045 |  | 
|  | 5046 | return NO_ERROR; | 
|  | 5047 | } | 
|  | 5048 |  | 
|  | 5049 | int AudioFlinger::openInput(uint32_t *pDevices, | 
|  | 5050 | uint32_t *pSamplingRate, | 
|  | 5051 | uint32_t *pFormat, | 
|  | 5052 | uint32_t *pChannels, | 
|  | 5053 | uint32_t acoustics) | 
|  | 5054 | { | 
|  | 5055 | status_t status; | 
|  | 5056 | RecordThread *thread = NULL; | 
|  | 5057 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; | 
|  | 5058 | uint32_t format = pFormat ? *pFormat : 0; | 
|  | 5059 | uint32_t channels = pChannels ? *pChannels : 0; | 
|  | 5060 | uint32_t reqSamplingRate = samplingRate; | 
|  | 5061 | uint32_t reqFormat = format; | 
|  | 5062 | uint32_t reqChannels = channels; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5063 | audio_stream_in_t *inStream; | 
|  | 5064 | audio_hw_device_t *inHwDev; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5065 |  | 
|  | 5066 | if (pDevices == NULL || *pDevices == 0) { | 
|  | 5067 | return 0; | 
|  | 5068 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5069 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5070 | Mutex::Autolock _l(mLock); | 
|  | 5071 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5072 | inHwDev = findSuitableHwDev_l(*pDevices); | 
|  | 5073 | if (inHwDev == NULL) | 
|  | 5074 | return 0; | 
|  | 5075 |  | 
|  | 5076 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, | 
|  | 5077 | &channels, &samplingRate, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5078 | (audio_in_acoustics_t)acoustics, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5079 | &inStream); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5080 | 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] | 5081 | inStream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5082 | samplingRate, | 
|  | 5083 | format, | 
|  | 5084 | channels, | 
|  | 5085 | acoustics, | 
|  | 5086 | status); | 
|  | 5087 |  | 
|  | 5088 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, | 
|  | 5089 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo | 
|  | 5090 | // or stereo to mono conversions on 16 bit PCM inputs. | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5091 | if (inStream == NULL && status == BAD_VALUE && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5092 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5093 | (samplingRate <= 2 * reqSamplingRate) && | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5094 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5095 | ALOGV("openInput() reopening with proposed sampling rate and channels"); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5096 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, | 
|  | 5097 | &channels, &samplingRate, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5098 | (audio_in_acoustics_t)acoustics, | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5099 | &inStream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5100 | } | 
|  | 5101 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5102 | if (inStream != NULL) { | 
|  | 5103 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); | 
|  | 5104 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5105 | int id = nextUniqueId(); | 
|  | 5106 | // Start record thread | 
|  | 5107 | // RecorThread require both input and output device indication to forward to audio | 
|  | 5108 | // pre processing modules | 
|  | 5109 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); | 
|  | 5110 | thread = new RecordThread(this, | 
|  | 5111 | input, | 
|  | 5112 | reqSamplingRate, | 
|  | 5113 | reqChannels, | 
|  | 5114 | id, | 
|  | 5115 | device); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5116 | mRecordThreads.add(id, thread); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5117 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5118 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; | 
|  | 5119 | if (pFormat) *pFormat = format; | 
|  | 5120 | if (pChannels) *pChannels = reqChannels; | 
|  | 5121 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5122 | input->stream->common.standby(&input->stream->common); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5123 |  | 
|  | 5124 | // notify client processes of the new input creation | 
|  | 5125 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); | 
|  | 5126 | return id; | 
|  | 5127 | } | 
|  | 5128 |  | 
|  | 5129 | return 0; | 
|  | 5130 | } | 
|  | 5131 |  | 
|  | 5132 | status_t AudioFlinger::closeInput(int input) | 
|  | 5133 | { | 
|  | 5134 | // keep strong reference on the record thread so that | 
|  | 5135 | // it is not destroyed while exit() is executed | 
|  | 5136 | sp <RecordThread> thread; | 
|  | 5137 | { | 
|  | 5138 | Mutex::Autolock _l(mLock); | 
|  | 5139 | thread = checkRecordThread_l(input); | 
|  | 5140 | if (thread == NULL) { | 
|  | 5141 | return BAD_VALUE; | 
|  | 5142 | } | 
|  | 5143 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5144 | ALOGV("closeInput() %d", input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5145 | void *param2 = 0; | 
|  | 5146 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); | 
|  | 5147 | mRecordThreads.removeItem(input); | 
|  | 5148 | } | 
|  | 5149 | thread->exit(); | 
|  | 5150 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5151 | AudioStreamIn *in = thread->clearInput(); | 
|  | 5152 | // from now on thread->mInput is NULL | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5153 | in->hwDev->close_input_stream(in->hwDev, in->stream); | 
|  | 5154 | delete in; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5155 |  | 
|  | 5156 | return NO_ERROR; | 
|  | 5157 | } | 
|  | 5158 |  | 
|  | 5159 | status_t AudioFlinger::setStreamOutput(uint32_t stream, int output) | 
|  | 5160 | { | 
|  | 5161 | Mutex::Autolock _l(mLock); | 
|  | 5162 | MixerThread *dstThread = checkMixerThread_l(output); | 
|  | 5163 | if (dstThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5164 | ALOGW("setStreamOutput() bad output id %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5165 | return BAD_VALUE; | 
|  | 5166 | } | 
|  | 5167 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5168 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5169 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); | 
|  | 5170 |  | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5171 | dstThread->setStreamValid(stream, true); | 
|  | 5172 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5173 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5174 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
|  | 5175 | if (thread != dstThread && | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5176 | thread->type() != ThreadBase::DIRECT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5177 | MixerThread *srcThread = (MixerThread *)thread; | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5178 | srcThread->setStreamValid(stream, false); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5179 | srcThread->invalidateTracks(stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5180 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5181 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5182 |  | 
|  | 5183 | return NO_ERROR; | 
|  | 5184 | } | 
|  | 5185 |  | 
|  | 5186 |  | 
|  | 5187 | int AudioFlinger::newAudioSessionId() | 
|  | 5188 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5189 | return nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5190 | } | 
|  | 5191 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5192 | void AudioFlinger::acquireAudioSessionId(int audioSession) | 
|  | 5193 | { | 
|  | 5194 | Mutex::Autolock _l(mLock); | 
|  | 5195 | int caller = IPCThreadState::self()->getCallingPid(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5196 | ALOGV("acquiring %d from %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5197 | int num = mAudioSessionRefs.size(); | 
|  | 5198 | for (int i = 0; i< num; i++) { | 
|  | 5199 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); | 
|  | 5200 | if (ref->sessionid == audioSession && ref->pid == caller) { | 
|  | 5201 | ref->cnt++; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5202 | ALOGV(" incremented refcount to %d", ref->cnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5203 | return; | 
|  | 5204 | } | 
|  | 5205 | } | 
|  | 5206 | AudioSessionRef *ref = new AudioSessionRef(); | 
|  | 5207 | ref->sessionid = audioSession; | 
|  | 5208 | ref->pid = caller; | 
|  | 5209 | ref->cnt = 1; | 
|  | 5210 | mAudioSessionRefs.push(ref); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5211 | ALOGV(" added new entry for %d", ref->sessionid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5212 | } | 
|  | 5213 |  | 
|  | 5214 | void AudioFlinger::releaseAudioSessionId(int audioSession) | 
|  | 5215 | { | 
|  | 5216 | Mutex::Autolock _l(mLock); | 
|  | 5217 | int caller = IPCThreadState::self()->getCallingPid(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5218 | ALOGV("releasing %d from %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5219 | int num = mAudioSessionRefs.size(); | 
|  | 5220 | for (int i = 0; i< num; i++) { | 
|  | 5221 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
|  | 5222 | if (ref->sessionid == audioSession && ref->pid == caller) { | 
|  | 5223 | ref->cnt--; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5224 | ALOGV(" decremented refcount to %d", ref->cnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5225 | if (ref->cnt == 0) { | 
|  | 5226 | mAudioSessionRefs.removeAt(i); | 
|  | 5227 | delete ref; | 
|  | 5228 | purgeStaleEffects_l(); | 
|  | 5229 | } | 
|  | 5230 | return; | 
|  | 5231 | } | 
|  | 5232 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5233 | ALOGW("session id %d not found for pid %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5234 | } | 
|  | 5235 |  | 
|  | 5236 | void AudioFlinger::purgeStaleEffects_l() { | 
|  | 5237 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5238 | ALOGV("purging stale effects"); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5239 |  | 
|  | 5240 | Vector< sp<EffectChain> > chains; | 
|  | 5241 |  | 
|  | 5242 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5243 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 5244 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 5245 | sp<EffectChain> ec = t->mEffectChains[j]; | 
| Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5246 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 5247 | chains.push(ec); | 
|  | 5248 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5249 | } | 
|  | 5250 | } | 
|  | 5251 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 5252 | sp<RecordThread> t = mRecordThreads.valueAt(i); | 
|  | 5253 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 5254 | sp<EffectChain> ec = t->mEffectChains[j]; | 
|  | 5255 | chains.push(ec); | 
|  | 5256 | } | 
|  | 5257 | } | 
|  | 5258 |  | 
|  | 5259 | for (size_t i = 0; i < chains.size(); i++) { | 
|  | 5260 | sp<EffectChain> ec = chains[i]; | 
|  | 5261 | int sessionid = ec->sessionId(); | 
|  | 5262 | sp<ThreadBase> t = ec->mThread.promote(); | 
|  | 5263 | if (t == 0) { | 
|  | 5264 | continue; | 
|  | 5265 | } | 
|  | 5266 | size_t numsessionrefs = mAudioSessionRefs.size(); | 
|  | 5267 | bool found = false; | 
|  | 5268 | for (size_t k = 0; k < numsessionrefs; k++) { | 
|  | 5269 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); | 
|  | 5270 | if (ref->sessionid == sessionid) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5271 | ALOGV(" session %d still exists for %d with %d refs", | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5272 | sessionid, ref->pid, ref->cnt); | 
|  | 5273 | found = true; | 
|  | 5274 | break; | 
|  | 5275 | } | 
|  | 5276 | } | 
|  | 5277 | if (!found) { | 
|  | 5278 | // remove all effects from the chain | 
|  | 5279 | while (ec->mEffects.size()) { | 
|  | 5280 | sp<EffectModule> effect = ec->mEffects[0]; | 
|  | 5281 | effect->unPin(); | 
|  | 5282 | Mutex::Autolock _l (t->mLock); | 
|  | 5283 | t->removeEffect_l(effect); | 
|  | 5284 | for (size_t j = 0; j < effect->mHandles.size(); j++) { | 
|  | 5285 | sp<EffectHandle> handle = effect->mHandles[j].promote(); | 
|  | 5286 | if (handle != 0) { | 
|  | 5287 | handle->mEffect.clear(); | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5288 | if (handle->mHasControl && handle->mEnabled) { | 
|  | 5289 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); | 
|  | 5290 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5291 | } | 
|  | 5292 | } | 
|  | 5293 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5294 | } | 
|  | 5295 | } | 
|  | 5296 | } | 
|  | 5297 | return; | 
|  | 5298 | } | 
|  | 5299 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5300 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held | 
|  | 5301 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const | 
|  | 5302 | { | 
|  | 5303 | PlaybackThread *thread = NULL; | 
|  | 5304 | if (mPlaybackThreads.indexOfKey(output) >= 0) { | 
|  | 5305 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); | 
|  | 5306 | } | 
|  | 5307 | return thread; | 
|  | 5308 | } | 
|  | 5309 |  | 
|  | 5310 | // checkMixerThread_l() must be called with AudioFlinger::mLock held | 
|  | 5311 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const | 
|  | 5312 | { | 
|  | 5313 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 5314 | if (thread != NULL) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5315 | if (thread->type() == ThreadBase::DIRECT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5316 | thread = NULL; | 
|  | 5317 | } | 
|  | 5318 | } | 
|  | 5319 | return (MixerThread *)thread; | 
|  | 5320 | } | 
|  | 5321 |  | 
|  | 5322 | // checkRecordThread_l() must be called with AudioFlinger::mLock held | 
|  | 5323 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const | 
|  | 5324 | { | 
|  | 5325 | RecordThread *thread = NULL; | 
|  | 5326 | if (mRecordThreads.indexOfKey(input) >= 0) { | 
|  | 5327 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); | 
|  | 5328 | } | 
|  | 5329 | return thread; | 
|  | 5330 | } | 
|  | 5331 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5332 | uint32_t AudioFlinger::nextUniqueId() | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5333 | { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5334 | return android_atomic_inc(&mNextUniqueId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5335 | } | 
|  | 5336 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5337 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() | 
|  | 5338 | { | 
|  | 5339 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5340 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5341 | AudioStreamOut *output = thread->getOutput(); | 
|  | 5342 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5343 | return thread; | 
|  | 5344 | } | 
|  | 5345 | } | 
|  | 5346 | return NULL; | 
|  | 5347 | } | 
|  | 5348 |  | 
|  | 5349 | uint32_t AudioFlinger::primaryOutputDevice_l() | 
|  | 5350 | { | 
|  | 5351 | PlaybackThread *thread = primaryPlaybackThread_l(); | 
|  | 5352 |  | 
|  | 5353 | if (thread == NULL) { | 
|  | 5354 | return 0; | 
|  | 5355 | } | 
|  | 5356 |  | 
|  | 5357 | return thread->device(); | 
|  | 5358 | } | 
|  | 5359 |  | 
|  | 5360 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5361 | // ---------------------------------------------------------------------------- | 
|  | 5362 | //  Effect management | 
|  | 5363 | // ---------------------------------------------------------------------------- | 
|  | 5364 |  | 
|  | 5365 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5366 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) | 
|  | 5367 | { | 
|  | 5368 | Mutex::Autolock _l(mLock); | 
|  | 5369 | return EffectQueryNumberEffects(numEffects); | 
|  | 5370 | } | 
|  | 5371 |  | 
|  | 5372 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) | 
|  | 5373 | { | 
|  | 5374 | Mutex::Autolock _l(mLock); | 
|  | 5375 | return EffectQueryEffect(index, descriptor); | 
|  | 5376 | } | 
|  | 5377 |  | 
|  | 5378 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) | 
|  | 5379 | { | 
|  | 5380 | Mutex::Autolock _l(mLock); | 
|  | 5381 | return EffectGetDescriptor(pUuid, descriptor); | 
|  | 5382 | } | 
|  | 5383 |  | 
|  | 5384 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5385 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, | 
|  | 5386 | effect_descriptor_t *pDesc, | 
|  | 5387 | const sp<IEffectClient>& effectClient, | 
|  | 5388 | int32_t priority, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5389 | int io, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5390 | int sessionId, | 
|  | 5391 | status_t *status, | 
|  | 5392 | int *id, | 
|  | 5393 | int *enabled) | 
|  | 5394 | { | 
|  | 5395 | status_t lStatus = NO_ERROR; | 
|  | 5396 | sp<EffectHandle> handle; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5397 | effect_descriptor_t desc; | 
|  | 5398 | sp<Client> client; | 
|  | 5399 | wp<Client> wclient; | 
|  | 5400 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5401 | ALOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5402 | pid, effectClient.get(), priority, sessionId, io); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5403 |  | 
|  | 5404 | if (pDesc == NULL) { | 
|  | 5405 | lStatus = BAD_VALUE; | 
|  | 5406 | goto Exit; | 
|  | 5407 | } | 
|  | 5408 |  | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5409 | // check audio settings permission for global effects | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5410 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5411 | lStatus = PERMISSION_DENIED; | 
|  | 5412 | goto Exit; | 
|  | 5413 | } | 
|  | 5414 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5415 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5416 | // that can only be created by audio policy manager (running in same process) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5417 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5418 | lStatus = PERMISSION_DENIED; | 
|  | 5419 | goto Exit; | 
|  | 5420 | } | 
|  | 5421 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5422 | if (io == 0) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5423 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5424 | // output must be specified by AudioPolicyManager when using session | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5425 | // AUDIO_SESSION_OUTPUT_STAGE | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5426 | lStatus = BAD_VALUE; | 
|  | 5427 | goto Exit; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5428 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5429 | // if the output returned by getOutputForEffect() is removed before we lock the | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5430 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5431 | // and we will exit safely | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5432 | io = AudioSystem::getOutputForEffect(&desc); | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5433 | } | 
|  | 5434 | } | 
|  | 5435 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5436 | { | 
|  | 5437 | Mutex::Autolock _l(mLock); | 
|  | 5438 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5439 |  | 
|  | 5440 | if (!EffectIsNullUuid(&pDesc->uuid)) { | 
|  | 5441 | // if uuid is specified, request effect descriptor | 
|  | 5442 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); | 
|  | 5443 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5444 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5445 | goto Exit; | 
|  | 5446 | } | 
|  | 5447 | } else { | 
|  | 5448 | // if uuid is not specified, look for an available implementation | 
|  | 5449 | // of the required type in effect factory | 
|  | 5450 | if (EffectIsNullUuid(&pDesc->type)) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5451 | ALOGW("createEffect() no effect type"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5452 | lStatus = BAD_VALUE; | 
|  | 5453 | goto Exit; | 
|  | 5454 | } | 
|  | 5455 | uint32_t numEffects = 0; | 
|  | 5456 | effect_descriptor_t d; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5457 | d.flags = 0; // prevent compiler warning | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5458 | bool found = false; | 
|  | 5459 |  | 
|  | 5460 | lStatus = EffectQueryNumberEffects(&numEffects); | 
|  | 5461 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5462 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5463 | goto Exit; | 
|  | 5464 | } | 
|  | 5465 | for (uint32_t i = 0; i < numEffects; i++) { | 
|  | 5466 | lStatus = EffectQueryEffect(i, &desc); | 
|  | 5467 | if (lStatus < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5468 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5469 | continue; | 
|  | 5470 | } | 
|  | 5471 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { | 
|  | 5472 | // If matching type found save effect descriptor. If the session is | 
|  | 5473 | // 0 and the effect is not auxiliary, continue enumeration in case | 
|  | 5474 | // an auxiliary version of this effect type is available | 
|  | 5475 | found = true; | 
|  | 5476 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5477 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5478 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5479 | break; | 
|  | 5480 | } | 
|  | 5481 | } | 
|  | 5482 | } | 
|  | 5483 | if (!found) { | 
|  | 5484 | lStatus = BAD_VALUE; | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5485 | ALOGW("createEffect() effect not found"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5486 | goto Exit; | 
|  | 5487 | } | 
|  | 5488 | // For same effect type, chose auxiliary version over insert version if | 
|  | 5489 | // connect to output mix (Compliance to OpenSL ES) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5490 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5491 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5492 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); | 
|  | 5493 | } | 
|  | 5494 | } | 
|  | 5495 |  | 
|  | 5496 | // 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] | 5497 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5498 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5499 | lStatus = INVALID_OPERATION; | 
|  | 5500 | goto Exit; | 
|  | 5501 | } | 
|  | 5502 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5503 | // check recording permission for visualizer | 
|  | 5504 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && | 
|  | 5505 | !recordingAllowed()) { | 
|  | 5506 | lStatus = PERMISSION_DENIED; | 
|  | 5507 | goto Exit; | 
|  | 5508 | } | 
|  | 5509 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5510 | // return effect descriptor | 
|  | 5511 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); | 
|  | 5512 |  | 
|  | 5513 | // If output is not specified try to find a matching audio session ID in one of the | 
|  | 5514 | // output threads. | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5515 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX | 
|  | 5516 | // because of code checking output when entering the function. | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5517 | // Note: io is never 0 when creating an effect on an input | 
|  | 5518 | if (io == 0) { | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5519 | // look for the thread where the specified audio session is present | 
|  | 5520 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 5521 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5522 | io = mPlaybackThreads.keyAt(i); | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5523 | break; | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5524 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5525 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5526 | if (io == 0) { | 
|  | 5527 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 5528 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { | 
|  | 5529 | io = mRecordThreads.keyAt(i); | 
|  | 5530 | break; | 
|  | 5531 | } | 
|  | 5532 | } | 
|  | 5533 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5534 | // If no output thread contains the requested session ID, default to | 
|  | 5535 | // first output. The effect chain will be moved to the correct output | 
|  | 5536 | // thread when a track with the same session ID is created | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5537 | if (io == 0 && mPlaybackThreads.size()) { | 
|  | 5538 | io = mPlaybackThreads.keyAt(0); | 
|  | 5539 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5540 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5541 | } | 
|  | 5542 | ThreadBase *thread = checkRecordThread_l(io); | 
|  | 5543 | if (thread == NULL) { | 
|  | 5544 | thread = checkPlaybackThread_l(io); | 
|  | 5545 | if (thread == NULL) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 5546 | ALOGE("createEffect() unknown output thread"); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5547 | lStatus = BAD_VALUE; | 
|  | 5548 | goto Exit; | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5549 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5550 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5551 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5552 | wclient = mClients.valueFor(pid); | 
|  | 5553 |  | 
|  | 5554 | if (wclient != NULL) { | 
|  | 5555 | client = wclient.promote(); | 
|  | 5556 | } else { | 
|  | 5557 | client = new Client(this, pid); | 
|  | 5558 | mClients.add(pid, client); | 
|  | 5559 | } | 
|  | 5560 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5561 | // create effect on selected output thread | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5562 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, | 
|  | 5563 | &desc, enabled, &lStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5564 | if (handle != 0 && id != NULL) { | 
|  | 5565 | *id = handle->id(); | 
|  | 5566 | } | 
|  | 5567 | } | 
|  | 5568 |  | 
|  | 5569 | Exit: | 
|  | 5570 | if(status) { | 
|  | 5571 | *status = lStatus; | 
|  | 5572 | } | 
|  | 5573 | return handle; | 
|  | 5574 | } | 
|  | 5575 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5576 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5577 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5578 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5579 | sessionId, srcOutput, dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5580 | Mutex::Autolock _l(mLock); | 
|  | 5581 | if (srcOutput == dstOutput) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5582 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5583 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5584 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5585 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); | 
|  | 5586 | if (srcThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5587 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5588 | return BAD_VALUE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5589 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5590 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); | 
|  | 5591 | if (dstThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5592 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5593 | return BAD_VALUE; | 
|  | 5594 | } | 
|  | 5595 |  | 
|  | 5596 | Mutex::Autolock _dl(dstThread->mLock); | 
|  | 5597 | Mutex::Autolock _sl(srcThread->mLock); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5598 | moveEffectChain_l(sessionId, srcThread, dstThread, false); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5599 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5600 | return NO_ERROR; | 
|  | 5601 | } | 
|  | 5602 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5603 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5604 | status_t AudioFlinger::moveEffectChain_l(int sessionId, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5605 | AudioFlinger::PlaybackThread *srcThread, | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5606 | AudioFlinger::PlaybackThread *dstThread, | 
|  | 5607 | bool reRegister) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5608 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5609 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5610 | sessionId, srcThread, dstThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5611 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5612 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5613 | if (chain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5614 | 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] | 5615 | sessionId, srcThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5616 | return INVALID_OPERATION; | 
|  | 5617 | } | 
|  | 5618 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5619 | // 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] | 5620 | // 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] | 5621 | // 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] | 5622 | // removed. | 
|  | 5623 | srcThread->removeEffectChain_l(chain); | 
|  | 5624 |  | 
|  | 5625 | // transfer all effects one by one so that new effect chain is created on new thread with | 
|  | 5626 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5627 | int dstOutput = dstThread->id(); | 
|  | 5628 | sp<EffectChain> dstChain; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5629 | uint32_t strategy = 0; // prevent compiler warning | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5630 | sp<EffectModule> effect = chain->getEffectFromId_l(0); | 
|  | 5631 | while (effect != 0) { | 
|  | 5632 | srcThread->removeEffect_l(effect); | 
|  | 5633 | dstThread->addEffect_l(effect); | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5634 | // removeEffect_l() has stopped the effect if it was active so it must be restarted | 
|  | 5635 | if (effect->state() == EffectModule::ACTIVE || | 
|  | 5636 | effect->state() == EffectModule::STOPPING) { | 
|  | 5637 | effect->start(); | 
|  | 5638 | } | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5639 | // if the move request is not received from audio policy manager, the effect must be | 
|  | 5640 | // re-registered with the new strategy and output | 
|  | 5641 | if (dstChain == 0) { | 
|  | 5642 | dstChain = effect->chain().promote(); | 
|  | 5643 | if (dstChain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5644 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5645 | srcThread->addEffect_l(effect); | 
|  | 5646 | return NO_INIT; | 
|  | 5647 | } | 
|  | 5648 | strategy = dstChain->strategy(); | 
|  | 5649 | } | 
|  | 5650 | if (reRegister) { | 
|  | 5651 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5652 | AudioSystem::registerEffect(&effect->desc(), | 
|  | 5653 | dstOutput, | 
|  | 5654 | strategy, | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5655 | sessionId, | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5656 | effect->id()); | 
|  | 5657 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5658 | effect = chain->getEffectFromId_l(0); | 
|  | 5659 | } | 
|  | 5660 |  | 
|  | 5661 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5662 | } | 
|  | 5663 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5664 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5665 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5666 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5667 | const sp<AudioFlinger::Client>& client, | 
|  | 5668 | const sp<IEffectClient>& effectClient, | 
|  | 5669 | int32_t priority, | 
|  | 5670 | int sessionId, | 
|  | 5671 | effect_descriptor_t *desc, | 
|  | 5672 | int *enabled, | 
|  | 5673 | status_t *status | 
|  | 5674 | ) | 
|  | 5675 | { | 
|  | 5676 | sp<EffectModule> effect; | 
|  | 5677 | sp<EffectHandle> handle; | 
|  | 5678 | status_t lStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5679 | sp<EffectChain> chain; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5680 | bool chainCreated = false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5681 | bool effectCreated = false; | 
|  | 5682 | bool effectRegistered = false; | 
|  | 5683 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5684 | lStatus = initCheck(); | 
|  | 5685 | if (lStatus != NO_ERROR) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5686 | ALOGW("createEffect_l() Audio driver not initialized."); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5687 | goto Exit; | 
|  | 5688 | } | 
|  | 5689 |  | 
|  | 5690 | // Do not allow effects with session ID 0 on direct output or duplicating threads | 
|  | 5691 | // 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] | 5692 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5693 | ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5694 | desc->name, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5695 | lStatus = BAD_VALUE; | 
|  | 5696 | goto Exit; | 
|  | 5697 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5698 | // Only Pre processor effects are allowed on input threads and only on input threads | 
|  | 5699 | if ((mType == RECORD && | 
|  | 5700 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || | 
|  | 5701 | (mType != RECORD && | 
|  | 5702 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5703 | 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] | 5704 | desc->name, desc->flags, mType); | 
|  | 5705 | lStatus = BAD_VALUE; | 
|  | 5706 | goto Exit; | 
|  | 5707 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5708 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5709 | 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] | 5710 |  | 
|  | 5711 | { // scope for mLock | 
|  | 5712 | Mutex::Autolock _l(mLock); | 
|  | 5713 |  | 
|  | 5714 | // check for existing effect chain with the requested audio session | 
|  | 5715 | chain = getEffectChain_l(sessionId); | 
|  | 5716 | if (chain == 0) { | 
|  | 5717 | // create a new chain for this session | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5718 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5719 | chain = new EffectChain(this, sessionId); | 
|  | 5720 | addEffectChain_l(chain); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5721 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 5722 | chainCreated = true; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5723 | } else { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5724 | effect = chain->getEffectFromDesc_l(desc); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5725 | } | 
|  | 5726 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5727 | ALOGV("createEffect_l() got effect %p on chain %p", effect == 0 ? 0 : effect.get(), chain.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5728 |  | 
|  | 5729 | if (effect == 0) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5730 | int id = mAudioFlinger->nextUniqueId(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5731 | // Check CPU and memory usage | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5732 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5733 | if (lStatus != NO_ERROR) { | 
|  | 5734 | goto Exit; | 
|  | 5735 | } | 
|  | 5736 | effectRegistered = true; | 
|  | 5737 | // create a new effect module if none present in the chain | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5738 | effect = new EffectModule(this, chain, desc, id, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5739 | lStatus = effect->status(); | 
|  | 5740 | if (lStatus != NO_ERROR) { | 
|  | 5741 | goto Exit; | 
|  | 5742 | } | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5743 | lStatus = chain->addEffect_l(effect); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5744 | if (lStatus != NO_ERROR) { | 
|  | 5745 | goto Exit; | 
|  | 5746 | } | 
|  | 5747 | effectCreated = true; | 
|  | 5748 |  | 
|  | 5749 | effect->setDevice(mDevice); | 
|  | 5750 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 5751 | } | 
|  | 5752 | // create effect handle and connect it to effect module | 
|  | 5753 | handle = new EffectHandle(effect, client, effectClient, priority); | 
|  | 5754 | lStatus = effect->addHandle(handle); | 
|  | 5755 | if (enabled) { | 
|  | 5756 | *enabled = (int)effect->isEnabled(); | 
|  | 5757 | } | 
|  | 5758 | } | 
|  | 5759 |  | 
|  | 5760 | Exit: | 
|  | 5761 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5762 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5763 | if (effectCreated) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5764 | chain->removeEffect_l(effect); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5765 | } | 
|  | 5766 | if (effectRegistered) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5767 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5768 | } | 
|  | 5769 | if (chainCreated) { | 
|  | 5770 | removeEffectChain_l(chain); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5771 | } | 
|  | 5772 | handle.clear(); | 
|  | 5773 | } | 
|  | 5774 |  | 
|  | 5775 | if(status) { | 
|  | 5776 | *status = lStatus; | 
|  | 5777 | } | 
|  | 5778 | return handle; | 
|  | 5779 | } | 
|  | 5780 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5781 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) | 
|  | 5782 | { | 
|  | 5783 | sp<EffectModule> effect; | 
|  | 5784 |  | 
|  | 5785 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 5786 | if (chain != 0) { | 
|  | 5787 | effect = chain->getEffectFromId_l(effectId); | 
|  | 5788 | } | 
|  | 5789 | return effect; | 
|  | 5790 | } | 
|  | 5791 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5792 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
|  | 5793 | // PlaybackThread::mLock held | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5794 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5795 | { | 
|  | 5796 | // check for existing effect chain with the requested audio session | 
|  | 5797 | int sessionId = effect->sessionId(); | 
|  | 5798 | sp<EffectChain> chain = getEffectChain_l(sessionId); | 
|  | 5799 | bool chainCreated = false; | 
|  | 5800 |  | 
|  | 5801 | if (chain == 0) { | 
|  | 5802 | // create a new chain for this session | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5803 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5804 | chain = new EffectChain(this, sessionId); | 
|  | 5805 | addEffectChain_l(chain); | 
|  | 5806 | chain->setStrategy(getStrategyForSession_l(sessionId)); | 
|  | 5807 | chainCreated = true; | 
|  | 5808 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5809 | 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] | 5810 |  | 
|  | 5811 | if (chain->getEffectFromId_l(effect->id()) != 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5812 | ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5813 | this, effect->desc().name, chain.get()); | 
|  | 5814 | return BAD_VALUE; | 
|  | 5815 | } | 
|  | 5816 |  | 
|  | 5817 | status_t status = chain->addEffect_l(effect); | 
|  | 5818 | if (status != NO_ERROR) { | 
|  | 5819 | if (chainCreated) { | 
|  | 5820 | removeEffectChain_l(chain); | 
|  | 5821 | } | 
|  | 5822 | return status; | 
|  | 5823 | } | 
|  | 5824 |  | 
|  | 5825 | effect->setDevice(mDevice); | 
|  | 5826 | effect->setMode(mAudioFlinger->getMode()); | 
|  | 5827 | return NO_ERROR; | 
|  | 5828 | } | 
|  | 5829 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5830 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5831 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5832 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5833 | effect_descriptor_t desc = effect->desc(); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5834 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 5835 | detachAuxEffect_l(effect->id()); | 
|  | 5836 | } | 
|  | 5837 |  | 
|  | 5838 | sp<EffectChain> chain = effect->chain().promote(); | 
|  | 5839 | if (chain != 0) { | 
|  | 5840 | // remove effect chain if removing last effect | 
|  | 5841 | if (chain->removeEffect_l(effect) == 0) { | 
|  | 5842 | removeEffectChain_l(chain); | 
|  | 5843 | } | 
|  | 5844 | } else { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5845 | 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] | 5846 | } | 
|  | 5847 | } | 
|  | 5848 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5849 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
|  | 5850 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) | 
|  | 5851 | { | 
|  | 5852 | effectChains = mEffectChains; | 
|  | 5853 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 5854 | mEffectChains[i]->lock(); | 
|  | 5855 | } | 
|  | 5856 | } | 
|  | 5857 |  | 
|  | 5858 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
|  | 5859 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) | 
|  | 5860 | { | 
|  | 5861 | for (size_t i = 0; i < effectChains.size(); i++) { | 
|  | 5862 | effectChains[i]->unlock(); | 
|  | 5863 | } | 
|  | 5864 | } | 
|  | 5865 |  | 
|  | 5866 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) | 
|  | 5867 | { | 
|  | 5868 | Mutex::Autolock _l(mLock); | 
|  | 5869 | return getEffectChain_l(sessionId); | 
|  | 5870 | } | 
|  | 5871 |  | 
|  | 5872 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) | 
|  | 5873 | { | 
|  | 5874 | sp<EffectChain> chain; | 
|  | 5875 |  | 
|  | 5876 | size_t size = mEffectChains.size(); | 
|  | 5877 | for (size_t i = 0; i < size; i++) { | 
|  | 5878 | if (mEffectChains[i]->sessionId() == sessionId) { | 
|  | 5879 | chain = mEffectChains[i]; | 
|  | 5880 | break; | 
|  | 5881 | } | 
|  | 5882 | } | 
|  | 5883 | return chain; | 
|  | 5884 | } | 
|  | 5885 |  | 
|  | 5886 | void AudioFlinger::ThreadBase::setMode(uint32_t mode) | 
|  | 5887 | { | 
|  | 5888 | Mutex::Autolock _l(mLock); | 
|  | 5889 | size_t size = mEffectChains.size(); | 
|  | 5890 | for (size_t i = 0; i < size; i++) { | 
|  | 5891 | mEffectChains[i]->setMode_l(mode); | 
|  | 5892 | } | 
|  | 5893 | } | 
|  | 5894 |  | 
|  | 5895 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5896 | const wp<EffectHandle>& handle, | 
|  | 5897 | bool unpiniflast) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5898 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5899 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5900 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5901 | // delete the effect module if removing last handle on it | 
|  | 5902 | if (effect->removeHandle(handle) == 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5903 | if (!effect->isPinned() || unpiniflast) { | 
|  | 5904 | removeEffect_l(effect); | 
|  | 5905 | AudioSystem::unregisterEffect(effect->id()); | 
|  | 5906 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5907 | } | 
|  | 5908 | } | 
|  | 5909 |  | 
|  | 5910 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 5911 | { | 
|  | 5912 | int session = chain->sessionId(); | 
|  | 5913 | int16_t *buffer = mMixBuffer; | 
|  | 5914 | bool ownsBuffer = false; | 
|  | 5915 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5916 | 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] | 5917 | if (session > 0) { | 
|  | 5918 | // Only one effect chain can be present in direct output thread and it uses | 
|  | 5919 | // the mix buffer as input | 
|  | 5920 | if (mType != DIRECT) { | 
|  | 5921 | size_t numSamples = mFrameCount * mChannelCount; | 
|  | 5922 | buffer = new int16_t[numSamples]; | 
|  | 5923 | memset(buffer, 0, numSamples * sizeof(int16_t)); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5924 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5925 | ownsBuffer = true; | 
|  | 5926 | } | 
|  | 5927 |  | 
|  | 5928 | // Attach all tracks with same session ID to this chain. | 
|  | 5929 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 5930 | sp<Track> track = mTracks[i]; | 
|  | 5931 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5932 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5933 | track->setMainBuffer(buffer); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5934 | chain->incTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5935 | } | 
|  | 5936 | } | 
|  | 5937 |  | 
|  | 5938 | // indicate all active tracks in the chain | 
|  | 5939 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 5940 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 5941 | if (track == 0) continue; | 
|  | 5942 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5943 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5944 | chain->incActiveTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5945 | } | 
|  | 5946 | } | 
|  | 5947 | } | 
|  | 5948 |  | 
|  | 5949 | chain->setInBuffer(buffer, ownsBuffer); | 
|  | 5950 | chain->setOutBuffer(mMixBuffer); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5951 | // 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] | 5952 | // 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] | 5953 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
|  | 5954 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5955 | // after track specific effects and before output stage | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5956 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
|  | 5957 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5958 | // Effect chain for other sessions are inserted at beginning of effect | 
|  | 5959 | // chains list to be processed before output mix effects. Relative order between other | 
|  | 5960 | // sessions is not important | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5961 | size_t size = mEffectChains.size(); | 
|  | 5962 | size_t i = 0; | 
|  | 5963 | for (i = 0; i < size; i++) { | 
|  | 5964 | if (mEffectChains[i]->sessionId() < session) break; | 
|  | 5965 | } | 
|  | 5966 | mEffectChains.insertAt(chain, i); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5967 | checkSuspendOnAddEffectChain_l(chain); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5968 |  | 
|  | 5969 | return NO_ERROR; | 
|  | 5970 | } | 
|  | 5971 |  | 
|  | 5972 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 5973 | { | 
|  | 5974 | int session = chain->sessionId(); | 
|  | 5975 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5976 | 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] | 5977 |  | 
|  | 5978 | for (size_t i = 0; i < mEffectChains.size(); i++) { | 
|  | 5979 | if (chain == mEffectChains[i]) { | 
|  | 5980 | mEffectChains.removeAt(i); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5981 | // detach all active tracks from the chain | 
|  | 5982 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
|  | 5983 | sp<Track> track = mActiveTracks[i].promote(); | 
|  | 5984 | if (track == 0) continue; | 
|  | 5985 | if (session == track->sessionId()) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5986 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5987 | chain.get(), session); | 
|  | 5988 | chain->decActiveTrackCnt(); | 
|  | 5989 | } | 
|  | 5990 | } | 
|  | 5991 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5992 | // detach all tracks with same session ID from this chain | 
|  | 5993 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 5994 | sp<Track> track = mTracks[i]; | 
|  | 5995 | if (session == track->sessionId()) { | 
|  | 5996 | track->setMainBuffer(mMixBuffer); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5997 | chain->decTrackCnt(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5998 | } | 
|  | 5999 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6000 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6001 | } | 
|  | 6002 | } | 
|  | 6003 | return mEffectChains.size(); | 
|  | 6004 | } | 
|  | 6005 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6006 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
|  | 6007 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6008 | { | 
|  | 6009 | Mutex::Autolock _l(mLock); | 
|  | 6010 | return attachAuxEffect_l(track, EffectId); | 
|  | 6011 | } | 
|  | 6012 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6013 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
|  | 6014 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6015 | { | 
|  | 6016 | status_t status = NO_ERROR; | 
|  | 6017 |  | 
|  | 6018 | if (EffectId == 0) { | 
|  | 6019 | track->setAuxBuffer(0, NULL); | 
|  | 6020 | } else { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6021 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
|  | 6022 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6023 | if (effect != 0) { | 
|  | 6024 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 6025 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
|  | 6026 | } else { | 
|  | 6027 | status = INVALID_OPERATION; | 
|  | 6028 | } | 
|  | 6029 | } else { | 
|  | 6030 | status = BAD_VALUE; | 
|  | 6031 | } | 
|  | 6032 | } | 
|  | 6033 | return status; | 
|  | 6034 | } | 
|  | 6035 |  | 
|  | 6036 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
|  | 6037 | { | 
|  | 6038 | for (size_t i = 0; i < mTracks.size(); ++i) { | 
|  | 6039 | sp<Track> track = mTracks[i]; | 
|  | 6040 | if (track->auxEffectId() == effectId) { | 
|  | 6041 | attachAuxEffect_l(track, 0); | 
|  | 6042 | } | 
|  | 6043 | } | 
|  | 6044 | } | 
|  | 6045 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6046 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
|  | 6047 | { | 
|  | 6048 | // only one chain per input thread | 
|  | 6049 | if (mEffectChains.size() != 0) { | 
|  | 6050 | return INVALID_OPERATION; | 
|  | 6051 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6052 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6053 |  | 
|  | 6054 | chain->setInBuffer(NULL); | 
|  | 6055 | chain->setOutBuffer(NULL); | 
|  | 6056 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6057 | checkSuspendOnAddEffectChain_l(chain); | 
|  | 6058 |  | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6059 | mEffectChains.add(chain); | 
|  | 6060 |  | 
|  | 6061 | return NO_ERROR; | 
|  | 6062 | } | 
|  | 6063 |  | 
|  | 6064 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
|  | 6065 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6066 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6067 | ALOGW_IF(mEffectChains.size() != 1, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6068 | "removeEffectChain_l() %p invalid chain size %d on thread %p", | 
|  | 6069 | chain.get(), mEffectChains.size(), this); | 
|  | 6070 | if (mEffectChains.size() == 1) { | 
|  | 6071 | mEffectChains.removeAt(0); | 
|  | 6072 | } | 
|  | 6073 | return 0; | 
|  | 6074 | } | 
|  | 6075 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6076 | // ---------------------------------------------------------------------------- | 
|  | 6077 | //  EffectModule implementation | 
|  | 6078 | // ---------------------------------------------------------------------------- | 
|  | 6079 |  | 
|  | 6080 | #undef LOG_TAG | 
|  | 6081 | #define LOG_TAG "AudioFlinger::EffectModule" | 
|  | 6082 |  | 
|  | 6083 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, | 
|  | 6084 | const wp<AudioFlinger::EffectChain>& chain, | 
|  | 6085 | effect_descriptor_t *desc, | 
|  | 6086 | int id, | 
|  | 6087 | int sessionId) | 
|  | 6088 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6089 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6090 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6091 | ALOGV("Constructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6092 | int lStatus; | 
|  | 6093 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6094 | if (thread == 0) { | 
|  | 6095 | return; | 
|  | 6096 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6097 |  | 
|  | 6098 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); | 
|  | 6099 |  | 
|  | 6100 | // create effect engine from effect factory | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6101 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6102 |  | 
|  | 6103 | if (mStatus != NO_ERROR) { | 
|  | 6104 | return; | 
|  | 6105 | } | 
|  | 6106 | lStatus = init(); | 
|  | 6107 | if (lStatus < 0) { | 
|  | 6108 | mStatus = lStatus; | 
|  | 6109 | goto Error; | 
|  | 6110 | } | 
|  | 6111 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6112 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 6113 | mPinned = true; | 
|  | 6114 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6115 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6116 | return; | 
|  | 6117 | Error: | 
|  | 6118 | EffectRelease(mEffectInterface); | 
|  | 6119 | mEffectInterface = NULL; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6120 | ALOGV("Constructor Error %d", mStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6121 | } | 
|  | 6122 |  | 
|  | 6123 | AudioFlinger::EffectModule::~EffectModule() | 
|  | 6124 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6125 | ALOGV("Destructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6126 | if (mEffectInterface != NULL) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6127 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6128 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { | 
|  | 6129 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6130 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6131 | audio_stream_t *stream = thread->stream(); | 
|  | 6132 | if (stream != NULL) { | 
|  | 6133 | stream->remove_audio_effect(stream, mEffectInterface); | 
|  | 6134 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6135 | } | 
|  | 6136 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6137 | // release effect engine | 
|  | 6138 | EffectRelease(mEffectInterface); | 
|  | 6139 | } | 
|  | 6140 | } | 
|  | 6141 |  | 
|  | 6142 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) | 
|  | 6143 | { | 
|  | 6144 | status_t status; | 
|  | 6145 |  | 
|  | 6146 | Mutex::Autolock _l(mLock); | 
|  | 6147 | // First handle in mHandles has highest priority and controls the effect module | 
|  | 6148 | int priority = handle->priority(); | 
|  | 6149 | size_t size = mHandles.size(); | 
|  | 6150 | sp<EffectHandle> h; | 
|  | 6151 | size_t i; | 
|  | 6152 | for (i = 0; i < size; i++) { | 
|  | 6153 | h = mHandles[i].promote(); | 
|  | 6154 | if (h == 0) continue; | 
|  | 6155 | if (h->priority() <= priority) break; | 
|  | 6156 | } | 
|  | 6157 | // if inserted in first place, move effect control from previous owner to this handle | 
|  | 6158 | if (i == 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6159 | bool enabled = false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6160 | if (h != 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6161 | enabled = h->enabled(); | 
|  | 6162 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6163 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6164 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6165 | status = NO_ERROR; | 
|  | 6166 | } else { | 
|  | 6167 | status = ALREADY_EXISTS; | 
|  | 6168 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6169 | 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] | 6170 | mHandles.insertAt(handle, i); | 
|  | 6171 | return status; | 
|  | 6172 | } | 
|  | 6173 |  | 
|  | 6174 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) | 
|  | 6175 | { | 
|  | 6176 | Mutex::Autolock _l(mLock); | 
|  | 6177 | size_t size = mHandles.size(); | 
|  | 6178 | size_t i; | 
|  | 6179 | for (i = 0; i < size; i++) { | 
|  | 6180 | if (mHandles[i] == handle) break; | 
|  | 6181 | } | 
|  | 6182 | if (i == size) { | 
|  | 6183 | return size; | 
|  | 6184 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6185 | 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] | 6186 |  | 
|  | 6187 | bool enabled = false; | 
|  | 6188 | EffectHandle *hdl = handle.unsafe_get(); | 
|  | 6189 | if (hdl) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6190 | ALOGV("removeHandle() unsafe_get OK"); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6191 | enabled = hdl->enabled(); | 
|  | 6192 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6193 | mHandles.removeAt(i); | 
|  | 6194 | size = mHandles.size(); | 
|  | 6195 | // if removed from first place, move effect control from this handle to next in line | 
|  | 6196 | if (i == 0 && size != 0) { | 
|  | 6197 | sp<EffectHandle> h = mHandles[0].promote(); | 
|  | 6198 | if (h != 0) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6199 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6200 | } | 
|  | 6201 | } | 
|  | 6202 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6203 | // Prevent calls to process() and other functions on effect interface from now on. | 
|  | 6204 | // The effect engine will be released by the destructor when the last strong reference on | 
|  | 6205 | // this object is released which can happen after next process is called. | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6206 | if (size == 0 && !mPinned) { | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6207 | mState = DESTROYED; | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6208 | } | 
|  | 6209 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6210 | return size; | 
|  | 6211 | } | 
|  | 6212 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6213 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() | 
|  | 6214 | { | 
|  | 6215 | Mutex::Autolock _l(mLock); | 
|  | 6216 | sp<EffectHandle> handle; | 
|  | 6217 | if (mHandles.size() != 0) { | 
|  | 6218 | handle = mHandles[0].promote(); | 
|  | 6219 | } | 
|  | 6220 | return handle; | 
|  | 6221 | } | 
|  | 6222 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6223 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6224 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6225 | ALOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6226 | // keep a strong reference on this EffectModule to avoid calling the | 
|  | 6227 | // destructor before we exit | 
|  | 6228 | sp<EffectModule> keep(this); | 
|  | 6229 | { | 
|  | 6230 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6231 | if (thread != 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6232 | thread->disconnectEffect(keep, handle, unpiniflast); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6233 | } | 
|  | 6234 | } | 
|  | 6235 | } | 
|  | 6236 |  | 
|  | 6237 | void AudioFlinger::EffectModule::updateState() { | 
|  | 6238 | Mutex::Autolock _l(mLock); | 
|  | 6239 |  | 
|  | 6240 | switch (mState) { | 
|  | 6241 | case RESTART: | 
|  | 6242 | reset_l(); | 
|  | 6243 | // FALL THROUGH | 
|  | 6244 |  | 
|  | 6245 | case STARTING: | 
|  | 6246 | // clear auxiliary effect input buffer for next accumulation | 
|  | 6247 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 6248 | memset(mConfig.inputCfg.buffer.raw, | 
|  | 6249 | 0, | 
|  | 6250 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
|  | 6251 | } | 
|  | 6252 | start_l(); | 
|  | 6253 | mState = ACTIVE; | 
|  | 6254 | break; | 
|  | 6255 | case STOPPING: | 
|  | 6256 | stop_l(); | 
|  | 6257 | mDisableWaitCnt = mMaxDisableWaitCnt; | 
|  | 6258 | mState = STOPPED; | 
|  | 6259 | break; | 
|  | 6260 | case STOPPED: | 
|  | 6261 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the | 
|  | 6262 | // turn off sequence. | 
|  | 6263 | if (--mDisableWaitCnt == 0) { | 
|  | 6264 | reset_l(); | 
|  | 6265 | mState = IDLE; | 
|  | 6266 | } | 
|  | 6267 | break; | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6268 | default: //IDLE , ACTIVE, DESTROYED | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6269 | break; | 
|  | 6270 | } | 
|  | 6271 | } | 
|  | 6272 |  | 
|  | 6273 | void AudioFlinger::EffectModule::process() | 
|  | 6274 | { | 
|  | 6275 | Mutex::Autolock _l(mLock); | 
|  | 6276 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6277 | if (mState == DESTROYED || mEffectInterface == NULL || | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6278 | mConfig.inputCfg.buffer.raw == NULL || | 
|  | 6279 | mConfig.outputCfg.buffer.raw == NULL) { | 
|  | 6280 | return; | 
|  | 6281 | } | 
|  | 6282 |  | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6283 | if (isProcessEnabled()) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6284 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer | 
|  | 6285 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 6286 | ditherAndClamp(mConfig.inputCfg.buffer.s32, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6287 | mConfig.inputCfg.buffer.s32, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6288 | mConfig.inputCfg.buffer.frameCount/2); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6289 | } | 
|  | 6290 |  | 
|  | 6291 | // do the actual processing in the effect engine | 
|  | 6292 | int ret = (*mEffectInterface)->process(mEffectInterface, | 
|  | 6293 | &mConfig.inputCfg.buffer, | 
|  | 6294 | &mConfig.outputCfg.buffer); | 
|  | 6295 |  | 
|  | 6296 | // force transition to IDLE state when engine is ready | 
|  | 6297 | if (mState == STOPPED && ret == -ENODATA) { | 
|  | 6298 | mDisableWaitCnt = 1; | 
|  | 6299 | } | 
|  | 6300 |  | 
|  | 6301 | // clear auxiliary effect input buffer for next accumulation | 
|  | 6302 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6303 | memset(mConfig.inputCfg.buffer.raw, 0, | 
|  | 6304 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6305 | } | 
|  | 6306 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6307 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
|  | 6308 | // If an insert effect is idle and input buffer is different from output buffer, | 
|  | 6309 | // accumulate input onto output | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6310 | sp<EffectChain> chain = mChain.promote(); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6311 | if (chain != 0 && chain->activeTrackCnt() != 0) { | 
| Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6312 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2;  //always stereo here | 
|  | 6313 | int16_t *in = mConfig.inputCfg.buffer.s16; | 
|  | 6314 | int16_t *out = mConfig.outputCfg.buffer.s16; | 
|  | 6315 | for (size_t i = 0; i < frameCnt; i++) { | 
|  | 6316 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6317 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6318 | } | 
|  | 6319 | } | 
|  | 6320 | } | 
|  | 6321 |  | 
|  | 6322 | void AudioFlinger::EffectModule::reset_l() | 
|  | 6323 | { | 
|  | 6324 | if (mEffectInterface == NULL) { | 
|  | 6325 | return; | 
|  | 6326 | } | 
|  | 6327 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); | 
|  | 6328 | } | 
|  | 6329 |  | 
|  | 6330 | status_t AudioFlinger::EffectModule::configure() | 
|  | 6331 | { | 
|  | 6332 | uint32_t channels; | 
|  | 6333 | if (mEffectInterface == NULL) { | 
|  | 6334 | return NO_INIT; | 
|  | 6335 | } | 
|  | 6336 |  | 
|  | 6337 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6338 | if (thread == 0) { | 
|  | 6339 | return DEAD_OBJECT; | 
|  | 6340 | } | 
|  | 6341 |  | 
|  | 6342 | // TODO: handle configuration of effects replacing track process | 
|  | 6343 | if (thread->channelCount() == 1) { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6344 | channels = AUDIO_CHANNEL_OUT_MONO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6345 | } else { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6346 | channels = AUDIO_CHANNEL_OUT_STEREO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6347 | } | 
|  | 6348 |  | 
|  | 6349 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6350 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6351 | } else { | 
|  | 6352 | mConfig.inputCfg.channels = channels; | 
|  | 6353 | } | 
|  | 6354 | mConfig.outputCfg.channels = channels; | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6355 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 6356 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6357 | mConfig.inputCfg.samplingRate = thread->sampleRate(); | 
|  | 6358 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; | 
|  | 6359 | mConfig.inputCfg.bufferProvider.cookie = NULL; | 
|  | 6360 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; | 
|  | 6361 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; | 
|  | 6362 | mConfig.outputCfg.bufferProvider.cookie = NULL; | 
|  | 6363 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; | 
|  | 6364 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; | 
|  | 6365 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; | 
|  | 6366 | // Insert effect: | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6367 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6368 | // always overwrites output buffer: input buffer == output buffer | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6369 | // - in other sessions: | 
|  | 6370 | //      last effect in the chain accumulates in output buffer: input buffer != output buffer | 
|  | 6371 | //      other effect: overwrites output buffer: input buffer == output buffer | 
|  | 6372 | // Auxiliary effect: | 
|  | 6373 | //      accumulates in output buffer: input buffer != output buffer | 
|  | 6374 | // Therefore: accumulate <=> input buffer != output buffer | 
|  | 6375 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
|  | 6376 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; | 
|  | 6377 | } else { | 
|  | 6378 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; | 
|  | 6379 | } | 
|  | 6380 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; | 
|  | 6381 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; | 
|  | 6382 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); | 
|  | 6383 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; | 
|  | 6384 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6385 | ALOGV("configure() %p thread %p buffer %p framecount %d", | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6386 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); | 
|  | 6387 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6388 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6389 | uint32_t size = sizeof(int); | 
|  | 6390 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 6391 | EFFECT_CMD_SET_CONFIG, | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6392 | sizeof(effect_config_t), | 
|  | 6393 | &mConfig, | 
|  | 6394 | &size, | 
|  | 6395 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6396 | if (status == 0) { | 
|  | 6397 | status = cmdStatus; | 
|  | 6398 | } | 
|  | 6399 |  | 
|  | 6400 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / | 
|  | 6401 | (1000 * mConfig.outputCfg.buffer.frameCount); | 
|  | 6402 |  | 
|  | 6403 | return status; | 
|  | 6404 | } | 
|  | 6405 |  | 
|  | 6406 | status_t AudioFlinger::EffectModule::init() | 
|  | 6407 | { | 
|  | 6408 | Mutex::Autolock _l(mLock); | 
|  | 6409 | if (mEffectInterface == NULL) { | 
|  | 6410 | return NO_INIT; | 
|  | 6411 | } | 
|  | 6412 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6413 | uint32_t size = sizeof(status_t); | 
|  | 6414 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6415 | EFFECT_CMD_INIT, | 
|  | 6416 | 0, | 
|  | 6417 | NULL, | 
|  | 6418 | &size, | 
|  | 6419 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6420 | if (status == 0) { | 
|  | 6421 | status = cmdStatus; | 
|  | 6422 | } | 
|  | 6423 | return status; | 
|  | 6424 | } | 
|  | 6425 |  | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6426 | status_t AudioFlinger::EffectModule::start() | 
|  | 6427 | { | 
|  | 6428 | Mutex::Autolock _l(mLock); | 
|  | 6429 | return start_l(); | 
|  | 6430 | } | 
|  | 6431 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6432 | status_t AudioFlinger::EffectModule::start_l() | 
|  | 6433 | { | 
|  | 6434 | if (mEffectInterface == NULL) { | 
|  | 6435 | return NO_INIT; | 
|  | 6436 | } | 
|  | 6437 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6438 | uint32_t size = sizeof(status_t); | 
|  | 6439 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6440 | EFFECT_CMD_ENABLE, | 
|  | 6441 | 0, | 
|  | 6442 | NULL, | 
|  | 6443 | &size, | 
|  | 6444 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6445 | if (status == 0) { | 
|  | 6446 | status = cmdStatus; | 
|  | 6447 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6448 | if (status == 0 && | 
|  | 6449 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6450 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { | 
|  | 6451 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6452 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6453 | audio_stream_t *stream = thread->stream(); | 
|  | 6454 | if (stream != NULL) { | 
|  | 6455 | stream->add_audio_effect(stream, mEffectInterface); | 
|  | 6456 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6457 | } | 
|  | 6458 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6459 | return status; | 
|  | 6460 | } | 
|  | 6461 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6462 | status_t AudioFlinger::EffectModule::stop() | 
|  | 6463 | { | 
|  | 6464 | Mutex::Autolock _l(mLock); | 
|  | 6465 | return stop_l(); | 
|  | 6466 | } | 
|  | 6467 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6468 | status_t AudioFlinger::EffectModule::stop_l() | 
|  | 6469 | { | 
|  | 6470 | if (mEffectInterface == NULL) { | 
|  | 6471 | return NO_INIT; | 
|  | 6472 | } | 
|  | 6473 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6474 | uint32_t size = sizeof(status_t); | 
|  | 6475 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6476 | EFFECT_CMD_DISABLE, | 
|  | 6477 | 0, | 
|  | 6478 | NULL, | 
|  | 6479 | &size, | 
|  | 6480 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6481 | if (status == 0) { | 
|  | 6482 | status = cmdStatus; | 
|  | 6483 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6484 | if (status == 0 && | 
|  | 6485 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
|  | 6486 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { | 
|  | 6487 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 6488 | if (thread != 0) { | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6489 | audio_stream_t *stream = thread->stream(); | 
|  | 6490 | if (stream != NULL) { | 
|  | 6491 | stream->remove_audio_effect(stream, mEffectInterface); | 
|  | 6492 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6493 | } | 
|  | 6494 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6495 | return status; | 
|  | 6496 | } | 
|  | 6497 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6498 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, | 
|  | 6499 | uint32_t cmdSize, | 
|  | 6500 | void *pCmdData, | 
|  | 6501 | uint32_t *replySize, | 
|  | 6502 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6503 | { | 
|  | 6504 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6505 | //    ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6506 |  | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6507 | if (mState == DESTROYED || mEffectInterface == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6508 | return NO_INIT; | 
|  | 6509 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6510 | status_t status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6511 | cmdCode, | 
|  | 6512 | cmdSize, | 
|  | 6513 | pCmdData, | 
|  | 6514 | replySize, | 
|  | 6515 | pReplyData); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6516 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6517 | uint32_t size = (replySize == NULL) ? 0 : *replySize; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6518 | for (size_t i = 1; i < mHandles.size(); i++) { | 
|  | 6519 | sp<EffectHandle> h = mHandles[i].promote(); | 
|  | 6520 | if (h != 0) { | 
|  | 6521 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); | 
|  | 6522 | } | 
|  | 6523 | } | 
|  | 6524 | } | 
|  | 6525 | return status; | 
|  | 6526 | } | 
|  | 6527 |  | 
|  | 6528 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) | 
|  | 6529 | { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6530 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6531 | Mutex::Autolock _l(mLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6532 | ALOGV("setEnabled %p enabled %d", this, enabled); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6533 |  | 
|  | 6534 | if (enabled != isEnabled()) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6535 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); | 
|  | 6536 | if (enabled && status != NO_ERROR) { | 
|  | 6537 | return status; | 
|  | 6538 | } | 
|  | 6539 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6540 | switch (mState) { | 
|  | 6541 | // going from disabled to enabled | 
|  | 6542 | case IDLE: | 
|  | 6543 | mState = STARTING; | 
|  | 6544 | break; | 
|  | 6545 | case STOPPED: | 
|  | 6546 | mState = RESTART; | 
|  | 6547 | break; | 
|  | 6548 | case STOPPING: | 
|  | 6549 | mState = ACTIVE; | 
|  | 6550 | break; | 
|  | 6551 |  | 
|  | 6552 | // going from enabled to disabled | 
|  | 6553 | case RESTART: | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6554 | mState = STOPPED; | 
|  | 6555 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6556 | case STARTING: | 
|  | 6557 | mState = IDLE; | 
|  | 6558 | break; | 
|  | 6559 | case ACTIVE: | 
|  | 6560 | mState = STOPPING; | 
|  | 6561 | break; | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6562 | case DESTROYED: | 
|  | 6563 | return NO_ERROR; // simply ignore as we are being destroyed | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6564 | } | 
|  | 6565 | for (size_t i = 1; i < mHandles.size(); i++) { | 
|  | 6566 | sp<EffectHandle> h = mHandles[i].promote(); | 
|  | 6567 | if (h != 0) { | 
|  | 6568 | h->setEnabled(enabled); | 
|  | 6569 | } | 
|  | 6570 | } | 
|  | 6571 | } | 
|  | 6572 | return NO_ERROR; | 
|  | 6573 | } | 
|  | 6574 |  | 
|  | 6575 | bool AudioFlinger::EffectModule::isEnabled() | 
|  | 6576 | { | 
|  | 6577 | switch (mState) { | 
|  | 6578 | case RESTART: | 
|  | 6579 | case STARTING: | 
|  | 6580 | case ACTIVE: | 
|  | 6581 | return true; | 
|  | 6582 | case IDLE: | 
|  | 6583 | case STOPPING: | 
|  | 6584 | case STOPPED: | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6585 | case DESTROYED: | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6586 | default: | 
|  | 6587 | return false; | 
|  | 6588 | } | 
|  | 6589 | } | 
|  | 6590 |  | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6591 | bool AudioFlinger::EffectModule::isProcessEnabled() | 
|  | 6592 | { | 
|  | 6593 | switch (mState) { | 
|  | 6594 | case RESTART: | 
|  | 6595 | case ACTIVE: | 
|  | 6596 | case STOPPING: | 
|  | 6597 | case STOPPED: | 
|  | 6598 | return true; | 
|  | 6599 | case IDLE: | 
|  | 6600 | case STARTING: | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6601 | case DESTROYED: | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6602 | default: | 
|  | 6603 | return false; | 
|  | 6604 | } | 
|  | 6605 | } | 
|  | 6606 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6607 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) | 
|  | 6608 | { | 
|  | 6609 | Mutex::Autolock _l(mLock); | 
|  | 6610 | status_t status = NO_ERROR; | 
|  | 6611 |  | 
|  | 6612 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume | 
|  | 6613 | // 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] | 6614 | if (isProcessEnabled() && | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6615 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || | 
|  | 6616 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6617 | status_t cmdStatus; | 
|  | 6618 | uint32_t volume[2]; | 
|  | 6619 | uint32_t *pVolume = NULL; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6620 | uint32_t size = sizeof(volume); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6621 | volume[0] = *left; | 
|  | 6622 | volume[1] = *right; | 
|  | 6623 | if (controller) { | 
|  | 6624 | pVolume = volume; | 
|  | 6625 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6626 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6627 | EFFECT_CMD_SET_VOLUME, | 
|  | 6628 | size, | 
|  | 6629 | volume, | 
|  | 6630 | &size, | 
|  | 6631 | pVolume); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6632 | if (controller && status == NO_ERROR && size == sizeof(volume)) { | 
|  | 6633 | *left = volume[0]; | 
|  | 6634 | *right = volume[1]; | 
|  | 6635 | } | 
|  | 6636 | } | 
|  | 6637 | return status; | 
|  | 6638 | } | 
|  | 6639 |  | 
|  | 6640 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) | 
|  | 6641 | { | 
|  | 6642 | Mutex::Autolock _l(mLock); | 
|  | 6643 | status_t status = NO_ERROR; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6644 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { | 
|  | 6645 | // audio pre processing modules on RecordThread can receive both output and | 
|  | 6646 | // input device indication in the same call | 
|  | 6647 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; | 
|  | 6648 | if (dev) { | 
|  | 6649 | status_t cmdStatus; | 
|  | 6650 | uint32_t size = sizeof(status_t); | 
|  | 6651 |  | 
|  | 6652 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6653 | EFFECT_CMD_SET_DEVICE, | 
|  | 6654 | sizeof(uint32_t), | 
|  | 6655 | &dev, | 
|  | 6656 | &size, | 
|  | 6657 | &cmdStatus); | 
|  | 6658 | if (status == NO_ERROR) { | 
|  | 6659 | status = cmdStatus; | 
|  | 6660 | } | 
|  | 6661 | } | 
|  | 6662 | dev = device & AUDIO_DEVICE_IN_ALL; | 
|  | 6663 | if (dev) { | 
|  | 6664 | status_t cmdStatus; | 
|  | 6665 | uint32_t size = sizeof(status_t); | 
|  | 6666 |  | 
|  | 6667 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6668 | EFFECT_CMD_SET_INPUT_DEVICE, | 
|  | 6669 | sizeof(uint32_t), | 
|  | 6670 | &dev, | 
|  | 6671 | &size, | 
|  | 6672 | &cmdStatus); | 
|  | 6673 | if (status2 == NO_ERROR) { | 
|  | 6674 | status2 = cmdStatus; | 
|  | 6675 | } | 
|  | 6676 | if (status == NO_ERROR) { | 
|  | 6677 | status = status2; | 
|  | 6678 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6679 | } | 
|  | 6680 | } | 
|  | 6681 | return status; | 
|  | 6682 | } | 
|  | 6683 |  | 
|  | 6684 | status_t AudioFlinger::EffectModule::setMode(uint32_t mode) | 
|  | 6685 | { | 
|  | 6686 | Mutex::Autolock _l(mLock); | 
|  | 6687 | status_t status = NO_ERROR; | 
|  | 6688 | 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] | 6689 | status_t cmdStatus; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6690 | uint32_t size = sizeof(status_t); | 
|  | 6691 | status = (*mEffectInterface)->command(mEffectInterface, | 
|  | 6692 | EFFECT_CMD_SET_AUDIO_MODE, | 
|  | 6693 | sizeof(int), | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6694 | &mode, | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6695 | &size, | 
|  | 6696 | &cmdStatus); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6697 | if (status == NO_ERROR) { | 
|  | 6698 | status = cmdStatus; | 
|  | 6699 | } | 
|  | 6700 | } | 
|  | 6701 | return status; | 
|  | 6702 | } | 
|  | 6703 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6704 | void AudioFlinger::EffectModule::setSuspended(bool suspended) | 
|  | 6705 | { | 
|  | 6706 | Mutex::Autolock _l(mLock); | 
|  | 6707 | mSuspended = suspended; | 
|  | 6708 | } | 
| Glenn Kasten | a3a8548 | 2012-01-04 11:01:11 -0800 | [diff] [blame] | 6709 |  | 
|  | 6710 | bool AudioFlinger::EffectModule::suspended() const | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6711 | { | 
|  | 6712 | Mutex::Autolock _l(mLock); | 
|  | 6713 | return mSuspended; | 
|  | 6714 | } | 
|  | 6715 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6716 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) | 
|  | 6717 | { | 
|  | 6718 | const size_t SIZE = 256; | 
|  | 6719 | char buffer[SIZE]; | 
|  | 6720 | String8 result; | 
|  | 6721 |  | 
|  | 6722 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); | 
|  | 6723 | result.append(buffer); | 
|  | 6724 |  | 
|  | 6725 | bool locked = tryLock(mLock); | 
|  | 6726 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 6727 | if (!locked) { | 
|  | 6728 | result.append("\t\tCould not lock Fx mutex:\n"); | 
|  | 6729 | } | 
|  | 6730 |  | 
|  | 6731 | result.append("\t\tSession Status State Engine:\n"); | 
|  | 6732 | snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   0x%08x\n", | 
|  | 6733 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); | 
|  | 6734 | result.append(buffer); | 
|  | 6735 |  | 
|  | 6736 | result.append("\t\tDescriptor:\n"); | 
|  | 6737 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
|  | 6738 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, | 
|  | 6739 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], | 
|  | 6740 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); | 
|  | 6741 | result.append(buffer); | 
|  | 6742 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
|  | 6743 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, | 
|  | 6744 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], | 
|  | 6745 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); | 
|  | 6746 | result.append(buffer); | 
| Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6747 | 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] | 6748 | mDescriptor.apiVersion, | 
|  | 6749 | mDescriptor.flags); | 
|  | 6750 | result.append(buffer); | 
|  | 6751 | snprintf(buffer, SIZE, "\t\t- name: %s\n", | 
|  | 6752 | mDescriptor.name); | 
|  | 6753 | result.append(buffer); | 
|  | 6754 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", | 
|  | 6755 | mDescriptor.implementor); | 
|  | 6756 | result.append(buffer); | 
|  | 6757 |  | 
|  | 6758 | result.append("\t\t- Input configuration:\n"); | 
|  | 6759 | result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
|  | 6760 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
|  | 6761 | (uint32_t)mConfig.inputCfg.buffer.raw, | 
|  | 6762 | mConfig.inputCfg.buffer.frameCount, | 
|  | 6763 | mConfig.inputCfg.samplingRate, | 
|  | 6764 | mConfig.inputCfg.channels, | 
|  | 6765 | mConfig.inputCfg.format); | 
|  | 6766 | result.append(buffer); | 
|  | 6767 |  | 
|  | 6768 | result.append("\t\t- Output configuration:\n"); | 
|  | 6769 | result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
|  | 6770 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
|  | 6771 | (uint32_t)mConfig.outputCfg.buffer.raw, | 
|  | 6772 | mConfig.outputCfg.buffer.frameCount, | 
|  | 6773 | mConfig.outputCfg.samplingRate, | 
|  | 6774 | mConfig.outputCfg.channels, | 
|  | 6775 | mConfig.outputCfg.format); | 
|  | 6776 | result.append(buffer); | 
|  | 6777 |  | 
|  | 6778 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); | 
|  | 6779 | result.append(buffer); | 
|  | 6780 | result.append("\t\t\tPid   Priority Ctrl Locked client server\n"); | 
|  | 6781 | for (size_t i = 0; i < mHandles.size(); ++i) { | 
|  | 6782 | sp<EffectHandle> handle = mHandles[i].promote(); | 
|  | 6783 | if (handle != 0) { | 
|  | 6784 | handle->dump(buffer, SIZE); | 
|  | 6785 | result.append(buffer); | 
|  | 6786 | } | 
|  | 6787 | } | 
|  | 6788 |  | 
|  | 6789 | result.append("\n"); | 
|  | 6790 |  | 
|  | 6791 | write(fd, result.string(), result.length()); | 
|  | 6792 |  | 
|  | 6793 | if (locked) { | 
|  | 6794 | mLock.unlock(); | 
|  | 6795 | } | 
|  | 6796 |  | 
|  | 6797 | return NO_ERROR; | 
|  | 6798 | } | 
|  | 6799 |  | 
|  | 6800 | // ---------------------------------------------------------------------------- | 
|  | 6801 | //  EffectHandle implementation | 
|  | 6802 | // ---------------------------------------------------------------------------- | 
|  | 6803 |  | 
|  | 6804 | #undef LOG_TAG | 
|  | 6805 | #define LOG_TAG "AudioFlinger::EffectHandle" | 
|  | 6806 |  | 
|  | 6807 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, | 
|  | 6808 | const sp<AudioFlinger::Client>& client, | 
|  | 6809 | const sp<IEffectClient>& effectClient, | 
|  | 6810 | int32_t priority) | 
|  | 6811 | : BnEffect(), | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6812 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6813 | mPriority(priority), mHasControl(false), mEnabled(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6814 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6815 | ALOGV("constructor %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6816 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6817 | if (client == 0) { | 
|  | 6818 | return; | 
|  | 6819 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6820 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); | 
|  | 6821 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); | 
|  | 6822 | if (mCblkMemory != 0) { | 
|  | 6823 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); | 
|  | 6824 |  | 
|  | 6825 | if (mCblk) { | 
|  | 6826 | new(mCblk) effect_param_cblk_t(); | 
|  | 6827 | mBuffer = (uint8_t *)mCblk + bufOffset; | 
|  | 6828 | } | 
|  | 6829 | } else { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 6830 | 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] | 6831 | return; | 
|  | 6832 | } | 
|  | 6833 | } | 
|  | 6834 |  | 
|  | 6835 | AudioFlinger::EffectHandle::~EffectHandle() | 
|  | 6836 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6837 | ALOGV("Destructor %p", this); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6838 | disconnect(false); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6839 | ALOGV("Destructor DONE %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6840 | } | 
|  | 6841 |  | 
|  | 6842 | status_t AudioFlinger::EffectHandle::enable() | 
|  | 6843 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6844 | ALOGV("enable %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6845 | if (!mHasControl) return INVALID_OPERATION; | 
|  | 6846 | if (mEffect == 0) return DEAD_OBJECT; | 
|  | 6847 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6848 | if (mEnabled) { | 
|  | 6849 | return NO_ERROR; | 
|  | 6850 | } | 
|  | 6851 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6852 | mEnabled = true; | 
|  | 6853 |  | 
|  | 6854 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6855 | if (thread != 0) { | 
|  | 6856 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); | 
|  | 6857 | } | 
|  | 6858 |  | 
|  | 6859 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled | 
|  | 6860 | if (mEffect->suspended()) { | 
|  | 6861 | return NO_ERROR; | 
|  | 6862 | } | 
|  | 6863 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6864 | status_t status = mEffect->setEnabled(true); | 
|  | 6865 | if (status != NO_ERROR) { | 
|  | 6866 | if (thread != 0) { | 
|  | 6867 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6868 | } | 
|  | 6869 | mEnabled = false; | 
|  | 6870 | } | 
|  | 6871 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6872 | } | 
|  | 6873 |  | 
|  | 6874 | status_t AudioFlinger::EffectHandle::disable() | 
|  | 6875 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6876 | ALOGV("disable %p", this); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6877 | if (!mHasControl) return INVALID_OPERATION; | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6878 | if (mEffect == 0) return DEAD_OBJECT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6879 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6880 | if (!mEnabled) { | 
|  | 6881 | return NO_ERROR; | 
|  | 6882 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6883 | mEnabled = false; | 
|  | 6884 |  | 
|  | 6885 | if (mEffect->suspended()) { | 
|  | 6886 | return NO_ERROR; | 
|  | 6887 | } | 
|  | 6888 |  | 
|  | 6889 | status_t status = mEffect->setEnabled(false); | 
|  | 6890 |  | 
|  | 6891 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6892 | if (thread != 0) { | 
|  | 6893 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6894 | } | 
|  | 6895 |  | 
|  | 6896 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6897 | } | 
|  | 6898 |  | 
|  | 6899 | void AudioFlinger::EffectHandle::disconnect() | 
|  | 6900 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6901 | disconnect(true); | 
|  | 6902 | } | 
|  | 6903 |  | 
|  | 6904 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) | 
|  | 6905 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6906 | ALOGV("disconnect(%s)", unpiniflast ? "true" : "false"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6907 | if (mEffect == 0) { | 
|  | 6908 | return; | 
|  | 6909 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6910 | mEffect->disconnect(this, unpiniflast); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6911 |  | 
| Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6912 | if (mHasControl && mEnabled) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6913 | sp<ThreadBase> thread = mEffect->thread().promote(); | 
|  | 6914 | if (thread != 0) { | 
|  | 6915 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
|  | 6916 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6917 | } | 
|  | 6918 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6919 | // release sp on module => module destructor can be called now | 
|  | 6920 | mEffect.clear(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6921 | if (mClient != 0) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6922 | if (mCblk) { | 
|  | 6923 | mCblk->~effect_param_cblk_t();   // destroy our shared-structure. | 
|  | 6924 | } | 
|  | 6925 | mCblkMemory.clear();            // and free the shared memory | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6926 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); | 
|  | 6927 | mClient.clear(); | 
|  | 6928 | } | 
|  | 6929 | } | 
|  | 6930 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6931 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, | 
|  | 6932 | uint32_t cmdSize, | 
|  | 6933 | void *pCmdData, | 
|  | 6934 | uint32_t *replySize, | 
|  | 6935 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6936 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6937 | //    ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6938 | //              cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6939 |  | 
|  | 6940 | // only get parameter command is permitted for applications not controlling the effect | 
|  | 6941 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { | 
|  | 6942 | return INVALID_OPERATION; | 
|  | 6943 | } | 
|  | 6944 | if (mEffect == 0) return DEAD_OBJECT; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6945 | if (mClient == 0) return INVALID_OPERATION; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6946 |  | 
|  | 6947 | // handle commands that are not forwarded transparently to effect engine | 
|  | 6948 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { | 
|  | 6949 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: | 
|  | 6950 | // no risk to block the whole media server process or mixer threads is we are stuck here | 
|  | 6951 | Mutex::Autolock _l(mCblk->lock); | 
|  | 6952 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || | 
|  | 6953 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { | 
|  | 6954 | mCblk->serverIndex = 0; | 
|  | 6955 | mCblk->clientIndex = 0; | 
|  | 6956 | return BAD_VALUE; | 
|  | 6957 | } | 
|  | 6958 | status_t status = NO_ERROR; | 
|  | 6959 | while (mCblk->serverIndex < mCblk->clientIndex) { | 
|  | 6960 | int reply; | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6961 | uint32_t rsize = sizeof(int); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6962 | int *p = (int *)(mBuffer + mCblk->serverIndex); | 
|  | 6963 | int size = *p++; | 
|  | 6964 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6965 | ALOGW("command(): invalid parameter block size"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6966 | break; | 
|  | 6967 | } | 
|  | 6968 | effect_param_t *param = (effect_param_t *)p; | 
|  | 6969 | if (param->psize == 0 || param->vsize == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6970 | ALOGW("command(): null parameter or value size"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6971 | mCblk->serverIndex += size; | 
|  | 6972 | continue; | 
|  | 6973 | } | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6974 | uint32_t psize = sizeof(effect_param_t) + | 
|  | 6975 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + | 
|  | 6976 | param->vsize; | 
|  | 6977 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, | 
|  | 6978 | psize, | 
|  | 6979 | p, | 
|  | 6980 | &rsize, | 
|  | 6981 | &reply); | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6982 | // stop at first error encountered | 
|  | 6983 | if (ret != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6984 | status = ret; | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6985 | *(int *)pReplyData = reply; | 
|  | 6986 | break; | 
|  | 6987 | } else if (reply != NO_ERROR) { | 
|  | 6988 | *(int *)pReplyData = reply; | 
|  | 6989 | break; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6990 | } | 
|  | 6991 | mCblk->serverIndex += size; | 
|  | 6992 | } | 
|  | 6993 | mCblk->serverIndex = 0; | 
|  | 6994 | mCblk->clientIndex = 0; | 
|  | 6995 | return status; | 
|  | 6996 | } else if (cmdCode == EFFECT_CMD_ENABLE) { | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6997 | *(int *)pReplyData = NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6998 | return enable(); | 
|  | 6999 | } else if (cmdCode == EFFECT_CMD_DISABLE) { | 
| Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7000 | *(int *)pReplyData = NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7001 | return disable(); | 
|  | 7002 | } | 
|  | 7003 |  | 
|  | 7004 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
|  | 7005 | } | 
|  | 7006 |  | 
|  | 7007 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { | 
|  | 7008 | return mCblkMemory; | 
|  | 7009 | } | 
|  | 7010 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7011 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7012 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7013 | ALOGV("setControl %p control %d", this, hasControl); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7014 |  | 
|  | 7015 | mHasControl = hasControl; | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7016 | mEnabled = enabled; | 
|  | 7017 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7018 | if (signal && mEffectClient != 0) { | 
|  | 7019 | mEffectClient->controlStatusChanged(hasControl); | 
|  | 7020 | } | 
|  | 7021 | } | 
|  | 7022 |  | 
| Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7023 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, | 
|  | 7024 | uint32_t cmdSize, | 
|  | 7025 | void *pCmdData, | 
|  | 7026 | uint32_t replySize, | 
|  | 7027 | void *pReplyData) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7028 | { | 
|  | 7029 | if (mEffectClient != 0) { | 
|  | 7030 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
|  | 7031 | } | 
|  | 7032 | } | 
|  | 7033 |  | 
|  | 7034 |  | 
|  | 7035 |  | 
|  | 7036 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) | 
|  | 7037 | { | 
|  | 7038 | if (mEffectClient != 0) { | 
|  | 7039 | mEffectClient->enableStatusChanged(enabled); | 
|  | 7040 | } | 
|  | 7041 | } | 
|  | 7042 |  | 
|  | 7043 | status_t AudioFlinger::EffectHandle::onTransact( | 
|  | 7044 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 7045 | { | 
|  | 7046 | return BnEffect::onTransact(code, data, reply, flags); | 
|  | 7047 | } | 
|  | 7048 |  | 
|  | 7049 |  | 
|  | 7050 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) | 
|  | 7051 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7052 | bool locked = mCblk ? tryLock(mCblk->lock) : false; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7053 |  | 
|  | 7054 | snprintf(buffer, size, "\t\t\t%05d %05d    %01u    %01u      %05u  %05u\n", | 
|  | 7055 | (mClient == NULL) ? getpid() : mClient->pid(), | 
|  | 7056 | mPriority, | 
|  | 7057 | mHasControl, | 
|  | 7058 | !locked, | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7059 | mCblk ? mCblk->clientIndex : 0, | 
|  | 7060 | mCblk ? mCblk->serverIndex : 0 | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7061 | ); | 
|  | 7062 |  | 
|  | 7063 | if (locked) { | 
|  | 7064 | mCblk->lock.unlock(); | 
|  | 7065 | } | 
|  | 7066 | } | 
|  | 7067 |  | 
|  | 7068 | #undef LOG_TAG | 
|  | 7069 | #define LOG_TAG "AudioFlinger::EffectChain" | 
|  | 7070 |  | 
|  | 7071 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, | 
|  | 7072 | int sessionId) | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7073 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7074 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), | 
|  | 7075 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7076 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7077 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7078 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7079 | if (thread == 0) { | 
|  | 7080 | return; | 
|  | 7081 | } | 
|  | 7082 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / | 
|  | 7083 | thread->frameCount(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7084 | } | 
|  | 7085 |  | 
|  | 7086 | AudioFlinger::EffectChain::~EffectChain() | 
|  | 7087 | { | 
|  | 7088 | if (mOwnInBuffer) { | 
|  | 7089 | delete mInBuffer; | 
|  | 7090 | } | 
|  | 7091 |  | 
|  | 7092 | } | 
|  | 7093 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7094 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7095 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7096 | { | 
|  | 7097 | sp<EffectModule> effect; | 
|  | 7098 | size_t size = mEffects.size(); | 
|  | 7099 |  | 
|  | 7100 | for (size_t i = 0; i < size; i++) { | 
|  | 7101 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { | 
|  | 7102 | effect = mEffects[i]; | 
|  | 7103 | break; | 
|  | 7104 | } | 
|  | 7105 | } | 
|  | 7106 | return effect; | 
|  | 7107 | } | 
|  | 7108 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7109 | // getEffectFromId_l() must be called with ThreadBase::mLock held | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7110 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7111 | { | 
|  | 7112 | sp<EffectModule> effect; | 
|  | 7113 | size_t size = mEffects.size(); | 
|  | 7114 |  | 
|  | 7115 | for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7116 | // by convention, return first effect if id provided is 0 (0 is never a valid id) | 
|  | 7117 | if (id == 0 || mEffects[i]->id() == id) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7118 | effect = mEffects[i]; | 
|  | 7119 | break; | 
|  | 7120 | } | 
|  | 7121 | } | 
|  | 7122 | return effect; | 
|  | 7123 | } | 
|  | 7124 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7125 | // getEffectFromType_l() must be called with ThreadBase::mLock held | 
|  | 7126 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( | 
|  | 7127 | const effect_uuid_t *type) | 
|  | 7128 | { | 
|  | 7129 | sp<EffectModule> effect; | 
|  | 7130 | size_t size = mEffects.size(); | 
|  | 7131 |  | 
|  | 7132 | for (size_t i = 0; i < size; i++) { | 
|  | 7133 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { | 
|  | 7134 | effect = mEffects[i]; | 
|  | 7135 | break; | 
|  | 7136 | } | 
|  | 7137 | } | 
|  | 7138 | return effect; | 
|  | 7139 | } | 
|  | 7140 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7141 | // Must be called with EffectChain::mLock locked | 
|  | 7142 | void AudioFlinger::EffectChain::process_l() | 
|  | 7143 | { | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7144 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7145 | if (thread == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7146 | ALOGW("process_l(): cannot promote mixer thread"); | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7147 | return; | 
|  | 7148 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7149 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || | 
|  | 7150 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7151 | // always process effects unless no more tracks are on the session and the effect tail | 
|  | 7152 | // has been rendered | 
|  | 7153 | bool doProcess = true; | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7154 | if (!isGlobalSession) { | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7155 | bool tracksOnSession = (trackCnt() != 0); | 
| Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7156 |  | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7157 | if (!tracksOnSession && mTailBufferCount == 0) { | 
|  | 7158 | doProcess = false; | 
|  | 7159 | } | 
|  | 7160 |  | 
|  | 7161 | if (activeTrackCnt() == 0) { | 
|  | 7162 | // if no track is active and the effect tail has not been rendered, | 
|  | 7163 | // the input buffer must be cleared here as the mixer process will not do it | 
|  | 7164 | if (tracksOnSession || mTailBufferCount > 0) { | 
|  | 7165 | size_t numSamples = thread->frameCount() * thread->channelCount(); | 
|  | 7166 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); | 
|  | 7167 | if (mTailBufferCount > 0) { | 
|  | 7168 | mTailBufferCount--; | 
|  | 7169 | } | 
|  | 7170 | } | 
|  | 7171 | } | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7172 | } | 
|  | 7173 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7174 | size_t size = mEffects.size(); | 
| Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7175 | if (doProcess) { | 
| Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7176 | for (size_t i = 0; i < size; i++) { | 
|  | 7177 | mEffects[i]->process(); | 
|  | 7178 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7179 | } | 
|  | 7180 | for (size_t i = 0; i < size; i++) { | 
|  | 7181 | mEffects[i]->updateState(); | 
|  | 7182 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7183 | } | 
|  | 7184 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7185 | // addEffect_l() must be called with PlaybackThread::mLock held | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7186 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7187 | { | 
|  | 7188 | effect_descriptor_t desc = effect->desc(); | 
|  | 7189 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; | 
|  | 7190 |  | 
|  | 7191 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7192 | effect->setChain(this); | 
|  | 7193 | sp<ThreadBase> thread = mThread.promote(); | 
|  | 7194 | if (thread == 0) { | 
|  | 7195 | return NO_INIT; | 
|  | 7196 | } | 
|  | 7197 | effect->setThread(thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7198 |  | 
|  | 7199 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 7200 | // Auxiliary effects are inserted at the beginning of mEffects vector as | 
|  | 7201 | // they are processed first and accumulated in chain input buffer | 
|  | 7202 | mEffects.insertAt(effect, 0); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7203 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7204 | // the input buffer for auxiliary effect contains mono samples in | 
|  | 7205 | // 32 bit format. This is to avoid saturation in AudoMixer | 
|  | 7206 | // accumulation stage. Saturation is done in EffectModule::process() before | 
|  | 7207 | // calling the process in effect engine | 
|  | 7208 | size_t numSamples = thread->frameCount(); | 
|  | 7209 | int32_t *buffer = new int32_t[numSamples]; | 
|  | 7210 | memset(buffer, 0, numSamples * sizeof(int32_t)); | 
|  | 7211 | effect->setInBuffer((int16_t *)buffer); | 
|  | 7212 | // auxiliary effects output samples to chain input buffer for further processing | 
|  | 7213 | // by insert effects | 
|  | 7214 | effect->setOutBuffer(mInBuffer); | 
|  | 7215 | } else { | 
|  | 7216 | // Insert effects are inserted at the end of mEffects vector as they are processed | 
|  | 7217 | //  after track and auxiliary effects. | 
|  | 7218 | // Insert effect order as a function of indicated preference: | 
|  | 7219 | //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if | 
|  | 7220 | //  another effect is present | 
|  | 7221 | //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the | 
|  | 7222 | //  last effect claiming first position | 
|  | 7223 | //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the | 
|  | 7224 | //  first effect claiming last position | 
|  | 7225 | //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last | 
|  | 7226 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is | 
|  | 7227 | // already present | 
|  | 7228 |  | 
|  | 7229 | int size = (int)mEffects.size(); | 
|  | 7230 | int idx_insert = size; | 
|  | 7231 | int idx_insert_first = -1; | 
|  | 7232 | int idx_insert_last = -1; | 
|  | 7233 |  | 
|  | 7234 | for (int i = 0; i < size; i++) { | 
|  | 7235 | effect_descriptor_t d = mEffects[i]->desc(); | 
|  | 7236 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; | 
|  | 7237 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; | 
|  | 7238 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { | 
|  | 7239 | // check invalid effect chaining combinations | 
|  | 7240 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || | 
|  | 7241 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7242 | 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] | 7243 | return INVALID_OPERATION; | 
|  | 7244 | } | 
|  | 7245 | // remember position of first insert effect and by default | 
|  | 7246 | // select this as insert position for new effect | 
|  | 7247 | if (idx_insert == size) { | 
|  | 7248 | idx_insert = i; | 
|  | 7249 | } | 
|  | 7250 | // remember position of last insert effect claiming | 
|  | 7251 | // first position | 
|  | 7252 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { | 
|  | 7253 | idx_insert_first = i; | 
|  | 7254 | } | 
|  | 7255 | // remember position of first insert effect claiming | 
|  | 7256 | // last position | 
|  | 7257 | if (iPref == EFFECT_FLAG_INSERT_LAST && | 
|  | 7258 | idx_insert_last == -1) { | 
|  | 7259 | idx_insert_last = i; | 
|  | 7260 | } | 
|  | 7261 | } | 
|  | 7262 | } | 
|  | 7263 |  | 
|  | 7264 | // modify idx_insert from first position if needed | 
|  | 7265 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { | 
|  | 7266 | if (idx_insert_last != -1) { | 
|  | 7267 | idx_insert = idx_insert_last; | 
|  | 7268 | } else { | 
|  | 7269 | idx_insert = size; | 
|  | 7270 | } | 
|  | 7271 | } else { | 
|  | 7272 | if (idx_insert_first != -1) { | 
|  | 7273 | idx_insert = idx_insert_first + 1; | 
|  | 7274 | } | 
|  | 7275 | } | 
|  | 7276 |  | 
|  | 7277 | // always read samples from chain input buffer | 
|  | 7278 | effect->setInBuffer(mInBuffer); | 
|  | 7279 |  | 
|  | 7280 | // if last effect in the chain, output samples to chain | 
|  | 7281 | // output buffer, otherwise to chain input buffer | 
|  | 7282 | if (idx_insert == size) { | 
|  | 7283 | if (idx_insert != 0) { | 
|  | 7284 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); | 
|  | 7285 | mEffects[idx_insert-1]->configure(); | 
|  | 7286 | } | 
|  | 7287 | effect->setOutBuffer(mOutBuffer); | 
|  | 7288 | } else { | 
|  | 7289 | effect->setOutBuffer(mInBuffer); | 
|  | 7290 | } | 
|  | 7291 | mEffects.insertAt(effect, idx_insert); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7292 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7293 | 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] | 7294 | } | 
|  | 7295 | effect->configure(); | 
|  | 7296 | return NO_ERROR; | 
|  | 7297 | } | 
|  | 7298 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7299 | // removeEffect_l() must be called with PlaybackThread::mLock held | 
|  | 7300 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7301 | { | 
|  | 7302 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7303 | int size = (int)mEffects.size(); | 
|  | 7304 | int i; | 
|  | 7305 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; | 
|  | 7306 |  | 
|  | 7307 | for (i = 0; i < size; i++) { | 
|  | 7308 | if (effect == mEffects[i]) { | 
| Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7309 | // calling stop here will remove pre-processing effect from the audio HAL. | 
|  | 7310 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in | 
|  | 7311 | // the middle of a read from audio HAL | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7312 | if (mEffects[i]->state() == EffectModule::ACTIVE || | 
|  | 7313 | mEffects[i]->state() == EffectModule::STOPPING) { | 
|  | 7314 | mEffects[i]->stop(); | 
|  | 7315 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7316 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 7317 | delete[] effect->inBuffer(); | 
|  | 7318 | } else { | 
|  | 7319 | if (i == size - 1 && i != 0) { | 
|  | 7320 | mEffects[i - 1]->setOutBuffer(mOutBuffer); | 
|  | 7321 | mEffects[i - 1]->configure(); | 
|  | 7322 | } | 
|  | 7323 | } | 
|  | 7324 | mEffects.removeAt(i); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7325 | 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] | 7326 | break; | 
|  | 7327 | } | 
|  | 7328 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7329 |  | 
|  | 7330 | return mEffects.size(); | 
|  | 7331 | } | 
|  | 7332 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7333 | // setDevice_l() must be called with PlaybackThread::mLock held | 
|  | 7334 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7335 | { | 
|  | 7336 | size_t size = mEffects.size(); | 
|  | 7337 | for (size_t i = 0; i < size; i++) { | 
|  | 7338 | mEffects[i]->setDevice(device); | 
|  | 7339 | } | 
|  | 7340 | } | 
|  | 7341 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7342 | // setMode_l() must be called with PlaybackThread::mLock held | 
|  | 7343 | void AudioFlinger::EffectChain::setMode_l(uint32_t mode) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7344 | { | 
|  | 7345 | size_t size = mEffects.size(); | 
|  | 7346 | for (size_t i = 0; i < size; i++) { | 
|  | 7347 | mEffects[i]->setMode(mode); | 
|  | 7348 | } | 
|  | 7349 | } | 
|  | 7350 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7351 | // setVolume_l() must be called with PlaybackThread::mLock held | 
|  | 7352 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7353 | { | 
|  | 7354 | uint32_t newLeft = *left; | 
|  | 7355 | uint32_t newRight = *right; | 
|  | 7356 | bool hasControl = false; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7357 | int ctrlIdx = -1; | 
|  | 7358 | size_t size = mEffects.size(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7359 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7360 | // first update volume controller | 
|  | 7361 | for (size_t i = size; i > 0; i--) { | 
| Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7362 | if (mEffects[i - 1]->isProcessEnabled() && | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7363 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { | 
|  | 7364 | ctrlIdx = i - 1; | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7365 | hasControl = true; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7366 | break; | 
|  | 7367 | } | 
|  | 7368 | } | 
|  | 7369 |  | 
|  | 7370 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7371 | if (hasControl) { | 
|  | 7372 | *left = mNewLeftVolume; | 
|  | 7373 | *right = mNewRightVolume; | 
|  | 7374 | } | 
|  | 7375 | return hasControl; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7376 | } | 
|  | 7377 |  | 
|  | 7378 | mVolumeCtrlIdx = ctrlIdx; | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7379 | mLeftVolume = newLeft; | 
|  | 7380 | mRightVolume = newRight; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7381 |  | 
|  | 7382 | // second get volume update from volume controller | 
|  | 7383 | if (ctrlIdx >= 0) { | 
|  | 7384 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); | 
| Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7385 | mNewLeftVolume = newLeft; | 
|  | 7386 | mNewRightVolume = newRight; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7387 | } | 
|  | 7388 | // then indicate volume to all other effects in chain. | 
|  | 7389 | // Pass altered volume to effects before volume controller | 
|  | 7390 | // and requested volume to effects after controller | 
|  | 7391 | uint32_t lVol = newLeft; | 
|  | 7392 | uint32_t rVol = newRight; | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7393 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7394 | for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7395 | if ((int)i == ctrlIdx) continue; | 
|  | 7396 | // this also works for ctrlIdx == -1 when there is no volume controller | 
|  | 7397 | if ((int)i > ctrlIdx) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7398 | lVol = *left; | 
|  | 7399 | rVol = *right; | 
|  | 7400 | } | 
|  | 7401 | mEffects[i]->setVolume(&lVol, &rVol, false); | 
|  | 7402 | } | 
|  | 7403 | *left = newLeft; | 
|  | 7404 | *right = newRight; | 
|  | 7405 |  | 
|  | 7406 | return hasControl; | 
|  | 7407 | } | 
|  | 7408 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7409 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) | 
|  | 7410 | { | 
|  | 7411 | const size_t SIZE = 256; | 
|  | 7412 | char buffer[SIZE]; | 
|  | 7413 | String8 result; | 
|  | 7414 |  | 
|  | 7415 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); | 
|  | 7416 | result.append(buffer); | 
|  | 7417 |  | 
|  | 7418 | bool locked = tryLock(mLock); | 
|  | 7419 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 7420 | if (!locked) { | 
|  | 7421 | result.append("\tCould not lock mutex:\n"); | 
|  | 7422 | } | 
|  | 7423 |  | 
| Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7424 | result.append("\tNum fx In buffer   Out buffer   Active tracks:\n"); | 
|  | 7425 | snprintf(buffer, SIZE, "\t%02d     0x%08x  0x%08x   %d\n", | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7426 | mEffects.size(), | 
|  | 7427 | (uint32_t)mInBuffer, | 
|  | 7428 | (uint32_t)mOutBuffer, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7429 | mActiveTrackCnt); | 
|  | 7430 | result.append(buffer); | 
|  | 7431 | write(fd, result.string(), result.size()); | 
|  | 7432 |  | 
|  | 7433 | for (size_t i = 0; i < mEffects.size(); ++i) { | 
|  | 7434 | sp<EffectModule> effect = mEffects[i]; | 
|  | 7435 | if (effect != 0) { | 
|  | 7436 | effect->dump(fd, args); | 
|  | 7437 | } | 
|  | 7438 | } | 
|  | 7439 |  | 
|  | 7440 | if (locked) { | 
|  | 7441 | mLock.unlock(); | 
|  | 7442 | } | 
|  | 7443 |  | 
|  | 7444 | return NO_ERROR; | 
|  | 7445 | } | 
|  | 7446 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7447 | // must be called with ThreadBase::mLock held | 
|  | 7448 | void AudioFlinger::EffectChain::setEffectSuspended_l( | 
|  | 7449 | const effect_uuid_t *type, bool suspend) | 
|  | 7450 | { | 
|  | 7451 | sp<SuspendedEffectDesc> desc; | 
|  | 7452 | // use effect type UUID timelow as key as there is no real risk of identical | 
|  | 7453 | // timeLow fields among effect type UUIDs. | 
|  | 7454 | int index = mSuspendedEffects.indexOfKey(type->timeLow); | 
|  | 7455 | if (suspend) { | 
|  | 7456 | if (index >= 0) { | 
|  | 7457 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7458 | } else { | 
|  | 7459 | desc = new SuspendedEffectDesc(); | 
|  | 7460 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); | 
|  | 7461 | mSuspendedEffects.add(type->timeLow, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7462 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7463 | } | 
|  | 7464 | if (desc->mRefCount++ == 0) { | 
|  | 7465 | sp<EffectModule> effect = getEffectIfEnabled(type); | 
|  | 7466 | if (effect != 0) { | 
|  | 7467 | desc->mEffect = effect; | 
|  | 7468 | effect->setSuspended(true); | 
|  | 7469 | effect->setEnabled(false); | 
|  | 7470 | } | 
|  | 7471 | } | 
|  | 7472 | } else { | 
|  | 7473 | if (index < 0) { | 
|  | 7474 | return; | 
|  | 7475 | } | 
|  | 7476 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7477 | if (desc->mRefCount <= 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7478 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7479 | desc->mRefCount = 1; | 
|  | 7480 | } | 
|  | 7481 | if (--desc->mRefCount == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7482 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7483 | if (desc->mEffect != 0) { | 
|  | 7484 | sp<EffectModule> effect = desc->mEffect.promote(); | 
|  | 7485 | if (effect != 0) { | 
|  | 7486 | effect->setSuspended(false); | 
|  | 7487 | sp<EffectHandle> handle = effect->controlHandle(); | 
|  | 7488 | if (handle != 0) { | 
|  | 7489 | effect->setEnabled(handle->enabled()); | 
|  | 7490 | } | 
|  | 7491 | } | 
|  | 7492 | desc->mEffect.clear(); | 
|  | 7493 | } | 
|  | 7494 | mSuspendedEffects.removeItemsAt(index); | 
|  | 7495 | } | 
|  | 7496 | } | 
|  | 7497 | } | 
|  | 7498 |  | 
|  | 7499 | // must be called with ThreadBase::mLock held | 
|  | 7500 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) | 
|  | 7501 | { | 
|  | 7502 | sp<SuspendedEffectDesc> desc; | 
|  | 7503 |  | 
|  | 7504 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
|  | 7505 | if (suspend) { | 
|  | 7506 | if (index >= 0) { | 
|  | 7507 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7508 | } else { | 
|  | 7509 | desc = new SuspendedEffectDesc(); | 
|  | 7510 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7511 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7512 | } | 
|  | 7513 | if (desc->mRefCount++ == 0) { | 
|  | 7514 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); | 
|  | 7515 | for (size_t i = 0; i < effects.size(); i++) { | 
|  | 7516 | setEffectSuspended_l(&effects[i]->desc().type, true); | 
|  | 7517 | } | 
|  | 7518 | } | 
|  | 7519 | } else { | 
|  | 7520 | if (index < 0) { | 
|  | 7521 | return; | 
|  | 7522 | } | 
|  | 7523 | desc = mSuspendedEffects.valueAt(index); | 
|  | 7524 | if (desc->mRefCount <= 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7525 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7526 | desc->mRefCount = 1; | 
|  | 7527 | } | 
|  | 7528 | if (--desc->mRefCount == 0) { | 
|  | 7529 | Vector<const effect_uuid_t *> types; | 
|  | 7530 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { | 
|  | 7531 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { | 
|  | 7532 | continue; | 
|  | 7533 | } | 
|  | 7534 | types.add(&mSuspendedEffects.valueAt(i)->mType); | 
|  | 7535 | } | 
|  | 7536 | for (size_t i = 0; i < types.size(); i++) { | 
|  | 7537 | setEffectSuspended_l(types[i], false); | 
|  | 7538 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7539 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7540 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); | 
|  | 7541 | } | 
|  | 7542 | } | 
|  | 7543 | } | 
|  | 7544 |  | 
| Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7545 |  | 
|  | 7546 | // The volume effect is used for automated tests only | 
|  | 7547 | #ifndef OPENSL_ES_H_ | 
|  | 7548 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, | 
|  | 7549 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; | 
|  | 7550 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; | 
|  | 7551 | #endif //OPENSL_ES_H_ | 
|  | 7552 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7553 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) | 
|  | 7554 | { | 
|  | 7555 | // auxiliary effects and visualizer are never suspended on output mix | 
|  | 7556 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && | 
|  | 7557 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || | 
| Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7558 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || | 
|  | 7559 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7560 | return false; | 
|  | 7561 | } | 
|  | 7562 | return true; | 
|  | 7563 | } | 
|  | 7564 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7565 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() | 
|  | 7566 | { | 
|  | 7567 | Vector< sp<EffectModule> > effects; | 
|  | 7568 | for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7569 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7570 | continue; | 
|  | 7571 | } | 
|  | 7572 | effects.add(mEffects[i]); | 
|  | 7573 | } | 
|  | 7574 | return effects; | 
|  | 7575 | } | 
|  | 7576 |  | 
|  | 7577 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( | 
|  | 7578 | const effect_uuid_t *type) | 
|  | 7579 | { | 
|  | 7580 | sp<EffectModule> effect; | 
|  | 7581 | effect = getEffectFromType_l(type); | 
|  | 7582 | if (effect != 0 && !effect->isEnabled()) { | 
|  | 7583 | effect.clear(); | 
|  | 7584 | } | 
|  | 7585 | return effect; | 
|  | 7586 | } | 
|  | 7587 |  | 
|  | 7588 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
|  | 7589 | bool enabled) | 
|  | 7590 | { | 
|  | 7591 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
|  | 7592 | if (enabled) { | 
|  | 7593 | if (index < 0) { | 
|  | 7594 | // if the effect is not suspend check if all effects are suspended | 
|  | 7595 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
|  | 7596 | if (index < 0) { | 
|  | 7597 | return; | 
|  | 7598 | } | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7599 | if (!isEffectEligibleForSuspend(effect->desc())) { | 
|  | 7600 | return; | 
|  | 7601 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7602 | setEffectSuspended_l(&effect->desc().type, enabled); | 
|  | 7603 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7604 | if (index < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 7605 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7606 | return; | 
|  | 7607 | } | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7608 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7609 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7610 | effect->desc().type.timeLow); | 
|  | 7611 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
|  | 7612 | // if effect is requested to suspended but was not yet enabled, supend it now. | 
|  | 7613 | if (desc->mEffect == 0) { | 
|  | 7614 | desc->mEffect = effect; | 
|  | 7615 | effect->setEnabled(false); | 
|  | 7616 | effect->setSuspended(true); | 
|  | 7617 | } | 
|  | 7618 | } else { | 
|  | 7619 | if (index < 0) { | 
|  | 7620 | return; | 
|  | 7621 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7622 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7623 | effect->desc().type.timeLow); | 
|  | 7624 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
|  | 7625 | desc->mEffect.clear(); | 
|  | 7626 | effect->setSuspended(false); | 
|  | 7627 | } | 
|  | 7628 | } | 
|  | 7629 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7630 | #undef LOG_TAG | 
|  | 7631 | #define LOG_TAG "AudioFlinger" | 
|  | 7632 |  | 
|  | 7633 | // ---------------------------------------------------------------------------- | 
|  | 7634 |  | 
|  | 7635 | status_t AudioFlinger::onTransact( | 
|  | 7636 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 7637 | { | 
|  | 7638 | return BnAudioFlinger::onTransact(code, data, reply, flags); | 
|  | 7639 | } | 
|  | 7640 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7641 | }; // namespace android |