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> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 26 | #include <linux/futex.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
Glenn Kasten | ad8510a | 2015-02-17 16:24:07 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/AudioParameter.h> |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 31 | #include <media/AudioResamplerPublic.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
Alex Ray | 371eb97 | 2012-11-30 11:11:54 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 34 | |
| 35 | #include <private/media/AudioTrackShared.h> |
| 36 | #include <hardware/audio.h> |
| 37 | #include <audio_effects/effect_ns.h> |
| 38 | #include <audio_effects/effect_aec.h> |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 39 | #include <audio_utils/conversion.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 41 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 42 | #include <audio_utils/minifloat.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 43 | |
| 44 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 45 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 46 | #include <media/nbaio/AudioStreamOutSink.h> |
| 47 | #include <media/nbaio/MonoPipe.h> |
| 48 | #include <media/nbaio/MonoPipeReader.h> |
| 49 | #include <media/nbaio/Pipe.h> |
| 50 | #include <media/nbaio/PipeReader.h> |
| 51 | #include <media/nbaio/SourceAudioBufferProvider.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 52 | #include <mediautils/BatteryNotifier.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 53 | |
| 54 | #include <powermanager/PowerManager.h> |
| 55 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 56 | #include "AudioFlinger.h" |
| 57 | #include "AudioMixer.h" |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 58 | #include "BufferProviders.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 60 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #include "ServiceUtilities.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 62 | #include "mediautils/SchedulingPolicyService.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 63 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | #ifdef ADD_BATTERY_DATA |
| 65 | #include <media/IMediaPlayerService.h> |
| 66 | #include <media/IMediaDeathNotifier.h> |
| 67 | #endif |
| 68 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | #ifdef DEBUG_CPU_USAGE |
| 70 | #include <cpustats/CentralTendencyStatistics.h> |
| 71 | #include <cpustats/ThreadCpuUsage.h> |
| 72 | #endif |
| 73 | |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
| 76 | // Note: the following macro is used for extremely verbose logging message. In |
| 77 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 78 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 79 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 80 | // turned on. Do not uncomment the #def below unless you really know what you |
| 81 | // are doing and want to see all of the extremely verbose messages. |
| 82 | //#define VERY_VERY_VERBOSE_LOGGING |
| 83 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 84 | #define ALOGVV ALOGV |
| 85 | #else |
| 86 | #define ALOGVV(a...) do { } while(0) |
| 87 | #endif |
| 88 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 89 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 90 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 91 | template <typename T> |
| 92 | static inline T min(const T& a, const T& b) |
| 93 | { |
| 94 | return a < b ? a : b; |
| 95 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 96 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 97 | #ifndef ARRAY_SIZE |
| 98 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 99 | #endif |
| 100 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 101 | namespace android { |
| 102 | |
| 103 | // retry counts for buffer fill timeout |
| 104 | // 50 * ~20msecs = 1 second |
| 105 | static const int8_t kMaxTrackRetries = 50; |
| 106 | static const int8_t kMaxTrackStartupRetries = 50; |
| 107 | // allow less retry attempts on direct output thread. |
| 108 | // direct outputs can be a scarce resource in audio hardware and should |
| 109 | // be released as quickly as possible. |
| 110 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 111 | |
| 112 | // don't warn about blocked writes or record buffer overflows more often than this |
| 113 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 114 | |
| 115 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 116 | static const int kRecordThreadSleepUs = 5000; |
| 117 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 118 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 119 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 120 | |
| 121 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 122 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 123 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 124 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 125 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 126 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 127 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 128 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 129 | // maximum normal sink buffer size |
| 130 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 131 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 132 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 133 | // FIXME This should be based on experimentally observed scheduling jitter |
| 134 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 135 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 136 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 137 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 138 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 139 | // Whether to use fast mixer |
| 140 | static const enum { |
| 141 | FastMixer_Never, // never initialize or use: for debugging only |
| 142 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 143 | // normal mixer multiplier is 1 |
| 144 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 145 | // multiplier is calculated based on min & max normal mixer buffer size |
| 146 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 147 | // multiplier is calculated based on min & max normal mixer buffer size |
| 148 | // FIXME for FastMixer_Dynamic: |
| 149 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 150 | // For example, one HAL implementation returns an error from a large write, |
| 151 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 152 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 153 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 154 | } kUseFastMixer = FastMixer_Static; |
| 155 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 156 | // Whether to use fast capture |
| 157 | static const enum { |
| 158 | FastCapture_Never, // never initialize or use: for debugging only |
| 159 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 160 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 161 | } kUseFastCapture = FastCapture_Static; |
| 162 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 | // Priorities for requestPriority |
| 164 | static const int kPriorityAudioApp = 2; |
| 165 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 166 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 167 | |
| 168 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 169 | // 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] | 170 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 171 | // So for now we just assume that client is double-buffered for fast tracks. |
| 172 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 173 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 174 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 175 | |
| 176 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 177 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 178 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 179 | // The minimum and maximum allowed values |
| 180 | static const int kFastTrackMultiplierMin = 1; |
| 181 | static const int kFastTrackMultiplierMax = 2; |
| 182 | |
| 183 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 184 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 185 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 186 | // See Thread::readOnlyHeap(). |
| 187 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 188 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 189 | // 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] | 190 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 191 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 192 | // ---------------------------------------------------------------------------- |
| 193 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 194 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 195 | |
| 196 | static void sFastTrackMultiplierInit() |
| 197 | { |
| 198 | char value[PROPERTY_VALUE_MAX]; |
| 199 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 200 | char *endptr; |
| 201 | unsigned long ul = strtoul(value, &endptr, 0); |
| 202 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 203 | sFastTrackMultiplier = (int) ul; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // ---------------------------------------------------------------------------- |
| 209 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 210 | #ifdef ADD_BATTERY_DATA |
| 211 | // To collect the amplifier usage |
| 212 | static void addBatteryData(uint32_t params) { |
| 213 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 214 | if (service == NULL) { |
| 215 | // it already logged |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | service->addBatteryData(params); |
| 220 | } |
| 221 | #endif |
| 222 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 223 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 224 | struct { |
| 225 | // call when you acquire a partial wakelock |
| 226 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 227 | pthread_mutex_lock(&mLock); |
| 228 | if (wakeLockToken.get() == nullptr) { |
| 229 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 230 | } else { |
| 231 | if (mCount == 0) { |
| 232 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 233 | } |
| 234 | ++mCount; |
| 235 | } |
| 236 | pthread_mutex_unlock(&mLock); |
| 237 | } |
| 238 | |
| 239 | // call when you release a partial wakelock. |
| 240 | void release(const sp<IBinder> &wakeLockToken) { |
| 241 | if (wakeLockToken.get() == nullptr) { |
| 242 | return; |
| 243 | } |
| 244 | pthread_mutex_lock(&mLock); |
| 245 | if (--mCount < 0) { |
| 246 | ALOGE("negative wakelock count"); |
| 247 | mCount = 0; |
| 248 | } |
| 249 | pthread_mutex_unlock(&mLock); |
| 250 | } |
| 251 | |
| 252 | // retrieves the boottime timebase offset from monotonic. |
| 253 | int64_t getBoottimeOffset() { |
| 254 | pthread_mutex_lock(&mLock); |
| 255 | int64_t boottimeOffset = mBoottimeOffset; |
| 256 | pthread_mutex_unlock(&mLock); |
| 257 | return boottimeOffset; |
| 258 | } |
| 259 | |
| 260 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 261 | // and the selected timebase. |
| 262 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 263 | // |
| 264 | // This only needs to be called upon acquiring the first partial wakelock |
| 265 | // after all other partial wakelocks are released. |
| 266 | // |
| 267 | // We do an empirical measurement of the offset rather than parsing |
| 268 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 269 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 270 | int clockbase; |
| 271 | switch (timebase) { |
| 272 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 273 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 274 | break; |
| 275 | default: |
| 276 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 277 | break; |
| 278 | } |
| 279 | // try three times to get the clock offset, choose the one |
| 280 | // with the minimum gap in measurements. |
| 281 | const int tries = 3; |
| 282 | nsecs_t bestGap, measured; |
| 283 | for (int i = 0; i < tries; ++i) { |
| 284 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 285 | const nsecs_t tbase = systemTime(clockbase); |
| 286 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 287 | const nsecs_t gap = tmono2 - tmono; |
| 288 | if (i == 0 || gap < bestGap) { |
| 289 | bestGap = gap; |
| 290 | measured = tbase - ((tmono + tmono2) >> 1); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // to avoid micro-adjusting, we don't change the timebase |
| 295 | // unless it is significantly different. |
| 296 | // |
| 297 | // Assumption: It probably takes more than toleranceNs to |
| 298 | // suspend and resume the device. |
| 299 | static int64_t toleranceNs = 10000; // 10 us |
| 300 | if (llabs(*offset - measured) > toleranceNs) { |
| 301 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 302 | (long long)*offset, (long long)measured); |
| 303 | *offset = measured; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | pthread_mutex_t mLock; |
| 308 | int32_t mCount; |
| 309 | int64_t mBoottimeOffset; |
| 310 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 311 | |
| 312 | // ---------------------------------------------------------------------------- |
| 313 | // CPU Stats |
| 314 | // ---------------------------------------------------------------------------- |
| 315 | |
| 316 | class CpuStats { |
| 317 | public: |
| 318 | CpuStats(); |
| 319 | void sample(const String8 &title); |
| 320 | #ifdef DEBUG_CPU_USAGE |
| 321 | private: |
| 322 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 323 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 324 | |
| 325 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 326 | |
| 327 | int mCpuNum; // thread's current CPU number |
| 328 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 329 | #endif |
| 330 | }; |
| 331 | |
| 332 | CpuStats::CpuStats() |
| 333 | #ifdef DEBUG_CPU_USAGE |
| 334 | : mCpuNum(-1), mCpukHz(-1) |
| 335 | #endif |
| 336 | { |
| 337 | } |
| 338 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 339 | void CpuStats::sample(const String8 &title |
| 340 | #ifndef DEBUG_CPU_USAGE |
| 341 | __unused |
| 342 | #endif |
| 343 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 344 | #ifdef DEBUG_CPU_USAGE |
| 345 | // get current thread's delta CPU time in wall clock ns |
| 346 | double wcNs; |
| 347 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 348 | |
| 349 | // record sample for wall clock statistics |
| 350 | if (valid) { |
| 351 | mWcStats.sample(wcNs); |
| 352 | } |
| 353 | |
| 354 | // get the current CPU number |
| 355 | int cpuNum = sched_getcpu(); |
| 356 | |
| 357 | // get the current CPU frequency in kHz |
| 358 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 359 | |
| 360 | // check if either CPU number or frequency changed |
| 361 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 362 | mCpuNum = cpuNum; |
| 363 | mCpukHz = cpukHz; |
| 364 | // ignore sample for purposes of cycles |
| 365 | valid = false; |
| 366 | } |
| 367 | |
| 368 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 369 | if (valid && mCpukHz > 0) { |
| 370 | double cycles = wcNs * cpukHz * 0.000001; |
| 371 | mHzStats.sample(cycles); |
| 372 | } |
| 373 | |
| 374 | unsigned n = mWcStats.n(); |
| 375 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 376 | if ((n & 127) == 1) { |
| 377 | long long elapsed = mCpuUsage.elapsed(); |
| 378 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 379 | double perLoop = elapsed / (double) n; |
| 380 | double perLoop100 = perLoop * 0.01; |
| 381 | double perLoop1k = perLoop * 0.001; |
| 382 | double mean = mWcStats.mean(); |
| 383 | double stddev = mWcStats.stddev(); |
| 384 | double minimum = mWcStats.minimum(); |
| 385 | double maximum = mWcStats.maximum(); |
| 386 | double meanCycles = mHzStats.mean(); |
| 387 | double stddevCycles = mHzStats.stddev(); |
| 388 | double minCycles = mHzStats.minimum(); |
| 389 | double maxCycles = mHzStats.maximum(); |
| 390 | mCpuUsage.resetElapsed(); |
| 391 | mWcStats.reset(); |
| 392 | mHzStats.reset(); |
| 393 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 394 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 395 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 396 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 397 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 398 | title.string(), |
| 399 | elapsed * .000000001, n, perLoop * .000001, |
| 400 | mean * .001, |
| 401 | stddev * .001, |
| 402 | minimum * .001, |
| 403 | maximum * .001, |
| 404 | mean / perLoop100, |
| 405 | stddev / perLoop100, |
| 406 | minimum / perLoop100, |
| 407 | maximum / perLoop100, |
| 408 | meanCycles / perLoop1k, |
| 409 | stddevCycles / perLoop1k, |
| 410 | minCycles / perLoop1k, |
| 411 | maxCycles / perLoop1k); |
| 412 | |
| 413 | } |
| 414 | } |
| 415 | #endif |
| 416 | }; |
| 417 | |
| 418 | // ---------------------------------------------------------------------------- |
| 419 | // ThreadBase |
| 420 | // ---------------------------------------------------------------------------- |
| 421 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 422 | // static |
| 423 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 424 | { |
| 425 | switch (type) { |
| 426 | case MIXER: |
| 427 | return "MIXER"; |
| 428 | case DIRECT: |
| 429 | return "DIRECT"; |
| 430 | case DUPLICATING: |
| 431 | return "DUPLICATING"; |
| 432 | case RECORD: |
| 433 | return "RECORD"; |
| 434 | case OFFLOAD: |
| 435 | return "OFFLOAD"; |
| 436 | default: |
| 437 | return "unknown"; |
| 438 | } |
| 439 | } |
| 440 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 441 | String8 devicesToString(audio_devices_t devices) |
| 442 | { |
| 443 | static const struct mapping { |
| 444 | audio_devices_t mDevices; |
| 445 | const char * mString; |
| 446 | } mappingsOut[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 447 | {AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE"}, |
| 448 | {AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER"}, |
| 449 | {AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 450 | {AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE"}, |
| 451 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO"}, |
| 452 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 453 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT"}, |
| 454 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 455 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,"BLUETOOTH_A2DP_HEADPHONES"}, |
| 456 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER"}, |
| 457 | {AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 458 | {AUDIO_DEVICE_OUT_HDMI, "HDMI"}, |
| 459 | {AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,"ANLG_DOCK_HEADSET"}, |
| 460 | {AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,"DGTL_DOCK_HEADSET"}, |
| 461 | {AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 462 | {AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE"}, |
| 463 | {AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX"}, |
| 464 | {AUDIO_DEVICE_OUT_LINE, "LINE"}, |
| 465 | {AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC"}, |
| 466 | {AUDIO_DEVICE_OUT_SPDIF, "SPDIF"}, |
| 467 | {AUDIO_DEVICE_OUT_FM, "FM"}, |
| 468 | {AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE"}, |
| 469 | {AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE"}, |
| 470 | {AUDIO_DEVICE_OUT_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame^] | 471 | {AUDIO_DEVICE_OUT_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 472 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 473 | }, mappingsIn[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 474 | {AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION"}, |
| 475 | {AUDIO_DEVICE_IN_AMBIENT, "AMBIENT"}, |
| 476 | {AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC"}, |
| 477 | {AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 478 | {AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 479 | {AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 480 | {AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL"}, |
| 481 | {AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX"}, |
| 482 | {AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC"}, |
| 483 | {AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX"}, |
| 484 | {AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET"}, |
| 485 | {AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET"}, |
| 486 | {AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 487 | {AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE"}, |
| 488 | {AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER"}, |
| 489 | {AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER"}, |
| 490 | {AUDIO_DEVICE_IN_LINE, "LINE"}, |
| 491 | {AUDIO_DEVICE_IN_SPDIF, "SPDIF"}, |
| 492 | {AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 493 | {AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK"}, |
| 494 | {AUDIO_DEVICE_IN_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame^] | 495 | {AUDIO_DEVICE_IN_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 496 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 497 | }; |
| 498 | String8 result; |
| 499 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 500 | const mapping *entry; |
| 501 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 502 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 503 | entry = mappingsIn; |
| 504 | } else { |
| 505 | entry = mappingsOut; |
| 506 | } |
| 507 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 508 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 509 | if (devices & entry->mDevices) { |
| 510 | if (!result.isEmpty()) { |
| 511 | result.append("|"); |
| 512 | } |
| 513 | result.append(entry->mString); |
| 514 | } |
| 515 | } |
| 516 | if (devices & ~allDevices) { |
| 517 | if (!result.isEmpty()) { |
| 518 | result.append("|"); |
| 519 | } |
| 520 | result.appendFormat("0x%X", devices & ~allDevices); |
| 521 | } |
| 522 | if (result.isEmpty()) { |
| 523 | result.append(entry->mString); |
| 524 | } |
| 525 | return result; |
| 526 | } |
| 527 | |
| 528 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 529 | { |
| 530 | static const struct mapping { |
| 531 | audio_input_flags_t mFlag; |
| 532 | const char * mString; |
| 533 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 534 | {AUDIO_INPUT_FLAG_FAST, "FAST"}, |
| 535 | {AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD"}, |
| 536 | {AUDIO_INPUT_FLAG_RAW, "RAW"}, |
| 537 | {AUDIO_INPUT_FLAG_SYNC, "SYNC"}, |
| 538 | {AUDIO_INPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 539 | }; |
| 540 | String8 result; |
| 541 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 542 | const mapping *entry; |
| 543 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 544 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 545 | if (flags & entry->mFlag) { |
| 546 | if (!result.isEmpty()) { |
| 547 | result.append("|"); |
| 548 | } |
| 549 | result.append(entry->mString); |
| 550 | } |
| 551 | } |
| 552 | if (flags & ~allFlags) { |
| 553 | if (!result.isEmpty()) { |
| 554 | result.append("|"); |
| 555 | } |
| 556 | result.appendFormat("0x%X", flags & ~allFlags); |
| 557 | } |
| 558 | if (result.isEmpty()) { |
| 559 | result.append(entry->mString); |
| 560 | } |
| 561 | return result; |
| 562 | } |
| 563 | |
| 564 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 565 | { |
| 566 | static const struct mapping { |
| 567 | audio_output_flags_t mFlag; |
| 568 | const char * mString; |
| 569 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 570 | {AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT"}, |
| 571 | {AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY"}, |
| 572 | {AUDIO_OUTPUT_FLAG_FAST, "FAST"}, |
| 573 | {AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER"}, |
| 574 | {AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD,"COMPRESS_OFFLOAD"}, |
| 575 | {AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING"}, |
| 576 | {AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC"}, |
| 577 | {AUDIO_OUTPUT_FLAG_RAW, "RAW"}, |
| 578 | {AUDIO_OUTPUT_FLAG_SYNC, "SYNC"}, |
| 579 | {AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO, "IEC958_NONAUDIO"}, |
| 580 | {AUDIO_OUTPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 581 | }; |
| 582 | String8 result; |
| 583 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 584 | const mapping *entry; |
| 585 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 586 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 587 | if (flags & entry->mFlag) { |
| 588 | if (!result.isEmpty()) { |
| 589 | result.append("|"); |
| 590 | } |
| 591 | result.append(entry->mString); |
| 592 | } |
| 593 | } |
| 594 | if (flags & ~allFlags) { |
| 595 | if (!result.isEmpty()) { |
| 596 | result.append("|"); |
| 597 | } |
| 598 | result.appendFormat("0x%X", flags & ~allFlags); |
| 599 | } |
| 600 | if (result.isEmpty()) { |
| 601 | result.append(entry->mString); |
| 602 | } |
| 603 | return result; |
| 604 | } |
| 605 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 606 | const char *sourceToString(audio_source_t source) |
| 607 | { |
| 608 | switch (source) { |
| 609 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 610 | case AUDIO_SOURCE_MIC: return "mic"; |
| 611 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 612 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 613 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 614 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 615 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 616 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 617 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 618 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 619 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 620 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 621 | default: return "unknown"; |
| 622 | } |
| 623 | } |
| 624 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 625 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 626 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 627 | : Thread(false /*canCallJava*/), |
| 628 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 629 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 630 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 631 | // are set by PlaybackThread::readOutputParameters_l() or |
| 632 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 633 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 634 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 635 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 636 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 637 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 638 | mDeathRecipient(new PMDeathRecipient(this)), |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 639 | mSystemReady(systemReady), |
| 640 | mNotifiedBatteryStart(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 642 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | AudioFlinger::ThreadBase::~ThreadBase() |
| 646 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 647 | // 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] | 648 | mConfigEvents.clear(); |
| 649 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 650 | // do not lock the mutex in destructor |
| 651 | releaseWakeLock_l(); |
| 652 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 653 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | binder->unlinkToDeath(mDeathRecipient); |
| 655 | } |
| 656 | } |
| 657 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 658 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 659 | { |
| 660 | status_t status = initCheck(); |
| 661 | if (status == NO_ERROR) { |
| 662 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 663 | } else { |
| 664 | ALOGE("No working audio driver found."); |
| 665 | } |
| 666 | return status; |
| 667 | } |
| 668 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 669 | void AudioFlinger::ThreadBase::exit() |
| 670 | { |
| 671 | ALOGV("ThreadBase::exit"); |
| 672 | // do any cleanup required for exit to succeed |
| 673 | preExit(); |
| 674 | { |
| 675 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 676 | // if (!exitPending()) { |
| 677 | // // context switch from here to exit() |
| 678 | // // exit() calls requestExit(), what exitPending() observes |
| 679 | // // exit() calls signal(), which is dropped since no waiters |
| 680 | // // context switch back from exit() to here |
| 681 | // mWaitWorkCV.wait(...); |
| 682 | // // now thread is hung |
| 683 | // } |
| 684 | AutoMutex lock(mLock); |
| 685 | requestExit(); |
| 686 | mWaitWorkCV.broadcast(); |
| 687 | } |
| 688 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 689 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 690 | requestExitAndWait(); |
| 691 | } |
| 692 | |
| 693 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 694 | { |
| 695 | status_t status; |
| 696 | |
| 697 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 698 | Mutex::Autolock _l(mLock); |
| 699 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 700 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 701 | } |
| 702 | |
| 703 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 704 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 705 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 706 | { |
| 707 | status_t status = NO_ERROR; |
| 708 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 709 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 710 | event->mWaitStatus = false; |
| 711 | mPendingConfigEvents.add(event); |
| 712 | return status; |
| 713 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 714 | mConfigEvents.add(event); |
| 715 | ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 716 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 717 | mLock.unlock(); |
| 718 | { |
| 719 | Mutex::Autolock _l(event->mLock); |
| 720 | while (event->mWaitStatus) { |
| 721 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 722 | event->mStatus = TIMED_OUT; |
| 723 | event->mWaitStatus = false; |
| 724 | } |
| 725 | } |
| 726 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 727 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 728 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | return status; |
| 730 | } |
| 731 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 732 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 733 | { |
| 734 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 735 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 739 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 740 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 741 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 742 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 745 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) |
| 746 | { |
| 747 | Mutex::Autolock _l(mLock); |
| 748 | sendPrioConfigEvent_l(pid, tid, prio); |
| 749 | } |
| 750 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 751 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 752 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 753 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 754 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 755 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 758 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 759 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 760 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 761 | sp<ConfigEvent> configEvent; |
| 762 | AudioParameter param(keyValuePair); |
| 763 | int value; |
| 764 | if (param.getInt(String8(AUDIO_PARAMETER_MONO_OUTPUT), value) == NO_ERROR) { |
| 765 | setMasterMono_l(value != 0); |
| 766 | if (param.size() == 1) { |
| 767 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 768 | } |
| 769 | param.remove(String8(AUDIO_PARAMETER_MONO_OUTPUT)); |
| 770 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 771 | } else { |
| 772 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 773 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 774 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 777 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 778 | const struct audio_patch *patch, |
| 779 | audio_patch_handle_t *handle) |
| 780 | { |
| 781 | Mutex::Autolock _l(mLock); |
| 782 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 783 | status_t status = sendConfigEvent_l(configEvent); |
| 784 | if (status == NO_ERROR) { |
| 785 | CreateAudioPatchConfigEventData *data = |
| 786 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 787 | *handle = data->mHandle; |
| 788 | } |
| 789 | return status; |
| 790 | } |
| 791 | |
| 792 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 793 | const audio_patch_handle_t handle) |
| 794 | { |
| 795 | Mutex::Autolock _l(mLock); |
| 796 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 797 | return sendConfigEvent_l(configEvent); |
| 798 | } |
| 799 | |
| 800 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 801 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 802 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 803 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 804 | bool configChanged = false; |
| 805 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 806 | while (!mConfigEvents.isEmpty()) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 807 | ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); |
| 808 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 809 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 810 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 811 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 812 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 813 | // FIXME Need to understand why this has to be done asynchronously |
| 814 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 815 | true /*asynchronous*/); |
| 816 | if (err != 0) { |
| 817 | 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] | 818 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 819 | } |
| 820 | } break; |
| 821 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 822 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 823 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 824 | } break; |
| 825 | case CFG_EVENT_SET_PARAMETER: { |
| 826 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 827 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 828 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 829 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 830 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 831 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 832 | CreateAudioPatchConfigEventData *data = |
| 833 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 834 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 835 | } break; |
| 836 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 837 | ReleaseAudioPatchConfigEventData *data = |
| 838 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 839 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 840 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 841 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 842 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 843 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 844 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 845 | { |
| 846 | Mutex::Autolock _l(event->mLock); |
| 847 | if (event->mWaitStatus) { |
| 848 | event->mWaitStatus = false; |
| 849 | event->mCond.signal(); |
| 850 | } |
| 851 | } |
| 852 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 853 | } |
| 854 | |
| 855 | if (configChanged) { |
| 856 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 857 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 860 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 861 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 862 | const audio_channel_representation_t representation = |
| 863 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 864 | |
| 865 | switch (representation) { |
| 866 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 867 | if (output) { |
| 868 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 869 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 870 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 871 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 872 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 873 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 874 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 875 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 876 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 877 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 878 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 879 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 880 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 881 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 882 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 883 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 884 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 885 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 886 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 887 | } else { |
| 888 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 889 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 890 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 891 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 892 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 893 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 894 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 895 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 896 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 897 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 898 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 899 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 900 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 901 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 902 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 903 | } |
| 904 | const int len = s.length(); |
| 905 | if (len > 2) { |
| 906 | char *str = s.lockBuffer(len); // needed? |
| 907 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 908 | } |
| 909 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 910 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 911 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 912 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 913 | return s; |
| 914 | default: |
| 915 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 916 | representation, audio_channel_mask_get_bits(mask)); |
| 917 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 918 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 921 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 922 | { |
| 923 | const size_t SIZE = 256; |
| 924 | char buffer[SIZE]; |
| 925 | String8 result; |
| 926 | |
| 927 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 928 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 929 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 932 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 933 | dprintf(fd, " I/O handle: %d\n", mId); |
| 934 | dprintf(fd, " TID: %d\n", getTid()); |
| 935 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 936 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 937 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 938 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 939 | dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 940 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 941 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 942 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 943 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 944 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 945 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 946 | size_t numConfig = mConfigEvents.size(); |
| 947 | if (numConfig) { |
| 948 | for (size_t i = 0; i < numConfig; i++) { |
| 949 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 950 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 951 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 952 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 953 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 954 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 955 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 956 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 957 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 958 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 959 | |
| 960 | if (locked) { |
| 961 | mLock.unlock(); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 966 | { |
| 967 | const size_t SIZE = 256; |
| 968 | char buffer[SIZE]; |
| 969 | String8 result; |
| 970 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 971 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 972 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 973 | write(fd, buffer, strlen(buffer)); |
| 974 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 975 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 976 | sp<EffectChain> chain = mEffectChains[i]; |
| 977 | if (chain != 0) { |
| 978 | chain->dump(fd, args); |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 983 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 984 | { |
| 985 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 986 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 989 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 990 | { |
| 991 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 992 | case MIXER: |
| 993 | return String16("AudioMix"); |
| 994 | case DIRECT: |
| 995 | return String16("AudioDirectOut"); |
| 996 | case DUPLICATING: |
| 997 | return String16("AudioDup"); |
| 998 | case RECORD: |
| 999 | return String16("AudioIn"); |
| 1000 | case OFFLOAD: |
| 1001 | return String16("AudioOffload"); |
| 1002 | default: |
| 1003 | ALOG_ASSERT(false); |
| 1004 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1008 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1009 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1010 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1011 | if (mPowerManager != 0) { |
| 1012 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1013 | status_t status; |
| 1014 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1015 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1016 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1017 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1018 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1019 | uid, |
| 1020 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1021 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1022 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1023 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1024 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1025 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1026 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1027 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1028 | if (status == NO_ERROR) { |
| 1029 | mWakeLockToken = binder; |
| 1030 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1031 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1032 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1033 | |
| 1034 | if (!mNotifiedBatteryStart) { |
| 1035 | BatteryNotifier::getInstance().noteStartAudio(); |
| 1036 | mNotifiedBatteryStart = true; |
| 1037 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1038 | gBoottime.acquire(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1042 | { |
| 1043 | Mutex::Autolock _l(mLock); |
| 1044 | releaseWakeLock_l(); |
| 1045 | } |
| 1046 | |
| 1047 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1048 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1049 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1050 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1051 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1052 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1053 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 1054 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1055 | } |
| 1056 | mWakeLockToken.clear(); |
| 1057 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1058 | |
| 1059 | if (mNotifiedBatteryStart) { |
| 1060 | BatteryNotifier::getInstance().noteStopAudio(); |
| 1061 | mNotifiedBatteryStart = false; |
| 1062 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1065 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 1066 | Mutex::Autolock _l(mLock); |
| 1067 | updateWakeLockUids_l(uids); |
| 1068 | } |
| 1069 | |
| 1070 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1071 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1072 | // use checkService() to avoid blocking if power service is not up yet |
| 1073 | sp<IBinder> binder = |
| 1074 | defaultServiceManager()->checkService(String16("power")); |
| 1075 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1076 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1077 | } else { |
| 1078 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1079 | binder->linkToDeath(mDeathRecipient); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1085 | getPowerManager_l(); |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1086 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1087 | if (mSystemReady) { |
| 1088 | ALOGE("no wake lock to update, but system ready!"); |
| 1089 | } else { |
| 1090 | ALOGW("no wake lock to update, system not ready yet"); |
| 1091 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1092 | return; |
| 1093 | } |
| 1094 | if (mPowerManager != 0) { |
| 1095 | sp<IBinder> binder = new BBinder(); |
| 1096 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1097 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 1098 | true /* FIXME force oneway contrary to .aidl */); |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1099 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1103 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1104 | { |
| 1105 | Mutex::Autolock _l(mLock); |
| 1106 | releaseWakeLock_l(); |
| 1107 | mPowerManager.clear(); |
| 1108 | } |
| 1109 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1110 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1111 | { |
| 1112 | sp<ThreadBase> thread = mThread.promote(); |
| 1113 | if (thread != 0) { |
| 1114 | thread->clearPowerManager(); |
| 1115 | } |
| 1116 | ALOGW("power manager service died !!!"); |
| 1117 | } |
| 1118 | |
| 1119 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 1120 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1121 | { |
| 1122 | Mutex::Autolock _l(mLock); |
| 1123 | setEffectSuspended_l(type, suspend, sessionId); |
| 1124 | } |
| 1125 | |
| 1126 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 1127 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 1128 | { |
| 1129 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1130 | if (chain != 0) { |
| 1131 | if (type != NULL) { |
| 1132 | chain->setEffectSuspended_l(type, suspend); |
| 1133 | } else { |
| 1134 | chain->setEffectSuspendedAll_l(suspend); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1139 | } |
| 1140 | |
| 1141 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1142 | { |
| 1143 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1144 | if (index < 0) { |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1149 | mSuspendedSessions.valueAt(index); |
| 1150 | |
| 1151 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1152 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1153 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1154 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1155 | chain->setEffectSuspendedAll_l(true); |
| 1156 | } else { |
| 1157 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1158 | desc->mType.timeLow); |
| 1159 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1166 | bool suspend, |
| 1167 | int sessionId) |
| 1168 | { |
| 1169 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1170 | |
| 1171 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1172 | |
| 1173 | if (suspend) { |
| 1174 | if (index >= 0) { |
| 1175 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1176 | } else { |
| 1177 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1178 | } |
| 1179 | } else { |
| 1180 | if (index < 0) { |
| 1181 | return; |
| 1182 | } |
| 1183 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | int key = EffectChain::kKeyForSuspendAll; |
| 1188 | if (type != NULL) { |
| 1189 | key = type->timeLow; |
| 1190 | } |
| 1191 | index = sessionEffects.indexOfKey(key); |
| 1192 | |
| 1193 | sp<SuspendedSessionDesc> desc; |
| 1194 | if (suspend) { |
| 1195 | if (index >= 0) { |
| 1196 | desc = sessionEffects.valueAt(index); |
| 1197 | } else { |
| 1198 | desc = new SuspendedSessionDesc(); |
| 1199 | if (type != NULL) { |
| 1200 | desc->mType = *type; |
| 1201 | } |
| 1202 | sessionEffects.add(key, desc); |
| 1203 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1204 | } |
| 1205 | desc->mRefCount++; |
| 1206 | } else { |
| 1207 | if (index < 0) { |
| 1208 | return; |
| 1209 | } |
| 1210 | desc = sessionEffects.valueAt(index); |
| 1211 | if (--desc->mRefCount == 0) { |
| 1212 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1213 | sessionEffects.removeItemsAt(index); |
| 1214 | if (sessionEffects.isEmpty()) { |
| 1215 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1216 | sessionId); |
| 1217 | mSuspendedSessions.removeItem(sessionId); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | if (!sessionEffects.isEmpty()) { |
| 1222 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1227 | bool enabled, |
| 1228 | int sessionId) |
| 1229 | { |
| 1230 | Mutex::Autolock _l(mLock); |
| 1231 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1232 | } |
| 1233 | |
| 1234 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1235 | bool enabled, |
| 1236 | int sessionId) |
| 1237 | { |
| 1238 | if (mType != RECORD) { |
| 1239 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1240 | // another session. This gives the priority to well behaved effect control panels |
| 1241 | // and applications not using global effects. |
| 1242 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1243 | // global effects |
| 1244 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1245 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1250 | if (chain != 0) { |
| 1251 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1256 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1257 | const sp<AudioFlinger::Client>& client, |
| 1258 | const sp<IEffectClient>& effectClient, |
| 1259 | int32_t priority, |
| 1260 | int sessionId, |
| 1261 | effect_descriptor_t *desc, |
| 1262 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1263 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1264 | { |
| 1265 | sp<EffectModule> effect; |
| 1266 | sp<EffectHandle> handle; |
| 1267 | status_t lStatus; |
| 1268 | sp<EffectChain> chain; |
| 1269 | bool chainCreated = false; |
| 1270 | bool effectCreated = false; |
| 1271 | bool effectRegistered = false; |
| 1272 | |
| 1273 | lStatus = initCheck(); |
| 1274 | if (lStatus != NO_ERROR) { |
| 1275 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1276 | goto Exit; |
| 1277 | } |
| 1278 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1279 | // Reject any effect on Direct output threads for now, since the format of |
| 1280 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1281 | if (mType == DIRECT) { |
| 1282 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1283 | desc->name, mThreadName); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1284 | lStatus = BAD_VALUE; |
| 1285 | goto Exit; |
| 1286 | } |
| 1287 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1288 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1289 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1290 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 1291 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 1292 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1293 | lStatus = BAD_VALUE; |
| 1294 | goto Exit; |
| 1295 | } |
| 1296 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1297 | // Allow global effects only on offloaded and mixer threads |
| 1298 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1299 | switch (mType) { |
| 1300 | case MIXER: |
| 1301 | case OFFLOAD: |
| 1302 | break; |
| 1303 | case DIRECT: |
| 1304 | case DUPLICATING: |
| 1305 | case RECORD: |
| 1306 | default: |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1307 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", |
| 1308 | desc->name, mThreadName); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1309 | lStatus = BAD_VALUE; |
| 1310 | goto Exit; |
| 1311 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1312 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1313 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1314 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 1315 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 1316 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 1317 | desc->name, desc->flags, mType); |
| 1318 | lStatus = BAD_VALUE; |
| 1319 | goto Exit; |
| 1320 | } |
| 1321 | |
| 1322 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1323 | |
| 1324 | { // scope for mLock |
| 1325 | Mutex::Autolock _l(mLock); |
| 1326 | |
| 1327 | // check for existing effect chain with the requested audio session |
| 1328 | chain = getEffectChain_l(sessionId); |
| 1329 | if (chain == 0) { |
| 1330 | // create a new chain for this session |
| 1331 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1332 | chain = new EffectChain(this, sessionId); |
| 1333 | addEffectChain_l(chain); |
| 1334 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1335 | chainCreated = true; |
| 1336 | } else { |
| 1337 | effect = chain->getEffectFromDesc_l(desc); |
| 1338 | } |
| 1339 | |
| 1340 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1341 | |
| 1342 | if (effect == 0) { |
| 1343 | int id = mAudioFlinger->nextUniqueId(); |
| 1344 | // Check CPU and memory usage |
| 1345 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1346 | if (lStatus != NO_ERROR) { |
| 1347 | goto Exit; |
| 1348 | } |
| 1349 | effectRegistered = true; |
| 1350 | // create a new effect module if none present in the chain |
| 1351 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1352 | lStatus = effect->status(); |
| 1353 | if (lStatus != NO_ERROR) { |
| 1354 | goto Exit; |
| 1355 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1356 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1357 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1358 | lStatus = chain->addEffect_l(effect); |
| 1359 | if (lStatus != NO_ERROR) { |
| 1360 | goto Exit; |
| 1361 | } |
| 1362 | effectCreated = true; |
| 1363 | |
| 1364 | effect->setDevice(mOutDevice); |
| 1365 | effect->setDevice(mInDevice); |
| 1366 | effect->setMode(mAudioFlinger->getMode()); |
| 1367 | effect->setAudioSource(mAudioSource); |
| 1368 | } |
| 1369 | // create effect handle and connect it to effect module |
| 1370 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1371 | lStatus = handle->initCheck(); |
| 1372 | if (lStatus == OK) { |
| 1373 | lStatus = effect->addHandle(handle.get()); |
| 1374 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1375 | if (enabled != NULL) { |
| 1376 | *enabled = (int)effect->isEnabled(); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | Exit: |
| 1381 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1382 | Mutex::Autolock _l(mLock); |
| 1383 | if (effectCreated) { |
| 1384 | chain->removeEffect_l(effect); |
| 1385 | } |
| 1386 | if (effectRegistered) { |
| 1387 | AudioSystem::unregisterEffect(effect->id()); |
| 1388 | } |
| 1389 | if (chainCreated) { |
| 1390 | removeEffectChain_l(chain); |
| 1391 | } |
| 1392 | handle.clear(); |
| 1393 | } |
| 1394 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1395 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1396 | return handle; |
| 1397 | } |
| 1398 | |
| 1399 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) |
| 1400 | { |
| 1401 | Mutex::Autolock _l(mLock); |
| 1402 | return getEffect_l(sessionId, effectId); |
| 1403 | } |
| 1404 | |
| 1405 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 1406 | { |
| 1407 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1408 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1409 | } |
| 1410 | |
| 1411 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1412 | // PlaybackThread::mLock held |
| 1413 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1414 | { |
| 1415 | // check for existing effect chain with the requested audio session |
| 1416 | int sessionId = effect->sessionId(); |
| 1417 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1418 | bool chainCreated = false; |
| 1419 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1420 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1421 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1422 | this, effect->desc().name, effect->desc().flags); |
| 1423 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1424 | if (chain == 0) { |
| 1425 | // create a new chain for this session |
| 1426 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1427 | chain = new EffectChain(this, sessionId); |
| 1428 | addEffectChain_l(chain); |
| 1429 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1430 | chainCreated = true; |
| 1431 | } |
| 1432 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1433 | |
| 1434 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1435 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1436 | this, effect->desc().name, chain.get()); |
| 1437 | return BAD_VALUE; |
| 1438 | } |
| 1439 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1440 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1441 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | status_t status = chain->addEffect_l(effect); |
| 1443 | if (status != NO_ERROR) { |
| 1444 | if (chainCreated) { |
| 1445 | removeEffectChain_l(chain); |
| 1446 | } |
| 1447 | return status; |
| 1448 | } |
| 1449 | |
| 1450 | effect->setDevice(mOutDevice); |
| 1451 | effect->setDevice(mInDevice); |
| 1452 | effect->setMode(mAudioFlinger->getMode()); |
| 1453 | effect->setAudioSource(mAudioSource); |
| 1454 | return NO_ERROR; |
| 1455 | } |
| 1456 | |
| 1457 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1458 | |
| 1459 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1460 | effect_descriptor_t desc = effect->desc(); |
| 1461 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1462 | detachAuxEffect_l(effect->id()); |
| 1463 | } |
| 1464 | |
| 1465 | sp<EffectChain> chain = effect->chain().promote(); |
| 1466 | if (chain != 0) { |
| 1467 | // remove effect chain if removing last effect |
| 1468 | if (chain->removeEffect_l(effect) == 0) { |
| 1469 | removeEffectChain_l(chain); |
| 1470 | } |
| 1471 | } else { |
| 1472 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1477 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1478 | { |
| 1479 | effectChains = mEffectChains; |
| 1480 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1481 | mEffectChains[i]->lock(); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1486 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1487 | { |
| 1488 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1489 | effectChains[i]->unlock(); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 1494 | { |
| 1495 | Mutex::Autolock _l(mLock); |
| 1496 | return getEffectChain_l(sessionId); |
| 1497 | } |
| 1498 | |
| 1499 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const |
| 1500 | { |
| 1501 | size_t size = mEffectChains.size(); |
| 1502 | for (size_t i = 0; i < size; i++) { |
| 1503 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1504 | return mEffectChains[i]; |
| 1505 | } |
| 1506 | } |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1511 | { |
| 1512 | Mutex::Autolock _l(mLock); |
| 1513 | size_t size = mEffectChains.size(); |
| 1514 | for (size_t i = 0; i < size; i++) { |
| 1515 | mEffectChains[i]->setMode_l(mode); |
| 1516 | } |
| 1517 | } |
| 1518 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1519 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1520 | { |
| 1521 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1522 | config->ext.mix.handle = mId; |
| 1523 | config->sample_rate = mSampleRate; |
| 1524 | config->format = mFormat; |
| 1525 | config->channel_mask = mChannelMask; |
| 1526 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1527 | AUDIO_PORT_CONFIG_FORMAT; |
| 1528 | } |
| 1529 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1530 | void AudioFlinger::ThreadBase::systemReady() |
| 1531 | { |
| 1532 | Mutex::Autolock _l(mLock); |
| 1533 | if (mSystemReady) { |
| 1534 | return; |
| 1535 | } |
| 1536 | mSystemReady = true; |
| 1537 | |
| 1538 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1539 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1540 | } |
| 1541 | mPendingConfigEvents.clear(); |
| 1542 | } |
| 1543 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1544 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1545 | // ---------------------------------------------------------------------------- |
| 1546 | // Playback |
| 1547 | // ---------------------------------------------------------------------------- |
| 1548 | |
| 1549 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1550 | AudioStreamOut* output, |
| 1551 | audio_io_handle_t id, |
| 1552 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1553 | type_t type, |
| 1554 | bool systemReady) |
| 1555 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1556 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1557 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1558 | mMixerBuffer(NULL), |
| 1559 | mMixerBufferSize(0), |
| 1560 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1561 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1562 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1563 | mEffectBuffer(NULL), |
| 1564 | mEffectBufferSize(0), |
| 1565 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1566 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1567 | mSuspended(0), mBytesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1568 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1569 | // mStreamTypes[] initialized in constructor body |
| 1570 | mOutput(output), |
| 1571 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1572 | mMixerStatus(MIXER_IDLE), |
| 1573 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1574 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1575 | mBytesRemaining(0), |
| 1576 | mCurrentWriteLength(0), |
| 1577 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1578 | mWriteAckSequence(0), |
| 1579 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1580 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1581 | mScreenState(AudioFlinger::mScreenState), |
| 1582 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1583 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1584 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1585 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1586 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1587 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1588 | |
| 1589 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1590 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1591 | // parameter. |
| 1592 | // |
| 1593 | // If the HAL we are using has support for master volume or master mute, |
| 1594 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1595 | // and the mute set to false). |
| 1596 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1597 | mMasterMute = audioFlinger->masterMute_l(); |
| 1598 | if (mOutput && mOutput->audioHwDev) { |
| 1599 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1600 | mMasterVolume = 1.0; |
| 1601 | } |
| 1602 | |
| 1603 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1604 | mMasterMute = false; |
| 1605 | } |
| 1606 | } |
| 1607 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1608 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1609 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1610 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1611 | 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] | 1612 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1613 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1614 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1615 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1619 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1620 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1621 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1622 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1623 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1627 | { |
| 1628 | dumpInternals(fd, args); |
| 1629 | dumpTracks(fd, args); |
| 1630 | dumpEffectChains(fd, args); |
| 1631 | } |
| 1632 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1633 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1634 | { |
| 1635 | const size_t SIZE = 256; |
| 1636 | char buffer[SIZE]; |
| 1637 | String8 result; |
| 1638 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1639 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1640 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1641 | const stream_type_t *st = &mStreamTypes[i]; |
| 1642 | if (i > 0) { |
| 1643 | result.appendFormat(", "); |
| 1644 | } |
| 1645 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1646 | if (st->mute) { |
| 1647 | result.append("M"); |
| 1648 | } |
| 1649 | } |
| 1650 | result.append("\n"); |
| 1651 | write(fd, result.string(), result.length()); |
| 1652 | result.clear(); |
| 1653 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1654 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1655 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1656 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1657 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1658 | |
| 1659 | size_t numtracks = mTracks.size(); |
| 1660 | size_t numactive = mActiveTracks.size(); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1661 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1662 | size_t numactiveseen = 0; |
| 1663 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1664 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1665 | Track::appendDumpHeader(result); |
| 1666 | for (size_t i = 0; i < numtracks; ++i) { |
| 1667 | sp<Track> track = mTracks[i]; |
| 1668 | if (track != 0) { |
| 1669 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1670 | if (active) { |
| 1671 | numactiveseen++; |
| 1672 | } |
| 1673 | track->dump(buffer, SIZE, active); |
| 1674 | result.append(buffer); |
| 1675 | } |
| 1676 | } |
| 1677 | } else { |
| 1678 | result.append("\n"); |
| 1679 | } |
| 1680 | if (numactiveseen != numactive) { |
| 1681 | // some tracks in the active list were not in the tracks list |
| 1682 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1683 | " not in the track list\n"); |
| 1684 | result.append(buffer); |
| 1685 | Track::appendDumpHeader(result); |
| 1686 | for (size_t i = 0; i < numactive; ++i) { |
| 1687 | sp<Track> track = mActiveTracks[i].promote(); |
| 1688 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1689 | track->dump(buffer, SIZE, true); |
| 1690 | result.append(buffer); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1699 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1700 | dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 1701 | |
| 1702 | dumpBase(fd, args); |
| 1703 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1704 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
| 1705 | dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1706 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1707 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1708 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1709 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1710 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1711 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1712 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1713 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1714 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1715 | AudioStreamOut *output = mOutput; |
| 1716 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1717 | String8 flagsAsString = outputFlagsToString(flags); |
| 1718 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1722 | |
| 1723 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1724 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1725 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | // ThreadBase virtuals |
| 1729 | void AudioFlinger::PlaybackThread::preExit() |
| 1730 | { |
| 1731 | ALOGV(" preExit()"); |
| 1732 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1733 | // converted to use audio HAL extensions required to support tunneling |
| 1734 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1735 | } |
| 1736 | |
| 1737 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1738 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1739 | const sp<AudioFlinger::Client>& client, |
| 1740 | audio_stream_type_t streamType, |
| 1741 | uint32_t sampleRate, |
| 1742 | audio_format_t format, |
| 1743 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1744 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1745 | const sp<IMemory>& sharedBuffer, |
| 1746 | int sessionId, |
| 1747 | IAudioFlinger::track_flags_t *flags, |
| 1748 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1749 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1750 | status_t *status) |
| 1751 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1752 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1753 | sp<Track> track; |
| 1754 | status_t lStatus; |
| 1755 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1756 | // client expresses a preference for FAST, but we get the final say |
| 1757 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1758 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1759 | // either of these use cases: |
| 1760 | ( |
| 1761 | // use case 1: shared buffer with any frame count |
| 1762 | ( |
| 1763 | (sharedBuffer != 0) |
| 1764 | ) || |
Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1765 | // use case 2: frame count is default or at least as large as HAL |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1766 | ( |
Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1767 | // we formerly checked for a callback handler (non-0 tid), |
| 1768 | // but that is no longer required for TRANSFER_OBTAIN mode |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1769 | ((frameCount == 0) || |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1770 | (frameCount >= mFrameCount)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1771 | ) |
| 1772 | ) && |
| 1773 | // PCM data |
| 1774 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1775 | // TODO: extract as a data library function that checks that a computationally |
| 1776 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1777 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1778 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1779 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1780 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1781 | // hardware sample rate |
| 1782 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1783 | // normal mixer has an associated fast mixer |
| 1784 | hasFastMixer() && |
| 1785 | // there are sufficient fast track slots available |
| 1786 | (mFastTrackAvailMask != 0) |
| 1787 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1788 | // FIXME add a permission test also? |
| 1789 | ) { |
| 1790 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1791 | if (frameCount == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1792 | // read the fast track multiplier property the first time it is needed |
| 1793 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1794 | if (ok != 0) { |
| 1795 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1796 | } |
| 1797 | frameCount = mFrameCount * sFastTrackMultiplier; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1798 | } |
| 1799 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1800 | frameCount, mFrameCount); |
| 1801 | } else { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1802 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%d " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1803 | "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
| 1804 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1805 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1806 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1807 | audio_is_linear_pcm(format), |
| 1808 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1809 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | // For normal PCM streaming tracks, update minimum frame count. |
| 1813 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1814 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1815 | // This is probably too conservative, but legacy application code may depend on it. |
| 1816 | // If you change this calculation, also review the start threshold which is related. |
| 1817 | if (!(*flags & IAudioFlinger::TRACK_FAST) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1818 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1819 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1820 | // TODO: Move to a common library |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1821 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1822 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1823 | if (minBufCount < 2) { |
| 1824 | minBufCount = 2; |
| 1825 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1826 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1827 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1828 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1829 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1830 | sampleRate, mNormalFrameCount, |
| 1831 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1832 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1833 | frameCount = minFrameCount; |
| 1834 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1835 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1836 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1838 | switch (mType) { |
| 1839 | |
| 1840 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1841 | if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1842 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1843 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1844 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1845 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1846 | lStatus = BAD_VALUE; |
| 1847 | goto Exit; |
| 1848 | } |
| 1849 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1850 | break; |
| 1851 | |
| 1852 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1853 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1854 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1855 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1856 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1857 | lStatus = BAD_VALUE; |
| 1858 | goto Exit; |
| 1859 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1860 | break; |
| 1861 | |
| 1862 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1863 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1864 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1865 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1866 | format, mOutput, mFormat); |
| 1867 | lStatus = BAD_VALUE; |
| 1868 | goto Exit; |
| 1869 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1870 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1871 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1872 | lStatus = BAD_VALUE; |
| 1873 | goto Exit; |
| 1874 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1875 | break; |
| 1876 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | lStatus = initCheck(); |
| 1880 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1881 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1882 | goto Exit; |
| 1883 | } |
| 1884 | |
| 1885 | { // scope for mLock |
| 1886 | Mutex::Autolock _l(mLock); |
| 1887 | |
| 1888 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1889 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1890 | // manager |
| 1891 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1892 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1893 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1894 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1895 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1896 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1897 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1898 | strategy, actual); |
| 1899 | lStatus = BAD_VALUE; |
| 1900 | goto Exit; |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1905 | track = new Track(this, client, streamType, sampleRate, format, |
| 1906 | channelMask, frameCount, NULL, sharedBuffer, |
| 1907 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1908 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1909 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1910 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1911 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1912 | // 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] | 1913 | goto Exit; |
| 1914 | } |
| 1915 | mTracks.add(track); |
| 1916 | |
| 1917 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1918 | if (chain != 0) { |
| 1919 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1920 | track->setMainBuffer(chain->inBuffer()); |
| 1921 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1922 | chain->incTrackCnt(); |
| 1923 | } |
| 1924 | |
| 1925 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1926 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1927 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1928 | // so ask activity manager to do this on our behalf |
| 1929 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | lStatus = NO_ERROR; |
| 1934 | |
| 1935 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1936 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1937 | return track; |
| 1938 | } |
| 1939 | |
| 1940 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1941 | { |
| 1942 | return latency; |
| 1943 | } |
| 1944 | |
| 1945 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1946 | { |
| 1947 | Mutex::Autolock _l(mLock); |
| 1948 | return latency_l(); |
| 1949 | } |
| 1950 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1951 | { |
| 1952 | if (initCheck() == NO_ERROR) { |
| 1953 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1954 | } else { |
| 1955 | return 0; |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1960 | { |
| 1961 | Mutex::Autolock _l(mLock); |
| 1962 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1963 | if (mOutput && mOutput->audioHwDev && |
| 1964 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1965 | mMasterVolume = 1.0; |
| 1966 | } else { |
| 1967 | mMasterVolume = value; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1972 | { |
| 1973 | Mutex::Autolock _l(mLock); |
| 1974 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1975 | if (mOutput && mOutput->audioHwDev && |
| 1976 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1977 | mMasterMute = false; |
| 1978 | } else { |
| 1979 | mMasterMute = muted; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 1984 | { |
| 1985 | Mutex::Autolock _l(mLock); |
| 1986 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1987 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 1991 | { |
| 1992 | Mutex::Autolock _l(mLock); |
| 1993 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1994 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 1998 | { |
| 1999 | Mutex::Autolock _l(mLock); |
| 2000 | return mStreamTypes[stream].volume; |
| 2001 | } |
| 2002 | |
| 2003 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2004 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2005 | { |
| 2006 | status_t status = ALREADY_EXISTS; |
| 2007 | |
| 2008 | // set retry count for buffer fill |
| 2009 | track->mRetryCount = kMaxTrackStartupRetries; |
| 2010 | if (mActiveTracks.indexOf(track) < 0) { |
| 2011 | // the track is newly added, make sure it fills up all its |
| 2012 | // buffers before playing. This is to ensure the client will |
| 2013 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2014 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2015 | TrackBase::track_state state = track->mState; |
| 2016 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2017 | status = AudioSystem::startOutput(mId, track->streamType(), |
| 2018 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2019 | mLock.lock(); |
| 2020 | // abort track was stopped/paused while we released the lock |
| 2021 | if (state != track->mState) { |
| 2022 | if (status == NO_ERROR) { |
| 2023 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2024 | AudioSystem::stopOutput(mId, track->streamType(), |
| 2025 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2026 | mLock.lock(); |
| 2027 | } |
| 2028 | return INVALID_OPERATION; |
| 2029 | } |
| 2030 | // abort if start is rejected by audio policy manager |
| 2031 | if (status != NO_ERROR) { |
| 2032 | return PERMISSION_DENIED; |
| 2033 | } |
| 2034 | #ifdef ADD_BATTERY_DATA |
| 2035 | // to track the speaker usage |
| 2036 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2037 | #endif |
| 2038 | } |
| 2039 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2040 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2041 | track->mResetDone = false; |
| 2042 | track->mPresentationCompleteFrames = 0; |
| 2043 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2044 | mWakeLockUids.add(track->uid()); |
| 2045 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2046 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2047 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2048 | if (chain != 0) { |
| 2049 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2050 | track->sessionId()); |
| 2051 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | status = NO_ERROR; |
| 2055 | } |
| 2056 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2057 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2058 | return status; |
| 2059 | } |
| 2060 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2061 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2062 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2063 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2064 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2065 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2066 | track->mState = TrackBase::STOPPED; |
| 2067 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2068 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2069 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2070 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2071 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2072 | |
| 2073 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2077 | { |
| 2078 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 2079 | mTracks.remove(track); |
| 2080 | deleteTrackName_l(track->name()); |
| 2081 | // redundant as track is about to be destroyed, for dumpsys only |
| 2082 | track->mName = -1; |
| 2083 | if (track->isFastTrack()) { |
| 2084 | int index = track->mFastIndex; |
| 2085 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 2086 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2087 | mFastTrackAvailMask |= 1 << index; |
| 2088 | // redundant as track is about to be destroyed, for dumpsys only |
| 2089 | track->mFastIndex = -1; |
| 2090 | } |
| 2091 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2092 | if (chain != 0) { |
| 2093 | chain->decTrackCnt(); |
| 2094 | } |
| 2095 | } |
| 2096 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2097 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2098 | { |
| 2099 | // Thread could be blocked waiting for async |
| 2100 | // so signal it to handle state changes immediately |
| 2101 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2102 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2103 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2104 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2107 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2108 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2109 | Mutex::Autolock _l(mLock); |
| 2110 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2111 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2112 | } |
| 2113 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2114 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 2115 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2116 | free(s); |
| 2117 | return out_s8; |
| 2118 | } |
| 2119 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2120 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2121 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2122 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2123 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2124 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2125 | |
| 2126 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2127 | case AUDIO_OUTPUT_OPENED: |
| 2128 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2129 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2130 | desc->mChannelMask = mChannelMask; |
| 2131 | desc->mSamplingRate = mSampleRate; |
| 2132 | desc->mFormat = mFormat; |
| 2133 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2134 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2135 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2136 | break; |
| 2137 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2138 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2139 | default: |
| 2140 | break; |
| 2141 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2142 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2145 | void AudioFlinger::PlaybackThread::writeCallback() |
| 2146 | { |
| 2147 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2148 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | void AudioFlinger::PlaybackThread::drainCallback() |
| 2152 | { |
| 2153 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2154 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2155 | } |
| 2156 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2157 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2158 | { |
| 2159 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2160 | // reject out of sequence requests |
| 2161 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2162 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2163 | mWaitWorkCV.signal(); |
| 2164 | } |
| 2165 | } |
| 2166 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2167 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2168 | { |
| 2169 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2170 | // reject out of sequence requests |
| 2171 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2172 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2173 | mWaitWorkCV.signal(); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | // static |
| 2178 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2179 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2180 | void *cookie) |
| 2181 | { |
| 2182 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 2183 | ALOGV("asyncCallback() event %d", event); |
| 2184 | switch (event) { |
| 2185 | case STREAM_CBK_EVENT_WRITE_READY: |
| 2186 | me->writeCallback(); |
| 2187 | break; |
| 2188 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 2189 | me->drainCallback(); |
| 2190 | break; |
| 2191 | default: |
| 2192 | ALOGW("asyncCallback() unknown event %d", event); |
| 2193 | break; |
| 2194 | } |
| 2195 | return 0; |
| 2196 | } |
| 2197 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2198 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2199 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2200 | // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2201 | mSampleRate = mOutput->getSampleRate(); |
| 2202 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2203 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2204 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2205 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2206 | if ((mType == MIXER || mType == DUPLICATING) |
| 2207 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2208 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2209 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2210 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2211 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2212 | |
| 2213 | // Get actual HAL format. |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2214 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2215 | // Get format from the shim, which will be different than the HAL format |
| 2216 | // if playing compressed audio over HDMI passthrough. |
| 2217 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2218 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2219 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2220 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2221 | if ((mType == MIXER || mType == DUPLICATING) |
| 2222 | && !isValidPcmSinkFormat(mFormat)) { |
| 2223 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2224 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2225 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2226 | mFrameSize = mOutput->getFrameSize(); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2227 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2228 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2229 | if (mFrameCount & 15) { |
| 2230 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 2231 | mFrameCount); |
| 2232 | } |
| 2233 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2234 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2235 | (mOutput->stream->set_callback != NULL)) { |
| 2236 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2237 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2238 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2239 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2243 | mHwSupportsPause = false; |
| 2244 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2245 | if (mOutput->stream->pause != NULL) { |
| 2246 | if (mOutput->stream->resume != NULL) { |
| 2247 | mHwSupportsPause = true; |
| 2248 | } else { |
| 2249 | ALOGW("direct output implements pause but not resume"); |
| 2250 | } |
| 2251 | } else if (mOutput->stream->resume != NULL) { |
| 2252 | ALOGW("direct output implements resume but not pause"); |
| 2253 | } |
| 2254 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2255 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2256 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2257 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2258 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2259 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2260 | // For best precision, we use float instead of the associated output |
| 2261 | // device format (typically PCM 16 bit). |
| 2262 | |
| 2263 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2264 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2265 | mBufferSize = mFrameSize * mFrameCount; |
| 2266 | |
| 2267 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2268 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2269 | // (if a valid mask) to avoid premature downmix. |
| 2270 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2271 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2272 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2273 | } |
| 2274 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2275 | // 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] | 2276 | double multiplier = 1.0; |
| 2277 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2278 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2279 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2280 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2281 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2282 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2283 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2284 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2285 | maxNormalFrameCount = minNormalFrameCount; |
| 2286 | } |
| 2287 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2288 | if (multiplier <= 1.0) { |
| 2289 | multiplier = 1.0; |
| 2290 | } else if (multiplier <= 2.0) { |
| 2291 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2292 | multiplier = 2.0; |
| 2293 | } else { |
| 2294 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2295 | } |
| 2296 | } else { |
| 2297 | // 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] | 2298 | // 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] | 2299 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 2300 | // constraint) |
| 2301 | // FIXME this rounding up should not be done if no HAL SRC |
| 2302 | uint32_t truncMult = (uint32_t) multiplier; |
| 2303 | if ((truncMult & 1)) { |
| 2304 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 2305 | ++truncMult; |
| 2306 | } |
| 2307 | } |
| 2308 | multiplier = (double) truncMult; |
| 2309 | } |
| 2310 | } |
| 2311 | mNormalFrameCount = multiplier * mFrameCount; |
| 2312 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2313 | if (mType == MIXER || mType == DUPLICATING) { |
| 2314 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2315 | } |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2316 | 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] | 2317 | mNormalFrameCount); |
| 2318 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2319 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2320 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2321 | mThreadThrottleTimeMs = 0; |
| 2322 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2323 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2324 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2325 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2326 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2327 | free(mSinkBuffer); |
| 2328 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2329 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2330 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2331 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2332 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2333 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2334 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2335 | // drives the output. |
| 2336 | free(mMixerBuffer); |
| 2337 | mMixerBuffer = NULL; |
| 2338 | if (mMixerBufferEnabled) { |
| 2339 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2340 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2341 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2342 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2343 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2344 | free(mEffectBuffer); |
| 2345 | mEffectBuffer = NULL; |
| 2346 | if (mEffectBufferEnabled) { |
| 2347 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2348 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2349 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2350 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2351 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2352 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2353 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2354 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2355 | // 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] | 2356 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2357 | // matter. |
| 2358 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2359 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2360 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2361 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2366 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2367 | { |
| 2368 | if (halFrames == NULL || dspFrames == NULL) { |
| 2369 | return BAD_VALUE; |
| 2370 | } |
| 2371 | Mutex::Autolock _l(mLock); |
| 2372 | if (initCheck() != NO_ERROR) { |
| 2373 | return INVALID_OPERATION; |
| 2374 | } |
| 2375 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 2376 | *halFrames = framesWritten; |
| 2377 | |
| 2378 | if (isSuspended()) { |
| 2379 | // return an estimation of rendered frames when the output is suspended |
| 2380 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
| 2381 | *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; |
| 2382 | return NO_ERROR; |
| 2383 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2384 | status_t status; |
| 2385 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2386 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2387 | *dspFrames = (size_t)frames; |
| 2388 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const |
| 2393 | { |
| 2394 | Mutex::Autolock _l(mLock); |
| 2395 | uint32_t result = 0; |
| 2396 | if (getEffectChain_l(sessionId) != 0) { |
| 2397 | result = EFFECT_SESSION; |
| 2398 | } |
| 2399 | |
| 2400 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2401 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2402 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2403 | result |= TRACK_SESSION; |
| 2404 | break; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | return result; |
| 2409 | } |
| 2410 | |
| 2411 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2412 | { |
| 2413 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2414 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2415 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2416 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2417 | } |
| 2418 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2419 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2420 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2421 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2422 | } |
| 2423 | } |
| 2424 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2425 | } |
| 2426 | |
| 2427 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2428 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2429 | { |
| 2430 | Mutex::Autolock _l(mLock); |
| 2431 | return mOutput; |
| 2432 | } |
| 2433 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2434 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2435 | { |
| 2436 | Mutex::Autolock _l(mLock); |
| 2437 | AudioStreamOut *output = mOutput; |
| 2438 | mOutput = NULL; |
| 2439 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2440 | // must push a NULL and wait for ack |
| 2441 | mOutputSink.clear(); |
| 2442 | mPipeSink.clear(); |
| 2443 | mNormalSink.clear(); |
| 2444 | return output; |
| 2445 | } |
| 2446 | |
| 2447 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2448 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2449 | { |
| 2450 | if (mOutput == NULL) { |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | return &mOutput->stream->common; |
| 2454 | } |
| 2455 | |
| 2456 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2457 | { |
| 2458 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2459 | } |
| 2460 | |
| 2461 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2462 | { |
| 2463 | if (!isValidSyncEvent(event)) { |
| 2464 | return BAD_VALUE; |
| 2465 | } |
| 2466 | |
| 2467 | Mutex::Autolock _l(mLock); |
| 2468 | |
| 2469 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2470 | sp<Track> track = mTracks[i]; |
| 2471 | if (event->triggerSession() == track->sessionId()) { |
| 2472 | (void) track->setSyncEvent(event); |
| 2473 | return NO_ERROR; |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | return NAME_NOT_FOUND; |
| 2478 | } |
| 2479 | |
| 2480 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2481 | { |
| 2482 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2483 | } |
| 2484 | |
| 2485 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2486 | const Vector< sp<Track> >& tracksToRemove) |
| 2487 | { |
| 2488 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2489 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2490 | for (size_t i = 0 ; i < count ; i++) { |
| 2491 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2492 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2493 | AudioSystem::stopOutput(mId, track->streamType(), |
| 2494 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2495 | #ifdef ADD_BATTERY_DATA |
| 2496 | // to track the speaker usage |
| 2497 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2498 | #endif |
| 2499 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2500 | AudioSystem::releaseOutput(mId, track->streamType(), |
| 2501 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2502 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2509 | { |
| 2510 | if (!mMasterMute) { |
| 2511 | char value[PROPERTY_VALUE_MAX]; |
| 2512 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2513 | char *endptr; |
| 2514 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2515 | if (*endptr == '\0' && ul != 0) { |
| 2516 | ALOGD("Silence is golden"); |
| 2517 | // The setprop command will not allow a property to be changed after |
| 2518 | // the first time it is set, so we don't have to worry about un-muting. |
| 2519 | setMasterMute_l(true); |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2526 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2527 | { |
| 2528 | // FIXME rewrite to reduce number of system calls |
| 2529 | mLastWriteTime = systemTime(); |
| 2530 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2531 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2532 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2533 | |
| 2534 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2535 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2536 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2537 | const size_t count = mBytesRemaining / mFrameSize; |
| 2538 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2539 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2540 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2541 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2542 | if (screenState != mScreenState) { |
| 2543 | mScreenState = screenState; |
| 2544 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2545 | if (pipe != NULL) { |
| 2546 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2547 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2548 | } |
| 2549 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2550 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2551 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2552 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2553 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2554 | } else { |
| 2555 | bytesWritten = framesWritten; |
| 2556 | } |
| 2557 | // otherwise use the HAL / AudioStreamOut directly |
| 2558 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2559 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2560 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2561 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2562 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2563 | mWriteAckSequence += 2; |
| 2564 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2565 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2566 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2567 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2568 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2569 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2570 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2571 | if (mUseAsyncWrite && |
| 2572 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2573 | // 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] | 2574 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2575 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2576 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2577 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2578 | } |
| 2579 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2580 | mNumWrites++; |
| 2581 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2582 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2583 | return bytesWritten; |
| 2584 | } |
| 2585 | |
| 2586 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2587 | { |
| 2588 | if (mOutput->stream->drain) { |
| 2589 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2590 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2591 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2592 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2593 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2594 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2595 | } |
| 2596 | mOutput->stream->drain(mOutput->stream, |
| 2597 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2598 | : AUDIO_DRAIN_ALL); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2603 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2604 | { |
| 2605 | Mutex::Autolock _l(mLock); |
| 2606 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2607 | sp<Track> track = mTracks[i]; |
| 2608 | track->invalidate(); |
| 2609 | } |
| 2610 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2615 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2616 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2617 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2618 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2619 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2620 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2621 | |
| 2622 | The parameters that affect these derived values are: |
| 2623 | - frame count |
| 2624 | - frame size |
| 2625 | - sample rate |
| 2626 | - device type: A2DP or not |
| 2627 | - device latency |
| 2628 | - format: PCM or not |
| 2629 | - active sleep time |
| 2630 | - idle sleep time |
| 2631 | */ |
| 2632 | |
| 2633 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2634 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2635 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2636 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2637 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2638 | |
| 2639 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2640 | // truncating audio when going to standby. |
| 2641 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2642 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2643 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2644 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2645 | } |
| 2646 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2650 | { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2651 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2652 | this, streamType, mTracks.size()); |
| 2653 | Mutex::Autolock _l(mLock); |
| 2654 | |
| 2655 | size_t size = mTracks.size(); |
| 2656 | for (size_t i = 0; i < size; i++) { |
| 2657 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2658 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2659 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2665 | { |
| 2666 | int session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2667 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2668 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2669 | bool ownsBuffer = false; |
| 2670 | |
| 2671 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 2672 | if (session > 0) { |
| 2673 | // 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] | 2674 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2675 | if (mType != DIRECT) { |
| 2676 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2677 | buffer = new int16_t[numSamples]; |
| 2678 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2679 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2680 | ownsBuffer = true; |
| 2681 | } |
| 2682 | |
| 2683 | // Attach all tracks with same session ID to this chain. |
| 2684 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2685 | sp<Track> track = mTracks[i]; |
| 2686 | if (session == track->sessionId()) { |
| 2687 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2688 | buffer); |
| 2689 | track->setMainBuffer(buffer); |
| 2690 | chain->incTrackCnt(); |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | // indicate all active tracks in the chain |
| 2695 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2696 | sp<Track> track = mActiveTracks[i].promote(); |
| 2697 | if (track == 0) { |
| 2698 | continue; |
| 2699 | } |
| 2700 | if (session == track->sessionId()) { |
| 2701 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2702 | chain->incActiveTrackCnt(); |
| 2703 | } |
| 2704 | } |
| 2705 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2706 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2707 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2708 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2709 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2710 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
| 2711 | // chains list in order to be processed last as it contains output stage effects |
| 2712 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2713 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
| 2714 | // after track specific effects and before output stage |
| 2715 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 2716 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
| 2717 | // Effect chain for other sessions are inserted at beginning of effect |
| 2718 | // chains list to be processed before output mix effects. Relative order between other |
| 2719 | // sessions is not important |
| 2720 | size_t size = mEffectChains.size(); |
| 2721 | size_t i = 0; |
| 2722 | for (i = 0; i < size; i++) { |
| 2723 | if (mEffectChains[i]->sessionId() < session) { |
| 2724 | break; |
| 2725 | } |
| 2726 | } |
| 2727 | mEffectChains.insertAt(chain, i); |
| 2728 | checkSuspendOnAddEffectChain_l(chain); |
| 2729 | |
| 2730 | return NO_ERROR; |
| 2731 | } |
| 2732 | |
| 2733 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2734 | { |
| 2735 | int session = chain->sessionId(); |
| 2736 | |
| 2737 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2738 | |
| 2739 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2740 | if (chain == mEffectChains[i]) { |
| 2741 | mEffectChains.removeAt(i); |
| 2742 | // detach all active tracks from the chain |
| 2743 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2744 | sp<Track> track = mActiveTracks[i].promote(); |
| 2745 | if (track == 0) { |
| 2746 | continue; |
| 2747 | } |
| 2748 | if (session == track->sessionId()) { |
| 2749 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2750 | chain.get(), session); |
| 2751 | chain->decActiveTrackCnt(); |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | // detach all tracks with same session ID from this chain |
| 2756 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2757 | sp<Track> track = mTracks[i]; |
| 2758 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2759 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2760 | chain->decTrackCnt(); |
| 2761 | } |
| 2762 | } |
| 2763 | break; |
| 2764 | } |
| 2765 | } |
| 2766 | return mEffectChains.size(); |
| 2767 | } |
| 2768 | |
| 2769 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2770 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2771 | { |
| 2772 | Mutex::Autolock _l(mLock); |
| 2773 | return attachAuxEffect_l(track, EffectId); |
| 2774 | } |
| 2775 | |
| 2776 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2777 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2778 | { |
| 2779 | status_t status = NO_ERROR; |
| 2780 | |
| 2781 | if (EffectId == 0) { |
| 2782 | track->setAuxBuffer(0, NULL); |
| 2783 | } else { |
| 2784 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2785 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2786 | if (effect != 0) { |
| 2787 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2788 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2789 | } else { |
| 2790 | status = INVALID_OPERATION; |
| 2791 | } |
| 2792 | } else { |
| 2793 | status = BAD_VALUE; |
| 2794 | } |
| 2795 | } |
| 2796 | return status; |
| 2797 | } |
| 2798 | |
| 2799 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2800 | { |
| 2801 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2802 | sp<Track> track = mTracks[i]; |
| 2803 | if (track->auxEffectId() == effectId) { |
| 2804 | attachAuxEffect_l(track, 0); |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2810 | { |
| 2811 | Vector< sp<Track> > tracksToRemove; |
| 2812 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2813 | mStandbyTimeNs = systemTime(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2814 | |
| 2815 | // MIXER |
| 2816 | nsecs_t lastWarning = 0; |
| 2817 | |
| 2818 | // DUPLICATING |
| 2819 | // FIXME could this be made local to while loop? |
| 2820 | writeFrames = 0; |
| 2821 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2822 | int lastGeneration = 0; |
| 2823 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2824 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2825 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2826 | |
| 2827 | if (mType == MIXER) { |
| 2828 | sleepTimeShift = 0; |
| 2829 | } |
| 2830 | |
| 2831 | CpuStats cpuStats; |
| 2832 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2833 | |
| 2834 | acquireWakeLock(); |
| 2835 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2836 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2837 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2838 | // and then that string will be logged at the next convenient opportunity. |
| 2839 | const char *logString = NULL; |
| 2840 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2841 | checkSilentMode_l(); |
| 2842 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2843 | while (!exitPending()) |
| 2844 | { |
| 2845 | cpuStats.sample(myName); |
| 2846 | |
| 2847 | Vector< sp<EffectChain> > effectChains; |
| 2848 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2849 | { // scope for mLock |
| 2850 | |
| 2851 | Mutex::Autolock _l(mLock); |
| 2852 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2853 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2854 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2855 | if (logString != NULL) { |
| 2856 | mNBLogWriter->logTimestamp(); |
| 2857 | mNBLogWriter->log(logString); |
| 2858 | logString = NULL; |
| 2859 | } |
| 2860 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2861 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2862 | // and associate with the sink frames written out. We need |
| 2863 | // this to convert the sink timestamp to the track timestamp. |
| 2864 | if (mNormalSink != 0) { |
| 2865 | bool updateTracks = true; |
| 2866 | bool cacheTimestamp = false; |
| 2867 | AudioTimestamp timeStamp; |
| 2868 | // FIXME: Use a 64 bit mNormalSink->framesWritten() counter. |
| 2869 | // At this time, we must always use cached timestamps even when |
| 2870 | // going through mPipeSink (which is non-blocking). The reason is that |
| 2871 | // the track may be removed from the active list for many hours and |
| 2872 | // the mNormalSink->framesWritten() will wrap making the linear |
| 2873 | // mapping fail. |
| 2874 | // |
| 2875 | // (Also mAudioTrackServerProxy->framesReleased() needs to be |
| 2876 | // updated to 64 bits for 64 bit frame position.) |
| 2877 | // |
| 2878 | if (true /* see comment above, should be: mNormalSink == mOutputSink */) { |
| 2879 | // If we use a hardware device, we must cache the sink timestamp now. |
| 2880 | // hardware devices can block timestamp access during data writes. |
| 2881 | if (mNormalSink->getTimestamp(timeStamp) == NO_ERROR) { |
| 2882 | cacheTimestamp = true; |
| 2883 | } else { |
| 2884 | updateTracks = false; |
| 2885 | } |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2886 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2887 | if (updateTracks) { |
| 2888 | // sinkFramesWritten for non-offloaded tracks are contiguous |
| 2889 | // even after standby() is called. This is useful for the track frame |
| 2890 | // to sink frame mapping. |
| 2891 | const uint32_t sinkFramesWritten = mNormalSink->framesWritten(); |
| 2892 | const size_t size = mActiveTracks.size(); |
| 2893 | for (size_t i = 0; i < size; ++i) { |
| 2894 | sp<Track> t = mActiveTracks[i].promote(); |
| 2895 | if (t != 0 && !t->isFastTrack()) { |
| 2896 | t->updateTrackFrameInfo( |
| 2897 | t->mAudioTrackServerProxy->framesReleased(), |
| 2898 | sinkFramesWritten, |
| 2899 | cacheTimestamp ? &timeStamp : NULL); |
| 2900 | } |
| 2901 | } |
| 2902 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2905 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2906 | if (mSignalPending) { |
| 2907 | // A signal was raised while we were unlocked |
| 2908 | mSignalPending = false; |
| 2909 | } else if (waitingAsyncCallback_l()) { |
| 2910 | if (exitPending()) { |
| 2911 | break; |
| 2912 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2913 | bool released = false; |
| 2914 | // The following works around a bug in the offload driver. Ideally we would release |
| 2915 | // the wake lock every time, but that causes the last offload buffer(s) to be |
| 2916 | // dropped while the device is on battery, so we need to hold a wake lock during |
| 2917 | // the drain phase. |
| 2918 | if (mBytesRemaining && !(mDrainSequence & 1)) { |
| 2919 | releaseWakeLock_l(); |
| 2920 | released = true; |
| 2921 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2922 | mWakeLockUids.clear(); |
| 2923 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2924 | ALOGV("wait async completion"); |
| 2925 | mWaitWorkCV.wait(mLock); |
| 2926 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2927 | if (released) { |
| 2928 | acquireWakeLock_l(); |
| 2929 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2930 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2931 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2932 | |
| 2933 | continue; |
| 2934 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2935 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2936 | isSuspended()) { |
| 2937 | // put audio hardware into standby after short delay |
| 2938 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2939 | |
| 2940 | threadLoop_standby(); |
| 2941 | |
| 2942 | mStandby = true; |
| 2943 | } |
| 2944 | |
| 2945 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2946 | // we're about to wait, flush the binder command buffer |
| 2947 | IPCThreadState::self()->flushCommands(); |
| 2948 | |
| 2949 | clearOutputTracks(); |
| 2950 | |
| 2951 | if (exitPending()) { |
| 2952 | break; |
| 2953 | } |
| 2954 | |
| 2955 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2956 | mWakeLockUids.clear(); |
| 2957 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2958 | // wait until we have something to do... |
| 2959 | ALOGV("%s going to sleep", myName.string()); |
| 2960 | mWaitWorkCV.wait(mLock); |
| 2961 | ALOGV("%s waking up", myName.string()); |
| 2962 | acquireWakeLock_l(); |
| 2963 | |
| 2964 | mMixerStatus = MIXER_IDLE; |
| 2965 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2966 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2967 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2968 | checkSilentMode_l(); |
| 2969 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2970 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2971 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2972 | if (mType == MIXER) { |
| 2973 | sleepTimeShift = 0; |
| 2974 | } |
| 2975 | |
| 2976 | continue; |
| 2977 | } |
| 2978 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2979 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2980 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2981 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2982 | // compare with previously applied list |
| 2983 | if (lastGeneration != mActiveTracksGeneration) { |
| 2984 | // update wakelock |
| 2985 | updateWakeLockUids_l(mWakeLockUids); |
| 2986 | lastGeneration = mActiveTracksGeneration; |
| 2987 | } |
| 2988 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2989 | // prevent any changes in effect chain list and in each effect chain |
| 2990 | // during mixing and effect process as the audio buffers could be deleted |
| 2991 | // or modified if an effect is created or deleted |
| 2992 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2993 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2994 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2995 | if (mBytesRemaining == 0) { |
| 2996 | mCurrentWriteLength = 0; |
| 2997 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 2998 | // threadLoop_mix() sets mCurrentWriteLength |
| 2999 | threadLoop_mix(); |
| 3000 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3001 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3002 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3003 | // must be written to HAL |
| 3004 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3005 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3006 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3007 | } |
| 3008 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3009 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3010 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3011 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3012 | // or mSinkBuffer (if there are no effects). |
| 3013 | // |
| 3014 | // This is done pre-effects computation; if effects change to |
| 3015 | // support higher precision, this needs to move. |
| 3016 | // |
| 3017 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3018 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3019 | if (mMixerBufferValid) { |
| 3020 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3021 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3022 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3023 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3024 | // and is handled here if we're going directly to the sink. |
| 3025 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3026 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3027 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3030 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3031 | mNormalFrameCount * mChannelCount); |
| 3032 | } |
| 3033 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3034 | mBytesRemaining = mCurrentWriteLength; |
| 3035 | if (isSuspended()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3036 | mSleepTimeUs = suspendSleepTimeUs(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3037 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3038 | mBytesWritten += mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3039 | mBytesRemaining = 0; |
| 3040 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3041 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3042 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3043 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3044 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3045 | effectChains[i]->process_l(); |
| 3046 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3047 | } |
| 3048 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3049 | // Process effect chains for offloaded thread even if no audio |
| 3050 | // was read from audio track: process only updates effect state |
| 3051 | // and thus does have to be synchronized with audio writes but may have |
| 3052 | // to be called while waiting for async write callback |
| 3053 | if (mType == OFFLOAD) { |
| 3054 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3055 | effectChains[i]->process_l(); |
| 3056 | } |
| 3057 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3058 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3059 | // Only if the Effects buffer is enabled and there is data in the |
| 3060 | // Effects buffer (buffer valid), we need to |
| 3061 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3062 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3063 | if (mEffectBufferValid) { |
| 3064 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3065 | |
| 3066 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3067 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3068 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3069 | } |
| 3070 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3071 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3072 | mNormalFrameCount * mChannelCount); |
| 3073 | } |
| 3074 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3075 | // enable changes in effect chain |
| 3076 | unlockEffectChains(effectChains); |
| 3077 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3078 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3079 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3080 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3081 | ssize_t ret = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3082 | if (mBytesRemaining) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3083 | ret = threadLoop_write(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3084 | if (ret < 0) { |
| 3085 | mBytesRemaining = 0; |
| 3086 | } else { |
| 3087 | mBytesWritten += ret; |
| 3088 | mBytesRemaining -= ret; |
| 3089 | } |
| 3090 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3091 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3092 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3093 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3094 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3095 | // write blocked detection |
| 3096 | nsecs_t now = systemTime(); |
| 3097 | nsecs_t delta = now - mLastWriteTime; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3098 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3099 | mNumDelayedWrites++; |
| 3100 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 3101 | ATRACE_NAME("underrun"); |
| 3102 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 3103 | ns2ms(delta), mNumDelayedWrites, this); |
| 3104 | lastWarning = now; |
| 3105 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3106 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3107 | |
| 3108 | if (mThreadThrottle |
| 3109 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3110 | && ret > 0) { // we wrote something |
| 3111 | // Limit MixerThread data processing to no more than twice the |
| 3112 | // expected processing rate. |
| 3113 | // |
| 3114 | // This helps prevent underruns with NuPlayer and other applications |
| 3115 | // which may set up buffers that are close to the minimum size, or use |
| 3116 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3117 | // |
| 3118 | // The throttle smooths out sudden large data drains from the device, |
| 3119 | // e.g. when it comes out of standby, which often causes problems with |
| 3120 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3121 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3122 | // the app won't fill fast enough to handle the sudden draw). |
| 3123 | |
| 3124 | const int32_t deltaMs = delta / 1000000; |
| 3125 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 3126 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3127 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3128 | // notify of throttle start on verbose log |
| 3129 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3130 | "mixer(%p) throttle begin:" |
| 3131 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3132 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3133 | mThreadThrottleTimeMs += throttleMs; |
| 3134 | } else { |
| 3135 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3136 | if (diff > 0) { |
| 3137 | // notify of throttle end on debug log |
| 3138 | ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff); |
| 3139 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3140 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3141 | } |
| 3142 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3143 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3144 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3145 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3146 | ATRACE_BEGIN("sleep"); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3147 | usleep(mSleepTimeUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3148 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3149 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | // Finally let go of removed track(s), without the lock held |
| 3153 | // since we can't guarantee the destructors won't acquire that |
| 3154 | // same lock. This will also mutate and push a new fast mixer state. |
| 3155 | threadLoop_removeTracks(tracksToRemove); |
| 3156 | tracksToRemove.clear(); |
| 3157 | |
| 3158 | // FIXME I don't understand the need for this here; |
| 3159 | // it was in the original code but maybe the |
| 3160 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3161 | clearOutputTracks(); |
| 3162 | |
| 3163 | // Effect chains will be actually deleted here if they were removed from |
| 3164 | // mEffectChains list during mixing or effects processing |
| 3165 | effectChains.clear(); |
| 3166 | |
| 3167 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3168 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3169 | } |
| 3170 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3171 | threadLoop_exit(); |
| 3172 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3173 | if (!mStandby) { |
| 3174 | threadLoop_standby(); |
| 3175 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3179 | mWakeLockUids.clear(); |
| 3180 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3181 | |
| 3182 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3183 | return false; |
| 3184 | } |
| 3185 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3186 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3187 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3188 | { |
| 3189 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3190 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3191 | for (size_t i=0 ; i<count ; i++) { |
| 3192 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3193 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3194 | mWakeLockUids.remove(track->uid()); |
| 3195 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3196 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3197 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3198 | if (chain != 0) { |
| 3199 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3200 | track->sessionId()); |
| 3201 | chain->decActiveTrackCnt(); |
| 3202 | } |
| 3203 | if (track->isTerminated()) { |
| 3204 | removeTrack_l(track); |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3210 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3211 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3212 | { |
| 3213 | if (mNormalSink != 0) { |
| 3214 | return mNormalSink->getTimestamp(timestamp); |
| 3215 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3216 | if ((mType == OFFLOAD || mType == DIRECT) |
| 3217 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3218 | uint64_t position64; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3219 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3220 | if (ret == 0) { |
| 3221 | timestamp.mPosition = (uint32_t)position64; |
| 3222 | return NO_ERROR; |
| 3223 | } |
| 3224 | } |
| 3225 | return INVALID_OPERATION; |
| 3226 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3227 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3228 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3229 | audio_patch_handle_t *handle) |
| 3230 | { |
| 3231 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3232 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
| 3233 | if (mFastMixer != 0) { |
| 3234 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3235 | FastMixerState *state = sq->begin(); |
| 3236 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3237 | previousCommand = state->mCommand; |
| 3238 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3239 | sq->end(); |
| 3240 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3241 | } else { |
| 3242 | sq->end(false /*didModify*/); |
| 3243 | } |
| 3244 | } |
| 3245 | status_t status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3246 | |
| 3247 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3248 | ALOG_ASSERT(mFastMixer != 0); |
| 3249 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3250 | FastMixerState *state = sq->begin(); |
| 3251 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3252 | state->mCommand = previousCommand; |
| 3253 | sq->end(); |
| 3254 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3255 | } |
| 3256 | |
| 3257 | return status; |
| 3258 | } |
| 3259 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3260 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3261 | audio_patch_handle_t *handle) |
| 3262 | { |
| 3263 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3264 | |
| 3265 | // store new device and send to effects |
| 3266 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3267 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3268 | type |= patch->sinks[i].ext.device.type; |
| 3269 | } |
| 3270 | |
| 3271 | #ifdef ADD_BATTERY_DATA |
| 3272 | // when changing the audio output device, call addBatteryData to notify |
| 3273 | // the change |
| 3274 | if (mOutDevice != type) { |
| 3275 | uint32_t params = 0; |
| 3276 | // check whether speaker is on |
| 3277 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3278 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3281 | audio_devices_t deviceWithoutSpeaker |
| 3282 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3283 | // check if any other device (except speaker) is on |
| 3284 | if (type & deviceWithoutSpeaker) { |
| 3285 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3286 | } |
| 3287 | |
| 3288 | if (params != 0) { |
| 3289 | addBatteryData(params); |
| 3290 | } |
| 3291 | } |
| 3292 | #endif |
| 3293 | |
| 3294 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3295 | mEffectChains[i]->setDevice_l(type); |
| 3296 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3297 | |
| 3298 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3299 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3300 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3301 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3302 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3303 | |
| 3304 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3305 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3306 | status = hwDevice->create_audio_patch(hwDevice, |
| 3307 | patch->num_sources, |
| 3308 | patch->sources, |
| 3309 | patch->num_sinks, |
| 3310 | patch->sinks, |
| 3311 | handle); |
| 3312 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3313 | char *address; |
| 3314 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3315 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3316 | address = audio_device_address_to_parameter( |
| 3317 | patch->sinks[0].ext.device.type, |
| 3318 | patch->sinks[0].ext.device.address); |
| 3319 | } else { |
| 3320 | address = (char *)calloc(1, 1); |
| 3321 | } |
| 3322 | AudioParameter param = AudioParameter(String8(address)); |
| 3323 | free(address); |
| 3324 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
| 3325 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3326 | param.toString().string()); |
| 3327 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3328 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3329 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3330 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3331 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3332 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3333 | return status; |
| 3334 | } |
| 3335 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3336 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3337 | { |
| 3338 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3339 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
| 3340 | if (mFastMixer != 0) { |
| 3341 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3342 | FastMixerState *state = sq->begin(); |
| 3343 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3344 | previousCommand = state->mCommand; |
| 3345 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3346 | sq->end(); |
| 3347 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3348 | } else { |
| 3349 | sq->end(false /*didModify*/); |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | status_t status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3354 | |
| 3355 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3356 | ALOG_ASSERT(mFastMixer != 0); |
| 3357 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3358 | FastMixerState *state = sq->begin(); |
| 3359 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3360 | state->mCommand = previousCommand; |
| 3361 | sq->end(); |
| 3362 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3363 | } |
| 3364 | |
| 3365 | return status; |
| 3366 | } |
| 3367 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3368 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3369 | { |
| 3370 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3371 | |
| 3372 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3373 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3374 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 3375 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3376 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 3377 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3378 | AudioParameter param; |
| 3379 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 3380 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3381 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3382 | } |
| 3383 | return status; |
| 3384 | } |
| 3385 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3386 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3387 | { |
| 3388 | Mutex::Autolock _l(mLock); |
| 3389 | mTracks.add(track); |
| 3390 | } |
| 3391 | |
| 3392 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3393 | { |
| 3394 | Mutex::Autolock _l(mLock); |
| 3395 | destroyTrack_l(track); |
| 3396 | } |
| 3397 | |
| 3398 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3399 | { |
| 3400 | ThreadBase::getAudioPortConfig(config); |
| 3401 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3402 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3403 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3404 | } |
| 3405 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3406 | // ---------------------------------------------------------------------------- |
| 3407 | |
| 3408 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3409 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3410 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3411 | // mAudioMixer below |
| 3412 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3413 | mFastMixerFutex(0), |
| 3414 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3415 | // mOutputSink below |
| 3416 | // mPipeSink below |
| 3417 | // mNormalSink below |
| 3418 | { |
| 3419 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 3420 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3421 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 3422 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3423 | mNormalFrameCount); |
| 3424 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3425 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3426 | if (type == DUPLICATING) { |
| 3427 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3428 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3429 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3430 | return; |
| 3431 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3432 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3433 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3434 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3435 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3436 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 3437 | ALOG_ASSERT(index == 0); |
| 3438 | |
| 3439 | // initialize fast mixer depending on configuration |
| 3440 | bool initFastMixer; |
| 3441 | switch (kUseFastMixer) { |
| 3442 | case FastMixer_Never: |
| 3443 | initFastMixer = false; |
| 3444 | break; |
| 3445 | case FastMixer_Always: |
| 3446 | initFastMixer = true; |
| 3447 | break; |
| 3448 | case FastMixer_Static: |
| 3449 | case FastMixer_Dynamic: |
| 3450 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3451 | break; |
| 3452 | } |
| 3453 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3454 | audio_format_t fastMixerFormat; |
| 3455 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3456 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3457 | } else { |
| 3458 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3459 | } |
| 3460 | if (mFormat != fastMixerFormat) { |
| 3461 | // change our Sink format to accept our intermediate precision |
| 3462 | mFormat = fastMixerFormat; |
| 3463 | free(mSinkBuffer); |
| 3464 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3465 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3466 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3467 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3468 | |
| 3469 | // create a MonoPipe to connect our submix to FastMixer |
| 3470 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3471 | NBAIO_Format origformat = format; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3472 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3473 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3474 | format.mFormat = fastMixerFormat; |
| 3475 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3476 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3477 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3478 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3479 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3480 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3481 | const NBAIO_Format offers[1] = {format}; |
| 3482 | size_t numCounterOffers = 0; |
| 3483 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 3484 | ALOG_ASSERT(index == 0); |
| 3485 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3486 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3487 | mPipeSink = monoPipe; |
| 3488 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3489 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3490 | if (mTeeSinkOutputEnabled) { |
| 3491 | // create a Pipe to archive a copy of FastMixer's output for dumpsys |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3492 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3493 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3494 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3495 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3496 | ALOG_ASSERT(index == 0); |
| 3497 | mTeeSink = teeSink; |
| 3498 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3499 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3500 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3501 | ALOG_ASSERT(index == 0); |
| 3502 | mTeeSource = teeSource; |
| 3503 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3504 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3505 | |
| 3506 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3507 | mFastMixer = new FastMixer(); |
| 3508 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3509 | #ifdef STATE_QUEUE_DUMP |
| 3510 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3511 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3512 | #endif |
| 3513 | FastMixerState *state = sq->begin(); |
| 3514 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3515 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3516 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3517 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3518 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3519 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3520 | fastTrack->mGeneration++; |
| 3521 | state->mFastTracksGen++; |
| 3522 | state->mTrackMask = 1; |
| 3523 | // fast mixer will use the HAL output sink |
| 3524 | state->mOutputSink = mOutputSink.get(); |
| 3525 | state->mOutputSinkGen++; |
| 3526 | state->mFrameCount = mFrameCount; |
| 3527 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3528 | // already done in constructor initialization list |
| 3529 | //mFastMixerFutex = 0; |
| 3530 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3531 | state->mColdGen++; |
| 3532 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3533 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3534 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3535 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3536 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3537 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3538 | sq->end(); |
| 3539 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3540 | |
| 3541 | // start the fast mixer |
| 3542 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3543 | pid_t tid = mFastMixer->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3544 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3545 | |
| 3546 | #ifdef AUDIO_WATCHDOG |
| 3547 | // create and start the watchdog |
| 3548 | mAudioWatchdog = new AudioWatchdog(); |
| 3549 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3550 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3551 | tid = mAudioWatchdog->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3552 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3553 | #endif |
| 3554 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | switch (kUseFastMixer) { |
| 3558 | case FastMixer_Never: |
| 3559 | case FastMixer_Dynamic: |
| 3560 | mNormalSink = mOutputSink; |
| 3561 | break; |
| 3562 | case FastMixer_Always: |
| 3563 | mNormalSink = mPipeSink; |
| 3564 | break; |
| 3565 | case FastMixer_Static: |
| 3566 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3567 | break; |
| 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | AudioFlinger::MixerThread::~MixerThread() |
| 3572 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3573 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3574 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3575 | FastMixerState *state = sq->begin(); |
| 3576 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3577 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3578 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3579 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3580 | } |
| 3581 | } |
| 3582 | state->mCommand = FastMixerState::EXIT; |
| 3583 | sq->end(); |
| 3584 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3585 | mFastMixer->join(); |
| 3586 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3587 | // We'll use that extract the final state which contains one remaining fast track |
| 3588 | // corresponding to our sub-mix. |
| 3589 | state = sq->begin(); |
| 3590 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3591 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3592 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3593 | delete fastTrack->mBufferProvider; |
| 3594 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3595 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3596 | #ifdef AUDIO_WATCHDOG |
| 3597 | if (mAudioWatchdog != 0) { |
| 3598 | mAudioWatchdog->requestExit(); |
| 3599 | mAudioWatchdog->requestExitAndWait(); |
| 3600 | mAudioWatchdog.clear(); |
| 3601 | } |
| 3602 | #endif |
| 3603 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3604 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3605 | delete mAudioMixer; |
| 3606 | } |
| 3607 | |
| 3608 | |
| 3609 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3610 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3611 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3613 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3614 | } |
| 3615 | return latency; |
| 3616 | } |
| 3617 | |
| 3618 | |
| 3619 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3620 | { |
| 3621 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3622 | } |
| 3623 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3624 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3625 | { |
| 3626 | // FIXME we should only do one push per cycle; confirm this is true |
| 3627 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3628 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3629 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3630 | FastMixerState *state = sq->begin(); |
| 3631 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3632 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3633 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3634 | |
| 3635 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3636 | ATRACE_BEGIN("write"); |
| 3637 | mOutput->write((char *)mSinkBuffer, 0); |
| 3638 | ATRACE_END(); |
| 3639 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3640 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3641 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3642 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3643 | } |
| 3644 | #ifdef AUDIO_WATCHDOG |
| 3645 | if (mAudioWatchdog != 0) { |
| 3646 | mAudioWatchdog->resume(); |
| 3647 | } |
| 3648 | #endif |
| 3649 | } |
| 3650 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3651 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3652 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3653 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3654 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3655 | sq->end(); |
| 3656 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3657 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3658 | mNormalSink = mPipeSink; |
| 3659 | } |
| 3660 | } else { |
| 3661 | sq->end(false /*didModify*/); |
| 3662 | } |
| 3663 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3664 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3668 | { |
| 3669 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3670 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3671 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3672 | FastMixerState *state = sq->begin(); |
| 3673 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3674 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3675 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3676 | state->mColdGen++; |
| 3677 | mFastMixerFutex = 0; |
| 3678 | sq->end(); |
| 3679 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3680 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3681 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3682 | mNormalSink = mOutputSink; |
| 3683 | } |
| 3684 | #ifdef AUDIO_WATCHDOG |
| 3685 | if (mAudioWatchdog != 0) { |
| 3686 | mAudioWatchdog->pause(); |
| 3687 | } |
| 3688 | #endif |
| 3689 | } else { |
| 3690 | sq->end(false /*didModify*/); |
| 3691 | } |
| 3692 | } |
| 3693 | PlaybackThread::threadLoop_standby(); |
| 3694 | } |
| 3695 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3696 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3697 | { |
| 3698 | return false; |
| 3699 | } |
| 3700 | |
| 3701 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3702 | { |
| 3703 | return !mStandby; |
| 3704 | } |
| 3705 | |
| 3706 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3707 | { |
| 3708 | Mutex::Autolock _l(mLock); |
| 3709 | return waitingAsyncCallback_l(); |
| 3710 | } |
| 3711 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3712 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3713 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3714 | { |
| 3715 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3716 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3717 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3718 | // discard any pending drain or write ack by incrementing sequence |
| 3719 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3720 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3721 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3722 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3723 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3724 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3725 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3728 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3729 | { |
| 3730 | ALOGV("signal playback thread"); |
| 3731 | broadcast_l(); |
| 3732 | } |
| 3733 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3734 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3735 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3736 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3737 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3738 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3739 | // increase sleep time progressively when application underrun condition clears. |
| 3740 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3741 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3742 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3743 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3744 | sleepTimeShift--; |
| 3745 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3746 | mSleepTimeUs = 0; |
| 3747 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3748 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3749 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3753 | { |
| 3754 | // If no tracks are ready, sleep once for the duration of an output |
| 3755 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3756 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3757 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3758 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3759 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3760 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3761 | } |
| 3762 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3763 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3764 | // duration we would end up writing less data than needed by the audio HAL if |
| 3765 | // the condition persists. |
| 3766 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3767 | sleepTimeShift++; |
| 3768 | } |
| 3769 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3770 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3771 | } |
| 3772 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3773 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3774 | // before effects processing or output. |
| 3775 | if (mMixerBufferValid) { |
| 3776 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3777 | } else { |
| 3778 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3779 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3780 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3781 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3782 | "anticipated start"); |
| 3783 | } |
| 3784 | // TODO add standby time extension fct of effect tail |
| 3785 | } |
| 3786 | |
| 3787 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3788 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3789 | Vector< sp<Track> > *tracksToRemove) |
| 3790 | { |
| 3791 | |
| 3792 | mixer_state mixerStatus = MIXER_IDLE; |
| 3793 | // find out which tracks need to be processed |
| 3794 | size_t count = mActiveTracks.size(); |
| 3795 | size_t mixedTracks = 0; |
| 3796 | size_t tracksWithEffect = 0; |
| 3797 | // counts only _active_ fast tracks |
| 3798 | size_t fastTracks = 0; |
| 3799 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3800 | |
| 3801 | float masterVolume = mMasterVolume; |
| 3802 | bool masterMute = mMasterMute; |
| 3803 | |
| 3804 | if (masterMute) { |
| 3805 | masterVolume = 0; |
| 3806 | } |
| 3807 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3808 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3809 | if (chain != 0) { |
| 3810 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3811 | chain->setVolume_l(&v, &v); |
| 3812 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3813 | chain.clear(); |
| 3814 | } |
| 3815 | |
| 3816 | // prepare a new state to push |
| 3817 | FastMixerStateQueue *sq = NULL; |
| 3818 | FastMixerState *state = NULL; |
| 3819 | bool didModify = false; |
| 3820 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3821 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3822 | sq = mFastMixer->sq(); |
| 3823 | state = sq->begin(); |
| 3824 | } |
| 3825 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3826 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3827 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3828 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3829 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3830 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3831 | if (t == 0) { |
| 3832 | continue; |
| 3833 | } |
| 3834 | |
| 3835 | // this const just means the local variable doesn't change |
| 3836 | Track* const track = t.get(); |
| 3837 | |
| 3838 | // process fast tracks |
| 3839 | if (track->isFastTrack()) { |
| 3840 | |
| 3841 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3842 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3843 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3844 | // The converse, of removing an (active) track and then creating a new track |
| 3845 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3846 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3847 | int j = track->mFastIndex; |
| 3848 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3849 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3850 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3851 | |
| 3852 | // Determine whether the track is currently in underrun condition, |
| 3853 | // and whether it had a recent underrun. |
| 3854 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3855 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3856 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3857 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3858 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3859 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3860 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3861 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3862 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3863 | track->mObservedUnderruns = underruns; |
| 3864 | // don't count underruns that occur while stopping or pausing |
| 3865 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3866 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3867 | recentUnderruns > 0) { |
| 3868 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3869 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 3870 | } else { |
| 3871 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | // This is similar to the state machine for normal tracks, |
| 3875 | // with a few modifications for fast tracks. |
| 3876 | bool isActive = true; |
| 3877 | switch (track->mState) { |
| 3878 | case TrackBase::STOPPING_1: |
| 3879 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3880 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3881 | track->mState = TrackBase::STOPPING_2; |
| 3882 | } |
| 3883 | break; |
| 3884 | case TrackBase::PAUSING: |
| 3885 | // ramp down is not yet implemented |
| 3886 | track->setPaused(); |
| 3887 | break; |
| 3888 | case TrackBase::RESUMING: |
| 3889 | // ramp up is not yet implemented |
| 3890 | track->mState = TrackBase::ACTIVE; |
| 3891 | break; |
| 3892 | case TrackBase::ACTIVE: |
| 3893 | if (recentFull > 0 || recentPartial > 0) { |
| 3894 | // track has provided at least some frames recently: reset retry count |
| 3895 | track->mRetryCount = kMaxTrackRetries; |
| 3896 | } |
| 3897 | if (recentUnderruns == 0) { |
| 3898 | // no recent underruns: stay active |
| 3899 | break; |
| 3900 | } |
| 3901 | // there has recently been an underrun of some kind |
| 3902 | if (track->sharedBuffer() == 0) { |
| 3903 | // were any of the recent underruns "empty" (no frames available)? |
| 3904 | if (recentEmpty == 0) { |
| 3905 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3906 | break; |
| 3907 | } |
| 3908 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3909 | if (--(track->mRetryCount) > 0) { |
| 3910 | break; |
| 3911 | } |
| 3912 | // indicate to client process that the track was disabled because of underrun; |
| 3913 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3914 | android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3915 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3916 | isActive = false; |
| 3917 | break; |
| 3918 | } |
| 3919 | // fall through |
| 3920 | case TrackBase::STOPPING_2: |
| 3921 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3922 | case TrackBase::STOPPED: |
| 3923 | case TrackBase::FLUSHED: // flush() while active |
| 3924 | // Check for presentation complete if track is inactive |
| 3925 | // We have consumed all the buffers of this track. |
| 3926 | // This would be incomplete if we auto-paused on underrun |
| 3927 | { |
| 3928 | size_t audioHALFrames = |
| 3929 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3930 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3931 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3932 | // track stays in active list until presentation is complete |
| 3933 | break; |
| 3934 | } |
| 3935 | } |
| 3936 | if (track->isStopping_2()) { |
| 3937 | track->mState = TrackBase::STOPPED; |
| 3938 | } |
| 3939 | if (track->isStopped()) { |
| 3940 | // Can't reset directly, as fast mixer is still polling this track |
| 3941 | // track->reset(); |
| 3942 | // So instead mark this track as needing to be reset after push with ack |
| 3943 | resetMask |= 1 << i; |
| 3944 | } |
| 3945 | isActive = false; |
| 3946 | break; |
| 3947 | case TrackBase::IDLE: |
| 3948 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3949 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | if (isActive) { |
| 3953 | // was it previously inactive? |
| 3954 | if (!(state->mTrackMask & (1 << j))) { |
| 3955 | ExtendedAudioBufferProvider *eabp = track; |
| 3956 | VolumeProvider *vp = track; |
| 3957 | fastTrack->mBufferProvider = eabp; |
| 3958 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3959 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3960 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3961 | fastTrack->mGeneration++; |
| 3962 | state->mTrackMask |= 1 << j; |
| 3963 | didModify = true; |
| 3964 | // no acknowledgement required for newly active tracks |
| 3965 | } |
| 3966 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3967 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3968 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3969 | ++fastTracks; |
| 3970 | } else { |
| 3971 | // was it previously active? |
| 3972 | if (state->mTrackMask & (1 << j)) { |
| 3973 | fastTrack->mBufferProvider = NULL; |
| 3974 | fastTrack->mGeneration++; |
| 3975 | state->mTrackMask &= ~(1 << j); |
| 3976 | didModify = true; |
| 3977 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3978 | // because we're about to decrement the last sp<> on those tracks. |
| 3979 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3980 | } else { |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 3981 | LOG_ALWAYS_FATAL("fast track %d should have been active; " |
| 3982 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 3983 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 3984 | track->sharedBuffer() != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3985 | } |
| 3986 | tracksToRemove->add(track); |
| 3987 | // Avoids a misleading display in dumpsys |
| 3988 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 3989 | } |
| 3990 | continue; |
| 3991 | } |
| 3992 | |
| 3993 | { // local variable scope to avoid goto warning |
| 3994 | |
| 3995 | audio_track_cblk_t* cblk = track->cblk(); |
| 3996 | |
| 3997 | // The first time a track is added we wait |
| 3998 | // for all its buffers to be filled before processing it |
| 3999 | int name = track->name(); |
| 4000 | // make sure that we have enough frames to mix one full buffer. |
| 4001 | // enforce this condition only once to enable draining the buffer in case the client |
| 4002 | // app does not call stop() and relies on underrun to stop: |
| 4003 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4004 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4005 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4006 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4007 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4008 | |
| 4009 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4010 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4011 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4012 | // add frames already consumed but not yet released by the resampler |
| 4013 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4014 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4015 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4016 | uint32_t minFrames = 1; |
| 4017 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4018 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4019 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4020 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4021 | |
| 4022 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4023 | if (ATRACE_ENABLED()) { |
| 4024 | // I wish we had formatted trace names |
| 4025 | char traceName[16]; |
| 4026 | strcpy(traceName, "nRdy"); |
| 4027 | int name = track->name(); |
| 4028 | if (AudioMixer::TRACK0 <= name && |
| 4029 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 4030 | name -= AudioMixer::TRACK0; |
| 4031 | traceName[4] = (name / 10) + '0'; |
| 4032 | traceName[5] = (name % 10) + '0'; |
| 4033 | } else { |
| 4034 | traceName[4] = '?'; |
| 4035 | traceName[5] = '?'; |
| 4036 | } |
| 4037 | traceName[6] = '\0'; |
| 4038 | ATRACE_INT(traceName, framesReady); |
| 4039 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4040 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4041 | !track->isPaused() && !track->isTerminated()) |
| 4042 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4043 | 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] | 4044 | |
| 4045 | mixedTracks++; |
| 4046 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4047 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4048 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4049 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4050 | if (track->mainBuffer() != mSinkBuffer && |
| 4051 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4052 | if (mEffectBufferEnabled) { |
| 4053 | mEffectBufferValid = true; // Later can set directly. |
| 4054 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4055 | chain = getEffectChain_l(track->sessionId()); |
| 4056 | // Delegate volume control to effect in track effect chain if needed |
| 4057 | if (chain != 0) { |
| 4058 | tracksWithEffect++; |
| 4059 | } else { |
| 4060 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4061 | "session %d", |
| 4062 | name, track->sessionId()); |
| 4063 | } |
| 4064 | } |
| 4065 | |
| 4066 | |
| 4067 | int param = AudioMixer::VOLUME; |
| 4068 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4069 | // no ramp for the first volume setting |
| 4070 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4071 | if (track->mState == TrackBase::RESUMING) { |
| 4072 | track->mState = TrackBase::ACTIVE; |
| 4073 | param = AudioMixer::RAMP_VOLUME; |
| 4074 | } |
| 4075 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4076 | // FIXME should not make a decision based on mServer |
| 4077 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4078 | // If the track is stopped before the first frame was mixed, |
| 4079 | // do not apply ramp |
| 4080 | param = AudioMixer::RAMP_VOLUME; |
| 4081 | } |
| 4082 | |
| 4083 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4084 | uint32_t vl, vr; // in U8.24 integer format |
| 4085 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4086 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4087 | vl = vr = 0; |
| 4088 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4089 | if (track->isPausing()) { |
| 4090 | track->setPaused(); |
| 4091 | } |
| 4092 | } else { |
| 4093 | |
| 4094 | // read original volumes with volume control |
| 4095 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4096 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4097 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4098 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4099 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4100 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4101 | // 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] | 4102 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4103 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4104 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4105 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4106 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4107 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4108 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4109 | } |
| 4110 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4111 | vlf *= v; |
| 4112 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4113 | // 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] | 4114 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4115 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4116 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4117 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4118 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4119 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4120 | // send level comes from shared memory and so may be corrupt |
| 4121 | if (sendLevel > MAX_GAIN_INT) { |
| 4122 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4123 | sendLevel = MAX_GAIN_INT; |
| 4124 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4125 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4126 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4127 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4128 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4129 | // Delegate volume control to effect in track effect chain if needed |
| 4130 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4131 | // Do not ramp volume if volume is controlled by effect |
| 4132 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4133 | // Update remaining floating point volume levels |
| 4134 | vlf = (float)vl / (1 << 24); |
| 4135 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4136 | track->mHasVolumeController = true; |
| 4137 | } else { |
| 4138 | // force no volume ramp when volume controller was just disabled or removed |
| 4139 | // from effect chain to avoid volume spike |
| 4140 | if (track->mHasVolumeController) { |
| 4141 | param = AudioMixer::VOLUME; |
| 4142 | } |
| 4143 | track->mHasVolumeController = false; |
| 4144 | } |
| 4145 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4146 | // XXX: these things DON'T need to be done each time |
| 4147 | mAudioMixer->setBufferProvider(name, track); |
| 4148 | mAudioMixer->enable(name); |
| 4149 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4150 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4151 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4152 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4153 | mAudioMixer->setParameter( |
| 4154 | name, |
| 4155 | AudioMixer::TRACK, |
| 4156 | AudioMixer::FORMAT, (void *)track->format()); |
| 4157 | mAudioMixer->setParameter( |
| 4158 | name, |
| 4159 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4160 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4161 | mAudioMixer->setParameter( |
| 4162 | name, |
| 4163 | AudioMixer::TRACK, |
| 4164 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4165 | // limit track sample rate to 2 x output sample rate, which changes at re-configuration |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 4166 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4167 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4168 | if (reqSampleRate == 0) { |
| 4169 | reqSampleRate = mSampleRate; |
| 4170 | } else if (reqSampleRate > maxSampleRate) { |
| 4171 | reqSampleRate = maxSampleRate; |
| 4172 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4173 | mAudioMixer->setParameter( |
| 4174 | name, |
| 4175 | AudioMixer::RESAMPLE, |
| 4176 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4177 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4178 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4179 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4180 | mAudioMixer->setParameter( |
| 4181 | name, |
| 4182 | AudioMixer::TIMESTRETCH, |
| 4183 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4184 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4185 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4186 | /* |
| 4187 | * Select the appropriate output buffer for the track. |
| 4188 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4189 | * Tracks with effects go into their own effects chain buffer |
| 4190 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4191 | * |
| 4192 | * Other tracks can use mMixerBuffer for higher precision |
| 4193 | * channel accumulation. If this buffer is enabled |
| 4194 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4195 | * into it. |
| 4196 | * |
| 4197 | */ |
| 4198 | if (mMixerBufferEnabled |
| 4199 | && (track->mainBuffer() == mSinkBuffer |
| 4200 | || track->mainBuffer() == mMixerBuffer)) { |
| 4201 | mAudioMixer->setParameter( |
| 4202 | name, |
| 4203 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4204 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4205 | mAudioMixer->setParameter( |
| 4206 | name, |
| 4207 | AudioMixer::TRACK, |
| 4208 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4209 | // TODO: override track->mainBuffer()? |
| 4210 | mMixerBufferValid = true; |
| 4211 | } else { |
| 4212 | mAudioMixer->setParameter( |
| 4213 | name, |
| 4214 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4215 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4216 | mAudioMixer->setParameter( |
| 4217 | name, |
| 4218 | AudioMixer::TRACK, |
| 4219 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4220 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4221 | mAudioMixer->setParameter( |
| 4222 | name, |
| 4223 | AudioMixer::TRACK, |
| 4224 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4225 | |
| 4226 | // reset retry count |
| 4227 | track->mRetryCount = kMaxTrackRetries; |
| 4228 | |
| 4229 | // If one track is ready, set the mixer ready if: |
| 4230 | // - the mixer was not ready during previous round OR |
| 4231 | // - no other track is not ready |
| 4232 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4233 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4234 | mixerStatus = MIXER_TRACKS_READY; |
| 4235 | } |
| 4236 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4237 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4238 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4239 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4240 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4241 | } else { |
| 4242 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4243 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4244 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4245 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4246 | // previous audio buffer again to effects |
| 4247 | chain = getEffectChain_l(track->sessionId()); |
| 4248 | if (chain != 0) { |
| 4249 | chain->clearInputBuffer(); |
| 4250 | } |
| 4251 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4252 | 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] | 4253 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4254 | track->isStopped() || track->isPaused()) { |
| 4255 | // We have consumed all the buffers of this track. |
| 4256 | // Remove it from the list of active tracks. |
| 4257 | // TODO: use actual buffer filling status instead of latency when available from |
| 4258 | // audio HAL |
| 4259 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4260 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 4261 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4262 | if (track->isStopped()) { |
| 4263 | track->reset(); |
| 4264 | } |
| 4265 | tracksToRemove->add(track); |
| 4266 | } |
| 4267 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4268 | // No buffers for this track. Give it a few chances to |
| 4269 | // fill a buffer, then remove it from active list. |
| 4270 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4271 | 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] | 4272 | tracksToRemove->add(track); |
| 4273 | // indicate to client process that the track was disabled because of underrun; |
| 4274 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 4275 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4276 | // If one track is not ready, mark the mixer also not ready if: |
| 4277 | // - the mixer was ready during previous round OR |
| 4278 | // - no other track is ready |
| 4279 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4280 | mixerStatus != MIXER_TRACKS_READY) { |
| 4281 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4282 | } |
| 4283 | } |
| 4284 | mAudioMixer->disable(name); |
| 4285 | } |
| 4286 | |
| 4287 | } // local variable scope to avoid goto warning |
| 4288 | track_is_ready: ; |
| 4289 | |
| 4290 | } |
| 4291 | |
| 4292 | // Push the new FastMixer state if necessary |
| 4293 | bool pauseAudioWatchdog = false; |
| 4294 | if (didModify) { |
| 4295 | state->mFastTracksGen++; |
| 4296 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4297 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4298 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4299 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4300 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4301 | state->mColdGen++; |
| 4302 | mFastMixerFutex = 0; |
| 4303 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4304 | mNormalSink = mOutputSink; |
| 4305 | } |
| 4306 | // If we go into cold idle, need to wait for acknowledgement |
| 4307 | // so that fast mixer stops doing I/O. |
| 4308 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4309 | pauseAudioWatchdog = true; |
| 4310 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4311 | } |
| 4312 | if (sq != NULL) { |
| 4313 | sq->end(didModify); |
| 4314 | sq->push(block); |
| 4315 | } |
| 4316 | #ifdef AUDIO_WATCHDOG |
| 4317 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4318 | mAudioWatchdog->pause(); |
| 4319 | } |
| 4320 | #endif |
| 4321 | |
| 4322 | // Now perform the deferred reset on fast tracks that have stopped |
| 4323 | while (resetMask != 0) { |
| 4324 | size_t i = __builtin_ctz(resetMask); |
| 4325 | ALOG_ASSERT(i < count); |
| 4326 | resetMask &= ~(1 << i); |
| 4327 | sp<Track> t = mActiveTracks[i].promote(); |
| 4328 | if (t == 0) { |
| 4329 | continue; |
| 4330 | } |
| 4331 | Track* track = t.get(); |
| 4332 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4333 | track->reset(); |
| 4334 | } |
| 4335 | |
| 4336 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4337 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4338 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4339 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4340 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4344 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4345 | // passing a non-clean buffer to the effect chain |
| 4346 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4347 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4348 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4349 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4350 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4351 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4352 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4353 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4354 | if (mMixerBufferValid) { |
| 4355 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4356 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4357 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4358 | // after mixing. |
| 4359 | // |
| 4360 | // To enforce this guarantee: |
| 4361 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4362 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4363 | // must imply MIXER_TRACKS_READY. |
| 4364 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4365 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4366 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4367 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | // if any fast tracks, then status is ready |
| 4371 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4372 | if (fastTracks > 0) { |
| 4373 | mixerStatus = MIXER_TRACKS_READY; |
| 4374 | } |
| 4375 | return mixerStatus; |
| 4376 | } |
| 4377 | |
| 4378 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4379 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
| 4380 | audio_format_t format, int sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4381 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4382 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4386 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4387 | { |
| 4388 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4389 | mAudioMixer->deleteTrackName(name); |
| 4390 | } |
| 4391 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4392 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4393 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4394 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4395 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4396 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4397 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4398 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4399 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4400 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4401 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 4402 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4403 | if (mFastMixer != 0) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4404 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4405 | FastMixerState *state = sq->begin(); |
| 4406 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 4407 | previousCommand = state->mCommand; |
| 4408 | state->mCommand = FastMixerState::HOT_IDLE; |
| 4409 | sq->end(); |
| 4410 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 4411 | } else { |
| 4412 | sq->end(false /*didModify*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4413 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4414 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4415 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4416 | AudioParameter param = AudioParameter(keyValuePair); |
| 4417 | int value; |
| 4418 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4419 | reconfig = true; |
| 4420 | } |
| 4421 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4422 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4423 | status = BAD_VALUE; |
| 4424 | } else { |
| 4425 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4426 | reconfig = true; |
| 4427 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4428 | } |
| 4429 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4430 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4431 | status = BAD_VALUE; |
| 4432 | } else { |
| 4433 | // no need to save value, since it's constant |
| 4434 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4435 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4436 | } |
| 4437 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4438 | // do not accept frame count changes if tracks are open as the track buffer |
| 4439 | // size depends on frame count and correct behavior would not be guaranteed |
| 4440 | // if frame count is changed after track creation |
| 4441 | if (!mTracks.isEmpty()) { |
| 4442 | status = INVALID_OPERATION; |
| 4443 | } else { |
| 4444 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4445 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4446 | } |
| 4447 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4448 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4449 | // when changing the audio output device, call addBatteryData to notify |
| 4450 | // the change |
| 4451 | if (mOutDevice != value) { |
| 4452 | uint32_t params = 0; |
| 4453 | // check whether speaker is on |
| 4454 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4455 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4456 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4457 | |
| 4458 | audio_devices_t deviceWithoutSpeaker |
| 4459 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4460 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4461 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4462 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4463 | } |
| 4464 | |
| 4465 | if (params != 0) { |
| 4466 | addBatteryData(params); |
| 4467 | } |
| 4468 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4469 | #endif |
| 4470 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4471 | // forward device change to effects that have requested to be |
| 4472 | // aware of attached audio device. |
| 4473 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4474 | a2dpDeviceChanged = |
| 4475 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4476 | mOutDevice = value; |
| 4477 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4478 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4479 | } |
| 4480 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4481 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4482 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4483 | if (status == NO_ERROR) { |
| 4484 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4485 | keyValuePair.string()); |
| 4486 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4487 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4488 | mStandby = true; |
| 4489 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4490 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4491 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4492 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4493 | if (status == NO_ERROR && reconfig) { |
| 4494 | readOutputParameters_l(); |
| 4495 | delete mAudioMixer; |
| 4496 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4497 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4498 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 4499 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4500 | if (name < 0) { |
| 4501 | break; |
| 4502 | } |
| 4503 | mTracks[i]->mName = name; |
| 4504 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4505 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4506 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | if (!(previousCommand & FastMixerState::IDLE)) { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4510 | ALOG_ASSERT(mFastMixer != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4511 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4512 | FastMixerState *state = sq->begin(); |
| 4513 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 4514 | state->mCommand = previousCommand; |
| 4515 | sq->end(); |
| 4516 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4517 | } |
| 4518 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4519 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | |
| 4523 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4524 | { |
| 4525 | const size_t SIZE = 256; |
| 4526 | char buffer[SIZE]; |
| 4527 | String8 result; |
| 4528 | |
| 4529 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4530 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4531 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4532 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4533 | |
| 4534 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 4535 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4536 | // This is a large object so we place it on the heap. |
| 4537 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4538 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4539 | copy->dump(fd); |
| 4540 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4541 | |
| 4542 | #ifdef STATE_QUEUE_DUMP |
| 4543 | // Similar for state queue |
| 4544 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4545 | observerCopy.dump(fd); |
| 4546 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4547 | mutatorCopy.dump(fd); |
| 4548 | #endif |
| 4549 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4550 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4551 | // Write the tee output to a .wav file |
| 4552 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4553 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4554 | |
| 4555 | #ifdef AUDIO_WATCHDOG |
| 4556 | if (mAudioWatchdog != 0) { |
| 4557 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4558 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4559 | wdCopy.dump(fd); |
| 4560 | } |
| 4561 | #endif |
| 4562 | } |
| 4563 | |
| 4564 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4565 | { |
| 4566 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4567 | } |
| 4568 | |
| 4569 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4570 | { |
| 4571 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4572 | } |
| 4573 | |
| 4574 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4575 | { |
| 4576 | PlaybackThread::cacheParameters_l(); |
| 4577 | |
| 4578 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4579 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4580 | // increase threshold again due to low power audio mode. The way this warning |
| 4581 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4582 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4583 | } |
| 4584 | |
| 4585 | // ---------------------------------------------------------------------------- |
| 4586 | |
| 4587 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4588 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 4589 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4590 | // mLeftVolFloat, mRightVolFloat |
| 4591 | { |
| 4592 | } |
| 4593 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4594 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4595 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4596 | ThreadBase::type_t type, bool systemReady) |
| 4597 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4598 | // mLeftVolFloat, mRightVolFloat |
| 4599 | { |
| 4600 | } |
| 4601 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4602 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4603 | { |
| 4604 | } |
| 4605 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4606 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4607 | { |
| 4608 | audio_track_cblk_t* cblk = track->cblk(); |
| 4609 | float left, right; |
| 4610 | |
| 4611 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4612 | left = right = 0; |
| 4613 | } else { |
| 4614 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4615 | float v = mMasterVolume * typeVolume; |
| 4616 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4617 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4618 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4619 | if (left > GAIN_FLOAT_UNITY) { |
| 4620 | left = GAIN_FLOAT_UNITY; |
| 4621 | } |
| 4622 | left *= v; |
| 4623 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4624 | if (right > GAIN_FLOAT_UNITY) { |
| 4625 | right = GAIN_FLOAT_UNITY; |
| 4626 | } |
| 4627 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | if (lastTrack) { |
| 4631 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4632 | mLeftVolFloat = left; |
| 4633 | mRightVolFloat = right; |
| 4634 | |
| 4635 | // Convert volumes from float to 8.24 |
| 4636 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4637 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4638 | |
| 4639 | // Delegate volume control to effect in track effect chain if needed |
| 4640 | // only one effect chain can be present on DirectOutputThread, so if |
| 4641 | // there is one, the track is connected to it |
| 4642 | if (!mEffectChains.isEmpty()) { |
| 4643 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4644 | left = (float)vl / (1 << 24); |
| 4645 | right = (float)vr / (1 << 24); |
| 4646 | } |
| 4647 | if (mOutput->stream->set_volume) { |
| 4648 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4649 | } |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4654 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4655 | { |
| 4656 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4657 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4658 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4659 | if (previousTrack != 0 && latestTrack != 0) { |
| 4660 | if (mType == DIRECT) { |
| 4661 | if (previousTrack.get() != latestTrack.get()) { |
| 4662 | mFlushPending = true; |
| 4663 | } |
| 4664 | } else /* mType == OFFLOAD */ { |
| 4665 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4666 | mFlushPending = true; |
| 4667 | } |
| 4668 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4669 | } |
| 4670 | PlaybackThread::onAddNewTrack_l(); |
| 4671 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4672 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4673 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4674 | Vector< sp<Track> > *tracksToRemove |
| 4675 | ) |
| 4676 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4677 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4678 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4679 | bool doHwPause = false; |
| 4680 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4681 | |
| 4682 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4683 | for (size_t i = 0; i < count; i++) { |
| 4684 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4685 | // The track died recently |
| 4686 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4687 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4688 | } |
| 4689 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4690 | if (t->isInvalid()) { |
| 4691 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4692 | tracksToRemove->add(t); |
| 4693 | continue; |
| 4694 | } |
| 4695 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4696 | Track* const track = t.get(); |
| 4697 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4698 | // Only consider last track started for volume and mixer state control. |
| 4699 | // In theory an older track could underrun and restart after the new one starts |
| 4700 | // but as we only care about the transition phase between two tracks on a |
| 4701 | // direct output, it is not a problem to ignore the underrun case. |
| 4702 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4703 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4704 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4705 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4706 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4707 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4708 | doHwPause = true; |
| 4709 | mHwPaused = true; |
| 4710 | } |
| 4711 | tracksToRemove->add(track); |
| 4712 | } else if (track->isFlushPending()) { |
| 4713 | track->flushAck(); |
| 4714 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4715 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4716 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4717 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4718 | track->resumeAck(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4719 | if (last && mHwPaused) { |
| 4720 | doHwResume = true; |
| 4721 | mHwPaused = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4725 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4726 | // for all its buffers to be filled before processing it. |
| 4727 | // Allow draining the buffer in case the client |
| 4728 | // app does not call stop() and relies on underrun to stop: |
| 4729 | // hence the test on (track->mRetryCount > 1). |
| 4730 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4731 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4732 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4733 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4734 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4735 | minFrames = mNormalFrameCount; |
| 4736 | } else { |
| 4737 | minFrames = 1; |
| 4738 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4739 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4740 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4741 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4742 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4743 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4744 | |
| 4745 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4746 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4747 | // make sure processVolume_l() will apply new volume even if 0 |
| 4748 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4749 | if (!mHwSupportsPause) { |
| 4750 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | |
| 4754 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4755 | processVolume_l(track, last); |
| 4756 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4757 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4758 | if (previousTrack != 0) { |
| 4759 | if (track != previousTrack.get()) { |
| 4760 | // Flush any data still being written from last track |
| 4761 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4762 | // Invalidate previous track to force a seek when resuming. |
| 4763 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | mPreviousTrack = track; |
| 4767 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4768 | // reset retry count |
| 4769 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4770 | mActiveTrack = t; |
| 4771 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4772 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4773 | doHwResume = true; |
| 4774 | mHwPaused = false; |
| 4775 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4776 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4777 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4778 | // clear effect chain input buffer if the last active track started underruns |
| 4779 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4780 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 | mEffectChains[0]->clearInputBuffer(); |
| 4782 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4783 | if (track->isStopping_1()) { |
| 4784 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4785 | if (last && mHwPaused) { |
| 4786 | doHwResume = true; |
| 4787 | mHwPaused = false; |
| 4788 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4789 | } |
| 4790 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4791 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4792 | // We have consumed all the buffers of this track. |
| 4793 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4794 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4795 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4796 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4797 | } else { |
| 4798 | audioHALFrames = 0; |
| 4799 | } |
| 4800 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4801 | size_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4802 | if (mStandby || !last || |
| 4803 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4804 | if (track->isStopping_2()) { |
| 4805 | track->mState = TrackBase::STOPPED; |
| 4806 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4807 | if (track->isStopped()) { |
| 4808 | track->reset(); |
| 4809 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4810 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4811 | } |
| 4812 | } else { |
| 4813 | // No buffers for this track. Give it a few chances to |
| 4814 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4815 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4816 | if (--(track->mRetryCount) <= 0) { |
| 4817 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4818 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4819 | // indicate to client process that the track was disabled because of underrun; |
| 4820 | // it will then automatically call start() when data is available |
| 4821 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4822 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4823 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 4824 | "minFrames = %u, mFormat = %#x", |
| 4825 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4826 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4827 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4828 | doHwPause = true; |
| 4829 | mHwPaused = true; |
| 4830 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4836 | // if an active track did not command a flush, check for pending flush on stopped tracks |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4837 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4838 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4839 | if (mTracks[i]->isFlushPending()) { |
| 4840 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4841 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4847 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4848 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4849 | // if resume is received before pause is executed. |
| 4850 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4851 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4852 | mOutput->stream->pause(mOutput->stream); |
| 4853 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4854 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4855 | flushHw_l(); |
| 4856 | } |
| 4857 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 4858 | mOutput->stream->resume(mOutput->stream); |
| 4859 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4860 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4861 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4862 | |
| 4863 | return mixerStatus; |
| 4864 | } |
| 4865 | |
| 4866 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4867 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4868 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4869 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4870 | // output audio to hardware |
| 4871 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4872 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4873 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4874 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 4875 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4876 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4877 | break; |
| 4878 | } |
| 4879 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4880 | frameCount -= buffer.frameCount; |
| 4881 | curBuf += buffer.frameCount * mFrameSize; |
| 4882 | mActiveTrack->releaseBuffer(&buffer); |
| 4883 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4884 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4885 | mSleepTimeUs = 0; |
| 4886 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4887 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4891 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4892 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4893 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4894 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4895 | return; |
| 4896 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4897 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4898 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4899 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4900 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4901 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4902 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4903 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4904 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4905 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4906 | } |
| 4907 | } |
| 4908 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4909 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 4910 | { |
| 4911 | { |
| 4912 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4913 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4914 | if (mTracks[i]->isFlushPending()) { |
| 4915 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4916 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4917 | } |
| 4918 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4919 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4920 | flushHw_l(); |
| 4921 | } |
| 4922 | } |
| 4923 | PlaybackThread::threadLoop_exit(); |
| 4924 | } |
| 4925 | |
| 4926 | // must be called with thread mutex locked |
| 4927 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 4928 | { |
| 4929 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4930 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4931 | |
| 4932 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4933 | // after a timeout and we will enter standby then. |
| 4934 | if (mTracks.size() > 0) { |
| 4935 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4936 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 4937 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4938 | } |
| 4939 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4940 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4941 | } |
| 4942 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4943 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4944 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4945 | audio_format_t format __unused, int sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4946 | { |
| 4947 | return 0; |
| 4948 | } |
| 4949 | |
| 4950 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4951 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4952 | { |
| 4953 | } |
| 4954 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4955 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4956 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4957 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4958 | { |
| 4959 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4960 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4961 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4962 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4963 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4964 | AudioParameter param = AudioParameter(keyValuePair); |
| 4965 | int value; |
| 4966 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4967 | // forward device change to effects that have requested to be |
| 4968 | // aware of attached audio device. |
| 4969 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4970 | a2dpDeviceChanged = |
| 4971 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4972 | mOutDevice = value; |
| 4973 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4974 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4975 | } |
| 4976 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4977 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4978 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4979 | // do not accept frame count changes if tracks are open as the track buffer |
| 4980 | // size depends on frame count and correct behavior would not be garantied |
| 4981 | // if frame count is changed after track creation |
| 4982 | if (!mTracks.isEmpty()) { |
| 4983 | status = INVALID_OPERATION; |
| 4984 | } else { |
| 4985 | reconfig = true; |
| 4986 | } |
| 4987 | } |
| 4988 | if (status == NO_ERROR) { |
| 4989 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4990 | keyValuePair.string()); |
| 4991 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4992 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4993 | mStandby = true; |
| 4994 | mBytesWritten = 0; |
| 4995 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4996 | keyValuePair.string()); |
| 4997 | } |
| 4998 | if (status == NO_ERROR && reconfig) { |
| 4999 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5000 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5004 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5008 | { |
| 5009 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5010 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5011 | time = PlaybackThread::activeSleepTimeUs(); |
| 5012 | } else { |
| 5013 | time = 10000; |
| 5014 | } |
| 5015 | return time; |
| 5016 | } |
| 5017 | |
| 5018 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5019 | { |
| 5020 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5021 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5022 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5023 | } else { |
| 5024 | time = 10000; |
| 5025 | } |
| 5026 | return time; |
| 5027 | } |
| 5028 | |
| 5029 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5030 | { |
| 5031 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5032 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5033 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5034 | } else { |
| 5035 | time = 10000; |
| 5036 | } |
| 5037 | return time; |
| 5038 | } |
| 5039 | |
| 5040 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5041 | { |
| 5042 | PlaybackThread::cacheParameters_l(); |
| 5043 | |
| 5044 | // use shorter standby delay as on normal output to release |
| 5045 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5046 | // no delay on outputs with HW A/V sync |
| 5047 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5048 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5049 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5050 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5051 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5052 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5053 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5056 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5057 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5058 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5059 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5060 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5061 | } |
| 5062 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5063 | // ---------------------------------------------------------------------------- |
| 5064 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5065 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5066 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5067 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5068 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5069 | mWriteAckSequence(0), |
| 5070 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5071 | { |
| 5072 | } |
| 5073 | |
| 5074 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5075 | { |
| 5076 | } |
| 5077 | |
| 5078 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5079 | { |
| 5080 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5081 | } |
| 5082 | |
| 5083 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5084 | { |
| 5085 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5086 | uint32_t writeAckSequence; |
| 5087 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5088 | |
| 5089 | { |
| 5090 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5091 | while (!((mWriteAckSequence & 1) || |
| 5092 | (mDrainSequence & 1) || |
| 5093 | exitPending())) { |
| 5094 | mWaitWorkCV.wait(mLock); |
| 5095 | } |
| 5096 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5097 | if (exitPending()) { |
| 5098 | break; |
| 5099 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5100 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5101 | mWriteAckSequence, mDrainSequence); |
| 5102 | writeAckSequence = mWriteAckSequence; |
| 5103 | mWriteAckSequence &= ~1; |
| 5104 | drainSequence = mDrainSequence; |
| 5105 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5106 | } |
| 5107 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5108 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5109 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5110 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5111 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5112 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5113 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5114 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5115 | } |
| 5116 | } |
| 5117 | } |
| 5118 | } |
| 5119 | return false; |
| 5120 | } |
| 5121 | |
| 5122 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5123 | { |
| 5124 | ALOGV("AsyncCallbackThread::exit"); |
| 5125 | Mutex::Autolock _l(mLock); |
| 5126 | requestExit(); |
| 5127 | mWaitWorkCV.broadcast(); |
| 5128 | } |
| 5129 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5130 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5131 | { |
| 5132 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5133 | // bit 0 is cleared |
| 5134 | mWriteAckSequence = sequence << 1; |
| 5135 | } |
| 5136 | |
| 5137 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5138 | { |
| 5139 | Mutex::Autolock _l(mLock); |
| 5140 | // ignore unexpected callbacks |
| 5141 | if (mWriteAckSequence & 2) { |
| 5142 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5143 | mWaitWorkCV.signal(); |
| 5144 | } |
| 5145 | } |
| 5146 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5147 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5148 | { |
| 5149 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5150 | // bit 0 is cleared |
| 5151 | mDrainSequence = sequence << 1; |
| 5152 | } |
| 5153 | |
| 5154 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5155 | { |
| 5156 | Mutex::Autolock _l(mLock); |
| 5157 | // ignore unexpected callbacks |
| 5158 | if (mDrainSequence & 2) { |
| 5159 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5160 | mWaitWorkCV.signal(); |
| 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | |
| 5165 | // ---------------------------------------------------------------------------- |
| 5166 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5167 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5168 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5169 | mPausedBytesRemaining(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5170 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5171 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5172 | mStandby = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5173 | } |
| 5174 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5175 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5176 | { |
| 5177 | if (mFlushPending || mHwPaused) { |
| 5178 | // If a flush is pending or track was paused, just discard buffered data |
| 5179 | flushHw_l(); |
| 5180 | } else { |
| 5181 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5182 | threadLoop_drain(); |
| 5183 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5184 | if (mUseAsyncWrite) { |
| 5185 | ALOG_ASSERT(mCallbackThread != 0); |
| 5186 | mCallbackThread->exit(); |
| 5187 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5188 | PlaybackThread::threadLoop_exit(); |
| 5189 | } |
| 5190 | |
| 5191 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5192 | Vector< sp<Track> > *tracksToRemove |
| 5193 | ) |
| 5194 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5195 | size_t count = mActiveTracks.size(); |
| 5196 | |
| 5197 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5198 | bool doHwPause = false; |
| 5199 | bool doHwResume = false; |
| 5200 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5201 | ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); |
| 5202 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5203 | // find out which tracks need to be processed |
| 5204 | for (size_t i = 0; i < count; i++) { |
| 5205 | sp<Track> t = mActiveTracks[i].promote(); |
| 5206 | // The track died recently |
| 5207 | if (t == 0) { |
| 5208 | continue; |
| 5209 | } |
| 5210 | Track* const track = t.get(); |
| 5211 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5212 | // Only consider last track started for volume and mixer state control. |
| 5213 | // In theory an older track could underrun and restart after the new one starts |
| 5214 | // but as we only care about the transition phase between two tracks on a |
| 5215 | // direct output, it is not a problem to ignore the underrun case. |
| 5216 | sp<Track> l = mLatestActiveTrack.promote(); |
| 5217 | bool last = l.get() == track; |
| 5218 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5219 | if (track->isInvalid()) { |
| 5220 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5221 | tracksToRemove->add(track); |
| 5222 | continue; |
| 5223 | } |
| 5224 | |
| 5225 | if (track->mState == TrackBase::IDLE) { |
| 5226 | ALOGW("An idle track shouldn't be in active list"); |
| 5227 | continue; |
| 5228 | } |
| 5229 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5230 | if (track->isPausing()) { |
| 5231 | track->setPaused(); |
| 5232 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5233 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5234 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5235 | mHwPaused = true; |
| 5236 | } |
| 5237 | // If we were part way through writing the mixbuffer to |
| 5238 | // the HAL we must save this until we resume |
| 5239 | // BUG - this will be wrong if a different track is made active, |
| 5240 | // in that case we want to discard the pending data in the |
| 5241 | // mixbuffer and tell the client to present it again when the |
| 5242 | // track is resumed |
| 5243 | mPausedWriteLength = mCurrentWriteLength; |
| 5244 | mPausedBytesRemaining = mBytesRemaining; |
| 5245 | mBytesRemaining = 0; // stop writing |
| 5246 | } |
| 5247 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5248 | } else if (track->isFlushPending()) { |
| 5249 | track->flushAck(); |
| 5250 | if (last) { |
| 5251 | mFlushPending = true; |
| 5252 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5253 | } else if (track->isResumePending()){ |
| 5254 | track->resumeAck(); |
| 5255 | if (last) { |
| 5256 | if (mPausedBytesRemaining) { |
| 5257 | // Need to continue write that was interrupted |
| 5258 | mCurrentWriteLength = mPausedWriteLength; |
| 5259 | mBytesRemaining = mPausedBytesRemaining; |
| 5260 | mPausedBytesRemaining = 0; |
| 5261 | } |
| 5262 | if (mHwPaused) { |
| 5263 | doHwResume = true; |
| 5264 | mHwPaused = false; |
| 5265 | // threadLoop_mix() will handle the case that we need to |
| 5266 | // resume an interrupted write |
| 5267 | } |
| 5268 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5269 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5270 | |
| 5271 | // Do not handle new data in this iteration even if track->framesReady() |
| 5272 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5273 | } |
| 5274 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5275 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5276 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5277 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5278 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5279 | // make sure processVolume_l() will apply new volume even if 0 |
| 5280 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5284 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5285 | if (previousTrack != 0) { |
| 5286 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5287 | // Flush any data still being written from last track |
| 5288 | mBytesRemaining = 0; |
| 5289 | if (mPausedBytesRemaining) { |
| 5290 | // Last track was paused so we also need to flush saved |
| 5291 | // mixbuffer state and invalidate track so that it will |
| 5292 | // re-submit that unwritten data when it is next resumed |
| 5293 | mPausedBytesRemaining = 0; |
| 5294 | // Invalidate is a bit drastic - would be more efficient |
| 5295 | // to have a flag to tell client that some of the |
| 5296 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5297 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5298 | } |
| 5299 | // flush data already sent to the DSP if changing audio session as audio |
| 5300 | // comes from a different source. Also invalidate previous track to force a |
| 5301 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5302 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5303 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5308 | // reset retry count |
| 5309 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5310 | mActiveTrack = t; |
| 5311 | mixerStatus = MIXER_TRACKS_READY; |
| 5312 | } |
| 5313 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5314 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5315 | if (track->isStopping_1()) { |
| 5316 | // Hardware buffer can hold a large amount of audio so we must |
| 5317 | // wait for all current track's data to drain before we say |
| 5318 | // that the track is stopped. |
| 5319 | if (mBytesRemaining == 0) { |
| 5320 | // Only start draining when all data in mixbuffer |
| 5321 | // has been written |
| 5322 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5323 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5324 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 5325 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5326 | // do not modify drain sequence if we are already draining. This happens |
| 5327 | // when resuming from pause after drain. |
| 5328 | if ((mDrainSequence & 1) == 0) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5329 | mSleepTimeUs = 0; |
| 5330 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5331 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5332 | mDrainSequence += 2; |
| 5333 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5334 | if (mHwPaused) { |
| 5335 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5336 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5337 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5338 | mHwPaused = false; |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5343 | // Drain has completed or we are in standby, signal presentation complete |
| 5344 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5345 | track->mState = TrackBase::STOPPED; |
| 5346 | size_t audioHALFrames = |
| 5347 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 5348 | size_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5349 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5350 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5351 | track->reset(); |
| 5352 | tracksToRemove->add(track); |
| 5353 | } |
| 5354 | } else { |
| 5355 | // No buffers for this track. Give it a few chances to |
| 5356 | // fill a buffer, then remove it from active list. |
| 5357 | if (--(track->mRetryCount) <= 0) { |
| 5358 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5359 | track->name()); |
| 5360 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5361 | // indicate to client process that the track was disabled because of underrun; |
| 5362 | // it will then automatically call start() when data is available |
| 5363 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5364 | } else if (last){ |
| 5365 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5366 | } |
| 5367 | } |
| 5368 | } |
| 5369 | // compute volume for this track |
| 5370 | processVolume_l(track, last); |
| 5371 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5372 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5373 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5374 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5375 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5376 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5377 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5378 | mOutput->stream->pause(mOutput->stream); |
| 5379 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5380 | if (mFlushPending) { |
| 5381 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5382 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5383 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5384 | mOutput->stream->resume(mOutput->stream); |
| 5385 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5386 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5387 | // remove all the tracks that need to be... |
| 5388 | removeTracks_l(*tracksToRemove); |
| 5389 | |
| 5390 | return mixerStatus; |
| 5391 | } |
| 5392 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5393 | // must be called with thread mutex locked |
| 5394 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5395 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5396 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5397 | mWriteAckSequence, mDrainSequence); |
| 5398 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5399 | return true; |
| 5400 | } |
| 5401 | return false; |
| 5402 | } |
| 5403 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5404 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5405 | { |
| 5406 | Mutex::Autolock _l(mLock); |
| 5407 | return waitingAsyncCallback_l(); |
| 5408 | } |
| 5409 | |
| 5410 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5411 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5412 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5413 | // Flush anything still waiting in the mixbuffer |
| 5414 | mCurrentWriteLength = 0; |
| 5415 | mBytesRemaining = 0; |
| 5416 | mPausedWriteLength = 0; |
| 5417 | mPausedBytesRemaining = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5418 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5419 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5420 | // discard any pending drain or write ack by incrementing sequence |
| 5421 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5422 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5423 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5424 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5425 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5426 | } |
| 5427 | } |
| 5428 | |
| 5429 | // ---------------------------------------------------------------------------- |
| 5430 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5431 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5432 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5433 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5434 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5435 | mWaitTimeMs(UINT_MAX) |
| 5436 | { |
| 5437 | addOutputTrack(mainThread); |
| 5438 | } |
| 5439 | |
| 5440 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5441 | { |
| 5442 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5443 | mOutputTracks[i]->destroy(); |
| 5444 | } |
| 5445 | } |
| 5446 | |
| 5447 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5448 | { |
| 5449 | // mix buffers... |
| 5450 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5451 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5452 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5453 | if (mMixerBufferValid) { |
| 5454 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5455 | } else { |
| 5456 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5457 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5458 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5459 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5460 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5461 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5462 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5466 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5467 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5468 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5469 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5470 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5471 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5472 | } |
| 5473 | } else if (mBytesWritten != 0) { |
| 5474 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5475 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5476 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5477 | } else { |
| 5478 | // flush remaining overflow buffers in output tracks |
| 5479 | writeFrames = 0; |
| 5480 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5481 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5482 | } |
| 5483 | } |
| 5484 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5485 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5486 | { |
| 5487 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5488 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5489 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5490 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5491 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5495 | { |
| 5496 | // DuplicatingThread implements standby by stopping all tracks |
| 5497 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5498 | outputTracks[i]->stop(); |
| 5499 | } |
| 5500 | } |
| 5501 | |
| 5502 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5503 | { |
| 5504 | outputTracks = mOutputTracks; |
| 5505 | } |
| 5506 | |
| 5507 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5508 | { |
| 5509 | outputTracks.clear(); |
| 5510 | } |
| 5511 | |
| 5512 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5513 | { |
| 5514 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5515 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5516 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5517 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5518 | const size_t frameCount = |
| 5519 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5520 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5521 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5522 | // nearly empty and the other may be dropping data). |
| 5523 | |
| 5524 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5525 | this, |
| 5526 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5527 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5528 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5529 | frameCount, |
| 5530 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5531 | if (outputTrack->cblk() != NULL) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5532 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5533 | mOutputTracks.add(outputTrack); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5534 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5535 | updateWaitTime_l(); |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5540 | { |
| 5541 | Mutex::Autolock _l(mLock); |
| 5542 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5543 | if (mOutputTracks[i]->thread() == thread) { |
| 5544 | mOutputTracks[i]->destroy(); |
| 5545 | mOutputTracks.removeAt(i); |
| 5546 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5547 | if (thread->getOutput() == mOutput) { |
| 5548 | mOutput = NULL; |
| 5549 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5550 | return; |
| 5551 | } |
| 5552 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5553 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | // caller must hold mLock |
| 5557 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5558 | { |
| 5559 | mWaitTimeMs = UINT_MAX; |
| 5560 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5561 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5562 | if (strong != 0) { |
| 5563 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5564 | if (waitTimeMs < mWaitTimeMs) { |
| 5565 | mWaitTimeMs = waitTimeMs; |
| 5566 | } |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | |
| 5571 | |
| 5572 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5573 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5574 | { |
| 5575 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5576 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5577 | if (thread == 0) { |
| 5578 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5579 | outputTracks[i].get()); |
| 5580 | return false; |
| 5581 | } |
| 5582 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5583 | // see note at standby() declaration |
| 5584 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5585 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5586 | thread.get()); |
| 5587 | return false; |
| 5588 | } |
| 5589 | } |
| 5590 | return true; |
| 5591 | } |
| 5592 | |
| 5593 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5594 | { |
| 5595 | return (mWaitTimeMs * 1000) / 2; |
| 5596 | } |
| 5597 | |
| 5598 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5599 | { |
| 5600 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5601 | updateWaitTime_l(); |
| 5602 | |
| 5603 | MixerThread::cacheParameters_l(); |
| 5604 | } |
| 5605 | |
| 5606 | // ---------------------------------------------------------------------------- |
| 5607 | // Record |
| 5608 | // ---------------------------------------------------------------------------- |
| 5609 | |
| 5610 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5611 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5612 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5613 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5614 | audio_devices_t inDevice, |
| 5615 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5616 | #ifdef TEE_SINK |
| 5617 | , const sp<NBAIO_Sink>& teeSink |
| 5618 | #endif |
| 5619 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5620 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5621 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5622 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5623 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5624 | #ifdef TEE_SINK |
| 5625 | , mTeeSink(teeSink) |
| 5626 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5627 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5628 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5629 | // mFastCapture below |
| 5630 | , mFastCaptureFutex(0) |
| 5631 | // mInputSource |
| 5632 | // mPipeSink |
| 5633 | // mPipeSource |
| 5634 | , mPipeFramesP2(0) |
| 5635 | // mPipeMemory |
| 5636 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5637 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5638 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5639 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5640 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5641 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5642 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5643 | |
| 5644 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5645 | mInputSource = new AudioStreamInSource(input->stream); |
| 5646 | size_t numCounterOffers = 0; |
| 5647 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
| 5648 | ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 5649 | ALOG_ASSERT(index == 0); |
| 5650 | |
| 5651 | // initialize fast capture depending on configuration |
| 5652 | bool initFastCapture; |
| 5653 | switch (kUseFastCapture) { |
| 5654 | case FastCapture_Never: |
| 5655 | initFastCapture = false; |
| 5656 | break; |
| 5657 | case FastCapture_Always: |
| 5658 | initFastCapture = true; |
| 5659 | break; |
| 5660 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5661 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5662 | break; |
| 5663 | // case FastCapture_Dynamic: |
| 5664 | } |
| 5665 | |
| 5666 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5667 | // create a Pipe for FastCapture to write to, and for us and fast tracks to read from |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5668 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5669 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5670 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5671 | void *pipeBuffer; |
| 5672 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5673 | sp<IMemory> pipeMemory; |
| 5674 | if ((roHeap == 0) || |
| 5675 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5676 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5677 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5678 | goto failed; |
| 5679 | } |
| 5680 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5681 | memset(pipeBuffer, 0, pipeSize); |
| 5682 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5683 | const NBAIO_Format offers[1] = {format}; |
| 5684 | size_t numCounterOffers = 0; |
| 5685 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5686 | ALOG_ASSERT(index == 0); |
| 5687 | mPipeSink = pipe; |
| 5688 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5689 | numCounterOffers = 0; |
| 5690 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5691 | ALOG_ASSERT(index == 0); |
| 5692 | mPipeSource = pipeReader; |
| 5693 | mPipeFramesP2 = pipeFramesP2; |
| 5694 | mPipeMemory = pipeMemory; |
| 5695 | |
| 5696 | // create fast capture |
| 5697 | mFastCapture = new FastCapture(); |
| 5698 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5699 | #ifdef STATE_QUEUE_DUMP |
| 5700 | // FIXME |
| 5701 | #endif |
| 5702 | FastCaptureState *state = sq->begin(); |
| 5703 | state->mCblk = NULL; |
| 5704 | state->mInputSource = mInputSource.get(); |
| 5705 | state->mInputSourceGen++; |
| 5706 | state->mPipeSink = pipe; |
| 5707 | state->mPipeSinkGen++; |
| 5708 | state->mFrameCount = mFrameCount; |
| 5709 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5710 | // already done in constructor initialization list |
| 5711 | //mFastCaptureFutex = 0; |
| 5712 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5713 | state->mColdGen++; |
| 5714 | state->mDumpState = &mFastCaptureDumpState; |
| 5715 | #ifdef TEE_SINK |
| 5716 | // FIXME |
| 5717 | #endif |
| 5718 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 5719 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 5720 | sq->end(); |
| 5721 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5722 | |
| 5723 | // start the fast capture |
| 5724 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5725 | pid_t tid = mFastCapture->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5726 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5727 | #ifdef AUDIO_WATCHDOG |
| 5728 | // FIXME |
| 5729 | #endif |
| 5730 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5731 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5732 | } |
| 5733 | failed: ; |
| 5734 | |
| 5735 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5736 | } |
| 5737 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5738 | AudioFlinger::RecordThread::~RecordThread() |
| 5739 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5740 | if (mFastCapture != 0) { |
| 5741 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5742 | FastCaptureState *state = sq->begin(); |
| 5743 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5744 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5745 | if (old == -1) { |
| 5746 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5747 | } |
| 5748 | } |
| 5749 | state->mCommand = FastCaptureState::EXIT; |
| 5750 | sq->end(); |
| 5751 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5752 | mFastCapture->join(); |
| 5753 | mFastCapture.clear(); |
| 5754 | } |
| 5755 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5756 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5757 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | void AudioFlinger::RecordThread::onFirstRef() |
| 5761 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5762 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5763 | } |
| 5764 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5765 | bool AudioFlinger::RecordThread::threadLoop() |
| 5766 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5767 | nsecs_t lastWarning = 0; |
| 5768 | |
| 5769 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5770 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5771 | reacquire_wakelock: |
| 5772 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5773 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5774 | { |
| 5775 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5776 | size_t size = mActiveTracks.size(); |
| 5777 | activeTracksGen = mActiveTracksGen; |
| 5778 | if (size > 0) { |
| 5779 | // FIXME an arbitrary choice |
| 5780 | activeTrack = mActiveTracks[0]; |
| 5781 | acquireWakeLock_l(activeTrack->uid()); |
| 5782 | if (size > 1) { |
| 5783 | SortedVector<int> tmp; |
| 5784 | for (size_t i = 0; i < size; i++) { |
| 5785 | tmp.add(mActiveTracks[i]->uid()); |
| 5786 | } |
| 5787 | updateWakeLockUids_l(tmp); |
| 5788 | } |
| 5789 | } else { |
| 5790 | acquireWakeLock_l(-1); |
| 5791 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5792 | } |
| 5793 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 5794 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 5795 | gBoottime.getBoottimeOffset(); |
| 5796 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5797 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 5798 | uint32_t sleepUs = 0; |
| 5799 | |
| 5800 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5801 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5802 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5803 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5804 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5805 | if (sleepUs > 0) { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5806 | ATRACE_BEGIN("sleep"); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5807 | usleep(sleepUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5808 | ATRACE_END(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5809 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5810 | } |
| 5811 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5812 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5813 | Vector< sp<RecordTrack> > activeTracks; |
| 5814 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5815 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5816 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5817 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5818 | // reference to a fast track which is about to be removed |
| 5819 | sp<RecordTrack> fastTrackToRemove; |
| 5820 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5821 | { // scope for mLock |
| 5822 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5823 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5824 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5825 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5826 | // check exitPending here because checkForNewParameters_l() and |
| 5827 | // checkForNewParameters_l() can temporarily release mLock |
| 5828 | if (exitPending()) { |
| 5829 | break; |
| 5830 | } |
| 5831 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5832 | // if no active track(s), then standby and release wakelock |
| 5833 | size_t size = mActiveTracks.size(); |
| 5834 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5835 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5836 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5837 | releaseWakeLock_l(); |
| 5838 | ALOGV("RecordThread: loop stopping"); |
| 5839 | // go to sleep |
| 5840 | mWaitWorkCV.wait(mLock); |
| 5841 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5842 | goto reacquire_wakelock; |
| 5843 | } |
| 5844 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5845 | if (mActiveTracksGen != activeTracksGen) { |
| 5846 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5847 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5848 | for (size_t i = 0; i < size; i++) { |
| 5849 | tmp.add(mActiveTracks[i]->uid()); |
| 5850 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5851 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5852 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5853 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5854 | bool doBroadcast = false; |
| 5855 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5856 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5857 | activeTrack = mActiveTracks[i]; |
| 5858 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5859 | if (activeTrack->isFastTrack()) { |
| 5860 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 5861 | fastTrackToRemove = activeTrack; |
| 5862 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5863 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5864 | mActiveTracks.remove(activeTrack); |
| 5865 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5866 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5867 | continue; |
| 5868 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5869 | |
| 5870 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5871 | switch (activeTrackState) { |
| 5872 | |
| 5873 | case TrackBase::PAUSING: |
| 5874 | mActiveTracks.remove(activeTrack); |
| 5875 | mActiveTracksGen++; |
| 5876 | doBroadcast = true; |
| 5877 | size--; |
| 5878 | continue; |
| 5879 | |
| 5880 | case TrackBase::STARTING_1: |
| 5881 | sleepUs = 10000; |
| 5882 | i++; |
| 5883 | continue; |
| 5884 | |
| 5885 | case TrackBase::STARTING_2: |
| 5886 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5887 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5888 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5889 | break; |
| 5890 | |
| 5891 | case TrackBase::ACTIVE: |
| 5892 | break; |
| 5893 | |
| 5894 | case TrackBase::IDLE: |
| 5895 | i++; |
| 5896 | continue; |
| 5897 | |
| 5898 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5899 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5900 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5901 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5902 | activeTracks.add(activeTrack); |
| 5903 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5904 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5905 | if (activeTrack->isFastTrack()) { |
| 5906 | ALOG_ASSERT(!mFastTrackAvail); |
| 5907 | ALOG_ASSERT(fastTrack == 0); |
| 5908 | fastTrack = activeTrack; |
| 5909 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5910 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5911 | if (doBroadcast) { |
| 5912 | mStartStopCond.broadcast(); |
| 5913 | } |
| 5914 | |
| 5915 | // sleep if there are no active tracks to process |
| 5916 | if (activeTracks.size() == 0) { |
| 5917 | if (sleepUs == 0) { |
| 5918 | sleepUs = kRecordThreadSleepUs; |
| 5919 | } |
| 5920 | continue; |
| 5921 | } |
| 5922 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5923 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5924 | lockEffectChains_l(effectChains); |
| 5925 | } |
| 5926 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5927 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5928 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5929 | size_t size = effectChains.size(); |
| 5930 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5931 | // thread mutex is not locked, but effect chain is locked |
| 5932 | effectChains[i]->process_l(); |
| 5933 | } |
| 5934 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5935 | // Push a new fast capture state if fast capture is not already running, or cblk change |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5936 | if (mFastCapture != 0) { |
| 5937 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5938 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5939 | bool didModify = false; |
| 5940 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5941 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5942 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5943 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5944 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5945 | if (old == -1) { |
| 5946 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5947 | } |
| 5948 | } |
| 5949 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5950 | #if 0 // FIXME |
| 5951 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5952 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 5953 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5954 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5955 | didModify = true; |
| 5956 | } |
| 5957 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 5958 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5959 | if (cblkNew != cblkOld) { |
| 5960 | state->mCblk = cblkNew; |
| 5961 | // block until acked if removing a fast track |
| 5962 | if (cblkOld != NULL) { |
| 5963 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 5964 | } |
| 5965 | didModify = true; |
| 5966 | } |
| 5967 | sq->end(didModify); |
| 5968 | if (didModify) { |
| 5969 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5970 | #if 0 |
| 5971 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5972 | mNormalSource = mPipeSource; |
| 5973 | } |
| 5974 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5975 | } |
| 5976 | } |
| 5977 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5978 | // now run the fast track destructor with thread mutex unlocked |
| 5979 | fastTrackToRemove.clear(); |
| 5980 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5981 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 5982 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 5983 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 5984 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 5985 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5986 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5987 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5988 | ssize_t framesRead; |
| 5989 | |
| 5990 | // If an NBAIO source is present, use it to read the normal capture's data |
| 5991 | if (mPipeSource != 0) { |
| 5992 | size_t framesToRead = mBufferSize / mFrameSize; |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5993 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5994 | framesToRead); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5995 | if (framesRead == 0) { |
| 5996 | // since pipe is non-blocking, simulate blocking input |
| 5997 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 5998 | } |
| 5999 | // otherwise use the HAL / AudioStreamIn directly |
| 6000 | } else { |
| 6001 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6002 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6003 | if (bytesRead < 0) { |
| 6004 | framesRead = bytesRead; |
| 6005 | } else { |
| 6006 | framesRead = bytesRead / mFrameSize; |
| 6007 | } |
| 6008 | } |
| 6009 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6010 | // Update server timestamp with server stats |
| 6011 | // systemTime() is optional if the hardware supports timestamps. |
| 6012 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6013 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6014 | |
| 6015 | // Update server timestamp with kernel stats |
| 6016 | if (mInput->stream->get_capture_position != nullptr) { |
| 6017 | int64_t position, time; |
| 6018 | int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time); |
| 6019 | if (ret == NO_ERROR) { |
| 6020 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6021 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6022 | // Note: In general record buffers should tend to be empty in |
| 6023 | // a properly running pipeline. |
| 6024 | // |
| 6025 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6026 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6027 | } |
| 6028 | } |
| 6029 | // Use this to track timestamp information |
| 6030 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6031 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6032 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
| 6033 | ALOGE("read failed: framesRead=%d", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6034 | // Force input into standby so that it tries to recover at next read attempt |
| 6035 | inputStandBy(); |
| 6036 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6037 | } |
| 6038 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6039 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6040 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6041 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6042 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6043 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6044 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6045 | } |
| 6046 | // 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] | 6047 | { |
| 6048 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6049 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6050 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6051 | (framesRead - part1) * mFrameSize); |
| 6052 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6053 | } |
| 6054 | rear = mRsmpInRear += framesRead; |
| 6055 | |
| 6056 | size = activeTracks.size(); |
| 6057 | // loop over each active track |
| 6058 | for (size_t i = 0; i < size; i++) { |
| 6059 | activeTrack = activeTracks[i]; |
| 6060 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6061 | // skip fast tracks, as those are handled directly by FastCapture |
| 6062 | if (activeTrack->isFastTrack()) { |
| 6063 | continue; |
| 6064 | } |
| 6065 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6066 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6067 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6068 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6069 | enum { |
| 6070 | OVERRUN_UNKNOWN, |
| 6071 | OVERRUN_TRUE, |
| 6072 | OVERRUN_FALSE |
| 6073 | } overrun = OVERRUN_UNKNOWN; |
| 6074 | |
| 6075 | // loop over getNextBuffer to handle circular sink |
| 6076 | for (;;) { |
| 6077 | |
| 6078 | activeTrack->mSink.frameCount = ~0; |
| 6079 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6080 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6081 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6082 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6083 | // check available frames and handle overrun conditions |
| 6084 | // if the record track isn't draining fast enough. |
| 6085 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6086 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6087 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6088 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6089 | overrun = OVERRUN_TRUE; |
| 6090 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6091 | if (framesOut == 0 || framesIn == 0) { |
| 6092 | break; |
| 6093 | } |
| 6094 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6095 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6096 | // from framesIn. |
| 6097 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6098 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6099 | framesOut = min(framesOut, |
| 6100 | destinationFramesPossible( |
| 6101 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6102 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6103 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6104 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6105 | |
| 6106 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6107 | overrun = OVERRUN_FALSE; |
| 6108 | } |
| 6109 | |
| 6110 | if (activeTrack->mFramesToDrop == 0) { |
| 6111 | if (framesOut > 0) { |
| 6112 | activeTrack->mSink.frameCount = framesOut; |
| 6113 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6114 | } |
| 6115 | } else { |
| 6116 | // FIXME could do a partial drop of framesOut |
| 6117 | if (activeTrack->mFramesToDrop > 0) { |
| 6118 | activeTrack->mFramesToDrop -= framesOut; |
| 6119 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6120 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6121 | } |
| 6122 | } else { |
| 6123 | activeTrack->mFramesToDrop += framesOut; |
| 6124 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6125 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6126 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6127 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6128 | activeTrack->sessionId(), |
| 6129 | (activeTrack->mSyncStartEvent != 0) ? |
| 6130 | activeTrack->mSyncStartEvent->triggerSession() : 0); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6131 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6132 | } |
| 6133 | } |
| 6134 | } |
| 6135 | |
| 6136 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6137 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6138 | } |
| 6139 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6140 | |
| 6141 | switch (overrun) { |
| 6142 | case OVERRUN_TRUE: |
| 6143 | // client isn't retrieving buffers fast enough |
| 6144 | if (!activeTrack->setOverflow()) { |
| 6145 | nsecs_t now = systemTime(); |
| 6146 | // FIXME should lastWarning per track? |
| 6147 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6148 | ALOGW("RecordThread: buffer overflow"); |
| 6149 | lastWarning = now; |
| 6150 | } |
| 6151 | } |
| 6152 | break; |
| 6153 | case OVERRUN_FALSE: |
| 6154 | activeTrack->clearOverflow(); |
| 6155 | break; |
| 6156 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6157 | break; |
| 6158 | } |
| 6159 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6160 | // update frame information and push timestamp out |
| 6161 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6162 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6163 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6164 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6167 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6168 | // enable changes in effect chain |
| 6169 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6170 | // 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] | 6171 | } |
| 6172 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6173 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6174 | |
| 6175 | { |
| 6176 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6177 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6178 | sp<RecordTrack> track = mTracks[i]; |
| 6179 | track->invalidate(); |
| 6180 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6181 | mActiveTracks.clear(); |
| 6182 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6183 | mStartStopCond.broadcast(); |
| 6184 | } |
| 6185 | |
| 6186 | releaseWakeLock(); |
| 6187 | |
| 6188 | ALOGV("RecordThread %p exiting", this); |
| 6189 | return false; |
| 6190 | } |
| 6191 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6192 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6193 | { |
| 6194 | if (!mStandby) { |
| 6195 | inputStandBy(); |
| 6196 | mStandby = true; |
| 6197 | } |
| 6198 | } |
| 6199 | |
| 6200 | void AudioFlinger::RecordThread::inputStandBy() |
| 6201 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6202 | // Idle the fast capture if it's currently running |
| 6203 | if (mFastCapture != 0) { |
| 6204 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6205 | FastCaptureState *state = sq->begin(); |
| 6206 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6207 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6208 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6209 | state->mColdGen++; |
| 6210 | mFastCaptureFutex = 0; |
| 6211 | sq->end(); |
| 6212 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6213 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6214 | #if 0 |
| 6215 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6216 | // FIXME |
| 6217 | } |
| 6218 | #endif |
| 6219 | #ifdef AUDIO_WATCHDOG |
| 6220 | // FIXME |
| 6221 | #endif |
| 6222 | } else { |
| 6223 | sq->end(false /*didModify*/); |
| 6224 | } |
| 6225 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6226 | mInput->stream->common.standby(&mInput->stream->common); |
| 6227 | } |
| 6228 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6229 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6230 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6231 | const sp<AudioFlinger::Client>& client, |
| 6232 | uint32_t sampleRate, |
| 6233 | audio_format_t format, |
| 6234 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6235 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6236 | int sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6237 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6238 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6239 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6240 | pid_t tid, |
| 6241 | status_t *status) |
| 6242 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6243 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6244 | sp<RecordTrack> track; |
| 6245 | status_t lStatus; |
| 6246 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6247 | // client expresses a preference for FAST, but we get the final say |
| 6248 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6249 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6250 | // we formerly checked for a callback handler (non-0 tid), |
| 6251 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6252 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6253 | // frame count is not specified, or is exactly the pipe depth |
| 6254 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6255 | // PCM data |
| 6256 | audio_is_linear_pcm(format) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6257 | // native format |
| 6258 | (format == mFormat) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6259 | // native channel mask |
| 6260 | (channelMask == mChannelMask) && |
| 6261 | // native hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6262 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6263 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6264 | hasFastCapture() && |
| 6265 | // there are sufficient fast track slots available |
| 6266 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6267 | ) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6268 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6269 | frameCount, mFrameCount); |
| 6270 | } else { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6271 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " |
| 6272 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6273 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6274 | frameCount, mFrameCount, mPipeFramesP2, |
| 6275 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 6276 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6277 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6278 | } |
| 6279 | } |
| 6280 | |
| 6281 | // compute track buffer size in frames, and suggest the notification frame count |
| 6282 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6283 | // fast track: frame count is exactly the pipe depth |
| 6284 | frameCount = mPipeFramesP2; |
| 6285 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6286 | *notificationFrames = mFrameCount; |
| 6287 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6288 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6289 | // or 20 ms if there is a fast capture |
| 6290 | // TODO This could be a roundupRatio inline, and const |
| 6291 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6292 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6293 | // minimum number of notification periods is at least kMinNotifications, |
| 6294 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6295 | static const size_t kMinNotifications = 3; |
| 6296 | static const uint32_t kMinMs = 30; |
| 6297 | // TODO This could be a roundupRatio inline |
| 6298 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6299 | // TODO This could be a roundupRatio inline |
| 6300 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6301 | maxNotificationFrames; |
| 6302 | const size_t minFrameCount = maxNotificationFrames * |
| 6303 | max(kMinNotifications, minNotificationsByMs); |
| 6304 | frameCount = max(frameCount, minFrameCount); |
| 6305 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6306 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6307 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6308 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6309 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6310 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6311 | lStatus = initCheck(); |
| 6312 | if (lStatus != NO_ERROR) { |
| 6313 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6314 | goto Exit; |
| 6315 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6316 | |
| 6317 | { // scope for mLock |
| 6318 | Mutex::Autolock _l(mLock); |
| 6319 | |
| 6320 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6321 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 6322 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6323 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6324 | lStatus = track->initCheck(); |
| 6325 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6326 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6327 | // 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] | 6328 | goto Exit; |
| 6329 | } |
| 6330 | mTracks.add(track); |
| 6331 | |
| 6332 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6333 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6334 | mAudioFlinger->btNrecIsOff(); |
| 6335 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6336 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6337 | |
| 6338 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 6339 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6340 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6341 | // so ask activity manager to do this on our behalf |
| 6342 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 6343 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6344 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6345 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6346 | lStatus = NO_ERROR; |
| 6347 | |
| 6348 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6349 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6350 | return track; |
| 6351 | } |
| 6352 | |
| 6353 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6354 | AudioSystem::sync_event_t event, |
| 6355 | int triggerSession) |
| 6356 | { |
| 6357 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6358 | sp<ThreadBase> strongMe = this; |
| 6359 | status_t status = NO_ERROR; |
| 6360 | |
| 6361 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6362 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6363 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6364 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6365 | triggerSession, |
| 6366 | recordTrack->sessionId(), |
| 6367 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6368 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6369 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6370 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6371 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6372 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6373 | } else { |
| 6374 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6375 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6376 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6381 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6382 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6383 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6384 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6385 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6386 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6387 | } else { |
| 6388 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6389 | } |
| 6390 | return status; |
| 6391 | } |
| 6392 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6393 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6394 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6395 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6396 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6397 | mActiveTracks.add(recordTrack); |
| 6398 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6399 | status_t status = NO_ERROR; |
| 6400 | if (recordTrack->isExternalTrack()) { |
| 6401 | mLock.unlock(); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6402 | status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6403 | mLock.lock(); |
| 6404 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6405 | if (status != NO_ERROR) { |
| 6406 | mActiveTracks.remove(recordTrack); |
| 6407 | mActiveTracksGen++; |
| 6408 | recordTrack->clearSyncStartEvent(); |
| 6409 | ALOGV("RecordThread::start error %d", status); |
| 6410 | return status; |
| 6411 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6412 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6413 | // Catch up with current buffer indices if thread is already running. |
| 6414 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6415 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6416 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6417 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6418 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6419 | // clear any converter state as new data will be discontinuous |
| 6420 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6421 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6422 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6423 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6424 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6425 | ALOGV("Record failed to start"); |
| 6426 | status = BAD_VALUE; |
| 6427 | goto startError; |
| 6428 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6429 | return status; |
| 6430 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6431 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6432 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6433 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6434 | AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6435 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6436 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6437 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6438 | return status; |
| 6439 | } |
| 6440 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6441 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6442 | { |
| 6443 | sp<SyncEvent> strongEvent = event.promote(); |
| 6444 | |
| 6445 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6446 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6447 | if (ptr != 0) { |
| 6448 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6449 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6450 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6451 | } |
| 6452 | } |
| 6453 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6454 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6455 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6456 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6457 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6458 | return false; |
| 6459 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6460 | // 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] | 6461 | recordTrack->mState = TrackBase::PAUSING; |
| 6462 | // do not wait for mStartStopCond if exiting |
| 6463 | if (exitPending()) { |
| 6464 | return true; |
| 6465 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6466 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6467 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6468 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6469 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6470 | ALOGV("Record stopped OK"); |
| 6471 | return true; |
| 6472 | } |
| 6473 | return false; |
| 6474 | } |
| 6475 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6476 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6477 | { |
| 6478 | return false; |
| 6479 | } |
| 6480 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6481 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6482 | { |
| 6483 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6484 | if (!isValidSyncEvent(event)) { |
| 6485 | return BAD_VALUE; |
| 6486 | } |
| 6487 | |
| 6488 | int eventSession = event->triggerSession(); |
| 6489 | status_t ret = NAME_NOT_FOUND; |
| 6490 | |
| 6491 | Mutex::Autolock _l(mLock); |
| 6492 | |
| 6493 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6494 | sp<RecordTrack> track = mTracks[i]; |
| 6495 | if (eventSession == track->sessionId()) { |
| 6496 | (void) track->setSyncEvent(event); |
| 6497 | ret = NO_ERROR; |
| 6498 | } |
| 6499 | } |
| 6500 | return ret; |
| 6501 | #else |
| 6502 | return BAD_VALUE; |
| 6503 | #endif |
| 6504 | } |
| 6505 | |
| 6506 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6507 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6508 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6509 | track->terminate(); |
| 6510 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6511 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6512 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6513 | removeTrack_l(track); |
| 6514 | } |
| 6515 | } |
| 6516 | |
| 6517 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6518 | { |
| 6519 | mTracks.remove(track); |
| 6520 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6521 | if (track->isFastTrack()) { |
| 6522 | ALOG_ASSERT(!mFastTrackAvail); |
| 6523 | mFastTrackAvail = true; |
| 6524 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6528 | { |
| 6529 | dumpInternals(fd, args); |
| 6530 | dumpTracks(fd, args); |
| 6531 | dumpEffectChains(fd, args); |
| 6532 | } |
| 6533 | |
| 6534 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6535 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6536 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6537 | |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6538 | dumpBase(fd, args); |
| 6539 | |
| 6540 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6541 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6542 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6543 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6544 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6545 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6546 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6547 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6548 | // This is a large object so we place it on the heap. |
| 6549 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 6550 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 6551 | copy->dump(fd); |
| 6552 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6553 | } |
| 6554 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6555 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6556 | { |
| 6557 | const size_t SIZE = 256; |
| 6558 | char buffer[SIZE]; |
| 6559 | String8 result; |
| 6560 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6561 | size_t numtracks = mTracks.size(); |
| 6562 | size_t numactive = mActiveTracks.size(); |
| 6563 | size_t numactiveseen = 0; |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6564 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6565 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6566 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6567 | RecordTrack::appendDumpHeader(result); |
| 6568 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6569 | sp<RecordTrack> track = mTracks[i]; |
| 6570 | if (track != 0) { |
| 6571 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6572 | if (active) { |
| 6573 | numactiveseen++; |
| 6574 | } |
| 6575 | track->dump(buffer, SIZE, active); |
| 6576 | result.append(buffer); |
| 6577 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6578 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6579 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6580 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6581 | } |
| 6582 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6583 | if (numactiveseen != numactive) { |
| 6584 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6585 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6586 | result.append(buffer); |
| 6587 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6588 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6589 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6590 | if (mTracks.indexOf(track) < 0) { |
| 6591 | track->dump(buffer, SIZE, true); |
| 6592 | result.append(buffer); |
| 6593 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6594 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6595 | |
| 6596 | } |
| 6597 | write(fd, result.string(), result.size()); |
| 6598 | } |
| 6599 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6600 | |
| 6601 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6602 | { |
| 6603 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6604 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6605 | mRsmpInFront = recordThread->mRsmpInRear; |
| 6606 | mRsmpInUnrel = 0; |
| 6607 | } |
| 6608 | |
| 6609 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 6610 | size_t *framesAvailable, bool *hasOverrun) |
| 6611 | { |
| 6612 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6613 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6614 | const int32_t rear = recordThread->mRsmpInRear; |
| 6615 | const int32_t front = mRsmpInFront; |
| 6616 | const ssize_t filled = rear - front; |
| 6617 | |
| 6618 | size_t framesIn; |
| 6619 | bool overrun = false; |
| 6620 | if (filled < 0) { |
| 6621 | // should not happen, but treat like a massive overrun and re-sync |
| 6622 | framesIn = 0; |
| 6623 | mRsmpInFront = rear; |
| 6624 | overrun = true; |
| 6625 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 6626 | framesIn = (size_t) filled; |
| 6627 | } else { |
| 6628 | // client is not keeping up with server, but give it latest data |
| 6629 | framesIn = recordThread->mRsmpInFrames; |
| 6630 | mRsmpInFront = /* front = */ rear - framesIn; |
| 6631 | overrun = true; |
| 6632 | } |
| 6633 | if (framesAvailable != NULL) { |
| 6634 | *framesAvailable = framesIn; |
| 6635 | } |
| 6636 | if (hasOverrun != NULL) { |
| 6637 | *hasOverrun = overrun; |
| 6638 | } |
| 6639 | } |
| 6640 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6641 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6642 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6643 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6644 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6645 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6646 | if (threadBase == 0) { |
| 6647 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6648 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6649 | return NOT_ENOUGH_DATA; |
| 6650 | } |
| 6651 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6652 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6653 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6654 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6655 | // FIXME should not be P2 (don't want to increase latency) |
| 6656 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6657 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6658 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6659 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6660 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6661 | if (part1 > (size_t) filled) { |
| 6662 | part1 = filled; |
| 6663 | } |
| 6664 | size_t ask = buffer->frameCount; |
| 6665 | ALOG_ASSERT(ask > 0); |
| 6666 | if (part1 > ask) { |
| 6667 | part1 = ask; |
| 6668 | } |
| 6669 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6670 | // out of data is fine since the resampler will return a short-count. |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6671 | buffer->raw = NULL; |
| 6672 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6673 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6674 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6675 | } |
| 6676 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6677 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6678 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6679 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6680 | return NO_ERROR; |
| 6681 | } |
| 6682 | |
| 6683 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6684 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 6685 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6686 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6687 | size_t stepCount = buffer->frameCount; |
| 6688 | if (stepCount == 0) { |
| 6689 | return; |
| 6690 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6691 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 6692 | mRsmpInUnrel -= stepCount; |
| 6693 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6694 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6695 | buffer->frameCount = 0; |
| 6696 | } |
| 6697 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6698 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( |
| 6699 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6700 | uint32_t srcSampleRate, |
| 6701 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6702 | uint32_t dstSampleRate) : |
| 6703 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars |
| 6704 | // mSrcFormat |
| 6705 | // mSrcSampleRate |
| 6706 | // mDstChannelMask |
| 6707 | // mDstFormat |
| 6708 | // mDstSampleRate |
| 6709 | // mSrcChannelCount |
| 6710 | // mDstChannelCount |
| 6711 | // mDstFrameSize |
| 6712 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6713 | mResampler(NULL), |
| 6714 | mIsLegacyDownmix(false), |
| 6715 | mIsLegacyUpmix(false), |
| 6716 | mRequiresFloat(false), |
| 6717 | mInputConverterProvider(NULL) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6718 | { |
| 6719 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, |
| 6720 | dstChannelMask, dstFormat, dstSampleRate); |
| 6721 | } |
| 6722 | |
| 6723 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { |
| 6724 | free(mBuf); |
| 6725 | delete mResampler; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6726 | delete mInputConverterProvider; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6727 | } |
| 6728 | |
| 6729 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, |
| 6730 | AudioBufferProvider *provider, size_t frames) |
| 6731 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6732 | if (mInputConverterProvider != NULL) { |
| 6733 | mInputConverterProvider->setBufferProvider(provider); |
| 6734 | provider = mInputConverterProvider; |
| 6735 | } |
| 6736 | |
| 6737 | if (mResampler == NULL) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6738 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6739 | mSrcSampleRate, mSrcFormat, mDstFormat); |
| 6740 | |
| 6741 | AudioBufferProvider::Buffer buffer; |
| 6742 | for (size_t i = frames; i > 0; ) { |
| 6743 | buffer.frameCount = i; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6744 | status_t status = provider->getNextBuffer(&buffer); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6745 | if (status != OK || buffer.frameCount == 0) { |
| 6746 | frames -= i; // cannot fill request. |
| 6747 | break; |
| 6748 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6749 | // format convert to destination buffer |
| 6750 | convertNoResampler(dst, buffer.raw, buffer.frameCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6751 | |
| 6752 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; |
| 6753 | i -= buffer.frameCount; |
| 6754 | provider->releaseBuffer(&buffer); |
| 6755 | } |
| 6756 | } else { |
| 6757 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6758 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); |
| 6759 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6760 | // reallocate buffer if needed |
| 6761 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6762 | free(mBuf); |
| 6763 | mBufFrames = frames; |
| 6764 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6765 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6766 | // resampler accumulates, but we only have one source track |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6767 | memset(mBuf, 0, frames * mBufFrameSize); |
| 6768 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); |
| 6769 | // format convert to destination buffer |
| 6770 | convertResampler(dst, mBuf, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6771 | } |
| 6772 | return frames; |
| 6773 | } |
| 6774 | |
| 6775 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( |
| 6776 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6777 | uint32_t srcSampleRate, |
| 6778 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6779 | uint32_t dstSampleRate) |
| 6780 | { |
| 6781 | // quick evaluation if there is any change. |
| 6782 | if (mSrcFormat == srcFormat |
| 6783 | && mSrcChannelMask == srcChannelMask |
| 6784 | && mSrcSampleRate == srcSampleRate |
| 6785 | && mDstFormat == dstFormat |
| 6786 | && mDstChannelMask == dstChannelMask |
| 6787 | && mDstSampleRate == dstSampleRate) { |
| 6788 | return NO_ERROR; |
| 6789 | } |
| 6790 | |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6791 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" |
| 6792 | " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", |
| 6793 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6794 | const bool valid = |
| 6795 | audio_is_input_channel(srcChannelMask) |
| 6796 | && audio_is_input_channel(dstChannelMask) |
| 6797 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) |
| 6798 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) |
| 6799 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) |
| 6800 | ; // no upsampling checks for now |
| 6801 | if (!valid) { |
| 6802 | return BAD_VALUE; |
| 6803 | } |
| 6804 | |
| 6805 | mSrcFormat = srcFormat; |
| 6806 | mSrcChannelMask = srcChannelMask; |
| 6807 | mSrcSampleRate = srcSampleRate; |
| 6808 | mDstFormat = dstFormat; |
| 6809 | mDstChannelMask = dstChannelMask; |
| 6810 | mDstSampleRate = dstSampleRate; |
| 6811 | |
| 6812 | // compute derived parameters |
| 6813 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); |
| 6814 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); |
| 6815 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); |
| 6816 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6817 | // do we need to resample? |
| 6818 | delete mResampler; |
| 6819 | mResampler = NULL; |
| 6820 | if (mSrcSampleRate != mDstSampleRate) { |
| 6821 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, |
| 6822 | mSrcChannelCount, mDstSampleRate); |
| 6823 | mResampler->setSampleRate(mSrcSampleRate); |
| 6824 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
| 6825 | } |
| 6826 | |
| 6827 | // are we running legacy channel conversion modes? |
| 6828 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6829 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 6830 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; |
| 6831 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO |
| 6832 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6833 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); |
| 6834 | |
| 6835 | // do we need to process in float? |
| 6836 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; |
| 6837 | |
| 6838 | // do we need a staging buffer to convert for destination (we can still optimize this)? |
| 6839 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity |
| 6840 | if (mResampler != NULL) { |
| 6841 | mBufFrameSize = max(mSrcChannelCount, FCC_2) |
| 6842 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 6843 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6844 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
| 6845 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6846 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); |
| 6847 | } else { |
| 6848 | mBufFrameSize = 0; |
| 6849 | } |
| 6850 | mBufFrames = 0; // force the buffer to be resized. |
| 6851 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6852 | // do we need an input converter buffer provider to give us float? |
| 6853 | delete mInputConverterProvider; |
| 6854 | mInputConverterProvider = NULL; |
| 6855 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6856 | mInputConverterProvider = new ReformatBufferProvider( |
| 6857 | audio_channel_count_from_in_mask(mSrcChannelMask), |
| 6858 | mSrcFormat, |
| 6859 | AUDIO_FORMAT_PCM_FLOAT, |
| 6860 | 256 /* provider buffer frame count */); |
| 6861 | } |
| 6862 | |
| 6863 | // do we need a remixer to do channel mask conversion |
| 6864 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { |
| 6865 | (void) memcpy_by_index_array_initialization_from_channel_mask( |
| 6866 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6867 | } |
| 6868 | return NO_ERROR; |
| 6869 | } |
| 6870 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6871 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( |
| 6872 | void *dst, const void *src, size_t frames) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6873 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6874 | // src is native type unless there is legacy upmix or downmix, whereupon it is float. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6875 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6876 | free(mBuf); |
| 6877 | mBufFrames = frames; |
| 6878 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6879 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6880 | // do we need to do legacy upmix and downmix? |
| 6881 | if (mIsLegacyUpmix || mIsLegacyDownmix) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6882 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6883 | if (mIsLegacyUpmix) { |
| 6884 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, |
| 6885 | (const float *)src, frames); |
| 6886 | } else /*mIsLegacyDownmix */ { |
| 6887 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, |
| 6888 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6889 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6890 | if (mBuf != NULL) { |
| 6891 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, |
| 6892 | frames * mDstChannelCount); |
| 6893 | } |
| 6894 | return; |
| 6895 | } |
| 6896 | // do we need to do channel mask conversion? |
| 6897 | if (mSrcChannelMask != mDstChannelMask) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6898 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6899 | memcpy_by_index_array(dstBuf, mDstChannelCount, |
| 6900 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); |
| 6901 | if (dstBuf == dst) { |
| 6902 | return; // format is the same |
| 6903 | } |
| 6904 | } |
| 6905 | // convert to destination buffer |
| 6906 | const void *convertBuf = mBuf != NULL ? mBuf : src; |
| 6907 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, |
| 6908 | frames * mDstChannelCount); |
| 6909 | } |
| 6910 | |
| 6911 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( |
| 6912 | void *dst, /*not-a-const*/ void *src, size_t frames) |
| 6913 | { |
| 6914 | // src buffer format is ALWAYS float when entering this routine |
| 6915 | if (mIsLegacyUpmix) { |
| 6916 | ; // mono to stereo already handled by resampler |
| 6917 | } else if (mIsLegacyDownmix |
| 6918 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { |
| 6919 | // the resampler outputs stereo for mono input channel (a feature?) |
| 6920 | // must convert to mono |
| 6921 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6922 | (const float *)src, frames); |
| 6923 | } else if (mSrcChannelMask != mDstChannelMask) { |
| 6924 | // convert to mono channel again for channel mask conversion (could be skipped |
| 6925 | // with further optimization). |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6926 | if (mSrcChannelCount == 1) { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6927 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6928 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6929 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6930 | // convert to destination format (in place, OK as float is larger than other types) |
| 6931 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6932 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6933 | frames * mSrcChannelCount); |
| 6934 | } |
| 6935 | // channel convert and save to dst |
| 6936 | memcpy_by_index_array(dst, mDstChannelCount, |
| 6937 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); |
| 6938 | return; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6939 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6940 | // convert to destination format and save to dst |
| 6941 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6942 | frames * mDstChannelCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6943 | } |
| 6944 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6945 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 6946 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6947 | { |
| 6948 | bool reconfig = false; |
| 6949 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6950 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6951 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6952 | audio_format_t reqFormat = mFormat; |
| 6953 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 6954 | // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs). |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6955 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 6956 | |
| 6957 | AudioParameter param = AudioParameter(keyValuePair); |
| 6958 | int value; |
| 6959 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 6960 | // channel count change can be requested. Do we mandate the first client defines the |
| 6961 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 6962 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6963 | samplingRate = value; |
| 6964 | reconfig = true; |
| 6965 | } |
| 6966 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6967 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6968 | status = BAD_VALUE; |
| 6969 | } else { |
| 6970 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6971 | reconfig = true; |
| 6972 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6973 | } |
| 6974 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 6975 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6976 | if (!audio_is_input_channel(mask) || |
| 6977 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6978 | status = BAD_VALUE; |
| 6979 | } else { |
| 6980 | channelMask = mask; |
| 6981 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6982 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6983 | } |
| 6984 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6985 | // do not accept frame count changes if tracks are open as the track buffer |
| 6986 | // size depends on frame count and correct behavior would not be guaranteed |
| 6987 | // if frame count is changed after track creation |
| 6988 | if (mActiveTracks.size() > 0) { |
| 6989 | status = INVALID_OPERATION; |
| 6990 | } else { |
| 6991 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6992 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6993 | } |
| 6994 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6995 | // forward device change to effects that have requested to be |
| 6996 | // aware of attached audio device. |
| 6997 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6998 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6999 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7000 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7001 | // store input device and output device but do not forward output device to audio HAL. |
| 7002 | // Note that status is ignored by the caller for output device |
| 7003 | // (see AudioFlinger::setParameters() |
| 7004 | if (audio_is_output_devices(value)) { |
| 7005 | mOutDevice = value; |
| 7006 | status = BAD_VALUE; |
| 7007 | } else { |
| 7008 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7009 | if (value != AUDIO_DEVICE_NONE) { |
| 7010 | mPrevInDevice = value; |
| 7011 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7012 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7013 | // pre processings |
| 7014 | if (mTracks.size() > 0) { |
| 7015 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7016 | mAudioFlinger->btNrecIsOff(); |
| 7017 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7018 | sp<RecordTrack> track = mTracks[i]; |
| 7019 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7020 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7024 | } |
| 7025 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7026 | mAudioSource != (audio_source_t)value) { |
| 7027 | // forward device change to effects that have requested to be |
| 7028 | // aware of attached audio device. |
| 7029 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7030 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7031 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7032 | mAudioSource = (audio_source_t)value; |
| 7033 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7034 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7035 | if (status == NO_ERROR) { |
| 7036 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7037 | keyValuePair.string()); |
| 7038 | if (status == INVALID_OPERATION) { |
| 7039 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7040 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7041 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7042 | } |
| 7043 | if (reconfig) { |
| 7044 | if (status == BAD_VALUE && |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7045 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && |
| 7046 | audio_is_linear_pcm(reqFormat) && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7047 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7048 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7049 | audio_channel_count_from_in_mask( |
Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 7050 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7051 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7052 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7053 | if (status == NO_ERROR) { |
| 7054 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7055 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7056 | } |
| 7057 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7058 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7059 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7060 | return reconfig; |
| 7061 | } |
| 7062 | |
| 7063 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7064 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7065 | Mutex::Autolock _l(mLock); |
| 7066 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7067 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7068 | } |
| 7069 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7070 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 7071 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7072 | free(s); |
| 7073 | return out_s8; |
| 7074 | } |
| 7075 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7076 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7077 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7078 | |
| 7079 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7080 | |
| 7081 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7082 | case AUDIO_INPUT_OPENED: |
| 7083 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7084 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7085 | desc->mChannelMask = mChannelMask; |
| 7086 | desc->mSamplingRate = mSampleRate; |
| 7087 | desc->mFormat = mFormat; |
| 7088 | desc->mFrameCount = mFrameCount; |
| 7089 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7090 | break; |
| 7091 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7092 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7093 | default: |
| 7094 | break; |
| 7095 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7096 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7097 | } |
| 7098 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7099 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7100 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7101 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 7102 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7103 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7104 | if (mChannelCount > FCC_8) { |
| 7105 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); |
| 7106 | } |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7107 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 7108 | mFormat = mHALFormat; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7109 | if (!audio_is_linear_pcm(mFormat)) { |
| 7110 | ALOGE("HAL format %#x is not linear pcm", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 7111 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 7112 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7113 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 7114 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7115 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7116 | // 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] | 7117 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7118 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7119 | // A larger value should allow more old data to be read after a track calls start(), |
| 7120 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7121 | // |
| 7122 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7123 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7124 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7125 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7126 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7127 | |
| 7128 | // TODO optimize audio capture buffer sizes ... |
| 7129 | // Here we calculate the size of the sliding buffer used as a source |
| 7130 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7131 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7132 | // be better to have it derived from the pipe depth in the long term. |
| 7133 | // The current value is higher than necessary. However it should not add to latency. |
| 7134 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7135 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7136 | size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; |
| 7137 | (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); |
| 7138 | memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7139 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7140 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7141 | // 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] | 7142 | } |
| 7143 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7144 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7145 | { |
| 7146 | Mutex::Autolock _l(mLock); |
| 7147 | if (initCheck() != NO_ERROR) { |
| 7148 | return 0; |
| 7149 | } |
| 7150 | |
| 7151 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 7152 | } |
| 7153 | |
| 7154 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const |
| 7155 | { |
| 7156 | Mutex::Autolock _l(mLock); |
| 7157 | uint32_t result = 0; |
| 7158 | if (getEffectChain_l(sessionId) != 0) { |
| 7159 | result = EFFECT_SESSION; |
| 7160 | } |
| 7161 | |
| 7162 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7163 | if (sessionId == mTracks[i]->sessionId()) { |
| 7164 | result |= TRACK_SESSION; |
| 7165 | break; |
| 7166 | } |
| 7167 | } |
| 7168 | |
| 7169 | return result; |
| 7170 | } |
| 7171 | |
| 7172 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const |
| 7173 | { |
| 7174 | KeyedVector<int, bool> ids; |
| 7175 | Mutex::Autolock _l(mLock); |
| 7176 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7177 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
| 7178 | int sessionId = track->sessionId(); |
| 7179 | if (ids.indexOfKey(sessionId) < 0) { |
| 7180 | ids.add(sessionId, true); |
| 7181 | } |
| 7182 | } |
| 7183 | return ids; |
| 7184 | } |
| 7185 | |
| 7186 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7187 | { |
| 7188 | Mutex::Autolock _l(mLock); |
| 7189 | AudioStreamIn *input = mInput; |
| 7190 | mInput = NULL; |
| 7191 | return input; |
| 7192 | } |
| 7193 | |
| 7194 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 7195 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 7196 | { |
| 7197 | if (mInput == NULL) { |
| 7198 | return NULL; |
| 7199 | } |
| 7200 | return &mInput->stream->common; |
| 7201 | } |
| 7202 | |
| 7203 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7204 | { |
| 7205 | // only one chain per input thread |
| 7206 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7207 | ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7208 | return INVALID_OPERATION; |
| 7209 | } |
| 7210 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7211 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7212 | chain->setInBuffer(NULL); |
| 7213 | chain->setOutBuffer(NULL); |
| 7214 | |
| 7215 | checkSuspendOnAddEffectChain_l(chain); |
| 7216 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7217 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7218 | // just moved them to a new input stream. |
| 7219 | chain->syncHalEffectsState(); |
| 7220 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7221 | mEffectChains.add(chain); |
| 7222 | |
| 7223 | return NO_ERROR; |
| 7224 | } |
| 7225 | |
| 7226 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7227 | { |
| 7228 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7229 | ALOGW_IF(mEffectChains.size() != 1, |
| 7230 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 7231 | chain.get(), mEffectChains.size(), this); |
| 7232 | if (mEffectChains.size() == 1) { |
| 7233 | mEffectChains.removeAt(0); |
| 7234 | } |
| 7235 | return 0; |
| 7236 | } |
| 7237 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7238 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7239 | audio_patch_handle_t *handle) |
| 7240 | { |
| 7241 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7242 | |
| 7243 | // store new device and send to effects |
| 7244 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7245 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7246 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7247 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7248 | } |
| 7249 | |
| 7250 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7251 | // pre processings |
| 7252 | if (mTracks.size() > 0) { |
| 7253 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7254 | mAudioFlinger->btNrecIsOff(); |
| 7255 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7256 | sp<RecordTrack> track = mTracks[i]; |
| 7257 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7258 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 7259 | } |
| 7260 | } |
| 7261 | |
| 7262 | // store new source and send to effects |
| 7263 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7264 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7265 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7266 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7267 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7268 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7269 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7270 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7271 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7272 | status = hwDevice->create_audio_patch(hwDevice, |
| 7273 | patch->num_sources, |
| 7274 | patch->sources, |
| 7275 | patch->num_sinks, |
| 7276 | patch->sinks, |
| 7277 | handle); |
| 7278 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7279 | char *address; |
| 7280 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7281 | address = audio_device_address_to_parameter( |
| 7282 | patch->sources[0].ext.device.type, |
| 7283 | patch->sources[0].ext.device.address); |
| 7284 | } else { |
| 7285 | address = (char *)calloc(1, 1); |
| 7286 | } |
| 7287 | AudioParameter param = AudioParameter(String8(address)); |
| 7288 | free(address); |
| 7289 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
| 7290 | (int)patch->sources[0].ext.device.type); |
| 7291 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 7292 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7293 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7294 | param.toString().string()); |
| 7295 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7296 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7297 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7298 | if (mInDevice != mPrevInDevice) { |
| 7299 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7300 | mPrevInDevice = mInDevice; |
| 7301 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7302 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7303 | return status; |
| 7304 | } |
| 7305 | |
| 7306 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7307 | { |
| 7308 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7309 | |
| 7310 | mInDevice = AUDIO_DEVICE_NONE; |
| 7311 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7312 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 7313 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7314 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 7315 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7316 | AudioParameter param; |
| 7317 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 7318 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7319 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7320 | } |
| 7321 | return status; |
| 7322 | } |
| 7323 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7324 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7325 | { |
| 7326 | Mutex::Autolock _l(mLock); |
| 7327 | mTracks.add(record); |
| 7328 | } |
| 7329 | |
| 7330 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7331 | { |
| 7332 | Mutex::Autolock _l(mLock); |
| 7333 | destroyTrack_l(record); |
| 7334 | } |
| 7335 | |
| 7336 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7337 | { |
| 7338 | ThreadBase::getAudioPortConfig(config); |
| 7339 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7340 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7341 | config->ext.mix.usecase.source = mAudioSource; |
| 7342 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7343 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7344 | } // namespace android |