Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* //device/include/server/AudioFlinger/AudioFlinger.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <math.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <sys/resource.h> |
| 26 | |
| 27 | #include <binder/IServiceManager.h> |
| 28 | #include <utils/Log.h> |
| 29 | #include <binder/Parcel.h> |
| 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <utils/String16.h> |
| 32 | #include <utils/threads.h> |
| 33 | |
| 34 | #include <cutils/properties.h> |
| 35 | |
| 36 | #include <media/AudioTrack.h> |
| 37 | #include <media/AudioRecord.h> |
| 38 | |
| 39 | #include <private/media/AudioTrackShared.h> |
| 40 | #include <private/media/AudioEffectShared.h> |
| 41 | #include <hardware_legacy/AudioHardwareInterface.h> |
| 42 | |
| 43 | #include "AudioMixer.h" |
| 44 | #include "AudioFlinger.h" |
| 45 | |
| 46 | #ifdef WITH_A2DP |
| 47 | #include "A2dpAudioInterface.h" |
| 48 | #endif |
| 49 | |
| 50 | #ifdef LVMX |
| 51 | #include "lifevibes.h" |
| 52 | #endif |
| 53 | |
| 54 | #include <media/EffectsFactoryApi.h> |
| 55 | #include <media/EffectVisualizerApi.h> |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // the sim build doesn't have gettid |
| 59 | |
| 60 | #ifndef HAVE_GETTID |
| 61 | # define gettid getpid |
| 62 | #endif |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 66 | extern const char * const gEffectLibPath; |
| 67 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 68 | namespace android { |
| 69 | |
| 70 | static const char* kDeadlockedString = "AudioFlinger may be deadlocked\n"; |
| 71 | static const char* kHardwareLockedString = "Hardware lock is taken\n"; |
| 72 | |
| 73 | //static const nsecs_t kStandbyTimeInNsecs = seconds(3); |
| 74 | static const float MAX_GAIN = 4096.0f; |
| 75 | static const float MAX_GAIN_INT = 0x1000; |
| 76 | |
| 77 | // retry counts for buffer fill timeout |
| 78 | // 50 * ~20msecs = 1 second |
| 79 | static const int8_t kMaxTrackRetries = 50; |
| 80 | static const int8_t kMaxTrackStartupRetries = 50; |
| 81 | // allow less retry attempts on direct output thread. |
| 82 | // direct outputs can be a scarce resource in audio hardware and should |
| 83 | // be released as quickly as possible. |
| 84 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 85 | |
| 86 | static const int kDumpLockRetries = 50; |
| 87 | static const int kDumpLockSleep = 20000; |
| 88 | |
| 89 | static const nsecs_t kWarningThrottle = seconds(5); |
| 90 | |
| 91 | |
| 92 | #define AUDIOFLINGER_SECURITY_ENABLED 1 |
| 93 | |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | static bool recordingAllowed() { |
| 97 | #ifndef HAVE_ANDROID_OS |
| 98 | return true; |
| 99 | #endif |
| 100 | #if AUDIOFLINGER_SECURITY_ENABLED |
| 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; |
| 105 | #else |
| 106 | if (!checkCallingPermission(String16("android.permission.RECORD_AUDIO"))) |
| 107 | LOGW("WARNING: Need to add android.permission.RECORD_AUDIO to manifest"); |
| 108 | return true; |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | static bool settingsAllowed() { |
| 113 | #ifndef HAVE_ANDROID_OS |
| 114 | return true; |
| 115 | #endif |
| 116 | #if AUDIOFLINGER_SECURITY_ENABLED |
| 117 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 118 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); |
| 119 | if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 120 | return ok; |
| 121 | #else |
| 122 | if (!checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"))) |
| 123 | LOGW("WARNING: Need to add android.permission.MODIFY_AUDIO_SETTINGS to manifest"); |
| 124 | return true; |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | |
| 130 | AudioFlinger::AudioFlinger() |
| 131 | : BnAudioFlinger(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 132 | mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextUniqueId(1) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 133 | { |
| 134 | mHardwareStatus = AUDIO_HW_IDLE; |
| 135 | |
| 136 | mAudioHardware = AudioHardwareInterface::create(); |
| 137 | |
| 138 | mHardwareStatus = AUDIO_HW_INIT; |
| 139 | if (mAudioHardware->initCheck() == NO_ERROR) { |
| 140 | // open 16-bit output stream for s/w mixer |
| 141 | mMode = AudioSystem::MODE_NORMAL; |
| 142 | setMode(mMode); |
| 143 | |
| 144 | setMasterVolume(1.0f); |
| 145 | setMasterMute(false); |
| 146 | } else { |
| 147 | LOGE("Couldn't even initialize the stubbed audio hardware!"); |
| 148 | } |
| 149 | #ifdef LVMX |
| 150 | LifeVibes::init(); |
| 151 | mLifeVibesClientPid = -1; |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | AudioFlinger::~AudioFlinger() |
| 156 | { |
| 157 | while (!mRecordThreads.isEmpty()) { |
| 158 | // closeInput() will remove first entry from mRecordThreads |
| 159 | closeInput(mRecordThreads.keyAt(0)); |
| 160 | } |
| 161 | while (!mPlaybackThreads.isEmpty()) { |
| 162 | // closeOutput() will remove first entry from mPlaybackThreads |
| 163 | closeOutput(mPlaybackThreads.keyAt(0)); |
| 164 | } |
| 165 | if (mAudioHardware) { |
| 166 | delete mAudioHardware; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args) |
| 173 | { |
| 174 | const size_t SIZE = 256; |
| 175 | char buffer[SIZE]; |
| 176 | String8 result; |
| 177 | |
| 178 | result.append("Clients:\n"); |
| 179 | for (size_t i = 0; i < mClients.size(); ++i) { |
| 180 | wp<Client> wClient = mClients.valueAt(i); |
| 181 | if (wClient != 0) { |
| 182 | sp<Client> client = wClient.promote(); |
| 183 | if (client != 0) { |
| 184 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 185 | result.append(buffer); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | write(fd, result.string(), result.size()); |
| 190 | return NO_ERROR; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) |
| 195 | { |
| 196 | const size_t SIZE = 256; |
| 197 | char buffer[SIZE]; |
| 198 | String8 result; |
| 199 | int hardwareStatus = mHardwareStatus; |
| 200 | |
| 201 | snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus); |
| 202 | result.append(buffer); |
| 203 | write(fd, result.string(), result.size()); |
| 204 | return NO_ERROR; |
| 205 | } |
| 206 | |
| 207 | status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) |
| 208 | { |
| 209 | const size_t SIZE = 256; |
| 210 | char buffer[SIZE]; |
| 211 | String8 result; |
| 212 | snprintf(buffer, SIZE, "Permission Denial: " |
| 213 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 214 | IPCThreadState::self()->getCallingPid(), |
| 215 | IPCThreadState::self()->getCallingUid()); |
| 216 | result.append(buffer); |
| 217 | write(fd, result.string(), result.size()); |
| 218 | return NO_ERROR; |
| 219 | } |
| 220 | |
| 221 | static bool tryLock(Mutex& mutex) |
| 222 | { |
| 223 | bool locked = false; |
| 224 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 225 | if (mutex.tryLock() == NO_ERROR) { |
| 226 | locked = true; |
| 227 | break; |
| 228 | } |
| 229 | usleep(kDumpLockSleep); |
| 230 | } |
| 231 | return locked; |
| 232 | } |
| 233 | |
| 234 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 235 | { |
| 236 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 237 | dumpPermissionDenial(fd, args); |
| 238 | } else { |
| 239 | // get state of hardware lock |
| 240 | bool hardwareLocked = tryLock(mHardwareLock); |
| 241 | if (!hardwareLocked) { |
| 242 | String8 result(kHardwareLockedString); |
| 243 | write(fd, result.string(), result.size()); |
| 244 | } else { |
| 245 | mHardwareLock.unlock(); |
| 246 | } |
| 247 | |
| 248 | bool locked = tryLock(mLock); |
| 249 | |
| 250 | // failed to lock - AudioFlinger is probably deadlocked |
| 251 | if (!locked) { |
| 252 | String8 result(kDeadlockedString); |
| 253 | write(fd, result.string(), result.size()); |
| 254 | } |
| 255 | |
| 256 | dumpClients(fd, args); |
| 257 | dumpInternals(fd, args); |
| 258 | |
| 259 | // dump playback threads |
| 260 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 261 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 262 | } |
| 263 | |
| 264 | // dump record threads |
| 265 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 266 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 267 | } |
| 268 | |
| 269 | if (mAudioHardware) { |
| 270 | mAudioHardware->dumpState(fd, args); |
| 271 | } |
| 272 | if (locked) mLock.unlock(); |
| 273 | } |
| 274 | return NO_ERROR; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // IAudioFlinger interface |
| 279 | |
| 280 | |
| 281 | sp<IAudioTrack> AudioFlinger::createTrack( |
| 282 | pid_t pid, |
| 283 | int streamType, |
| 284 | uint32_t sampleRate, |
| 285 | int format, |
| 286 | int channelCount, |
| 287 | int frameCount, |
| 288 | uint32_t flags, |
| 289 | const sp<IMemory>& sharedBuffer, |
| 290 | int output, |
| 291 | int *sessionId, |
| 292 | status_t *status) |
| 293 | { |
| 294 | sp<PlaybackThread::Track> track; |
| 295 | sp<TrackHandle> trackHandle; |
| 296 | sp<Client> client; |
| 297 | wp<Client> wclient; |
| 298 | status_t lStatus; |
| 299 | int lSessionId; |
| 300 | |
| 301 | if (streamType >= AudioSystem::NUM_STREAM_TYPES) { |
| 302 | LOGE("invalid stream type"); |
| 303 | lStatus = BAD_VALUE; |
| 304 | goto Exit; |
| 305 | } |
| 306 | |
| 307 | { |
| 308 | Mutex::Autolock _l(mLock); |
| 309 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 310 | PlaybackThread *effectThread = NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 311 | if (thread == NULL) { |
| 312 | LOGE("unknown output thread"); |
| 313 | lStatus = BAD_VALUE; |
| 314 | goto Exit; |
| 315 | } |
| 316 | |
| 317 | wclient = mClients.valueFor(pid); |
| 318 | |
| 319 | if (wclient != NULL) { |
| 320 | client = wclient.promote(); |
| 321 | } else { |
| 322 | client = new Client(this, pid); |
| 323 | mClients.add(pid, client); |
| 324 | } |
| 325 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | LOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 327 | if (sessionId != NULL && *sessionId != AudioSystem::SESSION_OUTPUT_MIX) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 328 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 329 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 330 | if (mPlaybackThreads.keyAt(i) != output) { |
| 331 | // prevent same audio session on different output threads |
| 332 | uint32_t sessions = t->hasAudioSession(*sessionId); |
| 333 | if (sessions & PlaybackThread::TRACK_SESSION) { |
| 334 | lStatus = BAD_VALUE; |
| 335 | goto Exit; |
| 336 | } |
| 337 | // check if an effect with same session ID is waiting for a track to be created |
| 338 | if (sessions & PlaybackThread::EFFECT_SESSION) { |
| 339 | effectThread = t.get(); |
| 340 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 343 | lSessionId = *sessionId; |
| 344 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 345 | // if no audio session id is provided, create one here |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 346 | lSessionId = nextUniqueId(); |
| 347 | if (sessionId != NULL) { |
| 348 | *sessionId = lSessionId; |
| 349 | } |
| 350 | } |
| 351 | LOGV("createTrack() lSessionId: %d", lSessionId); |
| 352 | |
| 353 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
| 354 | channelCount, frameCount, sharedBuffer, lSessionId, &lStatus); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 355 | |
| 356 | // move effect chain to this output thread if an effect on same session was waiting |
| 357 | // for a track to be created |
| 358 | if (lStatus == NO_ERROR && effectThread != NULL) { |
| 359 | Mutex::Autolock _dl(thread->mLock); |
| 360 | Mutex::Autolock _sl(effectThread->mLock); |
| 361 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 362 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 363 | } |
| 364 | if (lStatus == NO_ERROR) { |
| 365 | trackHandle = new TrackHandle(track); |
| 366 | } else { |
| 367 | // remove local strong reference to Client before deleting the Track so that the Client |
| 368 | // destructor is called by the TrackBase destructor with mLock held |
| 369 | client.clear(); |
| 370 | track.clear(); |
| 371 | } |
| 372 | |
| 373 | Exit: |
| 374 | if(status) { |
| 375 | *status = lStatus; |
| 376 | } |
| 377 | return trackHandle; |
| 378 | } |
| 379 | |
| 380 | uint32_t AudioFlinger::sampleRate(int output) const |
| 381 | { |
| 382 | Mutex::Autolock _l(mLock); |
| 383 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 384 | if (thread == NULL) { |
| 385 | LOGW("sampleRate() unknown thread %d", output); |
| 386 | return 0; |
| 387 | } |
| 388 | return thread->sampleRate(); |
| 389 | } |
| 390 | |
| 391 | int AudioFlinger::channelCount(int output) const |
| 392 | { |
| 393 | Mutex::Autolock _l(mLock); |
| 394 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 395 | if (thread == NULL) { |
| 396 | LOGW("channelCount() unknown thread %d", output); |
| 397 | return 0; |
| 398 | } |
| 399 | return thread->channelCount(); |
| 400 | } |
| 401 | |
| 402 | int AudioFlinger::format(int output) const |
| 403 | { |
| 404 | Mutex::Autolock _l(mLock); |
| 405 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 406 | if (thread == NULL) { |
| 407 | LOGW("format() unknown thread %d", output); |
| 408 | return 0; |
| 409 | } |
| 410 | return thread->format(); |
| 411 | } |
| 412 | |
| 413 | size_t AudioFlinger::frameCount(int output) const |
| 414 | { |
| 415 | Mutex::Autolock _l(mLock); |
| 416 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 417 | if (thread == NULL) { |
| 418 | LOGW("frameCount() unknown thread %d", output); |
| 419 | return 0; |
| 420 | } |
| 421 | return thread->frameCount(); |
| 422 | } |
| 423 | |
| 424 | uint32_t AudioFlinger::latency(int output) const |
| 425 | { |
| 426 | Mutex::Autolock _l(mLock); |
| 427 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 428 | if (thread == NULL) { |
| 429 | LOGW("latency() unknown thread %d", output); |
| 430 | return 0; |
| 431 | } |
| 432 | return thread->latency(); |
| 433 | } |
| 434 | |
| 435 | status_t AudioFlinger::setMasterVolume(float value) |
| 436 | { |
| 437 | // check calling permissions |
| 438 | if (!settingsAllowed()) { |
| 439 | return PERMISSION_DENIED; |
| 440 | } |
| 441 | |
| 442 | // when hw supports master volume, don't scale in sw mixer |
| 443 | AutoMutex lock(mHardwareLock); |
| 444 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 445 | if (mAudioHardware->setMasterVolume(value) == NO_ERROR) { |
| 446 | value = 1.0f; |
| 447 | } |
| 448 | mHardwareStatus = AUDIO_HW_IDLE; |
| 449 | |
| 450 | mMasterVolume = value; |
| 451 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 452 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
| 453 | |
| 454 | return NO_ERROR; |
| 455 | } |
| 456 | |
| 457 | status_t AudioFlinger::setMode(int mode) |
| 458 | { |
| 459 | status_t ret; |
| 460 | |
| 461 | // check calling permissions |
| 462 | if (!settingsAllowed()) { |
| 463 | return PERMISSION_DENIED; |
| 464 | } |
| 465 | if ((mode < 0) || (mode >= AudioSystem::NUM_MODES)) { |
| 466 | LOGW("Illegal value: setMode(%d)", mode); |
| 467 | return BAD_VALUE; |
| 468 | } |
| 469 | |
| 470 | { // scope for the lock |
| 471 | AutoMutex lock(mHardwareLock); |
| 472 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 473 | ret = mAudioHardware->setMode(mode); |
| 474 | mHardwareStatus = AUDIO_HW_IDLE; |
| 475 | } |
| 476 | |
| 477 | if (NO_ERROR == ret) { |
| 478 | Mutex::Autolock _l(mLock); |
| 479 | mMode = mode; |
| 480 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 481 | mPlaybackThreads.valueAt(i)->setMode(mode); |
| 482 | #ifdef LVMX |
| 483 | LifeVibes::setMode(mode); |
| 484 | #endif |
| 485 | } |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | status_t AudioFlinger::setMicMute(bool state) |
| 491 | { |
| 492 | // check calling permissions |
| 493 | if (!settingsAllowed()) { |
| 494 | return PERMISSION_DENIED; |
| 495 | } |
| 496 | |
| 497 | AutoMutex lock(mHardwareLock); |
| 498 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
| 499 | status_t ret = mAudioHardware->setMicMute(state); |
| 500 | mHardwareStatus = AUDIO_HW_IDLE; |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | bool AudioFlinger::getMicMute() const |
| 505 | { |
| 506 | bool state = AudioSystem::MODE_INVALID; |
| 507 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
| 508 | mAudioHardware->getMicMute(&state); |
| 509 | mHardwareStatus = AUDIO_HW_IDLE; |
| 510 | return state; |
| 511 | } |
| 512 | |
| 513 | status_t AudioFlinger::setMasterMute(bool muted) |
| 514 | { |
| 515 | // check calling permissions |
| 516 | if (!settingsAllowed()) { |
| 517 | return PERMISSION_DENIED; |
| 518 | } |
| 519 | |
| 520 | mMasterMute = muted; |
| 521 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 522 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
| 523 | |
| 524 | return NO_ERROR; |
| 525 | } |
| 526 | |
| 527 | float AudioFlinger::masterVolume() const |
| 528 | { |
| 529 | return mMasterVolume; |
| 530 | } |
| 531 | |
| 532 | bool AudioFlinger::masterMute() const |
| 533 | { |
| 534 | return mMasterMute; |
| 535 | } |
| 536 | |
| 537 | status_t AudioFlinger::setStreamVolume(int stream, float value, int output) |
| 538 | { |
| 539 | // check calling permissions |
| 540 | if (!settingsAllowed()) { |
| 541 | return PERMISSION_DENIED; |
| 542 | } |
| 543 | |
| 544 | if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) { |
| 545 | return BAD_VALUE; |
| 546 | } |
| 547 | |
| 548 | AutoMutex lock(mLock); |
| 549 | PlaybackThread *thread = NULL; |
| 550 | if (output) { |
| 551 | thread = checkPlaybackThread_l(output); |
| 552 | if (thread == NULL) { |
| 553 | return BAD_VALUE; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | mStreamTypes[stream].volume = value; |
| 558 | |
| 559 | if (thread == NULL) { |
| 560 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 561 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
| 562 | } |
| 563 | } else { |
| 564 | thread->setStreamVolume(stream, value); |
| 565 | } |
| 566 | |
| 567 | return NO_ERROR; |
| 568 | } |
| 569 | |
| 570 | status_t AudioFlinger::setStreamMute(int stream, bool muted) |
| 571 | { |
| 572 | // check calling permissions |
| 573 | if (!settingsAllowed()) { |
| 574 | return PERMISSION_DENIED; |
| 575 | } |
| 576 | |
| 577 | if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES || |
| 578 | uint32_t(stream) == AudioSystem::ENFORCED_AUDIBLE) { |
| 579 | return BAD_VALUE; |
| 580 | } |
| 581 | |
| 582 | mStreamTypes[stream].mute = muted; |
| 583 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) |
| 584 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
| 585 | |
| 586 | return NO_ERROR; |
| 587 | } |
| 588 | |
| 589 | float AudioFlinger::streamVolume(int stream, int output) const |
| 590 | { |
| 591 | if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) { |
| 592 | return 0.0f; |
| 593 | } |
| 594 | |
| 595 | AutoMutex lock(mLock); |
| 596 | float volume; |
| 597 | if (output) { |
| 598 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 599 | if (thread == NULL) { |
| 600 | return 0.0f; |
| 601 | } |
| 602 | volume = thread->streamVolume(stream); |
| 603 | } else { |
| 604 | volume = mStreamTypes[stream].volume; |
| 605 | } |
| 606 | |
| 607 | return volume; |
| 608 | } |
| 609 | |
| 610 | bool AudioFlinger::streamMute(int stream) const |
| 611 | { |
| 612 | if (stream < 0 || stream >= (int)AudioSystem::NUM_STREAM_TYPES) { |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | return mStreamTypes[stream].mute; |
| 617 | } |
| 618 | |
| 619 | bool AudioFlinger::isStreamActive(int stream) const |
| 620 | { |
| 621 | Mutex::Autolock _l(mLock); |
| 622 | for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 623 | if (mPlaybackThreads.valueAt(i)->isStreamActive(stream)) { |
| 624 | return true; |
| 625 | } |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs) |
| 631 | { |
| 632 | status_t result; |
| 633 | |
| 634 | LOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d", |
| 635 | ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 636 | // check calling permissions |
| 637 | if (!settingsAllowed()) { |
| 638 | return PERMISSION_DENIED; |
| 639 | } |
| 640 | |
| 641 | #ifdef LVMX |
| 642 | AudioParameter param = AudioParameter(keyValuePairs); |
| 643 | LifeVibes::setParameters(ioHandle,keyValuePairs); |
| 644 | String8 key = String8(AudioParameter::keyRouting); |
| 645 | int device; |
| 646 | if (NO_ERROR != param.getInt(key, device)) { |
| 647 | device = -1; |
| 648 | } |
| 649 | |
| 650 | key = String8(LifevibesTag); |
| 651 | String8 value; |
| 652 | int musicEnabled = -1; |
| 653 | if (NO_ERROR == param.get(key, value)) { |
| 654 | if (value == LifevibesEnable) { |
| 655 | mLifeVibesClientPid = IPCThreadState::self()->getCallingPid(); |
| 656 | musicEnabled = 1; |
| 657 | } else if (value == LifevibesDisable) { |
| 658 | mLifeVibesClientPid = -1; |
| 659 | musicEnabled = 0; |
| 660 | } |
| 661 | } |
| 662 | #endif |
| 663 | |
| 664 | // ioHandle == 0 means the parameters are global to the audio hardware interface |
| 665 | if (ioHandle == 0) { |
| 666 | AutoMutex lock(mHardwareLock); |
| 667 | mHardwareStatus = AUDIO_SET_PARAMETER; |
| 668 | result = mAudioHardware->setParameters(keyValuePairs); |
| 669 | #ifdef LVMX |
| 670 | if (musicEnabled != -1) { |
| 671 | LifeVibes::enableMusic((bool) musicEnabled); |
| 672 | } |
| 673 | #endif |
| 674 | mHardwareStatus = AUDIO_HW_IDLE; |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 679 | // and the thread is exited once the lock is released |
| 680 | sp<ThreadBase> thread; |
| 681 | { |
| 682 | Mutex::Autolock _l(mLock); |
| 683 | thread = checkPlaybackThread_l(ioHandle); |
| 684 | if (thread == NULL) { |
| 685 | thread = checkRecordThread_l(ioHandle); |
| 686 | } |
| 687 | } |
| 688 | if (thread != NULL) { |
| 689 | result = thread->setParameters(keyValuePairs); |
| 690 | #ifdef LVMX |
| 691 | if ((NO_ERROR == result) && (device != -1)) { |
| 692 | LifeVibes::setDevice(LifeVibes::threadIdToAudioOutputType(thread->id()), device); |
| 693 | } |
| 694 | #endif |
| 695 | return result; |
| 696 | } |
| 697 | return BAD_VALUE; |
| 698 | } |
| 699 | |
| 700 | String8 AudioFlinger::getParameters(int ioHandle, const String8& keys) |
| 701 | { |
| 702 | // LOGV("getParameters() io %d, keys %s, tid %d, calling tid %d", |
| 703 | // ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid()); |
| 704 | |
| 705 | if (ioHandle == 0) { |
| 706 | return mAudioHardware->getParameters(keys); |
| 707 | } |
| 708 | |
| 709 | Mutex::Autolock _l(mLock); |
| 710 | |
| 711 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 712 | if (playbackThread != NULL) { |
| 713 | return playbackThread->getParameters(keys); |
| 714 | } |
| 715 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 716 | if (recordThread != NULL) { |
| 717 | return recordThread->getParameters(keys); |
| 718 | } |
| 719 | return String8(""); |
| 720 | } |
| 721 | |
| 722 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) |
| 723 | { |
| 724 | return mAudioHardware->getInputBufferSize(sampleRate, format, channelCount); |
| 725 | } |
| 726 | |
| 727 | unsigned int AudioFlinger::getInputFramesLost(int ioHandle) |
| 728 | { |
| 729 | if (ioHandle == 0) { |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | Mutex::Autolock _l(mLock); |
| 734 | |
| 735 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 736 | if (recordThread != NULL) { |
| 737 | return recordThread->getInputFramesLost(); |
| 738 | } |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | status_t AudioFlinger::setVoiceVolume(float value) |
| 743 | { |
| 744 | // check calling permissions |
| 745 | if (!settingsAllowed()) { |
| 746 | return PERMISSION_DENIED; |
| 747 | } |
| 748 | |
| 749 | AutoMutex lock(mHardwareLock); |
| 750 | mHardwareStatus = AUDIO_SET_VOICE_VOLUME; |
| 751 | status_t ret = mAudioHardware->setVoiceVolume(value); |
| 752 | mHardwareStatus = AUDIO_HW_IDLE; |
| 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
| 757 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int output) |
| 758 | { |
| 759 | status_t status; |
| 760 | |
| 761 | Mutex::Autolock _l(mLock); |
| 762 | |
| 763 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 764 | if (playbackThread != NULL) { |
| 765 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 766 | } |
| 767 | |
| 768 | return BAD_VALUE; |
| 769 | } |
| 770 | |
| 771 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 772 | { |
| 773 | |
| 774 | Mutex::Autolock _l(mLock); |
| 775 | |
| 776 | int pid = IPCThreadState::self()->getCallingPid(); |
| 777 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 778 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 779 | client, |
| 780 | pid); |
| 781 | LOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
| 782 | |
| 783 | mNotificationClients.add(pid, notificationClient); |
| 784 | |
| 785 | sp<IBinder> binder = client->asBinder(); |
| 786 | binder->linkToDeath(notificationClient); |
| 787 | |
| 788 | // the config change is always sent from playback or record threads to avoid deadlock |
| 789 | // with AudioSystem::gLock |
| 790 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 791 | mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED); |
| 792 | } |
| 793 | |
| 794 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 795 | mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 801 | { |
| 802 | Mutex::Autolock _l(mLock); |
| 803 | |
| 804 | int index = mNotificationClients.indexOfKey(pid); |
| 805 | if (index >= 0) { |
| 806 | sp <NotificationClient> client = mNotificationClients.valueFor(pid); |
| 807 | LOGV("removeNotificationClient() %p, pid %d", client.get(), pid); |
| 808 | #ifdef LVMX |
| 809 | if (pid == mLifeVibesClientPid) { |
| 810 | LOGV("Disabling lifevibes"); |
| 811 | LifeVibes::enableMusic(false); |
| 812 | mLifeVibesClientPid = -1; |
| 813 | } |
| 814 | #endif |
| 815 | mNotificationClients.removeItem(pid); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held |
| 820 | void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) |
| 821 | { |
| 822 | size_t size = mNotificationClients.size(); |
| 823 | for (size_t i = 0; i < size; i++) { |
| 824 | mNotificationClients.valueAt(i)->client()->ioConfigChanged(event, ioHandle, param2); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // removeClient_l() must be called with AudioFlinger::mLock held |
| 829 | void AudioFlinger::removeClient_l(pid_t pid) |
| 830 | { |
| 831 | LOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid()); |
| 832 | mClients.removeItem(pid); |
| 833 | } |
| 834 | |
| 835 | |
| 836 | // ---------------------------------------------------------------------------- |
| 837 | |
| 838 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id) |
| 839 | : Thread(false), |
| 840 | mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0), |
| 841 | mFrameSize(1), mFormat(0), mStandby(false), mId(id), mExiting(false) |
| 842 | { |
| 843 | } |
| 844 | |
| 845 | AudioFlinger::ThreadBase::~ThreadBase() |
| 846 | { |
| 847 | mParamCond.broadcast(); |
| 848 | mNewParameters.clear(); |
| 849 | } |
| 850 | |
| 851 | void AudioFlinger::ThreadBase::exit() |
| 852 | { |
| 853 | // keep a strong ref on ourself so that we wont get |
| 854 | // destroyed in the middle of requestExitAndWait() |
| 855 | sp <ThreadBase> strongMe = this; |
| 856 | |
| 857 | LOGV("ThreadBase::exit"); |
| 858 | { |
| 859 | AutoMutex lock(&mLock); |
| 860 | mExiting = true; |
| 861 | requestExit(); |
| 862 | mWaitWorkCV.signal(); |
| 863 | } |
| 864 | requestExitAndWait(); |
| 865 | } |
| 866 | |
| 867 | uint32_t AudioFlinger::ThreadBase::sampleRate() const |
| 868 | { |
| 869 | return mSampleRate; |
| 870 | } |
| 871 | |
| 872 | int AudioFlinger::ThreadBase::channelCount() const |
| 873 | { |
| 874 | return (int)mChannelCount; |
| 875 | } |
| 876 | |
| 877 | int AudioFlinger::ThreadBase::format() const |
| 878 | { |
| 879 | return mFormat; |
| 880 | } |
| 881 | |
| 882 | size_t AudioFlinger::ThreadBase::frameCount() const |
| 883 | { |
| 884 | return mFrameCount; |
| 885 | } |
| 886 | |
| 887 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 888 | { |
| 889 | status_t status; |
| 890 | |
| 891 | LOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 892 | Mutex::Autolock _l(mLock); |
| 893 | |
| 894 | mNewParameters.add(keyValuePairs); |
| 895 | mWaitWorkCV.signal(); |
| 896 | // wait condition with timeout in case the thread loop has exited |
| 897 | // before the request could be processed |
| 898 | if (mParamCond.waitRelative(mLock, seconds(2)) == NO_ERROR) { |
| 899 | status = mParamStatus; |
| 900 | mWaitWorkCV.signal(); |
| 901 | } else { |
| 902 | status = TIMED_OUT; |
| 903 | } |
| 904 | return status; |
| 905 | } |
| 906 | |
| 907 | void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param) |
| 908 | { |
| 909 | Mutex::Autolock _l(mLock); |
| 910 | sendConfigEvent_l(event, param); |
| 911 | } |
| 912 | |
| 913 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 914 | void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param) |
| 915 | { |
| 916 | ConfigEvent *configEvent = new ConfigEvent(); |
| 917 | configEvent->mEvent = event; |
| 918 | configEvent->mParam = param; |
| 919 | mConfigEvents.add(configEvent); |
| 920 | LOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param); |
| 921 | mWaitWorkCV.signal(); |
| 922 | } |
| 923 | |
| 924 | void AudioFlinger::ThreadBase::processConfigEvents() |
| 925 | { |
| 926 | mLock.lock(); |
| 927 | while(!mConfigEvents.isEmpty()) { |
| 928 | LOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); |
| 929 | ConfigEvent *configEvent = mConfigEvents[0]; |
| 930 | mConfigEvents.removeAt(0); |
| 931 | // release mLock before locking AudioFlinger mLock: lock order is always |
| 932 | // AudioFlinger then ThreadBase to avoid cross deadlock |
| 933 | mLock.unlock(); |
| 934 | mAudioFlinger->mLock.lock(); |
| 935 | audioConfigChanged_l(configEvent->mEvent, configEvent->mParam); |
| 936 | mAudioFlinger->mLock.unlock(); |
| 937 | delete configEvent; |
| 938 | mLock.lock(); |
| 939 | } |
| 940 | mLock.unlock(); |
| 941 | } |
| 942 | |
| 943 | status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) |
| 944 | { |
| 945 | const size_t SIZE = 256; |
| 946 | char buffer[SIZE]; |
| 947 | String8 result; |
| 948 | |
| 949 | bool locked = tryLock(mLock); |
| 950 | if (!locked) { |
| 951 | snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); |
| 952 | write(fd, buffer, strlen(buffer)); |
| 953 | } |
| 954 | |
| 955 | snprintf(buffer, SIZE, "standby: %d\n", mStandby); |
| 956 | result.append(buffer); |
| 957 | snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate); |
| 958 | result.append(buffer); |
| 959 | snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount); |
| 960 | result.append(buffer); |
| 961 | snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); |
| 962 | result.append(buffer); |
| 963 | snprintf(buffer, SIZE, "Format: %d\n", mFormat); |
| 964 | result.append(buffer); |
| 965 | snprintf(buffer, SIZE, "Frame size: %d\n", mFrameSize); |
| 966 | result.append(buffer); |
| 967 | |
| 968 | snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); |
| 969 | result.append(buffer); |
| 970 | result.append(" Index Command"); |
| 971 | for (size_t i = 0; i < mNewParameters.size(); ++i) { |
| 972 | snprintf(buffer, SIZE, "\n %02d ", i); |
| 973 | result.append(buffer); |
| 974 | result.append(mNewParameters[i]); |
| 975 | } |
| 976 | |
| 977 | snprintf(buffer, SIZE, "\n\nPending config events: \n"); |
| 978 | result.append(buffer); |
| 979 | snprintf(buffer, SIZE, " Index event param\n"); |
| 980 | result.append(buffer); |
| 981 | for (size_t i = 0; i < mConfigEvents.size(); i++) { |
| 982 | snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i]->mEvent, mConfigEvents[i]->mParam); |
| 983 | result.append(buffer); |
| 984 | } |
| 985 | result.append("\n"); |
| 986 | |
| 987 | write(fd, result.string(), result.size()); |
| 988 | |
| 989 | if (locked) { |
| 990 | mLock.unlock(); |
| 991 | } |
| 992 | return NO_ERROR; |
| 993 | } |
| 994 | |
| 995 | |
| 996 | // ---------------------------------------------------------------------------- |
| 997 | |
| 998 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
| 999 | : ThreadBase(audioFlinger, id), |
| 1000 | mMixBuffer(0), mSuspended(0), mBytesWritten(0), mOutput(output), |
| 1001 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1002 | mDevice(device) |
| 1003 | { |
| 1004 | readOutputParameters(); |
| 1005 | |
| 1006 | mMasterVolume = mAudioFlinger->masterVolume(); |
| 1007 | mMasterMute = mAudioFlinger->masterMute(); |
| 1008 | |
| 1009 | for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) { |
| 1010 | mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream); |
| 1011 | mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1016 | { |
| 1017 | delete [] mMixBuffer; |
| 1018 | } |
| 1019 | |
| 1020 | status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1021 | { |
| 1022 | dumpInternals(fd, args); |
| 1023 | dumpTracks(fd, args); |
| 1024 | dumpEffectChains(fd, args); |
| 1025 | return NO_ERROR; |
| 1026 | } |
| 1027 | |
| 1028 | status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) |
| 1029 | { |
| 1030 | const size_t SIZE = 256; |
| 1031 | char buffer[SIZE]; |
| 1032 | String8 result; |
| 1033 | |
| 1034 | snprintf(buffer, SIZE, "Output thread %p tracks\n", this); |
| 1035 | result.append(buffer); |
| 1036 | result.append(" Name Clien Typ Fmt Chn Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
| 1037 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1038 | sp<Track> track = mTracks[i]; |
| 1039 | if (track != 0) { |
| 1040 | track->dump(buffer, SIZE); |
| 1041 | result.append(buffer); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); |
| 1046 | result.append(buffer); |
| 1047 | result.append(" Name Clien Typ Fmt Chn Session Buf S M F SRate LeftV RighV Serv User Main buf Aux Buf\n"); |
| 1048 | for (size_t i = 0; i < mActiveTracks.size(); ++i) { |
| 1049 | wp<Track> wTrack = mActiveTracks[i]; |
| 1050 | if (wTrack != 0) { |
| 1051 | sp<Track> track = wTrack.promote(); |
| 1052 | if (track != 0) { |
| 1053 | track->dump(buffer, SIZE); |
| 1054 | result.append(buffer); |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | write(fd, result.string(), result.size()); |
| 1059 | return NO_ERROR; |
| 1060 | } |
| 1061 | |
| 1062 | status_t AudioFlinger::PlaybackThread::dumpEffectChains(int fd, const Vector<String16>& args) |
| 1063 | { |
| 1064 | const size_t SIZE = 256; |
| 1065 | char buffer[SIZE]; |
| 1066 | String8 result; |
| 1067 | |
| 1068 | snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); |
| 1069 | write(fd, buffer, strlen(buffer)); |
| 1070 | |
| 1071 | for (size_t i = 0; i < mEffectChains.size(); ++i) { |
| 1072 | sp<EffectChain> chain = mEffectChains[i]; |
| 1073 | if (chain != 0) { |
| 1074 | chain->dump(fd, args); |
| 1075 | } |
| 1076 | } |
| 1077 | return NO_ERROR; |
| 1078 | } |
| 1079 | |
| 1080 | status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1081 | { |
| 1082 | const size_t SIZE = 256; |
| 1083 | char buffer[SIZE]; |
| 1084 | String8 result; |
| 1085 | |
| 1086 | snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); |
| 1087 | result.append(buffer); |
| 1088 | snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1089 | result.append(buffer); |
| 1090 | snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); |
| 1091 | result.append(buffer); |
| 1092 | snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); |
| 1093 | result.append(buffer); |
| 1094 | snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); |
| 1095 | result.append(buffer); |
| 1096 | snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); |
| 1097 | result.append(buffer); |
| 1098 | snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); |
| 1099 | result.append(buffer); |
| 1100 | write(fd, result.string(), result.size()); |
| 1101 | |
| 1102 | dumpBase(fd, args); |
| 1103 | |
| 1104 | return NO_ERROR; |
| 1105 | } |
| 1106 | |
| 1107 | // Thread virtuals |
| 1108 | status_t AudioFlinger::PlaybackThread::readyToRun() |
| 1109 | { |
| 1110 | if (mSampleRate == 0) { |
| 1111 | LOGE("No working audio driver found."); |
| 1112 | return NO_INIT; |
| 1113 | } |
| 1114 | LOGI("AudioFlinger's thread %p ready to run", this); |
| 1115 | return NO_ERROR; |
| 1116 | } |
| 1117 | |
| 1118 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1119 | { |
| 1120 | const size_t SIZE = 256; |
| 1121 | char buffer[SIZE]; |
| 1122 | |
| 1123 | snprintf(buffer, SIZE, "Playback Thread %p", this); |
| 1124 | |
| 1125 | run(buffer, ANDROID_PRIORITY_URGENT_AUDIO); |
| 1126 | } |
| 1127 | |
| 1128 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1129 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1130 | const sp<AudioFlinger::Client>& client, |
| 1131 | int streamType, |
| 1132 | uint32_t sampleRate, |
| 1133 | int format, |
| 1134 | int channelCount, |
| 1135 | int frameCount, |
| 1136 | const sp<IMemory>& sharedBuffer, |
| 1137 | int sessionId, |
| 1138 | status_t *status) |
| 1139 | { |
| 1140 | sp<Track> track; |
| 1141 | status_t lStatus; |
| 1142 | |
| 1143 | if (mType == DIRECT) { |
| 1144 | if (sampleRate != mSampleRate || format != mFormat || channelCount != (int)mChannelCount) { |
| 1145 | LOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelCount %d for output %p", |
| 1146 | sampleRate, format, channelCount, mOutput); |
| 1147 | lStatus = BAD_VALUE; |
| 1148 | goto Exit; |
| 1149 | } |
| 1150 | } else { |
| 1151 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1152 | if (sampleRate > mSampleRate*2) { |
| 1153 | LOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate); |
| 1154 | lStatus = BAD_VALUE; |
| 1155 | goto Exit; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | if (mOutput == 0) { |
| 1160 | LOGE("Audio driver not initialized."); |
| 1161 | lStatus = NO_INIT; |
| 1162 | goto Exit; |
| 1163 | } |
| 1164 | |
| 1165 | { // scope for mLock |
| 1166 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1167 | |
| 1168 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1169 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1170 | // manager |
| 1171 | uint32_t strategy = |
| 1172 | AudioSystem::getStrategyForStream((AudioSystem::stream_type)streamType); |
| 1173 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1174 | sp<Track> t = mTracks[i]; |
| 1175 | if (t != 0) { |
| 1176 | if (sessionId == t->sessionId() && |
| 1177 | strategy != AudioSystem::getStrategyForStream((AudioSystem::stream_type)t->type())) { |
| 1178 | lStatus = BAD_VALUE; |
| 1179 | goto Exit; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1184 | track = new Track(this, client, streamType, sampleRate, format, |
| 1185 | channelCount, frameCount, sharedBuffer, sessionId); |
| 1186 | if (track->getCblk() == NULL || track->name() < 0) { |
| 1187 | lStatus = NO_MEMORY; |
| 1188 | goto Exit; |
| 1189 | } |
| 1190 | mTracks.add(track); |
| 1191 | |
| 1192 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1193 | if (chain != 0) { |
| 1194 | LOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1195 | track->setMainBuffer(chain->inBuffer()); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1196 | chain->setStrategy(AudioSystem::getStrategyForStream((AudioSystem::stream_type)track->type())); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | lStatus = NO_ERROR; |
| 1200 | |
| 1201 | Exit: |
| 1202 | if(status) { |
| 1203 | *status = lStatus; |
| 1204 | } |
| 1205 | return track; |
| 1206 | } |
| 1207 | |
| 1208 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1209 | { |
| 1210 | if (mOutput) { |
| 1211 | return mOutput->latency(); |
| 1212 | } |
| 1213 | else { |
| 1214 | return 0; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | status_t AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1219 | { |
| 1220 | #ifdef LVMX |
| 1221 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1222 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) { |
| 1223 | LifeVibes::setMasterVolume(audioOutputType, value); |
| 1224 | } |
| 1225 | #endif |
| 1226 | mMasterVolume = value; |
| 1227 | return NO_ERROR; |
| 1228 | } |
| 1229 | |
| 1230 | status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1231 | { |
| 1232 | #ifdef LVMX |
| 1233 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1234 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) { |
| 1235 | LifeVibes::setMasterMute(audioOutputType, muted); |
| 1236 | } |
| 1237 | #endif |
| 1238 | mMasterMute = muted; |
| 1239 | return NO_ERROR; |
| 1240 | } |
| 1241 | |
| 1242 | float AudioFlinger::PlaybackThread::masterVolume() const |
| 1243 | { |
| 1244 | return mMasterVolume; |
| 1245 | } |
| 1246 | |
| 1247 | bool AudioFlinger::PlaybackThread::masterMute() const |
| 1248 | { |
| 1249 | return mMasterMute; |
| 1250 | } |
| 1251 | |
| 1252 | status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value) |
| 1253 | { |
| 1254 | #ifdef LVMX |
| 1255 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1256 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) { |
| 1257 | LifeVibes::setStreamVolume(audioOutputType, stream, value); |
| 1258 | } |
| 1259 | #endif |
| 1260 | mStreamTypes[stream].volume = value; |
| 1261 | return NO_ERROR; |
| 1262 | } |
| 1263 | |
| 1264 | status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted) |
| 1265 | { |
| 1266 | #ifdef LVMX |
| 1267 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1268 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) { |
| 1269 | LifeVibes::setStreamMute(audioOutputType, stream, muted); |
| 1270 | } |
| 1271 | #endif |
| 1272 | mStreamTypes[stream].mute = muted; |
| 1273 | return NO_ERROR; |
| 1274 | } |
| 1275 | |
| 1276 | float AudioFlinger::PlaybackThread::streamVolume(int stream) const |
| 1277 | { |
| 1278 | return mStreamTypes[stream].volume; |
| 1279 | } |
| 1280 | |
| 1281 | bool AudioFlinger::PlaybackThread::streamMute(int stream) const |
| 1282 | { |
| 1283 | return mStreamTypes[stream].mute; |
| 1284 | } |
| 1285 | |
| 1286 | bool AudioFlinger::PlaybackThread::isStreamActive(int stream) const |
| 1287 | { |
| 1288 | Mutex::Autolock _l(mLock); |
| 1289 | size_t count = mActiveTracks.size(); |
| 1290 | for (size_t i = 0 ; i < count ; ++i) { |
| 1291 | sp<Track> t = mActiveTracks[i].promote(); |
| 1292 | if (t == 0) continue; |
| 1293 | Track* const track = t.get(); |
| 1294 | if (t->type() == stream) |
| 1295 | return true; |
| 1296 | } |
| 1297 | return false; |
| 1298 | } |
| 1299 | |
| 1300 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1301 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1302 | { |
| 1303 | status_t status = ALREADY_EXISTS; |
| 1304 | |
| 1305 | // set retry count for buffer fill |
| 1306 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1307 | if (mActiveTracks.indexOf(track) < 0) { |
| 1308 | // the track is newly added, make sure it fills up all its |
| 1309 | // buffers before playing. This is to ensure the client will |
| 1310 | // effectively get the latency it requested. |
| 1311 | track->mFillingUpStatus = Track::FS_FILLING; |
| 1312 | track->mResetDone = false; |
| 1313 | mActiveTracks.add(track); |
| 1314 | if (track->mainBuffer() != mMixBuffer) { |
| 1315 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1316 | if (chain != 0) { |
| 1317 | LOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId()); |
| 1318 | chain->startTrack(); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | status = NO_ERROR; |
| 1323 | } |
| 1324 | |
| 1325 | LOGV("mWaitWorkCV.broadcast"); |
| 1326 | mWaitWorkCV.broadcast(); |
| 1327 | |
| 1328 | return status; |
| 1329 | } |
| 1330 | |
| 1331 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 1332 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
| 1333 | { |
| 1334 | track->mState = TrackBase::TERMINATED; |
| 1335 | if (mActiveTracks.indexOf(track) < 0) { |
| 1336 | mTracks.remove(track); |
| 1337 | deleteTrackName_l(track->name()); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1342 | { |
| 1343 | return mOutput->getParameters(keys); |
| 1344 | } |
| 1345 | |
| 1346 | // destroyTrack_l() must be called with AudioFlinger::mLock held |
| 1347 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { |
| 1348 | AudioSystem::OutputDescriptor desc; |
| 1349 | void *param2 = 0; |
| 1350 | |
| 1351 | LOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param); |
| 1352 | |
| 1353 | switch (event) { |
| 1354 | case AudioSystem::OUTPUT_OPENED: |
| 1355 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
| 1356 | desc.channels = mChannels; |
| 1357 | desc.samplingRate = mSampleRate; |
| 1358 | desc.format = mFormat; |
| 1359 | desc.frameCount = mFrameCount; |
| 1360 | desc.latency = latency(); |
| 1361 | param2 = &desc; |
| 1362 | break; |
| 1363 | |
| 1364 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1365 | param2 = ¶m; |
| 1366 | case AudioSystem::OUTPUT_CLOSED: |
| 1367 | default: |
| 1368 | break; |
| 1369 | } |
| 1370 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 1371 | } |
| 1372 | |
| 1373 | void AudioFlinger::PlaybackThread::readOutputParameters() |
| 1374 | { |
| 1375 | mSampleRate = mOutput->sampleRate(); |
| 1376 | mChannels = mOutput->channels(); |
| 1377 | mChannelCount = (uint16_t)AudioSystem::popCount(mChannels); |
| 1378 | mFormat = mOutput->format(); |
| 1379 | mFrameSize = (uint16_t)mOutput->frameSize(); |
| 1380 | mFrameCount = mOutput->bufferSize() / mFrameSize; |
| 1381 | |
| 1382 | // FIXME - Current mixer implementation only supports stereo output: Always |
| 1383 | // Allocate a stereo buffer even if HW output is mono. |
| 1384 | if (mMixBuffer != NULL) delete[] mMixBuffer; |
| 1385 | mMixBuffer = new int16_t[mFrameCount * 2]; |
| 1386 | memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t)); |
| 1387 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1388 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1389 | // parameters into account |
| 1390 | // Note that mLock is not held when readOutputParameters() is called from the constructor |
| 1391 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1392 | // matter. |
| 1393 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1394 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1395 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1396 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1397 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
| 1401 | { |
| 1402 | if (halFrames == 0 || dspFrames == 0) { |
| 1403 | return BAD_VALUE; |
| 1404 | } |
| 1405 | if (mOutput == 0) { |
| 1406 | return INVALID_OPERATION; |
| 1407 | } |
| 1408 | *halFrames = mBytesWritten/mOutput->frameSize(); |
| 1409 | |
| 1410 | return mOutput->getRenderPosition(dspFrames); |
| 1411 | } |
| 1412 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1413 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1414 | { |
| 1415 | Mutex::Autolock _l(mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1416 | uint32_t result = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1417 | if (getEffectChain_l(sessionId) != 0) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1418 | result = EFFECT_SESSION; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1422 | sp<Track> track = mTracks[i]; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1423 | if (sessionId == track->sessionId() && |
| 1424 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1425 | result |= TRACK_SESSION; |
| 1426 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 1430 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1433 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1434 | { |
| 1435 | // session AudioSystem::SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 1436 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 1437 | if (sessionId == AudioSystem::SESSION_OUTPUT_MIX) { |
| 1438 | return AudioSystem::getStrategyForStream(AudioSystem::MUSIC); |
| 1439 | } |
| 1440 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1441 | sp<Track> track = mTracks[i]; |
| 1442 | if (sessionId == track->sessionId() && |
| 1443 | !(track->mCblk->flags & CBLK_INVALID_MSK)) { |
| 1444 | return AudioSystem::getStrategyForStream((AudioSystem::stream_type) track->type()); |
| 1445 | } |
| 1446 | } |
| 1447 | return AudioSystem::getStrategyForStream(AudioSystem::MUSIC); |
| 1448 | } |
| 1449 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1450 | sp<AudioFlinger::EffectChain> AudioFlinger::PlaybackThread::getEffectChain(int sessionId) |
| 1451 | { |
| 1452 | Mutex::Autolock _l(mLock); |
| 1453 | return getEffectChain_l(sessionId); |
| 1454 | } |
| 1455 | |
| 1456 | sp<AudioFlinger::EffectChain> AudioFlinger::PlaybackThread::getEffectChain_l(int sessionId) |
| 1457 | { |
| 1458 | sp<EffectChain> chain; |
| 1459 | |
| 1460 | size_t size = mEffectChains.size(); |
| 1461 | for (size_t i = 0; i < size; i++) { |
| 1462 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1463 | chain = mEffectChains[i]; |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | return chain; |
| 1468 | } |
| 1469 | |
| 1470 | void AudioFlinger::PlaybackThread::setMode(uint32_t mode) |
| 1471 | { |
| 1472 | Mutex::Autolock _l(mLock); |
| 1473 | size_t size = mEffectChains.size(); |
| 1474 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1475 | mEffectChains[i]->setMode_l(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | // ---------------------------------------------------------------------------- |
| 1480 | |
| 1481 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
| 1482 | : PlaybackThread(audioFlinger, output, id, device), |
| 1483 | mAudioMixer(0) |
| 1484 | { |
| 1485 | mType = PlaybackThread::MIXER; |
| 1486 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 1487 | |
| 1488 | // FIXME - Current mixer implementation only supports stereo output |
| 1489 | if (mChannelCount == 1) { |
| 1490 | LOGE("Invalid audio hardware channel count"); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | AudioFlinger::MixerThread::~MixerThread() |
| 1495 | { |
| 1496 | delete mAudioMixer; |
| 1497 | } |
| 1498 | |
| 1499 | bool AudioFlinger::MixerThread::threadLoop() |
| 1500 | { |
| 1501 | Vector< sp<Track> > tracksToRemove; |
| 1502 | uint32_t mixerStatus = MIXER_IDLE; |
| 1503 | nsecs_t standbyTime = systemTime(); |
| 1504 | size_t mixBufferSize = mFrameCount * mFrameSize; |
| 1505 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1506 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 1507 | nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 3; |
| 1508 | nsecs_t lastWarning = 0; |
| 1509 | bool longStandbyExit = false; |
| 1510 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 1511 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 1512 | uint32_t sleepTime = idleSleepTime; |
| 1513 | Vector< sp<EffectChain> > effectChains; |
| 1514 | |
| 1515 | while (!exitPending()) |
| 1516 | { |
| 1517 | processConfigEvents(); |
| 1518 | |
| 1519 | mixerStatus = MIXER_IDLE; |
| 1520 | { // scope for mLock |
| 1521 | |
| 1522 | Mutex::Autolock _l(mLock); |
| 1523 | |
| 1524 | if (checkForNewParameters_l()) { |
| 1525 | mixBufferSize = mFrameCount * mFrameSize; |
| 1526 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 1527 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 1528 | maxPeriod = seconds(mFrameCount) / mSampleRate * 3; |
| 1529 | activeSleepTime = activeSleepTimeUs(); |
| 1530 | idleSleepTime = idleSleepTimeUs(); |
| 1531 | } |
| 1532 | |
| 1533 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 1534 | |
| 1535 | // put audio hardware into standby after short delay |
| 1536 | if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 1537 | mSuspended) { |
| 1538 | if (!mStandby) { |
| 1539 | LOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended); |
| 1540 | mOutput->standby(); |
| 1541 | mStandby = true; |
| 1542 | mBytesWritten = 0; |
| 1543 | } |
| 1544 | |
| 1545 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 1546 | // we're about to wait, flush the binder command buffer |
| 1547 | IPCThreadState::self()->flushCommands(); |
| 1548 | |
| 1549 | if (exitPending()) break; |
| 1550 | |
| 1551 | // wait until we have something to do... |
| 1552 | LOGV("MixerThread %p TID %d going to sleep\n", this, gettid()); |
| 1553 | mWaitWorkCV.wait(mLock); |
| 1554 | LOGV("MixerThread %p TID %d waking up\n", this, gettid()); |
| 1555 | |
| 1556 | if (mMasterMute == false) { |
| 1557 | char value[PROPERTY_VALUE_MAX]; |
| 1558 | property_get("ro.audio.silent", value, "0"); |
| 1559 | if (atoi(value)) { |
| 1560 | LOGD("Silence is golden"); |
| 1561 | setMasterMute(true); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1566 | sleepTime = idleSleepTime; |
| 1567 | continue; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 1572 | |
| 1573 | // prevent any changes in effect chain list and in each effect chain |
| 1574 | // during mixing and effect process as the audio buffers could be deleted |
| 1575 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1576 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 1580 | // mix buffers... |
| 1581 | mAudioMixer->process(); |
| 1582 | sleepTime = 0; |
| 1583 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 1584 | //TODO: delay standby when effects have a tail |
| 1585 | } else { |
| 1586 | // If no tracks are ready, sleep once for the duration of an output |
| 1587 | // buffer size, then write 0s to the output |
| 1588 | if (sleepTime == 0) { |
| 1589 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 1590 | sleepTime = activeSleepTime; |
| 1591 | } else { |
| 1592 | sleepTime = idleSleepTime; |
| 1593 | } |
| 1594 | } else if (mBytesWritten != 0 || |
| 1595 | (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) { |
| 1596 | memset (mMixBuffer, 0, mixBufferSize); |
| 1597 | sleepTime = 0; |
| 1598 | LOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start"); |
| 1599 | } |
| 1600 | // TODO add standby time extension fct of effect tail |
| 1601 | } |
| 1602 | |
| 1603 | if (mSuspended) { |
| 1604 | sleepTime = idleSleepTime; |
| 1605 | } |
| 1606 | // sleepTime == 0 means we must write to audio hardware |
| 1607 | if (sleepTime == 0) { |
| 1608 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1609 | effectChains[i]->process_l(); |
| 1610 | } |
| 1611 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1612 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1613 | #ifdef LVMX |
| 1614 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1615 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) { |
| 1616 | LifeVibes::process(audioOutputType, mMixBuffer, mixBufferSize); |
| 1617 | } |
| 1618 | #endif |
| 1619 | mLastWriteTime = systemTime(); |
| 1620 | mInWrite = true; |
| 1621 | mBytesWritten += mixBufferSize; |
| 1622 | |
| 1623 | int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize); |
| 1624 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 1625 | mNumWrites++; |
| 1626 | mInWrite = false; |
| 1627 | nsecs_t now = systemTime(); |
| 1628 | nsecs_t delta = now - mLastWriteTime; |
| 1629 | if (delta > maxPeriod) { |
| 1630 | mNumDelayedWrites++; |
| 1631 | if ((now - lastWarning) > kWarningThrottle) { |
| 1632 | LOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 1633 | ns2ms(delta), mNumDelayedWrites, this); |
| 1634 | lastWarning = now; |
| 1635 | } |
| 1636 | if (mStandby) { |
| 1637 | longStandbyExit = true; |
| 1638 | } |
| 1639 | } |
| 1640 | mStandby = false; |
| 1641 | } else { |
| 1642 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1643 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1644 | usleep(sleepTime); |
| 1645 | } |
| 1646 | |
| 1647 | // finally let go of all our tracks, without the lock held |
| 1648 | // since we can't guarantee the destructors won't acquire that |
| 1649 | // same lock. |
| 1650 | tracksToRemove.clear(); |
| 1651 | |
| 1652 | // Effect chains will be actually deleted here if they were removed from |
| 1653 | // mEffectChains list during mixing or effects processing |
| 1654 | effectChains.clear(); |
| 1655 | } |
| 1656 | |
| 1657 | if (!mStandby) { |
| 1658 | mOutput->standby(); |
| 1659 | } |
| 1660 | |
| 1661 | LOGV("MixerThread %p exiting", this); |
| 1662 | return false; |
| 1663 | } |
| 1664 | |
| 1665 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 1666 | uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove) |
| 1667 | { |
| 1668 | |
| 1669 | uint32_t mixerStatus = MIXER_IDLE; |
| 1670 | // find out which tracks need to be processed |
| 1671 | size_t count = activeTracks.size(); |
| 1672 | size_t mixedTracks = 0; |
| 1673 | size_t tracksWithEffect = 0; |
| 1674 | |
| 1675 | float masterVolume = mMasterVolume; |
| 1676 | bool masterMute = mMasterMute; |
| 1677 | |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame^] | 1678 | if (masterMute) { |
| 1679 | masterVolume = 0; |
| 1680 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1681 | #ifdef LVMX |
| 1682 | bool tracksConnectedChanged = false; |
| 1683 | bool stateChanged = false; |
| 1684 | |
| 1685 | int audioOutputType = LifeVibes::getMixerType(mId, mType); |
| 1686 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) |
| 1687 | { |
| 1688 | int activeTypes = 0; |
| 1689 | for (size_t i=0 ; i<count ; i++) { |
| 1690 | sp<Track> t = activeTracks[i].promote(); |
| 1691 | if (t == 0) continue; |
| 1692 | Track* const track = t.get(); |
| 1693 | int iTracktype=track->type(); |
| 1694 | activeTypes |= 1<<track->type(); |
| 1695 | } |
| 1696 | LifeVibes::computeVolumes(audioOutputType, activeTypes, tracksConnectedChanged, stateChanged, masterVolume, masterMute); |
| 1697 | } |
| 1698 | #endif |
| 1699 | // Delegate master volume control to effect in output mix effect chain if needed |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1700 | sp<EffectChain> chain = getEffectChain_l(AudioSystem::SESSION_OUTPUT_MIX); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1701 | if (chain != 0) { |
Eric Laurent | 571d49c | 2010-08-11 05:20:11 -0700 | [diff] [blame^] | 1702 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1703 | chain->setVolume_l(&v, &v); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1704 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 1705 | chain.clear(); |
| 1706 | } |
| 1707 | |
| 1708 | for (size_t i=0 ; i<count ; i++) { |
| 1709 | sp<Track> t = activeTracks[i].promote(); |
| 1710 | if (t == 0) continue; |
| 1711 | |
| 1712 | Track* const track = t.get(); |
| 1713 | audio_track_cblk_t* cblk = track->cblk(); |
| 1714 | |
| 1715 | // The first time a track is added we wait |
| 1716 | // for all its buffers to be filled before processing it |
| 1717 | mAudioMixer->setActiveTrack(track->name()); |
| 1718 | if (cblk->framesReady() && (track->isReady() || track->isStopped()) && |
| 1719 | !track->isPaused() && !track->isTerminated()) |
| 1720 | { |
| 1721 | //LOGV("track %d u=%08x, s=%08x [OK] on thread %p", track->name(), cblk->user, cblk->server, this); |
| 1722 | |
| 1723 | mixedTracks++; |
| 1724 | |
| 1725 | // track->mainBuffer() != mMixBuffer means there is an effect chain |
| 1726 | // connected to the track |
| 1727 | chain.clear(); |
| 1728 | if (track->mainBuffer() != mMixBuffer) { |
| 1729 | chain = getEffectChain_l(track->sessionId()); |
| 1730 | // Delegate volume control to effect in track effect chain if needed |
| 1731 | if (chain != 0) { |
| 1732 | tracksWithEffect++; |
| 1733 | } else { |
| 1734 | LOGW("prepareTracks_l(): track %08x attached to effect but no chain found on session %d", |
| 1735 | track->name(), track->sessionId()); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | int param = AudioMixer::VOLUME; |
| 1741 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 1742 | // no ramp for the first volume setting |
| 1743 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 1744 | if (track->mState == TrackBase::RESUMING) { |
| 1745 | track->mState = TrackBase::ACTIVE; |
| 1746 | param = AudioMixer::RAMP_VOLUME; |
| 1747 | } |
| 1748 | } else if (cblk->server != 0) { |
| 1749 | // If the track is stopped before the first frame was mixed, |
| 1750 | // do not apply ramp |
| 1751 | param = AudioMixer::RAMP_VOLUME; |
| 1752 | } |
| 1753 | |
| 1754 | // compute volume for this track |
| 1755 | int16_t left, right, aux; |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 1756 | if (track->isMuted() || track->isPausing() || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1757 | mStreamTypes[track->type()].mute) { |
| 1758 | left = right = aux = 0; |
| 1759 | if (track->isPausing()) { |
| 1760 | track->setPaused(); |
| 1761 | } |
| 1762 | } else { |
| 1763 | // read original volumes with volume control |
| 1764 | float typeVolume = mStreamTypes[track->type()].volume; |
| 1765 | #ifdef LVMX |
| 1766 | bool streamMute=false; |
| 1767 | // read the volume from the LivesVibes audio engine. |
| 1768 | if (LifeVibes::audioOutputTypeIsLifeVibes(audioOutputType)) |
| 1769 | { |
| 1770 | LifeVibes::getStreamVolumes(audioOutputType, track->type(), &typeVolume, &streamMute); |
| 1771 | if (streamMute) { |
| 1772 | typeVolume = 0; |
| 1773 | } |
| 1774 | } |
| 1775 | #endif |
| 1776 | float v = masterVolume * typeVolume; |
| 1777 | uint32_t vl = (uint32_t)(v * cblk->volume[0]) << 12; |
| 1778 | uint32_t vr = (uint32_t)(v * cblk->volume[1]) << 12; |
| 1779 | |
| 1780 | // Delegate volume control to effect in track effect chain if needed |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1781 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1782 | // Do not ramp volume is volume is controlled by effect |
| 1783 | param = AudioMixer::VOLUME; |
| 1784 | } |
| 1785 | |
| 1786 | // Convert volumes from 8.24 to 4.12 format |
| 1787 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 1788 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 1789 | left = int16_t(v_clamped); |
| 1790 | v_clamped = (vr + (1 << 11)) >> 12; |
| 1791 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 1792 | right = int16_t(v_clamped); |
| 1793 | |
| 1794 | v_clamped = (uint32_t)(v * cblk->sendLevel); |
| 1795 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 1796 | aux = int16_t(v_clamped); |
| 1797 | } |
| 1798 | |
| 1799 | #ifdef LVMX |
| 1800 | if ( tracksConnectedChanged || stateChanged ) |
| 1801 | { |
| 1802 | // only do the ramp when the volume is changed by the user / application |
| 1803 | param = AudioMixer::VOLUME; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
| 1807 | // XXX: these things DON'T need to be done each time |
| 1808 | mAudioMixer->setBufferProvider(track); |
| 1809 | mAudioMixer->enable(AudioMixer::MIXING); |
| 1810 | |
| 1811 | mAudioMixer->setParameter(param, AudioMixer::VOLUME0, (void *)left); |
| 1812 | mAudioMixer->setParameter(param, AudioMixer::VOLUME1, (void *)right); |
| 1813 | mAudioMixer->setParameter(param, AudioMixer::AUXLEVEL, (void *)aux); |
| 1814 | mAudioMixer->setParameter( |
| 1815 | AudioMixer::TRACK, |
| 1816 | AudioMixer::FORMAT, (void *)track->format()); |
| 1817 | mAudioMixer->setParameter( |
| 1818 | AudioMixer::TRACK, |
| 1819 | AudioMixer::CHANNEL_COUNT, (void *)track->channelCount()); |
| 1820 | mAudioMixer->setParameter( |
| 1821 | AudioMixer::RESAMPLE, |
| 1822 | AudioMixer::SAMPLE_RATE, |
| 1823 | (void *)(cblk->sampleRate)); |
| 1824 | mAudioMixer->setParameter( |
| 1825 | AudioMixer::TRACK, |
| 1826 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 1827 | mAudioMixer->setParameter( |
| 1828 | AudioMixer::TRACK, |
| 1829 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 1830 | |
| 1831 | // reset retry count |
| 1832 | track->mRetryCount = kMaxTrackRetries; |
| 1833 | mixerStatus = MIXER_TRACKS_READY; |
| 1834 | } else { |
| 1835 | //LOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", track->name(), cblk->user, cblk->server, this); |
| 1836 | if (track->isStopped()) { |
| 1837 | track->reset(); |
| 1838 | } |
| 1839 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 1840 | // We have consumed all the buffers of this track. |
| 1841 | // Remove it from the list of active tracks. |
| 1842 | tracksToRemove->add(track); |
| 1843 | } else { |
| 1844 | // No buffers for this track. Give it a few chances to |
| 1845 | // fill a buffer, then remove it from active list. |
| 1846 | if (--(track->mRetryCount) <= 0) { |
| 1847 | LOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", track->name(), this); |
| 1848 | tracksToRemove->add(track); |
| 1849 | } else if (mixerStatus != MIXER_TRACKS_READY) { |
| 1850 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 1851 | } |
| 1852 | } |
| 1853 | mAudioMixer->disable(AudioMixer::MIXING); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | // remove all the tracks that need to be... |
| 1858 | count = tracksToRemove->size(); |
| 1859 | if (UNLIKELY(count)) { |
| 1860 | for (size_t i=0 ; i<count ; i++) { |
| 1861 | const sp<Track>& track = tracksToRemove->itemAt(i); |
| 1862 | mActiveTracks.remove(track); |
| 1863 | if (track->mainBuffer() != mMixBuffer) { |
| 1864 | chain = getEffectChain_l(track->sessionId()); |
| 1865 | if (chain != 0) { |
| 1866 | LOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId()); |
| 1867 | chain->stopTrack(); |
| 1868 | } |
| 1869 | } |
| 1870 | if (track->isTerminated()) { |
| 1871 | mTracks.remove(track); |
| 1872 | deleteTrackName_l(track->mName); |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | // mix buffer must be cleared if all tracks are connected to an |
| 1878 | // effect chain as in this case the mixer will not write to |
| 1879 | // mix buffer and track effects will accumulate into it |
| 1880 | if (mixedTracks != 0 && mixedTracks == tracksWithEffect) { |
| 1881 | memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t)); |
| 1882 | } |
| 1883 | |
| 1884 | return mixerStatus; |
| 1885 | } |
| 1886 | |
| 1887 | void AudioFlinger::MixerThread::invalidateTracks(int streamType) |
| 1888 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1889 | LOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
| 1890 | this, streamType, mTracks.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1892 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1893 | size_t size = mTracks.size(); |
| 1894 | for (size_t i = 0; i < size; i++) { |
| 1895 | sp<Track> t = mTracks[i]; |
| 1896 | if (t->type() == streamType) { |
| 1897 | t->mCblk->lock.lock(); |
| 1898 | t->mCblk->flags |= CBLK_INVALID_ON; |
| 1899 | t->mCblk->cv.signal(); |
| 1900 | t->mCblk->lock.unlock(); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 1907 | int AudioFlinger::MixerThread::getTrackName_l() |
| 1908 | { |
| 1909 | return mAudioMixer->getTrackName(); |
| 1910 | } |
| 1911 | |
| 1912 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 1913 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 1914 | { |
| 1915 | LOGV("remove track (%d) and delete from mixer", name); |
| 1916 | mAudioMixer->deleteTrackName(name); |
| 1917 | } |
| 1918 | |
| 1919 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 1920 | bool AudioFlinger::MixerThread::checkForNewParameters_l() |
| 1921 | { |
| 1922 | bool reconfig = false; |
| 1923 | |
| 1924 | while (!mNewParameters.isEmpty()) { |
| 1925 | status_t status = NO_ERROR; |
| 1926 | String8 keyValuePair = mNewParameters[0]; |
| 1927 | AudioParameter param = AudioParameter(keyValuePair); |
| 1928 | int value; |
| 1929 | |
| 1930 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 1931 | reconfig = true; |
| 1932 | } |
| 1933 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 1934 | if (value != AudioSystem::PCM_16_BIT) { |
| 1935 | status = BAD_VALUE; |
| 1936 | } else { |
| 1937 | reconfig = true; |
| 1938 | } |
| 1939 | } |
| 1940 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 1941 | if (value != AudioSystem::CHANNEL_OUT_STEREO) { |
| 1942 | status = BAD_VALUE; |
| 1943 | } else { |
| 1944 | reconfig = true; |
| 1945 | } |
| 1946 | } |
| 1947 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 1948 | // do not accept frame count changes if tracks are open as the track buffer |
| 1949 | // size depends on frame count and correct behavior would not be garantied |
| 1950 | // if frame count is changed after track creation |
| 1951 | if (!mTracks.isEmpty()) { |
| 1952 | status = INVALID_OPERATION; |
| 1953 | } else { |
| 1954 | reconfig = true; |
| 1955 | } |
| 1956 | } |
| 1957 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 1958 | // forward device change to effects that have requested to be |
| 1959 | // aware of attached audio device. |
| 1960 | mDevice = (uint32_t)value; |
| 1961 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 1962 | mEffectChains[i]->setDevice_l(mDevice); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | if (status == NO_ERROR) { |
| 1967 | status = mOutput->setParameters(keyValuePair); |
| 1968 | if (!mStandby && status == INVALID_OPERATION) { |
| 1969 | mOutput->standby(); |
| 1970 | mStandby = true; |
| 1971 | mBytesWritten = 0; |
| 1972 | status = mOutput->setParameters(keyValuePair); |
| 1973 | } |
| 1974 | if (status == NO_ERROR && reconfig) { |
| 1975 | delete mAudioMixer; |
| 1976 | readOutputParameters(); |
| 1977 | mAudioMixer = new AudioMixer(mFrameCount, mSampleRate); |
| 1978 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 1979 | int name = getTrackName_l(); |
| 1980 | if (name < 0) break; |
| 1981 | mTracks[i]->mName = name; |
| 1982 | // limit track sample rate to 2 x new output sample rate |
| 1983 | if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) { |
| 1984 | mTracks[i]->mCblk->sampleRate = 2 * sampleRate(); |
| 1985 | } |
| 1986 | } |
| 1987 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | mNewParameters.removeAt(0); |
| 1992 | |
| 1993 | mParamStatus = status; |
| 1994 | mParamCond.signal(); |
| 1995 | mWaitWorkCV.wait(mLock); |
| 1996 | } |
| 1997 | return reconfig; |
| 1998 | } |
| 1999 | |
| 2000 | status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 2001 | { |
| 2002 | const size_t SIZE = 256; |
| 2003 | char buffer[SIZE]; |
| 2004 | String8 result; |
| 2005 | |
| 2006 | PlaybackThread::dumpInternals(fd, args); |
| 2007 | |
| 2008 | snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); |
| 2009 | result.append(buffer); |
| 2010 | write(fd, result.string(), result.size()); |
| 2011 | return NO_ERROR; |
| 2012 | } |
| 2013 | |
| 2014 | uint32_t AudioFlinger::MixerThread::activeSleepTimeUs() |
| 2015 | { |
| 2016 | return (uint32_t)(mOutput->latency() * 1000) / 2; |
| 2017 | } |
| 2018 | |
| 2019 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() |
| 2020 | { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2021 | return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | // ---------------------------------------------------------------------------- |
| 2025 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device) |
| 2026 | : PlaybackThread(audioFlinger, output, id, device) |
| 2027 | { |
| 2028 | mType = PlaybackThread::DIRECT; |
| 2029 | } |
| 2030 | |
| 2031 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 2032 | { |
| 2033 | } |
| 2034 | |
| 2035 | |
| 2036 | static inline int16_t clamp16(int32_t sample) |
| 2037 | { |
| 2038 | if ((sample>>15) ^ (sample>>31)) |
| 2039 | sample = 0x7FFF ^ (sample>>31); |
| 2040 | return sample; |
| 2041 | } |
| 2042 | |
| 2043 | static inline |
| 2044 | int32_t mul(int16_t in, int16_t v) |
| 2045 | { |
| 2046 | #if defined(__arm__) && !defined(__thumb__) |
| 2047 | int32_t out; |
| 2048 | asm( "smulbb %[out], %[in], %[v] \n" |
| 2049 | : [out]"=r"(out) |
| 2050 | : [in]"%r"(in), [v]"r"(v) |
| 2051 | : ); |
| 2052 | return out; |
| 2053 | #else |
| 2054 | return in * int32_t(v); |
| 2055 | #endif |
| 2056 | } |
| 2057 | |
| 2058 | void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp) |
| 2059 | { |
| 2060 | // Do not apply volume on compressed audio |
| 2061 | if (!AudioSystem::isLinearPCM(mFormat)) { |
| 2062 | return; |
| 2063 | } |
| 2064 | |
| 2065 | // convert to signed 16 bit before volume calculation |
| 2066 | if (mFormat == AudioSystem::PCM_8_BIT) { |
| 2067 | size_t count = mFrameCount * mChannelCount; |
| 2068 | uint8_t *src = (uint8_t *)mMixBuffer + count-1; |
| 2069 | int16_t *dst = mMixBuffer + count-1; |
| 2070 | while(count--) { |
| 2071 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | size_t frameCount = mFrameCount; |
| 2076 | int16_t *out = mMixBuffer; |
| 2077 | if (ramp) { |
| 2078 | if (mChannelCount == 1) { |
| 2079 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2080 | int32_t vlInc = d / (int32_t)frameCount; |
| 2081 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2082 | do { |
| 2083 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2084 | out++; |
| 2085 | vl += vlInc; |
| 2086 | } while (--frameCount); |
| 2087 | |
| 2088 | } else { |
| 2089 | int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16; |
| 2090 | int32_t vlInc = d / (int32_t)frameCount; |
| 2091 | d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16; |
| 2092 | int32_t vrInc = d / (int32_t)frameCount; |
| 2093 | int32_t vl = ((int32_t)mLeftVolShort << 16); |
| 2094 | int32_t vr = ((int32_t)mRightVolShort << 16); |
| 2095 | do { |
| 2096 | out[0] = clamp16(mul(out[0], vl >> 16) >> 12); |
| 2097 | out[1] = clamp16(mul(out[1], vr >> 16) >> 12); |
| 2098 | out += 2; |
| 2099 | vl += vlInc; |
| 2100 | vr += vrInc; |
| 2101 | } while (--frameCount); |
| 2102 | } |
| 2103 | } else { |
| 2104 | if (mChannelCount == 1) { |
| 2105 | do { |
| 2106 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2107 | out++; |
| 2108 | } while (--frameCount); |
| 2109 | } else { |
| 2110 | do { |
| 2111 | out[0] = clamp16(mul(out[0], leftVol) >> 12); |
| 2112 | out[1] = clamp16(mul(out[1], rightVol) >> 12); |
| 2113 | out += 2; |
| 2114 | } while (--frameCount); |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | // convert back to unsigned 8 bit after volume calculation |
| 2119 | if (mFormat == AudioSystem::PCM_8_BIT) { |
| 2120 | size_t count = mFrameCount * mChannelCount; |
| 2121 | int16_t *src = mMixBuffer; |
| 2122 | uint8_t *dst = (uint8_t *)mMixBuffer; |
| 2123 | while(count--) { |
| 2124 | *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80; |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | mLeftVolShort = leftVol; |
| 2129 | mRightVolShort = rightVol; |
| 2130 | } |
| 2131 | |
| 2132 | bool AudioFlinger::DirectOutputThread::threadLoop() |
| 2133 | { |
| 2134 | uint32_t mixerStatus = MIXER_IDLE; |
| 2135 | sp<Track> trackToRemove; |
| 2136 | sp<Track> activeTrack; |
| 2137 | nsecs_t standbyTime = systemTime(); |
| 2138 | int8_t *curBuf; |
| 2139 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2140 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2141 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2142 | uint32_t sleepTime = idleSleepTime; |
| 2143 | // use shorter standby delay as on normal output to release |
| 2144 | // hardware resources as soon as possible |
| 2145 | nsecs_t standbyDelay = microseconds(activeSleepTime*2); |
| 2146 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2147 | while (!exitPending()) |
| 2148 | { |
| 2149 | bool rampVolume; |
| 2150 | uint16_t leftVol; |
| 2151 | uint16_t rightVol; |
| 2152 | Vector< sp<EffectChain> > effectChains; |
| 2153 | |
| 2154 | processConfigEvents(); |
| 2155 | |
| 2156 | mixerStatus = MIXER_IDLE; |
| 2157 | |
| 2158 | { // scope for the mLock |
| 2159 | |
| 2160 | Mutex::Autolock _l(mLock); |
| 2161 | |
| 2162 | if (checkForNewParameters_l()) { |
| 2163 | mixBufferSize = mFrameCount*mFrameSize; |
| 2164 | activeSleepTime = activeSleepTimeUs(); |
| 2165 | idleSleepTime = idleSleepTimeUs(); |
| 2166 | standbyDelay = microseconds(activeSleepTime*2); |
| 2167 | } |
| 2168 | |
| 2169 | // put audio hardware into standby after short delay |
| 2170 | if UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || |
| 2171 | mSuspended) { |
| 2172 | // wait until we have something to do... |
| 2173 | if (!mStandby) { |
| 2174 | LOGV("Audio hardware entering standby, mixer %p\n", this); |
| 2175 | mOutput->standby(); |
| 2176 | mStandby = true; |
| 2177 | mBytesWritten = 0; |
| 2178 | } |
| 2179 | |
| 2180 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2181 | // we're about to wait, flush the binder command buffer |
| 2182 | IPCThreadState::self()->flushCommands(); |
| 2183 | |
| 2184 | if (exitPending()) break; |
| 2185 | |
| 2186 | LOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid()); |
| 2187 | mWaitWorkCV.wait(mLock); |
| 2188 | LOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid()); |
| 2189 | |
| 2190 | if (mMasterMute == false) { |
| 2191 | char value[PROPERTY_VALUE_MAX]; |
| 2192 | property_get("ro.audio.silent", value, "0"); |
| 2193 | if (atoi(value)) { |
| 2194 | LOGD("Silence is golden"); |
| 2195 | setMasterMute(true); |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | standbyTime = systemTime() + standbyDelay; |
| 2200 | sleepTime = idleSleepTime; |
| 2201 | continue; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | effectChains = mEffectChains; |
| 2206 | |
| 2207 | // find out which tracks need to be processed |
| 2208 | if (mActiveTracks.size() != 0) { |
| 2209 | sp<Track> t = mActiveTracks[0].promote(); |
| 2210 | if (t == 0) continue; |
| 2211 | |
| 2212 | Track* const track = t.get(); |
| 2213 | audio_track_cblk_t* cblk = track->cblk(); |
| 2214 | |
| 2215 | // The first time a track is added we wait |
| 2216 | // for all its buffers to be filled before processing it |
| 2217 | if (cblk->framesReady() && (track->isReady() || track->isStopped()) && |
| 2218 | !track->isPaused() && !track->isTerminated()) |
| 2219 | { |
| 2220 | //LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); |
| 2221 | |
| 2222 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 2223 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 2224 | mLeftVolFloat = mRightVolFloat = 0; |
| 2225 | mLeftVolShort = mRightVolShort = 0; |
| 2226 | if (track->mState == TrackBase::RESUMING) { |
| 2227 | track->mState = TrackBase::ACTIVE; |
| 2228 | rampVolume = true; |
| 2229 | } |
| 2230 | } else if (cblk->server != 0) { |
| 2231 | // If the track is stopped before the first frame was mixed, |
| 2232 | // do not apply ramp |
| 2233 | rampVolume = true; |
| 2234 | } |
| 2235 | // compute volume for this track |
| 2236 | float left, right; |
| 2237 | if (track->isMuted() || mMasterMute || track->isPausing() || |
| 2238 | mStreamTypes[track->type()].mute) { |
| 2239 | left = right = 0; |
| 2240 | if (track->isPausing()) { |
| 2241 | track->setPaused(); |
| 2242 | } |
| 2243 | } else { |
| 2244 | float typeVolume = mStreamTypes[track->type()].volume; |
| 2245 | float v = mMasterVolume * typeVolume; |
| 2246 | float v_clamped = v * cblk->volume[0]; |
| 2247 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2248 | left = v_clamped/MAX_GAIN; |
| 2249 | v_clamped = v * cblk->volume[1]; |
| 2250 | if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN; |
| 2251 | right = v_clamped/MAX_GAIN; |
| 2252 | } |
| 2253 | |
| 2254 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 2255 | mLeftVolFloat = left; |
| 2256 | mRightVolFloat = right; |
| 2257 | |
| 2258 | // If audio HAL implements volume control, |
| 2259 | // force software volume to nominal value |
| 2260 | if (mOutput->setVolume(left, right) == NO_ERROR) { |
| 2261 | left = 1.0f; |
| 2262 | right = 1.0f; |
| 2263 | } |
| 2264 | |
| 2265 | // Convert volumes from float to 8.24 |
| 2266 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 2267 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 2268 | |
| 2269 | // Delegate volume control to effect in track effect chain if needed |
| 2270 | // only one effect chain can be present on DirectOutputThread, so if |
| 2271 | // there is one, the track is connected to it |
| 2272 | if (!effectChains.isEmpty()) { |
| 2273 | // Do not ramp volume is volume is controlled by effect |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 2274 | if(effectChains[0]->setVolume_l(&vl, &vr)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2275 | rampVolume = false; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | // Convert volumes from 8.24 to 4.12 format |
| 2280 | uint32_t v_clamped = (vl + (1 << 11)) >> 12; |
| 2281 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2282 | leftVol = (uint16_t)v_clamped; |
| 2283 | v_clamped = (vr + (1 << 11)) >> 12; |
| 2284 | if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT; |
| 2285 | rightVol = (uint16_t)v_clamped; |
| 2286 | } else { |
| 2287 | leftVol = mLeftVolShort; |
| 2288 | rightVol = mRightVolShort; |
| 2289 | rampVolume = false; |
| 2290 | } |
| 2291 | |
| 2292 | // reset retry count |
| 2293 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 2294 | activeTrack = t; |
| 2295 | mixerStatus = MIXER_TRACKS_READY; |
| 2296 | } else { |
| 2297 | //LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); |
| 2298 | if (track->isStopped()) { |
| 2299 | track->reset(); |
| 2300 | } |
| 2301 | if (track->isTerminated() || track->isStopped() || track->isPaused()) { |
| 2302 | // We have consumed all the buffers of this track. |
| 2303 | // Remove it from the list of active tracks. |
| 2304 | trackToRemove = track; |
| 2305 | } else { |
| 2306 | // No buffers for this track. Give it a few chances to |
| 2307 | // fill a buffer, then remove it from active list. |
| 2308 | if (--(track->mRetryCount) <= 0) { |
| 2309 | LOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
| 2310 | trackToRemove = track; |
| 2311 | } else { |
| 2312 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | // remove all the tracks that need to be... |
| 2319 | if (UNLIKELY(trackToRemove != 0)) { |
| 2320 | mActiveTracks.remove(trackToRemove); |
| 2321 | if (!effectChains.isEmpty()) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2322 | LOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(), |
| 2323 | trackToRemove->sessionId()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2324 | effectChains[0]->stopTrack(); |
| 2325 | } |
| 2326 | if (trackToRemove->isTerminated()) { |
| 2327 | mTracks.remove(trackToRemove); |
| 2328 | deleteTrackName_l(trackToRemove->mName); |
| 2329 | } |
| 2330 | } |
| 2331 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2332 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 2336 | AudioBufferProvider::Buffer buffer; |
| 2337 | size_t frameCount = mFrameCount; |
| 2338 | curBuf = (int8_t *)mMixBuffer; |
| 2339 | // output audio to hardware |
| 2340 | while (frameCount) { |
| 2341 | buffer.frameCount = frameCount; |
| 2342 | activeTrack->getNextBuffer(&buffer); |
| 2343 | if (UNLIKELY(buffer.raw == 0)) { |
| 2344 | memset(curBuf, 0, frameCount * mFrameSize); |
| 2345 | break; |
| 2346 | } |
| 2347 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 2348 | frameCount -= buffer.frameCount; |
| 2349 | curBuf += buffer.frameCount * mFrameSize; |
| 2350 | activeTrack->releaseBuffer(&buffer); |
| 2351 | } |
| 2352 | sleepTime = 0; |
| 2353 | standbyTime = systemTime() + standbyDelay; |
| 2354 | } else { |
| 2355 | if (sleepTime == 0) { |
| 2356 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2357 | sleepTime = activeSleepTime; |
| 2358 | } else { |
| 2359 | sleepTime = idleSleepTime; |
| 2360 | } |
| 2361 | } else if (mBytesWritten != 0 && AudioSystem::isLinearPCM(mFormat)) { |
| 2362 | memset (mMixBuffer, 0, mFrameCount * mFrameSize); |
| 2363 | sleepTime = 0; |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | if (mSuspended) { |
| 2368 | sleepTime = idleSleepTime; |
| 2369 | } |
| 2370 | // sleepTime == 0 means we must write to audio hardware |
| 2371 | if (sleepTime == 0) { |
| 2372 | if (mixerStatus == MIXER_TRACKS_READY) { |
| 2373 | applyVolume(leftVol, rightVol, rampVolume); |
| 2374 | } |
| 2375 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2376 | effectChains[i]->process_l(); |
| 2377 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2378 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2379 | |
| 2380 | mLastWriteTime = systemTime(); |
| 2381 | mInWrite = true; |
| 2382 | mBytesWritten += mixBufferSize; |
| 2383 | int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize); |
| 2384 | if (bytesWritten < 0) mBytesWritten -= mixBufferSize; |
| 2385 | mNumWrites++; |
| 2386 | mInWrite = false; |
| 2387 | mStandby = false; |
| 2388 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2389 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2390 | usleep(sleepTime); |
| 2391 | } |
| 2392 | |
| 2393 | // finally let go of removed track, without the lock held |
| 2394 | // since we can't guarantee the destructors won't acquire that |
| 2395 | // same lock. |
| 2396 | trackToRemove.clear(); |
| 2397 | activeTrack.clear(); |
| 2398 | |
| 2399 | // Effect chains will be actually deleted here if they were removed from |
| 2400 | // mEffectChains list during mixing or effects processing |
| 2401 | effectChains.clear(); |
| 2402 | } |
| 2403 | |
| 2404 | if (!mStandby) { |
| 2405 | mOutput->standby(); |
| 2406 | } |
| 2407 | |
| 2408 | LOGV("DirectOutputThread %p exiting", this); |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
| 2412 | // getTrackName_l() must be called with ThreadBase::mLock held |
| 2413 | int AudioFlinger::DirectOutputThread::getTrackName_l() |
| 2414 | { |
| 2415 | return 0; |
| 2416 | } |
| 2417 | |
| 2418 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 2419 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) |
| 2420 | { |
| 2421 | } |
| 2422 | |
| 2423 | // checkForNewParameters_l() must be called with ThreadBase::mLock held |
| 2424 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() |
| 2425 | { |
| 2426 | bool reconfig = false; |
| 2427 | |
| 2428 | while (!mNewParameters.isEmpty()) { |
| 2429 | status_t status = NO_ERROR; |
| 2430 | String8 keyValuePair = mNewParameters[0]; |
| 2431 | AudioParameter param = AudioParameter(keyValuePair); |
| 2432 | int value; |
| 2433 | |
| 2434 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 2435 | // do not accept frame count changes if tracks are open as the track buffer |
| 2436 | // size depends on frame count and correct behavior would not be garantied |
| 2437 | // if frame count is changed after track creation |
| 2438 | if (!mTracks.isEmpty()) { |
| 2439 | status = INVALID_OPERATION; |
| 2440 | } else { |
| 2441 | reconfig = true; |
| 2442 | } |
| 2443 | } |
| 2444 | if (status == NO_ERROR) { |
| 2445 | status = mOutput->setParameters(keyValuePair); |
| 2446 | if (!mStandby && status == INVALID_OPERATION) { |
| 2447 | mOutput->standby(); |
| 2448 | mStandby = true; |
| 2449 | mBytesWritten = 0; |
| 2450 | status = mOutput->setParameters(keyValuePair); |
| 2451 | } |
| 2452 | if (status == NO_ERROR && reconfig) { |
| 2453 | readOutputParameters(); |
| 2454 | sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | mNewParameters.removeAt(0); |
| 2459 | |
| 2460 | mParamStatus = status; |
| 2461 | mParamCond.signal(); |
| 2462 | mWaitWorkCV.wait(mLock); |
| 2463 | } |
| 2464 | return reconfig; |
| 2465 | } |
| 2466 | |
| 2467 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() |
| 2468 | { |
| 2469 | uint32_t time; |
| 2470 | if (AudioSystem::isLinearPCM(mFormat)) { |
| 2471 | time = (uint32_t)(mOutput->latency() * 1000) / 2; |
| 2472 | } else { |
| 2473 | time = 10000; |
| 2474 | } |
| 2475 | return time; |
| 2476 | } |
| 2477 | |
| 2478 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() |
| 2479 | { |
| 2480 | uint32_t time; |
| 2481 | if (AudioSystem::isLinearPCM(mFormat)) { |
Eric Laurent | 60e1824 | 2010-07-29 06:50:24 -0700 | [diff] [blame] | 2482 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2483 | } else { |
| 2484 | time = 10000; |
| 2485 | } |
| 2486 | return time; |
| 2487 | } |
| 2488 | |
| 2489 | // ---------------------------------------------------------------------------- |
| 2490 | |
| 2491 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id) |
| 2492 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device()), mWaitTimeMs(UINT_MAX) |
| 2493 | { |
| 2494 | mType = PlaybackThread::DUPLICATING; |
| 2495 | addOutputTrack(mainThread); |
| 2496 | } |
| 2497 | |
| 2498 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 2499 | { |
| 2500 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2501 | mOutputTracks[i]->destroy(); |
| 2502 | } |
| 2503 | mOutputTracks.clear(); |
| 2504 | } |
| 2505 | |
| 2506 | bool AudioFlinger::DuplicatingThread::threadLoop() |
| 2507 | { |
| 2508 | Vector< sp<Track> > tracksToRemove; |
| 2509 | uint32_t mixerStatus = MIXER_IDLE; |
| 2510 | nsecs_t standbyTime = systemTime(); |
| 2511 | size_t mixBufferSize = mFrameCount*mFrameSize; |
| 2512 | SortedVector< sp<OutputTrack> > outputTracks; |
| 2513 | uint32_t writeFrames = 0; |
| 2514 | uint32_t activeSleepTime = activeSleepTimeUs(); |
| 2515 | uint32_t idleSleepTime = idleSleepTimeUs(); |
| 2516 | uint32_t sleepTime = idleSleepTime; |
| 2517 | Vector< sp<EffectChain> > effectChains; |
| 2518 | |
| 2519 | while (!exitPending()) |
| 2520 | { |
| 2521 | processConfigEvents(); |
| 2522 | |
| 2523 | mixerStatus = MIXER_IDLE; |
| 2524 | { // scope for the mLock |
| 2525 | |
| 2526 | Mutex::Autolock _l(mLock); |
| 2527 | |
| 2528 | if (checkForNewParameters_l()) { |
| 2529 | mixBufferSize = mFrameCount*mFrameSize; |
| 2530 | updateWaitTime(); |
| 2531 | activeSleepTime = activeSleepTimeUs(); |
| 2532 | idleSleepTime = idleSleepTimeUs(); |
| 2533 | } |
| 2534 | |
| 2535 | const SortedVector< wp<Track> >& activeTracks = mActiveTracks; |
| 2536 | |
| 2537 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2538 | outputTracks.add(mOutputTracks[i]); |
| 2539 | } |
| 2540 | |
| 2541 | // put audio hardware into standby after short delay |
| 2542 | if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) || |
| 2543 | mSuspended) { |
| 2544 | if (!mStandby) { |
| 2545 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 2546 | outputTracks[i]->stop(); |
| 2547 | } |
| 2548 | mStandby = true; |
| 2549 | mBytesWritten = 0; |
| 2550 | } |
| 2551 | |
| 2552 | if (!activeTracks.size() && mConfigEvents.isEmpty()) { |
| 2553 | // we're about to wait, flush the binder command buffer |
| 2554 | IPCThreadState::self()->flushCommands(); |
| 2555 | outputTracks.clear(); |
| 2556 | |
| 2557 | if (exitPending()) break; |
| 2558 | |
| 2559 | LOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid()); |
| 2560 | mWaitWorkCV.wait(mLock); |
| 2561 | LOGV("DuplicatingThread %p TID %d waking up\n", this, gettid()); |
| 2562 | if (mMasterMute == false) { |
| 2563 | char value[PROPERTY_VALUE_MAX]; |
| 2564 | property_get("ro.audio.silent", value, "0"); |
| 2565 | if (atoi(value)) { |
| 2566 | LOGD("Silence is golden"); |
| 2567 | setMasterMute(true); |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 2572 | sleepTime = idleSleepTime; |
| 2573 | continue; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove); |
| 2578 | |
| 2579 | // prevent any changes in effect chain list and in each effect chain |
| 2580 | // during mixing and effect process as the audio buffers could be deleted |
| 2581 | // or modified if an effect is created or deleted |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2582 | lockEffectChains_l(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) { |
| 2586 | // mix buffers... |
| 2587 | if (outputsReady(outputTracks)) { |
| 2588 | mAudioMixer->process(); |
| 2589 | } else { |
| 2590 | memset(mMixBuffer, 0, mixBufferSize); |
| 2591 | } |
| 2592 | sleepTime = 0; |
| 2593 | writeFrames = mFrameCount; |
| 2594 | } else { |
| 2595 | if (sleepTime == 0) { |
| 2596 | if (mixerStatus == MIXER_TRACKS_ENABLED) { |
| 2597 | sleepTime = activeSleepTime; |
| 2598 | } else { |
| 2599 | sleepTime = idleSleepTime; |
| 2600 | } |
| 2601 | } else if (mBytesWritten != 0) { |
| 2602 | // flush remaining overflow buffers in output tracks |
| 2603 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 2604 | if (outputTracks[i]->isActive()) { |
| 2605 | sleepTime = 0; |
| 2606 | writeFrames = 0; |
| 2607 | memset(mMixBuffer, 0, mixBufferSize); |
| 2608 | break; |
| 2609 | } |
| 2610 | } |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if (mSuspended) { |
| 2615 | sleepTime = idleSleepTime; |
| 2616 | } |
| 2617 | // sleepTime == 0 means we must write to audio hardware |
| 2618 | if (sleepTime == 0) { |
| 2619 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2620 | effectChains[i]->process_l(); |
| 2621 | } |
| 2622 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2623 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2624 | |
| 2625 | standbyTime = systemTime() + kStandbyTimeInNsecs; |
| 2626 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 2627 | outputTracks[i]->write(mMixBuffer, writeFrames); |
| 2628 | } |
| 2629 | mStandby = false; |
| 2630 | mBytesWritten += mixBufferSize; |
| 2631 | } else { |
| 2632 | // enable changes in effect chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2633 | unlockEffectChains(effectChains); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2634 | usleep(sleepTime); |
| 2635 | } |
| 2636 | |
| 2637 | // finally let go of all our tracks, without the lock held |
| 2638 | // since we can't guarantee the destructors won't acquire that |
| 2639 | // same lock. |
| 2640 | tracksToRemove.clear(); |
| 2641 | outputTracks.clear(); |
| 2642 | |
| 2643 | // Effect chains will be actually deleted here if they were removed from |
| 2644 | // mEffectChains list during mixing or effects processing |
| 2645 | effectChains.clear(); |
| 2646 | } |
| 2647 | |
| 2648 | return false; |
| 2649 | } |
| 2650 | |
| 2651 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 2652 | { |
| 2653 | int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate(); |
| 2654 | OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread, |
| 2655 | this, |
| 2656 | mSampleRate, |
| 2657 | mFormat, |
| 2658 | mChannelCount, |
| 2659 | frameCount); |
| 2660 | if (outputTrack->cblk() != NULL) { |
| 2661 | thread->setStreamVolume(AudioSystem::NUM_STREAM_TYPES, 1.0f); |
| 2662 | mOutputTracks.add(outputTrack); |
| 2663 | LOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
| 2664 | updateWaitTime(); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 2669 | { |
| 2670 | Mutex::Autolock _l(mLock); |
| 2671 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2672 | if (mOutputTracks[i]->thread() == (ThreadBase *)thread) { |
| 2673 | mOutputTracks[i]->destroy(); |
| 2674 | mOutputTracks.removeAt(i); |
| 2675 | updateWaitTime(); |
| 2676 | return; |
| 2677 | } |
| 2678 | } |
| 2679 | LOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
| 2680 | } |
| 2681 | |
| 2682 | void AudioFlinger::DuplicatingThread::updateWaitTime() |
| 2683 | { |
| 2684 | mWaitTimeMs = UINT_MAX; |
| 2685 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 2686 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 2687 | if (strong != NULL) { |
| 2688 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 2689 | if (waitTimeMs < mWaitTimeMs) { |
| 2690 | mWaitTimeMs = waitTimeMs; |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | |
| 2697 | bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks) |
| 2698 | { |
| 2699 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 2700 | sp <ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 2701 | if (thread == 0) { |
| 2702 | LOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get()); |
| 2703 | return false; |
| 2704 | } |
| 2705 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 2706 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 2707 | LOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get()); |
| 2708 | return false; |
| 2709 | } |
| 2710 | } |
| 2711 | return true; |
| 2712 | } |
| 2713 | |
| 2714 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() |
| 2715 | { |
| 2716 | return (mWaitTimeMs * 1000) / 2; |
| 2717 | } |
| 2718 | |
| 2719 | // ---------------------------------------------------------------------------- |
| 2720 | |
| 2721 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 2722 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 2723 | const wp<ThreadBase>& thread, |
| 2724 | const sp<Client>& client, |
| 2725 | uint32_t sampleRate, |
| 2726 | int format, |
| 2727 | int channelCount, |
| 2728 | int frameCount, |
| 2729 | uint32_t flags, |
| 2730 | const sp<IMemory>& sharedBuffer, |
| 2731 | int sessionId) |
| 2732 | : RefBase(), |
| 2733 | mThread(thread), |
| 2734 | mClient(client), |
| 2735 | mCblk(0), |
| 2736 | mFrameCount(0), |
| 2737 | mState(IDLE), |
| 2738 | mClientTid(-1), |
| 2739 | mFormat(format), |
| 2740 | mFlags(flags & ~SYSTEM_FLAGS_MASK), |
| 2741 | mSessionId(sessionId) |
| 2742 | { |
| 2743 | LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
| 2744 | |
| 2745 | // LOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
| 2746 | size_t size = sizeof(audio_track_cblk_t); |
| 2747 | size_t bufferSize = frameCount*channelCount*sizeof(int16_t); |
| 2748 | if (sharedBuffer == 0) { |
| 2749 | size += bufferSize; |
| 2750 | } |
| 2751 | |
| 2752 | if (client != NULL) { |
| 2753 | mCblkMemory = client->heap()->allocate(size); |
| 2754 | if (mCblkMemory != 0) { |
| 2755 | mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer()); |
| 2756 | if (mCblk) { // construct the shared structure in-place. |
| 2757 | new(mCblk) audio_track_cblk_t(); |
| 2758 | // clear all buffers |
| 2759 | mCblk->frameCount = frameCount; |
| 2760 | mCblk->sampleRate = sampleRate; |
| 2761 | mCblk->channelCount = (uint8_t)channelCount; |
| 2762 | if (sharedBuffer == 0) { |
| 2763 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 2764 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 2765 | // Force underrun condition to avoid false underrun callback until first data is |
| 2766 | // written to buffer |
| 2767 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 2768 | } else { |
| 2769 | mBuffer = sharedBuffer->pointer(); |
| 2770 | } |
| 2771 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 2772 | } |
| 2773 | } else { |
| 2774 | LOGE("not enough memory for AudioTrack size=%u", size); |
| 2775 | client->heap()->dump("AudioTrack"); |
| 2776 | return; |
| 2777 | } |
| 2778 | } else { |
| 2779 | mCblk = (audio_track_cblk_t *)(new uint8_t[size]); |
| 2780 | if (mCblk) { // construct the shared structure in-place. |
| 2781 | new(mCblk) audio_track_cblk_t(); |
| 2782 | // clear all buffers |
| 2783 | mCblk->frameCount = frameCount; |
| 2784 | mCblk->sampleRate = sampleRate; |
| 2785 | mCblk->channelCount = (uint8_t)channelCount; |
| 2786 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 2787 | memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t)); |
| 2788 | // Force underrun condition to avoid false underrun callback until first data is |
| 2789 | // written to buffer |
| 2790 | mCblk->flags = CBLK_UNDERRUN_ON; |
| 2791 | mBufferEnd = (uint8_t *)mBuffer + bufferSize; |
| 2792 | } |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 2797 | { |
| 2798 | if (mCblk) { |
| 2799 | mCblk->~audio_track_cblk_t(); // destroy our shared-structure. |
| 2800 | if (mClient == NULL) { |
| 2801 | delete mCblk; |
| 2802 | } |
| 2803 | } |
| 2804 | mCblkMemory.clear(); // and free the shared memory |
| 2805 | if (mClient != NULL) { |
| 2806 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 2807 | mClient.clear(); |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 2812 | { |
| 2813 | buffer->raw = 0; |
| 2814 | mFrameCount = buffer->frameCount; |
| 2815 | step(); |
| 2816 | buffer->frameCount = 0; |
| 2817 | } |
| 2818 | |
| 2819 | bool AudioFlinger::ThreadBase::TrackBase::step() { |
| 2820 | bool result; |
| 2821 | audio_track_cblk_t* cblk = this->cblk(); |
| 2822 | |
| 2823 | result = cblk->stepServer(mFrameCount); |
| 2824 | if (!result) { |
| 2825 | LOGV("stepServer failed acquiring cblk mutex"); |
| 2826 | mFlags |= STEPSERVER_FAILED; |
| 2827 | } |
| 2828 | return result; |
| 2829 | } |
| 2830 | |
| 2831 | void AudioFlinger::ThreadBase::TrackBase::reset() { |
| 2832 | audio_track_cblk_t* cblk = this->cblk(); |
| 2833 | |
| 2834 | cblk->user = 0; |
| 2835 | cblk->server = 0; |
| 2836 | cblk->userBase = 0; |
| 2837 | cblk->serverBase = 0; |
| 2838 | mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK); |
| 2839 | LOGV("TrackBase::reset"); |
| 2840 | } |
| 2841 | |
| 2842 | sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const |
| 2843 | { |
| 2844 | return mCblkMemory; |
| 2845 | } |
| 2846 | |
| 2847 | int AudioFlinger::ThreadBase::TrackBase::sampleRate() const { |
| 2848 | return (int)mCblk->sampleRate; |
| 2849 | } |
| 2850 | |
| 2851 | int AudioFlinger::ThreadBase::TrackBase::channelCount() const { |
| 2852 | return (int)mCblk->channelCount; |
| 2853 | } |
| 2854 | |
| 2855 | void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const { |
| 2856 | audio_track_cblk_t* cblk = this->cblk(); |
| 2857 | int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize; |
| 2858 | int8_t *bufferEnd = bufferStart + frames * cblk->frameSize; |
| 2859 | |
| 2860 | // Check validity of returned pointer in case the track control block would have been corrupted. |
| 2861 | if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd || |
| 2862 | ((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) { |
| 2863 | LOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \ |
| 2864 | server %d, serverBase %d, user %d, userBase %d, channelCount %d", |
| 2865 | bufferStart, bufferEnd, mBuffer, mBufferEnd, |
| 2866 | cblk->server, cblk->serverBase, cblk->user, cblk->userBase, cblk->channelCount); |
| 2867 | return 0; |
| 2868 | } |
| 2869 | |
| 2870 | return bufferStart; |
| 2871 | } |
| 2872 | |
| 2873 | // ---------------------------------------------------------------------------- |
| 2874 | |
| 2875 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 2876 | AudioFlinger::PlaybackThread::Track::Track( |
| 2877 | const wp<ThreadBase>& thread, |
| 2878 | const sp<Client>& client, |
| 2879 | int streamType, |
| 2880 | uint32_t sampleRate, |
| 2881 | int format, |
| 2882 | int channelCount, |
| 2883 | int frameCount, |
| 2884 | const sp<IMemory>& sharedBuffer, |
| 2885 | int sessionId) |
| 2886 | : TrackBase(thread, client, sampleRate, format, channelCount, frameCount, 0, sharedBuffer, sessionId), |
| 2887 | mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL), mAuxEffectId(0) |
| 2888 | { |
| 2889 | if (mCblk != NULL) { |
| 2890 | sp<ThreadBase> baseThread = thread.promote(); |
| 2891 | if (baseThread != 0) { |
| 2892 | PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get(); |
| 2893 | mName = playbackThread->getTrackName_l(); |
| 2894 | mMainBuffer = playbackThread->mixBuffer(); |
| 2895 | } |
| 2896 | LOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
| 2897 | if (mName < 0) { |
| 2898 | LOGE("no more track names available"); |
| 2899 | } |
| 2900 | mVolume[0] = 1.0f; |
| 2901 | mVolume[1] = 1.0f; |
| 2902 | mStreamType = streamType; |
| 2903 | // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of |
| 2904 | // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack |
| 2905 | mCblk->frameSize = AudioSystem::isLinearPCM(format) ? channelCount * sizeof(int16_t) : sizeof(int8_t); |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | AudioFlinger::PlaybackThread::Track::~Track() |
| 2910 | { |
| 2911 | LOGV("PlaybackThread::Track destructor"); |
| 2912 | sp<ThreadBase> thread = mThread.promote(); |
| 2913 | if (thread != 0) { |
| 2914 | Mutex::Autolock _l(thread->mLock); |
| 2915 | mState = TERMINATED; |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 2920 | { |
| 2921 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 2922 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 2923 | // desctructor is called. As the destructor needs to lock mLock, |
| 2924 | // we must acquire a strong reference on this Track before locking mLock |
| 2925 | // here so that the destructor is called only when exiting this function. |
| 2926 | // On the other hand, as long as Track::destroy() is only called by |
| 2927 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 2928 | // this Track with its member mTrack. |
| 2929 | sp<Track> keep(this); |
| 2930 | { // scope for mLock |
| 2931 | sp<ThreadBase> thread = mThread.promote(); |
| 2932 | if (thread != 0) { |
| 2933 | if (!isOutputTrack()) { |
| 2934 | if (mState == ACTIVE || mState == RESUMING) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2935 | AudioSystem::stopOutput(thread->id(), |
| 2936 | (AudioSystem::stream_type)mStreamType, |
| 2937 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2938 | } |
| 2939 | AudioSystem::releaseOutput(thread->id()); |
| 2940 | } |
| 2941 | Mutex::Autolock _l(thread->mLock); |
| 2942 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 2943 | playbackThread->destroyTrack_l(this); |
| 2944 | } |
| 2945 | } |
| 2946 | } |
| 2947 | |
| 2948 | void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size) |
| 2949 | { |
| 2950 | snprintf(buffer, size, " %05d %05d %03u %03u %03u %05u %04u %1d %1d %1d %05u %05u %05u 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 2951 | mName - AudioMixer::TRACK0, |
| 2952 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 2953 | mStreamType, |
| 2954 | mFormat, |
| 2955 | mCblk->channelCount, |
| 2956 | mSessionId, |
| 2957 | mFrameCount, |
| 2958 | mState, |
| 2959 | mMute, |
| 2960 | mFillingUpStatus, |
| 2961 | mCblk->sampleRate, |
| 2962 | mCblk->volume[0], |
| 2963 | mCblk->volume[1], |
| 2964 | mCblk->server, |
| 2965 | mCblk->user, |
| 2966 | (int)mMainBuffer, |
| 2967 | (int)mAuxBuffer); |
| 2968 | } |
| 2969 | |
| 2970 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 2971 | { |
| 2972 | audio_track_cblk_t* cblk = this->cblk(); |
| 2973 | uint32_t framesReady; |
| 2974 | uint32_t framesReq = buffer->frameCount; |
| 2975 | |
| 2976 | // Check if last stepServer failed, try to step now |
| 2977 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 2978 | if (!step()) goto getNextBuffer_exit; |
| 2979 | LOGV("stepServer recovered"); |
| 2980 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 2981 | } |
| 2982 | |
| 2983 | framesReady = cblk->framesReady(); |
| 2984 | |
| 2985 | if (LIKELY(framesReady)) { |
| 2986 | uint32_t s = cblk->server; |
| 2987 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 2988 | |
| 2989 | bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd; |
| 2990 | if (framesReq > framesReady) { |
| 2991 | framesReq = framesReady; |
| 2992 | } |
| 2993 | if (s + framesReq > bufferEnd) { |
| 2994 | framesReq = bufferEnd - s; |
| 2995 | } |
| 2996 | |
| 2997 | buffer->raw = getBuffer(s, framesReq); |
| 2998 | if (buffer->raw == 0) goto getNextBuffer_exit; |
| 2999 | |
| 3000 | buffer->frameCount = framesReq; |
| 3001 | return NO_ERROR; |
| 3002 | } |
| 3003 | |
| 3004 | getNextBuffer_exit: |
| 3005 | buffer->raw = 0; |
| 3006 | buffer->frameCount = 0; |
| 3007 | LOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get()); |
| 3008 | return NOT_ENOUGH_DATA; |
| 3009 | } |
| 3010 | |
| 3011 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
| 3012 | if (mFillingUpStatus != FS_FILLING) return true; |
| 3013 | |
| 3014 | if (mCblk->framesReady() >= mCblk->frameCount || |
| 3015 | (mCblk->flags & CBLK_FORCEREADY_MSK)) { |
| 3016 | mFillingUpStatus = FS_FILLED; |
| 3017 | mCblk->flags &= ~CBLK_FORCEREADY_MSK; |
| 3018 | return true; |
| 3019 | } |
| 3020 | return false; |
| 3021 | } |
| 3022 | |
| 3023 | status_t AudioFlinger::PlaybackThread::Track::start() |
| 3024 | { |
| 3025 | status_t status = NO_ERROR; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 3026 | LOGV("start(%d), calling thread %d session %d", |
| 3027 | mName, IPCThreadState::self()->getCallingPid(), mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3028 | sp<ThreadBase> thread = mThread.promote(); |
| 3029 | if (thread != 0) { |
| 3030 | Mutex::Autolock _l(thread->mLock); |
| 3031 | int state = mState; |
| 3032 | // here the track could be either new, or restarted |
| 3033 | // in both cases "unstop" the track |
| 3034 | if (mState == PAUSED) { |
| 3035 | mState = TrackBase::RESUMING; |
| 3036 | LOGV("PAUSED => RESUMING (%d) on thread %p", mName, this); |
| 3037 | } else { |
| 3038 | mState = TrackBase::ACTIVE; |
| 3039 | LOGV("? => ACTIVE (%d) on thread %p", mName, this); |
| 3040 | } |
| 3041 | |
| 3042 | if (!isOutputTrack() && state != ACTIVE && state != RESUMING) { |
| 3043 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3044 | status = AudioSystem::startOutput(thread->id(), |
| 3045 | (AudioSystem::stream_type)mStreamType, |
| 3046 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3047 | thread->mLock.lock(); |
| 3048 | } |
| 3049 | if (status == NO_ERROR) { |
| 3050 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3051 | playbackThread->addTrack_l(this); |
| 3052 | } else { |
| 3053 | mState = state; |
| 3054 | } |
| 3055 | } else { |
| 3056 | status = BAD_VALUE; |
| 3057 | } |
| 3058 | return status; |
| 3059 | } |
| 3060 | |
| 3061 | void AudioFlinger::PlaybackThread::Track::stop() |
| 3062 | { |
| 3063 | LOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
| 3064 | sp<ThreadBase> thread = mThread.promote(); |
| 3065 | if (thread != 0) { |
| 3066 | Mutex::Autolock _l(thread->mLock); |
| 3067 | int state = mState; |
| 3068 | if (mState > STOPPED) { |
| 3069 | mState = STOPPED; |
| 3070 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 3071 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3072 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 3073 | reset(); |
| 3074 | } |
| 3075 | LOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread); |
| 3076 | } |
| 3077 | if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) { |
| 3078 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3079 | AudioSystem::stopOutput(thread->id(), |
| 3080 | (AudioSystem::stream_type)mStreamType, |
| 3081 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3082 | thread->mLock.lock(); |
| 3083 | } |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | void AudioFlinger::PlaybackThread::Track::pause() |
| 3088 | { |
| 3089 | LOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid()); |
| 3090 | sp<ThreadBase> thread = mThread.promote(); |
| 3091 | if (thread != 0) { |
| 3092 | Mutex::Autolock _l(thread->mLock); |
| 3093 | if (mState == ACTIVE || mState == RESUMING) { |
| 3094 | mState = PAUSING; |
| 3095 | LOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get()); |
| 3096 | if (!isOutputTrack()) { |
| 3097 | thread->mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3098 | AudioSystem::stopOutput(thread->id(), |
| 3099 | (AudioSystem::stream_type)mStreamType, |
| 3100 | mSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3101 | thread->mLock.lock(); |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | void AudioFlinger::PlaybackThread::Track::flush() |
| 3108 | { |
| 3109 | LOGV("flush(%d)", mName); |
| 3110 | sp<ThreadBase> thread = mThread.promote(); |
| 3111 | if (thread != 0) { |
| 3112 | Mutex::Autolock _l(thread->mLock); |
| 3113 | if (mState != STOPPED && mState != PAUSED && mState != PAUSING) { |
| 3114 | return; |
| 3115 | } |
| 3116 | // No point remaining in PAUSED state after a flush => go to |
| 3117 | // STOPPED state |
| 3118 | mState = STOPPED; |
| 3119 | |
| 3120 | mCblk->lock.lock(); |
| 3121 | // NOTE: reset() will reset cblk->user and cblk->server with |
| 3122 | // the risk that at the same time, the AudioMixer is trying to read |
| 3123 | // data. In this case, getNextBuffer() would return a NULL pointer |
| 3124 | // as audio buffer => the AudioMixer code MUST always test that pointer |
| 3125 | // returned by getNextBuffer() is not NULL! |
| 3126 | reset(); |
| 3127 | mCblk->lock.unlock(); |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | void AudioFlinger::PlaybackThread::Track::reset() |
| 3132 | { |
| 3133 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 3134 | // the audioflinger thread detects the track is stopped. |
| 3135 | if (!mResetDone) { |
| 3136 | TrackBase::reset(); |
| 3137 | // Force underrun condition to avoid false underrun callback until first data is |
| 3138 | // written to buffer |
| 3139 | mCblk->flags |= CBLK_UNDERRUN_ON; |
| 3140 | mCblk->flags &= ~CBLK_FORCEREADY_MSK; |
| 3141 | mFillingUpStatus = FS_FILLING; |
| 3142 | mResetDone = true; |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | void AudioFlinger::PlaybackThread::Track::mute(bool muted) |
| 3147 | { |
| 3148 | mMute = muted; |
| 3149 | } |
| 3150 | |
| 3151 | void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right) |
| 3152 | { |
| 3153 | mVolume[0] = left; |
| 3154 | mVolume[1] = right; |
| 3155 | } |
| 3156 | |
| 3157 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 3158 | { |
| 3159 | status_t status = DEAD_OBJECT; |
| 3160 | sp<ThreadBase> thread = mThread.promote(); |
| 3161 | if (thread != 0) { |
| 3162 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 3163 | status = playbackThread->attachAuxEffect(this, EffectId); |
| 3164 | } |
| 3165 | return status; |
| 3166 | } |
| 3167 | |
| 3168 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 3169 | { |
| 3170 | mAuxEffectId = EffectId; |
| 3171 | mAuxBuffer = buffer; |
| 3172 | } |
| 3173 | |
| 3174 | // ---------------------------------------------------------------------------- |
| 3175 | |
| 3176 | // RecordTrack constructor must be called with AudioFlinger::mLock held |
| 3177 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 3178 | const wp<ThreadBase>& thread, |
| 3179 | const sp<Client>& client, |
| 3180 | uint32_t sampleRate, |
| 3181 | int format, |
| 3182 | int channelCount, |
| 3183 | int frameCount, |
| 3184 | uint32_t flags, |
| 3185 | int sessionId) |
| 3186 | : TrackBase(thread, client, sampleRate, format, |
| 3187 | channelCount, frameCount, flags, 0, sessionId), |
| 3188 | mOverflow(false) |
| 3189 | { |
| 3190 | if (mCblk != NULL) { |
| 3191 | LOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer); |
| 3192 | if (format == AudioSystem::PCM_16_BIT) { |
| 3193 | mCblk->frameSize = channelCount * sizeof(int16_t); |
| 3194 | } else if (format == AudioSystem::PCM_8_BIT) { |
| 3195 | mCblk->frameSize = channelCount * sizeof(int8_t); |
| 3196 | } else { |
| 3197 | mCblk->frameSize = sizeof(int8_t); |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 3203 | { |
| 3204 | sp<ThreadBase> thread = mThread.promote(); |
| 3205 | if (thread != 0) { |
| 3206 | AudioSystem::releaseInput(thread->id()); |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 3211 | { |
| 3212 | audio_track_cblk_t* cblk = this->cblk(); |
| 3213 | uint32_t framesAvail; |
| 3214 | uint32_t framesReq = buffer->frameCount; |
| 3215 | |
| 3216 | // Check if last stepServer failed, try to step now |
| 3217 | if (mFlags & TrackBase::STEPSERVER_FAILED) { |
| 3218 | if (!step()) goto getNextBuffer_exit; |
| 3219 | LOGV("stepServer recovered"); |
| 3220 | mFlags &= ~TrackBase::STEPSERVER_FAILED; |
| 3221 | } |
| 3222 | |
| 3223 | framesAvail = cblk->framesAvailable_l(); |
| 3224 | |
| 3225 | if (LIKELY(framesAvail)) { |
| 3226 | uint32_t s = cblk->server; |
| 3227 | uint32_t bufferEnd = cblk->serverBase + cblk->frameCount; |
| 3228 | |
| 3229 | if (framesReq > framesAvail) { |
| 3230 | framesReq = framesAvail; |
| 3231 | } |
| 3232 | if (s + framesReq > bufferEnd) { |
| 3233 | framesReq = bufferEnd - s; |
| 3234 | } |
| 3235 | |
| 3236 | buffer->raw = getBuffer(s, framesReq); |
| 3237 | if (buffer->raw == 0) goto getNextBuffer_exit; |
| 3238 | |
| 3239 | buffer->frameCount = framesReq; |
| 3240 | return NO_ERROR; |
| 3241 | } |
| 3242 | |
| 3243 | getNextBuffer_exit: |
| 3244 | buffer->raw = 0; |
| 3245 | buffer->frameCount = 0; |
| 3246 | return NOT_ENOUGH_DATA; |
| 3247 | } |
| 3248 | |
| 3249 | status_t AudioFlinger::RecordThread::RecordTrack::start() |
| 3250 | { |
| 3251 | sp<ThreadBase> thread = mThread.promote(); |
| 3252 | if (thread != 0) { |
| 3253 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3254 | return recordThread->start(this); |
| 3255 | } else { |
| 3256 | return BAD_VALUE; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 3261 | { |
| 3262 | sp<ThreadBase> thread = mThread.promote(); |
| 3263 | if (thread != 0) { |
| 3264 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 3265 | recordThread->stop(this); |
| 3266 | TrackBase::reset(); |
| 3267 | // Force overerrun condition to avoid false overrun callback until first data is |
| 3268 | // read from buffer |
| 3269 | mCblk->flags |= CBLK_UNDERRUN_ON; |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size) |
| 3274 | { |
| 3275 | snprintf(buffer, size, " %05d %03u %03u %05d %04u %01d %05u %08x %08x\n", |
| 3276 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 3277 | mFormat, |
| 3278 | mCblk->channelCount, |
| 3279 | mSessionId, |
| 3280 | mFrameCount, |
| 3281 | mState, |
| 3282 | mCblk->sampleRate, |
| 3283 | mCblk->server, |
| 3284 | mCblk->user); |
| 3285 | } |
| 3286 | |
| 3287 | |
| 3288 | // ---------------------------------------------------------------------------- |
| 3289 | |
| 3290 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 3291 | const wp<ThreadBase>& thread, |
| 3292 | DuplicatingThread *sourceThread, |
| 3293 | uint32_t sampleRate, |
| 3294 | int format, |
| 3295 | int channelCount, |
| 3296 | int frameCount) |
| 3297 | : Track(thread, NULL, AudioSystem::NUM_STREAM_TYPES, sampleRate, format, channelCount, frameCount, NULL, 0), |
| 3298 | mActive(false), mSourceThread(sourceThread) |
| 3299 | { |
| 3300 | |
| 3301 | PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get(); |
| 3302 | if (mCblk != NULL) { |
| 3303 | mCblk->flags |= CBLK_DIRECTION_OUT; |
| 3304 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 3305 | mCblk->volume[0] = mCblk->volume[1] = 0x1000; |
| 3306 | mOutBuffer.frameCount = 0; |
| 3307 | playbackThread->mTracks.add(this); |
| 3308 | LOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, mCblk->frameCount %d, mCblk->sampleRate %d, mCblk->channelCount %d mBufferEnd %p", |
| 3309 | mCblk, mBuffer, mCblk->buffers, mCblk->frameCount, mCblk->sampleRate, mCblk->channelCount, mBufferEnd); |
| 3310 | } else { |
| 3311 | LOGW("Error creating output track on thread %p", playbackThread); |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 3316 | { |
| 3317 | clearBufferQueue(); |
| 3318 | } |
| 3319 | |
| 3320 | status_t AudioFlinger::PlaybackThread::OutputTrack::start() |
| 3321 | { |
| 3322 | status_t status = Track::start(); |
| 3323 | if (status != NO_ERROR) { |
| 3324 | return status; |
| 3325 | } |
| 3326 | |
| 3327 | mActive = true; |
| 3328 | mRetryCount = 127; |
| 3329 | return status; |
| 3330 | } |
| 3331 | |
| 3332 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 3333 | { |
| 3334 | Track::stop(); |
| 3335 | clearBufferQueue(); |
| 3336 | mOutBuffer.frameCount = 0; |
| 3337 | mActive = false; |
| 3338 | } |
| 3339 | |
| 3340 | bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames) |
| 3341 | { |
| 3342 | Buffer *pInBuffer; |
| 3343 | Buffer inBuffer; |
| 3344 | uint32_t channelCount = mCblk->channelCount; |
| 3345 | bool outputBufferFull = false; |
| 3346 | inBuffer.frameCount = frames; |
| 3347 | inBuffer.i16 = data; |
| 3348 | |
| 3349 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 3350 | |
| 3351 | if (!mActive && frames != 0) { |
| 3352 | start(); |
| 3353 | sp<ThreadBase> thread = mThread.promote(); |
| 3354 | if (thread != 0) { |
| 3355 | MixerThread *mixerThread = (MixerThread *)thread.get(); |
| 3356 | if (mCblk->frameCount > frames){ |
| 3357 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3358 | uint32_t startFrames = (mCblk->frameCount - frames); |
| 3359 | pInBuffer = new Buffer; |
| 3360 | pInBuffer->mBuffer = new int16_t[startFrames * channelCount]; |
| 3361 | pInBuffer->frameCount = startFrames; |
| 3362 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3363 | memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t)); |
| 3364 | mBufferQueue.add(pInBuffer); |
| 3365 | } else { |
| 3366 | LOGW ("OutputTrack::write() %p no more buffers in queue", this); |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | while (waitTimeLeftMs) { |
| 3373 | // First write pending buffers, then new data |
| 3374 | if (mBufferQueue.size()) { |
| 3375 | pInBuffer = mBufferQueue.itemAt(0); |
| 3376 | } else { |
| 3377 | pInBuffer = &inBuffer; |
| 3378 | } |
| 3379 | |
| 3380 | if (pInBuffer->frameCount == 0) { |
| 3381 | break; |
| 3382 | } |
| 3383 | |
| 3384 | if (mOutBuffer.frameCount == 0) { |
| 3385 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 3386 | nsecs_t startTime = systemTime(); |
| 3387 | if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) { |
| 3388 | LOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get()); |
| 3389 | outputBufferFull = true; |
| 3390 | break; |
| 3391 | } |
| 3392 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 3393 | if (waitTimeLeftMs >= waitTimeMs) { |
| 3394 | waitTimeLeftMs -= waitTimeMs; |
| 3395 | } else { |
| 3396 | waitTimeLeftMs = 0; |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount; |
| 3401 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t)); |
| 3402 | mCblk->stepUser(outFrames); |
| 3403 | pInBuffer->frameCount -= outFrames; |
| 3404 | pInBuffer->i16 += outFrames * channelCount; |
| 3405 | mOutBuffer.frameCount -= outFrames; |
| 3406 | mOutBuffer.i16 += outFrames * channelCount; |
| 3407 | |
| 3408 | if (pInBuffer->frameCount == 0) { |
| 3409 | if (mBufferQueue.size()) { |
| 3410 | mBufferQueue.removeAt(0); |
| 3411 | delete [] pInBuffer->mBuffer; |
| 3412 | delete pInBuffer; |
| 3413 | LOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
| 3414 | } else { |
| 3415 | break; |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 3421 | if (inBuffer.frameCount) { |
| 3422 | sp<ThreadBase> thread = mThread.promote(); |
| 3423 | if (thread != 0 && !thread->standby()) { |
| 3424 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 3425 | pInBuffer = new Buffer; |
| 3426 | pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount]; |
| 3427 | pInBuffer->frameCount = inBuffer.frameCount; |
| 3428 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3429 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t)); |
| 3430 | mBufferQueue.add(pInBuffer); |
| 3431 | LOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size()); |
| 3432 | } else { |
| 3433 | LOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this); |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | // Calling write() with a 0 length buffer, means that no more data will be written: |
| 3439 | // If no more buffers are pending, fill output track buffer to make sure it is started |
| 3440 | // by output mixer. |
| 3441 | if (frames == 0 && mBufferQueue.size() == 0) { |
| 3442 | if (mCblk->user < mCblk->frameCount) { |
| 3443 | frames = mCblk->frameCount - mCblk->user; |
| 3444 | pInBuffer = new Buffer; |
| 3445 | pInBuffer->mBuffer = new int16_t[frames * channelCount]; |
| 3446 | pInBuffer->frameCount = frames; |
| 3447 | pInBuffer->i16 = pInBuffer->mBuffer; |
| 3448 | memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t)); |
| 3449 | mBufferQueue.add(pInBuffer); |
| 3450 | } else if (mActive) { |
| 3451 | stop(); |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | return outputBufferFull; |
| 3456 | } |
| 3457 | |
| 3458 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 3459 | { |
| 3460 | int active; |
| 3461 | status_t result; |
| 3462 | audio_track_cblk_t* cblk = mCblk; |
| 3463 | uint32_t framesReq = buffer->frameCount; |
| 3464 | |
| 3465 | // LOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server); |
| 3466 | buffer->frameCount = 0; |
| 3467 | |
| 3468 | uint32_t framesAvail = cblk->framesAvailable(); |
| 3469 | |
| 3470 | |
| 3471 | if (framesAvail == 0) { |
| 3472 | Mutex::Autolock _l(cblk->lock); |
| 3473 | goto start_loop_here; |
| 3474 | while (framesAvail == 0) { |
| 3475 | active = mActive; |
| 3476 | if (UNLIKELY(!active)) { |
| 3477 | LOGV("Not active and NO_MORE_BUFFERS"); |
| 3478 | return AudioTrack::NO_MORE_BUFFERS; |
| 3479 | } |
| 3480 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 3481 | if (result != NO_ERROR) { |
| 3482 | return AudioTrack::NO_MORE_BUFFERS; |
| 3483 | } |
| 3484 | // read the server count again |
| 3485 | start_loop_here: |
| 3486 | framesAvail = cblk->framesAvailable_l(); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | // if (framesAvail < framesReq) { |
| 3491 | // return AudioTrack::NO_MORE_BUFFERS; |
| 3492 | // } |
| 3493 | |
| 3494 | if (framesReq > framesAvail) { |
| 3495 | framesReq = framesAvail; |
| 3496 | } |
| 3497 | |
| 3498 | uint32_t u = cblk->user; |
| 3499 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 3500 | |
| 3501 | if (u + framesReq > bufferEnd) { |
| 3502 | framesReq = bufferEnd - u; |
| 3503 | } |
| 3504 | |
| 3505 | buffer->frameCount = framesReq; |
| 3506 | buffer->raw = (void *)cblk->buffer(u); |
| 3507 | return NO_ERROR; |
| 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 3512 | { |
| 3513 | size_t size = mBufferQueue.size(); |
| 3514 | Buffer *pBuffer; |
| 3515 | |
| 3516 | for (size_t i = 0; i < size; i++) { |
| 3517 | pBuffer = mBufferQueue.itemAt(i); |
| 3518 | delete [] pBuffer->mBuffer; |
| 3519 | delete pBuffer; |
| 3520 | } |
| 3521 | mBufferQueue.clear(); |
| 3522 | } |
| 3523 | |
| 3524 | // ---------------------------------------------------------------------------- |
| 3525 | |
| 3526 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 3527 | : RefBase(), |
| 3528 | mAudioFlinger(audioFlinger), |
| 3529 | mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), |
| 3530 | mPid(pid) |
| 3531 | { |
| 3532 | // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer |
| 3533 | } |
| 3534 | |
| 3535 | // Client destructor must be called with AudioFlinger::mLock held |
| 3536 | AudioFlinger::Client::~Client() |
| 3537 | { |
| 3538 | mAudioFlinger->removeClient_l(mPid); |
| 3539 | } |
| 3540 | |
| 3541 | const sp<MemoryDealer>& AudioFlinger::Client::heap() const |
| 3542 | { |
| 3543 | return mMemoryDealer; |
| 3544 | } |
| 3545 | |
| 3546 | // ---------------------------------------------------------------------------- |
| 3547 | |
| 3548 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 3549 | const sp<IAudioFlingerClient>& client, |
| 3550 | pid_t pid) |
| 3551 | : mAudioFlinger(audioFlinger), mPid(pid), mClient(client) |
| 3552 | { |
| 3553 | } |
| 3554 | |
| 3555 | AudioFlinger::NotificationClient::~NotificationClient() |
| 3556 | { |
| 3557 | mClient.clear(); |
| 3558 | } |
| 3559 | |
| 3560 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) |
| 3561 | { |
| 3562 | sp<NotificationClient> keep(this); |
| 3563 | { |
| 3564 | mAudioFlinger->removeNotificationClient(mPid); |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | // ---------------------------------------------------------------------------- |
| 3569 | |
| 3570 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 3571 | : BnAudioTrack(), |
| 3572 | mTrack(track) |
| 3573 | { |
| 3574 | } |
| 3575 | |
| 3576 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 3577 | // just stop the track on deletion, associated resources |
| 3578 | // will be freed from the main thread once all pending buffers have |
| 3579 | // been played. Unless it's not in the active track list, in which |
| 3580 | // case we free everything now... |
| 3581 | mTrack->destroy(); |
| 3582 | } |
| 3583 | |
| 3584 | status_t AudioFlinger::TrackHandle::start() { |
| 3585 | return mTrack->start(); |
| 3586 | } |
| 3587 | |
| 3588 | void AudioFlinger::TrackHandle::stop() { |
| 3589 | mTrack->stop(); |
| 3590 | } |
| 3591 | |
| 3592 | void AudioFlinger::TrackHandle::flush() { |
| 3593 | mTrack->flush(); |
| 3594 | } |
| 3595 | |
| 3596 | void AudioFlinger::TrackHandle::mute(bool e) { |
| 3597 | mTrack->mute(e); |
| 3598 | } |
| 3599 | |
| 3600 | void AudioFlinger::TrackHandle::pause() { |
| 3601 | mTrack->pause(); |
| 3602 | } |
| 3603 | |
| 3604 | void AudioFlinger::TrackHandle::setVolume(float left, float right) { |
| 3605 | mTrack->setVolume(left, right); |
| 3606 | } |
| 3607 | |
| 3608 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 3609 | return mTrack->getCblk(); |
| 3610 | } |
| 3611 | |
| 3612 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 3613 | { |
| 3614 | return mTrack->attachAuxEffect(EffectId); |
| 3615 | } |
| 3616 | |
| 3617 | status_t AudioFlinger::TrackHandle::onTransact( |
| 3618 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 3619 | { |
| 3620 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 3621 | } |
| 3622 | |
| 3623 | // ---------------------------------------------------------------------------- |
| 3624 | |
| 3625 | sp<IAudioRecord> AudioFlinger::openRecord( |
| 3626 | pid_t pid, |
| 3627 | int input, |
| 3628 | uint32_t sampleRate, |
| 3629 | int format, |
| 3630 | int channelCount, |
| 3631 | int frameCount, |
| 3632 | uint32_t flags, |
| 3633 | int *sessionId, |
| 3634 | status_t *status) |
| 3635 | { |
| 3636 | sp<RecordThread::RecordTrack> recordTrack; |
| 3637 | sp<RecordHandle> recordHandle; |
| 3638 | sp<Client> client; |
| 3639 | wp<Client> wclient; |
| 3640 | status_t lStatus; |
| 3641 | RecordThread *thread; |
| 3642 | size_t inFrameCount; |
| 3643 | int lSessionId; |
| 3644 | |
| 3645 | // check calling permissions |
| 3646 | if (!recordingAllowed()) { |
| 3647 | lStatus = PERMISSION_DENIED; |
| 3648 | goto Exit; |
| 3649 | } |
| 3650 | |
| 3651 | // add client to list |
| 3652 | { // scope for mLock |
| 3653 | Mutex::Autolock _l(mLock); |
| 3654 | thread = checkRecordThread_l(input); |
| 3655 | if (thread == NULL) { |
| 3656 | lStatus = BAD_VALUE; |
| 3657 | goto Exit; |
| 3658 | } |
| 3659 | |
| 3660 | wclient = mClients.valueFor(pid); |
| 3661 | if (wclient != NULL) { |
| 3662 | client = wclient.promote(); |
| 3663 | } else { |
| 3664 | client = new Client(this, pid); |
| 3665 | mClients.add(pid, client); |
| 3666 | } |
| 3667 | |
| 3668 | // If no audio session id is provided, create one here |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3669 | if (sessionId != NULL && *sessionId != AudioSystem::SESSION_OUTPUT_MIX) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3670 | lSessionId = *sessionId; |
| 3671 | } else { |
| 3672 | lSessionId = nextUniqueId(); |
| 3673 | if (sessionId != NULL) { |
| 3674 | *sessionId = lSessionId; |
| 3675 | } |
| 3676 | } |
| 3677 | // create new record track. The record track uses one track in mHardwareMixerThread by convention. |
| 3678 | recordTrack = new RecordThread::RecordTrack(thread, client, sampleRate, |
| 3679 | format, channelCount, frameCount, flags, lSessionId); |
| 3680 | } |
| 3681 | if (recordTrack->getCblk() == NULL) { |
| 3682 | // remove local strong reference to Client before deleting the RecordTrack so that the Client |
| 3683 | // destructor is called by the TrackBase destructor with mLock held |
| 3684 | client.clear(); |
| 3685 | recordTrack.clear(); |
| 3686 | lStatus = NO_MEMORY; |
| 3687 | goto Exit; |
| 3688 | } |
| 3689 | |
| 3690 | // return to handle to client |
| 3691 | recordHandle = new RecordHandle(recordTrack); |
| 3692 | lStatus = NO_ERROR; |
| 3693 | |
| 3694 | Exit: |
| 3695 | if (status) { |
| 3696 | *status = lStatus; |
| 3697 | } |
| 3698 | return recordHandle; |
| 3699 | } |
| 3700 | |
| 3701 | // ---------------------------------------------------------------------------- |
| 3702 | |
| 3703 | AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 3704 | : BnAudioRecord(), |
| 3705 | mRecordTrack(recordTrack) |
| 3706 | { |
| 3707 | } |
| 3708 | |
| 3709 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 3710 | stop(); |
| 3711 | } |
| 3712 | |
| 3713 | status_t AudioFlinger::RecordHandle::start() { |
| 3714 | LOGV("RecordHandle::start()"); |
| 3715 | return mRecordTrack->start(); |
| 3716 | } |
| 3717 | |
| 3718 | void AudioFlinger::RecordHandle::stop() { |
| 3719 | LOGV("RecordHandle::stop()"); |
| 3720 | mRecordTrack->stop(); |
| 3721 | } |
| 3722 | |
| 3723 | sp<IMemory> AudioFlinger::RecordHandle::getCblk() const { |
| 3724 | return mRecordTrack->getCblk(); |
| 3725 | } |
| 3726 | |
| 3727 | status_t AudioFlinger::RecordHandle::onTransact( |
| 3728 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 3729 | { |
| 3730 | return BnAudioRecord::onTransact(code, data, reply, flags); |
| 3731 | } |
| 3732 | |
| 3733 | // ---------------------------------------------------------------------------- |
| 3734 | |
| 3735 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, AudioStreamIn *input, uint32_t sampleRate, uint32_t channels, int id) : |
| 3736 | ThreadBase(audioFlinger, id), |
| 3737 | mInput(input), mResampler(0), mRsmpOutBuffer(0), mRsmpInBuffer(0) |
| 3738 | { |
| 3739 | mReqChannelCount = AudioSystem::popCount(channels); |
| 3740 | mReqSampleRate = sampleRate; |
| 3741 | readInputParameters(); |
| 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | AudioFlinger::RecordThread::~RecordThread() |
| 3746 | { |
| 3747 | delete[] mRsmpInBuffer; |
| 3748 | if (mResampler != 0) { |
| 3749 | delete mResampler; |
| 3750 | delete[] mRsmpOutBuffer; |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | void AudioFlinger::RecordThread::onFirstRef() |
| 3755 | { |
| 3756 | const size_t SIZE = 256; |
| 3757 | char buffer[SIZE]; |
| 3758 | |
| 3759 | snprintf(buffer, SIZE, "Record Thread %p", this); |
| 3760 | |
| 3761 | run(buffer, PRIORITY_URGENT_AUDIO); |
| 3762 | } |
| 3763 | |
| 3764 | bool AudioFlinger::RecordThread::threadLoop() |
| 3765 | { |
| 3766 | AudioBufferProvider::Buffer buffer; |
| 3767 | sp<RecordTrack> activeTrack; |
| 3768 | |
| 3769 | // start recording |
| 3770 | while (!exitPending()) { |
| 3771 | |
| 3772 | processConfigEvents(); |
| 3773 | |
| 3774 | { // scope for mLock |
| 3775 | Mutex::Autolock _l(mLock); |
| 3776 | checkForNewParameters_l(); |
| 3777 | if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { |
| 3778 | if (!mStandby) { |
| 3779 | mInput->standby(); |
| 3780 | mStandby = true; |
| 3781 | } |
| 3782 | |
| 3783 | if (exitPending()) break; |
| 3784 | |
| 3785 | LOGV("RecordThread: loop stopping"); |
| 3786 | // go to sleep |
| 3787 | mWaitWorkCV.wait(mLock); |
| 3788 | LOGV("RecordThread: loop starting"); |
| 3789 | continue; |
| 3790 | } |
| 3791 | if (mActiveTrack != 0) { |
| 3792 | if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 3793 | if (!mStandby) { |
| 3794 | mInput->standby(); |
| 3795 | mStandby = true; |
| 3796 | } |
| 3797 | mActiveTrack.clear(); |
| 3798 | mStartStopCond.broadcast(); |
| 3799 | } else if (mActiveTrack->mState == TrackBase::RESUMING) { |
| 3800 | if (mReqChannelCount != mActiveTrack->channelCount()) { |
| 3801 | mActiveTrack.clear(); |
| 3802 | mStartStopCond.broadcast(); |
| 3803 | } else if (mBytesRead != 0) { |
| 3804 | // record start succeeds only if first read from audio input |
| 3805 | // succeeds |
| 3806 | if (mBytesRead > 0) { |
| 3807 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 3808 | } else { |
| 3809 | mActiveTrack.clear(); |
| 3810 | } |
| 3811 | mStartStopCond.broadcast(); |
| 3812 | } |
| 3813 | mStandby = false; |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | if (mActiveTrack != 0) { |
| 3819 | if (mActiveTrack->mState != TrackBase::ACTIVE && |
| 3820 | mActiveTrack->mState != TrackBase::RESUMING) { |
| 3821 | usleep(5000); |
| 3822 | continue; |
| 3823 | } |
| 3824 | buffer.frameCount = mFrameCount; |
| 3825 | if (LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) { |
| 3826 | size_t framesOut = buffer.frameCount; |
| 3827 | if (mResampler == 0) { |
| 3828 | // no resampling |
| 3829 | while (framesOut) { |
| 3830 | size_t framesIn = mFrameCount - mRsmpInIndex; |
| 3831 | if (framesIn) { |
| 3832 | int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; |
| 3833 | int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize; |
| 3834 | if (framesIn > framesOut) |
| 3835 | framesIn = framesOut; |
| 3836 | mRsmpInIndex += framesIn; |
| 3837 | framesOut -= framesIn; |
| 3838 | if ((int)mChannelCount == mReqChannelCount || |
| 3839 | mFormat != AudioSystem::PCM_16_BIT) { |
| 3840 | memcpy(dst, src, framesIn * mFrameSize); |
| 3841 | } else { |
| 3842 | int16_t *src16 = (int16_t *)src; |
| 3843 | int16_t *dst16 = (int16_t *)dst; |
| 3844 | if (mChannelCount == 1) { |
| 3845 | while (framesIn--) { |
| 3846 | *dst16++ = *src16; |
| 3847 | *dst16++ = *src16++; |
| 3848 | } |
| 3849 | } else { |
| 3850 | while (framesIn--) { |
| 3851 | *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1); |
| 3852 | src16 += 2; |
| 3853 | } |
| 3854 | } |
| 3855 | } |
| 3856 | } |
| 3857 | if (framesOut && mFrameCount == mRsmpInIndex) { |
| 3858 | if (framesOut == mFrameCount && |
| 3859 | ((int)mChannelCount == mReqChannelCount || mFormat != AudioSystem::PCM_16_BIT)) { |
| 3860 | mBytesRead = mInput->read(buffer.raw, mInputBytes); |
| 3861 | framesOut = 0; |
| 3862 | } else { |
| 3863 | mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes); |
| 3864 | mRsmpInIndex = 0; |
| 3865 | } |
| 3866 | if (mBytesRead < 0) { |
| 3867 | LOGE("Error reading audio input"); |
| 3868 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 3869 | // Force input into standby so that it tries to |
| 3870 | // recover at next read attempt |
| 3871 | mInput->standby(); |
| 3872 | usleep(5000); |
| 3873 | } |
| 3874 | mRsmpInIndex = mFrameCount; |
| 3875 | framesOut = 0; |
| 3876 | buffer.frameCount = 0; |
| 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | } else { |
| 3881 | // resampling |
| 3882 | |
| 3883 | memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); |
| 3884 | // alter output frame count as if we were expecting stereo samples |
| 3885 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 3886 | framesOut >>= 1; |
| 3887 | } |
| 3888 | mResampler->resample(mRsmpOutBuffer, framesOut, this); |
| 3889 | // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer() |
| 3890 | // are 32 bit aligned which should be always true. |
| 3891 | if (mChannelCount == 2 && mReqChannelCount == 1) { |
| 3892 | AudioMixer::ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); |
| 3893 | // the resampler always outputs stereo samples: do post stereo to mono conversion |
| 3894 | int16_t *src = (int16_t *)mRsmpOutBuffer; |
| 3895 | int16_t *dst = buffer.i16; |
| 3896 | while (framesOut--) { |
| 3897 | *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1); |
| 3898 | src += 2; |
| 3899 | } |
| 3900 | } else { |
| 3901 | AudioMixer::ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); |
| 3902 | } |
| 3903 | |
| 3904 | } |
| 3905 | mActiveTrack->releaseBuffer(&buffer); |
| 3906 | mActiveTrack->overflow(); |
| 3907 | } |
| 3908 | // client isn't retrieving buffers fast enough |
| 3909 | else { |
| 3910 | if (!mActiveTrack->setOverflow()) |
| 3911 | LOGW("RecordThread: buffer overflow"); |
| 3912 | // Release the processor for a while before asking for a new buffer. |
| 3913 | // This will give the application more chance to read from the buffer and |
| 3914 | // clear the overflow. |
| 3915 | usleep(5000); |
| 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | |
| 3920 | if (!mStandby) { |
| 3921 | mInput->standby(); |
| 3922 | } |
| 3923 | mActiveTrack.clear(); |
| 3924 | |
| 3925 | mStartStopCond.broadcast(); |
| 3926 | |
| 3927 | LOGV("RecordThread %p exiting", this); |
| 3928 | return false; |
| 3929 | } |
| 3930 | |
| 3931 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack) |
| 3932 | { |
| 3933 | LOGV("RecordThread::start"); |
| 3934 | sp <ThreadBase> strongMe = this; |
| 3935 | status_t status = NO_ERROR; |
| 3936 | { |
| 3937 | AutoMutex lock(&mLock); |
| 3938 | if (mActiveTrack != 0) { |
| 3939 | if (recordTrack != mActiveTrack.get()) { |
| 3940 | status = -EBUSY; |
| 3941 | } else if (mActiveTrack->mState == TrackBase::PAUSING) { |
| 3942 | mActiveTrack->mState = TrackBase::ACTIVE; |
| 3943 | } |
| 3944 | return status; |
| 3945 | } |
| 3946 | |
| 3947 | recordTrack->mState = TrackBase::IDLE; |
| 3948 | mActiveTrack = recordTrack; |
| 3949 | mLock.unlock(); |
| 3950 | status_t status = AudioSystem::startInput(mId); |
| 3951 | mLock.lock(); |
| 3952 | if (status != NO_ERROR) { |
| 3953 | mActiveTrack.clear(); |
| 3954 | return status; |
| 3955 | } |
| 3956 | mActiveTrack->mState = TrackBase::RESUMING; |
| 3957 | mRsmpInIndex = mFrameCount; |
| 3958 | mBytesRead = 0; |
| 3959 | // signal thread to start |
| 3960 | LOGV("Signal record thread"); |
| 3961 | mWaitWorkCV.signal(); |
| 3962 | // do not wait for mStartStopCond if exiting |
| 3963 | if (mExiting) { |
| 3964 | mActiveTrack.clear(); |
| 3965 | status = INVALID_OPERATION; |
| 3966 | goto startError; |
| 3967 | } |
| 3968 | mStartStopCond.wait(mLock); |
| 3969 | if (mActiveTrack == 0) { |
| 3970 | LOGV("Record failed to start"); |
| 3971 | status = BAD_VALUE; |
| 3972 | goto startError; |
| 3973 | } |
| 3974 | LOGV("Record started OK"); |
| 3975 | return status; |
| 3976 | } |
| 3977 | startError: |
| 3978 | AudioSystem::stopInput(mId); |
| 3979 | return status; |
| 3980 | } |
| 3981 | |
| 3982 | void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
| 3983 | LOGV("RecordThread::stop"); |
| 3984 | sp <ThreadBase> strongMe = this; |
| 3985 | { |
| 3986 | AutoMutex lock(&mLock); |
| 3987 | if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) { |
| 3988 | mActiveTrack->mState = TrackBase::PAUSING; |
| 3989 | // do not wait for mStartStopCond if exiting |
| 3990 | if (mExiting) { |
| 3991 | return; |
| 3992 | } |
| 3993 | mStartStopCond.wait(mLock); |
| 3994 | // if we have been restarted, recordTrack == mActiveTrack.get() here |
| 3995 | if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) { |
| 3996 | mLock.unlock(); |
| 3997 | AudioSystem::stopInput(mId); |
| 3998 | mLock.lock(); |
| 3999 | LOGV("Record stopped OK"); |
| 4000 | } |
| 4001 | } |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 4006 | { |
| 4007 | const size_t SIZE = 256; |
| 4008 | char buffer[SIZE]; |
| 4009 | String8 result; |
| 4010 | pid_t pid = 0; |
| 4011 | |
| 4012 | snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); |
| 4013 | result.append(buffer); |
| 4014 | |
| 4015 | if (mActiveTrack != 0) { |
| 4016 | result.append("Active Track:\n"); |
| 4017 | result.append(" Clien Fmt Chn Session Buf S SRate Serv User\n"); |
| 4018 | mActiveTrack->dump(buffer, SIZE); |
| 4019 | result.append(buffer); |
| 4020 | |
| 4021 | snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); |
| 4022 | result.append(buffer); |
| 4023 | snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); |
| 4024 | result.append(buffer); |
| 4025 | snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != 0)); |
| 4026 | result.append(buffer); |
| 4027 | snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount); |
| 4028 | result.append(buffer); |
| 4029 | snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate); |
| 4030 | result.append(buffer); |
| 4031 | |
| 4032 | |
| 4033 | } else { |
| 4034 | result.append("No record client\n"); |
| 4035 | } |
| 4036 | write(fd, result.string(), result.size()); |
| 4037 | |
| 4038 | dumpBase(fd, args); |
| 4039 | |
| 4040 | return NO_ERROR; |
| 4041 | } |
| 4042 | |
| 4043 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 4044 | { |
| 4045 | size_t framesReq = buffer->frameCount; |
| 4046 | size_t framesReady = mFrameCount - mRsmpInIndex; |
| 4047 | int channelCount; |
| 4048 | |
| 4049 | if (framesReady == 0) { |
| 4050 | mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes); |
| 4051 | if (mBytesRead < 0) { |
| 4052 | LOGE("RecordThread::getNextBuffer() Error reading audio input"); |
| 4053 | if (mActiveTrack->mState == TrackBase::ACTIVE) { |
| 4054 | // Force input into standby so that it tries to |
| 4055 | // recover at next read attempt |
| 4056 | mInput->standby(); |
| 4057 | usleep(5000); |
| 4058 | } |
| 4059 | buffer->raw = 0; |
| 4060 | buffer->frameCount = 0; |
| 4061 | return NOT_ENOUGH_DATA; |
| 4062 | } |
| 4063 | mRsmpInIndex = 0; |
| 4064 | framesReady = mFrameCount; |
| 4065 | } |
| 4066 | |
| 4067 | if (framesReq > framesReady) { |
| 4068 | framesReq = framesReady; |
| 4069 | } |
| 4070 | |
| 4071 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4072 | channelCount = 1; |
| 4073 | } else { |
| 4074 | channelCount = 2; |
| 4075 | } |
| 4076 | buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; |
| 4077 | buffer->frameCount = framesReq; |
| 4078 | return NO_ERROR; |
| 4079 | } |
| 4080 | |
| 4081 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 4082 | { |
| 4083 | mRsmpInIndex += buffer->frameCount; |
| 4084 | buffer->frameCount = 0; |
| 4085 | } |
| 4086 | |
| 4087 | bool AudioFlinger::RecordThread::checkForNewParameters_l() |
| 4088 | { |
| 4089 | bool reconfig = false; |
| 4090 | |
| 4091 | while (!mNewParameters.isEmpty()) { |
| 4092 | status_t status = NO_ERROR; |
| 4093 | String8 keyValuePair = mNewParameters[0]; |
| 4094 | AudioParameter param = AudioParameter(keyValuePair); |
| 4095 | int value; |
| 4096 | int reqFormat = mFormat; |
| 4097 | int reqSamplingRate = mReqSampleRate; |
| 4098 | int reqChannelCount = mReqChannelCount; |
| 4099 | |
| 4100 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4101 | reqSamplingRate = value; |
| 4102 | reconfig = true; |
| 4103 | } |
| 4104 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 4105 | reqFormat = value; |
| 4106 | reconfig = true; |
| 4107 | } |
| 4108 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 4109 | reqChannelCount = AudioSystem::popCount(value); |
| 4110 | reconfig = true; |
| 4111 | } |
| 4112 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4113 | // do not accept frame count changes if tracks are open as the track buffer |
| 4114 | // size depends on frame count and correct behavior would not be garantied |
| 4115 | // if frame count is changed after track creation |
| 4116 | if (mActiveTrack != 0) { |
| 4117 | status = INVALID_OPERATION; |
| 4118 | } else { |
| 4119 | reconfig = true; |
| 4120 | } |
| 4121 | } |
| 4122 | if (status == NO_ERROR) { |
| 4123 | status = mInput->setParameters(keyValuePair); |
| 4124 | if (status == INVALID_OPERATION) { |
| 4125 | mInput->standby(); |
| 4126 | status = mInput->setParameters(keyValuePair); |
| 4127 | } |
| 4128 | if (reconfig) { |
| 4129 | if (status == BAD_VALUE && |
| 4130 | reqFormat == mInput->format() && reqFormat == AudioSystem::PCM_16_BIT && |
| 4131 | ((int)mInput->sampleRate() <= 2 * reqSamplingRate) && |
| 4132 | (AudioSystem::popCount(mInput->channels()) < 3) && (reqChannelCount < 3)) { |
| 4133 | status = NO_ERROR; |
| 4134 | } |
| 4135 | if (status == NO_ERROR) { |
| 4136 | readInputParameters(); |
| 4137 | sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
| 4138 | } |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | mNewParameters.removeAt(0); |
| 4143 | |
| 4144 | mParamStatus = status; |
| 4145 | mParamCond.signal(); |
| 4146 | mWaitWorkCV.wait(mLock); |
| 4147 | } |
| 4148 | return reconfig; |
| 4149 | } |
| 4150 | |
| 4151 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 4152 | { |
| 4153 | return mInput->getParameters(keys); |
| 4154 | } |
| 4155 | |
| 4156 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { |
| 4157 | AudioSystem::OutputDescriptor desc; |
| 4158 | void *param2 = 0; |
| 4159 | |
| 4160 | switch (event) { |
| 4161 | case AudioSystem::INPUT_OPENED: |
| 4162 | case AudioSystem::INPUT_CONFIG_CHANGED: |
| 4163 | desc.channels = mChannels; |
| 4164 | desc.samplingRate = mSampleRate; |
| 4165 | desc.format = mFormat; |
| 4166 | desc.frameCount = mFrameCount; |
| 4167 | desc.latency = 0; |
| 4168 | param2 = &desc; |
| 4169 | break; |
| 4170 | |
| 4171 | case AudioSystem::INPUT_CLOSED: |
| 4172 | default: |
| 4173 | break; |
| 4174 | } |
| 4175 | mAudioFlinger->audioConfigChanged_l(event, mId, param2); |
| 4176 | } |
| 4177 | |
| 4178 | void AudioFlinger::RecordThread::readInputParameters() |
| 4179 | { |
| 4180 | if (mRsmpInBuffer) delete mRsmpInBuffer; |
| 4181 | if (mRsmpOutBuffer) delete mRsmpOutBuffer; |
| 4182 | if (mResampler) delete mResampler; |
| 4183 | mResampler = 0; |
| 4184 | |
| 4185 | mSampleRate = mInput->sampleRate(); |
| 4186 | mChannels = mInput->channels(); |
| 4187 | mChannelCount = (uint16_t)AudioSystem::popCount(mChannels); |
| 4188 | mFormat = mInput->format(); |
| 4189 | mFrameSize = (uint16_t)mInput->frameSize(); |
| 4190 | mInputBytes = mInput->bufferSize(); |
| 4191 | mFrameCount = mInputBytes / mFrameSize; |
| 4192 | mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; |
| 4193 | |
| 4194 | if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3) |
| 4195 | { |
| 4196 | int channelCount; |
| 4197 | // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid |
| 4198 | // stereo to mono post process as the resampler always outputs stereo. |
| 4199 | if (mChannelCount == 1 && mReqChannelCount == 2) { |
| 4200 | channelCount = 1; |
| 4201 | } else { |
| 4202 | channelCount = 2; |
| 4203 | } |
| 4204 | mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); |
| 4205 | mResampler->setSampleRate(mSampleRate); |
| 4206 | mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); |
| 4207 | mRsmpOutBuffer = new int32_t[mFrameCount * 2]; |
| 4208 | |
| 4209 | // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples |
| 4210 | if (mChannelCount == 1 && mReqChannelCount == 1) { |
| 4211 | mFrameCount >>= 1; |
| 4212 | } |
| 4213 | |
| 4214 | } |
| 4215 | mRsmpInIndex = mFrameCount; |
| 4216 | } |
| 4217 | |
| 4218 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() |
| 4219 | { |
| 4220 | return mInput->getInputFramesLost(); |
| 4221 | } |
| 4222 | |
| 4223 | // ---------------------------------------------------------------------------- |
| 4224 | |
| 4225 | int AudioFlinger::openOutput(uint32_t *pDevices, |
| 4226 | uint32_t *pSamplingRate, |
| 4227 | uint32_t *pFormat, |
| 4228 | uint32_t *pChannels, |
| 4229 | uint32_t *pLatencyMs, |
| 4230 | uint32_t flags) |
| 4231 | { |
| 4232 | status_t status; |
| 4233 | PlaybackThread *thread = NULL; |
| 4234 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 4235 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 4236 | uint32_t format = pFormat ? *pFormat : 0; |
| 4237 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4238 | uint32_t latency = pLatencyMs ? *pLatencyMs : 0; |
| 4239 | |
| 4240 | LOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x", |
| 4241 | pDevices ? *pDevices : 0, |
| 4242 | samplingRate, |
| 4243 | format, |
| 4244 | channels, |
| 4245 | flags); |
| 4246 | |
| 4247 | if (pDevices == NULL || *pDevices == 0) { |
| 4248 | return 0; |
| 4249 | } |
| 4250 | Mutex::Autolock _l(mLock); |
| 4251 | |
| 4252 | AudioStreamOut *output = mAudioHardware->openOutputStream(*pDevices, |
| 4253 | (int *)&format, |
| 4254 | &channels, |
| 4255 | &samplingRate, |
| 4256 | &status); |
| 4257 | LOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d", |
| 4258 | output, |
| 4259 | samplingRate, |
| 4260 | format, |
| 4261 | channels, |
| 4262 | status); |
| 4263 | |
| 4264 | mHardwareStatus = AUDIO_HW_IDLE; |
| 4265 | if (output != 0) { |
| 4266 | int id = nextUniqueId(); |
| 4267 | if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) || |
| 4268 | (format != AudioSystem::PCM_16_BIT) || |
| 4269 | (channels != AudioSystem::CHANNEL_OUT_STEREO)) { |
| 4270 | thread = new DirectOutputThread(this, output, id, *pDevices); |
| 4271 | LOGV("openOutput() created direct output: ID %d thread %p", id, thread); |
| 4272 | } else { |
| 4273 | thread = new MixerThread(this, output, id, *pDevices); |
| 4274 | LOGV("openOutput() created mixer output: ID %d thread %p", id, thread); |
| 4275 | |
| 4276 | #ifdef LVMX |
| 4277 | unsigned bitsPerSample = |
| 4278 | (format == AudioSystem::PCM_16_BIT) ? 16 : |
| 4279 | ((format == AudioSystem::PCM_8_BIT) ? 8 : 0); |
| 4280 | unsigned channelCount = (channels == AudioSystem::CHANNEL_OUT_STEREO) ? 2 : 1; |
| 4281 | int audioOutputType = LifeVibes::threadIdToAudioOutputType(thread->id()); |
| 4282 | |
| 4283 | LifeVibes::init_aot(audioOutputType, samplingRate, bitsPerSample, channelCount); |
| 4284 | LifeVibes::setDevice(audioOutputType, *pDevices); |
| 4285 | #endif |
| 4286 | |
| 4287 | } |
| 4288 | mPlaybackThreads.add(id, thread); |
| 4289 | |
| 4290 | if (pSamplingRate) *pSamplingRate = samplingRate; |
| 4291 | if (pFormat) *pFormat = format; |
| 4292 | if (pChannels) *pChannels = channels; |
| 4293 | if (pLatencyMs) *pLatencyMs = thread->latency(); |
| 4294 | |
| 4295 | // notify client processes of the new output creation |
| 4296 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 4297 | return id; |
| 4298 | } |
| 4299 | |
| 4300 | return 0; |
| 4301 | } |
| 4302 | |
| 4303 | int AudioFlinger::openDuplicateOutput(int output1, int output2) |
| 4304 | { |
| 4305 | Mutex::Autolock _l(mLock); |
| 4306 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 4307 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 4308 | |
| 4309 | if (thread1 == NULL || thread2 == NULL) { |
| 4310 | LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2); |
| 4311 | return 0; |
| 4312 | } |
| 4313 | |
| 4314 | int id = nextUniqueId(); |
| 4315 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); |
| 4316 | thread->addOutputTrack(thread2); |
| 4317 | mPlaybackThreads.add(id, thread); |
| 4318 | // notify client processes of the new output creation |
| 4319 | thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); |
| 4320 | return id; |
| 4321 | } |
| 4322 | |
| 4323 | status_t AudioFlinger::closeOutput(int output) |
| 4324 | { |
| 4325 | // keep strong reference on the playback thread so that |
| 4326 | // it is not destroyed while exit() is executed |
| 4327 | sp <PlaybackThread> thread; |
| 4328 | { |
| 4329 | Mutex::Autolock _l(mLock); |
| 4330 | thread = checkPlaybackThread_l(output); |
| 4331 | if (thread == NULL) { |
| 4332 | return BAD_VALUE; |
| 4333 | } |
| 4334 | |
| 4335 | LOGV("closeOutput() %d", output); |
| 4336 | |
| 4337 | if (thread->type() == PlaybackThread::MIXER) { |
| 4338 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 4339 | if (mPlaybackThreads.valueAt(i)->type() == PlaybackThread::DUPLICATING) { |
| 4340 | DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 4341 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
| 4342 | } |
| 4343 | } |
| 4344 | } |
| 4345 | void *param2 = 0; |
| 4346 | audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2); |
| 4347 | mPlaybackThreads.removeItem(output); |
| 4348 | } |
| 4349 | thread->exit(); |
| 4350 | |
| 4351 | if (thread->type() != PlaybackThread::DUPLICATING) { |
| 4352 | mAudioHardware->closeOutputStream(thread->getOutput()); |
| 4353 | } |
| 4354 | return NO_ERROR; |
| 4355 | } |
| 4356 | |
| 4357 | status_t AudioFlinger::suspendOutput(int output) |
| 4358 | { |
| 4359 | Mutex::Autolock _l(mLock); |
| 4360 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 4361 | |
| 4362 | if (thread == NULL) { |
| 4363 | return BAD_VALUE; |
| 4364 | } |
| 4365 | |
| 4366 | LOGV("suspendOutput() %d", output); |
| 4367 | thread->suspend(); |
| 4368 | |
| 4369 | return NO_ERROR; |
| 4370 | } |
| 4371 | |
| 4372 | status_t AudioFlinger::restoreOutput(int output) |
| 4373 | { |
| 4374 | Mutex::Autolock _l(mLock); |
| 4375 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 4376 | |
| 4377 | if (thread == NULL) { |
| 4378 | return BAD_VALUE; |
| 4379 | } |
| 4380 | |
| 4381 | LOGV("restoreOutput() %d", output); |
| 4382 | |
| 4383 | thread->restore(); |
| 4384 | |
| 4385 | return NO_ERROR; |
| 4386 | } |
| 4387 | |
| 4388 | int AudioFlinger::openInput(uint32_t *pDevices, |
| 4389 | uint32_t *pSamplingRate, |
| 4390 | uint32_t *pFormat, |
| 4391 | uint32_t *pChannels, |
| 4392 | uint32_t acoustics) |
| 4393 | { |
| 4394 | status_t status; |
| 4395 | RecordThread *thread = NULL; |
| 4396 | uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0; |
| 4397 | uint32_t format = pFormat ? *pFormat : 0; |
| 4398 | uint32_t channels = pChannels ? *pChannels : 0; |
| 4399 | uint32_t reqSamplingRate = samplingRate; |
| 4400 | uint32_t reqFormat = format; |
| 4401 | uint32_t reqChannels = channels; |
| 4402 | |
| 4403 | if (pDevices == NULL || *pDevices == 0) { |
| 4404 | return 0; |
| 4405 | } |
| 4406 | Mutex::Autolock _l(mLock); |
| 4407 | |
| 4408 | AudioStreamIn *input = mAudioHardware->openInputStream(*pDevices, |
| 4409 | (int *)&format, |
| 4410 | &channels, |
| 4411 | &samplingRate, |
| 4412 | &status, |
| 4413 | (AudioSystem::audio_in_acoustics)acoustics); |
| 4414 | LOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d", |
| 4415 | input, |
| 4416 | samplingRate, |
| 4417 | format, |
| 4418 | channels, |
| 4419 | acoustics, |
| 4420 | status); |
| 4421 | |
| 4422 | // If the input could not be opened with the requested parameters and we can handle the conversion internally, |
| 4423 | // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo |
| 4424 | // or stereo to mono conversions on 16 bit PCM inputs. |
| 4425 | if (input == 0 && status == BAD_VALUE && |
| 4426 | reqFormat == format && format == AudioSystem::PCM_16_BIT && |
| 4427 | (samplingRate <= 2 * reqSamplingRate) && |
| 4428 | (AudioSystem::popCount(channels) < 3) && (AudioSystem::popCount(reqChannels) < 3)) { |
| 4429 | LOGV("openInput() reopening with proposed sampling rate and channels"); |
| 4430 | input = mAudioHardware->openInputStream(*pDevices, |
| 4431 | (int *)&format, |
| 4432 | &channels, |
| 4433 | &samplingRate, |
| 4434 | &status, |
| 4435 | (AudioSystem::audio_in_acoustics)acoustics); |
| 4436 | } |
| 4437 | |
| 4438 | if (input != 0) { |
| 4439 | int id = nextUniqueId(); |
| 4440 | // Start record thread |
| 4441 | thread = new RecordThread(this, input, reqSamplingRate, reqChannels, id); |
| 4442 | mRecordThreads.add(id, thread); |
| 4443 | LOGV("openInput() created record thread: ID %d thread %p", id, thread); |
| 4444 | if (pSamplingRate) *pSamplingRate = reqSamplingRate; |
| 4445 | if (pFormat) *pFormat = format; |
| 4446 | if (pChannels) *pChannels = reqChannels; |
| 4447 | |
| 4448 | input->standby(); |
| 4449 | |
| 4450 | // notify client processes of the new input creation |
| 4451 | thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); |
| 4452 | return id; |
| 4453 | } |
| 4454 | |
| 4455 | return 0; |
| 4456 | } |
| 4457 | |
| 4458 | status_t AudioFlinger::closeInput(int input) |
| 4459 | { |
| 4460 | // keep strong reference on the record thread so that |
| 4461 | // it is not destroyed while exit() is executed |
| 4462 | sp <RecordThread> thread; |
| 4463 | { |
| 4464 | Mutex::Autolock _l(mLock); |
| 4465 | thread = checkRecordThread_l(input); |
| 4466 | if (thread == NULL) { |
| 4467 | return BAD_VALUE; |
| 4468 | } |
| 4469 | |
| 4470 | LOGV("closeInput() %d", input); |
| 4471 | void *param2 = 0; |
| 4472 | audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2); |
| 4473 | mRecordThreads.removeItem(input); |
| 4474 | } |
| 4475 | thread->exit(); |
| 4476 | |
| 4477 | mAudioHardware->closeInputStream(thread->getInput()); |
| 4478 | |
| 4479 | return NO_ERROR; |
| 4480 | } |
| 4481 | |
| 4482 | status_t AudioFlinger::setStreamOutput(uint32_t stream, int output) |
| 4483 | { |
| 4484 | Mutex::Autolock _l(mLock); |
| 4485 | MixerThread *dstThread = checkMixerThread_l(output); |
| 4486 | if (dstThread == NULL) { |
| 4487 | LOGW("setStreamOutput() bad output id %d", output); |
| 4488 | return BAD_VALUE; |
| 4489 | } |
| 4490 | |
| 4491 | LOGV("setStreamOutput() stream %d to output %d", stream, output); |
| 4492 | audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream); |
| 4493 | |
| 4494 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 4495 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
| 4496 | if (thread != dstThread && |
| 4497 | thread->type() != PlaybackThread::DIRECT) { |
| 4498 | MixerThread *srcThread = (MixerThread *)thread; |
| 4499 | srcThread->invalidateTracks(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4500 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4501 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4502 | |
| 4503 | return NO_ERROR; |
| 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | int AudioFlinger::newAudioSessionId() |
| 4508 | { |
| 4509 | return nextUniqueId(); |
| 4510 | } |
| 4511 | |
| 4512 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
| 4513 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const |
| 4514 | { |
| 4515 | PlaybackThread *thread = NULL; |
| 4516 | if (mPlaybackThreads.indexOfKey(output) >= 0) { |
| 4517 | thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get(); |
| 4518 | } |
| 4519 | return thread; |
| 4520 | } |
| 4521 | |
| 4522 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
| 4523 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const |
| 4524 | { |
| 4525 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 4526 | if (thread != NULL) { |
| 4527 | if (thread->type() == PlaybackThread::DIRECT) { |
| 4528 | thread = NULL; |
| 4529 | } |
| 4530 | } |
| 4531 | return (MixerThread *)thread; |
| 4532 | } |
| 4533 | |
| 4534 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
| 4535 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const |
| 4536 | { |
| 4537 | RecordThread *thread = NULL; |
| 4538 | if (mRecordThreads.indexOfKey(input) >= 0) { |
| 4539 | thread = (RecordThread *)mRecordThreads.valueFor(input).get(); |
| 4540 | } |
| 4541 | return thread; |
| 4542 | } |
| 4543 | |
| 4544 | int AudioFlinger::nextUniqueId() |
| 4545 | { |
| 4546 | return android_atomic_inc(&mNextUniqueId); |
| 4547 | } |
| 4548 | |
| 4549 | // ---------------------------------------------------------------------------- |
| 4550 | // Effect management |
| 4551 | // ---------------------------------------------------------------------------- |
| 4552 | |
| 4553 | |
| 4554 | status_t AudioFlinger::loadEffectLibrary(const char *libPath, int *handle) |
| 4555 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4556 | // check calling permissions |
| 4557 | if (!settingsAllowed()) { |
| 4558 | return PERMISSION_DENIED; |
| 4559 | } |
| 4560 | // only allow libraries loaded from /system/lib/soundfx for now |
| 4561 | if (strncmp(gEffectLibPath, libPath, strlen(gEffectLibPath)) != 0) { |
| 4562 | return PERMISSION_DENIED; |
| 4563 | } |
| 4564 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4565 | Mutex::Autolock _l(mLock); |
| 4566 | return EffectLoadLibrary(libPath, handle); |
| 4567 | } |
| 4568 | |
| 4569 | status_t AudioFlinger::unloadEffectLibrary(int handle) |
| 4570 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4571 | // check calling permissions |
| 4572 | if (!settingsAllowed()) { |
| 4573 | return PERMISSION_DENIED; |
| 4574 | } |
| 4575 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4576 | Mutex::Autolock _l(mLock); |
| 4577 | return EffectUnloadLibrary(handle); |
| 4578 | } |
| 4579 | |
| 4580 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) |
| 4581 | { |
| 4582 | Mutex::Autolock _l(mLock); |
| 4583 | return EffectQueryNumberEffects(numEffects); |
| 4584 | } |
| 4585 | |
| 4586 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) |
| 4587 | { |
| 4588 | Mutex::Autolock _l(mLock); |
| 4589 | return EffectQueryEffect(index, descriptor); |
| 4590 | } |
| 4591 | |
| 4592 | status_t AudioFlinger::getEffectDescriptor(effect_uuid_t *pUuid, effect_descriptor_t *descriptor) |
| 4593 | { |
| 4594 | Mutex::Autolock _l(mLock); |
| 4595 | return EffectGetDescriptor(pUuid, descriptor); |
| 4596 | } |
| 4597 | |
| 4598 | |
| 4599 | // this UUID must match the one defined in media/libeffects/EffectVisualizer.cpp |
| 4600 | static const effect_uuid_t VISUALIZATION_UUID_ = |
| 4601 | {0xd069d9e0, 0x8329, 0x11df, 0x9168, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}; |
| 4602 | |
| 4603 | sp<IEffect> AudioFlinger::createEffect(pid_t pid, |
| 4604 | effect_descriptor_t *pDesc, |
| 4605 | const sp<IEffectClient>& effectClient, |
| 4606 | int32_t priority, |
| 4607 | int output, |
| 4608 | int sessionId, |
| 4609 | status_t *status, |
| 4610 | int *id, |
| 4611 | int *enabled) |
| 4612 | { |
| 4613 | status_t lStatus = NO_ERROR; |
| 4614 | sp<EffectHandle> handle; |
| 4615 | effect_interface_t itfe; |
| 4616 | effect_descriptor_t desc; |
| 4617 | sp<Client> client; |
| 4618 | wp<Client> wclient; |
| 4619 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4620 | LOGV("createEffect pid %d, client %p, priority %d, sessionId %d, output %d", |
| 4621 | pid, effectClient.get(), priority, sessionId, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4622 | |
| 4623 | if (pDesc == NULL) { |
| 4624 | lStatus = BAD_VALUE; |
| 4625 | goto Exit; |
| 4626 | } |
| 4627 | |
| 4628 | { |
| 4629 | Mutex::Autolock _l(mLock); |
| 4630 | |
| 4631 | // check recording permission for visualizer |
| 4632 | if (memcmp(&pDesc->type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0 || |
| 4633 | memcmp(&pDesc->uuid, &VISUALIZATION_UUID_, sizeof(effect_uuid_t)) == 0) { |
| 4634 | if (!recordingAllowed()) { |
| 4635 | lStatus = PERMISSION_DENIED; |
| 4636 | goto Exit; |
| 4637 | } |
| 4638 | } |
| 4639 | |
| 4640 | if (!EffectIsNullUuid(&pDesc->uuid)) { |
| 4641 | // if uuid is specified, request effect descriptor |
| 4642 | lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); |
| 4643 | if (lStatus < 0) { |
| 4644 | LOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
| 4645 | goto Exit; |
| 4646 | } |
| 4647 | } else { |
| 4648 | // if uuid is not specified, look for an available implementation |
| 4649 | // of the required type in effect factory |
| 4650 | if (EffectIsNullUuid(&pDesc->type)) { |
| 4651 | LOGW("createEffect() no effect type"); |
| 4652 | lStatus = BAD_VALUE; |
| 4653 | goto Exit; |
| 4654 | } |
| 4655 | uint32_t numEffects = 0; |
| 4656 | effect_descriptor_t d; |
| 4657 | bool found = false; |
| 4658 | |
| 4659 | lStatus = EffectQueryNumberEffects(&numEffects); |
| 4660 | if (lStatus < 0) { |
| 4661 | LOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
| 4662 | goto Exit; |
| 4663 | } |
| 4664 | for (uint32_t i = 0; i < numEffects; i++) { |
| 4665 | lStatus = EffectQueryEffect(i, &desc); |
| 4666 | if (lStatus < 0) { |
| 4667 | LOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
| 4668 | continue; |
| 4669 | } |
| 4670 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 4671 | // If matching type found save effect descriptor. If the session is |
| 4672 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 4673 | // an auxiliary version of this effect type is available |
| 4674 | found = true; |
| 4675 | memcpy(&d, &desc, sizeof(effect_descriptor_t)); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4676 | if (sessionId != AudioSystem::SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4677 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 4678 | break; |
| 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | if (!found) { |
| 4683 | lStatus = BAD_VALUE; |
| 4684 | LOGW("createEffect() effect not found"); |
| 4685 | goto Exit; |
| 4686 | } |
| 4687 | // For same effect type, chose auxiliary version over insert version if |
| 4688 | // connect to output mix (Compliance to OpenSL ES) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4689 | if (sessionId == AudioSystem::SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4690 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
| 4691 | memcpy(&desc, &d, sizeof(effect_descriptor_t)); |
| 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4696 | if (sessionId != AudioSystem::SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4697 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 4698 | lStatus = INVALID_OPERATION; |
| 4699 | goto Exit; |
| 4700 | } |
| 4701 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4702 | // Session AudioSystem::SESSION_OUTPUT_STAGE is reserved for output stage effects |
| 4703 | // that can only be created by audio policy manager (running in same process) |
| 4704 | if (sessionId == AudioSystem::SESSION_OUTPUT_STAGE && |
| 4705 | getpid() != IPCThreadState::self()->getCallingPid()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4706 | lStatus = INVALID_OPERATION; |
| 4707 | goto Exit; |
| 4708 | } |
| 4709 | |
| 4710 | // return effect descriptor |
| 4711 | memcpy(pDesc, &desc, sizeof(effect_descriptor_t)); |
| 4712 | |
| 4713 | // If output is not specified try to find a matching audio session ID in one of the |
| 4714 | // output threads. |
| 4715 | // TODO: allow attachment of effect to inputs |
| 4716 | if (output == 0) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4717 | if (sessionId == AudioSystem::SESSION_OUTPUT_STAGE) { |
| 4718 | // output must be specified by AudioPolicyManager when using session |
| 4719 | // AudioSystem::SESSION_OUTPUT_STAGE |
| 4720 | lStatus = BAD_VALUE; |
| 4721 | goto Exit; |
| 4722 | } else if (sessionId == AudioSystem::SESSION_OUTPUT_MIX) { |
| 4723 | output = AudioSystem::getOutputForEffect(&desc); |
| 4724 | LOGV("createEffect() got output %d for effect %s", output, desc.name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4725 | } else { |
| 4726 | // look for the thread where the specified audio session is present |
| 4727 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4728 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4729 | output = mPlaybackThreads.keyAt(i); |
| 4730 | break; |
| 4731 | } |
| 4732 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4733 | // If no output thread contains the requested session ID, default to |
| 4734 | // first output. The effect chain will be moved to the correct output |
| 4735 | // thread when a track with the same session ID is created |
| 4736 | if (output == 0 && mPlaybackThreads.size()) { |
| 4737 | output = mPlaybackThreads.keyAt(0); |
| 4738 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 4742 | if (thread == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4743 | LOGE("createEffect() unknown output thread"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4744 | lStatus = BAD_VALUE; |
| 4745 | goto Exit; |
| 4746 | } |
| 4747 | |
| 4748 | wclient = mClients.valueFor(pid); |
| 4749 | |
| 4750 | if (wclient != NULL) { |
| 4751 | client = wclient.promote(); |
| 4752 | } else { |
| 4753 | client = new Client(this, pid); |
| 4754 | mClients.add(pid, client); |
| 4755 | } |
| 4756 | |
| 4757 | // create effect on selected output trhead |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4758 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
| 4759 | &desc, enabled, &lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4760 | if (handle != 0 && id != NULL) { |
| 4761 | *id = handle->id(); |
| 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | Exit: |
| 4766 | if(status) { |
| 4767 | *status = lStatus; |
| 4768 | } |
| 4769 | return handle; |
| 4770 | } |
| 4771 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4772 | status_t AudioFlinger::moveEffects(int session, int srcOutput, int dstOutput) |
| 4773 | { |
| 4774 | LOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
| 4775 | session, srcOutput, dstOutput); |
| 4776 | Mutex::Autolock _l(mLock); |
| 4777 | if (srcOutput == dstOutput) { |
| 4778 | LOGW("moveEffects() same dst and src outputs %d", dstOutput); |
| 4779 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4780 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4781 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 4782 | if (srcThread == NULL) { |
| 4783 | LOGW("moveEffects() bad srcOutput %d", srcOutput); |
| 4784 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4785 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4786 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 4787 | if (dstThread == NULL) { |
| 4788 | LOGW("moveEffects() bad dstOutput %d", dstOutput); |
| 4789 | return BAD_VALUE; |
| 4790 | } |
| 4791 | |
| 4792 | Mutex::Autolock _dl(dstThread->mLock); |
| 4793 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4794 | moveEffectChain_l(session, srcThread, dstThread, false); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4795 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4796 | return NO_ERROR; |
| 4797 | } |
| 4798 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4799 | // moveEffectChain_l mustbe called with both srcThread and dstThread mLocks held |
| 4800 | status_t AudioFlinger::moveEffectChain_l(int session, |
| 4801 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4802 | AudioFlinger::PlaybackThread *dstThread, |
| 4803 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4804 | { |
| 4805 | LOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
| 4806 | session, srcThread, dstThread); |
| 4807 | |
| 4808 | sp<EffectChain> chain = srcThread->getEffectChain_l(session); |
| 4809 | if (chain == 0) { |
| 4810 | LOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
| 4811 | session, srcThread); |
| 4812 | return INVALID_OPERATION; |
| 4813 | } |
| 4814 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4815 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4816 | // so that a new chain is created with correct parameters when first effect is added. This is |
| 4817 | // otherwise unecessary as removeEffect_l() will remove the chain when last effect is |
| 4818 | // removed. |
| 4819 | srcThread->removeEffectChain_l(chain); |
| 4820 | |
| 4821 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 4822 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4823 | int dstOutput = dstThread->id(); |
| 4824 | sp<EffectChain> dstChain; |
| 4825 | uint32_t strategy; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4826 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
| 4827 | while (effect != 0) { |
| 4828 | srcThread->removeEffect_l(effect); |
| 4829 | dstThread->addEffect_l(effect); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 4830 | // if the move request is not received from audio policy manager, the effect must be |
| 4831 | // re-registered with the new strategy and output |
| 4832 | if (dstChain == 0) { |
| 4833 | dstChain = effect->chain().promote(); |
| 4834 | if (dstChain == 0) { |
| 4835 | LOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
| 4836 | srcThread->addEffect_l(effect); |
| 4837 | return NO_INIT; |
| 4838 | } |
| 4839 | strategy = dstChain->strategy(); |
| 4840 | } |
| 4841 | if (reRegister) { |
| 4842 | AudioSystem::unregisterEffect(effect->id()); |
| 4843 | AudioSystem::registerEffect(&effect->desc(), |
| 4844 | dstOutput, |
| 4845 | strategy, |
| 4846 | session, |
| 4847 | effect->id()); |
| 4848 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4849 | effect = chain->getEffectFromId_l(0); |
| 4850 | } |
| 4851 | |
| 4852 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | // PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held |
| 4856 | sp<AudioFlinger::EffectHandle> AudioFlinger::PlaybackThread::createEffect_l( |
| 4857 | const sp<AudioFlinger::Client>& client, |
| 4858 | const sp<IEffectClient>& effectClient, |
| 4859 | int32_t priority, |
| 4860 | int sessionId, |
| 4861 | effect_descriptor_t *desc, |
| 4862 | int *enabled, |
| 4863 | status_t *status |
| 4864 | ) |
| 4865 | { |
| 4866 | sp<EffectModule> effect; |
| 4867 | sp<EffectHandle> handle; |
| 4868 | status_t lStatus; |
| 4869 | sp<Track> track; |
| 4870 | sp<EffectChain> chain; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4871 | bool chainCreated = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4872 | bool effectCreated = false; |
| 4873 | bool effectRegistered = false; |
| 4874 | |
| 4875 | if (mOutput == 0) { |
| 4876 | LOGW("createEffect_l() Audio driver not initialized."); |
| 4877 | lStatus = NO_INIT; |
| 4878 | goto Exit; |
| 4879 | } |
| 4880 | |
| 4881 | // Do not allow auxiliary effect on session other than 0 |
| 4882 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY && |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4883 | sessionId != AudioSystem::SESSION_OUTPUT_MIX) { |
| 4884 | LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
| 4885 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4886 | lStatus = BAD_VALUE; |
| 4887 | goto Exit; |
| 4888 | } |
| 4889 | |
| 4890 | // Do not allow effects with session ID 0 on direct output or duplicating threads |
| 4891 | // TODO: add rule for hw accelerated effects on direct outputs with non PCM format |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4892 | if (sessionId == AudioSystem::SESSION_OUTPUT_MIX && mType != MIXER) { |
| 4893 | LOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", |
| 4894 | desc->name, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4895 | lStatus = BAD_VALUE; |
| 4896 | goto Exit; |
| 4897 | } |
| 4898 | |
| 4899 | LOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 4900 | |
| 4901 | { // scope for mLock |
| 4902 | Mutex::Autolock _l(mLock); |
| 4903 | |
| 4904 | // check for existing effect chain with the requested audio session |
| 4905 | chain = getEffectChain_l(sessionId); |
| 4906 | if (chain == 0) { |
| 4907 | // create a new chain for this session |
| 4908 | LOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 4909 | chain = new EffectChain(this, sessionId); |
| 4910 | addEffectChain_l(chain); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4911 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 4912 | chainCreated = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4913 | } else { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 4914 | effect = chain->getEffectFromDesc_l(desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | LOGV("createEffect_l() got effect %p on chain %p", effect == 0 ? 0 : effect.get(), chain.get()); |
| 4918 | |
| 4919 | if (effect == 0) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4920 | int id = mAudioFlinger->nextUniqueId(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4921 | // Check CPU and memory usage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4922 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4923 | if (lStatus != NO_ERROR) { |
| 4924 | goto Exit; |
| 4925 | } |
| 4926 | effectRegistered = true; |
| 4927 | // create a new effect module if none present in the chain |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4928 | effect = new EffectModule(this, chain, desc, id, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4929 | lStatus = effect->status(); |
| 4930 | if (lStatus != NO_ERROR) { |
| 4931 | goto Exit; |
| 4932 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 4933 | lStatus = chain->addEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4934 | if (lStatus != NO_ERROR) { |
| 4935 | goto Exit; |
| 4936 | } |
| 4937 | effectCreated = true; |
| 4938 | |
| 4939 | effect->setDevice(mDevice); |
| 4940 | effect->setMode(mAudioFlinger->getMode()); |
| 4941 | } |
| 4942 | // create effect handle and connect it to effect module |
| 4943 | handle = new EffectHandle(effect, client, effectClient, priority); |
| 4944 | lStatus = effect->addHandle(handle); |
| 4945 | if (enabled) { |
| 4946 | *enabled = (int)effect->isEnabled(); |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | Exit: |
| 4951 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4952 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4953 | if (effectCreated) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4954 | chain->removeEffect_l(effect); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4955 | } |
| 4956 | if (effectRegistered) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4957 | AudioSystem::unregisterEffect(effect->id()); |
| 4958 | } |
| 4959 | if (chainCreated) { |
| 4960 | removeEffectChain_l(chain); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4961 | } |
| 4962 | handle.clear(); |
| 4963 | } |
| 4964 | |
| 4965 | if(status) { |
| 4966 | *status = lStatus; |
| 4967 | } |
| 4968 | return handle; |
| 4969 | } |
| 4970 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 4971 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 4972 | // PlaybackThread::mLock held |
| 4973 | status_t AudioFlinger::PlaybackThread::addEffect_l(const sp<EffectModule>& effect) |
| 4974 | { |
| 4975 | // check for existing effect chain with the requested audio session |
| 4976 | int sessionId = effect->sessionId(); |
| 4977 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 4978 | bool chainCreated = false; |
| 4979 | |
| 4980 | if (chain == 0) { |
| 4981 | // create a new chain for this session |
| 4982 | LOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 4983 | chain = new EffectChain(this, sessionId); |
| 4984 | addEffectChain_l(chain); |
| 4985 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 4986 | chainCreated = true; |
| 4987 | } |
| 4988 | LOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 4989 | |
| 4990 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 4991 | LOGW("addEffect_l() %p effect %s already present in chain %p", |
| 4992 | this, effect->desc().name, chain.get()); |
| 4993 | return BAD_VALUE; |
| 4994 | } |
| 4995 | |
| 4996 | status_t status = chain->addEffect_l(effect); |
| 4997 | if (status != NO_ERROR) { |
| 4998 | if (chainCreated) { |
| 4999 | removeEffectChain_l(chain); |
| 5000 | } |
| 5001 | return status; |
| 5002 | } |
| 5003 | |
| 5004 | effect->setDevice(mDevice); |
| 5005 | effect->setMode(mAudioFlinger->getMode()); |
| 5006 | return NO_ERROR; |
| 5007 | } |
| 5008 | |
| 5009 | void AudioFlinger::PlaybackThread::removeEffect_l(const sp<EffectModule>& effect) { |
| 5010 | |
| 5011 | LOGV("removeEffect_l() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5012 | effect_descriptor_t desc = effect->desc(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5013 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5014 | detachAuxEffect_l(effect->id()); |
| 5015 | } |
| 5016 | |
| 5017 | sp<EffectChain> chain = effect->chain().promote(); |
| 5018 | if (chain != 0) { |
| 5019 | // remove effect chain if removing last effect |
| 5020 | if (chain->removeEffect_l(effect) == 0) { |
| 5021 | removeEffectChain_l(chain); |
| 5022 | } |
| 5023 | } else { |
| 5024 | LOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | void AudioFlinger::PlaybackThread::disconnectEffect(const sp<EffectModule>& effect, |
| 5029 | const wp<EffectHandle>& handle) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5030 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5031 | LOGV("disconnectEffect() %p effect %p", this, effect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5032 | // delete the effect module if removing last handle on it |
| 5033 | if (effect->removeHandle(handle) == 0) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5034 | removeEffect_l(effect); |
| 5035 | AudioSystem::unregisterEffect(effect->id()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 5040 | { |
| 5041 | int session = chain->sessionId(); |
| 5042 | int16_t *buffer = mMixBuffer; |
| 5043 | bool ownsBuffer = false; |
| 5044 | |
| 5045 | LOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 5046 | if (session > 0) { |
| 5047 | // Only one effect chain can be present in direct output thread and it uses |
| 5048 | // the mix buffer as input |
| 5049 | if (mType != DIRECT) { |
| 5050 | size_t numSamples = mFrameCount * mChannelCount; |
| 5051 | buffer = new int16_t[numSamples]; |
| 5052 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 5053 | LOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 5054 | ownsBuffer = true; |
| 5055 | } |
| 5056 | |
| 5057 | // Attach all tracks with same session ID to this chain. |
| 5058 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5059 | sp<Track> track = mTracks[i]; |
| 5060 | if (session == track->sessionId()) { |
| 5061 | LOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer); |
| 5062 | track->setMainBuffer(buffer); |
| 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | // indicate all active tracks in the chain |
| 5067 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 5068 | sp<Track> track = mActiveTracks[i].promote(); |
| 5069 | if (track == 0) continue; |
| 5070 | if (session == track->sessionId()) { |
| 5071 | LOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 5072 | chain->startTrack(); |
| 5073 | } |
| 5074 | } |
| 5075 | } |
| 5076 | |
| 5077 | chain->setInBuffer(buffer, ownsBuffer); |
| 5078 | chain->setOutBuffer(mMixBuffer); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5079 | // Effect chain for session AudioSystem::SESSION_OUTPUT_STAGE is inserted at end of effect |
| 5080 | // chains list in order to be processed last as it contains output stage effects |
| 5081 | // Effect chain for session AudioSystem::SESSION_OUTPUT_MIX is inserted before |
| 5082 | // session AudioSystem::SESSION_OUTPUT_STAGE to be processed |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5083 | // after track specific effects and before output stage |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5084 | // It is therefore mandatory that AudioSystem::SESSION_OUTPUT_MIX == 0 and |
| 5085 | // that AudioSystem::SESSION_OUTPUT_STAGE < AudioSystem::SESSION_OUTPUT_MIX |
| 5086 | // Effect chain for other sessions are inserted at beginning of effect |
| 5087 | // chains list to be processed before output mix effects. Relative order between other |
| 5088 | // sessions is not important |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5089 | size_t size = mEffectChains.size(); |
| 5090 | size_t i = 0; |
| 5091 | for (i = 0; i < size; i++) { |
| 5092 | if (mEffectChains[i]->sessionId() < session) break; |
| 5093 | } |
| 5094 | mEffectChains.insertAt(chain, i); |
| 5095 | |
| 5096 | return NO_ERROR; |
| 5097 | } |
| 5098 | |
| 5099 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 5100 | { |
| 5101 | int session = chain->sessionId(); |
| 5102 | |
| 5103 | LOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 5104 | |
| 5105 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5106 | if (chain == mEffectChains[i]) { |
| 5107 | mEffectChains.removeAt(i); |
| 5108 | // detach all tracks with same session ID from this chain |
| 5109 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5110 | sp<Track> track = mTracks[i]; |
| 5111 | if (session == track->sessionId()) { |
| 5112 | track->setMainBuffer(mMixBuffer); |
| 5113 | } |
| 5114 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5115 | break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | return mEffectChains.size(); |
| 5119 | } |
| 5120 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5121 | void AudioFlinger::PlaybackThread::lockEffectChains_l( |
| 5122 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5123 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5124 | effectChains = mEffectChains; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5125 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5126 | mEffectChains[i]->lock(); |
| 5127 | } |
| 5128 | } |
| 5129 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5130 | void AudioFlinger::PlaybackThread::unlockEffectChains( |
| 5131 | Vector<sp <AudioFlinger::EffectChain> >& effectChains) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5132 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5133 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 5134 | effectChains[i]->unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5138 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5139 | sp<AudioFlinger::EffectModule> AudioFlinger::PlaybackThread::getEffect_l(int sessionId, int effectId) |
| 5140 | { |
| 5141 | sp<EffectModule> effect; |
| 5142 | |
| 5143 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 5144 | if (chain != 0) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 5145 | effect = chain->getEffectFromId_l(effectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5146 | } |
| 5147 | return effect; |
| 5148 | } |
| 5149 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5150 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 5151 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5152 | { |
| 5153 | Mutex::Autolock _l(mLock); |
| 5154 | return attachAuxEffect_l(track, EffectId); |
| 5155 | } |
| 5156 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5157 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 5158 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5159 | { |
| 5160 | status_t status = NO_ERROR; |
| 5161 | |
| 5162 | if (EffectId == 0) { |
| 5163 | track->setAuxBuffer(0, NULL); |
| 5164 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5165 | // Auxiliary effects are always in audio session AudioSystem::SESSION_OUTPUT_MIX |
| 5166 | sp<EffectModule> effect = getEffect_l(AudioSystem::SESSION_OUTPUT_MIX, EffectId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5167 | if (effect != 0) { |
| 5168 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5169 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 5170 | } else { |
| 5171 | status = INVALID_OPERATION; |
| 5172 | } |
| 5173 | } else { |
| 5174 | status = BAD_VALUE; |
| 5175 | } |
| 5176 | } |
| 5177 | return status; |
| 5178 | } |
| 5179 | |
| 5180 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 5181 | { |
| 5182 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 5183 | sp<Track> track = mTracks[i]; |
| 5184 | if (track->auxEffectId() == effectId) { |
| 5185 | attachAuxEffect_l(track, 0); |
| 5186 | } |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | // ---------------------------------------------------------------------------- |
| 5191 | // EffectModule implementation |
| 5192 | // ---------------------------------------------------------------------------- |
| 5193 | |
| 5194 | #undef LOG_TAG |
| 5195 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 5196 | |
| 5197 | AudioFlinger::EffectModule::EffectModule(const wp<ThreadBase>& wThread, |
| 5198 | const wp<AudioFlinger::EffectChain>& chain, |
| 5199 | effect_descriptor_t *desc, |
| 5200 | int id, |
| 5201 | int sessionId) |
| 5202 | : mThread(wThread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL), |
| 5203 | mStatus(NO_INIT), mState(IDLE) |
| 5204 | { |
| 5205 | LOGV("Constructor %p", this); |
| 5206 | int lStatus; |
| 5207 | sp<ThreadBase> thread = mThread.promote(); |
| 5208 | if (thread == 0) { |
| 5209 | return; |
| 5210 | } |
| 5211 | PlaybackThread *p = (PlaybackThread *)thread.get(); |
| 5212 | |
| 5213 | memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t)); |
| 5214 | |
| 5215 | // create effect engine from effect factory |
| 5216 | mStatus = EffectCreate(&desc->uuid, sessionId, p->id(), &mEffectInterface); |
| 5217 | |
| 5218 | if (mStatus != NO_ERROR) { |
| 5219 | return; |
| 5220 | } |
| 5221 | lStatus = init(); |
| 5222 | if (lStatus < 0) { |
| 5223 | mStatus = lStatus; |
| 5224 | goto Error; |
| 5225 | } |
| 5226 | |
| 5227 | LOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); |
| 5228 | return; |
| 5229 | Error: |
| 5230 | EffectRelease(mEffectInterface); |
| 5231 | mEffectInterface = NULL; |
| 5232 | LOGV("Constructor Error %d", mStatus); |
| 5233 | } |
| 5234 | |
| 5235 | AudioFlinger::EffectModule::~EffectModule() |
| 5236 | { |
| 5237 | LOGV("Destructor %p", this); |
| 5238 | if (mEffectInterface != NULL) { |
| 5239 | // release effect engine |
| 5240 | EffectRelease(mEffectInterface); |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | status_t AudioFlinger::EffectModule::addHandle(sp<EffectHandle>& handle) |
| 5245 | { |
| 5246 | status_t status; |
| 5247 | |
| 5248 | Mutex::Autolock _l(mLock); |
| 5249 | // First handle in mHandles has highest priority and controls the effect module |
| 5250 | int priority = handle->priority(); |
| 5251 | size_t size = mHandles.size(); |
| 5252 | sp<EffectHandle> h; |
| 5253 | size_t i; |
| 5254 | for (i = 0; i < size; i++) { |
| 5255 | h = mHandles[i].promote(); |
| 5256 | if (h == 0) continue; |
| 5257 | if (h->priority() <= priority) break; |
| 5258 | } |
| 5259 | // if inserted in first place, move effect control from previous owner to this handle |
| 5260 | if (i == 0) { |
| 5261 | if (h != 0) { |
| 5262 | h->setControl(false, true); |
| 5263 | } |
| 5264 | handle->setControl(true, false); |
| 5265 | status = NO_ERROR; |
| 5266 | } else { |
| 5267 | status = ALREADY_EXISTS; |
| 5268 | } |
| 5269 | mHandles.insertAt(handle, i); |
| 5270 | return status; |
| 5271 | } |
| 5272 | |
| 5273 | size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle) |
| 5274 | { |
| 5275 | Mutex::Autolock _l(mLock); |
| 5276 | size_t size = mHandles.size(); |
| 5277 | size_t i; |
| 5278 | for (i = 0; i < size; i++) { |
| 5279 | if (mHandles[i] == handle) break; |
| 5280 | } |
| 5281 | if (i == size) { |
| 5282 | return size; |
| 5283 | } |
| 5284 | mHandles.removeAt(i); |
| 5285 | size = mHandles.size(); |
| 5286 | // if removed from first place, move effect control from this handle to next in line |
| 5287 | if (i == 0 && size != 0) { |
| 5288 | sp<EffectHandle> h = mHandles[0].promote(); |
| 5289 | if (h != 0) { |
| 5290 | h->setControl(true, true); |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | return size; |
| 5295 | } |
| 5296 | |
| 5297 | void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle) |
| 5298 | { |
| 5299 | // keep a strong reference on this EffectModule to avoid calling the |
| 5300 | // destructor before we exit |
| 5301 | sp<EffectModule> keep(this); |
| 5302 | { |
| 5303 | sp<ThreadBase> thread = mThread.promote(); |
| 5304 | if (thread != 0) { |
| 5305 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5306 | playbackThread->disconnectEffect(keep, handle); |
| 5307 | } |
| 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | void AudioFlinger::EffectModule::updateState() { |
| 5312 | Mutex::Autolock _l(mLock); |
| 5313 | |
| 5314 | switch (mState) { |
| 5315 | case RESTART: |
| 5316 | reset_l(); |
| 5317 | // FALL THROUGH |
| 5318 | |
| 5319 | case STARTING: |
| 5320 | // clear auxiliary effect input buffer for next accumulation |
| 5321 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5322 | memset(mConfig.inputCfg.buffer.raw, |
| 5323 | 0, |
| 5324 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 5325 | } |
| 5326 | start_l(); |
| 5327 | mState = ACTIVE; |
| 5328 | break; |
| 5329 | case STOPPING: |
| 5330 | stop_l(); |
| 5331 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 5332 | mState = STOPPED; |
| 5333 | break; |
| 5334 | case STOPPED: |
| 5335 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 5336 | // turn off sequence. |
| 5337 | if (--mDisableWaitCnt == 0) { |
| 5338 | reset_l(); |
| 5339 | mState = IDLE; |
| 5340 | } |
| 5341 | break; |
| 5342 | default: //IDLE , ACTIVE |
| 5343 | break; |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | void AudioFlinger::EffectModule::process() |
| 5348 | { |
| 5349 | Mutex::Autolock _l(mLock); |
| 5350 | |
| 5351 | if (mEffectInterface == NULL || |
| 5352 | mConfig.inputCfg.buffer.raw == NULL || |
| 5353 | mConfig.outputCfg.buffer.raw == NULL) { |
| 5354 | return; |
| 5355 | } |
| 5356 | |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 5357 | if (mState == ACTIVE || mState == STOPPING || mState == STOPPED || mState == RESTART) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5358 | // do 32 bit to 16 bit conversion for auxiliary effect input buffer |
| 5359 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5360 | AudioMixer::ditherAndClamp(mConfig.inputCfg.buffer.s32, |
| 5361 | mConfig.inputCfg.buffer.s32, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5362 | mConfig.inputCfg.buffer.frameCount/2); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | // do the actual processing in the effect engine |
| 5366 | int ret = (*mEffectInterface)->process(mEffectInterface, |
| 5367 | &mConfig.inputCfg.buffer, |
| 5368 | &mConfig.outputCfg.buffer); |
| 5369 | |
| 5370 | // force transition to IDLE state when engine is ready |
| 5371 | if (mState == STOPPED && ret == -ENODATA) { |
| 5372 | mDisableWaitCnt = 1; |
| 5373 | } |
| 5374 | |
| 5375 | // clear auxiliary effect input buffer for next accumulation |
| 5376 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5377 | memset(mConfig.inputCfg.buffer.raw, 0, mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 5378 | } |
| 5379 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
| 5380 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw){ |
| 5381 | // If an insert effect is idle and input buffer is different from output buffer, copy input to |
| 5382 | // output |
| 5383 | sp<EffectChain> chain = mChain.promote(); |
| 5384 | if (chain != 0 && chain->activeTracks() != 0) { |
| 5385 | size_t size = mConfig.inputCfg.buffer.frameCount * sizeof(int16_t); |
| 5386 | if (mConfig.inputCfg.channels == CHANNEL_STEREO) { |
| 5387 | size *= 2; |
| 5388 | } |
| 5389 | memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw, size); |
| 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | void AudioFlinger::EffectModule::reset_l() |
| 5395 | { |
| 5396 | if (mEffectInterface == NULL) { |
| 5397 | return; |
| 5398 | } |
| 5399 | (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
| 5400 | } |
| 5401 | |
| 5402 | status_t AudioFlinger::EffectModule::configure() |
| 5403 | { |
| 5404 | uint32_t channels; |
| 5405 | if (mEffectInterface == NULL) { |
| 5406 | return NO_INIT; |
| 5407 | } |
| 5408 | |
| 5409 | sp<ThreadBase> thread = mThread.promote(); |
| 5410 | if (thread == 0) { |
| 5411 | return DEAD_OBJECT; |
| 5412 | } |
| 5413 | |
| 5414 | // TODO: handle configuration of effects replacing track process |
| 5415 | if (thread->channelCount() == 1) { |
| 5416 | channels = CHANNEL_MONO; |
| 5417 | } else { |
| 5418 | channels = CHANNEL_STEREO; |
| 5419 | } |
| 5420 | |
| 5421 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 5422 | mConfig.inputCfg.channels = CHANNEL_MONO; |
| 5423 | } else { |
| 5424 | mConfig.inputCfg.channels = channels; |
| 5425 | } |
| 5426 | mConfig.outputCfg.channels = channels; |
| 5427 | mConfig.inputCfg.format = SAMPLE_FORMAT_PCM_S15; |
| 5428 | mConfig.outputCfg.format = SAMPLE_FORMAT_PCM_S15; |
| 5429 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 5430 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 5431 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 5432 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 5433 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 5434 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 5435 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 5436 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 5437 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 5438 | // Insert effect: |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5439 | // - in session AudioSystem::SESSION_OUTPUT_MIX or AudioSystem::SESSION_OUTPUT_STAGE, |
| 5440 | // always overwrites output buffer: input buffer == output buffer |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5441 | // - in other sessions: |
| 5442 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 5443 | // other effect: overwrites output buffer: input buffer == output buffer |
| 5444 | // Auxiliary effect: |
| 5445 | // accumulates in output buffer: input buffer != output buffer |
| 5446 | // Therefore: accumulate <=> input buffer != output buffer |
| 5447 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 5448 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 5449 | } else { |
| 5450 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 5451 | } |
| 5452 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 5453 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 5454 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 5455 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 5456 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 5457 | LOGV("configure() %p thread %p buffer %p framecount %d", |
| 5458 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 5459 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5460 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5461 | uint32_t size = sizeof(int); |
| 5462 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 5463 | EFFECT_CMD_CONFIGURE, |
| 5464 | sizeof(effect_config_t), |
| 5465 | &mConfig, |
| 5466 | &size, |
| 5467 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5468 | if (status == 0) { |
| 5469 | status = cmdStatus; |
| 5470 | } |
| 5471 | |
| 5472 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 5473 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 5474 | |
| 5475 | return status; |
| 5476 | } |
| 5477 | |
| 5478 | status_t AudioFlinger::EffectModule::init() |
| 5479 | { |
| 5480 | Mutex::Autolock _l(mLock); |
| 5481 | if (mEffectInterface == NULL) { |
| 5482 | return NO_INIT; |
| 5483 | } |
| 5484 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5485 | uint32_t size = sizeof(status_t); |
| 5486 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 5487 | EFFECT_CMD_INIT, |
| 5488 | 0, |
| 5489 | NULL, |
| 5490 | &size, |
| 5491 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5492 | if (status == 0) { |
| 5493 | status = cmdStatus; |
| 5494 | } |
| 5495 | return status; |
| 5496 | } |
| 5497 | |
| 5498 | status_t AudioFlinger::EffectModule::start_l() |
| 5499 | { |
| 5500 | if (mEffectInterface == NULL) { |
| 5501 | return NO_INIT; |
| 5502 | } |
| 5503 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5504 | uint32_t size = sizeof(status_t); |
| 5505 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 5506 | EFFECT_CMD_ENABLE, |
| 5507 | 0, |
| 5508 | NULL, |
| 5509 | &size, |
| 5510 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5511 | if (status == 0) { |
| 5512 | status = cmdStatus; |
| 5513 | } |
| 5514 | return status; |
| 5515 | } |
| 5516 | |
| 5517 | status_t AudioFlinger::EffectModule::stop_l() |
| 5518 | { |
| 5519 | if (mEffectInterface == NULL) { |
| 5520 | return NO_INIT; |
| 5521 | } |
| 5522 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5523 | uint32_t size = sizeof(status_t); |
| 5524 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 5525 | EFFECT_CMD_DISABLE, |
| 5526 | 0, |
| 5527 | NULL, |
| 5528 | &size, |
| 5529 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5530 | if (status == 0) { |
| 5531 | status = cmdStatus; |
| 5532 | } |
| 5533 | return status; |
| 5534 | } |
| 5535 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5536 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 5537 | uint32_t cmdSize, |
| 5538 | void *pCmdData, |
| 5539 | uint32_t *replySize, |
| 5540 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5541 | { |
| 5542 | Mutex::Autolock _l(mLock); |
| 5543 | // LOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); |
| 5544 | |
| 5545 | if (mEffectInterface == NULL) { |
| 5546 | return NO_INIT; |
| 5547 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5548 | status_t status = (*mEffectInterface)->command(mEffectInterface, |
| 5549 | cmdCode, |
| 5550 | cmdSize, |
| 5551 | pCmdData, |
| 5552 | replySize, |
| 5553 | pReplyData); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5554 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5555 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5556 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 5557 | sp<EffectHandle> h = mHandles[i].promote(); |
| 5558 | if (h != 0) { |
| 5559 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 5560 | } |
| 5561 | } |
| 5562 | } |
| 5563 | return status; |
| 5564 | } |
| 5565 | |
| 5566 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 5567 | { |
| 5568 | Mutex::Autolock _l(mLock); |
| 5569 | LOGV("setEnabled %p enabled %d", this, enabled); |
| 5570 | |
| 5571 | if (enabled != isEnabled()) { |
| 5572 | switch (mState) { |
| 5573 | // going from disabled to enabled |
| 5574 | case IDLE: |
| 5575 | mState = STARTING; |
| 5576 | break; |
| 5577 | case STOPPED: |
| 5578 | mState = RESTART; |
| 5579 | break; |
| 5580 | case STOPPING: |
| 5581 | mState = ACTIVE; |
| 5582 | break; |
| 5583 | |
| 5584 | // going from enabled to disabled |
| 5585 | case RESTART: |
| 5586 | case STARTING: |
| 5587 | mState = IDLE; |
| 5588 | break; |
| 5589 | case ACTIVE: |
| 5590 | mState = STOPPING; |
| 5591 | break; |
| 5592 | } |
| 5593 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 5594 | sp<EffectHandle> h = mHandles[i].promote(); |
| 5595 | if (h != 0) { |
| 5596 | h->setEnabled(enabled); |
| 5597 | } |
| 5598 | } |
| 5599 | } |
| 5600 | return NO_ERROR; |
| 5601 | } |
| 5602 | |
| 5603 | bool AudioFlinger::EffectModule::isEnabled() |
| 5604 | { |
| 5605 | switch (mState) { |
| 5606 | case RESTART: |
| 5607 | case STARTING: |
| 5608 | case ACTIVE: |
| 5609 | return true; |
| 5610 | case IDLE: |
| 5611 | case STOPPING: |
| 5612 | case STOPPED: |
| 5613 | default: |
| 5614 | return false; |
| 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 5619 | { |
| 5620 | Mutex::Autolock _l(mLock); |
| 5621 | status_t status = NO_ERROR; |
| 5622 | |
| 5623 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 5624 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 5625 | if ((mState >= ACTIVE) && |
| 5626 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 5627 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5628 | status_t cmdStatus; |
| 5629 | uint32_t volume[2]; |
| 5630 | uint32_t *pVolume = NULL; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5631 | uint32_t size = sizeof(volume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5632 | volume[0] = *left; |
| 5633 | volume[1] = *right; |
| 5634 | if (controller) { |
| 5635 | pVolume = volume; |
| 5636 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5637 | status = (*mEffectInterface)->command(mEffectInterface, |
| 5638 | EFFECT_CMD_SET_VOLUME, |
| 5639 | size, |
| 5640 | volume, |
| 5641 | &size, |
| 5642 | pVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5643 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 5644 | *left = volume[0]; |
| 5645 | *right = volume[1]; |
| 5646 | } |
| 5647 | } |
| 5648 | return status; |
| 5649 | } |
| 5650 | |
| 5651 | status_t AudioFlinger::EffectModule::setDevice(uint32_t device) |
| 5652 | { |
| 5653 | Mutex::Autolock _l(mLock); |
| 5654 | status_t status = NO_ERROR; |
| 5655 | if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
| 5656 | // convert device bit field from AudioSystem to EffectApi format. |
| 5657 | device = deviceAudioSystemToEffectApi(device); |
| 5658 | if (device == 0) { |
| 5659 | return BAD_VALUE; |
| 5660 | } |
| 5661 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5662 | uint32_t size = sizeof(status_t); |
| 5663 | status = (*mEffectInterface)->command(mEffectInterface, |
| 5664 | EFFECT_CMD_SET_DEVICE, |
| 5665 | sizeof(uint32_t), |
| 5666 | &device, |
| 5667 | &size, |
| 5668 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5669 | if (status == NO_ERROR) { |
| 5670 | status = cmdStatus; |
| 5671 | } |
| 5672 | } |
| 5673 | return status; |
| 5674 | } |
| 5675 | |
| 5676 | status_t AudioFlinger::EffectModule::setMode(uint32_t mode) |
| 5677 | { |
| 5678 | Mutex::Autolock _l(mLock); |
| 5679 | status_t status = NO_ERROR; |
| 5680 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
| 5681 | // convert audio mode from AudioSystem to EffectApi format. |
| 5682 | int effectMode = modeAudioSystemToEffectApi(mode); |
| 5683 | if (effectMode < 0) { |
| 5684 | return BAD_VALUE; |
| 5685 | } |
| 5686 | status_t cmdStatus; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5687 | uint32_t size = sizeof(status_t); |
| 5688 | status = (*mEffectInterface)->command(mEffectInterface, |
| 5689 | EFFECT_CMD_SET_AUDIO_MODE, |
| 5690 | sizeof(int), |
| 5691 | &effectMode, |
| 5692 | &size, |
| 5693 | &cmdStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5694 | if (status == NO_ERROR) { |
| 5695 | status = cmdStatus; |
| 5696 | } |
| 5697 | } |
| 5698 | return status; |
| 5699 | } |
| 5700 | |
| 5701 | // update this table when AudioSystem::audio_devices or audio_device_e (in EffectApi.h) are modified |
| 5702 | const uint32_t AudioFlinger::EffectModule::sDeviceConvTable[] = { |
| 5703 | DEVICE_EARPIECE, // AudioSystem::DEVICE_OUT_EARPIECE |
| 5704 | DEVICE_SPEAKER, // AudioSystem::DEVICE_OUT_SPEAKER |
| 5705 | DEVICE_WIRED_HEADSET, // case AudioSystem::DEVICE_OUT_WIRED_HEADSET |
| 5706 | DEVICE_WIRED_HEADPHONE, // AudioSystem::DEVICE_OUT_WIRED_HEADPHONE |
| 5707 | DEVICE_BLUETOOTH_SCO, // AudioSystem::DEVICE_OUT_BLUETOOTH_SCO |
| 5708 | DEVICE_BLUETOOTH_SCO_HEADSET, // AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
| 5709 | DEVICE_BLUETOOTH_SCO_CARKIT, // AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
| 5710 | DEVICE_BLUETOOTH_A2DP, // AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
| 5711 | DEVICE_BLUETOOTH_A2DP_HEADPHONES, // AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
| 5712 | DEVICE_BLUETOOTH_A2DP_SPEAKER, // AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
| 5713 | DEVICE_AUX_DIGITAL // AudioSystem::DEVICE_OUT_AUX_DIGITAL |
| 5714 | }; |
| 5715 | |
| 5716 | uint32_t AudioFlinger::EffectModule::deviceAudioSystemToEffectApi(uint32_t device) |
| 5717 | { |
| 5718 | uint32_t deviceOut = 0; |
| 5719 | while (device) { |
| 5720 | const uint32_t i = 31 - __builtin_clz(device); |
| 5721 | device &= ~(1 << i); |
| 5722 | if (i >= sizeof(sDeviceConvTable)/sizeof(uint32_t)) { |
| 5723 | LOGE("device convertion error for AudioSystem device 0x%08x", device); |
| 5724 | return 0; |
| 5725 | } |
| 5726 | deviceOut |= (uint32_t)sDeviceConvTable[i]; |
| 5727 | } |
| 5728 | return deviceOut; |
| 5729 | } |
| 5730 | |
| 5731 | // update this table when AudioSystem::audio_mode or audio_mode_e (in EffectApi.h) are modified |
| 5732 | const uint32_t AudioFlinger::EffectModule::sModeConvTable[] = { |
| 5733 | AUDIO_MODE_NORMAL, // AudioSystem::MODE_NORMAL |
| 5734 | AUDIO_MODE_RINGTONE, // AudioSystem::MODE_RINGTONE |
| 5735 | AUDIO_MODE_IN_CALL // AudioSystem::MODE_IN_CALL |
| 5736 | }; |
| 5737 | |
| 5738 | int AudioFlinger::EffectModule::modeAudioSystemToEffectApi(uint32_t mode) |
| 5739 | { |
| 5740 | int modeOut = -1; |
| 5741 | if (mode < sizeof(sModeConvTable) / sizeof(uint32_t)) { |
| 5742 | modeOut = (int)sModeConvTable[mode]; |
| 5743 | } |
| 5744 | return modeOut; |
| 5745 | } |
| 5746 | |
| 5747 | status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
| 5748 | { |
| 5749 | const size_t SIZE = 256; |
| 5750 | char buffer[SIZE]; |
| 5751 | String8 result; |
| 5752 | |
| 5753 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 5754 | result.append(buffer); |
| 5755 | |
| 5756 | bool locked = tryLock(mLock); |
| 5757 | // failed to lock - AudioFlinger is probably deadlocked |
| 5758 | if (!locked) { |
| 5759 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 5760 | } |
| 5761 | |
| 5762 | result.append("\t\tSession Status State Engine:\n"); |
| 5763 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n", |
| 5764 | mSessionId, mStatus, mState, (uint32_t)mEffectInterface); |
| 5765 | result.append(buffer); |
| 5766 | |
| 5767 | result.append("\t\tDescriptor:\n"); |
| 5768 | snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 5769 | mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, |
| 5770 | mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2], |
| 5771 | mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); |
| 5772 | result.append(buffer); |
| 5773 | snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", |
| 5774 | mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion, |
| 5775 | mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2], |
| 5776 | mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); |
| 5777 | result.append(buffer); |
| 5778 | snprintf(buffer, SIZE, "\t\t- apiVersion: %04X\n\t\t- flags: %08X\n", |
| 5779 | mDescriptor.apiVersion, |
| 5780 | mDescriptor.flags); |
| 5781 | result.append(buffer); |
| 5782 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 5783 | mDescriptor.name); |
| 5784 | result.append(buffer); |
| 5785 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 5786 | mDescriptor.implementor); |
| 5787 | result.append(buffer); |
| 5788 | |
| 5789 | result.append("\t\t- Input configuration:\n"); |
| 5790 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 5791 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 5792 | (uint32_t)mConfig.inputCfg.buffer.raw, |
| 5793 | mConfig.inputCfg.buffer.frameCount, |
| 5794 | mConfig.inputCfg.samplingRate, |
| 5795 | mConfig.inputCfg.channels, |
| 5796 | mConfig.inputCfg.format); |
| 5797 | result.append(buffer); |
| 5798 | |
| 5799 | result.append("\t\t- Output configuration:\n"); |
| 5800 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 5801 | snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n", |
| 5802 | (uint32_t)mConfig.outputCfg.buffer.raw, |
| 5803 | mConfig.outputCfg.buffer.frameCount, |
| 5804 | mConfig.outputCfg.samplingRate, |
| 5805 | mConfig.outputCfg.channels, |
| 5806 | mConfig.outputCfg.format); |
| 5807 | result.append(buffer); |
| 5808 | |
| 5809 | snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); |
| 5810 | result.append(buffer); |
| 5811 | result.append("\t\t\tPid Priority Ctrl Locked client server\n"); |
| 5812 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 5813 | sp<EffectHandle> handle = mHandles[i].promote(); |
| 5814 | if (handle != 0) { |
| 5815 | handle->dump(buffer, SIZE); |
| 5816 | result.append(buffer); |
| 5817 | } |
| 5818 | } |
| 5819 | |
| 5820 | result.append("\n"); |
| 5821 | |
| 5822 | write(fd, result.string(), result.length()); |
| 5823 | |
| 5824 | if (locked) { |
| 5825 | mLock.unlock(); |
| 5826 | } |
| 5827 | |
| 5828 | return NO_ERROR; |
| 5829 | } |
| 5830 | |
| 5831 | // ---------------------------------------------------------------------------- |
| 5832 | // EffectHandle implementation |
| 5833 | // ---------------------------------------------------------------------------- |
| 5834 | |
| 5835 | #undef LOG_TAG |
| 5836 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 5837 | |
| 5838 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 5839 | const sp<AudioFlinger::Client>& client, |
| 5840 | const sp<IEffectClient>& effectClient, |
| 5841 | int32_t priority) |
| 5842 | : BnEffect(), |
| 5843 | mEffect(effect), mEffectClient(effectClient), mClient(client), mPriority(priority), mHasControl(false) |
| 5844 | { |
| 5845 | LOGV("constructor %p", this); |
| 5846 | |
| 5847 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 5848 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
| 5849 | if (mCblkMemory != 0) { |
| 5850 | mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); |
| 5851 | |
| 5852 | if (mCblk) { |
| 5853 | new(mCblk) effect_param_cblk_t(); |
| 5854 | mBuffer = (uint8_t *)mCblk + bufOffset; |
| 5855 | } |
| 5856 | } else { |
| 5857 | LOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t)); |
| 5858 | return; |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | AudioFlinger::EffectHandle::~EffectHandle() |
| 5863 | { |
| 5864 | LOGV("Destructor %p", this); |
| 5865 | disconnect(); |
| 5866 | } |
| 5867 | |
| 5868 | status_t AudioFlinger::EffectHandle::enable() |
| 5869 | { |
| 5870 | if (!mHasControl) return INVALID_OPERATION; |
| 5871 | if (mEffect == 0) return DEAD_OBJECT; |
| 5872 | |
| 5873 | return mEffect->setEnabled(true); |
| 5874 | } |
| 5875 | |
| 5876 | status_t AudioFlinger::EffectHandle::disable() |
| 5877 | { |
| 5878 | if (!mHasControl) return INVALID_OPERATION; |
| 5879 | if (mEffect == NULL) return DEAD_OBJECT; |
| 5880 | |
| 5881 | return mEffect->setEnabled(false); |
| 5882 | } |
| 5883 | |
| 5884 | void AudioFlinger::EffectHandle::disconnect() |
| 5885 | { |
| 5886 | if (mEffect == 0) { |
| 5887 | return; |
| 5888 | } |
| 5889 | mEffect->disconnect(this); |
| 5890 | // release sp on module => module destructor can be called now |
| 5891 | mEffect.clear(); |
| 5892 | if (mCblk) { |
| 5893 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 5894 | } |
| 5895 | mCblkMemory.clear(); // and free the shared memory |
| 5896 | if (mClient != 0) { |
| 5897 | Mutex::Autolock _l(mClient->audioFlinger()->mLock); |
| 5898 | mClient.clear(); |
| 5899 | } |
| 5900 | } |
| 5901 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5902 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 5903 | uint32_t cmdSize, |
| 5904 | void *pCmdData, |
| 5905 | uint32_t *replySize, |
| 5906 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5907 | { |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5908 | // LOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
| 5909 | // cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5910 | |
| 5911 | // only get parameter command is permitted for applications not controlling the effect |
| 5912 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 5913 | return INVALID_OPERATION; |
| 5914 | } |
| 5915 | if (mEffect == 0) return DEAD_OBJECT; |
| 5916 | |
| 5917 | // handle commands that are not forwarded transparently to effect engine |
| 5918 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
| 5919 | // No need to trylock() here as this function is executed in the binder thread serving a particular client process: |
| 5920 | // no risk to block the whole media server process or mixer threads is we are stuck here |
| 5921 | Mutex::Autolock _l(mCblk->lock); |
| 5922 | if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 5923 | mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
| 5924 | mCblk->serverIndex = 0; |
| 5925 | mCblk->clientIndex = 0; |
| 5926 | return BAD_VALUE; |
| 5927 | } |
| 5928 | status_t status = NO_ERROR; |
| 5929 | while (mCblk->serverIndex < mCblk->clientIndex) { |
| 5930 | int reply; |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5931 | uint32_t rsize = sizeof(int); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5932 | int *p = (int *)(mBuffer + mCblk->serverIndex); |
| 5933 | int size = *p++; |
| 5934 | if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { |
| 5935 | LOGW("command(): invalid parameter block size"); |
| 5936 | break; |
| 5937 | } |
| 5938 | effect_param_t *param = (effect_param_t *)p; |
| 5939 | if (param->psize == 0 || param->vsize == 0) { |
| 5940 | LOGW("command(): null parameter or value size"); |
| 5941 | mCblk->serverIndex += size; |
| 5942 | continue; |
| 5943 | } |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5944 | uint32_t psize = sizeof(effect_param_t) + |
| 5945 | ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 5946 | param->vsize; |
| 5947 | status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, |
| 5948 | psize, |
| 5949 | p, |
| 5950 | &rsize, |
| 5951 | &reply); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5952 | if (ret == NO_ERROR) { |
| 5953 | if (reply != NO_ERROR) { |
| 5954 | status = reply; |
| 5955 | } |
| 5956 | } else { |
| 5957 | status = ret; |
| 5958 | } |
| 5959 | mCblk->serverIndex += size; |
| 5960 | } |
| 5961 | mCblk->serverIndex = 0; |
| 5962 | mCblk->clientIndex = 0; |
| 5963 | return status; |
| 5964 | } else if (cmdCode == EFFECT_CMD_ENABLE) { |
| 5965 | return enable(); |
| 5966 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
| 5967 | return disable(); |
| 5968 | } |
| 5969 | |
| 5970 | return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 5971 | } |
| 5972 | |
| 5973 | sp<IMemory> AudioFlinger::EffectHandle::getCblk() const { |
| 5974 | return mCblkMemory; |
| 5975 | } |
| 5976 | |
| 5977 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal) |
| 5978 | { |
| 5979 | LOGV("setControl %p control %d", this, hasControl); |
| 5980 | |
| 5981 | mHasControl = hasControl; |
| 5982 | if (signal && mEffectClient != 0) { |
| 5983 | mEffectClient->controlStatusChanged(hasControl); |
| 5984 | } |
| 5985 | } |
| 5986 | |
Eric Laurent | 25f4395 | 2010-07-28 05:40:18 -0700 | [diff] [blame] | 5987 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 5988 | uint32_t cmdSize, |
| 5989 | void *pCmdData, |
| 5990 | uint32_t replySize, |
| 5991 | void *pReplyData) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 5992 | { |
| 5993 | if (mEffectClient != 0) { |
| 5994 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 5995 | } |
| 5996 | } |
| 5997 | |
| 5998 | |
| 5999 | |
| 6000 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 6001 | { |
| 6002 | if (mEffectClient != 0) { |
| 6003 | mEffectClient->enableStatusChanged(enabled); |
| 6004 | } |
| 6005 | } |
| 6006 | |
| 6007 | status_t AudioFlinger::EffectHandle::onTransact( |
| 6008 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 6009 | { |
| 6010 | return BnEffect::onTransact(code, data, reply, flags); |
| 6011 | } |
| 6012 | |
| 6013 | |
| 6014 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) |
| 6015 | { |
| 6016 | bool locked = tryLock(mCblk->lock); |
| 6017 | |
| 6018 | snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n", |
| 6019 | (mClient == NULL) ? getpid() : mClient->pid(), |
| 6020 | mPriority, |
| 6021 | mHasControl, |
| 6022 | !locked, |
| 6023 | mCblk->clientIndex, |
| 6024 | mCblk->serverIndex |
| 6025 | ); |
| 6026 | |
| 6027 | if (locked) { |
| 6028 | mCblk->lock.unlock(); |
| 6029 | } |
| 6030 | } |
| 6031 | |
| 6032 | #undef LOG_TAG |
| 6033 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 6034 | |
| 6035 | AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread, |
| 6036 | int sessionId) |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6037 | : mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mOwnInBuffer(false), |
Eric Laurent | 8569f0d | 2010-07-29 23:43:43 -0700 | [diff] [blame] | 6038 | mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
| 6039 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6040 | { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6041 | mStrategy = AudioSystem::getStrategyForStream(AudioSystem::MUSIC); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | AudioFlinger::EffectChain::~EffectChain() |
| 6045 | { |
| 6046 | if (mOwnInBuffer) { |
| 6047 | delete mInBuffer; |
| 6048 | } |
| 6049 | |
| 6050 | } |
| 6051 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6052 | // getEffectFromDesc_l() must be called with PlaybackThread::mLock held |
| 6053 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6054 | { |
| 6055 | sp<EffectModule> effect; |
| 6056 | size_t size = mEffects.size(); |
| 6057 | |
| 6058 | for (size_t i = 0; i < size; i++) { |
| 6059 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 6060 | effect = mEffects[i]; |
| 6061 | break; |
| 6062 | } |
| 6063 | } |
| 6064 | return effect; |
| 6065 | } |
| 6066 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6067 | // getEffectFromId_l() must be called with PlaybackThread::mLock held |
| 6068 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6069 | { |
| 6070 | sp<EffectModule> effect; |
| 6071 | size_t size = mEffects.size(); |
| 6072 | |
| 6073 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6074 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 6075 | if (id == 0 || mEffects[i]->id() == id) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6076 | effect = mEffects[i]; |
| 6077 | break; |
| 6078 | } |
| 6079 | } |
| 6080 | return effect; |
| 6081 | } |
| 6082 | |
| 6083 | // Must be called with EffectChain::mLock locked |
| 6084 | void AudioFlinger::EffectChain::process_l() |
| 6085 | { |
| 6086 | size_t size = mEffects.size(); |
| 6087 | for (size_t i = 0; i < size; i++) { |
| 6088 | mEffects[i]->process(); |
| 6089 | } |
| 6090 | for (size_t i = 0; i < size; i++) { |
| 6091 | mEffects[i]->updateState(); |
| 6092 | } |
| 6093 | // if no track is active, input buffer must be cleared here as the mixer process |
| 6094 | // will not do it |
| 6095 | if (mSessionId > 0 && activeTracks() == 0) { |
| 6096 | sp<ThreadBase> thread = mThread.promote(); |
| 6097 | if (thread != 0) { |
| 6098 | size_t numSamples = thread->frameCount() * thread->channelCount(); |
| 6099 | memset(mInBuffer, 0, numSamples * sizeof(int16_t)); |
| 6100 | } |
| 6101 | } |
| 6102 | } |
| 6103 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6104 | // addEffect_l() must be called with PlaybackThread::mLock held |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6105 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6106 | { |
| 6107 | effect_descriptor_t desc = effect->desc(); |
| 6108 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 6109 | |
| 6110 | Mutex::Autolock _l(mLock); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6111 | effect->setChain(this); |
| 6112 | sp<ThreadBase> thread = mThread.promote(); |
| 6113 | if (thread == 0) { |
| 6114 | return NO_INIT; |
| 6115 | } |
| 6116 | effect->setThread(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6117 | |
| 6118 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6119 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 6120 | // they are processed first and accumulated in chain input buffer |
| 6121 | mEffects.insertAt(effect, 0); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 6122 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6123 | // the input buffer for auxiliary effect contains mono samples in |
| 6124 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 6125 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 6126 | // calling the process in effect engine |
| 6127 | size_t numSamples = thread->frameCount(); |
| 6128 | int32_t *buffer = new int32_t[numSamples]; |
| 6129 | memset(buffer, 0, numSamples * sizeof(int32_t)); |
| 6130 | effect->setInBuffer((int16_t *)buffer); |
| 6131 | // auxiliary effects output samples to chain input buffer for further processing |
| 6132 | // by insert effects |
| 6133 | effect->setOutBuffer(mInBuffer); |
| 6134 | } else { |
| 6135 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 6136 | // after track and auxiliary effects. |
| 6137 | // Insert effect order as a function of indicated preference: |
| 6138 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 6139 | // another effect is present |
| 6140 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 6141 | // last effect claiming first position |
| 6142 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 6143 | // first effect claiming last position |
| 6144 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 6145 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 6146 | // already present |
| 6147 | |
| 6148 | int size = (int)mEffects.size(); |
| 6149 | int idx_insert = size; |
| 6150 | int idx_insert_first = -1; |
| 6151 | int idx_insert_last = -1; |
| 6152 | |
| 6153 | for (int i = 0; i < size; i++) { |
| 6154 | effect_descriptor_t d = mEffects[i]->desc(); |
| 6155 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 6156 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 6157 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 6158 | // check invalid effect chaining combinations |
| 6159 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 6160 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6161 | LOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6162 | return INVALID_OPERATION; |
| 6163 | } |
| 6164 | // remember position of first insert effect and by default |
| 6165 | // select this as insert position for new effect |
| 6166 | if (idx_insert == size) { |
| 6167 | idx_insert = i; |
| 6168 | } |
| 6169 | // remember position of last insert effect claiming |
| 6170 | // first position |
| 6171 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 6172 | idx_insert_first = i; |
| 6173 | } |
| 6174 | // remember position of first insert effect claiming |
| 6175 | // last position |
| 6176 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 6177 | idx_insert_last == -1) { |
| 6178 | idx_insert_last = i; |
| 6179 | } |
| 6180 | } |
| 6181 | } |
| 6182 | |
| 6183 | // modify idx_insert from first position if needed |
| 6184 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 6185 | if (idx_insert_last != -1) { |
| 6186 | idx_insert = idx_insert_last; |
| 6187 | } else { |
| 6188 | idx_insert = size; |
| 6189 | } |
| 6190 | } else { |
| 6191 | if (idx_insert_first != -1) { |
| 6192 | idx_insert = idx_insert_first + 1; |
| 6193 | } |
| 6194 | } |
| 6195 | |
| 6196 | // always read samples from chain input buffer |
| 6197 | effect->setInBuffer(mInBuffer); |
| 6198 | |
| 6199 | // if last effect in the chain, output samples to chain |
| 6200 | // output buffer, otherwise to chain input buffer |
| 6201 | if (idx_insert == size) { |
| 6202 | if (idx_insert != 0) { |
| 6203 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 6204 | mEffects[idx_insert-1]->configure(); |
| 6205 | } |
| 6206 | effect->setOutBuffer(mOutBuffer); |
| 6207 | } else { |
| 6208 | effect->setOutBuffer(mInBuffer); |
| 6209 | } |
| 6210 | mEffects.insertAt(effect, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6211 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6212 | LOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6213 | } |
| 6214 | effect->configure(); |
| 6215 | return NO_ERROR; |
| 6216 | } |
| 6217 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6218 | // removeEffect_l() must be called with PlaybackThread::mLock held |
| 6219 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6220 | { |
| 6221 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6222 | int size = (int)mEffects.size(); |
| 6223 | int i; |
| 6224 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 6225 | |
| 6226 | for (i = 0; i < size; i++) { |
| 6227 | if (effect == mEffects[i]) { |
| 6228 | if (type == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 6229 | delete[] effect->inBuffer(); |
| 6230 | } else { |
| 6231 | if (i == size - 1 && i != 0) { |
| 6232 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 6233 | mEffects[i - 1]->configure(); |
| 6234 | } |
| 6235 | } |
| 6236 | mEffects.removeAt(i); |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6237 | LOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6238 | break; |
| 6239 | } |
| 6240 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6241 | |
| 6242 | return mEffects.size(); |
| 6243 | } |
| 6244 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6245 | // setDevice_l() must be called with PlaybackThread::mLock held |
| 6246 | void AudioFlinger::EffectChain::setDevice_l(uint32_t device) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6247 | { |
| 6248 | size_t size = mEffects.size(); |
| 6249 | for (size_t i = 0; i < size; i++) { |
| 6250 | mEffects[i]->setDevice(device); |
| 6251 | } |
| 6252 | } |
| 6253 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6254 | // setMode_l() must be called with PlaybackThread::mLock held |
| 6255 | void AudioFlinger::EffectChain::setMode_l(uint32_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6256 | { |
| 6257 | size_t size = mEffects.size(); |
| 6258 | for (size_t i = 0; i < size; i++) { |
| 6259 | mEffects[i]->setMode(mode); |
| 6260 | } |
| 6261 | } |
| 6262 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6263 | // setVolume_l() must be called with PlaybackThread::mLock held |
| 6264 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6265 | { |
| 6266 | uint32_t newLeft = *left; |
| 6267 | uint32_t newRight = *right; |
| 6268 | bool hasControl = false; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6269 | int ctrlIdx = -1; |
| 6270 | size_t size = mEffects.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6271 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6272 | // first update volume controller |
| 6273 | for (size_t i = size; i > 0; i--) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6274 | if ((mEffects[i - 1]->state() >= EffectModule::ACTIVE) && |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6275 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 6276 | ctrlIdx = i - 1; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6277 | hasControl = true; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6278 | break; |
| 6279 | } |
| 6280 | } |
| 6281 | |
| 6282 | if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6283 | if (hasControl) { |
| 6284 | *left = mNewLeftVolume; |
| 6285 | *right = mNewRightVolume; |
| 6286 | } |
| 6287 | return hasControl; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6288 | } |
| 6289 | |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6290 | if (mVolumeCtrlIdx != -1) { |
| 6291 | hasControl = true; |
| 6292 | } |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6293 | mVolumeCtrlIdx = ctrlIdx; |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6294 | mLeftVolume = newLeft; |
| 6295 | mRightVolume = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6296 | |
| 6297 | // second get volume update from volume controller |
| 6298 | if (ctrlIdx >= 0) { |
| 6299 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
Eric Laurent | f997cab | 2010-07-19 06:24:46 -0700 | [diff] [blame] | 6300 | mNewLeftVolume = newLeft; |
| 6301 | mNewRightVolume = newRight; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6302 | } |
| 6303 | // then indicate volume to all other effects in chain. |
| 6304 | // Pass altered volume to effects before volume controller |
| 6305 | // and requested volume to effects after controller |
| 6306 | uint32_t lVol = newLeft; |
| 6307 | uint32_t rVol = newRight; |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6308 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6309 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6310 | if ((int)i == ctrlIdx) continue; |
| 6311 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 6312 | if ((int)i > ctrlIdx) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6313 | lVol = *left; |
| 6314 | rVol = *right; |
| 6315 | } |
| 6316 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 6317 | } |
| 6318 | *left = newLeft; |
| 6319 | *right = newRight; |
| 6320 | |
| 6321 | return hasControl; |
| 6322 | } |
| 6323 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6324 | status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 6325 | { |
| 6326 | const size_t SIZE = 256; |
| 6327 | char buffer[SIZE]; |
| 6328 | String8 result; |
| 6329 | |
| 6330 | snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); |
| 6331 | result.append(buffer); |
| 6332 | |
| 6333 | bool locked = tryLock(mLock); |
| 6334 | // failed to lock - AudioFlinger is probably deadlocked |
| 6335 | if (!locked) { |
| 6336 | result.append("\tCould not lock mutex:\n"); |
| 6337 | } |
| 6338 | |
Eric Laurent | cab1124 | 2010-07-15 12:50:15 -0700 | [diff] [blame] | 6339 | result.append("\tNum fx In buffer Out buffer Active tracks:\n"); |
| 6340 | snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6341 | mEffects.size(), |
| 6342 | (uint32_t)mInBuffer, |
| 6343 | (uint32_t)mOutBuffer, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6344 | mActiveTrackCnt); |
| 6345 | result.append(buffer); |
| 6346 | write(fd, result.string(), result.size()); |
| 6347 | |
| 6348 | for (size_t i = 0; i < mEffects.size(); ++i) { |
| 6349 | sp<EffectModule> effect = mEffects[i]; |
| 6350 | if (effect != 0) { |
| 6351 | effect->dump(fd, args); |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | if (locked) { |
| 6356 | mLock.unlock(); |
| 6357 | } |
| 6358 | |
| 6359 | return NO_ERROR; |
| 6360 | } |
| 6361 | |
| 6362 | #undef LOG_TAG |
| 6363 | #define LOG_TAG "AudioFlinger" |
| 6364 | |
| 6365 | // ---------------------------------------------------------------------------- |
| 6366 | |
| 6367 | status_t AudioFlinger::onTransact( |
| 6368 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 6369 | { |
| 6370 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 6371 | } |
| 6372 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 6373 | }; // namespace android |