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> |
| 38 | |
| 39 | #include <media/AudioTrack.h> |
| 40 | #include <media/AudioRecord.h> |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 41 | #include <media/IMediaPlayerService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
| 43 | #include <private/media/AudioTrackShared.h> |
| 44 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 47 | #include <hardware/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | #include "AudioMixer.h" |
| 50 | #include "AudioFlinger.h" |
| 51 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 6d8b694 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 53 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 54 | #include <audio_effects/effect_ns.h> |
| 55 | #include <audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 57 | #include <cpustats/ThreadCpuUsage.h> |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 58 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 59 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
| 60 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 63 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | namespace android { |
| 65 | |
| 66 | static const char* kDeadlockedString = "AudioFlinger may be deadlocked\n"; |
| 67 | static const char* kHardwareLockedString = "Hardware lock is taken\n"; |
| 68 | |
| 69 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); |
| 70 | static const float MAX_GAIN = 4096.0f; |
| 71 | static const float MAX_GAIN_INT = 0x1000; |
| 72 | |
| 73 | // retry counts for buffer fill timeout |
| 74 | // 50 * ~20msecs = 1 second |
| 75 | static const int8_t kMaxTrackRetries = 50; |
| 76 | static const int8_t kMaxTrackStartupRetries = 50; |
| 77 | // allow less retry attempts on direct output thread. |
| 78 | // direct outputs can be a scarce resource in audio hardware and should |
| 79 | // be released as quickly as possible. |
| 80 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 81 | |
| 82 | static const int kDumpLockRetries = 50; |
| 83 | static const int kDumpLockSleep = 20000; |
| 84 | |
| 85 | static const nsecs_t kWarningThrottle = seconds(5); |
| 86 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 87 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 88 | static const int kRecordThreadSleepUs = 5000; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 | |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 90 | static const nsecs_t kSetParametersTimeout = seconds(2); |
| 91 | |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 92 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 93 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 94 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 95 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 96 | |
| 97 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 98 | // ---------------------------------------------------------------------------- |
| 99 | |
| 100 | static bool recordingAllowed() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 102 | bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO")); |
| 103 | if (!ok) LOGE("Request requires android.permission.RECORD_AUDIO"); |
| 104 | return ok; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static bool settingsAllowed() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 109 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); |
| 110 | if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 111 | return ok; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 114 | // To collect the amplifier usage |
| 115 | static void addBatteryData(uint32_t params) { |
| 116 | sp<IBinder> binder = |
| 117 | defaultServiceManager()->getService(String16("media.player")); |
| 118 | sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); |
| 119 | if (service.get() == NULL) { |
| 120 | LOGW("Cannot connect to the MediaPlayerService for battery tracking"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | service->addBatteryData(params); |
| 125 | } |
| 126 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 127 | static int load_audio_interface(const char *if_name, const hw_module_t **mod, |
| 128 | audio_hw_device_t **dev) |
| 129 | { |
| 130 | int rc; |
| 131 | |
| 132 | rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod); |
| 133 | if (rc) |
| 134 | goto out; |
| 135 | |
| 136 | rc = audio_hw_device_open(*mod, dev); |
| 137 | LOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)", |
| 138 | AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); |
| 139 | if (rc) |
| 140 | goto out; |
| 141 | |
| 142 | return 0; |
| 143 | |
| 144 | out: |
| 145 | *mod = NULL; |
| 146 | *dev = NULL; |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | static const char *audio_interfaces[] = { |
| 151 | "primary", |
| 152 | "a2dp", |
| 153 | "usb", |
| 154 | }; |
| 155 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 156 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 157 | // ---------------------------------------------------------------------------- |
| 158 | |
| 159 | AudioFlinger::AudioFlinger() |
| 160 | : BnAudioFlinger(), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 161 | mPrimaryHardwareDev(NULL), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1), |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 162 | mBtNrecIsOff(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 163 | { |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void AudioFlinger::onFirstRef() |
| 167 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 168 | int rc = 0; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 169 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 170 | Mutex::Autolock _l(mLock); |
| 171 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 172 | /* TODO: move all this work into an Init() function */ |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 173 | mHardwareStatus = AUDIO_HW_IDLE; |
| 174 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 175 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 176 | const hw_module_t *mod; |
| 177 | audio_hw_device_t *dev; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 178 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 179 | rc = load_audio_interface(audio_interfaces[i], &mod, &dev); |
| 180 | if (rc) |
| 181 | continue; |
| 182 | |
| 183 | LOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i], |
| 184 | mod->name, mod->id); |
| 185 | mAudioHwDevs.push(dev); |
| 186 | |
| 187 | if (!mPrimaryHardwareDev) { |
| 188 | mPrimaryHardwareDev = dev; |
| 189 | LOGI("Using '%s' (%s.%s) as the primary audio interface", |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 190 | mod->name, mod->id, audio_interfaces[i]); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 193 | |
| 194 | mHardwareStatus = AUDIO_HW_INIT; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 195 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 196 | if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) { |
| 197 | LOGE("Primary audio interface not found"); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 202 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 203 | |
| 204 | mHardwareStatus = AUDIO_HW_INIT; |
| 205 | rc = dev->init_check(dev); |
| 206 | if (rc == 0) { |
| 207 | AutoMutex lock(mHardwareLock); |
| 208 | |
| 209 | mMode = AUDIO_MODE_NORMAL; |
| 210 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 211 | dev->set_mode(dev, mMode); |
| 212 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 213 | dev->set_master_volume(dev, 1.0f); |
| 214 | mHardwareStatus = AUDIO_HW_IDLE; |
| 215 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 216 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 219 | status_t AudioFlinger::initCheck() const |
| 220 | { |
| 221 | Mutex::Autolock _l(mLock); |
| 222 | if (mPrimaryHardwareDev == NULL || mAudioHwDevs.size() == 0) |
| 223 | return NO_INIT; |
| 224 | return NO_ERROR; |
| 225 | } |
| 226 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 227 | AudioFlinger::~AudioFlinger() |
| 228 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 229 | int num_devs = mAudioHwDevs.size(); |
| 230 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 231 | while (!mRecordThreads.isEmpty()) { |
| 232 | // closeInput() will remove first entry from mRecordThreads |
| 233 | closeInput(mRecordThreads.keyAt(0)); |
| 234 | } |
| 235 | while (!mPlaybackThreads.isEmpty()) { |
| 236 | // closeOutput() will remove first entry from mPlaybackThreads |
| 237 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 238 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 239 | |
| 240 | for (int i = 0; i < num_devs; i++) { |
| 241 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 242 | audio_hw_device_close(dev); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 243 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 244 | mAudioHwDevs.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 247 | audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices) |
| 248 | { |
| 249 | /* first matching HW device is returned */ |
| 250 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 251 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 252 | if ((dev->get_supported_devices(dev) & devices) == devices) |
| 253 | return dev; |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 257 | |
| 258 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 259 | { |
| 260 | const size_t SIZE = 256; |
| 261 | char buffer[SIZE]; |
| 262 | String8 result; |
| 263 | |
| 264 | result.append("Clients:\n"); |
| 265 | for (size_t i = 0; i < mClients.size(); ++i) { |
| 266 | wp<Client> wClient = mClients.valueAt(i); |
| 267 | if (wClient != 0) { |
| 268 | sp<Client> client = wClient.promote(); |
| 269 | if (client != 0) { |
| 270 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 271 | result.append(buffer); |
| 272 | } |
| 273 | } |
| 274 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 275 | |
| 276 | result.append("Global session refs:\n"); |
| 277 | result.append(" session pid cnt\n"); |
| 278 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 279 | AudioSessionRef *r = mAudioSessionRefs[i]; |
| 280 | snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt); |
| 281 | result.append(buffer); |
| 282 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 283 | write(fd, result.string(), result.size()); |
| 284 | return NO_ERROR; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 289 | { |
| 290 | const size_t SIZE = 256; |
| 291 | char buffer[SIZE]; |
| 292 | String8 result; |
| 293 | int hardwareStatus = mHardwareStatus; |
| 294 | |
| 295 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); |
| 296 | result.append(buffer); |
| 297 | write(fd, result.string(), result.size()); |
| 298 | return NO_ERROR; |
| 299 | } |
| 300 | |
| 301 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 302 | { |
| 303 | const size_t SIZE = 256; |
| 304 | char buffer[SIZE]; |
| 305 | String8 result; |
| 306 | snprintf(buffer, SIZE, "Permission Denial: " |
| 307 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 308 | IPCThreadState::self()->getCallingPid(), |
| 309 | IPCThreadState::self()->getCallingUid()); |
| 310 | result.append(buffer); |
| 311 | write(fd, result.string(), result.size()); |
| 312 | return NO_ERROR; |
| 313 | } |
| 314 | |
| 315 | static bool tryLock(Mutex& mutex) |
| 316 | { |
| 317 | bool locked = false; |
| 318 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 319 | if (mutex.tryLock() == NO_ERROR) { |
| 320 | locked = true; |
| 321 | break; |
| 322 | } |
| 323 | usleep(kDumpLockSleep); |
| 324 | } |
| 325 | return locked; |
| 326 | } |
| 327 | |
| 328 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 329 | { |
| 330 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 331 | dumpPermissionDenial(fd, args); |
| 332 | } else { |
| 333 | // get state of hardware lock |
| 334 | bool hardwareLocked = tryLock(mHardwareLock); |
| 335 | if (!hardwareLocked) { |
| 336 | String8 result(kHardwareLockedString); |
| 337 | write(fd, result.string(), result.size()); |
| 338 | } else { |
| 339 | mHardwareLock.unlock(); |
| 340 | } |
| 341 | |
| 342 | bool locked = tryLock(mLock); |
| 343 | |
| 344 | // failed to lock - AudioFlinger is probably deadlocked |
| 345 | if (!locked) { |
| 346 | String8 result(kDeadlockedString); |
| 347 | write(fd, result.string(), result.size()); |
| 348 | } |
| 349 | |
| 350 | dumpClients(fd, args); |
| 351 | dumpInternals(fd, args); |
| 352 | |
| 353 | // dump playback threads |
| 354 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 355 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 356 | } |
| 357 | |
| 358 | // dump record threads |
| 359 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 360 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 361 | } |
| 362 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 363 | // dump all hardware devs |
| 364 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 365 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 366 | dev->dump(dev, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 367 | } |
| 368 | if (locked) mLock.unlock(); |
| 369 | } |
| 370 | return NO_ERROR; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | // IAudioFlinger interface |
| 375 | |
| 376 | |
| 377 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 378 | pid_t pid, |
| 379 | int streamType, |
| 380 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 381 | uint32_t format, |
| 382 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 383 | int frameCount, |
| 384 | uint32_t flags, |
| 385 | const sp<IMemory>& sharedBuffer, |
| 386 | int output, |
| 387 | int *sessionId, |
| 388 | status_t *status) |
| 389 | { |
| 390 | sp<PlaybackThread::Track> track; |
| 391 | sp<TrackHandle> trackHandle; |
| 392 | sp<Client> client; |
| 393 | wp<Client> wclient; |
| 394 | status_t lStatus; |
| 395 | int lSessionId; |
| 396 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 397 | if (streamType >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 398 | LOGE("invalid stream type"); |
| 399 | lStatus = BAD_VALUE; |
| 400 | goto Exit; |
| 401 | } |
| 402 | |
| 403 | { |
| 404 | Mutex::Autolock _l(mLock); |
| 405 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 406 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 407 | if (thread == NULL) { |
| 408 | LOGE("unknown output thread"); |
| 409 | lStatus = BAD_VALUE; |
| 410 | goto Exit; |
| 411 | } |
| 412 | |
| 413 | wclient = mClients.valueFor(pid); |
| 414 | |
| 415 | if (wclient != NULL) { |
| 416 | client = wclient.promote(); |
| 417 | } else { |
| 418 | client = new Client(this, pid); |
| 419 | mClients.add(pid, client); |
| 420 | } |
| 421 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 422 | ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 423 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 424 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 425 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 426 | if (mPlaybackThreads.keyAt(i) != output) { |
| 427 | // prevent same audio session on different output threads |
| 428 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 429 | if (sessions & PlaybackThread::TRACK_SESSION) { |
| 430 | lStatus = BAD_VALUE; |
| 431 | goto Exit; |
| 432 | } |
| 433 | // check if an effect with same session ID is waiting for a track to be created |
| 434 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 435 | effectThread = t.get(); |
| 436 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 439 | lSessionId = *sessionId; |
| 440 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 441 | // if no audio session id is provided, create one here |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 442 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 443 | if (sessionId != NULL) { |
| 444 | *sessionId = lSessionId; |
| 445 | } |
| 446 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 447 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 448 | |
| 449 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 450 | channelMask, frameCount, sharedBuffer, lSessionId, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 451 | |
| 452 | // move effect chain to this output thread if an effect on same session was waiting |
| 453 | // for a track to be created |
| 454 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 455 | Mutex::Autolock _dl(thread->mLock); |
| 456 | Mutex::Autolock _sl(effectThread->mLock); |
| 457 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 458 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 459 | } |
| 460 | if (lStatus == NO_ERROR) { |
| 461 | trackHandle = new TrackHandle(track); |
| 462 | } else { |
| 463 | // remove local strong reference to Client before deleting the Track so that the Client |
| 464 | // destructor is called by the TrackBase destructor with mLock held |
| 465 | client.clear(); |
| 466 | track.clear(); |
| 467 | } |
| 468 | |
| 469 | Exit: |
| 470 | if(status) { |
| 471 | *status = lStatus; |
| 472 | } |
| 473 | return trackHandle; |
| 474 | } |
| 475 | |
| 476 | uint32_t AudioFlinger::sampleRate(int output) const |
| 477 | { |
| 478 | Mutex::Autolock _l(mLock); |
| 479 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 480 | if (thread == NULL) { |
| 481 | LOGW("sampleRate() unknown thread %d", output); |
| 482 | return 0; |
| 483 | } |
| 484 | return thread->sampleRate(); |
| 485 | } |
| 486 | |
| 487 | int AudioFlinger::channelCount(int output) const |
| 488 | { |
| 489 | Mutex::Autolock _l(mLock); |
| 490 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 491 | if (thread == NULL) { |
| 492 | LOGW("channelCount() unknown thread %d", output); |
| 493 | return 0; |
| 494 | } |
| 495 | return thread->channelCount(); |
| 496 | } |
| 497 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 498 | uint32_t AudioFlinger::format(int output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 499 | { |
| 500 | Mutex::Autolock _l(mLock); |
| 501 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 502 | if (thread == NULL) { |
| 503 | LOGW("format() unknown thread %d", output); |
| 504 | return 0; |
| 505 | } |
| 506 | return thread->format(); |
| 507 | } |
| 508 | |
| 509 | size_t AudioFlinger::frameCount(int output) const |
| 510 | { |
| 511 | Mutex::Autolock _l(mLock); |
| 512 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 513 | if (thread == NULL) { |
| 514 | LOGW("frameCount() unknown thread %d", output); |
| 515 | return 0; |
| 516 | } |
| 517 | return thread->frameCount(); |
| 518 | } |
| 519 | |
| 520 | uint32_t AudioFlinger::latency(int output) const |
| 521 | { |
| 522 | Mutex::Autolock _l(mLock); |
| 523 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 524 | if (thread == NULL) { |
| 525 | LOGW("latency() unknown thread %d", output); |
| 526 | return 0; |
| 527 | } |
| 528 | return thread->latency(); |
| 529 | } |
| 530 | |
| 531 | status_t AudioFlinger::setMasterVolume(float value) |
| 532 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 533 | status_t ret = initCheck(); |
| 534 | if (ret != NO_ERROR) { |
| 535 | return ret; |
| 536 | } |
| 537 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 538 | // check calling permissions |
| 539 | if (!settingsAllowed()) { |
| 540 | return PERMISSION_DENIED; |
| 541 | } |
| 542 | |
| 543 | // when hw supports master volume, don't scale in sw mixer |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 544 | { // scope for the lock |
| 545 | AutoMutex lock(mHardwareLock); |
| 546 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 547 | if (mPrimaryHardwareDev->set_master_volume(mPrimaryHardwareDev, value) == NO_ERROR) { |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 548 | value = 1.0f; |
| 549 | } |
| 550 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 551 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 552 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 553 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 554 | mMasterVolume = value; |
| 555 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 556 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
| 557 | |
| 558 | return NO_ERROR; |
| 559 | } |
| 560 | |
| 561 | status_t AudioFlinger::setMode(int mode) |
| 562 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 563 | status_t ret = initCheck(); |
| 564 | if (ret != NO_ERROR) { |
| 565 | return ret; |
| 566 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 567 | |
| 568 | // check calling permissions |
| 569 | if (!settingsAllowed()) { |
| 570 | return PERMISSION_DENIED; |
| 571 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 572 | if ((mode < 0) || (mode >= AUDIO_MODE_CNT)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 573 | LOGW("Illegal value: setMode(%d)", mode); |
| 574 | return BAD_VALUE; |
| 575 | } |
| 576 | |
| 577 | { // scope for the lock |
| 578 | AutoMutex lock(mHardwareLock); |
| 579 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 580 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | mHardwareStatus = AUDIO_HW_IDLE; |
| 582 | } |
| 583 | |
| 584 | if (NO_ERROR == ret) { |
| 585 | Mutex::Autolock _l(mLock); |
| 586 | mMode = mode; |
| 587 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 588 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | status_t AudioFlinger::setMicMute(bool state) |
| 595 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 596 | status_t ret = initCheck(); |
| 597 | if (ret != NO_ERROR) { |
| 598 | return ret; |
| 599 | } |
| 600 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 601 | // check calling permissions |
| 602 | if (!settingsAllowed()) { |
| 603 | return PERMISSION_DENIED; |
| 604 | } |
| 605 | |
| 606 | AutoMutex lock(mHardwareLock); |
| 607 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 608 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 609 | mHardwareStatus = AUDIO_HW_IDLE; |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | bool AudioFlinger::getMicMute() const |
| 614 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 615 | status_t ret = initCheck(); |
| 616 | if (ret != NO_ERROR) { |
| 617 | return false; |
| 618 | } |
| 619 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 620 | bool state = AUDIO_MODE_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 621 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 622 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 623 | mHardwareStatus = AUDIO_HW_IDLE; |
| 624 | return state; |
| 625 | } |
| 626 | |
| 627 | status_t AudioFlinger::setMasterMute(bool muted) |
| 628 | { |
| 629 | // check calling permissions |
| 630 | if (!settingsAllowed()) { |
| 631 | return PERMISSION_DENIED; |
| 632 | } |
| 633 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 634 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 635 | mMasterMute = muted; |
| 636 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 637 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
| 638 | |
| 639 | return NO_ERROR; |
| 640 | } |
| 641 | |
| 642 | float AudioFlinger::masterVolume() const |
| 643 | { |
| 644 | return mMasterVolume; |
| 645 | } |
| 646 | |
| 647 | bool AudioFlinger::masterMute() const |
| 648 | { |
| 649 | return mMasterMute; |
| 650 | } |
| 651 | |
| 652 | status_t AudioFlinger::setStreamVolume(int stream, float value, int output) |
| 653 | { |
| 654 | // check calling permissions |
| 655 | if (!settingsAllowed()) { |
| 656 | return PERMISSION_DENIED; |
| 657 | } |
| 658 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 659 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 660 | return BAD_VALUE; |
| 661 | } |
| 662 | |
| 663 | AutoMutex lock(mLock); |
| 664 | PlaybackThread *thread = NULL; |
| 665 | if (output) { |
| 666 | thread = checkPlaybackThread_l(output); |
| 667 | if (thread == NULL) { |
| 668 | return BAD_VALUE; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | mStreamTypes[stream].volume = value; |
| 673 | |
| 674 | if (thread == NULL) { |
| 675 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 676 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
| 677 | } |
| 678 | } else { |
| 679 | thread->setStreamVolume(stream, value); |
| 680 | } |
| 681 | |
| 682 | return NO_ERROR; |
| 683 | } |
| 684 | |
| 685 | status_t AudioFlinger::setStreamMute(int stream, bool muted) |
| 686 | { |
| 687 | // check calling permissions |
| 688 | if (!settingsAllowed()) { |
| 689 | return PERMISSION_DENIED; |
| 690 | } |
| 691 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 692 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT || |
| 693 | uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 694 | return BAD_VALUE; |
| 695 | } |
| 696 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 697 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 698 | mStreamTypes[stream].mute = muted; |
| 699 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 700 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
| 701 | |
| 702 | return NO_ERROR; |
| 703 | } |
| 704 | |
| 705 | float AudioFlinger::streamVolume(int stream, int output) const |
| 706 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 707 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 708 | return 0.0f; |
| 709 | } |
| 710 | |
| 711 | AutoMutex lock(mLock); |
| 712 | float volume; |
| 713 | if (output) { |
| 714 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 715 | if (thread == NULL) { |
| 716 | return 0.0f; |
| 717 | } |
| 718 | volume = thread->streamVolume(stream); |
| 719 | } else { |
| 720 | volume = mStreamTypes[stream].volume; |
| 721 | } |
| 722 | |
| 723 | return volume; |
| 724 | } |
| 725 | |
| 726 | bool AudioFlinger::streamMute(int stream) const |
| 727 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 728 | if (stream < 0 || stream >= (int)AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | |
| 732 | return mStreamTypes[stream].mute; |
| 733 | } |
| 734 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 735 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) |
| 736 | { |
| 737 | status_t result; |
| 738 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 739 | ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 740 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 741 | // check calling permissions |
| 742 | if (!settingsAllowed()) { |
| 743 | return PERMISSION_DENIED; |
| 744 | } |
| 745 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 746 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 747 | if (ioHandle == 0) { |
| 748 | AutoMutex lock(mHardwareLock); |
| 749 | mHardwareStatus = AUDIO_SET_PARAMETER; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 750 | status_t final_result = NO_ERROR; |
| 751 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 752 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 753 | result = dev->set_parameters(dev, keyValuePairs.string()); |
| 754 | final_result = result ?: final_result; |
| 755 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 756 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 757 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 758 | AudioParameter param = AudioParameter(keyValuePairs); |
| 759 | String8 value; |
| 760 | if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { |
| 761 | Mutex::Autolock _l(mLock); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 762 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 763 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 764 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 765 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
| 766 | RecordThread::RecordTrack *track = thread->track(); |
| 767 | if (track != NULL) { |
| 768 | audio_devices_t device = (audio_devices_t)( |
| 769 | thread->device() & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 770 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 771 | thread->setEffectSuspended(FX_IID_AEC, |
| 772 | suspend, |
| 773 | track->sessionId()); |
| 774 | thread->setEffectSuspended(FX_IID_NS, |
| 775 | suspend, |
| 776 | track->sessionId()); |
| 777 | } |
| 778 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 779 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 782 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 786 | // and the thread is exited once the lock is released |
| 787 | sp<ThreadBase> thread; |
| 788 | { |
| 789 | Mutex::Autolock _l(mLock); |
| 790 | thread = checkPlaybackThread_l(ioHandle); |
| 791 | if (thread == NULL) { |
| 792 | thread = checkRecordThread_l(ioHandle); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 793 | } else if (thread.get() == primaryPlaybackThread_l()) { |
| 794 | // indicate output device change to all input threads for pre processing |
| 795 | AudioParameter param = AudioParameter(keyValuePairs); |
| 796 | int value; |
| 797 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 798 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 799 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 800 | } |
| 801 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | if (thread != NULL) { |
| 805 | result = thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 806 | return result; |
| 807 | } |
| 808 | return BAD_VALUE; |
| 809 | } |
| 810 | |
| 811 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) |
| 812 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 813 | // ALOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 814 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 815 | |
| 816 | if (ioHandle == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 817 | String8 out_s8; |
| 818 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 819 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 820 | audio_hw_device_t *dev = mAudioHwDevs[i]; |
| 821 | char *s = dev->get_parameters(dev, keys.string()); |
| 822 | out_s8 += String8(s); |
| 823 | free(s); |
| 824 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 825 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | Mutex::Autolock _l(mLock); |
| 829 | |
| 830 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 831 | if (playbackThread != NULL) { |
| 832 | return playbackThread->getParameters(keys); |
| 833 | } |
| 834 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 835 | if (recordThread != NULL) { |
| 836 | return recordThread->getParameters(keys); |
| 837 | } |
| 838 | return String8(""); |
| 839 | } |
| 840 | |
| 841 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) |
| 842 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 843 | status_t ret = initCheck(); |
| 844 | if (ret != NO_ERROR) { |
| 845 | return 0; |
| 846 | } |
| 847 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 848 | return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | unsigned int AudioFlinger::getInputFramesLost(int ioHandle) |
| 852 | { |
| 853 | if (ioHandle == 0) { |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | Mutex::Autolock _l(mLock); |
| 858 | |
| 859 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 860 | if (recordThread != NULL) { |
| 861 | return recordThread->getInputFramesLost(); |
| 862 | } |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | status_t AudioFlinger::setVoiceVolume(float value) |
| 867 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 868 | status_t ret = initCheck(); |
| 869 | if (ret != NO_ERROR) { |
| 870 | return ret; |
| 871 | } |
| 872 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 873 | // check calling permissions |
| 874 | if (!settingsAllowed()) { |
| 875 | return PERMISSION_DENIED; |
| 876 | } |
| 877 | |
| 878 | AutoMutex lock(mHardwareLock); |
| 879 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 880 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 881 | mHardwareStatus = AUDIO_HW_IDLE; |
| 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output) |
| 887 | { |
| 888 | status_t status; |
| 889 | |
| 890 | Mutex::Autolock _l(mLock); |
| 891 | |
| 892 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 893 | if (playbackThread != NULL) { |
| 894 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 895 | } |
| 896 | |
| 897 | return BAD_VALUE; |
| 898 | } |
| 899 | |
| 900 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 901 | { |
| 902 | |
| 903 | Mutex::Autolock _l(mLock); |
| 904 | |
| 905 | int pid = IPCThreadState::self()->getCallingPid(); |
| 906 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 907 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 908 | client, |
| 909 | pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 910 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 911 | |
| 912 | mNotificationClients.add(pid, notificationClient); |
| 913 | |
| 914 | sp<IBinder> binder = client->asBinder(); |
| 915 | binder->linkToDeath(notificationClient); |
| 916 | |
| 917 | // the config change is always sent from playback or record threads to avoid deadlock |
| 918 | // with AudioSystem::gLock |
| 919 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 920 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 921 | } |
| 922 | |
| 923 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 924 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 930 | { |
| 931 | Mutex::Autolock _l(mLock); |
| 932 | |
| 933 | int index = mNotificationClients.indexOfKey(pid); |
| 934 | if (index >= 0) { |
| 935 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 936 | ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 937 | mNotificationClients.removeItem(pid); |
| 938 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 939 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 940 | ALOGV("%d died, releasing its sessions", pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 941 | int num = mAudioSessionRefs.size(); |
| 942 | bool removed = false; |
| 943 | for (int i = 0; i< num; i++) { |
| 944 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 945 | ALOGV(" pid %d @ %d", ref->pid, i); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 946 | if (ref->pid == pid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 947 | ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 948 | mAudioSessionRefs.removeAt(i); |
| 949 | delete ref; |
| 950 | removed = true; |
| 951 | i--; |
| 952 | num--; |
| 953 | } |
| 954 | } |
| 955 | if (removed) { |
| 956 | purgeStaleEffects_l(); |
| 957 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
| 961 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) |
| 962 | { |
| 963 | size_t size = mNotificationClients.size(); |
| 964 | for (size_t i = 0; i < size; i++) { |
| 965 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 970 | void AudioFlinger::removeClient_l(pid_t pid) |
| 971 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 972 | 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] | 973 | mClients.removeItem(pid); |
| 974 | } |
| 975 | |
| 976 | |
| 977 | // ---------------------------------------------------------------------------- |
| 978 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 979 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 980 | : Thread(false), |
| 981 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 982 | mFrameSize(1), mFormat(0), mStandby(false), mId(id), mExiting(false), |
| 983 | mDevice(device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 984 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 985 | mDeathRecipient = new PMDeathRecipient(this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | AudioFlinger::ThreadBase::~ThreadBase() |
| 989 | { |
| 990 | mParamCond.broadcast(); |
| 991 | mNewParameters.clear(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 992 | // do not lock the mutex in destructor |
| 993 | releaseWakeLock_l(); |
Eric Laurent | 9d18ec5 | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 994 | if (mPowerManager != 0) { |
| 995 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 996 | binder->unlinkToDeath(mDeathRecipient); |
| 997 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | void AudioFlinger::ThreadBase::exit() |
| 1001 | { |
| 1002 | // keep a strong ref on ourself so that we wont get |
| 1003 | // destroyed in the middle of requestExitAndWait() |
| 1004 | sp <ThreadBase> strongMe = this; |
| 1005 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1006 | ALOGV("ThreadBase::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1007 | { |
| 1008 | AutoMutex lock(&mLock); |
| 1009 | mExiting = true; |
| 1010 | requestExit(); |
| 1011 | mWaitWorkCV.signal(); |
| 1012 | } |
| 1013 | requestExitAndWait(); |
| 1014 | } |
| 1015 | |
| 1016 | uint32_t AudioFlinger::ThreadBase::sampleRate() const |
| 1017 | { |
| 1018 | return mSampleRate; |
| 1019 | } |
| 1020 | |
| 1021 | int AudioFlinger::ThreadBase::channelCount() const |
| 1022 | { |
| 1023 | return (int)mChannelCount; |
| 1024 | } |
| 1025 | |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1026 | uint32_t AudioFlinger::ThreadBase::format() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1027 | { |
| 1028 | return mFormat; |
| 1029 | } |
| 1030 | |
| 1031 | size_t AudioFlinger::ThreadBase::frameCount() const |
| 1032 | { |
| 1033 | return mFrameCount; |
| 1034 | } |
| 1035 | |
| 1036 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 1037 | { |
| 1038 | status_t status; |
| 1039 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1040 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1041 | Mutex::Autolock _l(mLock); |
| 1042 | |
| 1043 | mNewParameters.add(keyValuePairs); |
| 1044 | mWaitWorkCV.signal(); |
| 1045 | // wait condition with timeout in case the thread loop has exited |
| 1046 | // before the request could be processed |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 1047 | if (mParamCond.waitRelative(mLock, kSetParametersTimeout) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1048 | status = mParamStatus; |
| 1049 | mWaitWorkCV.signal(); |
| 1050 | } else { |
| 1051 | status = TIMED_OUT; |
| 1052 | } |
| 1053 | return status; |
| 1054 | } |
| 1055 | |
| 1056 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1057 | { |
| 1058 | Mutex::Autolock _l(mLock); |
| 1059 | sendConfigEvent_l(event, param); |
| 1060 | } |
| 1061 | |
| 1062 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 1063 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 1064 | { |
| 1065 | ConfigEvent *configEvent = new ConfigEvent(); |
| 1066 | configEvent->mEvent = event; |
| 1067 | configEvent->mParam = param; |
| 1068 | mConfigEvents.add(configEvent); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1069 | 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] | 1070 | mWaitWorkCV.signal(); |
| 1071 | } |
| 1072 | |
| 1073 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1074 | { |
| 1075 | mLock.lock(); |
| 1076 | while(!mConfigEvents.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1077 | ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1078 | ConfigEvent *configEvent = mConfigEvents[0]; |
| 1079 | mConfigEvents.removeAt(0); |
| 1080 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1081 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 1082 | mLock.unlock(); |
| 1083 | mAudioFlinger->mLock.lock(); |
| 1084 | audioConfigChanged_l(configEvent->mEvent, configEvent->mParam); |
| 1085 | mAudioFlinger->mLock.unlock(); |
| 1086 | delete configEvent; |
| 1087 | mLock.lock(); |
| 1088 | } |
| 1089 | mLock.unlock(); |
| 1090 | } |
| 1091 | |
| 1092 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 1093 | { |
| 1094 | const size_t SIZE = 256; |
| 1095 | char buffer[SIZE]; |
| 1096 | String8 result; |
| 1097 | |
| 1098 | bool locked = tryLock(mLock); |
| 1099 | if (!locked) { |
| 1100 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 1101 | write(fd, buffer, strlen(buffer)); |
| 1102 | } |
| 1103 | |
| 1104 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 1105 | result.append(buffer); |
| 1106 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 1107 | result.append(buffer); |
| 1108 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); |
| 1109 | result.append(buffer); |
| 1110 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 1111 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1112 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1113 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1114 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 1115 | result.append(buffer); |
| 1116 | snprintf(buffer, SIZE, "Frame size: %d\n", mFrameSize); |
| 1117 | result.append(buffer); |
| 1118 | |
| 1119 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 1120 | result.append(buffer); |
| 1121 | result.append(" Index Command"); |
| 1122 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 1123 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 1124 | result.append(buffer); |
| 1125 | result.append(mNewParameters[i]); |
| 1126 | } |
| 1127 | |
| 1128 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 1129 | result.append(buffer); |
| 1130 | snprintf(buffer, SIZE, " Index event param\n"); |
| 1131 | result.append(buffer); |
| 1132 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
| 1133 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i]->mEvent, mConfigEvents[i]->mParam); |
| 1134 | result.append(buffer); |
| 1135 | } |
| 1136 | result.append("\n"); |
| 1137 | |
| 1138 | write(fd, result.string(), result.size()); |
| 1139 | |
| 1140 | if (locked) { |
| 1141 | mLock.unlock(); |
| 1142 | } |
| 1143 | return NO_ERROR; |
| 1144 | } |
| 1145 | |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1146 | status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1147 | { |
| 1148 | const size_t SIZE = 256; |
| 1149 | char buffer[SIZE]; |
| 1150 | String8 result; |
| 1151 | |
| 1152 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1153 | write(fd, buffer, strlen(buffer)); |
| 1154 | |
| 1155 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1156 | sp<EffectChain> chain = mEffectChains[i]; |
| 1157 | if (chain != 0) { |
| 1158 | chain->dump(fd, args); |
| 1159 | } |
| 1160 | } |
| 1161 | return NO_ERROR; |
| 1162 | } |
| 1163 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1164 | void AudioFlinger::ThreadBase::acquireWakeLock() |
| 1165 | { |
| 1166 | Mutex::Autolock _l(mLock); |
| 1167 | acquireWakeLock_l(); |
| 1168 | } |
| 1169 | |
| 1170 | void AudioFlinger::ThreadBase::acquireWakeLock_l() |
| 1171 | { |
| 1172 | if (mPowerManager == 0) { |
| 1173 | // use checkService() to avoid blocking if power service is not up yet |
| 1174 | sp<IBinder> binder = |
| 1175 | defaultServiceManager()->checkService(String16("power")); |
| 1176 | if (binder == 0) { |
| 1177 | LOGW("Thread %s cannot connect to the power manager service", mName); |
| 1178 | } else { |
| 1179 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1180 | binder->linkToDeath(mDeathRecipient); |
| 1181 | } |
| 1182 | } |
| 1183 | if (mPowerManager != 0) { |
| 1184 | sp<IBinder> binder = new BBinder(); |
| 1185 | status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
| 1186 | binder, |
| 1187 | String16(mName)); |
| 1188 | if (status == NO_ERROR) { |
| 1189 | mWakeLockToken = binder; |
| 1190 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1191 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1196 | { |
| 1197 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6dbe883 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1198 | releaseWakeLock_l(); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1202 | { |
| 1203 | if (mWakeLockToken != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1204 | ALOGV("releaseWakeLock_l() %s", mName); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1205 | if (mPowerManager != 0) { |
| 1206 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 1207 | } |
| 1208 | mWakeLockToken.clear(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1213 | { |
| 1214 | Mutex::Autolock _l(mLock); |
| 1215 | releaseWakeLock_l(); |
| 1216 | mPowerManager.clear(); |
| 1217 | } |
| 1218 | |
| 1219 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) |
| 1220 | { |
| 1221 | sp<ThreadBase> thread = mThread.promote(); |
| 1222 | if (thread != 0) { |
| 1223 | thread->clearPowerManager(); |
| 1224 | } |
| 1225 | LOGW("power manager service died !!!"); |
| 1226 | } |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1227 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1228 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1229 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1230 | { |
| 1231 | Mutex::Autolock _l(mLock); |
| 1232 | setEffectSuspended_l(type, suspend, sessionId); |
| 1233 | } |
| 1234 | |
| 1235 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1236 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1237 | { |
| 1238 | sp<EffectChain> chain; |
| 1239 | chain = getEffectChain_l(sessionId); |
| 1240 | if (chain != 0) { |
| 1241 | if (type != NULL) { |
| 1242 | chain->setEffectSuspended_l(type, suspend); |
| 1243 | } else { |
| 1244 | chain->setEffectSuspendedAll_l(suspend); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1249 | } |
| 1250 | |
| 1251 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1252 | { |
| 1253 | int index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1254 | if (index < 0) { |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects = |
| 1259 | mSuspendedSessions.editValueAt(index); |
| 1260 | |
| 1261 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1262 | sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1263 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1264 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1265 | chain->setEffectSuspendedAll_l(true); |
| 1266 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1267 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1268 | desc->mType.timeLow); |
| 1269 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1275 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1276 | bool suspend, |
| 1277 | int sessionId) |
| 1278 | { |
| 1279 | int index = mSuspendedSessions.indexOfKey(sessionId); |
| 1280 | |
| 1281 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1282 | |
| 1283 | if (suspend) { |
| 1284 | if (index >= 0) { |
| 1285 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1286 | } else { |
| 1287 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1288 | } |
| 1289 | } else { |
| 1290 | if (index < 0) { |
| 1291 | return; |
| 1292 | } |
| 1293 | sessionEffects = mSuspendedSessions.editValueAt(index); |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | int key = EffectChain::kKeyForSuspendAll; |
| 1298 | if (type != NULL) { |
| 1299 | key = type->timeLow; |
| 1300 | } |
| 1301 | index = sessionEffects.indexOfKey(key); |
| 1302 | |
| 1303 | sp <SuspendedSessionDesc> desc; |
| 1304 | if (suspend) { |
| 1305 | if (index >= 0) { |
| 1306 | desc = sessionEffects.valueAt(index); |
| 1307 | } else { |
| 1308 | desc = new SuspendedSessionDesc(); |
| 1309 | if (type != NULL) { |
| 1310 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 1311 | } |
| 1312 | sessionEffects.add(key, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1313 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1314 | } |
| 1315 | desc->mRefCount++; |
| 1316 | } else { |
| 1317 | if (index < 0) { |
| 1318 | return; |
| 1319 | } |
| 1320 | desc = sessionEffects.valueAt(index); |
| 1321 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1322 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1323 | sessionEffects.removeItemsAt(index); |
| 1324 | if (sessionEffects.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1325 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1326 | sessionId); |
| 1327 | mSuspendedSessions.removeItem(sessionId); |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | if (!sessionEffects.isEmpty()) { |
| 1332 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1337 | bool enabled, |
| 1338 | int sessionId) |
| 1339 | { |
| 1340 | Mutex::Autolock _l(mLock); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1341 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1342 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1343 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1344 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1345 | bool enabled, |
| 1346 | int sessionId) |
| 1347 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1348 | if (mType != RECORD) { |
| 1349 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1350 | // another session. This gives the priority to well behaved effect control panels |
| 1351 | // and applications not using global effects. |
| 1352 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
| 1353 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1354 | } |
| 1355 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1356 | |
| 1357 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1358 | if (chain != 0) { |
| 1359 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1363 | // ---------------------------------------------------------------------------- |
| 1364 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1365 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1366 | AudioStreamOut* output, |
| 1367 | int id, |
| 1368 | uint32_t device) |
| 1369 | : ThreadBase(audioFlinger, id, device), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 1370 | mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), mOutput(output), |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1371 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1372 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1373 | snprintf(mName, kNameLength, "AudioOut_%d", id); |
| 1374 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1375 | readOutputParameters(); |
| 1376 | |
| 1377 | mMasterVolume = mAudioFlinger->masterVolume(); |
| 1378 | mMasterMute = mAudioFlinger->masterMute(); |
| 1379 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1380 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1381 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); |
| 1382 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1383 | mStreamTypes[stream].valid = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1388 | { |
| 1389 | delete [] mMixBuffer; |
| 1390 | } |
| 1391 | |
| 1392 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1393 | { |
| 1394 | dumpInternals(fd, args); |
| 1395 | dumpTracks(fd, args); |
| 1396 | dumpEffectChains(fd, args); |
| 1397 | return NO_ERROR; |
| 1398 | } |
| 1399 | |
| 1400 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1401 | { |
| 1402 | const size_t SIZE = 256; |
| 1403 | char buffer[SIZE]; |
| 1404 | String8 result; |
| 1405 | |
| 1406 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1407 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1408 | 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] | 1409 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1410 | sp<Track> track = mTracks[i]; |
| 1411 | if (track != 0) { |
| 1412 | track->dump(buffer, SIZE); |
| 1413 | result.append(buffer); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1418 | result.append(buffer); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1419 | 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] | 1420 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
| 1421 | wp<Track> wTrack = mActiveTracks[i]; |
| 1422 | if (wTrack != 0) { |
| 1423 | sp<Track> track = wTrack.promote(); |
| 1424 | if (track != 0) { |
| 1425 | track->dump(buffer, SIZE); |
| 1426 | result.append(buffer); |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | write(fd, result.string(), result.size()); |
| 1431 | return NO_ERROR; |
| 1432 | } |
| 1433 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1434 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1435 | { |
| 1436 | const size_t SIZE = 256; |
| 1437 | char buffer[SIZE]; |
| 1438 | String8 result; |
| 1439 | |
| 1440 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1441 | result.append(buffer); |
| 1442 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1443 | result.append(buffer); |
| 1444 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1445 | result.append(buffer); |
| 1446 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1447 | result.append(buffer); |
| 1448 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1449 | result.append(buffer); |
| 1450 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1451 | result.append(buffer); |
| 1452 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1453 | result.append(buffer); |
| 1454 | write(fd, result.string(), result.size()); |
| 1455 | |
| 1456 | dumpBase(fd, args); |
| 1457 | |
| 1458 | return NO_ERROR; |
| 1459 | } |
| 1460 | |
| 1461 | // Thread virtuals |
| 1462 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1463 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1464 | status_t status = initCheck(); |
| 1465 | if (status == NO_ERROR) { |
| 1466 | LOGI("AudioFlinger's thread %p ready to run", this); |
| 1467 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1468 | LOGE("No working audio driver found."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1469 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1470 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1474 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1475 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1479 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1480 | const sp<AudioFlinger::Client>& client, |
| 1481 | int streamType, |
| 1482 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1483 | uint32_t format, |
| 1484 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1485 | int frameCount, |
| 1486 | const sp<IMemory>& sharedBuffer, |
| 1487 | int sessionId, |
| 1488 | status_t *status) |
| 1489 | { |
| 1490 | sp<Track> track; |
| 1491 | status_t lStatus; |
| 1492 | |
| 1493 | if (mType == DIRECT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1494 | if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { |
| 1495 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
| 1496 | LOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \"" |
| 1497 | "for output %p with format %d", |
| 1498 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1499 | lStatus = BAD_VALUE; |
| 1500 | goto Exit; |
| 1501 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } else { |
| 1504 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1505 | if (sampleRate > mSampleRate*2) { |
| 1506 | LOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
| 1507 | lStatus = BAD_VALUE; |
| 1508 | goto Exit; |
| 1509 | } |
| 1510 | } |
| 1511 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1512 | lStatus = initCheck(); |
| 1513 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1514 | LOGE("Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1515 | goto Exit; |
| 1516 | } |
| 1517 | |
| 1518 | { // scope for mLock |
| 1519 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1520 | |
| 1521 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1522 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1523 | // manager |
| 1524 | uint32_t strategy = |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1525 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1526 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1527 | sp<Track> t = mTracks[i]; |
| 1528 | if (t != 0) { |
| 1529 | if (sessionId == t->sessionId() && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1530 | strategy != AudioSystem::getStrategyForStream((audio_stream_type_t)t->type())) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1531 | lStatus = BAD_VALUE; |
| 1532 | goto Exit; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1537 | track = new Track(this, client, streamType, sampleRate, format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1538 | channelMask, frameCount, sharedBuffer, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1539 | if (track->getCblk() == NULL || track->name() < 0) { |
| 1540 | lStatus = NO_MEMORY; |
| 1541 | goto Exit; |
| 1542 | } |
| 1543 | mTracks.add(track); |
| 1544 | |
| 1545 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1546 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1547 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1548 | track->setMainBuffer(chain->inBuffer()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1549 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1550 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1551 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1552 | |
| 1553 | // invalidate track immediately if the stream type was moved to another thread since |
| 1554 | // createTrack() was called by the client process. |
| 1555 | if (!mStreamTypes[streamType].valid) { |
| 1556 | LOGW("createTrack_l() on thread %p: invalidating track on stream %d", |
| 1557 | this, streamType); |
| 1558 | android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags); |
| 1559 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1560 | } |
| 1561 | lStatus = NO_ERROR; |
| 1562 | |
| 1563 | Exit: |
| 1564 | if(status) { |
| 1565 | *status = lStatus; |
| 1566 | } |
| 1567 | return track; |
| 1568 | } |
| 1569 | |
| 1570 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1571 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1572 | Mutex::Autolock _l(mLock); |
| 1573 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1574 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1575 | } else { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1576 | return 0; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1581 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1582 | mMasterVolume = value; |
| 1583 | return NO_ERROR; |
| 1584 | } |
| 1585 | |
| 1586 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1587 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1588 | mMasterMute = muted; |
| 1589 | return NO_ERROR; |
| 1590 | } |
| 1591 | |
| 1592 | float AudioFlinger::PlaybackThread::masterVolume() const |
| 1593 | { |
| 1594 | return mMasterVolume; |
| 1595 | } |
| 1596 | |
| 1597 | bool AudioFlinger::PlaybackThread::masterMute() const |
| 1598 | { |
| 1599 | return mMasterMute; |
| 1600 | } |
| 1601 | |
| 1602 | status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value) |
| 1603 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1604 | mStreamTypes[stream].volume = value; |
| 1605 | return NO_ERROR; |
| 1606 | } |
| 1607 | |
| 1608 | status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted) |
| 1609 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1610 | mStreamTypes[stream].mute = muted; |
| 1611 | return NO_ERROR; |
| 1612 | } |
| 1613 | |
| 1614 | float AudioFlinger::PlaybackThread::streamVolume(int stream) const |
| 1615 | { |
| 1616 | return mStreamTypes[stream].volume; |
| 1617 | } |
| 1618 | |
| 1619 | bool AudioFlinger::PlaybackThread::streamMute(int stream) const |
| 1620 | { |
| 1621 | return mStreamTypes[stream].mute; |
| 1622 | } |
| 1623 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1624 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1625 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1626 | { |
| 1627 | status_t status = ALREADY_EXISTS; |
| 1628 | |
| 1629 | // set retry count for buffer fill |
| 1630 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1631 | if (mActiveTracks.indexOf(track) < 0) { |
| 1632 | // the track is newly added, make sure it fills up all its |
| 1633 | // buffers before playing. This is to ensure the client will |
| 1634 | // effectively get the latency it requested. |
| 1635 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1636 | track->mResetDone = false; |
| 1637 | mActiveTracks.add(track); |
| 1638 | if (track->mainBuffer() != mMixBuffer) { |
| 1639 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1640 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1641 | 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] | 1642 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | status = NO_ERROR; |
| 1647 | } |
| 1648 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1649 | ALOGV("mWaitWorkCV.broadcast"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1650 | mWaitWorkCV.broadcast(); |
| 1651 | |
| 1652 | return status; |
| 1653 | } |
| 1654 | |
| 1655 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1656 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1657 | { |
| 1658 | track->mState = TrackBase::TERMINATED; |
| 1659 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1660 | removeTrack_l(track); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1665 | { |
| 1666 | mTracks.remove(track); |
| 1667 | deleteTrackName_l(track->name()); |
| 1668 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1669 | if (chain != 0) { |
| 1670 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1675 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1676 | String8 out_s8 = String8(""); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1677 | char *s; |
| 1678 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1679 | Mutex::Autolock _l(mLock); |
| 1680 | if (initCheck() != NO_ERROR) { |
| 1681 | return out_s8; |
| 1682 | } |
| 1683 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1684 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1685 | out_s8 = String8(s); |
| 1686 | free(s); |
| 1687 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1690 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1691 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1692 | AudioSystem::OutputDescriptor desc; |
| 1693 | void *param2 = 0; |
| 1694 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1695 | 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] | 1696 | |
| 1697 | switch (event) { |
| 1698 | case AudioSystem::OUTPUT_OPENED: |
| 1699 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1700 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1701 | desc.samplingRate = mSampleRate; |
| 1702 | desc.format = mFormat; |
| 1703 | desc.frameCount = mFrameCount; |
| 1704 | desc.latency = latency(); |
| 1705 | param2 = &desc; |
| 1706 | break; |
| 1707 | |
| 1708 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1709 | param2 = ¶m; |
| 1710 | case AudioSystem::OUTPUT_CLOSED: |
| 1711 | default: |
| 1712 | break; |
| 1713 | } |
| 1714 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1715 | } |
| 1716 | |
| 1717 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1718 | { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1719 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1720 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
| 1721 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1722 | mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
| 1723 | mFrameSize = (uint16_t)audio_stream_frame_size(&mOutput->stream->common); |
| 1724 | mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1725 | |
| 1726 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1727 | // Allocate a stereo buffer even if HW output is mono. |
| 1728 | if (mMixBuffer != NULL) delete[] mMixBuffer; |
| 1729 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1730 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
| 1731 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1732 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1733 | // parameters into account |
| 1734 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1735 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1736 | // matter. |
| 1737 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1738 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1739 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1740 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1741 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1745 | { |
| 1746 | if (halFrames == 0 || dspFrames == 0) { |
| 1747 | return BAD_VALUE; |
| 1748 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1749 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1750 | if (initCheck() != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1751 | return INVALID_OPERATION; |
| 1752 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1753 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1754 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1755 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1758 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1759 | { |
| 1760 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1761 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1762 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1763 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1767 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1768 | if (sessionId == track->sessionId() && |
| 1769 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1770 | result |= TRACK_SESSION; |
| 1771 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1775 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1778 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1779 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1780 | // 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] | 1781 | // 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] | 1782 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1783 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1784 | } |
| 1785 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1786 | sp<Track> track = mTracks[i]; |
| 1787 | if (sessionId == track->sessionId() && |
| 1788 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1789 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1790 | } |
| 1791 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1792 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1795 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1796 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() |
| 1797 | { |
| 1798 | Mutex::Autolock _l(mLock); |
| 1799 | return mOutput; |
| 1800 | } |
| 1801 | |
| 1802 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 1803 | { |
| 1804 | Mutex::Autolock _l(mLock); |
| 1805 | AudioStreamOut *output = mOutput; |
| 1806 | mOutput = NULL; |
| 1807 | return output; |
| 1808 | } |
| 1809 | |
| 1810 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 1811 | audio_stream_t* AudioFlinger::PlaybackThread::stream() |
| 1812 | { |
| 1813 | if (mOutput == NULL) { |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | return &mOutput->stream->common; |
| 1817 | } |
| 1818 | |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 1819 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() |
| 1820 | { |
| 1821 | // A2DP output latency is not due only to buffering capacity. It also reflects encoding, |
| 1822 | // decoding and transfer time. So sleeping for half of the latency would likely cause |
| 1823 | // underruns |
| 1824 | if (audio_is_a2dp_device((audio_devices_t)mDevice)) { |
| 1825 | return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000); |
| 1826 | } else { |
| 1827 | return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2; |
| 1828 | } |
| 1829 | } |
| 1830 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1831 | // ---------------------------------------------------------------------------- |
| 1832 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1833 | 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] | 1834 | : PlaybackThread(audioFlinger, output, id, device), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 1835 | mAudioMixer(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1836 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1837 | mType = ThreadBase::MIXER; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1838 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 1839 | |
| 1840 | // FIXME - Current mixer implementation only supports stereo output |
| 1841 | if (mChannelCount == 1) { |
| 1842 | LOGE("Invalid audio hardware channel count"); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | AudioFlinger::MixerThread::~MixerThread() |
| 1847 | { |
| 1848 | delete mAudioMixer; |
| 1849 | } |
| 1850 | |
| 1851 | bool AudioFlinger::MixerThread::threadLoop() |
| 1852 | { |
| 1853 | Vector< sp<Track> > tracksToRemove; |
| 1854 | uint32_t mixerStatus = MIXER_IDLE; |
| 1855 | nsecs_t standbyTime = systemTime(); |
| 1856 | size_t mixBufferSize = mFrameCount * mFrameSize; |
| 1857 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1858 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1859 | // increase threshold again due to low power audio mode. The way this warning threshold is |
| 1860 | // calculated and its usefulness should be reconsidered anyway. |
| 1861 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1862 | nsecs_t lastWarning = 0; |
| 1863 | bool longStandbyExit = false; |
| 1864 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1865 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1866 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1867 | uint32_t sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1868 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1869 | #ifdef DEBUG_CPU_USAGE |
| 1870 | ThreadCpuUsage cpu; |
| 1871 | const CentralTendencyStatistics& stats = cpu.statistics(); |
| 1872 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1873 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1874 | acquireWakeLock(); |
| 1875 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1876 | while (!exitPending()) |
| 1877 | { |
Glenn Kasten | 4d8d0c3 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 1878 | #ifdef DEBUG_CPU_USAGE |
| 1879 | cpu.sampleAndEnable(); |
| 1880 | unsigned n = stats.n(); |
| 1881 | // cpu.elapsed() is expensive, so don't call it every loop |
| 1882 | if ((n & 127) == 1) { |
| 1883 | long long elapsed = cpu.elapsed(); |
| 1884 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 1885 | double perLoop = elapsed / (double) n; |
| 1886 | double perLoop100 = perLoop * 0.01; |
| 1887 | double mean = stats.mean(); |
| 1888 | double stddev = stats.stddev(); |
| 1889 | double minimum = stats.minimum(); |
| 1890 | double maximum = stats.maximum(); |
| 1891 | cpu.resetStatistics(); |
| 1892 | LOGI("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", |
| 1893 | elapsed * .000000001, n, perLoop * .000001, |
| 1894 | mean * .001, |
| 1895 | stddev * .001, |
| 1896 | minimum * .001, |
| 1897 | maximum * .001, |
| 1898 | mean / perLoop100, |
| 1899 | stddev / perLoop100, |
| 1900 | minimum / perLoop100, |
| 1901 | maximum / perLoop100); |
| 1902 | } |
| 1903 | } |
| 1904 | #endif |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1905 | processConfigEvents(); |
| 1906 | |
| 1907 | mixerStatus = MIXER_IDLE; |
| 1908 | { // scope for mLock |
| 1909 | |
| 1910 | Mutex::Autolock _l(mLock); |
| 1911 | |
| 1912 | if (checkForNewParameters_l()) { |
| 1913 | mixBufferSize = mFrameCount * mFrameSize; |
| 1914 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1915 | // Should be reduced to 2x after the vendor fixes the driver issue |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 1916 | // increase threshold again due to low power audio mode. The way this warning |
| 1917 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 1918 | maxPeriod = seconds(mFrameCount) / mSampleRate * 15; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1919 | activeSleepTime = activeSleepTimeUs(); |
| 1920 | idleSleepTime = idleSleepTimeUs(); |
| 1921 | } |
| 1922 | |
| 1923 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 1924 | |
| 1925 | // put audio hardware into standby after short delay |
| 1926 | if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 1927 | mSuspended) { |
| 1928 | if (!mStandby) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1929 | ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1930 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1931 | mStandby = true; |
| 1932 | mBytesWritten = 0; |
| 1933 | } |
| 1934 | |
| 1935 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 1936 | // we're about to wait, flush the binder command buffer |
| 1937 | IPCThreadState::self()->flushCommands(); |
| 1938 | |
| 1939 | if (exitPending()) break; |
| 1940 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1941 | releaseWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1942 | // wait until we have something to do... |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1943 | ALOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1944 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1945 | ALOGV("MixerThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1946 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1947 | |
| 1948 | if (mMasterMute == false) { |
| 1949 | char value[PROPERTY_VALUE_MAX]; |
| 1950 | property_get("ro.audio.silent", value, "0"); |
| 1951 | if (atoi(value)) { |
| 1952 | LOGD("Silence is golden"); |
| 1953 | setMasterMute(true); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1958 | sleepTime = idleSleepTime; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1959 | sleepTimeShift = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1960 | continue; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 1965 | |
| 1966 | // prevent any changes in effect chain list and in each effect chain |
| 1967 | // during mixing and effect process as the audio buffers could be deleted |
| 1968 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1969 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 1973 | // mix buffers... |
| 1974 | mAudioMixer->process(); |
| 1975 | sleepTime = 0; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1976 | // increase sleep time progressively when application underrun condition clears |
| 1977 | if (sleepTimeShift > 0) { |
| 1978 | sleepTimeShift--; |
| 1979 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1980 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1981 | //TODO: delay standby when effects have a tail |
| 1982 | } else { |
| 1983 | // If no tracks are ready, sleep once for the duration of an output |
| 1984 | // buffer size, then write 0s to the output |
| 1985 | if (sleepTime == 0) { |
| 1986 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1987 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 1988 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 1989 | sleepTime = kMinThreadSleepTimeUs; |
| 1990 | } |
| 1991 | // reduce sleep time in case of consecutive application underruns to avoid |
| 1992 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 1993 | // duration we would end up writing less data than needed by the audio HAL if |
| 1994 | // the condition persists. |
| 1995 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 1996 | sleepTimeShift++; |
| 1997 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1998 | } else { |
| 1999 | sleepTime = idleSleepTime; |
| 2000 | } |
| 2001 | } else if (mBytesWritten != 0 || |
| 2002 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
| 2003 | memset (mMixBuffer, 0, mixBufferSize); |
| 2004 | sleepTime = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2005 | ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2006 | } |
| 2007 | // TODO add standby time extension fct of effect tail |
| 2008 | } |
| 2009 | |
| 2010 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2011 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2012 | } |
| 2013 | // sleepTime == 0 means we must write to audio hardware |
| 2014 | if (sleepTime == 0) { |
| 2015 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2016 | effectChains[i]->process_l(); |
| 2017 | } |
| 2018 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2019 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2020 | mLastWriteTime = systemTime(); |
| 2021 | mInWrite = true; |
| 2022 | mBytesWritten += mixBufferSize; |
| 2023 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2024 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2025 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2026 | mNumWrites++; |
| 2027 | mInWrite = false; |
| 2028 | nsecs_t now = systemTime(); |
| 2029 | nsecs_t delta = now - mLastWriteTime; |
Eric Laurent | 5c4e818 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2030 | if (!mStandby && delta > maxPeriod) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2031 | mNumDelayedWrites++; |
| 2032 | if ((now - lastWarning) > kWarningThrottle) { |
| 2033 | LOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2034 | ns2ms(delta), mNumDelayedWrites, this); |
| 2035 | lastWarning = now; |
| 2036 | } |
| 2037 | if (mStandby) { |
| 2038 | longStandbyExit = true; |
| 2039 | } |
| 2040 | } |
| 2041 | mStandby = false; |
| 2042 | } else { |
| 2043 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2044 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2045 | usleep(sleepTime); |
| 2046 | } |
| 2047 | |
| 2048 | // finally let go of all our tracks, without the lock held |
| 2049 | // since we can't guarantee the destructors won't acquire that |
| 2050 | // same lock. |
| 2051 | tracksToRemove.clear(); |
| 2052 | |
| 2053 | // Effect chains will be actually deleted here if they were removed from |
| 2054 | // mEffectChains list during mixing or effects processing |
| 2055 | effectChains.clear(); |
| 2056 | } |
| 2057 | |
| 2058 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2059 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2062 | releaseWakeLock(); |
| 2063 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2064 | ALOGV("MixerThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2065 | return false; |
| 2066 | } |
| 2067 | |
| 2068 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 2069 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
| 2070 | { |
| 2071 | |
| 2072 | uint32_t mixerStatus = MIXER_IDLE; |
| 2073 | // find out which tracks need to be processed |
| 2074 | size_t count = activeTracks.size(); |
| 2075 | size_t mixedTracks = 0; |
| 2076 | size_t tracksWithEffect = 0; |
| 2077 | |
| 2078 | float masterVolume = mMasterVolume; |
| 2079 | bool masterMute = mMasterMute; |
| 2080 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2081 | if (masterMute) { |
| 2082 | masterVolume = 0; |
| 2083 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2084 | // 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] | 2085 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2086 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2087 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2088 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2089 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2090 | chain.clear(); |
| 2091 | } |
| 2092 | |
| 2093 | for (size_t i=0 ; i<count ; i++) { |
| 2094 | sp<Track> t = activeTracks[i].promote(); |
| 2095 | if (t == 0) continue; |
| 2096 | |
| 2097 | Track* const track = t.get(); |
| 2098 | audio_track_cblk_t* cblk = track->cblk(); |
| 2099 | |
| 2100 | // The first time a track is added we wait |
| 2101 | // for all its buffers to be filled before processing it |
| 2102 | mAudioMixer->setActiveTrack(track->name()); |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2103 | // make sure that we have enough frames to mix one full buffer. |
| 2104 | // enforce this condition only once to enable draining the buffer in case the client |
| 2105 | // app does not call stop() and relies on underrun to stop: |
| 2106 | // hence the test on (track->mRetryCount >= kMaxTrackRetries) meaning the track was mixed |
| 2107 | // during last round |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2108 | uint32_t minFrames = 1; |
Eric Laurent | a47b69c | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2109 | if (!track->isStopped() && !track->isPausing() && |
| 2110 | (track->mRetryCount >= kMaxTrackRetries)) { |
Eric Laurent | 3dbe320 | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2111 | if (t->sampleRate() == (int)mSampleRate) { |
| 2112 | minFrames = mFrameCount; |
| 2113 | } else { |
| 2114 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1; |
| 2115 | } |
| 2116 | } |
| 2117 | if ((cblk->framesReady() >= minFrames) && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2118 | !track->isPaused() && !track->isTerminated()) |
| 2119 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2120 | //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", track->name(), cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2121 | |
| 2122 | mixedTracks++; |
| 2123 | |
| 2124 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2125 | // connected to the track |
| 2126 | chain.clear(); |
| 2127 | if (track->mainBuffer() != mMixBuffer) { |
| 2128 | chain = getEffectChain_l(track->sessionId()); |
| 2129 | // Delegate volume control to effect in track effect chain if needed |
| 2130 | if (chain != 0) { |
| 2131 | tracksWithEffect++; |
| 2132 | } else { |
| 2133 | LOGW("prepareTracks_l(): track %08x attached to effect but no chain found on session %d", |
| 2134 | track->name(), track->sessionId()); |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | int param = AudioMixer::VOLUME; |
| 2140 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2141 | // no ramp for the first volume setting |
| 2142 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2143 | if (track->mState == TrackBase::RESUMING) { |
| 2144 | track->mState = TrackBase::ACTIVE; |
| 2145 | param = AudioMixer::RAMP_VOLUME; |
| 2146 | } |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 2147 | mAudioMixer->setParameter(AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2148 | } else if (cblk->server != 0) { |
| 2149 | // If the track is stopped before the first frame was mixed, |
| 2150 | // do not apply ramp |
| 2151 | param = AudioMixer::RAMP_VOLUME; |
| 2152 | } |
| 2153 | |
| 2154 | // compute volume for this track |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2155 | uint32_t vl, vr, va; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2156 | if (track->isMuted() || track->isPausing() || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2157 | mStreamTypes[track->type()].mute) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2158 | vl = vr = va = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2159 | if (track->isPausing()) { |
| 2160 | track->setPaused(); |
| 2161 | } |
| 2162 | } else { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2163 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2164 | // read original volumes with volume control |
| 2165 | float typeVolume = mStreamTypes[track->type()].volume; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2166 | float v = masterVolume * typeVolume; |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2167 | vl = (uint32_t)(v * cblk->volume[0]) << 12; |
| 2168 | vr = (uint32_t)(v * cblk->volume[1]) << 12; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2169 | |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2170 | va = (uint32_t)(v * cblk->sendLevel); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2171 | } |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2172 | // Delegate volume control to effect in track effect chain if needed |
| 2173 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2174 | // Do not ramp volume if volume is controlled by effect |
| 2175 | param = AudioMixer::VOLUME; |
| 2176 | track->mHasVolumeController = true; |
| 2177 | } else { |
| 2178 | // force no volume ramp when volume controller was just disabled or removed |
| 2179 | // from effect chain to avoid volume spike |
| 2180 | if (track->mHasVolumeController) { |
| 2181 | param = AudioMixer::VOLUME; |
| 2182 | } |
| 2183 | track->mHasVolumeController = false; |
| 2184 | } |
| 2185 | |
| 2186 | // Convert volumes from 8.24 to 4.12 format |
| 2187 | int16_t left, right, aux; |
| 2188 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2189 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2190 | left = int16_t(v_clamped); |
| 2191 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2192 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2193 | right = int16_t(v_clamped); |
| 2194 | |
| 2195 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; |
| 2196 | aux = int16_t(va); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2197 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2198 | // XXX: these things DON'T need to be done each time |
| 2199 | mAudioMixer->setBufferProvider(track); |
| 2200 | mAudioMixer->enable(AudioMixer::MIXING); |
| 2201 | |
| 2202 | mAudioMixer->setParameter(param, AudioMixer::VOLUME0, (void *)left); |
| 2203 | mAudioMixer->setParameter(param, AudioMixer::VOLUME1, (void *)right); |
| 2204 | mAudioMixer->setParameter(param, AudioMixer::AUXLEVEL, (void *)aux); |
| 2205 | mAudioMixer->setParameter( |
| 2206 | AudioMixer::TRACK, |
| 2207 | AudioMixer::FORMAT, (void *)track->format()); |
| 2208 | mAudioMixer->setParameter( |
| 2209 | AudioMixer::TRACK, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2210 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2211 | mAudioMixer->setParameter( |
| 2212 | AudioMixer::RESAMPLE, |
| 2213 | AudioMixer::SAMPLE_RATE, |
| 2214 | (void *)(cblk->sampleRate)); |
| 2215 | mAudioMixer->setParameter( |
| 2216 | AudioMixer::TRACK, |
| 2217 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2218 | mAudioMixer->setParameter( |
| 2219 | AudioMixer::TRACK, |
| 2220 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 2221 | |
| 2222 | // reset retry count |
| 2223 | track->mRetryCount = kMaxTrackRetries; |
| 2224 | mixerStatus = MIXER_TRACKS_READY; |
| 2225 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2226 | //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", track->name(), cblk->user, cblk->server, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2227 | if (track->isStopped()) { |
| 2228 | track->reset(); |
| 2229 | } |
| 2230 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2231 | // We have consumed all the buffers of this track. |
| 2232 | // Remove it from the list of active tracks. |
| 2233 | tracksToRemove->add(track); |
| 2234 | } else { |
| 2235 | // No buffers for this track. Give it a few chances to |
| 2236 | // fill a buffer, then remove it from active list. |
| 2237 | if (--(track->mRetryCount) <= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2238 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", track->name(), this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2239 | tracksToRemove->add(track); |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2240 | // indicate to client process that the track was disabled because of underrun |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2241 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2242 | } else if (mixerStatus != MIXER_TRACKS_READY) { |
| 2243 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2244 | } |
| 2245 | } |
| 2246 | mAudioMixer->disable(AudioMixer::MIXING); |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | // remove all the tracks that need to be... |
| 2251 | count = tracksToRemove->size(); |
| 2252 | if (UNLIKELY(count)) { |
| 2253 | for (size_t i=0 ; i<count ; i++) { |
| 2254 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 2255 | mActiveTracks.remove(track); |
| 2256 | if (track->mainBuffer() != mMixBuffer) { |
| 2257 | chain = getEffectChain_l(track->sessionId()); |
| 2258 | if (chain != 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2259 | 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] | 2260 | chain->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | if (track->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2264 | removeTrack_l(track); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | // mix buffer must be cleared if all tracks are connected to an |
| 2270 | // effect chain as in this case the mixer will not write to |
| 2271 | // mix buffer and track effects will accumulate into it |
| 2272 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 2273 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 2274 | } |
| 2275 | |
| 2276 | return mixerStatus; |
| 2277 | } |
| 2278 | |
| 2279 | void AudioFlinger::MixerThread::invalidateTracks(int streamType) |
| 2280 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2281 | ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2282 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2283 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2284 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2285 | size_t size = mTracks.size(); |
| 2286 | for (size_t i = 0; i < size; i++) { |
| 2287 | sp<Track> t = mTracks[i]; |
| 2288 | if (t->type() == streamType) { |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2289 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2290 | t->mCblk->cv.signal(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2295 | void AudioFlinger::PlaybackThread::setStreamValid(int streamType, bool valid) |
| 2296 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2297 | ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2298 | this, streamType, valid); |
| 2299 | Mutex::Autolock _l(mLock); |
| 2300 | |
| 2301 | mStreamTypes[streamType].valid = valid; |
| 2302 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2303 | |
| 2304 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2305 | int AudioFlinger::MixerThread::getTrackName_l() |
| 2306 | { |
| 2307 | return mAudioMixer->getTrackName(); |
| 2308 | } |
| 2309 | |
| 2310 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2311 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 2312 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2313 | ALOGV("remove track (%d) and delete from mixer", name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2314 | mAudioMixer->deleteTrackName(name); |
| 2315 | } |
| 2316 | |
| 2317 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2318 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 2319 | { |
| 2320 | bool reconfig = false; |
| 2321 | |
| 2322 | while (!mNewParameters.isEmpty()) { |
| 2323 | status_t status = NO_ERROR; |
| 2324 | String8 keyValuePair = mNewParameters[0]; |
| 2325 | AudioParameter param = AudioParameter(keyValuePair); |
| 2326 | int value; |
| 2327 | |
| 2328 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 2329 | reconfig = true; |
| 2330 | } |
| 2331 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2332 | if (value != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2333 | status = BAD_VALUE; |
| 2334 | } else { |
| 2335 | reconfig = true; |
| 2336 | } |
| 2337 | } |
| 2338 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2339 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2340 | status = BAD_VALUE; |
| 2341 | } else { |
| 2342 | reconfig = true; |
| 2343 | } |
| 2344 | } |
| 2345 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2346 | // do not accept frame count changes if tracks are open as the track buffer |
| 2347 | // size depends on frame count and correct behavior would not be garantied |
| 2348 | // if frame count is changed after track creation |
| 2349 | if (!mTracks.isEmpty()) { |
| 2350 | status = INVALID_OPERATION; |
| 2351 | } else { |
| 2352 | reconfig = true; |
| 2353 | } |
| 2354 | } |
| 2355 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2356 | // when changing the audio output device, call addBatteryData to notify |
| 2357 | // the change |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2358 | if ((int)mDevice != value) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2359 | uint32_t params = 0; |
| 2360 | // check whether speaker is on |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2361 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2362 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 2363 | } |
| 2364 | |
| 2365 | int deviceWithoutSpeaker |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2366 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2367 | // check if any other device (except speaker) is on |
| 2368 | if (value & deviceWithoutSpeaker ) { |
| 2369 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 2370 | } |
| 2371 | |
| 2372 | if (params != 0) { |
| 2373 | addBatteryData(params); |
| 2374 | } |
| 2375 | } |
| 2376 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2377 | // forward device change to effects that have requested to be |
| 2378 | // aware of attached audio device. |
| 2379 | mDevice = (uint32_t)value; |
| 2380 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2381 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2386 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2387 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2388 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2389 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2390 | mStandby = true; |
| 2391 | mBytesWritten = 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2392 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2393 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2394 | } |
| 2395 | if (status == NO_ERROR && reconfig) { |
| 2396 | delete mAudioMixer; |
| 2397 | readOutputParameters(); |
| 2398 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 2399 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 2400 | int name = getTrackName_l(); |
| 2401 | if (name < 0) break; |
| 2402 | mTracks[i]->mName = name; |
| 2403 | // limit track sample rate to 2 x new output sample rate |
| 2404 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 2405 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 2406 | } |
| 2407 | } |
| 2408 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | mNewParameters.removeAt(0); |
| 2413 | |
| 2414 | mParamStatus = status; |
| 2415 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2416 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2417 | // already timed out waiting for the status and will never signal the condition. |
| 2418 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2419 | } |
| 2420 | return reconfig; |
| 2421 | } |
| 2422 | |
| 2423 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2424 | { |
| 2425 | const size_t SIZE = 256; |
| 2426 | char buffer[SIZE]; |
| 2427 | String8 result; |
| 2428 | |
| 2429 | PlaybackThread::dumpInternals(fd, args); |
| 2430 | |
| 2431 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2432 | result.append(buffer); |
| 2433 | write(fd, result.string(), result.size()); |
| 2434 | return NO_ERROR; |
| 2435 | } |
| 2436 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2437 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2438 | { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2439 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2442 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() |
| 2443 | { |
| 2444 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2445 | } |
| 2446 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2447 | // ---------------------------------------------------------------------------- |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2448 | 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] | 2449 | : PlaybackThread(audioFlinger, output, id, device) |
| 2450 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2451 | mType = ThreadBase::DIRECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2455 | { |
| 2456 | } |
| 2457 | |
| 2458 | |
| 2459 | static inline int16_t clamp16(int32_t sample) |
| 2460 | { |
| 2461 | if ((sample>>15) ^ (sample>>31)) |
| 2462 | sample = 0x7FFF ^ (sample>>31); |
| 2463 | return sample; |
| 2464 | } |
| 2465 | |
| 2466 | static inline |
| 2467 | int32_t mul(int16_t in, int16_t v) |
| 2468 | { |
| 2469 | #if defined(__arm__) && !defined(__thumb__) |
| 2470 | int32_t out; |
| 2471 | asm( "smulbb %[out], %[in], %[v] \n" |
| 2472 | : [out]"=r"(out) |
| 2473 | : [in]"%r"(in), [v]"r"(v) |
| 2474 | : ); |
| 2475 | return out; |
| 2476 | #else |
| 2477 | return in * int32_t(v); |
| 2478 | #endif |
| 2479 | } |
| 2480 | |
| 2481 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2482 | { |
| 2483 | // Do not apply volume on compressed audio |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2484 | if (!audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2485 | return; |
| 2486 | } |
| 2487 | |
| 2488 | // convert to signed 16 bit before volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2489 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2490 | size_t count = mFrameCount * mChannelCount; |
| 2491 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2492 | int16_t *dst = mMixBuffer + count-1; |
| 2493 | while(count--) { |
| 2494 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | size_t frameCount = mFrameCount; |
| 2499 | int16_t *out = mMixBuffer; |
| 2500 | if (ramp) { |
| 2501 | if (mChannelCount == 1) { |
| 2502 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2503 | int32_t vlInc = d / (int32_t)frameCount; |
| 2504 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2505 | do { |
| 2506 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2507 | out++; |
| 2508 | vl += vlInc; |
| 2509 | } while (--frameCount); |
| 2510 | |
| 2511 | } else { |
| 2512 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2513 | int32_t vlInc = d / (int32_t)frameCount; |
| 2514 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2515 | int32_t vrInc = d / (int32_t)frameCount; |
| 2516 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2517 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2518 | do { |
| 2519 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2520 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2521 | out += 2; |
| 2522 | vl += vlInc; |
| 2523 | vr += vrInc; |
| 2524 | } while (--frameCount); |
| 2525 | } |
| 2526 | } else { |
| 2527 | if (mChannelCount == 1) { |
| 2528 | do { |
| 2529 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2530 | out++; |
| 2531 | } while (--frameCount); |
| 2532 | } else { |
| 2533 | do { |
| 2534 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2535 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2536 | out += 2; |
| 2537 | } while (--frameCount); |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | // convert back to unsigned 8 bit after volume calculation |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2542 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2543 | size_t count = mFrameCount * mChannelCount; |
| 2544 | int16_t *src = mMixBuffer; |
| 2545 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2546 | while(count--) { |
| 2547 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | mLeftVolShort = leftVol; |
| 2552 | mRightVolShort = rightVol; |
| 2553 | } |
| 2554 | |
| 2555 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2556 | { |
| 2557 | uint32_t mixerStatus = MIXER_IDLE; |
| 2558 | sp<Track> trackToRemove; |
| 2559 | sp<Track> activeTrack; |
| 2560 | nsecs_t standbyTime = systemTime(); |
| 2561 | int8_t *curBuf; |
| 2562 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2563 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2564 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2565 | uint32_t sleepTime = idleSleepTime; |
| 2566 | // use shorter standby delay as on normal output to release |
| 2567 | // hardware resources as soon as possible |
| 2568 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
| 2569 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2570 | acquireWakeLock(); |
| 2571 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2572 | while (!exitPending()) |
| 2573 | { |
| 2574 | bool rampVolume; |
| 2575 | uint16_t leftVol; |
| 2576 | uint16_t rightVol; |
| 2577 | Vector< sp<EffectChain> > effectChains; |
| 2578 | |
| 2579 | processConfigEvents(); |
| 2580 | |
| 2581 | mixerStatus = MIXER_IDLE; |
| 2582 | |
| 2583 | { // scope for the mLock |
| 2584 | |
| 2585 | Mutex::Autolock _l(mLock); |
| 2586 | |
| 2587 | if (checkForNewParameters_l()) { |
| 2588 | mixBufferSize = mFrameCount*mFrameSize; |
| 2589 | activeSleepTime = activeSleepTimeUs(); |
| 2590 | idleSleepTime = idleSleepTimeUs(); |
| 2591 | standbyDelay = microseconds(activeSleepTime*2); |
| 2592 | } |
| 2593 | |
| 2594 | // put audio hardware into standby after short delay |
| 2595 | if UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2596 | mSuspended) { |
| 2597 | // wait until we have something to do... |
| 2598 | if (!mStandby) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2599 | ALOGV("Audio hardware entering standby, mixer %p\n", this); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2600 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2601 | mStandby = true; |
| 2602 | mBytesWritten = 0; |
| 2603 | } |
| 2604 | |
| 2605 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2606 | // we're about to wait, flush the binder command buffer |
| 2607 | IPCThreadState::self()->flushCommands(); |
| 2608 | |
| 2609 | if (exitPending()) break; |
| 2610 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2611 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2612 | ALOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2613 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2614 | 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] | 2615 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2616 | |
| 2617 | if (mMasterMute == false) { |
| 2618 | char value[PROPERTY_VALUE_MAX]; |
| 2619 | property_get("ro.audio.silent", value, "0"); |
| 2620 | if (atoi(value)) { |
| 2621 | LOGD("Silence is golden"); |
| 2622 | setMasterMute(true); |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | standbyTime = systemTime() + standbyDelay; |
| 2627 | sleepTime = idleSleepTime; |
| 2628 | continue; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | effectChains = mEffectChains; |
| 2633 | |
| 2634 | // find out which tracks need to be processed |
| 2635 | if (mActiveTracks.size() != 0) { |
| 2636 | sp<Track> t = mActiveTracks[0].promote(); |
| 2637 | if (t == 0) continue; |
| 2638 | |
| 2639 | Track* const track = t.get(); |
| 2640 | audio_track_cblk_t* cblk = track->cblk(); |
| 2641 | |
| 2642 | // The first time a track is added we wait |
| 2643 | // for all its buffers to be filled before processing it |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2644 | if (cblk->framesReady() && track->isReady() && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2645 | !track->isPaused() && !track->isTerminated()) |
| 2646 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2647 | //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] | 2648 | |
| 2649 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2650 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2651 | mLeftVolFloat = mRightVolFloat = 0; |
| 2652 | mLeftVolShort = mRightVolShort = 0; |
| 2653 | if (track->mState == TrackBase::RESUMING) { |
| 2654 | track->mState = TrackBase::ACTIVE; |
| 2655 | rampVolume = true; |
| 2656 | } |
| 2657 | } else if (cblk->server != 0) { |
| 2658 | // If the track is stopped before the first frame was mixed, |
| 2659 | // do not apply ramp |
| 2660 | rampVolume = true; |
| 2661 | } |
| 2662 | // compute volume for this track |
| 2663 | float left, right; |
| 2664 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 2665 | mStreamTypes[track->type()].mute) { |
| 2666 | left = right = 0; |
| 2667 | if (track->isPausing()) { |
| 2668 | track->setPaused(); |
| 2669 | } |
| 2670 | } else { |
| 2671 | float typeVolume = mStreamTypes[track->type()].volume; |
| 2672 | float v = mMasterVolume * typeVolume; |
| 2673 | float v_clamped = v * cblk->volume[0]; |
| 2674 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2675 | left = v_clamped/MAX_GAIN; |
| 2676 | v_clamped = v * cblk->volume[1]; |
| 2677 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2678 | right = v_clamped/MAX_GAIN; |
| 2679 | } |
| 2680 | |
| 2681 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2682 | mLeftVolFloat = left; |
| 2683 | mRightVolFloat = right; |
| 2684 | |
| 2685 | // If audio HAL implements volume control, |
| 2686 | // force software volume to nominal value |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2687 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2688 | left = 1.0f; |
| 2689 | right = 1.0f; |
| 2690 | } |
| 2691 | |
| 2692 | // Convert volumes from float to 8.24 |
| 2693 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2694 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2695 | |
| 2696 | // Delegate volume control to effect in track effect chain if needed |
| 2697 | // only one effect chain can be present on DirectOutputThread, so if |
| 2698 | // there is one, the track is connected to it |
| 2699 | if (!effectChains.isEmpty()) { |
Eric Laurent | e0aed6d | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2700 | // Do not ramp volume if volume is controlled by effect |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2701 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2702 | rampVolume = false; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | // Convert volumes from 8.24 to 4.12 format |
| 2707 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2708 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2709 | leftVol = (uint16_t)v_clamped; |
| 2710 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2711 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2712 | rightVol = (uint16_t)v_clamped; |
| 2713 | } else { |
| 2714 | leftVol = mLeftVolShort; |
| 2715 | rightVol = mRightVolShort; |
| 2716 | rampVolume = false; |
| 2717 | } |
| 2718 | |
| 2719 | // reset retry count |
| 2720 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 2721 | activeTrack = t; |
| 2722 | mixerStatus = MIXER_TRACKS_READY; |
| 2723 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2724 | //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] | 2725 | if (track->isStopped()) { |
| 2726 | track->reset(); |
| 2727 | } |
| 2728 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2729 | // We have consumed all the buffers of this track. |
| 2730 | // Remove it from the list of active tracks. |
| 2731 | trackToRemove = track; |
| 2732 | } else { |
| 2733 | // No buffers for this track. Give it a few chances to |
| 2734 | // fill a buffer, then remove it from active list. |
| 2735 | if (--(track->mRetryCount) <= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2736 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2737 | trackToRemove = track; |
| 2738 | } else { |
| 2739 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | // remove all the tracks that need to be... |
| 2746 | if (UNLIKELY(trackToRemove != 0)) { |
| 2747 | mActiveTracks.remove(trackToRemove); |
| 2748 | if (!effectChains.isEmpty()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2749 | 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] | 2750 | trackToRemove->sessionId()); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2751 | effectChains[0]->decActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2752 | } |
| 2753 | if (trackToRemove->isTerminated()) { |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2754 | removeTrack_l(trackToRemove); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2758 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 2762 | AudioBufferProvider::Buffer buffer; |
| 2763 | size_t frameCount = mFrameCount; |
| 2764 | curBuf = (int8_t *)mMixBuffer; |
| 2765 | // output audio to hardware |
| 2766 | while (frameCount) { |
| 2767 | buffer.frameCount = frameCount; |
| 2768 | activeTrack->getNextBuffer(&buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 2769 | if (UNLIKELY(buffer.raw == NULL)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2770 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2771 | break; |
| 2772 | } |
| 2773 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2774 | frameCount -= buffer.frameCount; |
| 2775 | curBuf += buffer.frameCount * mFrameSize; |
| 2776 | activeTrack->releaseBuffer(&buffer); |
| 2777 | } |
| 2778 | sleepTime = 0; |
| 2779 | standbyTime = systemTime() + standbyDelay; |
| 2780 | } else { |
| 2781 | if (sleepTime == 0) { |
| 2782 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2783 | sleepTime = activeSleepTime; |
| 2784 | } else { |
| 2785 | sleepTime = idleSleepTime; |
| 2786 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2787 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2788 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
| 2789 | sleepTime = 0; |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2794 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2795 | } |
| 2796 | // sleepTime == 0 means we must write to audio hardware |
| 2797 | if (sleepTime == 0) { |
| 2798 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2799 | applyVolume(leftVol, rightVol, rampVolume); |
| 2800 | } |
| 2801 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2802 | effectChains[i]->process_l(); |
| 2803 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2804 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2805 | |
| 2806 | mLastWriteTime = systemTime(); |
| 2807 | mInWrite = true; |
| 2808 | mBytesWritten += mixBufferSize; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2809 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2810 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2811 | mNumWrites++; |
| 2812 | mInWrite = false; |
| 2813 | mStandby = false; |
| 2814 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2815 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2816 | usleep(sleepTime); |
| 2817 | } |
| 2818 | |
| 2819 | // finally let go of removed track, without the lock held |
| 2820 | // since we can't guarantee the destructors won't acquire that |
| 2821 | // same lock. |
| 2822 | trackToRemove.clear(); |
| 2823 | activeTrack.clear(); |
| 2824 | |
| 2825 | // Effect chains will be actually deleted here if they were removed from |
| 2826 | // mEffectChains list during mixing or effects processing |
| 2827 | effectChains.clear(); |
| 2828 | } |
| 2829 | |
| 2830 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2831 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2834 | releaseWakeLock(); |
| 2835 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2836 | ALOGV("DirectOutputThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2837 | return false; |
| 2838 | } |
| 2839 | |
| 2840 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2841 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2842 | { |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2847 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2848 | { |
| 2849 | } |
| 2850 | |
| 2851 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2852 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2853 | { |
| 2854 | bool reconfig = false; |
| 2855 | |
| 2856 | while (!mNewParameters.isEmpty()) { |
| 2857 | status_t status = NO_ERROR; |
| 2858 | String8 keyValuePair = mNewParameters[0]; |
| 2859 | AudioParameter param = AudioParameter(keyValuePair); |
| 2860 | int value; |
| 2861 | |
| 2862 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2863 | // do not accept frame count changes if tracks are open as the track buffer |
| 2864 | // size depends on frame count and correct behavior would not be garantied |
| 2865 | // if frame count is changed after track creation |
| 2866 | if (!mTracks.isEmpty()) { |
| 2867 | status = INVALID_OPERATION; |
| 2868 | } else { |
| 2869 | reconfig = true; |
| 2870 | } |
| 2871 | } |
| 2872 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2873 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2874 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2875 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2876 | mOutput->stream->common.standby(&mOutput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2877 | mStandby = true; |
| 2878 | mBytesWritten = 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2879 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2880 | keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2881 | } |
| 2882 | if (status == NO_ERROR && reconfig) { |
| 2883 | readOutputParameters(); |
| 2884 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | mNewParameters.removeAt(0); |
| 2889 | |
| 2890 | mParamStatus = status; |
| 2891 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2892 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2893 | // already timed out waiting for the status and will never signal the condition. |
| 2894 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2895 | } |
| 2896 | return reconfig; |
| 2897 | } |
| 2898 | |
| 2899 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
| 2900 | { |
| 2901 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2902 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 162b40b | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2903 | time = PlaybackThread::activeSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2904 | } else { |
| 2905 | time = 10000; |
| 2906 | } |
| 2907 | return time; |
| 2908 | } |
| 2909 | |
| 2910 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 2911 | { |
| 2912 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2913 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2914 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2915 | } else { |
| 2916 | time = 10000; |
| 2917 | } |
| 2918 | return time; |
| 2919 | } |
| 2920 | |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2921 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() |
| 2922 | { |
| 2923 | uint32_t time; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2924 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2925 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2926 | } else { |
| 2927 | time = 10000; |
| 2928 | } |
| 2929 | return time; |
| 2930 | } |
| 2931 | |
| 2932 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2933 | // ---------------------------------------------------------------------------- |
| 2934 | |
| 2935 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) |
| 2936 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) |
| 2937 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2938 | mType = ThreadBase::DUPLICATING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2939 | addOutputTrack(mainThread); |
| 2940 | } |
| 2941 | |
| 2942 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 2943 | { |
| 2944 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2945 | mOutputTracks[i]->destroy(); |
| 2946 | } |
| 2947 | mOutputTracks.clear(); |
| 2948 | } |
| 2949 | |
| 2950 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 2951 | { |
| 2952 | Vector< sp<Track> > tracksToRemove; |
| 2953 | uint32_t mixerStatus = MIXER_IDLE; |
| 2954 | nsecs_t standbyTime = systemTime(); |
| 2955 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2956 | SortedVector< sp<OutputTrack> > outputTracks; |
| 2957 | uint32_t writeFrames = 0; |
| 2958 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2959 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2960 | uint32_t sleepTime = idleSleepTime; |
| 2961 | Vector< sp<EffectChain> > effectChains; |
| 2962 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2963 | acquireWakeLock(); |
| 2964 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2965 | while (!exitPending()) |
| 2966 | { |
| 2967 | processConfigEvents(); |
| 2968 | |
| 2969 | mixerStatus = MIXER_IDLE; |
| 2970 | { // scope for the mLock |
| 2971 | |
| 2972 | Mutex::Autolock _l(mLock); |
| 2973 | |
| 2974 | if (checkForNewParameters_l()) { |
| 2975 | mixBufferSize = mFrameCount*mFrameSize; |
| 2976 | updateWaitTime(); |
| 2977 | activeSleepTime = activeSleepTimeUs(); |
| 2978 | idleSleepTime = idleSleepTimeUs(); |
| 2979 | } |
| 2980 | |
| 2981 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 2982 | |
| 2983 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2984 | outputTracks.add(mOutputTracks[i]); |
| 2985 | } |
| 2986 | |
| 2987 | // put audio hardware into standby after short delay |
| 2988 | if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 2989 | mSuspended) { |
| 2990 | if (!mStandby) { |
| 2991 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 2992 | outputTracks[i]->stop(); |
| 2993 | } |
| 2994 | mStandby = true; |
| 2995 | mBytesWritten = 0; |
| 2996 | } |
| 2997 | |
| 2998 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 2999 | // we're about to wait, flush the binder command buffer |
| 3000 | IPCThreadState::self()->flushCommands(); |
| 3001 | outputTracks.clear(); |
| 3002 | |
| 3003 | if (exitPending()) break; |
| 3004 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3005 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3006 | ALOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3007 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3008 | ALOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3009 | acquireWakeLock_l(); |
| 3010 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3011 | if (mMasterMute == false) { |
| 3012 | char value[PROPERTY_VALUE_MAX]; |
| 3013 | property_get("ro.audio.silent", value, "0"); |
| 3014 | if (atoi(value)) { |
| 3015 | LOGD("Silence is golden"); |
| 3016 | setMasterMute(true); |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 3021 | sleepTime = idleSleepTime; |
| 3022 | continue; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 3027 | |
| 3028 | // prevent any changes in effect chain list and in each effect chain |
| 3029 | // during mixing and effect process as the audio buffers could be deleted |
| 3030 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3031 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 3035 | // mix buffers... |
| 3036 | if (outputsReady(outputTracks)) { |
| 3037 | mAudioMixer->process(); |
| 3038 | } else { |
| 3039 | memset(mMixBuffer, 0, mixBufferSize); |
| 3040 | } |
| 3041 | sleepTime = 0; |
| 3042 | writeFrames = mFrameCount; |
| 3043 | } else { |
| 3044 | if (sleepTime == 0) { |
| 3045 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 3046 | sleepTime = activeSleepTime; |
| 3047 | } else { |
| 3048 | sleepTime = idleSleepTime; |
| 3049 | } |
| 3050 | } else if (mBytesWritten != 0) { |
| 3051 | // flush remaining overflow buffers in output tracks |
| 3052 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3053 | if (outputTracks[i]->isActive()) { |
| 3054 | sleepTime = 0; |
| 3055 | writeFrames = 0; |
| 3056 | memset(mMixBuffer, 0, mixBufferSize); |
| 3057 | break; |
| 3058 | } |
| 3059 | } |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | if (mSuspended) { |
Eric Laurent | 25cbe0e | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3064 | sleepTime = suspendSleepTimeUs(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3065 | } |
| 3066 | // sleepTime == 0 means we must write to audio hardware |
| 3067 | if (sleepTime == 0) { |
| 3068 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3069 | effectChains[i]->process_l(); |
| 3070 | } |
| 3071 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3072 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3073 | |
| 3074 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 3075 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3076 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 3077 | } |
| 3078 | mStandby = false; |
| 3079 | mBytesWritten += mixBufferSize; |
| 3080 | } else { |
| 3081 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3082 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3083 | usleep(sleepTime); |
| 3084 | } |
| 3085 | |
| 3086 | // finally let go of all our tracks, without the lock held |
| 3087 | // since we can't guarantee the destructors won't acquire that |
| 3088 | // same lock. |
| 3089 | tracksToRemove.clear(); |
| 3090 | outputTracks.clear(); |
| 3091 | |
| 3092 | // Effect chains will be actually deleted here if they were removed from |
| 3093 | // mEffectChains list during mixing or effects processing |
| 3094 | effectChains.clear(); |
| 3095 | } |
| 3096 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3097 | releaseWakeLock(); |
| 3098 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3099 | return false; |
| 3100 | } |
| 3101 | |
| 3102 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3103 | { |
| 3104 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
| 3105 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, |
| 3106 | this, |
| 3107 | mSampleRate, |
| 3108 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3109 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3110 | frameCount); |
| 3111 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3112 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3113 | mOutputTracks.add(outputTrack); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3114 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3115 | updateWaitTime(); |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3120 | { |
| 3121 | Mutex::Autolock _l(mLock); |
| 3122 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3123 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { |
| 3124 | mOutputTracks[i]->destroy(); |
| 3125 | mOutputTracks.removeAt(i); |
| 3126 | updateWaitTime(); |
| 3127 | return; |
| 3128 | } |
| 3129 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3130 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 3134 | { |
| 3135 | mWaitTimeMs = UINT_MAX; |
| 3136 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3137 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 3138 | if (strong != NULL) { |
| 3139 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3140 | if (waitTimeMs < mWaitTimeMs) { |
| 3141 | mWaitTimeMs = waitTimeMs; |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 3149 | { |
| 3150 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3151 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 3152 | if (thread == 0) { |
| 3153 | LOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
| 3154 | return false; |
| 3155 | } |
| 3156 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3157 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3158 | 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] | 3159 | return false; |
| 3160 | } |
| 3161 | } |
| 3162 | return true; |
| 3163 | } |
| 3164 | |
| 3165 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 3166 | { |
| 3167 | return (mWaitTimeMs * 1000) / 2; |
| 3168 | } |
| 3169 | |
| 3170 | // ---------------------------------------------------------------------------- |
| 3171 | |
| 3172 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 3173 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 3174 | const wp<ThreadBase>& thread, |
| 3175 | const sp<Client>& client, |
| 3176 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3177 | uint32_t format, |
| 3178 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3179 | int frameCount, |
| 3180 | uint32_t flags, |
| 3181 | const sp<IMemory>& sharedBuffer, |
| 3182 | int sessionId) |
| 3183 | : RefBase(), |
| 3184 | mThread(thread), |
| 3185 | mClient(client), |
| 3186 | mCblk(0), |
| 3187 | mFrameCount(0), |
| 3188 | mState(IDLE), |
| 3189 | mClientTid(-1), |
| 3190 | mFormat(format), |
| 3191 | mFlags(flags & ~SYSTEM_FLAGS_MASK), |
| 3192 | mSessionId(sessionId) |
| 3193 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3194 | ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3195 | |
| 3196 | // LOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
| 3197 | size_t size = sizeof(audio_track_cblk_t); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3198 | uint8_t channelCount = popcount(channelMask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3199 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3200 | if (sharedBuffer == 0) { |
| 3201 | size += bufferSize; |
| 3202 | } |
| 3203 | |
| 3204 | if (client != NULL) { |
| 3205 | mCblkMemory = client->heap()->allocate(size); |
| 3206 | if (mCblkMemory != 0) { |
| 3207 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
| 3208 | if (mCblk) { // construct the shared structure in-place. |
| 3209 | new(mCblk) audio_track_cblk_t(); |
| 3210 | // clear all buffers |
| 3211 | mCblk->frameCount = frameCount; |
| 3212 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3213 | mChannelCount = channelCount; |
| 3214 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3215 | if (sharedBuffer == 0) { |
| 3216 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3217 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3218 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3219 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3220 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3221 | } else { |
| 3222 | mBuffer = sharedBuffer->pointer(); |
| 3223 | } |
| 3224 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3225 | } |
| 3226 | } else { |
| 3227 | LOGE("not enough memory for AudioTrack size=%u", size); |
| 3228 | client->heap()->dump("AudioTrack"); |
| 3229 | return; |
| 3230 | } |
| 3231 | } else { |
| 3232 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
| 3233 | if (mCblk) { // construct the shared structure in-place. |
| 3234 | new(mCblk) audio_track_cblk_t(); |
| 3235 | // clear all buffers |
| 3236 | mCblk->frameCount = frameCount; |
| 3237 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3238 | mChannelCount = channelCount; |
| 3239 | mChannelMask = channelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3240 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3241 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3242 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3243 | // written to buffer (other flags are cleared) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3244 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 3245 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 3251 | { |
| 3252 | if (mCblk) { |
| 3253 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 3254 | if (mClient == NULL) { |
| 3255 | delete mCblk; |
| 3256 | } |
| 3257 | } |
| 3258 | mCblkMemory.clear(); // and free the shared memory |
| 3259 | if (mClient != NULL) { |
| 3260 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 3261 | mClient.clear(); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 3266 | { |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 3267 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3268 | mFrameCount = buffer->frameCount; |
| 3269 | step(); |
| 3270 | buffer->frameCount = 0; |
| 3271 | } |
| 3272 | |
| 3273 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 3274 | bool result; |
| 3275 | audio_track_cblk_t* cblk = this->cblk(); |
| 3276 | |
| 3277 | result = cblk->stepServer(mFrameCount); |
| 3278 | if (!result) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3279 | ALOGV("stepServer failed acquiring cblk mutex"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3280 | mFlags |= STEPSERVER_FAILED; |
| 3281 | } |
| 3282 | return result; |
| 3283 | } |
| 3284 | |
| 3285 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 3286 | audio_track_cblk_t* cblk = this->cblk(); |
| 3287 | |
| 3288 | cblk->user = 0; |
| 3289 | cblk->server = 0; |
| 3290 | cblk->userBase = 0; |
| 3291 | cblk->serverBase = 0; |
| 3292 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3293 | ALOGV("TrackBase::reset"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const |
| 3297 | { |
| 3298 | return mCblkMemory; |
| 3299 | } |
| 3300 | |
| 3301 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 3302 | return (int)mCblk->sampleRate; |
| 3303 | } |
| 3304 | |
| 3305 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3306 | return (const int)mChannelCount; |
| 3307 | } |
| 3308 | |
| 3309 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { |
| 3310 | return mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 3314 | audio_track_cblk_t* cblk = this->cblk(); |
| 3315 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize; |
| 3316 | int8_t *bufferEnd = bufferStart + frames * cblk->frameSize; |
| 3317 | |
| 3318 | // Check validity of returned pointer in case the track control block would have been corrupted. |
| 3319 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
| 3320 | ((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) { |
| 3321 | LOGE("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] | 3322 | server %d, serverBase %d, user %d, userBase %d", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3323 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3324 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3325 | return 0; |
| 3326 | } |
| 3327 | |
| 3328 | return bufferStart; |
| 3329 | } |
| 3330 | |
| 3331 | // ---------------------------------------------------------------------------- |
| 3332 | |
| 3333 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 3334 | AudioFlinger::PlaybackThread::Track::Track( |
| 3335 | const wp<ThreadBase>& thread, |
| 3336 | const sp<Client>& client, |
| 3337 | int streamType, |
| 3338 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3339 | uint32_t format, |
| 3340 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3341 | int frameCount, |
| 3342 | const sp<IMemory>& sharedBuffer, |
| 3343 | int sessionId) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3344 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3345 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), |
| 3346 | mAuxEffectId(0), mHasVolumeController(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3347 | { |
| 3348 | if (mCblk != NULL) { |
| 3349 | sp<ThreadBase> baseThread = thread.promote(); |
| 3350 | if (baseThread != 0) { |
| 3351 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); |
| 3352 | mName = playbackThread->getTrackName_l(); |
| 3353 | mMainBuffer = playbackThread->mixBuffer(); |
| 3354 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3355 | ALOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3356 | if (mName < 0) { |
| 3357 | LOGE("no more track names available"); |
| 3358 | } |
| 3359 | mVolume[0] = 1.0f; |
| 3360 | mVolume[1] = 1.0f; |
| 3361 | mStreamType = streamType; |
| 3362 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 3363 | // 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] | 3364 | 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] | 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | AudioFlinger::PlaybackThread::Track::~Track() |
| 3369 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3370 | ALOGV("PlaybackThread::Track destructor"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3371 | sp<ThreadBase> thread = mThread.promote(); |
| 3372 | if (thread != 0) { |
| 3373 | Mutex::Autolock _l(thread->mLock); |
| 3374 | mState = TERMINATED; |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 3379 | { |
| 3380 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 3381 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 3382 | // desctructor is called. As the destructor needs to lock mLock, |
| 3383 | // we must acquire a strong reference on this Track before locking mLock |
| 3384 | // here so that the destructor is called only when exiting this function. |
| 3385 | // On the other hand, as long as Track::destroy() is only called by |
| 3386 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 3387 | // this Track with its member mTrack. |
| 3388 | sp<Track> keep(this); |
| 3389 | { // scope for mLock |
| 3390 | sp<ThreadBase> thread = mThread.promote(); |
| 3391 | if (thread != 0) { |
| 3392 | if (!isOutputTrack()) { |
| 3393 | if (mState == ACTIVE || mState == RESUMING) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3394 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3395 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3396 | mSessionId); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3397 | |
| 3398 | // to track the speaker usage |
| 3399 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3400 | } |
| 3401 | AudioSystem::releaseOutput(thread->id()); |
| 3402 | } |
| 3403 | Mutex::Autolock _l(thread->mLock); |
| 3404 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3405 | playbackThread->destroyTrack_l(this); |
| 3406 | } |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 3411 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3412 | 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] | 3413 | mName - AudioMixer::TRACK0, |
| 3414 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3415 | mStreamType, |
| 3416 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3417 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3418 | mSessionId, |
| 3419 | mFrameCount, |
| 3420 | mState, |
| 3421 | mMute, |
| 3422 | mFillingUpStatus, |
| 3423 | mCblk->sampleRate, |
| 3424 | mCblk->volume[0], |
| 3425 | mCblk->volume[1], |
| 3426 | mCblk->server, |
| 3427 | mCblk->user, |
| 3428 | (int)mMainBuffer, |
| 3429 | (int)mAuxBuffer); |
| 3430 | } |
| 3431 | |
| 3432 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 3433 | { |
| 3434 | audio_track_cblk_t* cblk = this->cblk(); |
| 3435 | uint32_t framesReady; |
| 3436 | uint32_t framesReq = buffer->frameCount; |
| 3437 | |
| 3438 | // Check if last stepServer failed, try to step now |
| 3439 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3440 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3441 | ALOGV("stepServer recovered"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3442 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3443 | } |
| 3444 | |
| 3445 | framesReady = cblk->framesReady(); |
| 3446 | |
| 3447 | if (LIKELY(framesReady)) { |
| 3448 | uint32_t s = cblk->server; |
| 3449 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3450 | |
| 3451 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 3452 | if (framesReq > framesReady) { |
| 3453 | framesReq = framesReady; |
| 3454 | } |
| 3455 | if (s + framesReq > bufferEnd) { |
| 3456 | framesReq = bufferEnd - s; |
| 3457 | } |
| 3458 | |
| 3459 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 3460 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3461 | |
| 3462 | buffer->frameCount = framesReq; |
| 3463 | return NO_ERROR; |
| 3464 | } |
| 3465 | |
| 3466 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 3467 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3468 | buffer->frameCount = 0; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3469 | 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] | 3470 | return NOT_ENOUGH_DATA; |
| 3471 | } |
| 3472 | |
| 3473 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | af59ce2 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3474 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3475 | |
| 3476 | if (mCblk->framesReady() >= mCblk->frameCount || |
| 3477 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 3478 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3479 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | return true; |
| 3481 | } |
| 3482 | return false; |
| 3483 | } |
| 3484 | |
| 3485 | status_t AudioFlinger::PlaybackThread::Track::start() |
| 3486 | { |
| 3487 | status_t status = NO_ERROR; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3488 | ALOGV("start(%d), calling thread %d session %d", |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3489 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3490 | sp<ThreadBase> thread = mThread.promote(); |
| 3491 | if (thread != 0) { |
| 3492 | Mutex::Autolock _l(thread->mLock); |
| 3493 | int state = mState; |
| 3494 | // here the track could be either new, or restarted |
| 3495 | // in both cases "unstop" the track |
| 3496 | if (mState == PAUSED) { |
| 3497 | mState = TrackBase::RESUMING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3498 | ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3499 | } else { |
| 3500 | mState = TrackBase::ACTIVE; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3501 | ALOGV("? => ACTIVE (%d) on thread %p", mName, this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3505 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3506 | status = AudioSystem::startOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3507 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3508 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3509 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3510 | |
| 3511 | // to track the speaker usage |
| 3512 | if (status == NO_ERROR) { |
| 3513 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 3514 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3515 | } |
| 3516 | if (status == NO_ERROR) { |
| 3517 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3518 | playbackThread->addTrack_l(this); |
| 3519 | } else { |
| 3520 | mState = state; |
| 3521 | } |
| 3522 | } else { |
| 3523 | status = BAD_VALUE; |
| 3524 | } |
| 3525 | return status; |
| 3526 | } |
| 3527 | |
| 3528 | void AudioFlinger::PlaybackThread::Track::stop() |
| 3529 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3530 | ALOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3531 | sp<ThreadBase> thread = mThread.promote(); |
| 3532 | if (thread != 0) { |
| 3533 | Mutex::Autolock _l(thread->mLock); |
| 3534 | int state = mState; |
| 3535 | if (mState > STOPPED) { |
| 3536 | mState = STOPPED; |
| 3537 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3538 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3539 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3540 | reset(); |
| 3541 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3542 | ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3543 | } |
| 3544 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3545 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3546 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3547 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3548 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3549 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3550 | |
| 3551 | // to track the speaker usage |
| 3552 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | void AudioFlinger::PlaybackThread::Track::pause() |
| 3558 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3559 | ALOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3560 | sp<ThreadBase> thread = mThread.promote(); |
| 3561 | if (thread != 0) { |
| 3562 | Mutex::Autolock _l(thread->mLock); |
| 3563 | if (mState == ACTIVE || mState == RESUMING) { |
| 3564 | mState = PAUSING; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3565 | ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3566 | if (!isOutputTrack()) { |
| 3567 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3568 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3569 | (audio_stream_type_t)mStreamType, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3570 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3571 | thread->mLock.lock(); |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3572 | |
| 3573 | // to track the speaker usage |
| 3574 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | void AudioFlinger::PlaybackThread::Track::flush() |
| 3581 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3582 | ALOGV("flush(%d)", mName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3583 | sp<ThreadBase> thread = mThread.promote(); |
| 3584 | if (thread != 0) { |
| 3585 | Mutex::Autolock _l(thread->mLock); |
| 3586 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3587 | return; |
| 3588 | } |
| 3589 | // No point remaining in PAUSED state after a flush => go to |
| 3590 | // STOPPED state |
| 3591 | mState = STOPPED; |
| 3592 | |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3593 | // do not reset the track if it is still in the process of being stopped or paused. |
| 3594 | // this will be done by prepareTracks_l() when the track is stopped. |
| 3595 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3596 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3597 | reset(); |
| 3598 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | void AudioFlinger::PlaybackThread::Track::reset() |
| 3603 | { |
| 3604 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3605 | // the audioflinger thread detects the track is stopped. |
| 3606 | if (!mResetDone) { |
| 3607 | TrackBase::reset(); |
| 3608 | // Force underrun condition to avoid false underrun callback until first data is |
| 3609 | // written to buffer |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3610 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 3611 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3612 | mFillingUpStatus = FS_FILLING; |
| 3613 | mResetDone = true; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 3618 | { |
| 3619 | mMute = muted; |
| 3620 | } |
| 3621 | |
| 3622 | void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right) |
| 3623 | { |
| 3624 | mVolume[0] = left; |
| 3625 | mVolume[1] = right; |
| 3626 | } |
| 3627 | |
| 3628 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3629 | { |
| 3630 | status_t status = DEAD_OBJECT; |
| 3631 | sp<ThreadBase> thread = mThread.promote(); |
| 3632 | if (thread != 0) { |
| 3633 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3634 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3635 | } |
| 3636 | return status; |
| 3637 | } |
| 3638 | |
| 3639 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3640 | { |
| 3641 | mAuxEffectId = EffectId; |
| 3642 | mAuxBuffer = buffer; |
| 3643 | } |
| 3644 | |
| 3645 | // ---------------------------------------------------------------------------- |
| 3646 | |
| 3647 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 3648 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 3649 | const wp<ThreadBase>& thread, |
| 3650 | const sp<Client>& client, |
| 3651 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3652 | uint32_t format, |
| 3653 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3654 | int frameCount, |
| 3655 | uint32_t flags, |
| 3656 | int sessionId) |
| 3657 | : TrackBase(thread, client, sampleRate, format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3658 | channelMask, frameCount, flags, 0, sessionId), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3659 | mOverflow(false) |
| 3660 | { |
| 3661 | if (mCblk != NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3662 | ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3663 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3664 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3665 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3666 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3667 | } else { |
| 3668 | mCblk->frameSize = sizeof(int8_t); |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 3674 | { |
| 3675 | sp<ThreadBase> thread = mThread.promote(); |
| 3676 | if (thread != 0) { |
| 3677 | AudioSystem::releaseInput(thread->id()); |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 3682 | { |
| 3683 | audio_track_cblk_t* cblk = this->cblk(); |
| 3684 | uint32_t framesAvail; |
| 3685 | uint32_t framesReq = buffer->frameCount; |
| 3686 | |
| 3687 | // Check if last stepServer failed, try to step now |
| 3688 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3689 | if (!step()) goto getNextBuffer_exit; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3690 | ALOGV("stepServer recovered"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3691 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3692 | } |
| 3693 | |
| 3694 | framesAvail = cblk->framesAvailable_l(); |
| 3695 | |
| 3696 | if (LIKELY(framesAvail)) { |
| 3697 | uint32_t s = cblk->server; |
| 3698 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3699 | |
| 3700 | if (framesReq > framesAvail) { |
| 3701 | framesReq = framesAvail; |
| 3702 | } |
| 3703 | if (s + framesReq > bufferEnd) { |
| 3704 | framesReq = bufferEnd - s; |
| 3705 | } |
| 3706 | |
| 3707 | buffer->raw = getBuffer(s, framesReq); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 3708 | if (buffer->raw == NULL) goto getNextBuffer_exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3709 | |
| 3710 | buffer->frameCount = framesReq; |
| 3711 | return NO_ERROR; |
| 3712 | } |
| 3713 | |
| 3714 | getNextBuffer_exit: |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 3715 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3716 | buffer->frameCount = 0; |
| 3717 | return NOT_ENOUGH_DATA; |
| 3718 | } |
| 3719 | |
| 3720 | status_t AudioFlinger::RecordThread::RecordTrack::start() |
| 3721 | { |
| 3722 | sp<ThreadBase> thread = mThread.promote(); |
| 3723 | if (thread != 0) { |
| 3724 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3725 | return recordThread->start(this); |
| 3726 | } else { |
| 3727 | return BAD_VALUE; |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 3732 | { |
| 3733 | sp<ThreadBase> thread = mThread.promote(); |
| 3734 | if (thread != 0) { |
| 3735 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3736 | recordThread->stop(this); |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3737 | TrackBase::reset(); |
| 3738 | // Force overerrun condition to avoid false overrun callback until first data is |
| 3739 | // read from buffer |
| 3740 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 3745 | { |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3746 | 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] | 3747 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3748 | mFormat, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3749 | mChannelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3750 | mSessionId, |
| 3751 | mFrameCount, |
| 3752 | mState, |
| 3753 | mCblk->sampleRate, |
| 3754 | mCblk->server, |
| 3755 | mCblk->user); |
| 3756 | } |
| 3757 | |
| 3758 | |
| 3759 | // ---------------------------------------------------------------------------- |
| 3760 | |
| 3761 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 3762 | const wp<ThreadBase>& thread, |
| 3763 | DuplicatingThread *sourceThread, |
| 3764 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3765 | uint32_t format, |
| 3766 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3767 | int frameCount) |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3768 | : Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3769 | mActive(false), mSourceThread(sourceThread) |
| 3770 | { |
| 3771 | |
| 3772 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); |
| 3773 | if (mCblk != NULL) { |
| 3774 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 3775 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3776 | mCblk->volume[0] = mCblk->volume[1] = 0x1000; |
| 3777 | mOutBuffer.frameCount = 0; |
| 3778 | playbackThread->mTracks.add(this); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3779 | ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3780 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 3781 | mCblk, mBuffer, mCblk->buffers, |
| 3782 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3783 | } else { |
| 3784 | LOGW("Error creating output track on thread %p", playbackThread); |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 3789 | { |
| 3790 | clearBufferQueue(); |
| 3791 | } |
| 3792 | |
| 3793 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() |
| 3794 | { |
| 3795 | status_t status = Track::start(); |
| 3796 | if (status != NO_ERROR) { |
| 3797 | return status; |
| 3798 | } |
| 3799 | |
| 3800 | mActive = true; |
| 3801 | mRetryCount = 127; |
| 3802 | return status; |
| 3803 | } |
| 3804 | |
| 3805 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 3806 | { |
| 3807 | Track::stop(); |
| 3808 | clearBufferQueue(); |
| 3809 | mOutBuffer.frameCount = 0; |
| 3810 | mActive = false; |
| 3811 | } |
| 3812 | |
| 3813 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 3814 | { |
| 3815 | Buffer *pInBuffer; |
| 3816 | Buffer inBuffer; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3817 | uint32_t channelCount = mChannelCount; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3818 | bool outputBufferFull = false; |
| 3819 | inBuffer.frameCount = frames; |
| 3820 | inBuffer.i16 = data; |
| 3821 | |
| 3822 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 3823 | |
| 3824 | if (!mActive && frames != 0) { |
| 3825 | start(); |
| 3826 | sp<ThreadBase> thread = mThread.promote(); |
| 3827 | if (thread != 0) { |
| 3828 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 3829 | if (mCblk->frameCount > frames){ |
| 3830 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3831 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 3832 | pInBuffer = new Buffer; |
| 3833 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 3834 | pInBuffer->frameCount = startFrames; |
| 3835 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3836 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 3837 | mBufferQueue.add(pInBuffer); |
| 3838 | } else { |
| 3839 | LOGW ("OutputTrack::write() %p no more buffers in queue", this); |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | while (waitTimeLeftMs) { |
| 3846 | // First write pending buffers, then new data |
| 3847 | if (mBufferQueue.size()) { |
| 3848 | pInBuffer = mBufferQueue.itemAt(0); |
| 3849 | } else { |
| 3850 | pInBuffer = &inBuffer; |
| 3851 | } |
| 3852 | |
| 3853 | if (pInBuffer->frameCount == 0) { |
| 3854 | break; |
| 3855 | } |
| 3856 | |
| 3857 | if (mOutBuffer.frameCount == 0) { |
| 3858 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 3859 | nsecs_t startTime = systemTime(); |
| 3860 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3861 | 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] | 3862 | outputBufferFull = true; |
| 3863 | break; |
| 3864 | } |
| 3865 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 3866 | if (waitTimeLeftMs >= waitTimeMs) { |
| 3867 | waitTimeLeftMs -= waitTimeMs; |
| 3868 | } else { |
| 3869 | waitTimeLeftMs = 0; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 3874 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 3875 | mCblk->stepUser(outFrames); |
| 3876 | pInBuffer->frameCount -= outFrames; |
| 3877 | pInBuffer->i16 += outFrames * channelCount; |
| 3878 | mOutBuffer.frameCount -= outFrames; |
| 3879 | mOutBuffer.i16 += outFrames * channelCount; |
| 3880 | |
| 3881 | if (pInBuffer->frameCount == 0) { |
| 3882 | if (mBufferQueue.size()) { |
| 3883 | mBufferQueue.removeAt(0); |
| 3884 | delete [] pInBuffer->mBuffer; |
| 3885 | delete pInBuffer; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3886 | 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] | 3887 | } else { |
| 3888 | break; |
| 3889 | } |
| 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 3894 | if (inBuffer.frameCount) { |
| 3895 | sp<ThreadBase> thread = mThread.promote(); |
| 3896 | if (thread != 0 && !thread->standby()) { |
| 3897 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3898 | pInBuffer = new Buffer; |
| 3899 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 3900 | pInBuffer->frameCount = inBuffer.frameCount; |
| 3901 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3902 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 3903 | mBufferQueue.add(pInBuffer); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3904 | 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] | 3905 | } else { |
| 3906 | LOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 3912 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 3913 | // by output mixer. |
| 3914 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 3915 | if (mCblk->user < mCblk->frameCount) { |
| 3916 | frames = mCblk->frameCount - mCblk->user; |
| 3917 | pInBuffer = new Buffer; |
| 3918 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 3919 | pInBuffer->frameCount = frames; |
| 3920 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3921 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 3922 | mBufferQueue.add(pInBuffer); |
| 3923 | } else if (mActive) { |
| 3924 | stop(); |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | return outputBufferFull; |
| 3929 | } |
| 3930 | |
| 3931 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 3932 | { |
| 3933 | int active; |
| 3934 | status_t result; |
| 3935 | audio_track_cblk_t* cblk = mCblk; |
| 3936 | uint32_t framesReq = buffer->frameCount; |
| 3937 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3938 | // ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3939 | buffer->frameCount = 0; |
| 3940 | |
| 3941 | uint32_t framesAvail = cblk->framesAvailable(); |
| 3942 | |
| 3943 | |
| 3944 | if (framesAvail == 0) { |
| 3945 | Mutex::Autolock _l(cblk->lock); |
| 3946 | goto start_loop_here; |
| 3947 | while (framesAvail == 0) { |
| 3948 | active = mActive; |
| 3949 | if (UNLIKELY(!active)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3950 | ALOGV("Not active and NO_MORE_BUFFERS"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3951 | return AudioTrack::NO_MORE_BUFFERS; |
| 3952 | } |
| 3953 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 3954 | if (result != NO_ERROR) { |
| 3955 | return AudioTrack::NO_MORE_BUFFERS; |
| 3956 | } |
| 3957 | // read the server count again |
| 3958 | start_loop_here: |
| 3959 | framesAvail = cblk->framesAvailable_l(); |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | // if (framesAvail < framesReq) { |
| 3964 | // return AudioTrack::NO_MORE_BUFFERS; |
| 3965 | // } |
| 3966 | |
| 3967 | if (framesReq > framesAvail) { |
| 3968 | framesReq = framesAvail; |
| 3969 | } |
| 3970 | |
| 3971 | uint32_t u = cblk->user; |
| 3972 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 3973 | |
| 3974 | if (u + framesReq > bufferEnd) { |
| 3975 | framesReq = bufferEnd - u; |
| 3976 | } |
| 3977 | |
| 3978 | buffer->frameCount = framesReq; |
| 3979 | buffer->raw = (void *)cblk->buffer(u); |
| 3980 | return NO_ERROR; |
| 3981 | } |
| 3982 | |
| 3983 | |
| 3984 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 3985 | { |
| 3986 | size_t size = mBufferQueue.size(); |
| 3987 | Buffer *pBuffer; |
| 3988 | |
| 3989 | for (size_t i = 0; i < size; i++) { |
| 3990 | pBuffer = mBufferQueue.itemAt(i); |
| 3991 | delete [] pBuffer->mBuffer; |
| 3992 | delete pBuffer; |
| 3993 | } |
| 3994 | mBufferQueue.clear(); |
| 3995 | } |
| 3996 | |
| 3997 | // ---------------------------------------------------------------------------- |
| 3998 | |
| 3999 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 4000 | : RefBase(), |
| 4001 | mAudioFlinger(audioFlinger), |
| 4002 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
| 4003 | mPid(pid) |
| 4004 | { |
| 4005 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 4006 | } |
| 4007 | |
| 4008 | // Client destructor must be called with AudioFlinger::mLock held |
| 4009 | AudioFlinger::Client::~Client() |
| 4010 | { |
| 4011 | mAudioFlinger->removeClient_l(mPid); |
| 4012 | } |
| 4013 | |
| 4014 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const |
| 4015 | { |
| 4016 | return mMemoryDealer; |
| 4017 | } |
| 4018 | |
| 4019 | // ---------------------------------------------------------------------------- |
| 4020 | |
| 4021 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 4022 | const sp<IAudioFlingerClient>& client, |
| 4023 | pid_t pid) |
| 4024 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) |
| 4025 | { |
| 4026 | } |
| 4027 | |
| 4028 | AudioFlinger::NotificationClient::~NotificationClient() |
| 4029 | { |
| 4030 | mClient.clear(); |
| 4031 | } |
| 4032 | |
| 4033 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 4034 | { |
| 4035 | sp<NotificationClient> keep(this); |
| 4036 | { |
| 4037 | mAudioFlinger->removeNotificationClient(mPid); |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | // ---------------------------------------------------------------------------- |
| 4042 | |
| 4043 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 4044 | : BnAudioTrack(), |
| 4045 | mTrack(track) |
| 4046 | { |
| 4047 | } |
| 4048 | |
| 4049 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 4050 | // just stop the track on deletion, associated resources |
| 4051 | // will be freed from the main thread once all pending buffers have |
| 4052 | // been played. Unless it's not in the active track list, in which |
| 4053 | // case we free everything now... |
| 4054 | mTrack->destroy(); |
| 4055 | } |
| 4056 | |
| 4057 | status_t AudioFlinger::TrackHandle::start() { |
| 4058 | return mTrack->start(); |
| 4059 | } |
| 4060 | |
| 4061 | void AudioFlinger::TrackHandle::stop() { |
| 4062 | mTrack->stop(); |
| 4063 | } |
| 4064 | |
| 4065 | void AudioFlinger::TrackHandle::flush() { |
| 4066 | mTrack->flush(); |
| 4067 | } |
| 4068 | |
| 4069 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 4070 | mTrack->mute(e); |
| 4071 | } |
| 4072 | |
| 4073 | void AudioFlinger::TrackHandle::pause() { |
| 4074 | mTrack->pause(); |
| 4075 | } |
| 4076 | |
| 4077 | void AudioFlinger::TrackHandle::setVolume(float left, float right) { |
| 4078 | mTrack->setVolume(left, right); |
| 4079 | } |
| 4080 | |
| 4081 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 4082 | return mTrack->getCblk(); |
| 4083 | } |
| 4084 | |
| 4085 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 4086 | { |
| 4087 | return mTrack->attachAuxEffect(EffectId); |
| 4088 | } |
| 4089 | |
| 4090 | status_t AudioFlinger::TrackHandle::onTransact( |
| 4091 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4092 | { |
| 4093 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 4094 | } |
| 4095 | |
| 4096 | // ---------------------------------------------------------------------------- |
| 4097 | |
| 4098 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 4099 | pid_t pid, |
| 4100 | int input, |
| 4101 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4102 | uint32_t format, |
| 4103 | uint32_t channelMask, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4104 | int frameCount, |
| 4105 | uint32_t flags, |
| 4106 | int *sessionId, |
| 4107 | status_t *status) |
| 4108 | { |
| 4109 | sp<RecordThread::RecordTrack> recordTrack; |
| 4110 | sp<RecordHandle> recordHandle; |
| 4111 | sp<Client> client; |
| 4112 | wp<Client> wclient; |
| 4113 | status_t lStatus; |
| 4114 | RecordThread *thread; |
| 4115 | size_t inFrameCount; |
| 4116 | int lSessionId; |
| 4117 | |
| 4118 | // check calling permissions |
| 4119 | if (!recordingAllowed()) { |
| 4120 | lStatus = PERMISSION_DENIED; |
| 4121 | goto Exit; |
| 4122 | } |
| 4123 | |
| 4124 | // add client to list |
| 4125 | { // scope for mLock |
| 4126 | Mutex::Autolock _l(mLock); |
| 4127 | thread = checkRecordThread_l(input); |
| 4128 | if (thread == NULL) { |
| 4129 | lStatus = BAD_VALUE; |
| 4130 | goto Exit; |
| 4131 | } |
| 4132 | |
| 4133 | wclient = mClients.valueFor(pid); |
| 4134 | if (wclient != NULL) { |
| 4135 | client = wclient.promote(); |
| 4136 | } else { |
| 4137 | client = new Client(this, pid); |
| 4138 | mClients.add(pid, client); |
| 4139 | } |
| 4140 | |
| 4141 | // If no audio session id is provided, create one here |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4142 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4143 | lSessionId = *sessionId; |
| 4144 | } else { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4145 | lSessionId = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4146 | if (sessionId != NULL) { |
| 4147 | *sessionId = lSessionId; |
| 4148 | } |
| 4149 | } |
| 4150 | // 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] | 4151 | recordTrack = thread->createRecordTrack_l(client, |
| 4152 | sampleRate, |
| 4153 | format, |
| 4154 | channelMask, |
| 4155 | frameCount, |
| 4156 | flags, |
| 4157 | lSessionId, |
| 4158 | &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4159 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4160 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4161 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 4162 | // destructor is called by the TrackBase destructor with mLock held |
| 4163 | client.clear(); |
| 4164 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4165 | goto Exit; |
| 4166 | } |
| 4167 | |
| 4168 | // return to handle to client |
| 4169 | recordHandle = new RecordHandle(recordTrack); |
| 4170 | lStatus = NO_ERROR; |
| 4171 | |
| 4172 | Exit: |
| 4173 | if (status) { |
| 4174 | *status = lStatus; |
| 4175 | } |
| 4176 | return recordHandle; |
| 4177 | } |
| 4178 | |
| 4179 | // ---------------------------------------------------------------------------- |
| 4180 | |
| 4181 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 4182 | : BnAudioRecord(), |
| 4183 | mRecordTrack(recordTrack) |
| 4184 | { |
| 4185 | } |
| 4186 | |
| 4187 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 4188 | stop(); |
| 4189 | } |
| 4190 | |
| 4191 | status_t AudioFlinger::RecordHandle::start() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4192 | ALOGV("RecordHandle::start()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4193 | return mRecordTrack->start(); |
| 4194 | } |
| 4195 | |
| 4196 | void AudioFlinger::RecordHandle::stop() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4197 | ALOGV("RecordHandle::stop()"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4198 | mRecordTrack->stop(); |
| 4199 | } |
| 4200 | |
| 4201 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 4202 | return mRecordTrack->getCblk(); |
| 4203 | } |
| 4204 | |
| 4205 | status_t AudioFlinger::RecordHandle::onTransact( |
| 4206 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4207 | { |
| 4208 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 4209 | } |
| 4210 | |
| 4211 | // ---------------------------------------------------------------------------- |
| 4212 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4213 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4214 | AudioStreamIn *input, |
| 4215 | uint32_t sampleRate, |
| 4216 | uint32_t channels, |
| 4217 | int id, |
| 4218 | uint32_t device) : |
| 4219 | ThreadBase(audioFlinger, id, device), |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4220 | mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4221 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4222 | mType = ThreadBase::RECORD; |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4223 | |
| 4224 | snprintf(mName, kNameLength, "AudioIn_%d", id); |
| 4225 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4226 | mReqChannelCount = popcount(channels); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4227 | mReqSampleRate = sampleRate; |
| 4228 | readInputParameters(); |
| 4229 | } |
| 4230 | |
| 4231 | |
| 4232 | AudioFlinger::RecordThread::~RecordThread() |
| 4233 | { |
| 4234 | delete[] mRsmpInBuffer; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4235 | if (mResampler != NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4236 | delete mResampler; |
| 4237 | delete[] mRsmpOutBuffer; |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | void AudioFlinger::RecordThread::onFirstRef() |
| 4242 | { |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4243 | run(mName, PRIORITY_URGENT_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4246 | status_t AudioFlinger::RecordThread::readyToRun() |
| 4247 | { |
| 4248 | status_t status = initCheck(); |
| 4249 | LOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
| 4250 | return status; |
| 4251 | } |
| 4252 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4253 | bool AudioFlinger::RecordThread::threadLoop() |
| 4254 | { |
| 4255 | AudioBufferProvider::Buffer buffer; |
| 4256 | sp<RecordTrack> activeTrack; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4257 | Vector< sp<EffectChain> > effectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4258 | |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4259 | nsecs_t lastWarning = 0; |
| 4260 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4261 | acquireWakeLock(); |
| 4262 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4263 | // start recording |
| 4264 | while (!exitPending()) { |
| 4265 | |
| 4266 | processConfigEvents(); |
| 4267 | |
| 4268 | { // scope for mLock |
| 4269 | Mutex::Autolock _l(mLock); |
| 4270 | checkForNewParameters_l(); |
| 4271 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 4272 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4273 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4274 | mStandby = true; |
| 4275 | } |
| 4276 | |
| 4277 | if (exitPending()) break; |
| 4278 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4279 | releaseWakeLock_l(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4280 | ALOGV("RecordThread: loop stopping"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4281 | // go to sleep |
| 4282 | mWaitWorkCV.wait(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4283 | ALOGV("RecordThread: loop starting"); |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4284 | acquireWakeLock_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4285 | continue; |
| 4286 | } |
| 4287 | if (mActiveTrack != 0) { |
| 4288 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 4289 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4290 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4291 | mStandby = true; |
| 4292 | } |
| 4293 | mActiveTrack.clear(); |
| 4294 | mStartStopCond.broadcast(); |
| 4295 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 4296 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 4297 | mActiveTrack.clear(); |
| 4298 | mStartStopCond.broadcast(); |
| 4299 | } else if (mBytesRead != 0) { |
| 4300 | // record start succeeds only if first read from audio input |
| 4301 | // succeeds |
| 4302 | if (mBytesRead > 0) { |
| 4303 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4304 | } else { |
| 4305 | mActiveTrack.clear(); |
| 4306 | } |
| 4307 | mStartStopCond.broadcast(); |
| 4308 | } |
| 4309 | mStandby = false; |
| 4310 | } |
| 4311 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4312 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | if (mActiveTrack != 0) { |
| 4316 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 4317 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4318 | unlockEffectChains(effectChains); |
| 4319 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4320 | continue; |
| 4321 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4322 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4323 | effectChains[i]->process_l(); |
| 4324 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4325 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4326 | buffer.frameCount = mFrameCount; |
| 4327 | if (LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
| 4328 | size_t framesOut = buffer.frameCount; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4329 | if (mResampler == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4330 | // no resampling |
| 4331 | while (framesOut) { |
| 4332 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 4333 | if (framesIn) { |
| 4334 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 4335 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 4336 | if (framesIn > framesOut) |
| 4337 | framesIn = framesOut; |
| 4338 | mRsmpInIndex += framesIn; |
| 4339 | framesOut -= framesIn; |
| 4340 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4341 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4342 | memcpy(dst, src, framesIn * mFrameSize); |
| 4343 | } else { |
| 4344 | int16_t *src16 = (int16_t *)src; |
| 4345 | int16_t *dst16 = (int16_t *)dst; |
| 4346 | if (mChannelCount == 1) { |
| 4347 | while (framesIn--) { |
| 4348 | *dst16++ = *src16; |
| 4349 | *dst16++ = *src16++; |
| 4350 | } |
| 4351 | } else { |
| 4352 | while (framesIn--) { |
| 4353 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 4354 | src16 += 2; |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 4360 | if (framesOut == mFrameCount && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4361 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4362 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4363 | framesOut = 0; |
| 4364 | } else { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4365 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4366 | mRsmpInIndex = 0; |
| 4367 | } |
| 4368 | if (mBytesRead < 0) { |
| 4369 | LOGE("Error reading audio input"); |
| 4370 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4371 | // Force input into standby so that it tries to |
| 4372 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4373 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4374 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4375 | } |
| 4376 | mRsmpInIndex = mFrameCount; |
| 4377 | framesOut = 0; |
| 4378 | buffer.frameCount = 0; |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | } else { |
| 4383 | // resampling |
| 4384 | |
| 4385 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 4386 | // alter output frame count as if we were expecting stereo samples |
| 4387 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4388 | framesOut >>= 1; |
| 4389 | } |
| 4390 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 4391 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 4392 | // are 32 bit aligned which should be always true. |
| 4393 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
| 4394 | AudioMixer::ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
| 4395 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 4396 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 4397 | int16_t *dst = buffer.i16; |
| 4398 | while (framesOut--) { |
| 4399 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 4400 | src += 2; |
| 4401 | } |
| 4402 | } else { |
| 4403 | AudioMixer::ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
| 4404 | } |
| 4405 | |
| 4406 | } |
| 4407 | mActiveTrack->releaseBuffer(&buffer); |
| 4408 | mActiveTrack->overflow(); |
| 4409 | } |
| 4410 | // client isn't retrieving buffers fast enough |
| 4411 | else { |
Eric Laurent | 44d9848 | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4412 | if (!mActiveTrack->setOverflow()) { |
| 4413 | nsecs_t now = systemTime(); |
| 4414 | if ((now - lastWarning) > kWarningThrottle) { |
| 4415 | LOGW("RecordThread: buffer overflow"); |
| 4416 | lastWarning = now; |
| 4417 | } |
| 4418 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4419 | // Release the processor for a while before asking for a new buffer. |
| 4420 | // This will give the application more chance to read from the buffer and |
| 4421 | // clear the overflow. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4422 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4423 | } |
| 4424 | } |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4425 | // enable changes in effect chain |
| 4426 | unlockEffectChains(effectChains); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4427 | effectChains.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | if (!mStandby) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4431 | mInput->stream->common.standby(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4432 | } |
| 4433 | mActiveTrack.clear(); |
| 4434 | |
| 4435 | mStartStopCond.broadcast(); |
| 4436 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4437 | releaseWakeLock(); |
| 4438 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4439 | ALOGV("RecordThread %p exiting", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4440 | return false; |
| 4441 | } |
| 4442 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4443 | |
| 4444 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 4445 | const sp<AudioFlinger::Client>& client, |
| 4446 | uint32_t sampleRate, |
| 4447 | int format, |
| 4448 | int channelMask, |
| 4449 | int frameCount, |
| 4450 | uint32_t flags, |
| 4451 | int sessionId, |
| 4452 | status_t *status) |
| 4453 | { |
| 4454 | sp<RecordTrack> track; |
| 4455 | status_t lStatus; |
| 4456 | |
| 4457 | lStatus = initCheck(); |
| 4458 | if (lStatus != NO_ERROR) { |
| 4459 | LOGE("Audio driver not initialized."); |
| 4460 | goto Exit; |
| 4461 | } |
| 4462 | |
| 4463 | { // scope for mLock |
| 4464 | Mutex::Autolock _l(mLock); |
| 4465 | |
| 4466 | track = new RecordTrack(this, client, sampleRate, |
| 4467 | format, channelMask, frameCount, flags, sessionId); |
| 4468 | |
| 4469 | if (track->getCblk() == NULL) { |
| 4470 | lStatus = NO_MEMORY; |
| 4471 | goto Exit; |
| 4472 | } |
| 4473 | |
| 4474 | mTrack = track.get(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4475 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4476 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4477 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4478 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 4479 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4480 | } |
| 4481 | lStatus = NO_ERROR; |
| 4482 | |
| 4483 | Exit: |
| 4484 | if (status) { |
| 4485 | *status = lStatus; |
| 4486 | } |
| 4487 | return track; |
| 4488 | } |
| 4489 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4490 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) |
| 4491 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4492 | ALOGV("RecordThread::start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4493 | sp <ThreadBase> strongMe = this; |
| 4494 | status_t status = NO_ERROR; |
| 4495 | { |
| 4496 | AutoMutex lock(&mLock); |
| 4497 | if (mActiveTrack != 0) { |
| 4498 | if (recordTrack != mActiveTrack.get()) { |
| 4499 | status = -EBUSY; |
| 4500 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 4501 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4502 | } |
| 4503 | return status; |
| 4504 | } |
| 4505 | |
| 4506 | recordTrack->mState = TrackBase::IDLE; |
| 4507 | mActiveTrack = recordTrack; |
| 4508 | mLock.unlock(); |
| 4509 | status_t status = AudioSystem::startInput(mId); |
| 4510 | mLock.lock(); |
| 4511 | if (status != NO_ERROR) { |
| 4512 | mActiveTrack.clear(); |
| 4513 | return status; |
| 4514 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4515 | mRsmpInIndex = mFrameCount; |
| 4516 | mBytesRead = 0; |
Eric Laurent | 243f5f9 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4517 | if (mResampler != NULL) { |
| 4518 | mResampler->reset(); |
| 4519 | } |
| 4520 | mActiveTrack->mState = TrackBase::RESUMING; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4521 | // signal thread to start |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4522 | ALOGV("Signal record thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4523 | mWaitWorkCV.signal(); |
| 4524 | // do not wait for mStartStopCond if exiting |
| 4525 | if (mExiting) { |
| 4526 | mActiveTrack.clear(); |
| 4527 | status = INVALID_OPERATION; |
| 4528 | goto startError; |
| 4529 | } |
| 4530 | mStartStopCond.wait(mLock); |
| 4531 | if (mActiveTrack == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4532 | ALOGV("Record failed to start"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4533 | status = BAD_VALUE; |
| 4534 | goto startError; |
| 4535 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4536 | ALOGV("Record started OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4537 | return status; |
| 4538 | } |
| 4539 | startError: |
| 4540 | AudioSystem::stopInput(mId); |
| 4541 | return status; |
| 4542 | } |
| 4543 | |
| 4544 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4545 | ALOGV("RecordThread::stop"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4546 | sp <ThreadBase> strongMe = this; |
| 4547 | { |
| 4548 | AutoMutex lock(&mLock); |
| 4549 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 4550 | mActiveTrack->mState = TrackBase::PAUSING; |
| 4551 | // do not wait for mStartStopCond if exiting |
| 4552 | if (mExiting) { |
| 4553 | return; |
| 4554 | } |
| 4555 | mStartStopCond.wait(mLock); |
| 4556 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 4557 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 4558 | mLock.unlock(); |
| 4559 | AudioSystem::stopInput(mId); |
| 4560 | mLock.lock(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4561 | ALOGV("Record stopped OK"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4562 | } |
| 4563 | } |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 4568 | { |
| 4569 | const size_t SIZE = 256; |
| 4570 | char buffer[SIZE]; |
| 4571 | String8 result; |
| 4572 | pid_t pid = 0; |
| 4573 | |
| 4574 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 4575 | result.append(buffer); |
| 4576 | |
| 4577 | if (mActiveTrack != 0) { |
| 4578 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4579 | 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] | 4580 | mActiveTrack->dump(buffer, SIZE); |
| 4581 | result.append(buffer); |
| 4582 | |
| 4583 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 4584 | result.append(buffer); |
| 4585 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 4586 | result.append(buffer); |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4587 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4588 | result.append(buffer); |
| 4589 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 4590 | result.append(buffer); |
| 4591 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 4592 | result.append(buffer); |
| 4593 | |
| 4594 | |
| 4595 | } else { |
| 4596 | result.append("No record client\n"); |
| 4597 | } |
| 4598 | write(fd, result.string(), result.size()); |
| 4599 | |
| 4600 | dumpBase(fd, args); |
Eric Laurent | 1d2bff0 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4601 | dumpEffectChains(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4602 | |
| 4603 | return NO_ERROR; |
| 4604 | } |
| 4605 | |
| 4606 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 4607 | { |
| 4608 | size_t framesReq = buffer->frameCount; |
| 4609 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 4610 | int channelCount; |
| 4611 | |
| 4612 | if (framesReady == 0) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4613 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4614 | if (mBytesRead < 0) { |
| 4615 | LOGE("RecordThread::getNextBuffer() Error reading audio input"); |
| 4616 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4617 | // Force input into standby so that it tries to |
| 4618 | // recover at next read attempt |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4619 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4620 | usleep(kRecordThreadSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4621 | } |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4622 | buffer->raw = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4623 | buffer->frameCount = 0; |
| 4624 | return NOT_ENOUGH_DATA; |
| 4625 | } |
| 4626 | mRsmpInIndex = 0; |
| 4627 | framesReady = mFrameCount; |
| 4628 | } |
| 4629 | |
| 4630 | if (framesReq > framesReady) { |
| 4631 | framesReq = framesReady; |
| 4632 | } |
| 4633 | |
| 4634 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4635 | channelCount = 1; |
| 4636 | } else { |
| 4637 | channelCount = 2; |
| 4638 | } |
| 4639 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 4640 | buffer->frameCount = framesReq; |
| 4641 | return NO_ERROR; |
| 4642 | } |
| 4643 | |
| 4644 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4645 | { |
| 4646 | mRsmpInIndex += buffer->frameCount; |
| 4647 | buffer->frameCount = 0; |
| 4648 | } |
| 4649 | |
| 4650 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 4651 | { |
| 4652 | bool reconfig = false; |
| 4653 | |
| 4654 | while (!mNewParameters.isEmpty()) { |
| 4655 | status_t status = NO_ERROR; |
| 4656 | String8 keyValuePair = mNewParameters[0]; |
| 4657 | AudioParameter param = AudioParameter(keyValuePair); |
| 4658 | int value; |
| 4659 | int reqFormat = mFormat; |
| 4660 | int reqSamplingRate = mReqSampleRate; |
| 4661 | int reqChannelCount = mReqChannelCount; |
| 4662 | |
| 4663 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4664 | reqSamplingRate = value; |
| 4665 | reconfig = true; |
| 4666 | } |
| 4667 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 4668 | reqFormat = value; |
| 4669 | reconfig = true; |
| 4670 | } |
| 4671 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4672 | reqChannelCount = popcount(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4673 | reconfig = true; |
| 4674 | } |
| 4675 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4676 | // do not accept frame count changes if tracks are open as the track buffer |
| 4677 | // size depends on frame count and correct behavior would not be garantied |
| 4678 | // if frame count is changed after track creation |
| 4679 | if (mActiveTrack != 0) { |
| 4680 | status = INVALID_OPERATION; |
| 4681 | } else { |
| 4682 | reconfig = true; |
| 4683 | } |
| 4684 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4685 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4686 | // forward device change to effects that have requested to be |
| 4687 | // aware of attached audio device. |
| 4688 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4689 | mEffectChains[i]->setDevice_l(value); |
| 4690 | } |
| 4691 | // store input device and output device but do not forward output device to audio HAL. |
| 4692 | // Note that status is ignored by the caller for output device |
| 4693 | // (see AudioFlinger::setParameters() |
| 4694 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 4695 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 4696 | status = BAD_VALUE; |
| 4697 | } else { |
| 4698 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4699 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4700 | if (mTrack != NULL) { |
| 4701 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4702 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4703 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 4704 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 4705 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4706 | } |
| 4707 | mDevice |= (uint32_t)value; |
| 4708 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4709 | if (status == NO_ERROR) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4710 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4711 | if (status == INVALID_OPERATION) { |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4712 | mInput->stream->common.standby(&mInput->stream->common); |
| 4713 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4714 | } |
| 4715 | if (reconfig) { |
| 4716 | if (status == BAD_VALUE && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4717 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4718 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4719 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
| 4720 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4721 | (reqChannelCount < 3)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4722 | status = NO_ERROR; |
| 4723 | } |
| 4724 | if (status == NO_ERROR) { |
| 4725 | readInputParameters(); |
| 4726 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 4727 | } |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | mNewParameters.removeAt(0); |
| 4732 | |
| 4733 | mParamStatus = status; |
| 4734 | mParamCond.signal(); |
Eric Laurent | 60cd0a0 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4735 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 4736 | // already timed out waiting for the status and will never signal the condition. |
| 4737 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4738 | } |
| 4739 | return reconfig; |
| 4740 | } |
| 4741 | |
| 4742 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 4743 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4744 | char *s; |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4745 | String8 out_s8 = String8(); |
| 4746 | |
| 4747 | Mutex::Autolock _l(mLock); |
| 4748 | if (initCheck() != NO_ERROR) { |
| 4749 | return out_s8; |
| 4750 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4751 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4752 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4753 | out_s8 = String8(s); |
| 4754 | free(s); |
| 4755 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 4759 | AudioSystem::OutputDescriptor desc; |
| 4760 | void *param2 = 0; |
| 4761 | |
| 4762 | switch (event) { |
| 4763 | case AudioSystem::INPUT_OPENED: |
| 4764 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4765 | desc.channels = mChannelMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4766 | desc.samplingRate = mSampleRate; |
| 4767 | desc.format = mFormat; |
| 4768 | desc.frameCount = mFrameCount; |
| 4769 | desc.latency = 0; |
| 4770 | param2 = &desc; |
| 4771 | break; |
| 4772 | |
| 4773 | case AudioSystem::INPUT_CLOSED: |
| 4774 | default: |
| 4775 | break; |
| 4776 | } |
| 4777 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 4778 | } |
| 4779 | |
| 4780 | void AudioFlinger::RecordThread::readInputParameters() |
| 4781 | { |
| 4782 | if (mRsmpInBuffer) delete mRsmpInBuffer; |
| 4783 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; |
| 4784 | if (mResampler) delete mResampler; |
Glenn Kasten | e0feee3 | 2011-12-13 11:53:26 -0800 | [diff] [blame^] | 4785 | mResampler = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4786 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4787 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4788 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 4789 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4790 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 4791 | mFrameSize = (uint16_t)audio_stream_frame_size(&mInput->stream->common); |
| 4792 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4793 | mFrameCount = mInputBytes / mFrameSize; |
| 4794 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 4795 | |
| 4796 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 4797 | { |
| 4798 | int channelCount; |
| 4799 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 4800 | // stereo to mono post process as the resampler always outputs stereo. |
| 4801 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4802 | channelCount = 1; |
| 4803 | } else { |
| 4804 | channelCount = 2; |
| 4805 | } |
| 4806 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 4807 | mResampler->setSampleRate(mSampleRate); |
| 4808 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 4809 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 4810 | |
| 4811 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 4812 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4813 | mFrameCount >>= 1; |
| 4814 | } |
| 4815 | |
| 4816 | } |
| 4817 | mRsmpInIndex = mFrameCount; |
| 4818 | } |
| 4819 | |
| 4820 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 4821 | { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4822 | Mutex::Autolock _l(mLock); |
| 4823 | if (initCheck() != NO_ERROR) { |
| 4824 | return 0; |
| 4825 | } |
| 4826 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4827 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4828 | } |
| 4829 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4830 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 4831 | { |
| 4832 | Mutex::Autolock _l(mLock); |
| 4833 | uint32_t result = 0; |
| 4834 | if (getEffectChain_l(sessionId) != 0) { |
| 4835 | result = EFFECT_SESSION; |
| 4836 | } |
| 4837 | |
| 4838 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 4839 | result |= TRACK_SESSION; |
| 4840 | } |
| 4841 | |
| 4842 | return result; |
| 4843 | } |
| 4844 | |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4845 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 4846 | { |
| 4847 | Mutex::Autolock _l(mLock); |
| 4848 | return mTrack; |
| 4849 | } |
| 4850 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4851 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() |
| 4852 | { |
| 4853 | Mutex::Autolock _l(mLock); |
| 4854 | return mInput; |
| 4855 | } |
| 4856 | |
| 4857 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 4858 | { |
| 4859 | Mutex::Autolock _l(mLock); |
| 4860 | AudioStreamIn *input = mInput; |
| 4861 | mInput = NULL; |
| 4862 | return input; |
| 4863 | } |
| 4864 | |
| 4865 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 4866 | audio_stream_t* AudioFlinger::RecordThread::stream() |
| 4867 | { |
| 4868 | if (mInput == NULL) { |
| 4869 | return NULL; |
| 4870 | } |
| 4871 | return &mInput->stream->common; |
| 4872 | } |
| 4873 | |
| 4874 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4875 | // ---------------------------------------------------------------------------- |
| 4876 | |
| 4877 | int AudioFlinger::openOutput(uint32_t *pDevices, |
| 4878 | uint32_t *pSamplingRate, |
| 4879 | uint32_t *pFormat, |
| 4880 | uint32_t *pChannels, |
| 4881 | uint32_t *pLatencyMs, |
| 4882 | uint32_t flags) |
| 4883 | { |
| 4884 | status_t status; |
| 4885 | PlaybackThread *thread = NULL; |
| 4886 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 4887 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 4888 | uint32_t format = pFormat ? *pFormat : 0; |
| 4889 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4890 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4891 | audio_stream_out_t *outStream; |
| 4892 | audio_hw_device_t *outHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4893 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4894 | ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4895 | pDevices ? *pDevices : 0, |
| 4896 | samplingRate, |
| 4897 | format, |
| 4898 | channels, |
| 4899 | flags); |
| 4900 | |
| 4901 | if (pDevices == NULL || *pDevices == 0) { |
| 4902 | return 0; |
| 4903 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4904 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4905 | Mutex::Autolock _l(mLock); |
| 4906 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4907 | outHwDev = findSuitableHwDev_l(*pDevices); |
| 4908 | if (outHwDev == NULL) |
| 4909 | return 0; |
| 4910 | |
| 4911 | status = outHwDev->open_output_stream(outHwDev, *pDevices, (int *)&format, |
| 4912 | &channels, &samplingRate, &outStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4913 | 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] | 4914 | outStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4915 | samplingRate, |
| 4916 | format, |
| 4917 | channels, |
| 4918 | status); |
| 4919 | |
| 4920 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4921 | if (outStream != NULL) { |
| 4922 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4923 | int id = nextUniqueId(); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4924 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4925 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || |
| 4926 | (format != AUDIO_FORMAT_PCM_16_BIT) || |
| 4927 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4928 | thread = new DirectOutputThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4929 | ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4930 | } else { |
| 4931 | thread = new MixerThread(this, output, id, *pDevices); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4932 | ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4933 | } |
| 4934 | mPlaybackThreads.add(id, thread); |
| 4935 | |
| 4936 | if (pSamplingRate) *pSamplingRate = samplingRate; |
| 4937 | if (pFormat) *pFormat = format; |
| 4938 | if (pChannels) *pChannels = channels; |
| 4939 | if (pLatencyMs) *pLatencyMs = thread->latency(); |
| 4940 | |
| 4941 | // notify client processes of the new output creation |
| 4942 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 4943 | return id; |
| 4944 | } |
| 4945 | |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
| 4949 | int AudioFlinger::openDuplicateOutput(int output1, int output2) |
| 4950 | { |
| 4951 | Mutex::Autolock _l(mLock); |
| 4952 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 4953 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 4954 | |
| 4955 | if (thread1 == NULL || thread2 == NULL) { |
| 4956 | LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
| 4957 | return 0; |
| 4958 | } |
| 4959 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4960 | int id = nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4961 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 4962 | thread->addOutputTrack(thread2); |
| 4963 | mPlaybackThreads.add(id, thread); |
| 4964 | // notify client processes of the new output creation |
| 4965 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 4966 | return id; |
| 4967 | } |
| 4968 | |
| 4969 | status_t AudioFlinger::closeOutput(int output) |
| 4970 | { |
| 4971 | // keep strong reference on the playback thread so that |
| 4972 | // it is not destroyed while exit() is executed |
| 4973 | sp <PlaybackThread> thread; |
| 4974 | { |
| 4975 | Mutex::Autolock _l(mLock); |
| 4976 | thread = checkPlaybackThread_l(output); |
| 4977 | if (thread == NULL) { |
| 4978 | return BAD_VALUE; |
| 4979 | } |
| 4980 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 4981 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4982 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4983 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4984 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4985 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4986 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 4987 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 4988 | } |
| 4989 | } |
| 4990 | } |
| 4991 | void *param2 = 0; |
| 4992 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); |
| 4993 | mPlaybackThreads.removeItem(output); |
| 4994 | } |
| 4995 | thread->exit(); |
| 4996 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4997 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4998 | AudioStreamOut *out = thread->clearOutput(); |
| 4999 | // from now on thread->mOutput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5000 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 5001 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5002 | } |
| 5003 | return NO_ERROR; |
| 5004 | } |
| 5005 | |
| 5006 | status_t AudioFlinger::suspendOutput(int output) |
| 5007 | { |
| 5008 | Mutex::Autolock _l(mLock); |
| 5009 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5010 | |
| 5011 | if (thread == NULL) { |
| 5012 | return BAD_VALUE; |
| 5013 | } |
| 5014 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5015 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5016 | thread->suspend(); |
| 5017 | |
| 5018 | return NO_ERROR; |
| 5019 | } |
| 5020 | |
| 5021 | status_t AudioFlinger::restoreOutput(int output) |
| 5022 | { |
| 5023 | Mutex::Autolock _l(mLock); |
| 5024 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5025 | |
| 5026 | if (thread == NULL) { |
| 5027 | return BAD_VALUE; |
| 5028 | } |
| 5029 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5030 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5031 | |
| 5032 | thread->restore(); |
| 5033 | |
| 5034 | return NO_ERROR; |
| 5035 | } |
| 5036 | |
| 5037 | int AudioFlinger::openInput(uint32_t *pDevices, |
| 5038 | uint32_t *pSamplingRate, |
| 5039 | uint32_t *pFormat, |
| 5040 | uint32_t *pChannels, |
| 5041 | uint32_t acoustics) |
| 5042 | { |
| 5043 | status_t status; |
| 5044 | RecordThread *thread = NULL; |
| 5045 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 5046 | uint32_t format = pFormat ? *pFormat : 0; |
| 5047 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5048 | uint32_t reqSamplingRate = samplingRate; |
| 5049 | uint32_t reqFormat = format; |
| 5050 | uint32_t reqChannels = channels; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5051 | audio_stream_in_t *inStream; |
| 5052 | audio_hw_device_t *inHwDev; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5053 | |
| 5054 | if (pDevices == NULL || *pDevices == 0) { |
| 5055 | return 0; |
| 5056 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5057 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5058 | Mutex::Autolock _l(mLock); |
| 5059 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5060 | inHwDev = findSuitableHwDev_l(*pDevices); |
| 5061 | if (inHwDev == NULL) |
| 5062 | return 0; |
| 5063 | |
| 5064 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, |
| 5065 | &channels, &samplingRate, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5066 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5067 | &inStream); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5068 | 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] | 5069 | inStream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5070 | samplingRate, |
| 5071 | format, |
| 5072 | channels, |
| 5073 | acoustics, |
| 5074 | status); |
| 5075 | |
| 5076 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 5077 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 5078 | // or stereo to mono conversions on 16 bit PCM inputs. |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5079 | if (inStream == NULL && status == BAD_VALUE && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5080 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5081 | (samplingRate <= 2 * reqSamplingRate) && |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5082 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5083 | ALOGV("openInput() reopening with proposed sampling rate and channels"); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5084 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, |
| 5085 | &channels, &samplingRate, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5086 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5087 | &inStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5088 | } |
| 5089 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5090 | if (inStream != NULL) { |
| 5091 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 5092 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5093 | int id = nextUniqueId(); |
| 5094 | // Start record thread |
| 5095 | // RecorThread require both input and output device indication to forward to audio |
| 5096 | // pre processing modules |
| 5097 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 5098 | thread = new RecordThread(this, |
| 5099 | input, |
| 5100 | reqSamplingRate, |
| 5101 | reqChannels, |
| 5102 | id, |
| 5103 | device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5104 | mRecordThreads.add(id, thread); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5105 | ALOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5106 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; |
| 5107 | if (pFormat) *pFormat = format; |
| 5108 | if (pChannels) *pChannels = reqChannels; |
| 5109 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5110 | input->stream->common.standby(&input->stream->common); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5111 | |
| 5112 | // notify client processes of the new input creation |
| 5113 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 5114 | return id; |
| 5115 | } |
| 5116 | |
| 5117 | return 0; |
| 5118 | } |
| 5119 | |
| 5120 | status_t AudioFlinger::closeInput(int input) |
| 5121 | { |
| 5122 | // keep strong reference on the record thread so that |
| 5123 | // it is not destroyed while exit() is executed |
| 5124 | sp <RecordThread> thread; |
| 5125 | { |
| 5126 | Mutex::Autolock _l(mLock); |
| 5127 | thread = checkRecordThread_l(input); |
| 5128 | if (thread == NULL) { |
| 5129 | return BAD_VALUE; |
| 5130 | } |
| 5131 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5132 | ALOGV("closeInput() %d", input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5133 | void *param2 = 0; |
| 5134 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); |
| 5135 | mRecordThreads.removeItem(input); |
| 5136 | } |
| 5137 | thread->exit(); |
| 5138 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5139 | AudioStreamIn *in = thread->clearInput(); |
| 5140 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5141 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 5142 | delete in; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5143 | |
| 5144 | return NO_ERROR; |
| 5145 | } |
| 5146 | |
| 5147 | status_t AudioFlinger::setStreamOutput(uint32_t stream, int output) |
| 5148 | { |
| 5149 | Mutex::Autolock _l(mLock); |
| 5150 | MixerThread *dstThread = checkMixerThread_l(output); |
| 5151 | if (dstThread == NULL) { |
| 5152 | LOGW("setStreamOutput() bad output id %d", output); |
| 5153 | return BAD_VALUE; |
| 5154 | } |
| 5155 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5156 | ALOGV("setStreamOutput() stream %d to output %d", stream, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5157 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 5158 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5159 | dstThread->setStreamValid(stream, true); |
| 5160 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5161 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5162 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
| 5163 | if (thread != dstThread && |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5164 | thread->type() != ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5165 | MixerThread *srcThread = (MixerThread *)thread; |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5166 | srcThread->setStreamValid(stream, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5167 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5168 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5169 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5170 | |
| 5171 | return NO_ERROR; |
| 5172 | } |
| 5173 | |
| 5174 | |
| 5175 | int AudioFlinger::newAudioSessionId() |
| 5176 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5177 | return nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5180 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 5181 | { |
| 5182 | Mutex::Autolock _l(mLock); |
| 5183 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5184 | ALOGV("acquiring %d from %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5185 | int num = mAudioSessionRefs.size(); |
| 5186 | for (int i = 0; i< num; i++) { |
| 5187 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
| 5188 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5189 | ref->cnt++; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5190 | ALOGV(" incremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5191 | return; |
| 5192 | } |
| 5193 | } |
| 5194 | AudioSessionRef *ref = new AudioSessionRef(); |
| 5195 | ref->sessionid = audioSession; |
| 5196 | ref->pid = caller; |
| 5197 | ref->cnt = 1; |
| 5198 | mAudioSessionRefs.push(ref); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5199 | ALOGV(" added new entry for %d", ref->sessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 5203 | { |
| 5204 | Mutex::Autolock _l(mLock); |
| 5205 | int caller = IPCThreadState::self()->getCallingPid(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5206 | ALOGV("releasing %d from %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5207 | int num = mAudioSessionRefs.size(); |
| 5208 | for (int i = 0; i< num; i++) { |
| 5209 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 5210 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5211 | ref->cnt--; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5212 | ALOGV(" decremented refcount to %d", ref->cnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5213 | if (ref->cnt == 0) { |
| 5214 | mAudioSessionRefs.removeAt(i); |
| 5215 | delete ref; |
| 5216 | purgeStaleEffects_l(); |
| 5217 | } |
| 5218 | return; |
| 5219 | } |
| 5220 | } |
| 5221 | LOGW("session id %d not found for pid %d", audioSession, caller); |
| 5222 | } |
| 5223 | |
| 5224 | void AudioFlinger::purgeStaleEffects_l() { |
| 5225 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5226 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5227 | |
| 5228 | Vector< sp<EffectChain> > chains; |
| 5229 | |
| 5230 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5231 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 5232 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5233 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5234 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 5235 | chains.push(ec); |
| 5236 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5240 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 5241 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5242 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 5243 | chains.push(ec); |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | for (size_t i = 0; i < chains.size(); i++) { |
| 5248 | sp<EffectChain> ec = chains[i]; |
| 5249 | int sessionid = ec->sessionId(); |
| 5250 | sp<ThreadBase> t = ec->mThread.promote(); |
| 5251 | if (t == 0) { |
| 5252 | continue; |
| 5253 | } |
| 5254 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 5255 | bool found = false; |
| 5256 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 5257 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
| 5258 | if (ref->sessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5259 | ALOGV(" session %d still exists for %d with %d refs", |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5260 | sessionid, ref->pid, ref->cnt); |
| 5261 | found = true; |
| 5262 | break; |
| 5263 | } |
| 5264 | } |
| 5265 | if (!found) { |
| 5266 | // remove all effects from the chain |
| 5267 | while (ec->mEffects.size()) { |
| 5268 | sp<EffectModule> effect = ec->mEffects[0]; |
| 5269 | effect->unPin(); |
| 5270 | Mutex::Autolock _l (t->mLock); |
| 5271 | t->removeEffect_l(effect); |
| 5272 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 5273 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 5274 | if (handle != 0) { |
| 5275 | handle->mEffect.clear(); |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5276 | if (handle->mHasControl && handle->mEnabled) { |
| 5277 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 5278 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5279 | } |
| 5280 | } |
| 5281 | AudioSystem::unregisterEffect(effect->id()); |
| 5282 | } |
| 5283 | } |
| 5284 | } |
| 5285 | return; |
| 5286 | } |
| 5287 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5288 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
| 5289 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const |
| 5290 | { |
| 5291 | PlaybackThread *thread = NULL; |
| 5292 | if (mPlaybackThreads.indexOfKey(output) >= 0) { |
| 5293 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); |
| 5294 | } |
| 5295 | return thread; |
| 5296 | } |
| 5297 | |
| 5298 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
| 5299 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const |
| 5300 | { |
| 5301 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5302 | if (thread != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5303 | if (thread->type() == ThreadBase::DIRECT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5304 | thread = NULL; |
| 5305 | } |
| 5306 | } |
| 5307 | return (MixerThread *)thread; |
| 5308 | } |
| 5309 | |
| 5310 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
| 5311 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const |
| 5312 | { |
| 5313 | RecordThread *thread = NULL; |
| 5314 | if (mRecordThreads.indexOfKey(input) >= 0) { |
| 5315 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); |
| 5316 | } |
| 5317 | return thread; |
| 5318 | } |
| 5319 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5320 | uint32_t AudioFlinger::nextUniqueId() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5321 | { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5322 | return android_atomic_inc(&mNextUniqueId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5323 | } |
| 5324 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5325 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() |
| 5326 | { |
| 5327 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5328 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5329 | AudioStreamOut *output = thread->getOutput(); |
| 5330 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5331 | return thread; |
| 5332 | } |
| 5333 | } |
| 5334 | return NULL; |
| 5335 | } |
| 5336 | |
| 5337 | uint32_t AudioFlinger::primaryOutputDevice_l() |
| 5338 | { |
| 5339 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 5340 | |
| 5341 | if (thread == NULL) { |
| 5342 | return 0; |
| 5343 | } |
| 5344 | |
| 5345 | return thread->device(); |
| 5346 | } |
| 5347 | |
| 5348 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5349 | // ---------------------------------------------------------------------------- |
| 5350 | // Effect management |
| 5351 | // ---------------------------------------------------------------------------- |
| 5352 | |
| 5353 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5354 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) |
| 5355 | { |
| 5356 | Mutex::Autolock _l(mLock); |
| 5357 | return EffectQueryNumberEffects(numEffects); |
| 5358 | } |
| 5359 | |
| 5360 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
| 5361 | { |
| 5362 | Mutex::Autolock _l(mLock); |
| 5363 | return EffectQueryEffect(index, descriptor); |
| 5364 | } |
| 5365 | |
| 5366 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) |
| 5367 | { |
| 5368 | Mutex::Autolock _l(mLock); |
| 5369 | return EffectGetDescriptor(pUuid, descriptor); |
| 5370 | } |
| 5371 | |
| 5372 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5373 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 5374 | effect_descriptor_t *pDesc, |
| 5375 | const sp<IEffectClient>& effectClient, |
| 5376 | int32_t priority, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5377 | int io, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5378 | int sessionId, |
| 5379 | status_t *status, |
| 5380 | int *id, |
| 5381 | int *enabled) |
| 5382 | { |
| 5383 | status_t lStatus = NO_ERROR; |
| 5384 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5385 | effect_descriptor_t desc; |
| 5386 | sp<Client> client; |
| 5387 | wp<Client> wclient; |
| 5388 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5389 | ALOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5390 | pid, effectClient.get(), priority, sessionId, io); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5391 | |
| 5392 | if (pDesc == NULL) { |
| 5393 | lStatus = BAD_VALUE; |
| 5394 | goto Exit; |
| 5395 | } |
| 5396 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5397 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5398 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5399 | lStatus = PERMISSION_DENIED; |
| 5400 | goto Exit; |
| 5401 | } |
| 5402 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5403 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5404 | // 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] | 5405 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5406 | lStatus = PERMISSION_DENIED; |
| 5407 | goto Exit; |
| 5408 | } |
| 5409 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5410 | if (io == 0) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5411 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5412 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5413 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5414 | lStatus = BAD_VALUE; |
| 5415 | goto Exit; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5416 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5417 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5418 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5419 | // and we will exit safely |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5420 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5421 | } |
| 5422 | } |
| 5423 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5424 | { |
| 5425 | Mutex::Autolock _l(mLock); |
| 5426 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5427 | |
| 5428 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 5429 | // if uuid is specified, request effect descriptor |
| 5430 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 5431 | if (lStatus < 0) { |
| 5432 | LOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
| 5433 | goto Exit; |
| 5434 | } |
| 5435 | } else { |
| 5436 | // if uuid is not specified, look for an available implementation |
| 5437 | // of the required type in effect factory |
| 5438 | if (EffectIsNullUuid(&pDesc->type)) { |
| 5439 | LOGW("createEffect() no effect type"); |
| 5440 | lStatus = BAD_VALUE; |
| 5441 | goto Exit; |
| 5442 | } |
| 5443 | uint32_t numEffects = 0; |
| 5444 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5445 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5446 | bool found = false; |
| 5447 | |
| 5448 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 5449 | if (lStatus < 0) { |
| 5450 | LOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
| 5451 | goto Exit; |
| 5452 | } |
| 5453 | for (uint32_t i = 0; i < numEffects; i++) { |
| 5454 | lStatus = EffectQueryEffect(i, &desc); |
| 5455 | if (lStatus < 0) { |
| 5456 | LOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
| 5457 | continue; |
| 5458 | } |
| 5459 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 5460 | // If matching type found save effect descriptor. If the session is |
| 5461 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 5462 | // an auxiliary version of this effect type is available |
| 5463 | found = true; |
| 5464 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5465 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5466 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5467 | break; |
| 5468 | } |
| 5469 | } |
| 5470 | } |
| 5471 | if (!found) { |
| 5472 | lStatus = BAD_VALUE; |
| 5473 | LOGW("createEffect() effect not found"); |
| 5474 | goto Exit; |
| 5475 | } |
| 5476 | // For same effect type, chose auxiliary version over insert version if |
| 5477 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5478 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5479 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5480 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | // 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] | 5485 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5486 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5487 | lStatus = INVALID_OPERATION; |
| 5488 | goto Exit; |
| 5489 | } |
| 5490 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5491 | // check recording permission for visualizer |
| 5492 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 5493 | !recordingAllowed()) { |
| 5494 | lStatus = PERMISSION_DENIED; |
| 5495 | goto Exit; |
| 5496 | } |
| 5497 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5498 | // return effect descriptor |
| 5499 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 5500 | |
| 5501 | // If output is not specified try to find a matching audio session ID in one of the |
| 5502 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5503 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 5504 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5505 | // Note: io is never 0 when creating an effect on an input |
| 5506 | if (io == 0) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5507 | // look for the thread where the specified audio session is present |
| 5508 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5509 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5510 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5511 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5512 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5513 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5514 | if (io == 0) { |
| 5515 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5516 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 5517 | io = mRecordThreads.keyAt(i); |
| 5518 | break; |
| 5519 | } |
| 5520 | } |
| 5521 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5522 | // If no output thread contains the requested session ID, default to |
| 5523 | // first output. The effect chain will be moved to the correct output |
| 5524 | // thread when a track with the same session ID is created |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5525 | if (io == 0 && mPlaybackThreads.size()) { |
| 5526 | io = mPlaybackThreads.keyAt(0); |
| 5527 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5528 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5529 | } |
| 5530 | ThreadBase *thread = checkRecordThread_l(io); |
| 5531 | if (thread == NULL) { |
| 5532 | thread = checkPlaybackThread_l(io); |
| 5533 | if (thread == NULL) { |
| 5534 | LOGE("createEffect() unknown output thread"); |
| 5535 | lStatus = BAD_VALUE; |
| 5536 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5537 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5538 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5539 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5540 | wclient = mClients.valueFor(pid); |
| 5541 | |
| 5542 | if (wclient != NULL) { |
| 5543 | client = wclient.promote(); |
| 5544 | } else { |
| 5545 | client = new Client(this, pid); |
| 5546 | mClients.add(pid, client); |
| 5547 | } |
| 5548 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5549 | // create effect on selected output thread |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5550 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 5551 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5552 | if (handle != 0 && id != NULL) { |
| 5553 | *id = handle->id(); |
| 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | Exit: |
| 5558 | if(status) { |
| 5559 | *status = lStatus; |
| 5560 | } |
| 5561 | return handle; |
| 5562 | } |
| 5563 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5564 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5565 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5566 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5567 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5568 | Mutex::Autolock _l(mLock); |
| 5569 | if (srcOutput == dstOutput) { |
| 5570 | LOGW("moveEffects() same dst and src outputs %d", dstOutput); |
| 5571 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5572 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5573 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 5574 | if (srcThread == NULL) { |
| 5575 | LOGW("moveEffects() bad srcOutput %d", srcOutput); |
| 5576 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5577 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5578 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 5579 | if (dstThread == NULL) { |
| 5580 | LOGW("moveEffects() bad dstOutput %d", dstOutput); |
| 5581 | return BAD_VALUE; |
| 5582 | } |
| 5583 | |
| 5584 | Mutex::Autolock _dl(dstThread->mLock); |
| 5585 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5586 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5587 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5588 | return NO_ERROR; |
| 5589 | } |
| 5590 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5591 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5592 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5593 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5594 | AudioFlinger::PlaybackThread *dstThread, |
| 5595 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5596 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5597 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5598 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5599 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5600 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5601 | if (chain == 0) { |
| 5602 | LOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5603 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5604 | return INVALID_OPERATION; |
| 5605 | } |
| 5606 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5607 | // 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] | 5608 | // 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] | 5609 | // 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] | 5610 | // removed. |
| 5611 | srcThread->removeEffectChain_l(chain); |
| 5612 | |
| 5613 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 5614 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5615 | int dstOutput = dstThread->id(); |
| 5616 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5617 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5618 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 5619 | while (effect != 0) { |
| 5620 | srcThread->removeEffect_l(effect); |
| 5621 | dstThread->addEffect_l(effect); |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5622 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 5623 | if (effect->state() == EffectModule::ACTIVE || |
| 5624 | effect->state() == EffectModule::STOPPING) { |
| 5625 | effect->start(); |
| 5626 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5627 | // if the move request is not received from audio policy manager, the effect must be |
| 5628 | // re-registered with the new strategy and output |
| 5629 | if (dstChain == 0) { |
| 5630 | dstChain = effect->chain().promote(); |
| 5631 | if (dstChain == 0) { |
| 5632 | LOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
| 5633 | srcThread->addEffect_l(effect); |
| 5634 | return NO_INIT; |
| 5635 | } |
| 5636 | strategy = dstChain->strategy(); |
| 5637 | } |
| 5638 | if (reRegister) { |
| 5639 | AudioSystem::unregisterEffect(effect->id()); |
| 5640 | AudioSystem::registerEffect(&effect->desc(), |
| 5641 | dstOutput, |
| 5642 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5643 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5644 | effect->id()); |
| 5645 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5646 | effect = chain->getEffectFromId_l(0); |
| 5647 | } |
| 5648 | |
| 5649 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5652 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5653 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5654 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5655 | const sp<AudioFlinger::Client>& client, |
| 5656 | const sp<IEffectClient>& effectClient, |
| 5657 | int32_t priority, |
| 5658 | int sessionId, |
| 5659 | effect_descriptor_t *desc, |
| 5660 | int *enabled, |
| 5661 | status_t *status |
| 5662 | ) |
| 5663 | { |
| 5664 | sp<EffectModule> effect; |
| 5665 | sp<EffectHandle> handle; |
| 5666 | status_t lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5667 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5668 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5669 | bool effectCreated = false; |
| 5670 | bool effectRegistered = false; |
| 5671 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5672 | lStatus = initCheck(); |
| 5673 | if (lStatus != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5674 | LOGW("createEffect_l() Audio driver not initialized."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5675 | goto Exit; |
| 5676 | } |
| 5677 | |
| 5678 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 5679 | // 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] | 5680 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5681 | LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
| 5682 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5683 | lStatus = BAD_VALUE; |
| 5684 | goto Exit; |
| 5685 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5686 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 5687 | if ((mType == RECORD && |
| 5688 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || |
| 5689 | (mType != RECORD && |
| 5690 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 5691 | LOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 5692 | desc->name, desc->flags, mType); |
| 5693 | lStatus = BAD_VALUE; |
| 5694 | goto Exit; |
| 5695 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5696 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5697 | 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] | 5698 | |
| 5699 | { // scope for mLock |
| 5700 | Mutex::Autolock _l(mLock); |
| 5701 | |
| 5702 | // check for existing effect chain with the requested audio session |
| 5703 | chain = getEffectChain_l(sessionId); |
| 5704 | if (chain == 0) { |
| 5705 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5706 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5707 | chain = new EffectChain(this, sessionId); |
| 5708 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5709 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5710 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5711 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5712 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5715 | 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] | 5716 | |
| 5717 | if (effect == 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5718 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5719 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5720 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5721 | if (lStatus != NO_ERROR) { |
| 5722 | goto Exit; |
| 5723 | } |
| 5724 | effectRegistered = true; |
| 5725 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5726 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5727 | lStatus = effect->status(); |
| 5728 | if (lStatus != NO_ERROR) { |
| 5729 | goto Exit; |
| 5730 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5731 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5732 | if (lStatus != NO_ERROR) { |
| 5733 | goto Exit; |
| 5734 | } |
| 5735 | effectCreated = true; |
| 5736 | |
| 5737 | effect->setDevice(mDevice); |
| 5738 | effect->setMode(mAudioFlinger->getMode()); |
| 5739 | } |
| 5740 | // create effect handle and connect it to effect module |
| 5741 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 5742 | lStatus = effect->addHandle(handle); |
| 5743 | if (enabled) { |
| 5744 | *enabled = (int)effect->isEnabled(); |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | Exit: |
| 5749 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5750 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5751 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5752 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5753 | } |
| 5754 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5755 | AudioSystem::unregisterEffect(effect->id()); |
| 5756 | } |
| 5757 | if (chainCreated) { |
| 5758 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5759 | } |
| 5760 | handle.clear(); |
| 5761 | } |
| 5762 | |
| 5763 | if(status) { |
| 5764 | *status = lStatus; |
| 5765 | } |
| 5766 | return handle; |
| 5767 | } |
| 5768 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5769 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 5770 | { |
| 5771 | sp<EffectModule> effect; |
| 5772 | |
| 5773 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5774 | if (chain != 0) { |
| 5775 | effect = chain->getEffectFromId_l(effectId); |
| 5776 | } |
| 5777 | return effect; |
| 5778 | } |
| 5779 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5780 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 5781 | // PlaybackThread::mLock held |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5782 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5783 | { |
| 5784 | // check for existing effect chain with the requested audio session |
| 5785 | int sessionId = effect->sessionId(); |
| 5786 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5787 | bool chainCreated = false; |
| 5788 | |
| 5789 | if (chain == 0) { |
| 5790 | // create a new chain for this session |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5791 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5792 | chain = new EffectChain(this, sessionId); |
| 5793 | addEffectChain_l(chain); |
| 5794 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5795 | chainCreated = true; |
| 5796 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5797 | 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] | 5798 | |
| 5799 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 5800 | LOGW("addEffect_l() %p effect %s already present in chain %p", |
| 5801 | this, effect->desc().name, chain.get()); |
| 5802 | return BAD_VALUE; |
| 5803 | } |
| 5804 | |
| 5805 | status_t status = chain->addEffect_l(effect); |
| 5806 | if (status != NO_ERROR) { |
| 5807 | if (chainCreated) { |
| 5808 | removeEffectChain_l(chain); |
| 5809 | } |
| 5810 | return status; |
| 5811 | } |
| 5812 | |
| 5813 | effect->setDevice(mDevice); |
| 5814 | effect->setMode(mAudioFlinger->getMode()); |
| 5815 | return NO_ERROR; |
| 5816 | } |
| 5817 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5818 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5819 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5820 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5821 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5822 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5823 | detachAuxEffect_l(effect->id()); |
| 5824 | } |
| 5825 | |
| 5826 | sp<EffectChain> chain = effect->chain().promote(); |
| 5827 | if (chain != 0) { |
| 5828 | // remove effect chain if removing last effect |
| 5829 | if (chain->removeEffect_l(effect) == 0) { |
| 5830 | removeEffectChain_l(chain); |
| 5831 | } |
| 5832 | } else { |
| 5833 | LOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 5834 | } |
| 5835 | } |
| 5836 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5837 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 5838 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5839 | { |
| 5840 | effectChains = mEffectChains; |
| 5841 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5842 | mEffectChains[i]->lock(); |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 5847 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5848 | { |
| 5849 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 5850 | effectChains[i]->unlock(); |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 5855 | { |
| 5856 | Mutex::Autolock _l(mLock); |
| 5857 | return getEffectChain_l(sessionId); |
| 5858 | } |
| 5859 | |
| 5860 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 5861 | { |
| 5862 | sp<EffectChain> chain; |
| 5863 | |
| 5864 | size_t size = mEffectChains.size(); |
| 5865 | for (size_t i = 0; i < size; i++) { |
| 5866 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 5867 | chain = mEffectChains[i]; |
| 5868 | break; |
| 5869 | } |
| 5870 | } |
| 5871 | return chain; |
| 5872 | } |
| 5873 | |
| 5874 | void AudioFlinger::ThreadBase::setMode(uint32_t mode) |
| 5875 | { |
| 5876 | Mutex::Autolock _l(mLock); |
| 5877 | size_t size = mEffectChains.size(); |
| 5878 | for (size_t i = 0; i < size; i++) { |
| 5879 | mEffectChains[i]->setMode_l(mode); |
| 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5884 | const wp<EffectHandle>& handle, |
| 5885 | bool unpiniflast) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5886 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5887 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5888 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5889 | // delete the effect module if removing last handle on it |
| 5890 | if (effect->removeHandle(handle) == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5891 | if (!effect->isPinned() || unpiniflast) { |
| 5892 | removeEffect_l(effect); |
| 5893 | AudioSystem::unregisterEffect(effect->id()); |
| 5894 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 5899 | { |
| 5900 | int session = chain->sessionId(); |
| 5901 | int16_t *buffer = mMixBuffer; |
| 5902 | bool ownsBuffer = false; |
| 5903 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5904 | 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] | 5905 | if (session > 0) { |
| 5906 | // Only one effect chain can be present in direct output thread and it uses |
| 5907 | // the mix buffer as input |
| 5908 | if (mType != DIRECT) { |
| 5909 | size_t numSamples = mFrameCount * mChannelCount; |
| 5910 | buffer = new int16_t[numSamples]; |
| 5911 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5912 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5913 | ownsBuffer = true; |
| 5914 | } |
| 5915 | |
| 5916 | // Attach all tracks with same session ID to this chain. |
| 5917 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5918 | sp<Track> track = mTracks[i]; |
| 5919 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5920 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5921 | track->setMainBuffer(buffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5922 | chain->incTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | // indicate all active tracks in the chain |
| 5927 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5928 | sp<Track> track = mActiveTracks[i].promote(); |
| 5929 | if (track == 0) continue; |
| 5930 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5931 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5932 | chain->incActiveTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | chain->setInBuffer(buffer, ownsBuffer); |
| 5938 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5939 | // 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] | 5940 | // 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] | 5941 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 5942 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5943 | // after track specific effects and before output stage |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5944 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 5945 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5946 | // Effect chain for other sessions are inserted at beginning of effect |
| 5947 | // chains list to be processed before output mix effects. Relative order between other |
| 5948 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5949 | size_t size = mEffectChains.size(); |
| 5950 | size_t i = 0; |
| 5951 | for (i = 0; i < size; i++) { |
| 5952 | if (mEffectChains[i]->sessionId() < session) break; |
| 5953 | } |
| 5954 | mEffectChains.insertAt(chain, i); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5955 | checkSuspendOnAddEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5956 | |
| 5957 | return NO_ERROR; |
| 5958 | } |
| 5959 | |
| 5960 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 5961 | { |
| 5962 | int session = chain->sessionId(); |
| 5963 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5964 | 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] | 5965 | |
| 5966 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5967 | if (chain == mEffectChains[i]) { |
| 5968 | mEffectChains.removeAt(i); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5969 | // detach all active tracks from the chain |
| 5970 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5971 | sp<Track> track = mActiveTracks[i].promote(); |
| 5972 | if (track == 0) continue; |
| 5973 | if (session == track->sessionId()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 5974 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5975 | chain.get(), session); |
| 5976 | chain->decActiveTrackCnt(); |
| 5977 | } |
| 5978 | } |
| 5979 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5980 | // detach all tracks with same session ID from this chain |
| 5981 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5982 | sp<Track> track = mTracks[i]; |
| 5983 | if (session == track->sessionId()) { |
| 5984 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5985 | chain->decTrackCnt(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5986 | } |
| 5987 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5988 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5989 | } |
| 5990 | } |
| 5991 | return mEffectChains.size(); |
| 5992 | } |
| 5993 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5994 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 5995 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5996 | { |
| 5997 | Mutex::Autolock _l(mLock); |
| 5998 | return attachAuxEffect_l(track, EffectId); |
| 5999 | } |
| 6000 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6001 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 6002 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6003 | { |
| 6004 | status_t status = NO_ERROR; |
| 6005 | |
| 6006 | if (EffectId == 0) { |
| 6007 | track->setAuxBuffer(0, NULL); |
| 6008 | } else { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6009 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 6010 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6011 | if (effect != 0) { |
| 6012 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6013 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 6014 | } else { |
| 6015 | status = INVALID_OPERATION; |
| 6016 | } |
| 6017 | } else { |
| 6018 | status = BAD_VALUE; |
| 6019 | } |
| 6020 | } |
| 6021 | return status; |
| 6022 | } |
| 6023 | |
| 6024 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 6025 | { |
| 6026 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6027 | sp<Track> track = mTracks[i]; |
| 6028 | if (track->auxEffectId() == effectId) { |
| 6029 | attachAuxEffect_l(track, 0); |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6034 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6035 | { |
| 6036 | // only one chain per input thread |
| 6037 | if (mEffectChains.size() != 0) { |
| 6038 | return INVALID_OPERATION; |
| 6039 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6040 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6041 | |
| 6042 | chain->setInBuffer(NULL); |
| 6043 | chain->setOutBuffer(NULL); |
| 6044 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6045 | checkSuspendOnAddEffectChain_l(chain); |
| 6046 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6047 | mEffectChains.add(chain); |
| 6048 | |
| 6049 | return NO_ERROR; |
| 6050 | } |
| 6051 | |
| 6052 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6053 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6054 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6055 | LOGW_IF(mEffectChains.size() != 1, |
| 6056 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6057 | chain.get(), mEffectChains.size(), this); |
| 6058 | if (mEffectChains.size() == 1) { |
| 6059 | mEffectChains.removeAt(0); |
| 6060 | } |
| 6061 | return 0; |
| 6062 | } |
| 6063 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6064 | // ---------------------------------------------------------------------------- |
| 6065 | // EffectModule implementation |
| 6066 | // ---------------------------------------------------------------------------- |
| 6067 | |
| 6068 | #undef LOG_TAG |
| 6069 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 6070 | |
| 6071 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, |
| 6072 | const wp<AudioFlinger::EffectChain>& chain, |
| 6073 | effect_descriptor_t *desc, |
| 6074 | int id, |
| 6075 | int sessionId) |
| 6076 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6077 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6078 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6079 | ALOGV("Constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6080 | int lStatus; |
| 6081 | sp<ThreadBase> thread = mThread.promote(); |
| 6082 | if (thread == 0) { |
| 6083 | return; |
| 6084 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6085 | |
| 6086 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 6087 | |
| 6088 | // create effect engine from effect factory |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6089 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6090 | |
| 6091 | if (mStatus != NO_ERROR) { |
| 6092 | return; |
| 6093 | } |
| 6094 | lStatus = init(); |
| 6095 | if (lStatus < 0) { |
| 6096 | mStatus = lStatus; |
| 6097 | goto Error; |
| 6098 | } |
| 6099 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6100 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 6101 | mPinned = true; |
| 6102 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6103 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6104 | return; |
| 6105 | Error: |
| 6106 | EffectRelease(mEffectInterface); |
| 6107 | mEffectInterface = NULL; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6108 | ALOGV("Constructor Error %d", mStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | AudioFlinger::EffectModule::~EffectModule() |
| 6112 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6113 | ALOGV("Destructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6114 | if (mEffectInterface != NULL) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6115 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6116 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 6117 | sp<ThreadBase> thread = mThread.promote(); |
| 6118 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6119 | audio_stream_t *stream = thread->stream(); |
| 6120 | if (stream != NULL) { |
| 6121 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6122 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6123 | } |
| 6124 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6125 | // release effect engine |
| 6126 | EffectRelease(mEffectInterface); |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) |
| 6131 | { |
| 6132 | status_t status; |
| 6133 | |
| 6134 | Mutex::Autolock _l(mLock); |
| 6135 | // First handle in mHandles has highest priority and controls the effect module |
| 6136 | int priority = handle->priority(); |
| 6137 | size_t size = mHandles.size(); |
| 6138 | sp<EffectHandle> h; |
| 6139 | size_t i; |
| 6140 | for (i = 0; i < size; i++) { |
| 6141 | h = mHandles[i].promote(); |
| 6142 | if (h == 0) continue; |
| 6143 | if (h->priority() <= priority) break; |
| 6144 | } |
| 6145 | // if inserted in first place, move effect control from previous owner to this handle |
| 6146 | if (i == 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6147 | bool enabled = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6148 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6149 | enabled = h->enabled(); |
| 6150 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6151 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6152 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6153 | status = NO_ERROR; |
| 6154 | } else { |
| 6155 | status = ALREADY_EXISTS; |
| 6156 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6157 | 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] | 6158 | mHandles.insertAt(handle, i); |
| 6159 | return status; |
| 6160 | } |
| 6161 | |
| 6162 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 6163 | { |
| 6164 | Mutex::Autolock _l(mLock); |
| 6165 | size_t size = mHandles.size(); |
| 6166 | size_t i; |
| 6167 | for (i = 0; i < size; i++) { |
| 6168 | if (mHandles[i] == handle) break; |
| 6169 | } |
| 6170 | if (i == size) { |
| 6171 | return size; |
| 6172 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6173 | 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] | 6174 | |
| 6175 | bool enabled = false; |
| 6176 | EffectHandle *hdl = handle.unsafe_get(); |
| 6177 | if (hdl) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6178 | ALOGV("removeHandle() unsafe_get OK"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6179 | enabled = hdl->enabled(); |
| 6180 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6181 | mHandles.removeAt(i); |
| 6182 | size = mHandles.size(); |
| 6183 | // if removed from first place, move effect control from this handle to next in line |
| 6184 | if (i == 0 && size != 0) { |
| 6185 | sp<EffectHandle> h = mHandles[0].promote(); |
| 6186 | if (h != 0) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6187 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6188 | } |
| 6189 | } |
| 6190 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6191 | // Prevent calls to process() and other functions on effect interface from now on. |
| 6192 | // The effect engine will be released by the destructor when the last strong reference on |
| 6193 | // this object is released which can happen after next process is called. |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6194 | if (size == 0 && !mPinned) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6195 | mState = DESTROYED; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6196 | } |
| 6197 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6198 | return size; |
| 6199 | } |
| 6200 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6201 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 6202 | { |
| 6203 | Mutex::Autolock _l(mLock); |
| 6204 | sp<EffectHandle> handle; |
| 6205 | if (mHandles.size() != 0) { |
| 6206 | handle = mHandles[0].promote(); |
| 6207 | } |
| 6208 | return handle; |
| 6209 | } |
| 6210 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6211 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6212 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6213 | ALOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6214 | // keep a strong reference on this EffectModule to avoid calling the |
| 6215 | // destructor before we exit |
| 6216 | sp<EffectModule> keep(this); |
| 6217 | { |
| 6218 | sp<ThreadBase> thread = mThread.promote(); |
| 6219 | if (thread != 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6220 | thread->disconnectEffect(keep, handle, unpiniflast); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | void AudioFlinger::EffectModule::updateState() { |
| 6226 | Mutex::Autolock _l(mLock); |
| 6227 | |
| 6228 | switch (mState) { |
| 6229 | case RESTART: |
| 6230 | reset_l(); |
| 6231 | // FALL THROUGH |
| 6232 | |
| 6233 | case STARTING: |
| 6234 | // clear auxiliary effect input buffer for next accumulation |
| 6235 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6236 | memset(mConfig.inputCfg.buffer.raw, |
| 6237 | 0, |
| 6238 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 6239 | } |
| 6240 | start_l(); |
| 6241 | mState = ACTIVE; |
| 6242 | break; |
| 6243 | case STOPPING: |
| 6244 | stop_l(); |
| 6245 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 6246 | mState = STOPPED; |
| 6247 | break; |
| 6248 | case STOPPED: |
| 6249 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 6250 | // turn off sequence. |
| 6251 | if (--mDisableWaitCnt == 0) { |
| 6252 | reset_l(); |
| 6253 | mState = IDLE; |
| 6254 | } |
| 6255 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6256 | default: //IDLE , ACTIVE, DESTROYED |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6257 | break; |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | void AudioFlinger::EffectModule::process() |
| 6262 | { |
| 6263 | Mutex::Autolock _l(mLock); |
| 6264 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6265 | if (mState == DESTROYED || mEffectInterface == NULL || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6266 | mConfig.inputCfg.buffer.raw == NULL || |
| 6267 | mConfig.outputCfg.buffer.raw == NULL) { |
| 6268 | return; |
| 6269 | } |
| 6270 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6271 | if (isProcessEnabled()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6272 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 6273 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6274 | AudioMixer::ditherAndClamp(mConfig.inputCfg.buffer.s32, |
| 6275 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6276 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | // do the actual processing in the effect engine |
| 6280 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 6281 | &mConfig.inputCfg.buffer, |
| 6282 | &mConfig.outputCfg.buffer); |
| 6283 | |
| 6284 | // force transition to IDLE state when engine is ready |
| 6285 | if (mState == STOPPED && ret == -ENODATA) { |
| 6286 | mDisableWaitCnt = 1; |
| 6287 | } |
| 6288 | |
| 6289 | // clear auxiliary effect input buffer for next accumulation |
| 6290 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6291 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 6292 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6293 | } |
| 6294 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6295 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6296 | // If an insert effect is idle and input buffer is different from output buffer, |
| 6297 | // accumulate input onto output |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6298 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6299 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 7333748 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6300 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 6301 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 6302 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 6303 | for (size_t i = 0; i < frameCnt; i++) { |
| 6304 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6305 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6306 | } |
| 6307 | } |
| 6308 | } |
| 6309 | |
| 6310 | void AudioFlinger::EffectModule::reset_l() |
| 6311 | { |
| 6312 | if (mEffectInterface == NULL) { |
| 6313 | return; |
| 6314 | } |
| 6315 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 6316 | } |
| 6317 | |
| 6318 | status_t AudioFlinger::EffectModule::configure() |
| 6319 | { |
| 6320 | uint32_t channels; |
| 6321 | if (mEffectInterface == NULL) { |
| 6322 | return NO_INIT; |
| 6323 | } |
| 6324 | |
| 6325 | sp<ThreadBase> thread = mThread.promote(); |
| 6326 | if (thread == 0) { |
| 6327 | return DEAD_OBJECT; |
| 6328 | } |
| 6329 | |
| 6330 | // TODO: handle configuration of effects replacing track process |
| 6331 | if (thread->channelCount() == 1) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6332 | channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6333 | } else { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6334 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6338 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6339 | } else { |
| 6340 | mConfig.inputCfg.channels = channels; |
| 6341 | } |
| 6342 | mConfig.outputCfg.channels = channels; |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6343 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 6344 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6345 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 6346 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 6347 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 6348 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 6349 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 6350 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 6351 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 6352 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 6353 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 6354 | // Insert effect: |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6355 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6356 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6357 | // - in other sessions: |
| 6358 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 6359 | // other effect: overwrites output buffer: input buffer == output buffer |
| 6360 | // Auxiliary effect: |
| 6361 | // accumulates in output buffer: input buffer != output buffer |
| 6362 | // Therefore: accumulate <=> input buffer != output buffer |
| 6363 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6364 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 6365 | } else { |
| 6366 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 6367 | } |
| 6368 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 6369 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 6370 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 6371 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 6372 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6373 | ALOGV("configure() %p thread %p buffer %p framecount %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6374 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 6375 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6376 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6377 | uint32_t size = sizeof(int); |
| 6378 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6379 | EFFECT_CMD_CONFIGURE, |
| 6380 | sizeof(effect_config_t), |
| 6381 | &mConfig, |
| 6382 | &size, |
| 6383 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6384 | if (status == 0) { |
| 6385 | status = cmdStatus; |
| 6386 | } |
| 6387 | |
| 6388 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 6389 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 6390 | |
| 6391 | return status; |
| 6392 | } |
| 6393 | |
| 6394 | status_t AudioFlinger::EffectModule::init() |
| 6395 | { |
| 6396 | Mutex::Autolock _l(mLock); |
| 6397 | if (mEffectInterface == NULL) { |
| 6398 | return NO_INIT; |
| 6399 | } |
| 6400 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6401 | uint32_t size = sizeof(status_t); |
| 6402 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6403 | EFFECT_CMD_INIT, |
| 6404 | 0, |
| 6405 | NULL, |
| 6406 | &size, |
| 6407 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6408 | if (status == 0) { |
| 6409 | status = cmdStatus; |
| 6410 | } |
| 6411 | return status; |
| 6412 | } |
| 6413 | |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6414 | status_t AudioFlinger::EffectModule::start() |
| 6415 | { |
| 6416 | Mutex::Autolock _l(mLock); |
| 6417 | return start_l(); |
| 6418 | } |
| 6419 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6420 | status_t AudioFlinger::EffectModule::start_l() |
| 6421 | { |
| 6422 | if (mEffectInterface == NULL) { |
| 6423 | return NO_INIT; |
| 6424 | } |
| 6425 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6426 | uint32_t size = sizeof(status_t); |
| 6427 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6428 | EFFECT_CMD_ENABLE, |
| 6429 | 0, |
| 6430 | NULL, |
| 6431 | &size, |
| 6432 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6433 | if (status == 0) { |
| 6434 | status = cmdStatus; |
| 6435 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6436 | if (status == 0 && |
| 6437 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6438 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6439 | sp<ThreadBase> thread = mThread.promote(); |
| 6440 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6441 | audio_stream_t *stream = thread->stream(); |
| 6442 | if (stream != NULL) { |
| 6443 | stream->add_audio_effect(stream, mEffectInterface); |
| 6444 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6445 | } |
| 6446 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6447 | return status; |
| 6448 | } |
| 6449 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6450 | status_t AudioFlinger::EffectModule::stop() |
| 6451 | { |
| 6452 | Mutex::Autolock _l(mLock); |
| 6453 | return stop_l(); |
| 6454 | } |
| 6455 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6456 | status_t AudioFlinger::EffectModule::stop_l() |
| 6457 | { |
| 6458 | if (mEffectInterface == NULL) { |
| 6459 | return NO_INIT; |
| 6460 | } |
| 6461 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6462 | uint32_t size = sizeof(status_t); |
| 6463 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6464 | EFFECT_CMD_DISABLE, |
| 6465 | 0, |
| 6466 | NULL, |
| 6467 | &size, |
| 6468 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6469 | if (status == 0) { |
| 6470 | status = cmdStatus; |
| 6471 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6472 | if (status == 0 && |
| 6473 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6474 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6475 | sp<ThreadBase> thread = mThread.promote(); |
| 6476 | if (thread != 0) { |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6477 | audio_stream_t *stream = thread->stream(); |
| 6478 | if (stream != NULL) { |
| 6479 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6480 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6481 | } |
| 6482 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6483 | return status; |
| 6484 | } |
| 6485 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6486 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 6487 | uint32_t cmdSize, |
| 6488 | void *pCmdData, |
| 6489 | uint32_t *replySize, |
| 6490 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6491 | { |
| 6492 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6493 | // ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6494 | |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6495 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6496 | return NO_INIT; |
| 6497 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6498 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6499 | cmdCode, |
| 6500 | cmdSize, |
| 6501 | pCmdData, |
| 6502 | replySize, |
| 6503 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6504 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6505 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6506 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6507 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6508 | if (h != 0) { |
| 6509 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 6510 | } |
| 6511 | } |
| 6512 | } |
| 6513 | return status; |
| 6514 | } |
| 6515 | |
| 6516 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 6517 | { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6518 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6519 | Mutex::Autolock _l(mLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6520 | ALOGV("setEnabled %p enabled %d", this, enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6521 | |
| 6522 | if (enabled != isEnabled()) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6523 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 6524 | if (enabled && status != NO_ERROR) { |
| 6525 | return status; |
| 6526 | } |
| 6527 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6528 | switch (mState) { |
| 6529 | // going from disabled to enabled |
| 6530 | case IDLE: |
| 6531 | mState = STARTING; |
| 6532 | break; |
| 6533 | case STOPPED: |
| 6534 | mState = RESTART; |
| 6535 | break; |
| 6536 | case STOPPING: |
| 6537 | mState = ACTIVE; |
| 6538 | break; |
| 6539 | |
| 6540 | // going from enabled to disabled |
| 6541 | case RESTART: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6542 | mState = STOPPED; |
| 6543 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6544 | case STARTING: |
| 6545 | mState = IDLE; |
| 6546 | break; |
| 6547 | case ACTIVE: |
| 6548 | mState = STOPPING; |
| 6549 | break; |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6550 | case DESTROYED: |
| 6551 | return NO_ERROR; // simply ignore as we are being destroyed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6552 | } |
| 6553 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6554 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6555 | if (h != 0) { |
| 6556 | h->setEnabled(enabled); |
| 6557 | } |
| 6558 | } |
| 6559 | } |
| 6560 | return NO_ERROR; |
| 6561 | } |
| 6562 | |
| 6563 | bool AudioFlinger::EffectModule::isEnabled() |
| 6564 | { |
| 6565 | switch (mState) { |
| 6566 | case RESTART: |
| 6567 | case STARTING: |
| 6568 | case ACTIVE: |
| 6569 | return true; |
| 6570 | case IDLE: |
| 6571 | case STOPPING: |
| 6572 | case STOPPED: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6573 | case DESTROYED: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6574 | default: |
| 6575 | return false; |
| 6576 | } |
| 6577 | } |
| 6578 | |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6579 | bool AudioFlinger::EffectModule::isProcessEnabled() |
| 6580 | { |
| 6581 | switch (mState) { |
| 6582 | case RESTART: |
| 6583 | case ACTIVE: |
| 6584 | case STOPPING: |
| 6585 | case STOPPED: |
| 6586 | return true; |
| 6587 | case IDLE: |
| 6588 | case STARTING: |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6589 | case DESTROYED: |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6590 | default: |
| 6591 | return false; |
| 6592 | } |
| 6593 | } |
| 6594 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6595 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 6596 | { |
| 6597 | Mutex::Autolock _l(mLock); |
| 6598 | status_t status = NO_ERROR; |
| 6599 | |
| 6600 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 6601 | // 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] | 6602 | if (isProcessEnabled() && |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6603 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 6604 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6605 | status_t cmdStatus; |
| 6606 | uint32_t volume[2]; |
| 6607 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6608 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6609 | volume[0] = *left; |
| 6610 | volume[1] = *right; |
| 6611 | if (controller) { |
| 6612 | pVolume = volume; |
| 6613 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6614 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6615 | EFFECT_CMD_SET_VOLUME, |
| 6616 | size, |
| 6617 | volume, |
| 6618 | &size, |
| 6619 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6620 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 6621 | *left = volume[0]; |
| 6622 | *right = volume[1]; |
| 6623 | } |
| 6624 | } |
| 6625 | return status; |
| 6626 | } |
| 6627 | |
| 6628 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 6629 | { |
| 6630 | Mutex::Autolock _l(mLock); |
| 6631 | status_t status = NO_ERROR; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6632 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 6633 | // audio pre processing modules on RecordThread can receive both output and |
| 6634 | // input device indication in the same call |
| 6635 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 6636 | if (dev) { |
| 6637 | status_t cmdStatus; |
| 6638 | uint32_t size = sizeof(status_t); |
| 6639 | |
| 6640 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6641 | EFFECT_CMD_SET_DEVICE, |
| 6642 | sizeof(uint32_t), |
| 6643 | &dev, |
| 6644 | &size, |
| 6645 | &cmdStatus); |
| 6646 | if (status == NO_ERROR) { |
| 6647 | status = cmdStatus; |
| 6648 | } |
| 6649 | } |
| 6650 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 6651 | if (dev) { |
| 6652 | status_t cmdStatus; |
| 6653 | uint32_t size = sizeof(status_t); |
| 6654 | |
| 6655 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 6656 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 6657 | sizeof(uint32_t), |
| 6658 | &dev, |
| 6659 | &size, |
| 6660 | &cmdStatus); |
| 6661 | if (status2 == NO_ERROR) { |
| 6662 | status2 = cmdStatus; |
| 6663 | } |
| 6664 | if (status == NO_ERROR) { |
| 6665 | status = status2; |
| 6666 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6667 | } |
| 6668 | } |
| 6669 | return status; |
| 6670 | } |
| 6671 | |
| 6672 | status_t AudioFlinger::EffectModule::setMode(uint32_t mode) |
| 6673 | { |
| 6674 | Mutex::Autolock _l(mLock); |
| 6675 | status_t status = NO_ERROR; |
| 6676 | 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] | 6677 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6678 | uint32_t size = sizeof(status_t); |
| 6679 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6680 | EFFECT_CMD_SET_AUDIO_MODE, |
| 6681 | sizeof(int), |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6682 | &mode, |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6683 | &size, |
| 6684 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6685 | if (status == NO_ERROR) { |
| 6686 | status = cmdStatus; |
| 6687 | } |
| 6688 | } |
| 6689 | return status; |
| 6690 | } |
| 6691 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6692 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 6693 | { |
| 6694 | Mutex::Autolock _l(mLock); |
| 6695 | mSuspended = suspended; |
| 6696 | } |
| 6697 | bool AudioFlinger::EffectModule::suspended() |
| 6698 | { |
| 6699 | Mutex::Autolock _l(mLock); |
| 6700 | return mSuspended; |
| 6701 | } |
| 6702 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6703 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 6704 | { |
| 6705 | const size_t SIZE = 256; |
| 6706 | char buffer[SIZE]; |
| 6707 | String8 result; |
| 6708 | |
| 6709 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 6710 | result.append(buffer); |
| 6711 | |
| 6712 | bool locked = tryLock(mLock); |
| 6713 | // failed to lock - AudioFlinger is probably deadlocked |
| 6714 | if (!locked) { |
| 6715 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 6716 | } |
| 6717 | |
| 6718 | result.append("\t\tSession Status State Engine:\n"); |
| 6719 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 6720 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 6721 | result.append(buffer); |
| 6722 | |
| 6723 | result.append("\t\tDescriptor:\n"); |
| 6724 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6725 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 6726 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 6727 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 6728 | result.append(buffer); |
| 6729 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6730 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 6731 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 6732 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 6733 | result.append(buffer); |
Eric Laurent | e1315cf | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6734 | 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] | 6735 | mDescriptor.apiVersion, |
| 6736 | mDescriptor.flags); |
| 6737 | result.append(buffer); |
| 6738 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 6739 | mDescriptor.name); |
| 6740 | result.append(buffer); |
| 6741 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 6742 | mDescriptor.implementor); |
| 6743 | result.append(buffer); |
| 6744 | |
| 6745 | result.append("\t\t- Input configuration:\n"); |
| 6746 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6747 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6748 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 6749 | mConfig.inputCfg.buffer.frameCount, |
| 6750 | mConfig.inputCfg.samplingRate, |
| 6751 | mConfig.inputCfg.channels, |
| 6752 | mConfig.inputCfg.format); |
| 6753 | result.append(buffer); |
| 6754 | |
| 6755 | result.append("\t\t- Output configuration:\n"); |
| 6756 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6757 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6758 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 6759 | mConfig.outputCfg.buffer.frameCount, |
| 6760 | mConfig.outputCfg.samplingRate, |
| 6761 | mConfig.outputCfg.channels, |
| 6762 | mConfig.outputCfg.format); |
| 6763 | result.append(buffer); |
| 6764 | |
| 6765 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 6766 | result.append(buffer); |
| 6767 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 6768 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 6769 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 6770 | if (handle != 0) { |
| 6771 | handle->dump(buffer, SIZE); |
| 6772 | result.append(buffer); |
| 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | result.append("\n"); |
| 6777 | |
| 6778 | write(fd, result.string(), result.length()); |
| 6779 | |
| 6780 | if (locked) { |
| 6781 | mLock.unlock(); |
| 6782 | } |
| 6783 | |
| 6784 | return NO_ERROR; |
| 6785 | } |
| 6786 | |
| 6787 | // ---------------------------------------------------------------------------- |
| 6788 | // EffectHandle implementation |
| 6789 | // ---------------------------------------------------------------------------- |
| 6790 | |
| 6791 | #undef LOG_TAG |
| 6792 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 6793 | |
| 6794 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 6795 | const sp<AudioFlinger::Client>& client, |
| 6796 | const sp<IEffectClient>& effectClient, |
| 6797 | int32_t priority) |
| 6798 | : BnEffect(), |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6799 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6800 | mPriority(priority), mHasControl(false), mEnabled(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6801 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6802 | ALOGV("constructor %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6803 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6804 | if (client == 0) { |
| 6805 | return; |
| 6806 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6807 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 6808 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 6809 | if (mCblkMemory != 0) { |
| 6810 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 6811 | |
| 6812 | if (mCblk) { |
| 6813 | new(mCblk) effect_param_cblk_t(); |
| 6814 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 6815 | } |
| 6816 | } else { |
| 6817 | LOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
| 6818 | return; |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | AudioFlinger::EffectHandle::~EffectHandle() |
| 6823 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6824 | ALOGV("Destructor %p", this); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6825 | disconnect(false); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6826 | ALOGV("Destructor DONE %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6827 | } |
| 6828 | |
| 6829 | status_t AudioFlinger::EffectHandle::enable() |
| 6830 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6831 | ALOGV("enable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6832 | if (!mHasControl) return INVALID_OPERATION; |
| 6833 | if (mEffect == 0) return DEAD_OBJECT; |
| 6834 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6835 | if (mEnabled) { |
| 6836 | return NO_ERROR; |
| 6837 | } |
| 6838 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6839 | mEnabled = true; |
| 6840 | |
| 6841 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6842 | if (thread != 0) { |
| 6843 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 6844 | } |
| 6845 | |
| 6846 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 6847 | if (mEffect->suspended()) { |
| 6848 | return NO_ERROR; |
| 6849 | } |
| 6850 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6851 | status_t status = mEffect->setEnabled(true); |
| 6852 | if (status != NO_ERROR) { |
| 6853 | if (thread != 0) { |
| 6854 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6855 | } |
| 6856 | mEnabled = false; |
| 6857 | } |
| 6858 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6859 | } |
| 6860 | |
| 6861 | status_t AudioFlinger::EffectHandle::disable() |
| 6862 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6863 | ALOGV("disable %p", this); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6864 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6865 | if (mEffect == 0) return DEAD_OBJECT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6866 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6867 | if (!mEnabled) { |
| 6868 | return NO_ERROR; |
| 6869 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6870 | mEnabled = false; |
| 6871 | |
| 6872 | if (mEffect->suspended()) { |
| 6873 | return NO_ERROR; |
| 6874 | } |
| 6875 | |
| 6876 | status_t status = mEffect->setEnabled(false); |
| 6877 | |
| 6878 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6879 | if (thread != 0) { |
| 6880 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6881 | } |
| 6882 | |
| 6883 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | void AudioFlinger::EffectHandle::disconnect() |
| 6887 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6888 | disconnect(true); |
| 6889 | } |
| 6890 | |
| 6891 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) |
| 6892 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6893 | ALOGV("disconnect(%s)", unpiniflast ? "true" : "false"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6894 | if (mEffect == 0) { |
| 6895 | return; |
| 6896 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6897 | mEffect->disconnect(this, unpiniflast); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6898 | |
Eric Laurent | a85a74a | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6899 | if (mHasControl && mEnabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6900 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6901 | if (thread != 0) { |
| 6902 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6903 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6906 | // release sp on module => module destructor can be called now |
| 6907 | mEffect.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6908 | if (mClient != 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6909 | if (mCblk) { |
| 6910 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 6911 | } |
| 6912 | mCblkMemory.clear(); // and free the shared memory |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6913 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 6914 | mClient.clear(); |
| 6915 | } |
| 6916 | } |
| 6917 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6918 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 6919 | uint32_t cmdSize, |
| 6920 | void *pCmdData, |
| 6921 | uint32_t *replySize, |
| 6922 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6923 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 6924 | // ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6925 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6926 | |
| 6927 | // only get parameter command is permitted for applications not controlling the effect |
| 6928 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 6929 | return INVALID_OPERATION; |
| 6930 | } |
| 6931 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6932 | if (mClient == 0) return INVALID_OPERATION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6933 | |
| 6934 | // handle commands that are not forwarded transparently to effect engine |
| 6935 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 6936 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 6937 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 6938 | Mutex::Autolock _l(mCblk->lock); |
| 6939 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 6940 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 6941 | mCblk->serverIndex = 0; |
| 6942 | mCblk->clientIndex = 0; |
| 6943 | return BAD_VALUE; |
| 6944 | } |
| 6945 | status_t status = NO_ERROR; |
| 6946 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 6947 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6948 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6949 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 6950 | int size = *p++; |
| 6951 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
| 6952 | LOGW("command(): invalid parameter block size"); |
| 6953 | break; |
| 6954 | } |
| 6955 | effect_param_t *param = (effect_param_t *)p; |
| 6956 | if (param->psize == 0 || param->vsize == 0) { |
| 6957 | LOGW("command(): null parameter or value size"); |
| 6958 | mCblk->serverIndex += size; |
| 6959 | continue; |
| 6960 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6961 | uint32_t psize = sizeof(effect_param_t) + |
| 6962 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 6963 | param->vsize; |
| 6964 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 6965 | psize, |
| 6966 | p, |
| 6967 | &rsize, |
| 6968 | &reply); |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6969 | // stop at first error encountered |
| 6970 | if (ret != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6971 | status = ret; |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6972 | *(int *)pReplyData = reply; |
| 6973 | break; |
| 6974 | } else if (reply != NO_ERROR) { |
| 6975 | *(int *)pReplyData = reply; |
| 6976 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6977 | } |
| 6978 | mCblk->serverIndex += size; |
| 6979 | } |
| 6980 | mCblk->serverIndex = 0; |
| 6981 | mCblk->clientIndex = 0; |
| 6982 | return status; |
| 6983 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6984 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6985 | return enable(); |
| 6986 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | aeae3de | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6987 | *(int *)pReplyData = NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6988 | return disable(); |
| 6989 | } |
| 6990 | |
| 6991 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 6992 | } |
| 6993 | |
| 6994 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { |
| 6995 | return mCblkMemory; |
| 6996 | } |
| 6997 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6998 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6999 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7000 | ALOGV("setControl %p control %d", this, hasControl); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7001 | |
| 7002 | mHasControl = hasControl; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7003 | mEnabled = enabled; |
| 7004 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7005 | if (signal && mEffectClient != 0) { |
| 7006 | mEffectClient->controlStatusChanged(hasControl); |
| 7007 | } |
| 7008 | } |
| 7009 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7010 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 7011 | uint32_t cmdSize, |
| 7012 | void *pCmdData, |
| 7013 | uint32_t replySize, |
| 7014 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7015 | { |
| 7016 | if (mEffectClient != 0) { |
| 7017 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7018 | } |
| 7019 | } |
| 7020 | |
| 7021 | |
| 7022 | |
| 7023 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 7024 | { |
| 7025 | if (mEffectClient != 0) { |
| 7026 | mEffectClient->enableStatusChanged(enabled); |
| 7027 | } |
| 7028 | } |
| 7029 | |
| 7030 | status_t AudioFlinger::EffectHandle::onTransact( |
| 7031 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7032 | { |
| 7033 | return BnEffect::onTransact(code, data, reply, flags); |
| 7034 | } |
| 7035 | |
| 7036 | |
| 7037 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 7038 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7039 | bool locked = mCblk ? tryLock(mCblk->lock) : false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7040 | |
| 7041 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
| 7042 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 7043 | mPriority, |
| 7044 | mHasControl, |
| 7045 | !locked, |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7046 | mCblk ? mCblk->clientIndex : 0, |
| 7047 | mCblk ? mCblk->serverIndex : 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7048 | ); |
| 7049 | |
| 7050 | if (locked) { |
| 7051 | mCblk->lock.unlock(); |
| 7052 | } |
| 7053 | } |
| 7054 | |
| 7055 | #undef LOG_TAG |
| 7056 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 7057 | |
| 7058 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, |
| 7059 | int sessionId) |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7060 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7061 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 7062 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7063 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7064 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7065 | sp<ThreadBase> thread = mThread.promote(); |
| 7066 | if (thread == 0) { |
| 7067 | return; |
| 7068 | } |
| 7069 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 7070 | thread->frameCount(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | AudioFlinger::EffectChain::~EffectChain() |
| 7074 | { |
| 7075 | if (mOwnInBuffer) { |
| 7076 | delete mInBuffer; |
| 7077 | } |
| 7078 | |
| 7079 | } |
| 7080 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7081 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7082 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7083 | { |
| 7084 | sp<EffectModule> effect; |
| 7085 | size_t size = mEffects.size(); |
| 7086 | |
| 7087 | for (size_t i = 0; i < size; i++) { |
| 7088 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 7089 | effect = mEffects[i]; |
| 7090 | break; |
| 7091 | } |
| 7092 | } |
| 7093 | return effect; |
| 7094 | } |
| 7095 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7096 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7097 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7098 | { |
| 7099 | sp<EffectModule> effect; |
| 7100 | size_t size = mEffects.size(); |
| 7101 | |
| 7102 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7103 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 7104 | if (id == 0 || mEffects[i]->id() == id) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7105 | effect = mEffects[i]; |
| 7106 | break; |
| 7107 | } |
| 7108 | } |
| 7109 | return effect; |
| 7110 | } |
| 7111 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7112 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 7113 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 7114 | const effect_uuid_t *type) |
| 7115 | { |
| 7116 | sp<EffectModule> effect; |
| 7117 | size_t size = mEffects.size(); |
| 7118 | |
| 7119 | for (size_t i = 0; i < size; i++) { |
| 7120 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 7121 | effect = mEffects[i]; |
| 7122 | break; |
| 7123 | } |
| 7124 | } |
| 7125 | return effect; |
| 7126 | } |
| 7127 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7128 | // Must be called with EffectChain::mLock locked |
| 7129 | void AudioFlinger::EffectChain::process_l() |
| 7130 | { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7131 | sp<ThreadBase> thread = mThread.promote(); |
| 7132 | if (thread == 0) { |
| 7133 | LOGW("process_l(): cannot promote mixer thread"); |
| 7134 | return; |
| 7135 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7136 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 7137 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7138 | // always process effects unless no more tracks are on the session and the effect tail |
| 7139 | // has been rendered |
| 7140 | bool doProcess = true; |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7141 | if (!isGlobalSession) { |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7142 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | b469b94 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7143 | |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7144 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 7145 | doProcess = false; |
| 7146 | } |
| 7147 | |
| 7148 | if (activeTrackCnt() == 0) { |
| 7149 | // if no track is active and the effect tail has not been rendered, |
| 7150 | // the input buffer must be cleared here as the mixer process will not do it |
| 7151 | if (tracksOnSession || mTailBufferCount > 0) { |
| 7152 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 7153 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 7154 | if (mTailBufferCount > 0) { |
| 7155 | mTailBufferCount--; |
| 7156 | } |
| 7157 | } |
| 7158 | } |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7159 | } |
| 7160 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7161 | size_t size = mEffects.size(); |
Eric Laurent | 544fe9b | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7162 | if (doProcess) { |
Eric Laurent | dac6911 | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7163 | for (size_t i = 0; i < size; i++) { |
| 7164 | mEffects[i]->process(); |
| 7165 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7166 | } |
| 7167 | for (size_t i = 0; i < size; i++) { |
| 7168 | mEffects[i]->updateState(); |
| 7169 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7170 | } |
| 7171 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7172 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7173 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7174 | { |
| 7175 | effect_descriptor_t desc = effect->desc(); |
| 7176 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 7177 | |
| 7178 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7179 | effect->setChain(this); |
| 7180 | sp<ThreadBase> thread = mThread.promote(); |
| 7181 | if (thread == 0) { |
| 7182 | return NO_INIT; |
| 7183 | } |
| 7184 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7185 | |
| 7186 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7187 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 7188 | // they are processed first and accumulated in chain input buffer |
| 7189 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7190 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7191 | // the input buffer for auxiliary effect contains mono samples in |
| 7192 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 7193 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 7194 | // calling the process in effect engine |
| 7195 | size_t numSamples = thread->frameCount(); |
| 7196 | int32_t *buffer = new int32_t[numSamples]; |
| 7197 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 7198 | effect->setInBuffer((int16_t *)buffer); |
| 7199 | // auxiliary effects output samples to chain input buffer for further processing |
| 7200 | // by insert effects |
| 7201 | effect->setOutBuffer(mInBuffer); |
| 7202 | } else { |
| 7203 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 7204 | // after track and auxiliary effects. |
| 7205 | // Insert effect order as a function of indicated preference: |
| 7206 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 7207 | // another effect is present |
| 7208 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 7209 | // last effect claiming first position |
| 7210 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 7211 | // first effect claiming last position |
| 7212 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 7213 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 7214 | // already present |
| 7215 | |
| 7216 | int size = (int)mEffects.size(); |
| 7217 | int idx_insert = size; |
| 7218 | int idx_insert_first = -1; |
| 7219 | int idx_insert_last = -1; |
| 7220 | |
| 7221 | for (int i = 0; i < size; i++) { |
| 7222 | effect_descriptor_t d = mEffects[i]->desc(); |
| 7223 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 7224 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 7225 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 7226 | // check invalid effect chaining combinations |
| 7227 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 7228 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7229 | LOGW("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] | 7230 | return INVALID_OPERATION; |
| 7231 | } |
| 7232 | // remember position of first insert effect and by default |
| 7233 | // select this as insert position for new effect |
| 7234 | if (idx_insert == size) { |
| 7235 | idx_insert = i; |
| 7236 | } |
| 7237 | // remember position of last insert effect claiming |
| 7238 | // first position |
| 7239 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 7240 | idx_insert_first = i; |
| 7241 | } |
| 7242 | // remember position of first insert effect claiming |
| 7243 | // last position |
| 7244 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 7245 | idx_insert_last == -1) { |
| 7246 | idx_insert_last = i; |
| 7247 | } |
| 7248 | } |
| 7249 | } |
| 7250 | |
| 7251 | // modify idx_insert from first position if needed |
| 7252 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 7253 | if (idx_insert_last != -1) { |
| 7254 | idx_insert = idx_insert_last; |
| 7255 | } else { |
| 7256 | idx_insert = size; |
| 7257 | } |
| 7258 | } else { |
| 7259 | if (idx_insert_first != -1) { |
| 7260 | idx_insert = idx_insert_first + 1; |
| 7261 | } |
| 7262 | } |
| 7263 | |
| 7264 | // always read samples from chain input buffer |
| 7265 | effect->setInBuffer(mInBuffer); |
| 7266 | |
| 7267 | // if last effect in the chain, output samples to chain |
| 7268 | // output buffer, otherwise to chain input buffer |
| 7269 | if (idx_insert == size) { |
| 7270 | if (idx_insert != 0) { |
| 7271 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 7272 | mEffects[idx_insert-1]->configure(); |
| 7273 | } |
| 7274 | effect->setOutBuffer(mOutBuffer); |
| 7275 | } else { |
| 7276 | effect->setOutBuffer(mInBuffer); |
| 7277 | } |
| 7278 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7279 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7280 | 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] | 7281 | } |
| 7282 | effect->configure(); |
| 7283 | return NO_ERROR; |
| 7284 | } |
| 7285 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7286 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 7287 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7288 | { |
| 7289 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7290 | int size = (int)mEffects.size(); |
| 7291 | int i; |
| 7292 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 7293 | |
| 7294 | for (i = 0; i < size; i++) { |
| 7295 | if (effect == mEffects[i]) { |
Eric Laurent | ec437d8 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7296 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 7297 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 7298 | // the middle of a read from audio HAL |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7299 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 7300 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 7301 | mEffects[i]->stop(); |
| 7302 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7303 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7304 | delete[] effect->inBuffer(); |
| 7305 | } else { |
| 7306 | if (i == size - 1 && i != 0) { |
| 7307 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 7308 | mEffects[i - 1]->configure(); |
| 7309 | } |
| 7310 | } |
| 7311 | mEffects.removeAt(i); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7312 | 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] | 7313 | break; |
| 7314 | } |
| 7315 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7316 | |
| 7317 | return mEffects.size(); |
| 7318 | } |
| 7319 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7320 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 7321 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7322 | { |
| 7323 | size_t size = mEffects.size(); |
| 7324 | for (size_t i = 0; i < size; i++) { |
| 7325 | mEffects[i]->setDevice(device); |
| 7326 | } |
| 7327 | } |
| 7328 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7329 | // setMode_l() must be called with PlaybackThread::mLock held |
| 7330 | void AudioFlinger::EffectChain::setMode_l(uint32_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7331 | { |
| 7332 | size_t size = mEffects.size(); |
| 7333 | for (size_t i = 0; i < size; i++) { |
| 7334 | mEffects[i]->setMode(mode); |
| 7335 | } |
| 7336 | } |
| 7337 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7338 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 7339 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7340 | { |
| 7341 | uint32_t newLeft = *left; |
| 7342 | uint32_t newRight = *right; |
| 7343 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7344 | int ctrlIdx = -1; |
| 7345 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7346 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7347 | // first update volume controller |
| 7348 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | 8f45bd7 | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7349 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7350 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 7351 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7352 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7353 | break; |
| 7354 | } |
| 7355 | } |
| 7356 | |
| 7357 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7358 | if (hasControl) { |
| 7359 | *left = mNewLeftVolume; |
| 7360 | *right = mNewRightVolume; |
| 7361 | } |
| 7362 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7366 | mLeftVolume = newLeft; |
| 7367 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7368 | |
| 7369 | // second get volume update from volume controller |
| 7370 | if (ctrlIdx >= 0) { |
| 7371 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7372 | mNewLeftVolume = newLeft; |
| 7373 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7374 | } |
| 7375 | // then indicate volume to all other effects in chain. |
| 7376 | // Pass altered volume to effects before volume controller |
| 7377 | // and requested volume to effects after controller |
| 7378 | uint32_t lVol = newLeft; |
| 7379 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7380 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7381 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7382 | if ((int)i == ctrlIdx) continue; |
| 7383 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 7384 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7385 | lVol = *left; |
| 7386 | rVol = *right; |
| 7387 | } |
| 7388 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 7389 | } |
| 7390 | *left = newLeft; |
| 7391 | *right = newRight; |
| 7392 | |
| 7393 | return hasControl; |
| 7394 | } |
| 7395 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7396 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 7397 | { |
| 7398 | const size_t SIZE = 256; |
| 7399 | char buffer[SIZE]; |
| 7400 | String8 result; |
| 7401 | |
| 7402 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 7403 | result.append(buffer); |
| 7404 | |
| 7405 | bool locked = tryLock(mLock); |
| 7406 | // failed to lock - AudioFlinger is probably deadlocked |
| 7407 | if (!locked) { |
| 7408 | result.append("\tCould not lock mutex:\n"); |
| 7409 | } |
| 7410 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7411 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 7412 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7413 | mEffects.size(), |
| 7414 | (uint32_t)mInBuffer, |
| 7415 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7416 | mActiveTrackCnt); |
| 7417 | result.append(buffer); |
| 7418 | write(fd, result.string(), result.size()); |
| 7419 | |
| 7420 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 7421 | sp<EffectModule> effect = mEffects[i]; |
| 7422 | if (effect != 0) { |
| 7423 | effect->dump(fd, args); |
| 7424 | } |
| 7425 | } |
| 7426 | |
| 7427 | if (locked) { |
| 7428 | mLock.unlock(); |
| 7429 | } |
| 7430 | |
| 7431 | return NO_ERROR; |
| 7432 | } |
| 7433 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7434 | // must be called with ThreadBase::mLock held |
| 7435 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 7436 | const effect_uuid_t *type, bool suspend) |
| 7437 | { |
| 7438 | sp<SuspendedEffectDesc> desc; |
| 7439 | // use effect type UUID timelow as key as there is no real risk of identical |
| 7440 | // timeLow fields among effect type UUIDs. |
| 7441 | int index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 7442 | if (suspend) { |
| 7443 | if (index >= 0) { |
| 7444 | desc = mSuspendedEffects.valueAt(index); |
| 7445 | } else { |
| 7446 | desc = new SuspendedEffectDesc(); |
| 7447 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 7448 | mSuspendedEffects.add(type->timeLow, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7449 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7450 | } |
| 7451 | if (desc->mRefCount++ == 0) { |
| 7452 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 7453 | if (effect != 0) { |
| 7454 | desc->mEffect = effect; |
| 7455 | effect->setSuspended(true); |
| 7456 | effect->setEnabled(false); |
| 7457 | } |
| 7458 | } |
| 7459 | } else { |
| 7460 | if (index < 0) { |
| 7461 | return; |
| 7462 | } |
| 7463 | desc = mSuspendedEffects.valueAt(index); |
| 7464 | if (desc->mRefCount <= 0) { |
| 7465 | LOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 7466 | desc->mRefCount = 1; |
| 7467 | } |
| 7468 | if (--desc->mRefCount == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7469 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7470 | if (desc->mEffect != 0) { |
| 7471 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 7472 | if (effect != 0) { |
| 7473 | effect->setSuspended(false); |
| 7474 | sp<EffectHandle> handle = effect->controlHandle(); |
| 7475 | if (handle != 0) { |
| 7476 | effect->setEnabled(handle->enabled()); |
| 7477 | } |
| 7478 | } |
| 7479 | desc->mEffect.clear(); |
| 7480 | } |
| 7481 | mSuspendedEffects.removeItemsAt(index); |
| 7482 | } |
| 7483 | } |
| 7484 | } |
| 7485 | |
| 7486 | // must be called with ThreadBase::mLock held |
| 7487 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 7488 | { |
| 7489 | sp<SuspendedEffectDesc> desc; |
| 7490 | |
| 7491 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7492 | if (suspend) { |
| 7493 | if (index >= 0) { |
| 7494 | desc = mSuspendedEffects.valueAt(index); |
| 7495 | } else { |
| 7496 | desc = new SuspendedEffectDesc(); |
| 7497 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7498 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7499 | } |
| 7500 | if (desc->mRefCount++ == 0) { |
| 7501 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); |
| 7502 | for (size_t i = 0; i < effects.size(); i++) { |
| 7503 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 7504 | } |
| 7505 | } |
| 7506 | } else { |
| 7507 | if (index < 0) { |
| 7508 | return; |
| 7509 | } |
| 7510 | desc = mSuspendedEffects.valueAt(index); |
| 7511 | if (desc->mRefCount <= 0) { |
| 7512 | LOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 7513 | desc->mRefCount = 1; |
| 7514 | } |
| 7515 | if (--desc->mRefCount == 0) { |
| 7516 | Vector<const effect_uuid_t *> types; |
| 7517 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 7518 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 7519 | continue; |
| 7520 | } |
| 7521 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 7522 | } |
| 7523 | for (size_t i = 0; i < types.size(); i++) { |
| 7524 | setEffectSuspended_l(types[i], false); |
| 7525 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7526 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7527 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 7528 | } |
| 7529 | } |
| 7530 | } |
| 7531 | |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7532 | |
| 7533 | // The volume effect is used for automated tests only |
| 7534 | #ifndef OPENSL_ES_H_ |
| 7535 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 7536 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 7537 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 7538 | #endif //OPENSL_ES_H_ |
| 7539 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7540 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 7541 | { |
| 7542 | // auxiliary effects and visualizer are never suspended on output mix |
| 7543 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 7544 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 6bffdb8 | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7545 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 7546 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7547 | return false; |
| 7548 | } |
| 7549 | return true; |
| 7550 | } |
| 7551 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7552 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() |
| 7553 | { |
| 7554 | Vector< sp<EffectModule> > effects; |
| 7555 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7556 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7557 | continue; |
| 7558 | } |
| 7559 | effects.add(mEffects[i]); |
| 7560 | } |
| 7561 | return effects; |
| 7562 | } |
| 7563 | |
| 7564 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 7565 | const effect_uuid_t *type) |
| 7566 | { |
| 7567 | sp<EffectModule> effect; |
| 7568 | effect = getEffectFromType_l(type); |
| 7569 | if (effect != 0 && !effect->isEnabled()) { |
| 7570 | effect.clear(); |
| 7571 | } |
| 7572 | return effect; |
| 7573 | } |
| 7574 | |
| 7575 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 7576 | bool enabled) |
| 7577 | { |
| 7578 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 7579 | if (enabled) { |
| 7580 | if (index < 0) { |
| 7581 | // if the effect is not suspend check if all effects are suspended |
| 7582 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7583 | if (index < 0) { |
| 7584 | return; |
| 7585 | } |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7586 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 7587 | return; |
| 7588 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7589 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 7590 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7591 | if (index < 0) { |
| 7592 | LOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
| 7593 | return; |
| 7594 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7595 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7596 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7597 | effect->desc().type.timeLow); |
| 7598 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7599 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 7600 | if (desc->mEffect == 0) { |
| 7601 | desc->mEffect = effect; |
| 7602 | effect->setEnabled(false); |
| 7603 | effect->setSuspended(true); |
| 7604 | } |
| 7605 | } else { |
| 7606 | if (index < 0) { |
| 7607 | return; |
| 7608 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 7609 | ALOGV("checkSuspendOnEffectEnabled() disable restoring 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 | desc->mEffect.clear(); |
| 7613 | effect->setSuspended(false); |
| 7614 | } |
| 7615 | } |
| 7616 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7617 | #undef LOG_TAG |
| 7618 | #define LOG_TAG "AudioFlinger" |
| 7619 | |
| 7620 | // ---------------------------------------------------------------------------- |
| 7621 | |
| 7622 | status_t AudioFlinger::onTransact( |
| 7623 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7624 | { |
| 7625 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 7626 | } |
| 7627 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 7628 | }; // namespace android |