| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* | 
 | 2 | ** | 
 | 3 | ** Copyright 2012, 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 | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 |  | 
 | 23 | #include <math.h> | 
 | 24 | #include <fcntl.h> | 
 | 25 | #include <sys/stat.h> | 
 | 26 | #include <cutils/properties.h> | 
 | 27 | #include <cutils/compiler.h> | 
 | 28 | #include <utils/Log.h> | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 29 | #include <utils/Trace.h> | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 30 |  | 
 | 31 | #include <private/media/AudioTrackShared.h> | 
 | 32 | #include <hardware/audio.h> | 
 | 33 | #include <audio_effects/effect_ns.h> | 
 | 34 | #include <audio_effects/effect_aec.h> | 
 | 35 | #include <audio_utils/primitives.h> | 
 | 36 |  | 
 | 37 | // NBAIO implementations | 
 | 38 | #include <media/nbaio/AudioStreamOutSink.h> | 
 | 39 | #include <media/nbaio/MonoPipe.h> | 
 | 40 | #include <media/nbaio/MonoPipeReader.h> | 
 | 41 | #include <media/nbaio/Pipe.h> | 
 | 42 | #include <media/nbaio/PipeReader.h> | 
 | 43 | #include <media/nbaio/SourceAudioBufferProvider.h> | 
 | 44 |  | 
 | 45 | #include <powermanager/PowerManager.h> | 
 | 46 |  | 
 | 47 | #include <common_time/cc_helper.h> | 
 | 48 | #include <common_time/local_clock.h> | 
 | 49 |  | 
 | 50 | #include "AudioFlinger.h" | 
 | 51 | #include "AudioMixer.h" | 
 | 52 | #include "FastMixer.h" | 
 | 53 | #include "ServiceUtilities.h" | 
 | 54 | #include "SchedulingPolicyService.h" | 
 | 55 |  | 
 | 56 | #undef ADD_BATTERY_DATA | 
 | 57 |  | 
 | 58 | #ifdef ADD_BATTERY_DATA | 
 | 59 | #include <media/IMediaPlayerService.h> | 
 | 60 | #include <media/IMediaDeathNotifier.h> | 
 | 61 | #endif | 
 | 62 |  | 
 | 63 | // #define DEBUG_CPU_USAGE 10  // log statistics every n wall clock seconds | 
 | 64 | #ifdef DEBUG_CPU_USAGE | 
 | 65 | #include <cpustats/CentralTendencyStatistics.h> | 
 | 66 | #include <cpustats/ThreadCpuUsage.h> | 
 | 67 | #endif | 
 | 68 |  | 
 | 69 | // ---------------------------------------------------------------------------- | 
 | 70 |  | 
 | 71 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 72 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 73 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 74 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 75 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 76 | // are doing and want to see all of the extremely verbose messages. | 
 | 77 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 78 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 79 | #define ALOGVV ALOGV | 
 | 80 | #else | 
 | 81 | #define ALOGVV(a...) do { } while(0) | 
 | 82 | #endif | 
 | 83 |  | 
 | 84 | namespace android { | 
 | 85 |  | 
 | 86 | // retry counts for buffer fill timeout | 
 | 87 | // 50 * ~20msecs = 1 second | 
 | 88 | static const int8_t kMaxTrackRetries = 50; | 
 | 89 | static const int8_t kMaxTrackStartupRetries = 50; | 
 | 90 | // allow less retry attempts on direct output thread. | 
 | 91 | // direct outputs can be a scarce resource in audio hardware and should | 
 | 92 | // be released as quickly as possible. | 
 | 93 | static const int8_t kMaxTrackRetriesDirect = 2; | 
 | 94 |  | 
 | 95 | // don't warn about blocked writes or record buffer overflows more often than this | 
 | 96 | static const nsecs_t kWarningThrottleNs = seconds(5); | 
 | 97 |  | 
 | 98 | // RecordThread loop sleep time upon application overrun or audio HAL read error | 
 | 99 | static const int kRecordThreadSleepUs = 5000; | 
 | 100 |  | 
 | 101 | // maximum time to wait for setParameters to complete | 
 | 102 | static const nsecs_t kSetParametersTimeoutNs = seconds(2); | 
 | 103 |  | 
 | 104 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun | 
 | 105 | static const uint32_t kMinThreadSleepTimeUs = 5000; | 
 | 106 | // maximum divider applied to the active sleep time in the mixer thread loop | 
 | 107 | static const uint32_t kMaxThreadSleepTimeShift = 2; | 
 | 108 |  | 
 | 109 | // minimum normal mix buffer size, expressed in milliseconds rather than frames | 
 | 110 | static const uint32_t kMinNormalMixBufferSizeMs = 20; | 
 | 111 | // maximum normal mix buffer size | 
 | 112 | static const uint32_t kMaxNormalMixBufferSizeMs = 24; | 
 | 113 |  | 
 | 114 | // Whether to use fast mixer | 
 | 115 | static const enum { | 
 | 116 |     FastMixer_Never,    // never initialize or use: for debugging only | 
 | 117 |     FastMixer_Always,   // always initialize and use, even if not needed: for debugging only | 
 | 118 |                         // normal mixer multiplier is 1 | 
 | 119 |     FastMixer_Static,   // initialize if needed, then use all the time if initialized, | 
 | 120 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 121 |     FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load, | 
 | 122 |                         // multiplier is calculated based on min & max normal mixer buffer size | 
 | 123 |     // FIXME for FastMixer_Dynamic: | 
 | 124 |     //  Supporting this option will require fixing HALs that can't handle large writes. | 
 | 125 |     //  For example, one HAL implementation returns an error from a large write, | 
 | 126 |     //  and another HAL implementation corrupts memory, possibly in the sample rate converter. | 
 | 127 |     //  We could either fix the HAL implementations, or provide a wrapper that breaks | 
 | 128 |     //  up large writes into smaller ones, and the wrapper would need to deal with scheduler. | 
 | 129 | } kUseFastMixer = FastMixer_Static; | 
 | 130 |  | 
 | 131 | // Priorities for requestPriority | 
 | 132 | static const int kPriorityAudioApp = 2; | 
 | 133 | static const int kPriorityFastMixer = 3; | 
 | 134 |  | 
 | 135 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area | 
 | 136 | // for the track.  The client then sub-divides this into smaller buffers for its use. | 
 | 137 | // Currently the client uses double-buffering by default, but doesn't tell us about that. | 
 | 138 | // So for now we just assume that client is double-buffered. | 
 | 139 | // FIXME It would be better for client to tell AudioFlinger whether it wants double-buffering or | 
 | 140 | // N-buffering, so AudioFlinger could allocate the right amount of memory. | 
 | 141 | // See the client's minBufCount and mNotificationFramesAct calculations for details. | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 142 | static const int kFastTrackMultiplier = 1; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 143 |  | 
 | 144 | // ---------------------------------------------------------------------------- | 
 | 145 |  | 
 | 146 | #ifdef ADD_BATTERY_DATA | 
 | 147 | // To collect the amplifier usage | 
 | 148 | static void addBatteryData(uint32_t params) { | 
 | 149 |     sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); | 
 | 150 |     if (service == NULL) { | 
 | 151 |         // it already logged | 
 | 152 |         return; | 
 | 153 |     } | 
 | 154 |  | 
 | 155 |     service->addBatteryData(params); | 
 | 156 | } | 
 | 157 | #endif | 
 | 158 |  | 
 | 159 |  | 
 | 160 | // ---------------------------------------------------------------------------- | 
 | 161 | //      CPU Stats | 
 | 162 | // ---------------------------------------------------------------------------- | 
 | 163 |  | 
 | 164 | class CpuStats { | 
 | 165 | public: | 
 | 166 |     CpuStats(); | 
 | 167 |     void sample(const String8 &title); | 
 | 168 | #ifdef DEBUG_CPU_USAGE | 
 | 169 | private: | 
 | 170 |     ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns | 
 | 171 |     CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns | 
 | 172 |  | 
 | 173 |     CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles | 
 | 174 |  | 
 | 175 |     int mCpuNum;                        // thread's current CPU number | 
 | 176 |     int mCpukHz;                        // frequency of thread's current CPU in kHz | 
 | 177 | #endif | 
 | 178 | }; | 
 | 179 |  | 
 | 180 | CpuStats::CpuStats() | 
 | 181 | #ifdef DEBUG_CPU_USAGE | 
 | 182 |     : mCpuNum(-1), mCpukHz(-1) | 
 | 183 | #endif | 
 | 184 | { | 
 | 185 | } | 
 | 186 |  | 
 | 187 | void CpuStats::sample(const String8 &title) { | 
 | 188 | #ifdef DEBUG_CPU_USAGE | 
 | 189 |     // get current thread's delta CPU time in wall clock ns | 
 | 190 |     double wcNs; | 
 | 191 |     bool valid = mCpuUsage.sampleAndEnable(wcNs); | 
 | 192 |  | 
 | 193 |     // record sample for wall clock statistics | 
 | 194 |     if (valid) { | 
 | 195 |         mWcStats.sample(wcNs); | 
 | 196 |     } | 
 | 197 |  | 
 | 198 |     // get the current CPU number | 
 | 199 |     int cpuNum = sched_getcpu(); | 
 | 200 |  | 
 | 201 |     // get the current CPU frequency in kHz | 
 | 202 |     int cpukHz = mCpuUsage.getCpukHz(cpuNum); | 
 | 203 |  | 
 | 204 |     // check if either CPU number or frequency changed | 
 | 205 |     if (cpuNum != mCpuNum || cpukHz != mCpukHz) { | 
 | 206 |         mCpuNum = cpuNum; | 
 | 207 |         mCpukHz = cpukHz; | 
 | 208 |         // ignore sample for purposes of cycles | 
 | 209 |         valid = false; | 
 | 210 |     } | 
 | 211 |  | 
 | 212 |     // if no change in CPU number or frequency, then record sample for cycle statistics | 
 | 213 |     if (valid && mCpukHz > 0) { | 
 | 214 |         double cycles = wcNs * cpukHz * 0.000001; | 
 | 215 |         mHzStats.sample(cycles); | 
 | 216 |     } | 
 | 217 |  | 
 | 218 |     unsigned n = mWcStats.n(); | 
 | 219 |     // mCpuUsage.elapsed() is expensive, so don't call it every loop | 
 | 220 |     if ((n & 127) == 1) { | 
 | 221 |         long long elapsed = mCpuUsage.elapsed(); | 
 | 222 |         if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { | 
 | 223 |             double perLoop = elapsed / (double) n; | 
 | 224 |             double perLoop100 = perLoop * 0.01; | 
 | 225 |             double perLoop1k = perLoop * 0.001; | 
 | 226 |             double mean = mWcStats.mean(); | 
 | 227 |             double stddev = mWcStats.stddev(); | 
 | 228 |             double minimum = mWcStats.minimum(); | 
 | 229 |             double maximum = mWcStats.maximum(); | 
 | 230 |             double meanCycles = mHzStats.mean(); | 
 | 231 |             double stddevCycles = mHzStats.stddev(); | 
 | 232 |             double minCycles = mHzStats.minimum(); | 
 | 233 |             double maxCycles = mHzStats.maximum(); | 
 | 234 |             mCpuUsage.resetElapsed(); | 
 | 235 |             mWcStats.reset(); | 
 | 236 |             mHzStats.reset(); | 
 | 237 |             ALOGD("CPU usage for %s over past %.1f secs\n" | 
 | 238 |                 "  (%u mixer loops at %.1f mean ms per loop):\n" | 
 | 239 |                 "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" | 
 | 240 |                 "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" | 
 | 241 |                 "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", | 
 | 242 |                     title.string(), | 
 | 243 |                     elapsed * .000000001, n, perLoop * .000001, | 
 | 244 |                     mean * .001, | 
 | 245 |                     stddev * .001, | 
 | 246 |                     minimum * .001, | 
 | 247 |                     maximum * .001, | 
 | 248 |                     mean / perLoop100, | 
 | 249 |                     stddev / perLoop100, | 
 | 250 |                     minimum / perLoop100, | 
 | 251 |                     maximum / perLoop100, | 
 | 252 |                     meanCycles / perLoop1k, | 
 | 253 |                     stddevCycles / perLoop1k, | 
 | 254 |                     minCycles / perLoop1k, | 
 | 255 |                     maxCycles / perLoop1k); | 
 | 256 |  | 
 | 257 |         } | 
 | 258 |     } | 
 | 259 | #endif | 
 | 260 | }; | 
 | 261 |  | 
 | 262 | // ---------------------------------------------------------------------------- | 
 | 263 | //      ThreadBase | 
 | 264 | // ---------------------------------------------------------------------------- | 
 | 265 |  | 
 | 266 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, | 
 | 267 |         audio_devices_t outDevice, audio_devices_t inDevice, type_t type) | 
 | 268 |     :   Thread(false /*canCallJava*/), | 
 | 269 |         mType(type), | 
 | 270 |         mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mNormalFrameCount(0), | 
 | 271 |         // mChannelMask | 
 | 272 |         mChannelCount(0), | 
 | 273 |         mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID), | 
 | 274 |         mParamStatus(NO_ERROR), | 
 | 275 |         mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), | 
 | 276 |         mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), | 
 | 277 |         // mName will be set by concrete (non-virtual) subclass | 
 | 278 |         mDeathRecipient(new PMDeathRecipient(this)) | 
 | 279 | { | 
 | 280 | } | 
 | 281 |  | 
 | 282 | AudioFlinger::ThreadBase::~ThreadBase() | 
 | 283 | { | 
 | 284 |     mParamCond.broadcast(); | 
 | 285 |     // do not lock the mutex in destructor | 
 | 286 |     releaseWakeLock_l(); | 
 | 287 |     if (mPowerManager != 0) { | 
 | 288 |         sp<IBinder> binder = mPowerManager->asBinder(); | 
 | 289 |         binder->unlinkToDeath(mDeathRecipient); | 
 | 290 |     } | 
 | 291 | } | 
 | 292 |  | 
 | 293 | void AudioFlinger::ThreadBase::exit() | 
 | 294 | { | 
 | 295 |     ALOGV("ThreadBase::exit"); | 
 | 296 |     // do any cleanup required for exit to succeed | 
 | 297 |     preExit(); | 
 | 298 |     { | 
 | 299 |         // This lock prevents the following race in thread (uniprocessor for illustration): | 
 | 300 |         //  if (!exitPending()) { | 
 | 301 |         //      // context switch from here to exit() | 
 | 302 |         //      // exit() calls requestExit(), what exitPending() observes | 
 | 303 |         //      // exit() calls signal(), which is dropped since no waiters | 
 | 304 |         //      // context switch back from exit() to here | 
 | 305 |         //      mWaitWorkCV.wait(...); | 
 | 306 |         //      // now thread is hung | 
 | 307 |         //  } | 
 | 308 |         AutoMutex lock(mLock); | 
 | 309 |         requestExit(); | 
 | 310 |         mWaitWorkCV.broadcast(); | 
 | 311 |     } | 
 | 312 |     // When Thread::requestExitAndWait is made virtual and this method is renamed to | 
 | 313 |     // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" | 
 | 314 |     requestExitAndWait(); | 
 | 315 | } | 
 | 316 |  | 
 | 317 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) | 
 | 318 | { | 
 | 319 |     status_t status; | 
 | 320 |  | 
 | 321 |     ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); | 
 | 322 |     Mutex::Autolock _l(mLock); | 
 | 323 |  | 
 | 324 |     mNewParameters.add(keyValuePairs); | 
 | 325 |     mWaitWorkCV.signal(); | 
 | 326 |     // wait condition with timeout in case the thread loop has exited | 
 | 327 |     // before the request could be processed | 
 | 328 |     if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) { | 
 | 329 |         status = mParamStatus; | 
 | 330 |         mWaitWorkCV.signal(); | 
 | 331 |     } else { | 
 | 332 |         status = TIMED_OUT; | 
 | 333 |     } | 
 | 334 |     return status; | 
 | 335 | } | 
 | 336 |  | 
 | 337 | void AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) | 
 | 338 | { | 
 | 339 |     Mutex::Autolock _l(mLock); | 
 | 340 |     sendIoConfigEvent_l(event, param); | 
 | 341 | } | 
 | 342 |  | 
 | 343 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 344 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) | 
 | 345 | { | 
 | 346 |     IoConfigEvent *ioEvent = new IoConfigEvent(event, param); | 
 | 347 |     mConfigEvents.add(static_cast<ConfigEvent *>(ioEvent)); | 
 | 348 |     ALOGV("sendIoConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, | 
 | 349 |             param); | 
 | 350 |     mWaitWorkCV.signal(); | 
 | 351 | } | 
 | 352 |  | 
 | 353 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held | 
 | 354 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) | 
 | 355 | { | 
 | 356 |     PrioConfigEvent *prioEvent = new PrioConfigEvent(pid, tid, prio); | 
 | 357 |     mConfigEvents.add(static_cast<ConfigEvent *>(prioEvent)); | 
 | 358 |     ALOGV("sendPrioConfigEvent_l() num events %d pid %d, tid %d prio %d", | 
 | 359 |           mConfigEvents.size(), pid, tid, prio); | 
 | 360 |     mWaitWorkCV.signal(); | 
 | 361 | } | 
 | 362 |  | 
 | 363 | void AudioFlinger::ThreadBase::processConfigEvents() | 
 | 364 | { | 
 | 365 |     mLock.lock(); | 
 | 366 |     while (!mConfigEvents.isEmpty()) { | 
 | 367 |         ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size()); | 
 | 368 |         ConfigEvent *event = mConfigEvents[0]; | 
 | 369 |         mConfigEvents.removeAt(0); | 
 | 370 |         // release mLock before locking AudioFlinger mLock: lock order is always | 
 | 371 |         // AudioFlinger then ThreadBase to avoid cross deadlock | 
 | 372 |         mLock.unlock(); | 
 | 373 |         switch(event->type()) { | 
 | 374 |             case CFG_EVENT_PRIO: { | 
 | 375 |                 PrioConfigEvent *prioEvent = static_cast<PrioConfigEvent *>(event); | 
| Glenn Kasten | a07f17c | 2013-04-23 12:39:37 -0700 | [diff] [blame] | 376 |                 // FIXME Need to understand why this has be done asynchronously | 
 | 377 |                 int err = requestPriority(prioEvent->pid(), prioEvent->tid(), prioEvent->prio(), | 
 | 378 |                         true /*asynchronous*/); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 379 |                 if (err != 0) { | 
 | 380 |                     ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; " | 
 | 381 |                           "error %d", | 
 | 382 |                           prioEvent->prio(), prioEvent->pid(), prioEvent->tid(), err); | 
 | 383 |                 } | 
 | 384 |             } break; | 
 | 385 |             case CFG_EVENT_IO: { | 
 | 386 |                 IoConfigEvent *ioEvent = static_cast<IoConfigEvent *>(event); | 
 | 387 |                 mAudioFlinger->mLock.lock(); | 
 | 388 |                 audioConfigChanged_l(ioEvent->event(), ioEvent->param()); | 
 | 389 |                 mAudioFlinger->mLock.unlock(); | 
 | 390 |             } break; | 
 | 391 |             default: | 
 | 392 |                 ALOGE("processConfigEvents() unknown event type %d", event->type()); | 
 | 393 |                 break; | 
 | 394 |         } | 
 | 395 |         delete event; | 
 | 396 |         mLock.lock(); | 
 | 397 |     } | 
 | 398 |     mLock.unlock(); | 
 | 399 | } | 
 | 400 |  | 
 | 401 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args) | 
 | 402 | { | 
 | 403 |     const size_t SIZE = 256; | 
 | 404 |     char buffer[SIZE]; | 
 | 405 |     String8 result; | 
 | 406 |  | 
 | 407 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 408 |     if (!locked) { | 
 | 409 |         snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this); | 
 | 410 |         write(fd, buffer, strlen(buffer)); | 
 | 411 |     } | 
 | 412 |  | 
 | 413 |     snprintf(buffer, SIZE, "io handle: %d\n", mId); | 
 | 414 |     result.append(buffer); | 
 | 415 |     snprintf(buffer, SIZE, "TID: %d\n", getTid()); | 
 | 416 |     result.append(buffer); | 
 | 417 |     snprintf(buffer, SIZE, "standby: %d\n", mStandby); | 
 | 418 |     result.append(buffer); | 
 | 419 |     snprintf(buffer, SIZE, "Sample rate: %u\n", mSampleRate); | 
 | 420 |     result.append(buffer); | 
 | 421 |     snprintf(buffer, SIZE, "HAL frame count: %d\n", mFrameCount); | 
 | 422 |     result.append(buffer); | 
 | 423 |     snprintf(buffer, SIZE, "Normal frame count: %d\n", mNormalFrameCount); | 
 | 424 |     result.append(buffer); | 
 | 425 |     snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount); | 
 | 426 |     result.append(buffer); | 
 | 427 |     snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask); | 
 | 428 |     result.append(buffer); | 
 | 429 |     snprintf(buffer, SIZE, "Format: %d\n", mFormat); | 
 | 430 |     result.append(buffer); | 
 | 431 |     snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize); | 
 | 432 |     result.append(buffer); | 
 | 433 |  | 
 | 434 |     snprintf(buffer, SIZE, "\nPending setParameters commands: \n"); | 
 | 435 |     result.append(buffer); | 
 | 436 |     result.append(" Index Command"); | 
 | 437 |     for (size_t i = 0; i < mNewParameters.size(); ++i) { | 
 | 438 |         snprintf(buffer, SIZE, "\n %02d    ", i); | 
 | 439 |         result.append(buffer); | 
 | 440 |         result.append(mNewParameters[i]); | 
 | 441 |     } | 
 | 442 |  | 
 | 443 |     snprintf(buffer, SIZE, "\n\nPending config events: \n"); | 
 | 444 |     result.append(buffer); | 
 | 445 |     for (size_t i = 0; i < mConfigEvents.size(); i++) { | 
 | 446 |         mConfigEvents[i]->dump(buffer, SIZE); | 
 | 447 |         result.append(buffer); | 
 | 448 |     } | 
 | 449 |     result.append("\n"); | 
 | 450 |  | 
 | 451 |     write(fd, result.string(), result.size()); | 
 | 452 |  | 
 | 453 |     if (locked) { | 
 | 454 |         mLock.unlock(); | 
 | 455 |     } | 
 | 456 | } | 
 | 457 |  | 
 | 458 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) | 
 | 459 | { | 
 | 460 |     const size_t SIZE = 256; | 
 | 461 |     char buffer[SIZE]; | 
 | 462 |     String8 result; | 
 | 463 |  | 
 | 464 |     snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size()); | 
 | 465 |     write(fd, buffer, strlen(buffer)); | 
 | 466 |  | 
 | 467 |     for (size_t i = 0; i < mEffectChains.size(); ++i) { | 
 | 468 |         sp<EffectChain> chain = mEffectChains[i]; | 
 | 469 |         if (chain != 0) { | 
 | 470 |             chain->dump(fd, args); | 
 | 471 |         } | 
 | 472 |     } | 
 | 473 | } | 
 | 474 |  | 
 | 475 | void AudioFlinger::ThreadBase::acquireWakeLock() | 
 | 476 | { | 
 | 477 |     Mutex::Autolock _l(mLock); | 
 | 478 |     acquireWakeLock_l(); | 
 | 479 | } | 
 | 480 |  | 
 | 481 | void AudioFlinger::ThreadBase::acquireWakeLock_l() | 
 | 482 | { | 
 | 483 |     if (mPowerManager == 0) { | 
 | 484 |         // use checkService() to avoid blocking if power service is not up yet | 
 | 485 |         sp<IBinder> binder = | 
 | 486 |             defaultServiceManager()->checkService(String16("power")); | 
 | 487 |         if (binder == 0) { | 
 | 488 |             ALOGW("Thread %s cannot connect to the power manager service", mName); | 
 | 489 |         } else { | 
 | 490 |             mPowerManager = interface_cast<IPowerManager>(binder); | 
 | 491 |             binder->linkToDeath(mDeathRecipient); | 
 | 492 |         } | 
 | 493 |     } | 
 | 494 |     if (mPowerManager != 0) { | 
 | 495 |         sp<IBinder> binder = new BBinder(); | 
 | 496 |         status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, | 
 | 497 |                                                          binder, | 
| Dianne Hackborn | 61d404e | 2013-05-20 11:22:20 -0700 | [diff] [blame] | 498 |                                                          String16(mName), | 
 | 499 |                                                          String16("media")); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 500 |         if (status == NO_ERROR) { | 
 | 501 |             mWakeLockToken = binder; | 
 | 502 |         } | 
 | 503 |         ALOGV("acquireWakeLock_l() %s status %d", mName, status); | 
 | 504 |     } | 
 | 505 | } | 
 | 506 |  | 
 | 507 | void AudioFlinger::ThreadBase::releaseWakeLock() | 
 | 508 | { | 
 | 509 |     Mutex::Autolock _l(mLock); | 
 | 510 |     releaseWakeLock_l(); | 
 | 511 | } | 
 | 512 |  | 
 | 513 | void AudioFlinger::ThreadBase::releaseWakeLock_l() | 
 | 514 | { | 
 | 515 |     if (mWakeLockToken != 0) { | 
 | 516 |         ALOGV("releaseWakeLock_l() %s", mName); | 
 | 517 |         if (mPowerManager != 0) { | 
 | 518 |             mPowerManager->releaseWakeLock(mWakeLockToken, 0); | 
 | 519 |         } | 
 | 520 |         mWakeLockToken.clear(); | 
 | 521 |     } | 
 | 522 | } | 
 | 523 |  | 
 | 524 | void AudioFlinger::ThreadBase::clearPowerManager() | 
 | 525 | { | 
 | 526 |     Mutex::Autolock _l(mLock); | 
 | 527 |     releaseWakeLock_l(); | 
 | 528 |     mPowerManager.clear(); | 
 | 529 | } | 
 | 530 |  | 
 | 531 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who) | 
 | 532 | { | 
 | 533 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 534 |     if (thread != 0) { | 
 | 535 |         thread->clearPowerManager(); | 
 | 536 |     } | 
 | 537 |     ALOGW("power manager service died !!!"); | 
 | 538 | } | 
 | 539 |  | 
 | 540 | void AudioFlinger::ThreadBase::setEffectSuspended( | 
 | 541 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 542 | { | 
 | 543 |     Mutex::Autolock _l(mLock); | 
 | 544 |     setEffectSuspended_l(type, suspend, sessionId); | 
 | 545 | } | 
 | 546 |  | 
 | 547 | void AudioFlinger::ThreadBase::setEffectSuspended_l( | 
 | 548 |         const effect_uuid_t *type, bool suspend, int sessionId) | 
 | 549 | { | 
 | 550 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 551 |     if (chain != 0) { | 
 | 552 |         if (type != NULL) { | 
 | 553 |             chain->setEffectSuspended_l(type, suspend); | 
 | 554 |         } else { | 
 | 555 |             chain->setEffectSuspendedAll_l(suspend); | 
 | 556 |         } | 
 | 557 |     } | 
 | 558 |  | 
 | 559 |     updateSuspendedSessions_l(type, suspend, sessionId); | 
 | 560 | } | 
 | 561 |  | 
 | 562 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) | 
 | 563 | { | 
 | 564 |     ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); | 
 | 565 |     if (index < 0) { | 
 | 566 |         return; | 
 | 567 |     } | 
 | 568 |  | 
 | 569 |     const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = | 
 | 570 |             mSuspendedSessions.valueAt(index); | 
 | 571 |  | 
 | 572 |     for (size_t i = 0; i < sessionEffects.size(); i++) { | 
 | 573 |         sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); | 
 | 574 |         for (int j = 0; j < desc->mRefCount; j++) { | 
 | 575 |             if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { | 
 | 576 |                 chain->setEffectSuspendedAll_l(true); | 
 | 577 |             } else { | 
 | 578 |                 ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", | 
 | 579 |                     desc->mType.timeLow); | 
 | 580 |                 chain->setEffectSuspended_l(&desc->mType, true); | 
 | 581 |             } | 
 | 582 |         } | 
 | 583 |     } | 
 | 584 | } | 
 | 585 |  | 
 | 586 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, | 
 | 587 |                                                          bool suspend, | 
 | 588 |                                                          int sessionId) | 
 | 589 | { | 
 | 590 |     ssize_t index = mSuspendedSessions.indexOfKey(sessionId); | 
 | 591 |  | 
 | 592 |     KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; | 
 | 593 |  | 
 | 594 |     if (suspend) { | 
 | 595 |         if (index >= 0) { | 
 | 596 |             sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 597 |         } else { | 
 | 598 |             mSuspendedSessions.add(sessionId, sessionEffects); | 
 | 599 |         } | 
 | 600 |     } else { | 
 | 601 |         if (index < 0) { | 
 | 602 |             return; | 
 | 603 |         } | 
 | 604 |         sessionEffects = mSuspendedSessions.valueAt(index); | 
 | 605 |     } | 
 | 606 |  | 
 | 607 |  | 
 | 608 |     int key = EffectChain::kKeyForSuspendAll; | 
 | 609 |     if (type != NULL) { | 
 | 610 |         key = type->timeLow; | 
 | 611 |     } | 
 | 612 |     index = sessionEffects.indexOfKey(key); | 
 | 613 |  | 
 | 614 |     sp<SuspendedSessionDesc> desc; | 
 | 615 |     if (suspend) { | 
 | 616 |         if (index >= 0) { | 
 | 617 |             desc = sessionEffects.valueAt(index); | 
 | 618 |         } else { | 
 | 619 |             desc = new SuspendedSessionDesc(); | 
 | 620 |             if (type != NULL) { | 
 | 621 |                 desc->mType = *type; | 
 | 622 |             } | 
 | 623 |             sessionEffects.add(key, desc); | 
 | 624 |             ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); | 
 | 625 |         } | 
 | 626 |         desc->mRefCount++; | 
 | 627 |     } else { | 
 | 628 |         if (index < 0) { | 
 | 629 |             return; | 
 | 630 |         } | 
 | 631 |         desc = sessionEffects.valueAt(index); | 
 | 632 |         if (--desc->mRefCount == 0) { | 
 | 633 |             ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); | 
 | 634 |             sessionEffects.removeItemsAt(index); | 
 | 635 |             if (sessionEffects.isEmpty()) { | 
 | 636 |                 ALOGV("updateSuspendedSessions_l() restore removing session %d", | 
 | 637 |                                  sessionId); | 
 | 638 |                 mSuspendedSessions.removeItem(sessionId); | 
 | 639 |             } | 
 | 640 |         } | 
 | 641 |     } | 
 | 642 |     if (!sessionEffects.isEmpty()) { | 
 | 643 |         mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); | 
 | 644 |     } | 
 | 645 | } | 
 | 646 |  | 
 | 647 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
 | 648 |                                                             bool enabled, | 
 | 649 |                                                             int sessionId) | 
 | 650 | { | 
 | 651 |     Mutex::Autolock _l(mLock); | 
 | 652 |     checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); | 
 | 653 | } | 
 | 654 |  | 
 | 655 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, | 
 | 656 |                                                             bool enabled, | 
 | 657 |                                                             int sessionId) | 
 | 658 | { | 
 | 659 |     if (mType != RECORD) { | 
 | 660 |         // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on | 
 | 661 |         // another session. This gives the priority to well behaved effect control panels | 
 | 662 |         // and applications not using global effects. | 
 | 663 |         // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect | 
 | 664 |         // global effects | 
 | 665 |         if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { | 
 | 666 |             setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); | 
 | 667 |         } | 
 | 668 |     } | 
 | 669 |  | 
 | 670 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 671 |     if (chain != 0) { | 
 | 672 |         chain->checkSuspendOnEffectEnabled(effect, enabled); | 
 | 673 |     } | 
 | 674 | } | 
 | 675 |  | 
 | 676 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held | 
 | 677 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( | 
 | 678 |         const sp<AudioFlinger::Client>& client, | 
 | 679 |         const sp<IEffectClient>& effectClient, | 
 | 680 |         int32_t priority, | 
 | 681 |         int sessionId, | 
 | 682 |         effect_descriptor_t *desc, | 
 | 683 |         int *enabled, | 
 | 684 |         status_t *status | 
 | 685 |         ) | 
 | 686 | { | 
 | 687 |     sp<EffectModule> effect; | 
 | 688 |     sp<EffectHandle> handle; | 
 | 689 |     status_t lStatus; | 
 | 690 |     sp<EffectChain> chain; | 
 | 691 |     bool chainCreated = false; | 
 | 692 |     bool effectCreated = false; | 
 | 693 |     bool effectRegistered = false; | 
 | 694 |  | 
 | 695 |     lStatus = initCheck(); | 
 | 696 |     if (lStatus != NO_ERROR) { | 
 | 697 |         ALOGW("createEffect_l() Audio driver not initialized."); | 
 | 698 |         goto Exit; | 
 | 699 |     } | 
 | 700 |  | 
 | 701 |     // Do not allow effects with session ID 0 on direct output or duplicating threads | 
 | 702 |     // TODO: add rule for hw accelerated effects on direct outputs with non PCM format | 
 | 703 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) { | 
 | 704 |         ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d", | 
 | 705 |                 desc->name, sessionId); | 
 | 706 |         lStatus = BAD_VALUE; | 
 | 707 |         goto Exit; | 
 | 708 |     } | 
 | 709 |     // Only Pre processor effects are allowed on input threads and only on input threads | 
 | 710 |     if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { | 
 | 711 |         ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", | 
 | 712 |                 desc->name, desc->flags, mType); | 
 | 713 |         lStatus = BAD_VALUE; | 
 | 714 |         goto Exit; | 
 | 715 |     } | 
 | 716 |  | 
 | 717 |     ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); | 
 | 718 |  | 
 | 719 |     { // scope for mLock | 
 | 720 |         Mutex::Autolock _l(mLock); | 
 | 721 |  | 
 | 722 |         // check for existing effect chain with the requested audio session | 
 | 723 |         chain = getEffectChain_l(sessionId); | 
 | 724 |         if (chain == 0) { | 
 | 725 |             // create a new chain for this session | 
 | 726 |             ALOGV("createEffect_l() new effect chain for session %d", sessionId); | 
 | 727 |             chain = new EffectChain(this, sessionId); | 
 | 728 |             addEffectChain_l(chain); | 
 | 729 |             chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 730 |             chainCreated = true; | 
 | 731 |         } else { | 
 | 732 |             effect = chain->getEffectFromDesc_l(desc); | 
 | 733 |         } | 
 | 734 |  | 
 | 735 |         ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); | 
 | 736 |  | 
 | 737 |         if (effect == 0) { | 
 | 738 |             int id = mAudioFlinger->nextUniqueId(); | 
 | 739 |             // Check CPU and memory usage | 
 | 740 |             lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); | 
 | 741 |             if (lStatus != NO_ERROR) { | 
 | 742 |                 goto Exit; | 
 | 743 |             } | 
 | 744 |             effectRegistered = true; | 
 | 745 |             // create a new effect module if none present in the chain | 
 | 746 |             effect = new EffectModule(this, chain, desc, id, sessionId); | 
 | 747 |             lStatus = effect->status(); | 
 | 748 |             if (lStatus != NO_ERROR) { | 
 | 749 |                 goto Exit; | 
 | 750 |             } | 
 | 751 |             lStatus = chain->addEffect_l(effect); | 
 | 752 |             if (lStatus != NO_ERROR) { | 
 | 753 |                 goto Exit; | 
 | 754 |             } | 
 | 755 |             effectCreated = true; | 
 | 756 |  | 
 | 757 |             effect->setDevice(mOutDevice); | 
 | 758 |             effect->setDevice(mInDevice); | 
 | 759 |             effect->setMode(mAudioFlinger->getMode()); | 
 | 760 |             effect->setAudioSource(mAudioSource); | 
 | 761 |         } | 
 | 762 |         // create effect handle and connect it to effect module | 
 | 763 |         handle = new EffectHandle(effect, client, effectClient, priority); | 
 | 764 |         lStatus = effect->addHandle(handle.get()); | 
 | 765 |         if (enabled != NULL) { | 
 | 766 |             *enabled = (int)effect->isEnabled(); | 
 | 767 |         } | 
 | 768 |     } | 
 | 769 |  | 
 | 770 | Exit: | 
 | 771 |     if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
 | 772 |         Mutex::Autolock _l(mLock); | 
 | 773 |         if (effectCreated) { | 
 | 774 |             chain->removeEffect_l(effect); | 
 | 775 |         } | 
 | 776 |         if (effectRegistered) { | 
 | 777 |             AudioSystem::unregisterEffect(effect->id()); | 
 | 778 |         } | 
 | 779 |         if (chainCreated) { | 
 | 780 |             removeEffectChain_l(chain); | 
 | 781 |         } | 
 | 782 |         handle.clear(); | 
 | 783 |     } | 
 | 784 |  | 
 | 785 |     if (status != NULL) { | 
 | 786 |         *status = lStatus; | 
 | 787 |     } | 
 | 788 |     return handle; | 
 | 789 | } | 
 | 790 |  | 
 | 791 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) | 
 | 792 | { | 
 | 793 |     Mutex::Autolock _l(mLock); | 
 | 794 |     return getEffect_l(sessionId, effectId); | 
 | 795 | } | 
 | 796 |  | 
 | 797 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) | 
 | 798 | { | 
 | 799 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 800 |     return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; | 
 | 801 | } | 
 | 802 |  | 
 | 803 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and | 
 | 804 | // PlaybackThread::mLock held | 
 | 805 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) | 
 | 806 | { | 
 | 807 |     // check for existing effect chain with the requested audio session | 
 | 808 |     int sessionId = effect->sessionId(); | 
 | 809 |     sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 810 |     bool chainCreated = false; | 
 | 811 |  | 
 | 812 |     if (chain == 0) { | 
 | 813 |         // create a new chain for this session | 
 | 814 |         ALOGV("addEffect_l() new effect chain for session %d", sessionId); | 
 | 815 |         chain = new EffectChain(this, sessionId); | 
 | 816 |         addEffectChain_l(chain); | 
 | 817 |         chain->setStrategy(getStrategyForSession_l(sessionId)); | 
 | 818 |         chainCreated = true; | 
 | 819 |     } | 
 | 820 |     ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); | 
 | 821 |  | 
 | 822 |     if (chain->getEffectFromId_l(effect->id()) != 0) { | 
 | 823 |         ALOGW("addEffect_l() %p effect %s already present in chain %p", | 
 | 824 |                 this, effect->desc().name, chain.get()); | 
 | 825 |         return BAD_VALUE; | 
 | 826 |     } | 
 | 827 |  | 
 | 828 |     status_t status = chain->addEffect_l(effect); | 
 | 829 |     if (status != NO_ERROR) { | 
 | 830 |         if (chainCreated) { | 
 | 831 |             removeEffectChain_l(chain); | 
 | 832 |         } | 
 | 833 |         return status; | 
 | 834 |     } | 
 | 835 |  | 
 | 836 |     effect->setDevice(mOutDevice); | 
 | 837 |     effect->setDevice(mInDevice); | 
 | 838 |     effect->setMode(mAudioFlinger->getMode()); | 
 | 839 |     effect->setAudioSource(mAudioSource); | 
 | 840 |     return NO_ERROR; | 
 | 841 | } | 
 | 842 |  | 
 | 843 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { | 
 | 844 |  | 
 | 845 |     ALOGV("removeEffect_l() %p effect %p", this, effect.get()); | 
 | 846 |     effect_descriptor_t desc = effect->desc(); | 
 | 847 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 848 |         detachAuxEffect_l(effect->id()); | 
 | 849 |     } | 
 | 850 |  | 
 | 851 |     sp<EffectChain> chain = effect->chain().promote(); | 
 | 852 |     if (chain != 0) { | 
 | 853 |         // remove effect chain if removing last effect | 
 | 854 |         if (chain->removeEffect_l(effect) == 0) { | 
 | 855 |             removeEffectChain_l(chain); | 
 | 856 |         } | 
 | 857 |     } else { | 
 | 858 |         ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); | 
 | 859 |     } | 
 | 860 | } | 
 | 861 |  | 
 | 862 | void AudioFlinger::ThreadBase::lockEffectChains_l( | 
 | 863 |         Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 864 | { | 
 | 865 |     effectChains = mEffectChains; | 
 | 866 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 867 |         mEffectChains[i]->lock(); | 
 | 868 |     } | 
 | 869 | } | 
 | 870 |  | 
 | 871 | void AudioFlinger::ThreadBase::unlockEffectChains( | 
 | 872 |         const Vector< sp<AudioFlinger::EffectChain> >& effectChains) | 
 | 873 | { | 
 | 874 |     for (size_t i = 0; i < effectChains.size(); i++) { | 
 | 875 |         effectChains[i]->unlock(); | 
 | 876 |     } | 
 | 877 | } | 
 | 878 |  | 
 | 879 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) | 
 | 880 | { | 
 | 881 |     Mutex::Autolock _l(mLock); | 
 | 882 |     return getEffectChain_l(sessionId); | 
 | 883 | } | 
 | 884 |  | 
 | 885 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const | 
 | 886 | { | 
 | 887 |     size_t size = mEffectChains.size(); | 
 | 888 |     for (size_t i = 0; i < size; i++) { | 
 | 889 |         if (mEffectChains[i]->sessionId() == sessionId) { | 
 | 890 |             return mEffectChains[i]; | 
 | 891 |         } | 
 | 892 |     } | 
 | 893 |     return 0; | 
 | 894 | } | 
 | 895 |  | 
 | 896 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) | 
 | 897 | { | 
 | 898 |     Mutex::Autolock _l(mLock); | 
 | 899 |     size_t size = mEffectChains.size(); | 
 | 900 |     for (size_t i = 0; i < size; i++) { | 
 | 901 |         mEffectChains[i]->setMode_l(mode); | 
 | 902 |     } | 
 | 903 | } | 
 | 904 |  | 
 | 905 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, | 
 | 906 |                                                     EffectHandle *handle, | 
 | 907 |                                                     bool unpinIfLast) { | 
 | 908 |  | 
 | 909 |     Mutex::Autolock _l(mLock); | 
 | 910 |     ALOGV("disconnectEffect() %p effect %p", this, effect.get()); | 
 | 911 |     // delete the effect module if removing last handle on it | 
 | 912 |     if (effect->removeHandle(handle) == 0) { | 
 | 913 |         if (!effect->isPinned() || unpinIfLast) { | 
 | 914 |             removeEffect_l(effect); | 
 | 915 |             AudioSystem::unregisterEffect(effect->id()); | 
 | 916 |         } | 
 | 917 |     } | 
 | 918 | } | 
 | 919 |  | 
 | 920 | // ---------------------------------------------------------------------------- | 
 | 921 | //      Playback | 
 | 922 | // ---------------------------------------------------------------------------- | 
 | 923 |  | 
 | 924 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, | 
 | 925 |                                              AudioStreamOut* output, | 
 | 926 |                                              audio_io_handle_t id, | 
 | 927 |                                              audio_devices_t device, | 
 | 928 |                                              type_t type) | 
 | 929 |     :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), | 
 | 930 |         mMixBuffer(NULL), mSuspended(0), mBytesWritten(0), | 
 | 931 |         // mStreamTypes[] initialized in constructor body | 
 | 932 |         mOutput(output), | 
 | 933 |         mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), | 
 | 934 |         mMixerStatus(MIXER_IDLE), | 
 | 935 |         mMixerStatusIgnoringFastTracks(MIXER_IDLE), | 
 | 936 |         standbyDelay(AudioFlinger::mStandbyTimeInNsecs), | 
 | 937 |         mScreenState(AudioFlinger::mScreenState), | 
 | 938 |         // index 0 is reserved for normal mixer's submix | 
 | 939 |         mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1) | 
 | 940 | { | 
 | 941 |     snprintf(mName, kNameLength, "AudioOut_%X", id); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 942 |     mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 943 |  | 
 | 944 |     // Assumes constructor is called by AudioFlinger with it's mLock held, but | 
 | 945 |     // it would be safer to explicitly pass initial masterVolume/masterMute as | 
 | 946 |     // parameter. | 
 | 947 |     // | 
 | 948 |     // If the HAL we are using has support for master volume or master mute, | 
 | 949 |     // then do not attenuate or mute during mixing (just leave the volume at 1.0 | 
 | 950 |     // and the mute set to false). | 
 | 951 |     mMasterVolume = audioFlinger->masterVolume_l(); | 
 | 952 |     mMasterMute = audioFlinger->masterMute_l(); | 
 | 953 |     if (mOutput && mOutput->audioHwDev) { | 
 | 954 |         if (mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 955 |             mMasterVolume = 1.0; | 
 | 956 |         } | 
 | 957 |  | 
 | 958 |         if (mOutput->audioHwDev->canSetMasterMute()) { | 
 | 959 |             mMasterMute = false; | 
 | 960 |         } | 
 | 961 |     } | 
 | 962 |  | 
 | 963 |     readOutputParameters(); | 
 | 964 |  | 
 | 965 |     // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor | 
 | 966 |     // There is no AUDIO_STREAM_MIN, and ++ operator does not compile | 
 | 967 |     for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT; | 
 | 968 |             stream = (audio_stream_type_t) (stream + 1)) { | 
 | 969 |         mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); | 
 | 970 |         mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); | 
 | 971 |     } | 
 | 972 |     // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, | 
 | 973 |     // because mAudioFlinger doesn't have one to copy from | 
 | 974 | } | 
 | 975 |  | 
 | 976 | AudioFlinger::PlaybackThread::~PlaybackThread() | 
 | 977 | { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 978 |     mAudioFlinger->unregisterWriter(mNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 979 |     delete [] mMixBuffer; | 
 | 980 | } | 
 | 981 |  | 
 | 982 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) | 
 | 983 | { | 
 | 984 |     dumpInternals(fd, args); | 
 | 985 |     dumpTracks(fd, args); | 
 | 986 |     dumpEffectChains(fd, args); | 
 | 987 | } | 
 | 988 |  | 
 | 989 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args) | 
 | 990 | { | 
 | 991 |     const size_t SIZE = 256; | 
 | 992 |     char buffer[SIZE]; | 
 | 993 |     String8 result; | 
 | 994 |  | 
 | 995 |     result.appendFormat("Output thread %p stream volumes in dB:\n    ", this); | 
 | 996 |     for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { | 
 | 997 |         const stream_type_t *st = &mStreamTypes[i]; | 
 | 998 |         if (i > 0) { | 
 | 999 |             result.appendFormat(", "); | 
 | 1000 |         } | 
 | 1001 |         result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); | 
 | 1002 |         if (st->mute) { | 
 | 1003 |             result.append("M"); | 
 | 1004 |         } | 
 | 1005 |     } | 
 | 1006 |     result.append("\n"); | 
 | 1007 |     write(fd, result.string(), result.length()); | 
 | 1008 |     result.clear(); | 
 | 1009 |  | 
 | 1010 |     snprintf(buffer, SIZE, "Output thread %p tracks\n", this); | 
 | 1011 |     result.append(buffer); | 
 | 1012 |     Track::appendDumpHeader(result); | 
 | 1013 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1014 |         sp<Track> track = mTracks[i]; | 
 | 1015 |         if (track != 0) { | 
 | 1016 |             track->dump(buffer, SIZE); | 
 | 1017 |             result.append(buffer); | 
 | 1018 |         } | 
 | 1019 |     } | 
 | 1020 |  | 
 | 1021 |     snprintf(buffer, SIZE, "Output thread %p active tracks\n", this); | 
 | 1022 |     result.append(buffer); | 
 | 1023 |     Track::appendDumpHeader(result); | 
 | 1024 |     for (size_t i = 0; i < mActiveTracks.size(); ++i) { | 
 | 1025 |         sp<Track> track = mActiveTracks[i].promote(); | 
 | 1026 |         if (track != 0) { | 
 | 1027 |             track->dump(buffer, SIZE); | 
 | 1028 |             result.append(buffer); | 
 | 1029 |         } | 
 | 1030 |     } | 
 | 1031 |     write(fd, result.string(), result.size()); | 
 | 1032 |  | 
 | 1033 |     // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way. | 
 | 1034 |     FastTrackUnderruns underruns = getFastTrackUnderruns(0); | 
 | 1035 |     fdprintf(fd, "Normal mixer raw underrun counters: partial=%u empty=%u\n", | 
 | 1036 |             underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); | 
 | 1037 | } | 
 | 1038 |  | 
 | 1039 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 1040 | { | 
 | 1041 |     const size_t SIZE = 256; | 
 | 1042 |     char buffer[SIZE]; | 
 | 1043 |     String8 result; | 
 | 1044 |  | 
 | 1045 |     snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this); | 
 | 1046 |     result.append(buffer); | 
 | 1047 |     snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", | 
 | 1048 |             ns2ms(systemTime() - mLastWriteTime)); | 
 | 1049 |     result.append(buffer); | 
 | 1050 |     snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites); | 
 | 1051 |     result.append(buffer); | 
 | 1052 |     snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites); | 
 | 1053 |     result.append(buffer); | 
 | 1054 |     snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite); | 
 | 1055 |     result.append(buffer); | 
 | 1056 |     snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended); | 
 | 1057 |     result.append(buffer); | 
 | 1058 |     snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer); | 
 | 1059 |     result.append(buffer); | 
 | 1060 |     write(fd, result.string(), result.size()); | 
 | 1061 |     fdprintf(fd, "Fast track availMask=%#x\n", mFastTrackAvailMask); | 
 | 1062 |  | 
 | 1063 |     dumpBase(fd, args); | 
 | 1064 | } | 
 | 1065 |  | 
 | 1066 | // Thread virtuals | 
 | 1067 | status_t AudioFlinger::PlaybackThread::readyToRun() | 
 | 1068 | { | 
 | 1069 |     status_t status = initCheck(); | 
 | 1070 |     if (status == NO_ERROR) { | 
 | 1071 |         ALOGI("AudioFlinger's thread %p ready to run", this); | 
 | 1072 |     } else { | 
 | 1073 |         ALOGE("No working audio driver found."); | 
 | 1074 |     } | 
 | 1075 |     return status; | 
 | 1076 | } | 
 | 1077 |  | 
 | 1078 | void AudioFlinger::PlaybackThread::onFirstRef() | 
 | 1079 | { | 
 | 1080 |     run(mName, ANDROID_PRIORITY_URGENT_AUDIO); | 
 | 1081 | } | 
 | 1082 |  | 
 | 1083 | // ThreadBase virtuals | 
 | 1084 | void AudioFlinger::PlaybackThread::preExit() | 
 | 1085 | { | 
 | 1086 |     ALOGV("  preExit()"); | 
 | 1087 |     // FIXME this is using hard-coded strings but in the future, this functionality will be | 
 | 1088 |     //       converted to use audio HAL extensions required to support tunneling | 
 | 1089 |     mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); | 
 | 1090 | } | 
 | 1091 |  | 
 | 1092 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held | 
 | 1093 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( | 
 | 1094 |         const sp<AudioFlinger::Client>& client, | 
 | 1095 |         audio_stream_type_t streamType, | 
 | 1096 |         uint32_t sampleRate, | 
 | 1097 |         audio_format_t format, | 
 | 1098 |         audio_channel_mask_t channelMask, | 
 | 1099 |         size_t frameCount, | 
 | 1100 |         const sp<IMemory>& sharedBuffer, | 
 | 1101 |         int sessionId, | 
 | 1102 |         IAudioFlinger::track_flags_t *flags, | 
 | 1103 |         pid_t tid, | 
 | 1104 |         status_t *status) | 
 | 1105 | { | 
 | 1106 |     sp<Track> track; | 
 | 1107 |     status_t lStatus; | 
 | 1108 |  | 
 | 1109 |     bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; | 
 | 1110 |  | 
 | 1111 |     // client expresses a preference for FAST, but we get the final say | 
 | 1112 |     if (*flags & IAudioFlinger::TRACK_FAST) { | 
 | 1113 |       if ( | 
 | 1114 |             // not timed | 
 | 1115 |             (!isTimed) && | 
 | 1116 |             // either of these use cases: | 
 | 1117 |             ( | 
 | 1118 |               // use case 1: shared buffer with any frame count | 
 | 1119 |               ( | 
 | 1120 |                 (sharedBuffer != 0) | 
 | 1121 |               ) || | 
 | 1122 |               // use case 2: callback handler and frame count is default or at least as large as HAL | 
 | 1123 |               ( | 
 | 1124 |                 (tid != -1) && | 
 | 1125 |                 ((frameCount == 0) || | 
 | 1126 |                 (frameCount >= (mFrameCount * kFastTrackMultiplier))) | 
 | 1127 |               ) | 
 | 1128 |             ) && | 
 | 1129 |             // PCM data | 
 | 1130 |             audio_is_linear_pcm(format) && | 
 | 1131 |             // mono or stereo | 
 | 1132 |             ( (channelMask == AUDIO_CHANNEL_OUT_MONO) || | 
 | 1133 |               (channelMask == AUDIO_CHANNEL_OUT_STEREO) ) && | 
 | 1134 | #ifndef FAST_TRACKS_AT_NON_NATIVE_SAMPLE_RATE | 
 | 1135 |             // hardware sample rate | 
 | 1136 |             (sampleRate == mSampleRate) && | 
 | 1137 | #endif | 
 | 1138 |             // normal mixer has an associated fast mixer | 
 | 1139 |             hasFastMixer() && | 
 | 1140 |             // there are sufficient fast track slots available | 
 | 1141 |             (mFastTrackAvailMask != 0) | 
 | 1142 |             // FIXME test that MixerThread for this fast track has a capable output HAL | 
 | 1143 |             // FIXME add a permission test also? | 
 | 1144 |         ) { | 
 | 1145 |         // if frameCount not specified, then it defaults to fast mixer (HAL) frame count | 
 | 1146 |         if (frameCount == 0) { | 
 | 1147 |             frameCount = mFrameCount * kFastTrackMultiplier; | 
 | 1148 |         } | 
 | 1149 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", | 
 | 1150 |                 frameCount, mFrameCount); | 
 | 1151 |       } else { | 
 | 1152 |         ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " | 
 | 1153 |                 "mFrameCount=%d format=%d isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " | 
 | 1154 |                 "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", | 
 | 1155 |                 isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, | 
 | 1156 |                 audio_is_linear_pcm(format), | 
 | 1157 |                 channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); | 
 | 1158 |         *flags &= ~IAudioFlinger::TRACK_FAST; | 
 | 1159 |         // For compatibility with AudioTrack calculation, buffer depth is forced | 
 | 1160 |         // to be at least 2 x the normal mixer frame count and cover audio hardware latency. | 
 | 1161 |         // This is probably too conservative, but legacy application code may depend on it. | 
 | 1162 |         // If you change this calculation, also review the start threshold which is related. | 
 | 1163 |         uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); | 
 | 1164 |         uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); | 
 | 1165 |         if (minBufCount < 2) { | 
 | 1166 |             minBufCount = 2; | 
 | 1167 |         } | 
 | 1168 |         size_t minFrameCount = mNormalFrameCount * minBufCount; | 
 | 1169 |         if (frameCount < minFrameCount) { | 
 | 1170 |             frameCount = minFrameCount; | 
 | 1171 |         } | 
 | 1172 |       } | 
 | 1173 |     } | 
 | 1174 |  | 
 | 1175 |     if (mType == DIRECT) { | 
 | 1176 |         if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) { | 
 | 1177 |             if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { | 
 | 1178 |                 ALOGE("createTrack_l() Bad parameter: sampleRate %u format %d, channelMask 0x%08x " | 
 | 1179 |                         "for output %p with format %d", | 
 | 1180 |                         sampleRate, format, channelMask, mOutput, mFormat); | 
 | 1181 |                 lStatus = BAD_VALUE; | 
 | 1182 |                 goto Exit; | 
 | 1183 |             } | 
 | 1184 |         } | 
 | 1185 |     } else { | 
 | 1186 |         // Resampler implementation limits input sampling rate to 2 x output sampling rate. | 
 | 1187 |         if (sampleRate > mSampleRate*2) { | 
 | 1188 |             ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); | 
 | 1189 |             lStatus = BAD_VALUE; | 
 | 1190 |             goto Exit; | 
 | 1191 |         } | 
 | 1192 |     } | 
 | 1193 |  | 
 | 1194 |     lStatus = initCheck(); | 
 | 1195 |     if (lStatus != NO_ERROR) { | 
 | 1196 |         ALOGE("Audio driver not initialized."); | 
 | 1197 |         goto Exit; | 
 | 1198 |     } | 
 | 1199 |  | 
 | 1200 |     { // scope for mLock | 
 | 1201 |         Mutex::Autolock _l(mLock); | 
 | 1202 |  | 
 | 1203 |         // all tracks in same audio session must share the same routing strategy otherwise | 
 | 1204 |         // conflicts will happen when tracks are moved from one output to another by audio policy | 
 | 1205 |         // manager | 
 | 1206 |         uint32_t strategy = AudioSystem::getStrategyForStream(streamType); | 
 | 1207 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1208 |             sp<Track> t = mTracks[i]; | 
 | 1209 |             if (t != 0 && !t->isOutputTrack()) { | 
 | 1210 |                 uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); | 
 | 1211 |                 if (sessionId == t->sessionId() && strategy != actual) { | 
 | 1212 |                     ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", | 
 | 1213 |                             strategy, actual); | 
 | 1214 |                     lStatus = BAD_VALUE; | 
 | 1215 |                     goto Exit; | 
 | 1216 |                 } | 
 | 1217 |             } | 
 | 1218 |         } | 
 | 1219 |  | 
 | 1220 |         if (!isTimed) { | 
 | 1221 |             track = new Track(this, client, streamType, sampleRate, format, | 
 | 1222 |                     channelMask, frameCount, sharedBuffer, sessionId, *flags); | 
 | 1223 |         } else { | 
 | 1224 |             track = TimedTrack::create(this, client, streamType, sampleRate, format, | 
 | 1225 |                     channelMask, frameCount, sharedBuffer, sessionId); | 
 | 1226 |         } | 
 | 1227 |         if (track == 0 || track->getCblk() == NULL || track->name() < 0) { | 
 | 1228 |             lStatus = NO_MEMORY; | 
 | 1229 |             goto Exit; | 
 | 1230 |         } | 
 | 1231 |         mTracks.add(track); | 
 | 1232 |  | 
 | 1233 |         sp<EffectChain> chain = getEffectChain_l(sessionId); | 
 | 1234 |         if (chain != 0) { | 
 | 1235 |             ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); | 
 | 1236 |             track->setMainBuffer(chain->inBuffer()); | 
 | 1237 |             chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); | 
 | 1238 |             chain->incTrackCnt(); | 
 | 1239 |         } | 
 | 1240 |  | 
 | 1241 |         if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { | 
 | 1242 |             pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
 | 1243 |             // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, | 
 | 1244 |             // so ask activity manager to do this on our behalf | 
 | 1245 |             sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); | 
 | 1246 |         } | 
 | 1247 |     } | 
 | 1248 |  | 
 | 1249 |     lStatus = NO_ERROR; | 
 | 1250 |  | 
 | 1251 | Exit: | 
 | 1252 |     if (status) { | 
 | 1253 |         *status = lStatus; | 
 | 1254 |     } | 
 | 1255 |     return track; | 
 | 1256 | } | 
 | 1257 |  | 
 | 1258 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const | 
 | 1259 | { | 
 | 1260 |     return latency; | 
 | 1261 | } | 
 | 1262 |  | 
 | 1263 | uint32_t AudioFlinger::PlaybackThread::latency() const | 
 | 1264 | { | 
 | 1265 |     Mutex::Autolock _l(mLock); | 
 | 1266 |     return latency_l(); | 
 | 1267 | } | 
 | 1268 | uint32_t AudioFlinger::PlaybackThread::latency_l() const | 
 | 1269 | { | 
 | 1270 |     if (initCheck() == NO_ERROR) { | 
 | 1271 |         return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); | 
 | 1272 |     } else { | 
 | 1273 |         return 0; | 
 | 1274 |     } | 
 | 1275 | } | 
 | 1276 |  | 
 | 1277 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) | 
 | 1278 | { | 
 | 1279 |     Mutex::Autolock _l(mLock); | 
 | 1280 |     // Don't apply master volume in SW if our HAL can do it for us. | 
 | 1281 |     if (mOutput && mOutput->audioHwDev && | 
 | 1282 |         mOutput->audioHwDev->canSetMasterVolume()) { | 
 | 1283 |         mMasterVolume = 1.0; | 
 | 1284 |     } else { | 
 | 1285 |         mMasterVolume = value; | 
 | 1286 |     } | 
 | 1287 | } | 
 | 1288 |  | 
 | 1289 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) | 
 | 1290 | { | 
 | 1291 |     Mutex::Autolock _l(mLock); | 
 | 1292 |     // Don't apply master mute in SW if our HAL can do it for us. | 
 | 1293 |     if (mOutput && mOutput->audioHwDev && | 
 | 1294 |         mOutput->audioHwDev->canSetMasterMute()) { | 
 | 1295 |         mMasterMute = false; | 
 | 1296 |     } else { | 
 | 1297 |         mMasterMute = muted; | 
 | 1298 |     } | 
 | 1299 | } | 
 | 1300 |  | 
 | 1301 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) | 
 | 1302 | { | 
 | 1303 |     Mutex::Autolock _l(mLock); | 
 | 1304 |     mStreamTypes[stream].volume = value; | 
 | 1305 | } | 
 | 1306 |  | 
 | 1307 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) | 
 | 1308 | { | 
 | 1309 |     Mutex::Autolock _l(mLock); | 
 | 1310 |     mStreamTypes[stream].mute = muted; | 
 | 1311 | } | 
 | 1312 |  | 
 | 1313 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const | 
 | 1314 | { | 
 | 1315 |     Mutex::Autolock _l(mLock); | 
 | 1316 |     return mStreamTypes[stream].volume; | 
 | 1317 | } | 
 | 1318 |  | 
 | 1319 | // addTrack_l() must be called with ThreadBase::mLock held | 
 | 1320 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) | 
 | 1321 | { | 
 | 1322 |     status_t status = ALREADY_EXISTS; | 
 | 1323 |  | 
 | 1324 |     // set retry count for buffer fill | 
 | 1325 |     track->mRetryCount = kMaxTrackStartupRetries; | 
 | 1326 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 1327 |         // the track is newly added, make sure it fills up all its | 
 | 1328 |         // buffers before playing. This is to ensure the client will | 
 | 1329 |         // effectively get the latency it requested. | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1330 |         track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 |         track->mResetDone = false; | 
 | 1332 |         track->mPresentationCompleteFrames = 0; | 
 | 1333 |         mActiveTracks.add(track); | 
| Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1334 |         sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1335 |         if (chain != 0) { | 
 | 1336 |             ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), | 
 | 1337 |                     track->sessionId()); | 
 | 1338 |             chain->incActiveTrackCnt(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1339 |         } | 
 | 1340 |  | 
 | 1341 |         status = NO_ERROR; | 
 | 1342 |     } | 
 | 1343 |  | 
 | 1344 |     ALOGV("mWaitWorkCV.broadcast"); | 
 | 1345 |     mWaitWorkCV.broadcast(); | 
 | 1346 |  | 
 | 1347 |     return status; | 
 | 1348 | } | 
 | 1349 |  | 
 | 1350 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
 | 1351 | void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) | 
 | 1352 | { | 
 | 1353 |     track->mState = TrackBase::TERMINATED; | 
 | 1354 |     // active tracks are removed by threadLoop() | 
 | 1355 |     if (mActiveTracks.indexOf(track) < 0) { | 
 | 1356 |         removeTrack_l(track); | 
 | 1357 |     } | 
 | 1358 | } | 
 | 1359 |  | 
 | 1360 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) | 
 | 1361 | { | 
 | 1362 |     track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); | 
 | 1363 |     mTracks.remove(track); | 
 | 1364 |     deleteTrackName_l(track->name()); | 
 | 1365 |     // redundant as track is about to be destroyed, for dumpsys only | 
 | 1366 |     track->mName = -1; | 
 | 1367 |     if (track->isFastTrack()) { | 
 | 1368 |         int index = track->mFastIndex; | 
 | 1369 |         ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); | 
 | 1370 |         ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); | 
 | 1371 |         mFastTrackAvailMask |= 1 << index; | 
 | 1372 |         // redundant as track is about to be destroyed, for dumpsys only | 
 | 1373 |         track->mFastIndex = -1; | 
 | 1374 |     } | 
 | 1375 |     sp<EffectChain> chain = getEffectChain_l(track->sessionId()); | 
 | 1376 |     if (chain != 0) { | 
 | 1377 |         chain->decTrackCnt(); | 
 | 1378 |     } | 
 | 1379 | } | 
 | 1380 |  | 
 | 1381 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) | 
 | 1382 | { | 
 | 1383 |     String8 out_s8 = String8(""); | 
 | 1384 |     char *s; | 
 | 1385 |  | 
 | 1386 |     Mutex::Autolock _l(mLock); | 
 | 1387 |     if (initCheck() != NO_ERROR) { | 
 | 1388 |         return out_s8; | 
 | 1389 |     } | 
 | 1390 |  | 
 | 1391 |     s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); | 
 | 1392 |     out_s8 = String8(s); | 
 | 1393 |     free(s); | 
 | 1394 |     return out_s8; | 
 | 1395 | } | 
 | 1396 |  | 
 | 1397 | // audioConfigChanged_l() must be called with AudioFlinger::mLock held | 
 | 1398 | void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) { | 
 | 1399 |     AudioSystem::OutputDescriptor desc; | 
 | 1400 |     void *param2 = NULL; | 
 | 1401 |  | 
 | 1402 |     ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, | 
 | 1403 |             param); | 
 | 1404 |  | 
 | 1405 |     switch (event) { | 
 | 1406 |     case AudioSystem::OUTPUT_OPENED: | 
 | 1407 |     case AudioSystem::OUTPUT_CONFIG_CHANGED: | 
 | 1408 |         desc.channels = mChannelMask; | 
 | 1409 |         desc.samplingRate = mSampleRate; | 
 | 1410 |         desc.format = mFormat; | 
 | 1411 |         desc.frameCount = mNormalFrameCount; // FIXME see | 
 | 1412 |                                              // AudioFlinger::frameCount(audio_io_handle_t) | 
 | 1413 |         desc.latency = latency(); | 
 | 1414 |         param2 = &desc; | 
 | 1415 |         break; | 
 | 1416 |  | 
 | 1417 |     case AudioSystem::STREAM_CONFIG_CHANGED: | 
 | 1418 |         param2 = ¶m; | 
 | 1419 |     case AudioSystem::OUTPUT_CLOSED: | 
 | 1420 |     default: | 
 | 1421 |         break; | 
 | 1422 |     } | 
 | 1423 |     mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
 | 1424 | } | 
 | 1425 |  | 
 | 1426 | void AudioFlinger::PlaybackThread::readOutputParameters() | 
 | 1427 | { | 
 | 1428 |     mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); | 
 | 1429 |     mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); | 
 | 1430 |     mChannelCount = (uint16_t)popcount(mChannelMask); | 
 | 1431 |     mFormat = mOutput->stream->common.get_format(&mOutput->stream->common); | 
 | 1432 |     mFrameSize = audio_stream_frame_size(&mOutput->stream->common); | 
 | 1433 |     mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize; | 
 | 1434 |     if (mFrameCount & 15) { | 
 | 1435 |         ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", | 
 | 1436 |                 mFrameCount); | 
 | 1437 |     } | 
 | 1438 |  | 
 | 1439 |     // Calculate size of normal mix buffer relative to the HAL output buffer size | 
 | 1440 |     double multiplier = 1.0; | 
 | 1441 |     if (mType == MIXER && (kUseFastMixer == FastMixer_Static || | 
 | 1442 |             kUseFastMixer == FastMixer_Dynamic)) { | 
 | 1443 |         size_t minNormalFrameCount = (kMinNormalMixBufferSizeMs * mSampleRate) / 1000; | 
 | 1444 |         size_t maxNormalFrameCount = (kMaxNormalMixBufferSizeMs * mSampleRate) / 1000; | 
 | 1445 |         // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer | 
 | 1446 |         minNormalFrameCount = (minNormalFrameCount + 15) & ~15; | 
 | 1447 |         maxNormalFrameCount = maxNormalFrameCount & ~15; | 
 | 1448 |         if (maxNormalFrameCount < minNormalFrameCount) { | 
 | 1449 |             maxNormalFrameCount = minNormalFrameCount; | 
 | 1450 |         } | 
 | 1451 |         multiplier = (double) minNormalFrameCount / (double) mFrameCount; | 
 | 1452 |         if (multiplier <= 1.0) { | 
 | 1453 |             multiplier = 1.0; | 
 | 1454 |         } else if (multiplier <= 2.0) { | 
 | 1455 |             if (2 * mFrameCount <= maxNormalFrameCount) { | 
 | 1456 |                 multiplier = 2.0; | 
 | 1457 |             } else { | 
 | 1458 |                 multiplier = (double) maxNormalFrameCount / (double) mFrameCount; | 
 | 1459 |             } | 
 | 1460 |         } else { | 
 | 1461 |             // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL | 
 | 1462 |             // SRC (it would be unusual for the normal mix buffer size to not be a multiple of fast | 
 | 1463 |             // track, but we sometimes have to do this to satisfy the maximum frame count | 
 | 1464 |             // constraint) | 
 | 1465 |             // FIXME this rounding up should not be done if no HAL SRC | 
 | 1466 |             uint32_t truncMult = (uint32_t) multiplier; | 
 | 1467 |             if ((truncMult & 1)) { | 
 | 1468 |                 if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { | 
 | 1469 |                     ++truncMult; | 
 | 1470 |                 } | 
 | 1471 |             } | 
 | 1472 |             multiplier = (double) truncMult; | 
 | 1473 |         } | 
 | 1474 |     } | 
 | 1475 |     mNormalFrameCount = multiplier * mFrameCount; | 
 | 1476 |     // round up to nearest 16 frames to satisfy AudioMixer | 
 | 1477 |     mNormalFrameCount = (mNormalFrameCount + 15) & ~15; | 
 | 1478 |     ALOGI("HAL output buffer size %u frames, normal mix buffer size %u frames", mFrameCount, | 
 | 1479 |             mNormalFrameCount); | 
 | 1480 |  | 
 | 1481 |     delete[] mMixBuffer; | 
 | 1482 |     mMixBuffer = new int16_t[mNormalFrameCount * mChannelCount]; | 
 | 1483 |     memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); | 
 | 1484 |  | 
 | 1485 |     // force reconfiguration of effect chains and engines to take new buffer size and audio | 
 | 1486 |     // parameters into account | 
 | 1487 |     // Note that mLock is not held when readOutputParameters() is called from the constructor | 
 | 1488 |     // but in this case nothing is done below as no audio sessions have effect yet so it doesn't | 
 | 1489 |     // matter. | 
 | 1490 |     // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains | 
 | 1491 |     Vector< sp<EffectChain> > effectChains = mEffectChains; | 
 | 1492 |     for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 1493 |         mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); | 
 | 1494 |     } | 
 | 1495 | } | 
 | 1496 |  | 
 | 1497 |  | 
 | 1498 | status_t AudioFlinger::PlaybackThread::getRenderPosition(size_t *halFrames, size_t *dspFrames) | 
 | 1499 | { | 
 | 1500 |     if (halFrames == NULL || dspFrames == NULL) { | 
 | 1501 |         return BAD_VALUE; | 
 | 1502 |     } | 
 | 1503 |     Mutex::Autolock _l(mLock); | 
 | 1504 |     if (initCheck() != NO_ERROR) { | 
 | 1505 |         return INVALID_OPERATION; | 
 | 1506 |     } | 
 | 1507 |     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 1508 |     *halFrames = framesWritten; | 
 | 1509 |  | 
 | 1510 |     if (isSuspended()) { | 
 | 1511 |         // return an estimation of rendered frames when the output is suspended | 
 | 1512 |         size_t latencyFrames = (latency_l() * mSampleRate) / 1000; | 
 | 1513 |         *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; | 
 | 1514 |         return NO_ERROR; | 
 | 1515 |     } else { | 
 | 1516 |         return mOutput->stream->get_render_position(mOutput->stream, dspFrames); | 
 | 1517 |     } | 
 | 1518 | } | 
 | 1519 |  | 
 | 1520 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const | 
 | 1521 | { | 
 | 1522 |     Mutex::Autolock _l(mLock); | 
 | 1523 |     uint32_t result = 0; | 
 | 1524 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 1525 |         result = EFFECT_SESSION; | 
 | 1526 |     } | 
 | 1527 |  | 
 | 1528 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1529 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1530 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1531 |             result |= TRACK_SESSION; | 
 | 1532 |             break; | 
 | 1533 |         } | 
 | 1534 |     } | 
 | 1535 |  | 
 | 1536 |     return result; | 
 | 1537 | } | 
 | 1538 |  | 
 | 1539 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) | 
 | 1540 | { | 
 | 1541 |     // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that | 
 | 1542 |     // it is moved to correct output by audio policy manager when A2DP is connected or disconnected | 
 | 1543 |     if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1544 |         return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 1545 |     } | 
 | 1546 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 1547 |         sp<Track> track = mTracks[i]; | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1548 |         if (sessionId == track->sessionId() && !track->isInvalid()) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1549 |             return AudioSystem::getStrategyForStream(track->streamType()); | 
 | 1550 |         } | 
 | 1551 |     } | 
 | 1552 |     return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 1553 | } | 
 | 1554 |  | 
 | 1555 |  | 
 | 1556 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const | 
 | 1557 | { | 
 | 1558 |     Mutex::Autolock _l(mLock); | 
 | 1559 |     return mOutput; | 
 | 1560 | } | 
 | 1561 |  | 
 | 1562 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() | 
 | 1563 | { | 
 | 1564 |     Mutex::Autolock _l(mLock); | 
 | 1565 |     AudioStreamOut *output = mOutput; | 
 | 1566 |     mOutput = NULL; | 
 | 1567 |     // FIXME FastMixer might also have a raw ptr to mOutputSink; | 
 | 1568 |     //       must push a NULL and wait for ack | 
 | 1569 |     mOutputSink.clear(); | 
 | 1570 |     mPipeSink.clear(); | 
 | 1571 |     mNormalSink.clear(); | 
 | 1572 |     return output; | 
 | 1573 | } | 
 | 1574 |  | 
 | 1575 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 1576 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const | 
 | 1577 | { | 
 | 1578 |     if (mOutput == NULL) { | 
 | 1579 |         return NULL; | 
 | 1580 |     } | 
 | 1581 |     return &mOutput->stream->common; | 
 | 1582 | } | 
 | 1583 |  | 
 | 1584 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const | 
 | 1585 | { | 
 | 1586 |     return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 1587 | } | 
 | 1588 |  | 
 | 1589 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) | 
 | 1590 | { | 
 | 1591 |     if (!isValidSyncEvent(event)) { | 
 | 1592 |         return BAD_VALUE; | 
 | 1593 |     } | 
 | 1594 |  | 
 | 1595 |     Mutex::Autolock _l(mLock); | 
 | 1596 |  | 
 | 1597 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1598 |         sp<Track> track = mTracks[i]; | 
 | 1599 |         if (event->triggerSession() == track->sessionId()) { | 
 | 1600 |             (void) track->setSyncEvent(event); | 
 | 1601 |             return NO_ERROR; | 
 | 1602 |         } | 
 | 1603 |     } | 
 | 1604 |  | 
 | 1605 |     return NAME_NOT_FOUND; | 
 | 1606 | } | 
 | 1607 |  | 
 | 1608 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
 | 1609 | { | 
 | 1610 |     return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; | 
 | 1611 | } | 
 | 1612 |  | 
 | 1613 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( | 
 | 1614 |         const Vector< sp<Track> >& tracksToRemove) | 
 | 1615 | { | 
 | 1616 |     size_t count = tracksToRemove.size(); | 
 | 1617 |     if (CC_UNLIKELY(count)) { | 
 | 1618 |         for (size_t i = 0 ; i < count ; i++) { | 
 | 1619 |             const sp<Track>& track = tracksToRemove.itemAt(i); | 
 | 1620 |             if ((track->sharedBuffer() != 0) && | 
 | 1621 |                     (track->mState == TrackBase::ACTIVE || track->mState == TrackBase::RESUMING)) { | 
 | 1622 |                 AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); | 
 | 1623 |             } | 
 | 1624 |         } | 
 | 1625 |     } | 
 | 1626 |  | 
 | 1627 | } | 
 | 1628 |  | 
 | 1629 | void AudioFlinger::PlaybackThread::checkSilentMode_l() | 
 | 1630 | { | 
 | 1631 |     if (!mMasterMute) { | 
 | 1632 |         char value[PROPERTY_VALUE_MAX]; | 
 | 1633 |         if (property_get("ro.audio.silent", value, "0") > 0) { | 
 | 1634 |             char *endptr; | 
 | 1635 |             unsigned long ul = strtoul(value, &endptr, 0); | 
 | 1636 |             if (*endptr == '\0' && ul != 0) { | 
 | 1637 |                 ALOGD("Silence is golden"); | 
 | 1638 |                 // The setprop command will not allow a property to be changed after | 
 | 1639 |                 // the first time it is set, so we don't have to worry about un-muting. | 
 | 1640 |                 setMasterMute_l(true); | 
 | 1641 |             } | 
 | 1642 |         } | 
 | 1643 |     } | 
 | 1644 | } | 
 | 1645 |  | 
 | 1646 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
 | 1647 | void AudioFlinger::PlaybackThread::threadLoop_write() | 
 | 1648 | { | 
 | 1649 |     // FIXME rewrite to reduce number of system calls | 
 | 1650 |     mLastWriteTime = systemTime(); | 
 | 1651 |     mInWrite = true; | 
 | 1652 |     int bytesWritten; | 
 | 1653 |  | 
 | 1654 |     // If an NBAIO sink is present, use it to write the normal mixer's submix | 
 | 1655 |     if (mNormalSink != 0) { | 
 | 1656 | #define mBitShift 2 // FIXME | 
 | 1657 |         size_t count = mixBufferSize >> mBitShift; | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 1658 |         ATRACE_BEGIN("write"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 |         // update the setpoint when AudioFlinger::mScreenState changes | 
 | 1660 |         uint32_t screenState = AudioFlinger::mScreenState; | 
 | 1661 |         if (screenState != mScreenState) { | 
 | 1662 |             mScreenState = screenState; | 
 | 1663 |             MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 1664 |             if (pipe != NULL) { | 
 | 1665 |                 pipe->setAvgFrames((mScreenState & 1) ? | 
 | 1666 |                         (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 1667 |             } | 
 | 1668 |         } | 
 | 1669 |         ssize_t framesWritten = mNormalSink->write(mMixBuffer, count); | 
| Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 1670 |         ATRACE_END(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1671 |         if (framesWritten > 0) { | 
 | 1672 |             bytesWritten = framesWritten << mBitShift; | 
 | 1673 |         } else { | 
 | 1674 |             bytesWritten = framesWritten; | 
 | 1675 |         } | 
 | 1676 |     // otherwise use the HAL / AudioStreamOut directly | 
 | 1677 |     } else { | 
 | 1678 |         // Direct output thread. | 
 | 1679 |         bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize); | 
 | 1680 |     } | 
 | 1681 |  | 
 | 1682 |     if (bytesWritten > 0) { | 
 | 1683 |         mBytesWritten += mixBufferSize; | 
 | 1684 |     } | 
 | 1685 |     mNumWrites++; | 
 | 1686 |     mInWrite = false; | 
 | 1687 | } | 
 | 1688 |  | 
 | 1689 | /* | 
 | 1690 | The derived values that are cached: | 
 | 1691 |  - mixBufferSize from frame count * frame size | 
 | 1692 |  - activeSleepTime from activeSleepTimeUs() | 
 | 1693 |  - idleSleepTime from idleSleepTimeUs() | 
 | 1694 |  - standbyDelay from mActiveSleepTimeUs (DIRECT only) | 
 | 1695 |  - maxPeriod from frame count and sample rate (MIXER only) | 
 | 1696 |  | 
 | 1697 | The parameters that affect these derived values are: | 
 | 1698 |  - frame count | 
 | 1699 |  - frame size | 
 | 1700 |  - sample rate | 
 | 1701 |  - device type: A2DP or not | 
 | 1702 |  - device latency | 
 | 1703 |  - format: PCM or not | 
 | 1704 |  - active sleep time | 
 | 1705 |  - idle sleep time | 
 | 1706 | */ | 
 | 1707 |  | 
 | 1708 | void AudioFlinger::PlaybackThread::cacheParameters_l() | 
 | 1709 | { | 
 | 1710 |     mixBufferSize = mNormalFrameCount * mFrameSize; | 
 | 1711 |     activeSleepTime = activeSleepTimeUs(); | 
 | 1712 |     idleSleepTime = idleSleepTimeUs(); | 
 | 1713 | } | 
 | 1714 |  | 
 | 1715 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) | 
 | 1716 | { | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 1717 |     ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 |             this,  streamType, mTracks.size()); | 
 | 1719 |     Mutex::Autolock _l(mLock); | 
 | 1720 |  | 
 | 1721 |     size_t size = mTracks.size(); | 
 | 1722 |     for (size_t i = 0; i < size; i++) { | 
 | 1723 |         sp<Track> t = mTracks[i]; | 
 | 1724 |         if (t->streamType() == streamType) { | 
| Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1725 |             t->invalidate(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1726 |         } | 
 | 1727 |     } | 
 | 1728 | } | 
 | 1729 |  | 
 | 1730 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 1731 | { | 
 | 1732 |     int session = chain->sessionId(); | 
 | 1733 |     int16_t *buffer = mMixBuffer; | 
 | 1734 |     bool ownsBuffer = false; | 
 | 1735 |  | 
 | 1736 |     ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); | 
 | 1737 |     if (session > 0) { | 
 | 1738 |         // Only one effect chain can be present in direct output thread and it uses | 
 | 1739 |         // the mix buffer as input | 
 | 1740 |         if (mType != DIRECT) { | 
 | 1741 |             size_t numSamples = mNormalFrameCount * mChannelCount; | 
 | 1742 |             buffer = new int16_t[numSamples]; | 
 | 1743 |             memset(buffer, 0, numSamples * sizeof(int16_t)); | 
 | 1744 |             ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); | 
 | 1745 |             ownsBuffer = true; | 
 | 1746 |         } | 
 | 1747 |  | 
 | 1748 |         // Attach all tracks with same session ID to this chain. | 
 | 1749 |         for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1750 |             sp<Track> track = mTracks[i]; | 
 | 1751 |             if (session == track->sessionId()) { | 
 | 1752 |                 ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), | 
 | 1753 |                         buffer); | 
 | 1754 |                 track->setMainBuffer(buffer); | 
 | 1755 |                 chain->incTrackCnt(); | 
 | 1756 |             } | 
 | 1757 |         } | 
 | 1758 |  | 
 | 1759 |         // indicate all active tracks in the chain | 
 | 1760 |         for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 1761 |             sp<Track> track = mActiveTracks[i].promote(); | 
 | 1762 |             if (track == 0) { | 
 | 1763 |                 continue; | 
 | 1764 |             } | 
 | 1765 |             if (session == track->sessionId()) { | 
 | 1766 |                 ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); | 
 | 1767 |                 chain->incActiveTrackCnt(); | 
 | 1768 |             } | 
 | 1769 |         } | 
 | 1770 |     } | 
 | 1771 |  | 
 | 1772 |     chain->setInBuffer(buffer, ownsBuffer); | 
 | 1773 |     chain->setOutBuffer(mMixBuffer); | 
 | 1774 |     // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect | 
 | 1775 |     // chains list in order to be processed last as it contains output stage effects | 
 | 1776 |     // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before | 
 | 1777 |     // session AUDIO_SESSION_OUTPUT_STAGE to be processed | 
 | 1778 |     // after track specific effects and before output stage | 
 | 1779 |     // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and | 
 | 1780 |     // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX | 
 | 1781 |     // Effect chain for other sessions are inserted at beginning of effect | 
 | 1782 |     // chains list to be processed before output mix effects. Relative order between other | 
 | 1783 |     // sessions is not important | 
 | 1784 |     size_t size = mEffectChains.size(); | 
 | 1785 |     size_t i = 0; | 
 | 1786 |     for (i = 0; i < size; i++) { | 
 | 1787 |         if (mEffectChains[i]->sessionId() < session) { | 
 | 1788 |             break; | 
 | 1789 |         } | 
 | 1790 |     } | 
 | 1791 |     mEffectChains.insertAt(chain, i); | 
 | 1792 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 1793 |  | 
 | 1794 |     return NO_ERROR; | 
 | 1795 | } | 
 | 1796 |  | 
 | 1797 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 1798 | { | 
 | 1799 |     int session = chain->sessionId(); | 
 | 1800 |  | 
 | 1801 |     ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); | 
 | 1802 |  | 
 | 1803 |     for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 1804 |         if (chain == mEffectChains[i]) { | 
 | 1805 |             mEffectChains.removeAt(i); | 
 | 1806 |             // detach all active tracks from the chain | 
 | 1807 |             for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { | 
 | 1808 |                 sp<Track> track = mActiveTracks[i].promote(); | 
 | 1809 |                 if (track == 0) { | 
 | 1810 |                     continue; | 
 | 1811 |                 } | 
 | 1812 |                 if (session == track->sessionId()) { | 
 | 1813 |                     ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", | 
 | 1814 |                             chain.get(), session); | 
 | 1815 |                     chain->decActiveTrackCnt(); | 
 | 1816 |                 } | 
 | 1817 |             } | 
 | 1818 |  | 
 | 1819 |             // detach all tracks with same session ID from this chain | 
 | 1820 |             for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1821 |                 sp<Track> track = mTracks[i]; | 
 | 1822 |                 if (session == track->sessionId()) { | 
 | 1823 |                     track->setMainBuffer(mMixBuffer); | 
 | 1824 |                     chain->decTrackCnt(); | 
 | 1825 |                 } | 
 | 1826 |             } | 
 | 1827 |             break; | 
 | 1828 |         } | 
 | 1829 |     } | 
 | 1830 |     return mEffectChains.size(); | 
 | 1831 | } | 
 | 1832 |  | 
 | 1833 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( | 
 | 1834 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 1835 | { | 
 | 1836 |     Mutex::Autolock _l(mLock); | 
 | 1837 |     return attachAuxEffect_l(track, EffectId); | 
 | 1838 | } | 
 | 1839 |  | 
 | 1840 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( | 
 | 1841 |         const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) | 
 | 1842 | { | 
 | 1843 |     status_t status = NO_ERROR; | 
 | 1844 |  | 
 | 1845 |     if (EffectId == 0) { | 
 | 1846 |         track->setAuxBuffer(0, NULL); | 
 | 1847 |     } else { | 
 | 1848 |         // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX | 
 | 1849 |         sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); | 
 | 1850 |         if (effect != 0) { | 
 | 1851 |             if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1852 |                 track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); | 
 | 1853 |             } else { | 
 | 1854 |                 status = INVALID_OPERATION; | 
 | 1855 |             } | 
 | 1856 |         } else { | 
 | 1857 |             status = BAD_VALUE; | 
 | 1858 |         } | 
 | 1859 |     } | 
 | 1860 |     return status; | 
 | 1861 | } | 
 | 1862 |  | 
 | 1863 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) | 
 | 1864 | { | 
 | 1865 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 1866 |         sp<Track> track = mTracks[i]; | 
 | 1867 |         if (track->auxEffectId() == effectId) { | 
 | 1868 |             attachAuxEffect_l(track, 0); | 
 | 1869 |         } | 
 | 1870 |     } | 
 | 1871 | } | 
 | 1872 |  | 
 | 1873 | bool AudioFlinger::PlaybackThread::threadLoop() | 
 | 1874 | { | 
 | 1875 |     Vector< sp<Track> > tracksToRemove; | 
 | 1876 |  | 
 | 1877 |     standbyTime = systemTime(); | 
 | 1878 |  | 
 | 1879 |     // MIXER | 
 | 1880 |     nsecs_t lastWarning = 0; | 
 | 1881 |  | 
 | 1882 |     // DUPLICATING | 
 | 1883 |     // FIXME could this be made local to while loop? | 
 | 1884 |     writeFrames = 0; | 
 | 1885 |  | 
 | 1886 |     cacheParameters_l(); | 
 | 1887 |     sleepTime = idleSleepTime; | 
 | 1888 |  | 
 | 1889 |     if (mType == MIXER) { | 
 | 1890 |         sleepTimeShift = 0; | 
 | 1891 |     } | 
 | 1892 |  | 
 | 1893 |     CpuStats cpuStats; | 
 | 1894 |     const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); | 
 | 1895 |  | 
 | 1896 |     acquireWakeLock(); | 
 | 1897 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1898 |     // mNBLogWriter->log can only be called while thread mutex mLock is held. | 
 | 1899 |     // So if you need to log when mutex is unlocked, set logString to a non-NULL string, | 
 | 1900 |     // and then that string will be logged at the next convenient opportunity. | 
 | 1901 |     const char *logString = NULL; | 
 | 1902 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1903 |     while (!exitPending()) | 
 | 1904 |     { | 
 | 1905 |         cpuStats.sample(myName); | 
 | 1906 |  | 
 | 1907 |         Vector< sp<EffectChain> > effectChains; | 
 | 1908 |  | 
 | 1909 |         processConfigEvents(); | 
 | 1910 |  | 
 | 1911 |         { // scope for mLock | 
 | 1912 |  | 
 | 1913 |             Mutex::Autolock _l(mLock); | 
 | 1914 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1915 |             if (logString != NULL) { | 
 | 1916 |                 mNBLogWriter->logTimestamp(); | 
 | 1917 |                 mNBLogWriter->log(logString); | 
 | 1918 |                 logString = NULL; | 
 | 1919 |             } | 
 | 1920 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1921 |             if (checkForNewParameters_l()) { | 
 | 1922 |                 cacheParameters_l(); | 
 | 1923 |             } | 
 | 1924 |  | 
 | 1925 |             saveOutputTracks(); | 
 | 1926 |  | 
 | 1927 |             // put audio hardware into standby after short delay | 
 | 1928 |             if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) || | 
 | 1929 |                         isSuspended())) { | 
 | 1930 |                 if (!mStandby) { | 
 | 1931 |  | 
 | 1932 |                     threadLoop_standby(); | 
 | 1933 |  | 
 | 1934 |                     mStandby = true; | 
 | 1935 |                 } | 
 | 1936 |  | 
 | 1937 |                 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { | 
 | 1938 |                     // we're about to wait, flush the binder command buffer | 
 | 1939 |                     IPCThreadState::self()->flushCommands(); | 
 | 1940 |  | 
 | 1941 |                     clearOutputTracks(); | 
 | 1942 |  | 
 | 1943 |                     if (exitPending()) { | 
 | 1944 |                         break; | 
 | 1945 |                     } | 
 | 1946 |  | 
 | 1947 |                     releaseWakeLock_l(); | 
 | 1948 |                     // wait until we have something to do... | 
 | 1949 |                     ALOGV("%s going to sleep", myName.string()); | 
 | 1950 |                     mWaitWorkCV.wait(mLock); | 
 | 1951 |                     ALOGV("%s waking up", myName.string()); | 
 | 1952 |                     acquireWakeLock_l(); | 
 | 1953 |  | 
 | 1954 |                     mMixerStatus = MIXER_IDLE; | 
 | 1955 |                     mMixerStatusIgnoringFastTracks = MIXER_IDLE; | 
 | 1956 |                     mBytesWritten = 0; | 
 | 1957 |  | 
 | 1958 |                     checkSilentMode_l(); | 
 | 1959 |  | 
 | 1960 |                     standbyTime = systemTime() + standbyDelay; | 
 | 1961 |                     sleepTime = idleSleepTime; | 
 | 1962 |                     if (mType == MIXER) { | 
 | 1963 |                         sleepTimeShift = 0; | 
 | 1964 |                     } | 
 | 1965 |  | 
 | 1966 |                     continue; | 
 | 1967 |                 } | 
 | 1968 |             } | 
 | 1969 |  | 
 | 1970 |             // mMixerStatusIgnoringFastTracks is also updated internally | 
 | 1971 |             mMixerStatus = prepareTracks_l(&tracksToRemove); | 
 | 1972 |  | 
 | 1973 |             // prevent any changes in effect chain list and in each effect chain | 
 | 1974 |             // during mixing and effect process as the audio buffers could be deleted | 
 | 1975 |             // or modified if an effect is created or deleted | 
 | 1976 |             lockEffectChains_l(effectChains); | 
 | 1977 |         } | 
 | 1978 |  | 
 | 1979 |         if (CC_LIKELY(mMixerStatus == MIXER_TRACKS_READY)) { | 
 | 1980 |             threadLoop_mix(); | 
 | 1981 |         } else { | 
 | 1982 |             threadLoop_sleepTime(); | 
 | 1983 |         } | 
 | 1984 |  | 
 | 1985 |         if (isSuspended()) { | 
 | 1986 |             sleepTime = suspendSleepTimeUs(); | 
 | 1987 |             mBytesWritten += mixBufferSize; | 
 | 1988 |         } | 
 | 1989 |  | 
 | 1990 |         // only process effects if we're going to write | 
 | 1991 |         if (sleepTime == 0) { | 
 | 1992 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 1993 |                 effectChains[i]->process_l(); | 
 | 1994 |             } | 
 | 1995 |         } | 
 | 1996 |  | 
 | 1997 |         // enable changes in effect chain | 
 | 1998 |         unlockEffectChains(effectChains); | 
 | 1999 |  | 
 | 2000 |         // sleepTime == 0 means we must write to audio hardware | 
 | 2001 |         if (sleepTime == 0) { | 
 | 2002 |  | 
 | 2003 |             threadLoop_write(); | 
 | 2004 |  | 
 | 2005 | if (mType == MIXER) { | 
 | 2006 |             // write blocked detection | 
 | 2007 |             nsecs_t now = systemTime(); | 
 | 2008 |             nsecs_t delta = now - mLastWriteTime; | 
 | 2009 |             if (!mStandby && delta > maxPeriod) { | 
 | 2010 |                 mNumDelayedWrites++; | 
 | 2011 |                 if ((now - lastWarning) > kWarningThrottleNs) { | 
| Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 2012 |                     ATRACE_NAME("underrun"); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2013 |                     ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", | 
 | 2014 |                             ns2ms(delta), mNumDelayedWrites, this); | 
 | 2015 |                     lastWarning = now; | 
 | 2016 |                 } | 
 | 2017 |             } | 
 | 2018 | } | 
 | 2019 |  | 
 | 2020 |             mStandby = false; | 
 | 2021 |         } else { | 
 | 2022 |             usleep(sleepTime); | 
 | 2023 |         } | 
 | 2024 |  | 
 | 2025 |         // Finally let go of removed track(s), without the lock held | 
 | 2026 |         // since we can't guarantee the destructors won't acquire that | 
 | 2027 |         // same lock.  This will also mutate and push a new fast mixer state. | 
 | 2028 |         threadLoop_removeTracks(tracksToRemove); | 
 | 2029 |         tracksToRemove.clear(); | 
 | 2030 |  | 
 | 2031 |         // FIXME I don't understand the need for this here; | 
 | 2032 |         //       it was in the original code but maybe the | 
 | 2033 |         //       assignment in saveOutputTracks() makes this unnecessary? | 
 | 2034 |         clearOutputTracks(); | 
 | 2035 |  | 
 | 2036 |         // Effect chains will be actually deleted here if they were removed from | 
 | 2037 |         // mEffectChains list during mixing or effects processing | 
 | 2038 |         effectChains.clear(); | 
 | 2039 |  | 
 | 2040 |         // FIXME Note that the above .clear() is no longer necessary since effectChains | 
 | 2041 |         // is now local to this block, but will keep it for now (at least until merge done). | 
 | 2042 |     } | 
 | 2043 |  | 
 | 2044 |     // for DuplicatingThread, standby mode is handled by the outputTracks, otherwise ... | 
 | 2045 |     if (mType == MIXER || mType == DIRECT) { | 
 | 2046 |         // put output stream into standby mode | 
 | 2047 |         if (!mStandby) { | 
 | 2048 |             mOutput->stream->common.standby(&mOutput->stream->common); | 
 | 2049 |         } | 
 | 2050 |     } | 
 | 2051 |  | 
 | 2052 |     releaseWakeLock(); | 
 | 2053 |  | 
 | 2054 |     ALOGV("Thread %p type %d exiting", this, mType); | 
 | 2055 |     return false; | 
 | 2056 | } | 
 | 2057 |  | 
 | 2058 |  | 
 | 2059 | // ---------------------------------------------------------------------------- | 
 | 2060 |  | 
 | 2061 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, | 
 | 2062 |         audio_io_handle_t id, audio_devices_t device, type_t type) | 
 | 2063 |     :   PlaybackThread(audioFlinger, output, id, device, type), | 
 | 2064 |         // mAudioMixer below | 
 | 2065 |         // mFastMixer below | 
 | 2066 |         mFastMixerFutex(0) | 
 | 2067 |         // mOutputSink below | 
 | 2068 |         // mPipeSink below | 
 | 2069 |         // mNormalSink below | 
 | 2070 | { | 
 | 2071 |     ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); | 
 | 2072 |     ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%d, mFormat=%d, mFrameSize=%u, " | 
 | 2073 |             "mFrameCount=%d, mNormalFrameCount=%d", | 
 | 2074 |             mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, | 
 | 2075 |             mNormalFrameCount); | 
 | 2076 |     mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 2077 |  | 
 | 2078 |     // FIXME - Current mixer implementation only supports stereo output | 
 | 2079 |     if (mChannelCount != FCC_2) { | 
 | 2080 |         ALOGE("Invalid audio hardware channel count %d", mChannelCount); | 
 | 2081 |     } | 
 | 2082 |  | 
 | 2083 |     // create an NBAIO sink for the HAL output stream, and negotiate | 
 | 2084 |     mOutputSink = new AudioStreamOutSink(output->stream); | 
 | 2085 |     size_t numCounterOffers = 0; | 
 | 2086 |     const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount)}; | 
 | 2087 |     ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 2088 |     ALOG_ASSERT(index == 0); | 
 | 2089 |  | 
 | 2090 |     // initialize fast mixer depending on configuration | 
 | 2091 |     bool initFastMixer; | 
 | 2092 |     switch (kUseFastMixer) { | 
 | 2093 |     case FastMixer_Never: | 
 | 2094 |         initFastMixer = false; | 
 | 2095 |         break; | 
 | 2096 |     case FastMixer_Always: | 
 | 2097 |         initFastMixer = true; | 
 | 2098 |         break; | 
 | 2099 |     case FastMixer_Static: | 
 | 2100 |     case FastMixer_Dynamic: | 
 | 2101 |         initFastMixer = mFrameCount < mNormalFrameCount; | 
 | 2102 |         break; | 
 | 2103 |     } | 
 | 2104 |     if (initFastMixer) { | 
 | 2105 |  | 
 | 2106 |         // create a MonoPipe to connect our submix to FastMixer | 
 | 2107 |         NBAIO_Format format = mOutputSink->format(); | 
 | 2108 |         // This pipe depth compensates for scheduling latency of the normal mixer thread. | 
 | 2109 |         // When it wakes up after a maximum latency, it runs a few cycles quickly before | 
 | 2110 |         // finally blocking.  Note the pipe implementation rounds up the request to a power of 2. | 
 | 2111 |         MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); | 
 | 2112 |         const NBAIO_Format offers[1] = {format}; | 
 | 2113 |         size_t numCounterOffers = 0; | 
 | 2114 |         ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 2115 |         ALOG_ASSERT(index == 0); | 
 | 2116 |         monoPipe->setAvgFrames((mScreenState & 1) ? | 
 | 2117 |                 (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); | 
 | 2118 |         mPipeSink = monoPipe; | 
 | 2119 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2120 | #ifdef TEE_SINK | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2121 |         if (mTeeSinkOutputEnabled) { | 
 | 2122 |             // create a Pipe to archive a copy of FastMixer's output for dumpsys | 
 | 2123 |             Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, format); | 
 | 2124 |             numCounterOffers = 0; | 
 | 2125 |             index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 2126 |             ALOG_ASSERT(index == 0); | 
 | 2127 |             mTeeSink = teeSink; | 
 | 2128 |             PipeReader *teeSource = new PipeReader(*teeSink); | 
 | 2129 |             numCounterOffers = 0; | 
 | 2130 |             index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); | 
 | 2131 |             ALOG_ASSERT(index == 0); | 
 | 2132 |             mTeeSource = teeSource; | 
 | 2133 |         } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2134 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2135 |  | 
 | 2136 |         // create fast mixer and configure it initially with just one fast track for our submix | 
 | 2137 |         mFastMixer = new FastMixer(); | 
 | 2138 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 2139 | #ifdef STATE_QUEUE_DUMP | 
 | 2140 |         sq->setObserverDump(&mStateQueueObserverDump); | 
 | 2141 |         sq->setMutatorDump(&mStateQueueMutatorDump); | 
 | 2142 | #endif | 
 | 2143 |         FastMixerState *state = sq->begin(); | 
 | 2144 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 2145 |         // wrap the source side of the MonoPipe to make it an AudioBufferProvider | 
 | 2146 |         fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); | 
 | 2147 |         fastTrack->mVolumeProvider = NULL; | 
 | 2148 |         fastTrack->mGeneration++; | 
 | 2149 |         state->mFastTracksGen++; | 
 | 2150 |         state->mTrackMask = 1; | 
 | 2151 |         // fast mixer will use the HAL output sink | 
 | 2152 |         state->mOutputSink = mOutputSink.get(); | 
 | 2153 |         state->mOutputSinkGen++; | 
 | 2154 |         state->mFrameCount = mFrameCount; | 
 | 2155 |         state->mCommand = FastMixerState::COLD_IDLE; | 
 | 2156 |         // already done in constructor initialization list | 
 | 2157 |         //mFastMixerFutex = 0; | 
 | 2158 |         state->mColdFutexAddr = &mFastMixerFutex; | 
 | 2159 |         state->mColdGen++; | 
 | 2160 |         state->mDumpState = &mFastMixerDumpState; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2161 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2162 |         state->mTeeSink = mTeeSink.get(); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2163 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2164 |         mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); | 
 | 2165 |         state->mNBLogWriter = mFastMixerNBLogWriter.get(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2166 |         sq->end(); | 
 | 2167 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 2168 |  | 
 | 2169 |         // start the fast mixer | 
 | 2170 |         mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); | 
 | 2171 |         pid_t tid = mFastMixer->getTid(); | 
 | 2172 |         int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); | 
 | 2173 |         if (err != 0) { | 
 | 2174 |             ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", | 
 | 2175 |                     kPriorityFastMixer, getpid_cached, tid, err); | 
 | 2176 |         } | 
 | 2177 |  | 
 | 2178 | #ifdef AUDIO_WATCHDOG | 
 | 2179 |         // create and start the watchdog | 
 | 2180 |         mAudioWatchdog = new AudioWatchdog(); | 
 | 2181 |         mAudioWatchdog->setDump(&mAudioWatchdogDump); | 
 | 2182 |         mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); | 
 | 2183 |         tid = mAudioWatchdog->getTid(); | 
 | 2184 |         err = requestPriority(getpid_cached, tid, kPriorityFastMixer); | 
 | 2185 |         if (err != 0) { | 
 | 2186 |             ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", | 
 | 2187 |                     kPriorityFastMixer, getpid_cached, tid, err); | 
 | 2188 |         } | 
 | 2189 | #endif | 
 | 2190 |  | 
 | 2191 |     } else { | 
 | 2192 |         mFastMixer = NULL; | 
 | 2193 |     } | 
 | 2194 |  | 
 | 2195 |     switch (kUseFastMixer) { | 
 | 2196 |     case FastMixer_Never: | 
 | 2197 |     case FastMixer_Dynamic: | 
 | 2198 |         mNormalSink = mOutputSink; | 
 | 2199 |         break; | 
 | 2200 |     case FastMixer_Always: | 
 | 2201 |         mNormalSink = mPipeSink; | 
 | 2202 |         break; | 
 | 2203 |     case FastMixer_Static: | 
 | 2204 |         mNormalSink = initFastMixer ? mPipeSink : mOutputSink; | 
 | 2205 |         break; | 
 | 2206 |     } | 
 | 2207 | } | 
 | 2208 |  | 
 | 2209 | AudioFlinger::MixerThread::~MixerThread() | 
 | 2210 | { | 
 | 2211 |     if (mFastMixer != NULL) { | 
 | 2212 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 2213 |         FastMixerState *state = sq->begin(); | 
 | 2214 |         if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 2215 |             int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 2216 |             if (old == -1) { | 
 | 2217 |                 __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 2218 |             } | 
 | 2219 |         } | 
 | 2220 |         state->mCommand = FastMixerState::EXIT; | 
 | 2221 |         sq->end(); | 
 | 2222 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 2223 |         mFastMixer->join(); | 
 | 2224 |         // Though the fast mixer thread has exited, it's state queue is still valid. | 
 | 2225 |         // We'll use that extract the final state which contains one remaining fast track | 
 | 2226 |         // corresponding to our sub-mix. | 
 | 2227 |         state = sq->begin(); | 
 | 2228 |         ALOG_ASSERT(state->mTrackMask == 1); | 
 | 2229 |         FastTrack *fastTrack = &state->mFastTracks[0]; | 
 | 2230 |         ALOG_ASSERT(fastTrack->mBufferProvider != NULL); | 
 | 2231 |         delete fastTrack->mBufferProvider; | 
 | 2232 |         sq->end(false /*didModify*/); | 
 | 2233 |         delete mFastMixer; | 
 | 2234 | #ifdef AUDIO_WATCHDOG | 
 | 2235 |         if (mAudioWatchdog != 0) { | 
 | 2236 |             mAudioWatchdog->requestExit(); | 
 | 2237 |             mAudioWatchdog->requestExitAndWait(); | 
 | 2238 |             mAudioWatchdog.clear(); | 
 | 2239 |         } | 
 | 2240 | #endif | 
 | 2241 |     } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2242 |     mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2243 |     delete mAudioMixer; | 
 | 2244 | } | 
 | 2245 |  | 
 | 2246 |  | 
 | 2247 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const | 
 | 2248 | { | 
 | 2249 |     if (mFastMixer != NULL) { | 
 | 2250 |         MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); | 
 | 2251 |         latency += (pipe->getAvgFrames() * 1000) / mSampleRate; | 
 | 2252 |     } | 
 | 2253 |     return latency; | 
 | 2254 | } | 
 | 2255 |  | 
 | 2256 |  | 
 | 2257 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) | 
 | 2258 | { | 
 | 2259 |     PlaybackThread::threadLoop_removeTracks(tracksToRemove); | 
 | 2260 | } | 
 | 2261 |  | 
 | 2262 | void AudioFlinger::MixerThread::threadLoop_write() | 
 | 2263 | { | 
 | 2264 |     // FIXME we should only do one push per cycle; confirm this is true | 
 | 2265 |     // Start the fast mixer if it's not already running | 
 | 2266 |     if (mFastMixer != NULL) { | 
 | 2267 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 2268 |         FastMixerState *state = sq->begin(); | 
 | 2269 |         if (state->mCommand != FastMixerState::MIX_WRITE && | 
 | 2270 |                 (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { | 
 | 2271 |             if (state->mCommand == FastMixerState::COLD_IDLE) { | 
 | 2272 |                 int32_t old = android_atomic_inc(&mFastMixerFutex); | 
 | 2273 |                 if (old == -1) { | 
 | 2274 |                     __futex_syscall3(&mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); | 
 | 2275 |                 } | 
 | 2276 | #ifdef AUDIO_WATCHDOG | 
 | 2277 |                 if (mAudioWatchdog != 0) { | 
 | 2278 |                     mAudioWatchdog->resume(); | 
 | 2279 |                 } | 
 | 2280 | #endif | 
 | 2281 |             } | 
 | 2282 |             state->mCommand = FastMixerState::MIX_WRITE; | 
 | 2283 |             sq->end(); | 
 | 2284 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 2285 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 2286 |                 mNormalSink = mPipeSink; | 
 | 2287 |             } | 
 | 2288 |         } else { | 
 | 2289 |             sq->end(false /*didModify*/); | 
 | 2290 |         } | 
 | 2291 |     } | 
 | 2292 |     PlaybackThread::threadLoop_write(); | 
 | 2293 | } | 
 | 2294 |  | 
 | 2295 | void AudioFlinger::MixerThread::threadLoop_standby() | 
 | 2296 | { | 
 | 2297 |     // Idle the fast mixer if it's currently running | 
 | 2298 |     if (mFastMixer != NULL) { | 
 | 2299 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 2300 |         FastMixerState *state = sq->begin(); | 
 | 2301 |         if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 2302 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 2303 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 2304 |             state->mColdGen++; | 
 | 2305 |             mFastMixerFutex = 0; | 
 | 2306 |             sq->end(); | 
 | 2307 |             // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now | 
 | 2308 |             sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 2309 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 2310 |                 mNormalSink = mOutputSink; | 
 | 2311 |             } | 
 | 2312 | #ifdef AUDIO_WATCHDOG | 
 | 2313 |             if (mAudioWatchdog != 0) { | 
 | 2314 |                 mAudioWatchdog->pause(); | 
 | 2315 |             } | 
 | 2316 | #endif | 
 | 2317 |         } else { | 
 | 2318 |             sq->end(false /*didModify*/); | 
 | 2319 |         } | 
 | 2320 |     } | 
 | 2321 |     PlaybackThread::threadLoop_standby(); | 
 | 2322 | } | 
 | 2323 |  | 
 | 2324 | // shared by MIXER and DIRECT, overridden by DUPLICATING | 
 | 2325 | void AudioFlinger::PlaybackThread::threadLoop_standby() | 
 | 2326 | { | 
 | 2327 |     ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); | 
 | 2328 |     mOutput->stream->common.standby(&mOutput->stream->common); | 
 | 2329 | } | 
 | 2330 |  | 
 | 2331 | void AudioFlinger::MixerThread::threadLoop_mix() | 
 | 2332 | { | 
 | 2333 |     // obtain the presentation timestamp of the next output buffer | 
 | 2334 |     int64_t pts; | 
 | 2335 |     status_t status = INVALID_OPERATION; | 
 | 2336 |  | 
 | 2337 |     if (mNormalSink != 0) { | 
 | 2338 |         status = mNormalSink->getNextWriteTimestamp(&pts); | 
 | 2339 |     } else { | 
 | 2340 |         status = mOutputSink->getNextWriteTimestamp(&pts); | 
 | 2341 |     } | 
 | 2342 |  | 
 | 2343 |     if (status != NO_ERROR) { | 
 | 2344 |         pts = AudioBufferProvider::kInvalidPTS; | 
 | 2345 |     } | 
 | 2346 |  | 
 | 2347 |     // mix buffers... | 
 | 2348 |     mAudioMixer->process(pts); | 
 | 2349 |     // increase sleep time progressively when application underrun condition clears. | 
 | 2350 |     // Only increase sleep time if the mixer is ready for two consecutive times to avoid | 
 | 2351 |     // that a steady state of alternating ready/not ready conditions keeps the sleep time | 
 | 2352 |     // such that we would underrun the audio HAL. | 
 | 2353 |     if ((sleepTime == 0) && (sleepTimeShift > 0)) { | 
 | 2354 |         sleepTimeShift--; | 
 | 2355 |     } | 
 | 2356 |     sleepTime = 0; | 
 | 2357 |     standbyTime = systemTime() + standbyDelay; | 
 | 2358 |     //TODO: delay standby when effects have a tail | 
 | 2359 | } | 
 | 2360 |  | 
 | 2361 | void AudioFlinger::MixerThread::threadLoop_sleepTime() | 
 | 2362 | { | 
 | 2363 |     // If no tracks are ready, sleep once for the duration of an output | 
 | 2364 |     // buffer size, then write 0s to the output | 
 | 2365 |     if (sleepTime == 0) { | 
 | 2366 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 2367 |             sleepTime = activeSleepTime >> sleepTimeShift; | 
 | 2368 |             if (sleepTime < kMinThreadSleepTimeUs) { | 
 | 2369 |                 sleepTime = kMinThreadSleepTimeUs; | 
 | 2370 |             } | 
 | 2371 |             // reduce sleep time in case of consecutive application underruns to avoid | 
 | 2372 |             // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer | 
 | 2373 |             // duration we would end up writing less data than needed by the audio HAL if | 
 | 2374 |             // the condition persists. | 
 | 2375 |             if (sleepTimeShift < kMaxThreadSleepTimeShift) { | 
 | 2376 |                 sleepTimeShift++; | 
 | 2377 |             } | 
 | 2378 |         } else { | 
 | 2379 |             sleepTime = idleSleepTime; | 
 | 2380 |         } | 
 | 2381 |     } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { | 
 | 2382 |         memset (mMixBuffer, 0, mixBufferSize); | 
 | 2383 |         sleepTime = 0; | 
 | 2384 |         ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), | 
 | 2385 |                 "anticipated start"); | 
 | 2386 |     } | 
 | 2387 |     // TODO add standby time extension fct of effect tail | 
 | 2388 | } | 
 | 2389 |  | 
 | 2390 | // prepareTracks_l() must be called with ThreadBase::mLock held | 
 | 2391 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( | 
 | 2392 |         Vector< sp<Track> > *tracksToRemove) | 
 | 2393 | { | 
 | 2394 |  | 
 | 2395 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 2396 |     // find out which tracks need to be processed | 
 | 2397 |     size_t count = mActiveTracks.size(); | 
 | 2398 |     size_t mixedTracks = 0; | 
 | 2399 |     size_t tracksWithEffect = 0; | 
 | 2400 |     // counts only _active_ fast tracks | 
 | 2401 |     size_t fastTracks = 0; | 
 | 2402 |     uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset | 
 | 2403 |  | 
 | 2404 |     float masterVolume = mMasterVolume; | 
 | 2405 |     bool masterMute = mMasterMute; | 
 | 2406 |  | 
 | 2407 |     if (masterMute) { | 
 | 2408 |         masterVolume = 0; | 
 | 2409 |     } | 
 | 2410 |     // Delegate master volume control to effect in output mix effect chain if needed | 
 | 2411 |     sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
 | 2412 |     if (chain != 0) { | 
 | 2413 |         uint32_t v = (uint32_t)(masterVolume * (1 << 24)); | 
 | 2414 |         chain->setVolume_l(&v, &v); | 
 | 2415 |         masterVolume = (float)((v + (1 << 23)) >> 24); | 
 | 2416 |         chain.clear(); | 
 | 2417 |     } | 
 | 2418 |  | 
 | 2419 |     // prepare a new state to push | 
 | 2420 |     FastMixerStateQueue *sq = NULL; | 
 | 2421 |     FastMixerState *state = NULL; | 
 | 2422 |     bool didModify = false; | 
 | 2423 |     FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; | 
 | 2424 |     if (mFastMixer != NULL) { | 
 | 2425 |         sq = mFastMixer->sq(); | 
 | 2426 |         state = sq->begin(); | 
 | 2427 |     } | 
 | 2428 |  | 
 | 2429 |     for (size_t i=0 ; i<count ; i++) { | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame^] | 2430 |         const sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2431 |         if (t == 0) { | 
 | 2432 |             continue; | 
 | 2433 |         } | 
 | 2434 |  | 
 | 2435 |         // this const just means the local variable doesn't change | 
 | 2436 |         Track* const track = t.get(); | 
 | 2437 |  | 
 | 2438 |         // process fast tracks | 
 | 2439 |         if (track->isFastTrack()) { | 
 | 2440 |  | 
 | 2441 |             // It's theoretically possible (though unlikely) for a fast track to be created | 
 | 2442 |             // and then removed within the same normal mix cycle.  This is not a problem, as | 
 | 2443 |             // the track never becomes active so it's fast mixer slot is never touched. | 
 | 2444 |             // The converse, of removing an (active) track and then creating a new track | 
 | 2445 |             // at the identical fast mixer slot within the same normal mix cycle, | 
 | 2446 |             // is impossible because the slot isn't marked available until the end of each cycle. | 
 | 2447 |             int j = track->mFastIndex; | 
 | 2448 |             ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); | 
 | 2449 |             ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); | 
 | 2450 |             FastTrack *fastTrack = &state->mFastTracks[j]; | 
 | 2451 |  | 
 | 2452 |             // Determine whether the track is currently in underrun condition, | 
 | 2453 |             // and whether it had a recent underrun. | 
 | 2454 |             FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; | 
 | 2455 |             FastTrackUnderruns underruns = ftDump->mUnderruns; | 
 | 2456 |             uint32_t recentFull = (underruns.mBitFields.mFull - | 
 | 2457 |                     track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; | 
 | 2458 |             uint32_t recentPartial = (underruns.mBitFields.mPartial - | 
 | 2459 |                     track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; | 
 | 2460 |             uint32_t recentEmpty = (underruns.mBitFields.mEmpty - | 
 | 2461 |                     track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; | 
 | 2462 |             uint32_t recentUnderruns = recentPartial + recentEmpty; | 
 | 2463 |             track->mObservedUnderruns = underruns; | 
 | 2464 |             // don't count underruns that occur while stopping or pausing | 
 | 2465 |             // or stopped which can occur when flush() is called while active | 
 | 2466 |             if (!(track->isStopping() || track->isPausing() || track->isStopped())) { | 
 | 2467 |                 track->mUnderrunCount += recentUnderruns; | 
 | 2468 |             } | 
 | 2469 |  | 
 | 2470 |             // This is similar to the state machine for normal tracks, | 
 | 2471 |             // with a few modifications for fast tracks. | 
 | 2472 |             bool isActive = true; | 
 | 2473 |             switch (track->mState) { | 
 | 2474 |             case TrackBase::STOPPING_1: | 
 | 2475 |                 // track stays active in STOPPING_1 state until first underrun | 
 | 2476 |                 if (recentUnderruns > 0) { | 
 | 2477 |                     track->mState = TrackBase::STOPPING_2; | 
 | 2478 |                 } | 
 | 2479 |                 break; | 
 | 2480 |             case TrackBase::PAUSING: | 
 | 2481 |                 // ramp down is not yet implemented | 
 | 2482 |                 track->setPaused(); | 
 | 2483 |                 break; | 
 | 2484 |             case TrackBase::RESUMING: | 
 | 2485 |                 // ramp up is not yet implemented | 
 | 2486 |                 track->mState = TrackBase::ACTIVE; | 
 | 2487 |                 break; | 
 | 2488 |             case TrackBase::ACTIVE: | 
 | 2489 |                 if (recentFull > 0 || recentPartial > 0) { | 
 | 2490 |                     // track has provided at least some frames recently: reset retry count | 
 | 2491 |                     track->mRetryCount = kMaxTrackRetries; | 
 | 2492 |                 } | 
 | 2493 |                 if (recentUnderruns == 0) { | 
 | 2494 |                     // no recent underruns: stay active | 
 | 2495 |                     break; | 
 | 2496 |                 } | 
 | 2497 |                 // there has recently been an underrun of some kind | 
 | 2498 |                 if (track->sharedBuffer() == 0) { | 
 | 2499 |                     // were any of the recent underruns "empty" (no frames available)? | 
 | 2500 |                     if (recentEmpty == 0) { | 
 | 2501 |                         // no, then ignore the partial underruns as they are allowed indefinitely | 
 | 2502 |                         break; | 
 | 2503 |                     } | 
 | 2504 |                     // there has recently been an "empty" underrun: decrement the retry counter | 
 | 2505 |                     if (--(track->mRetryCount) > 0) { | 
 | 2506 |                         break; | 
 | 2507 |                     } | 
 | 2508 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 2509 |                     // it will then automatically call start() when data is available | 
 | 2510 |                     android_atomic_or(CBLK_DISABLED, &track->mCblk->flags); | 
 | 2511 |                     // remove from active list, but state remains ACTIVE [confusing but true] | 
 | 2512 |                     isActive = false; | 
 | 2513 |                     break; | 
 | 2514 |                 } | 
 | 2515 |                 // fall through | 
 | 2516 |             case TrackBase::STOPPING_2: | 
 | 2517 |             case TrackBase::PAUSED: | 
 | 2518 |             case TrackBase::TERMINATED: | 
 | 2519 |             case TrackBase::STOPPED: | 
 | 2520 |             case TrackBase::FLUSHED:   // flush() while active | 
 | 2521 |                 // Check for presentation complete if track is inactive | 
 | 2522 |                 // We have consumed all the buffers of this track. | 
 | 2523 |                 // This would be incomplete if we auto-paused on underrun | 
 | 2524 |                 { | 
 | 2525 |                     size_t audioHALFrames = | 
 | 2526 |                             (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; | 
 | 2527 |                     size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 2528 |                     if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { | 
 | 2529 |                         // track stays in active list until presentation is complete | 
 | 2530 |                         break; | 
 | 2531 |                     } | 
 | 2532 |                 } | 
 | 2533 |                 if (track->isStopping_2()) { | 
 | 2534 |                     track->mState = TrackBase::STOPPED; | 
 | 2535 |                 } | 
 | 2536 |                 if (track->isStopped()) { | 
 | 2537 |                     // Can't reset directly, as fast mixer is still polling this track | 
 | 2538 |                     //   track->reset(); | 
 | 2539 |                     // So instead mark this track as needing to be reset after push with ack | 
 | 2540 |                     resetMask |= 1 << i; | 
 | 2541 |                 } | 
 | 2542 |                 isActive = false; | 
 | 2543 |                 break; | 
 | 2544 |             case TrackBase::IDLE: | 
 | 2545 |             default: | 
 | 2546 |                 LOG_FATAL("unexpected track state %d", track->mState); | 
 | 2547 |             } | 
 | 2548 |  | 
 | 2549 |             if (isActive) { | 
 | 2550 |                 // was it previously inactive? | 
 | 2551 |                 if (!(state->mTrackMask & (1 << j))) { | 
 | 2552 |                     ExtendedAudioBufferProvider *eabp = track; | 
 | 2553 |                     VolumeProvider *vp = track; | 
 | 2554 |                     fastTrack->mBufferProvider = eabp; | 
 | 2555 |                     fastTrack->mVolumeProvider = vp; | 
 | 2556 |                     fastTrack->mSampleRate = track->mSampleRate; | 
 | 2557 |                     fastTrack->mChannelMask = track->mChannelMask; | 
 | 2558 |                     fastTrack->mGeneration++; | 
 | 2559 |                     state->mTrackMask |= 1 << j; | 
 | 2560 |                     didModify = true; | 
 | 2561 |                     // no acknowledgement required for newly active tracks | 
 | 2562 |                 } | 
 | 2563 |                 // cache the combined master volume and stream type volume for fast mixer; this | 
 | 2564 |                 // lacks any synchronization or barrier so VolumeProvider may read a stale value | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 2565 |                 track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2566 |                 ++fastTracks; | 
 | 2567 |             } else { | 
 | 2568 |                 // was it previously active? | 
 | 2569 |                 if (state->mTrackMask & (1 << j)) { | 
 | 2570 |                     fastTrack->mBufferProvider = NULL; | 
 | 2571 |                     fastTrack->mGeneration++; | 
 | 2572 |                     state->mTrackMask &= ~(1 << j); | 
 | 2573 |                     didModify = true; | 
 | 2574 |                     // If any fast tracks were removed, we must wait for acknowledgement | 
 | 2575 |                     // because we're about to decrement the last sp<> on those tracks. | 
 | 2576 |                     block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 2577 |                 } else { | 
 | 2578 |                     LOG_FATAL("fast track %d should have been active", j); | 
 | 2579 |                 } | 
 | 2580 |                 tracksToRemove->add(track); | 
 | 2581 |                 // Avoids a misleading display in dumpsys | 
 | 2582 |                 track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; | 
 | 2583 |             } | 
 | 2584 |             continue; | 
 | 2585 |         } | 
 | 2586 |  | 
 | 2587 |         {   // local variable scope to avoid goto warning | 
 | 2588 |  | 
 | 2589 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 2590 |  | 
 | 2591 |         // The first time a track is added we wait | 
 | 2592 |         // for all its buffers to be filled before processing it | 
 | 2593 |         int name = track->name(); | 
 | 2594 |         // make sure that we have enough frames to mix one full buffer. | 
 | 2595 |         // enforce this condition only once to enable draining the buffer in case the client | 
 | 2596 |         // app does not call stop() and relies on underrun to stop: | 
 | 2597 |         // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed | 
 | 2598 |         // during last round | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2599 |         size_t desiredFrames; | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame^] | 2600 |         uint32_t sr = track->sampleRate(); | 
 | 2601 |         if (sr == mSampleRate) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2602 |             desiredFrames = mNormalFrameCount; | 
 | 2603 |         } else { | 
 | 2604 |             // +1 for rounding and +1 for additional sample needed for interpolation | 
| Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame^] | 2605 |             desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2606 |             // add frames already consumed but not yet released by the resampler | 
 | 2607 |             // because cblk->framesReady() will include these frames | 
 | 2608 |             desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); | 
 | 2609 |             // the minimum track buffer size is normally twice the number of frames necessary | 
 | 2610 |             // to fill one buffer and the resampler should not leave more than one buffer worth | 
 | 2611 |             // of unreleased frames after each pass, but just in case... | 
 | 2612 |             ALOG_ASSERT(desiredFrames <= cblk->frameCount_); | 
 | 2613 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2614 |         uint32_t minFrames = 1; | 
 | 2615 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && | 
 | 2616 |                 (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2617 |             minFrames = desiredFrames; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2618 |         } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2619 |         // It's not safe to call framesReady() for a static buffer track, so assume it's ready | 
 | 2620 |         size_t framesReady; | 
 | 2621 |         if (track->sharedBuffer() == 0) { | 
 | 2622 |             framesReady = track->framesReady(); | 
 | 2623 |         } else if (track->isStopped()) { | 
 | 2624 |             framesReady = 0; | 
 | 2625 |         } else { | 
 | 2626 |             framesReady = 1; | 
 | 2627 |         } | 
 | 2628 |         if ((framesReady >= minFrames) && track->isReady() && | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2629 |                 !track->isPaused() && !track->isTerminated()) | 
 | 2630 |         { | 
 | 2631 |             ALOGVV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, | 
 | 2632 |                     this); | 
 | 2633 |  | 
 | 2634 |             mixedTracks++; | 
 | 2635 |  | 
 | 2636 |             // track->mainBuffer() != mMixBuffer means there is an effect chain | 
 | 2637 |             // connected to the track | 
 | 2638 |             chain.clear(); | 
 | 2639 |             if (track->mainBuffer() != mMixBuffer) { | 
 | 2640 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 2641 |                 // Delegate volume control to effect in track effect chain if needed | 
 | 2642 |                 if (chain != 0) { | 
 | 2643 |                     tracksWithEffect++; | 
 | 2644 |                 } else { | 
 | 2645 |                     ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " | 
 | 2646 |                             "session %d", | 
 | 2647 |                             name, track->sessionId()); | 
 | 2648 |                 } | 
 | 2649 |             } | 
 | 2650 |  | 
 | 2651 |  | 
 | 2652 |             int param = AudioMixer::VOLUME; | 
 | 2653 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 2654 |                 // no ramp for the first volume setting | 
 | 2655 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
 | 2656 |                 if (track->mState == TrackBase::RESUMING) { | 
 | 2657 |                     track->mState = TrackBase::ACTIVE; | 
 | 2658 |                     param = AudioMixer::RAMP_VOLUME; | 
 | 2659 |                 } | 
 | 2660 |                 mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); | 
 | 2661 |             } else if (cblk->server != 0) { | 
 | 2662 |                 // If the track is stopped before the first frame was mixed, | 
 | 2663 |                 // do not apply ramp | 
 | 2664 |                 param = AudioMixer::RAMP_VOLUME; | 
 | 2665 |             } | 
 | 2666 |  | 
 | 2667 |             // compute volume for this track | 
 | 2668 |             uint32_t vl, vr, va; | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 2669 |             if (track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 |                 vl = vr = va = 0; | 
 | 2671 |                 if (track->isPausing()) { | 
 | 2672 |                     track->setPaused(); | 
 | 2673 |                 } | 
 | 2674 |             } else { | 
 | 2675 |  | 
 | 2676 |                 // read original volumes with volume control | 
 | 2677 |                 float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 2678 |                 float v = masterVolume * typeVolume; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2679 |                 AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2680 |                 uint32_t vlr = proxy->getVolumeLR(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2681 |                 vl = vlr & 0xFFFF; | 
 | 2682 |                 vr = vlr >> 16; | 
 | 2683 |                 // track volumes come from shared memory, so can't be trusted and must be clamped | 
 | 2684 |                 if (vl > MAX_GAIN_INT) { | 
 | 2685 |                     ALOGV("Track left volume out of range: %04X", vl); | 
 | 2686 |                     vl = MAX_GAIN_INT; | 
 | 2687 |                 } | 
 | 2688 |                 if (vr > MAX_GAIN_INT) { | 
 | 2689 |                     ALOGV("Track right volume out of range: %04X", vr); | 
 | 2690 |                     vr = MAX_GAIN_INT; | 
 | 2691 |                 } | 
 | 2692 |                 // now apply the master volume and stream type volume | 
 | 2693 |                 vl = (uint32_t)(v * vl) << 12; | 
 | 2694 |                 vr = (uint32_t)(v * vr) << 12; | 
 | 2695 |                 // assuming master volume and stream type volume each go up to 1.0, | 
 | 2696 |                 // vl and vr are now in 8.24 format | 
 | 2697 |  | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2698 |                 uint16_t sendLevel = proxy->getSendLevel_U4_12(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2699 |                 // send level comes from shared memory and so may be corrupt | 
 | 2700 |                 if (sendLevel > MAX_GAIN_INT) { | 
 | 2701 |                     ALOGV("Track send level out of range: %04X", sendLevel); | 
 | 2702 |                     sendLevel = MAX_GAIN_INT; | 
 | 2703 |                 } | 
 | 2704 |                 va = (uint32_t)(v * sendLevel); | 
 | 2705 |             } | 
 | 2706 |             // Delegate volume control to effect in track effect chain if needed | 
 | 2707 |             if (chain != 0 && chain->setVolume_l(&vl, &vr)) { | 
 | 2708 |                 // Do not ramp volume if volume is controlled by effect | 
 | 2709 |                 param = AudioMixer::VOLUME; | 
 | 2710 |                 track->mHasVolumeController = true; | 
 | 2711 |             } else { | 
 | 2712 |                 // force no volume ramp when volume controller was just disabled or removed | 
 | 2713 |                 // from effect chain to avoid volume spike | 
 | 2714 |                 if (track->mHasVolumeController) { | 
 | 2715 |                     param = AudioMixer::VOLUME; | 
 | 2716 |                 } | 
 | 2717 |                 track->mHasVolumeController = false; | 
 | 2718 |             } | 
 | 2719 |  | 
 | 2720 |             // Convert volumes from 8.24 to 4.12 format | 
 | 2721 |             // This additional clamping is needed in case chain->setVolume_l() overshot | 
 | 2722 |             vl = (vl + (1 << 11)) >> 12; | 
 | 2723 |             if (vl > MAX_GAIN_INT) { | 
 | 2724 |                 vl = MAX_GAIN_INT; | 
 | 2725 |             } | 
 | 2726 |             vr = (vr + (1 << 11)) >> 12; | 
 | 2727 |             if (vr > MAX_GAIN_INT) { | 
 | 2728 |                 vr = MAX_GAIN_INT; | 
 | 2729 |             } | 
 | 2730 |  | 
 | 2731 |             if (va > MAX_GAIN_INT) { | 
 | 2732 |                 va = MAX_GAIN_INT;   // va is uint32_t, so no need to check for - | 
 | 2733 |             } | 
 | 2734 |  | 
 | 2735 |             // XXX: these things DON'T need to be done each time | 
 | 2736 |             mAudioMixer->setBufferProvider(name, track); | 
 | 2737 |             mAudioMixer->enable(name); | 
 | 2738 |  | 
 | 2739 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl); | 
 | 2740 |             mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr); | 
 | 2741 |             mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va); | 
 | 2742 |             mAudioMixer->setParameter( | 
 | 2743 |                 name, | 
 | 2744 |                 AudioMixer::TRACK, | 
 | 2745 |                 AudioMixer::FORMAT, (void *)track->format()); | 
 | 2746 |             mAudioMixer->setParameter( | 
 | 2747 |                 name, | 
 | 2748 |                 AudioMixer::TRACK, | 
 | 2749 |                 AudioMixer::CHANNEL_MASK, (void *)track->channelMask()); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2750 |             // limit track sample rate to 2 x output sample rate, which changes at re-configuration | 
 | 2751 |             uint32_t maxSampleRate = mSampleRate * 2; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2752 |             uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2753 |             if (reqSampleRate == 0) { | 
 | 2754 |                 reqSampleRate = mSampleRate; | 
 | 2755 |             } else if (reqSampleRate > maxSampleRate) { | 
 | 2756 |                 reqSampleRate = maxSampleRate; | 
 | 2757 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2758 |             mAudioMixer->setParameter( | 
 | 2759 |                 name, | 
 | 2760 |                 AudioMixer::RESAMPLE, | 
 | 2761 |                 AudioMixer::SAMPLE_RATE, | 
| Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2762 |                 (void *)reqSampleRate); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2763 |             mAudioMixer->setParameter( | 
 | 2764 |                 name, | 
 | 2765 |                 AudioMixer::TRACK, | 
 | 2766 |                 AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); | 
 | 2767 |             mAudioMixer->setParameter( | 
 | 2768 |                 name, | 
 | 2769 |                 AudioMixer::TRACK, | 
 | 2770 |                 AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); | 
 | 2771 |  | 
 | 2772 |             // reset retry count | 
 | 2773 |             track->mRetryCount = kMaxTrackRetries; | 
 | 2774 |  | 
 | 2775 |             // If one track is ready, set the mixer ready if: | 
 | 2776 |             //  - the mixer was not ready during previous round OR | 
 | 2777 |             //  - no other track is not ready | 
 | 2778 |             if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || | 
 | 2779 |                     mixerStatus != MIXER_TRACKS_ENABLED) { | 
 | 2780 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 2781 |             } | 
 | 2782 |         } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2783 |             // only implemented for normal tracks, not fast tracks | 
 | 2784 |             if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { | 
 | 2785 |                 // we missed desiredFrames whatever the actual number of frames missing was | 
 | 2786 |                 cblk->u.mStreaming.mUnderrunFrames += desiredFrames; | 
 | 2787 |                 // FIXME also wake futex so that underrun is noticed more quickly | 
 | 2788 |                 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->flags); | 
 | 2789 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2790 |             // clear effect chain input buffer if an active track underruns to avoid sending | 
 | 2791 |             // previous audio buffer again to effects | 
 | 2792 |             chain = getEffectChain_l(track->sessionId()); | 
 | 2793 |             if (chain != 0) { | 
 | 2794 |                 chain->clearInputBuffer(); | 
 | 2795 |             } | 
 | 2796 |  | 
 | 2797 |             ALOGVV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, | 
 | 2798 |                     cblk->server, this); | 
 | 2799 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 2800 |                     track->isStopped() || track->isPaused()) { | 
 | 2801 |                 // We have consumed all the buffers of this track. | 
 | 2802 |                 // Remove it from the list of active tracks. | 
 | 2803 |                 // TODO: use actual buffer filling status instead of latency when available from | 
 | 2804 |                 // audio HAL | 
 | 2805 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 2806 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 2807 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 2808 |                     if (track->isStopped()) { | 
 | 2809 |                         track->reset(); | 
 | 2810 |                     } | 
 | 2811 |                     tracksToRemove->add(track); | 
 | 2812 |                 } | 
 | 2813 |             } else { | 
 | 2814 |                 track->mUnderrunCount++; | 
 | 2815 |                 // No buffers for this track. Give it a few chances to | 
 | 2816 |                 // fill a buffer, then remove it from active list. | 
 | 2817 |                 if (--(track->mRetryCount) <= 0) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 2818 |                     ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2819 |                     tracksToRemove->add(track); | 
 | 2820 |                     // indicate to client process that the track was disabled because of underrun; | 
 | 2821 |                     // it will then automatically call start() when data is available | 
 | 2822 |                     android_atomic_or(CBLK_DISABLED, &cblk->flags); | 
 | 2823 |                 // If one track is not ready, mark the mixer also not ready if: | 
 | 2824 |                 //  - the mixer was ready during previous round OR | 
 | 2825 |                 //  - no other track is ready | 
 | 2826 |                 } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || | 
 | 2827 |                                 mixerStatus != MIXER_TRACKS_READY) { | 
 | 2828 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 2829 |                 } | 
 | 2830 |             } | 
 | 2831 |             mAudioMixer->disable(name); | 
 | 2832 |         } | 
 | 2833 |  | 
 | 2834 |         }   // local variable scope to avoid goto warning | 
 | 2835 | track_is_ready: ; | 
 | 2836 |  | 
 | 2837 |     } | 
 | 2838 |  | 
 | 2839 |     // Push the new FastMixer state if necessary | 
 | 2840 |     bool pauseAudioWatchdog = false; | 
 | 2841 |     if (didModify) { | 
 | 2842 |         state->mFastTracksGen++; | 
 | 2843 |         // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle | 
 | 2844 |         if (kUseFastMixer == FastMixer_Dynamic && | 
 | 2845 |                 state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { | 
 | 2846 |             state->mCommand = FastMixerState::COLD_IDLE; | 
 | 2847 |             state->mColdFutexAddr = &mFastMixerFutex; | 
 | 2848 |             state->mColdGen++; | 
 | 2849 |             mFastMixerFutex = 0; | 
 | 2850 |             if (kUseFastMixer == FastMixer_Dynamic) { | 
 | 2851 |                 mNormalSink = mOutputSink; | 
 | 2852 |             } | 
 | 2853 |             // If we go into cold idle, need to wait for acknowledgement | 
 | 2854 |             // so that fast mixer stops doing I/O. | 
 | 2855 |             block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; | 
 | 2856 |             pauseAudioWatchdog = true; | 
 | 2857 |         } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2858 |     } | 
 | 2859 |     if (sq != NULL) { | 
 | 2860 |         sq->end(didModify); | 
 | 2861 |         sq->push(block); | 
 | 2862 |     } | 
 | 2863 | #ifdef AUDIO_WATCHDOG | 
 | 2864 |     if (pauseAudioWatchdog && mAudioWatchdog != 0) { | 
 | 2865 |         mAudioWatchdog->pause(); | 
 | 2866 |     } | 
 | 2867 | #endif | 
 | 2868 |  | 
 | 2869 |     // Now perform the deferred reset on fast tracks that have stopped | 
 | 2870 |     while (resetMask != 0) { | 
 | 2871 |         size_t i = __builtin_ctz(resetMask); | 
 | 2872 |         ALOG_ASSERT(i < count); | 
 | 2873 |         resetMask &= ~(1 << i); | 
 | 2874 |         sp<Track> t = mActiveTracks[i].promote(); | 
 | 2875 |         if (t == 0) { | 
 | 2876 |             continue; | 
 | 2877 |         } | 
 | 2878 |         Track* track = t.get(); | 
 | 2879 |         ALOG_ASSERT(track->isFastTrack() && track->isStopped()); | 
 | 2880 |         track->reset(); | 
 | 2881 |     } | 
 | 2882 |  | 
 | 2883 |     // remove all the tracks that need to be... | 
 | 2884 |     count = tracksToRemove->size(); | 
 | 2885 |     if (CC_UNLIKELY(count)) { | 
 | 2886 |         for (size_t i=0 ; i<count ; i++) { | 
 | 2887 |             const sp<Track>& track = tracksToRemove->itemAt(i); | 
 | 2888 |             mActiveTracks.remove(track); | 
 | 2889 |             if (track->mainBuffer() != mMixBuffer) { | 
 | 2890 |                 chain = getEffectChain_l(track->sessionId()); | 
 | 2891 |                 if (chain != 0) { | 
 | 2892 |                     ALOGV("stopping track on chain %p for session Id: %d", chain.get(), | 
 | 2893 |                             track->sessionId()); | 
 | 2894 |                     chain->decActiveTrackCnt(); | 
 | 2895 |                 } | 
 | 2896 |             } | 
 | 2897 |             if (track->isTerminated()) { | 
 | 2898 |                 removeTrack_l(track); | 
 | 2899 |             } | 
 | 2900 |         } | 
 | 2901 |     } | 
 | 2902 |  | 
 | 2903 |     // mix buffer must be cleared if all tracks are connected to an | 
 | 2904 |     // effect chain as in this case the mixer will not write to | 
 | 2905 |     // mix buffer and track effects will accumulate into it | 
 | 2906 |     if ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || | 
 | 2907 |             (mixedTracks == 0 && fastTracks > 0)) { | 
 | 2908 |         // FIXME as a performance optimization, should remember previous zero status | 
 | 2909 |         memset(mMixBuffer, 0, mNormalFrameCount * mChannelCount * sizeof(int16_t)); | 
 | 2910 |     } | 
 | 2911 |  | 
 | 2912 |     // if any fast tracks, then status is ready | 
 | 2913 |     mMixerStatusIgnoringFastTracks = mixerStatus; | 
 | 2914 |     if (fastTracks > 0) { | 
 | 2915 |         mixerStatus = MIXER_TRACKS_READY; | 
 | 2916 |     } | 
 | 2917 |     return mixerStatus; | 
 | 2918 | } | 
 | 2919 |  | 
 | 2920 | // getTrackName_l() must be called with ThreadBase::mLock held | 
 | 2921 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, int sessionId) | 
 | 2922 | { | 
 | 2923 |     return mAudioMixer->getTrackName(channelMask, sessionId); | 
 | 2924 | } | 
 | 2925 |  | 
 | 2926 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
 | 2927 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) | 
 | 2928 | { | 
 | 2929 |     ALOGV("remove track (%d) and delete from mixer", name); | 
 | 2930 |     mAudioMixer->deleteTrackName(name); | 
 | 2931 | } | 
 | 2932 |  | 
 | 2933 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
 | 2934 | bool AudioFlinger::MixerThread::checkForNewParameters_l() | 
 | 2935 | { | 
 | 2936 |     // if !&IDLE, holds the FastMixer state to restore after new parameters processed | 
 | 2937 |     FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; | 
 | 2938 |     bool reconfig = false; | 
 | 2939 |  | 
 | 2940 |     while (!mNewParameters.isEmpty()) { | 
 | 2941 |  | 
 | 2942 |         if (mFastMixer != NULL) { | 
 | 2943 |             FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 2944 |             FastMixerState *state = sq->begin(); | 
 | 2945 |             if (!(state->mCommand & FastMixerState::IDLE)) { | 
 | 2946 |                 previousCommand = state->mCommand; | 
 | 2947 |                 state->mCommand = FastMixerState::HOT_IDLE; | 
 | 2948 |                 sq->end(); | 
 | 2949 |                 sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); | 
 | 2950 |             } else { | 
 | 2951 |                 sq->end(false /*didModify*/); | 
 | 2952 |             } | 
 | 2953 |         } | 
 | 2954 |  | 
 | 2955 |         status_t status = NO_ERROR; | 
 | 2956 |         String8 keyValuePair = mNewParameters[0]; | 
 | 2957 |         AudioParameter param = AudioParameter(keyValuePair); | 
 | 2958 |         int value; | 
 | 2959 |  | 
 | 2960 |         if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 2961 |             reconfig = true; | 
 | 2962 |         } | 
 | 2963 |         if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
 | 2964 |             if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { | 
 | 2965 |                 status = BAD_VALUE; | 
 | 2966 |             } else { | 
 | 2967 |                 reconfig = true; | 
 | 2968 |             } | 
 | 2969 |         } | 
 | 2970 |         if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 2971 |             if (value != AUDIO_CHANNEL_OUT_STEREO) { | 
 | 2972 |                 status = BAD_VALUE; | 
 | 2973 |             } else { | 
 | 2974 |                 reconfig = true; | 
 | 2975 |             } | 
 | 2976 |         } | 
 | 2977 |         if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 2978 |             // do not accept frame count changes if tracks are open as the track buffer | 
 | 2979 |             // size depends on frame count and correct behavior would not be guaranteed | 
 | 2980 |             // if frame count is changed after track creation | 
 | 2981 |             if (!mTracks.isEmpty()) { | 
 | 2982 |                 status = INVALID_OPERATION; | 
 | 2983 |             } else { | 
 | 2984 |                 reconfig = true; | 
 | 2985 |             } | 
 | 2986 |         } | 
 | 2987 |         if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 2988 | #ifdef ADD_BATTERY_DATA | 
 | 2989 |             // when changing the audio output device, call addBatteryData to notify | 
 | 2990 |             // the change | 
 | 2991 |             if (mOutDevice != value) { | 
 | 2992 |                 uint32_t params = 0; | 
 | 2993 |                 // check whether speaker is on | 
 | 2994 |                 if (value & AUDIO_DEVICE_OUT_SPEAKER) { | 
 | 2995 |                     params |= IMediaPlayerService::kBatteryDataSpeakerOn; | 
 | 2996 |                 } | 
 | 2997 |  | 
 | 2998 |                 audio_devices_t deviceWithoutSpeaker | 
 | 2999 |                     = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; | 
 | 3000 |                 // check if any other device (except speaker) is on | 
 | 3001 |                 if (value & deviceWithoutSpeaker ) { | 
 | 3002 |                     params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; | 
 | 3003 |                 } | 
 | 3004 |  | 
 | 3005 |                 if (params != 0) { | 
 | 3006 |                     addBatteryData(params); | 
 | 3007 |                 } | 
 | 3008 |             } | 
 | 3009 | #endif | 
 | 3010 |  | 
 | 3011 |             // forward device change to effects that have requested to be | 
 | 3012 |             // aware of attached audio device. | 
| Eric Laurent | 7e1139c | 2013-06-06 18:29:01 -0700 | [diff] [blame] | 3013 |             if (value != AUDIO_DEVICE_NONE) { | 
 | 3014 |                 mOutDevice = value; | 
 | 3015 |                 for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 3016 |                     mEffectChains[i]->setDevice_l(mOutDevice); | 
 | 3017 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3018 |             } | 
 | 3019 |         } | 
 | 3020 |  | 
 | 3021 |         if (status == NO_ERROR) { | 
 | 3022 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3023 |                                                     keyValuePair.string()); | 
 | 3024 |             if (!mStandby && status == INVALID_OPERATION) { | 
 | 3025 |                 mOutput->stream->common.standby(&mOutput->stream->common); | 
 | 3026 |                 mStandby = true; | 
 | 3027 |                 mBytesWritten = 0; | 
 | 3028 |                 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3029 |                                                        keyValuePair.string()); | 
 | 3030 |             } | 
 | 3031 |             if (status == NO_ERROR && reconfig) { | 
 | 3032 |                 delete mAudioMixer; | 
 | 3033 |                 // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't) | 
 | 3034 |                 mAudioMixer = NULL; | 
 | 3035 |                 readOutputParameters(); | 
 | 3036 |                 mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); | 
 | 3037 |                 for (size_t i = 0; i < mTracks.size() ; i++) { | 
 | 3038 |                     int name = getTrackName_l(mTracks[i]->mChannelMask, mTracks[i]->mSessionId); | 
 | 3039 |                     if (name < 0) { | 
 | 3040 |                         break; | 
 | 3041 |                     } | 
 | 3042 |                     mTracks[i]->mName = name; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3043 |                 } | 
 | 3044 |                 sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
 | 3045 |             } | 
 | 3046 |         } | 
 | 3047 |  | 
 | 3048 |         mNewParameters.removeAt(0); | 
 | 3049 |  | 
 | 3050 |         mParamStatus = status; | 
 | 3051 |         mParamCond.signal(); | 
 | 3052 |         // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
 | 3053 |         // already timed out waiting for the status and will never signal the condition. | 
 | 3054 |         mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
 | 3055 |     } | 
 | 3056 |  | 
 | 3057 |     if (!(previousCommand & FastMixerState::IDLE)) { | 
 | 3058 |         ALOG_ASSERT(mFastMixer != NULL); | 
 | 3059 |         FastMixerStateQueue *sq = mFastMixer->sq(); | 
 | 3060 |         FastMixerState *state = sq->begin(); | 
 | 3061 |         ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); | 
 | 3062 |         state->mCommand = previousCommand; | 
 | 3063 |         sq->end(); | 
 | 3064 |         sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); | 
 | 3065 |     } | 
 | 3066 |  | 
 | 3067 |     return reconfig; | 
 | 3068 | } | 
 | 3069 |  | 
 | 3070 |  | 
 | 3071 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 3072 | { | 
 | 3073 |     const size_t SIZE = 256; | 
 | 3074 |     char buffer[SIZE]; | 
 | 3075 |     String8 result; | 
 | 3076 |  | 
 | 3077 |     PlaybackThread::dumpInternals(fd, args); | 
 | 3078 |  | 
 | 3079 |     snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames()); | 
 | 3080 |     result.append(buffer); | 
 | 3081 |     write(fd, result.string(), result.size()); | 
 | 3082 |  | 
 | 3083 |     // Make a non-atomic copy of fast mixer dump state so it won't change underneath us | 
 | 3084 |     FastMixerDumpState copy = mFastMixerDumpState; | 
 | 3085 |     copy.dump(fd); | 
 | 3086 |  | 
 | 3087 | #ifdef STATE_QUEUE_DUMP | 
 | 3088 |     // Similar for state queue | 
 | 3089 |     StateQueueObserverDump observerCopy = mStateQueueObserverDump; | 
 | 3090 |     observerCopy.dump(fd); | 
 | 3091 |     StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; | 
 | 3092 |     mutatorCopy.dump(fd); | 
 | 3093 | #endif | 
 | 3094 |  | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3095 | #ifdef TEE_SINK | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3096 |     // Write the tee output to a .wav file | 
 | 3097 |     dumpTee(fd, mTeeSource, mId); | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3098 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3099 |  | 
 | 3100 | #ifdef AUDIO_WATCHDOG | 
 | 3101 |     if (mAudioWatchdog != 0) { | 
 | 3102 |         // Make a non-atomic copy of audio watchdog dump so it won't change underneath us | 
 | 3103 |         AudioWatchdogDump wdCopy = mAudioWatchdogDump; | 
 | 3104 |         wdCopy.dump(fd); | 
 | 3105 |     } | 
 | 3106 | #endif | 
 | 3107 | } | 
 | 3108 |  | 
 | 3109 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const | 
 | 3110 | { | 
 | 3111 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 3112 | } | 
 | 3113 |  | 
 | 3114 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const | 
 | 3115 | { | 
 | 3116 |     return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); | 
 | 3117 | } | 
 | 3118 |  | 
 | 3119 | void AudioFlinger::MixerThread::cacheParameters_l() | 
 | 3120 | { | 
 | 3121 |     PlaybackThread::cacheParameters_l(); | 
 | 3122 |  | 
 | 3123 |     // FIXME: Relaxed timing because of a certain device that can't meet latency | 
 | 3124 |     // Should be reduced to 2x after the vendor fixes the driver issue | 
 | 3125 |     // increase threshold again due to low power audio mode. The way this warning | 
 | 3126 |     // threshold is calculated and its usefulness should be reconsidered anyway. | 
 | 3127 |     maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; | 
 | 3128 | } | 
 | 3129 |  | 
 | 3130 | // ---------------------------------------------------------------------------- | 
 | 3131 |  | 
 | 3132 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, | 
 | 3133 |         AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) | 
 | 3134 |     :   PlaybackThread(audioFlinger, output, id, device, DIRECT) | 
 | 3135 |         // mLeftVolFloat, mRightVolFloat | 
 | 3136 | { | 
 | 3137 | } | 
 | 3138 |  | 
 | 3139 | AudioFlinger::DirectOutputThread::~DirectOutputThread() | 
 | 3140 | { | 
 | 3141 | } | 
 | 3142 |  | 
 | 3143 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( | 
 | 3144 |     Vector< sp<Track> > *tracksToRemove | 
 | 3145 | ) | 
 | 3146 | { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3147 |     size_t count = mActiveTracks.size(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3148 |     mixer_state mixerStatus = MIXER_IDLE; | 
 | 3149 |  | 
 | 3150 |     // find out which tracks need to be processed | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3151 |     for (size_t i = 0; i < count; i++) { | 
 | 3152 |         sp<Track> t = mActiveTracks[i].promote(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3153 |         // The track died recently | 
 | 3154 |         if (t == 0) { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3155 |             continue; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3156 |         } | 
 | 3157 |  | 
 | 3158 |         Track* const track = t.get(); | 
 | 3159 |         audio_track_cblk_t* cblk = track->cblk(); | 
 | 3160 |  | 
 | 3161 |         // The first time a track is added we wait | 
 | 3162 |         // for all its buffers to be filled before processing it | 
 | 3163 |         uint32_t minFrames; | 
 | 3164 |         if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing()) { | 
 | 3165 |             minFrames = mNormalFrameCount; | 
 | 3166 |         } else { | 
 | 3167 |             minFrames = 1; | 
 | 3168 |         } | 
 | 3169 |         if ((track->framesReady() >= minFrames) && track->isReady() && | 
 | 3170 |                 !track->isPaused() && !track->isTerminated()) | 
 | 3171 |         { | 
 | 3172 |             ALOGVV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server); | 
 | 3173 |  | 
 | 3174 |             if (track->mFillingUpStatus == Track::FS_FILLED) { | 
 | 3175 |                 track->mFillingUpStatus = Track::FS_ACTIVE; | 
 | 3176 |                 mLeftVolFloat = mRightVolFloat = 0; | 
 | 3177 |                 if (track->mState == TrackBase::RESUMING) { | 
 | 3178 |                     track->mState = TrackBase::ACTIVE; | 
 | 3179 |                 } | 
 | 3180 |             } | 
 | 3181 |  | 
 | 3182 |             // compute volume for this track | 
 | 3183 |             float left, right; | 
| Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3184 |             if (mMasterMute || track->isPausing() || mStreamTypes[track->streamType()].mute) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3185 |                 left = right = 0; | 
 | 3186 |                 if (track->isPausing()) { | 
 | 3187 |                     track->setPaused(); | 
 | 3188 |                 } | 
 | 3189 |             } else { | 
 | 3190 |                 float typeVolume = mStreamTypes[track->streamType()].volume; | 
 | 3191 |                 float v = mMasterVolume * typeVolume; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3192 |                 uint32_t vlr = track->mAudioTrackServerProxy->getVolumeLR(); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3193 |                 float v_clamped = v * (vlr & 0xFFFF); | 
 | 3194 |                 if (v_clamped > MAX_GAIN) { | 
 | 3195 |                     v_clamped = MAX_GAIN; | 
 | 3196 |                 } | 
 | 3197 |                 left = v_clamped/MAX_GAIN; | 
 | 3198 |                 v_clamped = v * (vlr >> 16); | 
 | 3199 |                 if (v_clamped > MAX_GAIN) { | 
 | 3200 |                     v_clamped = MAX_GAIN; | 
 | 3201 |                 } | 
 | 3202 |                 right = v_clamped/MAX_GAIN; | 
 | 3203 |             } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3204 |             // Only consider last track started for volume and mixer state control. | 
 | 3205 |             // This is the last entry in mActiveTracks unless a track underruns. | 
 | 3206 |             // As we only care about the transition phase between two tracks on a | 
 | 3207 |             // direct output, it is not a problem to ignore the underrun case. | 
 | 3208 |             if (i == (count - 1)) { | 
 | 3209 |                 if (left != mLeftVolFloat || right != mRightVolFloat) { | 
 | 3210 |                     mLeftVolFloat = left; | 
 | 3211 |                     mRightVolFloat = right; | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3212 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3213 |                     // Convert volumes from float to 8.24 | 
 | 3214 |                     uint32_t vl = (uint32_t)(left * (1 << 24)); | 
 | 3215 |                     uint32_t vr = (uint32_t)(right * (1 << 24)); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3216 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3217 |                     // Delegate volume control to effect in track effect chain if needed | 
 | 3218 |                     // only one effect chain can be present on DirectOutputThread, so if | 
 | 3219 |                     // there is one, the track is connected to it | 
 | 3220 |                     if (!mEffectChains.isEmpty()) { | 
 | 3221 |                         // Do not ramp volume if volume is controlled by effect | 
 | 3222 |                         mEffectChains[0]->setVolume_l(&vl, &vr); | 
 | 3223 |                         left = (float)vl / (1 << 24); | 
 | 3224 |                         right = (float)vr / (1 << 24); | 
 | 3225 |                     } | 
 | 3226 |                     mOutput->stream->set_volume(mOutput->stream, left, right); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3227 |                 } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3228 |  | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3229 |                 // reset retry count | 
 | 3230 |                 track->mRetryCount = kMaxTrackRetriesDirect; | 
 | 3231 |                 mActiveTrack = t; | 
 | 3232 |                 mixerStatus = MIXER_TRACKS_READY; | 
 | 3233 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3234 |         } else { | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3235 |             // clear effect chain input buffer if the last active track started underruns | 
 | 3236 |             // to avoid sending previous audio buffer again to effects | 
 | 3237 |             if (!mEffectChains.isEmpty() && (i == (count -1))) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3238 |                 mEffectChains[0]->clearInputBuffer(); | 
 | 3239 |             } | 
 | 3240 |  | 
 | 3241 |             ALOGVV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server); | 
 | 3242 |             if ((track->sharedBuffer() != 0) || track->isTerminated() || | 
 | 3243 |                     track->isStopped() || track->isPaused()) { | 
 | 3244 |                 // We have consumed all the buffers of this track. | 
 | 3245 |                 // Remove it from the list of active tracks. | 
 | 3246 |                 // TODO: implement behavior for compressed audio | 
 | 3247 |                 size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; | 
 | 3248 |                 size_t framesWritten = mBytesWritten / mFrameSize; | 
 | 3249 |                 if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { | 
 | 3250 |                     if (track->isStopped()) { | 
 | 3251 |                         track->reset(); | 
 | 3252 |                     } | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3253 |                     tracksToRemove->add(track); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3254 |                 } | 
 | 3255 |             } else { | 
 | 3256 |                 // No buffers for this track. Give it a few chances to | 
 | 3257 |                 // fill a buffer, then remove it from active list. | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3258 |                 // Only consider last track started for mixer state control | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3259 |                 if (--(track->mRetryCount) <= 0) { | 
 | 3260 |                     ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3261 |                     tracksToRemove->add(track); | 
 | 3262 |                 } else if (i == (count -1)){ | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3263 |                     mixerStatus = MIXER_TRACKS_ENABLED; | 
 | 3264 |                 } | 
 | 3265 |             } | 
 | 3266 |         } | 
 | 3267 |     } | 
 | 3268 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3269 |     // remove all the tracks that need to be... | 
| Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3270 |     count = tracksToRemove->size(); | 
 | 3271 |     if (CC_UNLIKELY(count)) { | 
 | 3272 |         for (size_t i = 0 ; i < count ; i++) { | 
 | 3273 |             const sp<Track>& track = tracksToRemove->itemAt(i); | 
 | 3274 |             mActiveTracks.remove(track); | 
 | 3275 |             if (!mEffectChains.isEmpty()) { | 
 | 3276 |                 ALOGV("stopping track on chain %p for session Id: %d", mEffectChains[0].get(), | 
 | 3277 |                       track->sessionId()); | 
 | 3278 |                 mEffectChains[0]->decActiveTrackCnt(); | 
 | 3279 |             } | 
 | 3280 |             if (track->isTerminated()) { | 
 | 3281 |                 removeTrack_l(track); | 
 | 3282 |             } | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3283 |         } | 
 | 3284 |     } | 
 | 3285 |  | 
 | 3286 |     return mixerStatus; | 
 | 3287 | } | 
 | 3288 |  | 
 | 3289 | void AudioFlinger::DirectOutputThread::threadLoop_mix() | 
 | 3290 | { | 
 | 3291 |     AudioBufferProvider::Buffer buffer; | 
 | 3292 |     size_t frameCount = mFrameCount; | 
 | 3293 |     int8_t *curBuf = (int8_t *)mMixBuffer; | 
 | 3294 |     // output audio to hardware | 
 | 3295 |     while (frameCount) { | 
 | 3296 |         buffer.frameCount = frameCount; | 
 | 3297 |         mActiveTrack->getNextBuffer(&buffer); | 
 | 3298 |         if (CC_UNLIKELY(buffer.raw == NULL)) { | 
 | 3299 |             memset(curBuf, 0, frameCount * mFrameSize); | 
 | 3300 |             break; | 
 | 3301 |         } | 
 | 3302 |         memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); | 
 | 3303 |         frameCount -= buffer.frameCount; | 
 | 3304 |         curBuf += buffer.frameCount * mFrameSize; | 
 | 3305 |         mActiveTrack->releaseBuffer(&buffer); | 
 | 3306 |     } | 
 | 3307 |     sleepTime = 0; | 
 | 3308 |     standbyTime = systemTime() + standbyDelay; | 
 | 3309 |     mActiveTrack.clear(); | 
 | 3310 |  | 
 | 3311 | } | 
 | 3312 |  | 
 | 3313 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() | 
 | 3314 | { | 
 | 3315 |     if (sleepTime == 0) { | 
 | 3316 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 3317 |             sleepTime = activeSleepTime; | 
 | 3318 |         } else { | 
 | 3319 |             sleepTime = idleSleepTime; | 
 | 3320 |         } | 
 | 3321 |     } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { | 
 | 3322 |         memset(mMixBuffer, 0, mFrameCount * mFrameSize); | 
 | 3323 |         sleepTime = 0; | 
 | 3324 |     } | 
 | 3325 | } | 
 | 3326 |  | 
 | 3327 | // getTrackName_l() must be called with ThreadBase::mLock held | 
 | 3328 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask, | 
 | 3329 |         int sessionId) | 
 | 3330 | { | 
 | 3331 |     return 0; | 
 | 3332 | } | 
 | 3333 |  | 
 | 3334 | // deleteTrackName_l() must be called with ThreadBase::mLock held | 
 | 3335 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name) | 
 | 3336 | { | 
 | 3337 | } | 
 | 3338 |  | 
 | 3339 | // checkForNewParameters_l() must be called with ThreadBase::mLock held | 
 | 3340 | bool AudioFlinger::DirectOutputThread::checkForNewParameters_l() | 
 | 3341 | { | 
 | 3342 |     bool reconfig = false; | 
 | 3343 |  | 
 | 3344 |     while (!mNewParameters.isEmpty()) { | 
 | 3345 |         status_t status = NO_ERROR; | 
 | 3346 |         String8 keyValuePair = mNewParameters[0]; | 
 | 3347 |         AudioParameter param = AudioParameter(keyValuePair); | 
 | 3348 |         int value; | 
 | 3349 |  | 
 | 3350 |         if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 3351 |             // do not accept frame count changes if tracks are open as the track buffer | 
 | 3352 |             // size depends on frame count and correct behavior would not be garantied | 
 | 3353 |             // if frame count is changed after track creation | 
 | 3354 |             if (!mTracks.isEmpty()) { | 
 | 3355 |                 status = INVALID_OPERATION; | 
 | 3356 |             } else { | 
 | 3357 |                 reconfig = true; | 
 | 3358 |             } | 
 | 3359 |         } | 
 | 3360 |         if (status == NO_ERROR) { | 
 | 3361 |             status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3362 |                                                     keyValuePair.string()); | 
 | 3363 |             if (!mStandby && status == INVALID_OPERATION) { | 
 | 3364 |                 mOutput->stream->common.standby(&mOutput->stream->common); | 
 | 3365 |                 mStandby = true; | 
 | 3366 |                 mBytesWritten = 0; | 
 | 3367 |                 status = mOutput->stream->common.set_parameters(&mOutput->stream->common, | 
 | 3368 |                                                        keyValuePair.string()); | 
 | 3369 |             } | 
 | 3370 |             if (status == NO_ERROR && reconfig) { | 
 | 3371 |                 readOutputParameters(); | 
 | 3372 |                 sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); | 
 | 3373 |             } | 
 | 3374 |         } | 
 | 3375 |  | 
 | 3376 |         mNewParameters.removeAt(0); | 
 | 3377 |  | 
 | 3378 |         mParamStatus = status; | 
 | 3379 |         mParamCond.signal(); | 
 | 3380 |         // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
 | 3381 |         // already timed out waiting for the status and will never signal the condition. | 
 | 3382 |         mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
 | 3383 |     } | 
 | 3384 |     return reconfig; | 
 | 3385 | } | 
 | 3386 |  | 
 | 3387 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const | 
 | 3388 | { | 
 | 3389 |     uint32_t time; | 
 | 3390 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 3391 |         time = PlaybackThread::activeSleepTimeUs(); | 
 | 3392 |     } else { | 
 | 3393 |         time = 10000; | 
 | 3394 |     } | 
 | 3395 |     return time; | 
 | 3396 | } | 
 | 3397 |  | 
 | 3398 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const | 
 | 3399 | { | 
 | 3400 |     uint32_t time; | 
 | 3401 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 3402 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; | 
 | 3403 |     } else { | 
 | 3404 |         time = 10000; | 
 | 3405 |     } | 
 | 3406 |     return time; | 
 | 3407 | } | 
 | 3408 |  | 
 | 3409 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const | 
 | 3410 | { | 
 | 3411 |     uint32_t time; | 
 | 3412 |     if (audio_is_linear_pcm(mFormat)) { | 
 | 3413 |         time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); | 
 | 3414 |     } else { | 
 | 3415 |         time = 10000; | 
 | 3416 |     } | 
 | 3417 |     return time; | 
 | 3418 | } | 
 | 3419 |  | 
 | 3420 | void AudioFlinger::DirectOutputThread::cacheParameters_l() | 
 | 3421 | { | 
 | 3422 |     PlaybackThread::cacheParameters_l(); | 
 | 3423 |  | 
 | 3424 |     // use shorter standby delay as on normal output to release | 
 | 3425 |     // hardware resources as soon as possible | 
 | 3426 |     standbyDelay = microseconds(activeSleepTime*2); | 
 | 3427 | } | 
 | 3428 |  | 
 | 3429 | // ---------------------------------------------------------------------------- | 
 | 3430 |  | 
 | 3431 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, | 
 | 3432 |         AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) | 
 | 3433 |     :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), | 
 | 3434 |                 DUPLICATING), | 
 | 3435 |         mWaitTimeMs(UINT_MAX) | 
 | 3436 | { | 
 | 3437 |     addOutputTrack(mainThread); | 
 | 3438 | } | 
 | 3439 |  | 
 | 3440 | AudioFlinger::DuplicatingThread::~DuplicatingThread() | 
 | 3441 | { | 
 | 3442 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 3443 |         mOutputTracks[i]->destroy(); | 
 | 3444 |     } | 
 | 3445 | } | 
 | 3446 |  | 
 | 3447 | void AudioFlinger::DuplicatingThread::threadLoop_mix() | 
 | 3448 | { | 
 | 3449 |     // mix buffers... | 
 | 3450 |     if (outputsReady(outputTracks)) { | 
 | 3451 |         mAudioMixer->process(AudioBufferProvider::kInvalidPTS); | 
 | 3452 |     } else { | 
 | 3453 |         memset(mMixBuffer, 0, mixBufferSize); | 
 | 3454 |     } | 
 | 3455 |     sleepTime = 0; | 
 | 3456 |     writeFrames = mNormalFrameCount; | 
 | 3457 |     standbyTime = systemTime() + standbyDelay; | 
 | 3458 | } | 
 | 3459 |  | 
 | 3460 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() | 
 | 3461 | { | 
 | 3462 |     if (sleepTime == 0) { | 
 | 3463 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 3464 |             sleepTime = activeSleepTime; | 
 | 3465 |         } else { | 
 | 3466 |             sleepTime = idleSleepTime; | 
 | 3467 |         } | 
 | 3468 |     } else if (mBytesWritten != 0) { | 
 | 3469 |         if (mMixerStatus == MIXER_TRACKS_ENABLED) { | 
 | 3470 |             writeFrames = mNormalFrameCount; | 
 | 3471 |             memset(mMixBuffer, 0, mixBufferSize); | 
 | 3472 |         } else { | 
 | 3473 |             // flush remaining overflow buffers in output tracks | 
 | 3474 |             writeFrames = 0; | 
 | 3475 |         } | 
 | 3476 |         sleepTime = 0; | 
 | 3477 |     } | 
 | 3478 | } | 
 | 3479 |  | 
 | 3480 | void AudioFlinger::DuplicatingThread::threadLoop_write() | 
 | 3481 | { | 
 | 3482 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 3483 |         outputTracks[i]->write(mMixBuffer, writeFrames); | 
 | 3484 |     } | 
 | 3485 |     mBytesWritten += mixBufferSize; | 
 | 3486 | } | 
 | 3487 |  | 
 | 3488 | void AudioFlinger::DuplicatingThread::threadLoop_standby() | 
 | 3489 | { | 
 | 3490 |     // DuplicatingThread implements standby by stopping all tracks | 
 | 3491 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 3492 |         outputTracks[i]->stop(); | 
 | 3493 |     } | 
 | 3494 | } | 
 | 3495 |  | 
 | 3496 | void AudioFlinger::DuplicatingThread::saveOutputTracks() | 
 | 3497 | { | 
 | 3498 |     outputTracks = mOutputTracks; | 
 | 3499 | } | 
 | 3500 |  | 
 | 3501 | void AudioFlinger::DuplicatingThread::clearOutputTracks() | 
 | 3502 | { | 
 | 3503 |     outputTracks.clear(); | 
 | 3504 | } | 
 | 3505 |  | 
 | 3506 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) | 
 | 3507 | { | 
 | 3508 |     Mutex::Autolock _l(mLock); | 
 | 3509 |     // FIXME explain this formula | 
 | 3510 |     size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); | 
 | 3511 |     OutputTrack *outputTrack = new OutputTrack(thread, | 
 | 3512 |                                             this, | 
 | 3513 |                                             mSampleRate, | 
 | 3514 |                                             mFormat, | 
 | 3515 |                                             mChannelMask, | 
 | 3516 |                                             frameCount); | 
 | 3517 |     if (outputTrack->cblk() != NULL) { | 
 | 3518 |         thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); | 
 | 3519 |         mOutputTracks.add(outputTrack); | 
 | 3520 |         ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); | 
 | 3521 |         updateWaitTime_l(); | 
 | 3522 |     } | 
 | 3523 | } | 
 | 3524 |  | 
 | 3525 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) | 
 | 3526 | { | 
 | 3527 |     Mutex::Autolock _l(mLock); | 
 | 3528 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 3529 |         if (mOutputTracks[i]->thread() == thread) { | 
 | 3530 |             mOutputTracks[i]->destroy(); | 
 | 3531 |             mOutputTracks.removeAt(i); | 
 | 3532 |             updateWaitTime_l(); | 
 | 3533 |             return; | 
 | 3534 |         } | 
 | 3535 |     } | 
 | 3536 |     ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); | 
 | 3537 | } | 
 | 3538 |  | 
 | 3539 | // caller must hold mLock | 
 | 3540 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() | 
 | 3541 | { | 
 | 3542 |     mWaitTimeMs = UINT_MAX; | 
 | 3543 |     for (size_t i = 0; i < mOutputTracks.size(); i++) { | 
 | 3544 |         sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); | 
 | 3545 |         if (strong != 0) { | 
 | 3546 |             uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); | 
 | 3547 |             if (waitTimeMs < mWaitTimeMs) { | 
 | 3548 |                 mWaitTimeMs = waitTimeMs; | 
 | 3549 |             } | 
 | 3550 |         } | 
 | 3551 |     } | 
 | 3552 | } | 
 | 3553 |  | 
 | 3554 |  | 
 | 3555 | bool AudioFlinger::DuplicatingThread::outputsReady( | 
 | 3556 |         const SortedVector< sp<OutputTrack> > &outputTracks) | 
 | 3557 | { | 
 | 3558 |     for (size_t i = 0; i < outputTracks.size(); i++) { | 
 | 3559 |         sp<ThreadBase> thread = outputTracks[i]->thread().promote(); | 
 | 3560 |         if (thread == 0) { | 
 | 3561 |             ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", | 
 | 3562 |                     outputTracks[i].get()); | 
 | 3563 |             return false; | 
 | 3564 |         } | 
 | 3565 |         PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
 | 3566 |         // see note at standby() declaration | 
 | 3567 |         if (playbackThread->standby() && !playbackThread->isSuspended()) { | 
 | 3568 |             ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), | 
 | 3569 |                     thread.get()); | 
 | 3570 |             return false; | 
 | 3571 |         } | 
 | 3572 |     } | 
 | 3573 |     return true; | 
 | 3574 | } | 
 | 3575 |  | 
 | 3576 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const | 
 | 3577 | { | 
 | 3578 |     return (mWaitTimeMs * 1000) / 2; | 
 | 3579 | } | 
 | 3580 |  | 
 | 3581 | void AudioFlinger::DuplicatingThread::cacheParameters_l() | 
 | 3582 | { | 
 | 3583 |     // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first | 
 | 3584 |     updateWaitTime_l(); | 
 | 3585 |  | 
 | 3586 |     MixerThread::cacheParameters_l(); | 
 | 3587 | } | 
 | 3588 |  | 
 | 3589 | // ---------------------------------------------------------------------------- | 
 | 3590 | //      Record | 
 | 3591 | // ---------------------------------------------------------------------------- | 
 | 3592 |  | 
 | 3593 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, | 
 | 3594 |                                          AudioStreamIn *input, | 
 | 3595 |                                          uint32_t sampleRate, | 
 | 3596 |                                          audio_channel_mask_t channelMask, | 
 | 3597 |                                          audio_io_handle_t id, | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 3598 |                                          audio_devices_t outDevice, | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3599 |                                          audio_devices_t inDevice | 
 | 3600 | #ifdef TEE_SINK | 
 | 3601 |                                          , const sp<NBAIO_Sink>& teeSink | 
 | 3602 | #endif | 
 | 3603 |                                          ) : | 
| Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 3604 |     ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3605 |     mInput(input), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL), | 
 | 3606 |     // mRsmpInIndex and mInputBytes set by readInputParameters() | 
 | 3607 |     mReqChannelCount(popcount(channelMask)), | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3608 |     mReqSampleRate(sampleRate) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3609 |     // mBytesRead is only meaningful while active, and so is cleared in start() | 
 | 3610 |     // (but might be better to also clear here for dump?) | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3611 | #ifdef TEE_SINK | 
 | 3612 |     , mTeeSink(teeSink) | 
 | 3613 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3614 | { | 
 | 3615 |     snprintf(mName, kNameLength, "AudioIn_%X", id); | 
 | 3616 |  | 
 | 3617 |     readInputParameters(); | 
 | 3618 |  | 
 | 3619 | } | 
 | 3620 |  | 
 | 3621 |  | 
 | 3622 | AudioFlinger::RecordThread::~RecordThread() | 
 | 3623 | { | 
 | 3624 |     delete[] mRsmpInBuffer; | 
 | 3625 |     delete mResampler; | 
 | 3626 |     delete[] mRsmpOutBuffer; | 
 | 3627 | } | 
 | 3628 |  | 
 | 3629 | void AudioFlinger::RecordThread::onFirstRef() | 
 | 3630 | { | 
 | 3631 |     run(mName, PRIORITY_URGENT_AUDIO); | 
 | 3632 | } | 
 | 3633 |  | 
 | 3634 | status_t AudioFlinger::RecordThread::readyToRun() | 
 | 3635 | { | 
 | 3636 |     status_t status = initCheck(); | 
 | 3637 |     ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this); | 
 | 3638 |     return status; | 
 | 3639 | } | 
 | 3640 |  | 
 | 3641 | bool AudioFlinger::RecordThread::threadLoop() | 
 | 3642 | { | 
 | 3643 |     AudioBufferProvider::Buffer buffer; | 
 | 3644 |     sp<RecordTrack> activeTrack; | 
 | 3645 |     Vector< sp<EffectChain> > effectChains; | 
 | 3646 |  | 
 | 3647 |     nsecs_t lastWarning = 0; | 
 | 3648 |  | 
 | 3649 |     inputStandBy(); | 
 | 3650 |     acquireWakeLock(); | 
 | 3651 |  | 
 | 3652 |     // used to verify we've read at least once before evaluating how many bytes were read | 
 | 3653 |     bool readOnce = false; | 
 | 3654 |  | 
 | 3655 |     // start recording | 
 | 3656 |     while (!exitPending()) { | 
 | 3657 |  | 
 | 3658 |         processConfigEvents(); | 
 | 3659 |  | 
 | 3660 |         { // scope for mLock | 
 | 3661 |             Mutex::Autolock _l(mLock); | 
 | 3662 |             checkForNewParameters_l(); | 
 | 3663 |             if (mActiveTrack == 0 && mConfigEvents.isEmpty()) { | 
 | 3664 |                 standby(); | 
 | 3665 |  | 
 | 3666 |                 if (exitPending()) { | 
 | 3667 |                     break; | 
 | 3668 |                 } | 
 | 3669 |  | 
 | 3670 |                 releaseWakeLock_l(); | 
 | 3671 |                 ALOGV("RecordThread: loop stopping"); | 
 | 3672 |                 // go to sleep | 
 | 3673 |                 mWaitWorkCV.wait(mLock); | 
 | 3674 |                 ALOGV("RecordThread: loop starting"); | 
 | 3675 |                 acquireWakeLock_l(); | 
 | 3676 |                 continue; | 
 | 3677 |             } | 
 | 3678 |             if (mActiveTrack != 0) { | 
 | 3679 |                 if (mActiveTrack->mState == TrackBase::PAUSING) { | 
 | 3680 |                     standby(); | 
 | 3681 |                     mActiveTrack.clear(); | 
 | 3682 |                     mStartStopCond.broadcast(); | 
 | 3683 |                 } else if (mActiveTrack->mState == TrackBase::RESUMING) { | 
 | 3684 |                     if (mReqChannelCount != mActiveTrack->channelCount()) { | 
 | 3685 |                         mActiveTrack.clear(); | 
 | 3686 |                         mStartStopCond.broadcast(); | 
 | 3687 |                     } else if (readOnce) { | 
 | 3688 |                         // record start succeeds only if first read from audio input | 
 | 3689 |                         // succeeds | 
 | 3690 |                         if (mBytesRead >= 0) { | 
 | 3691 |                             mActiveTrack->mState = TrackBase::ACTIVE; | 
 | 3692 |                         } else { | 
 | 3693 |                             mActiveTrack.clear(); | 
 | 3694 |                         } | 
 | 3695 |                         mStartStopCond.broadcast(); | 
 | 3696 |                     } | 
 | 3697 |                     mStandby = false; | 
 | 3698 |                 } else if (mActiveTrack->mState == TrackBase::TERMINATED) { | 
 | 3699 |                     removeTrack_l(mActiveTrack); | 
 | 3700 |                     mActiveTrack.clear(); | 
 | 3701 |                 } | 
 | 3702 |             } | 
 | 3703 |             lockEffectChains_l(effectChains); | 
 | 3704 |         } | 
 | 3705 |  | 
 | 3706 |         if (mActiveTrack != 0) { | 
 | 3707 |             if (mActiveTrack->mState != TrackBase::ACTIVE && | 
 | 3708 |                 mActiveTrack->mState != TrackBase::RESUMING) { | 
 | 3709 |                 unlockEffectChains(effectChains); | 
 | 3710 |                 usleep(kRecordThreadSleepUs); | 
 | 3711 |                 continue; | 
 | 3712 |             } | 
 | 3713 |             for (size_t i = 0; i < effectChains.size(); i ++) { | 
 | 3714 |                 effectChains[i]->process_l(); | 
 | 3715 |             } | 
 | 3716 |  | 
 | 3717 |             buffer.frameCount = mFrameCount; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3718 |             status_t status = mActiveTrack->getNextBuffer(&buffer); | 
 | 3719 |             if (CC_LIKELY(status == NO_ERROR)) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3720 |                 readOnce = true; | 
 | 3721 |                 size_t framesOut = buffer.frameCount; | 
 | 3722 |                 if (mResampler == NULL) { | 
 | 3723 |                     // no resampling | 
 | 3724 |                     while (framesOut) { | 
 | 3725 |                         size_t framesIn = mFrameCount - mRsmpInIndex; | 
 | 3726 |                         if (framesIn) { | 
 | 3727 |                             int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize; | 
 | 3728 |                             int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * | 
 | 3729 |                                     mActiveTrack->mFrameSize; | 
 | 3730 |                             if (framesIn > framesOut) | 
 | 3731 |                                 framesIn = framesOut; | 
 | 3732 |                             mRsmpInIndex += framesIn; | 
 | 3733 |                             framesOut -= framesIn; | 
 | 3734 |                             if (mChannelCount == mReqChannelCount || | 
 | 3735 |                                 mFormat != AUDIO_FORMAT_PCM_16_BIT) { | 
 | 3736 |                                 memcpy(dst, src, framesIn * mFrameSize); | 
 | 3737 |                             } else { | 
 | 3738 |                                 if (mChannelCount == 1) { | 
 | 3739 |                                     upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, | 
 | 3740 |                                             (int16_t *)src, framesIn); | 
 | 3741 |                                 } else { | 
 | 3742 |                                     downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, | 
 | 3743 |                                             (int16_t *)src, framesIn); | 
 | 3744 |                                 } | 
 | 3745 |                             } | 
 | 3746 |                         } | 
 | 3747 |                         if (framesOut && mFrameCount == mRsmpInIndex) { | 
 | 3748 |                             void *readInto; | 
 | 3749 |                             if (framesOut == mFrameCount && | 
 | 3750 |                                 (mChannelCount == mReqChannelCount || | 
 | 3751 |                                         mFormat != AUDIO_FORMAT_PCM_16_BIT)) { | 
 | 3752 |                                 readInto = buffer.raw; | 
 | 3753 |                                 framesOut = 0; | 
 | 3754 |                             } else { | 
 | 3755 |                                 readInto = mRsmpInBuffer; | 
 | 3756 |                                 mRsmpInIndex = 0; | 
 | 3757 |                             } | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 3758 |                             mBytesRead = mInput->stream->read(mInput->stream, readInto, | 
 | 3759 |                                     mInputBytes); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3760 |                             if (mBytesRead <= 0) { | 
 | 3761 |                                 if ((mBytesRead < 0) && (mActiveTrack->mState == TrackBase::ACTIVE)) | 
 | 3762 |                                 { | 
 | 3763 |                                     ALOGE("Error reading audio input"); | 
 | 3764 |                                     // Force input into standby so that it tries to | 
 | 3765 |                                     // recover at next read attempt | 
 | 3766 |                                     inputStandBy(); | 
 | 3767 |                                     usleep(kRecordThreadSleepUs); | 
 | 3768 |                                 } | 
 | 3769 |                                 mRsmpInIndex = mFrameCount; | 
 | 3770 |                                 framesOut = 0; | 
 | 3771 |                                 buffer.frameCount = 0; | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3772 |                             } | 
 | 3773 | #ifdef TEE_SINK | 
 | 3774 |                             else if (mTeeSink != 0) { | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3775 |                                 (void) mTeeSink->write(readInto, | 
 | 3776 |                                         mBytesRead >> Format_frameBitShift(mTeeSink->format())); | 
 | 3777 |                             } | 
| Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3778 | #endif | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3779 |                         } | 
 | 3780 |                     } | 
 | 3781 |                 } else { | 
 | 3782 |                     // resampling | 
 | 3783 |  | 
 | 3784 |                     memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t)); | 
 | 3785 |                     // alter output frame count as if we were expecting stereo samples | 
 | 3786 |                     if (mChannelCount == 1 && mReqChannelCount == 1) { | 
 | 3787 |                         framesOut >>= 1; | 
 | 3788 |                     } | 
 | 3789 |                     mResampler->resample(mRsmpOutBuffer, framesOut, | 
 | 3790 |                             this /* AudioBufferProvider* */); | 
 | 3791 |                     // ditherAndClamp() works as long as all buffers returned by | 
 | 3792 |                     // mActiveTrack->getNextBuffer() are 32 bit aligned which should be always true. | 
 | 3793 |                     if (mChannelCount == 2 && mReqChannelCount == 1) { | 
 | 3794 |                         ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut); | 
 | 3795 |                         // the resampler always outputs stereo samples: | 
 | 3796 |                         // do post stereo to mono conversion | 
 | 3797 |                         downmix_to_mono_i16_from_stereo_i16(buffer.i16, (int16_t *)mRsmpOutBuffer, | 
 | 3798 |                                 framesOut); | 
 | 3799 |                     } else { | 
 | 3800 |                         ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut); | 
 | 3801 |                     } | 
 | 3802 |  | 
 | 3803 |                 } | 
 | 3804 |                 if (mFramestoDrop == 0) { | 
 | 3805 |                     mActiveTrack->releaseBuffer(&buffer); | 
 | 3806 |                 } else { | 
 | 3807 |                     if (mFramestoDrop > 0) { | 
 | 3808 |                         mFramestoDrop -= buffer.frameCount; | 
 | 3809 |                         if (mFramestoDrop <= 0) { | 
 | 3810 |                             clearSyncStartEvent(); | 
 | 3811 |                         } | 
 | 3812 |                     } else { | 
 | 3813 |                         mFramestoDrop += buffer.frameCount; | 
 | 3814 |                         if (mFramestoDrop >= 0 || mSyncStartEvent == 0 || | 
 | 3815 |                                 mSyncStartEvent->isCancelled()) { | 
 | 3816 |                             ALOGW("Synced record %s, session %d, trigger session %d", | 
 | 3817 |                                   (mFramestoDrop >= 0) ? "timed out" : "cancelled", | 
 | 3818 |                                   mActiveTrack->sessionId(), | 
 | 3819 |                                   (mSyncStartEvent != 0) ? mSyncStartEvent->triggerSession() : 0); | 
 | 3820 |                             clearSyncStartEvent(); | 
 | 3821 |                         } | 
 | 3822 |                     } | 
 | 3823 |                 } | 
 | 3824 |                 mActiveTrack->clearOverflow(); | 
 | 3825 |             } | 
 | 3826 |             // client isn't retrieving buffers fast enough | 
 | 3827 |             else { | 
 | 3828 |                 if (!mActiveTrack->setOverflow()) { | 
 | 3829 |                     nsecs_t now = systemTime(); | 
 | 3830 |                     if ((now - lastWarning) > kWarningThrottleNs) { | 
 | 3831 |                         ALOGW("RecordThread: buffer overflow"); | 
 | 3832 |                         lastWarning = now; | 
 | 3833 |                     } | 
 | 3834 |                 } | 
 | 3835 |                 // Release the processor for a while before asking for a new buffer. | 
 | 3836 |                 // This will give the application more chance to read from the buffer and | 
 | 3837 |                 // clear the overflow. | 
 | 3838 |                 usleep(kRecordThreadSleepUs); | 
 | 3839 |             } | 
 | 3840 |         } | 
 | 3841 |         // enable changes in effect chain | 
 | 3842 |         unlockEffectChains(effectChains); | 
 | 3843 |         effectChains.clear(); | 
 | 3844 |     } | 
 | 3845 |  | 
 | 3846 |     standby(); | 
 | 3847 |  | 
 | 3848 |     { | 
 | 3849 |         Mutex::Autolock _l(mLock); | 
 | 3850 |         mActiveTrack.clear(); | 
 | 3851 |         mStartStopCond.broadcast(); | 
 | 3852 |     } | 
 | 3853 |  | 
 | 3854 |     releaseWakeLock(); | 
 | 3855 |  | 
 | 3856 |     ALOGV("RecordThread %p exiting", this); | 
 | 3857 |     return false; | 
 | 3858 | } | 
 | 3859 |  | 
 | 3860 | void AudioFlinger::RecordThread::standby() | 
 | 3861 | { | 
 | 3862 |     if (!mStandby) { | 
 | 3863 |         inputStandBy(); | 
 | 3864 |         mStandby = true; | 
 | 3865 |     } | 
 | 3866 | } | 
 | 3867 |  | 
 | 3868 | void AudioFlinger::RecordThread::inputStandBy() | 
 | 3869 | { | 
 | 3870 |     mInput->stream->common.standby(&mInput->stream->common); | 
 | 3871 | } | 
 | 3872 |  | 
 | 3873 | sp<AudioFlinger::RecordThread::RecordTrack>  AudioFlinger::RecordThread::createRecordTrack_l( | 
 | 3874 |         const sp<AudioFlinger::Client>& client, | 
 | 3875 |         uint32_t sampleRate, | 
 | 3876 |         audio_format_t format, | 
 | 3877 |         audio_channel_mask_t channelMask, | 
 | 3878 |         size_t frameCount, | 
 | 3879 |         int sessionId, | 
 | 3880 |         IAudioFlinger::track_flags_t flags, | 
 | 3881 |         pid_t tid, | 
 | 3882 |         status_t *status) | 
 | 3883 | { | 
 | 3884 |     sp<RecordTrack> track; | 
 | 3885 |     status_t lStatus; | 
 | 3886 |  | 
 | 3887 |     lStatus = initCheck(); | 
 | 3888 |     if (lStatus != NO_ERROR) { | 
 | 3889 |         ALOGE("Audio driver not initialized."); | 
 | 3890 |         goto Exit; | 
 | 3891 |     } | 
 | 3892 |  | 
 | 3893 |     // FIXME use flags and tid similar to createTrack_l() | 
 | 3894 |  | 
 | 3895 |     { // scope for mLock | 
 | 3896 |         Mutex::Autolock _l(mLock); | 
 | 3897 |  | 
 | 3898 |         track = new RecordTrack(this, client, sampleRate, | 
 | 3899 |                       format, channelMask, frameCount, sessionId); | 
 | 3900 |  | 
 | 3901 |         if (track->getCblk() == 0) { | 
 | 3902 |             lStatus = NO_MEMORY; | 
 | 3903 |             goto Exit; | 
 | 3904 |         } | 
 | 3905 |         mTracks.add(track); | 
 | 3906 |  | 
 | 3907 |         // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
 | 3908 |         bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 3909 |                         mAudioFlinger->btNrecIsOff(); | 
 | 3910 |         setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); | 
 | 3911 |         setEffectSuspended_l(FX_IID_NS, suspend, sessionId); | 
 | 3912 |     } | 
 | 3913 |     lStatus = NO_ERROR; | 
 | 3914 |  | 
 | 3915 | Exit: | 
 | 3916 |     if (status) { | 
 | 3917 |         *status = lStatus; | 
 | 3918 |     } | 
 | 3919 |     return track; | 
 | 3920 | } | 
 | 3921 |  | 
 | 3922 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, | 
 | 3923 |                                            AudioSystem::sync_event_t event, | 
 | 3924 |                                            int triggerSession) | 
 | 3925 | { | 
 | 3926 |     ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); | 
 | 3927 |     sp<ThreadBase> strongMe = this; | 
 | 3928 |     status_t status = NO_ERROR; | 
 | 3929 |  | 
 | 3930 |     if (event == AudioSystem::SYNC_EVENT_NONE) { | 
 | 3931 |         clearSyncStartEvent(); | 
 | 3932 |     } else if (event != AudioSystem::SYNC_EVENT_SAME) { | 
 | 3933 |         mSyncStartEvent = mAudioFlinger->createSyncEvent(event, | 
 | 3934 |                                        triggerSession, | 
 | 3935 |                                        recordTrack->sessionId(), | 
 | 3936 |                                        syncStartEventCallback, | 
 | 3937 |                                        this); | 
 | 3938 |         // Sync event can be cancelled by the trigger session if the track is not in a | 
 | 3939 |         // compatible state in which case we start record immediately | 
 | 3940 |         if (mSyncStartEvent->isCancelled()) { | 
 | 3941 |             clearSyncStartEvent(); | 
 | 3942 |         } else { | 
 | 3943 |             // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs | 
 | 3944 |             mFramestoDrop = - ((AudioSystem::kSyncRecordStartTimeOutMs * mReqSampleRate) / 1000); | 
 | 3945 |         } | 
 | 3946 |     } | 
 | 3947 |  | 
 | 3948 |     { | 
 | 3949 |         AutoMutex lock(mLock); | 
 | 3950 |         if (mActiveTrack != 0) { | 
 | 3951 |             if (recordTrack != mActiveTrack.get()) { | 
 | 3952 |                 status = -EBUSY; | 
 | 3953 |             } else if (mActiveTrack->mState == TrackBase::PAUSING) { | 
 | 3954 |                 mActiveTrack->mState = TrackBase::ACTIVE; | 
 | 3955 |             } | 
 | 3956 |             return status; | 
 | 3957 |         } | 
 | 3958 |  | 
 | 3959 |         recordTrack->mState = TrackBase::IDLE; | 
 | 3960 |         mActiveTrack = recordTrack; | 
 | 3961 |         mLock.unlock(); | 
 | 3962 |         status_t status = AudioSystem::startInput(mId); | 
 | 3963 |         mLock.lock(); | 
 | 3964 |         if (status != NO_ERROR) { | 
 | 3965 |             mActiveTrack.clear(); | 
 | 3966 |             clearSyncStartEvent(); | 
 | 3967 |             return status; | 
 | 3968 |         } | 
 | 3969 |         mRsmpInIndex = mFrameCount; | 
 | 3970 |         mBytesRead = 0; | 
 | 3971 |         if (mResampler != NULL) { | 
 | 3972 |             mResampler->reset(); | 
 | 3973 |         } | 
 | 3974 |         mActiveTrack->mState = TrackBase::RESUMING; | 
 | 3975 |         // signal thread to start | 
 | 3976 |         ALOGV("Signal record thread"); | 
 | 3977 |         mWaitWorkCV.broadcast(); | 
 | 3978 |         // do not wait for mStartStopCond if exiting | 
 | 3979 |         if (exitPending()) { | 
 | 3980 |             mActiveTrack.clear(); | 
 | 3981 |             status = INVALID_OPERATION; | 
 | 3982 |             goto startError; | 
 | 3983 |         } | 
 | 3984 |         mStartStopCond.wait(mLock); | 
 | 3985 |         if (mActiveTrack == 0) { | 
 | 3986 |             ALOGV("Record failed to start"); | 
 | 3987 |             status = BAD_VALUE; | 
 | 3988 |             goto startError; | 
 | 3989 |         } | 
 | 3990 |         ALOGV("Record started OK"); | 
 | 3991 |         return status; | 
 | 3992 |     } | 
| Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 3993 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3994 | startError: | 
 | 3995 |     AudioSystem::stopInput(mId); | 
 | 3996 |     clearSyncStartEvent(); | 
 | 3997 |     return status; | 
 | 3998 | } | 
 | 3999 |  | 
 | 4000 | void AudioFlinger::RecordThread::clearSyncStartEvent() | 
 | 4001 | { | 
 | 4002 |     if (mSyncStartEvent != 0) { | 
 | 4003 |         mSyncStartEvent->cancel(); | 
 | 4004 |     } | 
 | 4005 |     mSyncStartEvent.clear(); | 
 | 4006 |     mFramestoDrop = 0; | 
 | 4007 | } | 
 | 4008 |  | 
 | 4009 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) | 
 | 4010 | { | 
 | 4011 |     sp<SyncEvent> strongEvent = event.promote(); | 
 | 4012 |  | 
 | 4013 |     if (strongEvent != 0) { | 
 | 4014 |         RecordThread *me = (RecordThread *)strongEvent->cookie(); | 
 | 4015 |         me->handleSyncStartEvent(strongEvent); | 
 | 4016 |     } | 
 | 4017 | } | 
 | 4018 |  | 
 | 4019 | void AudioFlinger::RecordThread::handleSyncStartEvent(const sp<SyncEvent>& event) | 
 | 4020 | { | 
 | 4021 |     if (event == mSyncStartEvent) { | 
 | 4022 |         // TODO: use actual buffer filling status instead of 2 buffers when info is available | 
 | 4023 |         // from audio HAL | 
 | 4024 |         mFramestoDrop = mFrameCount * 2; | 
 | 4025 |     } | 
 | 4026 | } | 
 | 4027 |  | 
 | 4028 | bool AudioFlinger::RecordThread::stop_l(RecordThread::RecordTrack* recordTrack) { | 
 | 4029 |     ALOGV("RecordThread::stop"); | 
 | 4030 |     if (recordTrack != mActiveTrack.get() || recordTrack->mState == TrackBase::PAUSING) { | 
 | 4031 |         return false; | 
 | 4032 |     } | 
 | 4033 |     recordTrack->mState = TrackBase::PAUSING; | 
 | 4034 |     // do not wait for mStartStopCond if exiting | 
 | 4035 |     if (exitPending()) { | 
 | 4036 |         return true; | 
 | 4037 |     } | 
 | 4038 |     mStartStopCond.wait(mLock); | 
 | 4039 |     // if we have been restarted, recordTrack == mActiveTrack.get() here | 
 | 4040 |     if (exitPending() || recordTrack != mActiveTrack.get()) { | 
 | 4041 |         ALOGV("Record stopped OK"); | 
 | 4042 |         return true; | 
 | 4043 |     } | 
 | 4044 |     return false; | 
 | 4045 | } | 
 | 4046 |  | 
 | 4047 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event) const | 
 | 4048 | { | 
 | 4049 |     return false; | 
 | 4050 | } | 
 | 4051 |  | 
 | 4052 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event) | 
 | 4053 | { | 
 | 4054 | #if 0   // This branch is currently dead code, but is preserved in case it will be needed in future | 
 | 4055 |     if (!isValidSyncEvent(event)) { | 
 | 4056 |         return BAD_VALUE; | 
 | 4057 |     } | 
 | 4058 |  | 
 | 4059 |     int eventSession = event->triggerSession(); | 
 | 4060 |     status_t ret = NAME_NOT_FOUND; | 
 | 4061 |  | 
 | 4062 |     Mutex::Autolock _l(mLock); | 
 | 4063 |  | 
 | 4064 |     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4065 |         sp<RecordTrack> track = mTracks[i]; | 
 | 4066 |         if (eventSession == track->sessionId()) { | 
 | 4067 |             (void) track->setSyncEvent(event); | 
 | 4068 |             ret = NO_ERROR; | 
 | 4069 |         } | 
 | 4070 |     } | 
 | 4071 |     return ret; | 
 | 4072 | #else | 
 | 4073 |     return BAD_VALUE; | 
 | 4074 | #endif | 
 | 4075 | } | 
 | 4076 |  | 
 | 4077 | // destroyTrack_l() must be called with ThreadBase::mLock held | 
 | 4078 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) | 
 | 4079 | { | 
 | 4080 |     track->mState = TrackBase::TERMINATED; | 
 | 4081 |     // active tracks are removed by threadLoop() | 
 | 4082 |     if (mActiveTrack != track) { | 
 | 4083 |         removeTrack_l(track); | 
 | 4084 |     } | 
 | 4085 | } | 
 | 4086 |  | 
 | 4087 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) | 
 | 4088 | { | 
 | 4089 |     mTracks.remove(track); | 
 | 4090 |     // need anything related to effects here? | 
 | 4091 | } | 
 | 4092 |  | 
 | 4093 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) | 
 | 4094 | { | 
 | 4095 |     dumpInternals(fd, args); | 
 | 4096 |     dumpTracks(fd, args); | 
 | 4097 |     dumpEffectChains(fd, args); | 
 | 4098 | } | 
 | 4099 |  | 
 | 4100 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) | 
 | 4101 | { | 
 | 4102 |     const size_t SIZE = 256; | 
 | 4103 |     char buffer[SIZE]; | 
 | 4104 |     String8 result; | 
 | 4105 |  | 
 | 4106 |     snprintf(buffer, SIZE, "\nInput thread %p internals\n", this); | 
 | 4107 |     result.append(buffer); | 
 | 4108 |  | 
 | 4109 |     if (mActiveTrack != 0) { | 
 | 4110 |         snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex); | 
 | 4111 |         result.append(buffer); | 
 | 4112 |         snprintf(buffer, SIZE, "In size: %d\n", mInputBytes); | 
 | 4113 |         result.append(buffer); | 
 | 4114 |         snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL)); | 
 | 4115 |         result.append(buffer); | 
 | 4116 |         snprintf(buffer, SIZE, "Out channel count: %u\n", mReqChannelCount); | 
 | 4117 |         result.append(buffer); | 
 | 4118 |         snprintf(buffer, SIZE, "Out sample rate: %u\n", mReqSampleRate); | 
 | 4119 |         result.append(buffer); | 
 | 4120 |     } else { | 
 | 4121 |         result.append("No active record client\n"); | 
 | 4122 |     } | 
 | 4123 |  | 
 | 4124 |     write(fd, result.string(), result.size()); | 
 | 4125 |  | 
 | 4126 |     dumpBase(fd, args); | 
 | 4127 | } | 
 | 4128 |  | 
 | 4129 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args) | 
 | 4130 | { | 
 | 4131 |     const size_t SIZE = 256; | 
 | 4132 |     char buffer[SIZE]; | 
 | 4133 |     String8 result; | 
 | 4134 |  | 
 | 4135 |     snprintf(buffer, SIZE, "Input thread %p tracks\n", this); | 
 | 4136 |     result.append(buffer); | 
 | 4137 |     RecordTrack::appendDumpHeader(result); | 
 | 4138 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 4139 |         sp<RecordTrack> track = mTracks[i]; | 
 | 4140 |         if (track != 0) { | 
 | 4141 |             track->dump(buffer, SIZE); | 
 | 4142 |             result.append(buffer); | 
 | 4143 |         } | 
 | 4144 |     } | 
 | 4145 |  | 
 | 4146 |     if (mActiveTrack != 0) { | 
 | 4147 |         snprintf(buffer, SIZE, "\nInput thread %p active tracks\n", this); | 
 | 4148 |         result.append(buffer); | 
 | 4149 |         RecordTrack::appendDumpHeader(result); | 
 | 4150 |         mActiveTrack->dump(buffer, SIZE); | 
 | 4151 |         result.append(buffer); | 
 | 4152 |  | 
 | 4153 |     } | 
 | 4154 |     write(fd, result.string(), result.size()); | 
 | 4155 | } | 
 | 4156 |  | 
 | 4157 | // AudioBufferProvider interface | 
 | 4158 | status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts) | 
 | 4159 | { | 
 | 4160 |     size_t framesReq = buffer->frameCount; | 
 | 4161 |     size_t framesReady = mFrameCount - mRsmpInIndex; | 
 | 4162 |     int channelCount; | 
 | 4163 |  | 
 | 4164 |     if (framesReady == 0) { | 
 | 4165 |         mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes); | 
 | 4166 |         if (mBytesRead <= 0) { | 
 | 4167 |             if ((mBytesRead < 0) && (mActiveTrack->mState == TrackBase::ACTIVE)) { | 
 | 4168 |                 ALOGE("RecordThread::getNextBuffer() Error reading audio input"); | 
 | 4169 |                 // Force input into standby so that it tries to | 
 | 4170 |                 // recover at next read attempt | 
 | 4171 |                 inputStandBy(); | 
 | 4172 |                 usleep(kRecordThreadSleepUs); | 
 | 4173 |             } | 
 | 4174 |             buffer->raw = NULL; | 
 | 4175 |             buffer->frameCount = 0; | 
 | 4176 |             return NOT_ENOUGH_DATA; | 
 | 4177 |         } | 
 | 4178 |         mRsmpInIndex = 0; | 
 | 4179 |         framesReady = mFrameCount; | 
 | 4180 |     } | 
 | 4181 |  | 
 | 4182 |     if (framesReq > framesReady) { | 
 | 4183 |         framesReq = framesReady; | 
 | 4184 |     } | 
 | 4185 |  | 
 | 4186 |     if (mChannelCount == 1 && mReqChannelCount == 2) { | 
 | 4187 |         channelCount = 1; | 
 | 4188 |     } else { | 
 | 4189 |         channelCount = 2; | 
 | 4190 |     } | 
 | 4191 |     buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount; | 
 | 4192 |     buffer->frameCount = framesReq; | 
 | 4193 |     return NO_ERROR; | 
 | 4194 | } | 
 | 4195 |  | 
 | 4196 | // AudioBufferProvider interface | 
 | 4197 | void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer) | 
 | 4198 | { | 
 | 4199 |     mRsmpInIndex += buffer->frameCount; | 
 | 4200 |     buffer->frameCount = 0; | 
 | 4201 | } | 
 | 4202 |  | 
 | 4203 | bool AudioFlinger::RecordThread::checkForNewParameters_l() | 
 | 4204 | { | 
 | 4205 |     bool reconfig = false; | 
 | 4206 |  | 
 | 4207 |     while (!mNewParameters.isEmpty()) { | 
 | 4208 |         status_t status = NO_ERROR; | 
 | 4209 |         String8 keyValuePair = mNewParameters[0]; | 
 | 4210 |         AudioParameter param = AudioParameter(keyValuePair); | 
 | 4211 |         int value; | 
 | 4212 |         audio_format_t reqFormat = mFormat; | 
 | 4213 |         uint32_t reqSamplingRate = mReqSampleRate; | 
 | 4214 |         uint32_t reqChannelCount = mReqChannelCount; | 
 | 4215 |  | 
 | 4216 |         if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { | 
 | 4217 |             reqSamplingRate = value; | 
 | 4218 |             reconfig = true; | 
 | 4219 |         } | 
 | 4220 |         if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { | 
 | 4221 |             reqFormat = (audio_format_t) value; | 
 | 4222 |             reconfig = true; | 
 | 4223 |         } | 
 | 4224 |         if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { | 
 | 4225 |             reqChannelCount = popcount(value); | 
 | 4226 |             reconfig = true; | 
 | 4227 |         } | 
 | 4228 |         if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { | 
 | 4229 |             // do not accept frame count changes if tracks are open as the track buffer | 
 | 4230 |             // size depends on frame count and correct behavior would not be guaranteed | 
 | 4231 |             // if frame count is changed after track creation | 
 | 4232 |             if (mActiveTrack != 0) { | 
 | 4233 |                 status = INVALID_OPERATION; | 
 | 4234 |             } else { | 
 | 4235 |                 reconfig = true; | 
 | 4236 |             } | 
 | 4237 |         } | 
 | 4238 |         if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { | 
 | 4239 |             // forward device change to effects that have requested to be | 
 | 4240 |             // aware of attached audio device. | 
 | 4241 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4242 |                 mEffectChains[i]->setDevice_l(value); | 
 | 4243 |             } | 
 | 4244 |  | 
 | 4245 |             // store input device and output device but do not forward output device to audio HAL. | 
 | 4246 |             // Note that status is ignored by the caller for output device | 
 | 4247 |             // (see AudioFlinger::setParameters() | 
 | 4248 |             if (audio_is_output_devices(value)) { | 
 | 4249 |                 mOutDevice = value; | 
 | 4250 |                 status = BAD_VALUE; | 
 | 4251 |             } else { | 
 | 4252 |                 mInDevice = value; | 
 | 4253 |                 // disable AEC and NS if the device is a BT SCO headset supporting those | 
 | 4254 |                 // pre processings | 
 | 4255 |                 if (mTracks.size() > 0) { | 
 | 4256 |                     bool suspend = audio_is_bluetooth_sco_device(mInDevice) && | 
 | 4257 |                                         mAudioFlinger->btNrecIsOff(); | 
 | 4258 |                     for (size_t i = 0; i < mTracks.size(); i++) { | 
 | 4259 |                         sp<RecordTrack> track = mTracks[i]; | 
 | 4260 |                         setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); | 
 | 4261 |                         setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); | 
 | 4262 |                     } | 
 | 4263 |                 } | 
 | 4264 |             } | 
 | 4265 |         } | 
 | 4266 |         if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && | 
 | 4267 |                 mAudioSource != (audio_source_t)value) { | 
 | 4268 |             // forward device change to effects that have requested to be | 
 | 4269 |             // aware of attached audio device. | 
 | 4270 |             for (size_t i = 0; i < mEffectChains.size(); i++) { | 
 | 4271 |                 mEffectChains[i]->setAudioSource_l((audio_source_t)value); | 
 | 4272 |             } | 
 | 4273 |             mAudioSource = (audio_source_t)value; | 
 | 4274 |         } | 
 | 4275 |         if (status == NO_ERROR) { | 
 | 4276 |             status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 4277 |                     keyValuePair.string()); | 
 | 4278 |             if (status == INVALID_OPERATION) { | 
 | 4279 |                 inputStandBy(); | 
 | 4280 |                 status = mInput->stream->common.set_parameters(&mInput->stream->common, | 
 | 4281 |                         keyValuePair.string()); | 
 | 4282 |             } | 
 | 4283 |             if (reconfig) { | 
 | 4284 |                 if (status == BAD_VALUE && | 
 | 4285 |                     reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && | 
 | 4286 |                     reqFormat == AUDIO_FORMAT_PCM_16_BIT && | 
| Glenn Kasten | c497431 | 2012-12-14 07:13:28 -0800 | [diff] [blame] | 4287 |                     (mInput->stream->common.get_sample_rate(&mInput->stream->common) | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4288 |                             <= (2 * reqSamplingRate)) && | 
 | 4289 |                     popcount(mInput->stream->common.get_channels(&mInput->stream->common)) | 
 | 4290 |                             <= FCC_2 && | 
 | 4291 |                     (reqChannelCount <= FCC_2)) { | 
 | 4292 |                     status = NO_ERROR; | 
 | 4293 |                 } | 
 | 4294 |                 if (status == NO_ERROR) { | 
 | 4295 |                     readInputParameters(); | 
 | 4296 |                     sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); | 
 | 4297 |                 } | 
 | 4298 |             } | 
 | 4299 |         } | 
 | 4300 |  | 
 | 4301 |         mNewParameters.removeAt(0); | 
 | 4302 |  | 
 | 4303 |         mParamStatus = status; | 
 | 4304 |         mParamCond.signal(); | 
 | 4305 |         // wait for condition with time out in case the thread calling ThreadBase::setParameters() | 
 | 4306 |         // already timed out waiting for the status and will never signal the condition. | 
 | 4307 |         mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs); | 
 | 4308 |     } | 
 | 4309 |     return reconfig; | 
 | 4310 | } | 
 | 4311 |  | 
 | 4312 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) | 
 | 4313 | { | 
 | 4314 |     char *s; | 
 | 4315 |     String8 out_s8 = String8(); | 
 | 4316 |  | 
 | 4317 |     Mutex::Autolock _l(mLock); | 
 | 4318 |     if (initCheck() != NO_ERROR) { | 
 | 4319 |         return out_s8; | 
 | 4320 |     } | 
 | 4321 |  | 
 | 4322 |     s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); | 
 | 4323 |     out_s8 = String8(s); | 
 | 4324 |     free(s); | 
 | 4325 |     return out_s8; | 
 | 4326 | } | 
 | 4327 |  | 
 | 4328 | void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) { | 
 | 4329 |     AudioSystem::OutputDescriptor desc; | 
 | 4330 |     void *param2 = NULL; | 
 | 4331 |  | 
 | 4332 |     switch (event) { | 
 | 4333 |     case AudioSystem::INPUT_OPENED: | 
 | 4334 |     case AudioSystem::INPUT_CONFIG_CHANGED: | 
 | 4335 |         desc.channels = mChannelMask; | 
 | 4336 |         desc.samplingRate = mSampleRate; | 
 | 4337 |         desc.format = mFormat; | 
 | 4338 |         desc.frameCount = mFrameCount; | 
 | 4339 |         desc.latency = 0; | 
 | 4340 |         param2 = &desc; | 
 | 4341 |         break; | 
 | 4342 |  | 
 | 4343 |     case AudioSystem::INPUT_CLOSED: | 
 | 4344 |     default: | 
 | 4345 |         break; | 
 | 4346 |     } | 
 | 4347 |     mAudioFlinger->audioConfigChanged_l(event, mId, param2); | 
 | 4348 | } | 
 | 4349 |  | 
 | 4350 | void AudioFlinger::RecordThread::readInputParameters() | 
 | 4351 | { | 
 | 4352 |     delete mRsmpInBuffer; | 
 | 4353 |     // mRsmpInBuffer is always assigned a new[] below | 
 | 4354 |     delete mRsmpOutBuffer; | 
 | 4355 |     mRsmpOutBuffer = NULL; | 
 | 4356 |     delete mResampler; | 
 | 4357 |     mResampler = NULL; | 
 | 4358 |  | 
 | 4359 |     mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); | 
 | 4360 |     mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); | 
 | 4361 |     mChannelCount = (uint16_t)popcount(mChannelMask); | 
 | 4362 |     mFormat = mInput->stream->common.get_format(&mInput->stream->common); | 
 | 4363 |     mFrameSize = audio_stream_frame_size(&mInput->stream->common); | 
 | 4364 |     mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common); | 
 | 4365 |     mFrameCount = mInputBytes / mFrameSize; | 
 | 4366 |     mNormalFrameCount = mFrameCount; // not used by record, but used by input effects | 
 | 4367 |     mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount]; | 
 | 4368 |  | 
 | 4369 |     if (mSampleRate != mReqSampleRate && mChannelCount <= FCC_2 && mReqChannelCount <= FCC_2) | 
 | 4370 |     { | 
 | 4371 |         int channelCount; | 
 | 4372 |         // optimization: if mono to mono, use the resampler in stereo to stereo mode to avoid | 
 | 4373 |         // stereo to mono post process as the resampler always outputs stereo. | 
 | 4374 |         if (mChannelCount == 1 && mReqChannelCount == 2) { | 
 | 4375 |             channelCount = 1; | 
 | 4376 |         } else { | 
 | 4377 |             channelCount = 2; | 
 | 4378 |         } | 
 | 4379 |         mResampler = AudioResampler::create(16, channelCount, mReqSampleRate); | 
 | 4380 |         mResampler->setSampleRate(mSampleRate); | 
 | 4381 |         mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN); | 
 | 4382 |         mRsmpOutBuffer = new int32_t[mFrameCount * 2]; | 
 | 4383 |  | 
 | 4384 |         // optmization: if mono to mono, alter input frame count as if we were inputing | 
 | 4385 |         // stereo samples | 
 | 4386 |         if (mChannelCount == 1 && mReqChannelCount == 1) { | 
 | 4387 |             mFrameCount >>= 1; | 
 | 4388 |         } | 
 | 4389 |  | 
 | 4390 |     } | 
 | 4391 |     mRsmpInIndex = mFrameCount; | 
 | 4392 | } | 
 | 4393 |  | 
 | 4394 | unsigned int AudioFlinger::RecordThread::getInputFramesLost() | 
 | 4395 | { | 
 | 4396 |     Mutex::Autolock _l(mLock); | 
 | 4397 |     if (initCheck() != NO_ERROR) { | 
 | 4398 |         return 0; | 
 | 4399 |     } | 
 | 4400 |  | 
 | 4401 |     return mInput->stream->get_input_frames_lost(mInput->stream); | 
 | 4402 | } | 
 | 4403 |  | 
 | 4404 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const | 
 | 4405 | { | 
 | 4406 |     Mutex::Autolock _l(mLock); | 
 | 4407 |     uint32_t result = 0; | 
 | 4408 |     if (getEffectChain_l(sessionId) != 0) { | 
 | 4409 |         result = EFFECT_SESSION; | 
 | 4410 |     } | 
 | 4411 |  | 
 | 4412 |     for (size_t i = 0; i < mTracks.size(); ++i) { | 
 | 4413 |         if (sessionId == mTracks[i]->sessionId()) { | 
 | 4414 |             result |= TRACK_SESSION; | 
 | 4415 |             break; | 
 | 4416 |         } | 
 | 4417 |     } | 
 | 4418 |  | 
 | 4419 |     return result; | 
 | 4420 | } | 
 | 4421 |  | 
 | 4422 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const | 
 | 4423 | { | 
 | 4424 |     KeyedVector<int, bool> ids; | 
 | 4425 |     Mutex::Autolock _l(mLock); | 
 | 4426 |     for (size_t j = 0; j < mTracks.size(); ++j) { | 
 | 4427 |         sp<RecordThread::RecordTrack> track = mTracks[j]; | 
 | 4428 |         int sessionId = track->sessionId(); | 
 | 4429 |         if (ids.indexOfKey(sessionId) < 0) { | 
 | 4430 |             ids.add(sessionId, true); | 
 | 4431 |         } | 
 | 4432 |     } | 
 | 4433 |     return ids; | 
 | 4434 | } | 
 | 4435 |  | 
 | 4436 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() | 
 | 4437 | { | 
 | 4438 |     Mutex::Autolock _l(mLock); | 
 | 4439 |     AudioStreamIn *input = mInput; | 
 | 4440 |     mInput = NULL; | 
 | 4441 |     return input; | 
 | 4442 | } | 
 | 4443 |  | 
 | 4444 | // this method must always be called either with ThreadBase mLock held or inside the thread loop | 
 | 4445 | audio_stream_t* AudioFlinger::RecordThread::stream() const | 
 | 4446 | { | 
 | 4447 |     if (mInput == NULL) { | 
 | 4448 |         return NULL; | 
 | 4449 |     } | 
 | 4450 |     return &mInput->stream->common; | 
 | 4451 | } | 
 | 4452 |  | 
 | 4453 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) | 
 | 4454 | { | 
 | 4455 |     // only one chain per input thread | 
 | 4456 |     if (mEffectChains.size() != 0) { | 
 | 4457 |         return INVALID_OPERATION; | 
 | 4458 |     } | 
 | 4459 |     ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); | 
 | 4460 |  | 
 | 4461 |     chain->setInBuffer(NULL); | 
 | 4462 |     chain->setOutBuffer(NULL); | 
 | 4463 |  | 
 | 4464 |     checkSuspendOnAddEffectChain_l(chain); | 
 | 4465 |  | 
 | 4466 |     mEffectChains.add(chain); | 
 | 4467 |  | 
 | 4468 |     return NO_ERROR; | 
 | 4469 | } | 
 | 4470 |  | 
 | 4471 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) | 
 | 4472 | { | 
 | 4473 |     ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); | 
 | 4474 |     ALOGW_IF(mEffectChains.size() != 1, | 
 | 4475 |             "removeEffectChain_l() %p invalid chain size %d on thread %p", | 
 | 4476 |             chain.get(), mEffectChains.size(), this); | 
 | 4477 |     if (mEffectChains.size() == 1) { | 
 | 4478 |         mEffectChains.removeAt(0); | 
 | 4479 |     } | 
 | 4480 |     return 0; | 
 | 4481 | } | 
 | 4482 |  | 
 | 4483 | }; // namespace android |