The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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" |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/Parcel.h> |
| 31 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | #include <utils/String16.h> |
| 33 | #include <utils/threads.h> |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 34 | #include <utils/Atomic.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | #include <cutils/bitops.h> |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 37 | #include <cutils/properties.h> |
| 38 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | #include <media/AudioTrack.h> |
| 40 | #include <media/AudioRecord.h> |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 41 | #include <media/IMediaPlayerService.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
| 43 | #include <private/media/AudioTrackShared.h> |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 44 | #include <private/media/AudioEffectShared.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 45 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 47 | #include <hardware/audio.h> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
| 49 | #include "AudioMixer.h" |
| 50 | #include "AudioFlinger.h" |
| 51 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 52 | #include <media/EffectsFactoryApi.h> |
Eric Laurent | 5cc0526 | 2011-06-24 07:01:31 -0700 | [diff] [blame] | 53 | #include <audio_effects/effect_visualizer.h> |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 54 | #include <audio_effects/effect_ns.h> |
| 55 | #include <audio_effects/effect_aec.h> |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 56 | |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 57 | #include <cpustats/ThreadCpuUsage.h> |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 58 | #include <powermanager/PowerManager.h> |
Glenn Kasten | da494f9 | 2011-07-08 15:26:12 -0700 | [diff] [blame] | 59 | // #define DEBUG_CPU_USAGE 10 // log statistics every n wall clock seconds |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 63 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | namespace android { |
| 65 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 66 | static const char* kDeadlockedString = "AudioFlinger may be deadlocked\n"; |
| 67 | static const char* kHardwareLockedString = "Hardware lock is taken\n"; |
| 68 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 69 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | static const float MAX_GAIN = 4096.0f; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 71 | static const float MAX_GAIN_INT = 0x1000; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 82 | static const int kDumpLockRetries = 50; |
| 83 | static const int kDumpLockSleep = 20000; |
| 84 | |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 85 | static const nsecs_t kWarningThrottle = seconds(5); |
| 86 | |
Eric Laurent | 464d5b3 | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 90 | static const nsecs_t kSetParametersTimeout = seconds(2); |
| 91 | |
Eric Laurent | f1f5fc8 | 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 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | // ---------------------------------------------------------------------------- |
| 99 | |
| 100 | static bool recordingAllowed() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static bool settingsAllowed() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Gloria Wang | 9b3f152 | 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 | 31f18889 | 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 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 157 | // ---------------------------------------------------------------------------- |
| 158 | |
| 159 | AudioFlinger::AudioFlinger() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | : BnAudioFlinger(), |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 161 | mPrimaryHardwareDev(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1), |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 162 | mBtNrecIsOff(false) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | { |
Dima Zavin | 2986f5b | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void AudioFlinger::onFirstRef() |
| 167 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 168 | int rc = 0; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 169 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 170 | Mutex::Autolock _l(mLock); |
| 171 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 172 | /* TODO: move all this work into an Init() function */ |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 174 | |
Dima Zavin | 31f18889 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 178 | |
Dima Zavin | 31f18889 | 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 | 2986f5b | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 190 | mod->name, mod->id, audio_interfaces[i]); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 193 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | mHardwareStatus = AUDIO_HW_INIT; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 195 | |
Dima Zavin | 31f18889 | 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 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Dima Zavin | 2986f5b | 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 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | AudioFlinger::~AudioFlinger() |
| 228 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 229 | int num_devs = mAudioHwDevs.size(); |
| 230 | |
Eric Laurent | 7954c46 | 2009-08-28 10:39:03 -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 | 31f18889 | 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); |
Eric Laurent | 7954c46 | 2009-08-28 10:39:03 -0700 | [diff] [blame] | 243 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 244 | mAudioHwDevs.clear(); |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 245 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 246 | |
Dima Zavin | 31f18889 | 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 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 257 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | c74b93f | 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 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | write(fd, result.string(), result.size()); |
| 284 | return NO_ERROR; |
| 285 | } |
| 286 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 293 | int hardwareStatus = mHardwareStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 294 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 295 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 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()); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 348 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 349 | |
| 350 | dumpClients(fd, args); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 351 | dumpInternals(fd, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 353 | // dump playback threads |
| 354 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 355 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // dump record threads |
Eric Laurent | 102313a | 2009-07-23 13:35:01 -0700 | [diff] [blame] | 359 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 360 | mRecordThreads.valueAt(i)->dump(fd, args); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
Dima Zavin | 31f18889 | 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); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 368 | if (locked) mLock.unlock(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | } |
| 370 | return NO_ERROR; |
| 371 | } |
| 372 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 373 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 374 | // IAudioFlinger interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | |
| 376 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 377 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 378 | pid_t pid, |
| 379 | int streamType, |
| 380 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 381 | uint32_t format, |
| 382 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 383 | int frameCount, |
| 384 | uint32_t flags, |
| 385 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 386 | int output, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 387 | int *sessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 388 | status_t *status) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 389 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 390 | sp<PlaybackThread::Track> track; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | sp<TrackHandle> trackHandle; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 392 | sp<Client> client; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 393 | wp<Client> wclient; |
| 394 | status_t lStatus; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 395 | int lSessionId; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 397 | if (streamType >= AUDIO_STREAM_CNT) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 398 | LOGE("invalid stream type"); |
| 399 | lStatus = BAD_VALUE; |
| 400 | goto Exit; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 401 | } |
| 402 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 403 | { |
| 404 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 405 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 406 | PlaybackThread *effectThread = NULL; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 407 | if (thread == NULL) { |
| 408 | LOGE("unknown output thread"); |
| 409 | lStatus = BAD_VALUE; |
| 410 | goto Exit; |
| 411 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 412 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 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 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 421 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 422 | LOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 423 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 424 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 493941b | 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 | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 439 | lSessionId = *sessionId; |
| 440 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 441 | // if no audio session id is provided, create one here |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 442 | lSessionId = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 443 | if (sessionId != NULL) { |
| 444 | *sessionId = lSessionId; |
| 445 | } |
| 446 | } |
| 447 | LOGV("createTrack() lSessionId: %d", lSessionId); |
| 448 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 449 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 450 | channelMask, frameCount, sharedBuffer, lSessionId, &lStatus); |
Eric Laurent | 493941b | 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 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 459 | } |
| 460 | if (lStatus == NO_ERROR) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 461 | trackHandle = new TrackHandle(track); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 462 | } else { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 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(); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 466 | track.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | Exit: |
| 470 | if(status) { |
| 471 | *status = lStatus; |
| 472 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | return trackHandle; |
| 474 | } |
| 475 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 476 | uint32_t AudioFlinger::sampleRate(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 477 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 478 | Mutex::Autolock _l(mLock); |
| 479 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 480 | if (thread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 481 | LOGW("sampleRate() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | return thread->sampleRate(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 487 | int AudioFlinger::channelCount(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 489 | Mutex::Autolock _l(mLock); |
| 490 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 491 | if (thread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 492 | LOGW("channelCount() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | return thread->channelCount(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 498 | uint32_t AudioFlinger::format(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 500 | Mutex::Autolock _l(mLock); |
| 501 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 502 | if (thread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 503 | LOGW("format() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | return thread->format(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 509 | size_t AudioFlinger::frameCount(int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 510 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 511 | Mutex::Autolock _l(mLock); |
| 512 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 513 | if (thread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 514 | LOGW("frameCount() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | return thread->frameCount(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 520 | uint32_t AudioFlinger::latency(int output) const |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 521 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 522 | Mutex::Autolock _l(mLock); |
| 523 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 524 | if (thread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 525 | LOGW("latency() unknown thread %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | return thread->latency(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 529 | } |
| 530 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 531 | status_t AudioFlinger::setMasterVolume(float value) |
| 532 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 533 | status_t ret = initCheck(); |
| 534 | if (ret != NO_ERROR) { |
| 535 | return ret; |
| 536 | } |
| 537 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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 | 0163594 | 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 | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 547 | if (mPrimaryHardwareDev->set_master_volume(mPrimaryHardwareDev, value) == NO_ERROR) { |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 548 | value = 1.0f; |
| 549 | } |
| 550 | mHardwareStatus = AUDIO_HW_IDLE; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 551 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 552 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 553 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 554 | mMasterVolume = value; |
| 555 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 556 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 557 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 558 | return NO_ERROR; |
| 559 | } |
| 560 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 561 | status_t AudioFlinger::setMode(int mode) |
| 562 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 563 | status_t ret = initCheck(); |
| 564 | if (ret != NO_ERROR) { |
| 565 | return ret; |
| 566 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 567 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 568 | // check calling permissions |
| 569 | if (!settingsAllowed()) { |
| 570 | return PERMISSION_DENIED; |
| 571 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 572 | if ((mode < 0) || (mode >= AUDIO_MODE_CNT)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 573 | LOGW("Illegal value: setMode(%d)", mode); |
| 574 | return BAD_VALUE; |
| 575 | } |
| 576 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 577 | { // scope for the lock |
| 578 | AutoMutex lock(mHardwareLock); |
| 579 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 580 | ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 581 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 582 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 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); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 589 | } |
| 590 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | return ret; |
| 592 | } |
| 593 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 594 | status_t AudioFlinger::setMicMute(bool state) |
| 595 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 596 | status_t ret = initCheck(); |
| 597 | if (ret != NO_ERROR) { |
| 598 | return ret; |
| 599 | } |
| 600 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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 | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 608 | ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 609 | mHardwareStatus = AUDIO_HW_IDLE; |
| 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | bool AudioFlinger::getMicMute() const |
| 614 | { |
Eric Laurent | 8b4dbf7 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 620 | bool state = AUDIO_MODE_INVALID; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 621 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 622 | mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -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 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 633 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 634 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 635 | mMasterMute = muted; |
| 636 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 637 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 638 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 639 | return NO_ERROR; |
| 640 | } |
| 641 | |
| 642 | float AudioFlinger::masterVolume() const |
| 643 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 644 | return mMasterVolume; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | bool AudioFlinger::masterMute() const |
| 648 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 649 | return mMasterMute; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 652 | status_t AudioFlinger::setStreamVolume(int stream, float value, int output) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 653 | { |
| 654 | // check calling permissions |
| 655 | if (!settingsAllowed()) { |
| 656 | return PERMISSION_DENIED; |
| 657 | } |
| 658 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 659 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 660 | return BAD_VALUE; |
| 661 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 662 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 672 | mStreamTypes[stream].volume = value; |
| 673 | |
| 674 | if (thread == NULL) { |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 675 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 676 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 677 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 678 | } else { |
| 679 | thread->setStreamVolume(stream, value); |
| 680 | } |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 681 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 682 | return NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | 24fc2fb | 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) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 694 | return BAD_VALUE; |
| 695 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 696 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 697 | AutoMutex lock(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 698 | mStreamTypes[stream].mute = muted; |
| 699 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 700 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 702 | return NO_ERROR; |
| 703 | } |
| 704 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 705 | float AudioFlinger::streamVolume(int stream, int output) const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 706 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 707 | if (stream < 0 || uint32_t(stream) >= AUDIO_STREAM_CNT) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 708 | return 0.0f; |
| 709 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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 | |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 723 | return volume; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | bool AudioFlinger::streamMute(int stream) const |
| 727 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 728 | if (stream < 0 || stream >= (int)AUDIO_STREAM_CNT) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 729 | return true; |
| 730 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 731 | |
| 732 | return mStreamTypes[stream].mute; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 735 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 736 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 737 | status_t result; |
| 738 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 739 | LOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 740 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 741 | // check calling permissions |
| 742 | if (!settingsAllowed()) { |
| 743 | return PERMISSION_DENIED; |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 744 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 745 | |
| 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 | 31f18889 | 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 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 756 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 6639b55 | 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 | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 762 | bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); |
| 763 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 6639b55 | 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 | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 770 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
Eric Laurent | 6639b55 | 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 | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 779 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 782 | return final_result; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 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 | 464d5b3 | 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 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 802 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 803 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 804 | if (thread != NULL) { |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 805 | result = thread->setParameters(keyValuePairs); |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 806 | return result; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 807 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 808 | return BAD_VALUE; |
| 809 | } |
| 810 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 811 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 812 | { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 813 | // LOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 814 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 815 | |
| 816 | if (ioHandle == 0) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 817 | String8 out_s8; |
| 818 | |
Dima Zavin | 31f18889 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 825 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 826 | } |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 827 | |
| 828 | Mutex::Autolock _l(mLock); |
| 829 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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(""); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 839 | } |
| 840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) |
| 842 | { |
Eric Laurent | 8b4dbf7 | 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 | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 848 | return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | } |
| 850 | |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 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 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 866 | status_t AudioFlinger::setVoiceVolume(float value) |
| 867 | { |
Eric Laurent | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 868 | status_t ret = initCheck(); |
| 869 | if (ret != NO_ERROR) { |
| 870 | return ret; |
| 871 | } |
| 872 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -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 | 8b4dbf7 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 880 | ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 881 | mHardwareStatus = AUDIO_HW_IDLE; |
| 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 901 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 902 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | Mutex::Autolock _l(mLock); |
| 904 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 905 | int pid = IPCThreadState::self()->getCallingPid(); |
| 906 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 907 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 908 | client, |
| 909 | pid); |
| 910 | LOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 911 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 912 | mNotificationClients.add(pid, notificationClient); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 913 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 914 | sp<IBinder> binder = client->asBinder(); |
| 915 | binder->linkToDeath(notificationClient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 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 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 922 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 923 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 924 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 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); |
| 936 | LOGV("removeNotificationClient() %p, pid %d", client.get(), pid); |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 937 | mNotificationClients.removeItem(pid); |
| 938 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 939 | |
| 940 | LOGV("%d died, releasing its sessions", pid); |
| 941 | int num = mAudioSessionRefs.size(); |
| 942 | bool removed = false; |
| 943 | for (int i = 0; i< num; i++) { |
| 944 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 945 | LOGV(" pid %d @ %d", ref->pid, i); |
| 946 | if (ref->pid == pid) { |
| 947 | LOGV(" removing entry for pid %d session %d", pid, ref->sessionid); |
| 948 | mAudioSessionRefs.removeAt(i); |
| 949 | delete ref; |
| 950 | removed = true; |
| 951 | i--; |
| 952 | num--; |
| 953 | } |
| 954 | } |
| 955 | if (removed) { |
| 956 | purgeStaleEffects_l(); |
| 957 | } |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 960 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 961 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) |
| 962 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 963 | size_t size = mNotificationClients.size(); |
| 964 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 965 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 969 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 970 | void AudioFlinger::removeClient_l(pid_t pid) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 971 | { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 972 | LOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 973 | mClients.removeItem(pid); |
| 974 | } |
| 975 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 976 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 977 | // ---------------------------------------------------------------------------- |
| 978 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 979 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id, uint32_t device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 980 | : Thread(false), |
| 981 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 982 | mFrameSize(1), mFormat(0), mStandby(false), mId(id), mExiting(false), |
| 983 | mDevice(device) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 984 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 985 | mDeathRecipient = new PMDeathRecipient(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | } |
| 987 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 988 | AudioFlinger::ThreadBase::~ThreadBase() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | { |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 990 | mParamCond.broadcast(); |
| 991 | mNewParameters.clear(); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 992 | // do not lock the mutex in destructor |
| 993 | releaseWakeLock_l(); |
Eric Laurent | 4a64a6e | 2011-09-27 12:07:15 -0700 | [diff] [blame] | 994 | if (mPowerManager != 0) { |
| 995 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 996 | binder->unlinkToDeath(mDeathRecipient); |
| 997 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1000 | void AudioFlinger::ThreadBase::exit() |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1001 | { |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 1002 | // keep a strong ref on ourself so that we wont get |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1003 | // destroyed in the middle of requestExitAndWait() |
| 1004 | sp <ThreadBase> strongMe = this; |
| 1005 | |
| 1006 | LOGV("ThreadBase::exit"); |
| 1007 | { |
| 1008 | AutoMutex lock(&mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 1009 | mExiting = true; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1010 | requestExit(); |
| 1011 | mWaitWorkCV.signal(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1012 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1013 | requestExitAndWait(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1014 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1015 | |
| 1016 | uint32_t AudioFlinger::ThreadBase::sampleRate() const |
| 1017 | { |
| 1018 | return mSampleRate; |
| 1019 | } |
| 1020 | |
| 1021 | int AudioFlinger::ThreadBase::channelCount() const |
| 1022 | { |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 1023 | return (int)mChannelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1026 | uint32_t AudioFlinger::ThreadBase::format() const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 | { |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1038 | status_t status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1039 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1040 | LOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1041 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1042 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1043 | mNewParameters.add(keyValuePairs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1044 | mWaitWorkCV.signal(); |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 1045 | // wait condition with timeout in case the thread loop has exited |
| 1046 | // before the request could be processed |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 1047 | if (mParamCond.waitRelative(mLock, kSetParametersTimeout) == NO_ERROR) { |
Eric Laurent | b7d9460 | 2009-09-29 11:12:57 -0700 | [diff] [blame] | 1048 | status = mParamStatus; |
| 1049 | mWaitWorkCV.signal(); |
| 1050 | } else { |
| 1051 | status = TIMED_OUT; |
| 1052 | } |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 1053 | return status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 1057 | { |
| 1058 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 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 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1065 | ConfigEvent *configEvent = new ConfigEvent(); |
| 1066 | configEvent->mEvent = event; |
| 1067 | configEvent->mParam = param; |
| 1068 | mConfigEvents.add(configEvent); |
| 1069 | LOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
| 1070 | mWaitWorkCV.signal(); |
| 1071 | } |
| 1072 | |
| 1073 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 1074 | { |
| 1075 | mLock.lock(); |
| 1076 | while(!mConfigEvents.isEmpty()) { |
| 1077 | LOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
| 1078 | ConfigEvent *configEvent = mConfigEvents[0]; |
| 1079 | mConfigEvents.removeAt(0); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1080 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 1081 | // AudioFlinger then ThreadBase to avoid cross deadlock |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1082 | mLock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1083 | mAudioFlinger->mLock.lock(); |
| 1084 | audioConfigChanged_l(configEvent->mEvent, configEvent->mParam); |
| 1085 | mAudioFlinger->mLock.unlock(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1086 | delete configEvent; |
| 1087 | mLock.lock(); |
| 1088 | } |
| 1089 | mLock.unlock(); |
| 1090 | } |
| 1091 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 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 | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1112 | snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); |
| 1113 | result.append(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [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 | 1345d33 | 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 | 6dbdc40 | 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 | } |
| 1191 | LOGV("acquireWakeLock_l() %s status %d", mName, status); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1196 | { |
| 1197 | Mutex::Autolock _l(mLock); |
Eric Laurent | d49ead6 | 2011-07-28 13:59:02 -0700 | [diff] [blame] | 1198 | releaseWakeLock_l(); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1202 | { |
| 1203 | if (mWakeLockToken != 0) { |
| 1204 | LOGV("releaseWakeLock_l() %s", mName); |
| 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 | 1345d33 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 1227 | |
Eric Laurent | f82fccd | 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 { |
| 1267 | LOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1268 | desc->mType.timeLow); |
| 1269 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
Eric Laurent | f82fccd | 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); |
| 1313 | LOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1314 | } |
| 1315 | desc->mRefCount++; |
| 1316 | } else { |
| 1317 | if (index < 0) { |
| 1318 | return; |
| 1319 | } |
| 1320 | desc = sessionEffects.valueAt(index); |
| 1321 | if (--desc->mRefCount == 0) { |
| 1322 | LOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1323 | sessionEffects.removeItemsAt(index); |
| 1324 | if (sessionEffects.isEmpty()) { |
| 1325 | LOGV("updateSuspendedSessions_l() restore removing session %d", |
| 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 | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 1341 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1342 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 1343 | |
Eric Laurent | 7fa1cee | 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 | 6752ec8 | 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 | f82fccd | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | // ---------------------------------------------------------------------------- |
| 1364 | |
Eric Laurent | 464d5b3 | 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), |
Eric Laurent | d5603c1 | 2009-08-06 08:49:39 -0700 | [diff] [blame] | 1370 | mMixBuffer(0), mSuspended(0), mBytesWritten(0), mOutput(output), |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1371 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1373 | snprintf(mName, kNameLength, "AudioOut_%d", id); |
| 1374 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1375 | readOutputParameters(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1377 | mMasterVolume = mAudioFlinger->masterVolume(); |
| 1378 | mMasterMute = mAudioFlinger->masterMute(); |
| 1379 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1380 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1381 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); |
| 1382 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1383 | mStreamTypes[stream].valid = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1387 | AudioFlinger::PlaybackThread::~PlaybackThread() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1388 | { |
| 1389 | delete [] mMixBuffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1392 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | { |
| 1394 | dumpInternals(fd, args); |
| 1395 | dumpTracks(fd, args); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1396 | dumpEffectChains(fd, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | return NO_ERROR; |
| 1398 | } |
| 1399 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1400 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | { |
| 1402 | const size_t SIZE = 256; |
| 1403 | char buffer[SIZE]; |
| 1404 | String8 result; |
| 1405 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1406 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | result.append(buffer); |
Jean-Michel Trivi | 5439223 | 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"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | for (size_t i = 0; i < mTracks.size(); ++i) { |
Eric Laurent | d3b4d0c | 2009-03-31 14:34:35 -0700 | [diff] [blame] | 1410 | sp<Track> track = mTracks[i]; |
| 1411 | if (track != 0) { |
| 1412 | track->dump(buffer, SIZE); |
| 1413 | result.append(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1417 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | result.append(buffer); |
Jean-Michel Trivi | 5439223 | 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"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1420 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
Eric Laurent | d3b4d0c | 2009-03-31 14:34:35 -0700 | [diff] [blame] | 1421 | wp<Track> wTrack = mActiveTracks[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1434 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | { |
| 1436 | const size_t SIZE = 256; |
| 1437 | char buffer[SIZE]; |
| 1438 | String8 result; |
| 1439 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1440 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 1450 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1451 | result.append(buffer); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1452 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1453 | result.append(buffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | write(fd, result.string(), result.size()); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1455 | |
| 1456 | dumpBase(fd, args); |
| 1457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | return NO_ERROR; |
| 1459 | } |
| 1460 | |
| 1461 | // Thread virtuals |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1462 | status_t AudioFlinger::PlaybackThread::readyToRun() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | { |
Eric Laurent | 828b977 | 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 { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | LOGE("No working audio driver found."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | } |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1470 | return status; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1473 | void AudioFlinger::PlaybackThread::onFirstRef() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1475 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1476 | } |
| 1477 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1478 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1479 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | const sp<AudioFlinger::Client>& client, |
| 1481 | int streamType, |
| 1482 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1483 | uint32_t format, |
| 1484 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | int frameCount, |
| 1486 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1487 | int sessionId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | status_t *status) |
| 1489 | { |
| 1490 | sp<Track> track; |
| 1491 | status_t lStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1492 | |
| 1493 | if (mType == DIRECT) { |
Jean-Michel Trivi | 5439223 | 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 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1512 | lStatus = initCheck(); |
| 1513 | if (lStatus != NO_ERROR) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1514 | LOGE("Audio driver not initialized."); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1515 | goto Exit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1518 | { // scope for mLock |
| 1519 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1525 | AudioSystem::getStrategyForStream((audio_stream_type_t)streamType); |
Eric Laurent | 8ed6ed0 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1530 | strategy != AudioSystem::getStrategyForStream((audio_stream_type_t)t->type())) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1531 | lStatus = BAD_VALUE; |
| 1532 | goto Exit; |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1537 | track = new Track(this, client, streamType, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1538 | channelMask, frameCount, sharedBuffer, sessionId); |
Eric Laurent | 73b6035 | 2009-11-09 04:45:39 -0800 | [diff] [blame] | 1539 | if (track->getCblk() == NULL || track->name() < 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1540 | lStatus = NO_MEMORY; |
| 1541 | goto Exit; |
| 1542 | } |
| 1543 | mTracks.add(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1544 | |
| 1545 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1546 | if (chain != 0) { |
| 1547 | LOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1548 | track->setMainBuffer(chain->inBuffer()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1549 | chain->setStrategy(AudioSystem::getStrategyForStream((audio_stream_type_t)track->type())); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1550 | chain->incTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1551 | } |
Eric Laurent | 05ce094 | 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 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1560 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1561 | lStatus = NO_ERROR; |
| 1562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | Exit: |
| 1564 | if(status) { |
| 1565 | *status = lStatus; |
| 1566 | } |
| 1567 | return track; |
| 1568 | } |
| 1569 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1570 | uint32_t AudioFlinger::PlaybackThread::latency() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1571 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1572 | Mutex::Autolock _l(mLock); |
| 1573 | if (initCheck() == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1574 | return mOutput->stream->get_latency(mOutput->stream); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1575 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | return 0; |
| 1577 | } |
| 1578 | } |
| 1579 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1580 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | { |
| 1582 | mMasterVolume = value; |
| 1583 | return NO_ERROR; |
| 1584 | } |
| 1585 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1586 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | { |
| 1588 | mMasterMute = muted; |
| 1589 | return NO_ERROR; |
| 1590 | } |
| 1591 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1592 | float AudioFlinger::PlaybackThread::masterVolume() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | { |
| 1594 | return mMasterVolume; |
| 1595 | } |
| 1596 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1597 | bool AudioFlinger::PlaybackThread::masterMute() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | { |
| 1599 | return mMasterMute; |
| 1600 | } |
| 1601 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1602 | status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | { |
| 1604 | mStreamTypes[stream].volume = value; |
| 1605 | return NO_ERROR; |
| 1606 | } |
| 1607 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1608 | status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | { |
| 1610 | mStreamTypes[stream].mute = muted; |
| 1611 | return NO_ERROR; |
| 1612 | } |
| 1613 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1614 | float AudioFlinger::PlaybackThread::streamVolume(int stream) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | { |
| 1616 | return mStreamTypes[stream].volume; |
| 1617 | } |
| 1618 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1619 | bool AudioFlinger::PlaybackThread::streamMute(int stream) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | { |
| 1621 | return mStreamTypes[stream].mute; |
| 1622 | } |
| 1623 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1624 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1625 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | { |
| 1627 | status_t status = ALREADY_EXISTS; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1628 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1629 | // set retry count for buffer fill |
| 1630 | track->mRetryCount = kMaxTrackStartupRetries; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 1636 | track->mResetDone = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1637 | mActiveTracks.add(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1638 | if (track->mainBuffer() != mMixBuffer) { |
| 1639 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1640 | if (chain != 0) { |
| 1641 | LOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 1642 | chain->incActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | status = NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1647 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1648 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | LOGV("mWaitWorkCV.broadcast"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1650 | mWaitWorkCV.broadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | |
| 1652 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1655 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1656 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1657 | { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1658 | track->mState = TrackBase::TERMINATED; |
| 1659 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 90681d6 | 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(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1674 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1675 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1676 | String8 out_s8 = String8(""); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1677 | char *s; |
| 1678 | |
Eric Laurent | 828b977 | 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 | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1684 | s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1685 | out_s8 = String8(s); |
| 1686 | free(s); |
| 1687 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1688 | } |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1689 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1690 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1691 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1692 | AudioSystem::OutputDescriptor desc; |
| 1693 | void *param2 = 0; |
| 1694 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1695 | LOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1696 | |
| 1697 | switch (event) { |
| 1698 | case AudioSystem::OUTPUT_OPENED: |
| 1699 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1700 | desc.channels = mChannelMask; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 1714 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1718 | { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1719 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
Jean-Michel Trivi | 5439223 | 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 | 31f18889 | 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; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1725 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1726 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1727 | // Allocate a stereo buffer even if HW output is mono. |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1728 | if (mMixBuffer != NULL) delete[] mMixBuffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1729 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1730 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1731 | |
Eric Laurent | 8ed6ed0 | 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 | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1740 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1741 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 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 | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1749 | Mutex::Autolock _l(mLock); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1750 | if (initCheck() != NO_ERROR) { |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1751 | return INVALID_OPERATION; |
| 1752 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1753 | *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1754 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1755 | return mOutput->stream->get_render_position(mOutput->stream, dspFrames); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 1756 | } |
| 1757 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1758 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1759 | { |
| 1760 | Mutex::Autolock _l(mLock); |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1761 | uint32_t result = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1762 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1763 | result = EFFECT_SESSION; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1767 | sp<Track> track = mTracks[i]; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1768 | if (sessionId == track->sessionId() && |
| 1769 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1770 | result |= TRACK_SESSION; |
| 1771 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1775 | return result; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1778 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1779 | { |
Dima Zavin | 24fc2fb | 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 | 8ed6ed0 | 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 | 24fc2fb | 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 | 8ed6ed0 | 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 | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1789 | return AudioSystem::getStrategyForStream((audio_stream_type_t) track->type()); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1790 | } |
| 1791 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1792 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 1795 | |
Eric Laurent | 828b977 | 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 | 4433169 | 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 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1831 | // ---------------------------------------------------------------------------- |
| 1832 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1833 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1834 | : PlaybackThread(audioFlinger, output, id, device), |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 1835 | mAudioMixer(0), mPrevMixerStatus(MIXER_IDLE) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1836 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1837 | mType = ThreadBase::MIXER; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | } |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1846 | AudioFlinger::MixerThread::~MixerThread() |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 1847 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1848 | delete mAudioMixer; |
| 1849 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1850 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1851 | bool AudioFlinger::MixerThread::threadLoop() |
| 1852 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1853 | Vector< sp<Track> > tracksToRemove; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1854 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1855 | nsecs_t standbyTime = systemTime(); |
| 1856 | size_t mixBufferSize = mFrameCount * mFrameSize; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 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 | 276fa43 | 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; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 1862 | nsecs_t lastWarning = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1863 | bool longStandbyExit = false; |
| 1864 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1865 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1866 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1867 | uint32_t sleepTimeShift = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1868 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | da494f9 | 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 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1873 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1874 | acquireWakeLock(); |
| 1875 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1876 | while (!exitPending()) |
| 1877 | { |
Glenn Kasten | da494f9 | 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 |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1905 | processConfigEvents(); |
| 1906 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1907 | mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1908 | { // scope for mLock |
| 1909 | |
| 1910 | Mutex::Autolock _l(mLock); |
| 1911 | |
| 1912 | if (checkForNewParameters_l()) { |
| 1913 | mixBufferSize = mFrameCount * mFrameSize; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 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 | 276fa43 | 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; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1919 | activeSleepTime = activeSleepTimeUs(); |
| 1920 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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) { |
| 1929 | LOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1930 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1941 | releaseWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1942 | // wait until we have something to do... |
| 1943 | LOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); |
| 1944 | mWaitWorkCV.wait(mLock); |
| 1945 | LOGV("MixerThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 1946 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1947 | |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 1948 | mPrevMixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1949 | if (mMasterMute == false) { |
| 1950 | char value[PROPERTY_VALUE_MAX]; |
| 1951 | property_get("ro.audio.silent", value, "0"); |
| 1952 | if (atoi(value)) { |
| 1953 | LOGD("Silence is golden"); |
| 1954 | setMasterMute(true); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1959 | sleepTime = idleSleepTime; |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1960 | sleepTimeShift = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1961 | continue; |
| 1962 | } |
| 1963 | } |
| 1964 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1965 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1966 | |
| 1967 | // prevent any changes in effect chain list and in each effect chain |
| 1968 | // during mixing and effect process as the audio buffers could be deleted |
| 1969 | // or modified if an effect is created or deleted |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1970 | lockEffectChains_l(effectChains); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1973 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 1974 | // mix buffers... |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1975 | mAudioMixer->process(); |
Eric Laurent | 0609231 | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 1976 | // increase sleep time progressively when application underrun condition clears. |
| 1977 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 1978 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 1979 | // such that we would underrun the audio HAL. |
| 1980 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1981 | sleepTimeShift--; |
| 1982 | } |
Eric Laurent | 0609231 | 2012-01-23 18:56:59 -0800 | [diff] [blame] | 1983 | sleepTime = 0; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 1984 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1985 | //TODO: delay standby when effects have a tail |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 1986 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 1987 | // If no tracks are ready, sleep once for the duration of an output |
| 1988 | // buffer size, then write 0s to the output |
| 1989 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 1990 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | f1f5fc8 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 1991 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 1992 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 1993 | sleepTime = kMinThreadSleepTimeUs; |
| 1994 | } |
| 1995 | // reduce sleep time in case of consecutive application underruns to avoid |
| 1996 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 1997 | // duration we would end up writing less data than needed by the audio HAL if |
| 1998 | // the condition persists. |
| 1999 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 2000 | sleepTimeShift++; |
| 2001 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2002 | } else { |
| 2003 | sleepTime = idleSleepTime; |
| 2004 | } |
| 2005 | } else if (mBytesWritten != 0 || |
| 2006 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2007 | memset (mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2008 | sleepTime = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2009 | LOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2010 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2011 | // TODO add standby time extension fct of effect tail |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2015 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2016 | } |
| 2017 | // sleepTime == 0 means we must write to audio hardware |
| 2018 | if (sleepTime == 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2019 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2020 | effectChains[i]->process_l(); |
| 2021 | } |
| 2022 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2023 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2024 | mLastWriteTime = systemTime(); |
| 2025 | mInWrite = true; |
| 2026 | mBytesWritten += mixBufferSize; |
| 2027 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2028 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2029 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2030 | mNumWrites++; |
| 2031 | mInWrite = false; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 2032 | nsecs_t now = systemTime(); |
| 2033 | nsecs_t delta = now - mLastWriteTime; |
Eric Laurent | 276fa43 | 2011-10-18 15:42:27 -0700 | [diff] [blame] | 2034 | if (!mStandby && delta > maxPeriod) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2035 | mNumDelayedWrites++; |
Dave Sparks | d0ac8c0 | 2009-09-30 03:09:03 -0700 | [diff] [blame] | 2036 | if ((now - lastWarning) > kWarningThrottle) { |
| 2037 | LOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2038 | ns2ms(delta), mNumDelayedWrites, this); |
| 2039 | lastWarning = now; |
| 2040 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2041 | if (mStandby) { |
| 2042 | longStandbyExit = true; |
| 2043 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2044 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2045 | mStandby = false; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2046 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2047 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2048 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2049 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | // finally let go of all our tracks, without the lock held |
| 2053 | // since we can't guarantee the destructors won't acquire that |
| 2054 | // same lock. |
| 2055 | tracksToRemove.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2056 | |
| 2057 | // Effect chains will be actually deleted here if they were removed from |
| 2058 | // mEffectChains list during mixing or effects processing |
| 2059 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2063 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2064 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2065 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2066 | releaseWakeLock(); |
| 2067 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2068 | LOGV("MixerThread %p exiting", this); |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | // prepareTracks_l() must be called with ThreadBase::mLock held |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2073 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2074 | { |
| 2075 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2076 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2077 | // find out which tracks need to be processed |
| 2078 | size_t count = activeTracks.size(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2079 | size_t mixedTracks = 0; |
| 2080 | size_t tracksWithEffect = 0; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2081 | |
| 2082 | float masterVolume = mMasterVolume; |
| 2083 | bool masterMute = mMasterMute; |
| 2084 | |
Eric Laurent | 8cc93b9 | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2085 | if (masterMute) { |
| 2086 | masterVolume = 0; |
| 2087 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2088 | // Delegate master volume control to effect in output mix effect chain if needed |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2089 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2090 | if (chain != 0) { |
Eric Laurent | 8cc93b9 | 2010-08-11 05:20:11 -0700 | [diff] [blame] | 2091 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2092 | chain->setVolume_l(&v, &v); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2093 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 2094 | chain.clear(); |
| 2095 | } |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2096 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2097 | for (size_t i=0 ; i<count ; i++) { |
| 2098 | sp<Track> t = activeTracks[i].promote(); |
| 2099 | if (t == 0) continue; |
| 2100 | |
| 2101 | Track* const track = t.get(); |
| 2102 | audio_track_cblk_t* cblk = track->cblk(); |
| 2103 | |
| 2104 | // The first time a track is added we wait |
| 2105 | // for all its buffers to be filled before processing it |
| 2106 | mAudioMixer->setActiveTrack(track->name()); |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2107 | // make sure that we have enough frames to mix one full buffer. |
| 2108 | // enforce this condition only once to enable draining the buffer in case the client |
| 2109 | // app does not call stop() and relies on underrun to stop: |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 2110 | // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2111 | // during last round |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2112 | uint32_t minFrames = 1; |
Eric Laurent | 8a04fe0 | 2011-11-08 18:10:16 -0800 | [diff] [blame] | 2113 | if (!track->isStopped() && !track->isPausing() && |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 2114 | (mPrevMixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2115 | if (t->sampleRate() == (int)mSampleRate) { |
| 2116 | minFrames = mFrameCount; |
| 2117 | } else { |
Eric Laurent | 72dafb2 | 2011-12-22 16:08:41 -0800 | [diff] [blame] | 2118 | // +1 for rounding and +1 for additional sample needed for interpolation |
| 2119 | minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1; |
| 2120 | // add frames already consumed but not yet released by the resampler |
| 2121 | // because cblk->framesReady() will include these frames |
| 2122 | minFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 2123 | // the minimum track buffer size is normally twice the number of frames necessary |
| 2124 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 2125 | // of unreleased frames after each pass, but just in case... |
| 2126 | LOG_ASSERT(minFrames <= cblk->frameCount); |
Eric Laurent | 19ddf0e | 2011-11-03 12:16:05 -0700 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | if ((cblk->framesReady() >= minFrames) && track->isReady() && |
Eric Laurent | 71f37cd | 2010-03-31 12:21:17 -0700 | [diff] [blame] | 2130 | !track->isPaused() && !track->isTerminated()) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2131 | { |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 2132 | //LOGV("track %d u=%08x, s=%08x [OK] on thread %p", track->name(), cblk->user, cblk->server, this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2133 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2134 | mixedTracks++; |
| 2135 | |
| 2136 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 2137 | // connected to the track |
| 2138 | chain.clear(); |
| 2139 | if (track->mainBuffer() != mMixBuffer) { |
| 2140 | chain = getEffectChain_l(track->sessionId()); |
| 2141 | // Delegate volume control to effect in track effect chain if needed |
| 2142 | if (chain != 0) { |
| 2143 | tracksWithEffect++; |
| 2144 | } else { |
| 2145 | LOGW("prepareTracks_l(): track %08x attached to effect but no chain found on session %d", |
| 2146 | track->name(), track->sessionId()); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | |
| 2151 | int param = AudioMixer::VOLUME; |
| 2152 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2153 | // no ramp for the first volume setting |
| 2154 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2155 | if (track->mState == TrackBase::RESUMING) { |
| 2156 | track->mState = TrackBase::ACTIVE; |
| 2157 | param = AudioMixer::RAMP_VOLUME; |
| 2158 | } |
Eric Laurent | 4bb21c4 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 2159 | mAudioMixer->setParameter(AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2160 | } else if (cblk->server != 0) { |
| 2161 | // If the track is stopped before the first frame was mixed, |
| 2162 | // do not apply ramp |
| 2163 | param = AudioMixer::RAMP_VOLUME; |
| 2164 | } |
| 2165 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2166 | // compute volume for this track |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2167 | uint32_t vl, vr, va; |
Eric Laurent | 2a6b80b | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 2168 | if (track->isMuted() || track->isPausing() || |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2169 | mStreamTypes[track->type()].mute) { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2170 | vl = vr = va = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2171 | if (track->isPausing()) { |
| 2172 | track->setPaused(); |
| 2173 | } |
| 2174 | } else { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2175 | |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2176 | // read original volumes with volume control |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2177 | float typeVolume = mStreamTypes[track->type()].volume; |
Glenn Kasten | 871c16c | 2010-03-05 12:18:01 -0800 | [diff] [blame] | 2178 | float v = masterVolume * typeVolume; |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2179 | vl = (uint32_t)(v * cblk->volume[0]) << 12; |
| 2180 | vr = (uint32_t)(v * cblk->volume[1]) << 12; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2181 | |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2182 | va = (uint32_t)(v * cblk->sendLevel); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2183 | } |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2184 | // Delegate volume control to effect in track effect chain if needed |
| 2185 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 2186 | // Do not ramp volume if volume is controlled by effect |
| 2187 | param = AudioMixer::VOLUME; |
| 2188 | track->mHasVolumeController = true; |
| 2189 | } else { |
| 2190 | // force no volume ramp when volume controller was just disabled or removed |
| 2191 | // from effect chain to avoid volume spike |
| 2192 | if (track->mHasVolumeController) { |
| 2193 | param = AudioMixer::VOLUME; |
| 2194 | } |
| 2195 | track->mHasVolumeController = false; |
| 2196 | } |
| 2197 | |
| 2198 | // Convert volumes from 8.24 to 4.12 format |
| 2199 | int16_t left, right, aux; |
| 2200 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2201 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2202 | left = int16_t(v_clamped); |
| 2203 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2204 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2205 | right = int16_t(v_clamped); |
| 2206 | |
| 2207 | if (va > MAX_GAIN_INT) va = MAX_GAIN_INT; |
| 2208 | aux = int16_t(va); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2209 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2210 | // XXX: these things DON'T need to be done each time |
| 2211 | mAudioMixer->setBufferProvider(track); |
| 2212 | mAudioMixer->enable(AudioMixer::MIXING); |
| 2213 | |
| 2214 | mAudioMixer->setParameter(param, AudioMixer::VOLUME0, (void *)left); |
| 2215 | mAudioMixer->setParameter(param, AudioMixer::VOLUME1, (void *)right); |
| 2216 | mAudioMixer->setParameter(param, AudioMixer::AUXLEVEL, (void *)aux); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2217 | mAudioMixer->setParameter( |
| 2218 | AudioMixer::TRACK, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2219 | AudioMixer::FORMAT, (void *)track->format()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2220 | mAudioMixer->setParameter( |
| 2221 | AudioMixer::TRACK, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 2222 | AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2223 | mAudioMixer->setParameter( |
| 2224 | AudioMixer::RESAMPLE, |
| 2225 | AudioMixer::SAMPLE_RATE, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2226 | (void *)(cblk->sampleRate)); |
| 2227 | mAudioMixer->setParameter( |
| 2228 | AudioMixer::TRACK, |
| 2229 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 2230 | mAudioMixer->setParameter( |
| 2231 | AudioMixer::TRACK, |
| 2232 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2233 | |
| 2234 | // reset retry count |
| 2235 | track->mRetryCount = kMaxTrackRetries; |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 2236 | // If one track is ready, set the mixer ready if: |
| 2237 | // - the mixer was not ready during previous round OR |
| 2238 | // - no other track is not ready |
| 2239 | if (mPrevMixerStatus != MIXER_TRACKS_READY || |
| 2240 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 2241 | mixerStatus = MIXER_TRACKS_READY; |
| 2242 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2243 | } else { |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 2244 | //LOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", track->name(), cblk->user, cblk->server, this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2245 | if (track->isStopped()) { |
| 2246 | track->reset(); |
| 2247 | } |
| 2248 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2249 | // We have consumed all the buffers of this track. |
| 2250 | // Remove it from the list of active tracks. |
| 2251 | tracksToRemove->add(track); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2252 | } else { |
| 2253 | // No buffers for this track. Give it a few chances to |
| 2254 | // fill a buffer, then remove it from active list. |
| 2255 | if (--(track->mRetryCount) <= 0) { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2256 | LOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", track->name(), this); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2257 | tracksToRemove->add(track); |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 2258 | // indicate to client process that the track was disabled because of underrun |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2259 | android_atomic_or(CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 2260 | // If one track is not ready, mark the mixer also not ready if: |
| 2261 | // - the mixer was ready during previous round OR |
| 2262 | // - no other track is ready |
| 2263 | } else if (mPrevMixerStatus == MIXER_TRACKS_READY || |
| 2264 | mixerStatus != MIXER_TRACKS_READY) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2265 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2266 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2267 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2268 | mAudioMixer->disable(AudioMixer::MIXING); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | // remove all the tracks that need to be... |
| 2273 | count = tracksToRemove->size(); |
| 2274 | if (UNLIKELY(count)) { |
| 2275 | for (size_t i=0 ; i<count ; i++) { |
| 2276 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 2277 | mActiveTracks.remove(track); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2278 | if (track->mainBuffer() != mMixBuffer) { |
| 2279 | chain = getEffectChain_l(track->sessionId()); |
| 2280 | if (chain != 0) { |
| 2281 | LOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2282 | chain->decActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2283 | } |
| 2284 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2285 | if (track->isTerminated()) { |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2286 | removeTrack_l(track); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2291 | // mix buffer must be cleared if all tracks are connected to an |
| 2292 | // effect chain as in this case the mixer will not write to |
| 2293 | // mix buffer and track effects will accumulate into it |
| 2294 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 2295 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 2296 | } |
| 2297 | |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 2298 | mPrevMixerStatus = mixerStatus; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2299 | return mixerStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 2302 | void AudioFlinger::MixerThread::invalidateTracks(int streamType) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2303 | { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2304 | LOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
| 2305 | this, streamType, mTracks.size()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2306 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2307 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2308 | size_t size = mTracks.size(); |
| 2309 | for (size_t i = 0; i < size; i++) { |
| 2310 | sp<Track> t = mTracks[i]; |
| 2311 | if (t->type() == streamType) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 2312 | android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 2313 | t->mCblk->cv.signal(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2314 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 2315 | } |
| 2316 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2317 | |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 2318 | void AudioFlinger::PlaybackThread::setStreamValid(int streamType, bool valid) |
| 2319 | { |
| 2320 | LOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d", |
| 2321 | this, streamType, valid); |
| 2322 | Mutex::Autolock _l(mLock); |
| 2323 | |
| 2324 | mStreamTypes[streamType].valid = valid; |
| 2325 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2326 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2327 | // getTrackName_l() must be called with ThreadBase::mLock held |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2328 | int AudioFlinger::MixerThread::getTrackName_l() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2329 | { |
| 2330 | return mAudioMixer->getTrackName(); |
| 2331 | } |
| 2332 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2333 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2334 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2335 | { |
Eric Laurent | 0a08029 | 2009-12-07 10:53:10 -0800 | [diff] [blame] | 2336 | LOGV("remove track (%d) and delete from mixer", name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2337 | mAudioMixer->deleteTrackName(name); |
| 2338 | } |
| 2339 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2340 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2341 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2342 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2343 | bool reconfig = false; |
| 2344 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2345 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2346 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2347 | String8 keyValuePair = mNewParameters[0]; |
| 2348 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2349 | int value; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2350 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2351 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 2352 | reconfig = true; |
| 2353 | } |
| 2354 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2355 | if (value != AUDIO_FORMAT_PCM_16_BIT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2356 | status = BAD_VALUE; |
| 2357 | } else { |
| 2358 | reconfig = true; |
| 2359 | } |
| 2360 | } |
| 2361 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2362 | if (value != AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2363 | status = BAD_VALUE; |
| 2364 | } else { |
| 2365 | reconfig = true; |
| 2366 | } |
| 2367 | } |
| 2368 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2369 | // do not accept frame count changes if tracks are open as the track buffer |
| 2370 | // size depends on frame count and correct behavior would not be garantied |
| 2371 | // if frame count is changed after track creation |
| 2372 | if (!mTracks.isEmpty()) { |
| 2373 | status = INVALID_OPERATION; |
| 2374 | } else { |
| 2375 | reconfig = true; |
| 2376 | } |
| 2377 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2378 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2379 | // when changing the audio output device, call addBatteryData to notify |
| 2380 | // the change |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2381 | if ((int)mDevice != value) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2382 | uint32_t params = 0; |
| 2383 | // check whether speaker is on |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2384 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2385 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
| 2386 | } |
| 2387 | |
| 2388 | int deviceWithoutSpeaker |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2389 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 2390 | // check if any other device (except speaker) is on |
| 2391 | if (value & deviceWithoutSpeaker ) { |
| 2392 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 2393 | } |
| 2394 | |
| 2395 | if (params != 0) { |
| 2396 | addBatteryData(params); |
| 2397 | } |
| 2398 | } |
| 2399 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2400 | // forward device change to effects that have requested to be |
| 2401 | // aware of attached audio device. |
| 2402 | mDevice = (uint32_t)value; |
| 2403 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2404 | mEffectChains[i]->setDevice_l(mDevice); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2408 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2409 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2410 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2411 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2412 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2413 | mStandby = true; |
| 2414 | mBytesWritten = 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2415 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2416 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2417 | } |
| 2418 | if (status == NO_ERROR && reconfig) { |
| 2419 | delete mAudioMixer; |
| 2420 | readOutputParameters(); |
| 2421 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 2422 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 2423 | int name = getTrackName_l(); |
| 2424 | if (name < 0) break; |
| 2425 | mTracks[i]->mName = name; |
Eric Laurent | 6f7e097 | 2009-08-10 08:15:12 -0700 | [diff] [blame] | 2426 | // limit track sample rate to 2 x new output sample rate |
| 2427 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 2428 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 2429 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2430 | } |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2431 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2432 | } |
| 2433 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 2434 | |
| 2435 | mNewParameters.removeAt(0); |
| 2436 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2437 | mParamStatus = status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2438 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2439 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2440 | // already timed out waiting for the status and will never signal the condition. |
| 2441 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2442 | } |
| 2443 | return reconfig; |
| 2444 | } |
| 2445 | |
| 2446 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2447 | { |
| 2448 | const size_t SIZE = 256; |
| 2449 | char buffer[SIZE]; |
| 2450 | String8 result; |
| 2451 | |
| 2452 | PlaybackThread::dumpInternals(fd, args); |
| 2453 | |
| 2454 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2455 | result.append(buffer); |
| 2456 | write(fd, result.string(), result.size()); |
| 2457 | return NO_ERROR; |
| 2458 | } |
| 2459 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2460 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2461 | { |
Eric Laurent | a54d7d3 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2462 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2465 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() |
| 2466 | { |
| 2467 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2468 | } |
| 2469 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2470 | // ---------------------------------------------------------------------------- |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2471 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2472 | : PlaybackThread(audioFlinger, output, id, device) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2473 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2474 | mType = ThreadBase::DIRECT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2478 | { |
| 2479 | } |
| 2480 | |
| 2481 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2482 | static inline int16_t clamp16(int32_t sample) |
| 2483 | { |
| 2484 | if ((sample>>15) ^ (sample>>31)) |
| 2485 | sample = 0x7FFF ^ (sample>>31); |
| 2486 | return sample; |
| 2487 | } |
| 2488 | |
| 2489 | static inline |
| 2490 | int32_t mul(int16_t in, int16_t v) |
| 2491 | { |
| 2492 | #if defined(__arm__) && !defined(__thumb__) |
| 2493 | int32_t out; |
| 2494 | asm( "smulbb %[out], %[in], %[v] \n" |
| 2495 | : [out]"=r"(out) |
| 2496 | : [in]"%r"(in), [v]"r"(v) |
| 2497 | : ); |
| 2498 | return out; |
| 2499 | #else |
| 2500 | return in * int32_t(v); |
| 2501 | #endif |
| 2502 | } |
| 2503 | |
| 2504 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2505 | { |
| 2506 | // Do not apply volume on compressed audio |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2507 | if (!audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2508 | return; |
| 2509 | } |
| 2510 | |
| 2511 | // convert to signed 16 bit before volume calculation |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2512 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2513 | size_t count = mFrameCount * mChannelCount; |
| 2514 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2515 | int16_t *dst = mMixBuffer + count-1; |
| 2516 | while(count--) { |
| 2517 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | size_t frameCount = mFrameCount; |
| 2522 | int16_t *out = mMixBuffer; |
| 2523 | if (ramp) { |
| 2524 | if (mChannelCount == 1) { |
| 2525 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2526 | int32_t vlInc = d / (int32_t)frameCount; |
| 2527 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2528 | do { |
| 2529 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2530 | out++; |
| 2531 | vl += vlInc; |
| 2532 | } while (--frameCount); |
| 2533 | |
| 2534 | } else { |
| 2535 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2536 | int32_t vlInc = d / (int32_t)frameCount; |
| 2537 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2538 | int32_t vrInc = d / (int32_t)frameCount; |
| 2539 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2540 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2541 | do { |
| 2542 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2543 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2544 | out += 2; |
| 2545 | vl += vlInc; |
| 2546 | vr += vrInc; |
| 2547 | } while (--frameCount); |
| 2548 | } |
| 2549 | } else { |
| 2550 | if (mChannelCount == 1) { |
| 2551 | do { |
| 2552 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2553 | out++; |
| 2554 | } while (--frameCount); |
| 2555 | } else { |
| 2556 | do { |
| 2557 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2558 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2559 | out += 2; |
| 2560 | } while (--frameCount); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | // convert back to unsigned 8 bit after volume calculation |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2565 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2566 | size_t count = mFrameCount * mChannelCount; |
| 2567 | int16_t *src = mMixBuffer; |
| 2568 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2569 | while(count--) { |
| 2570 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | mLeftVolShort = leftVol; |
| 2575 | mRightVolShort = rightVol; |
| 2576 | } |
| 2577 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2578 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2579 | { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2580 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2581 | sp<Track> trackToRemove; |
| 2582 | sp<Track> activeTrack; |
| 2583 | nsecs_t standbyTime = systemTime(); |
| 2584 | int8_t *curBuf; |
| 2585 | size_t mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2586 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2587 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2588 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2589 | // use shorter standby delay as on normal output to release |
| 2590 | // hardware resources as soon as possible |
| 2591 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2592 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2593 | acquireWakeLock(); |
| 2594 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2595 | while (!exitPending()) |
| 2596 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2597 | bool rampVolume; |
| 2598 | uint16_t leftVol; |
| 2599 | uint16_t rightVol; |
| 2600 | Vector< sp<EffectChain> > effectChains; |
| 2601 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2602 | processConfigEvents(); |
| 2603 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2604 | mixerStatus = MIXER_IDLE; |
| 2605 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2606 | { // scope for the mLock |
| 2607 | |
| 2608 | Mutex::Autolock _l(mLock); |
| 2609 | |
| 2610 | if (checkForNewParameters_l()) { |
| 2611 | mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2612 | activeSleepTime = activeSleepTimeUs(); |
| 2613 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2614 | standbyDelay = microseconds(activeSleepTime*2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | // put audio hardware into standby after short delay |
| 2618 | if UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2619 | mSuspended) { |
| 2620 | // wait until we have something to do... |
| 2621 | if (!mStandby) { |
| 2622 | LOGV("Audio hardware entering standby, mixer %p\n", this); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2623 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2624 | mStandby = true; |
| 2625 | mBytesWritten = 0; |
| 2626 | } |
| 2627 | |
| 2628 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2629 | // we're about to wait, flush the binder command buffer |
| 2630 | IPCThreadState::self()->flushCommands(); |
| 2631 | |
| 2632 | if (exitPending()) break; |
| 2633 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2634 | releaseWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2635 | LOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); |
| 2636 | mWaitWorkCV.wait(mLock); |
| 2637 | LOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2638 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2639 | |
| 2640 | if (mMasterMute == false) { |
| 2641 | char value[PROPERTY_VALUE_MAX]; |
| 2642 | property_get("ro.audio.silent", value, "0"); |
| 2643 | if (atoi(value)) { |
| 2644 | LOGD("Silence is golden"); |
| 2645 | setMasterMute(true); |
| 2646 | } |
| 2647 | } |
| 2648 | |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2649 | standbyTime = systemTime() + standbyDelay; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2650 | sleepTime = idleSleepTime; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2651 | continue; |
| 2652 | } |
| 2653 | } |
| 2654 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2655 | effectChains = mEffectChains; |
| 2656 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2657 | // find out which tracks need to be processed |
| 2658 | if (mActiveTracks.size() != 0) { |
| 2659 | sp<Track> t = mActiveTracks[0].promote(); |
| 2660 | if (t == 0) continue; |
| 2661 | |
| 2662 | Track* const track = t.get(); |
| 2663 | audio_track_cblk_t* cblk = track->cblk(); |
| 2664 | |
| 2665 | // The first time a track is added we wait |
| 2666 | // for all its buffers to be filled before processing it |
Eric Laurent | 9a30fc1 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 2667 | if (cblk->framesReady() && track->isReady() && |
Eric Laurent | 380558b | 2010-04-09 06:11:48 -0700 | [diff] [blame] | 2668 | !track->isPaused() && !track->isTerminated()) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2669 | { |
| 2670 | //LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
| 2671 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2672 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2673 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2674 | mLeftVolFloat = mRightVolFloat = 0; |
| 2675 | mLeftVolShort = mRightVolShort = 0; |
| 2676 | if (track->mState == TrackBase::RESUMING) { |
| 2677 | track->mState = TrackBase::ACTIVE; |
| 2678 | rampVolume = true; |
| 2679 | } |
| 2680 | } else if (cblk->server != 0) { |
| 2681 | // If the track is stopped before the first frame was mixed, |
| 2682 | // do not apply ramp |
| 2683 | rampVolume = true; |
| 2684 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2685 | // compute volume for this track |
| 2686 | float left, right; |
| 2687 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 2688 | mStreamTypes[track->type()].mute) { |
| 2689 | left = right = 0; |
| 2690 | if (track->isPausing()) { |
| 2691 | track->setPaused(); |
| 2692 | } |
| 2693 | } else { |
| 2694 | float typeVolume = mStreamTypes[track->type()].volume; |
| 2695 | float v = mMasterVolume * typeVolume; |
| 2696 | float v_clamped = v * cblk->volume[0]; |
| 2697 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2698 | left = v_clamped/MAX_GAIN; |
| 2699 | v_clamped = v * cblk->volume[1]; |
| 2700 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2701 | right = v_clamped/MAX_GAIN; |
| 2702 | } |
| 2703 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2704 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2705 | mLeftVolFloat = left; |
| 2706 | mRightVolFloat = right; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2707 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2708 | // If audio HAL implements volume control, |
| 2709 | // force software volume to nominal value |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2710 | if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2711 | left = 1.0f; |
| 2712 | right = 1.0f; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2713 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2714 | |
| 2715 | // Convert volumes from float to 8.24 |
| 2716 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2717 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2718 | |
| 2719 | // Delegate volume control to effect in track effect chain if needed |
| 2720 | // only one effect chain can be present on DirectOutputThread, so if |
| 2721 | // there is one, the track is connected to it |
| 2722 | if (!effectChains.isEmpty()) { |
Eric Laurent | 27a2fdf | 2010-09-10 17:44:44 -0700 | [diff] [blame] | 2723 | // Do not ramp volume if volume is controlled by effect |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2724 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2725 | rampVolume = false; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | // Convert volumes from 8.24 to 4.12 format |
| 2730 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2731 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2732 | leftVol = (uint16_t)v_clamped; |
| 2733 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2734 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2735 | rightVol = (uint16_t)v_clamped; |
| 2736 | } else { |
| 2737 | leftVol = mLeftVolShort; |
| 2738 | rightVol = mRightVolShort; |
| 2739 | rampVolume = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | // reset retry count |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2743 | track->mRetryCount = kMaxTrackRetriesDirect; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2744 | activeTrack = t; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2745 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2746 | } else { |
| 2747 | //LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
| 2748 | if (track->isStopped()) { |
| 2749 | track->reset(); |
| 2750 | } |
| 2751 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2752 | // We have consumed all the buffers of this track. |
| 2753 | // Remove it from the list of active tracks. |
| 2754 | trackToRemove = track; |
| 2755 | } else { |
| 2756 | // No buffers for this track. Give it a few chances to |
| 2757 | // fill a buffer, then remove it from active list. |
| 2758 | if (--(track->mRetryCount) <= 0) { |
| 2759 | LOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
| 2760 | trackToRemove = track; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2761 | } else { |
| 2762 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2763 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2764 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | // remove all the tracks that need to be... |
| 2769 | if (UNLIKELY(trackToRemove != 0)) { |
| 2770 | mActiveTracks.remove(trackToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2771 | if (!effectChains.isEmpty()) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2772 | LOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(), |
| 2773 | trackToRemove->sessionId()); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2774 | effectChains[0]->decActiveTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2775 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2776 | if (trackToRemove->isTerminated()) { |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 2777 | removeTrack_l(trackToRemove); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2778 | } |
| 2779 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2780 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2781 | lockEffectChains_l(effectChains); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2784 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2785 | AudioBufferProvider::Buffer buffer; |
| 2786 | size_t frameCount = mFrameCount; |
| 2787 | curBuf = (int8_t *)mMixBuffer; |
| 2788 | // output audio to hardware |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2789 | while (frameCount) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2790 | buffer.frameCount = frameCount; |
| 2791 | activeTrack->getNextBuffer(&buffer); |
| 2792 | if (UNLIKELY(buffer.raw == 0)) { |
| 2793 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2794 | break; |
| 2795 | } |
| 2796 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2797 | frameCount -= buffer.frameCount; |
| 2798 | curBuf += buffer.frameCount * mFrameSize; |
| 2799 | activeTrack->releaseBuffer(&buffer); |
| 2800 | } |
| 2801 | sleepTime = 0; |
Eric Laurent | ef9500f | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 2802 | standbyTime = systemTime() + standbyDelay; |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2803 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2804 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2805 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2806 | sleepTime = activeSleepTime; |
| 2807 | } else { |
| 2808 | sleepTime = idleSleepTime; |
| 2809 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2810 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2811 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2812 | sleepTime = 0; |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2813 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2814 | } |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 2815 | |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2816 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2817 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2818 | } |
| 2819 | // sleepTime == 0 means we must write to audio hardware |
| 2820 | if (sleepTime == 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2821 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2822 | applyVolume(leftVol, rightVol, rampVolume); |
| 2823 | } |
| 2824 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2825 | effectChains[i]->process_l(); |
| 2826 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2827 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2828 | |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2829 | mLastWriteTime = systemTime(); |
| 2830 | mInWrite = true; |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2831 | mBytesWritten += mixBufferSize; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2832 | int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 2833 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2834 | mNumWrites++; |
| 2835 | mInWrite = false; |
| 2836 | mStandby = false; |
| 2837 | } else { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2838 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 2839 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | // finally let go of removed track, without the lock held |
| 2843 | // since we can't guarantee the destructors won't acquire that |
| 2844 | // same lock. |
| 2845 | trackToRemove.clear(); |
| 2846 | activeTrack.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2847 | |
| 2848 | // Effect chains will be actually deleted here if they were removed from |
| 2849 | // mEffectChains list during mixing or effects processing |
| 2850 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2854 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2855 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2856 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2857 | releaseWakeLock(); |
| 2858 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2859 | LOGV("DirectOutputThread %p exiting", this); |
| 2860 | return false; |
| 2861 | } |
| 2862 | |
| 2863 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2864 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2865 | { |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2870 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2871 | { |
| 2872 | } |
| 2873 | |
| 2874 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2875 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2876 | { |
| 2877 | bool reconfig = false; |
| 2878 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2879 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2880 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2881 | String8 keyValuePair = mNewParameters[0]; |
| 2882 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2883 | int value; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2884 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2885 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2886 | // do not accept frame count changes if tracks are open as the track buffer |
| 2887 | // size depends on frame count and correct behavior would not be garantied |
| 2888 | // if frame count is changed after track creation |
| 2889 | if (!mTracks.isEmpty()) { |
| 2890 | status = INVALID_OPERATION; |
| 2891 | } else { |
| 2892 | reconfig = true; |
| 2893 | } |
| 2894 | } |
| 2895 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2896 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2897 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2898 | if (!mStandby && status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2899 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2900 | mStandby = true; |
| 2901 | mBytesWritten = 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2902 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2903 | keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2904 | } |
| 2905 | if (status == NO_ERROR && reconfig) { |
| 2906 | readOutputParameters(); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 2907 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2908 | } |
| 2909 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 2910 | |
| 2911 | mNewParameters.removeAt(0); |
| 2912 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2913 | mParamStatus = status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2914 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 2915 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 2916 | // already timed out waiting for the status and will never signal the condition. |
| 2917 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2918 | } |
| 2919 | return reconfig; |
The Android Open Source Project | f1e484a | 2009-01-22 00:13:42 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2922 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2923 | { |
| 2924 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2925 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 4433169 | 2011-12-05 09:47:19 -0800 | [diff] [blame] | 2926 | time = PlaybackThread::activeSleepTimeUs(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2927 | } else { |
| 2928 | time = 10000; |
| 2929 | } |
| 2930 | return time; |
| 2931 | } |
| 2932 | |
| 2933 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 2934 | { |
| 2935 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2936 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | a54d7d3 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2937 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2938 | } else { |
| 2939 | time = 10000; |
| 2940 | } |
| 2941 | return time; |
| 2942 | } |
| 2943 | |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2944 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() |
| 2945 | { |
| 2946 | uint32_t time; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2947 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 2948 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 2949 | } else { |
| 2950 | time = 10000; |
| 2951 | } |
| 2952 | return time; |
| 2953 | } |
| 2954 | |
| 2955 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2956 | // ---------------------------------------------------------------------------- |
| 2957 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 2958 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2959 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2960 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2961 | mType = ThreadBase::DUPLICATING; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2962 | addOutputTrack(mainThread); |
| 2963 | } |
| 2964 | |
| 2965 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 2966 | { |
Eric Laurent | 0a08029 | 2009-12-07 10:53:10 -0800 | [diff] [blame] | 2967 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2968 | mOutputTracks[i]->destroy(); |
| 2969 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2970 | mOutputTracks.clear(); |
| 2971 | } |
| 2972 | |
| 2973 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 2974 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2975 | Vector< sp<Track> > tracksToRemove; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2976 | uint32_t mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2977 | nsecs_t standbyTime = systemTime(); |
| 2978 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2979 | SortedVector< sp<OutputTrack> > outputTracks; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 2980 | uint32_t writeFrames = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2981 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2982 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2983 | uint32_t sleepTime = idleSleepTime; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 2984 | Vector< sp<EffectChain> > effectChains; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2985 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 2986 | acquireWakeLock(); |
| 2987 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2988 | while (!exitPending()) |
| 2989 | { |
| 2990 | processConfigEvents(); |
| 2991 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 2992 | mixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2993 | { // scope for the mLock |
| 2994 | |
| 2995 | Mutex::Autolock _l(mLock); |
| 2996 | |
| 2997 | if (checkForNewParameters_l()) { |
| 2998 | mixBufferSize = mFrameCount*mFrameSize; |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 2999 | updateWaitTime(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3000 | activeSleepTime = activeSleepTimeUs(); |
| 3001 | idleSleepTime = idleSleepTimeUs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 3005 | |
| 3006 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3007 | outputTracks.add(mOutputTracks[i]); |
| 3008 | } |
| 3009 | |
| 3010 | // put audio hardware into standby after short delay |
| 3011 | if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 3012 | mSuspended) { |
| 3013 | if (!mStandby) { |
| 3014 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3015 | outputTracks[i]->stop(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3016 | } |
| 3017 | mStandby = true; |
| 3018 | mBytesWritten = 0; |
| 3019 | } |
| 3020 | |
| 3021 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 3022 | // we're about to wait, flush the binder command buffer |
| 3023 | IPCThreadState::self()->flushCommands(); |
| 3024 | outputTracks.clear(); |
| 3025 | |
| 3026 | if (exitPending()) break; |
| 3027 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3028 | releaseWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3029 | LOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); |
| 3030 | mWaitWorkCV.wait(mLock); |
| 3031 | LOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3032 | acquireWakeLock_l(); |
| 3033 | |
Eric Laurent | 0b08965 | 2012-01-23 18:35:10 -0800 | [diff] [blame] | 3034 | mPrevMixerStatus = MIXER_IDLE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3035 | if (mMasterMute == false) { |
| 3036 | char value[PROPERTY_VALUE_MAX]; |
| 3037 | property_get("ro.audio.silent", value, "0"); |
| 3038 | if (atoi(value)) { |
| 3039 | LOGD("Silence is golden"); |
| 3040 | setMasterMute(true); |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3045 | sleepTime = idleSleepTime; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3046 | continue; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3050 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3051 | |
| 3052 | // prevent any changes in effect chain list and in each effect chain |
| 3053 | // during mixing and effect process as the audio buffers could be deleted |
| 3054 | // or modified if an effect is created or deleted |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3055 | lockEffectChains_l(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3056 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3057 | |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3058 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3059 | // mix buffers... |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3060 | if (outputsReady(outputTracks)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3061 | mAudioMixer->process(); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3062 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3063 | memset(mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3064 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3065 | sleepTime = 0; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3066 | writeFrames = mFrameCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3067 | } else { |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3068 | if (sleepTime == 0) { |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 3069 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 3070 | sleepTime = activeSleepTime; |
| 3071 | } else { |
| 3072 | sleepTime = idleSleepTime; |
| 3073 | } |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3074 | } else if (mBytesWritten != 0) { |
| 3075 | // flush remaining overflow buffers in output tracks |
| 3076 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3077 | if (outputTracks[i]->isActive()) { |
| 3078 | sleepTime = 0; |
| 3079 | writeFrames = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3080 | memset(mMixBuffer, 0, mixBufferSize); |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3081 | break; |
| 3082 | } |
| 3083 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3084 | } |
| 3085 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3086 | |
| 3087 | if (mSuspended) { |
Eric Laurent | 8448a79 | 2010-08-18 18:13:17 -0700 | [diff] [blame] | 3088 | sleepTime = suspendSleepTimeUs(); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3089 | } |
| 3090 | // sleepTime == 0 means we must write to audio hardware |
| 3091 | if (sleepTime == 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3092 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3093 | effectChains[i]->process_l(); |
| 3094 | } |
| 3095 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3096 | unlockEffectChains(effectChains); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3097 | |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3098 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3099 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3100 | outputTracks[i]->write(mMixBuffer, writeFrames); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3101 | } |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3102 | mStandby = false; |
| 3103 | mBytesWritten += mixBufferSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3104 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3105 | // enable changes in effect chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3106 | unlockEffectChains(effectChains); |
Eric Laurent | f69a3f8 | 2009-09-22 00:35:48 -0700 | [diff] [blame] | 3107 | usleep(sleepTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | // finally let go of all our tracks, without the lock held |
| 3111 | // since we can't guarantee the destructors won't acquire that |
| 3112 | // same lock. |
| 3113 | tracksToRemove.clear(); |
| 3114 | outputTracks.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3115 | |
| 3116 | // Effect chains will be actually deleted here if they were removed from |
| 3117 | // mEffectChains list during mixing or effects processing |
| 3118 | effectChains.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 3121 | releaseWakeLock(); |
| 3122 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3123 | return false; |
| 3124 | } |
| 3125 | |
| 3126 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 3127 | { |
| 3128 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
| 3129 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3130 | this, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3131 | mSampleRate, |
| 3132 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3133 | mChannelMask, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3134 | frameCount); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3135 | if (outputTrack->cblk() != NULL) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3136 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3137 | mOutputTracks.add(outputTrack); |
| 3138 | LOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3139 | updateWaitTime(); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3140 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 3144 | { |
| 3145 | Mutex::Autolock _l(mLock); |
| 3146 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3147 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3148 | mOutputTracks[i]->destroy(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3149 | mOutputTracks.removeAt(i); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3150 | updateWaitTime(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3151 | return; |
| 3152 | } |
| 3153 | } |
| 3154 | LOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
| 3155 | } |
| 3156 | |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3157 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 3158 | { |
| 3159 | mWaitTimeMs = UINT_MAX; |
| 3160 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 3161 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 3162 | if (strong != NULL) { |
| 3163 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 3164 | if (waitTimeMs < mWaitTimeMs) { |
| 3165 | mWaitTimeMs = waitTimeMs; |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | |
| 3172 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 3173 | { |
| 3174 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 3175 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 3176 | if (thread == 0) { |
| 3177 | LOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
| 3178 | return false; |
| 3179 | } |
| 3180 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3181 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 3182 | LOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
| 3183 | return false; |
| 3184 | } |
| 3185 | } |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
| 3189 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 3190 | { |
| 3191 | return (mWaitTimeMs * 1000) / 2; |
| 3192 | } |
| 3193 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3194 | // ---------------------------------------------------------------------------- |
| 3195 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3196 | // TrackBase constructor must be called with AudioFlinger::mLock held |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3197 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 3198 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3199 | const sp<Client>& client, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3200 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3201 | uint32_t format, |
| 3202 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3203 | int frameCount, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3204 | uint32_t flags, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3205 | const sp<IMemory>& sharedBuffer, |
| 3206 | int sessionId) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3207 | : RefBase(), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3208 | mThread(thread), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3209 | mClient(client), |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3210 | mCblk(0), |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3211 | mFrameCount(0), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3212 | mState(IDLE), |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3213 | mClientTid(-1), |
| 3214 | mFormat(format), |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3215 | mFlags(flags & ~SYSTEM_FLAGS_MASK), |
| 3216 | mSessionId(sessionId) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3217 | { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3218 | LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
| 3219 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3220 | // LOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3221 | size_t size = sizeof(audio_track_cblk_t); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3222 | uint8_t channelCount = popcount(channelMask); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3223 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 3224 | if (sharedBuffer == 0) { |
| 3225 | size += bufferSize; |
| 3226 | } |
| 3227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3228 | if (client != NULL) { |
| 3229 | mCblkMemory = client->heap()->allocate(size); |
| 3230 | if (mCblkMemory != 0) { |
| 3231 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
| 3232 | if (mCblk) { // construct the shared structure in-place. |
| 3233 | new(mCblk) audio_track_cblk_t(); |
| 3234 | // clear all buffers |
| 3235 | mCblk->frameCount = frameCount; |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 3236 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3237 | mChannelCount = channelCount; |
| 3238 | mChannelMask = channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | if (sharedBuffer == 0) { |
| 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 | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3243 | // written to buffer (other flags are cleared) |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3244 | mCblk->flags = CBLK_UNDERRUN_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3245 | } else { |
| 3246 | mBuffer = sharedBuffer->pointer(); |
| 3247 | } |
| 3248 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3250 | } else { |
| 3251 | LOGE("not enough memory for AudioTrack size=%u", size); |
| 3252 | client->heap()->dump("AudioTrack"); |
| 3253 | return; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3254 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3255 | } else { |
| 3256 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
| 3257 | if (mCblk) { // construct the shared structure in-place. |
| 3258 | new(mCblk) audio_track_cblk_t(); |
| 3259 | // clear all buffers |
| 3260 | mCblk->frameCount = frameCount; |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 3261 | mCblk->sampleRate = sampleRate; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3262 | mChannelCount = channelCount; |
| 3263 | mChannelMask = channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3264 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3265 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 3266 | // Force underrun condition to avoid false underrun callback until first data is |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 3267 | // written to buffer (other flags are cleared) |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3268 | mCblk->flags = CBLK_UNDERRUN_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3269 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 3270 | } |
| 3271 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3274 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3275 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3276 | if (mCblk) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3277 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 3278 | if (mClient == NULL) { |
| 3279 | delete mCblk; |
| 3280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3281 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3282 | mCblkMemory.clear(); // and free the shared memory |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 3283 | if (mClient != NULL) { |
| 3284 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 3285 | mClient.clear(); |
| 3286 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3289 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3290 | { |
| 3291 | buffer->raw = 0; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3292 | mFrameCount = buffer->frameCount; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3293 | step(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3294 | buffer->frameCount = 0; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3297 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3298 | bool result; |
| 3299 | audio_track_cblk_t* cblk = this->cblk(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3300 | |
| 3301 | result = cblk->stepServer(mFrameCount); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3302 | if (!result) { |
| 3303 | LOGV("stepServer failed acquiring cblk mutex"); |
| 3304 | mFlags |= STEPSERVER_FAILED; |
| 3305 | } |
| 3306 | return result; |
| 3307 | } |
| 3308 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3309 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3310 | audio_track_cblk_t* cblk = this->cblk(); |
| 3311 | |
| 3312 | cblk->user = 0; |
| 3313 | cblk->server = 0; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3314 | cblk->userBase = 0; |
| 3315 | cblk->serverBase = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3316 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3317 | LOGV("TrackBase::reset"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3320 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3321 | { |
| 3322 | return mCblkMemory; |
| 3323 | } |
| 3324 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3325 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3326 | return (int)mCblk->sampleRate; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3329 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3330 | return (const int)mChannelCount; |
| 3331 | } |
| 3332 | |
| 3333 | uint32_t AudioFlinger::ThreadBase::TrackBase::channelMask() const { |
| 3334 | return mChannelMask; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Eric Laurent | 2bb6b2a | 2009-09-16 06:02:45 -0700 | [diff] [blame] | 3337 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3338 | audio_track_cblk_t* cblk = this->cblk(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3339 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize; |
| 3340 | int8_t *bufferEnd = bufferStart + frames * cblk->frameSize; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3341 | |
| 3342 | // Check validity of returned pointer in case the track control block would have been corrupted. |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3343 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
| 3344 | ((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3345 | LOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \ |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3346 | server %d, serverBase %d, user %d, userBase %d", |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3347 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3348 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3349 | return 0; |
| 3350 | } |
| 3351 | |
| 3352 | return bufferStart; |
| 3353 | } |
| 3354 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3355 | // ---------------------------------------------------------------------------- |
| 3356 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3357 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 3358 | AudioFlinger::PlaybackThread::Track::Track( |
| 3359 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3360 | const sp<Client>& client, |
| 3361 | int streamType, |
| 3362 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3363 | uint32_t format, |
| 3364 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3365 | int frameCount, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3366 | const sp<IMemory>& sharedBuffer, |
| 3367 | int sessionId) |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3368 | : TrackBase(thread, client, sampleRate, format, channelMask, frameCount, 0, sharedBuffer, sessionId), |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 3369 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), |
| 3370 | mAuxEffectId(0), mHasVolumeController(false) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3371 | { |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3372 | if (mCblk != NULL) { |
| 3373 | sp<ThreadBase> baseThread = thread.promote(); |
| 3374 | if (baseThread != 0) { |
| 3375 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); |
| 3376 | mName = playbackThread->getTrackName_l(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3377 | mMainBuffer = playbackThread->mixBuffer(); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3378 | } |
| 3379 | LOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
| 3380 | if (mName < 0) { |
| 3381 | LOGE("no more track names available"); |
| 3382 | } |
| 3383 | mVolume[0] = 1.0f; |
| 3384 | mVolume[1] = 1.0f; |
| 3385 | mStreamType = streamType; |
| 3386 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 3387 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
Eric Laurent | 0950861 | 2011-07-21 19:35:01 -0700 | [diff] [blame] | 3388 | mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3389 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3392 | AudioFlinger::PlaybackThread::Track::~Track() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3393 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3394 | LOGV("PlaybackThread::Track destructor"); |
| 3395 | sp<ThreadBase> thread = mThread.promote(); |
| 3396 | if (thread != 0) { |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3397 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3398 | mState = TERMINATED; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3399 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3402 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 3403 | { |
| 3404 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 3405 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 3406 | // desctructor is called. As the destructor needs to lock mLock, |
| 3407 | // we must acquire a strong reference on this Track before locking mLock |
| 3408 | // here so that the destructor is called only when exiting this function. |
| 3409 | // On the other hand, as long as Track::destroy() is only called by |
| 3410 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 3411 | // this Track with its member mTrack. |
| 3412 | sp<Track> keep(this); |
| 3413 | { // scope for mLock |
| 3414 | sp<ThreadBase> thread = mThread.promote(); |
| 3415 | if (thread != 0) { |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3416 | if (!isOutputTrack()) { |
| 3417 | if (mState == ACTIVE || mState == RESUMING) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3418 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3419 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3420 | mSessionId); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3421 | |
| 3422 | // to track the speaker usage |
| 3423 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | ac196e1 | 2009-12-01 02:17:41 -0800 | [diff] [blame] | 3424 | } |
| 3425 | AudioSystem::releaseOutput(thread->id()); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3426 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3427 | Mutex::Autolock _l(thread->mLock); |
| 3428 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3429 | playbackThread->destroyTrack_l(this); |
| 3430 | } |
| 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3435 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3436 | 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", |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3437 | mName - AudioMixer::TRACK0, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3438 | (mClient == NULL) ? getpid() : mClient->pid(), |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3439 | mStreamType, |
| 3440 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3441 | mChannelMask, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3442 | mSessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3443 | mFrameCount, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3444 | mState, |
| 3445 | mMute, |
| 3446 | mFillingUpStatus, |
| 3447 | mCblk->sampleRate, |
| 3448 | mCblk->volume[0], |
| 3449 | mCblk->volume[1], |
| 3450 | mCblk->server, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3451 | mCblk->user, |
| 3452 | (int)mMainBuffer, |
| 3453 | (int)mAuxBuffer); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3456 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3457 | { |
| 3458 | audio_track_cblk_t* cblk = this->cblk(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3459 | uint32_t framesReady; |
| 3460 | uint32_t framesReq = buffer->frameCount; |
| 3461 | |
| 3462 | // Check if last stepServer failed, try to step now |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3463 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3464 | if (!step()) goto getNextBuffer_exit; |
| 3465 | LOGV("stepServer recovered"); |
| 3466 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3467 | } |
| 3468 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3469 | framesReady = cblk->framesReady(); |
| 3470 | |
| 3471 | if (LIKELY(framesReady)) { |
| 3472 | uint32_t s = cblk->server; |
| 3473 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3474 | |
| 3475 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 3476 | if (framesReq > framesReady) { |
| 3477 | framesReq = framesReady; |
| 3478 | } |
| 3479 | if (s + framesReq > bufferEnd) { |
| 3480 | framesReq = bufferEnd - s; |
| 3481 | } |
| 3482 | |
| 3483 | buffer->raw = getBuffer(s, framesReq); |
| 3484 | if (buffer->raw == 0) goto getNextBuffer_exit; |
| 3485 | |
| 3486 | buffer->frameCount = framesReq; |
| 3487 | return NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3488 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3489 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3490 | getNextBuffer_exit: |
| 3491 | buffer->raw = 0; |
| 3492 | buffer->frameCount = 0; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3493 | LOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3494 | return NOT_ENOUGH_DATA; |
| 3495 | } |
| 3496 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3497 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Eric Laurent | 9a30fc1 | 2010-10-05 14:41:42 -0700 | [diff] [blame] | 3498 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3499 | |
| 3500 | if (mCblk->framesReady() >= mCblk->frameCount || |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3501 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3502 | mFillingUpStatus = FS_FILLED; |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3503 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3504 | return true; |
| 3505 | } |
| 3506 | return false; |
| 3507 | } |
| 3508 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3509 | status_t AudioFlinger::PlaybackThread::Track::start() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3510 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3511 | status_t status = NO_ERROR; |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3512 | LOGV("start(%d), calling thread %d session %d", |
| 3513 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3514 | sp<ThreadBase> thread = mThread.promote(); |
| 3515 | if (thread != 0) { |
| 3516 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3517 | int state = mState; |
| 3518 | // here the track could be either new, or restarted |
| 3519 | // in both cases "unstop" the track |
| 3520 | if (mState == PAUSED) { |
| 3521 | mState = TrackBase::RESUMING; |
| 3522 | LOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
| 3523 | } else { |
| 3524 | mState = TrackBase::ACTIVE; |
| 3525 | LOGV("? => ACTIVE (%d) on thread %p", mName, this); |
| 3526 | } |
| 3527 | |
| 3528 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3529 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3530 | status = AudioSystem::startOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3531 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3532 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3533 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3534 | |
| 3535 | // to track the speaker usage |
| 3536 | if (status == NO_ERROR) { |
| 3537 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 3538 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3539 | } |
| 3540 | if (status == NO_ERROR) { |
| 3541 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3542 | playbackThread->addTrack_l(this); |
| 3543 | } else { |
| 3544 | mState = state; |
| 3545 | } |
| 3546 | } else { |
| 3547 | status = BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3548 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3549 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3550 | } |
| 3551 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3552 | void AudioFlinger::PlaybackThread::Track::stop() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3553 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3554 | LOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
| 3555 | sp<ThreadBase> thread = mThread.promote(); |
| 3556 | if (thread != 0) { |
| 3557 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3558 | int state = mState; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3559 | if (mState > STOPPED) { |
| 3560 | mState = STOPPED; |
| 3561 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3562 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3563 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3564 | reset(); |
| 3565 | } |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3566 | LOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3567 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3568 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3569 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3570 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3571 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3572 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3573 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3574 | |
| 3575 | // to track the speaker usage |
| 3576 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3577 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3578 | } |
| 3579 | } |
| 3580 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3581 | void AudioFlinger::PlaybackThread::Track::pause() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3582 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3583 | LOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3584 | sp<ThreadBase> thread = mThread.promote(); |
| 3585 | if (thread != 0) { |
| 3586 | Mutex::Autolock _l(thread->mLock); |
| 3587 | if (mState == ACTIVE || mState == RESUMING) { |
| 3588 | mState = PAUSING; |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3589 | LOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3590 | if (!isOutputTrack()) { |
| 3591 | thread->mLock.unlock(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3592 | AudioSystem::stopOutput(thread->id(), |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3593 | (audio_stream_type_t)mStreamType, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3594 | mSessionId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3595 | thread->mLock.lock(); |
Gloria Wang | 9b3f152 | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 3596 | |
| 3597 | // to track the speaker usage |
| 3598 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3599 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3600 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3601 | } |
| 3602 | } |
| 3603 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3604 | void AudioFlinger::PlaybackThread::Track::flush() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3605 | { |
| 3606 | LOGV("flush(%d)", mName); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3607 | sp<ThreadBase> thread = mThread.promote(); |
| 3608 | if (thread != 0) { |
| 3609 | Mutex::Autolock _l(thread->mLock); |
| 3610 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3611 | return; |
| 3612 | } |
| 3613 | // No point remaining in PAUSED state after a flush => go to |
| 3614 | // STOPPED state |
| 3615 | mState = STOPPED; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3616 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3617 | // do not reset the track if it is still in the process of being stopped or paused. |
| 3618 | // this will be done by prepareTracks_l() when the track is stopped. |
| 3619 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3620 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3621 | reset(); |
| 3622 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3623 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3624 | } |
| 3625 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3626 | void AudioFlinger::PlaybackThread::Track::reset() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3627 | { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3628 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3629 | // the audioflinger thread detects the track is stopped. |
| 3630 | if (!mResetDone) { |
| 3631 | TrackBase::reset(); |
| 3632 | // Force underrun condition to avoid false underrun callback until first data is |
| 3633 | // written to buffer |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3634 | android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags); |
| 3635 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3636 | mFillingUpStatus = FS_FILLING; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 3637 | mResetDone = true; |
| 3638 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3641 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3642 | { |
| 3643 | mMute = muted; |
| 3644 | } |
| 3645 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3646 | void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3647 | { |
| 3648 | mVolume[0] = left; |
| 3649 | mVolume[1] = right; |
| 3650 | } |
| 3651 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3652 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3653 | { |
| 3654 | status_t status = DEAD_OBJECT; |
| 3655 | sp<ThreadBase> thread = mThread.promote(); |
| 3656 | if (thread != 0) { |
| 3657 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3658 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3659 | } |
| 3660 | return status; |
| 3661 | } |
| 3662 | |
| 3663 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3664 | { |
| 3665 | mAuxEffectId = EffectId; |
| 3666 | mAuxBuffer = buffer; |
| 3667 | } |
| 3668 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3669 | // ---------------------------------------------------------------------------- |
| 3670 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3671 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3672 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 3673 | const wp<ThreadBase>& thread, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3674 | const sp<Client>& client, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3675 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3676 | uint32_t format, |
| 3677 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3678 | int frameCount, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3679 | uint32_t flags, |
| 3680 | int sessionId) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3681 | : TrackBase(thread, client, sampleRate, format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3682 | channelMask, frameCount, flags, 0, sessionId), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3683 | mOverflow(false) |
| 3684 | { |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3685 | if (mCblk != NULL) { |
| 3686 | LOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3687 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3688 | mCblk->frameSize = mChannelCount * sizeof(int16_t); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3689 | } else if (format == AUDIO_FORMAT_PCM_8_BIT) { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3690 | mCblk->frameSize = mChannelCount * sizeof(int8_t); |
Eric Laurent | 8a77a99 | 2009-09-09 05:16:08 -0700 | [diff] [blame] | 3691 | } else { |
| 3692 | mCblk->frameSize = sizeof(int8_t); |
| 3693 | } |
| 3694 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3698 | { |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3699 | sp<ThreadBase> thread = mThread.promote(); |
| 3700 | if (thread != 0) { |
| 3701 | AudioSystem::releaseInput(thread->id()); |
| 3702 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3703 | } |
| 3704 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3705 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3706 | { |
| 3707 | audio_track_cblk_t* cblk = this->cblk(); |
| 3708 | uint32_t framesAvail; |
| 3709 | uint32_t framesReq = buffer->frameCount; |
| 3710 | |
| 3711 | // Check if last stepServer failed, try to step now |
| 3712 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3713 | if (!step()) goto getNextBuffer_exit; |
| 3714 | LOGV("stepServer recovered"); |
| 3715 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3716 | } |
| 3717 | |
| 3718 | framesAvail = cblk->framesAvailable_l(); |
| 3719 | |
| 3720 | if (LIKELY(framesAvail)) { |
| 3721 | uint32_t s = cblk->server; |
| 3722 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3723 | |
| 3724 | if (framesReq > framesAvail) { |
| 3725 | framesReq = framesAvail; |
| 3726 | } |
| 3727 | if (s + framesReq > bufferEnd) { |
| 3728 | framesReq = bufferEnd - s; |
| 3729 | } |
| 3730 | |
| 3731 | buffer->raw = getBuffer(s, framesReq); |
| 3732 | if (buffer->raw == 0) goto getNextBuffer_exit; |
| 3733 | |
| 3734 | buffer->frameCount = framesReq; |
| 3735 | return NO_ERROR; |
| 3736 | } |
| 3737 | |
| 3738 | getNextBuffer_exit: |
| 3739 | buffer->raw = 0; |
| 3740 | buffer->frameCount = 0; |
| 3741 | return NOT_ENOUGH_DATA; |
| 3742 | } |
| 3743 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3744 | status_t AudioFlinger::RecordThread::RecordTrack::start() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3746 | sp<ThreadBase> thread = mThread.promote(); |
| 3747 | if (thread != 0) { |
| 3748 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3749 | return recordThread->start(this); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 3750 | } else { |
| 3751 | return BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3752 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3753 | } |
| 3754 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3755 | void AudioFlinger::RecordThread::RecordTrack::stop() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3756 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3757 | sp<ThreadBase> thread = mThread.promote(); |
| 3758 | if (thread != 0) { |
| 3759 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3760 | recordThread->stop(this); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 3761 | TrackBase::reset(); |
| 3762 | // Force overerrun condition to avoid false overrun callback until first data is |
| 3763 | // read from buffer |
| 3764 | android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3765 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | } |
| 3767 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3768 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 3769 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3770 | snprintf(buffer, size, " %05d %03u 0x%08x %05d %04u %01d %05u %08x %08x\n", |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3771 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3772 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3773 | mChannelMask, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 3774 | mSessionId, |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 3775 | mFrameCount, |
| 3776 | mState, |
| 3777 | mCblk->sampleRate, |
| 3778 | mCblk->server, |
| 3779 | mCblk->user); |
| 3780 | } |
| 3781 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | |
| 3783 | // ---------------------------------------------------------------------------- |
| 3784 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3785 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 3786 | const wp<ThreadBase>& thread, |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3787 | DuplicatingThread *sourceThread, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3788 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3789 | uint32_t format, |
| 3790 | uint32_t channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3791 | int frameCount) |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3792 | : Track(thread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0), |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3793 | mActive(false), mSourceThread(sourceThread) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3794 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3795 | |
| 3796 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3797 | if (mCblk != NULL) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 3798 | mCblk->flags |= CBLK_DIRECTION_OUT; |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3799 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3800 | mCblk->volume[0] = mCblk->volume[1] = 0x1000; |
| 3801 | mOutBuffer.frameCount = 0; |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3802 | playbackThread->mTracks.add(this); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3803 | LOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \ |
| 3804 | "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p", |
| 3805 | mCblk, mBuffer, mCblk->buffers, |
| 3806 | mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd); |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3807 | } else { |
| 3808 | LOGW("Error creating output track on thread %p", playbackThread); |
| 3809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3810 | } |
| 3811 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3812 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3813 | { |
Eric Laurent | 6c30a71 | 2009-08-10 23:22:32 -0700 | [diff] [blame] | 3814 | clearBufferQueue(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3815 | } |
| 3816 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3817 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3818 | { |
| 3819 | status_t status = Track::start(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3820 | if (status != NO_ERROR) { |
| 3821 | return status; |
| 3822 | } |
| 3823 | |
| 3824 | mActive = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3825 | mRetryCount = 127; |
| 3826 | return status; |
| 3827 | } |
| 3828 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3829 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | { |
| 3831 | Track::stop(); |
| 3832 | clearBufferQueue(); |
| 3833 | mOutBuffer.frameCount = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3834 | mActive = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3837 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3838 | { |
| 3839 | Buffer *pInBuffer; |
| 3840 | Buffer inBuffer; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 3841 | uint32_t channelCount = mChannelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3842 | bool outputBufferFull = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3843 | inBuffer.frameCount = frames; |
| 3844 | inBuffer.i16 = data; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3845 | |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3846 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3847 | |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3848 | if (!mActive && frames != 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3849 | start(); |
| 3850 | sp<ThreadBase> thread = mThread.promote(); |
| 3851 | if (thread != 0) { |
| 3852 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 3853 | if (mCblk->frameCount > frames){ |
| 3854 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3855 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 3856 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3857 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3858 | pInBuffer->frameCount = startFrames; |
| 3859 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3860 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3861 | mBufferQueue.add(pInBuffer); |
| 3862 | } else { |
| 3863 | LOGW ("OutputTrack::write() %p no more buffers in queue", this); |
| 3864 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3865 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3867 | } |
| 3868 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3869 | while (waitTimeLeftMs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3870 | // First write pending buffers, then new data |
| 3871 | if (mBufferQueue.size()) { |
| 3872 | pInBuffer = mBufferQueue.itemAt(0); |
| 3873 | } else { |
| 3874 | pInBuffer = &inBuffer; |
| 3875 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3876 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3877 | if (pInBuffer->frameCount == 0) { |
| 3878 | break; |
| 3879 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3881 | if (mOutBuffer.frameCount == 0) { |
| 3882 | mOutBuffer.frameCount = pInBuffer->frameCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3883 | nsecs_t startTime = systemTime(); |
| 3884 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) { |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3885 | LOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3886 | outputBufferFull = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3887 | break; |
| 3888 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3889 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3890 | if (waitTimeLeftMs >= waitTimeMs) { |
| 3891 | waitTimeLeftMs -= waitTimeMs; |
| 3892 | } else { |
| 3893 | waitTimeLeftMs = 0; |
| 3894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3895 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3897 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3898 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | mCblk->stepUser(outFrames); |
| 3900 | pInBuffer->frameCount -= outFrames; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3901 | pInBuffer->i16 += outFrames * channelCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3902 | mOutBuffer.frameCount -= outFrames; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3903 | mOutBuffer.i16 += outFrames * channelCount; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3905 | if (pInBuffer->frameCount == 0) { |
| 3906 | if (mBufferQueue.size()) { |
| 3907 | mBufferQueue.removeAt(0); |
| 3908 | delete [] pInBuffer->mBuffer; |
| 3909 | delete pInBuffer; |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3910 | LOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3911 | } else { |
| 3912 | break; |
| 3913 | } |
| 3914 | } |
| 3915 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3916 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3917 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 3918 | if (inBuffer.frameCount) { |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3919 | sp<ThreadBase> thread = mThread.promote(); |
| 3920 | if (thread != 0 && !thread->standby()) { |
| 3921 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3922 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3923 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3924 | pInBuffer->frameCount = inBuffer.frameCount; |
| 3925 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3926 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
Eric Laurent | 8ac9f8d | 2009-12-18 05:47:48 -0800 | [diff] [blame] | 3927 | mBufferQueue.add(pInBuffer); |
| 3928 | LOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
| 3929 | } else { |
| 3930 | LOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
| 3931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | } |
| 3933 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3934 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3935 | // Calling write() with a 0 length buffer, means that no more data will be written: |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3936 | // If no more buffers are pending, fill output track buffer to make sure it is started |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3937 | // by output mixer. |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3938 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 3939 | if (mCblk->user < mCblk->frameCount) { |
| 3940 | frames = mCblk->frameCount - mCblk->user; |
| 3941 | pInBuffer = new Buffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3942 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3943 | pInBuffer->frameCount = frames; |
| 3944 | pInBuffer->i16 = pInBuffer->mBuffer; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 3945 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3946 | mBufferQueue.add(pInBuffer); |
Eric Laurent | 62443f5 | 2009-10-05 20:29:18 -0700 | [diff] [blame] | 3947 | } else if (mActive) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3948 | stop(); |
| 3949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3952 | return outputBufferFull; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3955 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3956 | { |
| 3957 | int active; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | status_t result; |
| 3959 | audio_track_cblk_t* cblk = mCblk; |
| 3960 | uint32_t framesReq = buffer->frameCount; |
| 3961 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3962 | // LOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3963 | buffer->frameCount = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3965 | uint32_t framesAvail = cblk->framesAvailable(); |
| 3966 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3967 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3968 | if (framesAvail == 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3969 | Mutex::Autolock _l(cblk->lock); |
| 3970 | goto start_loop_here; |
| 3971 | while (framesAvail == 0) { |
| 3972 | active = mActive; |
| 3973 | if (UNLIKELY(!active)) { |
| 3974 | LOGV("Not active and NO_MORE_BUFFERS"); |
| 3975 | return AudioTrack::NO_MORE_BUFFERS; |
| 3976 | } |
| 3977 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 3978 | if (result != NO_ERROR) { |
| 3979 | return AudioTrack::NO_MORE_BUFFERS; |
| 3980 | } |
| 3981 | // read the server count again |
| 3982 | start_loop_here: |
| 3983 | framesAvail = cblk->framesAvailable_l(); |
| 3984 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3985 | } |
| 3986 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3987 | // if (framesAvail < framesReq) { |
| 3988 | // return AudioTrack::NO_MORE_BUFFERS; |
| 3989 | // } |
| 3990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3991 | if (framesReq > framesAvail) { |
| 3992 | framesReq = framesAvail; |
| 3993 | } |
| 3994 | |
| 3995 | uint32_t u = cblk->user; |
| 3996 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 3997 | |
| 3998 | if (u + framesReq > bufferEnd) { |
| 3999 | framesReq = bufferEnd - u; |
| 4000 | } |
| 4001 | |
| 4002 | buffer->frameCount = framesReq; |
| 4003 | buffer->raw = (void *)cblk->buffer(u); |
| 4004 | return NO_ERROR; |
| 4005 | } |
| 4006 | |
| 4007 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4008 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4009 | { |
| 4010 | size_t size = mBufferQueue.size(); |
| 4011 | Buffer *pBuffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4012 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4013 | for (size_t i = 0; i < size; i++) { |
| 4014 | pBuffer = mBufferQueue.itemAt(i); |
| 4015 | delete [] pBuffer->mBuffer; |
| 4016 | delete pBuffer; |
| 4017 | } |
| 4018 | mBufferQueue.clear(); |
| 4019 | } |
| 4020 | |
| 4021 | // ---------------------------------------------------------------------------- |
| 4022 | |
| 4023 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 4024 | : RefBase(), |
| 4025 | mAudioFlinger(audioFlinger), |
Mathias Agopian | 6faf789 | 2010-01-25 19:00:00 -0800 | [diff] [blame] | 4026 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4027 | mPid(pid) |
| 4028 | { |
| 4029 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 4030 | } |
| 4031 | |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4032 | // Client destructor must be called with AudioFlinger::mLock held |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | AudioFlinger::Client::~Client() |
| 4034 | { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4035 | mAudioFlinger->removeClient_l(mPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const |
| 4039 | { |
| 4040 | return mMemoryDealer; |
| 4041 | } |
| 4042 | |
| 4043 | // ---------------------------------------------------------------------------- |
| 4044 | |
Eric Laurent | 4f0f17d | 2010-05-12 02:05:53 -0700 | [diff] [blame] | 4045 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 4046 | const sp<IAudioFlingerClient>& client, |
| 4047 | pid_t pid) |
| 4048 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) |
| 4049 | { |
| 4050 | } |
| 4051 | |
| 4052 | AudioFlinger::NotificationClient::~NotificationClient() |
| 4053 | { |
| 4054 | mClient.clear(); |
| 4055 | } |
| 4056 | |
| 4057 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 4058 | { |
| 4059 | sp<NotificationClient> keep(this); |
| 4060 | { |
| 4061 | mAudioFlinger->removeNotificationClient(mPid); |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | // ---------------------------------------------------------------------------- |
| 4066 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4067 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4068 | : BnAudioTrack(), |
| 4069 | mTrack(track) |
| 4070 | { |
| 4071 | } |
| 4072 | |
| 4073 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 4074 | // just stop the track on deletion, associated resources |
| 4075 | // will be freed from the main thread once all pending buffers have |
| 4076 | // been played. Unless it's not in the active track list, in which |
| 4077 | // case we free everything now... |
| 4078 | mTrack->destroy(); |
| 4079 | } |
| 4080 | |
| 4081 | status_t AudioFlinger::TrackHandle::start() { |
| 4082 | return mTrack->start(); |
| 4083 | } |
| 4084 | |
| 4085 | void AudioFlinger::TrackHandle::stop() { |
| 4086 | mTrack->stop(); |
| 4087 | } |
| 4088 | |
| 4089 | void AudioFlinger::TrackHandle::flush() { |
| 4090 | mTrack->flush(); |
| 4091 | } |
| 4092 | |
| 4093 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 4094 | mTrack->mute(e); |
| 4095 | } |
| 4096 | |
| 4097 | void AudioFlinger::TrackHandle::pause() { |
| 4098 | mTrack->pause(); |
| 4099 | } |
| 4100 | |
| 4101 | void AudioFlinger::TrackHandle::setVolume(float left, float right) { |
| 4102 | mTrack->setVolume(left, right); |
| 4103 | } |
| 4104 | |
| 4105 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 4106 | return mTrack->getCblk(); |
| 4107 | } |
| 4108 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4109 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 4110 | { |
| 4111 | return mTrack->attachAuxEffect(EffectId); |
| 4112 | } |
| 4113 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4114 | status_t AudioFlinger::TrackHandle::onTransact( |
| 4115 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4116 | { |
| 4117 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 4118 | } |
| 4119 | |
| 4120 | // ---------------------------------------------------------------------------- |
| 4121 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4122 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 4123 | pid_t pid, |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4124 | int input, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4125 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4126 | uint32_t format, |
| 4127 | uint32_t channelMask, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4128 | int frameCount, |
| 4129 | uint32_t flags, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4130 | int *sessionId, |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4131 | status_t *status) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4132 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4133 | sp<RecordThread::RecordTrack> recordTrack; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4134 | sp<RecordHandle> recordHandle; |
| 4135 | sp<Client> client; |
| 4136 | wp<Client> wclient; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4137 | status_t lStatus; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4138 | RecordThread *thread; |
| 4139 | size_t inFrameCount; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4140 | int lSessionId; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4141 | |
| 4142 | // check calling permissions |
| 4143 | if (!recordingAllowed()) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4144 | lStatus = PERMISSION_DENIED; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4145 | goto Exit; |
| 4146 | } |
| 4147 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4148 | // add client to list |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4149 | { // scope for mLock |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4150 | Mutex::Autolock _l(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4151 | thread = checkRecordThread_l(input); |
| 4152 | if (thread == NULL) { |
| 4153 | lStatus = BAD_VALUE; |
| 4154 | goto Exit; |
| 4155 | } |
| 4156 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4157 | wclient = mClients.valueFor(pid); |
| 4158 | if (wclient != NULL) { |
| 4159 | client = wclient.promote(); |
| 4160 | } else { |
| 4161 | client = new Client(this, pid); |
| 4162 | mClients.add(pid, client); |
| 4163 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4164 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4165 | // If no audio session id is provided, create one here |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4166 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4167 | lSessionId = *sessionId; |
| 4168 | } else { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4169 | lSessionId = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4170 | if (sessionId != NULL) { |
| 4171 | *sessionId = lSessionId; |
| 4172 | } |
| 4173 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4174 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4175 | recordTrack = thread->createRecordTrack_l(client, |
| 4176 | sampleRate, |
| 4177 | format, |
| 4178 | channelMask, |
| 4179 | frameCount, |
| 4180 | flags, |
| 4181 | lSessionId, |
| 4182 | &lStatus); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4183 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4184 | if (lStatus != NO_ERROR) { |
Eric Laurent | b9481d8 | 2009-09-17 05:12:56 -0700 | [diff] [blame] | 4185 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 4186 | // destructor is called by the TrackBase destructor with mLock held |
| 4187 | client.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4188 | recordTrack.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4189 | goto Exit; |
| 4190 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4191 | |
| 4192 | // return to handle to client |
| 4193 | recordHandle = new RecordHandle(recordTrack); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4194 | lStatus = NO_ERROR; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4195 | |
| 4196 | Exit: |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4197 | if (status) { |
| 4198 | *status = lStatus; |
| 4199 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4200 | return recordHandle; |
| 4201 | } |
| 4202 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4203 | // ---------------------------------------------------------------------------- |
| 4204 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4205 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4206 | : BnAudioRecord(), |
| 4207 | mRecordTrack(recordTrack) |
| 4208 | { |
| 4209 | } |
| 4210 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4211 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 4212 | stop(); |
| 4213 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4214 | |
| 4215 | status_t AudioFlinger::RecordHandle::start() { |
| 4216 | LOGV("RecordHandle::start()"); |
| 4217 | return mRecordTrack->start(); |
| 4218 | } |
| 4219 | |
| 4220 | void AudioFlinger::RecordHandle::stop() { |
| 4221 | LOGV("RecordHandle::stop()"); |
| 4222 | mRecordTrack->stop(); |
| 4223 | } |
| 4224 | |
| 4225 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 4226 | return mRecordTrack->getCblk(); |
| 4227 | } |
| 4228 | |
| 4229 | status_t AudioFlinger::RecordHandle::onTransact( |
| 4230 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4231 | { |
| 4232 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 4233 | } |
| 4234 | |
| 4235 | // ---------------------------------------------------------------------------- |
| 4236 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4237 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4238 | AudioStreamIn *input, |
| 4239 | uint32_t sampleRate, |
| 4240 | uint32_t channels, |
| 4241 | int id, |
| 4242 | uint32_t device) : |
| 4243 | ThreadBase(audioFlinger, id, device), |
| 4244 | mInput(input), mTrack(NULL), mResampler(0), mRsmpOutBuffer(0), mRsmpInBuffer(0) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4245 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4246 | mType = ThreadBase::RECORD; |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4247 | |
| 4248 | snprintf(mName, kNameLength, "AudioIn_%d", id); |
| 4249 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4250 | mReqChannelCount = popcount(channels); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4251 | mReqSampleRate = sampleRate; |
| 4252 | readInputParameters(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4255 | |
| 4256 | AudioFlinger::RecordThread::~RecordThread() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4257 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4258 | delete[] mRsmpInBuffer; |
| 4259 | if (mResampler != 0) { |
| 4260 | delete mResampler; |
| 4261 | delete[] mRsmpOutBuffer; |
| 4262 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4265 | void AudioFlinger::RecordThread::onFirstRef() |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4266 | { |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4267 | run(mName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4268 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4269 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4270 | status_t AudioFlinger::RecordThread::readyToRun() |
| 4271 | { |
| 4272 | status_t status = initCheck(); |
| 4273 | LOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); |
| 4274 | return status; |
| 4275 | } |
| 4276 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4277 | bool AudioFlinger::RecordThread::threadLoop() |
| 4278 | { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4279 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4280 | sp<RecordTrack> activeTrack; |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4281 | Vector< sp<EffectChain> > effectChains; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4282 | |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4283 | nsecs_t lastWarning = 0; |
| 4284 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4285 | acquireWakeLock(); |
| 4286 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4287 | // start recording |
| 4288 | while (!exitPending()) { |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4289 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4290 | processConfigEvents(); |
| 4291 | |
| 4292 | { // scope for mLock |
| 4293 | Mutex::Autolock _l(mLock); |
| 4294 | checkForNewParameters_l(); |
| 4295 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 4296 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4297 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4298 | mStandby = true; |
| 4299 | } |
| 4300 | |
| 4301 | if (exitPending()) break; |
| 4302 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4303 | releaseWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4304 | LOGV("RecordThread: loop stopping"); |
| 4305 | // go to sleep |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4306 | mWaitWorkCV.wait(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4307 | LOGV("RecordThread: loop starting"); |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4308 | acquireWakeLock_l(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4309 | continue; |
| 4310 | } |
| 4311 | if (mActiveTrack != 0) { |
| 4312 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4313 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4314 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4315 | mStandby = true; |
| 4316 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4317 | mActiveTrack.clear(); |
| 4318 | mStartStopCond.broadcast(); |
| 4319 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4320 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 4321 | mActiveTrack.clear(); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4322 | mStartStopCond.broadcast(); |
| 4323 | } else if (mBytesRead != 0) { |
| 4324 | // record start succeeds only if first read from audio input |
| 4325 | // succeeds |
| 4326 | if (mBytesRead > 0) { |
| 4327 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 4328 | } else { |
| 4329 | mActiveTrack.clear(); |
| 4330 | } |
| 4331 | mStartStopCond.broadcast(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4332 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4333 | mStandby = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4334 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4335 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4336 | lockEffectChains_l(effectChains); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | if (mActiveTrack != 0) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4340 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 4341 | mActiveTrack->mState != TrackBase::RESUMING) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4342 | unlockEffectChains(effectChains); |
| 4343 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4344 | continue; |
| 4345 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4346 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 4347 | effectChains[i]->process_l(); |
| 4348 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4349 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4350 | buffer.frameCount = mFrameCount; |
| 4351 | if (LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
| 4352 | size_t framesOut = buffer.frameCount; |
| 4353 | if (mResampler == 0) { |
| 4354 | // no resampling |
| 4355 | while (framesOut) { |
| 4356 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 4357 | if (framesIn) { |
| 4358 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 4359 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 4360 | if (framesIn > framesOut) |
| 4361 | framesIn = framesOut; |
| 4362 | mRsmpInIndex += framesIn; |
| 4363 | framesOut -= framesIn; |
Eric Laurent | b0a0147 | 2010-05-14 05:45:46 -0700 | [diff] [blame] | 4364 | if ((int)mChannelCount == mReqChannelCount || |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4365 | mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4366 | memcpy(dst, src, framesIn * mFrameSize); |
| 4367 | } else { |
| 4368 | int16_t *src16 = (int16_t *)src; |
| 4369 | int16_t *dst16 = (int16_t *)dst; |
| 4370 | if (mChannelCount == 1) { |
| 4371 | while (framesIn--) { |
| 4372 | *dst16++ = *src16; |
| 4373 | *dst16++ = *src16++; |
| 4374 | } |
| 4375 | } else { |
| 4376 | while (framesIn--) { |
| 4377 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 4378 | src16 += 2; |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | if (framesOut && mFrameCount == mRsmpInIndex) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4384 | if (framesOut == mFrameCount && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4385 | ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4386 | mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4387 | framesOut = 0; |
| 4388 | } else { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4389 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4390 | mRsmpInIndex = 0; |
| 4391 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4392 | if (mBytesRead < 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4393 | LOGE("Error reading audio input"); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4394 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 4395 | // Force input into standby so that it tries to |
| 4396 | // recover at next read attempt |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4397 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4398 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4399 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4400 | mRsmpInIndex = mFrameCount; |
| 4401 | framesOut = 0; |
| 4402 | buffer.frameCount = 0; |
| 4403 | } |
| 4404 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4405 | } |
| 4406 | } else { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4407 | // resampling |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4408 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4409 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 4410 | // alter output frame count as if we were expecting stereo samples |
| 4411 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4412 | framesOut >>= 1; |
| 4413 | } |
| 4414 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 4415 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 4416 | // are 32 bit aligned which should be always true. |
| 4417 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
| 4418 | AudioMixer::ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
| 4419 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 4420 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 4421 | int16_t *dst = buffer.i16; |
| 4422 | while (framesOut--) { |
| 4423 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 4424 | src += 2; |
| 4425 | } |
| 4426 | } else { |
| 4427 | AudioMixer::ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
| 4428 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4429 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4430 | } |
| 4431 | mActiveTrack->releaseBuffer(&buffer); |
| 4432 | mActiveTrack->overflow(); |
| 4433 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4434 | // client isn't retrieving buffers fast enough |
| 4435 | else { |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 4436 | if (!mActiveTrack->setOverflow()) { |
| 4437 | nsecs_t now = systemTime(); |
| 4438 | if ((now - lastWarning) > kWarningThrottle) { |
| 4439 | LOGW("RecordThread: buffer overflow"); |
| 4440 | lastWarning = now; |
| 4441 | } |
| 4442 | } |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4443 | // Release the processor for a while before asking for a new buffer. |
| 4444 | // This will give the application more chance to read from the buffer and |
| 4445 | // clear the overflow. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4446 | usleep(kRecordThreadSleepUs); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4447 | } |
| 4448 | } |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 4449 | // enable changes in effect chain |
| 4450 | unlockEffectChains(effectChains); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4451 | effectChains.clear(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4452 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4453 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4454 | if (!mStandby) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4455 | mInput->stream->common.standby(&mInput->stream->common); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 4456 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4457 | mActiveTrack.clear(); |
| 4458 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4459 | mStartStopCond.broadcast(); |
| 4460 | |
Eric Laurent | 6dbdc40 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 4461 | releaseWakeLock(); |
| 4462 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4463 | LOGV("RecordThread %p exiting", this); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4464 | return false; |
| 4465 | } |
| 4466 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4467 | |
| 4468 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
| 4469 | const sp<AudioFlinger::Client>& client, |
| 4470 | uint32_t sampleRate, |
| 4471 | int format, |
| 4472 | int channelMask, |
| 4473 | int frameCount, |
| 4474 | uint32_t flags, |
| 4475 | int sessionId, |
| 4476 | status_t *status) |
| 4477 | { |
| 4478 | sp<RecordTrack> track; |
| 4479 | status_t lStatus; |
| 4480 | |
| 4481 | lStatus = initCheck(); |
| 4482 | if (lStatus != NO_ERROR) { |
| 4483 | LOGE("Audio driver not initialized."); |
| 4484 | goto Exit; |
| 4485 | } |
| 4486 | |
| 4487 | { // scope for mLock |
| 4488 | Mutex::Autolock _l(mLock); |
| 4489 | |
| 4490 | track = new RecordTrack(this, client, sampleRate, |
| 4491 | format, channelMask, frameCount, flags, sessionId); |
| 4492 | |
| 4493 | if (track->getCblk() == NULL) { |
| 4494 | lStatus = NO_MEMORY; |
| 4495 | goto Exit; |
| 4496 | } |
| 4497 | |
| 4498 | mTrack = track.get(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4499 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4500 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4501 | (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4502 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 4503 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4504 | } |
| 4505 | lStatus = NO_ERROR; |
| 4506 | |
| 4507 | Exit: |
| 4508 | if (status) { |
| 4509 | *status = lStatus; |
| 4510 | } |
| 4511 | return track; |
| 4512 | } |
| 4513 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4514 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4515 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4516 | LOGV("RecordThread::start"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4517 | sp <ThreadBase> strongMe = this; |
| 4518 | status_t status = NO_ERROR; |
| 4519 | { |
| 4520 | AutoMutex lock(&mLock); |
| 4521 | if (mActiveTrack != 0) { |
| 4522 | if (recordTrack != mActiveTrack.get()) { |
| 4523 | status = -EBUSY; |
| 4524 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4525 | mActiveTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4526 | } |
| 4527 | return status; |
| 4528 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4529 | |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4530 | recordTrack->mState = TrackBase::IDLE; |
| 4531 | mActiveTrack = recordTrack; |
| 4532 | mLock.unlock(); |
| 4533 | status_t status = AudioSystem::startInput(mId); |
| 4534 | mLock.lock(); |
| 4535 | if (status != NO_ERROR) { |
| 4536 | mActiveTrack.clear(); |
| 4537 | return status; |
| 4538 | } |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4539 | mRsmpInIndex = mFrameCount; |
| 4540 | mBytesRead = 0; |
Eric Laurent | 4bb21c4 | 2011-02-28 16:52:51 -0800 | [diff] [blame] | 4541 | if (mResampler != NULL) { |
| 4542 | mResampler->reset(); |
| 4543 | } |
| 4544 | mActiveTrack->mState = TrackBase::RESUMING; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4545 | // signal thread to start |
| 4546 | LOGV("Signal record thread"); |
| 4547 | mWaitWorkCV.signal(); |
| 4548 | // do not wait for mStartStopCond if exiting |
| 4549 | if (mExiting) { |
| 4550 | mActiveTrack.clear(); |
| 4551 | status = INVALID_OPERATION; |
| 4552 | goto startError; |
| 4553 | } |
| 4554 | mStartStopCond.wait(mLock); |
| 4555 | if (mActiveTrack == 0) { |
| 4556 | LOGV("Record failed to start"); |
| 4557 | status = BAD_VALUE; |
| 4558 | goto startError; |
| 4559 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4560 | LOGV("Record started OK"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4561 | return status; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4562 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4563 | startError: |
| 4564 | AudioSystem::stopInput(mId); |
| 4565 | return status; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4566 | } |
| 4567 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4568 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
| 4569 | LOGV("RecordThread::stop"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4570 | sp <ThreadBase> strongMe = this; |
| 4571 | { |
| 4572 | AutoMutex lock(&mLock); |
| 4573 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 4574 | mActiveTrack->mState = TrackBase::PAUSING; |
| 4575 | // do not wait for mStartStopCond if exiting |
| 4576 | if (mExiting) { |
| 4577 | return; |
| 4578 | } |
| 4579 | mStartStopCond.wait(mLock); |
| 4580 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 4581 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 4582 | mLock.unlock(); |
| 4583 | AudioSystem::stopInput(mId); |
| 4584 | mLock.lock(); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4585 | LOGV("Record stopped OK"); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4586 | } |
| 4587 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4588 | } |
| 4589 | } |
| 4590 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4591 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4592 | { |
| 4593 | const size_t SIZE = 256; |
| 4594 | char buffer[SIZE]; |
| 4595 | String8 result; |
| 4596 | pid_t pid = 0; |
| 4597 | |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4598 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 4599 | result.append(buffer); |
| 4600 | |
| 4601 | if (mActiveTrack != 0) { |
| 4602 | result.append("Active Track:\n"); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4603 | result.append(" Clien Fmt Chn mask Session Buf S SRate Serv User\n"); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4604 | mActiveTrack->dump(buffer, SIZE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4605 | result.append(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4606 | |
| 4607 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 4608 | result.append(buffer); |
| 4609 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 4610 | result.append(buffer); |
| 4611 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != 0)); |
| 4612 | result.append(buffer); |
| 4613 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 4614 | result.append(buffer); |
| 4615 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 4616 | result.append(buffer); |
| 4617 | |
| 4618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4619 | } else { |
| 4620 | result.append("No record client\n"); |
| 4621 | } |
| 4622 | write(fd, result.string(), result.size()); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4623 | |
| 4624 | dumpBase(fd, args); |
Eric Laurent | 1345d33 | 2011-07-24 17:49:51 -0700 | [diff] [blame] | 4625 | dumpEffectChains(fd, args); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4627 | return NO_ERROR; |
| 4628 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4629 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4630 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 4631 | { |
| 4632 | size_t framesReq = buffer->frameCount; |
| 4633 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 4634 | int channelCount; |
| 4635 | |
| 4636 | if (framesReady == 0) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4637 | mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4638 | if (mBytesRead < 0) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4639 | LOGE("RecordThread::getNextBuffer() Error reading audio input"); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4640 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
Eric Laurent | ba8811f | 2010-03-02 18:38:06 -0800 | [diff] [blame] | 4641 | // Force input into standby so that it tries to |
| 4642 | // recover at next read attempt |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4643 | mInput->stream->common.standby(&mInput->stream->common); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4644 | usleep(kRecordThreadSleepUs); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4645 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4646 | buffer->raw = 0; |
| 4647 | buffer->frameCount = 0; |
| 4648 | return NOT_ENOUGH_DATA; |
| 4649 | } |
| 4650 | mRsmpInIndex = 0; |
| 4651 | framesReady = mFrameCount; |
| 4652 | } |
| 4653 | |
| 4654 | if (framesReq > framesReady) { |
| 4655 | framesReq = framesReady; |
| 4656 | } |
| 4657 | |
| 4658 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4659 | channelCount = 1; |
| 4660 | } else { |
| 4661 | channelCount = 2; |
| 4662 | } |
| 4663 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 4664 | buffer->frameCount = framesReq; |
| 4665 | return NO_ERROR; |
| 4666 | } |
| 4667 | |
| 4668 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4669 | { |
| 4670 | mRsmpInIndex += buffer->frameCount; |
| 4671 | buffer->frameCount = 0; |
| 4672 | } |
| 4673 | |
| 4674 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 4675 | { |
| 4676 | bool reconfig = false; |
| 4677 | |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4678 | while (!mNewParameters.isEmpty()) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4679 | status_t status = NO_ERROR; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4680 | String8 keyValuePair = mNewParameters[0]; |
| 4681 | AudioParameter param = AudioParameter(keyValuePair); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4682 | int value; |
| 4683 | int reqFormat = mFormat; |
| 4684 | int reqSamplingRate = mReqSampleRate; |
| 4685 | int reqChannelCount = mReqChannelCount; |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4686 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4687 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4688 | reqSamplingRate = value; |
| 4689 | reconfig = true; |
| 4690 | } |
| 4691 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 4692 | reqFormat = value; |
| 4693 | reconfig = true; |
| 4694 | } |
| 4695 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4696 | reqChannelCount = popcount(value); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4697 | reconfig = true; |
| 4698 | } |
| 4699 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4700 | // do not accept frame count changes if tracks are open as the track buffer |
| 4701 | // size depends on frame count and correct behavior would not be garantied |
| 4702 | // if frame count is changed after track creation |
| 4703 | if (mActiveTrack != 0) { |
| 4704 | status = INVALID_OPERATION; |
| 4705 | } else { |
| 4706 | reconfig = true; |
| 4707 | } |
| 4708 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4709 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4710 | // forward device change to effects that have requested to be |
| 4711 | // aware of attached audio device. |
| 4712 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4713 | mEffectChains[i]->setDevice_l(value); |
| 4714 | } |
| 4715 | // store input device and output device but do not forward output device to audio HAL. |
| 4716 | // Note that status is ignored by the caller for output device |
| 4717 | // (see AudioFlinger::setParameters() |
| 4718 | if (value & AUDIO_DEVICE_OUT_ALL) { |
| 4719 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL); |
| 4720 | status = BAD_VALUE; |
| 4721 | } else { |
| 4722 | mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4723 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 4724 | if (mTrack != NULL) { |
| 4725 | bool suspend = audio_is_bluetooth_sco_device( |
Eric Laurent | 2d95dfb | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 4726 | (audio_devices_t)value) && mAudioFlinger->btNrecIsOff(); |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4727 | setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId()); |
| 4728 | setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId()); |
| 4729 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4730 | } |
| 4731 | mDevice |= (uint32_t)value; |
| 4732 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4733 | if (status == NO_ERROR) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4734 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4735 | if (status == INVALID_OPERATION) { |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4736 | mInput->stream->common.standby(&mInput->stream->common); |
| 4737 | status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4738 | } |
| 4739 | if (reconfig) { |
| 4740 | if (status == BAD_VALUE && |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4741 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4742 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4743 | ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) && |
| 4744 | (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4745 | (reqChannelCount < 3)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4746 | status = NO_ERROR; |
| 4747 | } |
| 4748 | if (status == NO_ERROR) { |
| 4749 | readInputParameters(); |
Eric Laurent | 8fce46a | 2009-08-04 09:45:33 -0700 | [diff] [blame] | 4750 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 4754 | |
| 4755 | mNewParameters.removeAt(0); |
| 4756 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4757 | mParamStatus = status; |
| 4758 | mParamCond.signal(); |
Eric Laurent | 5f37be3 | 2011-09-13 11:40:21 -0700 | [diff] [blame] | 4759 | // wait for condition with time out in case the thread calling ThreadBase::setParameters() |
| 4760 | // already timed out waiting for the status and will never signal the condition. |
| 4761 | mWaitWorkCV.waitRelative(mLock, kSetParametersTimeout); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4762 | } |
| 4763 | return reconfig; |
| 4764 | } |
| 4765 | |
| 4766 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 4767 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4768 | char *s; |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4769 | String8 out_s8 = String8(); |
| 4770 | |
| 4771 | Mutex::Autolock _l(mLock); |
| 4772 | if (initCheck() != NO_ERROR) { |
| 4773 | return out_s8; |
| 4774 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4775 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4776 | s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4777 | out_s8 = String8(s); |
| 4778 | free(s); |
| 4779 | return out_s8; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4780 | } |
| 4781 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 4782 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4783 | AudioSystem::OutputDescriptor desc; |
| 4784 | void *param2 = 0; |
| 4785 | |
| 4786 | switch (event) { |
| 4787 | case AudioSystem::INPUT_OPENED: |
| 4788 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4789 | desc.channels = mChannelMask; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4790 | desc.samplingRate = mSampleRate; |
| 4791 | desc.format = mFormat; |
| 4792 | desc.frameCount = mFrameCount; |
| 4793 | desc.latency = 0; |
| 4794 | param2 = &desc; |
| 4795 | break; |
| 4796 | |
| 4797 | case AudioSystem::INPUT_CLOSED: |
| 4798 | default: |
| 4799 | break; |
| 4800 | } |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 4801 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | void AudioFlinger::RecordThread::readInputParameters() |
| 4805 | { |
| 4806 | if (mRsmpInBuffer) delete mRsmpInBuffer; |
| 4807 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; |
| 4808 | if (mResampler) delete mResampler; |
| 4809 | mResampler = 0; |
| 4810 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4811 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 4812 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
| 4813 | mChannelCount = (uint16_t)popcount(mChannelMask); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4814 | mFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 4815 | mFrameSize = (uint16_t)audio_stream_frame_size(&mInput->stream->common); |
| 4816 | mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4817 | mFrameCount = mInputBytes / mFrameSize; |
| 4818 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 4819 | |
| 4820 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 4821 | { |
| 4822 | int channelCount; |
| 4823 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 4824 | // stereo to mono post process as the resampler always outputs stereo. |
| 4825 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4826 | channelCount = 1; |
| 4827 | } else { |
| 4828 | channelCount = 2; |
| 4829 | } |
| 4830 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 4831 | mResampler->setSampleRate(mSampleRate); |
| 4832 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 4833 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 4834 | |
| 4835 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 4836 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4837 | mFrameCount >>= 1; |
| 4838 | } |
| 4839 | |
| 4840 | } |
| 4841 | mRsmpInIndex = mFrameCount; |
| 4842 | } |
| 4843 | |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 4844 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 4845 | { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4846 | Mutex::Autolock _l(mLock); |
| 4847 | if (initCheck() != NO_ERROR) { |
| 4848 | return 0; |
| 4849 | } |
| 4850 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4851 | return mInput->stream->get_input_frames_lost(mInput->stream); |
Eric Laurent | 47d0a92 | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 4852 | } |
| 4853 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4854 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) |
| 4855 | { |
| 4856 | Mutex::Autolock _l(mLock); |
| 4857 | uint32_t result = 0; |
| 4858 | if (getEffectChain_l(sessionId) != 0) { |
| 4859 | result = EFFECT_SESSION; |
| 4860 | } |
| 4861 | |
| 4862 | if (mTrack != NULL && sessionId == mTrack->sessionId()) { |
| 4863 | result |= TRACK_SESSION; |
| 4864 | } |
| 4865 | |
| 4866 | return result; |
| 4867 | } |
| 4868 | |
Eric Laurent | 6639b55 | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 4869 | AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track() |
| 4870 | { |
| 4871 | Mutex::Autolock _l(mLock); |
| 4872 | return mTrack; |
| 4873 | } |
| 4874 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 4875 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() |
| 4876 | { |
| 4877 | Mutex::Autolock _l(mLock); |
| 4878 | return mInput; |
| 4879 | } |
| 4880 | |
| 4881 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 4882 | { |
| 4883 | Mutex::Autolock _l(mLock); |
| 4884 | AudioStreamIn *input = mInput; |
| 4885 | mInput = NULL; |
| 4886 | return input; |
| 4887 | } |
| 4888 | |
| 4889 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 4890 | audio_stream_t* AudioFlinger::RecordThread::stream() |
| 4891 | { |
| 4892 | if (mInput == NULL) { |
| 4893 | return NULL; |
| 4894 | } |
| 4895 | return &mInput->stream->common; |
| 4896 | } |
| 4897 | |
| 4898 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4899 | // ---------------------------------------------------------------------------- |
| 4900 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4901 | int AudioFlinger::openOutput(uint32_t *pDevices, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4902 | uint32_t *pSamplingRate, |
| 4903 | uint32_t *pFormat, |
| 4904 | uint32_t *pChannels, |
| 4905 | uint32_t *pLatencyMs, |
| 4906 | uint32_t flags) |
| 4907 | { |
| 4908 | status_t status; |
| 4909 | PlaybackThread *thread = NULL; |
| 4910 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 4911 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 4912 | uint32_t format = pFormat ? *pFormat : 0; |
| 4913 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4914 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4915 | audio_stream_out_t *outStream; |
| 4916 | audio_hw_device_t *outHwDev; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4917 | |
| 4918 | LOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
| 4919 | pDevices ? *pDevices : 0, |
| 4920 | samplingRate, |
| 4921 | format, |
| 4922 | channels, |
| 4923 | flags); |
| 4924 | |
| 4925 | if (pDevices == NULL || *pDevices == 0) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4926 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4927 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4928 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4929 | Mutex::Autolock _l(mLock); |
| 4930 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4931 | outHwDev = findSuitableHwDev_l(*pDevices); |
| 4932 | if (outHwDev == NULL) |
| 4933 | return 0; |
| 4934 | |
| 4935 | status = outHwDev->open_output_stream(outHwDev, *pDevices, (int *)&format, |
| 4936 | &channels, &samplingRate, &outStream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4937 | LOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4938 | outStream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4939 | samplingRate, |
| 4940 | format, |
| 4941 | channels, |
| 4942 | status); |
| 4943 | |
| 4944 | mHardwareStatus = AUDIO_HW_IDLE; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4945 | if (outStream != NULL) { |
| 4946 | AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream); |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4947 | int id = nextUniqueId(); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 4948 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 4949 | if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) || |
| 4950 | (format != AUDIO_FORMAT_PCM_16_BIT) || |
| 4951 | (channels != AUDIO_CHANNEL_OUT_STEREO)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4952 | thread = new DirectOutputThread(this, output, id, *pDevices); |
| 4953 | LOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4954 | } else { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4955 | thread = new MixerThread(this, output, id, *pDevices); |
| 4956 | LOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4957 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4958 | mPlaybackThreads.add(id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4959 | |
| 4960 | if (pSamplingRate) *pSamplingRate = samplingRate; |
| 4961 | if (pFormat) *pFormat = format; |
| 4962 | if (pChannels) *pChannels = channels; |
| 4963 | if (pLatencyMs) *pLatencyMs = thread->latency(); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4964 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 4965 | // notify client processes of the new output creation |
| 4966 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4967 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4968 | } |
| 4969 | |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 4970 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4973 | int AudioFlinger::openDuplicateOutput(int output1, int output2) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4974 | { |
| 4975 | Mutex::Autolock _l(mLock); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4976 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 4977 | MixerThread *thread2 = checkMixerThread_l(output2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4978 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4979 | if (thread1 == NULL || thread2 == NULL) { |
| 4980 | LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
| 4981 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 4984 | int id = nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4985 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4986 | thread->addOutputTrack(thread2); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4987 | mPlaybackThreads.add(id, thread); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 4988 | // notify client processes of the new output creation |
| 4989 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 4990 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4991 | } |
| 4992 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 4993 | status_t AudioFlinger::closeOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4994 | { |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 4995 | // keep strong reference on the playback thread so that |
| 4996 | // it is not destroyed while exit() is executed |
| 4997 | sp <PlaybackThread> thread; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 4998 | { |
| 4999 | Mutex::Autolock _l(mLock); |
| 5000 | thread = checkPlaybackThread_l(output); |
| 5001 | if (thread == NULL) { |
| 5002 | return BAD_VALUE; |
| 5003 | } |
| 5004 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5005 | LOGV("closeOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5006 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5007 | if (thread->type() == ThreadBase::MIXER) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5008 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5009 | if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5010 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5011 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | } |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 5015 | void *param2 = 0; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 5016 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5017 | mPlaybackThreads.removeItem(output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5018 | } |
| 5019 | thread->exit(); |
| 5020 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5021 | if (thread->type() != ThreadBase::DUPLICATING) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5022 | AudioStreamOut *out = thread->clearOutput(); |
| 5023 | // from now on thread->mOutput is NULL |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5024 | out->hwDev->close_output_stream(out->hwDev, out->stream); |
| 5025 | delete out; |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5026 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5027 | return NO_ERROR; |
| 5028 | } |
| 5029 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5030 | status_t AudioFlinger::suspendOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5031 | { |
| 5032 | Mutex::Autolock _l(mLock); |
| 5033 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5034 | |
| 5035 | if (thread == NULL) { |
| 5036 | return BAD_VALUE; |
| 5037 | } |
| 5038 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5039 | LOGV("suspendOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5040 | thread->suspend(); |
| 5041 | |
| 5042 | return NO_ERROR; |
| 5043 | } |
| 5044 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5045 | status_t AudioFlinger::restoreOutput(int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5046 | { |
| 5047 | Mutex::Autolock _l(mLock); |
| 5048 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5049 | |
| 5050 | if (thread == NULL) { |
| 5051 | return BAD_VALUE; |
| 5052 | } |
| 5053 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5054 | LOGV("restoreOutput() %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5055 | |
| 5056 | thread->restore(); |
| 5057 | |
| 5058 | return NO_ERROR; |
| 5059 | } |
| 5060 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5061 | int AudioFlinger::openInput(uint32_t *pDevices, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5062 | uint32_t *pSamplingRate, |
| 5063 | uint32_t *pFormat, |
| 5064 | uint32_t *pChannels, |
| 5065 | uint32_t acoustics) |
| 5066 | { |
| 5067 | status_t status; |
| 5068 | RecordThread *thread = NULL; |
| 5069 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 5070 | uint32_t format = pFormat ? *pFormat : 0; |
| 5071 | uint32_t channels = pChannels ? *pChannels : 0; |
| 5072 | uint32_t reqSamplingRate = samplingRate; |
| 5073 | uint32_t reqFormat = format; |
| 5074 | uint32_t reqChannels = channels; |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5075 | audio_stream_in_t *inStream; |
| 5076 | audio_hw_device_t *inHwDev; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5077 | |
| 5078 | if (pDevices == NULL || *pDevices == 0) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5079 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5080 | } |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5081 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5082 | Mutex::Autolock _l(mLock); |
| 5083 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5084 | inHwDev = findSuitableHwDev_l(*pDevices); |
| 5085 | if (inHwDev == NULL) |
| 5086 | return 0; |
| 5087 | |
| 5088 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, |
| 5089 | &channels, &samplingRate, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5090 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5091 | &inStream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5092 | LOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d", |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5093 | inStream, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5094 | samplingRate, |
| 5095 | format, |
| 5096 | channels, |
| 5097 | acoustics, |
| 5098 | status); |
| 5099 | |
| 5100 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 5101 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 5102 | // or stereo to mono conversions on 16 bit PCM inputs. |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5103 | if (inStream == NULL && status == BAD_VALUE && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5104 | reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT && |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5105 | (samplingRate <= 2 * reqSamplingRate) && |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5106 | (popcount(channels) < 3) && (popcount(reqChannels) < 3)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5107 | LOGV("openInput() reopening with proposed sampling rate and channels"); |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5108 | status = inHwDev->open_input_stream(inHwDev, *pDevices, (int *)&format, |
| 5109 | &channels, &samplingRate, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5110 | (audio_in_acoustics_t)acoustics, |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5111 | &inStream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5114 | if (inStream != NULL) { |
| 5115 | AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); |
| 5116 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5117 | int id = nextUniqueId(); |
| 5118 | // Start record thread |
| 5119 | // RecorThread require both input and output device indication to forward to audio |
| 5120 | // pre processing modules |
| 5121 | uint32_t device = (*pDevices) | primaryOutputDevice_l(); |
| 5122 | thread = new RecordThread(this, |
| 5123 | input, |
| 5124 | reqSamplingRate, |
| 5125 | reqChannels, |
| 5126 | id, |
| 5127 | device); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5128 | mRecordThreads.add(id, thread); |
| 5129 | LOGV("openInput() created record thread: ID %d thread %p", id, thread); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5130 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; |
| 5131 | if (pFormat) *pFormat = format; |
| 5132 | if (pChannels) *pChannels = reqChannels; |
| 5133 | |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5134 | input->stream->common.standby(&input->stream->common); |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 5135 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5136 | // notify client processes of the new input creation |
| 5137 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5138 | return id; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5139 | } |
| 5140 | |
Eric Laurent | 9cc489a | 2009-12-05 05:20:01 -0800 | [diff] [blame] | 5141 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5142 | } |
| 5143 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5144 | status_t AudioFlinger::closeInput(int input) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5145 | { |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5146 | // keep strong reference on the record thread so that |
| 5147 | // it is not destroyed while exit() is executed |
| 5148 | sp <RecordThread> thread; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5149 | { |
| 5150 | Mutex::Autolock _l(mLock); |
| 5151 | thread = checkRecordThread_l(input); |
| 5152 | if (thread == NULL) { |
| 5153 | return BAD_VALUE; |
| 5154 | } |
| 5155 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5156 | LOGV("closeInput() %d", input); |
Eric Laurent | 296a0ec | 2009-09-15 07:10:12 -0700 | [diff] [blame] | 5157 | void *param2 = 0; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 5158 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5159 | mRecordThreads.removeItem(input); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5160 | } |
| 5161 | thread->exit(); |
| 5162 | |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5163 | AudioStreamIn *in = thread->clearInput(); |
| 5164 | // from now on thread->mInput is NULL |
Dima Zavin | 31f18889 | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 5165 | in->hwDev->close_input_stream(in->hwDev, in->stream); |
| 5166 | delete in; |
Eric Laurent | 49018a5 | 2009-08-04 08:37:05 -0700 | [diff] [blame] | 5167 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5168 | return NO_ERROR; |
| 5169 | } |
| 5170 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5171 | status_t AudioFlinger::setStreamOutput(uint32_t stream, int output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5172 | { |
| 5173 | Mutex::Autolock _l(mLock); |
| 5174 | MixerThread *dstThread = checkMixerThread_l(output); |
| 5175 | if (dstThread == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5176 | LOGW("setStreamOutput() bad output id %d", output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5177 | return BAD_VALUE; |
| 5178 | } |
| 5179 | |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5180 | LOGV("setStreamOutput() stream %d to output %d", stream, output); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5181 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5182 | |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5183 | dstThread->setStreamValid(stream, true); |
| 5184 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5185 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5186 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5187 | if (thread != dstThread && |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5188 | thread->type() != ThreadBase::DIRECT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5189 | MixerThread *srcThread = (MixerThread *)thread; |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 5190 | srcThread->setStreamValid(stream, false); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 5191 | srcThread->invalidateTracks(stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5192 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5193 | } |
Eric Laurent | d069f32 | 2009-09-01 05:56:26 -0700 | [diff] [blame] | 5194 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5195 | return NO_ERROR; |
| 5196 | } |
| 5197 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5198 | |
| 5199 | int AudioFlinger::newAudioSessionId() |
| 5200 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5201 | return nextUniqueId(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5202 | } |
| 5203 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5204 | void AudioFlinger::acquireAudioSessionId(int audioSession) |
| 5205 | { |
| 5206 | Mutex::Autolock _l(mLock); |
| 5207 | int caller = IPCThreadState::self()->getCallingPid(); |
| 5208 | LOGV("acquiring %d from %d", audioSession, caller); |
| 5209 | int num = mAudioSessionRefs.size(); |
| 5210 | for (int i = 0; i< num; i++) { |
| 5211 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
| 5212 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5213 | ref->cnt++; |
| 5214 | LOGV(" incremented refcount to %d", ref->cnt); |
| 5215 | return; |
| 5216 | } |
| 5217 | } |
| 5218 | AudioSessionRef *ref = new AudioSessionRef(); |
| 5219 | ref->sessionid = audioSession; |
| 5220 | ref->pid = caller; |
| 5221 | ref->cnt = 1; |
| 5222 | mAudioSessionRefs.push(ref); |
| 5223 | LOGV(" added new entry for %d", ref->sessionid); |
| 5224 | } |
| 5225 | |
| 5226 | void AudioFlinger::releaseAudioSessionId(int audioSession) |
| 5227 | { |
| 5228 | Mutex::Autolock _l(mLock); |
| 5229 | int caller = IPCThreadState::self()->getCallingPid(); |
| 5230 | LOGV("releasing %d from %d", audioSession, caller); |
| 5231 | int num = mAudioSessionRefs.size(); |
| 5232 | for (int i = 0; i< num; i++) { |
| 5233 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 5234 | if (ref->sessionid == audioSession && ref->pid == caller) { |
| 5235 | ref->cnt--; |
| 5236 | LOGV(" decremented refcount to %d", ref->cnt); |
| 5237 | if (ref->cnt == 0) { |
| 5238 | mAudioSessionRefs.removeAt(i); |
| 5239 | delete ref; |
| 5240 | purgeStaleEffects_l(); |
| 5241 | } |
| 5242 | return; |
| 5243 | } |
| 5244 | } |
| 5245 | LOGW("session id %d not found for pid %d", audioSession, caller); |
| 5246 | } |
| 5247 | |
| 5248 | void AudioFlinger::purgeStaleEffects_l() { |
| 5249 | |
| 5250 | LOGV("purging stale effects"); |
| 5251 | |
| 5252 | Vector< sp<EffectChain> > chains; |
| 5253 | |
| 5254 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5255 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 5256 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5257 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 2255a1e | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 5258 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 5259 | chains.push(ec); |
| 5260 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5264 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 5265 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 5266 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 5267 | chains.push(ec); |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | for (size_t i = 0; i < chains.size(); i++) { |
| 5272 | sp<EffectChain> ec = chains[i]; |
| 5273 | int sessionid = ec->sessionId(); |
| 5274 | sp<ThreadBase> t = ec->mThread.promote(); |
| 5275 | if (t == 0) { |
| 5276 | continue; |
| 5277 | } |
| 5278 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 5279 | bool found = false; |
| 5280 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 5281 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
| 5282 | if (ref->sessionid == sessionid) { |
| 5283 | LOGV(" session %d still exists for %d with %d refs", |
| 5284 | sessionid, ref->pid, ref->cnt); |
| 5285 | found = true; |
| 5286 | break; |
| 5287 | } |
| 5288 | } |
| 5289 | if (!found) { |
| 5290 | // remove all effects from the chain |
| 5291 | while (ec->mEffects.size()) { |
| 5292 | sp<EffectModule> effect = ec->mEffects[0]; |
| 5293 | effect->unPin(); |
| 5294 | Mutex::Autolock _l (t->mLock); |
| 5295 | t->removeEffect_l(effect); |
| 5296 | for (size_t j = 0; j < effect->mHandles.size(); j++) { |
| 5297 | sp<EffectHandle> handle = effect->mHandles[j].promote(); |
| 5298 | if (handle != 0) { |
| 5299 | handle->mEffect.clear(); |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 5300 | if (handle->mHasControl && handle->mEnabled) { |
| 5301 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
| 5302 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5303 | } |
| 5304 | } |
| 5305 | AudioSystem::unregisterEffect(effect->id()); |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | return; |
| 5310 | } |
| 5311 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5312 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5313 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5314 | { |
| 5315 | PlaybackThread *thread = NULL; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5316 | if (mPlaybackThreads.indexOfKey(output) >= 0) { |
| 5317 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5318 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5319 | return thread; |
| 5320 | } |
| 5321 | |
| 5322 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5323 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5324 | { |
| 5325 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 5326 | if (thread != NULL) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5327 | if (thread->type() == ThreadBase::DIRECT) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5328 | thread = NULL; |
| 5329 | } |
| 5330 | } |
| 5331 | return (MixerThread *)thread; |
| 5332 | } |
| 5333 | |
| 5334 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5335 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5336 | { |
| 5337 | RecordThread *thread = NULL; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 5338 | if (mRecordThreads.indexOfKey(input) >= 0) { |
| 5339 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5340 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 5341 | return thread; |
| 5342 | } |
| 5343 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5344 | uint32_t AudioFlinger::nextUniqueId() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5345 | { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5346 | return android_atomic_inc(&mNextUniqueId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5347 | } |
| 5348 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5349 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() |
| 5350 | { |
| 5351 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5352 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 5353 | AudioStreamOut *output = thread->getOutput(); |
| 5354 | if (output != NULL && output->hwDev == mPrimaryHardwareDev) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5355 | return thread; |
| 5356 | } |
| 5357 | } |
| 5358 | return NULL; |
| 5359 | } |
| 5360 | |
| 5361 | uint32_t AudioFlinger::primaryOutputDevice_l() |
| 5362 | { |
| 5363 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 5364 | |
| 5365 | if (thread == NULL) { |
| 5366 | return 0; |
| 5367 | } |
| 5368 | |
| 5369 | return thread->device(); |
| 5370 | } |
| 5371 | |
| 5372 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5373 | // ---------------------------------------------------------------------------- |
| 5374 | // Effect management |
| 5375 | // ---------------------------------------------------------------------------- |
| 5376 | |
| 5377 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5378 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) |
| 5379 | { |
| 5380 | Mutex::Autolock _l(mLock); |
| 5381 | return EffectQueryNumberEffects(numEffects); |
| 5382 | } |
| 5383 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5384 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5385 | { |
| 5386 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5387 | return EffectQueryEffect(index, descriptor); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) |
| 5391 | { |
| 5392 | Mutex::Autolock _l(mLock); |
| 5393 | return EffectGetDescriptor(pUuid, descriptor); |
| 5394 | } |
| 5395 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 5396 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5397 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 5398 | effect_descriptor_t *pDesc, |
| 5399 | const sp<IEffectClient>& effectClient, |
| 5400 | int32_t priority, |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5401 | int io, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5402 | int sessionId, |
| 5403 | status_t *status, |
| 5404 | int *id, |
| 5405 | int *enabled) |
| 5406 | { |
| 5407 | status_t lStatus = NO_ERROR; |
| 5408 | sp<EffectHandle> handle; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5409 | effect_descriptor_t desc; |
| 5410 | sp<Client> client; |
| 5411 | wp<Client> wclient; |
| 5412 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5413 | LOGV("createEffect pid %d, client %p, priority %d, sessionId %d, io %d", |
| 5414 | pid, effectClient.get(), priority, sessionId, io); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5415 | |
| 5416 | if (pDesc == NULL) { |
| 5417 | lStatus = BAD_VALUE; |
| 5418 | goto Exit; |
| 5419 | } |
| 5420 | |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5421 | // check audio settings permission for global effects |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5422 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5423 | lStatus = PERMISSION_DENIED; |
| 5424 | goto Exit; |
| 5425 | } |
| 5426 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5427 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5428 | // that can only be created by audio policy manager (running in same process) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5429 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid() != pid) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5430 | lStatus = PERMISSION_DENIED; |
| 5431 | goto Exit; |
| 5432 | } |
| 5433 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5434 | if (io == 0) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5435 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5436 | // output must be specified by AudioPolicyManager when using session |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5437 | // AUDIO_SESSION_OUTPUT_STAGE |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5438 | lStatus = BAD_VALUE; |
| 5439 | goto Exit; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5440 | } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5441 | // if the output returned by getOutputForEffect() is removed before we lock the |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5442 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5443 | // and we will exit safely |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5444 | io = AudioSystem::getOutputForEffect(&desc); |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5448 | { |
| 5449 | Mutex::Autolock _l(mLock); |
| 5450 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 5451 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5452 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 5453 | // if uuid is specified, request effect descriptor |
| 5454 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 5455 | if (lStatus < 0) { |
| 5456 | LOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
| 5457 | goto Exit; |
| 5458 | } |
| 5459 | } else { |
| 5460 | // if uuid is not specified, look for an available implementation |
| 5461 | // of the required type in effect factory |
| 5462 | if (EffectIsNullUuid(&pDesc->type)) { |
| 5463 | LOGW("createEffect() no effect type"); |
| 5464 | lStatus = BAD_VALUE; |
| 5465 | goto Exit; |
| 5466 | } |
| 5467 | uint32_t numEffects = 0; |
| 5468 | effect_descriptor_t d; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5469 | d.flags = 0; // prevent compiler warning |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5470 | bool found = false; |
| 5471 | |
| 5472 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 5473 | if (lStatus < 0) { |
| 5474 | LOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
| 5475 | goto Exit; |
| 5476 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5477 | for (uint32_t i = 0; i < numEffects; i++) { |
| 5478 | lStatus = EffectQueryEffect(i, &desc); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5479 | if (lStatus < 0) { |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5480 | LOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5481 | continue; |
| 5482 | } |
| 5483 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 5484 | // If matching type found save effect descriptor. If the session is |
| 5485 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 5486 | // an auxiliary version of this effect type is available |
| 5487 | found = true; |
| 5488 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5489 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5490 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5491 | break; |
| 5492 | } |
| 5493 | } |
| 5494 | } |
| 5495 | if (!found) { |
| 5496 | lStatus = BAD_VALUE; |
| 5497 | LOGW("createEffect() effect not found"); |
| 5498 | goto Exit; |
| 5499 | } |
| 5500 | // For same effect type, chose auxiliary version over insert version if |
| 5501 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5502 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5503 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5504 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 5505 | } |
| 5506 | } |
| 5507 | |
| 5508 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5509 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5510 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5511 | lStatus = INVALID_OPERATION; |
| 5512 | goto Exit; |
| 5513 | } |
| 5514 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5515 | // check recording permission for visualizer |
| 5516 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
| 5517 | !recordingAllowed()) { |
| 5518 | lStatus = PERMISSION_DENIED; |
| 5519 | goto Exit; |
| 5520 | } |
| 5521 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5522 | // return effect descriptor |
| 5523 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 5524 | |
| 5525 | // If output is not specified try to find a matching audio session ID in one of the |
| 5526 | // output threads. |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5527 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 5528 | // because of code checking output when entering the function. |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5529 | // Note: io is never 0 when creating an effect on an input |
| 5530 | if (io == 0) { |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5531 | // look for the thread where the specified audio session is present |
| 5532 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 5533 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5534 | io = mPlaybackThreads.keyAt(i); |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5535 | break; |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5536 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5537 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5538 | if (io == 0) { |
| 5539 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 5540 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 5541 | io = mRecordThreads.keyAt(i); |
| 5542 | break; |
| 5543 | } |
| 5544 | } |
| 5545 | } |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5546 | // If no output thread contains the requested session ID, default to |
| 5547 | // first output. The effect chain will be moved to the correct output |
| 5548 | // thread when a track with the same session ID is created |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5549 | if (io == 0 && mPlaybackThreads.size()) { |
| 5550 | io = mPlaybackThreads.keyAt(0); |
| 5551 | } |
| 5552 | LOGV("createEffect() got io %d for effect %s", io, desc.name); |
| 5553 | } |
| 5554 | ThreadBase *thread = checkRecordThread_l(io); |
| 5555 | if (thread == NULL) { |
| 5556 | thread = checkPlaybackThread_l(io); |
| 5557 | if (thread == NULL) { |
| 5558 | LOGE("createEffect() unknown output thread"); |
| 5559 | lStatus = BAD_VALUE; |
| 5560 | goto Exit; |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5561 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5562 | } |
Eric Laurent | 98c9259 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 5563 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5564 | wclient = mClients.valueFor(pid); |
| 5565 | |
| 5566 | if (wclient != NULL) { |
| 5567 | client = wclient.promote(); |
| 5568 | } else { |
| 5569 | client = new Client(this, pid); |
| 5570 | mClients.add(pid, client); |
| 5571 | } |
| 5572 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5573 | // create effect on selected output thread |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5574 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 5575 | &desc, enabled, &lStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5576 | if (handle != 0 && id != NULL) { |
| 5577 | *id = handle->id(); |
| 5578 | } |
| 5579 | } |
| 5580 | |
| 5581 | Exit: |
| 5582 | if(status) { |
| 5583 | *status = lStatus; |
| 5584 | } |
| 5585 | return handle; |
| 5586 | } |
| 5587 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5588 | status_t AudioFlinger::moveEffects(int sessionId, int srcOutput, int dstOutput) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5589 | { |
| 5590 | LOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5591 | sessionId, srcOutput, dstOutput); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5592 | Mutex::Autolock _l(mLock); |
| 5593 | if (srcOutput == dstOutput) { |
| 5594 | LOGW("moveEffects() same dst and src outputs %d", dstOutput); |
| 5595 | return NO_ERROR; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5596 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5597 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 5598 | if (srcThread == NULL) { |
| 5599 | LOGW("moveEffects() bad srcOutput %d", srcOutput); |
| 5600 | return BAD_VALUE; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5601 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5602 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 5603 | if (dstThread == NULL) { |
| 5604 | LOGW("moveEffects() bad dstOutput %d", dstOutput); |
| 5605 | return BAD_VALUE; |
| 5606 | } |
| 5607 | |
| 5608 | Mutex::Autolock _dl(dstThread->mLock); |
| 5609 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5610 | moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5611 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5612 | return NO_ERROR; |
| 5613 | } |
| 5614 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5615 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5616 | status_t AudioFlinger::moveEffectChain_l(int sessionId, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5617 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5618 | AudioFlinger::PlaybackThread *dstThread, |
| 5619 | bool reRegister) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5620 | { |
| 5621 | LOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5622 | sessionId, srcThread, dstThread); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5623 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5624 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5625 | if (chain == 0) { |
| 5626 | LOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5627 | sessionId, srcThread); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5628 | return INVALID_OPERATION; |
| 5629 | } |
| 5630 | |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5631 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5632 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5633 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5634 | // removed. |
| 5635 | srcThread->removeEffectChain_l(chain); |
| 5636 | |
| 5637 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 5638 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5639 | int dstOutput = dstThread->id(); |
| 5640 | sp<EffectChain> dstChain; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5641 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5642 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 5643 | while (effect != 0) { |
| 5644 | srcThread->removeEffect_l(effect); |
| 5645 | dstThread->addEffect_l(effect); |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 5646 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 5647 | if (effect->state() == EffectModule::ACTIVE || |
| 5648 | effect->state() == EffectModule::STOPPING) { |
| 5649 | effect->start(); |
| 5650 | } |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5651 | // if the move request is not received from audio policy manager, the effect must be |
| 5652 | // re-registered with the new strategy and output |
| 5653 | if (dstChain == 0) { |
| 5654 | dstChain = effect->chain().promote(); |
| 5655 | if (dstChain == 0) { |
| 5656 | LOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
| 5657 | srcThread->addEffect_l(effect); |
| 5658 | return NO_INIT; |
| 5659 | } |
| 5660 | strategy = dstChain->strategy(); |
| 5661 | } |
| 5662 | if (reRegister) { |
| 5663 | AudioSystem::unregisterEffect(effect->id()); |
| 5664 | AudioSystem::registerEffect(&effect->desc(), |
| 5665 | dstOutput, |
| 5666 | strategy, |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5667 | sessionId, |
Eric Laurent | 493941b | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 5668 | effect->id()); |
| 5669 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5670 | effect = chain->getEffectFromId_l(0); |
| 5671 | } |
| 5672 | |
| 5673 | return NO_ERROR; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5674 | } |
| 5675 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5676 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5677 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5678 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5679 | const sp<AudioFlinger::Client>& client, |
| 5680 | const sp<IEffectClient>& effectClient, |
| 5681 | int32_t priority, |
| 5682 | int sessionId, |
| 5683 | effect_descriptor_t *desc, |
| 5684 | int *enabled, |
| 5685 | status_t *status |
| 5686 | ) |
| 5687 | { |
| 5688 | sp<EffectModule> effect; |
| 5689 | sp<EffectHandle> handle; |
| 5690 | status_t lStatus; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5691 | sp<EffectChain> chain; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5692 | bool chainCreated = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5693 | bool effectCreated = false; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5694 | bool effectRegistered = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5695 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5696 | lStatus = initCheck(); |
| 5697 | if (lStatus != NO_ERROR) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5698 | LOGW("createEffect_l() Audio driver not initialized."); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5699 | goto Exit; |
| 5700 | } |
| 5701 | |
| 5702 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 5703 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5704 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5705 | LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
| 5706 | desc->name, sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5707 | lStatus = BAD_VALUE; |
| 5708 | goto Exit; |
| 5709 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5710 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 5711 | if ((mType == RECORD && |
| 5712 | (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) || |
| 5713 | (mType != RECORD && |
| 5714 | (desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 5715 | LOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 5716 | desc->name, desc->flags, mType); |
| 5717 | lStatus = BAD_VALUE; |
| 5718 | goto Exit; |
| 5719 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5720 | |
| 5721 | LOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 5722 | |
| 5723 | { // scope for mLock |
| 5724 | Mutex::Autolock _l(mLock); |
| 5725 | |
| 5726 | // check for existing effect chain with the requested audio session |
| 5727 | chain = getEffectChain_l(sessionId); |
| 5728 | if (chain == 0) { |
| 5729 | // create a new chain for this session |
| 5730 | LOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 5731 | chain = new EffectChain(this, sessionId); |
| 5732 | addEffectChain_l(chain); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5733 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5734 | chainCreated = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5735 | } else { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5736 | effect = chain->getEffectFromDesc_l(desc); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | LOGV("createEffect_l() got effect %p on chain %p", effect == 0 ? 0 : effect.get(), chain.get()); |
| 5740 | |
| 5741 | if (effect == 0) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5742 | int id = mAudioFlinger->nextUniqueId(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5743 | // Check CPU and memory usage |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5744 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5745 | if (lStatus != NO_ERROR) { |
| 5746 | goto Exit; |
| 5747 | } |
| 5748 | effectRegistered = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5749 | // create a new effect module if none present in the chain |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5750 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5751 | lStatus = effect->status(); |
| 5752 | if (lStatus != NO_ERROR) { |
| 5753 | goto Exit; |
| 5754 | } |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5755 | lStatus = chain->addEffect_l(effect); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5756 | if (lStatus != NO_ERROR) { |
| 5757 | goto Exit; |
| 5758 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5759 | effectCreated = true; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5760 | |
| 5761 | effect->setDevice(mDevice); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5762 | effect->setMode(mAudioFlinger->getMode()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5763 | } |
| 5764 | // create effect handle and connect it to effect module |
| 5765 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 5766 | lStatus = effect->addHandle(handle); |
| 5767 | if (enabled) { |
| 5768 | *enabled = (int)effect->isEnabled(); |
| 5769 | } |
| 5770 | } |
| 5771 | |
| 5772 | Exit: |
| 5773 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5774 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5775 | if (effectCreated) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5776 | chain->removeEffect_l(effect); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5777 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5778 | if (effectRegistered) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5779 | AudioSystem::unregisterEffect(effect->id()); |
| 5780 | } |
| 5781 | if (chainCreated) { |
| 5782 | removeEffectChain_l(chain); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5783 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5784 | handle.clear(); |
| 5785 | } |
| 5786 | |
| 5787 | if(status) { |
| 5788 | *status = lStatus; |
| 5789 | } |
| 5790 | return handle; |
| 5791 | } |
| 5792 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5793 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 5794 | { |
| 5795 | sp<EffectModule> effect; |
| 5796 | |
| 5797 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5798 | if (chain != 0) { |
| 5799 | effect = chain->getEffectFromId_l(effectId); |
| 5800 | } |
| 5801 | return effect; |
| 5802 | } |
| 5803 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5804 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 5805 | // PlaybackThread::mLock held |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5806 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5807 | { |
| 5808 | // check for existing effect chain with the requested audio session |
| 5809 | int sessionId = effect->sessionId(); |
| 5810 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5811 | bool chainCreated = false; |
| 5812 | |
| 5813 | if (chain == 0) { |
| 5814 | // create a new chain for this session |
| 5815 | LOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 5816 | chain = new EffectChain(this, sessionId); |
| 5817 | addEffectChain_l(chain); |
| 5818 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 5819 | chainCreated = true; |
| 5820 | } |
| 5821 | LOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 5822 | |
| 5823 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 5824 | LOGW("addEffect_l() %p effect %s already present in chain %p", |
| 5825 | this, effect->desc().name, chain.get()); |
| 5826 | return BAD_VALUE; |
| 5827 | } |
| 5828 | |
| 5829 | status_t status = chain->addEffect_l(effect); |
| 5830 | if (status != NO_ERROR) { |
| 5831 | if (chainCreated) { |
| 5832 | removeEffectChain_l(chain); |
| 5833 | } |
| 5834 | return status; |
| 5835 | } |
| 5836 | |
| 5837 | effect->setDevice(mDevice); |
| 5838 | effect->setMode(mAudioFlinger->getMode()); |
| 5839 | return NO_ERROR; |
| 5840 | } |
| 5841 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5842 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5843 | |
| 5844 | LOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5845 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5846 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5847 | detachAuxEffect_l(effect->id()); |
| 5848 | } |
| 5849 | |
| 5850 | sp<EffectChain> chain = effect->chain().promote(); |
| 5851 | if (chain != 0) { |
| 5852 | // remove effect chain if removing last effect |
| 5853 | if (chain->removeEffect_l(effect) == 0) { |
| 5854 | removeEffectChain_l(chain); |
| 5855 | } |
| 5856 | } else { |
| 5857 | LOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 5858 | } |
| 5859 | } |
| 5860 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 5861 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 5862 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5863 | { |
| 5864 | effectChains = mEffectChains; |
| 5865 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5866 | mEffectChains[i]->lock(); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 5871 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
| 5872 | { |
| 5873 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 5874 | effectChains[i]->unlock(); |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 5879 | { |
| 5880 | Mutex::Autolock _l(mLock); |
| 5881 | return getEffectChain_l(sessionId); |
| 5882 | } |
| 5883 | |
| 5884 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) |
| 5885 | { |
| 5886 | sp<EffectChain> chain; |
| 5887 | |
| 5888 | size_t size = mEffectChains.size(); |
| 5889 | for (size_t i = 0; i < size; i++) { |
| 5890 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 5891 | chain = mEffectChains[i]; |
| 5892 | break; |
| 5893 | } |
| 5894 | } |
| 5895 | return chain; |
| 5896 | } |
| 5897 | |
| 5898 | void AudioFlinger::ThreadBase::setMode(uint32_t mode) |
| 5899 | { |
| 5900 | Mutex::Autolock _l(mLock); |
| 5901 | size_t size = mEffectChains.size(); |
| 5902 | for (size_t i = 0; i < size; i++) { |
| 5903 | mEffectChains[i]->setMode_l(mode); |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5908 | const wp<EffectHandle>& handle, |
| 5909 | bool unpiniflast) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5910 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5911 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5912 | LOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5913 | // delete the effect module if removing last handle on it |
| 5914 | if (effect->removeHandle(handle) == 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 5915 | if (!effect->isPinned() || unpiniflast) { |
| 5916 | removeEffect_l(effect); |
| 5917 | AudioSystem::unregisterEffect(effect->id()); |
| 5918 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5919 | } |
| 5920 | } |
| 5921 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5922 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 5923 | { |
| 5924 | int session = chain->sessionId(); |
| 5925 | int16_t *buffer = mMixBuffer; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5926 | bool ownsBuffer = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5927 | |
| 5928 | LOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5929 | if (session > 0) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5930 | // Only one effect chain can be present in direct output thread and it uses |
| 5931 | // the mix buffer as input |
| 5932 | if (mType != DIRECT) { |
| 5933 | size_t numSamples = mFrameCount * mChannelCount; |
| 5934 | buffer = new int16_t[numSamples]; |
| 5935 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 5936 | LOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 5937 | ownsBuffer = true; |
| 5938 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5939 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5940 | // Attach all tracks with same session ID to this chain. |
| 5941 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5942 | sp<Track> track = mTracks[i]; |
| 5943 | if (session == track->sessionId()) { |
| 5944 | LOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
| 5945 | track->setMainBuffer(buffer); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5946 | chain->incTrackCnt(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | // indicate all active tracks in the chain |
| 5951 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5952 | sp<Track> track = mActiveTracks[i].promote(); |
| 5953 | if (track == 0) continue; |
| 5954 | if (session == track->sessionId()) { |
| 5955 | LOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5956 | chain->incActiveTrackCnt(); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5957 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5958 | } |
| 5959 | } |
| 5960 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5961 | chain->setInBuffer(buffer, ownsBuffer); |
| 5962 | chain->setOutBuffer(mMixBuffer); |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5963 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5964 | // chains list in order to be processed last as it contains output stage effects |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5965 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 5966 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5967 | // after track specific effects and before output stage |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 5968 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 5969 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5970 | // Effect chain for other sessions are inserted at beginning of effect |
| 5971 | // chains list to be processed before output mix effects. Relative order between other |
| 5972 | // sessions is not important |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5973 | size_t size = mEffectChains.size(); |
| 5974 | size_t i = 0; |
| 5975 | for (i = 0; i < size; i++) { |
| 5976 | if (mEffectChains[i]->sessionId() < session) break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5977 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 5978 | mEffectChains.insertAt(chain, i); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 5979 | checkSuspendOnAddEffectChain_l(chain); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 5980 | |
| 5981 | return NO_ERROR; |
| 5982 | } |
| 5983 | |
| 5984 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 5985 | { |
| 5986 | int session = chain->sessionId(); |
| 5987 | |
| 5988 | LOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 5989 | |
| 5990 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5991 | if (chain == mEffectChains[i]) { |
| 5992 | mEffectChains.removeAt(i); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 5993 | // detach all active tracks from the chain |
| 5994 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5995 | sp<Track> track = mActiveTracks[i].promote(); |
| 5996 | if (track == 0) continue; |
| 5997 | if (session == track->sessionId()) { |
| 5998 | LOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 5999 | chain.get(), session); |
| 6000 | chain->decActiveTrackCnt(); |
| 6001 | } |
| 6002 | } |
| 6003 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6004 | // detach all tracks with same session ID from this chain |
| 6005 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6006 | sp<Track> track = mTracks[i]; |
| 6007 | if (session == track->sessionId()) { |
| 6008 | track->setMainBuffer(mMixBuffer); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6009 | chain->decTrackCnt(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6010 | } |
| 6011 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6012 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6013 | } |
| 6014 | } |
| 6015 | return mEffectChains.size(); |
| 6016 | } |
| 6017 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6018 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 6019 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6020 | { |
| 6021 | Mutex::Autolock _l(mLock); |
| 6022 | return attachAuxEffect_l(track, EffectId); |
| 6023 | } |
| 6024 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6025 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 6026 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6027 | { |
| 6028 | status_t status = NO_ERROR; |
| 6029 | |
| 6030 | if (EffectId == 0) { |
| 6031 | track->setAuxBuffer(0, NULL); |
| 6032 | } else { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6033 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 6034 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6035 | if (effect != 0) { |
| 6036 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6037 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 6038 | } else { |
| 6039 | status = INVALID_OPERATION; |
| 6040 | } |
| 6041 | } else { |
| 6042 | status = BAD_VALUE; |
| 6043 | } |
| 6044 | } |
| 6045 | return status; |
| 6046 | } |
| 6047 | |
| 6048 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 6049 | { |
| 6050 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6051 | sp<Track> track = mTracks[i]; |
| 6052 | if (track->auxEffectId() == effectId) { |
| 6053 | attachAuxEffect_l(track, 0); |
| 6054 | } |
| 6055 | } |
| 6056 | } |
| 6057 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6058 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6059 | { |
| 6060 | // only one chain per input thread |
| 6061 | if (mEffectChains.size() != 0) { |
| 6062 | return INVALID_OPERATION; |
| 6063 | } |
| 6064 | LOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
| 6065 | |
| 6066 | chain->setInBuffer(NULL); |
| 6067 | chain->setOutBuffer(NULL); |
| 6068 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6069 | checkSuspendOnAddEffectChain_l(chain); |
| 6070 | |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6071 | mEffectChains.add(chain); |
| 6072 | |
| 6073 | return NO_ERROR; |
| 6074 | } |
| 6075 | |
| 6076 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6077 | { |
| 6078 | LOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 6079 | LOGW_IF(mEffectChains.size() != 1, |
| 6080 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6081 | chain.get(), mEffectChains.size(), this); |
| 6082 | if (mEffectChains.size() == 1) { |
| 6083 | mEffectChains.removeAt(0); |
| 6084 | } |
| 6085 | return 0; |
| 6086 | } |
| 6087 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6088 | // ---------------------------------------------------------------------------- |
| 6089 | // EffectModule implementation |
| 6090 | // ---------------------------------------------------------------------------- |
| 6091 | |
| 6092 | #undef LOG_TAG |
| 6093 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 6094 | |
| 6095 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, |
| 6096 | const wp<AudioFlinger::EffectChain>& chain, |
| 6097 | effect_descriptor_t *desc, |
| 6098 | int id, |
| 6099 | int sessionId) |
| 6100 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6101 | mStatus(NO_INIT), mState(IDLE), mSuspended(false) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6102 | { |
| 6103 | LOGV("Constructor %p", this); |
| 6104 | int lStatus; |
| 6105 | sp<ThreadBase> thread = mThread.promote(); |
| 6106 | if (thread == 0) { |
| 6107 | return; |
| 6108 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6109 | |
| 6110 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 6111 | |
| 6112 | // create effect engine from effect factory |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6113 | mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6114 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6115 | if (mStatus != NO_ERROR) { |
| 6116 | return; |
| 6117 | } |
| 6118 | lStatus = init(); |
| 6119 | if (lStatus < 0) { |
| 6120 | mStatus = lStatus; |
| 6121 | goto Error; |
| 6122 | } |
| 6123 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6124 | if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) { |
| 6125 | mPinned = true; |
| 6126 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6127 | LOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
| 6128 | return; |
| 6129 | Error: |
| 6130 | EffectRelease(mEffectInterface); |
| 6131 | mEffectInterface = NULL; |
| 6132 | LOGV("Constructor Error %d", mStatus); |
| 6133 | } |
| 6134 | |
| 6135 | AudioFlinger::EffectModule::~EffectModule() |
| 6136 | { |
| 6137 | LOGV("Destructor %p", this); |
| 6138 | if (mEffectInterface != NULL) { |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6139 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6140 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 6141 | sp<ThreadBase> thread = mThread.promote(); |
| 6142 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6143 | audio_stream_t *stream = thread->stream(); |
| 6144 | if (stream != NULL) { |
| 6145 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6146 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6147 | } |
| 6148 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6149 | // release effect engine |
| 6150 | EffectRelease(mEffectInterface); |
| 6151 | } |
| 6152 | } |
| 6153 | |
| 6154 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) |
| 6155 | { |
| 6156 | status_t status; |
| 6157 | |
| 6158 | Mutex::Autolock _l(mLock); |
| 6159 | // First handle in mHandles has highest priority and controls the effect module |
| 6160 | int priority = handle->priority(); |
| 6161 | size_t size = mHandles.size(); |
| 6162 | sp<EffectHandle> h; |
| 6163 | size_t i; |
| 6164 | for (i = 0; i < size; i++) { |
| 6165 | h = mHandles[i].promote(); |
| 6166 | if (h == 0) continue; |
| 6167 | if (h->priority() <= priority) break; |
| 6168 | } |
| 6169 | // if inserted in first place, move effect control from previous owner to this handle |
| 6170 | if (i == 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6171 | bool enabled = false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6172 | if (h != 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6173 | enabled = h->enabled(); |
| 6174 | h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6175 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6176 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6177 | status = NO_ERROR; |
| 6178 | } else { |
| 6179 | status = ALREADY_EXISTS; |
| 6180 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6181 | LOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6182 | mHandles.insertAt(handle, i); |
| 6183 | return status; |
| 6184 | } |
| 6185 | |
| 6186 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 6187 | { |
| 6188 | Mutex::Autolock _l(mLock); |
| 6189 | size_t size = mHandles.size(); |
| 6190 | size_t i; |
| 6191 | for (i = 0; i < size; i++) { |
| 6192 | if (mHandles[i] == handle) break; |
| 6193 | } |
| 6194 | if (i == size) { |
| 6195 | return size; |
| 6196 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6197 | LOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i); |
| 6198 | |
| 6199 | bool enabled = false; |
| 6200 | EffectHandle *hdl = handle.unsafe_get(); |
| 6201 | if (hdl) { |
| 6202 | LOGV("removeHandle() unsafe_get OK"); |
| 6203 | enabled = hdl->enabled(); |
| 6204 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6205 | mHandles.removeAt(i); |
| 6206 | size = mHandles.size(); |
| 6207 | // if removed from first place, move effect control from this handle to next in line |
| 6208 | if (i == 0 && size != 0) { |
| 6209 | sp<EffectHandle> h = mHandles[0].promote(); |
| 6210 | if (h != 0) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6211 | h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6212 | } |
| 6213 | } |
| 6214 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6215 | // Prevent calls to process() and other functions on effect interface from now on. |
| 6216 | // The effect engine will be released by the destructor when the last strong reference on |
| 6217 | // this object is released which can happen after next process is called. |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6218 | if (size == 0 && !mPinned) { |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6219 | mState = DESTROYED; |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 6220 | } |
| 6221 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6222 | return size; |
| 6223 | } |
| 6224 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6225 | sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle() |
| 6226 | { |
| 6227 | Mutex::Autolock _l(mLock); |
| 6228 | sp<EffectHandle> handle; |
| 6229 | if (mHandles.size() != 0) { |
| 6230 | handle = mHandles[0].promote(); |
| 6231 | } |
| 6232 | return handle; |
| 6233 | } |
| 6234 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6235 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpiniflast) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6236 | { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6237 | LOGV("disconnect() %p handle %p ", this, handle.unsafe_get()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6238 | // keep a strong reference on this EffectModule to avoid calling the |
| 6239 | // destructor before we exit |
| 6240 | sp<EffectModule> keep(this); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6241 | { |
| 6242 | sp<ThreadBase> thread = mThread.promote(); |
| 6243 | if (thread != 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6244 | thread->disconnectEffect(keep, handle, unpiniflast); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6245 | } |
| 6246 | } |
| 6247 | } |
| 6248 | |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6249 | void AudioFlinger::EffectModule::updateState() { |
| 6250 | Mutex::Autolock _l(mLock); |
| 6251 | |
| 6252 | switch (mState) { |
| 6253 | case RESTART: |
| 6254 | reset_l(); |
| 6255 | // FALL THROUGH |
| 6256 | |
| 6257 | case STARTING: |
| 6258 | // clear auxiliary effect input buffer for next accumulation |
| 6259 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6260 | memset(mConfig.inputCfg.buffer.raw, |
| 6261 | 0, |
| 6262 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 6263 | } |
| 6264 | start_l(); |
| 6265 | mState = ACTIVE; |
| 6266 | break; |
| 6267 | case STOPPING: |
| 6268 | stop_l(); |
| 6269 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 6270 | mState = STOPPED; |
| 6271 | break; |
| 6272 | case STOPPED: |
| 6273 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 6274 | // turn off sequence. |
| 6275 | if (--mDisableWaitCnt == 0) { |
| 6276 | reset_l(); |
| 6277 | mState = IDLE; |
| 6278 | } |
| 6279 | break; |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6280 | default: //IDLE , ACTIVE, DESTROYED |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6281 | break; |
| 6282 | } |
| 6283 | } |
| 6284 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6285 | void AudioFlinger::EffectModule::process() |
| 6286 | { |
| 6287 | Mutex::Autolock _l(mLock); |
| 6288 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6289 | if (mState == DESTROYED || mEffectInterface == NULL || |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6290 | mConfig.inputCfg.buffer.raw == NULL || |
| 6291 | mConfig.outputCfg.buffer.raw == NULL) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6292 | return; |
| 6293 | } |
| 6294 | |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6295 | if (isProcessEnabled()) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6296 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 6297 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6298 | AudioMixer::ditherAndClamp(mConfig.inputCfg.buffer.s32, |
| 6299 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6300 | mConfig.inputCfg.buffer.frameCount/2); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6301 | } |
| 6302 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6303 | // do the actual processing in the effect engine |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6304 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 6305 | &mConfig.inputCfg.buffer, |
| 6306 | &mConfig.outputCfg.buffer); |
| 6307 | |
| 6308 | // force transition to IDLE state when engine is ready |
| 6309 | if (mState == STOPPED && ret == -ENODATA) { |
| 6310 | mDisableWaitCnt = 1; |
| 6311 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6312 | |
| 6313 | // clear auxiliary effect input buffer for next accumulation |
| 6314 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6315 | memset(mConfig.inputCfg.buffer.raw, 0, |
| 6316 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6317 | } |
| 6318 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6319 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6320 | // If an insert effect is idle and input buffer is different from output buffer, |
| 6321 | // accumulate input onto output |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6322 | sp<EffectChain> chain = mChain.promote(); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 6323 | if (chain != 0 && chain->activeTrackCnt() != 0) { |
Eric Laurent | 67b5ed3 | 2011-01-19 18:36:13 -0800 | [diff] [blame] | 6324 | size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here |
| 6325 | int16_t *in = mConfig.inputCfg.buffer.s16; |
| 6326 | int16_t *out = mConfig.outputCfg.buffer.s16; |
| 6327 | for (size_t i = 0; i < frameCnt; i++) { |
| 6328 | out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6329 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6330 | } |
| 6331 | } |
| 6332 | } |
| 6333 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6334 | void AudioFlinger::EffectModule::reset_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6335 | { |
| 6336 | if (mEffectInterface == NULL) { |
| 6337 | return; |
| 6338 | } |
| 6339 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 6340 | } |
| 6341 | |
| 6342 | status_t AudioFlinger::EffectModule::configure() |
| 6343 | { |
| 6344 | uint32_t channels; |
| 6345 | if (mEffectInterface == NULL) { |
| 6346 | return NO_INIT; |
| 6347 | } |
| 6348 | |
| 6349 | sp<ThreadBase> thread = mThread.promote(); |
| 6350 | if (thread == 0) { |
| 6351 | return DEAD_OBJECT; |
| 6352 | } |
| 6353 | |
| 6354 | // TODO: handle configuration of effects replacing track process |
| 6355 | if (thread->channelCount() == 1) { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6356 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6357 | } else { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6358 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6362 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6363 | } else { |
| 6364 | mConfig.inputCfg.channels = channels; |
| 6365 | } |
| 6366 | mConfig.outputCfg.channels = channels; |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6367 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 6368 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6369 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 6370 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 6371 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 6372 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 6373 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 6374 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 6375 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 6376 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 6377 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 6378 | // Insert effect: |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 6379 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6380 | // always overwrites output buffer: input buffer == output buffer |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6381 | // - in other sessions: |
| 6382 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 6383 | // other effect: overwrites output buffer: input buffer == output buffer |
| 6384 | // Auxiliary effect: |
| 6385 | // accumulates in output buffer: input buffer != output buffer |
| 6386 | // Therefore: accumulate <=> input buffer != output buffer |
| 6387 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 6388 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 6389 | } else { |
| 6390 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 6391 | } |
| 6392 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 6393 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 6394 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 6395 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 6396 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6397 | LOGV("configure() %p thread %p buffer %p framecount %d", |
| 6398 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 6399 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6400 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6401 | uint32_t size = sizeof(int); |
| 6402 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6403 | EFFECT_CMD_CONFIGURE, |
| 6404 | sizeof(effect_config_t), |
| 6405 | &mConfig, |
| 6406 | &size, |
| 6407 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6408 | if (status == 0) { |
| 6409 | status = cmdStatus; |
| 6410 | } |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6411 | |
| 6412 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 6413 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 6414 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6415 | return status; |
| 6416 | } |
| 6417 | |
| 6418 | status_t AudioFlinger::EffectModule::init() |
| 6419 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6420 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6421 | if (mEffectInterface == NULL) { |
| 6422 | return NO_INIT; |
| 6423 | } |
| 6424 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6425 | uint32_t size = sizeof(status_t); |
| 6426 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6427 | EFFECT_CMD_INIT, |
| 6428 | 0, |
| 6429 | NULL, |
| 6430 | &size, |
| 6431 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6432 | if (status == 0) { |
| 6433 | status = cmdStatus; |
| 6434 | } |
| 6435 | return status; |
| 6436 | } |
| 6437 | |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 6438 | status_t AudioFlinger::EffectModule::start() |
| 6439 | { |
| 6440 | Mutex::Autolock _l(mLock); |
| 6441 | return start_l(); |
| 6442 | } |
| 6443 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6444 | status_t AudioFlinger::EffectModule::start_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6445 | { |
| 6446 | if (mEffectInterface == NULL) { |
| 6447 | return NO_INIT; |
| 6448 | } |
| 6449 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6450 | uint32_t size = sizeof(status_t); |
| 6451 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6452 | EFFECT_CMD_ENABLE, |
| 6453 | 0, |
| 6454 | NULL, |
| 6455 | &size, |
| 6456 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6457 | if (status == 0) { |
| 6458 | status = cmdStatus; |
| 6459 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6460 | if (status == 0 && |
| 6461 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6462 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6463 | sp<ThreadBase> thread = mThread.promote(); |
| 6464 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6465 | audio_stream_t *stream = thread->stream(); |
| 6466 | if (stream != NULL) { |
| 6467 | stream->add_audio_effect(stream, mEffectInterface); |
| 6468 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6469 | } |
| 6470 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6471 | return status; |
| 6472 | } |
| 6473 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6474 | status_t AudioFlinger::EffectModule::stop() |
| 6475 | { |
| 6476 | Mutex::Autolock _l(mLock); |
| 6477 | return stop_l(); |
| 6478 | } |
| 6479 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6480 | status_t AudioFlinger::EffectModule::stop_l() |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6481 | { |
| 6482 | if (mEffectInterface == NULL) { |
| 6483 | return NO_INIT; |
| 6484 | } |
| 6485 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6486 | uint32_t size = sizeof(status_t); |
| 6487 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6488 | EFFECT_CMD_DISABLE, |
| 6489 | 0, |
| 6490 | NULL, |
| 6491 | &size, |
| 6492 | &cmdStatus); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6493 | if (status == 0) { |
| 6494 | status = cmdStatus; |
| 6495 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6496 | if (status == 0 && |
| 6497 | ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 6498 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { |
| 6499 | sp<ThreadBase> thread = mThread.promote(); |
| 6500 | if (thread != 0) { |
Eric Laurent | 828b977 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 6501 | audio_stream_t *stream = thread->stream(); |
| 6502 | if (stream != NULL) { |
| 6503 | stream->remove_audio_effect(stream, mEffectInterface); |
| 6504 | } |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6505 | } |
| 6506 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6507 | return status; |
| 6508 | } |
| 6509 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6510 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 6511 | uint32_t cmdSize, |
| 6512 | void *pCmdData, |
| 6513 | uint32_t *replySize, |
| 6514 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6515 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6516 | Mutex::Autolock _l(mLock); |
| 6517 | // LOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6518 | |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6519 | if (mState == DESTROYED || mEffectInterface == NULL) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6520 | return NO_INIT; |
| 6521 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6522 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 6523 | cmdCode, |
| 6524 | cmdSize, |
| 6525 | pCmdData, |
| 6526 | replySize, |
| 6527 | pReplyData); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6528 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6529 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6530 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6531 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6532 | if (h != 0) { |
| 6533 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 6534 | } |
| 6535 | } |
| 6536 | } |
| 6537 | return status; |
| 6538 | } |
| 6539 | |
| 6540 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 6541 | { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6542 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6543 | Mutex::Autolock _l(mLock); |
| 6544 | LOGV("setEnabled %p enabled %d", this, enabled); |
| 6545 | |
| 6546 | if (enabled != isEnabled()) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6547 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 6548 | if (enabled && status != NO_ERROR) { |
| 6549 | return status; |
| 6550 | } |
| 6551 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6552 | switch (mState) { |
| 6553 | // going from disabled to enabled |
| 6554 | case IDLE: |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6555 | mState = STARTING; |
| 6556 | break; |
| 6557 | case STOPPED: |
| 6558 | mState = RESTART; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6559 | break; |
| 6560 | case STOPPING: |
| 6561 | mState = ACTIVE; |
| 6562 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6563 | |
| 6564 | // going from enabled to disabled |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6565 | case RESTART: |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6566 | mState = STOPPED; |
| 6567 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6568 | case STARTING: |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6569 | mState = IDLE; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6570 | break; |
| 6571 | case ACTIVE: |
| 6572 | mState = STOPPING; |
| 6573 | break; |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6574 | case DESTROYED: |
| 6575 | return NO_ERROR; // simply ignore as we are being destroyed |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6576 | } |
| 6577 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 6578 | sp<EffectHandle> h = mHandles[i].promote(); |
| 6579 | if (h != 0) { |
| 6580 | h->setEnabled(enabled); |
| 6581 | } |
| 6582 | } |
| 6583 | } |
| 6584 | return NO_ERROR; |
| 6585 | } |
| 6586 | |
| 6587 | bool AudioFlinger::EffectModule::isEnabled() |
| 6588 | { |
| 6589 | switch (mState) { |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 6590 | case RESTART: |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6591 | case STARTING: |
| 6592 | case ACTIVE: |
| 6593 | return true; |
| 6594 | case IDLE: |
| 6595 | case STOPPING: |
| 6596 | case STOPPED: |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6597 | case DESTROYED: |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6598 | default: |
| 6599 | return false; |
| 6600 | } |
| 6601 | } |
| 6602 | |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6603 | bool AudioFlinger::EffectModule::isProcessEnabled() |
| 6604 | { |
| 6605 | switch (mState) { |
| 6606 | case RESTART: |
| 6607 | case ACTIVE: |
| 6608 | case STOPPING: |
| 6609 | case STOPPED: |
| 6610 | return true; |
| 6611 | case IDLE: |
| 6612 | case STARTING: |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 6613 | case DESTROYED: |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6614 | default: |
| 6615 | return false; |
| 6616 | } |
| 6617 | } |
| 6618 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6619 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 6620 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6621 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6622 | status_t status = NO_ERROR; |
| 6623 | |
| 6624 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 6625 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 6626 | if (isProcessEnabled() && |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6627 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 6628 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6629 | status_t cmdStatus; |
| 6630 | uint32_t volume[2]; |
| 6631 | uint32_t *pVolume = NULL; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6632 | uint32_t size = sizeof(volume); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6633 | volume[0] = *left; |
| 6634 | volume[1] = *right; |
| 6635 | if (controller) { |
| 6636 | pVolume = volume; |
| 6637 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6638 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6639 | EFFECT_CMD_SET_VOLUME, |
| 6640 | size, |
| 6641 | volume, |
| 6642 | &size, |
| 6643 | pVolume); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6644 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 6645 | *left = volume[0]; |
| 6646 | *right = volume[1]; |
| 6647 | } |
| 6648 | } |
| 6649 | return status; |
| 6650 | } |
| 6651 | |
| 6652 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 6653 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6654 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6655 | status_t status = NO_ERROR; |
Eric Laurent | 464d5b3 | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 6656 | if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 6657 | // audio pre processing modules on RecordThread can receive both output and |
| 6658 | // input device indication in the same call |
| 6659 | uint32_t dev = device & AUDIO_DEVICE_OUT_ALL; |
| 6660 | if (dev) { |
| 6661 | status_t cmdStatus; |
| 6662 | uint32_t size = sizeof(status_t); |
| 6663 | |
| 6664 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6665 | EFFECT_CMD_SET_DEVICE, |
| 6666 | sizeof(uint32_t), |
| 6667 | &dev, |
| 6668 | &size, |
| 6669 | &cmdStatus); |
| 6670 | if (status == NO_ERROR) { |
| 6671 | status = cmdStatus; |
| 6672 | } |
| 6673 | } |
| 6674 | dev = device & AUDIO_DEVICE_IN_ALL; |
| 6675 | if (dev) { |
| 6676 | status_t cmdStatus; |
| 6677 | uint32_t size = sizeof(status_t); |
| 6678 | |
| 6679 | status_t status2 = (*mEffectInterface)->command(mEffectInterface, |
| 6680 | EFFECT_CMD_SET_INPUT_DEVICE, |
| 6681 | sizeof(uint32_t), |
| 6682 | &dev, |
| 6683 | &size, |
| 6684 | &cmdStatus); |
| 6685 | if (status2 == NO_ERROR) { |
| 6686 | status2 = cmdStatus; |
| 6687 | } |
| 6688 | if (status == NO_ERROR) { |
| 6689 | status = status2; |
| 6690 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6691 | } |
| 6692 | } |
| 6693 | return status; |
| 6694 | } |
| 6695 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6696 | status_t AudioFlinger::EffectModule::setMode(uint32_t mode) |
| 6697 | { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 6698 | Mutex::Autolock _l(mLock); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6699 | status_t status = NO_ERROR; |
| 6700 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6701 | status_t cmdStatus; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6702 | uint32_t size = sizeof(status_t); |
| 6703 | status = (*mEffectInterface)->command(mEffectInterface, |
| 6704 | EFFECT_CMD_SET_AUDIO_MODE, |
| 6705 | sizeof(int), |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6706 | &mode, |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6707 | &size, |
| 6708 | &cmdStatus); |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6709 | if (status == NO_ERROR) { |
| 6710 | status = cmdStatus; |
| 6711 | } |
| 6712 | } |
| 6713 | return status; |
| 6714 | } |
| 6715 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6716 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 6717 | { |
| 6718 | Mutex::Autolock _l(mLock); |
| 6719 | mSuspended = suspended; |
| 6720 | } |
| 6721 | bool AudioFlinger::EffectModule::suspended() |
| 6722 | { |
| 6723 | Mutex::Autolock _l(mLock); |
| 6724 | return mSuspended; |
| 6725 | } |
| 6726 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6727 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 6728 | { |
| 6729 | const size_t SIZE = 256; |
| 6730 | char buffer[SIZE]; |
| 6731 | String8 result; |
| 6732 | |
| 6733 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 6734 | result.append(buffer); |
| 6735 | |
| 6736 | bool locked = tryLock(mLock); |
| 6737 | // failed to lock - AudioFlinger is probably deadlocked |
| 6738 | if (!locked) { |
| 6739 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 6740 | } |
| 6741 | |
| 6742 | result.append("\t\tSession Status State Engine:\n"); |
| 6743 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 6744 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 6745 | result.append(buffer); |
| 6746 | |
| 6747 | result.append("\t\tDescriptor:\n"); |
| 6748 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6749 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 6750 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 6751 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 6752 | result.append(buffer); |
| 6753 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 6754 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 6755 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 6756 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 6757 | result.append(buffer); |
Eric Laurent | 0fb66c2 | 2011-05-17 19:16:02 -0700 | [diff] [blame] | 6758 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6759 | mDescriptor.apiVersion, |
| 6760 | mDescriptor.flags); |
| 6761 | result.append(buffer); |
| 6762 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 6763 | mDescriptor.name); |
| 6764 | result.append(buffer); |
| 6765 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 6766 | mDescriptor.implementor); |
| 6767 | result.append(buffer); |
| 6768 | |
| 6769 | result.append("\t\t- Input configuration:\n"); |
| 6770 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6771 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6772 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 6773 | mConfig.inputCfg.buffer.frameCount, |
| 6774 | mConfig.inputCfg.samplingRate, |
| 6775 | mConfig.inputCfg.channels, |
| 6776 | mConfig.inputCfg.format); |
| 6777 | result.append(buffer); |
| 6778 | |
| 6779 | result.append("\t\t- Output configuration:\n"); |
| 6780 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 6781 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 6782 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 6783 | mConfig.outputCfg.buffer.frameCount, |
| 6784 | mConfig.outputCfg.samplingRate, |
| 6785 | mConfig.outputCfg.channels, |
| 6786 | mConfig.outputCfg.format); |
| 6787 | result.append(buffer); |
| 6788 | |
| 6789 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 6790 | result.append(buffer); |
| 6791 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 6792 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 6793 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 6794 | if (handle != 0) { |
| 6795 | handle->dump(buffer, SIZE); |
| 6796 | result.append(buffer); |
| 6797 | } |
| 6798 | } |
| 6799 | |
| 6800 | result.append("\n"); |
| 6801 | |
| 6802 | write(fd, result.string(), result.length()); |
| 6803 | |
| 6804 | if (locked) { |
| 6805 | mLock.unlock(); |
| 6806 | } |
| 6807 | |
| 6808 | return NO_ERROR; |
| 6809 | } |
| 6810 | |
| 6811 | // ---------------------------------------------------------------------------- |
| 6812 | // EffectHandle implementation |
| 6813 | // ---------------------------------------------------------------------------- |
| 6814 | |
| 6815 | #undef LOG_TAG |
| 6816 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 6817 | |
| 6818 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 6819 | const sp<AudioFlinger::Client>& client, |
| 6820 | const sp<IEffectClient>& effectClient, |
| 6821 | int32_t priority) |
| 6822 | : BnEffect(), |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6823 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6824 | mPriority(priority), mHasControl(false), mEnabled(false) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6825 | { |
| 6826 | LOGV("constructor %p", this); |
| 6827 | |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6828 | if (client == 0) { |
| 6829 | return; |
| 6830 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6831 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 6832 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 6833 | if (mCblkMemory != 0) { |
| 6834 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 6835 | |
| 6836 | if (mCblk) { |
| 6837 | new(mCblk) effect_param_cblk_t(); |
| 6838 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 6839 | } |
| 6840 | } else { |
| 6841 | LOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
| 6842 | return; |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | AudioFlinger::EffectHandle::~EffectHandle() |
| 6847 | { |
| 6848 | LOGV("Destructor %p", this); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6849 | disconnect(false); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6850 | LOGV("Destructor DONE %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | status_t AudioFlinger::EffectHandle::enable() |
| 6854 | { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6855 | LOGV("enable %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6856 | if (!mHasControl) return INVALID_OPERATION; |
| 6857 | if (mEffect == 0) return DEAD_OBJECT; |
| 6858 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6859 | if (mEnabled) { |
| 6860 | return NO_ERROR; |
| 6861 | } |
| 6862 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6863 | mEnabled = true; |
| 6864 | |
| 6865 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6866 | if (thread != 0) { |
| 6867 | thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); |
| 6868 | } |
| 6869 | |
| 6870 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
| 6871 | if (mEffect->suspended()) { |
| 6872 | return NO_ERROR; |
| 6873 | } |
| 6874 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6875 | status_t status = mEffect->setEnabled(true); |
| 6876 | if (status != NO_ERROR) { |
| 6877 | if (thread != 0) { |
| 6878 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6879 | } |
| 6880 | mEnabled = false; |
| 6881 | } |
| 6882 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | status_t AudioFlinger::EffectHandle::disable() |
| 6886 | { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6887 | LOGV("disable %p", this); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6888 | if (!mHasControl) return INVALID_OPERATION; |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6889 | if (mEffect == 0) return DEAD_OBJECT; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6890 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6891 | if (!mEnabled) { |
| 6892 | return NO_ERROR; |
| 6893 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6894 | mEnabled = false; |
| 6895 | |
| 6896 | if (mEffect->suspended()) { |
| 6897 | return NO_ERROR; |
| 6898 | } |
| 6899 | |
| 6900 | status_t status = mEffect->setEnabled(false); |
| 6901 | |
| 6902 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6903 | if (thread != 0) { |
| 6904 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6905 | } |
| 6906 | |
| 6907 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | void AudioFlinger::EffectHandle::disconnect() |
| 6911 | { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6912 | disconnect(true); |
| 6913 | } |
| 6914 | |
| 6915 | void AudioFlinger::EffectHandle::disconnect(bool unpiniflast) |
| 6916 | { |
| 6917 | LOGV("disconnect(%s)", unpiniflast ? "true" : "false"); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6918 | if (mEffect == 0) { |
| 6919 | return; |
| 6920 | } |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6921 | mEffect->disconnect(this, unpiniflast); |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6922 | |
Eric Laurent | 7fa1cee | 2011-10-19 11:44:54 -0700 | [diff] [blame] | 6923 | if (mHasControl && mEnabled) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 6924 | sp<ThreadBase> thread = mEffect->thread().promote(); |
| 6925 | if (thread != 0) { |
| 6926 | thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); |
| 6927 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 6928 | } |
| 6929 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6930 | // release sp on module => module destructor can be called now |
| 6931 | mEffect.clear(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6932 | if (mClient != 0) { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6933 | if (mCblk) { |
| 6934 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 6935 | } |
| 6936 | mCblkMemory.clear(); // and free the shared memory |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6937 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 6938 | mClient.clear(); |
| 6939 | } |
| 6940 | } |
| 6941 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6942 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 6943 | uint32_t cmdSize, |
| 6944 | void *pCmdData, |
| 6945 | uint32_t *replySize, |
| 6946 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6947 | { |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6948 | // LOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
| 6949 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6950 | |
| 6951 | // only get parameter command is permitted for applications not controlling the effect |
| 6952 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 6953 | return INVALID_OPERATION; |
| 6954 | } |
| 6955 | if (mEffect == 0) return DEAD_OBJECT; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 6956 | if (mClient == 0) return INVALID_OPERATION; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6957 | |
| 6958 | // handle commands that are not forwarded transparently to effect engine |
| 6959 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 6960 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 6961 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 6962 | Mutex::Autolock _l(mCblk->lock); |
| 6963 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 6964 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 6965 | mCblk->serverIndex = 0; |
| 6966 | mCblk->clientIndex = 0; |
| 6967 | return BAD_VALUE; |
| 6968 | } |
| 6969 | status_t status = NO_ERROR; |
| 6970 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 6971 | int reply; |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6972 | uint32_t rsize = sizeof(int); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6973 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 6974 | int size = *p++; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6975 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
| 6976 | LOGW("command(): invalid parameter block size"); |
| 6977 | break; |
| 6978 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6979 | effect_param_t *param = (effect_param_t *)p; |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 6980 | if (param->psize == 0 || param->vsize == 0) { |
| 6981 | LOGW("command(): null parameter or value size"); |
| 6982 | mCblk->serverIndex += size; |
| 6983 | continue; |
| 6984 | } |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 6985 | uint32_t psize = sizeof(effect_param_t) + |
| 6986 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 6987 | param->vsize; |
| 6988 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 6989 | psize, |
| 6990 | p, |
| 6991 | &rsize, |
| 6992 | &reply); |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6993 | // stop at first error encountered |
| 6994 | if (ret != NO_ERROR) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 6995 | status = ret; |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 6996 | *(int *)pReplyData = reply; |
| 6997 | break; |
| 6998 | } else if (reply != NO_ERROR) { |
| 6999 | *(int *)pReplyData = reply; |
| 7000 | break; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7001 | } |
| 7002 | mCblk->serverIndex += size; |
| 7003 | } |
| 7004 | mCblk->serverIndex = 0; |
| 7005 | mCblk->clientIndex = 0; |
| 7006 | return status; |
| 7007 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7008 | *(int *)pReplyData = NO_ERROR; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7009 | return enable(); |
| 7010 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
Eric Laurent | e65280c | 2010-09-02 11:56:55 -0700 | [diff] [blame] | 7011 | *(int *)pReplyData = NO_ERROR; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7012 | return disable(); |
| 7013 | } |
| 7014 | |
| 7015 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7016 | } |
| 7017 | |
| 7018 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { |
| 7019 | return mCblkMemory; |
| 7020 | } |
| 7021 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7022 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7023 | { |
| 7024 | LOGV("setControl %p control %d", this, hasControl); |
| 7025 | |
| 7026 | mHasControl = hasControl; |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7027 | mEnabled = enabled; |
| 7028 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7029 | if (signal && mEffectClient != 0) { |
| 7030 | mEffectClient->controlStatusChanged(hasControl); |
| 7031 | } |
| 7032 | } |
| 7033 | |
Eric Laurent | a4c72ac | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 7034 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 7035 | uint32_t cmdSize, |
| 7036 | void *pCmdData, |
| 7037 | uint32_t replySize, |
| 7038 | void *pReplyData) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7039 | { |
| 7040 | if (mEffectClient != 0) { |
| 7041 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 7042 | } |
| 7043 | } |
| 7044 | |
| 7045 | |
| 7046 | |
| 7047 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 7048 | { |
| 7049 | if (mEffectClient != 0) { |
| 7050 | mEffectClient->enableStatusChanged(enabled); |
| 7051 | } |
| 7052 | } |
| 7053 | |
| 7054 | status_t AudioFlinger::EffectHandle::onTransact( |
| 7055 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7056 | { |
| 7057 | return BnEffect::onTransact(code, data, reply, flags); |
| 7058 | } |
| 7059 | |
| 7060 | |
| 7061 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 7062 | { |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7063 | bool locked = mCblk ? tryLock(mCblk->lock) : false; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7064 | |
| 7065 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
| 7066 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 7067 | mPriority, |
| 7068 | mHasControl, |
| 7069 | !locked, |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 7070 | mCblk ? mCblk->clientIndex : 0, |
| 7071 | mCblk ? mCblk->serverIndex : 0 |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7072 | ); |
| 7073 | |
| 7074 | if (locked) { |
| 7075 | mCblk->lock.unlock(); |
| 7076 | } |
| 7077 | } |
| 7078 | |
| 7079 | #undef LOG_TAG |
| 7080 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 7081 | |
| 7082 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, |
| 7083 | int sessionId) |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7084 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7085 | mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 7086 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7087 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7088 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7089 | sp<ThreadBase> thread = mThread.promote(); |
| 7090 | if (thread == 0) { |
| 7091 | return; |
| 7092 | } |
| 7093 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 7094 | thread->frameCount(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | AudioFlinger::EffectChain::~EffectChain() |
| 7098 | { |
| 7099 | if (mOwnInBuffer) { |
| 7100 | delete mInBuffer; |
| 7101 | } |
| 7102 | |
| 7103 | } |
| 7104 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7105 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7106 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7107 | { |
| 7108 | sp<EffectModule> effect; |
| 7109 | size_t size = mEffects.size(); |
| 7110 | |
| 7111 | for (size_t i = 0; i < size; i++) { |
| 7112 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 7113 | effect = mEffects[i]; |
| 7114 | break; |
| 7115 | } |
| 7116 | } |
| 7117 | return effect; |
| 7118 | } |
| 7119 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7120 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7121 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7122 | { |
| 7123 | sp<EffectModule> effect; |
| 7124 | size_t size = mEffects.size(); |
| 7125 | |
| 7126 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7127 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 7128 | if (id == 0 || mEffects[i]->id() == id) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7129 | effect = mEffects[i]; |
| 7130 | break; |
| 7131 | } |
| 7132 | } |
| 7133 | return effect; |
| 7134 | } |
| 7135 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7136 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 7137 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 7138 | const effect_uuid_t *type) |
| 7139 | { |
| 7140 | sp<EffectModule> effect; |
| 7141 | size_t size = mEffects.size(); |
| 7142 | |
| 7143 | for (size_t i = 0; i < size; i++) { |
| 7144 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 7145 | effect = mEffects[i]; |
| 7146 | break; |
| 7147 | } |
| 7148 | } |
| 7149 | return effect; |
| 7150 | } |
| 7151 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7152 | // Must be called with EffectChain::mLock locked |
| 7153 | void AudioFlinger::EffectChain::process_l() |
| 7154 | { |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7155 | sp<ThreadBase> thread = mThread.promote(); |
| 7156 | if (thread == 0) { |
| 7157 | LOGW("process_l(): cannot promote mixer thread"); |
| 7158 | return; |
| 7159 | } |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 7160 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 7161 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7162 | // always process effects unless no more tracks are on the session and the effect tail |
| 7163 | // has been rendered |
| 7164 | bool doProcess = true; |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7165 | if (!isGlobalSession) { |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7166 | bool tracksOnSession = (trackCnt() != 0); |
Eric Laurent | 90681d6 | 2011-05-09 12:09:06 -0700 | [diff] [blame] | 7167 | |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7168 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 7169 | doProcess = false; |
| 7170 | } |
| 7171 | |
| 7172 | if (activeTrackCnt() == 0) { |
| 7173 | // if no track is active and the effect tail has not been rendered, |
| 7174 | // the input buffer must be cleared here as the mixer process will not do it |
| 7175 | if (tracksOnSession || mTailBufferCount > 0) { |
| 7176 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 7177 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 7178 | if (mTailBufferCount > 0) { |
| 7179 | mTailBufferCount--; |
| 7180 | } |
| 7181 | } |
| 7182 | } |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7183 | } |
| 7184 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7185 | size_t size = mEffects.size(); |
Eric Laurent | f9c361d | 2011-11-11 15:42:52 -0800 | [diff] [blame] | 7186 | if (doProcess) { |
Eric Laurent | 4fd3ecc | 2010-09-28 14:09:57 -0700 | [diff] [blame] | 7187 | for (size_t i = 0; i < size; i++) { |
| 7188 | mEffects[i]->process(); |
| 7189 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7190 | } |
Eric Laurent | 7d850f2 | 2010-07-09 13:34:17 -0700 | [diff] [blame] | 7191 | for (size_t i = 0; i < size; i++) { |
| 7192 | mEffects[i]->updateState(); |
| 7193 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7194 | } |
| 7195 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7196 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7197 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7198 | { |
| 7199 | effect_descriptor_t desc = effect->desc(); |
| 7200 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 7201 | |
| 7202 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7203 | effect->setChain(this); |
| 7204 | sp<ThreadBase> thread = mThread.promote(); |
| 7205 | if (thread == 0) { |
| 7206 | return NO_INIT; |
| 7207 | } |
| 7208 | effect->setThread(thread); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7209 | |
| 7210 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7211 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 7212 | // they are processed first and accumulated in chain input buffer |
| 7213 | mEffects.insertAt(effect, 0); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 7214 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7215 | // the input buffer for auxiliary effect contains mono samples in |
| 7216 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 7217 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 7218 | // calling the process in effect engine |
| 7219 | size_t numSamples = thread->frameCount(); |
| 7220 | int32_t *buffer = new int32_t[numSamples]; |
| 7221 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 7222 | effect->setInBuffer((int16_t *)buffer); |
| 7223 | // auxiliary effects output samples to chain input buffer for further processing |
| 7224 | // by insert effects |
| 7225 | effect->setOutBuffer(mInBuffer); |
| 7226 | } else { |
| 7227 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 7228 | // after track and auxiliary effects. |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7229 | // Insert effect order as a function of indicated preference: |
| 7230 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 7231 | // another effect is present |
| 7232 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 7233 | // last effect claiming first position |
| 7234 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 7235 | // first effect claiming last position |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7236 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7237 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 7238 | // already present |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7239 | |
| 7240 | int size = (int)mEffects.size(); |
| 7241 | int idx_insert = size; |
| 7242 | int idx_insert_first = -1; |
| 7243 | int idx_insert_last = -1; |
| 7244 | |
| 7245 | for (int i = 0; i < size; i++) { |
| 7246 | effect_descriptor_t d = mEffects[i]->desc(); |
| 7247 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 7248 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 7249 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 7250 | // check invalid effect chaining combinations |
| 7251 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7252 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7253 | LOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7254 | return INVALID_OPERATION; |
| 7255 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7256 | // remember position of first insert effect and by default |
| 7257 | // select this as insert position for new effect |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7258 | if (idx_insert == size) { |
| 7259 | idx_insert = i; |
| 7260 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7261 | // remember position of last insert effect claiming |
| 7262 | // first position |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7263 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 7264 | idx_insert_first = i; |
| 7265 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7266 | // remember position of first insert effect claiming |
| 7267 | // last position |
| 7268 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 7269 | idx_insert_last == -1) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7270 | idx_insert_last = i; |
| 7271 | } |
| 7272 | } |
| 7273 | } |
| 7274 | |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7275 | // modify idx_insert from first position if needed |
| 7276 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 7277 | if (idx_insert_last != -1) { |
| 7278 | idx_insert = idx_insert_last; |
| 7279 | } else { |
| 7280 | idx_insert = size; |
| 7281 | } |
| 7282 | } else { |
| 7283 | if (idx_insert_first != -1) { |
| 7284 | idx_insert = idx_insert_first + 1; |
| 7285 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7286 | } |
| 7287 | |
| 7288 | // always read samples from chain input buffer |
| 7289 | effect->setInBuffer(mInBuffer); |
| 7290 | |
| 7291 | // if last effect in the chain, output samples to chain |
| 7292 | // output buffer, otherwise to chain input buffer |
| 7293 | if (idx_insert == size) { |
| 7294 | if (idx_insert != 0) { |
| 7295 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 7296 | mEffects[idx_insert-1]->configure(); |
| 7297 | } |
| 7298 | effect->setOutBuffer(mOutBuffer); |
| 7299 | } else { |
| 7300 | effect->setOutBuffer(mInBuffer); |
| 7301 | } |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7302 | mEffects.insertAt(effect, idx_insert); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7303 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7304 | LOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7305 | } |
| 7306 | effect->configure(); |
| 7307 | return NO_ERROR; |
| 7308 | } |
| 7309 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7310 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 7311 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7312 | { |
| 7313 | Mutex::Autolock _l(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7314 | int size = (int)mEffects.size(); |
| 7315 | int i; |
| 7316 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 7317 | |
| 7318 | for (i = 0; i < size; i++) { |
| 7319 | if (effect == mEffects[i]) { |
Eric Laurent | 21b5c47 | 2011-07-26 20:54:46 -0700 | [diff] [blame] | 7320 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 7321 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 7322 | // the middle of a read from audio HAL |
Eric Laurent | 6fccbd0 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 7323 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 7324 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 7325 | mEffects[i]->stop(); |
| 7326 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7327 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 7328 | delete[] effect->inBuffer(); |
| 7329 | } else { |
| 7330 | if (i == size - 1 && i != 0) { |
| 7331 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 7332 | mEffects[i - 1]->configure(); |
| 7333 | } |
| 7334 | } |
| 7335 | mEffects.removeAt(i); |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7336 | LOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7337 | break; |
| 7338 | } |
| 7339 | } |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7340 | |
| 7341 | return mEffects.size(); |
| 7342 | } |
| 7343 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7344 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 7345 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7346 | { |
| 7347 | size_t size = mEffects.size(); |
| 7348 | for (size_t i = 0; i < size; i++) { |
| 7349 | mEffects[i]->setDevice(device); |
| 7350 | } |
| 7351 | } |
| 7352 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7353 | // setMode_l() must be called with PlaybackThread::mLock held |
| 7354 | void AudioFlinger::EffectChain::setMode_l(uint32_t mode) |
Eric Laurent | 53334cd | 2010-06-23 17:38:20 -0700 | [diff] [blame] | 7355 | { |
| 7356 | size_t size = mEffects.size(); |
| 7357 | for (size_t i = 0; i < size; i++) { |
| 7358 | mEffects[i]->setMode(mode); |
| 7359 | } |
| 7360 | } |
| 7361 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7362 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 7363 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7364 | { |
| 7365 | uint32_t newLeft = *left; |
| 7366 | uint32_t newRight = *right; |
| 7367 | bool hasControl = false; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7368 | int ctrlIdx = -1; |
| 7369 | size_t size = mEffects.size(); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7370 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7371 | // first update volume controller |
| 7372 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | a92ebfa | 2010-08-31 13:50:07 -0700 | [diff] [blame] | 7373 | if (mEffects[i - 1]->isProcessEnabled() && |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7374 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 7375 | ctrlIdx = i - 1; |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7376 | hasControl = true; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7377 | break; |
| 7378 | } |
| 7379 | } |
| 7380 | |
| 7381 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7382 | if (hasControl) { |
| 7383 | *left = mNewLeftVolume; |
| 7384 | *right = mNewRightVolume; |
| 7385 | } |
| 7386 | return hasControl; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7390 | mLeftVolume = newLeft; |
| 7391 | mRightVolume = newRight; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7392 | |
| 7393 | // second get volume update from volume controller |
| 7394 | if (ctrlIdx >= 0) { |
| 7395 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | 0d7e048 | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 7396 | mNewLeftVolume = newLeft; |
| 7397 | mNewRightVolume = newRight; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7398 | } |
| 7399 | // then indicate volume to all other effects in chain. |
| 7400 | // Pass altered volume to effects before volume controller |
| 7401 | // and requested volume to effects after controller |
| 7402 | uint32_t lVol = newLeft; |
| 7403 | uint32_t rVol = newRight; |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7404 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7405 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7406 | if ((int)i == ctrlIdx) continue; |
| 7407 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 7408 | if ((int)i > ctrlIdx) { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7409 | lVol = *left; |
| 7410 | rVol = *right; |
| 7411 | } |
| 7412 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 7413 | } |
| 7414 | *left = newLeft; |
| 7415 | *right = newRight; |
| 7416 | |
| 7417 | return hasControl; |
| 7418 | } |
| 7419 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7420 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 7421 | { |
| 7422 | const size_t SIZE = 256; |
| 7423 | char buffer[SIZE]; |
| 7424 | String8 result; |
| 7425 | |
| 7426 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 7427 | result.append(buffer); |
| 7428 | |
| 7429 | bool locked = tryLock(mLock); |
| 7430 | // failed to lock - AudioFlinger is probably deadlocked |
| 7431 | if (!locked) { |
| 7432 | result.append("\tCould not lock mutex:\n"); |
| 7433 | } |
| 7434 | |
Eric Laurent | 76c40f7 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 7435 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 7436 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7437 | mEffects.size(), |
| 7438 | (uint32_t)mInBuffer, |
| 7439 | (uint32_t)mOutBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7440 | mActiveTrackCnt); |
| 7441 | result.append(buffer); |
| 7442 | write(fd, result.string(), result.size()); |
| 7443 | |
| 7444 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 7445 | sp<EffectModule> effect = mEffects[i]; |
| 7446 | if (effect != 0) { |
| 7447 | effect->dump(fd, args); |
| 7448 | } |
| 7449 | } |
| 7450 | |
| 7451 | if (locked) { |
| 7452 | mLock.unlock(); |
| 7453 | } |
| 7454 | |
| 7455 | return NO_ERROR; |
| 7456 | } |
| 7457 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7458 | // must be called with ThreadBase::mLock held |
| 7459 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 7460 | const effect_uuid_t *type, bool suspend) |
| 7461 | { |
| 7462 | sp<SuspendedEffectDesc> desc; |
| 7463 | // use effect type UUID timelow as key as there is no real risk of identical |
| 7464 | // timeLow fields among effect type UUIDs. |
| 7465 | int index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 7466 | if (suspend) { |
| 7467 | if (index >= 0) { |
| 7468 | desc = mSuspendedEffects.valueAt(index); |
| 7469 | } else { |
| 7470 | desc = new SuspendedEffectDesc(); |
| 7471 | memcpy(&desc->mType, type, sizeof(effect_uuid_t)); |
| 7472 | mSuspendedEffects.add(type->timeLow, desc); |
| 7473 | LOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
| 7474 | } |
| 7475 | if (desc->mRefCount++ == 0) { |
| 7476 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 7477 | if (effect != 0) { |
| 7478 | desc->mEffect = effect; |
| 7479 | effect->setSuspended(true); |
| 7480 | effect->setEnabled(false); |
| 7481 | } |
| 7482 | } |
| 7483 | } else { |
| 7484 | if (index < 0) { |
| 7485 | return; |
| 7486 | } |
| 7487 | desc = mSuspendedEffects.valueAt(index); |
| 7488 | if (desc->mRefCount <= 0) { |
| 7489 | LOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 7490 | desc->mRefCount = 1; |
| 7491 | } |
| 7492 | if (--desc->mRefCount == 0) { |
| 7493 | LOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
| 7494 | if (desc->mEffect != 0) { |
| 7495 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 7496 | if (effect != 0) { |
| 7497 | effect->setSuspended(false); |
| 7498 | sp<EffectHandle> handle = effect->controlHandle(); |
| 7499 | if (handle != 0) { |
| 7500 | effect->setEnabled(handle->enabled()); |
| 7501 | } |
| 7502 | } |
| 7503 | desc->mEffect.clear(); |
| 7504 | } |
| 7505 | mSuspendedEffects.removeItemsAt(index); |
| 7506 | } |
| 7507 | } |
| 7508 | } |
| 7509 | |
| 7510 | // must be called with ThreadBase::mLock held |
| 7511 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 7512 | { |
| 7513 | sp<SuspendedEffectDesc> desc; |
| 7514 | |
| 7515 | int index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7516 | if (suspend) { |
| 7517 | if (index >= 0) { |
| 7518 | desc = mSuspendedEffects.valueAt(index); |
| 7519 | } else { |
| 7520 | desc = new SuspendedEffectDesc(); |
| 7521 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
| 7522 | LOGV("setEffectSuspendedAll_l() add entry for 0"); |
| 7523 | } |
| 7524 | if (desc->mRefCount++ == 0) { |
| 7525 | Vector< sp<EffectModule> > effects = getSuspendEligibleEffects(); |
| 7526 | for (size_t i = 0; i < effects.size(); i++) { |
| 7527 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 7528 | } |
| 7529 | } |
| 7530 | } else { |
| 7531 | if (index < 0) { |
| 7532 | return; |
| 7533 | } |
| 7534 | desc = mSuspendedEffects.valueAt(index); |
| 7535 | if (desc->mRefCount <= 0) { |
| 7536 | LOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 7537 | desc->mRefCount = 1; |
| 7538 | } |
| 7539 | if (--desc->mRefCount == 0) { |
| 7540 | Vector<const effect_uuid_t *> types; |
| 7541 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 7542 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 7543 | continue; |
| 7544 | } |
| 7545 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 7546 | } |
| 7547 | for (size_t i = 0; i < types.size(); i++) { |
| 7548 | setEffectSuspended_l(types[i], false); |
| 7549 | } |
| 7550 | LOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
| 7551 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 7552 | } |
| 7553 | } |
| 7554 | } |
| 7555 | |
Eric Laurent | 5e7acae | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7556 | |
| 7557 | // The volume effect is used for automated tests only |
| 7558 | #ifndef OPENSL_ES_H_ |
| 7559 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 7560 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 7561 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 7562 | #endif //OPENSL_ES_H_ |
| 7563 | |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7564 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 7565 | { |
| 7566 | // auxiliary effects and visualizer are never suspended on output mix |
| 7567 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 7568 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
Eric Laurent | 5e7acae | 2011-09-23 08:40:41 -0700 | [diff] [blame] | 7569 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 7570 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7571 | return false; |
| 7572 | } |
| 7573 | return true; |
| 7574 | } |
| 7575 | |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7576 | Vector< sp<AudioFlinger::EffectModule> > AudioFlinger::EffectChain::getSuspendEligibleEffects() |
| 7577 | { |
| 7578 | Vector< sp<EffectModule> > effects; |
| 7579 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7580 | if (!isEffectEligibleForSuspend(mEffects[i]->desc())) { |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7581 | continue; |
| 7582 | } |
| 7583 | effects.add(mEffects[i]); |
| 7584 | } |
| 7585 | return effects; |
| 7586 | } |
| 7587 | |
| 7588 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 7589 | const effect_uuid_t *type) |
| 7590 | { |
| 7591 | sp<EffectModule> effect; |
| 7592 | effect = getEffectFromType_l(type); |
| 7593 | if (effect != 0 && !effect->isEnabled()) { |
| 7594 | effect.clear(); |
| 7595 | } |
| 7596 | return effect; |
| 7597 | } |
| 7598 | |
| 7599 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 7600 | bool enabled) |
| 7601 | { |
| 7602 | int index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 7603 | if (enabled) { |
| 7604 | if (index < 0) { |
| 7605 | // if the effect is not suspend check if all effects are suspended |
| 7606 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 7607 | if (index < 0) { |
| 7608 | return; |
| 7609 | } |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7610 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 7611 | return; |
| 7612 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7613 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 7614 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
Eric Laurent | 6752ec8 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 7615 | if (index < 0) { |
| 7616 | LOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
| 7617 | return; |
| 7618 | } |
Eric Laurent | f82fccd | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 7619 | } |
| 7620 | LOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
| 7621 | effect->desc().type.timeLow); |
| 7622 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7623 | // if effect is requested to suspended but was not yet enabled, supend it now. |
| 7624 | if (desc->mEffect == 0) { |
| 7625 | desc->mEffect = effect; |
| 7626 | effect->setEnabled(false); |
| 7627 | effect->setSuspended(true); |
| 7628 | } |
| 7629 | } else { |
| 7630 | if (index < 0) { |
| 7631 | return; |
| 7632 | } |
| 7633 | LOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
| 7634 | effect->desc().type.timeLow); |
| 7635 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 7636 | desc->mEffect.clear(); |
| 7637 | effect->setSuspended(false); |
| 7638 | } |
| 7639 | } |
| 7640 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 7641 | #undef LOG_TAG |
| 7642 | #define LOG_TAG "AudioFlinger" |
| 7643 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 7644 | // ---------------------------------------------------------------------------- |
| 7645 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7646 | status_t AudioFlinger::onTransact( |
| 7647 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 7648 | { |
| 7649 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 7650 | } |
| 7651 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 7652 | }; // namespace android |