Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2012, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 22 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 23 | #include "Configuration.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 24 | #include <math.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <cutils/properties.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 28 | #include <media/AudioParameter.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 30 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 31 | |
| 32 | #include <private/media/AudioTrackShared.h> |
| 33 | #include <hardware/audio.h> |
| 34 | #include <audio_effects/effect_ns.h> |
| 35 | #include <audio_effects/effect_aec.h> |
| 36 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 37 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 38 | #include <audio_utils/minifloat.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 39 | |
| 40 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 41 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 42 | #include <media/nbaio/AudioStreamOutSink.h> |
| 43 | #include <media/nbaio/MonoPipe.h> |
| 44 | #include <media/nbaio/MonoPipeReader.h> |
| 45 | #include <media/nbaio/Pipe.h> |
| 46 | #include <media/nbaio/PipeReader.h> |
| 47 | #include <media/nbaio/SourceAudioBufferProvider.h> |
| 48 | |
| 49 | #include <powermanager/PowerManager.h> |
| 50 | |
| 51 | #include <common_time/cc_helper.h> |
| 52 | #include <common_time/local_clock.h> |
| 53 | |
| 54 | #include "AudioFlinger.h" |
| 55 | #include "AudioMixer.h" |
| 56 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 57 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 58 | #include "ServiceUtilities.h" |
| 59 | #include "SchedulingPolicyService.h" |
| 60 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #ifdef ADD_BATTERY_DATA |
| 62 | #include <media/IMediaPlayerService.h> |
| 63 | #include <media/IMediaDeathNotifier.h> |
| 64 | #endif |
| 65 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 | #ifdef DEBUG_CPU_USAGE |
| 67 | #include <cpustats/CentralTendencyStatistics.h> |
| 68 | #include <cpustats/ThreadCpuUsage.h> |
| 69 | #endif |
| 70 | |
| 71 | // ---------------------------------------------------------------------------- |
| 72 | |
| 73 | // Note: the following macro is used for extremely verbose logging message. In |
| 74 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 75 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 76 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 77 | // turned on. Do not uncomment the #def below unless you really know what you |
| 78 | // are doing and want to see all of the extremely verbose messages. |
| 79 | //#define VERY_VERY_VERBOSE_LOGGING |
| 80 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 81 | #define ALOGVV ALOGV |
| 82 | #else |
| 83 | #define ALOGVV(a...) do { } while(0) |
| 84 | #endif |
| 85 | |
| 86 | namespace android { |
| 87 | |
| 88 | // retry counts for buffer fill timeout |
| 89 | // 50 * ~20msecs = 1 second |
| 90 | static const int8_t kMaxTrackRetries = 50; |
| 91 | static const int8_t kMaxTrackStartupRetries = 50; |
| 92 | // allow less retry attempts on direct output thread. |
| 93 | // direct outputs can be a scarce resource in audio hardware and should |
| 94 | // be released as quickly as possible. |
| 95 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 96 | |
| 97 | // don't warn about blocked writes or record buffer overflows more often than this |
| 98 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 99 | |
| 100 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 101 | static const int kRecordThreadSleepUs = 5000; |
| 102 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 103 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 104 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 105 | |
| 106 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 107 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 108 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 109 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 110 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 111 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
| 112 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 113 | // maximum normal sink buffer size |
| 114 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 115 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 116 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 117 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 118 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 119 | // Whether to use fast mixer |
| 120 | static const enum { |
| 121 | FastMixer_Never, // never initialize or use: for debugging only |
| 122 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 123 | // normal mixer multiplier is 1 |
| 124 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 125 | // multiplier is calculated based on min & max normal mixer buffer size |
| 126 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 127 | // multiplier is calculated based on min & max normal mixer buffer size |
| 128 | // FIXME for FastMixer_Dynamic: |
| 129 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 130 | // For example, one HAL implementation returns an error from a large write, |
| 131 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 132 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 133 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 134 | } kUseFastMixer = FastMixer_Static; |
| 135 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 136 | // Whether to use fast capture |
| 137 | static const enum { |
| 138 | FastCapture_Never, // never initialize or use: for debugging only |
| 139 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 140 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 141 | } kUseFastCapture = FastCapture_Static; |
| 142 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 143 | // Priorities for requestPriority |
| 144 | static const int kPriorityAudioApp = 2; |
| 145 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 146 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 147 | |
| 148 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 149 | // 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] | 150 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 151 | // So for now we just assume that client is double-buffered for fast tracks. |
| 152 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 153 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 154 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 155 | |
| 156 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 157 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 158 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 159 | // The minimum and maximum allowed values |
| 160 | static const int kFastTrackMultiplierMin = 1; |
| 161 | static const int kFastTrackMultiplierMax = 2; |
| 162 | |
| 163 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 164 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 165 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 166 | // See Thread::readOnlyHeap(). |
| 167 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 168 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 169 | // 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] | 170 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 171 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 172 | // ---------------------------------------------------------------------------- |
| 173 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 174 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 175 | |
| 176 | static void sFastTrackMultiplierInit() |
| 177 | { |
| 178 | char value[PROPERTY_VALUE_MAX]; |
| 179 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 180 | char *endptr; |
| 181 | unsigned long ul = strtoul(value, &endptr, 0); |
| 182 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 183 | sFastTrackMultiplier = (int) ul; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // ---------------------------------------------------------------------------- |
| 189 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 190 | #ifdef ADD_BATTERY_DATA |
| 191 | // To collect the amplifier usage |
| 192 | static void addBatteryData(uint32_t params) { |
| 193 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 194 | if (service == NULL) { |
| 195 | // it already logged |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | service->addBatteryData(params); |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | |
| 204 | // ---------------------------------------------------------------------------- |
| 205 | // CPU Stats |
| 206 | // ---------------------------------------------------------------------------- |
| 207 | |
| 208 | class CpuStats { |
| 209 | public: |
| 210 | CpuStats(); |
| 211 | void sample(const String8 &title); |
| 212 | #ifdef DEBUG_CPU_USAGE |
| 213 | private: |
| 214 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 215 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 216 | |
| 217 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 218 | |
| 219 | int mCpuNum; // thread's current CPU number |
| 220 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 221 | #endif |
| 222 | }; |
| 223 | |
| 224 | CpuStats::CpuStats() |
| 225 | #ifdef DEBUG_CPU_USAGE |
| 226 | : mCpuNum(-1), mCpukHz(-1) |
| 227 | #endif |
| 228 | { |
| 229 | } |
| 230 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 231 | void CpuStats::sample(const String8 &title |
| 232 | #ifndef DEBUG_CPU_USAGE |
| 233 | __unused |
| 234 | #endif |
| 235 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 236 | #ifdef DEBUG_CPU_USAGE |
| 237 | // get current thread's delta CPU time in wall clock ns |
| 238 | double wcNs; |
| 239 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 240 | |
| 241 | // record sample for wall clock statistics |
| 242 | if (valid) { |
| 243 | mWcStats.sample(wcNs); |
| 244 | } |
| 245 | |
| 246 | // get the current CPU number |
| 247 | int cpuNum = sched_getcpu(); |
| 248 | |
| 249 | // get the current CPU frequency in kHz |
| 250 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 251 | |
| 252 | // check if either CPU number or frequency changed |
| 253 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 254 | mCpuNum = cpuNum; |
| 255 | mCpukHz = cpukHz; |
| 256 | // ignore sample for purposes of cycles |
| 257 | valid = false; |
| 258 | } |
| 259 | |
| 260 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 261 | if (valid && mCpukHz > 0) { |
| 262 | double cycles = wcNs * cpukHz * 0.000001; |
| 263 | mHzStats.sample(cycles); |
| 264 | } |
| 265 | |
| 266 | unsigned n = mWcStats.n(); |
| 267 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 268 | if ((n & 127) == 1) { |
| 269 | long long elapsed = mCpuUsage.elapsed(); |
| 270 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 271 | double perLoop = elapsed / (double) n; |
| 272 | double perLoop100 = perLoop * 0.01; |
| 273 | double perLoop1k = perLoop * 0.001; |
| 274 | double mean = mWcStats.mean(); |
| 275 | double stddev = mWcStats.stddev(); |
| 276 | double minimum = mWcStats.minimum(); |
| 277 | double maximum = mWcStats.maximum(); |
| 278 | double meanCycles = mHzStats.mean(); |
| 279 | double stddevCycles = mHzStats.stddev(); |
| 280 | double minCycles = mHzStats.minimum(); |
| 281 | double maxCycles = mHzStats.maximum(); |
| 282 | mCpuUsage.resetElapsed(); |
| 283 | mWcStats.reset(); |
| 284 | mHzStats.reset(); |
| 285 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 286 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 287 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 288 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 289 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 290 | title.string(), |
| 291 | elapsed * .000000001, n, perLoop * .000001, |
| 292 | mean * .001, |
| 293 | stddev * .001, |
| 294 | minimum * .001, |
| 295 | maximum * .001, |
| 296 | mean / perLoop100, |
| 297 | stddev / perLoop100, |
| 298 | minimum / perLoop100, |
| 299 | maximum / perLoop100, |
| 300 | meanCycles / perLoop1k, |
| 301 | stddevCycles / perLoop1k, |
| 302 | minCycles / perLoop1k, |
| 303 | maxCycles / perLoop1k); |
| 304 | |
| 305 | } |
| 306 | } |
| 307 | #endif |
| 308 | }; |
| 309 | |
| 310 | // ---------------------------------------------------------------------------- |
| 311 | // ThreadBase |
| 312 | // ---------------------------------------------------------------------------- |
| 313 | |
| 314 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 315 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type) |
| 316 | : Thread(false /*canCallJava*/), |
| 317 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 318 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 319 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 320 | // are set by PlaybackThread::readOutputParameters_l() or |
| 321 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 322 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 323 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
| 324 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
| 325 | // mName will be set by concrete (non-virtual) subclass |
| 326 | mDeathRecipient(new PMDeathRecipient(this)) |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | AudioFlinger::ThreadBase::~ThreadBase() |
| 331 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 332 | // 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] | 333 | mConfigEvents.clear(); |
| 334 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 335 | // do not lock the mutex in destructor |
| 336 | releaseWakeLock_l(); |
| 337 | if (mPowerManager != 0) { |
| 338 | sp<IBinder> binder = mPowerManager->asBinder(); |
| 339 | binder->unlinkToDeath(mDeathRecipient); |
| 340 | } |
| 341 | } |
| 342 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 343 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 344 | { |
| 345 | status_t status = initCheck(); |
| 346 | if (status == NO_ERROR) { |
| 347 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 348 | } else { |
| 349 | ALOGE("No working audio driver found."); |
| 350 | } |
| 351 | return status; |
| 352 | } |
| 353 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 354 | void AudioFlinger::ThreadBase::exit() |
| 355 | { |
| 356 | ALOGV("ThreadBase::exit"); |
| 357 | // do any cleanup required for exit to succeed |
| 358 | preExit(); |
| 359 | { |
| 360 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 361 | // if (!exitPending()) { |
| 362 | // // context switch from here to exit() |
| 363 | // // exit() calls requestExit(), what exitPending() observes |
| 364 | // // exit() calls signal(), which is dropped since no waiters |
| 365 | // // context switch back from exit() to here |
| 366 | // mWaitWorkCV.wait(...); |
| 367 | // // now thread is hung |
| 368 | // } |
| 369 | AutoMutex lock(mLock); |
| 370 | requestExit(); |
| 371 | mWaitWorkCV.broadcast(); |
| 372 | } |
| 373 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 374 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 375 | requestExitAndWait(); |
| 376 | } |
| 377 | |
| 378 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 379 | { |
| 380 | status_t status; |
| 381 | |
| 382 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 383 | Mutex::Autolock _l(mLock); |
| 384 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 385 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 386 | } |
| 387 | |
| 388 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 389 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 390 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 391 | { |
| 392 | status_t status = NO_ERROR; |
| 393 | |
| 394 | mConfigEvents.add(event); |
| 395 | ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 396 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 397 | mLock.unlock(); |
| 398 | { |
| 399 | Mutex::Autolock _l(event->mLock); |
| 400 | while (event->mWaitStatus) { |
| 401 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 402 | event->mStatus = TIMED_OUT; |
| 403 | event->mWaitStatus = false; |
| 404 | } |
| 405 | } |
| 406 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 407 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 408 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 409 | return status; |
| 410 | } |
| 411 | |
| 412 | void AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) |
| 413 | { |
| 414 | Mutex::Autolock _l(mLock); |
| 415 | sendIoConfigEvent_l(event, param); |
| 416 | } |
| 417 | |
| 418 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
| 419 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) |
| 420 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 421 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param); |
| 422 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 426 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 427 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 428 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 429 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 432 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 433 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 434 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 435 | sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); |
| 436 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 439 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 440 | const struct audio_patch *patch, |
| 441 | audio_patch_handle_t *handle) |
| 442 | { |
| 443 | Mutex::Autolock _l(mLock); |
| 444 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 445 | status_t status = sendConfigEvent_l(configEvent); |
| 446 | if (status == NO_ERROR) { |
| 447 | CreateAudioPatchConfigEventData *data = |
| 448 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 449 | *handle = data->mHandle; |
| 450 | } |
| 451 | return status; |
| 452 | } |
| 453 | |
| 454 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 455 | const audio_patch_handle_t handle) |
| 456 | { |
| 457 | Mutex::Autolock _l(mLock); |
| 458 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 459 | return sendConfigEvent_l(configEvent); |
| 460 | } |
| 461 | |
| 462 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 463 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 464 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 465 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 466 | bool configChanged = false; |
| 467 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 468 | while (!mConfigEvents.isEmpty()) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 469 | ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); |
| 470 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 471 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 472 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 473 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 474 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 475 | // FIXME Need to understand why this has to be done asynchronously |
| 476 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 477 | true /*asynchronous*/); |
| 478 | if (err != 0) { |
| 479 | 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] | 480 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 481 | } |
| 482 | } break; |
| 483 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 484 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 485 | audioConfigChanged(data->mEvent, data->mParam); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 486 | } break; |
| 487 | case CFG_EVENT_SET_PARAMETER: { |
| 488 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 489 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 490 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 491 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 492 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 493 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 494 | CreateAudioPatchConfigEventData *data = |
| 495 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 496 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 497 | } break; |
| 498 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 499 | ReleaseAudioPatchConfigEventData *data = |
| 500 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 501 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 502 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 503 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 504 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 505 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 506 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 507 | { |
| 508 | Mutex::Autolock _l(event->mLock); |
| 509 | if (event->mWaitStatus) { |
| 510 | event->mWaitStatus = false; |
| 511 | event->mCond.signal(); |
| 512 | } |
| 513 | } |
| 514 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 515 | } |
| 516 | |
| 517 | if (configChanged) { |
| 518 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 519 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 522 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 523 | String8 s; |
| 524 | if (output) { |
| 525 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 526 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 527 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 528 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 529 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 530 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 531 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 532 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 533 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 534 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 535 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 536 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 537 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 538 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 539 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 540 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 541 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 542 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 543 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 544 | } else { |
| 545 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 546 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 547 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 548 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 549 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 550 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 551 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 552 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 553 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 554 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 555 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 556 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 557 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 558 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 559 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 560 | } |
| 561 | int len = s.length(); |
| 562 | if (s.length() > 2) { |
| 563 | char *str = s.lockBuffer(len); |
| 564 | s.unlockBuffer(len - 2); |
| 565 | } |
| 566 | return s; |
| 567 | } |
| 568 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 569 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 570 | { |
| 571 | const size_t SIZE = 256; |
| 572 | char buffer[SIZE]; |
| 573 | String8 result; |
| 574 | |
| 575 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 576 | if (!locked) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 577 | dprintf(fd, "thread %p maybe dead locked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 580 | dprintf(fd, " I/O handle: %d\n", mId); |
| 581 | dprintf(fd, " TID: %d\n", getTid()); |
| 582 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
| 583 | dprintf(fd, " Sample rate: %u\n", mSampleRate); |
| 584 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
| 585 | dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); |
| 586 | dprintf(fd, " Channel Count: %u\n", mChannelCount); |
| 587 | dprintf(fd, " Channel Mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 588 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 589 | dprintf(fd, " Format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 590 | dprintf(fd, " Frame size: %zu\n", mFrameSize); |
| 591 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 592 | size_t numConfig = mConfigEvents.size(); |
| 593 | if (numConfig) { |
| 594 | for (size_t i = 0; i < numConfig; i++) { |
| 595 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 596 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 597 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 598 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 599 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 600 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 601 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 602 | |
| 603 | if (locked) { |
| 604 | mLock.unlock(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 609 | { |
| 610 | const size_t SIZE = 256; |
| 611 | char buffer[SIZE]; |
| 612 | String8 result; |
| 613 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 614 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 615 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 616 | write(fd, buffer, strlen(buffer)); |
| 617 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 618 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 619 | sp<EffectChain> chain = mEffectChains[i]; |
| 620 | if (chain != 0) { |
| 621 | chain->dump(fd, args); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 626 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 | { |
| 628 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 629 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 632 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 633 | { |
| 634 | switch (mType) { |
| 635 | case MIXER: |
| 636 | return String16("AudioMix"); |
| 637 | case DIRECT: |
| 638 | return String16("AudioDirectOut"); |
| 639 | case DUPLICATING: |
| 640 | return String16("AudioDup"); |
| 641 | case RECORD: |
| 642 | return String16("AudioIn"); |
| 643 | case OFFLOAD: |
| 644 | return String16("AudioOffload"); |
| 645 | default: |
| 646 | ALOG_ASSERT(false); |
| 647 | return String16("AudioUnknown"); |
| 648 | } |
| 649 | } |
| 650 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 651 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 652 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 653 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | if (mPowerManager != 0) { |
| 655 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 656 | status_t status; |
| 657 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 658 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 659 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 660 | getWakeLockTag(), |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 661 | String16("media"), |
| 662 | uid); |
| 663 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 664 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 665 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 666 | getWakeLockTag(), |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 667 | String16("media")); |
| 668 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 669 | if (status == NO_ERROR) { |
| 670 | mWakeLockToken = binder; |
| 671 | } |
| 672 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 677 | { |
| 678 | Mutex::Autolock _l(mLock); |
| 679 | releaseWakeLock_l(); |
| 680 | } |
| 681 | |
| 682 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 683 | { |
| 684 | if (mWakeLockToken != 0) { |
| 685 | ALOGV("releaseWakeLock_l() %s", mName); |
| 686 | if (mPowerManager != 0) { |
| 687 | mPowerManager->releaseWakeLock(mWakeLockToken, 0); |
| 688 | } |
| 689 | mWakeLockToken.clear(); |
| 690 | } |
| 691 | } |
| 692 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 693 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 694 | Mutex::Autolock _l(mLock); |
| 695 | updateWakeLockUids_l(uids); |
| 696 | } |
| 697 | |
| 698 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
| 699 | |
| 700 | if (mPowerManager == 0) { |
| 701 | // use checkService() to avoid blocking if power service is not up yet |
| 702 | sp<IBinder> binder = |
| 703 | defaultServiceManager()->checkService(String16("power")); |
| 704 | if (binder == 0) { |
| 705 | ALOGW("Thread %s cannot connect to the power manager service", mName); |
| 706 | } else { |
| 707 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 708 | binder->linkToDeath(mDeathRecipient); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
| 714 | |
| 715 | getPowerManager_l(); |
| 716 | if (mWakeLockToken == NULL) { |
| 717 | ALOGE("no wake lock to update!"); |
| 718 | return; |
| 719 | } |
| 720 | if (mPowerManager != 0) { |
| 721 | sp<IBinder> binder = new BBinder(); |
| 722 | status_t status; |
| 723 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array()); |
| 724 | ALOGV("acquireWakeLock_l() %s status %d", mName, status); |
| 725 | } |
| 726 | } |
| 727 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 728 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 729 | { |
| 730 | Mutex::Autolock _l(mLock); |
| 731 | releaseWakeLock_l(); |
| 732 | mPowerManager.clear(); |
| 733 | } |
| 734 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 735 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 736 | { |
| 737 | sp<ThreadBase> thread = mThread.promote(); |
| 738 | if (thread != 0) { |
| 739 | thread->clearPowerManager(); |
| 740 | } |
| 741 | ALOGW("power manager service died !!!"); |
| 742 | } |
| 743 | |
| 744 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 745 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 746 | { |
| 747 | Mutex::Autolock _l(mLock); |
| 748 | setEffectSuspended_l(type, suspend, sessionId); |
| 749 | } |
| 750 | |
| 751 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 752 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 753 | { |
| 754 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 755 | if (chain != 0) { |
| 756 | if (type != NULL) { |
| 757 | chain->setEffectSuspended_l(type, suspend); |
| 758 | } else { |
| 759 | chain->setEffectSuspendedAll_l(suspend); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 764 | } |
| 765 | |
| 766 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 767 | { |
| 768 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 769 | if (index < 0) { |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 774 | mSuspendedSessions.valueAt(index); |
| 775 | |
| 776 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 777 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 778 | for (int j = 0; j < desc->mRefCount; j++) { |
| 779 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 780 | chain->setEffectSuspendedAll_l(true); |
| 781 | } else { |
| 782 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 783 | desc->mType.timeLow); |
| 784 | chain->setEffectSuspended_l(&desc->mType, true); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 791 | bool suspend, |
| 792 | int sessionId) |
| 793 | { |
| 794 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 795 | |
| 796 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 797 | |
| 798 | if (suspend) { |
| 799 | if (index >= 0) { |
| 800 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 801 | } else { |
| 802 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 803 | } |
| 804 | } else { |
| 805 | if (index < 0) { |
| 806 | return; |
| 807 | } |
| 808 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 809 | } |
| 810 | |
| 811 | |
| 812 | int key = EffectChain::kKeyForSuspendAll; |
| 813 | if (type != NULL) { |
| 814 | key = type->timeLow; |
| 815 | } |
| 816 | index = sessionEffects.indexOfKey(key); |
| 817 | |
| 818 | sp<SuspendedSessionDesc> desc; |
| 819 | if (suspend) { |
| 820 | if (index >= 0) { |
| 821 | desc = sessionEffects.valueAt(index); |
| 822 | } else { |
| 823 | desc = new SuspendedSessionDesc(); |
| 824 | if (type != NULL) { |
| 825 | desc->mType = *type; |
| 826 | } |
| 827 | sessionEffects.add(key, desc); |
| 828 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 829 | } |
| 830 | desc->mRefCount++; |
| 831 | } else { |
| 832 | if (index < 0) { |
| 833 | return; |
| 834 | } |
| 835 | desc = sessionEffects.valueAt(index); |
| 836 | if (--desc->mRefCount == 0) { |
| 837 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 838 | sessionEffects.removeItemsAt(index); |
| 839 | if (sessionEffects.isEmpty()) { |
| 840 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 841 | sessionId); |
| 842 | mSuspendedSessions.removeItem(sessionId); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | if (!sessionEffects.isEmpty()) { |
| 847 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 852 | bool enabled, |
| 853 | int sessionId) |
| 854 | { |
| 855 | Mutex::Autolock _l(mLock); |
| 856 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 857 | } |
| 858 | |
| 859 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 860 | bool enabled, |
| 861 | int sessionId) |
| 862 | { |
| 863 | if (mType != RECORD) { |
| 864 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 865 | // another session. This gives the priority to well behaved effect control panels |
| 866 | // and applications not using global effects. |
| 867 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 868 | // global effects |
| 869 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 870 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 875 | if (chain != 0) { |
| 876 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 881 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 882 | const sp<AudioFlinger::Client>& client, |
| 883 | const sp<IEffectClient>& effectClient, |
| 884 | int32_t priority, |
| 885 | int sessionId, |
| 886 | effect_descriptor_t *desc, |
| 887 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 888 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 889 | { |
| 890 | sp<EffectModule> effect; |
| 891 | sp<EffectHandle> handle; |
| 892 | status_t lStatus; |
| 893 | sp<EffectChain> chain; |
| 894 | bool chainCreated = false; |
| 895 | bool effectCreated = false; |
| 896 | bool effectRegistered = false; |
| 897 | |
| 898 | lStatus = initCheck(); |
| 899 | if (lStatus != NO_ERROR) { |
| 900 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 901 | goto Exit; |
| 902 | } |
| 903 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 904 | // Reject any effect on Direct output threads for now, since the format of |
| 905 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 906 | if (mType == DIRECT) { |
| 907 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", |
| 908 | desc->name, mName); |
| 909 | lStatus = BAD_VALUE; |
| 910 | goto Exit; |
| 911 | } |
| 912 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame^] | 913 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 914 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame^] | 915 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 916 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 917 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 918 | lStatus = BAD_VALUE; |
| 919 | goto Exit; |
| 920 | } |
| 921 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 922 | // Allow global effects only on offloaded and mixer threads |
| 923 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 924 | switch (mType) { |
| 925 | case MIXER: |
| 926 | case OFFLOAD: |
| 927 | break; |
| 928 | case DIRECT: |
| 929 | case DUPLICATING: |
| 930 | case RECORD: |
| 931 | default: |
| 932 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", desc->name, mName); |
| 933 | lStatus = BAD_VALUE; |
| 934 | goto Exit; |
| 935 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 936 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 937 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 938 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 939 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 940 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 941 | desc->name, desc->flags, mType); |
| 942 | lStatus = BAD_VALUE; |
| 943 | goto Exit; |
| 944 | } |
| 945 | |
| 946 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 947 | |
| 948 | { // scope for mLock |
| 949 | Mutex::Autolock _l(mLock); |
| 950 | |
| 951 | // check for existing effect chain with the requested audio session |
| 952 | chain = getEffectChain_l(sessionId); |
| 953 | if (chain == 0) { |
| 954 | // create a new chain for this session |
| 955 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 956 | chain = new EffectChain(this, sessionId); |
| 957 | addEffectChain_l(chain); |
| 958 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 959 | chainCreated = true; |
| 960 | } else { |
| 961 | effect = chain->getEffectFromDesc_l(desc); |
| 962 | } |
| 963 | |
| 964 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 965 | |
| 966 | if (effect == 0) { |
| 967 | int id = mAudioFlinger->nextUniqueId(); |
| 968 | // Check CPU and memory usage |
| 969 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 970 | if (lStatus != NO_ERROR) { |
| 971 | goto Exit; |
| 972 | } |
| 973 | effectRegistered = true; |
| 974 | // create a new effect module if none present in the chain |
| 975 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 976 | lStatus = effect->status(); |
| 977 | if (lStatus != NO_ERROR) { |
| 978 | goto Exit; |
| 979 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 980 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 981 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 982 | lStatus = chain->addEffect_l(effect); |
| 983 | if (lStatus != NO_ERROR) { |
| 984 | goto Exit; |
| 985 | } |
| 986 | effectCreated = true; |
| 987 | |
| 988 | effect->setDevice(mOutDevice); |
| 989 | effect->setDevice(mInDevice); |
| 990 | effect->setMode(mAudioFlinger->getMode()); |
| 991 | effect->setAudioSource(mAudioSource); |
| 992 | } |
| 993 | // create effect handle and connect it to effect module |
| 994 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 995 | lStatus = handle->initCheck(); |
| 996 | if (lStatus == OK) { |
| 997 | lStatus = effect->addHandle(handle.get()); |
| 998 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 999 | if (enabled != NULL) { |
| 1000 | *enabled = (int)effect->isEnabled(); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | Exit: |
| 1005 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1006 | Mutex::Autolock _l(mLock); |
| 1007 | if (effectCreated) { |
| 1008 | chain->removeEffect_l(effect); |
| 1009 | } |
| 1010 | if (effectRegistered) { |
| 1011 | AudioSystem::unregisterEffect(effect->id()); |
| 1012 | } |
| 1013 | if (chainCreated) { |
| 1014 | removeEffectChain_l(chain); |
| 1015 | } |
| 1016 | handle.clear(); |
| 1017 | } |
| 1018 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1019 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1020 | return handle; |
| 1021 | } |
| 1022 | |
| 1023 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) |
| 1024 | { |
| 1025 | Mutex::Autolock _l(mLock); |
| 1026 | return getEffect_l(sessionId, effectId); |
| 1027 | } |
| 1028 | |
| 1029 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 1030 | { |
| 1031 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1032 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1033 | } |
| 1034 | |
| 1035 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1036 | // PlaybackThread::mLock held |
| 1037 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1038 | { |
| 1039 | // check for existing effect chain with the requested audio session |
| 1040 | int sessionId = effect->sessionId(); |
| 1041 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1042 | bool chainCreated = false; |
| 1043 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1044 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1045 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1046 | this, effect->desc().name, effect->desc().flags); |
| 1047 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1048 | if (chain == 0) { |
| 1049 | // create a new chain for this session |
| 1050 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1051 | chain = new EffectChain(this, sessionId); |
| 1052 | addEffectChain_l(chain); |
| 1053 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1054 | chainCreated = true; |
| 1055 | } |
| 1056 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1057 | |
| 1058 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1059 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1060 | this, effect->desc().name, chain.get()); |
| 1061 | return BAD_VALUE; |
| 1062 | } |
| 1063 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1064 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1065 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1066 | status_t status = chain->addEffect_l(effect); |
| 1067 | if (status != NO_ERROR) { |
| 1068 | if (chainCreated) { |
| 1069 | removeEffectChain_l(chain); |
| 1070 | } |
| 1071 | return status; |
| 1072 | } |
| 1073 | |
| 1074 | effect->setDevice(mOutDevice); |
| 1075 | effect->setDevice(mInDevice); |
| 1076 | effect->setMode(mAudioFlinger->getMode()); |
| 1077 | effect->setAudioSource(mAudioSource); |
| 1078 | return NO_ERROR; |
| 1079 | } |
| 1080 | |
| 1081 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1082 | |
| 1083 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1084 | effect_descriptor_t desc = effect->desc(); |
| 1085 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1086 | detachAuxEffect_l(effect->id()); |
| 1087 | } |
| 1088 | |
| 1089 | sp<EffectChain> chain = effect->chain().promote(); |
| 1090 | if (chain != 0) { |
| 1091 | // remove effect chain if removing last effect |
| 1092 | if (chain->removeEffect_l(effect) == 0) { |
| 1093 | removeEffectChain_l(chain); |
| 1094 | } |
| 1095 | } else { |
| 1096 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1101 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1102 | { |
| 1103 | effectChains = mEffectChains; |
| 1104 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1105 | mEffectChains[i]->lock(); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1110 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1111 | { |
| 1112 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1113 | effectChains[i]->unlock(); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 1118 | { |
| 1119 | Mutex::Autolock _l(mLock); |
| 1120 | return getEffectChain_l(sessionId); |
| 1121 | } |
| 1122 | |
| 1123 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const |
| 1124 | { |
| 1125 | size_t size = mEffectChains.size(); |
| 1126 | for (size_t i = 0; i < size; i++) { |
| 1127 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1128 | return mEffectChains[i]; |
| 1129 | } |
| 1130 | } |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1135 | { |
| 1136 | Mutex::Autolock _l(mLock); |
| 1137 | size_t size = mEffectChains.size(); |
| 1138 | for (size_t i = 0; i < size; i++) { |
| 1139 | mEffectChains[i]->setMode_l(mode); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect, |
| 1144 | EffectHandle *handle, |
| 1145 | bool unpinIfLast) { |
| 1146 | |
| 1147 | Mutex::Autolock _l(mLock); |
| 1148 | ALOGV("disconnectEffect() %p effect %p", this, effect.get()); |
| 1149 | // delete the effect module if removing last handle on it |
| 1150 | if (effect->removeHandle(handle) == 0) { |
| 1151 | if (!effect->isPinned() || unpinIfLast) { |
| 1152 | removeEffect_l(effect); |
| 1153 | AudioSystem::unregisterEffect(effect->id()); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1158 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1159 | { |
| 1160 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1161 | config->ext.mix.handle = mId; |
| 1162 | config->sample_rate = mSampleRate; |
| 1163 | config->format = mFormat; |
| 1164 | config->channel_mask = mChannelMask; |
| 1165 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1166 | AUDIO_PORT_CONFIG_FORMAT; |
| 1167 | } |
| 1168 | |
| 1169 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1170 | // ---------------------------------------------------------------------------- |
| 1171 | // Playback |
| 1172 | // ---------------------------------------------------------------------------- |
| 1173 | |
| 1174 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1175 | AudioStreamOut* output, |
| 1176 | audio_io_handle_t id, |
| 1177 | audio_devices_t device, |
| 1178 | type_t type) |
| 1179 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1180 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1181 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1182 | mMixerBuffer(NULL), |
| 1183 | mMixerBufferSize(0), |
| 1184 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1185 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1186 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1187 | mEffectBuffer(NULL), |
| 1188 | mEffectBufferSize(0), |
| 1189 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1190 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1191 | mSuspended(0), mBytesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1192 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1193 | // mStreamTypes[] initialized in constructor body |
| 1194 | mOutput(output), |
| 1195 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1196 | mMixerStatus(MIXER_IDLE), |
| 1197 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
| 1198 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1199 | mBytesRemaining(0), |
| 1200 | mCurrentWriteLength(0), |
| 1201 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1202 | mWriteAckSequence(0), |
| 1203 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1204 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1205 | mScreenState(AudioFlinger::mScreenState), |
| 1206 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1207 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
| 1208 | // mLatchD, mLatchQ, |
| 1209 | mLatchDValid(false), mLatchQValid(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1210 | { |
| 1211 | snprintf(mName, kNameLength, "AudioOut_%X", id); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1212 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1213 | |
| 1214 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1215 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1216 | // parameter. |
| 1217 | // |
| 1218 | // If the HAL we are using has support for master volume or master mute, |
| 1219 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1220 | // and the mute set to false). |
| 1221 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1222 | mMasterMute = audioFlinger->masterMute_l(); |
| 1223 | if (mOutput && mOutput->audioHwDev) { |
| 1224 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1225 | mMasterVolume = 1.0; |
| 1226 | } |
| 1227 | |
| 1228 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1229 | mMasterMute = false; |
| 1230 | } |
| 1231 | } |
| 1232 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1233 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1234 | |
| 1235 | // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor |
| 1236 | // There is no AUDIO_STREAM_MIN, and ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1237 | 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] | 1238 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1239 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1240 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1241 | } |
| 1242 | // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here, |
| 1243 | // because mAudioFlinger doesn't have one to copy from |
| 1244 | } |
| 1245 | |
| 1246 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1247 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1248 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1249 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1250 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1251 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1255 | { |
| 1256 | dumpInternals(fd, args); |
| 1257 | dumpTracks(fd, args); |
| 1258 | dumpEffectChains(fd, args); |
| 1259 | } |
| 1260 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1261 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1262 | { |
| 1263 | const size_t SIZE = 256; |
| 1264 | char buffer[SIZE]; |
| 1265 | String8 result; |
| 1266 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1267 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1268 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1269 | const stream_type_t *st = &mStreamTypes[i]; |
| 1270 | if (i > 0) { |
| 1271 | result.appendFormat(", "); |
| 1272 | } |
| 1273 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1274 | if (st->mute) { |
| 1275 | result.append("M"); |
| 1276 | } |
| 1277 | } |
| 1278 | result.append("\n"); |
| 1279 | write(fd, result.string(), result.length()); |
| 1280 | result.clear(); |
| 1281 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1282 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1283 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1284 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1285 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1286 | |
| 1287 | size_t numtracks = mTracks.size(); |
| 1288 | size_t numactive = mActiveTracks.size(); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1289 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1290 | size_t numactiveseen = 0; |
| 1291 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1292 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1293 | Track::appendDumpHeader(result); |
| 1294 | for (size_t i = 0; i < numtracks; ++i) { |
| 1295 | sp<Track> track = mTracks[i]; |
| 1296 | if (track != 0) { |
| 1297 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1298 | if (active) { |
| 1299 | numactiveseen++; |
| 1300 | } |
| 1301 | track->dump(buffer, SIZE, active); |
| 1302 | result.append(buffer); |
| 1303 | } |
| 1304 | } |
| 1305 | } else { |
| 1306 | result.append("\n"); |
| 1307 | } |
| 1308 | if (numactiveseen != numactive) { |
| 1309 | // some tracks in the active list were not in the tracks list |
| 1310 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1311 | " not in the track list\n"); |
| 1312 | result.append(buffer); |
| 1313 | Track::appendDumpHeader(result); |
| 1314 | for (size_t i = 0; i < numactive; ++i) { |
| 1315 | sp<Track> track = mActiveTracks[i].promote(); |
| 1316 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1317 | track->dump(buffer, SIZE, true); |
| 1318 | result.append(buffer); |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1327 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1328 | dprintf(fd, "\nOutput thread %p:\n", this); |
| 1329 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
| 1330 | dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1331 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1332 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1333 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1334 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1335 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1336 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1337 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1338 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1339 | |
| 1340 | dumpBase(fd, args); |
| 1341 | } |
| 1342 | |
| 1343 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1344 | |
| 1345 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1346 | { |
| 1347 | run(mName, ANDROID_PRIORITY_URGENT_AUDIO); |
| 1348 | } |
| 1349 | |
| 1350 | // ThreadBase virtuals |
| 1351 | void AudioFlinger::PlaybackThread::preExit() |
| 1352 | { |
| 1353 | ALOGV(" preExit()"); |
| 1354 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1355 | // converted to use audio HAL extensions required to support tunneling |
| 1356 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1357 | } |
| 1358 | |
| 1359 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1360 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1361 | const sp<AudioFlinger::Client>& client, |
| 1362 | audio_stream_type_t streamType, |
| 1363 | uint32_t sampleRate, |
| 1364 | audio_format_t format, |
| 1365 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1366 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1367 | const sp<IMemory>& sharedBuffer, |
| 1368 | int sessionId, |
| 1369 | IAudioFlinger::track_flags_t *flags, |
| 1370 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1371 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1372 | status_t *status) |
| 1373 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1374 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1375 | sp<Track> track; |
| 1376 | status_t lStatus; |
| 1377 | |
| 1378 | bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1379 | |
| 1380 | // client expresses a preference for FAST, but we get the final say |
| 1381 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1382 | if ( |
| 1383 | // not timed |
| 1384 | (!isTimed) && |
| 1385 | // either of these use cases: |
| 1386 | ( |
| 1387 | // use case 1: shared buffer with any frame count |
| 1388 | ( |
| 1389 | (sharedBuffer != 0) |
| 1390 | ) || |
| 1391 | // use case 2: callback handler and frame count is default or at least as large as HAL |
| 1392 | ( |
| 1393 | (tid != -1) && |
| 1394 | ((frameCount == 0) || |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1395 | (frameCount >= mFrameCount)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1396 | ) |
| 1397 | ) && |
| 1398 | // PCM data |
| 1399 | audio_is_linear_pcm(format) && |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1400 | // identical channel mask to sink, or mono in and stereo sink |
| 1401 | (channelMask == mChannelMask || |
| 1402 | (channelMask == AUDIO_CHANNEL_OUT_MONO && |
| 1403 | mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1404 | // hardware sample rate |
| 1405 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1406 | // normal mixer has an associated fast mixer |
| 1407 | hasFastMixer() && |
| 1408 | // there are sufficient fast track slots available |
| 1409 | (mFastTrackAvailMask != 0) |
| 1410 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1411 | // FIXME add a permission test also? |
| 1412 | ) { |
| 1413 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1414 | if (frameCount == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1415 | // read the fast track multiplier property the first time it is needed |
| 1416 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1417 | if (ok != 0) { |
| 1418 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1419 | } |
| 1420 | frameCount = mFrameCount * sFastTrackMultiplier; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1421 | } |
| 1422 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1423 | frameCount, mFrameCount); |
| 1424 | } else { |
| 1425 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1426 | "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
| 1427 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1428 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1429 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1430 | audio_is_linear_pcm(format), |
| 1431 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1432 | *flags &= ~IAudioFlinger::TRACK_FAST; |
| 1433 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1434 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1435 | // This is probably too conservative, but legacy application code may depend on it. |
| 1436 | // If you change this calculation, also review the start threshold which is related. |
| 1437 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1438 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1439 | if (minBufCount < 2) { |
| 1440 | minBufCount = 2; |
| 1441 | } |
| 1442 | size_t minFrameCount = mNormalFrameCount * minBufCount; |
| 1443 | if (frameCount < minFrameCount) { |
| 1444 | frameCount = minFrameCount; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1448 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1449 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1450 | switch (mType) { |
| 1451 | |
| 1452 | case DIRECT: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1453 | if (audio_is_linear_pcm(format)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1454 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1455 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1456 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1457 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1458 | lStatus = BAD_VALUE; |
| 1459 | goto Exit; |
| 1460 | } |
| 1461 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1462 | break; |
| 1463 | |
| 1464 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1465 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1466 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1467 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1468 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1469 | lStatus = BAD_VALUE; |
| 1470 | goto Exit; |
| 1471 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | |
| 1474 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1475 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1476 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1477 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1478 | format, mOutput, mFormat); |
| 1479 | lStatus = BAD_VALUE; |
| 1480 | goto Exit; |
| 1481 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1482 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
| 1483 | if (sampleRate > mSampleRate*2) { |
| 1484 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1485 | lStatus = BAD_VALUE; |
| 1486 | goto Exit; |
| 1487 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1488 | break; |
| 1489 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | lStatus = initCheck(); |
| 1493 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1494 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1495 | goto Exit; |
| 1496 | } |
| 1497 | |
| 1498 | { // scope for mLock |
| 1499 | Mutex::Autolock _l(mLock); |
| 1500 | |
| 1501 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1502 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1503 | // manager |
| 1504 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1505 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1506 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1507 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1508 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1509 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1510 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1511 | strategy, actual); |
| 1512 | lStatus = BAD_VALUE; |
| 1513 | goto Exit; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | if (!isTimed) { |
| 1519 | track = new Track(this, client, streamType, sampleRate, format, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1520 | channelMask, frameCount, NULL, sharedBuffer, |
| 1521 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1522 | } else { |
| 1523 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1524 | channelMask, frameCount, sharedBuffer, sessionId, uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1525 | } |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1526 | |
| 1527 | // new Track always returns non-NULL, |
| 1528 | // but TimedTrack::create() is a factory that could fail by returning NULL |
| 1529 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1530 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1531 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1532 | // 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] | 1533 | goto Exit; |
| 1534 | } |
| 1535 | mTracks.add(track); |
| 1536 | |
| 1537 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1538 | if (chain != 0) { |
| 1539 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1540 | track->setMainBuffer(chain->inBuffer()); |
| 1541 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1542 | chain->incTrackCnt(); |
| 1543 | } |
| 1544 | |
| 1545 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1546 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1547 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1548 | // so ask activity manager to do this on our behalf |
| 1549 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | lStatus = NO_ERROR; |
| 1554 | |
| 1555 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1556 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1557 | return track; |
| 1558 | } |
| 1559 | |
| 1560 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1561 | { |
| 1562 | return latency; |
| 1563 | } |
| 1564 | |
| 1565 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1566 | { |
| 1567 | Mutex::Autolock _l(mLock); |
| 1568 | return latency_l(); |
| 1569 | } |
| 1570 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1571 | { |
| 1572 | if (initCheck() == NO_ERROR) { |
| 1573 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1574 | } else { |
| 1575 | return 0; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1580 | { |
| 1581 | Mutex::Autolock _l(mLock); |
| 1582 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1583 | if (mOutput && mOutput->audioHwDev && |
| 1584 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1585 | mMasterVolume = 1.0; |
| 1586 | } else { |
| 1587 | mMasterVolume = value; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1592 | { |
| 1593 | Mutex::Autolock _l(mLock); |
| 1594 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1595 | if (mOutput && mOutput->audioHwDev && |
| 1596 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1597 | mMasterMute = false; |
| 1598 | } else { |
| 1599 | mMasterMute = muted; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 1604 | { |
| 1605 | Mutex::Autolock _l(mLock); |
| 1606 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1607 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 1611 | { |
| 1612 | Mutex::Autolock _l(mLock); |
| 1613 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1614 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 1618 | { |
| 1619 | Mutex::Autolock _l(mLock); |
| 1620 | return mStreamTypes[stream].volume; |
| 1621 | } |
| 1622 | |
| 1623 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1624 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1625 | { |
| 1626 | status_t status = ALREADY_EXISTS; |
| 1627 | |
| 1628 | // set retry count for buffer fill |
| 1629 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1630 | if (mActiveTracks.indexOf(track) < 0) { |
| 1631 | // the track is newly added, make sure it fills up all its |
| 1632 | // buffers before playing. This is to ensure the client will |
| 1633 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1634 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1635 | TrackBase::track_state state = track->mState; |
| 1636 | mLock.unlock(); |
| 1637 | status = AudioSystem::startOutput(mId, track->streamType(), track->sessionId()); |
| 1638 | mLock.lock(); |
| 1639 | // abort track was stopped/paused while we released the lock |
| 1640 | if (state != track->mState) { |
| 1641 | if (status == NO_ERROR) { |
| 1642 | mLock.unlock(); |
| 1643 | AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); |
| 1644 | mLock.lock(); |
| 1645 | } |
| 1646 | return INVALID_OPERATION; |
| 1647 | } |
| 1648 | // abort if start is rejected by audio policy manager |
| 1649 | if (status != NO_ERROR) { |
| 1650 | return PERMISSION_DENIED; |
| 1651 | } |
| 1652 | #ifdef ADD_BATTERY_DATA |
| 1653 | // to track the speaker usage |
| 1654 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 1655 | #endif |
| 1656 | } |
| 1657 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1658 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 | track->mResetDone = false; |
| 1660 | track->mPresentationCompleteFrames = 0; |
| 1661 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1662 | mWakeLockUids.add(track->uid()); |
| 1663 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1664 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1665 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1666 | if (chain != 0) { |
| 1667 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 1668 | track->sessionId()); |
| 1669 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | status = NO_ERROR; |
| 1673 | } |
| 1674 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1675 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1676 | return status; |
| 1677 | } |
| 1678 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1679 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
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 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1683 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 1684 | track->mState = TrackBase::STOPPED; |
| 1685 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1686 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1687 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1688 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1689 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1690 | |
| 1691 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1695 | { |
| 1696 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 1697 | mTracks.remove(track); |
| 1698 | deleteTrackName_l(track->name()); |
| 1699 | // redundant as track is about to be destroyed, for dumpsys only |
| 1700 | track->mName = -1; |
| 1701 | if (track->isFastTrack()) { |
| 1702 | int index = track->mFastIndex; |
| 1703 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 1704 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1705 | mFastTrackAvailMask |= 1 << index; |
| 1706 | // redundant as track is about to be destroyed, for dumpsys only |
| 1707 | track->mFastIndex = -1; |
| 1708 | } |
| 1709 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1710 | if (chain != 0) { |
| 1711 | chain->decTrackCnt(); |
| 1712 | } |
| 1713 | } |
| 1714 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1715 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1716 | { |
| 1717 | // Thread could be blocked waiting for async |
| 1718 | // so signal it to handle state changes immediately |
| 1719 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1720 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1721 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1722 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1723 | } |
| 1724 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1726 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1727 | Mutex::Autolock _l(mLock); |
| 1728 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1729 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1732 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 1733 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | free(s); |
| 1735 | return out_s8; |
| 1736 | } |
| 1737 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1738 | void AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1739 | AudioSystem::OutputDescriptor desc; |
| 1740 | void *param2 = NULL; |
| 1741 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1742 | ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1743 | param); |
| 1744 | |
| 1745 | switch (event) { |
| 1746 | case AudioSystem::OUTPUT_OPENED: |
| 1747 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 1748 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1749 | desc.samplingRate = mSampleRate; |
| 1750 | desc.format = mFormat; |
| 1751 | desc.frameCount = mNormalFrameCount; // FIXME see |
| 1752 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 1753 | desc.latency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1754 | param2 = &desc; |
| 1755 | break; |
| 1756 | |
| 1757 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1758 | param2 = ¶m; |
| 1759 | case AudioSystem::OUTPUT_CLOSED: |
| 1760 | default: |
| 1761 | break; |
| 1762 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1763 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1766 | void AudioFlinger::PlaybackThread::writeCallback() |
| 1767 | { |
| 1768 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1769 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | void AudioFlinger::PlaybackThread::drainCallback() |
| 1773 | { |
| 1774 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1775 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1778 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1779 | { |
| 1780 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1781 | // reject out of sequence requests |
| 1782 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 1783 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1784 | mWaitWorkCV.signal(); |
| 1785 | } |
| 1786 | } |
| 1787 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1788 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1789 | { |
| 1790 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1791 | // reject out of sequence requests |
| 1792 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 1793 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1794 | mWaitWorkCV.signal(); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | // static |
| 1799 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1800 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1801 | void *cookie) |
| 1802 | { |
| 1803 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 1804 | ALOGV("asyncCallback() event %d", event); |
| 1805 | switch (event) { |
| 1806 | case STREAM_CBK_EVENT_WRITE_READY: |
| 1807 | me->writeCallback(); |
| 1808 | break; |
| 1809 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 1810 | me->drainCallback(); |
| 1811 | break; |
| 1812 | default: |
| 1813 | ALOGW("asyncCallback() unknown event %d", event); |
| 1814 | break; |
| 1815 | } |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1819 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1820 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1821 | // 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] | 1822 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
| 1823 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1824 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1825 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1826 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1827 | if ((mType == MIXER || mType == DUPLICATING) |
| 1828 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 1829 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 1830 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1831 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 1832 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 1833 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
| 1834 | mFormat = mHALFormat; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1835 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1836 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1837 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1838 | if ((mType == MIXER || mType == DUPLICATING) |
| 1839 | && !isValidPcmSinkFormat(mFormat)) { |
| 1840 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 1841 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1842 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 1843 | mFrameSize = audio_stream_out_frame_size(mOutput->stream); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 1844 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 1845 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1846 | if (mFrameCount & 15) { |
| 1847 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 1848 | mFrameCount); |
| 1849 | } |
| 1850 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1851 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 1852 | (mOutput->stream->set_callback != NULL)) { |
| 1853 | if (mOutput->stream->set_callback(mOutput->stream, |
| 1854 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 1855 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 1856 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1860 | // 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] | 1861 | double multiplier = 1.0; |
| 1862 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 1863 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1864 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 1865 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1866 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 1867 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 1868 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 1869 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 1870 | maxNormalFrameCount = minNormalFrameCount; |
| 1871 | } |
| 1872 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 1873 | if (multiplier <= 1.0) { |
| 1874 | multiplier = 1.0; |
| 1875 | } else if (multiplier <= 2.0) { |
| 1876 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 1877 | multiplier = 2.0; |
| 1878 | } else { |
| 1879 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 1880 | } |
| 1881 | } else { |
| 1882 | // 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] | 1883 | // 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] | 1884 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 1885 | // constraint) |
| 1886 | // FIXME this rounding up should not be done if no HAL SRC |
| 1887 | uint32_t truncMult = (uint32_t) multiplier; |
| 1888 | if ((truncMult & 1)) { |
| 1889 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 1890 | ++truncMult; |
| 1891 | } |
| 1892 | } |
| 1893 | multiplier = (double) truncMult; |
| 1894 | } |
| 1895 | } |
| 1896 | mNormalFrameCount = multiplier * mFrameCount; |
| 1897 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1898 | if (mType == MIXER || mType == DUPLICATING) { |
| 1899 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 1900 | } |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 1901 | 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] | 1902 | mNormalFrameCount); |
| 1903 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1904 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 1905 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 1906 | free(mSinkBuffer); |
| 1907 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 1908 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 1909 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 1910 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1911 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1912 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1913 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 1914 | // drives the output. |
| 1915 | free(mMixerBuffer); |
| 1916 | mMixerBuffer = NULL; |
| 1917 | if (mMixerBufferEnabled) { |
| 1918 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 1919 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 1920 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 1921 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 1922 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1923 | free(mEffectBuffer); |
| 1924 | mEffectBuffer = NULL; |
| 1925 | if (mEffectBufferEnabled) { |
| 1926 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 1927 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 1928 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 1929 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 1930 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1931 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1932 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 1933 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1934 | // 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] | 1935 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 1936 | // matter. |
| 1937 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 1938 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 1939 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1940 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1945 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1946 | { |
| 1947 | if (halFrames == NULL || dspFrames == NULL) { |
| 1948 | return BAD_VALUE; |
| 1949 | } |
| 1950 | Mutex::Autolock _l(mLock); |
| 1951 | if (initCheck() != NO_ERROR) { |
| 1952 | return INVALID_OPERATION; |
| 1953 | } |
| 1954 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 1955 | *halFrames = framesWritten; |
| 1956 | |
| 1957 | if (isSuspended()) { |
| 1958 | // return an estimation of rendered frames when the output is suspended |
| 1959 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
| 1960 | *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; |
| 1961 | return NO_ERROR; |
| 1962 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1963 | status_t status; |
| 1964 | uint32_t frames; |
| 1965 | status = mOutput->stream->get_render_position(mOutput->stream, &frames); |
| 1966 | *dspFrames = (size_t)frames; |
| 1967 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const |
| 1972 | { |
| 1973 | Mutex::Autolock _l(mLock); |
| 1974 | uint32_t result = 0; |
| 1975 | if (getEffectChain_l(sessionId) != 0) { |
| 1976 | result = EFFECT_SESSION; |
| 1977 | } |
| 1978 | |
| 1979 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1980 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1981 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1982 | result |= TRACK_SESSION; |
| 1983 | break; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | return result; |
| 1988 | } |
| 1989 | |
| 1990 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 1991 | { |
| 1992 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 1993 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 1994 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1995 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 1996 | } |
| 1997 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 1998 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1999 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2000 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2001 | } |
| 2002 | } |
| 2003 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
| 2008 | { |
| 2009 | Mutex::Autolock _l(mLock); |
| 2010 | return mOutput; |
| 2011 | } |
| 2012 | |
| 2013 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2014 | { |
| 2015 | Mutex::Autolock _l(mLock); |
| 2016 | AudioStreamOut *output = mOutput; |
| 2017 | mOutput = NULL; |
| 2018 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2019 | // must push a NULL and wait for ack |
| 2020 | mOutputSink.clear(); |
| 2021 | mPipeSink.clear(); |
| 2022 | mNormalSink.clear(); |
| 2023 | return output; |
| 2024 | } |
| 2025 | |
| 2026 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2027 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2028 | { |
| 2029 | if (mOutput == NULL) { |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | return &mOutput->stream->common; |
| 2033 | } |
| 2034 | |
| 2035 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2036 | { |
| 2037 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2038 | } |
| 2039 | |
| 2040 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2041 | { |
| 2042 | if (!isValidSyncEvent(event)) { |
| 2043 | return BAD_VALUE; |
| 2044 | } |
| 2045 | |
| 2046 | Mutex::Autolock _l(mLock); |
| 2047 | |
| 2048 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2049 | sp<Track> track = mTracks[i]; |
| 2050 | if (event->triggerSession() == track->sessionId()) { |
| 2051 | (void) track->setSyncEvent(event); |
| 2052 | return NO_ERROR; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | return NAME_NOT_FOUND; |
| 2057 | } |
| 2058 | |
| 2059 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2060 | { |
| 2061 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2062 | } |
| 2063 | |
| 2064 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2065 | const Vector< sp<Track> >& tracksToRemove) |
| 2066 | { |
| 2067 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2068 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2069 | for (size_t i = 0 ; i < count ; i++) { |
| 2070 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2071 | if (track->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2072 | AudioSystem::stopOutput(mId, track->streamType(), track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2073 | #ifdef ADD_BATTERY_DATA |
| 2074 | // to track the speaker usage |
| 2075 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2076 | #endif |
| 2077 | if (track->isTerminated()) { |
| 2078 | AudioSystem::releaseOutput(mId); |
| 2079 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2086 | { |
| 2087 | if (!mMasterMute) { |
| 2088 | char value[PROPERTY_VALUE_MAX]; |
| 2089 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2090 | char *endptr; |
| 2091 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2092 | if (*endptr == '\0' && ul != 0) { |
| 2093 | ALOGD("Silence is golden"); |
| 2094 | // The setprop command will not allow a property to be changed after |
| 2095 | // the first time it is set, so we don't have to worry about un-muting. |
| 2096 | setMasterMute_l(true); |
| 2097 | } |
| 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2103 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2104 | { |
| 2105 | // FIXME rewrite to reduce number of system calls |
| 2106 | mLastWriteTime = systemTime(); |
| 2107 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2108 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2109 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2110 | |
| 2111 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2112 | if (mNormalSink != 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2113 | const size_t count = mBytesRemaining / mFrameSize; |
| 2114 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2115 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2116 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2117 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2118 | if (screenState != mScreenState) { |
| 2119 | mScreenState = screenState; |
| 2120 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2121 | if (pipe != NULL) { |
| 2122 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2123 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2124 | } |
| 2125 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2126 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2127 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2128 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2129 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2130 | } else { |
| 2131 | bytesWritten = framesWritten; |
| 2132 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2133 | status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2134 | if (status == NO_ERROR) { |
| 2135 | size_t totalFramesWritten = mNormalSink->framesWritten(); |
| 2136 | if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { |
| 2137 | mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; |
| 2138 | mLatchDValid = true; |
| 2139 | } |
| 2140 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2141 | // otherwise use the HAL / AudioStreamOut directly |
| 2142 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2143 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2144 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2145 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2146 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2147 | mWriteAckSequence += 2; |
| 2148 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2149 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2150 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2151 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2152 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2153 | // They are used e.g for multichannel PCM playback over HDMI. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2154 | bytesWritten = mOutput->stream->write(mOutput->stream, |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2155 | (char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2156 | if (mUseAsyncWrite && |
| 2157 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2158 | // 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] | 2159 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2160 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2161 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2162 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2163 | } |
| 2164 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2165 | mNumWrites++; |
| 2166 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2167 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2168 | return bytesWritten; |
| 2169 | } |
| 2170 | |
| 2171 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2172 | { |
| 2173 | if (mOutput->stream->drain) { |
| 2174 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2175 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2176 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2177 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2178 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2179 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2180 | } |
| 2181 | mOutput->stream->drain(mOutput->stream, |
| 2182 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2183 | : AUDIO_DRAIN_ALL); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2188 | { |
| 2189 | // Default implementation has nothing to do |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2194 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2195 | - activeSleepTime from activeSleepTimeUs() |
| 2196 | - idleSleepTime from idleSleepTimeUs() |
| 2197 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 2198 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2199 | |
| 2200 | The parameters that affect these derived values are: |
| 2201 | - frame count |
| 2202 | - frame size |
| 2203 | - sample rate |
| 2204 | - device type: A2DP or not |
| 2205 | - device latency |
| 2206 | - format: PCM or not |
| 2207 | - active sleep time |
| 2208 | - idle sleep time |
| 2209 | */ |
| 2210 | |
| 2211 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2212 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2213 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2214 | activeSleepTime = activeSleepTimeUs(); |
| 2215 | idleSleepTime = idleSleepTimeUs(); |
| 2216 | } |
| 2217 | |
| 2218 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2219 | { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2220 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2221 | this, streamType, mTracks.size()); |
| 2222 | Mutex::Autolock _l(mLock); |
| 2223 | |
| 2224 | size_t size = mTracks.size(); |
| 2225 | for (size_t i = 0; i < size; i++) { |
| 2226 | sp<Track> t = mTracks[i]; |
| 2227 | if (t->streamType() == streamType) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2228 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2234 | { |
| 2235 | int session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2236 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2237 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2238 | bool ownsBuffer = false; |
| 2239 | |
| 2240 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 2241 | if (session > 0) { |
| 2242 | // 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] | 2243 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2244 | if (mType != DIRECT) { |
| 2245 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2246 | buffer = new int16_t[numSamples]; |
| 2247 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2248 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2249 | ownsBuffer = true; |
| 2250 | } |
| 2251 | |
| 2252 | // Attach all tracks with same session ID to this chain. |
| 2253 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2254 | sp<Track> track = mTracks[i]; |
| 2255 | if (session == track->sessionId()) { |
| 2256 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2257 | buffer); |
| 2258 | track->setMainBuffer(buffer); |
| 2259 | chain->incTrackCnt(); |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | // indicate all active tracks in the chain |
| 2264 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2265 | sp<Track> track = mActiveTracks[i].promote(); |
| 2266 | if (track == 0) { |
| 2267 | continue; |
| 2268 | } |
| 2269 | if (session == track->sessionId()) { |
| 2270 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2271 | chain->incActiveTrackCnt(); |
| 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2277 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2278 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2279 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
| 2280 | // chains list in order to be processed last as it contains output stage effects |
| 2281 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2282 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
| 2283 | // after track specific effects and before output stage |
| 2284 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 2285 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
| 2286 | // Effect chain for other sessions are inserted at beginning of effect |
| 2287 | // chains list to be processed before output mix effects. Relative order between other |
| 2288 | // sessions is not important |
| 2289 | size_t size = mEffectChains.size(); |
| 2290 | size_t i = 0; |
| 2291 | for (i = 0; i < size; i++) { |
| 2292 | if (mEffectChains[i]->sessionId() < session) { |
| 2293 | break; |
| 2294 | } |
| 2295 | } |
| 2296 | mEffectChains.insertAt(chain, i); |
| 2297 | checkSuspendOnAddEffectChain_l(chain); |
| 2298 | |
| 2299 | return NO_ERROR; |
| 2300 | } |
| 2301 | |
| 2302 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2303 | { |
| 2304 | int session = chain->sessionId(); |
| 2305 | |
| 2306 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2307 | |
| 2308 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2309 | if (chain == mEffectChains[i]) { |
| 2310 | mEffectChains.removeAt(i); |
| 2311 | // detach all active tracks from the chain |
| 2312 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2313 | sp<Track> track = mActiveTracks[i].promote(); |
| 2314 | if (track == 0) { |
| 2315 | continue; |
| 2316 | } |
| 2317 | if (session == track->sessionId()) { |
| 2318 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2319 | chain.get(), session); |
| 2320 | chain->decActiveTrackCnt(); |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | // detach all tracks with same session ID from this chain |
| 2325 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2326 | sp<Track> track = mTracks[i]; |
| 2327 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2328 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2329 | chain->decTrackCnt(); |
| 2330 | } |
| 2331 | } |
| 2332 | break; |
| 2333 | } |
| 2334 | } |
| 2335 | return mEffectChains.size(); |
| 2336 | } |
| 2337 | |
| 2338 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2339 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2340 | { |
| 2341 | Mutex::Autolock _l(mLock); |
| 2342 | return attachAuxEffect_l(track, EffectId); |
| 2343 | } |
| 2344 | |
| 2345 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2346 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2347 | { |
| 2348 | status_t status = NO_ERROR; |
| 2349 | |
| 2350 | if (EffectId == 0) { |
| 2351 | track->setAuxBuffer(0, NULL); |
| 2352 | } else { |
| 2353 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2354 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2355 | if (effect != 0) { |
| 2356 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2357 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2358 | } else { |
| 2359 | status = INVALID_OPERATION; |
| 2360 | } |
| 2361 | } else { |
| 2362 | status = BAD_VALUE; |
| 2363 | } |
| 2364 | } |
| 2365 | return status; |
| 2366 | } |
| 2367 | |
| 2368 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2369 | { |
| 2370 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2371 | sp<Track> track = mTracks[i]; |
| 2372 | if (track->auxEffectId() == effectId) { |
| 2373 | attachAuxEffect_l(track, 0); |
| 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2379 | { |
| 2380 | Vector< sp<Track> > tracksToRemove; |
| 2381 | |
| 2382 | standbyTime = systemTime(); |
| 2383 | |
| 2384 | // MIXER |
| 2385 | nsecs_t lastWarning = 0; |
| 2386 | |
| 2387 | // DUPLICATING |
| 2388 | // FIXME could this be made local to while loop? |
| 2389 | writeFrames = 0; |
| 2390 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2391 | int lastGeneration = 0; |
| 2392 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2393 | cacheParameters_l(); |
| 2394 | sleepTime = idleSleepTime; |
| 2395 | |
| 2396 | if (mType == MIXER) { |
| 2397 | sleepTimeShift = 0; |
| 2398 | } |
| 2399 | |
| 2400 | CpuStats cpuStats; |
| 2401 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2402 | |
| 2403 | acquireWakeLock(); |
| 2404 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2405 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2406 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2407 | // and then that string will be logged at the next convenient opportunity. |
| 2408 | const char *logString = NULL; |
| 2409 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2410 | checkSilentMode_l(); |
| 2411 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2412 | while (!exitPending()) |
| 2413 | { |
| 2414 | cpuStats.sample(myName); |
| 2415 | |
| 2416 | Vector< sp<EffectChain> > effectChains; |
| 2417 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2418 | { // scope for mLock |
| 2419 | |
| 2420 | Mutex::Autolock _l(mLock); |
| 2421 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2422 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2423 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2424 | if (logString != NULL) { |
| 2425 | mNBLogWriter->logTimestamp(); |
| 2426 | mNBLogWriter->log(logString); |
| 2427 | logString = NULL; |
| 2428 | } |
| 2429 | |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2430 | if (mLatchDValid) { |
| 2431 | mLatchQ = mLatchD; |
| 2432 | mLatchDValid = false; |
| 2433 | mLatchQValid = true; |
| 2434 | } |
| 2435 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2436 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2437 | if (mSignalPending) { |
| 2438 | // A signal was raised while we were unlocked |
| 2439 | mSignalPending = false; |
| 2440 | } else if (waitingAsyncCallback_l()) { |
| 2441 | if (exitPending()) { |
| 2442 | break; |
| 2443 | } |
| 2444 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2445 | mWakeLockUids.clear(); |
| 2446 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2447 | ALOGV("wait async completion"); |
| 2448 | mWaitWorkCV.wait(mLock); |
| 2449 | ALOGV("async completion/wake"); |
| 2450 | acquireWakeLock_l(); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 2451 | standbyTime = systemTime() + standbyDelay; |
| 2452 | sleepTime = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2453 | |
| 2454 | continue; |
| 2455 | } |
| 2456 | if ((!mActiveTracks.size() && systemTime() > standbyTime) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2457 | isSuspended()) { |
| 2458 | // put audio hardware into standby after short delay |
| 2459 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2460 | |
| 2461 | threadLoop_standby(); |
| 2462 | |
| 2463 | mStandby = true; |
| 2464 | } |
| 2465 | |
| 2466 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2467 | // we're about to wait, flush the binder command buffer |
| 2468 | IPCThreadState::self()->flushCommands(); |
| 2469 | |
| 2470 | clearOutputTracks(); |
| 2471 | |
| 2472 | if (exitPending()) { |
| 2473 | break; |
| 2474 | } |
| 2475 | |
| 2476 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2477 | mWakeLockUids.clear(); |
| 2478 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2479 | // wait until we have something to do... |
| 2480 | ALOGV("%s going to sleep", myName.string()); |
| 2481 | mWaitWorkCV.wait(mLock); |
| 2482 | ALOGV("%s waking up", myName.string()); |
| 2483 | acquireWakeLock_l(); |
| 2484 | |
| 2485 | mMixerStatus = MIXER_IDLE; |
| 2486 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2487 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2488 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2489 | checkSilentMode_l(); |
| 2490 | |
| 2491 | standbyTime = systemTime() + standbyDelay; |
| 2492 | sleepTime = idleSleepTime; |
| 2493 | if (mType == MIXER) { |
| 2494 | sleepTimeShift = 0; |
| 2495 | } |
| 2496 | |
| 2497 | continue; |
| 2498 | } |
| 2499 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2500 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2501 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2502 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2503 | // compare with previously applied list |
| 2504 | if (lastGeneration != mActiveTracksGeneration) { |
| 2505 | // update wakelock |
| 2506 | updateWakeLockUids_l(mWakeLockUids); |
| 2507 | lastGeneration = mActiveTracksGeneration; |
| 2508 | } |
| 2509 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2510 | // prevent any changes in effect chain list and in each effect chain |
| 2511 | // during mixing and effect process as the audio buffers could be deleted |
| 2512 | // or modified if an effect is created or deleted |
| 2513 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2514 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2515 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2516 | if (mBytesRemaining == 0) { |
| 2517 | mCurrentWriteLength = 0; |
| 2518 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 2519 | // threadLoop_mix() sets mCurrentWriteLength |
| 2520 | threadLoop_mix(); |
| 2521 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 2522 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
| 2523 | // threadLoop_sleepTime sets sleepTime to 0 if data |
| 2524 | // must be written to HAL |
| 2525 | threadLoop_sleepTime(); |
| 2526 | if (sleepTime == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2527 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2528 | } |
| 2529 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2530 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
| 2531 | // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. |
| 2532 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 2533 | // or mSinkBuffer (if there are no effects). |
| 2534 | // |
| 2535 | // This is done pre-effects computation; if effects change to |
| 2536 | // support higher precision, this needs to move. |
| 2537 | // |
| 2538 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
| 2539 | // TODO use sleepTime == 0 as an additional condition. |
| 2540 | if (mMixerBufferValid) { |
| 2541 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 2542 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 2543 | |
| 2544 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 2545 | mNormalFrameCount * mChannelCount); |
| 2546 | } |
| 2547 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2548 | mBytesRemaining = mCurrentWriteLength; |
| 2549 | if (isSuspended()) { |
| 2550 | sleepTime = suspendSleepTimeUs(); |
| 2551 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2552 | mBytesWritten += mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2553 | mBytesRemaining = 0; |
| 2554 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2555 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2556 | // only process effects if we're going to write |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2557 | if (sleepTime == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2558 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2559 | effectChains[i]->process_l(); |
| 2560 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2561 | } |
| 2562 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2563 | // Process effect chains for offloaded thread even if no audio |
| 2564 | // was read from audio track: process only updates effect state |
| 2565 | // and thus does have to be synchronized with audio writes but may have |
| 2566 | // to be called while waiting for async write callback |
| 2567 | if (mType == OFFLOAD) { |
| 2568 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2569 | effectChains[i]->process_l(); |
| 2570 | } |
| 2571 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2572 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2573 | // Only if the Effects buffer is enabled and there is data in the |
| 2574 | // Effects buffer (buffer valid), we need to |
| 2575 | // copy into the sink buffer. |
| 2576 | // TODO use sleepTime == 0 as an additional condition. |
| 2577 | if (mEffectBufferValid) { |
| 2578 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
| 2579 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 2580 | mNormalFrameCount * mChannelCount); |
| 2581 | } |
| 2582 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2583 | // enable changes in effect chain |
| 2584 | unlockEffectChains(effectChains); |
| 2585 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2586 | if (!waitingAsyncCallback()) { |
| 2587 | // sleepTime == 0 means we must write to audio hardware |
| 2588 | if (sleepTime == 0) { |
| 2589 | if (mBytesRemaining) { |
| 2590 | ssize_t ret = threadLoop_write(); |
| 2591 | if (ret < 0) { |
| 2592 | mBytesRemaining = 0; |
| 2593 | } else { |
| 2594 | mBytesWritten += ret; |
| 2595 | mBytesRemaining -= ret; |
| 2596 | } |
| 2597 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 2598 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 2599 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2600 | } |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2601 | if (mType == MIXER) { |
| 2602 | // write blocked detection |
| 2603 | nsecs_t now = systemTime(); |
| 2604 | nsecs_t delta = now - mLastWriteTime; |
| 2605 | if (!mStandby && delta > maxPeriod) { |
| 2606 | mNumDelayedWrites++; |
| 2607 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2608 | ATRACE_NAME("underrun"); |
| 2609 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2610 | ns2ms(delta), mNumDelayedWrites, this); |
| 2611 | lastWarning = now; |
| 2612 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2613 | } |
| 2614 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2615 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2616 | } else { |
| 2617 | usleep(sleepTime); |
| 2618 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | // Finally let go of removed track(s), without the lock held |
| 2622 | // since we can't guarantee the destructors won't acquire that |
| 2623 | // same lock. This will also mutate and push a new fast mixer state. |
| 2624 | threadLoop_removeTracks(tracksToRemove); |
| 2625 | tracksToRemove.clear(); |
| 2626 | |
| 2627 | // FIXME I don't understand the need for this here; |
| 2628 | // it was in the original code but maybe the |
| 2629 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2630 | clearOutputTracks(); |
| 2631 | |
| 2632 | // Effect chains will be actually deleted here if they were removed from |
| 2633 | // mEffectChains list during mixing or effects processing |
| 2634 | effectChains.clear(); |
| 2635 | |
| 2636 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2637 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2638 | } |
| 2639 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2640 | threadLoop_exit(); |
| 2641 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 2642 | if (!mStandby) { |
| 2643 | threadLoop_standby(); |
| 2644 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2648 | mWakeLockUids.clear(); |
| 2649 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2650 | |
| 2651 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2652 | return false; |
| 2653 | } |
| 2654 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2655 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 2656 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 2657 | { |
| 2658 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2659 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2660 | for (size_t i=0 ; i<count ; i++) { |
| 2661 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 2662 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2663 | mWakeLockUids.remove(track->uid()); |
| 2664 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2665 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 2666 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2667 | if (chain != 0) { |
| 2668 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 2669 | track->sessionId()); |
| 2670 | chain->decActiveTrackCnt(); |
| 2671 | } |
| 2672 | if (track->isTerminated()) { |
| 2673 | removeTrack_l(track); |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2679 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2680 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 2681 | { |
| 2682 | if (mNormalSink != 0) { |
| 2683 | return mNormalSink->getTimestamp(timestamp); |
| 2684 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2685 | if ((mType == OFFLOAD || mType == DIRECT) && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2686 | uint64_t position64; |
| 2687 | int ret = mOutput->stream->get_presentation_position( |
| 2688 | mOutput->stream, &position64, ×tamp.mTime); |
| 2689 | if (ret == 0) { |
| 2690 | timestamp.mPosition = (uint32_t)position64; |
| 2691 | return NO_ERROR; |
| 2692 | } |
| 2693 | } |
| 2694 | return INVALID_OPERATION; |
| 2695 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2696 | |
| 2697 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 2698 | audio_patch_handle_t *handle) |
| 2699 | { |
| 2700 | status_t status = NO_ERROR; |
| 2701 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2702 | // store new device and send to effects |
| 2703 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 2704 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 2705 | type |= patch->sinks[i].ext.device.type; |
| 2706 | } |
| 2707 | mOutDevice = type; |
| 2708 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2709 | mEffectChains[i]->setDevice_l(mOutDevice); |
| 2710 | } |
| 2711 | |
| 2712 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2713 | status = hwDevice->create_audio_patch(hwDevice, |
| 2714 | patch->num_sources, |
| 2715 | patch->sources, |
| 2716 | patch->num_sinks, |
| 2717 | patch->sinks, |
| 2718 | handle); |
| 2719 | } else { |
| 2720 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 2721 | } |
| 2722 | return status; |
| 2723 | } |
| 2724 | |
| 2725 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 2726 | { |
| 2727 | status_t status = NO_ERROR; |
| 2728 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2729 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2730 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 2731 | } else { |
| 2732 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 2733 | } |
| 2734 | return status; |
| 2735 | } |
| 2736 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2737 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 2738 | { |
| 2739 | Mutex::Autolock _l(mLock); |
| 2740 | mTracks.add(track); |
| 2741 | } |
| 2742 | |
| 2743 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 2744 | { |
| 2745 | Mutex::Autolock _l(mLock); |
| 2746 | destroyTrack_l(track); |
| 2747 | } |
| 2748 | |
| 2749 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 2750 | { |
| 2751 | ThreadBase::getAudioPortConfig(config); |
| 2752 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 2753 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 2754 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 2755 | } |
| 2756 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2757 | // ---------------------------------------------------------------------------- |
| 2758 | |
| 2759 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 2760 | audio_io_handle_t id, audio_devices_t device, type_t type) |
| 2761 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2762 | // mAudioMixer below |
| 2763 | // mFastMixer below |
| 2764 | mFastMixerFutex(0) |
| 2765 | // mOutputSink below |
| 2766 | // mPipeSink below |
| 2767 | // mNormalSink below |
| 2768 | { |
| 2769 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 2770 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2771 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2772 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2773 | mNormalFrameCount); |
| 2774 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2775 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2776 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 2777 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 2778 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 2779 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2780 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2781 | ALOG_ASSERT(index == 0); |
| 2782 | |
| 2783 | // initialize fast mixer depending on configuration |
| 2784 | bool initFastMixer; |
| 2785 | switch (kUseFastMixer) { |
| 2786 | case FastMixer_Never: |
| 2787 | initFastMixer = false; |
| 2788 | break; |
| 2789 | case FastMixer_Always: |
| 2790 | initFastMixer = true; |
| 2791 | break; |
| 2792 | case FastMixer_Static: |
| 2793 | case FastMixer_Dynamic: |
| 2794 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 2795 | break; |
| 2796 | } |
| 2797 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 2798 | audio_format_t fastMixerFormat; |
| 2799 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2800 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2801 | } else { |
| 2802 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2803 | } |
| 2804 | if (mFormat != fastMixerFormat) { |
| 2805 | // change our Sink format to accept our intermediate precision |
| 2806 | mFormat = fastMixerFormat; |
| 2807 | free(mSinkBuffer); |
| 2808 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2809 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 2810 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 2811 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2812 | |
| 2813 | // create a MonoPipe to connect our submix to FastMixer |
| 2814 | NBAIO_Format format = mOutputSink->format(); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 2815 | // adjust format to match that of the Fast Mixer |
| 2816 | format.mFormat = fastMixerFormat; |
| 2817 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 2818 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2819 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 2820 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 2821 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 2822 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 2823 | const NBAIO_Format offers[1] = {format}; |
| 2824 | size_t numCounterOffers = 0; |
| 2825 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2826 | ALOG_ASSERT(index == 0); |
| 2827 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 2828 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2829 | mPipeSink = monoPipe; |
| 2830 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2831 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2832 | if (mTeeSinkOutputEnabled) { |
| 2833 | // create a Pipe to archive a copy of FastMixer's output for dumpsys |
| 2834 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, format); |
| 2835 | numCounterOffers = 0; |
| 2836 | index = teeSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 2837 | ALOG_ASSERT(index == 0); |
| 2838 | mTeeSink = teeSink; |
| 2839 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 2840 | numCounterOffers = 0; |
| 2841 | index = teeSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 2842 | ALOG_ASSERT(index == 0); |
| 2843 | mTeeSource = teeSource; |
| 2844 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2845 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2846 | |
| 2847 | // create fast mixer and configure it initially with just one fast track for our submix |
| 2848 | mFastMixer = new FastMixer(); |
| 2849 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2850 | #ifdef STATE_QUEUE_DUMP |
| 2851 | sq->setObserverDump(&mStateQueueObserverDump); |
| 2852 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 2853 | #endif |
| 2854 | FastMixerState *state = sq->begin(); |
| 2855 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2856 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 2857 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 2858 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 2859 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 2860 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2861 | fastTrack->mGeneration++; |
| 2862 | state->mFastTracksGen++; |
| 2863 | state->mTrackMask = 1; |
| 2864 | // fast mixer will use the HAL output sink |
| 2865 | state->mOutputSink = mOutputSink.get(); |
| 2866 | state->mOutputSinkGen++; |
| 2867 | state->mFrameCount = mFrameCount; |
| 2868 | state->mCommand = FastMixerState::COLD_IDLE; |
| 2869 | // already done in constructor initialization list |
| 2870 | //mFastMixerFutex = 0; |
| 2871 | state->mColdFutexAddr = &mFastMixerFutex; |
| 2872 | state->mColdGen++; |
| 2873 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2874 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2875 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2876 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2877 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 2878 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2879 | sq->end(); |
| 2880 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2881 | |
| 2882 | // start the fast mixer |
| 2883 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 2884 | pid_t tid = mFastMixer->getTid(); |
| 2885 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 2886 | if (err != 0) { |
| 2887 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2888 | kPriorityFastMixer, getpid_cached, tid, err); |
| 2889 | } |
| 2890 | |
| 2891 | #ifdef AUDIO_WATCHDOG |
| 2892 | // create and start the watchdog |
| 2893 | mAudioWatchdog = new AudioWatchdog(); |
| 2894 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 2895 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 2896 | tid = mAudioWatchdog->getTid(); |
| 2897 | err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 2898 | if (err != 0) { |
| 2899 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 2900 | kPriorityFastMixer, getpid_cached, tid, err); |
| 2901 | } |
| 2902 | #endif |
| 2903 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | switch (kUseFastMixer) { |
| 2907 | case FastMixer_Never: |
| 2908 | case FastMixer_Dynamic: |
| 2909 | mNormalSink = mOutputSink; |
| 2910 | break; |
| 2911 | case FastMixer_Always: |
| 2912 | mNormalSink = mPipeSink; |
| 2913 | break; |
| 2914 | case FastMixer_Static: |
| 2915 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 2916 | break; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | AudioFlinger::MixerThread::~MixerThread() |
| 2921 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2922 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2923 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2924 | FastMixerState *state = sq->begin(); |
| 2925 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2926 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2927 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 2928 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | state->mCommand = FastMixerState::EXIT; |
| 2932 | sq->end(); |
| 2933 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2934 | mFastMixer->join(); |
| 2935 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 2936 | // We'll use that extract the final state which contains one remaining fast track |
| 2937 | // corresponding to our sub-mix. |
| 2938 | state = sq->begin(); |
| 2939 | ALOG_ASSERT(state->mTrackMask == 1); |
| 2940 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 2941 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 2942 | delete fastTrack->mBufferProvider; |
| 2943 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2944 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2945 | #ifdef AUDIO_WATCHDOG |
| 2946 | if (mAudioWatchdog != 0) { |
| 2947 | mAudioWatchdog->requestExit(); |
| 2948 | mAudioWatchdog->requestExitAndWait(); |
| 2949 | mAudioWatchdog.clear(); |
| 2950 | } |
| 2951 | #endif |
| 2952 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2953 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2954 | delete mAudioMixer; |
| 2955 | } |
| 2956 | |
| 2957 | |
| 2958 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 2959 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2960 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2961 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2962 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 2963 | } |
| 2964 | return latency; |
| 2965 | } |
| 2966 | |
| 2967 | |
| 2968 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 2969 | { |
| 2970 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 2971 | } |
| 2972 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2973 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2974 | { |
| 2975 | // FIXME we should only do one push per cycle; confirm this is true |
| 2976 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 2977 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2978 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 2979 | FastMixerState *state = sq->begin(); |
| 2980 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 2981 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 2982 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 2983 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 2984 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 2985 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2986 | } |
| 2987 | #ifdef AUDIO_WATCHDOG |
| 2988 | if (mAudioWatchdog != 0) { |
| 2989 | mAudioWatchdog->resume(); |
| 2990 | } |
| 2991 | #endif |
| 2992 | } |
| 2993 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2994 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
| 2995 | FastMixerDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2996 | sq->end(); |
| 2997 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 2998 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 2999 | mNormalSink = mPipeSink; |
| 3000 | } |
| 3001 | } else { |
| 3002 | sq->end(false /*didModify*/); |
| 3003 | } |
| 3004 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3005 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3009 | { |
| 3010 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3011 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3012 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3013 | FastMixerState *state = sq->begin(); |
| 3014 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3015 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3016 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3017 | state->mColdGen++; |
| 3018 | mFastMixerFutex = 0; |
| 3019 | sq->end(); |
| 3020 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3021 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3022 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3023 | mNormalSink = mOutputSink; |
| 3024 | } |
| 3025 | #ifdef AUDIO_WATCHDOG |
| 3026 | if (mAudioWatchdog != 0) { |
| 3027 | mAudioWatchdog->pause(); |
| 3028 | } |
| 3029 | #endif |
| 3030 | } else { |
| 3031 | sq->end(false /*didModify*/); |
| 3032 | } |
| 3033 | } |
| 3034 | PlaybackThread::threadLoop_standby(); |
| 3035 | } |
| 3036 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3037 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3038 | { |
| 3039 | return false; |
| 3040 | } |
| 3041 | |
| 3042 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3043 | { |
| 3044 | return !mStandby; |
| 3045 | } |
| 3046 | |
| 3047 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3048 | { |
| 3049 | Mutex::Autolock _l(mLock); |
| 3050 | return waitingAsyncCallback_l(); |
| 3051 | } |
| 3052 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3053 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3054 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3055 | { |
| 3056 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
| 3057 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3058 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3059 | // discard any pending drain or write ack by incrementing sequence |
| 3060 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3061 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3062 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3063 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3064 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3065 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3068 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3069 | { |
| 3070 | ALOGV("signal playback thread"); |
| 3071 | broadcast_l(); |
| 3072 | } |
| 3073 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3074 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3075 | { |
| 3076 | // obtain the presentation timestamp of the next output buffer |
| 3077 | int64_t pts; |
| 3078 | status_t status = INVALID_OPERATION; |
| 3079 | |
| 3080 | if (mNormalSink != 0) { |
| 3081 | status = mNormalSink->getNextWriteTimestamp(&pts); |
| 3082 | } else { |
| 3083 | status = mOutputSink->getNextWriteTimestamp(&pts); |
| 3084 | } |
| 3085 | |
| 3086 | if (status != NO_ERROR) { |
| 3087 | pts = AudioBufferProvider::kInvalidPTS; |
| 3088 | } |
| 3089 | |
| 3090 | // mix buffers... |
| 3091 | mAudioMixer->process(pts); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3092 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3093 | // increase sleep time progressively when application underrun condition clears. |
| 3094 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3095 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3096 | // such that we would underrun the audio HAL. |
| 3097 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 3098 | sleepTimeShift--; |
| 3099 | } |
| 3100 | sleepTime = 0; |
| 3101 | standbyTime = systemTime() + standbyDelay; |
| 3102 | //TODO: delay standby when effects have a tail |
| 3103 | } |
| 3104 | |
| 3105 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3106 | { |
| 3107 | // If no tracks are ready, sleep once for the duration of an output |
| 3108 | // buffer size, then write 0s to the output |
| 3109 | if (sleepTime == 0) { |
| 3110 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 3111 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 3112 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 3113 | sleepTime = kMinThreadSleepTimeUs; |
| 3114 | } |
| 3115 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3116 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3117 | // duration we would end up writing less data than needed by the audio HAL if |
| 3118 | // the condition persists. |
| 3119 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3120 | sleepTimeShift++; |
| 3121 | } |
| 3122 | } else { |
| 3123 | sleepTime = idleSleepTime; |
| 3124 | } |
| 3125 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3126 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3127 | // before effects processing or output. |
| 3128 | if (mMixerBufferValid) { |
| 3129 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3130 | } else { |
| 3131 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3132 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3133 | sleepTime = 0; |
| 3134 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3135 | "anticipated start"); |
| 3136 | } |
| 3137 | // TODO add standby time extension fct of effect tail |
| 3138 | } |
| 3139 | |
| 3140 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3141 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3142 | Vector< sp<Track> > *tracksToRemove) |
| 3143 | { |
| 3144 | |
| 3145 | mixer_state mixerStatus = MIXER_IDLE; |
| 3146 | // find out which tracks need to be processed |
| 3147 | size_t count = mActiveTracks.size(); |
| 3148 | size_t mixedTracks = 0; |
| 3149 | size_t tracksWithEffect = 0; |
| 3150 | // counts only _active_ fast tracks |
| 3151 | size_t fastTracks = 0; |
| 3152 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3153 | |
| 3154 | float masterVolume = mMasterVolume; |
| 3155 | bool masterMute = mMasterMute; |
| 3156 | |
| 3157 | if (masterMute) { |
| 3158 | masterVolume = 0; |
| 3159 | } |
| 3160 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3161 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3162 | if (chain != 0) { |
| 3163 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3164 | chain->setVolume_l(&v, &v); |
| 3165 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3166 | chain.clear(); |
| 3167 | } |
| 3168 | |
| 3169 | // prepare a new state to push |
| 3170 | FastMixerStateQueue *sq = NULL; |
| 3171 | FastMixerState *state = NULL; |
| 3172 | bool didModify = false; |
| 3173 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3174 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3175 | sq = mFastMixer->sq(); |
| 3176 | state = sq->begin(); |
| 3177 | } |
| 3178 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3179 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3180 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3181 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3182 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3183 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3184 | if (t == 0) { |
| 3185 | continue; |
| 3186 | } |
| 3187 | |
| 3188 | // this const just means the local variable doesn't change |
| 3189 | Track* const track = t.get(); |
| 3190 | |
| 3191 | // process fast tracks |
| 3192 | if (track->isFastTrack()) { |
| 3193 | |
| 3194 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3195 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3196 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3197 | // The converse, of removing an (active) track and then creating a new track |
| 3198 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3199 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3200 | int j = track->mFastIndex; |
| 3201 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3202 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3203 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3204 | |
| 3205 | // Determine whether the track is currently in underrun condition, |
| 3206 | // and whether it had a recent underrun. |
| 3207 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3208 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3209 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3210 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3211 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3212 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3213 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3214 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3215 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3216 | track->mObservedUnderruns = underruns; |
| 3217 | // don't count underruns that occur while stopping or pausing |
| 3218 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3219 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3220 | recentUnderruns > 0) { |
| 3221 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3222 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | // This is similar to the state machine for normal tracks, |
| 3226 | // with a few modifications for fast tracks. |
| 3227 | bool isActive = true; |
| 3228 | switch (track->mState) { |
| 3229 | case TrackBase::STOPPING_1: |
| 3230 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3231 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3232 | track->mState = TrackBase::STOPPING_2; |
| 3233 | } |
| 3234 | break; |
| 3235 | case TrackBase::PAUSING: |
| 3236 | // ramp down is not yet implemented |
| 3237 | track->setPaused(); |
| 3238 | break; |
| 3239 | case TrackBase::RESUMING: |
| 3240 | // ramp up is not yet implemented |
| 3241 | track->mState = TrackBase::ACTIVE; |
| 3242 | break; |
| 3243 | case TrackBase::ACTIVE: |
| 3244 | if (recentFull > 0 || recentPartial > 0) { |
| 3245 | // track has provided at least some frames recently: reset retry count |
| 3246 | track->mRetryCount = kMaxTrackRetries; |
| 3247 | } |
| 3248 | if (recentUnderruns == 0) { |
| 3249 | // no recent underruns: stay active |
| 3250 | break; |
| 3251 | } |
| 3252 | // there has recently been an underrun of some kind |
| 3253 | if (track->sharedBuffer() == 0) { |
| 3254 | // were any of the recent underruns "empty" (no frames available)? |
| 3255 | if (recentEmpty == 0) { |
| 3256 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3257 | break; |
| 3258 | } |
| 3259 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3260 | if (--(track->mRetryCount) > 0) { |
| 3261 | break; |
| 3262 | } |
| 3263 | // indicate to client process that the track was disabled because of underrun; |
| 3264 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3265 | android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3266 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3267 | isActive = false; |
| 3268 | break; |
| 3269 | } |
| 3270 | // fall through |
| 3271 | case TrackBase::STOPPING_2: |
| 3272 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3273 | case TrackBase::STOPPED: |
| 3274 | case TrackBase::FLUSHED: // flush() while active |
| 3275 | // Check for presentation complete if track is inactive |
| 3276 | // We have consumed all the buffers of this track. |
| 3277 | // This would be incomplete if we auto-paused on underrun |
| 3278 | { |
| 3279 | size_t audioHALFrames = |
| 3280 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3281 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3282 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3283 | // track stays in active list until presentation is complete |
| 3284 | break; |
| 3285 | } |
| 3286 | } |
| 3287 | if (track->isStopping_2()) { |
| 3288 | track->mState = TrackBase::STOPPED; |
| 3289 | } |
| 3290 | if (track->isStopped()) { |
| 3291 | // Can't reset directly, as fast mixer is still polling this track |
| 3292 | // track->reset(); |
| 3293 | // So instead mark this track as needing to be reset after push with ack |
| 3294 | resetMask |= 1 << i; |
| 3295 | } |
| 3296 | isActive = false; |
| 3297 | break; |
| 3298 | case TrackBase::IDLE: |
| 3299 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3300 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | if (isActive) { |
| 3304 | // was it previously inactive? |
| 3305 | if (!(state->mTrackMask & (1 << j))) { |
| 3306 | ExtendedAudioBufferProvider *eabp = track; |
| 3307 | VolumeProvider *vp = track; |
| 3308 | fastTrack->mBufferProvider = eabp; |
| 3309 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3310 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3311 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3312 | fastTrack->mGeneration++; |
| 3313 | state->mTrackMask |= 1 << j; |
| 3314 | didModify = true; |
| 3315 | // no acknowledgement required for newly active tracks |
| 3316 | } |
| 3317 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3318 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3319 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3320 | ++fastTracks; |
| 3321 | } else { |
| 3322 | // was it previously active? |
| 3323 | if (state->mTrackMask & (1 << j)) { |
| 3324 | fastTrack->mBufferProvider = NULL; |
| 3325 | fastTrack->mGeneration++; |
| 3326 | state->mTrackMask &= ~(1 << j); |
| 3327 | didModify = true; |
| 3328 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3329 | // because we're about to decrement the last sp<> on those tracks. |
| 3330 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3331 | } else { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3332 | LOG_ALWAYS_FATAL("fast track %d should have been active", j); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3333 | } |
| 3334 | tracksToRemove->add(track); |
| 3335 | // Avoids a misleading display in dumpsys |
| 3336 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 3337 | } |
| 3338 | continue; |
| 3339 | } |
| 3340 | |
| 3341 | { // local variable scope to avoid goto warning |
| 3342 | |
| 3343 | audio_track_cblk_t* cblk = track->cblk(); |
| 3344 | |
| 3345 | // The first time a track is added we wait |
| 3346 | // for all its buffers to be filled before processing it |
| 3347 | int name = track->name(); |
| 3348 | // make sure that we have enough frames to mix one full buffer. |
| 3349 | // enforce this condition only once to enable draining the buffer in case the client |
| 3350 | // app does not call stop() and relies on underrun to stop: |
| 3351 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 3352 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3353 | size_t desiredFrames; |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3354 | uint32_t sr = track->sampleRate(); |
| 3355 | if (sr == mSampleRate) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3356 | desiredFrames = mNormalFrameCount; |
| 3357 | } else { |
| 3358 | // +1 for rounding and +1 for additional sample needed for interpolation |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3359 | desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3360 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 3361 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3362 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3363 | #if 0 |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3364 | // the minimum track buffer size is normally twice the number of frames necessary |
| 3365 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 3366 | // of unreleased frames after each pass, but just in case... |
| 3367 | ALOG_ASSERT(desiredFrames <= cblk->frameCount_); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3368 | #endif |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3369 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3370 | uint32_t minFrames = 1; |
| 3371 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 3372 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3373 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3374 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3375 | |
| 3376 | size_t framesReady = track->framesReady(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3377 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3378 | !track->isPaused() && !track->isTerminated()) |
| 3379 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3380 | 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] | 3381 | |
| 3382 | mixedTracks++; |
| 3383 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3384 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 3385 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3386 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3387 | if (track->mainBuffer() != mSinkBuffer && |
| 3388 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3389 | if (mEffectBufferEnabled) { |
| 3390 | mEffectBufferValid = true; // Later can set directly. |
| 3391 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3392 | chain = getEffectChain_l(track->sessionId()); |
| 3393 | // Delegate volume control to effect in track effect chain if needed |
| 3394 | if (chain != 0) { |
| 3395 | tracksWithEffect++; |
| 3396 | } else { |
| 3397 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 3398 | "session %d", |
| 3399 | name, track->sessionId()); |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | |
| 3404 | int param = AudioMixer::VOLUME; |
| 3405 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3406 | // no ramp for the first volume setting |
| 3407 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3408 | if (track->mState == TrackBase::RESUMING) { |
| 3409 | track->mState = TrackBase::ACTIVE; |
| 3410 | param = AudioMixer::RAMP_VOLUME; |
| 3411 | } |
| 3412 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3413 | // FIXME should not make a decision based on mServer |
| 3414 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3415 | // If the track is stopped before the first frame was mixed, |
| 3416 | // do not apply ramp |
| 3417 | param = AudioMixer::RAMP_VOLUME; |
| 3418 | } |
| 3419 | |
| 3420 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3421 | uint32_t vl, vr; // in U8.24 integer format |
| 3422 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3423 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3424 | vl = vr = 0; |
| 3425 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3426 | if (track->isPausing()) { |
| 3427 | track->setPaused(); |
| 3428 | } |
| 3429 | } else { |
| 3430 | |
| 3431 | // read original volumes with volume control |
| 3432 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3433 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3434 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3435 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3436 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3437 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3438 | // 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] | 3439 | if (vlf > GAIN_FLOAT_UNITY) { |
| 3440 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 3441 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3442 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3443 | if (vrf > GAIN_FLOAT_UNITY) { |
| 3444 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 3445 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3446 | } |
| 3447 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3448 | vlf *= v; |
| 3449 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3450 | // 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] | 3451 | // then derive vl and vr as U8.24 versions for the effect chain |
| 3452 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 3453 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 3454 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 3455 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3456 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3457 | // send level comes from shared memory and so may be corrupt |
| 3458 | if (sendLevel > MAX_GAIN_INT) { |
| 3459 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 3460 | sendLevel = MAX_GAIN_INT; |
| 3461 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3462 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 3463 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3464 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3465 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3466 | // Delegate volume control to effect in track effect chain if needed |
| 3467 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 3468 | // Do not ramp volume if volume is controlled by effect |
| 3469 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 3470 | // Update remaining floating point volume levels |
| 3471 | vlf = (float)vl / (1 << 24); |
| 3472 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3473 | track->mHasVolumeController = true; |
| 3474 | } else { |
| 3475 | // force no volume ramp when volume controller was just disabled or removed |
| 3476 | // from effect chain to avoid volume spike |
| 3477 | if (track->mHasVolumeController) { |
| 3478 | param = AudioMixer::VOLUME; |
| 3479 | } |
| 3480 | track->mHasVolumeController = false; |
| 3481 | } |
| 3482 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3483 | // XXX: these things DON'T need to be done each time |
| 3484 | mAudioMixer->setBufferProvider(name, track); |
| 3485 | mAudioMixer->enable(name); |
| 3486 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3487 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 3488 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 3489 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3490 | mAudioMixer->setParameter( |
| 3491 | name, |
| 3492 | AudioMixer::TRACK, |
| 3493 | AudioMixer::FORMAT, (void *)track->format()); |
| 3494 | mAudioMixer->setParameter( |
| 3495 | name, |
| 3496 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3497 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3498 | mAudioMixer->setParameter( |
| 3499 | name, |
| 3500 | AudioMixer::TRACK, |
| 3501 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3502 | // limit track sample rate to 2 x output sample rate, which changes at re-configuration |
| 3503 | uint32_t maxSampleRate = mSampleRate * 2; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3504 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3505 | if (reqSampleRate == 0) { |
| 3506 | reqSampleRate = mSampleRate; |
| 3507 | } else if (reqSampleRate > maxSampleRate) { |
| 3508 | reqSampleRate = maxSampleRate; |
| 3509 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3510 | mAudioMixer->setParameter( |
| 3511 | name, |
| 3512 | AudioMixer::RESAMPLE, |
| 3513 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3514 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3515 | /* |
| 3516 | * Select the appropriate output buffer for the track. |
| 3517 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3518 | * Tracks with effects go into their own effects chain buffer |
| 3519 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3520 | * |
| 3521 | * Other tracks can use mMixerBuffer for higher precision |
| 3522 | * channel accumulation. If this buffer is enabled |
| 3523 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 3524 | * into it. |
| 3525 | * |
| 3526 | */ |
| 3527 | if (mMixerBufferEnabled |
| 3528 | && (track->mainBuffer() == mSinkBuffer |
| 3529 | || track->mainBuffer() == mMixerBuffer)) { |
| 3530 | mAudioMixer->setParameter( |
| 3531 | name, |
| 3532 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3533 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3534 | mAudioMixer->setParameter( |
| 3535 | name, |
| 3536 | AudioMixer::TRACK, |
| 3537 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 3538 | // TODO: override track->mainBuffer()? |
| 3539 | mMixerBufferValid = true; |
| 3540 | } else { |
| 3541 | mAudioMixer->setParameter( |
| 3542 | name, |
| 3543 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3544 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3545 | mAudioMixer->setParameter( |
| 3546 | name, |
| 3547 | AudioMixer::TRACK, |
| 3548 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 3549 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3550 | mAudioMixer->setParameter( |
| 3551 | name, |
| 3552 | AudioMixer::TRACK, |
| 3553 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 3554 | |
| 3555 | // reset retry count |
| 3556 | track->mRetryCount = kMaxTrackRetries; |
| 3557 | |
| 3558 | // If one track is ready, set the mixer ready if: |
| 3559 | // - the mixer was not ready during previous round OR |
| 3560 | // - no other track is not ready |
| 3561 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 3562 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 3563 | mixerStatus = MIXER_TRACKS_READY; |
| 3564 | } |
| 3565 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3566 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3567 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3568 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3569 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 3570 | // previous audio buffer again to effects |
| 3571 | chain = getEffectChain_l(track->sessionId()); |
| 3572 | if (chain != 0) { |
| 3573 | chain->clearInputBuffer(); |
| 3574 | } |
| 3575 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3576 | 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] | 3577 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3578 | track->isStopped() || track->isPaused()) { |
| 3579 | // We have consumed all the buffers of this track. |
| 3580 | // Remove it from the list of active tracks. |
| 3581 | // TODO: use actual buffer filling status instead of latency when available from |
| 3582 | // audio HAL |
| 3583 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 3584 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3585 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3586 | if (track->isStopped()) { |
| 3587 | track->reset(); |
| 3588 | } |
| 3589 | tracksToRemove->add(track); |
| 3590 | } |
| 3591 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3592 | // No buffers for this track. Give it a few chances to |
| 3593 | // fill a buffer, then remove it from active list. |
| 3594 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 3595 | 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] | 3596 | tracksToRemove->add(track); |
| 3597 | // indicate to client process that the track was disabled because of underrun; |
| 3598 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3599 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3600 | // If one track is not ready, mark the mixer also not ready if: |
| 3601 | // - the mixer was ready during previous round OR |
| 3602 | // - no other track is ready |
| 3603 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 3604 | mixerStatus != MIXER_TRACKS_READY) { |
| 3605 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3606 | } |
| 3607 | } |
| 3608 | mAudioMixer->disable(name); |
| 3609 | } |
| 3610 | |
| 3611 | } // local variable scope to avoid goto warning |
| 3612 | track_is_ready: ; |
| 3613 | |
| 3614 | } |
| 3615 | |
| 3616 | // Push the new FastMixer state if necessary |
| 3617 | bool pauseAudioWatchdog = false; |
| 3618 | if (didModify) { |
| 3619 | state->mFastTracksGen++; |
| 3620 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3621 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3622 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3623 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3624 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3625 | state->mColdGen++; |
| 3626 | mFastMixerFutex = 0; |
| 3627 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3628 | mNormalSink = mOutputSink; |
| 3629 | } |
| 3630 | // If we go into cold idle, need to wait for acknowledgement |
| 3631 | // so that fast mixer stops doing I/O. |
| 3632 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3633 | pauseAudioWatchdog = true; |
| 3634 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3635 | } |
| 3636 | if (sq != NULL) { |
| 3637 | sq->end(didModify); |
| 3638 | sq->push(block); |
| 3639 | } |
| 3640 | #ifdef AUDIO_WATCHDOG |
| 3641 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 3642 | mAudioWatchdog->pause(); |
| 3643 | } |
| 3644 | #endif |
| 3645 | |
| 3646 | // Now perform the deferred reset on fast tracks that have stopped |
| 3647 | while (resetMask != 0) { |
| 3648 | size_t i = __builtin_ctz(resetMask); |
| 3649 | ALOG_ASSERT(i < count); |
| 3650 | resetMask &= ~(1 << i); |
| 3651 | sp<Track> t = mActiveTracks[i].promote(); |
| 3652 | if (t == 0) { |
| 3653 | continue; |
| 3654 | } |
| 3655 | Track* track = t.get(); |
| 3656 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3657 | track->reset(); |
| 3658 | } |
| 3659 | |
| 3660 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3661 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3662 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3663 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 3664 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 3665 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3666 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3667 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3668 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3669 | if (mMixerBufferValid) { |
| 3670 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3671 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 3672 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 3673 | // after mixing. |
| 3674 | // |
| 3675 | // To enforce this guarantee: |
| 3676 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3677 | // (mixedTracks == 0 && fastTracks > 0)) |
| 3678 | // must imply MIXER_TRACKS_READY. |
| 3679 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 3680 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3681 | // TODO - either mEffectBuffer or mSinkBuffer needs to be cleared. |
| 3682 | if (mEffectBufferValid) { |
| 3683 | memset(mEffectBuffer, 0, mEffectBufferSize); |
| 3684 | } |
| 3685 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 3686 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | // if any fast tracks, then status is ready |
| 3690 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 3691 | if (fastTracks > 0) { |
| 3692 | mixerStatus = MIXER_TRACKS_READY; |
| 3693 | } |
| 3694 | return mixerStatus; |
| 3695 | } |
| 3696 | |
| 3697 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3698 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
| 3699 | audio_format_t format, int sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3700 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3701 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3705 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3706 | { |
| 3707 | ALOGV("remove track (%d) and delete from mixer", name); |
| 3708 | mAudioMixer->deleteTrackName(name); |
| 3709 | } |
| 3710 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3711 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 3712 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 3713 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3714 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3715 | bool reconfig = false; |
| 3716 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3717 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3718 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3719 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3720 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3721 | if (mFastMixer != 0) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3722 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3723 | FastMixerState *state = sq->begin(); |
| 3724 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3725 | previousCommand = state->mCommand; |
| 3726 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3727 | sq->end(); |
| 3728 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3729 | } else { |
| 3730 | sq->end(false /*didModify*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3731 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3732 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3733 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3734 | AudioParameter param = AudioParameter(keyValuePair); |
| 3735 | int value; |
| 3736 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3737 | reconfig = true; |
| 3738 | } |
| 3739 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3740 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3741 | status = BAD_VALUE; |
| 3742 | } else { |
| 3743 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3744 | reconfig = true; |
| 3745 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3746 | } |
| 3747 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3748 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3749 | status = BAD_VALUE; |
| 3750 | } else { |
| 3751 | // no need to save value, since it's constant |
| 3752 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3753 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3754 | } |
| 3755 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 3756 | // do not accept frame count changes if tracks are open as the track buffer |
| 3757 | // size depends on frame count and correct behavior would not be guaranteed |
| 3758 | // if frame count is changed after track creation |
| 3759 | if (!mTracks.isEmpty()) { |
| 3760 | status = INVALID_OPERATION; |
| 3761 | } else { |
| 3762 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3763 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3764 | } |
| 3765 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3766 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3767 | // when changing the audio output device, call addBatteryData to notify |
| 3768 | // the change |
| 3769 | if (mOutDevice != value) { |
| 3770 | uint32_t params = 0; |
| 3771 | // check whether speaker is on |
| 3772 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3773 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3774 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3775 | |
| 3776 | audio_devices_t deviceWithoutSpeaker |
| 3777 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3778 | // check if any other device (except speaker) is on |
| 3779 | if (value & deviceWithoutSpeaker ) { |
| 3780 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3781 | } |
| 3782 | |
| 3783 | if (params != 0) { |
| 3784 | addBatteryData(params); |
| 3785 | } |
| 3786 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3787 | #endif |
| 3788 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3789 | // forward device change to effects that have requested to be |
| 3790 | // aware of attached audio device. |
| 3791 | if (value != AUDIO_DEVICE_NONE) { |
| 3792 | mOutDevice = value; |
| 3793 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3794 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3795 | } |
| 3796 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3797 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3798 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3799 | if (status == NO_ERROR) { |
| 3800 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3801 | keyValuePair.string()); |
| 3802 | if (!mStandby && status == INVALID_OPERATION) { |
| 3803 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 3804 | mStandby = true; |
| 3805 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3806 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3807 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3808 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3809 | if (status == NO_ERROR && reconfig) { |
| 3810 | readOutputParameters_l(); |
| 3811 | delete mAudioMixer; |
| 3812 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3813 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3814 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 3815 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3816 | if (name < 0) { |
| 3817 | break; |
| 3818 | } |
| 3819 | mTracks[i]->mName = name; |
| 3820 | } |
| 3821 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 3822 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | if (!(previousCommand & FastMixerState::IDLE)) { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3826 | ALOG_ASSERT(mFastMixer != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3827 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3828 | FastMixerState *state = sq->begin(); |
| 3829 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3830 | state->mCommand = previousCommand; |
| 3831 | sq->end(); |
| 3832 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3833 | } |
| 3834 | |
| 3835 | return reconfig; |
| 3836 | } |
| 3837 | |
| 3838 | |
| 3839 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 3840 | { |
| 3841 | const size_t SIZE = 256; |
| 3842 | char buffer[SIZE]; |
| 3843 | String8 result; |
| 3844 | |
| 3845 | PlaybackThread::dumpInternals(fd, args); |
| 3846 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 3847 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3848 | |
| 3849 | // 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] | 3850 | const FastMixerDumpState copy(mFastMixerDumpState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3851 | copy.dump(fd); |
| 3852 | |
| 3853 | #ifdef STATE_QUEUE_DUMP |
| 3854 | // Similar for state queue |
| 3855 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 3856 | observerCopy.dump(fd); |
| 3857 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 3858 | mutatorCopy.dump(fd); |
| 3859 | #endif |
| 3860 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3861 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3862 | // Write the tee output to a .wav file |
| 3863 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3864 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3865 | |
| 3866 | #ifdef AUDIO_WATCHDOG |
| 3867 | if (mAudioWatchdog != 0) { |
| 3868 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 3869 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 3870 | wdCopy.dump(fd); |
| 3871 | } |
| 3872 | #endif |
| 3873 | } |
| 3874 | |
| 3875 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 3876 | { |
| 3877 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 3878 | } |
| 3879 | |
| 3880 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 3881 | { |
| 3882 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 3883 | } |
| 3884 | |
| 3885 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 3886 | { |
| 3887 | PlaybackThread::cacheParameters_l(); |
| 3888 | |
| 3889 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 3890 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 3891 | // increase threshold again due to low power audio mode. The way this warning |
| 3892 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 3893 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 3894 | } |
| 3895 | |
| 3896 | // ---------------------------------------------------------------------------- |
| 3897 | |
| 3898 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3899 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) |
| 3900 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
| 3901 | // mLeftVolFloat, mRightVolFloat |
| 3902 | { |
| 3903 | } |
| 3904 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3905 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 3906 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
| 3907 | ThreadBase::type_t type) |
| 3908 | : PlaybackThread(audioFlinger, output, id, device, type) |
| 3909 | // mLeftVolFloat, mRightVolFloat |
| 3910 | { |
| 3911 | } |
| 3912 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3913 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 3914 | { |
| 3915 | } |
| 3916 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3917 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 3918 | { |
| 3919 | audio_track_cblk_t* cblk = track->cblk(); |
| 3920 | float left, right; |
| 3921 | |
| 3922 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 3923 | left = right = 0; |
| 3924 | } else { |
| 3925 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3926 | float v = mMasterVolume * typeVolume; |
| 3927 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3928 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 3929 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3930 | if (left > GAIN_FLOAT_UNITY) { |
| 3931 | left = GAIN_FLOAT_UNITY; |
| 3932 | } |
| 3933 | left *= v; |
| 3934 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 3935 | if (right > GAIN_FLOAT_UNITY) { |
| 3936 | right = GAIN_FLOAT_UNITY; |
| 3937 | } |
| 3938 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | if (lastTrack) { |
| 3942 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 3943 | mLeftVolFloat = left; |
| 3944 | mRightVolFloat = right; |
| 3945 | |
| 3946 | // Convert volumes from float to 8.24 |
| 3947 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 3948 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 3949 | |
| 3950 | // Delegate volume control to effect in track effect chain if needed |
| 3951 | // only one effect chain can be present on DirectOutputThread, so if |
| 3952 | // there is one, the track is connected to it |
| 3953 | if (!mEffectChains.isEmpty()) { |
| 3954 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 3955 | left = (float)vl / (1 << 24); |
| 3956 | right = (float)vr / (1 << 24); |
| 3957 | } |
| 3958 | if (mOutput->stream->set_volume) { |
| 3959 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 3960 | } |
| 3961 | } |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3966 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 3967 | Vector< sp<Track> > *tracksToRemove |
| 3968 | ) |
| 3969 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3970 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3971 | mixer_state mixerStatus = MIXER_IDLE; |
| 3972 | |
| 3973 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3974 | for (size_t i = 0; i < count; i++) { |
| 3975 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3976 | // The track died recently |
| 3977 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 3978 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | Track* const track = t.get(); |
| 3982 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 3983 | // Only consider last track started for volume and mixer state control. |
| 3984 | // In theory an older track could underrun and restart after the new one starts |
| 3985 | // but as we only care about the transition phase between two tracks on a |
| 3986 | // direct output, it is not a problem to ignore the underrun case. |
| 3987 | sp<Track> l = mLatestActiveTrack.promote(); |
| 3988 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3989 | |
| 3990 | // The first time a track is added we wait |
| 3991 | // for all its buffers to be filled before processing it |
| 3992 | uint32_t minFrames; |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3993 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3994 | minFrames = mNormalFrameCount; |
| 3995 | } else { |
| 3996 | minFrames = 1; |
| 3997 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3998 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 3999 | ALOGI("prepareTracks_l minFrames %d state %d frames ready %d, ", |
| 4000 | minFrames, track->mState, track->framesReady()); |
| 4001 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4002 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4003 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4004 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4005 | |
| 4006 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4007 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4008 | // make sure processVolume_l() will apply new volume even if 0 |
| 4009 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4010 | if (track->mState == TrackBase::RESUMING) { |
| 4011 | track->mState = TrackBase::ACTIVE; |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4016 | processVolume_l(track, last); |
| 4017 | if (last) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4018 | // reset retry count |
| 4019 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4020 | mActiveTrack = t; |
| 4021 | mixerStatus = MIXER_TRACKS_READY; |
| 4022 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4023 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4024 | // clear effect chain input buffer if the last active track started underruns |
| 4025 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4026 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4027 | mEffectChains[0]->clearInputBuffer(); |
| 4028 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4029 | if (track->isStopping_1()) { |
| 4030 | track->mState = TrackBase::STOPPING_2; |
| 4031 | } |
| 4032 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4033 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4034 | // We have consumed all the buffers of this track. |
| 4035 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4036 | size_t audioHALFrames; |
| 4037 | if (audio_is_linear_pcm(mFormat)) { |
| 4038 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4039 | } else { |
| 4040 | audioHALFrames = 0; |
| 4041 | } |
| 4042 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4043 | size_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4044 | if (mStandby || !last || |
| 4045 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4046 | if (track->isStopping_2()) { |
| 4047 | track->mState = TrackBase::STOPPED; |
| 4048 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4049 | if (track->isStopped()) { |
| 4050 | track->reset(); |
| 4051 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4052 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4053 | } |
| 4054 | } else { |
| 4055 | // No buffers for this track. Give it a few chances to |
| 4056 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4057 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4058 | if (--(track->mRetryCount) <= 0) { |
| 4059 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4060 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4061 | // indicate to client process that the track was disabled because of underrun; |
| 4062 | // it will then automatically call start() when data is available |
| 4063 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4064 | } else if (last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4065 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | } |
| 4070 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4071 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4072 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4073 | |
| 4074 | return mixerStatus; |
| 4075 | } |
| 4076 | |
| 4077 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4078 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4079 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4080 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4081 | // output audio to hardware |
| 4082 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4083 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4084 | buffer.frameCount = frameCount; |
| 4085 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | fa319e6 | 2013-07-29 17:17:38 -0700 | [diff] [blame] | 4086 | if (buffer.raw == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4087 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4088 | break; |
| 4089 | } |
| 4090 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4091 | frameCount -= buffer.frameCount; |
| 4092 | curBuf += buffer.frameCount * mFrameSize; |
| 4093 | mActiveTrack->releaseBuffer(&buffer); |
| 4094 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4095 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4096 | sleepTime = 0; |
| 4097 | standbyTime = systemTime() + standbyDelay; |
| 4098 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4102 | { |
| 4103 | if (sleepTime == 0) { |
| 4104 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4105 | sleepTime = activeSleepTime; |
| 4106 | } else { |
| 4107 | sleepTime = idleSleepTime; |
| 4108 | } |
| 4109 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4110 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4111 | sleepTime = 0; |
| 4112 | } |
| 4113 | } |
| 4114 | |
| 4115 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4116 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4117 | audio_format_t format __unused, int sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4118 | { |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
| 4122 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4123 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4124 | { |
| 4125 | } |
| 4126 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4127 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4128 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4129 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4130 | { |
| 4131 | bool reconfig = false; |
| 4132 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4133 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4134 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4135 | AudioParameter param = AudioParameter(keyValuePair); |
| 4136 | int value; |
| 4137 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4138 | // forward device change to effects that have requested to be |
| 4139 | // aware of attached audio device. |
| 4140 | if (value != AUDIO_DEVICE_NONE) { |
| 4141 | mOutDevice = value; |
| 4142 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4143 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4144 | } |
| 4145 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4146 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4147 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4148 | // do not accept frame count changes if tracks are open as the track buffer |
| 4149 | // size depends on frame count and correct behavior would not be garantied |
| 4150 | // if frame count is changed after track creation |
| 4151 | if (!mTracks.isEmpty()) { |
| 4152 | status = INVALID_OPERATION; |
| 4153 | } else { |
| 4154 | reconfig = true; |
| 4155 | } |
| 4156 | } |
| 4157 | if (status == NO_ERROR) { |
| 4158 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4159 | keyValuePair.string()); |
| 4160 | if (!mStandby && status == INVALID_OPERATION) { |
| 4161 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 4162 | mStandby = true; |
| 4163 | mBytesWritten = 0; |
| 4164 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4165 | keyValuePair.string()); |
| 4166 | } |
| 4167 | if (status == NO_ERROR && reconfig) { |
| 4168 | readOutputParameters_l(); |
| 4169 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 4170 | } |
| 4171 | } |
| 4172 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4173 | return reconfig; |
| 4174 | } |
| 4175 | |
| 4176 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 4177 | { |
| 4178 | uint32_t time; |
| 4179 | if (audio_is_linear_pcm(mFormat)) { |
| 4180 | time = PlaybackThread::activeSleepTimeUs(); |
| 4181 | } else { |
| 4182 | time = 10000; |
| 4183 | } |
| 4184 | return time; |
| 4185 | } |
| 4186 | |
| 4187 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 4188 | { |
| 4189 | uint32_t time; |
| 4190 | if (audio_is_linear_pcm(mFormat)) { |
| 4191 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4192 | } else { |
| 4193 | time = 10000; |
| 4194 | } |
| 4195 | return time; |
| 4196 | } |
| 4197 | |
| 4198 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 4199 | { |
| 4200 | uint32_t time; |
| 4201 | if (audio_is_linear_pcm(mFormat)) { |
| 4202 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 4203 | } else { |
| 4204 | time = 10000; |
| 4205 | } |
| 4206 | return time; |
| 4207 | } |
| 4208 | |
| 4209 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 4210 | { |
| 4211 | PlaybackThread::cacheParameters_l(); |
| 4212 | |
| 4213 | // use shorter standby delay as on normal output to release |
| 4214 | // hardware resources as soon as possible |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4215 | if (audio_is_linear_pcm(mFormat)) { |
| 4216 | standbyDelay = microseconds(activeSleepTime*2); |
| 4217 | } else { |
| 4218 | standbyDelay = kOffloadStandbyDelayNs; |
| 4219 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | // ---------------------------------------------------------------------------- |
| 4223 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4224 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4225 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4226 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4227 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4228 | mWriteAckSequence(0), |
| 4229 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4230 | { |
| 4231 | } |
| 4232 | |
| 4233 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 4234 | { |
| 4235 | } |
| 4236 | |
| 4237 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 4238 | { |
| 4239 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4240 | } |
| 4241 | |
| 4242 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 4243 | { |
| 4244 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4245 | uint32_t writeAckSequence; |
| 4246 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4247 | |
| 4248 | { |
| 4249 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4250 | while (!((mWriteAckSequence & 1) || |
| 4251 | (mDrainSequence & 1) || |
| 4252 | exitPending())) { |
| 4253 | mWaitWorkCV.wait(mLock); |
| 4254 | } |
| 4255 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4256 | if (exitPending()) { |
| 4257 | break; |
| 4258 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4259 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 4260 | mWriteAckSequence, mDrainSequence); |
| 4261 | writeAckSequence = mWriteAckSequence; |
| 4262 | mWriteAckSequence &= ~1; |
| 4263 | drainSequence = mDrainSequence; |
| 4264 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4265 | } |
| 4266 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4267 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 4268 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4269 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4270 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4271 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4272 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4273 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4274 | } |
| 4275 | } |
| 4276 | } |
| 4277 | } |
| 4278 | return false; |
| 4279 | } |
| 4280 | |
| 4281 | void AudioFlinger::AsyncCallbackThread::exit() |
| 4282 | { |
| 4283 | ALOGV("AsyncCallbackThread::exit"); |
| 4284 | Mutex::Autolock _l(mLock); |
| 4285 | requestExit(); |
| 4286 | mWaitWorkCV.broadcast(); |
| 4287 | } |
| 4288 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4289 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4290 | { |
| 4291 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4292 | // bit 0 is cleared |
| 4293 | mWriteAckSequence = sequence << 1; |
| 4294 | } |
| 4295 | |
| 4296 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 4297 | { |
| 4298 | Mutex::Autolock _l(mLock); |
| 4299 | // ignore unexpected callbacks |
| 4300 | if (mWriteAckSequence & 2) { |
| 4301 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4302 | mWaitWorkCV.signal(); |
| 4303 | } |
| 4304 | } |
| 4305 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4306 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4307 | { |
| 4308 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4309 | // bit 0 is cleared |
| 4310 | mDrainSequence = sequence << 1; |
| 4311 | } |
| 4312 | |
| 4313 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 4314 | { |
| 4315 | Mutex::Autolock _l(mLock); |
| 4316 | // ignore unexpected callbacks |
| 4317 | if (mDrainSequence & 2) { |
| 4318 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4319 | mWaitWorkCV.signal(); |
| 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | |
| 4324 | // ---------------------------------------------------------------------------- |
| 4325 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
| 4326 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
| 4327 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), |
| 4328 | mHwPaused(false), |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 4329 | mFlushPending(false), |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4330 | mPausedBytesRemaining(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4331 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4332 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 4333 | mStandby = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4334 | } |
| 4335 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4336 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 4337 | { |
| 4338 | if (mFlushPending || mHwPaused) { |
| 4339 | // If a flush is pending or track was paused, just discard buffered data |
| 4340 | flushHw_l(); |
| 4341 | } else { |
| 4342 | mMixerStatus = MIXER_DRAIN_ALL; |
| 4343 | threadLoop_drain(); |
| 4344 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 4345 | if (mUseAsyncWrite) { |
| 4346 | ALOG_ASSERT(mCallbackThread != 0); |
| 4347 | mCallbackThread->exit(); |
| 4348 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4349 | PlaybackThread::threadLoop_exit(); |
| 4350 | } |
| 4351 | |
| 4352 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 4353 | Vector< sp<Track> > *tracksToRemove |
| 4354 | ) |
| 4355 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4356 | size_t count = mActiveTracks.size(); |
| 4357 | |
| 4358 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4359 | bool doHwPause = false; |
| 4360 | bool doHwResume = false; |
| 4361 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4362 | ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); |
| 4363 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4364 | // find out which tracks need to be processed |
| 4365 | for (size_t i = 0; i < count; i++) { |
| 4366 | sp<Track> t = mActiveTracks[i].promote(); |
| 4367 | // The track died recently |
| 4368 | if (t == 0) { |
| 4369 | continue; |
| 4370 | } |
| 4371 | Track* const track = t.get(); |
| 4372 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4373 | // Only consider last track started for volume and mixer state control. |
| 4374 | // In theory an older track could underrun and restart after the new one starts |
| 4375 | // but as we only care about the transition phase between two tracks on a |
| 4376 | // direct output, it is not a problem to ignore the underrun case. |
| 4377 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4378 | bool last = l.get() == track; |
| 4379 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4380 | if (track->isInvalid()) { |
| 4381 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4382 | tracksToRemove->add(track); |
| 4383 | continue; |
| 4384 | } |
| 4385 | |
| 4386 | if (track->mState == TrackBase::IDLE) { |
| 4387 | ALOGW("An idle track shouldn't be in active list"); |
| 4388 | continue; |
| 4389 | } |
| 4390 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4391 | if (track->isPausing()) { |
| 4392 | track->setPaused(); |
| 4393 | if (last) { |
| 4394 | if (!mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4395 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4396 | mHwPaused = true; |
| 4397 | } |
| 4398 | // If we were part way through writing the mixbuffer to |
| 4399 | // the HAL we must save this until we resume |
| 4400 | // BUG - this will be wrong if a different track is made active, |
| 4401 | // in that case we want to discard the pending data in the |
| 4402 | // mixbuffer and tell the client to present it again when the |
| 4403 | // track is resumed |
| 4404 | mPausedWriteLength = mCurrentWriteLength; |
| 4405 | mPausedBytesRemaining = mBytesRemaining; |
| 4406 | mBytesRemaining = 0; // stop writing |
| 4407 | } |
| 4408 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4409 | } else if (track->isFlushPending()) { |
| 4410 | track->flushAck(); |
| 4411 | if (last) { |
| 4412 | mFlushPending = true; |
| 4413 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 4414 | } else if (track->isResumePending()){ |
| 4415 | track->resumeAck(); |
| 4416 | if (last) { |
| 4417 | if (mPausedBytesRemaining) { |
| 4418 | // Need to continue write that was interrupted |
| 4419 | mCurrentWriteLength = mPausedWriteLength; |
| 4420 | mBytesRemaining = mPausedBytesRemaining; |
| 4421 | mPausedBytesRemaining = 0; |
| 4422 | } |
| 4423 | if (mHwPaused) { |
| 4424 | doHwResume = true; |
| 4425 | mHwPaused = false; |
| 4426 | // threadLoop_mix() will handle the case that we need to |
| 4427 | // resume an interrupted write |
| 4428 | } |
| 4429 | // enable write to audio HAL |
| 4430 | sleepTime = 0; |
| 4431 | |
| 4432 | // Do not handle new data in this iteration even if track->framesReady() |
| 4433 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4434 | } |
| 4435 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4436 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4437 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4438 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4439 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4440 | // make sure processVolume_l() will apply new volume even if 0 |
| 4441 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4445 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4446 | if (previousTrack != 0) { |
| 4447 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4448 | // Flush any data still being written from last track |
| 4449 | mBytesRemaining = 0; |
| 4450 | if (mPausedBytesRemaining) { |
| 4451 | // Last track was paused so we also need to flush saved |
| 4452 | // mixbuffer state and invalidate track so that it will |
| 4453 | // re-submit that unwritten data when it is next resumed |
| 4454 | mPausedBytesRemaining = 0; |
| 4455 | // Invalidate is a bit drastic - would be more efficient |
| 4456 | // to have a flag to tell client that some of the |
| 4457 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4458 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4459 | } |
| 4460 | // flush data already sent to the DSP if changing audio session as audio |
| 4461 | // comes from a different source. Also invalidate previous track to force a |
| 4462 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4463 | if (previousTrack->sessionId() != track->sessionId()) { |
| 4464 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4465 | } |
| 4466 | } |
| 4467 | } |
| 4468 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4469 | // reset retry count |
| 4470 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 4471 | mActiveTrack = t; |
| 4472 | mixerStatus = MIXER_TRACKS_READY; |
| 4473 | } |
| 4474 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4475 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4476 | if (track->isStopping_1()) { |
| 4477 | // Hardware buffer can hold a large amount of audio so we must |
| 4478 | // wait for all current track's data to drain before we say |
| 4479 | // that the track is stopped. |
| 4480 | if (mBytesRemaining == 0) { |
| 4481 | // Only start draining when all data in mixbuffer |
| 4482 | // has been written |
| 4483 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 4484 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4485 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 4486 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4487 | // do not modify drain sequence if we are already draining. This happens |
| 4488 | // when resuming from pause after drain. |
| 4489 | if ((mDrainSequence & 1) == 0) { |
| 4490 | sleepTime = 0; |
| 4491 | standbyTime = systemTime() + standbyDelay; |
| 4492 | mixerStatus = MIXER_DRAIN_TRACK; |
| 4493 | mDrainSequence += 2; |
| 4494 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4495 | if (mHwPaused) { |
| 4496 | // It is possible to move from PAUSED to STOPPING_1 without |
| 4497 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4498 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4499 | mHwPaused = false; |
| 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4504 | // Drain has completed or we are in standby, signal presentation complete |
| 4505 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4506 | track->mState = TrackBase::STOPPED; |
| 4507 | size_t audioHALFrames = |
| 4508 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 4509 | size_t framesWritten = |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 4510 | mBytesWritten / audio_stream_out_frame_size(mOutput->stream); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4511 | track->presentationComplete(framesWritten, audioHALFrames); |
| 4512 | track->reset(); |
| 4513 | tracksToRemove->add(track); |
| 4514 | } |
| 4515 | } else { |
| 4516 | // No buffers for this track. Give it a few chances to |
| 4517 | // fill a buffer, then remove it from active list. |
| 4518 | if (--(track->mRetryCount) <= 0) { |
| 4519 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 4520 | track->name()); |
| 4521 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4522 | // indicate to client process that the track was disabled because of underrun; |
| 4523 | // it will then automatically call start() when data is available |
| 4524 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4525 | } else if (last){ |
| 4526 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4527 | } |
| 4528 | } |
| 4529 | } |
| 4530 | // compute volume for this track |
| 4531 | processVolume_l(track, last); |
| 4532 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4533 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 4534 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4535 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4536 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4537 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4538 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4539 | mOutput->stream->pause(mOutput->stream); |
| 4540 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4541 | if (mFlushPending) { |
| 4542 | flushHw_l(); |
| 4543 | mFlushPending = false; |
| 4544 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4545 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4546 | mOutput->stream->resume(mOutput->stream); |
| 4547 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4548 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4549 | // remove all the tracks that need to be... |
| 4550 | removeTracks_l(*tracksToRemove); |
| 4551 | |
| 4552 | return mixerStatus; |
| 4553 | } |
| 4554 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4555 | // must be called with thread mutex locked |
| 4556 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 4557 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4558 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 4559 | mWriteAckSequence, mDrainSequence); |
| 4560 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4561 | return true; |
| 4562 | } |
| 4563 | return false; |
| 4564 | } |
| 4565 | |
| 4566 | // must be called with thread mutex locked |
| 4567 | bool AudioFlinger::OffloadThread::shouldStandby_l() |
| 4568 | { |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4569 | bool trackPaused = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4570 | |
| 4571 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4572 | // after a timeout and we will enter standby then. |
| 4573 | if (mTracks.size() > 0) { |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4574 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
Glenn Kasten | e6f35b1 | 2013-08-19 09:58:50 -0700 | [diff] [blame] | 4577 | return !mStandby && !trackPaused; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | |
| 4581 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 4582 | { |
| 4583 | Mutex::Autolock _l(mLock); |
| 4584 | return waitingAsyncCallback_l(); |
| 4585 | } |
| 4586 | |
| 4587 | void AudioFlinger::OffloadThread::flushHw_l() |
| 4588 | { |
| 4589 | mOutput->stream->flush(mOutput->stream); |
| 4590 | // Flush anything still waiting in the mixbuffer |
| 4591 | mCurrentWriteLength = 0; |
| 4592 | mBytesRemaining = 0; |
| 4593 | mPausedWriteLength = 0; |
| 4594 | mPausedBytesRemaining = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 4595 | mHwPaused = false; |
| 4596 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4597 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4598 | // discard any pending drain or write ack by incrementing sequence |
| 4599 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4600 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4601 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4602 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4603 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4607 | void AudioFlinger::OffloadThread::onAddNewTrack_l() |
| 4608 | { |
| 4609 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4610 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4611 | |
| 4612 | if (previousTrack != 0 && latestTrack != 0 && |
| 4613 | (previousTrack->sessionId() != latestTrack->sessionId())) { |
| 4614 | mFlushPending = true; |
| 4615 | } |
| 4616 | PlaybackThread::onAddNewTrack_l(); |
| 4617 | } |
| 4618 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4619 | // ---------------------------------------------------------------------------- |
| 4620 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4621 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
| 4622 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
| 4623 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
| 4624 | DUPLICATING), |
| 4625 | mWaitTimeMs(UINT_MAX) |
| 4626 | { |
| 4627 | addOutputTrack(mainThread); |
| 4628 | } |
| 4629 | |
| 4630 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 4631 | { |
| 4632 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4633 | mOutputTracks[i]->destroy(); |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 4638 | { |
| 4639 | // mix buffers... |
| 4640 | if (outputsReady(outputTracks)) { |
| 4641 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 4642 | } else { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4643 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4644 | } |
| 4645 | sleepTime = 0; |
| 4646 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4647 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4648 | standbyTime = systemTime() + standbyDelay; |
| 4649 | } |
| 4650 | |
| 4651 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 4652 | { |
| 4653 | if (sleepTime == 0) { |
| 4654 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4655 | sleepTime = activeSleepTime; |
| 4656 | } else { |
| 4657 | sleepTime = idleSleepTime; |
| 4658 | } |
| 4659 | } else if (mBytesWritten != 0) { |
| 4660 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4661 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4662 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4663 | } else { |
| 4664 | // flush remaining overflow buffers in output tracks |
| 4665 | writeFrames = 0; |
| 4666 | } |
| 4667 | sleepTime = 0; |
| 4668 | } |
| 4669 | } |
| 4670 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4671 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4672 | { |
| 4673 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4674 | // We convert the duplicating thread format to AUDIO_FORMAT_PCM_16_BIT |
| 4675 | // for delivery downstream as needed. This in-place conversion is safe as |
| 4676 | // AUDIO_FORMAT_PCM_16_BIT is smaller than any other supported format |
| 4677 | // (AUDIO_FORMAT_PCM_8_BIT is not allowed here). |
| 4678 | if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
| 4679 | memcpy_by_audio_format(mSinkBuffer, AUDIO_FORMAT_PCM_16_BIT, |
| 4680 | mSinkBuffer, mFormat, writeFrames * mChannelCount); |
| 4681 | } |
| 4682 | outputTracks[i]->write(reinterpret_cast<int16_t*>(mSinkBuffer), writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4683 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 4684 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4685 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 4689 | { |
| 4690 | // DuplicatingThread implements standby by stopping all tracks |
| 4691 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 4692 | outputTracks[i]->stop(); |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 4697 | { |
| 4698 | outputTracks = mOutputTracks; |
| 4699 | } |
| 4700 | |
| 4701 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 4702 | { |
| 4703 | outputTracks.clear(); |
| 4704 | } |
| 4705 | |
| 4706 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 4707 | { |
| 4708 | Mutex::Autolock _l(mLock); |
| 4709 | // FIXME explain this formula |
| 4710 | size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4711 | // OutputTrack is forced to AUDIO_FORMAT_PCM_16_BIT regardless of mFormat |
| 4712 | // due to current usage case and restrictions on the AudioBufferProvider. |
| 4713 | // Actual buffer conversion is done in threadLoop_write(). |
| 4714 | // |
| 4715 | // TODO: This may change in the future, depending on multichannel |
| 4716 | // (and non int16_t*) support on AF::PlaybackThread::OutputTrack |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4717 | OutputTrack *outputTrack = new OutputTrack(thread, |
| 4718 | this, |
| 4719 | mSampleRate, |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 4720 | AUDIO_FORMAT_PCM_16_BIT, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4721 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 4722 | frameCount, |
| 4723 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4724 | if (outputTrack->cblk() != NULL) { |
| 4725 | thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f); |
| 4726 | mOutputTracks.add(outputTrack); |
| 4727 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread); |
| 4728 | updateWaitTime_l(); |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 4733 | { |
| 4734 | Mutex::Autolock _l(mLock); |
| 4735 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4736 | if (mOutputTracks[i]->thread() == thread) { |
| 4737 | mOutputTracks[i]->destroy(); |
| 4738 | mOutputTracks.removeAt(i); |
| 4739 | updateWaitTime_l(); |
| 4740 | return; |
| 4741 | } |
| 4742 | } |
| 4743 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
| 4744 | } |
| 4745 | |
| 4746 | // caller must hold mLock |
| 4747 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 4748 | { |
| 4749 | mWaitTimeMs = UINT_MAX; |
| 4750 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4751 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 4752 | if (strong != 0) { |
| 4753 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 4754 | if (waitTimeMs < mWaitTimeMs) { |
| 4755 | mWaitTimeMs = waitTimeMs; |
| 4756 | } |
| 4757 | } |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | |
| 4762 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 4763 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 4764 | { |
| 4765 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 4766 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 4767 | if (thread == 0) { |
| 4768 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 4769 | outputTracks[i].get()); |
| 4770 | return false; |
| 4771 | } |
| 4772 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 4773 | // see note at standby() declaration |
| 4774 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 4775 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 4776 | thread.get()); |
| 4777 | return false; |
| 4778 | } |
| 4779 | } |
| 4780 | return true; |
| 4781 | } |
| 4782 | |
| 4783 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 4784 | { |
| 4785 | return (mWaitTimeMs * 1000) / 2; |
| 4786 | } |
| 4787 | |
| 4788 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 4789 | { |
| 4790 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 4791 | updateWaitTime_l(); |
| 4792 | |
| 4793 | MixerThread::cacheParameters_l(); |
| 4794 | } |
| 4795 | |
| 4796 | // ---------------------------------------------------------------------------- |
| 4797 | // Record |
| 4798 | // ---------------------------------------------------------------------------- |
| 4799 | |
| 4800 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 4801 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4802 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 4803 | audio_devices_t outDevice, |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4804 | audio_devices_t inDevice |
| 4805 | #ifdef TEE_SINK |
| 4806 | , const sp<NBAIO_Sink>& teeSink |
| 4807 | #endif |
| 4808 | ) : |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 4809 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 4810 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 4811 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 4812 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4813 | #ifdef TEE_SINK |
| 4814 | , mTeeSink(teeSink) |
| 4815 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 4816 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 4817 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4818 | // mFastCapture below |
| 4819 | , mFastCaptureFutex(0) |
| 4820 | // mInputSource |
| 4821 | // mPipeSink |
| 4822 | // mPipeSource |
| 4823 | , mPipeFramesP2(0) |
| 4824 | // mPipeMemory |
| 4825 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 4826 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4827 | { |
| 4828 | snprintf(mName, kNameLength, "AudioIn_%X", id); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 4829 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4830 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 4831 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4832 | |
| 4833 | // create an NBAIO source for the HAL input stream, and negotiate |
| 4834 | mInputSource = new AudioStreamInSource(input->stream); |
| 4835 | size_t numCounterOffers = 0; |
| 4836 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
| 4837 | ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 4838 | ALOG_ASSERT(index == 0); |
| 4839 | |
| 4840 | // initialize fast capture depending on configuration |
| 4841 | bool initFastCapture; |
| 4842 | switch (kUseFastCapture) { |
| 4843 | case FastCapture_Never: |
| 4844 | initFastCapture = false; |
| 4845 | break; |
| 4846 | case FastCapture_Always: |
| 4847 | initFastCapture = true; |
| 4848 | break; |
| 4849 | case FastCapture_Static: |
| 4850 | uint32_t primaryOutputSampleRate; |
| 4851 | { |
| 4852 | AutoMutex _l(audioFlinger->mHardwareLock); |
| 4853 | primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; |
| 4854 | } |
| 4855 | initFastCapture = |
| 4856 | // either capture sample rate is same as (a reasonable) primary output sample rate |
| 4857 | (((primaryOutputSampleRate == 44100 || primaryOutputSampleRate == 48000) && |
| 4858 | (mSampleRate == primaryOutputSampleRate)) || |
| 4859 | // or primary output sample rate is unknown, and capture sample rate is reasonable |
| 4860 | ((primaryOutputSampleRate == 0) && |
| 4861 | ((mSampleRate == 44100 || mSampleRate == 48000)))) && |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 4862 | // and the buffer size is < 12 ms |
| 4863 | (mFrameCount * 1000) / mSampleRate < 12; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4864 | break; |
| 4865 | // case FastCapture_Dynamic: |
| 4866 | } |
| 4867 | |
| 4868 | if (initFastCapture) { |
| 4869 | // create a Pipe for FastMixer to write to, and for us and fast tracks to read from |
| 4870 | NBAIO_Format format = mInputSource->format(); |
| 4871 | size_t pipeFramesP2 = roundup(mFrameCount * 8); |
| 4872 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 4873 | void *pipeBuffer; |
| 4874 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 4875 | sp<IMemory> pipeMemory; |
| 4876 | if ((roHeap == 0) || |
| 4877 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 4878 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 4879 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 4880 | goto failed; |
| 4881 | } |
| 4882 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 4883 | memset(pipeBuffer, 0, pipeSize); |
| 4884 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 4885 | const NBAIO_Format offers[1] = {format}; |
| 4886 | size_t numCounterOffers = 0; |
| 4887 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 4888 | ALOG_ASSERT(index == 0); |
| 4889 | mPipeSink = pipe; |
| 4890 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 4891 | numCounterOffers = 0; |
| 4892 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 4893 | ALOG_ASSERT(index == 0); |
| 4894 | mPipeSource = pipeReader; |
| 4895 | mPipeFramesP2 = pipeFramesP2; |
| 4896 | mPipeMemory = pipeMemory; |
| 4897 | |
| 4898 | // create fast capture |
| 4899 | mFastCapture = new FastCapture(); |
| 4900 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 4901 | #ifdef STATE_QUEUE_DUMP |
| 4902 | // FIXME |
| 4903 | #endif |
| 4904 | FastCaptureState *state = sq->begin(); |
| 4905 | state->mCblk = NULL; |
| 4906 | state->mInputSource = mInputSource.get(); |
| 4907 | state->mInputSourceGen++; |
| 4908 | state->mPipeSink = pipe; |
| 4909 | state->mPipeSinkGen++; |
| 4910 | state->mFrameCount = mFrameCount; |
| 4911 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 4912 | // already done in constructor initialization list |
| 4913 | //mFastCaptureFutex = 0; |
| 4914 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 4915 | state->mColdGen++; |
| 4916 | state->mDumpState = &mFastCaptureDumpState; |
| 4917 | #ifdef TEE_SINK |
| 4918 | // FIXME |
| 4919 | #endif |
| 4920 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 4921 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 4922 | sq->end(); |
| 4923 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 4924 | |
| 4925 | // start the fast capture |
| 4926 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4927 | pid_t tid = mFastCapture->getTid(); |
| 4928 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 4929 | if (err != 0) { |
| 4930 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 4931 | kPriorityFastCapture, getpid_cached, tid, err); |
| 4932 | } |
| 4933 | |
| 4934 | #ifdef AUDIO_WATCHDOG |
| 4935 | // FIXME |
| 4936 | #endif |
| 4937 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 4938 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4939 | } |
| 4940 | failed: ; |
| 4941 | |
| 4942 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | |
| 4946 | AudioFlinger::RecordThread::~RecordThread() |
| 4947 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 4948 | if (mFastCapture != 0) { |
| 4949 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 4950 | FastCaptureState *state = sq->begin(); |
| 4951 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 4952 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 4953 | if (old == -1) { |
| 4954 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 4955 | } |
| 4956 | } |
| 4957 | state->mCommand = FastCaptureState::EXIT; |
| 4958 | sq->end(); |
| 4959 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 4960 | mFastCapture->join(); |
| 4961 | mFastCapture.clear(); |
| 4962 | } |
| 4963 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 4964 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4965 | delete[] mRsmpInBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | void AudioFlinger::RecordThread::onFirstRef() |
| 4969 | { |
| 4970 | run(mName, PRIORITY_URGENT_AUDIO); |
| 4971 | } |
| 4972 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4973 | bool AudioFlinger::RecordThread::threadLoop() |
| 4974 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4975 | nsecs_t lastWarning = 0; |
| 4976 | |
| 4977 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4978 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 4979 | reacquire_wakelock: |
| 4980 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 4981 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 4982 | { |
| 4983 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 4984 | size_t size = mActiveTracks.size(); |
| 4985 | activeTracksGen = mActiveTracksGen; |
| 4986 | if (size > 0) { |
| 4987 | // FIXME an arbitrary choice |
| 4988 | activeTrack = mActiveTracks[0]; |
| 4989 | acquireWakeLock_l(activeTrack->uid()); |
| 4990 | if (size > 1) { |
| 4991 | SortedVector<int> tmp; |
| 4992 | for (size_t i = 0; i < size; i++) { |
| 4993 | tmp.add(mActiveTracks[i]->uid()); |
| 4994 | } |
| 4995 | updateWakeLockUids_l(tmp); |
| 4996 | } |
| 4997 | } else { |
| 4998 | acquireWakeLock_l(-1); |
| 4999 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5000 | } |
| 5001 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5002 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 5003 | uint32_t sleepUs = 0; |
| 5004 | |
| 5005 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5006 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5007 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5008 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5009 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5010 | if (sleepUs > 0) { |
| 5011 | usleep(sleepUs); |
| 5012 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5015 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5016 | Vector< sp<RecordTrack> > activeTracks; |
| 5017 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5018 | // reference to the (first and only) fast track |
| 5019 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5020 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5021 | { // scope for mLock |
| 5022 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5023 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5024 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5025 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5026 | // check exitPending here because checkForNewParameters_l() and |
| 5027 | // checkForNewParameters_l() can temporarily release mLock |
| 5028 | if (exitPending()) { |
| 5029 | break; |
| 5030 | } |
| 5031 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5032 | // if no active track(s), then standby and release wakelock |
| 5033 | size_t size = mActiveTracks.size(); |
| 5034 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5035 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5036 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5037 | releaseWakeLock_l(); |
| 5038 | ALOGV("RecordThread: loop stopping"); |
| 5039 | // go to sleep |
| 5040 | mWaitWorkCV.wait(mLock); |
| 5041 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5042 | goto reacquire_wakelock; |
| 5043 | } |
| 5044 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5045 | if (mActiveTracksGen != activeTracksGen) { |
| 5046 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5047 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5048 | for (size_t i = 0; i < size; i++) { |
| 5049 | tmp.add(mActiveTracks[i]->uid()); |
| 5050 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5051 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5052 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5053 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5054 | bool doBroadcast = false; |
| 5055 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5056 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5057 | activeTrack = mActiveTracks[i]; |
| 5058 | if (activeTrack->isTerminated()) { |
| 5059 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5060 | mActiveTracks.remove(activeTrack); |
| 5061 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5062 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5063 | continue; |
| 5064 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5065 | |
| 5066 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5067 | switch (activeTrackState) { |
| 5068 | |
| 5069 | case TrackBase::PAUSING: |
| 5070 | mActiveTracks.remove(activeTrack); |
| 5071 | mActiveTracksGen++; |
| 5072 | doBroadcast = true; |
| 5073 | size--; |
| 5074 | continue; |
| 5075 | |
| 5076 | case TrackBase::STARTING_1: |
| 5077 | sleepUs = 10000; |
| 5078 | i++; |
| 5079 | continue; |
| 5080 | |
| 5081 | case TrackBase::STARTING_2: |
| 5082 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5083 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5084 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5085 | break; |
| 5086 | |
| 5087 | case TrackBase::ACTIVE: |
| 5088 | break; |
| 5089 | |
| 5090 | case TrackBase::IDLE: |
| 5091 | i++; |
| 5092 | continue; |
| 5093 | |
| 5094 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5095 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5096 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5097 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5098 | activeTracks.add(activeTrack); |
| 5099 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5100 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5101 | if (activeTrack->isFastTrack()) { |
| 5102 | ALOG_ASSERT(!mFastTrackAvail); |
| 5103 | ALOG_ASSERT(fastTrack == 0); |
| 5104 | fastTrack = activeTrack; |
| 5105 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5106 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5107 | if (doBroadcast) { |
| 5108 | mStartStopCond.broadcast(); |
| 5109 | } |
| 5110 | |
| 5111 | // sleep if there are no active tracks to process |
| 5112 | if (activeTracks.size() == 0) { |
| 5113 | if (sleepUs == 0) { |
| 5114 | sleepUs = kRecordThreadSleepUs; |
| 5115 | } |
| 5116 | continue; |
| 5117 | } |
| 5118 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5119 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5120 | lockEffectChains_l(effectChains); |
| 5121 | } |
| 5122 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5123 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5124 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5125 | size_t size = effectChains.size(); |
| 5126 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5127 | // thread mutex is not locked, but effect chain is locked |
| 5128 | effectChains[i]->process_l(); |
| 5129 | } |
| 5130 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5131 | // Start the fast capture if it's not already running |
| 5132 | if (mFastCapture != 0) { |
| 5133 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5134 | FastCaptureState *state = sq->begin(); |
| 5135 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5136 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5137 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5138 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5139 | if (old == -1) { |
| 5140 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5141 | } |
| 5142 | } |
| 5143 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5144 | #if 0 // FIXME |
| 5145 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
| 5146 | FastCaptureDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN); |
| 5147 | #endif |
| 5148 | state->mCblk = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5149 | sq->end(); |
| 5150 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5151 | #if 0 |
| 5152 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5153 | mNormalSource = mPipeSource; |
| 5154 | } |
| 5155 | #endif |
| 5156 | } else { |
| 5157 | sq->end(false /*didModify*/); |
| 5158 | } |
| 5159 | } |
| 5160 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5161 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 5162 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 5163 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 5164 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 5165 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5166 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5167 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5168 | ssize_t framesRead; |
| 5169 | |
| 5170 | // If an NBAIO source is present, use it to read the normal capture's data |
| 5171 | if (mPipeSource != 0) { |
| 5172 | size_t framesToRead = mBufferSize / mFrameSize; |
| 5173 | framesRead = mPipeSource->read(&mRsmpInBuffer[rear * mChannelCount], |
| 5174 | framesToRead, AudioBufferProvider::kInvalidPTS); |
| 5175 | if (framesRead == 0) { |
| 5176 | // since pipe is non-blocking, simulate blocking input |
| 5177 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 5178 | } |
| 5179 | // otherwise use the HAL / AudioStreamIn directly |
| 5180 | } else { |
| 5181 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
| 5182 | &mRsmpInBuffer[rear * mChannelCount], mBufferSize); |
| 5183 | if (bytesRead < 0) { |
| 5184 | framesRead = bytesRead; |
| 5185 | } else { |
| 5186 | framesRead = bytesRead / mFrameSize; |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
| 5191 | ALOGE("read failed: framesRead=%d", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5192 | // Force input into standby so that it tries to recover at next read attempt |
| 5193 | inputStandBy(); |
| 5194 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5195 | } |
| 5196 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5197 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5198 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5199 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5200 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5201 | if (mTeeSink != 0) { |
| 5202 | (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead); |
| 5203 | } |
| 5204 | // 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] | 5205 | { |
| 5206 | size_t part1 = mRsmpInFramesP2 - rear; |
| 5207 | if ((size_t) framesRead > part1) { |
| 5208 | memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount], |
| 5209 | (framesRead - part1) * mFrameSize); |
| 5210 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5211 | } |
| 5212 | rear = mRsmpInRear += framesRead; |
| 5213 | |
| 5214 | size = activeTracks.size(); |
| 5215 | // loop over each active track |
| 5216 | for (size_t i = 0; i < size; i++) { |
| 5217 | activeTrack = activeTracks[i]; |
| 5218 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5219 | // skip fast tracks, as those are handled directly by FastCapture |
| 5220 | if (activeTrack->isFastTrack()) { |
| 5221 | continue; |
| 5222 | } |
| 5223 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5224 | enum { |
| 5225 | OVERRUN_UNKNOWN, |
| 5226 | OVERRUN_TRUE, |
| 5227 | OVERRUN_FALSE |
| 5228 | } overrun = OVERRUN_UNKNOWN; |
| 5229 | |
| 5230 | // loop over getNextBuffer to handle circular sink |
| 5231 | for (;;) { |
| 5232 | |
| 5233 | activeTrack->mSink.frameCount = ~0; |
| 5234 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 5235 | size_t framesOut = activeTrack->mSink.frameCount; |
| 5236 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 5237 | |
| 5238 | int32_t front = activeTrack->mRsmpInFront; |
| 5239 | ssize_t filled = rear - front; |
| 5240 | size_t framesIn; |
| 5241 | |
| 5242 | if (filled < 0) { |
| 5243 | // should not happen, but treat like a massive overrun and re-sync |
| 5244 | framesIn = 0; |
| 5245 | activeTrack->mRsmpInFront = rear; |
| 5246 | overrun = OVERRUN_TRUE; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5247 | } else if ((size_t) filled <= mRsmpInFrames) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5248 | framesIn = (size_t) filled; |
| 5249 | } else { |
| 5250 | // client is not keeping up with server, but give it latest data |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5251 | framesIn = mRsmpInFrames; |
| 5252 | activeTrack->mRsmpInFront = front = rear - framesIn; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5253 | overrun = OVERRUN_TRUE; |
| 5254 | } |
| 5255 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5256 | if (framesOut == 0 || framesIn == 0) { |
| 5257 | break; |
| 5258 | } |
| 5259 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5260 | if (activeTrack->mResampler == NULL) { |
| 5261 | // no resampling |
| 5262 | if (framesIn > framesOut) { |
| 5263 | framesIn = framesOut; |
| 5264 | } else { |
| 5265 | framesOut = framesIn; |
| 5266 | } |
| 5267 | int8_t *dst = activeTrack->mSink.i8; |
| 5268 | while (framesIn > 0) { |
| 5269 | front &= mRsmpInFramesP2 - 1; |
| 5270 | size_t part1 = mRsmpInFramesP2 - front; |
| 5271 | if (part1 > framesIn) { |
| 5272 | part1 = framesIn; |
| 5273 | } |
| 5274 | int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize); |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5275 | if (mChannelCount == activeTrack->mChannelCount) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5276 | memcpy(dst, src, part1 * mFrameSize); |
| 5277 | } else if (mChannelCount == 1) { |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5278 | 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] | 5279 | part1); |
| 5280 | } else { |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5281 | 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] | 5282 | part1); |
| 5283 | } |
| 5284 | dst += part1 * activeTrack->mFrameSize; |
| 5285 | front += part1; |
| 5286 | framesIn -= part1; |
| 5287 | } |
| 5288 | activeTrack->mRsmpInFront += framesOut; |
| 5289 | |
| 5290 | } else { |
| 5291 | // resampling |
| 5292 | // FIXME framesInNeeded should really be part of resampler API, and should |
| 5293 | // depend on the SRC ratio |
| 5294 | // to keep mRsmpInBuffer full so resampler always has sufficient input |
| 5295 | size_t framesInNeeded; |
| 5296 | // FIXME only re-calculate when it changes, and optimize for common ratios |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5297 | // Do not precompute in/out because floating point is not associative |
| 5298 | // e.g. a*b/c != a*(b/c). |
| 5299 | const double in(mSampleRate); |
| 5300 | const double out(activeTrack->mSampleRate); |
| 5301 | framesInNeeded = ceil(framesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5302 | 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] | 5303 | framesInNeeded, framesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5304 | // Although we theoretically have framesIn in circular buffer, some of those are |
| 5305 | // unreleased frames, and thus must be discounted for purpose of budgeting. |
| 5306 | size_t unreleased = activeTrack->mRsmpInUnrel; |
| 5307 | framesIn = framesIn > unreleased ? framesIn - unreleased : 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5308 | if (framesIn < framesInNeeded) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5309 | ALOGV("not enough to resample: have %u frames in but need %u in to " |
| 5310 | "produce %u out given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5311 | framesIn, framesInNeeded, framesOut, in / out); |
| 5312 | size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * out / in) : 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5313 | LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut); |
| 5314 | if (newFramesOut == 0) { |
| 5315 | break; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5316 | } |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5317 | framesInNeeded = ceil(newFramesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5318 | 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] | 5319 | framesInNeeded, newFramesOut, out / in); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5320 | LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded); |
| 5321 | ALOGV("success 2: have %u frames in and need %u in to produce %u out " |
| 5322 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5323 | framesIn, framesInNeeded, newFramesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5324 | framesOut = newFramesOut; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5325 | } else { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5326 | 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] | 5327 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5328 | framesIn, framesInNeeded, framesOut, in / out); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | // reallocate mRsmpOutBuffer as needed; we will grow but never shrink |
| 5332 | if (activeTrack->mRsmpOutFrameCount < framesOut) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5333 | // 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] | 5334 | delete[] activeTrack->mRsmpOutBuffer; |
| 5335 | // resampler always outputs stereo |
| 5336 | activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2]; |
| 5337 | activeTrack->mRsmpOutFrameCount = framesOut; |
| 5338 | } |
| 5339 | |
| 5340 | // resampler accumulates, but we only have one source track |
| 5341 | memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t)); |
| 5342 | activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut, |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5343 | // FIXME how about having activeTrack implement this interface itself? |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5344 | activeTrack->mResamplerBufferProvider |
| 5345 | /*this*/ /* AudioBufferProvider* */); |
| 5346 | // ditherAndClamp() works as long as all buffers returned by |
| 5347 | // activeTrack->getNextBuffer() are 32 bit aligned which should be always true. |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5348 | if (activeTrack->mChannelCount == 1) { |
Andy Hung | 84a0c6e | 2014-04-02 11:24:53 -0700 | [diff] [blame] | 5349 | // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5350 | ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer, |
| 5351 | framesOut); |
| 5352 | // the resampler always outputs stereo samples: |
| 5353 | // do post stereo to mono conversion |
| 5354 | downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16, |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5355 | (const int16_t *)activeTrack->mRsmpOutBuffer, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5356 | } else { |
| 5357 | ditherAndClamp((int32_t *)activeTrack->mSink.raw, |
| 5358 | activeTrack->mRsmpOutBuffer, framesOut); |
| 5359 | } |
| 5360 | // now done with mRsmpOutBuffer |
| 5361 | |
| 5362 | } |
| 5363 | |
| 5364 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 5365 | overrun = OVERRUN_FALSE; |
| 5366 | } |
| 5367 | |
| 5368 | if (activeTrack->mFramesToDrop == 0) { |
| 5369 | if (framesOut > 0) { |
| 5370 | activeTrack->mSink.frameCount = framesOut; |
| 5371 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 5372 | } |
| 5373 | } else { |
| 5374 | // FIXME could do a partial drop of framesOut |
| 5375 | if (activeTrack->mFramesToDrop > 0) { |
| 5376 | activeTrack->mFramesToDrop -= framesOut; |
| 5377 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5378 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5379 | } |
| 5380 | } else { |
| 5381 | activeTrack->mFramesToDrop += framesOut; |
| 5382 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 5383 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 5384 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 5385 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 5386 | activeTrack->sessionId(), |
| 5387 | (activeTrack->mSyncStartEvent != 0) ? |
| 5388 | activeTrack->mSyncStartEvent->triggerSession() : 0); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5389 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5395 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5396 | } |
| 5397 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5398 | |
| 5399 | switch (overrun) { |
| 5400 | case OVERRUN_TRUE: |
| 5401 | // client isn't retrieving buffers fast enough |
| 5402 | if (!activeTrack->setOverflow()) { |
| 5403 | nsecs_t now = systemTime(); |
| 5404 | // FIXME should lastWarning per track? |
| 5405 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 5406 | ALOGW("RecordThread: buffer overflow"); |
| 5407 | lastWarning = now; |
| 5408 | } |
| 5409 | } |
| 5410 | break; |
| 5411 | case OVERRUN_FALSE: |
| 5412 | activeTrack->clearOverflow(); |
| 5413 | break; |
| 5414 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5415 | break; |
| 5416 | } |
| 5417 | |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5418 | } |
| 5419 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5420 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5421 | // enable changes in effect chain |
| 5422 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5423 | // 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] | 5424 | } |
| 5425 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5426 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5427 | |
| 5428 | { |
| 5429 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 5430 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5431 | sp<RecordTrack> track = mTracks[i]; |
| 5432 | track->invalidate(); |
| 5433 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5434 | mActiveTracks.clear(); |
| 5435 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5436 | mStartStopCond.broadcast(); |
| 5437 | } |
| 5438 | |
| 5439 | releaseWakeLock(); |
| 5440 | |
| 5441 | ALOGV("RecordThread %p exiting", this); |
| 5442 | return false; |
| 5443 | } |
| 5444 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5445 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5446 | { |
| 5447 | if (!mStandby) { |
| 5448 | inputStandBy(); |
| 5449 | mStandby = true; |
| 5450 | } |
| 5451 | } |
| 5452 | |
| 5453 | void AudioFlinger::RecordThread::inputStandBy() |
| 5454 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5455 | // Idle the fast capture if it's currently running |
| 5456 | if (mFastCapture != 0) { |
| 5457 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5458 | FastCaptureState *state = sq->begin(); |
| 5459 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 5460 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5461 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5462 | state->mColdGen++; |
| 5463 | mFastCaptureFutex = 0; |
| 5464 | sq->end(); |
| 5465 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 5466 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 5467 | #if 0 |
| 5468 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5469 | // FIXME |
| 5470 | } |
| 5471 | #endif |
| 5472 | #ifdef AUDIO_WATCHDOG |
| 5473 | // FIXME |
| 5474 | #endif |
| 5475 | } else { |
| 5476 | sq->end(false /*didModify*/); |
| 5477 | } |
| 5478 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5479 | mInput->stream->common.standby(&mInput->stream->common); |
| 5480 | } |
| 5481 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5482 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 5483 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5484 | const sp<AudioFlinger::Client>& client, |
| 5485 | uint32_t sampleRate, |
| 5486 | audio_format_t format, |
| 5487 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5488 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5489 | int sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 5490 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5491 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 5492 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5493 | pid_t tid, |
| 5494 | status_t *status) |
| 5495 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5496 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5497 | sp<RecordTrack> track; |
| 5498 | status_t lStatus; |
| 5499 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5500 | // client expresses a preference for FAST, but we get the final say |
| 5501 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5502 | if ( |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5503 | // use case: callback handler |
| 5504 | (tid != -1) && |
| 5505 | // frame count is not specified, or is exactly the pipe depth |
| 5506 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5507 | // PCM data |
| 5508 | audio_is_linear_pcm(format) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5509 | // native format |
| 5510 | (format == mFormat) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5511 | // native channel mask |
| 5512 | (channelMask == mChannelMask) && |
| 5513 | // native hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5514 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5515 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5516 | hasFastCapture() && |
| 5517 | // there are sufficient fast track slots available |
| 5518 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5519 | ) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5520 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5521 | frameCount, mFrameCount); |
| 5522 | } else { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5523 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " |
| 5524 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5525 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5526 | frameCount, mFrameCount, mPipeFramesP2, |
| 5527 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 5528 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5529 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | // compute track buffer size in frames, and suggest the notification frame count |
| 5534 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5535 | // fast track: frame count is exactly the pipe depth |
| 5536 | frameCount = mPipeFramesP2; |
| 5537 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 5538 | *notificationFrames = mFrameCount; |
| 5539 | } else { |
| 5540 | // not fast track: frame count is at least 2 HAL buffers and at least 20 ms |
| 5541 | size_t minFrameCount = ((int64_t) mFrameCount * 2 * sampleRate + mSampleRate - 1) / |
| 5542 | mSampleRate; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5543 | if (frameCount < minFrameCount) { |
| 5544 | frameCount = minFrameCount; |
| 5545 | } |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5546 | minFrameCount = (sampleRate * 20 / 1000 + 1) & ~1; |
| 5547 | if (frameCount < minFrameCount) { |
| 5548 | frameCount = minFrameCount; |
| 5549 | } |
| 5550 | // notification is forced to be at least double-buffering |
| 5551 | size_t maxNotification = frameCount / 2; |
| 5552 | if (*notificationFrames == 0 || *notificationFrames > maxNotification) { |
| 5553 | *notificationFrames = maxNotification; |
| 5554 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5555 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5556 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5557 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 5558 | lStatus = initCheck(); |
| 5559 | if (lStatus != NO_ERROR) { |
| 5560 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 5561 | goto Exit; |
| 5562 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5563 | |
| 5564 | { // scope for mLock |
| 5565 | Mutex::Autolock _l(mLock); |
| 5566 | |
| 5567 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5568 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 5569 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5570 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 5571 | lStatus = track->initCheck(); |
| 5572 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 5573 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 5574 | // 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] | 5575 | goto Exit; |
| 5576 | } |
| 5577 | mTracks.add(track); |
| 5578 | |
| 5579 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5580 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 5581 | mAudioFlinger->btNrecIsOff(); |
| 5582 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5583 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5584 | |
| 5585 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 5586 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 5587 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 5588 | // so ask activity manager to do this on our behalf |
| 5589 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 5590 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5591 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5592 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5593 | lStatus = NO_ERROR; |
| 5594 | |
| 5595 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 5596 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5597 | return track; |
| 5598 | } |
| 5599 | |
| 5600 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 5601 | AudioSystem::sync_event_t event, |
| 5602 | int triggerSession) |
| 5603 | { |
| 5604 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 5605 | sp<ThreadBase> strongMe = this; |
| 5606 | status_t status = NO_ERROR; |
| 5607 | |
| 5608 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5609 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5610 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5611 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5612 | triggerSession, |
| 5613 | recordTrack->sessionId(), |
| 5614 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5615 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5616 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 5617 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5618 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5619 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5620 | } else { |
| 5621 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5622 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5623 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5624 | } |
| 5625 | } |
| 5626 | |
| 5627 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5628 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5629 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5630 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 5631 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 5632 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5633 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5634 | } else { |
| 5635 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5636 | } |
| 5637 | return status; |
| 5638 | } |
| 5639 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5640 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 5641 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 5642 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5643 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5644 | mActiveTracks.add(recordTrack); |
| 5645 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5646 | status_t status = NO_ERROR; |
| 5647 | if (recordTrack->isExternalTrack()) { |
| 5648 | mLock.unlock(); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 5649 | status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5650 | mLock.lock(); |
| 5651 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 5652 | if (status != NO_ERROR) { |
| 5653 | mActiveTracks.remove(recordTrack); |
| 5654 | mActiveTracksGen++; |
| 5655 | recordTrack->clearSyncStartEvent(); |
| 5656 | ALOGV("RecordThread::start error %d", status); |
| 5657 | return status; |
| 5658 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5659 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5660 | // Catch up with current buffer indices if thread is already running. |
| 5661 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 5662 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 5663 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 5664 | |
| 5665 | recordTrack->mRsmpInFront = mRsmpInRear; |
| 5666 | recordTrack->mRsmpInUnrel = 0; |
| 5667 | // FIXME why reset? |
| 5668 | if (recordTrack->mResampler != NULL) { |
| 5669 | recordTrack->mResampler->reset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5670 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5671 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5672 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5673 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5674 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5675 | ALOGV("Record failed to start"); |
| 5676 | status = BAD_VALUE; |
| 5677 | goto startError; |
| 5678 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5679 | return status; |
| 5680 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 5681 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5682 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5683 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 5684 | AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5685 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5686 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5687 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5688 | return status; |
| 5689 | } |
| 5690 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5691 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 5692 | { |
| 5693 | sp<SyncEvent> strongEvent = event.promote(); |
| 5694 | |
| 5695 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 5696 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 5697 | if (ptr != 0) { |
| 5698 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 5699 | recordTrack->handleSyncStartEvent(strongEvent); |
| 5700 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5701 | } |
| 5702 | } |
| 5703 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 5704 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5705 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 5706 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5707 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5708 | return false; |
| 5709 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5710 | // 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] | 5711 | recordTrack->mState = TrackBase::PAUSING; |
| 5712 | // do not wait for mStartStopCond if exiting |
| 5713 | if (exitPending()) { |
| 5714 | return true; |
| 5715 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5716 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5717 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5718 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 5719 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5720 | ALOGV("Record stopped OK"); |
| 5721 | return true; |
| 5722 | } |
| 5723 | return false; |
| 5724 | } |
| 5725 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5726 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5727 | { |
| 5728 | return false; |
| 5729 | } |
| 5730 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5731 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5732 | { |
| 5733 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 5734 | if (!isValidSyncEvent(event)) { |
| 5735 | return BAD_VALUE; |
| 5736 | } |
| 5737 | |
| 5738 | int eventSession = event->triggerSession(); |
| 5739 | status_t ret = NAME_NOT_FOUND; |
| 5740 | |
| 5741 | Mutex::Autolock _l(mLock); |
| 5742 | |
| 5743 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5744 | sp<RecordTrack> track = mTracks[i]; |
| 5745 | if (eventSession == track->sessionId()) { |
| 5746 | (void) track->setSyncEvent(event); |
| 5747 | ret = NO_ERROR; |
| 5748 | } |
| 5749 | } |
| 5750 | return ret; |
| 5751 | #else |
| 5752 | return BAD_VALUE; |
| 5753 | #endif |
| 5754 | } |
| 5755 | |
| 5756 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 5757 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 5758 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5759 | track->terminate(); |
| 5760 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5761 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5762 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5763 | removeTrack_l(track); |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 5768 | { |
| 5769 | mTracks.remove(track); |
| 5770 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5771 | if (track->isFastTrack()) { |
| 5772 | ALOG_ASSERT(!mFastTrackAvail); |
| 5773 | mFastTrackAvail = true; |
| 5774 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 5778 | { |
| 5779 | dumpInternals(fd, args); |
| 5780 | dumpTracks(fd, args); |
| 5781 | dumpEffectChains(fd, args); |
| 5782 | } |
| 5783 | |
| 5784 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 5785 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5786 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5787 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5788 | if (mActiveTracks.size() > 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5789 | dprintf(fd, " Buffer size: %zu bytes\n", mBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5790 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5791 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5792 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5793 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5794 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5795 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5796 | dumpBase(fd, args); |
| 5797 | } |
| 5798 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5799 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5800 | { |
| 5801 | const size_t SIZE = 256; |
| 5802 | char buffer[SIZE]; |
| 5803 | String8 result; |
| 5804 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5805 | size_t numtracks = mTracks.size(); |
| 5806 | size_t numactive = mActiveTracks.size(); |
| 5807 | size_t numactiveseen = 0; |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5808 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5809 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5810 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5811 | RecordTrack::appendDumpHeader(result); |
| 5812 | for (size_t i = 0; i < numtracks ; ++i) { |
| 5813 | sp<RecordTrack> track = mTracks[i]; |
| 5814 | if (track != 0) { |
| 5815 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 5816 | if (active) { |
| 5817 | numactiveseen++; |
| 5818 | } |
| 5819 | track->dump(buffer, SIZE, active); |
| 5820 | result.append(buffer); |
| 5821 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5822 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5823 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 5824 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5825 | } |
| 5826 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5827 | if (numactiveseen != numactive) { |
| 5828 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 5829 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5830 | result.append(buffer); |
| 5831 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5832 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5833 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 5834 | if (mTracks.indexOf(track) < 0) { |
| 5835 | track->dump(buffer, SIZE, true); |
| 5836 | result.append(buffer); |
| 5837 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5838 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5839 | |
| 5840 | } |
| 5841 | write(fd, result.string(), result.size()); |
| 5842 | } |
| 5843 | |
| 5844 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5845 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
| 5846 | AudioBufferProvider::Buffer* buffer, int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5847 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5848 | RecordTrack *activeTrack = mRecordTrack; |
| 5849 | sp<ThreadBase> threadBase = activeTrack->mThread.promote(); |
| 5850 | if (threadBase == 0) { |
| 5851 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5852 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5853 | return NOT_ENOUGH_DATA; |
| 5854 | } |
| 5855 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 5856 | int32_t rear = recordThread->mRsmpInRear; |
| 5857 | int32_t front = activeTrack->mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5858 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5859 | // FIXME should not be P2 (don't want to increase latency) |
| 5860 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5861 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5862 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5863 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 5864 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5865 | if (part1 > (size_t) filled) { |
| 5866 | part1 = filled; |
| 5867 | } |
| 5868 | size_t ask = buffer->frameCount; |
| 5869 | ALOG_ASSERT(ask > 0); |
| 5870 | if (part1 > ask) { |
| 5871 | part1 = ask; |
| 5872 | } |
| 5873 | if (part1 == 0) { |
| 5874 | // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5875 | LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved"); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5876 | buffer->raw = NULL; |
| 5877 | buffer->frameCount = 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5878 | activeTrack->mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5879 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5880 | } |
| 5881 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5882 | buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5883 | buffer->frameCount = part1; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5884 | activeTrack->mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5885 | return NO_ERROR; |
| 5886 | } |
| 5887 | |
| 5888 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5889 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 5890 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5891 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5892 | RecordTrack *activeTrack = mRecordTrack; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5893 | size_t stepCount = buffer->frameCount; |
| 5894 | if (stepCount == 0) { |
| 5895 | return; |
| 5896 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5897 | ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel); |
| 5898 | activeTrack->mRsmpInUnrel -= stepCount; |
| 5899 | activeTrack->mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 5900 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5901 | buffer->frameCount = 0; |
| 5902 | } |
| 5903 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5904 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5905 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5906 | { |
| 5907 | bool reconfig = false; |
| 5908 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5909 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5910 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5911 | audio_format_t reqFormat = mFormat; |
| 5912 | uint32_t samplingRate = mSampleRate; |
| 5913 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 5914 | |
| 5915 | AudioParameter param = AudioParameter(keyValuePair); |
| 5916 | int value; |
| 5917 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 5918 | // channel count change can be requested. Do we mandate the first client defines the |
| 5919 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 5920 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 5921 | samplingRate = value; |
| 5922 | reconfig = true; |
| 5923 | } |
| 5924 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 5925 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
| 5926 | status = BAD_VALUE; |
| 5927 | } else { |
| 5928 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5929 | reconfig = true; |
| 5930 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5931 | } |
| 5932 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 5933 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
| 5934 | if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) { |
| 5935 | status = BAD_VALUE; |
| 5936 | } else { |
| 5937 | channelMask = mask; |
| 5938 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5939 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5940 | } |
| 5941 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5942 | // do not accept frame count changes if tracks are open as the track buffer |
| 5943 | // size depends on frame count and correct behavior would not be guaranteed |
| 5944 | // if frame count is changed after track creation |
| 5945 | if (mActiveTracks.size() > 0) { |
| 5946 | status = INVALID_OPERATION; |
| 5947 | } else { |
| 5948 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5949 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5950 | } |
| 5951 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5952 | // forward device change to effects that have requested to be |
| 5953 | // aware of attached audio device. |
| 5954 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5955 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5956 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5957 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5958 | // store input device and output device but do not forward output device to audio HAL. |
| 5959 | // Note that status is ignored by the caller for output device |
| 5960 | // (see AudioFlinger::setParameters() |
| 5961 | if (audio_is_output_devices(value)) { |
| 5962 | mOutDevice = value; |
| 5963 | status = BAD_VALUE; |
| 5964 | } else { |
| 5965 | mInDevice = value; |
| 5966 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 5967 | // pre processings |
| 5968 | if (mTracks.size() > 0) { |
| 5969 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 5970 | mAudioFlinger->btNrecIsOff(); |
| 5971 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5972 | sp<RecordTrack> track = mTracks[i]; |
| 5973 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 5974 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5975 | } |
| 5976 | } |
| 5977 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5978 | } |
| 5979 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 5980 | mAudioSource != (audio_source_t)value) { |
| 5981 | // forward device change to effects that have requested to be |
| 5982 | // aware of attached audio device. |
| 5983 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5984 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5985 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5986 | mAudioSource = (audio_source_t)value; |
| 5987 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 5988 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5989 | if (status == NO_ERROR) { |
| 5990 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 5991 | keyValuePair.string()); |
| 5992 | if (status == INVALID_OPERATION) { |
| 5993 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5994 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 5995 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5996 | } |
| 5997 | if (reconfig) { |
| 5998 | if (status == BAD_VALUE && |
| 5999 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
| 6000 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
| 6001 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
| 6002 | <= (2 * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6003 | audio_channel_count_from_in_mask( |
| 6004 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6005 | (channelMask == AUDIO_CHANNEL_IN_MONO || |
| 6006 | channelMask == AUDIO_CHANNEL_IN_STEREO)) { |
| 6007 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6008 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6009 | if (status == NO_ERROR) { |
| 6010 | readInputParameters_l(); |
| 6011 | sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6012 | } |
| 6013 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6014 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6015 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6016 | return reconfig; |
| 6017 | } |
| 6018 | |
| 6019 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6020 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6021 | Mutex::Autolock _l(mLock); |
| 6022 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6023 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6024 | } |
| 6025 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6026 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 6027 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6028 | free(s); |
| 6029 | return out_s8; |
| 6030 | } |
| 6031 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6032 | void AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6033 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | b2737d0 | 2013-08-19 12:03:11 -0700 | [diff] [blame] | 6034 | const void *param2 = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6035 | |
| 6036 | switch (event) { |
| 6037 | case AudioSystem::INPUT_OPENED: |
| 6038 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 6039 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6040 | desc.samplingRate = mSampleRate; |
| 6041 | desc.format = mFormat; |
| 6042 | desc.frameCount = mFrameCount; |
| 6043 | desc.latency = 0; |
| 6044 | param2 = &desc; |
| 6045 | break; |
| 6046 | |
| 6047 | case AudioSystem::INPUT_CLOSED: |
| 6048 | default: |
| 6049 | break; |
| 6050 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6051 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6052 | } |
| 6053 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6054 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6055 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6056 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 6057 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6058 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6059 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 6060 | mFormat = mHALFormat; |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6061 | if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 6062 | 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] | 6063 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6064 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6065 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 6066 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6067 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6068 | // 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] | 6069 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6070 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6071 | // A larger value should allow more old data to be read after a track calls start(), |
| 6072 | // without increasing latency. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6073 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6074 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6075 | delete[] mRsmpInBuffer; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6076 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 6077 | mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6078 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6079 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 6080 | // 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] | 6081 | } |
| 6082 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6083 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6084 | { |
| 6085 | Mutex::Autolock _l(mLock); |
| 6086 | if (initCheck() != NO_ERROR) { |
| 6087 | return 0; |
| 6088 | } |
| 6089 | |
| 6090 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 6091 | } |
| 6092 | |
| 6093 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const |
| 6094 | { |
| 6095 | Mutex::Autolock _l(mLock); |
| 6096 | uint32_t result = 0; |
| 6097 | if (getEffectChain_l(sessionId) != 0) { |
| 6098 | result = EFFECT_SESSION; |
| 6099 | } |
| 6100 | |
| 6101 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6102 | if (sessionId == mTracks[i]->sessionId()) { |
| 6103 | result |= TRACK_SESSION; |
| 6104 | break; |
| 6105 | } |
| 6106 | } |
| 6107 | |
| 6108 | return result; |
| 6109 | } |
| 6110 | |
| 6111 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const |
| 6112 | { |
| 6113 | KeyedVector<int, bool> ids; |
| 6114 | Mutex::Autolock _l(mLock); |
| 6115 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 6116 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
| 6117 | int sessionId = track->sessionId(); |
| 6118 | if (ids.indexOfKey(sessionId) < 0) { |
| 6119 | ids.add(sessionId, true); |
| 6120 | } |
| 6121 | } |
| 6122 | return ids; |
| 6123 | } |
| 6124 | |
| 6125 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6126 | { |
| 6127 | Mutex::Autolock _l(mLock); |
| 6128 | AudioStreamIn *input = mInput; |
| 6129 | mInput = NULL; |
| 6130 | return input; |
| 6131 | } |
| 6132 | |
| 6133 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 6134 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 6135 | { |
| 6136 | if (mInput == NULL) { |
| 6137 | return NULL; |
| 6138 | } |
| 6139 | return &mInput->stream->common; |
| 6140 | } |
| 6141 | |
| 6142 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6143 | { |
| 6144 | // only one chain per input thread |
| 6145 | if (mEffectChains.size() != 0) { |
| 6146 | return INVALID_OPERATION; |
| 6147 | } |
| 6148 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
| 6149 | |
| 6150 | chain->setInBuffer(NULL); |
| 6151 | chain->setOutBuffer(NULL); |
| 6152 | |
| 6153 | checkSuspendOnAddEffectChain_l(chain); |
| 6154 | |
| 6155 | mEffectChains.add(chain); |
| 6156 | |
| 6157 | return NO_ERROR; |
| 6158 | } |
| 6159 | |
| 6160 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6161 | { |
| 6162 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 6163 | ALOGW_IF(mEffectChains.size() != 1, |
| 6164 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6165 | chain.get(), mEffectChains.size(), this); |
| 6166 | if (mEffectChains.size() == 1) { |
| 6167 | mEffectChains.removeAt(0); |
| 6168 | } |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6172 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 6173 | audio_patch_handle_t *handle) |
| 6174 | { |
| 6175 | status_t status = NO_ERROR; |
| 6176 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6177 | // store new device and send to effects |
| 6178 | mInDevice = patch->sources[0].ext.device.type; |
| 6179 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6180 | mEffectChains[i]->setDevice_l(mInDevice); |
| 6181 | } |
| 6182 | |
| 6183 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 6184 | // pre processings |
| 6185 | if (mTracks.size() > 0) { |
| 6186 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6187 | mAudioFlinger->btNrecIsOff(); |
| 6188 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6189 | sp<RecordTrack> track = mTracks[i]; |
| 6190 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 6191 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | // store new source and send to effects |
| 6196 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 6197 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 6198 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6199 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 6200 | } |
| 6201 | } |
| 6202 | |
| 6203 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6204 | status = hwDevice->create_audio_patch(hwDevice, |
| 6205 | patch->num_sources, |
| 6206 | patch->sources, |
| 6207 | patch->num_sinks, |
| 6208 | patch->sinks, |
| 6209 | handle); |
| 6210 | } else { |
| 6211 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 6212 | } |
| 6213 | return status; |
| 6214 | } |
| 6215 | |
| 6216 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 6217 | { |
| 6218 | status_t status = NO_ERROR; |
| 6219 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6220 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6221 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 6222 | } else { |
| 6223 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 6224 | } |
| 6225 | return status; |
| 6226 | } |
| 6227 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6228 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 6229 | { |
| 6230 | Mutex::Autolock _l(mLock); |
| 6231 | mTracks.add(record); |
| 6232 | } |
| 6233 | |
| 6234 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 6235 | { |
| 6236 | Mutex::Autolock _l(mLock); |
| 6237 | destroyTrack_l(record); |
| 6238 | } |
| 6239 | |
| 6240 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 6241 | { |
| 6242 | ThreadBase::getAudioPortConfig(config); |
| 6243 | config->role = AUDIO_PORT_ROLE_SINK; |
| 6244 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 6245 | config->ext.mix.usecase.source = mAudioSource; |
| 6246 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6247 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6248 | }; // namespace android |