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