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> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 29 | #include <media/AudioResamplerPublic.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 30 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 31 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 32 | |
| 33 | #include <private/media/AudioTrackShared.h> |
| 34 | #include <hardware/audio.h> |
| 35 | #include <audio_effects/effect_ns.h> |
| 36 | #include <audio_effects/effect_aec.h> |
| 37 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 38 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 39 | #include <audio_utils/minifloat.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | |
| 41 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 42 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 43 | #include <media/nbaio/AudioStreamOutSink.h> |
| 44 | #include <media/nbaio/MonoPipe.h> |
| 45 | #include <media/nbaio/MonoPipeReader.h> |
| 46 | #include <media/nbaio/Pipe.h> |
| 47 | #include <media/nbaio/PipeReader.h> |
| 48 | #include <media/nbaio/SourceAudioBufferProvider.h> |
| 49 | |
| 50 | #include <powermanager/PowerManager.h> |
| 51 | |
| 52 | #include <common_time/cc_helper.h> |
| 53 | #include <common_time/local_clock.h> |
| 54 | |
| 55 | #include "AudioFlinger.h" |
| 56 | #include "AudioMixer.h" |
| 57 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 58 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include "ServiceUtilities.h" |
| 60 | #include "SchedulingPolicyService.h" |
| 61 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #ifdef ADD_BATTERY_DATA |
| 63 | #include <media/IMediaPlayerService.h> |
| 64 | #include <media/IMediaDeathNotifier.h> |
| 65 | #endif |
| 66 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 67 | #ifdef DEBUG_CPU_USAGE |
| 68 | #include <cpustats/CentralTendencyStatistics.h> |
| 69 | #include <cpustats/ThreadCpuUsage.h> |
| 70 | #endif |
| 71 | |
| 72 | // ---------------------------------------------------------------------------- |
| 73 | |
| 74 | // Note: the following macro is used for extremely verbose logging message. In |
| 75 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 76 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 77 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 78 | // turned on. Do not uncomment the #def below unless you really know what you |
| 79 | // are doing and want to see all of the extremely verbose messages. |
| 80 | //#define VERY_VERY_VERBOSE_LOGGING |
| 81 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 82 | #define ALOGVV ALOGV |
| 83 | #else |
| 84 | #define ALOGVV(a...) do { } while(0) |
| 85 | #endif |
| 86 | |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 87 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 88 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 89 | namespace android { |
| 90 | |
| 91 | // retry counts for buffer fill timeout |
| 92 | // 50 * ~20msecs = 1 second |
| 93 | static const int8_t kMaxTrackRetries = 50; |
| 94 | static const int8_t kMaxTrackStartupRetries = 50; |
| 95 | // allow less retry attempts on direct output thread. |
| 96 | // direct outputs can be a scarce resource in audio hardware and should |
| 97 | // be released as quickly as possible. |
| 98 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 99 | |
| 100 | // don't warn about blocked writes or record buffer overflows more often than this |
| 101 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 102 | |
| 103 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 104 | static const int kRecordThreadSleepUs = 5000; |
| 105 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 106 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 107 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 108 | |
| 109 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 110 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 111 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 112 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 113 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 114 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
| 115 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 116 | // maximum normal sink buffer size |
| 117 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 118 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 119 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 120 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 121 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 122 | // Whether to use fast mixer |
| 123 | static const enum { |
| 124 | FastMixer_Never, // never initialize or use: for debugging only |
| 125 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 126 | // normal mixer multiplier is 1 |
| 127 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 128 | // multiplier is calculated based on min & max normal mixer buffer size |
| 129 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 130 | // multiplier is calculated based on min & max normal mixer buffer size |
| 131 | // FIXME for FastMixer_Dynamic: |
| 132 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 133 | // For example, one HAL implementation returns an error from a large write, |
| 134 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 135 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 136 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 137 | } kUseFastMixer = FastMixer_Static; |
| 138 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 139 | // Whether to use fast capture |
| 140 | static const enum { |
| 141 | FastCapture_Never, // never initialize or use: for debugging only |
| 142 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 143 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 144 | } kUseFastCapture = FastCapture_Static; |
| 145 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 146 | // Priorities for requestPriority |
| 147 | static const int kPriorityAudioApp = 2; |
| 148 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 149 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 150 | |
| 151 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 152 | // 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] | 153 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 154 | // So for now we just assume that client is double-buffered for fast tracks. |
| 155 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 156 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 157 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 158 | |
| 159 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 160 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 161 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 162 | // The minimum and maximum allowed values |
| 163 | static const int kFastTrackMultiplierMin = 1; |
| 164 | static const int kFastTrackMultiplierMax = 2; |
| 165 | |
| 166 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 167 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 168 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 169 | // See Thread::readOnlyHeap(). |
| 170 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 171 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 172 | // and that all "fast" AudioRecord clients read from. In either case, the size can be small. |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 173 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 174 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 175 | // ---------------------------------------------------------------------------- |
| 176 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 177 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 178 | |
| 179 | static void sFastTrackMultiplierInit() |
| 180 | { |
| 181 | char value[PROPERTY_VALUE_MAX]; |
| 182 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 183 | char *endptr; |
| 184 | unsigned long ul = strtoul(value, &endptr, 0); |
| 185 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 186 | sFastTrackMultiplier = (int) ul; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 193 | #ifdef ADD_BATTERY_DATA |
| 194 | // To collect the amplifier usage |
| 195 | static void addBatteryData(uint32_t params) { |
| 196 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 197 | if (service == NULL) { |
| 198 | // it already logged |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | service->addBatteryData(params); |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | |
| 207 | // ---------------------------------------------------------------------------- |
| 208 | // CPU Stats |
| 209 | // ---------------------------------------------------------------------------- |
| 210 | |
| 211 | class CpuStats { |
| 212 | public: |
| 213 | CpuStats(); |
| 214 | void sample(const String8 &title); |
| 215 | #ifdef DEBUG_CPU_USAGE |
| 216 | private: |
| 217 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 218 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 219 | |
| 220 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 221 | |
| 222 | int mCpuNum; // thread's current CPU number |
| 223 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 224 | #endif |
| 225 | }; |
| 226 | |
| 227 | CpuStats::CpuStats() |
| 228 | #ifdef DEBUG_CPU_USAGE |
| 229 | : mCpuNum(-1), mCpukHz(-1) |
| 230 | #endif |
| 231 | { |
| 232 | } |
| 233 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 234 | void CpuStats::sample(const String8 &title |
| 235 | #ifndef DEBUG_CPU_USAGE |
| 236 | __unused |
| 237 | #endif |
| 238 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 239 | #ifdef DEBUG_CPU_USAGE |
| 240 | // get current thread's delta CPU time in wall clock ns |
| 241 | double wcNs; |
| 242 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 243 | |
| 244 | // record sample for wall clock statistics |
| 245 | if (valid) { |
| 246 | mWcStats.sample(wcNs); |
| 247 | } |
| 248 | |
| 249 | // get the current CPU number |
| 250 | int cpuNum = sched_getcpu(); |
| 251 | |
| 252 | // get the current CPU frequency in kHz |
| 253 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 254 | |
| 255 | // check if either CPU number or frequency changed |
| 256 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 257 | mCpuNum = cpuNum; |
| 258 | mCpukHz = cpukHz; |
| 259 | // ignore sample for purposes of cycles |
| 260 | valid = false; |
| 261 | } |
| 262 | |
| 263 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 264 | if (valid && mCpukHz > 0) { |
| 265 | double cycles = wcNs * cpukHz * 0.000001; |
| 266 | mHzStats.sample(cycles); |
| 267 | } |
| 268 | |
| 269 | unsigned n = mWcStats.n(); |
| 270 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 271 | if ((n & 127) == 1) { |
| 272 | long long elapsed = mCpuUsage.elapsed(); |
| 273 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 274 | double perLoop = elapsed / (double) n; |
| 275 | double perLoop100 = perLoop * 0.01; |
| 276 | double perLoop1k = perLoop * 0.001; |
| 277 | double mean = mWcStats.mean(); |
| 278 | double stddev = mWcStats.stddev(); |
| 279 | double minimum = mWcStats.minimum(); |
| 280 | double maximum = mWcStats.maximum(); |
| 281 | double meanCycles = mHzStats.mean(); |
| 282 | double stddevCycles = mHzStats.stddev(); |
| 283 | double minCycles = mHzStats.minimum(); |
| 284 | double maxCycles = mHzStats.maximum(); |
| 285 | mCpuUsage.resetElapsed(); |
| 286 | mWcStats.reset(); |
| 287 | mHzStats.reset(); |
| 288 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 289 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 290 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 291 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 292 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 293 | title.string(), |
| 294 | elapsed * .000000001, n, perLoop * .000001, |
| 295 | mean * .001, |
| 296 | stddev * .001, |
| 297 | minimum * .001, |
| 298 | maximum * .001, |
| 299 | mean / perLoop100, |
| 300 | stddev / perLoop100, |
| 301 | minimum / perLoop100, |
| 302 | maximum / perLoop100, |
| 303 | meanCycles / perLoop1k, |
| 304 | stddevCycles / perLoop1k, |
| 305 | minCycles / perLoop1k, |
| 306 | maxCycles / perLoop1k); |
| 307 | |
| 308 | } |
| 309 | } |
| 310 | #endif |
| 311 | }; |
| 312 | |
| 313 | // ---------------------------------------------------------------------------- |
| 314 | // ThreadBase |
| 315 | // ---------------------------------------------------------------------------- |
| 316 | |
| 317 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 318 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type) |
| 319 | : Thread(false /*canCallJava*/), |
| 320 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 321 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 322 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 323 | // are set by PlaybackThread::readOutputParameters_l() or |
| 324 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 325 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 326 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
| 327 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
| 328 | // mName will be set by concrete (non-virtual) subclass |
| 329 | mDeathRecipient(new PMDeathRecipient(this)) |
| 330 | { |
| 331 | } |
| 332 | |
| 333 | AudioFlinger::ThreadBase::~ThreadBase() |
| 334 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 335 | // mConfigEvents should be empty, but just in case it isn't, free the memory it owns |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 336 | mConfigEvents.clear(); |
| 337 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 338 | // do not lock the mutex in destructor |
| 339 | releaseWakeLock_l(); |
| 340 | if (mPowerManager != 0) { |
| 341 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 342 | binder->unlinkToDeath(mDeathRecipient); |
| 343 | } |
| 344 | } |
| 345 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 346 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 347 | { |
| 348 | status_t status = initCheck(); |
| 349 | if (status == NO_ERROR) { |
| 350 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 351 | } else { |
| 352 | ALOGE("No working audio driver found."); |
| 353 | } |
| 354 | return status; |
| 355 | } |
| 356 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 357 | void AudioFlinger::ThreadBase::exit() |
| 358 | { |
| 359 | ALOGV("ThreadBase::exit"); |
| 360 | // do any cleanup required for exit to succeed |
| 361 | preExit(); |
| 362 | { |
| 363 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 364 | // if (!exitPending()) { |
| 365 | // // context switch from here to exit() |
| 366 | // // exit() calls requestExit(), what exitPending() observes |
| 367 | // // exit() calls signal(), which is dropped since no waiters |
| 368 | // // context switch back from exit() to here |
| 369 | // mWaitWorkCV.wait(...); |
| 370 | // // now thread is hung |
| 371 | // } |
| 372 | AutoMutex lock(mLock); |
| 373 | requestExit(); |
| 374 | mWaitWorkCV.broadcast(); |
| 375 | } |
| 376 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 377 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 378 | requestExitAndWait(); |
| 379 | } |
| 380 | |
| 381 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 382 | { |
| 383 | status_t status; |
| 384 | |
| 385 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 386 | Mutex::Autolock _l(mLock); |
| 387 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 388 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 389 | } |
| 390 | |
| 391 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 392 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 393 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 394 | { |
| 395 | status_t status = NO_ERROR; |
| 396 | |
| 397 | mConfigEvents.add(event); |
| 398 | ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 399 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 400 | mLock.unlock(); |
| 401 | { |
| 402 | Mutex::Autolock _l(event->mLock); |
| 403 | while (event->mWaitStatus) { |
| 404 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 405 | event->mStatus = TIMED_OUT; |
| 406 | event->mWaitStatus = false; |
| 407 | } |
| 408 | } |
| 409 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 410 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 411 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 412 | return status; |
| 413 | } |
| 414 | |
| 415 | void AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) |
| 416 | { |
| 417 | Mutex::Autolock _l(mLock); |
| 418 | sendIoConfigEvent_l(event, param); |
| 419 | } |
| 420 | |
| 421 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
| 422 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) |
| 423 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 424 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param); |
| 425 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 429 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 430 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 431 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 432 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 435 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 436 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 437 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 438 | sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); |
| 439 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 442 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 443 | const struct audio_patch *patch, |
| 444 | audio_patch_handle_t *handle) |
| 445 | { |
| 446 | Mutex::Autolock _l(mLock); |
| 447 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 448 | status_t status = sendConfigEvent_l(configEvent); |
| 449 | if (status == NO_ERROR) { |
| 450 | CreateAudioPatchConfigEventData *data = |
| 451 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 452 | *handle = data->mHandle; |
| 453 | } |
| 454 | return status; |
| 455 | } |
| 456 | |
| 457 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 458 | const audio_patch_handle_t handle) |
| 459 | { |
| 460 | Mutex::Autolock _l(mLock); |
| 461 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 462 | return sendConfigEvent_l(configEvent); |
| 463 | } |
| 464 | |
| 465 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 466 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 467 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 468 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 469 | bool configChanged = false; |
| 470 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 471 | while (!mConfigEvents.isEmpty()) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 472 | ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); |
| 473 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 474 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 475 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 476 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 477 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 478 | // FIXME Need to understand why this has to be done asynchronously |
| 479 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 480 | true /*asynchronous*/); |
| 481 | if (err != 0) { |
| 482 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 483 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 484 | } |
| 485 | } break; |
| 486 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 487 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 488 | audioConfigChanged(data->mEvent, data->mParam); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 489 | } break; |
| 490 | case CFG_EVENT_SET_PARAMETER: { |
| 491 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 492 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 493 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 494 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 495 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 496 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 497 | CreateAudioPatchConfigEventData *data = |
| 498 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 499 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 500 | } break; |
| 501 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 502 | ReleaseAudioPatchConfigEventData *data = |
| 503 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 504 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 505 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 506 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 507 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 508 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 509 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 510 | { |
| 511 | Mutex::Autolock _l(event->mLock); |
| 512 | if (event->mWaitStatus) { |
| 513 | event->mWaitStatus = false; |
| 514 | event->mCond.signal(); |
| 515 | } |
| 516 | } |
| 517 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 518 | } |
| 519 | |
| 520 | if (configChanged) { |
| 521 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 525 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 526 | String8 s; |
| 527 | if (output) { |
| 528 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 529 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 530 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 531 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 532 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 533 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 534 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 535 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 536 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 537 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 538 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 539 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 540 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 541 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 542 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 543 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 544 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 545 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 546 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 547 | } else { |
| 548 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 549 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 550 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 551 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 552 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 553 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 554 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 555 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 556 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 557 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 558 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 559 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 560 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 561 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 562 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 563 | } |
| 564 | int len = s.length(); |
| 565 | if (s.length() > 2) { |
| 566 | char *str = s.lockBuffer(len); |
| 567 | s.unlockBuffer(len - 2); |
| 568 | } |
| 569 | return s; |
| 570 | } |
| 571 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 572 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 573 | { |
| 574 | const size_t SIZE = 256; |
| 575 | char buffer[SIZE]; |
| 576 | String8 result; |
| 577 | |
| 578 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 579 | if (!locked) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 580 | dprintf(fd, "thread %p maybe dead locked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 583 | dprintf(fd, " I/O handle: %d\n", mId); |
| 584 | dprintf(fd, " TID: %d\n", getTid()); |
| 585 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
| 586 | dprintf(fd, " Sample rate: %u\n", mSampleRate); |
| 587 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
| 588 | dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); |
| 589 | dprintf(fd, " Channel Count: %u\n", mChannelCount); |
| 590 | dprintf(fd, " Channel Mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 591 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 592 | dprintf(fd, " Format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 593 | dprintf(fd, " Frame size: %zu\n", mFrameSize); |
| 594 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 595 | size_t numConfig = mConfigEvents.size(); |
| 596 | if (numConfig) { |
| 597 | for (size_t i = 0; i < numConfig; i++) { |
| 598 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 599 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 600 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 601 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 602 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 603 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 604 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 605 | |
| 606 | if (locked) { |
| 607 | mLock.unlock(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 612 | { |
| 613 | const size_t SIZE = 256; |
| 614 | char buffer[SIZE]; |
| 615 | String8 result; |
| 616 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 617 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 618 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 619 | write(fd, buffer, strlen(buffer)); |
| 620 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 621 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 622 | sp<EffectChain> chain = mEffectChains[i]; |
| 623 | if (chain != 0) { |
| 624 | chain->dump(fd, args); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 629 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 630 | { |
| 631 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 632 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 635 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 636 | { |
| 637 | switch (mType) { |
| 638 | case MIXER: |
| 639 | return String16("AudioMix"); |
| 640 | case DIRECT: |
| 641 | return String16("AudioDirectOut"); |
| 642 | case DUPLICATING: |
| 643 | return String16("AudioDup"); |
| 644 | case RECORD: |
| 645 | return String16("AudioIn"); |
| 646 | case OFFLOAD: |
| 647 | return String16("AudioOffload"); |
| 648 | default: |
| 649 | ALOG_ASSERT(false); |
| 650 | return String16("AudioUnknown"); |
| 651 | } |
| 652 | } |
| 653 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 654 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 656 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 657 | if (mPowerManager != 0) { |
| 658 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 659 | status_t status; |
| 660 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 661 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 662 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 663 | getWakeLockTag(), |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 664 | String16("media"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 665 | uid, |
| 666 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 667 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 668 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 669 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 670 | getWakeLockTag(), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 671 | String16("media"), |
| 672 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 673 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 674 | if (status == NO_ERROR) { |
| 675 | mWakeLockToken = binder; |
| 676 | } |
| 677 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 682 | { |
| 683 | Mutex::Autolock _l(mLock); |
| 684 | releaseWakeLock_l(); |
| 685 | } |
| 686 | |
| 687 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 688 | { |
| 689 | if (mWakeLockToken != 0) { |
| 690 | ALOGV("releaseWakeLock_l() %s", mName); |
| 691 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 692 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 693 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 694 | } |
| 695 | mWakeLockToken.clear(); |
| 696 | } |
| 697 | } |
| 698 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 699 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 700 | Mutex::Autolock _l(mLock); |
| 701 | updateWakeLockUids_l(uids); |
| 702 | } |
| 703 | |
| 704 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
| 705 | |
| 706 | if (mPowerManager == 0) { |
| 707 | // use checkService() to avoid blocking if power service is not up yet |
| 708 | sp<IBinder> binder = |
| 709 | defaultServiceManager()->checkService(String16("power")); |
| 710 | if (binder == 0) { |
| 711 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
| 712 | } else { |
| 713 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 714 | binder->linkToDeath(mDeathRecipient); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
| 720 | |
| 721 | getPowerManager_l(); |
| 722 | if (mWakeLockToken == NULL) { |
| 723 | ALOGE("no wake lock to update!"); |
| 724 | return; |
| 725 | } |
| 726 | if (mPowerManager != 0) { |
| 727 | sp<IBinder> binder = new BBinder(); |
| 728 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 729 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 730 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 731 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
| 732 | } |
| 733 | } |
| 734 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 735 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 736 | { |
| 737 | Mutex::Autolock _l(mLock); |
| 738 | releaseWakeLock_l(); |
| 739 | mPowerManager.clear(); |
| 740 | } |
| 741 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 742 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | { |
| 744 | sp<ThreadBase> thread = mThread.promote(); |
| 745 | if (thread != 0) { |
| 746 | thread->clearPowerManager(); |
| 747 | } |
| 748 | ALOGW("power manager service died !!!"); |
| 749 | } |
| 750 | |
| 751 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 752 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 753 | { |
| 754 | Mutex::Autolock _l(mLock); |
| 755 | setEffectSuspended_l(type, suspend, sessionId); |
| 756 | } |
| 757 | |
| 758 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 759 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 760 | { |
| 761 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 762 | if (chain != 0) { |
| 763 | if (type != NULL) { |
| 764 | chain->setEffectSuspended_l(type, suspend); |
| 765 | } else { |
| 766 | chain->setEffectSuspendedAll_l(suspend); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 771 | } |
| 772 | |
| 773 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 774 | { |
| 775 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 776 | if (index < 0) { |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 781 | mSuspendedSessions.valueAt(index); |
| 782 | |
| 783 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 784 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 785 | for (int j = 0; j < desc->mRefCount; j++) { |
| 786 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 787 | chain->setEffectSuspendedAll_l(true); |
| 788 | } else { |
| 789 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 790 | desc->mType.timeLow); |
| 791 | chain->setEffectSuspended_l(&desc->mType, true); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 798 | bool suspend, |
| 799 | int sessionId) |
| 800 | { |
| 801 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 802 | |
| 803 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 804 | |
| 805 | if (suspend) { |
| 806 | if (index >= 0) { |
| 807 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 808 | } else { |
| 809 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 810 | } |
| 811 | } else { |
| 812 | if (index < 0) { |
| 813 | return; |
| 814 | } |
| 815 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 816 | } |
| 817 | |
| 818 | |
| 819 | int key = EffectChain::kKeyForSuspendAll; |
| 820 | if (type != NULL) { |
| 821 | key = type->timeLow; |
| 822 | } |
| 823 | index = sessionEffects.indexOfKey(key); |
| 824 | |
| 825 | sp<SuspendedSessionDesc> desc; |
| 826 | if (suspend) { |
| 827 | if (index >= 0) { |
| 828 | desc = sessionEffects.valueAt(index); |
| 829 | } else { |
| 830 | desc = new SuspendedSessionDesc(); |
| 831 | if (type != NULL) { |
| 832 | desc->mType = *type; |
| 833 | } |
| 834 | sessionEffects.add(key, desc); |
| 835 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 836 | } |
| 837 | desc->mRefCount++; |
| 838 | } else { |
| 839 | if (index < 0) { |
| 840 | return; |
| 841 | } |
| 842 | desc = sessionEffects.valueAt(index); |
| 843 | if (--desc->mRefCount == 0) { |
| 844 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 845 | sessionEffects.removeItemsAt(index); |
| 846 | if (sessionEffects.isEmpty()) { |
| 847 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 848 | sessionId); |
| 849 | mSuspendedSessions.removeItem(sessionId); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | if (!sessionEffects.isEmpty()) { |
| 854 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 859 | bool enabled, |
| 860 | int sessionId) |
| 861 | { |
| 862 | Mutex::Autolock _l(mLock); |
| 863 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 864 | } |
| 865 | |
| 866 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 867 | bool enabled, |
| 868 | int sessionId) |
| 869 | { |
| 870 | if (mType != RECORD) { |
| 871 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 872 | // another session. This gives the priority to well behaved effect control panels |
| 873 | // and applications not using global effects. |
| 874 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 875 | // global effects |
| 876 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 877 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 882 | if (chain != 0) { |
| 883 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 888 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 889 | const sp<AudioFlinger::Client>& client, |
| 890 | const sp<IEffectClient>& effectClient, |
| 891 | int32_t priority, |
| 892 | int sessionId, |
| 893 | effect_descriptor_t *desc, |
| 894 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 895 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 896 | { |
| 897 | sp<EffectModule> effect; |
| 898 | sp<EffectHandle> handle; |
| 899 | status_t lStatus; |
| 900 | sp<EffectChain> chain; |
| 901 | bool chainCreated = false; |
| 902 | bool effectCreated = false; |
| 903 | bool effectRegistered = false; |
| 904 | |
| 905 | lStatus = initCheck(); |
| 906 | if (lStatus != NO_ERROR) { |
| 907 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 908 | goto Exit; |
| 909 | } |
| 910 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 911 | // Reject any effect on Direct output threads for now, since the format of |
| 912 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 913 | if (mType == DIRECT) { |
| 914 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", |
| 915 | desc->name, mName); |
| 916 | lStatus = BAD_VALUE; |
| 917 | goto Exit; |
| 918 | } |
| 919 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 920 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 921 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 922 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 923 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 924 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 925 | lStatus = BAD_VALUE; |
| 926 | goto Exit; |
| 927 | } |
| 928 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 929 | // Allow global effects only on offloaded and mixer threads |
| 930 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 931 | switch (mType) { |
| 932 | case MIXER: |
| 933 | case OFFLOAD: |
| 934 | break; |
| 935 | case DIRECT: |
| 936 | case DUPLICATING: |
| 937 | case RECORD: |
| 938 | default: |
| 939 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", desc->name, mName); |
| 940 | lStatus = BAD_VALUE; |
| 941 | goto Exit; |
| 942 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 943 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 944 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 945 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 946 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 947 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 948 | desc->name, desc->flags, mType); |
| 949 | lStatus = BAD_VALUE; |
| 950 | goto Exit; |
| 951 | } |
| 952 | |
| 953 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 954 | |
| 955 | { // scope for mLock |
| 956 | Mutex::Autolock _l(mLock); |
| 957 | |
| 958 | // check for existing effect chain with the requested audio session |
| 959 | chain = getEffectChain_l(sessionId); |
| 960 | if (chain == 0) { |
| 961 | // create a new chain for this session |
| 962 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 963 | chain = new EffectChain(this, sessionId); |
| 964 | addEffectChain_l(chain); |
| 965 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 966 | chainCreated = true; |
| 967 | } else { |
| 968 | effect = chain->getEffectFromDesc_l(desc); |
| 969 | } |
| 970 | |
| 971 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 972 | |
| 973 | if (effect == 0) { |
| 974 | int id = mAudioFlinger->nextUniqueId(); |
| 975 | // Check CPU and memory usage |
| 976 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 977 | if (lStatus != NO_ERROR) { |
| 978 | goto Exit; |
| 979 | } |
| 980 | effectRegistered = true; |
| 981 | // create a new effect module if none present in the chain |
| 982 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 983 | lStatus = effect->status(); |
| 984 | if (lStatus != NO_ERROR) { |
| 985 | goto Exit; |
| 986 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 987 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 988 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 989 | lStatus = chain->addEffect_l(effect); |
| 990 | if (lStatus != NO_ERROR) { |
| 991 | goto Exit; |
| 992 | } |
| 993 | effectCreated = true; |
| 994 | |
| 995 | effect->setDevice(mOutDevice); |
| 996 | effect->setDevice(mInDevice); |
| 997 | effect->setMode(mAudioFlinger->getMode()); |
| 998 | effect->setAudioSource(mAudioSource); |
| 999 | } |
| 1000 | // create effect handle and connect it to effect module |
| 1001 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1002 | lStatus = handle->initCheck(); |
| 1003 | if (lStatus == OK) { |
| 1004 | lStatus = effect->addHandle(handle.get()); |
| 1005 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1006 | if (enabled != NULL) { |
| 1007 | *enabled = (int)effect->isEnabled(); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | Exit: |
| 1012 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1013 | Mutex::Autolock _l(mLock); |
| 1014 | if (effectCreated) { |
| 1015 | chain->removeEffect_l(effect); |
| 1016 | } |
| 1017 | if (effectRegistered) { |
| 1018 | AudioSystem::unregisterEffect(effect->id()); |
| 1019 | } |
| 1020 | if (chainCreated) { |
| 1021 | removeEffectChain_l(chain); |
| 1022 | } |
| 1023 | handle.clear(); |
| 1024 | } |
| 1025 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1026 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1027 | return handle; |
| 1028 | } |
| 1029 | |
| 1030 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) |
| 1031 | { |
| 1032 | Mutex::Autolock _l(mLock); |
| 1033 | return getEffect_l(sessionId, effectId); |
| 1034 | } |
| 1035 | |
| 1036 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 1037 | { |
| 1038 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1039 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1040 | } |
| 1041 | |
| 1042 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1043 | // PlaybackThread::mLock held |
| 1044 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1045 | { |
| 1046 | // check for existing effect chain with the requested audio session |
| 1047 | int sessionId = effect->sessionId(); |
| 1048 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1049 | bool chainCreated = false; |
| 1050 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1051 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1052 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1053 | this, effect->desc().name, effect->desc().flags); |
| 1054 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1055 | if (chain == 0) { |
| 1056 | // create a new chain for this session |
| 1057 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1058 | chain = new EffectChain(this, sessionId); |
| 1059 | addEffectChain_l(chain); |
| 1060 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1061 | chainCreated = true; |
| 1062 | } |
| 1063 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1064 | |
| 1065 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1066 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1067 | this, effect->desc().name, chain.get()); |
| 1068 | return BAD_VALUE; |
| 1069 | } |
| 1070 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1071 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1072 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1073 | status_t status = chain->addEffect_l(effect); |
| 1074 | if (status != NO_ERROR) { |
| 1075 | if (chainCreated) { |
| 1076 | removeEffectChain_l(chain); |
| 1077 | } |
| 1078 | return status; |
| 1079 | } |
| 1080 | |
| 1081 | effect->setDevice(mOutDevice); |
| 1082 | effect->setDevice(mInDevice); |
| 1083 | effect->setMode(mAudioFlinger->getMode()); |
| 1084 | effect->setAudioSource(mAudioSource); |
| 1085 | return NO_ERROR; |
| 1086 | } |
| 1087 | |
| 1088 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1089 | |
| 1090 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1091 | effect_descriptor_t desc = effect->desc(); |
| 1092 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1093 | detachAuxEffect_l(effect->id()); |
| 1094 | } |
| 1095 | |
| 1096 | sp<EffectChain> chain = effect->chain().promote(); |
| 1097 | if (chain != 0) { |
| 1098 | // remove effect chain if removing last effect |
| 1099 | if (chain->removeEffect_l(effect) == 0) { |
| 1100 | removeEffectChain_l(chain); |
| 1101 | } |
| 1102 | } else { |
| 1103 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1108 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1109 | { |
| 1110 | effectChains = mEffectChains; |
| 1111 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1112 | mEffectChains[i]->lock(); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1117 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1118 | { |
| 1119 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1120 | effectChains[i]->unlock(); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 1125 | { |
| 1126 | Mutex::Autolock _l(mLock); |
| 1127 | return getEffectChain_l(sessionId); |
| 1128 | } |
| 1129 | |
| 1130 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const |
| 1131 | { |
| 1132 | size_t size = mEffectChains.size(); |
| 1133 | for (size_t i = 0; i < size; i++) { |
| 1134 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1135 | return mEffectChains[i]; |
| 1136 | } |
| 1137 | } |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1142 | { |
| 1143 | Mutex::Autolock _l(mLock); |
| 1144 | size_t size = mEffectChains.size(); |
| 1145 | for (size_t i = 0; i < size; i++) { |
| 1146 | mEffectChains[i]->setMode_l(mode); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1150 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1151 | { |
| 1152 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1153 | config->ext.mix.handle = mId; |
| 1154 | config->sample_rate = mSampleRate; |
| 1155 | config->format = mFormat; |
| 1156 | config->channel_mask = mChannelMask; |
| 1157 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1158 | AUDIO_PORT_CONFIG_FORMAT; |
| 1159 | } |
| 1160 | |
| 1161 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1162 | // ---------------------------------------------------------------------------- |
| 1163 | // Playback |
| 1164 | // ---------------------------------------------------------------------------- |
| 1165 | |
| 1166 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1167 | AudioStreamOut* output, |
| 1168 | audio_io_handle_t id, |
| 1169 | audio_devices_t device, |
| 1170 | type_t type) |
| 1171 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1172 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1173 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1174 | mMixerBuffer(NULL), |
| 1175 | mMixerBufferSize(0), |
| 1176 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1177 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1178 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1179 | mEffectBuffer(NULL), |
| 1180 | mEffectBufferSize(0), |
| 1181 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1182 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1183 | mSuspended(0), mBytesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1184 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1185 | // mStreamTypes[] initialized in constructor body |
| 1186 | mOutput(output), |
| 1187 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1188 | mMixerStatus(MIXER_IDLE), |
| 1189 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
| 1190 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1191 | mBytesRemaining(0), |
| 1192 | mCurrentWriteLength(0), |
| 1193 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1194 | mWriteAckSequence(0), |
| 1195 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1196 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1197 | mScreenState(AudioFlinger::mScreenState), |
| 1198 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1199 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
| 1200 | // mLatchD, mLatchQ, |
| 1201 | mLatchDValid(false), mLatchQValid(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1202 | { |
| 1203 | snprintf(mName, kNameLength, "AudioOut_%X", id); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1204 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1205 | |
| 1206 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1207 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1208 | // parameter. |
| 1209 | // |
| 1210 | // If the HAL we are using has support for master volume or master mute, |
| 1211 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1212 | // and the mute set to false). |
| 1213 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1214 | mMasterMute = audioFlinger->masterMute_l(); |
| 1215 | if (mOutput && mOutput->audioHwDev) { |
| 1216 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1217 | mMasterVolume = 1.0; |
| 1218 | } |
| 1219 | |
| 1220 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1221 | mMasterMute = false; |
| 1222 | } |
| 1223 | } |
| 1224 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1225 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1226 | |
| 1227 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
| 1228 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1229 | for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1230 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1231 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1232 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1233 | } |
| 1234 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1235 | // because mAudioFlinger doesn't have one to copy from |
| 1236 | } |
| 1237 | |
| 1238 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1239 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1240 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1241 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1242 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1243 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1247 | { |
| 1248 | dumpInternals(fd, args); |
| 1249 | dumpTracks(fd, args); |
| 1250 | dumpEffectChains(fd, args); |
| 1251 | } |
| 1252 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1253 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1254 | { |
| 1255 | const size_t SIZE = 256; |
| 1256 | char buffer[SIZE]; |
| 1257 | String8 result; |
| 1258 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1259 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1260 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1261 | const stream_type_t *st = &mStreamTypes[i]; |
| 1262 | if (i > 0) { |
| 1263 | result.appendFormat(", "); |
| 1264 | } |
| 1265 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1266 | if (st->mute) { |
| 1267 | result.append("M"); |
| 1268 | } |
| 1269 | } |
| 1270 | result.append("\n"); |
| 1271 | write(fd, result.string(), result.length()); |
| 1272 | result.clear(); |
| 1273 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1274 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1275 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1276 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1277 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1278 | |
| 1279 | size_t numtracks = mTracks.size(); |
| 1280 | size_t numactive = mActiveTracks.size(); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1281 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1282 | size_t numactiveseen = 0; |
| 1283 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1284 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1285 | Track::appendDumpHeader(result); |
| 1286 | for (size_t i = 0; i < numtracks; ++i) { |
| 1287 | sp<Track> track = mTracks[i]; |
| 1288 | if (track != 0) { |
| 1289 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1290 | if (active) { |
| 1291 | numactiveseen++; |
| 1292 | } |
| 1293 | track->dump(buffer, SIZE, active); |
| 1294 | result.append(buffer); |
| 1295 | } |
| 1296 | } |
| 1297 | } else { |
| 1298 | result.append("\n"); |
| 1299 | } |
| 1300 | if (numactiveseen != numactive) { |
| 1301 | // some tracks in the active list were not in the tracks list |
| 1302 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1303 | " not in the track list\n"); |
| 1304 | result.append(buffer); |
| 1305 | Track::appendDumpHeader(result); |
| 1306 | for (size_t i = 0; i < numactive; ++i) { |
| 1307 | sp<Track> track = mActiveTracks[i].promote(); |
| 1308 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1309 | track->dump(buffer, SIZE, true); |
| 1310 | result.append(buffer); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1319 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1320 | dprintf(fd, "\nOutput thread %p:\n", this); |
| 1321 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
| 1322 | dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1323 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1324 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1325 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1326 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1327 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1328 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1329 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1330 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 | |
| 1332 | dumpBase(fd, args); |
| 1333 | } |
| 1334 | |
| 1335 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1336 | |
| 1337 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1338 | { |
| 1339 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 1340 | } |
| 1341 | |
| 1342 | // ThreadBase virtuals |
| 1343 | void AudioFlinger::PlaybackThread::preExit() |
| 1344 | { |
| 1345 | ALOGV(" preExit()"); |
| 1346 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1347 | // converted to use audio HAL extensions required to support tunneling |
| 1348 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1349 | } |
| 1350 | |
| 1351 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1352 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1353 | const sp<AudioFlinger::Client>& client, |
| 1354 | audio_stream_type_t streamType, |
| 1355 | uint32_t sampleRate, |
| 1356 | audio_format_t format, |
| 1357 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1358 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1359 | const sp<IMemory>& sharedBuffer, |
| 1360 | int sessionId, |
| 1361 | IAudioFlinger::track_flags_t *flags, |
| 1362 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1363 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1364 | status_t *status) |
| 1365 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1366 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1367 | sp<Track> track; |
| 1368 | status_t lStatus; |
| 1369 | |
| 1370 | bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1371 | |
| 1372 | // client expresses a preference for FAST, but we get the final say |
| 1373 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1374 | if ( |
| 1375 | // not timed |
| 1376 | (!isTimed) && |
| 1377 | // either of these use cases: |
| 1378 | ( |
| 1379 | // use case 1: shared buffer with any frame count |
| 1380 | ( |
| 1381 | (sharedBuffer != 0) |
| 1382 | ) || |
| 1383 | // use case 2: callback handler and frame count is default or at least as large as HAL |
| 1384 | ( |
| 1385 | (tid != -1) && |
| 1386 | ((frameCount == 0) || |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1387 | (frameCount >= mFrameCount)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1388 | ) |
| 1389 | ) && |
| 1390 | // PCM data |
| 1391 | audio_is_linear_pcm(format) && |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1392 | // identical channel mask to sink, or mono in and stereo sink |
| 1393 | (channelMask == mChannelMask || |
| 1394 | (channelMask == AUDIO_CHANNEL_OUT_MONO && |
| 1395 | mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1396 | // hardware sample rate |
| 1397 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1398 | // normal mixer has an associated fast mixer |
| 1399 | hasFastMixer() && |
| 1400 | // there are sufficient fast track slots available |
| 1401 | (mFastTrackAvailMask != 0) |
| 1402 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1403 | // FIXME add a permission test also? |
| 1404 | ) { |
| 1405 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1406 | if (frameCount == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1407 | // read the fast track multiplier property the first time it is needed |
| 1408 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1409 | if (ok != 0) { |
| 1410 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1411 | } |
| 1412 | frameCount = mFrameCount * sFastTrackMultiplier; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1413 | } |
| 1414 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1415 | frameCount, mFrameCount); |
| 1416 | } else { |
| 1417 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1418 | "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
| 1419 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1420 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1421 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1422 | audio_is_linear_pcm(format), |
| 1423 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1424 | *flags &= ~IAudioFlinger::TRACK_FAST; |
| 1425 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1426 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1427 | // This is probably too conservative, but legacy application code may depend on it. |
| 1428 | // If you change this calculation, also review the start threshold which is related. |
| 1429 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1430 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1431 | if (minBufCount < 2) { |
| 1432 | minBufCount = 2; |
| 1433 | } |
| 1434 | size_t minFrameCount = mNormalFrameCount * minBufCount; |
| 1435 | if (frameCount < minFrameCount) { |
| 1436 | frameCount = minFrameCount; |
| 1437 | } |
| 1438 | } |
| 1439 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1440 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1441 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1442 | switch (mType) { |
| 1443 | |
| 1444 | case DIRECT: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1445 | if (audio_is_linear_pcm(format)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1446 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1447 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1448 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1449 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1450 | lStatus = BAD_VALUE; |
| 1451 | goto Exit; |
| 1452 | } |
| 1453 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1454 | break; |
| 1455 | |
| 1456 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1457 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1458 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1459 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1460 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1461 | lStatus = BAD_VALUE; |
| 1462 | goto Exit; |
| 1463 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1464 | break; |
| 1465 | |
| 1466 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1467 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1468 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1469 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1470 | format, mOutput, mFormat); |
| 1471 | lStatus = BAD_VALUE; |
| 1472 | goto Exit; |
| 1473 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1474 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1475 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1476 | lStatus = BAD_VALUE; |
| 1477 | goto Exit; |
| 1478 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | lStatus = initCheck(); |
| 1484 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1485 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1486 | goto Exit; |
| 1487 | } |
| 1488 | |
| 1489 | { // scope for mLock |
| 1490 | Mutex::Autolock _l(mLock); |
| 1491 | |
| 1492 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1493 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1494 | // manager |
| 1495 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1496 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1497 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1498 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1499 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1500 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1501 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1502 | strategy, actual); |
| 1503 | lStatus = BAD_VALUE; |
| 1504 | goto Exit; |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | if (!isTimed) { |
| 1510 | track = new Track(this, client, streamType, sampleRate, format, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1511 | channelMask, frameCount, NULL, sharedBuffer, |
| 1512 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1513 | } else { |
| 1514 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1515 | channelMask, frameCount, sharedBuffer, sessionId, uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1516 | } |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1517 | |
| 1518 | // new Track always returns non-NULL, |
| 1519 | // but TimedTrack::create() is a factory that could fail by returning NULL |
| 1520 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1521 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1522 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1523 | // track must be cleared from the caller as the caller has the AF lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1524 | goto Exit; |
| 1525 | } |
| 1526 | mTracks.add(track); |
| 1527 | |
| 1528 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1529 | if (chain != 0) { |
| 1530 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1531 | track->setMainBuffer(chain->inBuffer()); |
| 1532 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1533 | chain->incTrackCnt(); |
| 1534 | } |
| 1535 | |
| 1536 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1537 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1538 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1539 | // so ask activity manager to do this on our behalf |
| 1540 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | lStatus = NO_ERROR; |
| 1545 | |
| 1546 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1547 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1548 | return track; |
| 1549 | } |
| 1550 | |
| 1551 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1552 | { |
| 1553 | return latency; |
| 1554 | } |
| 1555 | |
| 1556 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1557 | { |
| 1558 | Mutex::Autolock _l(mLock); |
| 1559 | return latency_l(); |
| 1560 | } |
| 1561 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1562 | { |
| 1563 | if (initCheck() == NO_ERROR) { |
| 1564 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1565 | } else { |
| 1566 | return 0; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1571 | { |
| 1572 | Mutex::Autolock _l(mLock); |
| 1573 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1574 | if (mOutput && mOutput->audioHwDev && |
| 1575 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1576 | mMasterVolume = 1.0; |
| 1577 | } else { |
| 1578 | mMasterVolume = value; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1583 | { |
| 1584 | Mutex::Autolock _l(mLock); |
| 1585 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1586 | if (mOutput && mOutput->audioHwDev && |
| 1587 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1588 | mMasterMute = false; |
| 1589 | } else { |
| 1590 | mMasterMute = muted; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 1595 | { |
| 1596 | Mutex::Autolock _l(mLock); |
| 1597 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1598 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 1602 | { |
| 1603 | Mutex::Autolock _l(mLock); |
| 1604 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1605 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 1609 | { |
| 1610 | Mutex::Autolock _l(mLock); |
| 1611 | return mStreamTypes[stream].volume; |
| 1612 | } |
| 1613 | |
| 1614 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1615 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1616 | { |
| 1617 | status_t status = ALREADY_EXISTS; |
| 1618 | |
| 1619 | // set retry count for buffer fill |
| 1620 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1621 | if (mActiveTracks.indexOf(track) < 0) { |
| 1622 | // the track is newly added, make sure it fills up all its |
| 1623 | // buffers before playing. This is to ensure the client will |
| 1624 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1625 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1626 | TrackBase::track_state state = track->mState; |
| 1627 | mLock.unlock(); |
| 1628 | status = AudioSystem::startOutput(mId, track->streamType(), track->sessionId()); |
| 1629 | mLock.lock(); |
| 1630 | // abort track was stopped/paused while we released the lock |
| 1631 | if (state != track->mState) { |
| 1632 | if (status == NO_ERROR) { |
| 1633 | mLock.unlock(); |
| 1634 | AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); |
| 1635 | mLock.lock(); |
| 1636 | } |
| 1637 | return INVALID_OPERATION; |
| 1638 | } |
| 1639 | // abort if start is rejected by audio policy manager |
| 1640 | if (status != NO_ERROR) { |
| 1641 | return PERMISSION_DENIED; |
| 1642 | } |
| 1643 | #ifdef ADD_BATTERY_DATA |
| 1644 | // to track the speaker usage |
| 1645 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 1646 | #endif |
| 1647 | } |
| 1648 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1649 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1650 | track->mResetDone = false; |
| 1651 | track->mPresentationCompleteFrames = 0; |
| 1652 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1653 | mWakeLockUids.add(track->uid()); |
| 1654 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1655 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1656 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1657 | if (chain != 0) { |
| 1658 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 1659 | track->sessionId()); |
| 1660 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | status = NO_ERROR; |
| 1664 | } |
| 1665 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1666 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1667 | return status; |
| 1668 | } |
| 1669 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1670 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1671 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1672 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1673 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1674 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 1675 | track->mState = TrackBase::STOPPED; |
| 1676 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1677 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1678 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1679 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1680 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1681 | |
| 1682 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1686 | { |
| 1687 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 1688 | mTracks.remove(track); |
| 1689 | deleteTrackName_l(track->name()); |
| 1690 | // redundant as track is about to be destroyed, for dumpsys only |
| 1691 | track->mName = -1; |
| 1692 | if (track->isFastTrack()) { |
| 1693 | int index = track->mFastIndex; |
| 1694 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 1695 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1696 | mFastTrackAvailMask |= 1 << index; |
| 1697 | // redundant as track is about to be destroyed, for dumpsys only |
| 1698 | track->mFastIndex = -1; |
| 1699 | } |
| 1700 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1701 | if (chain != 0) { |
| 1702 | chain->decTrackCnt(); |
| 1703 | } |
| 1704 | } |
| 1705 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1706 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1707 | { |
| 1708 | // Thread could be blocked waiting for async |
| 1709 | // so signal it to handle state changes immediately |
| 1710 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1711 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1712 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1713 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1716 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1717 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 | Mutex::Autolock _l(mLock); |
| 1719 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1720 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1723 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 1724 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | free(s); |
| 1726 | return out_s8; |
| 1727 | } |
| 1728 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1729 | void AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1730 | AudioSystem::OutputDescriptor desc; |
| 1731 | void *param2 = NULL; |
| 1732 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1733 | ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | param); |
| 1735 | |
| 1736 | switch (event) { |
| 1737 | case AudioSystem::OUTPUT_OPENED: |
| 1738 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 1739 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1740 | desc.samplingRate = mSampleRate; |
| 1741 | desc.format = mFormat; |
| 1742 | desc.frameCount = mNormalFrameCount; // FIXME see |
| 1743 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 1744 | desc.latency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1745 | param2 = &desc; |
| 1746 | break; |
| 1747 | |
| 1748 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1749 | param2 = ¶m; |
| 1750 | case AudioSystem::OUTPUT_CLOSED: |
| 1751 | default: |
| 1752 | break; |
| 1753 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1754 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1757 | void AudioFlinger::PlaybackThread::writeCallback() |
| 1758 | { |
| 1759 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1760 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | void AudioFlinger::PlaybackThread::drainCallback() |
| 1764 | { |
| 1765 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1766 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1769 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1770 | { |
| 1771 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1772 | // reject out of sequence requests |
| 1773 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 1774 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1775 | mWaitWorkCV.signal(); |
| 1776 | } |
| 1777 | } |
| 1778 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1779 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1780 | { |
| 1781 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1782 | // reject out of sequence requests |
| 1783 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 1784 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1785 | mWaitWorkCV.signal(); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | // static |
| 1790 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1791 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1792 | void *cookie) |
| 1793 | { |
| 1794 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 1795 | ALOGV("asyncCallback() event %d", event); |
| 1796 | switch (event) { |
| 1797 | case STREAM_CBK_EVENT_WRITE_READY: |
| 1798 | me->writeCallback(); |
| 1799 | break; |
| 1800 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 1801 | me->drainCallback(); |
| 1802 | break; |
| 1803 | default: |
| 1804 | ALOGW("asyncCallback() unknown event %d", event); |
| 1805 | break; |
| 1806 | } |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1810 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1811 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1812 | // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1813 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
| 1814 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1815 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1816 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1817 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1818 | if ((mType == MIXER || mType == DUPLICATING) |
| 1819 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 1820 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 1821 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1822 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 1823 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 1824 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
| 1825 | mFormat = mHALFormat; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1826 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1827 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1828 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1829 | if ((mType == MIXER || mType == DUPLICATING) |
| 1830 | && !isValidPcmSinkFormat(mFormat)) { |
| 1831 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 1832 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1833 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 1834 | mFrameSize = audio_stream_out_frame_size(mOutput->stream); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 1835 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 1836 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 | if (mFrameCount & 15) { |
| 1838 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 1839 | mFrameCount); |
| 1840 | } |
| 1841 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1842 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 1843 | (mOutput->stream->set_callback != NULL)) { |
| 1844 | if (mOutput->stream->set_callback(mOutput->stream, |
| 1845 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 1846 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 1847 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1851 | // Calculate size of normal sink buffer relative to the HAL output buffer size |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1852 | double multiplier = 1.0; |
| 1853 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 1854 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1855 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 1856 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1857 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 1858 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 1859 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 1860 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 1861 | maxNormalFrameCount = minNormalFrameCount; |
| 1862 | } |
| 1863 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 1864 | if (multiplier <= 1.0) { |
| 1865 | multiplier = 1.0; |
| 1866 | } else if (multiplier <= 2.0) { |
| 1867 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 1868 | multiplier = 2.0; |
| 1869 | } else { |
| 1870 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 1871 | } |
| 1872 | } else { |
| 1873 | // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1874 | // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1875 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 1876 | // constraint) |
| 1877 | // FIXME this rounding up should not be done if no HAL SRC |
| 1878 | uint32_t truncMult = (uint32_t) multiplier; |
| 1879 | if ((truncMult & 1)) { |
| 1880 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 1881 | ++truncMult; |
| 1882 | } |
| 1883 | } |
| 1884 | multiplier = (double) truncMult; |
| 1885 | } |
| 1886 | } |
| 1887 | mNormalFrameCount = multiplier * mFrameCount; |
| 1888 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1889 | if (mType == MIXER || mType == DUPLICATING) { |
| 1890 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 1891 | } |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1892 | ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1893 | mNormalFrameCount); |
| 1894 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1895 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 1896 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 1897 | free(mSinkBuffer); |
| 1898 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 1899 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 1900 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 1901 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1902 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1903 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1904 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 1905 | // drives the output. |
| 1906 | free(mMixerBuffer); |
| 1907 | mMixerBuffer = NULL; |
| 1908 | if (mMixerBufferEnabled) { |
| 1909 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 1910 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 1911 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 1912 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 1913 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1914 | free(mEffectBuffer); |
| 1915 | mEffectBuffer = NULL; |
| 1916 | if (mEffectBufferEnabled) { |
| 1917 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 1918 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 1919 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 1920 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 1921 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1922 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1923 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1924 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1925 | // Note that mLock is not held when readOutputParameters_l() is called from the constructor |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1926 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1927 | // matter. |
| 1928 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1929 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1930 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1931 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1936 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1937 | { |
| 1938 | if (halFrames == NULL || dspFrames == NULL) { |
| 1939 | return BAD_VALUE; |
| 1940 | } |
| 1941 | Mutex::Autolock _l(mLock); |
| 1942 | if (initCheck() != NO_ERROR) { |
| 1943 | return INVALID_OPERATION; |
| 1944 | } |
| 1945 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 1946 | *halFrames = framesWritten; |
| 1947 | |
| 1948 | if (isSuspended()) { |
| 1949 | // return an estimation of rendered frames when the output is suspended |
| 1950 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
| 1951 | *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; |
| 1952 | return NO_ERROR; |
| 1953 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1954 | status_t status; |
| 1955 | uint32_t frames; |
| 1956 | status = mOutput->stream->get_render_position(mOutput->stream, &frames); |
| 1957 | *dspFrames = (size_t)frames; |
| 1958 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const |
| 1963 | { |
| 1964 | Mutex::Autolock _l(mLock); |
| 1965 | uint32_t result = 0; |
| 1966 | if (getEffectChain_l(sessionId) != 0) { |
| 1967 | result = EFFECT_SESSION; |
| 1968 | } |
| 1969 | |
| 1970 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1971 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1972 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1973 | result |= TRACK_SESSION; |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | return result; |
| 1979 | } |
| 1980 | |
| 1981 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1982 | { |
| 1983 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 1984 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 1985 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1986 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 1987 | } |
| 1988 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1989 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1990 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1991 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 1992 | } |
| 1993 | } |
| 1994 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 1995 | } |
| 1996 | |
| 1997 | |
| 1998 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
| 1999 | { |
| 2000 | Mutex::Autolock _l(mLock); |
| 2001 | return mOutput; |
| 2002 | } |
| 2003 | |
| 2004 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2005 | { |
| 2006 | Mutex::Autolock _l(mLock); |
| 2007 | AudioStreamOut *output = mOutput; |
| 2008 | mOutput = NULL; |
| 2009 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2010 | // must push a NULL and wait for ack |
| 2011 | mOutputSink.clear(); |
| 2012 | mPipeSink.clear(); |
| 2013 | mNormalSink.clear(); |
| 2014 | return output; |
| 2015 | } |
| 2016 | |
| 2017 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2018 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2019 | { |
| 2020 | if (mOutput == NULL) { |
| 2021 | return NULL; |
| 2022 | } |
| 2023 | return &mOutput->stream->common; |
| 2024 | } |
| 2025 | |
| 2026 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2027 | { |
| 2028 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2029 | } |
| 2030 | |
| 2031 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2032 | { |
| 2033 | if (!isValidSyncEvent(event)) { |
| 2034 | return BAD_VALUE; |
| 2035 | } |
| 2036 | |
| 2037 | Mutex::Autolock _l(mLock); |
| 2038 | |
| 2039 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2040 | sp<Track> track = mTracks[i]; |
| 2041 | if (event->triggerSession() == track->sessionId()) { |
| 2042 | (void) track->setSyncEvent(event); |
| 2043 | return NO_ERROR; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | return NAME_NOT_FOUND; |
| 2048 | } |
| 2049 | |
| 2050 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2051 | { |
| 2052 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2053 | } |
| 2054 | |
| 2055 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2056 | const Vector< sp<Track> >& tracksToRemove) |
| 2057 | { |
| 2058 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2059 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2060 | for (size_t i = 0 ; i < count ; i++) { |
| 2061 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2062 | if (track->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2063 | AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2064 | #ifdef ADD_BATTERY_DATA |
| 2065 | // to track the speaker usage |
| 2066 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2067 | #endif |
| 2068 | if (track->isTerminated()) { |
| 2069 | AudioSystem::releaseOutput(mId); |
| 2070 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2077 | { |
| 2078 | if (!mMasterMute) { |
| 2079 | char value[PROPERTY_VALUE_MAX]; |
| 2080 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2081 | char *endptr; |
| 2082 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2083 | if (*endptr == '\0' && ul != 0) { |
| 2084 | ALOGD("Silence is golden"); |
| 2085 | // The setprop command will not allow a property to be changed after |
| 2086 | // the first time it is set, so we don't have to worry about un-muting. |
| 2087 | setMasterMute_l(true); |
| 2088 | } |
| 2089 | } |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2094 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2095 | { |
| 2096 | // FIXME rewrite to reduce number of system calls |
| 2097 | mLastWriteTime = systemTime(); |
| 2098 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2099 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2100 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2101 | |
| 2102 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2103 | if (mNormalSink != 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2104 | const size_t count = mBytesRemaining / mFrameSize; |
| 2105 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2106 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2107 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2108 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2109 | if (screenState != mScreenState) { |
| 2110 | mScreenState = screenState; |
| 2111 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2112 | if (pipe != NULL) { |
| 2113 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2114 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2115 | } |
| 2116 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2117 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2118 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2120 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2121 | } else { |
| 2122 | bytesWritten = framesWritten; |
| 2123 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2124 | status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2125 | if (status == NO_ERROR) { |
| 2126 | size_t totalFramesWritten = mNormalSink->framesWritten(); |
| 2127 | if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { |
| 2128 | mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; |
| 2129 | mLatchDValid = true; |
| 2130 | } |
| 2131 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2132 | // otherwise use the HAL / AudioStreamOut directly |
| 2133 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2134 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2135 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2136 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2137 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2138 | mWriteAckSequence += 2; |
| 2139 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2140 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2141 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2142 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2143 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2144 | // They are used e.g for multichannel PCM playback over HDMI. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2145 | bytesWritten = mOutput->stream->write(mOutput->stream, |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2146 | (char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2147 | if (mUseAsyncWrite && |
| 2148 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2149 | // 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] | 2150 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2151 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2152 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2153 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2156 | mNumWrites++; |
| 2157 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2158 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2159 | return bytesWritten; |
| 2160 | } |
| 2161 | |
| 2162 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2163 | { |
| 2164 | if (mOutput->stream->drain) { |
| 2165 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2166 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2167 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2168 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2169 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2170 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2171 | } |
| 2172 | mOutput->stream->drain(mOutput->stream, |
| 2173 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2174 | : AUDIO_DRAIN_ALL); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2179 | { |
| 2180 | // Default implementation has nothing to do |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2185 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2186 | - activeSleepTime from activeSleepTimeUs() |
| 2187 | - idleSleepTime from idleSleepTimeUs() |
| 2188 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 2189 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2190 | |
| 2191 | The parameters that affect these derived values are: |
| 2192 | - frame count |
| 2193 | - frame size |
| 2194 | - sample rate |
| 2195 | - device type: A2DP or not |
| 2196 | - device latency |
| 2197 | - format: PCM or not |
| 2198 | - active sleep time |
| 2199 | - idle sleep time |
| 2200 | */ |
| 2201 | |
| 2202 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2203 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2204 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2205 | activeSleepTime = activeSleepTimeUs(); |
| 2206 | idleSleepTime = idleSleepTimeUs(); |
| 2207 | } |
| 2208 | |
| 2209 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2210 | { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2211 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2212 | this, streamType, mTracks.size()); |
| 2213 | Mutex::Autolock _l(mLock); |
| 2214 | |
| 2215 | size_t size = mTracks.size(); |
| 2216 | for (size_t i = 0; i < size; i++) { |
| 2217 | sp<Track> t = mTracks[i]; |
| 2218 | if (t->streamType() == streamType) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2219 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2225 | { |
| 2226 | int session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2227 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2228 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2229 | bool ownsBuffer = false; |
| 2230 | |
| 2231 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 2232 | if (session > 0) { |
| 2233 | // Only one effect chain can be present in direct output thread and it uses |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2234 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2235 | if (mType != DIRECT) { |
| 2236 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2237 | buffer = new int16_t[numSamples]; |
| 2238 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2239 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2240 | ownsBuffer = true; |
| 2241 | } |
| 2242 | |
| 2243 | // Attach all tracks with same session ID to this chain. |
| 2244 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2245 | sp<Track> track = mTracks[i]; |
| 2246 | if (session == track->sessionId()) { |
| 2247 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2248 | buffer); |
| 2249 | track->setMainBuffer(buffer); |
| 2250 | chain->incTrackCnt(); |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | // indicate all active tracks in the chain |
| 2255 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2256 | sp<Track> track = mActiveTracks[i].promote(); |
| 2257 | if (track == 0) { |
| 2258 | continue; |
| 2259 | } |
| 2260 | if (session == track->sessionId()) { |
| 2261 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2262 | chain->incActiveTrackCnt(); |
| 2263 | } |
| 2264 | } |
| 2265 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2266 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2267 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2268 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2269 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2270 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
| 2271 | // chains list in order to be processed last as it contains output stage effects |
| 2272 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2273 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
| 2274 | // after track specific effects and before output stage |
| 2275 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 2276 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
| 2277 | // Effect chain for other sessions are inserted at beginning of effect |
| 2278 | // chains list to be processed before output mix effects. Relative order between other |
| 2279 | // sessions is not important |
| 2280 | size_t size = mEffectChains.size(); |
| 2281 | size_t i = 0; |
| 2282 | for (i = 0; i < size; i++) { |
| 2283 | if (mEffectChains[i]->sessionId() < session) { |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | mEffectChains.insertAt(chain, i); |
| 2288 | checkSuspendOnAddEffectChain_l(chain); |
| 2289 | |
| 2290 | return NO_ERROR; |
| 2291 | } |
| 2292 | |
| 2293 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2294 | { |
| 2295 | int session = chain->sessionId(); |
| 2296 | |
| 2297 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2298 | |
| 2299 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2300 | if (chain == mEffectChains[i]) { |
| 2301 | mEffectChains.removeAt(i); |
| 2302 | // detach all active tracks from the chain |
| 2303 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2304 | sp<Track> track = mActiveTracks[i].promote(); |
| 2305 | if (track == 0) { |
| 2306 | continue; |
| 2307 | } |
| 2308 | if (session == track->sessionId()) { |
| 2309 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2310 | chain.get(), session); |
| 2311 | chain->decActiveTrackCnt(); |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | // detach all tracks with same session ID from this chain |
| 2316 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2317 | sp<Track> track = mTracks[i]; |
| 2318 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2319 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2320 | chain->decTrackCnt(); |
| 2321 | } |
| 2322 | } |
| 2323 | break; |
| 2324 | } |
| 2325 | } |
| 2326 | return mEffectChains.size(); |
| 2327 | } |
| 2328 | |
| 2329 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2330 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2331 | { |
| 2332 | Mutex::Autolock _l(mLock); |
| 2333 | return attachAuxEffect_l(track, EffectId); |
| 2334 | } |
| 2335 | |
| 2336 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2337 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2338 | { |
| 2339 | status_t status = NO_ERROR; |
| 2340 | |
| 2341 | if (EffectId == 0) { |
| 2342 | track->setAuxBuffer(0, NULL); |
| 2343 | } else { |
| 2344 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2345 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2346 | if (effect != 0) { |
| 2347 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2348 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2349 | } else { |
| 2350 | status = INVALID_OPERATION; |
| 2351 | } |
| 2352 | } else { |
| 2353 | status = BAD_VALUE; |
| 2354 | } |
| 2355 | } |
| 2356 | return status; |
| 2357 | } |
| 2358 | |
| 2359 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2360 | { |
| 2361 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2362 | sp<Track> track = mTracks[i]; |
| 2363 | if (track->auxEffectId() == effectId) { |
| 2364 | attachAuxEffect_l(track, 0); |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2370 | { |
| 2371 | Vector< sp<Track> > tracksToRemove; |
| 2372 | |
| 2373 | standbyTime = systemTime(); |
| 2374 | |
| 2375 | // MIXER |
| 2376 | nsecs_t lastWarning = 0; |
| 2377 | |
| 2378 | // DUPLICATING |
| 2379 | // FIXME could this be made local to while loop? |
| 2380 | writeFrames = 0; |
| 2381 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2382 | int lastGeneration = 0; |
| 2383 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2384 | cacheParameters_l(); |
| 2385 | sleepTime = idleSleepTime; |
| 2386 | |
| 2387 | if (mType == MIXER) { |
| 2388 | sleepTimeShift = 0; |
| 2389 | } |
| 2390 | |
| 2391 | CpuStats cpuStats; |
| 2392 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2393 | |
| 2394 | acquireWakeLock(); |
| 2395 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2396 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2397 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2398 | // and then that string will be logged at the next convenient opportunity. |
| 2399 | const char *logString = NULL; |
| 2400 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2401 | checkSilentMode_l(); |
| 2402 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2403 | while (!exitPending()) |
| 2404 | { |
| 2405 | cpuStats.sample(myName); |
| 2406 | |
| 2407 | Vector< sp<EffectChain> > effectChains; |
| 2408 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2409 | { // scope for mLock |
| 2410 | |
| 2411 | Mutex::Autolock _l(mLock); |
| 2412 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2413 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2414 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2415 | if (logString != NULL) { |
| 2416 | mNBLogWriter->logTimestamp(); |
| 2417 | mNBLogWriter->log(logString); |
| 2418 | logString = NULL; |
| 2419 | } |
| 2420 | |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2421 | if (mLatchDValid) { |
| 2422 | mLatchQ = mLatchD; |
| 2423 | mLatchDValid = false; |
| 2424 | mLatchQValid = true; |
| 2425 | } |
| 2426 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2427 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2428 | if (mSignalPending) { |
| 2429 | // A signal was raised while we were unlocked |
| 2430 | mSignalPending = false; |
| 2431 | } else if (waitingAsyncCallback_l()) { |
| 2432 | if (exitPending()) { |
| 2433 | break; |
| 2434 | } |
| 2435 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2436 | mWakeLockUids.clear(); |
| 2437 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2438 | ALOGV("wait async completion"); |
| 2439 | mWaitWorkCV.wait(mLock); |
| 2440 | ALOGV("async completion/wake"); |
| 2441 | acquireWakeLock_l(); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 2442 | standbyTime = systemTime() + standbyDelay; |
| 2443 | sleepTime = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2444 | |
| 2445 | continue; |
| 2446 | } |
| 2447 | if ((!mActiveTracks.size() && systemTime() > standbyTime) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2448 | isSuspended()) { |
| 2449 | // put audio hardware into standby after short delay |
| 2450 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2451 | |
| 2452 | threadLoop_standby(); |
| 2453 | |
| 2454 | mStandby = true; |
| 2455 | } |
| 2456 | |
| 2457 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2458 | // we're about to wait, flush the binder command buffer |
| 2459 | IPCThreadState::self()->flushCommands(); |
| 2460 | |
| 2461 | clearOutputTracks(); |
| 2462 | |
| 2463 | if (exitPending()) { |
| 2464 | break; |
| 2465 | } |
| 2466 | |
| 2467 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2468 | mWakeLockUids.clear(); |
| 2469 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2470 | // wait until we have something to do... |
| 2471 | ALOGV("%s going to sleep", myName.string()); |
| 2472 | mWaitWorkCV.wait(mLock); |
| 2473 | ALOGV("%s waking up", myName.string()); |
| 2474 | acquireWakeLock_l(); |
| 2475 | |
| 2476 | mMixerStatus = MIXER_IDLE; |
| 2477 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2478 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2479 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2480 | checkSilentMode_l(); |
| 2481 | |
| 2482 | standbyTime = systemTime() + standbyDelay; |
| 2483 | sleepTime = idleSleepTime; |
| 2484 | if (mType == MIXER) { |
| 2485 | sleepTimeShift = 0; |
| 2486 | } |
| 2487 | |
| 2488 | continue; |
| 2489 | } |
| 2490 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2491 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2492 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2493 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2494 | // compare with previously applied list |
| 2495 | if (lastGeneration != mActiveTracksGeneration) { |
| 2496 | // update wakelock |
| 2497 | updateWakeLockUids_l(mWakeLockUids); |
| 2498 | lastGeneration = mActiveTracksGeneration; |
| 2499 | } |
| 2500 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2501 | // prevent any changes in effect chain list and in each effect chain |
| 2502 | // during mixing and effect process as the audio buffers could be deleted |
| 2503 | // or modified if an effect is created or deleted |
| 2504 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2505 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2506 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2507 | if (mBytesRemaining == 0) { |
| 2508 | mCurrentWriteLength = 0; |
| 2509 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 2510 | // threadLoop_mix() sets mCurrentWriteLength |
| 2511 | threadLoop_mix(); |
| 2512 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 2513 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
| 2514 | // threadLoop_sleepTime sets sleepTime to 0 if data |
| 2515 | // must be written to HAL |
| 2516 | threadLoop_sleepTime(); |
| 2517 | if (sleepTime == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2518 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2519 | } |
| 2520 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2521 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
| 2522 | // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. |
| 2523 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 2524 | // or mSinkBuffer (if there are no effects). |
| 2525 | // |
| 2526 | // This is done pre-effects computation; if effects change to |
| 2527 | // support higher precision, this needs to move. |
| 2528 | // |
| 2529 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
| 2530 | // TODO use sleepTime == 0 as an additional condition. |
| 2531 | if (mMixerBufferValid) { |
| 2532 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 2533 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 2534 | |
| 2535 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 2536 | mNormalFrameCount * mChannelCount); |
| 2537 | } |
| 2538 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2539 | mBytesRemaining = mCurrentWriteLength; |
| 2540 | if (isSuspended()) { |
| 2541 | sleepTime = suspendSleepTimeUs(); |
| 2542 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2543 | mBytesWritten += mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2544 | mBytesRemaining = 0; |
| 2545 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2546 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2547 | // only process effects if we're going to write |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2548 | if (sleepTime == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2549 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2550 | effectChains[i]->process_l(); |
| 2551 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2552 | } |
| 2553 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2554 | // Process effect chains for offloaded thread even if no audio |
| 2555 | // was read from audio track: process only updates effect state |
| 2556 | // and thus does have to be synchronized with audio writes but may have |
| 2557 | // to be called while waiting for async write callback |
| 2558 | if (mType == OFFLOAD) { |
| 2559 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2560 | effectChains[i]->process_l(); |
| 2561 | } |
| 2562 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2563 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2564 | // Only if the Effects buffer is enabled and there is data in the |
| 2565 | // Effects buffer (buffer valid), we need to |
| 2566 | // copy into the sink buffer. |
| 2567 | // TODO use sleepTime == 0 as an additional condition. |
| 2568 | if (mEffectBufferValid) { |
| 2569 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
| 2570 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 2571 | mNormalFrameCount * mChannelCount); |
| 2572 | } |
| 2573 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2574 | // enable changes in effect chain |
| 2575 | unlockEffectChains(effectChains); |
| 2576 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2577 | if (!waitingAsyncCallback()) { |
| 2578 | // sleepTime == 0 means we must write to audio hardware |
| 2579 | if (sleepTime == 0) { |
| 2580 | if (mBytesRemaining) { |
| 2581 | ssize_t ret = threadLoop_write(); |
| 2582 | if (ret < 0) { |
| 2583 | mBytesRemaining = 0; |
| 2584 | } else { |
| 2585 | mBytesWritten += ret; |
| 2586 | mBytesRemaining -= ret; |
| 2587 | } |
| 2588 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 2589 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 2590 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2591 | } |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2592 | if (mType == MIXER) { |
| 2593 | // write blocked detection |
| 2594 | nsecs_t now = systemTime(); |
| 2595 | nsecs_t delta = now - mLastWriteTime; |
| 2596 | if (!mStandby && delta > maxPeriod) { |
| 2597 | mNumDelayedWrites++; |
| 2598 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2599 | ATRACE_NAME("underrun"); |
| 2600 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2601 | ns2ms(delta), mNumDelayedWrites, this); |
| 2602 | lastWarning = now; |
| 2603 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2604 | } |
| 2605 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2606 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2607 | } else { |
| 2608 | usleep(sleepTime); |
| 2609 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | // Finally let go of removed track(s), without the lock held |
| 2613 | // since we can't guarantee the destructors won't acquire that |
| 2614 | // same lock. This will also mutate and push a new fast mixer state. |
| 2615 | threadLoop_removeTracks(tracksToRemove); |
| 2616 | tracksToRemove.clear(); |
| 2617 | |
| 2618 | // FIXME I don't understand the need for this here; |
| 2619 | // it was in the original code but maybe the |
| 2620 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2621 | clearOutputTracks(); |
| 2622 | |
| 2623 | // Effect chains will be actually deleted here if they were removed from |
| 2624 | // mEffectChains list during mixing or effects processing |
| 2625 | effectChains.clear(); |
| 2626 | |
| 2627 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2628 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2629 | } |
| 2630 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2631 | threadLoop_exit(); |
| 2632 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 2633 | if (!mStandby) { |
| 2634 | threadLoop_standby(); |
| 2635 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2639 | mWakeLockUids.clear(); |
| 2640 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2641 | |
| 2642 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2643 | return false; |
| 2644 | } |
| 2645 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2646 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 2647 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 2648 | { |
| 2649 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2650 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2651 | for (size_t i=0 ; i<count ; i++) { |
| 2652 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 2653 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2654 | mWakeLockUids.remove(track->uid()); |
| 2655 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2656 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 2657 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2658 | if (chain != 0) { |
| 2659 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 2660 | track->sessionId()); |
| 2661 | chain->decActiveTrackCnt(); |
| 2662 | } |
| 2663 | if (track->isTerminated()) { |
| 2664 | removeTrack_l(track); |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2671 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 2672 | { |
| 2673 | if (mNormalSink != 0) { |
| 2674 | return mNormalSink->getTimestamp(timestamp); |
| 2675 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2676 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2677 | uint64_t position64; |
| 2678 | int ret = mOutput->stream->get_presentation_position( |
| 2679 | mOutput->stream, &position64, ×tamp.mTime); |
| 2680 | if (ret == 0) { |
| 2681 | timestamp.mPosition = (uint32_t)position64; |
| 2682 | return NO_ERROR; |
| 2683 | } |
| 2684 | } |
| 2685 | return INVALID_OPERATION; |
| 2686 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2687 | |
| 2688 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 2689 | audio_patch_handle_t *handle) |
| 2690 | { |
| 2691 | status_t status = NO_ERROR; |
| 2692 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2693 | // store new device and send to effects |
| 2694 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 2695 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 2696 | type |= patch->sinks[i].ext.device.type; |
| 2697 | } |
| 2698 | mOutDevice = type; |
| 2699 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2700 | mEffectChains[i]->setDevice_l(mOutDevice); |
| 2701 | } |
| 2702 | |
| 2703 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2704 | status = hwDevice->create_audio_patch(hwDevice, |
| 2705 | patch->num_sources, |
| 2706 | patch->sources, |
| 2707 | patch->num_sinks, |
| 2708 | patch->sinks, |
| 2709 | handle); |
| 2710 | } else { |
| 2711 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 2712 | } |
| 2713 | return status; |
| 2714 | } |
| 2715 | |
| 2716 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 2717 | { |
| 2718 | status_t status = NO_ERROR; |
| 2719 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2720 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2721 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 2722 | } else { |
| 2723 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 2724 | } |
| 2725 | return status; |
| 2726 | } |
| 2727 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2728 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 2729 | { |
| 2730 | Mutex::Autolock _l(mLock); |
| 2731 | mTracks.add(track); |
| 2732 | } |
| 2733 | |
| 2734 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 2735 | { |
| 2736 | Mutex::Autolock _l(mLock); |
| 2737 | destroyTrack_l(track); |
| 2738 | } |
| 2739 | |
| 2740 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 2741 | { |
| 2742 | ThreadBase::getAudioPortConfig(config); |
| 2743 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 2744 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 2745 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 2746 | } |
| 2747 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2748 | // ---------------------------------------------------------------------------- |
| 2749 | |
| 2750 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 2751 | audio_io_handle_t id, audio_devices_t device, type_t type) |
| 2752 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2753 | // mAudioMixer below |
| 2754 | // mFastMixer below |
| 2755 | mFastMixerFutex(0) |
| 2756 | // mOutputSink below |
| 2757 | // mPipeSink below |
| 2758 | // mNormalSink below |
| 2759 | { |
| 2760 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 2761 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2762 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2763 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2764 | mNormalFrameCount); |
| 2765 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2766 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2767 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 2768 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 2769 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 2770 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2771 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2772 | ALOG_ASSERT(index == 0); |
| 2773 | |
| 2774 | // initialize fast mixer depending on configuration |
| 2775 | bool initFastMixer; |
| 2776 | switch (kUseFastMixer) { |
| 2777 | case FastMixer_Never: |
| 2778 | initFastMixer = false; |
| 2779 | break; |
| 2780 | case FastMixer_Always: |
| 2781 | initFastMixer = true; |
| 2782 | break; |
| 2783 | case FastMixer_Static: |
| 2784 | case FastMixer_Dynamic: |
| 2785 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 2786 | break; |
| 2787 | } |
| 2788 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 2789 | audio_format_t fastMixerFormat; |
| 2790 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2791 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2792 | } else { |
| 2793 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2794 | } |
| 2795 | if (mFormat != fastMixerFormat) { |
| 2796 | // change our Sink format to accept our intermediate precision |
| 2797 | mFormat = fastMixerFormat; |
| 2798 | free(mSinkBuffer); |
| 2799 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2800 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 2801 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 2802 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2803 | |
| 2804 | // create a MonoPipe to connect our submix to FastMixer |
| 2805 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame^] | 2806 | NBAIO_Format origformat = format; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 2807 | // adjust format to match that of the Fast Mixer |
| 2808 | format.mFormat = fastMixerFormat; |
| 2809 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 2810 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2811 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 2812 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 2813 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 2814 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 2815 | const NBAIO_Format offers[1] = {format}; |
| 2816 | size_t numCounterOffers = 0; |
| 2817 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2818 | ALOG_ASSERT(index == 0); |
| 2819 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 2820 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2821 | mPipeSink = monoPipe; |
| 2822 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2823 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2824 | if (mTeeSinkOutputEnabled) { |
| 2825 | // create a Pipe to archive a copy of FastMixer's output for dumpsys |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame^] | 2826 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 2827 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2828 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame^] | 2829 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2830 | ALOG_ASSERT(index == 0); |
| 2831 | mTeeSink = teeSink; |
| 2832 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 2833 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame^] | 2834 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2835 | ALOG_ASSERT(index == 0); |
| 2836 | mTeeSource = teeSource; |
| 2837 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2838 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2839 | |
| 2840 | // create fast mixer and configure it initially with just one fast track for our submix |
| 2841 | mFastMixer = new FastMixer(); |
| 2842 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2843 | #ifdef STATE_QUEUE_DUMP |
| 2844 | sq->setObserverDump(&mStateQueueObserverDump); |
| 2845 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 2846 | #endif |
| 2847 | FastMixerState *state = sq->begin(); |
| 2848 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2849 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 2850 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 2851 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 2852 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 2853 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2854 | fastTrack->mGeneration++; |
| 2855 | state->mFastTracksGen++; |
| 2856 | state->mTrackMask = 1; |
| 2857 | // fast mixer will use the HAL output sink |
| 2858 | state->mOutputSink = mOutputSink.get(); |
| 2859 | state->mOutputSinkGen++; |
| 2860 | state->mFrameCount = mFrameCount; |
| 2861 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2862 | // already done in constructor initialization list |
| 2863 | //mFastMixerFutex = 0; |
| 2864 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2865 | state->mColdGen++; |
| 2866 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2867 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2868 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2869 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2870 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 2871 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2872 | sq->end(); |
| 2873 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2874 | |
| 2875 | // start the fast mixer |
| 2876 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 2877 | pid_t tid = mFastMixer->getTid(); |
| 2878 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 2879 | if (err != 0) { |
| 2880 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2881 | kPriorityFastMixer, getpid_cached, tid, err); |
| 2882 | } |
| 2883 | |
| 2884 | #ifdef AUDIO_WATCHDOG |
| 2885 | // create and start the watchdog |
| 2886 | mAudioWatchdog = new AudioWatchdog(); |
| 2887 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 2888 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 2889 | tid = mAudioWatchdog->getTid(); |
| 2890 | err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 2891 | if (err != 0) { |
| 2892 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2893 | kPriorityFastMixer, getpid_cached, tid, err); |
| 2894 | } |
| 2895 | #endif |
| 2896 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | switch (kUseFastMixer) { |
| 2900 | case FastMixer_Never: |
| 2901 | case FastMixer_Dynamic: |
| 2902 | mNormalSink = mOutputSink; |
| 2903 | break; |
| 2904 | case FastMixer_Always: |
| 2905 | mNormalSink = mPipeSink; |
| 2906 | break; |
| 2907 | case FastMixer_Static: |
| 2908 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 2909 | break; |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | AudioFlinger::MixerThread::~MixerThread() |
| 2914 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2915 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2916 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2917 | FastMixerState *state = sq->begin(); |
| 2918 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2919 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2920 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 2921 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2922 | } |
| 2923 | } |
| 2924 | state->mCommand = FastMixerState::EXIT; |
| 2925 | sq->end(); |
| 2926 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2927 | mFastMixer->join(); |
| 2928 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 2929 | // We'll use that extract the final state which contains one remaining fast track |
| 2930 | // corresponding to our sub-mix. |
| 2931 | state = sq->begin(); |
| 2932 | ALOG_ASSERT(state->mTrackMask == 1); |
| 2933 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2934 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 2935 | delete fastTrack->mBufferProvider; |
| 2936 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2937 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2938 | #ifdef AUDIO_WATCHDOG |
| 2939 | if (mAudioWatchdog != 0) { |
| 2940 | mAudioWatchdog->requestExit(); |
| 2941 | mAudioWatchdog->requestExitAndWait(); |
| 2942 | mAudioWatchdog.clear(); |
| 2943 | } |
| 2944 | #endif |
| 2945 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2946 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2947 | delete mAudioMixer; |
| 2948 | } |
| 2949 | |
| 2950 | |
| 2951 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 2952 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2953 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2954 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2955 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 2956 | } |
| 2957 | return latency; |
| 2958 | } |
| 2959 | |
| 2960 | |
| 2961 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2962 | { |
| 2963 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 2964 | } |
| 2965 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2966 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2967 | { |
| 2968 | // FIXME we should only do one push per cycle; confirm this is true |
| 2969 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2970 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2971 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2972 | FastMixerState *state = sq->begin(); |
| 2973 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 2974 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 2975 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2976 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2977 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 2978 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2979 | } |
| 2980 | #ifdef AUDIO_WATCHDOG |
| 2981 | if (mAudioWatchdog != 0) { |
| 2982 | mAudioWatchdog->resume(); |
| 2983 | } |
| 2984 | #endif |
| 2985 | } |
| 2986 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2987 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
| 2988 | FastMixerDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2989 | sq->end(); |
| 2990 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2991 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2992 | mNormalSink = mPipeSink; |
| 2993 | } |
| 2994 | } else { |
| 2995 | sq->end(false /*didModify*/); |
| 2996 | } |
| 2997 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2998 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3002 | { |
| 3003 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3004 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3005 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3006 | FastMixerState *state = sq->begin(); |
| 3007 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3008 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3009 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3010 | state->mColdGen++; |
| 3011 | mFastMixerFutex = 0; |
| 3012 | sq->end(); |
| 3013 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3014 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3015 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3016 | mNormalSink = mOutputSink; |
| 3017 | } |
| 3018 | #ifdef AUDIO_WATCHDOG |
| 3019 | if (mAudioWatchdog != 0) { |
| 3020 | mAudioWatchdog->pause(); |
| 3021 | } |
| 3022 | #endif |
| 3023 | } else { |
| 3024 | sq->end(false /*didModify*/); |
| 3025 | } |
| 3026 | } |
| 3027 | PlaybackThread::threadLoop_standby(); |
| 3028 | } |
| 3029 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3030 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3031 | { |
| 3032 | return false; |
| 3033 | } |
| 3034 | |
| 3035 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3036 | { |
| 3037 | return !mStandby; |
| 3038 | } |
| 3039 | |
| 3040 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3041 | { |
| 3042 | Mutex::Autolock _l(mLock); |
| 3043 | return waitingAsyncCallback_l(); |
| 3044 | } |
| 3045 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3046 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3047 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3048 | { |
| 3049 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
| 3050 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3051 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3052 | // discard any pending drain or write ack by incrementing sequence |
| 3053 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3054 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3055 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3056 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3057 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3058 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3059 | } |
| 3060 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3061 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3062 | { |
| 3063 | ALOGV("signal playback thread"); |
| 3064 | broadcast_l(); |
| 3065 | } |
| 3066 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3067 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3068 | { |
| 3069 | // obtain the presentation timestamp of the next output buffer |
| 3070 | int64_t pts; |
| 3071 | status_t status = INVALID_OPERATION; |
| 3072 | |
| 3073 | if (mNormalSink != 0) { |
| 3074 | status = mNormalSink->getNextWriteTimestamp(&pts); |
| 3075 | } else { |
| 3076 | status = mOutputSink->getNextWriteTimestamp(&pts); |
| 3077 | } |
| 3078 | |
| 3079 | if (status != NO_ERROR) { |
| 3080 | pts = AudioBufferProvider::kInvalidPTS; |
| 3081 | } |
| 3082 | |
| 3083 | // mix buffers... |
| 3084 | mAudioMixer->process(pts); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3085 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3086 | // increase sleep time progressively when application underrun condition clears. |
| 3087 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3088 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3089 | // such that we would underrun the audio HAL. |
| 3090 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 3091 | sleepTimeShift--; |
| 3092 | } |
| 3093 | sleepTime = 0; |
| 3094 | standbyTime = systemTime() + standbyDelay; |
| 3095 | //TODO: delay standby when effects have a tail |
| 3096 | } |
| 3097 | |
| 3098 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3099 | { |
| 3100 | // If no tracks are ready, sleep once for the duration of an output |
| 3101 | // buffer size, then write 0s to the output |
| 3102 | if (sleepTime == 0) { |
| 3103 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 3104 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 3105 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 3106 | sleepTime = kMinThreadSleepTimeUs; |
| 3107 | } |
| 3108 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3109 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3110 | // duration we would end up writing less data than needed by the audio HAL if |
| 3111 | // the condition persists. |
| 3112 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3113 | sleepTimeShift++; |
| 3114 | } |
| 3115 | } else { |
| 3116 | sleepTime = idleSleepTime; |
| 3117 | } |
| 3118 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3119 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3120 | // before effects processing or output. |
| 3121 | if (mMixerBufferValid) { |
| 3122 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3123 | } else { |
| 3124 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3125 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3126 | sleepTime = 0; |
| 3127 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3128 | "anticipated start"); |
| 3129 | } |
| 3130 | // TODO add standby time extension fct of effect tail |
| 3131 | } |
| 3132 | |
| 3133 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3134 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3135 | Vector< sp<Track> > *tracksToRemove) |
| 3136 | { |
| 3137 | |
| 3138 | mixer_state mixerStatus = MIXER_IDLE; |
| 3139 | // find out which tracks need to be processed |
| 3140 | size_t count = mActiveTracks.size(); |
| 3141 | size_t mixedTracks = 0; |
| 3142 | size_t tracksWithEffect = 0; |
| 3143 | // counts only _active_ fast tracks |
| 3144 | size_t fastTracks = 0; |
| 3145 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3146 | |
| 3147 | float masterVolume = mMasterVolume; |
| 3148 | bool masterMute = mMasterMute; |
| 3149 | |
| 3150 | if (masterMute) { |
| 3151 | masterVolume = 0; |
| 3152 | } |
| 3153 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3154 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3155 | if (chain != 0) { |
| 3156 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3157 | chain->setVolume_l(&v, &v); |
| 3158 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3159 | chain.clear(); |
| 3160 | } |
| 3161 | |
| 3162 | // prepare a new state to push |
| 3163 | FastMixerStateQueue *sq = NULL; |
| 3164 | FastMixerState *state = NULL; |
| 3165 | bool didModify = false; |
| 3166 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3167 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3168 | sq = mFastMixer->sq(); |
| 3169 | state = sq->begin(); |
| 3170 | } |
| 3171 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3172 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3173 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3174 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3175 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3176 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3177 | if (t == 0) { |
| 3178 | continue; |
| 3179 | } |
| 3180 | |
| 3181 | // this const just means the local variable doesn't change |
| 3182 | Track* const track = t.get(); |
| 3183 | |
| 3184 | // process fast tracks |
| 3185 | if (track->isFastTrack()) { |
| 3186 | |
| 3187 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3188 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3189 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3190 | // The converse, of removing an (active) track and then creating a new track |
| 3191 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3192 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3193 | int j = track->mFastIndex; |
| 3194 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3195 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3196 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3197 | |
| 3198 | // Determine whether the track is currently in underrun condition, |
| 3199 | // and whether it had a recent underrun. |
| 3200 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3201 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3202 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3203 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3204 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3205 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3206 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3207 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3208 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3209 | track->mObservedUnderruns = underruns; |
| 3210 | // don't count underruns that occur while stopping or pausing |
| 3211 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3212 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3213 | recentUnderruns > 0) { |
| 3214 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3215 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | // This is similar to the state machine for normal tracks, |
| 3219 | // with a few modifications for fast tracks. |
| 3220 | bool isActive = true; |
| 3221 | switch (track->mState) { |
| 3222 | case TrackBase::STOPPING_1: |
| 3223 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3224 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3225 | track->mState = TrackBase::STOPPING_2; |
| 3226 | } |
| 3227 | break; |
| 3228 | case TrackBase::PAUSING: |
| 3229 | // ramp down is not yet implemented |
| 3230 | track->setPaused(); |
| 3231 | break; |
| 3232 | case TrackBase::RESUMING: |
| 3233 | // ramp up is not yet implemented |
| 3234 | track->mState = TrackBase::ACTIVE; |
| 3235 | break; |
| 3236 | case TrackBase::ACTIVE: |
| 3237 | if (recentFull > 0 || recentPartial > 0) { |
| 3238 | // track has provided at least some frames recently: reset retry count |
| 3239 | track->mRetryCount = kMaxTrackRetries; |
| 3240 | } |
| 3241 | if (recentUnderruns == 0) { |
| 3242 | // no recent underruns: stay active |
| 3243 | break; |
| 3244 | } |
| 3245 | // there has recently been an underrun of some kind |
| 3246 | if (track->sharedBuffer() == 0) { |
| 3247 | // were any of the recent underruns "empty" (no frames available)? |
| 3248 | if (recentEmpty == 0) { |
| 3249 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3250 | break; |
| 3251 | } |
| 3252 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3253 | if (--(track->mRetryCount) > 0) { |
| 3254 | break; |
| 3255 | } |
| 3256 | // indicate to client process that the track was disabled because of underrun; |
| 3257 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3258 | android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3259 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3260 | isActive = false; |
| 3261 | break; |
| 3262 | } |
| 3263 | // fall through |
| 3264 | case TrackBase::STOPPING_2: |
| 3265 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3266 | case TrackBase::STOPPED: |
| 3267 | case TrackBase::FLUSHED: // flush() while active |
| 3268 | // Check for presentation complete if track is inactive |
| 3269 | // We have consumed all the buffers of this track. |
| 3270 | // This would be incomplete if we auto-paused on underrun |
| 3271 | { |
| 3272 | size_t audioHALFrames = |
| 3273 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3274 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3275 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3276 | // track stays in active list until presentation is complete |
| 3277 | break; |
| 3278 | } |
| 3279 | } |
| 3280 | if (track->isStopping_2()) { |
| 3281 | track->mState = TrackBase::STOPPED; |
| 3282 | } |
| 3283 | if (track->isStopped()) { |
| 3284 | // Can't reset directly, as fast mixer is still polling this track |
| 3285 | // track->reset(); |
| 3286 | // So instead mark this track as needing to be reset after push with ack |
| 3287 | resetMask |= 1 << i; |
| 3288 | } |
| 3289 | isActive = false; |
| 3290 | break; |
| 3291 | case TrackBase::IDLE: |
| 3292 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3293 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | if (isActive) { |
| 3297 | // was it previously inactive? |
| 3298 | if (!(state->mTrackMask & (1 << j))) { |
| 3299 | ExtendedAudioBufferProvider *eabp = track; |
| 3300 | VolumeProvider *vp = track; |
| 3301 | fastTrack->mBufferProvider = eabp; |
| 3302 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3303 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3304 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3305 | fastTrack->mGeneration++; |
| 3306 | state->mTrackMask |= 1 << j; |
| 3307 | didModify = true; |
| 3308 | // no acknowledgement required for newly active tracks |
| 3309 | } |
| 3310 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3311 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3312 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3313 | ++fastTracks; |
| 3314 | } else { |
| 3315 | // was it previously active? |
| 3316 | if (state->mTrackMask & (1 << j)) { |
| 3317 | fastTrack->mBufferProvider = NULL; |
| 3318 | fastTrack->mGeneration++; |
| 3319 | state->mTrackMask &= ~(1 << j); |
| 3320 | didModify = true; |
| 3321 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3322 | // because we're about to decrement the last sp<> on those tracks. |
| 3323 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3324 | } else { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3325 | LOG_ALWAYS_FATAL("fast track %d should have been active", j); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3326 | } |
| 3327 | tracksToRemove->add(track); |
| 3328 | // Avoids a misleading display in dumpsys |
| 3329 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 3330 | } |
| 3331 | continue; |
| 3332 | } |
| 3333 | |
| 3334 | { // local variable scope to avoid goto warning |
| 3335 | |
| 3336 | audio_track_cblk_t* cblk = track->cblk(); |
| 3337 | |
| 3338 | // The first time a track is added we wait |
| 3339 | // for all its buffers to be filled before processing it |
| 3340 | int name = track->name(); |
| 3341 | // make sure that we have enough frames to mix one full buffer. |
| 3342 | // enforce this condition only once to enable draining the buffer in case the client |
| 3343 | // app does not call stop() and relies on underrun to stop: |
| 3344 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 3345 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3346 | size_t desiredFrames; |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3347 | uint32_t sr = track->sampleRate(); |
| 3348 | if (sr == mSampleRate) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3349 | desiredFrames = mNormalFrameCount; |
| 3350 | } else { |
| 3351 | // +1 for rounding and +1 for additional sample needed for interpolation |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3352 | desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3353 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 3354 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3355 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3356 | #if 0 |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3357 | // the minimum track buffer size is normally twice the number of frames necessary |
| 3358 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 3359 | // of unreleased frames after each pass, but just in case... |
| 3360 | ALOG_ASSERT(desiredFrames <= cblk->frameCount_); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3361 | #endif |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3362 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3363 | uint32_t minFrames = 1; |
| 3364 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 3365 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3366 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3367 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3368 | |
| 3369 | size_t framesReady = track->framesReady(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3370 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3371 | !track->isPaused() && !track->isTerminated()) |
| 3372 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3373 | 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] | 3374 | |
| 3375 | mixedTracks++; |
| 3376 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3377 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 3378 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3379 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3380 | if (track->mainBuffer() != mSinkBuffer && |
| 3381 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3382 | if (mEffectBufferEnabled) { |
| 3383 | mEffectBufferValid = true; // Later can set directly. |
| 3384 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3385 | chain = getEffectChain_l(track->sessionId()); |
| 3386 | // Delegate volume control to effect in track effect chain if needed |
| 3387 | if (chain != 0) { |
| 3388 | tracksWithEffect++; |
| 3389 | } else { |
| 3390 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 3391 | "session %d", |
| 3392 | name, track->sessionId()); |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | |
| 3397 | int param = AudioMixer::VOLUME; |
| 3398 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3399 | // no ramp for the first volume setting |
| 3400 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3401 | if (track->mState == TrackBase::RESUMING) { |
| 3402 | track->mState = TrackBase::ACTIVE; |
| 3403 | param = AudioMixer::RAMP_VOLUME; |
| 3404 | } |
| 3405 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3406 | // FIXME should not make a decision based on mServer |
| 3407 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3408 | // If the track is stopped before the first frame was mixed, |
| 3409 | // do not apply ramp |
| 3410 | param = AudioMixer::RAMP_VOLUME; |
| 3411 | } |
| 3412 | |
| 3413 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3414 | uint32_t vl, vr; // in U8.24 integer format |
| 3415 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3416 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3417 | vl = vr = 0; |
| 3418 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3419 | if (track->isPausing()) { |
| 3420 | track->setPaused(); |
| 3421 | } |
| 3422 | } else { |
| 3423 | |
| 3424 | // read original volumes with volume control |
| 3425 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3426 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3427 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3428 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3429 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3430 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3431 | // track volumes come from shared memory, so can't be trusted and must be clamped |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3432 | if (vlf > GAIN_FLOAT_UNITY) { |
| 3433 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 3434 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3435 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3436 | if (vrf > GAIN_FLOAT_UNITY) { |
| 3437 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 3438 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3439 | } |
| 3440 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3441 | vlf *= v; |
| 3442 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3443 | // assuming master volume and stream type volume each go up to 1.0, |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3444 | // then derive vl and vr as U8.24 versions for the effect chain |
| 3445 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 3446 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 3447 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 3448 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3449 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3450 | // send level comes from shared memory and so may be corrupt |
| 3451 | if (sendLevel > MAX_GAIN_INT) { |
| 3452 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 3453 | sendLevel = MAX_GAIN_INT; |
| 3454 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3455 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 3456 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3457 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3458 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3459 | // Delegate volume control to effect in track effect chain if needed |
| 3460 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 3461 | // Do not ramp volume if volume is controlled by effect |
| 3462 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 3463 | // Update remaining floating point volume levels |
| 3464 | vlf = (float)vl / (1 << 24); |
| 3465 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3466 | track->mHasVolumeController = true; |
| 3467 | } else { |
| 3468 | // force no volume ramp when volume controller was just disabled or removed |
| 3469 | // from effect chain to avoid volume spike |
| 3470 | if (track->mHasVolumeController) { |
| 3471 | param = AudioMixer::VOLUME; |
| 3472 | } |
| 3473 | track->mHasVolumeController = false; |
| 3474 | } |
| 3475 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3476 | // XXX: these things DON'T need to be done each time |
| 3477 | mAudioMixer->setBufferProvider(name, track); |
| 3478 | mAudioMixer->enable(name); |
| 3479 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3480 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 3481 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 3482 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3483 | mAudioMixer->setParameter( |
| 3484 | name, |
| 3485 | AudioMixer::TRACK, |
| 3486 | AudioMixer::FORMAT, (void *)track->format()); |
| 3487 | mAudioMixer->setParameter( |
| 3488 | name, |
| 3489 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3490 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3491 | mAudioMixer->setParameter( |
| 3492 | name, |
| 3493 | AudioMixer::TRACK, |
| 3494 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3495 | // limit track sample rate to 2 x output sample rate, which changes at re-configuration |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 3496 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3497 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3498 | if (reqSampleRate == 0) { |
| 3499 | reqSampleRate = mSampleRate; |
| 3500 | } else if (reqSampleRate > maxSampleRate) { |
| 3501 | reqSampleRate = maxSampleRate; |
| 3502 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3503 | mAudioMixer->setParameter( |
| 3504 | name, |
| 3505 | AudioMixer::RESAMPLE, |
| 3506 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3507 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3508 | /* |
| 3509 | * Select the appropriate output buffer for the track. |
| 3510 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3511 | * Tracks with effects go into their own effects chain buffer |
| 3512 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3513 | * |
| 3514 | * Other tracks can use mMixerBuffer for higher precision |
| 3515 | * channel accumulation. If this buffer is enabled |
| 3516 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 3517 | * into it. |
| 3518 | * |
| 3519 | */ |
| 3520 | if (mMixerBufferEnabled |
| 3521 | && (track->mainBuffer() == mSinkBuffer |
| 3522 | || track->mainBuffer() == mMixerBuffer)) { |
| 3523 | mAudioMixer->setParameter( |
| 3524 | name, |
| 3525 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3526 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3527 | mAudioMixer->setParameter( |
| 3528 | name, |
| 3529 | AudioMixer::TRACK, |
| 3530 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 3531 | // TODO: override track->mainBuffer()? |
| 3532 | mMixerBufferValid = true; |
| 3533 | } else { |
| 3534 | mAudioMixer->setParameter( |
| 3535 | name, |
| 3536 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3537 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3538 | mAudioMixer->setParameter( |
| 3539 | name, |
| 3540 | AudioMixer::TRACK, |
| 3541 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 3542 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3543 | mAudioMixer->setParameter( |
| 3544 | name, |
| 3545 | AudioMixer::TRACK, |
| 3546 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 3547 | |
| 3548 | // reset retry count |
| 3549 | track->mRetryCount = kMaxTrackRetries; |
| 3550 | |
| 3551 | // If one track is ready, set the mixer ready if: |
| 3552 | // - the mixer was not ready during previous round OR |
| 3553 | // - no other track is not ready |
| 3554 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 3555 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 3556 | mixerStatus = MIXER_TRACKS_READY; |
| 3557 | } |
| 3558 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3559 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3560 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3561 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3562 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 3563 | // previous audio buffer again to effects |
| 3564 | chain = getEffectChain_l(track->sessionId()); |
| 3565 | if (chain != 0) { |
| 3566 | chain->clearInputBuffer(); |
| 3567 | } |
| 3568 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3569 | 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] | 3570 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3571 | track->isStopped() || track->isPaused()) { |
| 3572 | // We have consumed all the buffers of this track. |
| 3573 | // Remove it from the list of active tracks. |
| 3574 | // TODO: use actual buffer filling status instead of latency when available from |
| 3575 | // audio HAL |
| 3576 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 3577 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3578 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3579 | if (track->isStopped()) { |
| 3580 | track->reset(); |
| 3581 | } |
| 3582 | tracksToRemove->add(track); |
| 3583 | } |
| 3584 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3585 | // No buffers for this track. Give it a few chances to |
| 3586 | // fill a buffer, then remove it from active list. |
| 3587 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 3588 | 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] | 3589 | tracksToRemove->add(track); |
| 3590 | // indicate to client process that the track was disabled because of underrun; |
| 3591 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3592 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3593 | // If one track is not ready, mark the mixer also not ready if: |
| 3594 | // - the mixer was ready during previous round OR |
| 3595 | // - no other track is ready |
| 3596 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 3597 | mixerStatus != MIXER_TRACKS_READY) { |
| 3598 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3599 | } |
| 3600 | } |
| 3601 | mAudioMixer->disable(name); |
| 3602 | } |
| 3603 | |
| 3604 | } // local variable scope to avoid goto warning |
| 3605 | track_is_ready: ; |
| 3606 | |
| 3607 | } |
| 3608 | |
| 3609 | // Push the new FastMixer state if necessary |
| 3610 | bool pauseAudioWatchdog = false; |
| 3611 | if (didModify) { |
| 3612 | state->mFastTracksGen++; |
| 3613 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3614 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3615 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3616 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3617 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3618 | state->mColdGen++; |
| 3619 | mFastMixerFutex = 0; |
| 3620 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3621 | mNormalSink = mOutputSink; |
| 3622 | } |
| 3623 | // If we go into cold idle, need to wait for acknowledgement |
| 3624 | // so that fast mixer stops doing I/O. |
| 3625 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3626 | pauseAudioWatchdog = true; |
| 3627 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3628 | } |
| 3629 | if (sq != NULL) { |
| 3630 | sq->end(didModify); |
| 3631 | sq->push(block); |
| 3632 | } |
| 3633 | #ifdef AUDIO_WATCHDOG |
| 3634 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 3635 | mAudioWatchdog->pause(); |
| 3636 | } |
| 3637 | #endif |
| 3638 | |
| 3639 | // Now perform the deferred reset on fast tracks that have stopped |
| 3640 | while (resetMask != 0) { |
| 3641 | size_t i = __builtin_ctz(resetMask); |
| 3642 | ALOG_ASSERT(i < count); |
| 3643 | resetMask &= ~(1 << i); |
| 3644 | sp<Track> t = mActiveTracks[i].promote(); |
| 3645 | if (t == 0) { |
| 3646 | continue; |
| 3647 | } |
| 3648 | Track* track = t.get(); |
| 3649 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3650 | track->reset(); |
| 3651 | } |
| 3652 | |
| 3653 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3654 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3655 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 3656 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 3657 | mEffectBufferValid = true; |
| 3658 | } |
| 3659 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3660 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 3661 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 3662 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3663 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3664 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3665 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3666 | if (mMixerBufferValid) { |
| 3667 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3668 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 3669 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 3670 | // after mixing. |
| 3671 | // |
| 3672 | // To enforce this guarantee: |
| 3673 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3674 | // (mixedTracks == 0 && fastTracks > 0)) |
| 3675 | // must imply MIXER_TRACKS_READY. |
| 3676 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 3677 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3678 | // TODO - either mEffectBuffer or mSinkBuffer needs to be cleared. |
| 3679 | if (mEffectBufferValid) { |
| 3680 | memset(mEffectBuffer, 0, mEffectBufferSize); |
| 3681 | } |
| 3682 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 3683 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | // if any fast tracks, then status is ready |
| 3687 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 3688 | if (fastTracks > 0) { |
| 3689 | mixerStatus = MIXER_TRACKS_READY; |
| 3690 | } |
| 3691 | return mixerStatus; |
| 3692 | } |
| 3693 | |
| 3694 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3695 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
| 3696 | audio_format_t format, int sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3697 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3698 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3702 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3703 | { |
| 3704 | ALOGV("remove track (%d) and delete from mixer", name); |
| 3705 | mAudioMixer->deleteTrackName(name); |
| 3706 | } |
| 3707 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3708 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 3709 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 3710 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3711 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3712 | bool reconfig = false; |
| 3713 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3714 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3715 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3716 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3717 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3718 | if (mFastMixer != 0) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3719 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3720 | FastMixerState *state = sq->begin(); |
| 3721 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3722 | previousCommand = state->mCommand; |
| 3723 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3724 | sq->end(); |
| 3725 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3726 | } else { |
| 3727 | sq->end(false /*didModify*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3728 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3729 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3730 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3731 | AudioParameter param = AudioParameter(keyValuePair); |
| 3732 | int value; |
| 3733 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3734 | reconfig = true; |
| 3735 | } |
| 3736 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3737 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3738 | status = BAD_VALUE; |
| 3739 | } else { |
| 3740 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3741 | reconfig = true; |
| 3742 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3743 | } |
| 3744 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3745 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3746 | status = BAD_VALUE; |
| 3747 | } else { |
| 3748 | // no need to save value, since it's constant |
| 3749 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3750 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3751 | } |
| 3752 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3753 | // do not accept frame count changes if tracks are open as the track buffer |
| 3754 | // size depends on frame count and correct behavior would not be guaranteed |
| 3755 | // if frame count is changed after track creation |
| 3756 | if (!mTracks.isEmpty()) { |
| 3757 | status = INVALID_OPERATION; |
| 3758 | } else { |
| 3759 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3760 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3761 | } |
| 3762 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3763 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3764 | // when changing the audio output device, call addBatteryData to notify |
| 3765 | // the change |
| 3766 | if (mOutDevice != value) { |
| 3767 | uint32_t params = 0; |
| 3768 | // check whether speaker is on |
| 3769 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3770 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3771 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3772 | |
| 3773 | audio_devices_t deviceWithoutSpeaker |
| 3774 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3775 | // check if any other device (except speaker) is on |
| 3776 | if (value & deviceWithoutSpeaker ) { |
| 3777 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3778 | } |
| 3779 | |
| 3780 | if (params != 0) { |
| 3781 | addBatteryData(params); |
| 3782 | } |
| 3783 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3784 | #endif |
| 3785 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3786 | // forward device change to effects that have requested to be |
| 3787 | // aware of attached audio device. |
| 3788 | if (value != AUDIO_DEVICE_NONE) { |
| 3789 | mOutDevice = value; |
| 3790 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3791 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3792 | } |
| 3793 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3794 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3795 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3796 | if (status == NO_ERROR) { |
| 3797 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3798 | keyValuePair.string()); |
| 3799 | if (!mStandby && status == INVALID_OPERATION) { |
| 3800 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3801 | mStandby = true; |
| 3802 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3803 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3804 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3805 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3806 | if (status == NO_ERROR && reconfig) { |
| 3807 | readOutputParameters_l(); |
| 3808 | delete mAudioMixer; |
| 3809 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3810 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3811 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 3812 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3813 | if (name < 0) { |
| 3814 | break; |
| 3815 | } |
| 3816 | mTracks[i]->mName = name; |
| 3817 | } |
| 3818 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3819 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | if (!(previousCommand & FastMixerState::IDLE)) { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3823 | ALOG_ASSERT(mFastMixer != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3824 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3825 | FastMixerState *state = sq->begin(); |
| 3826 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3827 | state->mCommand = previousCommand; |
| 3828 | sq->end(); |
| 3829 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3830 | } |
| 3831 | |
| 3832 | return reconfig; |
| 3833 | } |
| 3834 | |
| 3835 | |
| 3836 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 3837 | { |
| 3838 | const size_t SIZE = 256; |
| 3839 | char buffer[SIZE]; |
| 3840 | String8 result; |
| 3841 | |
| 3842 | PlaybackThread::dumpInternals(fd, args); |
| 3843 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 3844 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3845 | |
| 3846 | // 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] | 3847 | const FastMixerDumpState copy(mFastMixerDumpState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3848 | copy.dump(fd); |
| 3849 | |
| 3850 | #ifdef STATE_QUEUE_DUMP |
| 3851 | // Similar for state queue |
| 3852 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 3853 | observerCopy.dump(fd); |
| 3854 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 3855 | mutatorCopy.dump(fd); |
| 3856 | #endif |
| 3857 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3858 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3859 | // Write the tee output to a .wav file |
| 3860 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3861 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3862 | |
| 3863 | #ifdef AUDIO_WATCHDOG |
| 3864 | if (mAudioWatchdog != 0) { |
| 3865 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 3866 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 3867 | wdCopy.dump(fd); |
| 3868 | } |
| 3869 | #endif |
| 3870 | } |
| 3871 | |
| 3872 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 3873 | { |
| 3874 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 3875 | } |
| 3876 | |
| 3877 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 3878 | { |
| 3879 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 3880 | } |
| 3881 | |
| 3882 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 3883 | { |
| 3884 | PlaybackThread::cacheParameters_l(); |
| 3885 | |
| 3886 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 3887 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 3888 | // increase threshold again due to low power audio mode. The way this warning |
| 3889 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 3890 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 3891 | } |
| 3892 | |
| 3893 | // ---------------------------------------------------------------------------- |
| 3894 | |
| 3895 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3896 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) |
| 3897 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
| 3898 | // mLeftVolFloat, mRightVolFloat |
| 3899 | { |
| 3900 | } |
| 3901 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3902 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3903 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
| 3904 | ThreadBase::type_t type) |
| 3905 | : PlaybackThread(audioFlinger, output, id, device, type) |
| 3906 | // mLeftVolFloat, mRightVolFloat |
| 3907 | { |
| 3908 | } |
| 3909 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3910 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 3911 | { |
| 3912 | } |
| 3913 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3914 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 3915 | { |
| 3916 | audio_track_cblk_t* cblk = track->cblk(); |
| 3917 | float left, right; |
| 3918 | |
| 3919 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 3920 | left = right = 0; |
| 3921 | } else { |
| 3922 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3923 | float v = mMasterVolume * typeVolume; |
| 3924 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3925 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 3926 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3927 | if (left > GAIN_FLOAT_UNITY) { |
| 3928 | left = GAIN_FLOAT_UNITY; |
| 3929 | } |
| 3930 | left *= v; |
| 3931 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 3932 | if (right > GAIN_FLOAT_UNITY) { |
| 3933 | right = GAIN_FLOAT_UNITY; |
| 3934 | } |
| 3935 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | if (lastTrack) { |
| 3939 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 3940 | mLeftVolFloat = left; |
| 3941 | mRightVolFloat = right; |
| 3942 | |
| 3943 | // Convert volumes from float to 8.24 |
| 3944 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 3945 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 3946 | |
| 3947 | // Delegate volume control to effect in track effect chain if needed |
| 3948 | // only one effect chain can be present on DirectOutputThread, so if |
| 3949 | // there is one, the track is connected to it |
| 3950 | if (!mEffectChains.isEmpty()) { |
| 3951 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 3952 | left = (float)vl / (1 << 24); |
| 3953 | right = (float)vr / (1 << 24); |
| 3954 | } |
| 3955 | if (mOutput->stream->set_volume) { |
| 3956 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3963 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 3964 | Vector< sp<Track> > *tracksToRemove |
| 3965 | ) |
| 3966 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3967 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3968 | mixer_state mixerStatus = MIXER_IDLE; |
| 3969 | |
| 3970 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3971 | for (size_t i = 0; i < count; i++) { |
| 3972 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3973 | // The track died recently |
| 3974 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3975 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | Track* const track = t.get(); |
| 3979 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 3980 | // Only consider last track started for volume and mixer state control. |
| 3981 | // In theory an older track could underrun and restart after the new one starts |
| 3982 | // but as we only care about the transition phase between two tracks on a |
| 3983 | // direct output, it is not a problem to ignore the underrun case. |
| 3984 | sp<Track> l = mLatestActiveTrack.promote(); |
| 3985 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3986 | |
| 3987 | // The first time a track is added we wait |
| 3988 | // for all its buffers to be filled before processing it |
| 3989 | uint32_t minFrames; |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3990 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3991 | minFrames = mNormalFrameCount; |
| 3992 | } else { |
| 3993 | minFrames = 1; |
| 3994 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3995 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3996 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 3997 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3998 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3999 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4000 | |
| 4001 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4002 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4003 | // make sure processVolume_l() will apply new volume even if 0 |
| 4004 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4005 | if (track->mState == TrackBase::RESUMING) { |
| 4006 | track->mState = TrackBase::ACTIVE; |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4011 | processVolume_l(track, last); |
| 4012 | if (last) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4013 | // reset retry count |
| 4014 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4015 | mActiveTrack = t; |
| 4016 | mixerStatus = MIXER_TRACKS_READY; |
| 4017 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4018 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4019 | // clear effect chain input buffer if the last active track started underruns |
| 4020 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4021 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4022 | mEffectChains[0]->clearInputBuffer(); |
| 4023 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4024 | if (track->isStopping_1()) { |
| 4025 | track->mState = TrackBase::STOPPING_2; |
| 4026 | } |
| 4027 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4028 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4029 | // We have consumed all the buffers of this track. |
| 4030 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4031 | size_t audioHALFrames; |
| 4032 | if (audio_is_linear_pcm(mFormat)) { |
| 4033 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4034 | } else { |
| 4035 | audioHALFrames = 0; |
| 4036 | } |
| 4037 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4038 | size_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4039 | if (mStandby || !last || |
| 4040 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4041 | if (track->isStopping_2()) { |
| 4042 | track->mState = TrackBase::STOPPED; |
| 4043 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4044 | if (track->isStopped()) { |
| 4045 | track->reset(); |
| 4046 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4047 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4048 | } |
| 4049 | } else { |
| 4050 | // No buffers for this track. Give it a few chances to |
| 4051 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4052 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4053 | if (--(track->mRetryCount) <= 0) { |
| 4054 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4055 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4056 | // indicate to client process that the track was disabled because of underrun; |
| 4057 | // it will then automatically call start() when data is available |
| 4058 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4059 | } else if (last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4060 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4061 | } |
| 4062 | } |
| 4063 | } |
| 4064 | } |
| 4065 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4066 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4067 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4068 | |
| 4069 | return mixerStatus; |
| 4070 | } |
| 4071 | |
| 4072 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4073 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4074 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4075 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4076 | // output audio to hardware |
| 4077 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4078 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4079 | buffer.frameCount = frameCount; |
| 4080 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | fa319e6 | 2013-07-29 17:17:38 -0700 | [diff] [blame] | 4081 | if (buffer.raw == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4082 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4083 | break; |
| 4084 | } |
| 4085 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4086 | frameCount -= buffer.frameCount; |
| 4087 | curBuf += buffer.frameCount * mFrameSize; |
| 4088 | mActiveTrack->releaseBuffer(&buffer); |
| 4089 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4090 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4091 | sleepTime = 0; |
| 4092 | standbyTime = systemTime() + standbyDelay; |
| 4093 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4097 | { |
| 4098 | if (sleepTime == 0) { |
| 4099 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4100 | sleepTime = activeSleepTime; |
| 4101 | } else { |
| 4102 | sleepTime = idleSleepTime; |
| 4103 | } |
| 4104 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4105 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4106 | sleepTime = 0; |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4111 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4112 | audio_format_t format __unused, int sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4113 | { |
| 4114 | return 0; |
| 4115 | } |
| 4116 | |
| 4117 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4118 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4119 | { |
| 4120 | } |
| 4121 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4122 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4123 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4124 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4125 | { |
| 4126 | bool reconfig = false; |
| 4127 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4128 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4129 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4130 | AudioParameter param = AudioParameter(keyValuePair); |
| 4131 | int value; |
| 4132 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4133 | // forward device change to effects that have requested to be |
| 4134 | // aware of attached audio device. |
| 4135 | if (value != AUDIO_DEVICE_NONE) { |
| 4136 | mOutDevice = value; |
| 4137 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4138 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4139 | } |
| 4140 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4141 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4142 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4143 | // do not accept frame count changes if tracks are open as the track buffer |
| 4144 | // size depends on frame count and correct behavior would not be garantied |
| 4145 | // if frame count is changed after track creation |
| 4146 | if (!mTracks.isEmpty()) { |
| 4147 | status = INVALID_OPERATION; |
| 4148 | } else { |
| 4149 | reconfig = true; |
| 4150 | } |
| 4151 | } |
| 4152 | if (status == NO_ERROR) { |
| 4153 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4154 | keyValuePair.string()); |
| 4155 | if (!mStandby && status == INVALID_OPERATION) { |
| 4156 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 4157 | mStandby = true; |
| 4158 | mBytesWritten = 0; |
| 4159 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4160 | keyValuePair.string()); |
| 4161 | } |
| 4162 | if (status == NO_ERROR && reconfig) { |
| 4163 | readOutputParameters_l(); |
| 4164 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 4165 | } |
| 4166 | } |
| 4167 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4168 | return reconfig; |
| 4169 | } |
| 4170 | |
| 4171 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 4172 | { |
| 4173 | uint32_t time; |
| 4174 | if (audio_is_linear_pcm(mFormat)) { |
| 4175 | time = PlaybackThread::activeSleepTimeUs(); |
| 4176 | } else { |
| 4177 | time = 10000; |
| 4178 | } |
| 4179 | return time; |
| 4180 | } |
| 4181 | |
| 4182 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 4183 | { |
| 4184 | uint32_t time; |
| 4185 | if (audio_is_linear_pcm(mFormat)) { |
| 4186 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4187 | } else { |
| 4188 | time = 10000; |
| 4189 | } |
| 4190 | return time; |
| 4191 | } |
| 4192 | |
| 4193 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 4194 | { |
| 4195 | uint32_t time; |
| 4196 | if (audio_is_linear_pcm(mFormat)) { |
| 4197 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 4198 | } else { |
| 4199 | time = 10000; |
| 4200 | } |
| 4201 | return time; |
| 4202 | } |
| 4203 | |
| 4204 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 4205 | { |
| 4206 | PlaybackThread::cacheParameters_l(); |
| 4207 | |
| 4208 | // use shorter standby delay as on normal output to release |
| 4209 | // hardware resources as soon as possible |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4210 | if (audio_is_linear_pcm(mFormat)) { |
| 4211 | standbyDelay = microseconds(activeSleepTime*2); |
| 4212 | } else { |
| 4213 | standbyDelay = kOffloadStandbyDelayNs; |
| 4214 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | // ---------------------------------------------------------------------------- |
| 4218 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4219 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4220 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4221 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4222 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4223 | mWriteAckSequence(0), |
| 4224 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4225 | { |
| 4226 | } |
| 4227 | |
| 4228 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 4229 | { |
| 4230 | } |
| 4231 | |
| 4232 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 4233 | { |
| 4234 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4235 | } |
| 4236 | |
| 4237 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 4238 | { |
| 4239 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4240 | uint32_t writeAckSequence; |
| 4241 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4242 | |
| 4243 | { |
| 4244 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4245 | while (!((mWriteAckSequence & 1) || |
| 4246 | (mDrainSequence & 1) || |
| 4247 | exitPending())) { |
| 4248 | mWaitWorkCV.wait(mLock); |
| 4249 | } |
| 4250 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4251 | if (exitPending()) { |
| 4252 | break; |
| 4253 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4254 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 4255 | mWriteAckSequence, mDrainSequence); |
| 4256 | writeAckSequence = mWriteAckSequence; |
| 4257 | mWriteAckSequence &= ~1; |
| 4258 | drainSequence = mDrainSequence; |
| 4259 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4260 | } |
| 4261 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4262 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 4263 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4264 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4265 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4266 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4267 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4268 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4269 | } |
| 4270 | } |
| 4271 | } |
| 4272 | } |
| 4273 | return false; |
| 4274 | } |
| 4275 | |
| 4276 | void AudioFlinger::AsyncCallbackThread::exit() |
| 4277 | { |
| 4278 | ALOGV("AsyncCallbackThread::exit"); |
| 4279 | Mutex::Autolock _l(mLock); |
| 4280 | requestExit(); |
| 4281 | mWaitWorkCV.broadcast(); |
| 4282 | } |
| 4283 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4284 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4285 | { |
| 4286 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4287 | // bit 0 is cleared |
| 4288 | mWriteAckSequence = sequence << 1; |
| 4289 | } |
| 4290 | |
| 4291 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 4292 | { |
| 4293 | Mutex::Autolock _l(mLock); |
| 4294 | // ignore unexpected callbacks |
| 4295 | if (mWriteAckSequence & 2) { |
| 4296 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4297 | mWaitWorkCV.signal(); |
| 4298 | } |
| 4299 | } |
| 4300 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4301 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4302 | { |
| 4303 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4304 | // bit 0 is cleared |
| 4305 | mDrainSequence = sequence << 1; |
| 4306 | } |
| 4307 | |
| 4308 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 4309 | { |
| 4310 | Mutex::Autolock _l(mLock); |
| 4311 | // ignore unexpected callbacks |
| 4312 | if (mDrainSequence & 2) { |
| 4313 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4314 | mWaitWorkCV.signal(); |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | |
| 4319 | // ---------------------------------------------------------------------------- |
| 4320 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
| 4321 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
| 4322 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), |
| 4323 | mHwPaused(false), |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 4324 | mFlushPending(false), |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4325 | mPausedBytesRemaining(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4326 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4327 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 4328 | mStandby = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4329 | } |
| 4330 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4331 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 4332 | { |
| 4333 | if (mFlushPending || mHwPaused) { |
| 4334 | // If a flush is pending or track was paused, just discard buffered data |
| 4335 | flushHw_l(); |
| 4336 | } else { |
| 4337 | mMixerStatus = MIXER_DRAIN_ALL; |
| 4338 | threadLoop_drain(); |
| 4339 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 4340 | if (mUseAsyncWrite) { |
| 4341 | ALOG_ASSERT(mCallbackThread != 0); |
| 4342 | mCallbackThread->exit(); |
| 4343 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4344 | PlaybackThread::threadLoop_exit(); |
| 4345 | } |
| 4346 | |
| 4347 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 4348 | Vector< sp<Track> > *tracksToRemove |
| 4349 | ) |
| 4350 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4351 | size_t count = mActiveTracks.size(); |
| 4352 | |
| 4353 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4354 | bool doHwPause = false; |
| 4355 | bool doHwResume = false; |
| 4356 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4357 | ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); |
| 4358 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4359 | // find out which tracks need to be processed |
| 4360 | for (size_t i = 0; i < count; i++) { |
| 4361 | sp<Track> t = mActiveTracks[i].promote(); |
| 4362 | // The track died recently |
| 4363 | if (t == 0) { |
| 4364 | continue; |
| 4365 | } |
| 4366 | Track* const track = t.get(); |
| 4367 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4368 | // Only consider last track started for volume and mixer state control. |
| 4369 | // In theory an older track could underrun and restart after the new one starts |
| 4370 | // but as we only care about the transition phase between two tracks on a |
| 4371 | // direct output, it is not a problem to ignore the underrun case. |
| 4372 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4373 | bool last = l.get() == track; |
| 4374 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4375 | if (track->isInvalid()) { |
| 4376 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4377 | tracksToRemove->add(track); |
| 4378 | continue; |
| 4379 | } |
| 4380 | |
| 4381 | if (track->mState == TrackBase::IDLE) { |
| 4382 | ALOGW("An idle track shouldn't be in active list"); |
| 4383 | continue; |
| 4384 | } |
| 4385 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4386 | if (track->isPausing()) { |
| 4387 | track->setPaused(); |
| 4388 | if (last) { |
| 4389 | if (!mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4390 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4391 | mHwPaused = true; |
| 4392 | } |
| 4393 | // If we were part way through writing the mixbuffer to |
| 4394 | // the HAL we must save this until we resume |
| 4395 | // BUG - this will be wrong if a different track is made active, |
| 4396 | // in that case we want to discard the pending data in the |
| 4397 | // mixbuffer and tell the client to present it again when the |
| 4398 | // track is resumed |
| 4399 | mPausedWriteLength = mCurrentWriteLength; |
| 4400 | mPausedBytesRemaining = mBytesRemaining; |
| 4401 | mBytesRemaining = 0; // stop writing |
| 4402 | } |
| 4403 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4404 | } else if (track->isFlushPending()) { |
| 4405 | track->flushAck(); |
| 4406 | if (last) { |
| 4407 | mFlushPending = true; |
| 4408 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 4409 | } else if (track->isResumePending()){ |
| 4410 | track->resumeAck(); |
| 4411 | if (last) { |
| 4412 | if (mPausedBytesRemaining) { |
| 4413 | // Need to continue write that was interrupted |
| 4414 | mCurrentWriteLength = mPausedWriteLength; |
| 4415 | mBytesRemaining = mPausedBytesRemaining; |
| 4416 | mPausedBytesRemaining = 0; |
| 4417 | } |
| 4418 | if (mHwPaused) { |
| 4419 | doHwResume = true; |
| 4420 | mHwPaused = false; |
| 4421 | // threadLoop_mix() will handle the case that we need to |
| 4422 | // resume an interrupted write |
| 4423 | } |
| 4424 | // enable write to audio HAL |
| 4425 | sleepTime = 0; |
| 4426 | |
| 4427 | // Do not handle new data in this iteration even if track->framesReady() |
| 4428 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4429 | } |
| 4430 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4431 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4432 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4433 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4434 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4435 | // make sure processVolume_l() will apply new volume even if 0 |
| 4436 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4440 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4441 | if (previousTrack != 0) { |
| 4442 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4443 | // Flush any data still being written from last track |
| 4444 | mBytesRemaining = 0; |
| 4445 | if (mPausedBytesRemaining) { |
| 4446 | // Last track was paused so we also need to flush saved |
| 4447 | // mixbuffer state and invalidate track so that it will |
| 4448 | // re-submit that unwritten data when it is next resumed |
| 4449 | mPausedBytesRemaining = 0; |
| 4450 | // Invalidate is a bit drastic - would be more efficient |
| 4451 | // to have a flag to tell client that some of the |
| 4452 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4453 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4454 | } |
| 4455 | // flush data already sent to the DSP if changing audio session as audio |
| 4456 | // comes from a different source. Also invalidate previous track to force a |
| 4457 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4458 | if (previousTrack->sessionId() != track->sessionId()) { |
| 4459 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | } |
| 4463 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4464 | // reset retry count |
| 4465 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 4466 | mActiveTrack = t; |
| 4467 | mixerStatus = MIXER_TRACKS_READY; |
| 4468 | } |
| 4469 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4470 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4471 | if (track->isStopping_1()) { |
| 4472 | // Hardware buffer can hold a large amount of audio so we must |
| 4473 | // wait for all current track's data to drain before we say |
| 4474 | // that the track is stopped. |
| 4475 | if (mBytesRemaining == 0) { |
| 4476 | // Only start draining when all data in mixbuffer |
| 4477 | // has been written |
| 4478 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 4479 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4480 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 4481 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4482 | // do not modify drain sequence if we are already draining. This happens |
| 4483 | // when resuming from pause after drain. |
| 4484 | if ((mDrainSequence & 1) == 0) { |
| 4485 | sleepTime = 0; |
| 4486 | standbyTime = systemTime() + standbyDelay; |
| 4487 | mixerStatus = MIXER_DRAIN_TRACK; |
| 4488 | mDrainSequence += 2; |
| 4489 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4490 | if (mHwPaused) { |
| 4491 | // It is possible to move from PAUSED to STOPPING_1 without |
| 4492 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4493 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4494 | mHwPaused = false; |
| 4495 | } |
| 4496 | } |
| 4497 | } |
| 4498 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4499 | // Drain has completed or we are in standby, signal presentation complete |
| 4500 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4501 | track->mState = TrackBase::STOPPED; |
| 4502 | size_t audioHALFrames = |
| 4503 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 4504 | size_t framesWritten = |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 4505 | mBytesWritten / audio_stream_out_frame_size(mOutput->stream); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4506 | track->presentationComplete(framesWritten, audioHALFrames); |
| 4507 | track->reset(); |
| 4508 | tracksToRemove->add(track); |
| 4509 | } |
| 4510 | } else { |
| 4511 | // No buffers for this track. Give it a few chances to |
| 4512 | // fill a buffer, then remove it from active list. |
| 4513 | if (--(track->mRetryCount) <= 0) { |
| 4514 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 4515 | track->name()); |
| 4516 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4517 | // indicate to client process that the track was disabled because of underrun; |
| 4518 | // it will then automatically call start() when data is available |
| 4519 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4520 | } else if (last){ |
| 4521 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4522 | } |
| 4523 | } |
| 4524 | } |
| 4525 | // compute volume for this track |
| 4526 | processVolume_l(track, last); |
| 4527 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4528 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 4529 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4530 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4531 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4532 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4533 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4534 | mOutput->stream->pause(mOutput->stream); |
| 4535 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4536 | if (mFlushPending) { |
| 4537 | flushHw_l(); |
| 4538 | mFlushPending = false; |
| 4539 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4540 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4541 | mOutput->stream->resume(mOutput->stream); |
| 4542 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4543 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4544 | // remove all the tracks that need to be... |
| 4545 | removeTracks_l(*tracksToRemove); |
| 4546 | |
| 4547 | return mixerStatus; |
| 4548 | } |
| 4549 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4550 | // must be called with thread mutex locked |
| 4551 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 4552 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4553 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 4554 | mWriteAckSequence, mDrainSequence); |
| 4555 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4556 | return true; |
| 4557 | } |
| 4558 | return false; |
| 4559 | } |
| 4560 | |
| 4561 | // must be called with thread mutex locked |
| 4562 | bool AudioFlinger::OffloadThread::shouldStandby_l() |
| 4563 | { |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4564 | bool trackPaused = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4565 | |
| 4566 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4567 | // after a timeout and we will enter standby then. |
| 4568 | if (mTracks.size() > 0) { |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4569 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4570 | } |
| 4571 | |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4572 | return !mStandby && !trackPaused; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | |
| 4576 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 4577 | { |
| 4578 | Mutex::Autolock _l(mLock); |
| 4579 | return waitingAsyncCallback_l(); |
| 4580 | } |
| 4581 | |
| 4582 | void AudioFlinger::OffloadThread::flushHw_l() |
| 4583 | { |
| 4584 | mOutput->stream->flush(mOutput->stream); |
| 4585 | // Flush anything still waiting in the mixbuffer |
| 4586 | mCurrentWriteLength = 0; |
| 4587 | mBytesRemaining = 0; |
| 4588 | mPausedWriteLength = 0; |
| 4589 | mPausedBytesRemaining = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 4590 | mHwPaused = false; |
| 4591 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4592 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4593 | // discard any pending drain or write ack by incrementing sequence |
| 4594 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4595 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4596 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4597 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4598 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4602 | void AudioFlinger::OffloadThread::onAddNewTrack_l() |
| 4603 | { |
| 4604 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4605 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4606 | |
| 4607 | if (previousTrack != 0 && latestTrack != 0 && |
| 4608 | (previousTrack->sessionId() != latestTrack->sessionId())) { |
| 4609 | mFlushPending = true; |
| 4610 | } |
| 4611 | PlaybackThread::onAddNewTrack_l(); |
| 4612 | } |
| 4613 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4614 | // ---------------------------------------------------------------------------- |
| 4615 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4616 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
| 4617 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
| 4618 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
| 4619 | DUPLICATING), |
| 4620 | mWaitTimeMs(UINT_MAX) |
| 4621 | { |
| 4622 | addOutputTrack(mainThread); |
| 4623 | } |
| 4624 | |
| 4625 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 4626 | { |
| 4627 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4628 | mOutputTracks[i]->destroy(); |
| 4629 | } |
| 4630 | } |
| 4631 | |
| 4632 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 4633 | { |
| 4634 | // mix buffers... |
| 4635 | if (outputsReady(outputTracks)) { |
| 4636 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 4637 | } else { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4638 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4639 | } |
| 4640 | sleepTime = 0; |
| 4641 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4642 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4643 | standbyTime = systemTime() + standbyDelay; |
| 4644 | } |
| 4645 | |
| 4646 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 4647 | { |
| 4648 | if (sleepTime == 0) { |
| 4649 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4650 | sleepTime = activeSleepTime; |
| 4651 | } else { |
| 4652 | sleepTime = idleSleepTime; |
| 4653 | } |
| 4654 | } else if (mBytesWritten != 0) { |
| 4655 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4656 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4657 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4658 | } else { |
| 4659 | // flush remaining overflow buffers in output tracks |
| 4660 | writeFrames = 0; |
| 4661 | } |
| 4662 | sleepTime = 0; |
| 4663 | } |
| 4664 | } |
| 4665 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4666 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4667 | { |
| 4668 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4669 | // We convert the duplicating thread format to AUDIO_FORMAT_PCM_16_BIT |
| 4670 | // for delivery downstream as needed. This in-place conversion is safe as |
| 4671 | // AUDIO_FORMAT_PCM_16_BIT is smaller than any other supported format |
| 4672 | // (AUDIO_FORMAT_PCM_8_BIT is not allowed here). |
| 4673 | if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
| 4674 | memcpy_by_audio_format(mSinkBuffer, AUDIO_FORMAT_PCM_16_BIT, |
| 4675 | mSinkBuffer, mFormat, writeFrames * mChannelCount); |
| 4676 | } |
| 4677 | outputTracks[i]->write(reinterpret_cast<int16_t*>(mSinkBuffer), writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4678 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 4679 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4680 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 4684 | { |
| 4685 | // DuplicatingThread implements standby by stopping all tracks |
| 4686 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 4687 | outputTracks[i]->stop(); |
| 4688 | } |
| 4689 | } |
| 4690 | |
| 4691 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 4692 | { |
| 4693 | outputTracks = mOutputTracks; |
| 4694 | } |
| 4695 | |
| 4696 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 4697 | { |
| 4698 | outputTracks.clear(); |
| 4699 | } |
| 4700 | |
| 4701 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 4702 | { |
| 4703 | Mutex::Autolock _l(mLock); |
| 4704 | // FIXME explain this formula |
| 4705 | size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4706 | // OutputTrack is forced to AUDIO_FORMAT_PCM_16_BIT regardless of mFormat |
| 4707 | // due to current usage case and restrictions on the AudioBufferProvider. |
| 4708 | // Actual buffer conversion is done in threadLoop_write(). |
| 4709 | // |
| 4710 | // TODO: This may change in the future, depending on multichannel |
| 4711 | // (and non int16_t*) support on AF::PlaybackThread::OutputTrack |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4712 | OutputTrack *outputTrack = new OutputTrack(thread, |
| 4713 | this, |
| 4714 | mSampleRate, |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4715 | AUDIO_FORMAT_PCM_16_BIT, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4716 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 4717 | frameCount, |
| 4718 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4719 | if (outputTrack->cblk() != NULL) { |
| 4720 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
| 4721 | mOutputTracks.add(outputTrack); |
| 4722 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
| 4723 | updateWaitTime_l(); |
| 4724 | } |
| 4725 | } |
| 4726 | |
| 4727 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 4728 | { |
| 4729 | Mutex::Autolock _l(mLock); |
| 4730 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4731 | if (mOutputTracks[i]->thread() == thread) { |
| 4732 | mOutputTracks[i]->destroy(); |
| 4733 | mOutputTracks.removeAt(i); |
| 4734 | updateWaitTime_l(); |
| 4735 | return; |
| 4736 | } |
| 4737 | } |
| 4738 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
| 4739 | } |
| 4740 | |
| 4741 | // caller must hold mLock |
| 4742 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 4743 | { |
| 4744 | mWaitTimeMs = UINT_MAX; |
| 4745 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4746 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 4747 | if (strong != 0) { |
| 4748 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 4749 | if (waitTimeMs < mWaitTimeMs) { |
| 4750 | mWaitTimeMs = waitTimeMs; |
| 4751 | } |
| 4752 | } |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | |
| 4757 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 4758 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 4759 | { |
| 4760 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 4761 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 4762 | if (thread == 0) { |
| 4763 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 4764 | outputTracks[i].get()); |
| 4765 | return false; |
| 4766 | } |
| 4767 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4768 | // see note at standby() declaration |
| 4769 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 4770 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 4771 | thread.get()); |
| 4772 | return false; |
| 4773 | } |
| 4774 | } |
| 4775 | return true; |
| 4776 | } |
| 4777 | |
| 4778 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 4779 | { |
| 4780 | return (mWaitTimeMs * 1000) / 2; |
| 4781 | } |
| 4782 | |
| 4783 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 4784 | { |
| 4785 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 4786 | updateWaitTime_l(); |
| 4787 | |
| 4788 | MixerThread::cacheParameters_l(); |
| 4789 | } |
| 4790 | |
| 4791 | // ---------------------------------------------------------------------------- |
| 4792 | // Record |
| 4793 | // ---------------------------------------------------------------------------- |
| 4794 | |
| 4795 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4796 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4797 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 4798 | audio_devices_t outDevice, |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4799 | audio_devices_t inDevice |
| 4800 | #ifdef TEE_SINK |
| 4801 | , const sp<NBAIO_Sink>& teeSink |
| 4802 | #endif |
| 4803 | ) : |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 4804 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 4805 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 4806 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 4807 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4808 | #ifdef TEE_SINK |
| 4809 | , mTeeSink(teeSink) |
| 4810 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 4811 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 4812 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4813 | // mFastCapture below |
| 4814 | , mFastCaptureFutex(0) |
| 4815 | // mInputSource |
| 4816 | // mPipeSink |
| 4817 | // mPipeSource |
| 4818 | , mPipeFramesP2(0) |
| 4819 | // mPipeMemory |
| 4820 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 4821 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4822 | { |
| 4823 | snprintf(mName, kNameLength, "AudioIn_%X", id); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 4824 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4825 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 4826 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4827 | |
| 4828 | // create an NBAIO source for the HAL input stream, and negotiate |
| 4829 | mInputSource = new AudioStreamInSource(input->stream); |
| 4830 | size_t numCounterOffers = 0; |
| 4831 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
| 4832 | ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 4833 | ALOG_ASSERT(index == 0); |
| 4834 | |
| 4835 | // initialize fast capture depending on configuration |
| 4836 | bool initFastCapture; |
| 4837 | switch (kUseFastCapture) { |
| 4838 | case FastCapture_Never: |
| 4839 | initFastCapture = false; |
| 4840 | break; |
| 4841 | case FastCapture_Always: |
| 4842 | initFastCapture = true; |
| 4843 | break; |
| 4844 | case FastCapture_Static: |
| 4845 | uint32_t primaryOutputSampleRate; |
| 4846 | { |
| 4847 | AutoMutex _l(audioFlinger->mHardwareLock); |
| 4848 | primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; |
| 4849 | } |
| 4850 | initFastCapture = |
| 4851 | // either capture sample rate is same as (a reasonable) primary output sample rate |
| 4852 | (((primaryOutputSampleRate == 44100 || primaryOutputSampleRate == 48000) && |
| 4853 | (mSampleRate == primaryOutputSampleRate)) || |
| 4854 | // or primary output sample rate is unknown, and capture sample rate is reasonable |
| 4855 | ((primaryOutputSampleRate == 0) && |
| 4856 | ((mSampleRate == 44100 || mSampleRate == 48000)))) && |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 4857 | // and the buffer size is < 12 ms |
| 4858 | (mFrameCount * 1000) / mSampleRate < 12; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4859 | break; |
| 4860 | // case FastCapture_Dynamic: |
| 4861 | } |
| 4862 | |
| 4863 | if (initFastCapture) { |
| 4864 | // create a Pipe for FastMixer to write to, and for us and fast tracks to read from |
| 4865 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 4866 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4867 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 4868 | void *pipeBuffer; |
| 4869 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 4870 | sp<IMemory> pipeMemory; |
| 4871 | if ((roHeap == 0) || |
| 4872 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 4873 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 4874 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 4875 | goto failed; |
| 4876 | } |
| 4877 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 4878 | memset(pipeBuffer, 0, pipeSize); |
| 4879 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 4880 | const NBAIO_Format offers[1] = {format}; |
| 4881 | size_t numCounterOffers = 0; |
| 4882 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 4883 | ALOG_ASSERT(index == 0); |
| 4884 | mPipeSink = pipe; |
| 4885 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 4886 | numCounterOffers = 0; |
| 4887 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 4888 | ALOG_ASSERT(index == 0); |
| 4889 | mPipeSource = pipeReader; |
| 4890 | mPipeFramesP2 = pipeFramesP2; |
| 4891 | mPipeMemory = pipeMemory; |
| 4892 | |
| 4893 | // create fast capture |
| 4894 | mFastCapture = new FastCapture(); |
| 4895 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 4896 | #ifdef STATE_QUEUE_DUMP |
| 4897 | // FIXME |
| 4898 | #endif |
| 4899 | FastCaptureState *state = sq->begin(); |
| 4900 | state->mCblk = NULL; |
| 4901 | state->mInputSource = mInputSource.get(); |
| 4902 | state->mInputSourceGen++; |
| 4903 | state->mPipeSink = pipe; |
| 4904 | state->mPipeSinkGen++; |
| 4905 | state->mFrameCount = mFrameCount; |
| 4906 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 4907 | // already done in constructor initialization list |
| 4908 | //mFastCaptureFutex = 0; |
| 4909 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 4910 | state->mColdGen++; |
| 4911 | state->mDumpState = &mFastCaptureDumpState; |
| 4912 | #ifdef TEE_SINK |
| 4913 | // FIXME |
| 4914 | #endif |
| 4915 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 4916 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 4917 | sq->end(); |
| 4918 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 4919 | |
| 4920 | // start the fast capture |
| 4921 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4922 | pid_t tid = mFastCapture->getTid(); |
| 4923 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 4924 | if (err != 0) { |
| 4925 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 4926 | kPriorityFastCapture, getpid_cached, tid, err); |
| 4927 | } |
| 4928 | |
| 4929 | #ifdef AUDIO_WATCHDOG |
| 4930 | // FIXME |
| 4931 | #endif |
| 4932 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 4933 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4934 | } |
| 4935 | failed: ; |
| 4936 | |
| 4937 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | |
| 4941 | AudioFlinger::RecordThread::~RecordThread() |
| 4942 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4943 | if (mFastCapture != 0) { |
| 4944 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 4945 | FastCaptureState *state = sq->begin(); |
| 4946 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 4947 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 4948 | if (old == -1) { |
| 4949 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 4950 | } |
| 4951 | } |
| 4952 | state->mCommand = FastCaptureState::EXIT; |
| 4953 | sq->end(); |
| 4954 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 4955 | mFastCapture->join(); |
| 4956 | mFastCapture.clear(); |
| 4957 | } |
| 4958 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 4959 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4960 | delete[] mRsmpInBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | void AudioFlinger::RecordThread::onFirstRef() |
| 4964 | { |
| 4965 | run(mName, PRIORITY_URGENT_AUDIO); |
| 4966 | } |
| 4967 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4968 | bool AudioFlinger::RecordThread::threadLoop() |
| 4969 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4970 | nsecs_t lastWarning = 0; |
| 4971 | |
| 4972 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4973 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 4974 | reacquire_wakelock: |
| 4975 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 4976 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 4977 | { |
| 4978 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 4979 | size_t size = mActiveTracks.size(); |
| 4980 | activeTracksGen = mActiveTracksGen; |
| 4981 | if (size > 0) { |
| 4982 | // FIXME an arbitrary choice |
| 4983 | activeTrack = mActiveTracks[0]; |
| 4984 | acquireWakeLock_l(activeTrack->uid()); |
| 4985 | if (size > 1) { |
| 4986 | SortedVector<int> tmp; |
| 4987 | for (size_t i = 0; i < size; i++) { |
| 4988 | tmp.add(mActiveTracks[i]->uid()); |
| 4989 | } |
| 4990 | updateWakeLockUids_l(tmp); |
| 4991 | } |
| 4992 | } else { |
| 4993 | acquireWakeLock_l(-1); |
| 4994 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 4995 | } |
| 4996 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 4997 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 4998 | uint32_t sleepUs = 0; |
| 4999 | |
| 5000 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5001 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5002 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5003 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5004 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5005 | if (sleepUs > 0) { |
| 5006 | usleep(sleepUs); |
| 5007 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5010 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5011 | Vector< sp<RecordTrack> > activeTracks; |
| 5012 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5013 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5014 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5015 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5016 | // reference to a fast track which is about to be removed |
| 5017 | sp<RecordTrack> fastTrackToRemove; |
| 5018 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5019 | { // scope for mLock |
| 5020 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5021 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5022 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5023 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5024 | // check exitPending here because checkForNewParameters_l() and |
| 5025 | // checkForNewParameters_l() can temporarily release mLock |
| 5026 | if (exitPending()) { |
| 5027 | break; |
| 5028 | } |
| 5029 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5030 | // if no active track(s), then standby and release wakelock |
| 5031 | size_t size = mActiveTracks.size(); |
| 5032 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5033 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5034 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5035 | releaseWakeLock_l(); |
| 5036 | ALOGV("RecordThread: loop stopping"); |
| 5037 | // go to sleep |
| 5038 | mWaitWorkCV.wait(mLock); |
| 5039 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5040 | goto reacquire_wakelock; |
| 5041 | } |
| 5042 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5043 | if (mActiveTracksGen != activeTracksGen) { |
| 5044 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5045 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5046 | for (size_t i = 0; i < size; i++) { |
| 5047 | tmp.add(mActiveTracks[i]->uid()); |
| 5048 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5049 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5050 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5051 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5052 | bool doBroadcast = false; |
| 5053 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5054 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5055 | activeTrack = mActiveTracks[i]; |
| 5056 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5057 | if (activeTrack->isFastTrack()) { |
| 5058 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 5059 | fastTrackToRemove = activeTrack; |
| 5060 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5061 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5062 | mActiveTracks.remove(activeTrack); |
| 5063 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5064 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5065 | continue; |
| 5066 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5067 | |
| 5068 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5069 | switch (activeTrackState) { |
| 5070 | |
| 5071 | case TrackBase::PAUSING: |
| 5072 | mActiveTracks.remove(activeTrack); |
| 5073 | mActiveTracksGen++; |
| 5074 | doBroadcast = true; |
| 5075 | size--; |
| 5076 | continue; |
| 5077 | |
| 5078 | case TrackBase::STARTING_1: |
| 5079 | sleepUs = 10000; |
| 5080 | i++; |
| 5081 | continue; |
| 5082 | |
| 5083 | case TrackBase::STARTING_2: |
| 5084 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5085 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5086 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5087 | break; |
| 5088 | |
| 5089 | case TrackBase::ACTIVE: |
| 5090 | break; |
| 5091 | |
| 5092 | case TrackBase::IDLE: |
| 5093 | i++; |
| 5094 | continue; |
| 5095 | |
| 5096 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5097 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5098 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5099 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5100 | activeTracks.add(activeTrack); |
| 5101 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5102 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5103 | if (activeTrack->isFastTrack()) { |
| 5104 | ALOG_ASSERT(!mFastTrackAvail); |
| 5105 | ALOG_ASSERT(fastTrack == 0); |
| 5106 | fastTrack = activeTrack; |
| 5107 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5108 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5109 | if (doBroadcast) { |
| 5110 | mStartStopCond.broadcast(); |
| 5111 | } |
| 5112 | |
| 5113 | // sleep if there are no active tracks to process |
| 5114 | if (activeTracks.size() == 0) { |
| 5115 | if (sleepUs == 0) { |
| 5116 | sleepUs = kRecordThreadSleepUs; |
| 5117 | } |
| 5118 | continue; |
| 5119 | } |
| 5120 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5121 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5122 | lockEffectChains_l(effectChains); |
| 5123 | } |
| 5124 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5125 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5126 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5127 | size_t size = effectChains.size(); |
| 5128 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5129 | // thread mutex is not locked, but effect chain is locked |
| 5130 | effectChains[i]->process_l(); |
| 5131 | } |
| 5132 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5133 | // Push a new fast capture state if fast capture is not already running, or cblk change |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5134 | if (mFastCapture != 0) { |
| 5135 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5136 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5137 | bool didModify = false; |
| 5138 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5139 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5140 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5141 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5142 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5143 | if (old == -1) { |
| 5144 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5145 | } |
| 5146 | } |
| 5147 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5148 | #if 0 // FIXME |
| 5149 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
| 5150 | FastCaptureDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); |
| 5151 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5152 | didModify = true; |
| 5153 | } |
| 5154 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 5155 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5156 | if (cblkNew != cblkOld) { |
| 5157 | state->mCblk = cblkNew; |
| 5158 | // block until acked if removing a fast track |
| 5159 | if (cblkOld != NULL) { |
| 5160 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 5161 | } |
| 5162 | didModify = true; |
| 5163 | } |
| 5164 | sq->end(didModify); |
| 5165 | if (didModify) { |
| 5166 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5167 | #if 0 |
| 5168 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5169 | mNormalSource = mPipeSource; |
| 5170 | } |
| 5171 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5172 | } |
| 5173 | } |
| 5174 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5175 | // now run the fast track destructor with thread mutex unlocked |
| 5176 | fastTrackToRemove.clear(); |
| 5177 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5178 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 5179 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 5180 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 5181 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 5182 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5183 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5184 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5185 | ssize_t framesRead; |
| 5186 | |
| 5187 | // If an NBAIO source is present, use it to read the normal capture's data |
| 5188 | if (mPipeSource != 0) { |
| 5189 | size_t framesToRead = mBufferSize / mFrameSize; |
| 5190 | framesRead = mPipeSource->read(&mRsmpInBuffer[rear * mChannelCount], |
| 5191 | framesToRead, AudioBufferProvider::kInvalidPTS); |
| 5192 | if (framesRead == 0) { |
| 5193 | // since pipe is non-blocking, simulate blocking input |
| 5194 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 5195 | } |
| 5196 | // otherwise use the HAL / AudioStreamIn directly |
| 5197 | } else { |
| 5198 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
| 5199 | &mRsmpInBuffer[rear * mChannelCount], mBufferSize); |
| 5200 | if (bytesRead < 0) { |
| 5201 | framesRead = bytesRead; |
| 5202 | } else { |
| 5203 | framesRead = bytesRead / mFrameSize; |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
| 5208 | ALOGE("read failed: framesRead=%d", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5209 | // Force input into standby so that it tries to recover at next read attempt |
| 5210 | inputStandBy(); |
| 5211 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5212 | } |
| 5213 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5214 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5215 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5216 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5217 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5218 | if (mTeeSink != 0) { |
| 5219 | (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead); |
| 5220 | } |
| 5221 | // If destination is non-contiguous, we now correct for reading past end of buffer. |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5222 | { |
| 5223 | size_t part1 = mRsmpInFramesP2 - rear; |
| 5224 | if ((size_t) framesRead > part1) { |
| 5225 | memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount], |
| 5226 | (framesRead - part1) * mFrameSize); |
| 5227 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5228 | } |
| 5229 | rear = mRsmpInRear += framesRead; |
| 5230 | |
| 5231 | size = activeTracks.size(); |
| 5232 | // loop over each active track |
| 5233 | for (size_t i = 0; i < size; i++) { |
| 5234 | activeTrack = activeTracks[i]; |
| 5235 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5236 | // skip fast tracks, as those are handled directly by FastCapture |
| 5237 | if (activeTrack->isFastTrack()) { |
| 5238 | continue; |
| 5239 | } |
| 5240 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5241 | enum { |
| 5242 | OVERRUN_UNKNOWN, |
| 5243 | OVERRUN_TRUE, |
| 5244 | OVERRUN_FALSE |
| 5245 | } overrun = OVERRUN_UNKNOWN; |
| 5246 | |
| 5247 | // loop over getNextBuffer to handle circular sink |
| 5248 | for (;;) { |
| 5249 | |
| 5250 | activeTrack->mSink.frameCount = ~0; |
| 5251 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 5252 | size_t framesOut = activeTrack->mSink.frameCount; |
| 5253 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 5254 | |
| 5255 | int32_t front = activeTrack->mRsmpInFront; |
| 5256 | ssize_t filled = rear - front; |
| 5257 | size_t framesIn; |
| 5258 | |
| 5259 | if (filled < 0) { |
| 5260 | // should not happen, but treat like a massive overrun and re-sync |
| 5261 | framesIn = 0; |
| 5262 | activeTrack->mRsmpInFront = rear; |
| 5263 | overrun = OVERRUN_TRUE; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5264 | } else if ((size_t) filled <= mRsmpInFrames) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5265 | framesIn = (size_t) filled; |
| 5266 | } else { |
| 5267 | // client is not keeping up with server, but give it latest data |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5268 | framesIn = mRsmpInFrames; |
| 5269 | activeTrack->mRsmpInFront = front = rear - framesIn; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5270 | overrun = OVERRUN_TRUE; |
| 5271 | } |
| 5272 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5273 | if (framesOut == 0 || framesIn == 0) { |
| 5274 | break; |
| 5275 | } |
| 5276 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5277 | if (activeTrack->mResampler == NULL) { |
| 5278 | // no resampling |
| 5279 | if (framesIn > framesOut) { |
| 5280 | framesIn = framesOut; |
| 5281 | } else { |
| 5282 | framesOut = framesIn; |
| 5283 | } |
| 5284 | int8_t *dst = activeTrack->mSink.i8; |
| 5285 | while (framesIn > 0) { |
| 5286 | front &= mRsmpInFramesP2 - 1; |
| 5287 | size_t part1 = mRsmpInFramesP2 - front; |
| 5288 | if (part1 > framesIn) { |
| 5289 | part1 = framesIn; |
| 5290 | } |
| 5291 | int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize); |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5292 | if (mChannelCount == activeTrack->mChannelCount) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5293 | memcpy(dst, src, part1 * mFrameSize); |
| 5294 | } else if (mChannelCount == 1) { |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5295 | upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, (const int16_t *)src, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5296 | part1); |
| 5297 | } else { |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5298 | downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, (const int16_t *)src, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5299 | part1); |
| 5300 | } |
| 5301 | dst += part1 * activeTrack->mFrameSize; |
| 5302 | front += part1; |
| 5303 | framesIn -= part1; |
| 5304 | } |
| 5305 | activeTrack->mRsmpInFront += framesOut; |
| 5306 | |
| 5307 | } else { |
| 5308 | // resampling |
| 5309 | // FIXME framesInNeeded should really be part of resampler API, and should |
| 5310 | // depend on the SRC ratio |
| 5311 | // to keep mRsmpInBuffer full so resampler always has sufficient input |
| 5312 | size_t framesInNeeded; |
| 5313 | // FIXME only re-calculate when it changes, and optimize for common ratios |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5314 | // Do not precompute in/out because floating point is not associative |
| 5315 | // e.g. a*b/c != a*(b/c). |
| 5316 | const double in(mSampleRate); |
| 5317 | const double out(activeTrack->mSampleRate); |
| 5318 | framesInNeeded = ceil(framesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5319 | ALOGV("need %u frames in to produce %u out given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5320 | framesInNeeded, framesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5321 | // Although we theoretically have framesIn in circular buffer, some of those are |
| 5322 | // unreleased frames, and thus must be discounted for purpose of budgeting. |
| 5323 | size_t unreleased = activeTrack->mRsmpInUnrel; |
| 5324 | framesIn = framesIn > unreleased ? framesIn - unreleased : 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5325 | if (framesIn < framesInNeeded) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5326 | ALOGV("not enough to resample: have %u frames in but need %u in to " |
| 5327 | "produce %u out given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5328 | framesIn, framesInNeeded, framesOut, in / out); |
| 5329 | size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * out / in) : 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5330 | LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut); |
| 5331 | if (newFramesOut == 0) { |
| 5332 | break; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5333 | } |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5334 | framesInNeeded = ceil(newFramesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5335 | ALOGV("now need %u frames in to produce %u out given out/in ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5336 | framesInNeeded, newFramesOut, out / in); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5337 | LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded); |
| 5338 | ALOGV("success 2: have %u frames in and need %u in to produce %u out " |
| 5339 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5340 | framesIn, framesInNeeded, newFramesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5341 | framesOut = newFramesOut; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5342 | } else { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5343 | ALOGV("success 1: have %u in and need %u in to produce %u out " |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5344 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5345 | framesIn, framesInNeeded, framesOut, in / out); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | // reallocate mRsmpOutBuffer as needed; we will grow but never shrink |
| 5349 | if (activeTrack->mRsmpOutFrameCount < framesOut) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5350 | // FIXME why does each track need it's own mRsmpOutBuffer? can't they share? |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5351 | delete[] activeTrack->mRsmpOutBuffer; |
| 5352 | // resampler always outputs stereo |
| 5353 | activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2]; |
| 5354 | activeTrack->mRsmpOutFrameCount = framesOut; |
| 5355 | } |
| 5356 | |
| 5357 | // resampler accumulates, but we only have one source track |
| 5358 | memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t)); |
| 5359 | activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut, |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5360 | // FIXME how about having activeTrack implement this interface itself? |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5361 | activeTrack->mResamplerBufferProvider |
| 5362 | /*this*/ /* AudioBufferProvider* */); |
| 5363 | // ditherAndClamp() works as long as all buffers returned by |
| 5364 | // activeTrack->getNextBuffer() are 32 bit aligned which should be always true. |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5365 | if (activeTrack->mChannelCount == 1) { |
Andy Hung | 84a0c6e | 2014-04-02 11:24:53 -0700 | [diff] [blame] | 5366 | // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5367 | ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer, |
| 5368 | framesOut); |
| 5369 | // the resampler always outputs stereo samples: |
| 5370 | // do post stereo to mono conversion |
| 5371 | downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16, |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5372 | (const int16_t *)activeTrack->mRsmpOutBuffer, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5373 | } else { |
| 5374 | ditherAndClamp((int32_t *)activeTrack->mSink.raw, |
| 5375 | activeTrack->mRsmpOutBuffer, framesOut); |
| 5376 | } |
| 5377 | // now done with mRsmpOutBuffer |
| 5378 | |
| 5379 | } |
| 5380 | |
| 5381 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 5382 | overrun = OVERRUN_FALSE; |
| 5383 | } |
| 5384 | |
| 5385 | if (activeTrack->mFramesToDrop == 0) { |
| 5386 | if (framesOut > 0) { |
| 5387 | activeTrack->mSink.frameCount = framesOut; |
| 5388 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 5389 | } |
| 5390 | } else { |
| 5391 | // FIXME could do a partial drop of framesOut |
| 5392 | if (activeTrack->mFramesToDrop > 0) { |
| 5393 | activeTrack->mFramesToDrop -= framesOut; |
| 5394 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5395 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5396 | } |
| 5397 | } else { |
| 5398 | activeTrack->mFramesToDrop += framesOut; |
| 5399 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 5400 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 5401 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 5402 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 5403 | activeTrack->sessionId(), |
| 5404 | (activeTrack->mSyncStartEvent != 0) ? |
| 5405 | activeTrack->mSyncStartEvent->triggerSession() : 0); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5406 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5407 | } |
| 5408 | } |
| 5409 | } |
| 5410 | |
| 5411 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5412 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5413 | } |
| 5414 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5415 | |
| 5416 | switch (overrun) { |
| 5417 | case OVERRUN_TRUE: |
| 5418 | // client isn't retrieving buffers fast enough |
| 5419 | if (!activeTrack->setOverflow()) { |
| 5420 | nsecs_t now = systemTime(); |
| 5421 | // FIXME should lastWarning per track? |
| 5422 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 5423 | ALOGW("RecordThread: buffer overflow"); |
| 5424 | lastWarning = now; |
| 5425 | } |
| 5426 | } |
| 5427 | break; |
| 5428 | case OVERRUN_FALSE: |
| 5429 | activeTrack->clearOverflow(); |
| 5430 | break; |
| 5431 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5432 | break; |
| 5433 | } |
| 5434 | |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5435 | } |
| 5436 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5437 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5438 | // enable changes in effect chain |
| 5439 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5440 | // 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] | 5441 | } |
| 5442 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5443 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5444 | |
| 5445 | { |
| 5446 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 5447 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5448 | sp<RecordTrack> track = mTracks[i]; |
| 5449 | track->invalidate(); |
| 5450 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5451 | mActiveTracks.clear(); |
| 5452 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5453 | mStartStopCond.broadcast(); |
| 5454 | } |
| 5455 | |
| 5456 | releaseWakeLock(); |
| 5457 | |
| 5458 | ALOGV("RecordThread %p exiting", this); |
| 5459 | return false; |
| 5460 | } |
| 5461 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5462 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5463 | { |
| 5464 | if (!mStandby) { |
| 5465 | inputStandBy(); |
| 5466 | mStandby = true; |
| 5467 | } |
| 5468 | } |
| 5469 | |
| 5470 | void AudioFlinger::RecordThread::inputStandBy() |
| 5471 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5472 | // Idle the fast capture if it's currently running |
| 5473 | if (mFastCapture != 0) { |
| 5474 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5475 | FastCaptureState *state = sq->begin(); |
| 5476 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 5477 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5478 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5479 | state->mColdGen++; |
| 5480 | mFastCaptureFutex = 0; |
| 5481 | sq->end(); |
| 5482 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 5483 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 5484 | #if 0 |
| 5485 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5486 | // FIXME |
| 5487 | } |
| 5488 | #endif |
| 5489 | #ifdef AUDIO_WATCHDOG |
| 5490 | // FIXME |
| 5491 | #endif |
| 5492 | } else { |
| 5493 | sq->end(false /*didModify*/); |
| 5494 | } |
| 5495 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5496 | mInput->stream->common.standby(&mInput->stream->common); |
| 5497 | } |
| 5498 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5499 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 5500 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5501 | const sp<AudioFlinger::Client>& client, |
| 5502 | uint32_t sampleRate, |
| 5503 | audio_format_t format, |
| 5504 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5505 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5506 | int sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 5507 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5508 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 5509 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5510 | pid_t tid, |
| 5511 | status_t *status) |
| 5512 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5513 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5514 | sp<RecordTrack> track; |
| 5515 | status_t lStatus; |
| 5516 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5517 | // client expresses a preference for FAST, but we get the final say |
| 5518 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5519 | if ( |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5520 | // use case: callback handler |
| 5521 | (tid != -1) && |
| 5522 | // frame count is not specified, or is exactly the pipe depth |
| 5523 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5524 | // PCM data |
| 5525 | audio_is_linear_pcm(format) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5526 | // native format |
| 5527 | (format == mFormat) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5528 | // native channel mask |
| 5529 | (channelMask == mChannelMask) && |
| 5530 | // native hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5531 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5532 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5533 | hasFastCapture() && |
| 5534 | // there are sufficient fast track slots available |
| 5535 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5536 | ) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5537 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5538 | frameCount, mFrameCount); |
| 5539 | } else { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5540 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " |
| 5541 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5542 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5543 | frameCount, mFrameCount, mPipeFramesP2, |
| 5544 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 5545 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5546 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | // compute track buffer size in frames, and suggest the notification frame count |
| 5551 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5552 | // fast track: frame count is exactly the pipe depth |
| 5553 | frameCount = mPipeFramesP2; |
| 5554 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 5555 | *notificationFrames = mFrameCount; |
| 5556 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5557 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 5558 | // or 20 ms if there is a fast capture |
| 5559 | // TODO This could be a roundupRatio inline, and const |
| 5560 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 5561 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 5562 | // minimum number of notification periods is at least kMinNotifications, |
| 5563 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 5564 | static const size_t kMinNotifications = 3; |
| 5565 | static const uint32_t kMinMs = 30; |
| 5566 | // TODO This could be a roundupRatio inline |
| 5567 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 5568 | // TODO This could be a roundupRatio inline |
| 5569 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 5570 | maxNotificationFrames; |
| 5571 | const size_t minFrameCount = maxNotificationFrames * |
| 5572 | max(kMinNotifications, minNotificationsByMs); |
| 5573 | frameCount = max(frameCount, minFrameCount); |
| 5574 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 5575 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5576 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5577 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5578 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5579 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 5580 | lStatus = initCheck(); |
| 5581 | if (lStatus != NO_ERROR) { |
| 5582 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 5583 | goto Exit; |
| 5584 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5585 | |
| 5586 | { // scope for mLock |
| 5587 | Mutex::Autolock _l(mLock); |
| 5588 | |
| 5589 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5590 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 5591 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5592 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 5593 | lStatus = track->initCheck(); |
| 5594 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 5595 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 5596 | // track must be cleared from the caller as the caller has the AF lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5597 | goto Exit; |
| 5598 | } |
| 5599 | mTracks.add(track); |
| 5600 | |
| 5601 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5602 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 5603 | mAudioFlinger->btNrecIsOff(); |
| 5604 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5605 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5606 | |
| 5607 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 5608 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 5609 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 5610 | // so ask activity manager to do this on our behalf |
| 5611 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 5612 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5613 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5614 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5615 | lStatus = NO_ERROR; |
| 5616 | |
| 5617 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 5618 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5619 | return track; |
| 5620 | } |
| 5621 | |
| 5622 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 5623 | AudioSystem::sync_event_t event, |
| 5624 | int triggerSession) |
| 5625 | { |
| 5626 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 5627 | sp<ThreadBase> strongMe = this; |
| 5628 | status_t status = NO_ERROR; |
| 5629 | |
| 5630 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5631 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5632 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5633 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5634 | triggerSession, |
| 5635 | recordTrack->sessionId(), |
| 5636 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5637 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5638 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 5639 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5640 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5641 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5642 | } else { |
| 5643 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5644 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5645 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5650 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5651 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5652 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 5653 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 5654 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5655 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5656 | } else { |
| 5657 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5658 | } |
| 5659 | return status; |
| 5660 | } |
| 5661 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5662 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 5663 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 5664 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5665 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5666 | mActiveTracks.add(recordTrack); |
| 5667 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5668 | status_t status = NO_ERROR; |
| 5669 | if (recordTrack->isExternalTrack()) { |
| 5670 | mLock.unlock(); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 5671 | status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5672 | mLock.lock(); |
| 5673 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 5674 | if (status != NO_ERROR) { |
| 5675 | mActiveTracks.remove(recordTrack); |
| 5676 | mActiveTracksGen++; |
| 5677 | recordTrack->clearSyncStartEvent(); |
| 5678 | ALOGV("RecordThread::start error %d", status); |
| 5679 | return status; |
| 5680 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5681 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5682 | // Catch up with current buffer indices if thread is already running. |
| 5683 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 5684 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 5685 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 5686 | |
| 5687 | recordTrack->mRsmpInFront = mRsmpInRear; |
| 5688 | recordTrack->mRsmpInUnrel = 0; |
| 5689 | // FIXME why reset? |
| 5690 | if (recordTrack->mResampler != NULL) { |
| 5691 | recordTrack->mResampler->reset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5692 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5693 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5694 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5695 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5696 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5697 | ALOGV("Record failed to start"); |
| 5698 | status = BAD_VALUE; |
| 5699 | goto startError; |
| 5700 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5701 | return status; |
| 5702 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 5703 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5704 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5705 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 5706 | AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5707 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5708 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5709 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5710 | return status; |
| 5711 | } |
| 5712 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5713 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 5714 | { |
| 5715 | sp<SyncEvent> strongEvent = event.promote(); |
| 5716 | |
| 5717 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 5718 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 5719 | if (ptr != 0) { |
| 5720 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 5721 | recordTrack->handleSyncStartEvent(strongEvent); |
| 5722 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 5726 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5727 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 5728 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5729 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5730 | return false; |
| 5731 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5732 | // 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] | 5733 | recordTrack->mState = TrackBase::PAUSING; |
| 5734 | // do not wait for mStartStopCond if exiting |
| 5735 | if (exitPending()) { |
| 5736 | return true; |
| 5737 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5738 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5739 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5740 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 5741 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5742 | ALOGV("Record stopped OK"); |
| 5743 | return true; |
| 5744 | } |
| 5745 | return false; |
| 5746 | } |
| 5747 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5748 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5749 | { |
| 5750 | return false; |
| 5751 | } |
| 5752 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5753 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5754 | { |
| 5755 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 5756 | if (!isValidSyncEvent(event)) { |
| 5757 | return BAD_VALUE; |
| 5758 | } |
| 5759 | |
| 5760 | int eventSession = event->triggerSession(); |
| 5761 | status_t ret = NAME_NOT_FOUND; |
| 5762 | |
| 5763 | Mutex::Autolock _l(mLock); |
| 5764 | |
| 5765 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5766 | sp<RecordTrack> track = mTracks[i]; |
| 5767 | if (eventSession == track->sessionId()) { |
| 5768 | (void) track->setSyncEvent(event); |
| 5769 | ret = NO_ERROR; |
| 5770 | } |
| 5771 | } |
| 5772 | return ret; |
| 5773 | #else |
| 5774 | return BAD_VALUE; |
| 5775 | #endif |
| 5776 | } |
| 5777 | |
| 5778 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 5779 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 5780 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5781 | track->terminate(); |
| 5782 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5783 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5784 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5785 | removeTrack_l(track); |
| 5786 | } |
| 5787 | } |
| 5788 | |
| 5789 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 5790 | { |
| 5791 | mTracks.remove(track); |
| 5792 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5793 | if (track->isFastTrack()) { |
| 5794 | ALOG_ASSERT(!mFastTrackAvail); |
| 5795 | mFastTrackAvail = true; |
| 5796 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5797 | } |
| 5798 | |
| 5799 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 5800 | { |
| 5801 | dumpInternals(fd, args); |
| 5802 | dumpTracks(fd, args); |
| 5803 | dumpEffectChains(fd, args); |
| 5804 | } |
| 5805 | |
| 5806 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 5807 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5808 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5809 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5810 | if (mActiveTracks.size() > 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5811 | dprintf(fd, " Buffer size: %zu bytes\n", mBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5812 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5813 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5814 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5815 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5816 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5817 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5818 | dumpBase(fd, args); |
| 5819 | } |
| 5820 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5821 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5822 | { |
| 5823 | const size_t SIZE = 256; |
| 5824 | char buffer[SIZE]; |
| 5825 | String8 result; |
| 5826 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5827 | size_t numtracks = mTracks.size(); |
| 5828 | size_t numactive = mActiveTracks.size(); |
| 5829 | size_t numactiveseen = 0; |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5830 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5831 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5832 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5833 | RecordTrack::appendDumpHeader(result); |
| 5834 | for (size_t i = 0; i < numtracks ; ++i) { |
| 5835 | sp<RecordTrack> track = mTracks[i]; |
| 5836 | if (track != 0) { |
| 5837 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 5838 | if (active) { |
| 5839 | numactiveseen++; |
| 5840 | } |
| 5841 | track->dump(buffer, SIZE, active); |
| 5842 | result.append(buffer); |
| 5843 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5844 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5845 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5846 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5849 | if (numactiveseen != numactive) { |
| 5850 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 5851 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5852 | result.append(buffer); |
| 5853 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5854 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5855 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5856 | if (mTracks.indexOf(track) < 0) { |
| 5857 | track->dump(buffer, SIZE, true); |
| 5858 | result.append(buffer); |
| 5859 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5860 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5861 | |
| 5862 | } |
| 5863 | write(fd, result.string(), result.size()); |
| 5864 | } |
| 5865 | |
| 5866 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5867 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
| 5868 | AudioBufferProvider::Buffer* buffer, int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5869 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5870 | RecordTrack *activeTrack = mRecordTrack; |
| 5871 | sp<ThreadBase> threadBase = activeTrack->mThread.promote(); |
| 5872 | if (threadBase == 0) { |
| 5873 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5874 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5875 | return NOT_ENOUGH_DATA; |
| 5876 | } |
| 5877 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 5878 | int32_t rear = recordThread->mRsmpInRear; |
| 5879 | int32_t front = activeTrack->mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5880 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5881 | // FIXME should not be P2 (don't want to increase latency) |
| 5882 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5883 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5884 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5885 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 5886 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5887 | if (part1 > (size_t) filled) { |
| 5888 | part1 = filled; |
| 5889 | } |
| 5890 | size_t ask = buffer->frameCount; |
| 5891 | ALOG_ASSERT(ask > 0); |
| 5892 | if (part1 > ask) { |
| 5893 | part1 = ask; |
| 5894 | } |
| 5895 | if (part1 == 0) { |
| 5896 | // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5897 | LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved"); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5898 | buffer->raw = NULL; |
| 5899 | buffer->frameCount = 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5900 | activeTrack->mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5901 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5902 | } |
| 5903 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5904 | buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5905 | buffer->frameCount = part1; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5906 | activeTrack->mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5907 | return NO_ERROR; |
| 5908 | } |
| 5909 | |
| 5910 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5911 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 5912 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5913 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5914 | RecordTrack *activeTrack = mRecordTrack; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5915 | size_t stepCount = buffer->frameCount; |
| 5916 | if (stepCount == 0) { |
| 5917 | return; |
| 5918 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5919 | ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel); |
| 5920 | activeTrack->mRsmpInUnrel -= stepCount; |
| 5921 | activeTrack->mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5922 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5923 | buffer->frameCount = 0; |
| 5924 | } |
| 5925 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5926 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5927 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5928 | { |
| 5929 | bool reconfig = false; |
| 5930 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5931 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5932 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5933 | audio_format_t reqFormat = mFormat; |
| 5934 | uint32_t samplingRate = mSampleRate; |
| 5935 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 5936 | |
| 5937 | AudioParameter param = AudioParameter(keyValuePair); |
| 5938 | int value; |
| 5939 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 5940 | // channel count change can be requested. Do we mandate the first client defines the |
| 5941 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 5942 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 5943 | samplingRate = value; |
| 5944 | reconfig = true; |
| 5945 | } |
| 5946 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 5947 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
| 5948 | status = BAD_VALUE; |
| 5949 | } else { |
| 5950 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5951 | reconfig = true; |
| 5952 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5953 | } |
| 5954 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 5955 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
| 5956 | if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) { |
| 5957 | status = BAD_VALUE; |
| 5958 | } else { |
| 5959 | channelMask = mask; |
| 5960 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5961 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5962 | } |
| 5963 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5964 | // do not accept frame count changes if tracks are open as the track buffer |
| 5965 | // size depends on frame count and correct behavior would not be guaranteed |
| 5966 | // if frame count is changed after track creation |
| 5967 | if (mActiveTracks.size() > 0) { |
| 5968 | status = INVALID_OPERATION; |
| 5969 | } else { |
| 5970 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5971 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5972 | } |
| 5973 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5974 | // forward device change to effects that have requested to be |
| 5975 | // aware of attached audio device. |
| 5976 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5977 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5978 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5979 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5980 | // store input device and output device but do not forward output device to audio HAL. |
| 5981 | // Note that status is ignored by the caller for output device |
| 5982 | // (see AudioFlinger::setParameters() |
| 5983 | if (audio_is_output_devices(value)) { |
| 5984 | mOutDevice = value; |
| 5985 | status = BAD_VALUE; |
| 5986 | } else { |
| 5987 | mInDevice = value; |
| 5988 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 5989 | // pre processings |
| 5990 | if (mTracks.size() > 0) { |
| 5991 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 5992 | mAudioFlinger->btNrecIsOff(); |
| 5993 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5994 | sp<RecordTrack> track = mTracks[i]; |
| 5995 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 5996 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5997 | } |
| 5998 | } |
| 5999 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6000 | } |
| 6001 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 6002 | mAudioSource != (audio_source_t)value) { |
| 6003 | // forward device change to effects that have requested to be |
| 6004 | // aware of attached audio device. |
| 6005 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6006 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6007 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6008 | mAudioSource = (audio_source_t)value; |
| 6009 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6010 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6011 | if (status == NO_ERROR) { |
| 6012 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6013 | keyValuePair.string()); |
| 6014 | if (status == INVALID_OPERATION) { |
| 6015 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6016 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6017 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6018 | } |
| 6019 | if (reconfig) { |
| 6020 | if (status == BAD_VALUE && |
| 6021 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
| 6022 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
| 6023 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
| 6024 | <= (2 * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6025 | audio_channel_count_from_in_mask( |
| 6026 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6027 | (channelMask == AUDIO_CHANNEL_IN_MONO || |
| 6028 | channelMask == AUDIO_CHANNEL_IN_STEREO)) { |
| 6029 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6030 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6031 | if (status == NO_ERROR) { |
| 6032 | readInputParameters_l(); |
| 6033 | sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6034 | } |
| 6035 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6036 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6037 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6038 | return reconfig; |
| 6039 | } |
| 6040 | |
| 6041 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6042 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6043 | Mutex::Autolock _l(mLock); |
| 6044 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6045 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6046 | } |
| 6047 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6048 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 6049 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6050 | free(s); |
| 6051 | return out_s8; |
| 6052 | } |
| 6053 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6054 | void AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6055 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | b2737d0 | 2013-08-19 12:03:11 -0700 | [diff] [blame] | 6056 | const void *param2 = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6057 | |
| 6058 | switch (event) { |
| 6059 | case AudioSystem::INPUT_OPENED: |
| 6060 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 6061 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6062 | desc.samplingRate = mSampleRate; |
| 6063 | desc.format = mFormat; |
| 6064 | desc.frameCount = mFrameCount; |
| 6065 | desc.latency = 0; |
| 6066 | param2 = &desc; |
| 6067 | break; |
| 6068 | |
| 6069 | case AudioSystem::INPUT_CLOSED: |
| 6070 | default: |
| 6071 | break; |
| 6072 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6073 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6074 | } |
| 6075 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6076 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6077 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6078 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 6079 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6080 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6081 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 6082 | mFormat = mHALFormat; |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6083 | if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 6084 | ALOGE("HAL format %#x not supported; must be AUDIO_FORMAT_PCM_16_BIT", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6085 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6086 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6087 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 6088 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6089 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6090 | // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6091 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6092 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6093 | // A larger value should allow more old data to be read after a track calls start(), |
| 6094 | // without increasing latency. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6095 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6096 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6097 | delete[] mRsmpInBuffer; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6098 | |
| 6099 | // TODO optimize audio capture buffer sizes ... |
| 6100 | // Here we calculate the size of the sliding buffer used as a source |
| 6101 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 6102 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 6103 | // be better to have it derived from the pipe depth in the long term. |
| 6104 | // The current value is higher than necessary. However it should not add to latency. |
| 6105 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6106 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 6107 | mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6108 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6109 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 6110 | // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6111 | } |
| 6112 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6113 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6114 | { |
| 6115 | Mutex::Autolock _l(mLock); |
| 6116 | if (initCheck() != NO_ERROR) { |
| 6117 | return 0; |
| 6118 | } |
| 6119 | |
| 6120 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 6121 | } |
| 6122 | |
| 6123 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const |
| 6124 | { |
| 6125 | Mutex::Autolock _l(mLock); |
| 6126 | uint32_t result = 0; |
| 6127 | if (getEffectChain_l(sessionId) != 0) { |
| 6128 | result = EFFECT_SESSION; |
| 6129 | } |
| 6130 | |
| 6131 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6132 | if (sessionId == mTracks[i]->sessionId()) { |
| 6133 | result |= TRACK_SESSION; |
| 6134 | break; |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | return result; |
| 6139 | } |
| 6140 | |
| 6141 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const |
| 6142 | { |
| 6143 | KeyedVector<int, bool> ids; |
| 6144 | Mutex::Autolock _l(mLock); |
| 6145 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 6146 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
| 6147 | int sessionId = track->sessionId(); |
| 6148 | if (ids.indexOfKey(sessionId) < 0) { |
| 6149 | ids.add(sessionId, true); |
| 6150 | } |
| 6151 | } |
| 6152 | return ids; |
| 6153 | } |
| 6154 | |
| 6155 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6156 | { |
| 6157 | Mutex::Autolock _l(mLock); |
| 6158 | AudioStreamIn *input = mInput; |
| 6159 | mInput = NULL; |
| 6160 | return input; |
| 6161 | } |
| 6162 | |
| 6163 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 6164 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 6165 | { |
| 6166 | if (mInput == NULL) { |
| 6167 | return NULL; |
| 6168 | } |
| 6169 | return &mInput->stream->common; |
| 6170 | } |
| 6171 | |
| 6172 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6173 | { |
| 6174 | // only one chain per input thread |
| 6175 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 6176 | ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6177 | return INVALID_OPERATION; |
| 6178 | } |
| 6179 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 6180 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6181 | chain->setInBuffer(NULL); |
| 6182 | chain->setOutBuffer(NULL); |
| 6183 | |
| 6184 | checkSuspendOnAddEffectChain_l(chain); |
| 6185 | |
| 6186 | mEffectChains.add(chain); |
| 6187 | |
| 6188 | return NO_ERROR; |
| 6189 | } |
| 6190 | |
| 6191 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6192 | { |
| 6193 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 6194 | ALOGW_IF(mEffectChains.size() != 1, |
| 6195 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6196 | chain.get(), mEffectChains.size(), this); |
| 6197 | if (mEffectChains.size() == 1) { |
| 6198 | mEffectChains.removeAt(0); |
| 6199 | } |
| 6200 | return 0; |
| 6201 | } |
| 6202 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6203 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 6204 | audio_patch_handle_t *handle) |
| 6205 | { |
| 6206 | status_t status = NO_ERROR; |
| 6207 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6208 | // store new device and send to effects |
| 6209 | mInDevice = patch->sources[0].ext.device.type; |
| 6210 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6211 | mEffectChains[i]->setDevice_l(mInDevice); |
| 6212 | } |
| 6213 | |
| 6214 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 6215 | // pre processings |
| 6216 | if (mTracks.size() > 0) { |
| 6217 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6218 | mAudioFlinger->btNrecIsOff(); |
| 6219 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6220 | sp<RecordTrack> track = mTracks[i]; |
| 6221 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 6222 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 6223 | } |
| 6224 | } |
| 6225 | |
| 6226 | // store new source and send to effects |
| 6227 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 6228 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 6229 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6230 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 6231 | } |
| 6232 | } |
| 6233 | |
| 6234 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6235 | status = hwDevice->create_audio_patch(hwDevice, |
| 6236 | patch->num_sources, |
| 6237 | patch->sources, |
| 6238 | patch->num_sinks, |
| 6239 | patch->sinks, |
| 6240 | handle); |
| 6241 | } else { |
| 6242 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 6243 | } |
| 6244 | return status; |
| 6245 | } |
| 6246 | |
| 6247 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 6248 | { |
| 6249 | status_t status = NO_ERROR; |
| 6250 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6251 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6252 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 6253 | } else { |
| 6254 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 6255 | } |
| 6256 | return status; |
| 6257 | } |
| 6258 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6259 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 6260 | { |
| 6261 | Mutex::Autolock _l(mLock); |
| 6262 | mTracks.add(record); |
| 6263 | } |
| 6264 | |
| 6265 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 6266 | { |
| 6267 | Mutex::Autolock _l(mLock); |
| 6268 | destroyTrack_l(record); |
| 6269 | } |
| 6270 | |
| 6271 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 6272 | { |
| 6273 | ThreadBase::getAudioPortConfig(config); |
| 6274 | config->role = AUDIO_PORT_ROLE_SINK; |
| 6275 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 6276 | config->ext.mix.usecase.source = mAudioSource; |
| 6277 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6278 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6279 | }; // namespace android |