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 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 74 | #include "AutoPark.h" |
| 75 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 76 | // ---------------------------------------------------------------------------- |
| 77 | |
| 78 | // Note: the following macro is used for extremely verbose logging message. In |
| 79 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 80 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 81 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 82 | // turned on. Do not uncomment the #def below unless you really know what you |
| 83 | // are doing and want to see all of the extremely verbose messages. |
| 84 | //#define VERY_VERY_VERBOSE_LOGGING |
| 85 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 86 | #define ALOGVV ALOGV |
| 87 | #else |
| 88 | #define ALOGVV(a...) do { } while(0) |
| 89 | #endif |
| 90 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 91 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 92 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 93 | template <typename T> |
| 94 | static inline T min(const T& a, const T& b) |
| 95 | { |
| 96 | return a < b ? a : b; |
| 97 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 98 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 99 | #ifndef ARRAY_SIZE |
| 100 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 101 | #endif |
| 102 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | namespace android { |
| 104 | |
| 105 | // retry counts for buffer fill timeout |
| 106 | // 50 * ~20msecs = 1 second |
| 107 | static const int8_t kMaxTrackRetries = 50; |
| 108 | static const int8_t kMaxTrackStartupRetries = 50; |
| 109 | // allow less retry attempts on direct output thread. |
| 110 | // direct outputs can be a scarce resource in audio hardware and should |
| 111 | // be released as quickly as possible. |
| 112 | static const int8_t kMaxTrackRetriesDirect = 2; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 113 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 114 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 115 | |
| 116 | // don't warn about blocked writes or record buffer overflows more often than this |
| 117 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 118 | |
| 119 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 120 | static const int kRecordThreadSleepUs = 5000; |
| 121 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 122 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 123 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 124 | |
| 125 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 126 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 127 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 128 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 129 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 130 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 131 | // FIXME This should be based on experimentally observed scheduling jitter |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 132 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 133 | // maximum normal sink buffer size |
| 134 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 | |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 136 | // minimum capture buffer size in milliseconds to _not_ need a fast capture thread |
| 137 | // FIXME This should be based on experimentally observed scheduling jitter |
| 138 | static const uint32_t kMinNormalCaptureBufferSizeMs = 12; |
| 139 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 140 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 141 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 142 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 143 | // Direct output thread minimum sleep time in idle or active(underrun) state |
| 144 | static const nsecs_t kDirectMinSleepTimeUs = 10000; |
| 145 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 146 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 147 | // Whether to use fast mixer |
| 148 | static const enum { |
| 149 | FastMixer_Never, // never initialize or use: for debugging only |
| 150 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 151 | // normal mixer multiplier is 1 |
| 152 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 153 | // multiplier is calculated based on min & max normal mixer buffer size |
| 154 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 155 | // multiplier is calculated based on min & max normal mixer buffer size |
| 156 | // FIXME for FastMixer_Dynamic: |
| 157 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 158 | // For example, one HAL implementation returns an error from a large write, |
| 159 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 160 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 161 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 162 | } kUseFastMixer = FastMixer_Static; |
| 163 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 164 | // Whether to use fast capture |
| 165 | static const enum { |
| 166 | FastCapture_Never, // never initialize or use: for debugging only |
| 167 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 168 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 169 | } kUseFastCapture = FastCapture_Static; |
| 170 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 171 | // Priorities for requestPriority |
| 172 | static const int kPriorityAudioApp = 2; |
| 173 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 174 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 175 | |
Glenn Kasten | ea38ee7 | 2016-04-18 11:08:01 -0700 | [diff] [blame] | 176 | // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the |
| 177 | // track buffer in shared memory. Zero on input means to use a default value. For fast tracks, |
| 178 | // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 179 | |
| 180 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 181 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 182 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 183 | // The minimum and maximum allowed values |
| 184 | static const int kFastTrackMultiplierMin = 1; |
| 185 | static const int kFastTrackMultiplierMax = 2; |
| 186 | |
| 187 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 188 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 189 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 190 | // See Thread::readOnlyHeap(). |
| 191 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 192 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 193 | // 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] | 194 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 195 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 196 | // ---------------------------------------------------------------------------- |
| 197 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 198 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 199 | |
| 200 | static void sFastTrackMultiplierInit() |
| 201 | { |
| 202 | char value[PROPERTY_VALUE_MAX]; |
| 203 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 204 | char *endptr; |
| 205 | unsigned long ul = strtoul(value, &endptr, 0); |
| 206 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 207 | sFastTrackMultiplier = (int) ul; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ---------------------------------------------------------------------------- |
| 213 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 214 | #ifdef ADD_BATTERY_DATA |
| 215 | // To collect the amplifier usage |
| 216 | static void addBatteryData(uint32_t params) { |
| 217 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 218 | if (service == NULL) { |
| 219 | // it already logged |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | service->addBatteryData(params); |
| 224 | } |
| 225 | #endif |
| 226 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 227 | // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset |
| 228 | struct { |
| 229 | // call when you acquire a partial wakelock |
| 230 | void acquire(const sp<IBinder> &wakeLockToken) { |
| 231 | pthread_mutex_lock(&mLock); |
| 232 | if (wakeLockToken.get() == nullptr) { |
| 233 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 234 | } else { |
| 235 | if (mCount == 0) { |
| 236 | adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME); |
| 237 | } |
| 238 | ++mCount; |
| 239 | } |
| 240 | pthread_mutex_unlock(&mLock); |
| 241 | } |
| 242 | |
| 243 | // call when you release a partial wakelock. |
| 244 | void release(const sp<IBinder> &wakeLockToken) { |
| 245 | if (wakeLockToken.get() == nullptr) { |
| 246 | return; |
| 247 | } |
| 248 | pthread_mutex_lock(&mLock); |
| 249 | if (--mCount < 0) { |
| 250 | ALOGE("negative wakelock count"); |
| 251 | mCount = 0; |
| 252 | } |
| 253 | pthread_mutex_unlock(&mLock); |
| 254 | } |
| 255 | |
| 256 | // retrieves the boottime timebase offset from monotonic. |
| 257 | int64_t getBoottimeOffset() { |
| 258 | pthread_mutex_lock(&mLock); |
| 259 | int64_t boottimeOffset = mBoottimeOffset; |
| 260 | pthread_mutex_unlock(&mLock); |
| 261 | return boottimeOffset; |
| 262 | } |
| 263 | |
| 264 | // Adjusts the timebase offset between TIMEBASE_MONOTONIC |
| 265 | // and the selected timebase. |
| 266 | // Currently only TIMEBASE_BOOTTIME is allowed. |
| 267 | // |
| 268 | // This only needs to be called upon acquiring the first partial wakelock |
| 269 | // after all other partial wakelocks are released. |
| 270 | // |
| 271 | // We do an empirical measurement of the offset rather than parsing |
| 272 | // /proc/timer_list since the latter is not a formal kernel ABI. |
| 273 | static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) { |
| 274 | int clockbase; |
| 275 | switch (timebase) { |
| 276 | case ExtendedTimestamp::TIMEBASE_BOOTTIME: |
| 277 | clockbase = SYSTEM_TIME_BOOTTIME; |
| 278 | break; |
| 279 | default: |
| 280 | LOG_ALWAYS_FATAL("invalid timebase %d", timebase); |
| 281 | break; |
| 282 | } |
| 283 | // try three times to get the clock offset, choose the one |
| 284 | // with the minimum gap in measurements. |
| 285 | const int tries = 3; |
| 286 | nsecs_t bestGap, measured; |
| 287 | for (int i = 0; i < tries; ++i) { |
| 288 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 289 | const nsecs_t tbase = systemTime(clockbase); |
| 290 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 291 | const nsecs_t gap = tmono2 - tmono; |
| 292 | if (i == 0 || gap < bestGap) { |
| 293 | bestGap = gap; |
| 294 | measured = tbase - ((tmono + tmono2) >> 1); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // to avoid micro-adjusting, we don't change the timebase |
| 299 | // unless it is significantly different. |
| 300 | // |
| 301 | // Assumption: It probably takes more than toleranceNs to |
| 302 | // suspend and resume the device. |
| 303 | static int64_t toleranceNs = 10000; // 10 us |
| 304 | if (llabs(*offset - measured) > toleranceNs) { |
| 305 | ALOGV("Adjusting timebase offset old: %lld new: %lld", |
| 306 | (long long)*offset, (long long)measured); |
| 307 | *offset = measured; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | pthread_mutex_t mLock; |
| 312 | int32_t mCount; |
| 313 | int64_t mBoottimeOffset; |
| 314 | } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 315 | |
| 316 | // ---------------------------------------------------------------------------- |
| 317 | // CPU Stats |
| 318 | // ---------------------------------------------------------------------------- |
| 319 | |
| 320 | class CpuStats { |
| 321 | public: |
| 322 | CpuStats(); |
| 323 | void sample(const String8 &title); |
| 324 | #ifdef DEBUG_CPU_USAGE |
| 325 | private: |
| 326 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 327 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 328 | |
| 329 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 330 | |
| 331 | int mCpuNum; // thread's current CPU number |
| 332 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 333 | #endif |
| 334 | }; |
| 335 | |
| 336 | CpuStats::CpuStats() |
| 337 | #ifdef DEBUG_CPU_USAGE |
| 338 | : mCpuNum(-1), mCpukHz(-1) |
| 339 | #endif |
| 340 | { |
| 341 | } |
| 342 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 343 | void CpuStats::sample(const String8 &title |
| 344 | #ifndef DEBUG_CPU_USAGE |
| 345 | __unused |
| 346 | #endif |
| 347 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 348 | #ifdef DEBUG_CPU_USAGE |
| 349 | // get current thread's delta CPU time in wall clock ns |
| 350 | double wcNs; |
| 351 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 352 | |
| 353 | // record sample for wall clock statistics |
| 354 | if (valid) { |
| 355 | mWcStats.sample(wcNs); |
| 356 | } |
| 357 | |
| 358 | // get the current CPU number |
| 359 | int cpuNum = sched_getcpu(); |
| 360 | |
| 361 | // get the current CPU frequency in kHz |
| 362 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 363 | |
| 364 | // check if either CPU number or frequency changed |
| 365 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 366 | mCpuNum = cpuNum; |
| 367 | mCpukHz = cpukHz; |
| 368 | // ignore sample for purposes of cycles |
| 369 | valid = false; |
| 370 | } |
| 371 | |
| 372 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 373 | if (valid && mCpukHz > 0) { |
| 374 | double cycles = wcNs * cpukHz * 0.000001; |
| 375 | mHzStats.sample(cycles); |
| 376 | } |
| 377 | |
| 378 | unsigned n = mWcStats.n(); |
| 379 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 380 | if ((n & 127) == 1) { |
| 381 | long long elapsed = mCpuUsage.elapsed(); |
| 382 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 383 | double perLoop = elapsed / (double) n; |
| 384 | double perLoop100 = perLoop * 0.01; |
| 385 | double perLoop1k = perLoop * 0.001; |
| 386 | double mean = mWcStats.mean(); |
| 387 | double stddev = mWcStats.stddev(); |
| 388 | double minimum = mWcStats.minimum(); |
| 389 | double maximum = mWcStats.maximum(); |
| 390 | double meanCycles = mHzStats.mean(); |
| 391 | double stddevCycles = mHzStats.stddev(); |
| 392 | double minCycles = mHzStats.minimum(); |
| 393 | double maxCycles = mHzStats.maximum(); |
| 394 | mCpuUsage.resetElapsed(); |
| 395 | mWcStats.reset(); |
| 396 | mHzStats.reset(); |
| 397 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 398 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 399 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 400 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 401 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 402 | title.string(), |
| 403 | elapsed * .000000001, n, perLoop * .000001, |
| 404 | mean * .001, |
| 405 | stddev * .001, |
| 406 | minimum * .001, |
| 407 | maximum * .001, |
| 408 | mean / perLoop100, |
| 409 | stddev / perLoop100, |
| 410 | minimum / perLoop100, |
| 411 | maximum / perLoop100, |
| 412 | meanCycles / perLoop1k, |
| 413 | stddevCycles / perLoop1k, |
| 414 | minCycles / perLoop1k, |
| 415 | maxCycles / perLoop1k); |
| 416 | |
| 417 | } |
| 418 | } |
| 419 | #endif |
| 420 | }; |
| 421 | |
| 422 | // ---------------------------------------------------------------------------- |
| 423 | // ThreadBase |
| 424 | // ---------------------------------------------------------------------------- |
| 425 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 426 | // static |
| 427 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 428 | { |
| 429 | switch (type) { |
| 430 | case MIXER: |
| 431 | return "MIXER"; |
| 432 | case DIRECT: |
| 433 | return "DIRECT"; |
| 434 | case DUPLICATING: |
| 435 | return "DUPLICATING"; |
| 436 | case RECORD: |
| 437 | return "RECORD"; |
| 438 | case OFFLOAD: |
| 439 | return "OFFLOAD"; |
| 440 | default: |
| 441 | return "unknown"; |
| 442 | } |
| 443 | } |
| 444 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 445 | String8 devicesToString(audio_devices_t devices) |
| 446 | { |
| 447 | static const struct mapping { |
| 448 | audio_devices_t mDevices; |
| 449 | const char * mString; |
| 450 | } mappingsOut[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 451 | {AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE"}, |
| 452 | {AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER"}, |
| 453 | {AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 454 | {AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE"}, |
| 455 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO"}, |
| 456 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 457 | {AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT"}, |
| 458 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 459 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,"BLUETOOTH_A2DP_HEADPHONES"}, |
| 460 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER"}, |
| 461 | {AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 462 | {AUDIO_DEVICE_OUT_HDMI, "HDMI"}, |
| 463 | {AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET,"ANLG_DOCK_HEADSET"}, |
| 464 | {AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,"DGTL_DOCK_HEADSET"}, |
| 465 | {AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 466 | {AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE"}, |
| 467 | {AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX"}, |
| 468 | {AUDIO_DEVICE_OUT_LINE, "LINE"}, |
| 469 | {AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC"}, |
| 470 | {AUDIO_DEVICE_OUT_SPDIF, "SPDIF"}, |
| 471 | {AUDIO_DEVICE_OUT_FM, "FM"}, |
| 472 | {AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE"}, |
| 473 | {AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE"}, |
| 474 | {AUDIO_DEVICE_OUT_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 475 | {AUDIO_DEVICE_OUT_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 476 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 477 | }, mappingsIn[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 478 | {AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION"}, |
| 479 | {AUDIO_DEVICE_IN_AMBIENT, "AMBIENT"}, |
| 480 | {AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC"}, |
| 481 | {AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET"}, |
| 482 | {AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET"}, |
| 483 | {AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL"}, |
| 484 | {AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL"}, |
| 485 | {AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX"}, |
| 486 | {AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC"}, |
| 487 | {AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX"}, |
| 488 | {AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET"}, |
| 489 | {AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET"}, |
| 490 | {AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY"}, |
| 491 | {AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE"}, |
| 492 | {AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER"}, |
| 493 | {AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER"}, |
| 494 | {AUDIO_DEVICE_IN_LINE, "LINE"}, |
| 495 | {AUDIO_DEVICE_IN_SPDIF, "SPDIF"}, |
| 496 | {AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"}, |
| 497 | {AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK"}, |
| 498 | {AUDIO_DEVICE_IN_IP, "IP"}, |
Eric Laurent | 58545be | 2016-02-22 18:54:20 -0800 | [diff] [blame] | 499 | {AUDIO_DEVICE_IN_BUS, "BUS"}, |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 500 | {AUDIO_DEVICE_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 501 | }; |
| 502 | String8 result; |
| 503 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 504 | const mapping *entry; |
| 505 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 506 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 507 | entry = mappingsIn; |
| 508 | } else { |
| 509 | entry = mappingsOut; |
| 510 | } |
| 511 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 512 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 513 | if (devices & entry->mDevices) { |
| 514 | if (!result.isEmpty()) { |
| 515 | result.append("|"); |
| 516 | } |
| 517 | result.append(entry->mString); |
| 518 | } |
| 519 | } |
| 520 | if (devices & ~allDevices) { |
| 521 | if (!result.isEmpty()) { |
| 522 | result.append("|"); |
| 523 | } |
| 524 | result.appendFormat("0x%X", devices & ~allDevices); |
| 525 | } |
| 526 | if (result.isEmpty()) { |
| 527 | result.append(entry->mString); |
| 528 | } |
| 529 | return result; |
| 530 | } |
| 531 | |
| 532 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 533 | { |
| 534 | static const struct mapping { |
| 535 | audio_input_flags_t mFlag; |
| 536 | const char * mString; |
| 537 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 538 | {AUDIO_INPUT_FLAG_FAST, "FAST"}, |
| 539 | {AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD"}, |
| 540 | {AUDIO_INPUT_FLAG_RAW, "RAW"}, |
| 541 | {AUDIO_INPUT_FLAG_SYNC, "SYNC"}, |
| 542 | {AUDIO_INPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 543 | }; |
| 544 | String8 result; |
| 545 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 546 | const mapping *entry; |
| 547 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 548 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 549 | if (flags & entry->mFlag) { |
| 550 | if (!result.isEmpty()) { |
| 551 | result.append("|"); |
| 552 | } |
| 553 | result.append(entry->mString); |
| 554 | } |
| 555 | } |
| 556 | if (flags & ~allFlags) { |
| 557 | if (!result.isEmpty()) { |
| 558 | result.append("|"); |
| 559 | } |
| 560 | result.appendFormat("0x%X", flags & ~allFlags); |
| 561 | } |
| 562 | if (result.isEmpty()) { |
| 563 | result.append(entry->mString); |
| 564 | } |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 569 | { |
| 570 | static const struct mapping { |
| 571 | audio_output_flags_t mFlag; |
| 572 | const char * mString; |
| 573 | } mappings[] = { |
Glenn Kasten | 818da52 | 2015-12-02 13:53:26 -0800 | [diff] [blame] | 574 | {AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT"}, |
| 575 | {AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY"}, |
| 576 | {AUDIO_OUTPUT_FLAG_FAST, "FAST"}, |
| 577 | {AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER"}, |
| 578 | {AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD,"COMPRESS_OFFLOAD"}, |
| 579 | {AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING"}, |
| 580 | {AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC"}, |
| 581 | {AUDIO_OUTPUT_FLAG_RAW, "RAW"}, |
| 582 | {AUDIO_OUTPUT_FLAG_SYNC, "SYNC"}, |
| 583 | {AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO, "IEC958_NONAUDIO"}, |
| 584 | {AUDIO_OUTPUT_FLAG_NONE, "NONE"}, // must be last |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 585 | }; |
| 586 | String8 result; |
| 587 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 588 | const mapping *entry; |
| 589 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 590 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 591 | if (flags & entry->mFlag) { |
| 592 | if (!result.isEmpty()) { |
| 593 | result.append("|"); |
| 594 | } |
| 595 | result.append(entry->mString); |
| 596 | } |
| 597 | } |
| 598 | if (flags & ~allFlags) { |
| 599 | if (!result.isEmpty()) { |
| 600 | result.append("|"); |
| 601 | } |
| 602 | result.appendFormat("0x%X", flags & ~allFlags); |
| 603 | } |
| 604 | if (result.isEmpty()) { |
| 605 | result.append(entry->mString); |
| 606 | } |
| 607 | return result; |
| 608 | } |
| 609 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 610 | const char *sourceToString(audio_source_t source) |
| 611 | { |
| 612 | switch (source) { |
| 613 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 614 | case AUDIO_SOURCE_MIC: return "mic"; |
| 615 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 616 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 617 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 618 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 619 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 620 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 621 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
rago | 8a397d5 | 2015-12-02 11:27:57 -0800 | [diff] [blame] | 622 | case AUDIO_SOURCE_UNPROCESSED: return "unprocessed"; |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 623 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 624 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 625 | default: return "unknown"; |
| 626 | } |
| 627 | } |
| 628 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 629 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 630 | 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] | 631 | : Thread(false /*canCallJava*/), |
| 632 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 633 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 634 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 635 | // are set by PlaybackThread::readOutputParameters_l() or |
| 636 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 637 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 638 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 639 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 640 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 642 | mDeathRecipient(new PMDeathRecipient(this)), |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 643 | mSystemReady(systemReady), |
| 644 | mNotifiedBatteryStart(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 645 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 646 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | AudioFlinger::ThreadBase::~ThreadBase() |
| 650 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 651 | // 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] | 652 | mConfigEvents.clear(); |
| 653 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 654 | // do not lock the mutex in destructor |
| 655 | releaseWakeLock_l(); |
| 656 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 657 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 658 | binder->unlinkToDeath(mDeathRecipient); |
| 659 | } |
| 660 | } |
| 661 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 662 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 663 | { |
| 664 | status_t status = initCheck(); |
| 665 | if (status == NO_ERROR) { |
| 666 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 667 | } else { |
| 668 | ALOGE("No working audio driver found."); |
| 669 | } |
| 670 | return status; |
| 671 | } |
| 672 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 673 | void AudioFlinger::ThreadBase::exit() |
| 674 | { |
| 675 | ALOGV("ThreadBase::exit"); |
| 676 | // do any cleanup required for exit to succeed |
| 677 | preExit(); |
| 678 | { |
| 679 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 680 | // if (!exitPending()) { |
| 681 | // // context switch from here to exit() |
| 682 | // // exit() calls requestExit(), what exitPending() observes |
| 683 | // // exit() calls signal(), which is dropped since no waiters |
| 684 | // // context switch back from exit() to here |
| 685 | // mWaitWorkCV.wait(...); |
| 686 | // // now thread is hung |
| 687 | // } |
| 688 | AutoMutex lock(mLock); |
| 689 | requestExit(); |
| 690 | mWaitWorkCV.broadcast(); |
| 691 | } |
| 692 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 693 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 694 | requestExitAndWait(); |
| 695 | } |
| 696 | |
| 697 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 698 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 699 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 700 | Mutex::Autolock _l(mLock); |
| 701 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 702 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 703 | } |
| 704 | |
| 705 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 706 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 707 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 708 | { |
| 709 | status_t status = NO_ERROR; |
| 710 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 711 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 712 | event->mWaitStatus = false; |
| 713 | mPendingConfigEvents.add(event); |
| 714 | return status; |
| 715 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 716 | mConfigEvents.add(event); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 717 | ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 718 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 719 | mLock.unlock(); |
| 720 | { |
| 721 | Mutex::Autolock _l(event->mLock); |
| 722 | while (event->mWaitStatus) { |
| 723 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 724 | event->mStatus = TIMED_OUT; |
| 725 | event->mWaitStatus = false; |
| 726 | } |
| 727 | } |
| 728 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 729 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 730 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 731 | return status; |
| 732 | } |
| 733 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 734 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 735 | { |
| 736 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 737 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 741 | 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] | 742 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 743 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 744 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 747 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) |
| 748 | { |
| 749 | Mutex::Autolock _l(mLock); |
| 750 | sendPrioConfigEvent_l(pid, tid, prio); |
| 751 | } |
| 752 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 753 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 754 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 755 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 756 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 757 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 760 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 761 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 762 | { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 763 | sp<ConfigEvent> configEvent; |
| 764 | AudioParameter param(keyValuePair); |
| 765 | int value; |
| 766 | if (param.getInt(String8(AUDIO_PARAMETER_MONO_OUTPUT), value) == NO_ERROR) { |
| 767 | setMasterMono_l(value != 0); |
| 768 | if (param.size() == 1) { |
| 769 | return NO_ERROR; // should be a solo parameter - we don't pass down |
| 770 | } |
| 771 | param.remove(String8(AUDIO_PARAMETER_MONO_OUTPUT)); |
| 772 | configEvent = new SetParameterConfigEvent(param.toString()); |
| 773 | } else { |
| 774 | configEvent = new SetParameterConfigEvent(keyValuePair); |
| 775 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 776 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 779 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 780 | const struct audio_patch *patch, |
| 781 | audio_patch_handle_t *handle) |
| 782 | { |
| 783 | Mutex::Autolock _l(mLock); |
| 784 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 785 | status_t status = sendConfigEvent_l(configEvent); |
| 786 | if (status == NO_ERROR) { |
| 787 | CreateAudioPatchConfigEventData *data = |
| 788 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 789 | *handle = data->mHandle; |
| 790 | } |
| 791 | return status; |
| 792 | } |
| 793 | |
| 794 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 795 | const audio_patch_handle_t handle) |
| 796 | { |
| 797 | Mutex::Autolock _l(mLock); |
| 798 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 799 | return sendConfigEvent_l(configEvent); |
| 800 | } |
| 801 | |
| 802 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 803 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 804 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 805 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 806 | bool configChanged = false; |
| 807 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 808 | while (!mConfigEvents.isEmpty()) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 809 | ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 810 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 811 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 812 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 813 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 814 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 815 | // FIXME Need to understand why this has to be done asynchronously |
| 816 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 817 | true /*asynchronous*/); |
| 818 | if (err != 0) { |
| 819 | 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] | 820 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 821 | } |
| 822 | } break; |
| 823 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 824 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 825 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 826 | } break; |
| 827 | case CFG_EVENT_SET_PARAMETER: { |
| 828 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 829 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 830 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 831 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 832 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 833 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 834 | CreateAudioPatchConfigEventData *data = |
| 835 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 836 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 837 | } break; |
| 838 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 839 | ReleaseAudioPatchConfigEventData *data = |
| 840 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 841 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 842 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 843 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 844 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 845 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 846 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 847 | { |
| 848 | Mutex::Autolock _l(event->mLock); |
| 849 | if (event->mWaitStatus) { |
| 850 | event->mWaitStatus = false; |
| 851 | event->mCond.signal(); |
| 852 | } |
| 853 | } |
| 854 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 855 | } |
| 856 | |
| 857 | if (configChanged) { |
| 858 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 859 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 863 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 864 | const audio_channel_representation_t representation = |
| 865 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 866 | |
| 867 | switch (representation) { |
| 868 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 869 | if (output) { |
| 870 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 871 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 872 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 873 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 874 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 875 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 876 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 877 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 878 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 879 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 880 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 881 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 882 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 883 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 884 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 885 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 886 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 887 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 888 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 889 | } else { |
| 890 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 891 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 892 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 893 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 894 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 895 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 896 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 897 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 898 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 899 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 900 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 901 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 902 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 903 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 904 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 905 | } |
| 906 | const int len = s.length(); |
| 907 | if (len > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 908 | (void) s.lockBuffer(len); // needed? |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 909 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 910 | } |
| 911 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 912 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 913 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 914 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 915 | return s; |
| 916 | default: |
| 917 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 918 | representation, audio_channel_mask_get_bits(mask)); |
| 919 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 920 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 923 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 924 | { |
| 925 | const size_t SIZE = 256; |
| 926 | char buffer[SIZE]; |
| 927 | String8 result; |
| 928 | |
| 929 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 930 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 931 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 934 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 935 | dprintf(fd, " I/O handle: %d\n", mId); |
| 936 | dprintf(fd, " TID: %d\n", getTid()); |
| 937 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 938 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 939 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 940 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 941 | dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 942 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 943 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 944 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | f87c2f5 | 2015-08-21 08:03:57 -0700 | [diff] [blame] | 945 | dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 946 | dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 947 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 948 | size_t numConfig = mConfigEvents.size(); |
| 949 | if (numConfig) { |
| 950 | for (size_t i = 0; i < numConfig; i++) { |
| 951 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 952 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 953 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 954 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 955 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 956 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 957 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 958 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 959 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 960 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 961 | |
| 962 | if (locked) { |
| 963 | mLock.unlock(); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 968 | { |
| 969 | const size_t SIZE = 256; |
| 970 | char buffer[SIZE]; |
| 971 | String8 result; |
| 972 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 973 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 974 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 975 | write(fd, buffer, strlen(buffer)); |
| 976 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 977 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 978 | sp<EffectChain> chain = mEffectChains[i]; |
| 979 | if (chain != 0) { |
| 980 | chain->dump(fd, args); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 985 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 986 | { |
| 987 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 988 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 991 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 992 | { |
| 993 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 994 | case MIXER: |
| 995 | return String16("AudioMix"); |
| 996 | case DIRECT: |
| 997 | return String16("AudioDirectOut"); |
| 998 | case DUPLICATING: |
| 999 | return String16("AudioDup"); |
| 1000 | case RECORD: |
| 1001 | return String16("AudioIn"); |
| 1002 | case OFFLOAD: |
| 1003 | return String16("AudioOffload"); |
| 1004 | default: |
| 1005 | ALOG_ASSERT(false); |
| 1006 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1010 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1011 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1012 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1013 | if (mPowerManager != 0) { |
| 1014 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1015 | status_t status; |
| 1016 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1017 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1018 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1019 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1020 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1021 | uid, |
| 1022 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1023 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 1024 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1025 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 1026 | getWakeLockTag(), |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1027 | String16("audioserver"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1028 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 1029 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1030 | if (status == NO_ERROR) { |
| 1031 | mWakeLockToken = binder; |
| 1032 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1033 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1034 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1035 | |
| 1036 | if (!mNotifiedBatteryStart) { |
| 1037 | BatteryNotifier::getInstance().noteStartAudio(); |
| 1038 | mNotifiedBatteryStart = true; |
| 1039 | } |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1040 | gBoottime.acquire(mWakeLockToken); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1041 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] = |
| 1042 | gBoottime.getBoottimeOffset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 1046 | { |
| 1047 | Mutex::Autolock _l(mLock); |
| 1048 | releaseWakeLock_l(); |
| 1049 | } |
| 1050 | |
| 1051 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 1052 | { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1053 | gBoottime.release(mWakeLockToken); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1054 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1055 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1056 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1057 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 1058 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1059 | } |
| 1060 | mWakeLockToken.clear(); |
| 1061 | } |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 1062 | |
| 1063 | if (mNotifiedBatteryStart) { |
| 1064 | BatteryNotifier::getInstance().noteStopAudio(); |
| 1065 | mNotifiedBatteryStart = false; |
| 1066 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1069 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 1070 | Mutex::Autolock _l(mLock); |
| 1071 | updateWakeLockUids_l(uids); |
| 1072 | } |
| 1073 | |
| 1074 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1075 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1076 | // use checkService() to avoid blocking if power service is not up yet |
| 1077 | sp<IBinder> binder = |
| 1078 | defaultServiceManager()->checkService(String16("power")); |
| 1079 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1080 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1081 | } else { |
| 1082 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 1083 | binder->linkToDeath(mDeathRecipient); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1089 | getPowerManager_l(); |
Andy Hung | 438e757 | 2015-12-14 15:51:17 -0800 | [diff] [blame] | 1090 | if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called. |
| 1091 | if (mSystemReady) { |
| 1092 | ALOGE("no wake lock to update, but system ready!"); |
| 1093 | } else { |
| 1094 | ALOGW("no wake lock to update, system not ready yet"); |
| 1095 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | if (mPowerManager != 0) { |
| 1099 | sp<IBinder> binder = new BBinder(); |
| 1100 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 1101 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 1102 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1103 | ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1107 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 1108 | { |
| 1109 | Mutex::Autolock _l(mLock); |
| 1110 | releaseWakeLock_l(); |
| 1111 | mPowerManager.clear(); |
| 1112 | } |
| 1113 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1114 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1115 | { |
| 1116 | sp<ThreadBase> thread = mThread.promote(); |
| 1117 | if (thread != 0) { |
| 1118 | thread->clearPowerManager(); |
| 1119 | } |
| 1120 | ALOGW("power manager service died !!!"); |
| 1121 | } |
| 1122 | |
| 1123 | void AudioFlinger::ThreadBase::setEffectSuspended( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1124 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1125 | { |
| 1126 | Mutex::Autolock _l(mLock); |
| 1127 | setEffectSuspended_l(type, suspend, sessionId); |
| 1128 | } |
| 1129 | |
| 1130 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1131 | const effect_uuid_t *type, bool suspend, audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1132 | { |
| 1133 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1134 | if (chain != 0) { |
| 1135 | if (type != NULL) { |
| 1136 | chain->setEffectSuspended_l(type, suspend); |
| 1137 | } else { |
| 1138 | chain->setEffectSuspendedAll_l(suspend); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1143 | } |
| 1144 | |
| 1145 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1146 | { |
| 1147 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1148 | if (index < 0) { |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1153 | mSuspendedSessions.valueAt(index); |
| 1154 | |
| 1155 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1156 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1157 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1158 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1159 | chain->setEffectSuspendedAll_l(true); |
| 1160 | } else { |
| 1161 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1162 | desc->mType.timeLow); |
| 1163 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1170 | bool suspend, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1171 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1172 | { |
| 1173 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1174 | |
| 1175 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1176 | |
| 1177 | if (suspend) { |
| 1178 | if (index >= 0) { |
| 1179 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1180 | } else { |
| 1181 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1182 | } |
| 1183 | } else { |
| 1184 | if (index < 0) { |
| 1185 | return; |
| 1186 | } |
| 1187 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | int key = EffectChain::kKeyForSuspendAll; |
| 1192 | if (type != NULL) { |
| 1193 | key = type->timeLow; |
| 1194 | } |
| 1195 | index = sessionEffects.indexOfKey(key); |
| 1196 | |
| 1197 | sp<SuspendedSessionDesc> desc; |
| 1198 | if (suspend) { |
| 1199 | if (index >= 0) { |
| 1200 | desc = sessionEffects.valueAt(index); |
| 1201 | } else { |
| 1202 | desc = new SuspendedSessionDesc(); |
| 1203 | if (type != NULL) { |
| 1204 | desc->mType = *type; |
| 1205 | } |
| 1206 | sessionEffects.add(key, desc); |
| 1207 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1208 | } |
| 1209 | desc->mRefCount++; |
| 1210 | } else { |
| 1211 | if (index < 0) { |
| 1212 | return; |
| 1213 | } |
| 1214 | desc = sessionEffects.valueAt(index); |
| 1215 | if (--desc->mRefCount == 0) { |
| 1216 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1217 | sessionEffects.removeItemsAt(index); |
| 1218 | if (sessionEffects.isEmpty()) { |
| 1219 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1220 | sessionId); |
| 1221 | mSuspendedSessions.removeItem(sessionId); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | if (!sessionEffects.isEmpty()) { |
| 1226 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1231 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1232 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1233 | { |
| 1234 | Mutex::Autolock _l(mLock); |
| 1235 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1236 | } |
| 1237 | |
| 1238 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1239 | bool enabled, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1240 | audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1241 | { |
| 1242 | if (mType != RECORD) { |
| 1243 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1244 | // another session. This gives the priority to well behaved effect control panels |
| 1245 | // and applications not using global effects. |
| 1246 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1247 | // global effects |
| 1248 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1249 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1254 | if (chain != 0) { |
| 1255 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1259 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1260 | status_t AudioFlinger::RecordThread::checkEffectCompatibility_l( |
| 1261 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1262 | { |
| 1263 | // No global effect sessions on record threads |
| 1264 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1265 | ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s", |
| 1266 | desc->name, mThreadName); |
| 1267 | return BAD_VALUE; |
| 1268 | } |
| 1269 | // only pre processing effects on record thread |
| 1270 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1271 | ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s", |
| 1272 | desc->name, mThreadName); |
| 1273 | return BAD_VALUE; |
| 1274 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1275 | |
| 1276 | // always allow effects without processing load or latency |
| 1277 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1278 | return NO_ERROR; |
| 1279 | } |
| 1280 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1281 | audio_input_flags_t flags = mInput->flags; |
| 1282 | if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) { |
| 1283 | if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 1284 | ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode", |
| 1285 | desc->name, mThreadName); |
| 1286 | return BAD_VALUE; |
| 1287 | } |
| 1288 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1289 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode", |
| 1290 | desc->name, mThreadName); |
| 1291 | return BAD_VALUE; |
| 1292 | } |
| 1293 | } |
| 1294 | return NO_ERROR; |
| 1295 | } |
| 1296 | |
| 1297 | // checkEffectCompatibility_l() must be called with ThreadBase::mLock held |
| 1298 | status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l( |
| 1299 | const effect_descriptor_t *desc, audio_session_t sessionId) |
| 1300 | { |
| 1301 | // no preprocessing on playback threads |
| 1302 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) { |
| 1303 | ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback" |
| 1304 | " thread %s", desc->name, mThreadName); |
| 1305 | return BAD_VALUE; |
| 1306 | } |
| 1307 | |
| 1308 | switch (mType) { |
| 1309 | case MIXER: { |
| 1310 | // Reject any effect on mixer multichannel sinks. |
| 1311 | // TODO: fix both format and multichannel issues with effects. |
| 1312 | if (mChannelCount != FCC_2) { |
| 1313 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER" |
| 1314 | " thread %s", desc->name, mChannelCount, mThreadName); |
| 1315 | return BAD_VALUE; |
| 1316 | } |
| 1317 | audio_output_flags_t flags = mOutput->flags; |
| 1318 | if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) { |
| 1319 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1320 | // global effects are applied only to non fast tracks if they are SW |
| 1321 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1322 | break; |
| 1323 | } |
| 1324 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 1325 | // only post processing on output stage session |
| 1326 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) { |
| 1327 | ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed" |
| 1328 | " on output stage session", desc->name); |
| 1329 | return BAD_VALUE; |
| 1330 | } |
| 1331 | } else { |
| 1332 | // no restriction on effects applied on non fast tracks |
| 1333 | if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) { |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 1337 | |
| 1338 | // always allow effects without processing load or latency |
| 1339 | if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) { |
| 1340 | break; |
| 1341 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1342 | if (flags & AUDIO_OUTPUT_FLAG_RAW) { |
| 1343 | ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode", |
| 1344 | desc->name); |
| 1345 | return BAD_VALUE; |
| 1346 | } |
| 1347 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) { |
| 1348 | ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread" |
| 1349 | " in fast mode", desc->name); |
| 1350 | return BAD_VALUE; |
| 1351 | } |
| 1352 | } |
| 1353 | } break; |
| 1354 | case OFFLOAD: |
Jean-Michel Trivi | 773ee95 | 2016-07-11 16:53:18 -0700 | [diff] [blame] | 1355 | // nothing actionable on offload threads, if the effect: |
| 1356 | // - is offloadable: the effect can be created |
| 1357 | // - is NOT offloadable: the effect should still be created, but EffectHandle::enable() |
| 1358 | // will take care of invalidating the tracks of the thread |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1359 | break; |
| 1360 | case DIRECT: |
| 1361 | // Reject any effect on Direct output threads for now, since the format of |
| 1362 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1363 | ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s", |
| 1364 | desc->name, mThreadName); |
| 1365 | return BAD_VALUE; |
| 1366 | case DUPLICATING: |
| 1367 | // Reject any effect on mixer multichannel sinks. |
| 1368 | // TODO: fix both format and multichannel issues with effects. |
| 1369 | if (mChannelCount != FCC_2) { |
| 1370 | ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)" |
| 1371 | " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName); |
| 1372 | return BAD_VALUE; |
| 1373 | } |
| 1374 | if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) { |
| 1375 | ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING" |
| 1376 | " thread %s", desc->name, mThreadName); |
| 1377 | return BAD_VALUE; |
| 1378 | } |
| 1379 | if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 1380 | ALOGW("checkEffectCompatibility_l(): post processing effect %s on" |
| 1381 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1382 | return BAD_VALUE; |
| 1383 | } |
| 1384 | if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) { |
| 1385 | ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on" |
| 1386 | " DUPLICATING thread %s", desc->name, mThreadName); |
| 1387 | return BAD_VALUE; |
| 1388 | } |
| 1389 | break; |
| 1390 | default: |
| 1391 | LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType); |
| 1392 | } |
| 1393 | |
| 1394 | return NO_ERROR; |
| 1395 | } |
| 1396 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1397 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1398 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1399 | const sp<AudioFlinger::Client>& client, |
| 1400 | const sp<IEffectClient>& effectClient, |
| 1401 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1402 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1403 | effect_descriptor_t *desc, |
| 1404 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1405 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1406 | { |
| 1407 | sp<EffectModule> effect; |
| 1408 | sp<EffectHandle> handle; |
| 1409 | status_t lStatus; |
| 1410 | sp<EffectChain> chain; |
| 1411 | bool chainCreated = false; |
| 1412 | bool effectCreated = false; |
| 1413 | bool effectRegistered = false; |
| 1414 | |
| 1415 | lStatus = initCheck(); |
| 1416 | if (lStatus != NO_ERROR) { |
| 1417 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1418 | goto Exit; |
| 1419 | } |
| 1420 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1421 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1422 | |
| 1423 | { // scope for mLock |
| 1424 | Mutex::Autolock _l(mLock); |
| 1425 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1426 | lStatus = checkEffectCompatibility_l(desc, sessionId); |
| 1427 | if (lStatus != NO_ERROR) { |
| 1428 | goto Exit; |
| 1429 | } |
| 1430 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1431 | // check for existing effect chain with the requested audio session |
| 1432 | chain = getEffectChain_l(sessionId); |
| 1433 | if (chain == 0) { |
| 1434 | // create a new chain for this session |
| 1435 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1436 | chain = new EffectChain(this, sessionId); |
| 1437 | addEffectChain_l(chain); |
| 1438 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1439 | chainCreated = true; |
| 1440 | } else { |
| 1441 | effect = chain->getEffectFromDesc_l(desc); |
| 1442 | } |
| 1443 | |
| 1444 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1445 | |
| 1446 | if (effect == 0) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1447 | audio_unique_id_t id = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1448 | // Check CPU and memory usage |
| 1449 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1450 | if (lStatus != NO_ERROR) { |
| 1451 | goto Exit; |
| 1452 | } |
| 1453 | effectRegistered = true; |
| 1454 | // create a new effect module if none present in the chain |
| 1455 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1456 | lStatus = effect->status(); |
| 1457 | if (lStatus != NO_ERROR) { |
| 1458 | goto Exit; |
| 1459 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1460 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1461 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1462 | lStatus = chain->addEffect_l(effect); |
| 1463 | if (lStatus != NO_ERROR) { |
| 1464 | goto Exit; |
| 1465 | } |
| 1466 | effectCreated = true; |
| 1467 | |
| 1468 | effect->setDevice(mOutDevice); |
| 1469 | effect->setDevice(mInDevice); |
| 1470 | effect->setMode(mAudioFlinger->getMode()); |
| 1471 | effect->setAudioSource(mAudioSource); |
| 1472 | } |
| 1473 | // create effect handle and connect it to effect module |
| 1474 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1475 | lStatus = handle->initCheck(); |
| 1476 | if (lStatus == OK) { |
| 1477 | lStatus = effect->addHandle(handle.get()); |
| 1478 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1479 | if (enabled != NULL) { |
| 1480 | *enabled = (int)effect->isEnabled(); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | Exit: |
| 1485 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1486 | Mutex::Autolock _l(mLock); |
| 1487 | if (effectCreated) { |
| 1488 | chain->removeEffect_l(effect); |
| 1489 | } |
| 1490 | if (effectRegistered) { |
| 1491 | AudioSystem::unregisterEffect(effect->id()); |
| 1492 | } |
| 1493 | if (chainCreated) { |
| 1494 | removeEffectChain_l(chain); |
| 1495 | } |
| 1496 | handle.clear(); |
| 1497 | } |
| 1498 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1499 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1500 | return handle; |
| 1501 | } |
| 1502 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1503 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId, |
| 1504 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1505 | { |
| 1506 | Mutex::Autolock _l(mLock); |
| 1507 | return getEffect_l(sessionId, effectId); |
| 1508 | } |
| 1509 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1510 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId, |
| 1511 | int effectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1512 | { |
| 1513 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1514 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1515 | } |
| 1516 | |
| 1517 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1518 | // PlaybackThread::mLock held |
| 1519 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1520 | { |
| 1521 | // check for existing effect chain with the requested audio session |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1522 | audio_session_t sessionId = effect->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1523 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1524 | bool chainCreated = false; |
| 1525 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1526 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1527 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1528 | this, effect->desc().name, effect->desc().flags); |
| 1529 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1530 | if (chain == 0) { |
| 1531 | // create a new chain for this session |
| 1532 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1533 | chain = new EffectChain(this, sessionId); |
| 1534 | addEffectChain_l(chain); |
| 1535 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1536 | chainCreated = true; |
| 1537 | } |
| 1538 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1539 | |
| 1540 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1541 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1542 | this, effect->desc().name, chain.get()); |
| 1543 | return BAD_VALUE; |
| 1544 | } |
| 1545 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1546 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1547 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1548 | status_t status = chain->addEffect_l(effect); |
| 1549 | if (status != NO_ERROR) { |
| 1550 | if (chainCreated) { |
| 1551 | removeEffectChain_l(chain); |
| 1552 | } |
| 1553 | return status; |
| 1554 | } |
| 1555 | |
| 1556 | effect->setDevice(mOutDevice); |
| 1557 | effect->setDevice(mInDevice); |
| 1558 | effect->setMode(mAudioFlinger->getMode()); |
| 1559 | effect->setAudioSource(mAudioSource); |
| 1560 | return NO_ERROR; |
| 1561 | } |
| 1562 | |
| 1563 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1564 | |
| 1565 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1566 | effect_descriptor_t desc = effect->desc(); |
| 1567 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1568 | detachAuxEffect_l(effect->id()); |
| 1569 | } |
| 1570 | |
| 1571 | sp<EffectChain> chain = effect->chain().promote(); |
| 1572 | if (chain != 0) { |
| 1573 | // remove effect chain if removing last effect |
| 1574 | if (chain->removeEffect_l(effect) == 0) { |
| 1575 | removeEffectChain_l(chain); |
| 1576 | } |
| 1577 | } else { |
| 1578 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1583 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1584 | { |
| 1585 | effectChains = mEffectChains; |
| 1586 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1587 | mEffectChains[i]->lock(); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1592 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1593 | { |
| 1594 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1595 | effectChains[i]->unlock(); |
| 1596 | } |
| 1597 | } |
| 1598 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1599 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1600 | { |
| 1601 | Mutex::Autolock _l(mLock); |
| 1602 | return getEffectChain_l(sessionId); |
| 1603 | } |
| 1604 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1605 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId) |
| 1606 | const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1607 | { |
| 1608 | size_t size = mEffectChains.size(); |
| 1609 | for (size_t i = 0; i < size; i++) { |
| 1610 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1611 | return mEffectChains[i]; |
| 1612 | } |
| 1613 | } |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1618 | { |
| 1619 | Mutex::Autolock _l(mLock); |
| 1620 | size_t size = mEffectChains.size(); |
| 1621 | for (size_t i = 0; i < size; i++) { |
| 1622 | mEffectChains[i]->setMode_l(mode); |
| 1623 | } |
| 1624 | } |
| 1625 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1626 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1627 | { |
| 1628 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1629 | config->ext.mix.handle = mId; |
| 1630 | config->sample_rate = mSampleRate; |
| 1631 | config->format = mFormat; |
| 1632 | config->channel_mask = mChannelMask; |
| 1633 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1634 | AUDIO_PORT_CONFIG_FORMAT; |
| 1635 | } |
| 1636 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1637 | void AudioFlinger::ThreadBase::systemReady() |
| 1638 | { |
| 1639 | Mutex::Autolock _l(mLock); |
| 1640 | if (mSystemReady) { |
| 1641 | return; |
| 1642 | } |
| 1643 | mSystemReady = true; |
| 1644 | |
| 1645 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1646 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1647 | } |
| 1648 | mPendingConfigEvents.clear(); |
| 1649 | } |
| 1650 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1651 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1652 | // ---------------------------------------------------------------------------- |
| 1653 | // Playback |
| 1654 | // ---------------------------------------------------------------------------- |
| 1655 | |
| 1656 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1657 | AudioStreamOut* output, |
| 1658 | audio_io_handle_t id, |
| 1659 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1660 | type_t type, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1661 | bool systemReady) |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1662 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1663 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1664 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1665 | mMixerBuffer(NULL), |
| 1666 | mMixerBufferSize(0), |
| 1667 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1668 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1669 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1670 | mEffectBuffer(NULL), |
| 1671 | mEffectBufferSize(0), |
| 1672 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1673 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1674 | mSuspended(0), mBytesWritten(0), |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1675 | mFramesWritten(0), |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1676 | mSuspendedFrames(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1677 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1678 | // mStreamTypes[] initialized in constructor body |
| 1679 | mOutput(output), |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 1680 | mLastWriteTime(-1), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1681 | mMixerStatus(MIXER_IDLE), |
| 1682 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1683 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1684 | mBytesRemaining(0), |
| 1685 | mCurrentWriteLength(0), |
| 1686 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1687 | mWriteAckSequence(0), |
| 1688 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1689 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1690 | mScreenState(AudioFlinger::mScreenState), |
| 1691 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1692 | mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1693 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1694 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1695 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1696 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1697 | |
| 1698 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1699 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1700 | // parameter. |
| 1701 | // |
| 1702 | // If the HAL we are using has support for master volume or master mute, |
| 1703 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1704 | // and the mute set to false). |
| 1705 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1706 | mMasterMute = audioFlinger->masterMute_l(); |
| 1707 | if (mOutput && mOutput->audioHwDev) { |
| 1708 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1709 | mMasterVolume = 1.0; |
| 1710 | } |
| 1711 | |
| 1712 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1713 | mMasterMute = false; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1717 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1719 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1720 | 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] | 1721 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1722 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1723 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1724 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1728 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1729 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1730 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1731 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1732 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1736 | { |
| 1737 | dumpInternals(fd, args); |
| 1738 | dumpTracks(fd, args); |
| 1739 | dumpEffectChains(fd, args); |
| 1740 | } |
| 1741 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1742 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1743 | { |
| 1744 | const size_t SIZE = 256; |
| 1745 | char buffer[SIZE]; |
| 1746 | String8 result; |
| 1747 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1748 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1749 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1750 | const stream_type_t *st = &mStreamTypes[i]; |
| 1751 | if (i > 0) { |
| 1752 | result.appendFormat(", "); |
| 1753 | } |
| 1754 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1755 | if (st->mute) { |
| 1756 | result.append("M"); |
| 1757 | } |
| 1758 | } |
| 1759 | result.append("\n"); |
| 1760 | write(fd, result.string(), result.length()); |
| 1761 | result.clear(); |
| 1762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1763 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1764 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1765 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1766 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1767 | |
| 1768 | size_t numtracks = mTracks.size(); |
| 1769 | size_t numactive = mActiveTracks.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1770 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1771 | size_t numactiveseen = 0; |
| 1772 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1773 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1774 | Track::appendDumpHeader(result); |
| 1775 | for (size_t i = 0; i < numtracks; ++i) { |
| 1776 | sp<Track> track = mTracks[i]; |
| 1777 | if (track != 0) { |
| 1778 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1779 | if (active) { |
| 1780 | numactiveseen++; |
| 1781 | } |
| 1782 | track->dump(buffer, SIZE, active); |
| 1783 | result.append(buffer); |
| 1784 | } |
| 1785 | } |
| 1786 | } else { |
| 1787 | result.append("\n"); |
| 1788 | } |
| 1789 | if (numactiveseen != numactive) { |
| 1790 | // some tracks in the active list were not in the tracks list |
| 1791 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1792 | " not in the track list\n"); |
| 1793 | result.append(buffer); |
| 1794 | Track::appendDumpHeader(result); |
| 1795 | for (size_t i = 0; i < numactive; ++i) { |
| 1796 | sp<Track> track = mActiveTracks[i].promote(); |
| 1797 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1798 | track->dump(buffer, SIZE, true); |
| 1799 | result.append(buffer); |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1808 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1809 | 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] | 1810 | |
| 1811 | dumpBase(fd, args); |
| 1812 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1813 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1814 | dprintf(fd, " Last write occurred (msecs): %llu\n", |
| 1815 | (unsigned long long) ns2ms(systemTime() - mLastWriteTime)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1816 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1817 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1818 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1819 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1820 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1821 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1822 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1823 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 1824 | dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1825 | AudioStreamOut *output = mOutput; |
| 1826 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1827 | String8 flagsAsString = outputFlagsToString(flags); |
| 1828 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Andy Hung | 2c45393 | 2016-09-21 12:55:15 -0700 | [diff] [blame] | 1829 | dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten); |
| 1830 | dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames); |
| 1831 | if (mPipeSink.get() != nullptr) { |
| 1832 | dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten()); |
| 1833 | } |
| 1834 | if (output != nullptr) { |
| 1835 | dprintf(fd, " Hal stream dump:\n"); |
| 1836 | (void)output->stream->common.dump(&output->stream->common, fd); |
| 1837 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1841 | |
| 1842 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1843 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1844 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // ThreadBase virtuals |
| 1848 | void AudioFlinger::PlaybackThread::preExit() |
| 1849 | { |
| 1850 | ALOGV(" preExit()"); |
| 1851 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1852 | // converted to use audio HAL extensions required to support tunneling |
| 1853 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1854 | } |
| 1855 | |
| 1856 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1857 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1858 | const sp<AudioFlinger::Client>& client, |
| 1859 | audio_stream_type_t streamType, |
| 1860 | uint32_t sampleRate, |
| 1861 | audio_format_t format, |
| 1862 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1863 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1864 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1865 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1866 | audio_output_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1868 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1869 | status_t *status) |
| 1870 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1871 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1872 | sp<Track> track; |
| 1873 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1874 | audio_output_flags_t outputFlags = mOutput->flags; |
| 1875 | |
| 1876 | // special case for FAST flag considered OK if fast mixer is present |
| 1877 | if (hasFastMixer()) { |
| 1878 | outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
| 1879 | } |
| 1880 | |
| 1881 | // Check if requested flags are compatible with output stream flags |
| 1882 | if ((*flags & outputFlags) != *flags) { |
| 1883 | ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)", |
| 1884 | *flags, outputFlags); |
| 1885 | *flags = (audio_output_flags_t)(*flags & outputFlags); |
| 1886 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1887 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1888 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1889 | if (*flags & AUDIO_OUTPUT_FLAG_FAST) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1890 | if ( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1891 | // PCM data |
| 1892 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1893 | // TODO: extract as a data library function that checks that a computationally |
| 1894 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1895 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1896 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1897 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1898 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1899 | // hardware sample rate |
| 1900 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1901 | // normal mixer has an associated fast mixer |
| 1902 | hasFastMixer() && |
| 1903 | // there are sufficient fast track slots available |
| 1904 | (mFastTrackAvailMask != 0) |
| 1905 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1906 | // FIXME add a permission test also? |
| 1907 | ) { |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1908 | // static tracks can have any nonzero framecount, streaming tracks check against minimum. |
| 1909 | if (sharedBuffer == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1910 | // read the fast track multiplier property the first time it is needed |
| 1911 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1912 | if (ok != 0) { |
| 1913 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1914 | } |
Andy Hung | e0a269a | 2016-03-23 15:13:42 -0700 | [diff] [blame] | 1915 | frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1916 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1917 | |
| 1918 | // check compatibility with audio effects. |
| 1919 | { // scope for mLock |
| 1920 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame^] | 1921 | for (audio_session_t session : { |
| 1922 | AUDIO_SESSION_OUTPUT_STAGE, |
| 1923 | AUDIO_SESSION_OUTPUT_MIX, |
| 1924 | sessionId, |
| 1925 | }) { |
| 1926 | sp<EffectChain> chain = getEffectChain_l(session); |
| 1927 | if (chain.get() != nullptr) { |
| 1928 | audio_output_flags_t old = *flags; |
| 1929 | chain->checkOutputFlagCompatibility(flags); |
| 1930 | if (old != *flags) { |
| 1931 | ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x", |
| 1932 | (int)session, (int)old, (int)*flags); |
| 1933 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 1937 | ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1938 | "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 1939 | frameCount, mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1940 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1941 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu " |
| 1942 | "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1943 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1944 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1945 | sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1946 | audio_is_linear_pcm(format), |
| 1947 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1948 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | // For normal PCM streaming tracks, update minimum frame count. |
| 1952 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1953 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1954 | // This is probably too conservative, but legacy application code may depend on it. |
| 1955 | // If you change this calculation, also review the start threshold which is related. |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1956 | if (!(*flags & AUDIO_OUTPUT_FLAG_FAST) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1957 | && audio_has_proportional_frames(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1958 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1959 | // TODO: Move to a common library |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1960 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1961 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1962 | if (minBufCount < 2) { |
| 1963 | minBufCount = 2; |
| 1964 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1965 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1966 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1967 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1968 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1969 | sampleRate, mNormalFrameCount, |
| 1970 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1971 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1972 | frameCount = minFrameCount; |
| 1973 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1974 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1975 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1976 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1977 | switch (mType) { |
| 1978 | |
| 1979 | case DIRECT: |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1980 | 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] | 1981 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1982 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1983 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1984 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1985 | lStatus = BAD_VALUE; |
| 1986 | goto Exit; |
| 1987 | } |
| 1988 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1989 | break; |
| 1990 | |
| 1991 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1992 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1993 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1994 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1995 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1996 | lStatus = BAD_VALUE; |
| 1997 | goto Exit; |
| 1998 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1999 | break; |
| 2000 | |
| 2001 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 2002 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 2003 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 2004 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2005 | format, mOutput, mFormat); |
| 2006 | lStatus = BAD_VALUE; |
| 2007 | goto Exit; |
| 2008 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 2009 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2010 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 2011 | lStatus = BAD_VALUE; |
| 2012 | goto Exit; |
| 2013 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 2014 | break; |
| 2015 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | lStatus = initCheck(); |
| 2019 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 2020 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2021 | goto Exit; |
| 2022 | } |
| 2023 | |
| 2024 | { // scope for mLock |
| 2025 | Mutex::Autolock _l(mLock); |
| 2026 | |
| 2027 | // all tracks in same audio session must share the same routing strategy otherwise |
| 2028 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 2029 | // manager |
| 2030 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 2031 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2032 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2033 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2034 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 2035 | if (sessionId == t->sessionId() && strategy != actual) { |
| 2036 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 2037 | strategy, actual); |
| 2038 | lStatus = BAD_VALUE; |
| 2039 | goto Exit; |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2044 | track = new Track(this, client, streamType, sampleRate, format, |
| 2045 | channelMask, frameCount, NULL, sharedBuffer, |
| 2046 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2047 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 2048 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 2049 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 2050 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2051 | // 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] | 2052 | goto Exit; |
| 2053 | } |
| 2054 | mTracks.add(track); |
| 2055 | |
| 2056 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 2057 | if (chain != 0) { |
| 2058 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 2059 | track->setMainBuffer(chain->inBuffer()); |
| 2060 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 2061 | chain->incTrackCnt(); |
| 2062 | } |
| 2063 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2064 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2065 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2066 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 2067 | // so ask activity manager to do this on our behalf |
| 2068 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | lStatus = NO_ERROR; |
| 2073 | |
| 2074 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2075 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2076 | return track; |
| 2077 | } |
| 2078 | |
| 2079 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 2080 | { |
| 2081 | return latency; |
| 2082 | } |
| 2083 | |
| 2084 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 2085 | { |
| 2086 | Mutex::Autolock _l(mLock); |
| 2087 | return latency_l(); |
| 2088 | } |
| 2089 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 2090 | { |
| 2091 | if (initCheck() == NO_ERROR) { |
| 2092 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 2093 | } else { |
| 2094 | return 0; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 2099 | { |
| 2100 | Mutex::Autolock _l(mLock); |
| 2101 | // Don't apply master volume in SW if our HAL can do it for us. |
| 2102 | if (mOutput && mOutput->audioHwDev && |
| 2103 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 2104 | mMasterVolume = 1.0; |
| 2105 | } else { |
| 2106 | mMasterVolume = value; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 2111 | { |
| 2112 | Mutex::Autolock _l(mLock); |
| 2113 | // Don't apply master mute in SW if our HAL can do it for us. |
| 2114 | if (mOutput && mOutput->audioHwDev && |
| 2115 | mOutput->audioHwDev->canSetMasterMute()) { |
| 2116 | mMasterMute = false; |
| 2117 | } else { |
| 2118 | mMasterMute = muted; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 2123 | { |
| 2124 | Mutex::Autolock _l(mLock); |
| 2125 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2126 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 2130 | { |
| 2131 | Mutex::Autolock _l(mLock); |
| 2132 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2133 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 2137 | { |
| 2138 | Mutex::Autolock _l(mLock); |
| 2139 | return mStreamTypes[stream].volume; |
| 2140 | } |
| 2141 | |
| 2142 | // addTrack_l() must be called with ThreadBase::mLock held |
| 2143 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 2144 | { |
| 2145 | status_t status = ALREADY_EXISTS; |
| 2146 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2147 | if (mActiveTracks.indexOf(track) < 0) { |
| 2148 | // the track is newly added, make sure it fills up all its |
| 2149 | // buffers before playing. This is to ensure the client will |
| 2150 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2151 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2152 | TrackBase::track_state state = track->mState; |
| 2153 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2154 | status = AudioSystem::startOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2155 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2156 | mLock.lock(); |
| 2157 | // abort track was stopped/paused while we released the lock |
| 2158 | if (state != track->mState) { |
| 2159 | if (status == NO_ERROR) { |
| 2160 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2161 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2162 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2163 | mLock.lock(); |
| 2164 | } |
| 2165 | return INVALID_OPERATION; |
| 2166 | } |
| 2167 | // abort if start is rejected by audio policy manager |
| 2168 | if (status != NO_ERROR) { |
| 2169 | return PERMISSION_DENIED; |
| 2170 | } |
| 2171 | #ifdef ADD_BATTERY_DATA |
| 2172 | // to track the speaker usage |
| 2173 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 2174 | #endif |
| 2175 | } |
| 2176 | |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2177 | // set retry count for buffer fill |
| 2178 | if (track->isOffloaded()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2179 | if (track->isStopping_1()) { |
| 2180 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 2181 | } else { |
| 2182 | track->mRetryCount = kMaxTrackStartupRetriesOffload; |
| 2183 | } |
| 2184 | track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2185 | } else { |
| 2186 | track->mRetryCount = kMaxTrackStartupRetries; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 2187 | track->mFillingUpStatus = |
| 2188 | track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2189 | } |
| 2190 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2191 | track->mResetDone = false; |
| 2192 | track->mPresentationCompleteFrames = 0; |
| 2193 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2194 | mWakeLockUids.add(track->uid()); |
| 2195 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2196 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 2197 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2198 | if (chain != 0) { |
| 2199 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 2200 | track->sessionId()); |
| 2201 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | status = NO_ERROR; |
| 2205 | } |
| 2206 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 2207 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2208 | return status; |
| 2209 | } |
| 2210 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2211 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2212 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2213 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2214 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2215 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 2216 | track->mState = TrackBase::STOPPED; |
| 2217 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2218 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2219 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2220 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2221 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2222 | |
| 2223 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 2227 | { |
| 2228 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 2229 | mTracks.remove(track); |
| 2230 | deleteTrackName_l(track->name()); |
| 2231 | // redundant as track is about to be destroyed, for dumpsys only |
| 2232 | track->mName = -1; |
| 2233 | if (track->isFastTrack()) { |
| 2234 | int index = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 2235 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2236 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 2237 | mFastTrackAvailMask |= 1 << index; |
| 2238 | // redundant as track is about to be destroyed, for dumpsys only |
| 2239 | track->mFastIndex = -1; |
| 2240 | } |
| 2241 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2242 | if (chain != 0) { |
| 2243 | chain->decTrackCnt(); |
| 2244 | } |
| 2245 | } |
| 2246 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2247 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2248 | { |
| 2249 | // Thread could be blocked waiting for async |
| 2250 | // so signal it to handle state changes immediately |
| 2251 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 2252 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 2253 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2254 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2257 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 2258 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2259 | Mutex::Autolock _l(mLock); |
| 2260 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2261 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 2264 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 2265 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2266 | free(s); |
| 2267 | return out_s8; |
| 2268 | } |
| 2269 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2270 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2271 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2272 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2273 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2274 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2275 | |
| 2276 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2277 | case AUDIO_OUTPUT_OPENED: |
| 2278 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2279 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2280 | desc->mChannelMask = mChannelMask; |
| 2281 | desc->mSamplingRate = mSampleRate; |
| 2282 | desc->mFormat = mFormat; |
| 2283 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2284 | // AudioFlinger::frameCount(audio_io_handle_t) |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 2285 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2286 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2287 | break; |
| 2288 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2289 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2290 | default: |
| 2291 | break; |
| 2292 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2293 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2294 | } |
| 2295 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2296 | void AudioFlinger::PlaybackThread::writeCallback() |
| 2297 | { |
| 2298 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2299 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | void AudioFlinger::PlaybackThread::drainCallback() |
| 2303 | { |
| 2304 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2305 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2306 | } |
| 2307 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2308 | void AudioFlinger::PlaybackThread::errorCallback() |
| 2309 | { |
| 2310 | ALOG_ASSERT(mCallbackThread != 0); |
| 2311 | mCallbackThread->setAsyncError(); |
| 2312 | } |
| 2313 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2314 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2315 | { |
| 2316 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2317 | // reject out of sequence requests |
| 2318 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2319 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2320 | mWaitWorkCV.signal(); |
| 2321 | } |
| 2322 | } |
| 2323 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2324 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2325 | { |
| 2326 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2327 | // reject out of sequence requests |
| 2328 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2329 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2330 | mWaitWorkCV.signal(); |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | // static |
| 2335 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2336 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2337 | void *cookie) |
| 2338 | { |
| 2339 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 2340 | ALOGV("asyncCallback() event %d", event); |
| 2341 | switch (event) { |
| 2342 | case STREAM_CBK_EVENT_WRITE_READY: |
| 2343 | me->writeCallback(); |
| 2344 | break; |
| 2345 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 2346 | me->drainCallback(); |
| 2347 | break; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 2348 | case STREAM_CBK_EVENT_ERROR: |
| 2349 | me->errorCallback(); |
| 2350 | break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2351 | default: |
| 2352 | ALOGW("asyncCallback() unknown event %d", event); |
| 2353 | break; |
| 2354 | } |
| 2355 | return 0; |
| 2356 | } |
| 2357 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2358 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2359 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2360 | // 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] | 2361 | mSampleRate = mOutput->getSampleRate(); |
| 2362 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2363 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2364 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2365 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2366 | if ((mType == MIXER || mType == DUPLICATING) |
| 2367 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2368 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2369 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2370 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2371 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2372 | |
| 2373 | // Get actual HAL format. |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2374 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2375 | // Get format from the shim, which will be different than the HAL format |
| 2376 | // if playing compressed audio over HDMI passthrough. |
| 2377 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2378 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2379 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2380 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2381 | if ((mType == MIXER || mType == DUPLICATING) |
| 2382 | && !isValidPcmSinkFormat(mFormat)) { |
| 2383 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2384 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2385 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2386 | mFrameSize = mOutput->getFrameSize(); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2387 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2388 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | if (mFrameCount & 15) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2390 | ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2391 | mFrameCount); |
| 2392 | } |
| 2393 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2394 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2395 | (mOutput->stream->set_callback != NULL)) { |
| 2396 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2397 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2398 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2399 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2400 | } |
| 2401 | } |
| 2402 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2403 | mHwSupportsPause = false; |
| 2404 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2405 | if (mOutput->stream->pause != NULL) { |
| 2406 | if (mOutput->stream->resume != NULL) { |
| 2407 | mHwSupportsPause = true; |
| 2408 | } else { |
| 2409 | ALOGW("direct output implements pause but not resume"); |
| 2410 | } |
| 2411 | } else if (mOutput->stream->resume != NULL) { |
| 2412 | ALOGW("direct output implements resume but not pause"); |
| 2413 | } |
| 2414 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2415 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2416 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2417 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2418 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2419 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2420 | // For best precision, we use float instead of the associated output |
| 2421 | // device format (typically PCM 16 bit). |
| 2422 | |
| 2423 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2424 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2425 | mBufferSize = mFrameSize * mFrameCount; |
| 2426 | |
| 2427 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2428 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2429 | // (if a valid mask) to avoid premature downmix. |
| 2430 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2431 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2432 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2433 | } |
| 2434 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2435 | // 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] | 2436 | double multiplier = 1.0; |
| 2437 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2438 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2439 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2440 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2441 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2442 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2443 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2444 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2445 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2446 | maxNormalFrameCount = minNormalFrameCount; |
| 2447 | } |
| 2448 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2449 | if (multiplier <= 1.0) { |
| 2450 | multiplier = 1.0; |
| 2451 | } else if (multiplier <= 2.0) { |
| 2452 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2453 | multiplier = 2.0; |
| 2454 | } else { |
| 2455 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2456 | } |
| 2457 | } else { |
Haynes Mathew George | 227a14b | 2016-05-09 12:45:48 -0700 | [diff] [blame] | 2458 | multiplier = floor(multiplier); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | mNormalFrameCount = multiplier * mFrameCount; |
| 2462 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2463 | if (mType == MIXER || mType == DUPLICATING) { |
| 2464 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2465 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2466 | ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2467 | mNormalFrameCount); |
| 2468 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2469 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2470 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2471 | mThreadThrottleTimeMs = 0; |
| 2472 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2473 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2474 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2475 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2476 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2477 | free(mSinkBuffer); |
| 2478 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2479 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2480 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2481 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2482 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2483 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2484 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2485 | // drives the output. |
| 2486 | free(mMixerBuffer); |
| 2487 | mMixerBuffer = NULL; |
| 2488 | if (mMixerBufferEnabled) { |
| 2489 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2490 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2491 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2492 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2493 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2494 | free(mEffectBuffer); |
| 2495 | mEffectBuffer = NULL; |
| 2496 | if (mEffectBufferEnabled) { |
| 2497 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2498 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2499 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2500 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2501 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2502 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2503 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2504 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2505 | // 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] | 2506 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2507 | // matter. |
| 2508 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2509 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2510 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2511 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2516 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2517 | { |
| 2518 | if (halFrames == NULL || dspFrames == NULL) { |
| 2519 | return BAD_VALUE; |
| 2520 | } |
| 2521 | Mutex::Autolock _l(mLock); |
| 2522 | if (initCheck() != NO_ERROR) { |
| 2523 | return INVALID_OPERATION; |
| 2524 | } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2525 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2526 | *halFrames = framesWritten; |
| 2527 | |
| 2528 | if (isSuspended()) { |
| 2529 | // return an estimation of rendered frames when the output is suspended |
| 2530 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2531 | *dspFrames = (uint32_t) |
| 2532 | (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2533 | return NO_ERROR; |
| 2534 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2535 | status_t status; |
| 2536 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2537 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2538 | *dspFrames = (size_t)frames; |
| 2539 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2543 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 2544 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2545 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2546 | uint32_t result = 0; |
| 2547 | if (getEffectChain_l(sessionId) != 0) { |
| 2548 | result = EFFECT_SESSION; |
| 2549 | } |
| 2550 | |
| 2551 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2552 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2553 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2554 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2555 | if (track->isFastTrack()) { |
| 2556 | result |= FAST_SESSION; |
| 2557 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | return result; |
| 2563 | } |
| 2564 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2565 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2566 | { |
| 2567 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2568 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2569 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2570 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2571 | } |
| 2572 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2573 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2574 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2575 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2576 | } |
| 2577 | } |
| 2578 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2579 | } |
| 2580 | |
| 2581 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2582 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2583 | { |
| 2584 | Mutex::Autolock _l(mLock); |
| 2585 | return mOutput; |
| 2586 | } |
| 2587 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2588 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2589 | { |
| 2590 | Mutex::Autolock _l(mLock); |
| 2591 | AudioStreamOut *output = mOutput; |
| 2592 | mOutput = NULL; |
| 2593 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2594 | // must push a NULL and wait for ack |
| 2595 | mOutputSink.clear(); |
| 2596 | mPipeSink.clear(); |
| 2597 | mNormalSink.clear(); |
| 2598 | return output; |
| 2599 | } |
| 2600 | |
| 2601 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2602 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2603 | { |
| 2604 | if (mOutput == NULL) { |
| 2605 | return NULL; |
| 2606 | } |
| 2607 | return &mOutput->stream->common; |
| 2608 | } |
| 2609 | |
| 2610 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2611 | { |
| 2612 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2613 | } |
| 2614 | |
| 2615 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2616 | { |
| 2617 | if (!isValidSyncEvent(event)) { |
| 2618 | return BAD_VALUE; |
| 2619 | } |
| 2620 | |
| 2621 | Mutex::Autolock _l(mLock); |
| 2622 | |
| 2623 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2624 | sp<Track> track = mTracks[i]; |
| 2625 | if (event->triggerSession() == track->sessionId()) { |
| 2626 | (void) track->setSyncEvent(event); |
| 2627 | return NO_ERROR; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | return NAME_NOT_FOUND; |
| 2632 | } |
| 2633 | |
| 2634 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2635 | { |
| 2636 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2637 | } |
| 2638 | |
| 2639 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2640 | const Vector< sp<Track> >& tracksToRemove) |
| 2641 | { |
| 2642 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2643 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2644 | for (size_t i = 0 ; i < count ; i++) { |
| 2645 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2646 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2647 | AudioSystem::stopOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2648 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2649 | #ifdef ADD_BATTERY_DATA |
| 2650 | // to track the speaker usage |
| 2651 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2652 | #endif |
| 2653 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2654 | AudioSystem::releaseOutput(mId, track->streamType(), |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2655 | track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2656 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2663 | { |
| 2664 | if (!mMasterMute) { |
| 2665 | char value[PROPERTY_VALUE_MAX]; |
Jean-Michel Trivi | 32f37c2 | 2016-03-31 16:00:32 -0700 | [diff] [blame] | 2666 | if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 2667 | ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX"); |
| 2668 | return; |
| 2669 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2671 | char *endptr; |
| 2672 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2673 | if (*endptr == '\0' && ul != 0) { |
| 2674 | ALOGD("Silence is golden"); |
| 2675 | // The setprop command will not allow a property to be changed after |
| 2676 | // the first time it is set, so we don't have to worry about un-muting. |
| 2677 | setMasterMute_l(true); |
| 2678 | } |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2684 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2685 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2686 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2687 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2688 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2689 | |
| 2690 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2691 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2692 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2693 | const size_t count = mBytesRemaining / mFrameSize; |
| 2694 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2695 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2696 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2697 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2698 | if (screenState != mScreenState) { |
| 2699 | mScreenState = screenState; |
| 2700 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2701 | if (pipe != NULL) { |
| 2702 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2703 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2704 | } |
| 2705 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2706 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2707 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2708 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2709 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2710 | } else { |
| 2711 | bytesWritten = framesWritten; |
| 2712 | } |
| 2713 | // otherwise use the HAL / AudioStreamOut directly |
| 2714 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2715 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2716 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2717 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2718 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2719 | mWriteAckSequence += 2; |
| 2720 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2721 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2722 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2723 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2724 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2725 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2726 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 2727 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2728 | if (mUseAsyncWrite && |
| 2729 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2730 | // 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] | 2731 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2732 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2733 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2734 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2737 | mNumWrites++; |
| 2738 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2739 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2740 | return bytesWritten; |
| 2741 | } |
| 2742 | |
| 2743 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2744 | { |
| 2745 | if (mOutput->stream->drain) { |
| 2746 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2747 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2748 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2749 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2750 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2751 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2752 | } |
| 2753 | mOutput->stream->drain(mOutput->stream, |
| 2754 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2755 | : AUDIO_DRAIN_ALL); |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2760 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2761 | { |
| 2762 | Mutex::Autolock _l(mLock); |
| 2763 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2764 | sp<Track> track = mTracks[i]; |
| 2765 | track->invalidate(); |
| 2766 | } |
| 2767 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | /* |
| 2771 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2772 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2773 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2774 | - mIdleSleepTimeUs from idleSleepTimeUs() |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2775 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least |
| 2776 | kDefaultStandbyTimeInNsecs when connected to an A2DP device. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2777 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2778 | |
| 2779 | The parameters that affect these derived values are: |
| 2780 | - frame count |
| 2781 | - frame size |
| 2782 | - sample rate |
| 2783 | - device type: A2DP or not |
| 2784 | - device latency |
| 2785 | - format: PCM or not |
| 2786 | - active sleep time |
| 2787 | - idle sleep time |
| 2788 | */ |
| 2789 | |
| 2790 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2791 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2792 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2793 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2794 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 2795 | |
| 2796 | // make sure standby delay is not too short when connected to an A2DP sink to avoid |
| 2797 | // truncating audio when going to standby. |
| 2798 | mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs; |
| 2799 | if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) { |
| 2800 | if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) { |
| 2801 | mStandbyDelayNs = kDefaultStandbyTimeInNsecs; |
| 2802 | } |
| 2803 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2806 | bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2807 | { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2808 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2809 | this, streamType, mTracks.size()); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2810 | bool trackMatch = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2811 | size_t size = mTracks.size(); |
| 2812 | for (size_t i = 0; i < size; i++) { |
| 2813 | sp<Track> t = mTracks[i]; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2814 | if (t->streamType() == streamType && t->isExternalTrack()) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2815 | t->invalidate(); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2816 | trackMatch = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2817 | } |
| 2818 | } |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 2819 | return trackMatch; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2820 | } |
| 2821 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 2822 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2823 | { |
| 2824 | Mutex::Autolock _l(mLock); |
| 2825 | invalidateTracks_l(streamType); |
| 2826 | } |
| 2827 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2828 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2829 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2830 | audio_session_t session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2831 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2832 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2833 | bool ownsBuffer = false; |
| 2834 | |
| 2835 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2836 | if (session > AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | // 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] | 2838 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2839 | if (mType != DIRECT) { |
| 2840 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2841 | buffer = new int16_t[numSamples]; |
| 2842 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2843 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2844 | ownsBuffer = true; |
| 2845 | } |
| 2846 | |
| 2847 | // Attach all tracks with same session ID to this chain. |
| 2848 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2849 | sp<Track> track = mTracks[i]; |
| 2850 | if (session == track->sessionId()) { |
| 2851 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2852 | buffer); |
| 2853 | track->setMainBuffer(buffer); |
| 2854 | chain->incTrackCnt(); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | // indicate all active tracks in the chain |
| 2859 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2860 | sp<Track> track = mActiveTracks[i].promote(); |
| 2861 | if (track == 0) { |
| 2862 | continue; |
| 2863 | } |
| 2864 | if (session == track->sessionId()) { |
| 2865 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2866 | chain->incActiveTrackCnt(); |
| 2867 | } |
| 2868 | } |
| 2869 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2870 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2871 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2872 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2873 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2874 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2875 | // chains list in order to be processed last as it contains output stage effects. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2876 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2877 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2878 | // after track specific effects and before output stage. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2879 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2880 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2881 | // Effect chain for other sessions are inserted at beginning of effect |
| 2882 | // chains list to be processed before output mix effects. Relative order between other |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2883 | // sessions is not important. |
| 2884 | static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 && |
| 2885 | AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX, |
| 2886 | "audio_session_t constants misdefined"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2887 | size_t size = mEffectChains.size(); |
| 2888 | size_t i = 0; |
| 2889 | for (i = 0; i < size; i++) { |
| 2890 | if (mEffectChains[i]->sessionId() < session) { |
| 2891 | break; |
| 2892 | } |
| 2893 | } |
| 2894 | mEffectChains.insertAt(chain, i); |
| 2895 | checkSuspendOnAddEffectChain_l(chain); |
| 2896 | |
| 2897 | return NO_ERROR; |
| 2898 | } |
| 2899 | |
| 2900 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2901 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2902 | audio_session_t session = chain->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2903 | |
| 2904 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2905 | |
| 2906 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2907 | if (chain == mEffectChains[i]) { |
| 2908 | mEffectChains.removeAt(i); |
| 2909 | // detach all active tracks from the chain |
| 2910 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2911 | sp<Track> track = mActiveTracks[i].promote(); |
| 2912 | if (track == 0) { |
| 2913 | continue; |
| 2914 | } |
| 2915 | if (session == track->sessionId()) { |
| 2916 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2917 | chain.get(), session); |
| 2918 | chain->decActiveTrackCnt(); |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | // detach all tracks with same session ID from this chain |
| 2923 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2924 | sp<Track> track = mTracks[i]; |
| 2925 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2926 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2927 | chain->decTrackCnt(); |
| 2928 | } |
| 2929 | } |
| 2930 | break; |
| 2931 | } |
| 2932 | } |
| 2933 | return mEffectChains.size(); |
| 2934 | } |
| 2935 | |
| 2936 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2937 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2938 | { |
| 2939 | Mutex::Autolock _l(mLock); |
| 2940 | return attachAuxEffect_l(track, EffectId); |
| 2941 | } |
| 2942 | |
| 2943 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2944 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2945 | { |
| 2946 | status_t status = NO_ERROR; |
| 2947 | |
| 2948 | if (EffectId == 0) { |
| 2949 | track->setAuxBuffer(0, NULL); |
| 2950 | } else { |
| 2951 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2952 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2953 | if (effect != 0) { |
| 2954 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2955 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2956 | } else { |
| 2957 | status = INVALID_OPERATION; |
| 2958 | } |
| 2959 | } else { |
| 2960 | status = BAD_VALUE; |
| 2961 | } |
| 2962 | } |
| 2963 | return status; |
| 2964 | } |
| 2965 | |
| 2966 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2967 | { |
| 2968 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2969 | sp<Track> track = mTracks[i]; |
| 2970 | if (track->auxEffectId() == effectId) { |
| 2971 | attachAuxEffect_l(track, 0); |
| 2972 | } |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2977 | { |
| 2978 | Vector< sp<Track> > tracksToRemove; |
| 2979 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2980 | mStandbyTimeNs = systemTime(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 2981 | nsecs_t lastWriteFinished = -1; // time last server write completed |
| 2982 | int64_t lastFramesWritten = -1; // track changes in timestamp server frames written |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2983 | |
| 2984 | // MIXER |
| 2985 | nsecs_t lastWarning = 0; |
| 2986 | |
| 2987 | // DUPLICATING |
| 2988 | // FIXME could this be made local to while loop? |
| 2989 | writeFrames = 0; |
| 2990 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2991 | int lastGeneration = 0; |
| 2992 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2993 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2994 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2995 | |
| 2996 | if (mType == MIXER) { |
| 2997 | sleepTimeShift = 0; |
| 2998 | } |
| 2999 | |
| 3000 | CpuStats cpuStats; |
| 3001 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 3002 | |
| 3003 | acquireWakeLock(); |
| 3004 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3005 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 3006 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 3007 | // and then that string will be logged at the next convenient opportunity. |
| 3008 | const char *logString = NULL; |
| 3009 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 3010 | checkSilentMode_l(); |
| 3011 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3012 | while (!exitPending()) |
| 3013 | { |
| 3014 | cpuStats.sample(myName); |
| 3015 | |
| 3016 | Vector< sp<EffectChain> > effectChains; |
| 3017 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3018 | { // scope for mLock |
| 3019 | |
| 3020 | Mutex::Autolock _l(mLock); |
| 3021 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3022 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3023 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3024 | if (logString != NULL) { |
| 3025 | mNBLogWriter->logTimestamp(); |
| 3026 | mNBLogWriter->log(logString); |
| 3027 | logString = NULL; |
| 3028 | } |
| 3029 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3030 | // Gather the framesReleased counters for all active tracks, |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3031 | // and associate with the sink frames written out. We need |
| 3032 | // this to convert the sink timestamp to the track timestamp. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3033 | bool kernelLocationUpdate = false; |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3034 | if (mNormalSink != 0) { |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3035 | // Note: The DuplicatingThread may not have a mNormalSink. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3036 | // We always fetch the timestamp here because often the downstream |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3037 | // sink will block while writing. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3038 | ExtendedTimestamp timestamp; // use private copy to fetch |
| 3039 | (void) mNormalSink->getTimestamp(timestamp); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3040 | |
| 3041 | // We keep track of the last valid kernel position in case we are in underrun |
| 3042 | // and the normal mixer period is the same as the fast mixer period, or there |
| 3043 | // is some error from the HAL. |
| 3044 | if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3045 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3046 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3047 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] = |
| 3048 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 3049 | |
| 3050 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3051 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER]; |
| 3052 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] = |
| 3053 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER]; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) { |
| 3057 | kernelLocationUpdate = true; |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3058 | } else { |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 3059 | ALOGVV("getTimestamp error - no valid kernel position"); |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 3060 | } |
| 3061 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3062 | // copy over kernel info |
| 3063 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3064 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 3065 | + mSuspendedFrames; // add frames discarded when suspended |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3066 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 3067 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3068 | } |
| 3069 | // mFramesWritten for non-offloaded tracks are contiguous |
| 3070 | // even after standby() is called. This is useful for the track frame |
| 3071 | // to sink frame mapping. |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3072 | bool serverLocationUpdate = false; |
| 3073 | if (mFramesWritten != lastFramesWritten) { |
| 3074 | serverLocationUpdate = true; |
| 3075 | lastFramesWritten = mFramesWritten; |
| 3076 | } |
| 3077 | // Only update timestamps if there is a meaningful change. |
| 3078 | // Either the kernel timestamp must be valid or we have written something. |
| 3079 | if (kernelLocationUpdate || serverLocationUpdate) { |
| 3080 | if (serverLocationUpdate) { |
| 3081 | // use the time before we called the HAL write - it is a bit more accurate |
| 3082 | // to when the server last read data than the current time here. |
| 3083 | // |
| 3084 | // If we haven't written anything, mLastWriteTime will be -1 |
| 3085 | // and we use systemTime(). |
| 3086 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten; |
| 3087 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastWriteTime == -1 |
| 3088 | ? systemTime() : mLastWriteTime; |
| 3089 | } |
| 3090 | const size_t size = mActiveTracks.size(); |
| 3091 | for (size_t i = 0; i < size; ++i) { |
| 3092 | sp<Track> t = mActiveTracks[i].promote(); |
| 3093 | if (t != 0 && !t->isFastTrack()) { |
| 3094 | t->updateTrackFrameInfo( |
| 3095 | t->mAudioTrackServerProxy->framesReleased(), |
| 3096 | mFramesWritten, |
| 3097 | mTimestamp); |
| 3098 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 3099 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3102 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3103 | if (mSignalPending) { |
| 3104 | // A signal was raised while we were unlocked |
| 3105 | mSignalPending = false; |
| 3106 | } else if (waitingAsyncCallback_l()) { |
| 3107 | if (exitPending()) { |
| 3108 | break; |
| 3109 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3110 | bool released = false; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 3111 | if (!keepWakeLock()) { |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3112 | releaseWakeLock_l(); |
| 3113 | released = true; |
Mikhail Naganov | e94c27a | 2016-08-18 17:31:46 -0700 | [diff] [blame] | 3114 | mWakeLockUids.clear(); |
| 3115 | mActiveTracksGeneration++; |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3116 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3117 | ALOGV("wait async completion"); |
| 3118 | mWaitWorkCV.wait(mLock); |
| 3119 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 3120 | if (released) { |
| 3121 | acquireWakeLock_l(); |
| 3122 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3123 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3124 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 3125 | |
| 3126 | continue; |
| 3127 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3128 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3129 | isSuspended()) { |
| 3130 | // put audio hardware into standby after short delay |
| 3131 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3132 | |
| 3133 | threadLoop_standby(); |
| 3134 | |
| 3135 | mStandby = true; |
| 3136 | } |
| 3137 | |
| 3138 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 3139 | // we're about to wait, flush the binder command buffer |
| 3140 | IPCThreadState::self()->flushCommands(); |
| 3141 | |
| 3142 | clearOutputTracks(); |
| 3143 | |
| 3144 | if (exitPending()) { |
| 3145 | break; |
| 3146 | } |
| 3147 | |
| 3148 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3149 | mWakeLockUids.clear(); |
| 3150 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3151 | // wait until we have something to do... |
| 3152 | ALOGV("%s going to sleep", myName.string()); |
| 3153 | mWaitWorkCV.wait(mLock); |
| 3154 | ALOGV("%s waking up", myName.string()); |
| 3155 | acquireWakeLock_l(); |
| 3156 | |
| 3157 | mMixerStatus = MIXER_IDLE; |
| 3158 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 3159 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3160 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3161 | checkSilentMode_l(); |
| 3162 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3163 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 3164 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3165 | if (mType == MIXER) { |
| 3166 | sleepTimeShift = 0; |
| 3167 | } |
| 3168 | |
| 3169 | continue; |
| 3170 | } |
| 3171 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3172 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 3173 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 3174 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3175 | // compare with previously applied list |
| 3176 | if (lastGeneration != mActiveTracksGeneration) { |
| 3177 | // update wakelock |
| 3178 | updateWakeLockUids_l(mWakeLockUids); |
| 3179 | lastGeneration = mActiveTracksGeneration; |
| 3180 | } |
| 3181 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3182 | // prevent any changes in effect chain list and in each effect chain |
| 3183 | // during mixing and effect process as the audio buffers could be deleted |
| 3184 | // or modified if an effect is created or deleted |
| 3185 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3186 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3187 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3188 | if (mBytesRemaining == 0) { |
| 3189 | mCurrentWriteLength = 0; |
| 3190 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 3191 | // threadLoop_mix() sets mCurrentWriteLength |
| 3192 | threadLoop_mix(); |
| 3193 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 3194 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3195 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3196 | // must be written to HAL |
| 3197 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3198 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3199 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3200 | } |
| 3201 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3202 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3203 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3204 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 3205 | // or mSinkBuffer (if there are no effects). |
| 3206 | // |
| 3207 | // This is done pre-effects computation; if effects change to |
| 3208 | // support higher precision, this needs to move. |
| 3209 | // |
| 3210 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3211 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3212 | if (mMixerBufferValid) { |
| 3213 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 3214 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 3215 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3216 | // mono blend occurs for mixer threads only (not direct or offloaded) |
| 3217 | // and is handled here if we're going directly to the sink. |
| 3218 | if (requireMonoBlend() && !mEffectBufferValid) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3219 | mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount, |
| 3220 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3221 | } |
| 3222 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3223 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 3224 | mNormalFrameCount * mChannelCount); |
| 3225 | } |
| 3226 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3227 | mBytesRemaining = mCurrentWriteLength; |
| 3228 | if (isSuspended()) { |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 3229 | // Simulate write to HAL when suspended (e.g. BT SCO phone call). |
| 3230 | mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer. |
| 3231 | const size_t framesRemaining = mBytesRemaining / mFrameSize; |
| 3232 | mBytesWritten += mBytesRemaining; |
| 3233 | mFramesWritten += framesRemaining; |
| 3234 | mSuspendedFrames += framesRemaining; // to adjust kernel HAL position |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3235 | mBytesRemaining = 0; |
| 3236 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3237 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3238 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3239 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3240 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3241 | effectChains[i]->process_l(); |
| 3242 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3243 | } |
| 3244 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 3245 | // Process effect chains for offloaded thread even if no audio |
| 3246 | // was read from audio track: process only updates effect state |
| 3247 | // and thus does have to be synchronized with audio writes but may have |
| 3248 | // to be called while waiting for async write callback |
| 3249 | if (mType == OFFLOAD) { |
| 3250 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 3251 | effectChains[i]->process_l(); |
| 3252 | } |
| 3253 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3254 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3255 | // Only if the Effects buffer is enabled and there is data in the |
| 3256 | // Effects buffer (buffer valid), we need to |
| 3257 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3258 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3259 | if (mEffectBufferValid) { |
| 3260 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3261 | |
| 3262 | if (requireMonoBlend()) { |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 3263 | mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount, |
| 3264 | true /*limit*/); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3265 | } |
| 3266 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3267 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 3268 | mNormalFrameCount * mChannelCount); |
| 3269 | } |
| 3270 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3271 | // enable changes in effect chain |
| 3272 | unlockEffectChains(effectChains); |
| 3273 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3274 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3275 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 3276 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3277 | ssize_t ret = 0; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3278 | // We save lastWriteFinished here, as previousLastWriteFinished, |
| 3279 | // for throttling. On thread start, previousLastWriteFinished will be |
| 3280 | // set to -1, which properly results in no throttling after the first write. |
| 3281 | nsecs_t previousLastWriteFinished = lastWriteFinished; |
| 3282 | nsecs_t delta = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3283 | if (mBytesRemaining) { |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3284 | // FIXME rewrite to reduce number of system calls |
| 3285 | mLastWriteTime = systemTime(); // also used for dumpsys |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3286 | ret = threadLoop_write(); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3287 | lastWriteFinished = systemTime(); |
| 3288 | delta = lastWriteFinished - mLastWriteTime; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3289 | if (ret < 0) { |
| 3290 | mBytesRemaining = 0; |
| 3291 | } else { |
| 3292 | mBytesWritten += ret; |
| 3293 | mBytesRemaining -= ret; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 3294 | mFramesWritten += ret / mFrameSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3295 | } |
| 3296 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 3297 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 3298 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3299 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3300 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3301 | // write blocked detection |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3302 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3303 | mNumDelayedWrites++; |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3304 | if ((lastWriteFinished - lastWarning) > kWarningThrottleNs) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3305 | ATRACE_NAME("underrun"); |
| 3306 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3307 | (unsigned long long) ns2ms(delta), mNumDelayedWrites, this); |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3308 | lastWarning = lastWriteFinished; |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 3309 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3310 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3311 | |
| 3312 | if (mThreadThrottle |
| 3313 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 3314 | && ret > 0) { // we wrote something |
| 3315 | // Limit MixerThread data processing to no more than twice the |
| 3316 | // expected processing rate. |
| 3317 | // |
| 3318 | // This helps prevent underruns with NuPlayer and other applications |
| 3319 | // which may set up buffers that are close to the minimum size, or use |
| 3320 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 3321 | // |
| 3322 | // The throttle smooths out sudden large data drains from the device, |
| 3323 | // e.g. when it comes out of standby, which often causes problems with |
| 3324 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 3325 | // (2) minimum buffer sized tracks (even if the track is full, |
| 3326 | // the app won't fill fast enough to handle the sudden draw). |
Haynes Mathew George | f92b217 | 2016-05-09 11:34:15 -0700 | [diff] [blame] | 3327 | // |
| 3328 | // Total time spent in last processing cycle equals time spent in |
| 3329 | // 1. threadLoop_write, as well as time spent in |
| 3330 | // 2. threadLoop_mix (significant for heavy mixing, especially |
| 3331 | // on low tier processors) |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3332 | |
Andy Hung | 69488c4 | 2016-05-16 18:43:33 -0700 | [diff] [blame] | 3333 | // it's OK if deltaMs is an overestimate. |
| 3334 | const int32_t deltaMs = |
| 3335 | (lastWriteFinished - previousLastWriteFinished) / 1000000; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3336 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 3337 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 3338 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3339 | // notify of throttle start on verbose log |
| 3340 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 3341 | "mixer(%p) throttle begin:" |
| 3342 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3343 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3344 | mThreadThrottleTimeMs += throttleMs; |
Andy Hung | 0a31ddd | 2016-07-06 19:10:29 -0700 | [diff] [blame] | 3345 | // Throttle must be attributed to the previous mixer loop's write time |
| 3346 | // to allow back-to-back throttling. |
| 3347 | lastWriteFinished += throttleMs * 1000000; |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3348 | } else { |
| 3349 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 3350 | if (diff > 0) { |
| 3351 | // notify of throttle end on debug log |
Andy Hung | 3ea004d | 2016-05-05 16:48:37 -0700 | [diff] [blame] | 3352 | // but prevent spamming for bluetooth |
| 3353 | ALOGD_IF(!audio_is_a2dp_out_device(outDevice()), |
| 3354 | "mixer(%p) throttle end: throttle time(%u)", this, diff); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 3355 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 3356 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 3357 | } |
| 3358 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3359 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3360 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3361 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3362 | ATRACE_BEGIN("sleep"); |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 3363 | Mutex::Autolock _l(mLock); |
| 3364 | if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) { |
| 3365 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs)); |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 3366 | } |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3367 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3368 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | // Finally let go of removed track(s), without the lock held |
| 3372 | // since we can't guarantee the destructors won't acquire that |
| 3373 | // same lock. This will also mutate and push a new fast mixer state. |
| 3374 | threadLoop_removeTracks(tracksToRemove); |
| 3375 | tracksToRemove.clear(); |
| 3376 | |
| 3377 | // FIXME I don't understand the need for this here; |
| 3378 | // it was in the original code but maybe the |
| 3379 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3380 | clearOutputTracks(); |
| 3381 | |
| 3382 | // Effect chains will be actually deleted here if they were removed from |
| 3383 | // mEffectChains list during mixing or effects processing |
| 3384 | effectChains.clear(); |
| 3385 | |
| 3386 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3387 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3388 | } |
| 3389 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3390 | threadLoop_exit(); |
| 3391 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3392 | if (!mStandby) { |
| 3393 | threadLoop_standby(); |
| 3394 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3398 | mWakeLockUids.clear(); |
| 3399 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3400 | |
| 3401 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3402 | return false; |
| 3403 | } |
| 3404 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3405 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3406 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3407 | { |
| 3408 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3409 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3410 | for (size_t i=0 ; i<count ; i++) { |
| 3411 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3412 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3413 | mWakeLockUids.remove(track->uid()); |
| 3414 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3415 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3416 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3417 | if (chain != 0) { |
| 3418 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3419 | track->sessionId()); |
| 3420 | chain->decActiveTrackCnt(); |
| 3421 | } |
| 3422 | if (track->isTerminated()) { |
| 3423 | removeTrack_l(track); |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3429 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3430 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3431 | { |
| 3432 | if (mNormalSink != 0) { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 3433 | ExtendedTimestamp ets; |
| 3434 | status_t status = mNormalSink->getTimestamp(ets); |
| 3435 | if (status == NO_ERROR) { |
| 3436 | status = ets.getBestTimestamp(×tamp); |
| 3437 | } |
| 3438 | return status; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3439 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3440 | if ((mType == OFFLOAD || mType == DIRECT) |
| 3441 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3442 | uint64_t position64; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3443 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3444 | if (ret == 0) { |
| 3445 | timestamp.mPosition = (uint32_t)position64; |
| 3446 | return NO_ERROR; |
| 3447 | } |
| 3448 | } |
| 3449 | return INVALID_OPERATION; |
| 3450 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3451 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3452 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3453 | audio_patch_handle_t *handle) |
| 3454 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3455 | status_t status; |
| 3456 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3457 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3458 | // or if HAL does not properly lock against access. |
| 3459 | AutoPark<FastMixer> park(mFastMixer); |
| 3460 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3461 | } else { |
| 3462 | status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3463 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3464 | return status; |
| 3465 | } |
| 3466 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3467 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3468 | audio_patch_handle_t *handle) |
| 3469 | { |
| 3470 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3471 | |
| 3472 | // store new device and send to effects |
| 3473 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3474 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3475 | type |= patch->sinks[i].ext.device.type; |
| 3476 | } |
| 3477 | |
| 3478 | #ifdef ADD_BATTERY_DATA |
| 3479 | // when changing the audio output device, call addBatteryData to notify |
| 3480 | // the change |
| 3481 | if (mOutDevice != type) { |
| 3482 | uint32_t params = 0; |
| 3483 | // check whether speaker is on |
| 3484 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3485 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3488 | audio_devices_t deviceWithoutSpeaker |
| 3489 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3490 | // check if any other device (except speaker) is on |
| 3491 | if (type & deviceWithoutSpeaker) { |
| 3492 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3493 | } |
| 3494 | |
| 3495 | if (params != 0) { |
| 3496 | addBatteryData(params); |
| 3497 | } |
| 3498 | } |
| 3499 | #endif |
| 3500 | |
| 3501 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3502 | mEffectChains[i]->setDevice_l(type); |
| 3503 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3504 | |
| 3505 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3506 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3507 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3508 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3509 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3510 | |
| 3511 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3512 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3513 | status = hwDevice->create_audio_patch(hwDevice, |
| 3514 | patch->num_sources, |
| 3515 | patch->sources, |
| 3516 | patch->num_sinks, |
| 3517 | patch->sinks, |
| 3518 | handle); |
| 3519 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3520 | char *address; |
| 3521 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3522 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3523 | address = audio_device_address_to_parameter( |
| 3524 | patch->sinks[0].ext.device.type, |
| 3525 | patch->sinks[0].ext.device.address); |
| 3526 | } else { |
| 3527 | address = (char *)calloc(1, 1); |
| 3528 | } |
| 3529 | AudioParameter param = AudioParameter(String8(address)); |
| 3530 | free(address); |
| 3531 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
| 3532 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3533 | param.toString().string()); |
| 3534 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3535 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3536 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3537 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3538 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3539 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3540 | return status; |
| 3541 | } |
| 3542 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3543 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3544 | { |
Andy Hung | f60abce | 2016-08-26 11:37:54 -0700 | [diff] [blame] | 3545 | status_t status; |
| 3546 | if (property_get_bool("af.patch_park", false /* default_value */)) { |
| 3547 | // Park FastMixer to avoid potential DOS issues with writing to the HAL |
| 3548 | // or if HAL does not properly lock against access. |
| 3549 | AutoPark<FastMixer> park(mFastMixer); |
| 3550 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3551 | } else { |
| 3552 | status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3553 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3554 | return status; |
| 3555 | } |
| 3556 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3557 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3558 | { |
| 3559 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3560 | |
| 3561 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3562 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3563 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 3564 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3565 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 3566 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3567 | AudioParameter param; |
| 3568 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 3569 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3570 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3571 | } |
| 3572 | return status; |
| 3573 | } |
| 3574 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3575 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3576 | { |
| 3577 | Mutex::Autolock _l(mLock); |
| 3578 | mTracks.add(track); |
| 3579 | } |
| 3580 | |
| 3581 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3582 | { |
| 3583 | Mutex::Autolock _l(mLock); |
| 3584 | destroyTrack_l(track); |
| 3585 | } |
| 3586 | |
| 3587 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3588 | { |
| 3589 | ThreadBase::getAudioPortConfig(config); |
| 3590 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3591 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3592 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3593 | } |
| 3594 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3595 | // ---------------------------------------------------------------------------- |
| 3596 | |
| 3597 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3598 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3599 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3600 | // mAudioMixer below |
| 3601 | // mFastMixer below |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3602 | mFastMixerFutex(0), |
| 3603 | mMasterMono(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3604 | // mOutputSink below |
| 3605 | // mPipeSink below |
| 3606 | // mNormalSink below |
| 3607 | { |
| 3608 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3609 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%zu, " |
| 3610 | "mFrameCount=%zu, mNormalFrameCount=%zu", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3611 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3612 | mNormalFrameCount); |
| 3613 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3614 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3615 | if (type == DUPLICATING) { |
| 3616 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3617 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3618 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3619 | return; |
| 3620 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3621 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3622 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3623 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3624 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3625 | #if !LOG_NDEBUG |
| 3626 | ssize_t index = |
| 3627 | #else |
| 3628 | (void) |
| 3629 | #endif |
| 3630 | mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3631 | ALOG_ASSERT(index == 0); |
| 3632 | |
| 3633 | // initialize fast mixer depending on configuration |
| 3634 | bool initFastMixer; |
| 3635 | switch (kUseFastMixer) { |
| 3636 | case FastMixer_Never: |
| 3637 | initFastMixer = false; |
| 3638 | break; |
| 3639 | case FastMixer_Always: |
| 3640 | initFastMixer = true; |
| 3641 | break; |
| 3642 | case FastMixer_Static: |
| 3643 | case FastMixer_Dynamic: |
| 3644 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3645 | break; |
| 3646 | } |
| 3647 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3648 | audio_format_t fastMixerFormat; |
| 3649 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3650 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3651 | } else { |
| 3652 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3653 | } |
| 3654 | if (mFormat != fastMixerFormat) { |
| 3655 | // change our Sink format to accept our intermediate precision |
| 3656 | mFormat = fastMixerFormat; |
| 3657 | free(mSinkBuffer); |
| 3658 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3659 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3660 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3661 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3662 | |
| 3663 | // create a MonoPipe to connect our submix to FastMixer |
| 3664 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3665 | #ifdef TEE_SINK |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3666 | NBAIO_Format origformat = format; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3667 | #endif |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3668 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3669 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3670 | format.mFormat = fastMixerFormat; |
| 3671 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3672 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3673 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3674 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3675 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3676 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3677 | const NBAIO_Format offers[1] = {format}; |
| 3678 | size_t numCounterOffers = 0; |
Glenn Kasten | fc302fd | 2016-04-11 14:11:26 -0700 | [diff] [blame] | 3679 | #if !LOG_NDEBUG || defined(TEE_SINK) |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 3680 | ssize_t index = |
| 3681 | #else |
| 3682 | (void) |
| 3683 | #endif |
| 3684 | monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3685 | ALOG_ASSERT(index == 0); |
| 3686 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3687 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3688 | mPipeSink = monoPipe; |
| 3689 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3690 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3691 | if (mTeeSinkOutputEnabled) { |
| 3692 | // 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] | 3693 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3694 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3695 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3696 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3697 | ALOG_ASSERT(index == 0); |
| 3698 | mTeeSink = teeSink; |
| 3699 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3700 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3701 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3702 | ALOG_ASSERT(index == 0); |
| 3703 | mTeeSource = teeSource; |
| 3704 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3705 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3706 | |
| 3707 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3708 | mFastMixer = new FastMixer(); |
| 3709 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3710 | #ifdef STATE_QUEUE_DUMP |
| 3711 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3712 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3713 | #endif |
| 3714 | FastMixerState *state = sq->begin(); |
| 3715 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3716 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3717 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3718 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3719 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3720 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3721 | fastTrack->mGeneration++; |
| 3722 | state->mFastTracksGen++; |
| 3723 | state->mTrackMask = 1; |
| 3724 | // fast mixer will use the HAL output sink |
| 3725 | state->mOutputSink = mOutputSink.get(); |
| 3726 | state->mOutputSinkGen++; |
| 3727 | state->mFrameCount = mFrameCount; |
| 3728 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3729 | // already done in constructor initialization list |
| 3730 | //mFastMixerFutex = 0; |
| 3731 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3732 | state->mColdGen++; |
| 3733 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3734 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3735 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3736 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3737 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3738 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3739 | sq->end(); |
| 3740 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3741 | |
| 3742 | // start the fast mixer |
| 3743 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3744 | pid_t tid = mFastMixer->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3745 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3746 | |
| 3747 | #ifdef AUDIO_WATCHDOG |
| 3748 | // create and start the watchdog |
| 3749 | mAudioWatchdog = new AudioWatchdog(); |
| 3750 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3751 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3752 | tid = mAudioWatchdog->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3753 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3754 | #endif |
| 3755 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | switch (kUseFastMixer) { |
| 3759 | case FastMixer_Never: |
| 3760 | case FastMixer_Dynamic: |
| 3761 | mNormalSink = mOutputSink; |
| 3762 | break; |
| 3763 | case FastMixer_Always: |
| 3764 | mNormalSink = mPipeSink; |
| 3765 | break; |
| 3766 | case FastMixer_Static: |
| 3767 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3768 | break; |
| 3769 | } |
| 3770 | } |
| 3771 | |
| 3772 | AudioFlinger::MixerThread::~MixerThread() |
| 3773 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3774 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3775 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3776 | FastMixerState *state = sq->begin(); |
| 3777 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3778 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3779 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3780 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | state->mCommand = FastMixerState::EXIT; |
| 3784 | sq->end(); |
| 3785 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3786 | mFastMixer->join(); |
| 3787 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3788 | // We'll use that extract the final state which contains one remaining fast track |
| 3789 | // corresponding to our sub-mix. |
| 3790 | state = sq->begin(); |
| 3791 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3792 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3793 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3794 | delete fastTrack->mBufferProvider; |
| 3795 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3796 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3797 | #ifdef AUDIO_WATCHDOG |
| 3798 | if (mAudioWatchdog != 0) { |
| 3799 | mAudioWatchdog->requestExit(); |
| 3800 | mAudioWatchdog->requestExitAndWait(); |
| 3801 | mAudioWatchdog.clear(); |
| 3802 | } |
| 3803 | #endif |
| 3804 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3805 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3806 | delete mAudioMixer; |
| 3807 | } |
| 3808 | |
| 3809 | |
| 3810 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3811 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3812 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3813 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3814 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3815 | } |
| 3816 | return latency; |
| 3817 | } |
| 3818 | |
| 3819 | |
| 3820 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3821 | { |
| 3822 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3823 | } |
| 3824 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3825 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3826 | { |
| 3827 | // FIXME we should only do one push per cycle; confirm this is true |
| 3828 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3829 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3830 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3831 | FastMixerState *state = sq->begin(); |
| 3832 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3833 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3834 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
Eric Laurent | a2ab450 | 2015-09-09 12:25:51 -0700 | [diff] [blame] | 3835 | |
| 3836 | // FIXME workaround for first HAL write being CPU bound on some devices |
| 3837 | ATRACE_BEGIN("write"); |
| 3838 | mOutput->write((char *)mSinkBuffer, 0); |
| 3839 | ATRACE_END(); |
| 3840 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3841 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3842 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3843 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3844 | } |
| 3845 | #ifdef AUDIO_WATCHDOG |
| 3846 | if (mAudioWatchdog != 0) { |
| 3847 | mAudioWatchdog->resume(); |
| 3848 | } |
| 3849 | #endif |
| 3850 | } |
| 3851 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3852 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3853 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3854 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3855 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3856 | sq->end(); |
| 3857 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3858 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3859 | mNormalSink = mPipeSink; |
| 3860 | } |
| 3861 | } else { |
| 3862 | sq->end(false /*didModify*/); |
| 3863 | } |
| 3864 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3865 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3869 | { |
| 3870 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3871 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3872 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3873 | FastMixerState *state = sq->begin(); |
| 3874 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3875 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3876 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3877 | state->mColdGen++; |
| 3878 | mFastMixerFutex = 0; |
| 3879 | sq->end(); |
| 3880 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3881 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3882 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3883 | mNormalSink = mOutputSink; |
| 3884 | } |
| 3885 | #ifdef AUDIO_WATCHDOG |
| 3886 | if (mAudioWatchdog != 0) { |
| 3887 | mAudioWatchdog->pause(); |
| 3888 | } |
| 3889 | #endif |
| 3890 | } else { |
| 3891 | sq->end(false /*didModify*/); |
| 3892 | } |
| 3893 | } |
| 3894 | PlaybackThread::threadLoop_standby(); |
| 3895 | } |
| 3896 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3897 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3898 | { |
| 3899 | return false; |
| 3900 | } |
| 3901 | |
| 3902 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3903 | { |
| 3904 | return !mStandby; |
| 3905 | } |
| 3906 | |
| 3907 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3908 | { |
| 3909 | Mutex::Autolock _l(mLock); |
| 3910 | return waitingAsyncCallback_l(); |
| 3911 | } |
| 3912 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3913 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3914 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3915 | { |
| 3916 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3917 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3918 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3919 | // discard any pending drain or write ack by incrementing sequence |
| 3920 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3921 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3922 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3923 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3924 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3925 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3926 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3929 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3930 | { |
| 3931 | ALOGV("signal playback thread"); |
| 3932 | broadcast_l(); |
| 3933 | } |
| 3934 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 3935 | void AudioFlinger::PlaybackThread::onAsyncError() |
| 3936 | { |
| 3937 | for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) { |
| 3938 | invalidateTracks((audio_stream_type_t)i); |
| 3939 | } |
| 3940 | } |
| 3941 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3942 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3943 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3944 | // mix buffers... |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3945 | mAudioMixer->process(); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3946 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3947 | // increase sleep time progressively when application underrun condition clears. |
| 3948 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3949 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3950 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3951 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3952 | sleepTimeShift--; |
| 3953 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3954 | mSleepTimeUs = 0; |
| 3955 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3956 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3957 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3961 | { |
| 3962 | // If no tracks are ready, sleep once for the duration of an output |
| 3963 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3964 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3965 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3966 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3967 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3968 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3969 | } |
| 3970 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3971 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3972 | // duration we would end up writing less data than needed by the audio HAL if |
| 3973 | // the condition persists. |
| 3974 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3975 | sleepTimeShift++; |
| 3976 | } |
| 3977 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3978 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3979 | } |
| 3980 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3981 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3982 | // before effects processing or output. |
| 3983 | if (mMixerBufferValid) { |
| 3984 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3985 | } else { |
| 3986 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3987 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3988 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3989 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3990 | "anticipated start"); |
| 3991 | } |
| 3992 | // TODO add standby time extension fct of effect tail |
| 3993 | } |
| 3994 | |
| 3995 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3996 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3997 | Vector< sp<Track> > *tracksToRemove) |
| 3998 | { |
| 3999 | |
| 4000 | mixer_state mixerStatus = MIXER_IDLE; |
| 4001 | // find out which tracks need to be processed |
| 4002 | size_t count = mActiveTracks.size(); |
| 4003 | size_t mixedTracks = 0; |
| 4004 | size_t tracksWithEffect = 0; |
| 4005 | // counts only _active_ fast tracks |
| 4006 | size_t fastTracks = 0; |
| 4007 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 4008 | |
| 4009 | float masterVolume = mMasterVolume; |
| 4010 | bool masterMute = mMasterMute; |
| 4011 | |
| 4012 | if (masterMute) { |
| 4013 | masterVolume = 0; |
| 4014 | } |
| 4015 | // Delegate master volume control to effect in output mix effect chain if needed |
| 4016 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 4017 | if (chain != 0) { |
| 4018 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 4019 | chain->setVolume_l(&v, &v); |
| 4020 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 4021 | chain.clear(); |
| 4022 | } |
| 4023 | |
| 4024 | // prepare a new state to push |
| 4025 | FastMixerStateQueue *sq = NULL; |
| 4026 | FastMixerState *state = NULL; |
| 4027 | bool didModify = false; |
| 4028 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4029 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4030 | sq = mFastMixer->sq(); |
| 4031 | state = sq->begin(); |
| 4032 | } |
| 4033 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4034 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4035 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4036 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4037 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 4038 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4039 | if (t == 0) { |
| 4040 | continue; |
| 4041 | } |
| 4042 | |
| 4043 | // this const just means the local variable doesn't change |
| 4044 | Track* const track = t.get(); |
| 4045 | |
| 4046 | // process fast tracks |
| 4047 | if (track->isFastTrack()) { |
| 4048 | |
| 4049 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 4050 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 4051 | // the track never becomes active so it's fast mixer slot is never touched. |
| 4052 | // The converse, of removing an (active) track and then creating a new track |
| 4053 | // at the identical fast mixer slot within the same normal mix cycle, |
| 4054 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 4055 | int j = track->mFastIndex; |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 4056 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4057 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 4058 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 4059 | |
| 4060 | // Determine whether the track is currently in underrun condition, |
| 4061 | // and whether it had a recent underrun. |
| 4062 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 4063 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 4064 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 4065 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 4066 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 4067 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 4068 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 4069 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 4070 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 4071 | track->mObservedUnderruns = underruns; |
| 4072 | // don't count underruns that occur while stopping or pausing |
| 4073 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4074 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 4075 | recentUnderruns > 0) { |
| 4076 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 4077 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4078 | } else { |
| 4079 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | // This is similar to the state machine for normal tracks, |
| 4083 | // with a few modifications for fast tracks. |
| 4084 | bool isActive = true; |
| 4085 | switch (track->mState) { |
| 4086 | case TrackBase::STOPPING_1: |
| 4087 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4088 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4089 | track->mState = TrackBase::STOPPING_2; |
| 4090 | } |
| 4091 | break; |
| 4092 | case TrackBase::PAUSING: |
| 4093 | // ramp down is not yet implemented |
| 4094 | track->setPaused(); |
| 4095 | break; |
| 4096 | case TrackBase::RESUMING: |
| 4097 | // ramp up is not yet implemented |
| 4098 | track->mState = TrackBase::ACTIVE; |
| 4099 | break; |
| 4100 | case TrackBase::ACTIVE: |
| 4101 | if (recentFull > 0 || recentPartial > 0) { |
| 4102 | // track has provided at least some frames recently: reset retry count |
| 4103 | track->mRetryCount = kMaxTrackRetries; |
| 4104 | } |
| 4105 | if (recentUnderruns == 0) { |
| 4106 | // no recent underruns: stay active |
| 4107 | break; |
| 4108 | } |
| 4109 | // there has recently been an underrun of some kind |
| 4110 | if (track->sharedBuffer() == 0) { |
| 4111 | // were any of the recent underruns "empty" (no frames available)? |
| 4112 | if (recentEmpty == 0) { |
| 4113 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 4114 | break; |
| 4115 | } |
| 4116 | // there has recently been an "empty" underrun: decrement the retry counter |
| 4117 | if (--(track->mRetryCount) > 0) { |
| 4118 | break; |
| 4119 | } |
| 4120 | // indicate to client process that the track was disabled because of underrun; |
| 4121 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4122 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4123 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 4124 | isActive = false; |
| 4125 | break; |
| 4126 | } |
| 4127 | // fall through |
| 4128 | case TrackBase::STOPPING_2: |
| 4129 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4130 | case TrackBase::STOPPED: |
| 4131 | case TrackBase::FLUSHED: // flush() while active |
| 4132 | // Check for presentation complete if track is inactive |
| 4133 | // We have consumed all the buffers of this track. |
| 4134 | // This would be incomplete if we auto-paused on underrun |
| 4135 | { |
| 4136 | size_t audioHALFrames = |
| 4137 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4138 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4139 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 4140 | // track stays in active list until presentation is complete |
| 4141 | break; |
| 4142 | } |
| 4143 | } |
| 4144 | if (track->isStopping_2()) { |
| 4145 | track->mState = TrackBase::STOPPED; |
| 4146 | } |
| 4147 | if (track->isStopped()) { |
| 4148 | // Can't reset directly, as fast mixer is still polling this track |
| 4149 | // track->reset(); |
| 4150 | // So instead mark this track as needing to be reset after push with ack |
| 4151 | resetMask |= 1 << i; |
| 4152 | } |
| 4153 | isActive = false; |
| 4154 | break; |
| 4155 | case TrackBase::IDLE: |
| 4156 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 4157 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | if (isActive) { |
| 4161 | // was it previously inactive? |
| 4162 | if (!(state->mTrackMask & (1 << j))) { |
| 4163 | ExtendedAudioBufferProvider *eabp = track; |
| 4164 | VolumeProvider *vp = track; |
| 4165 | fastTrack->mBufferProvider = eabp; |
| 4166 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4167 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4168 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4169 | fastTrack->mGeneration++; |
| 4170 | state->mTrackMask |= 1 << j; |
| 4171 | didModify = true; |
| 4172 | // no acknowledgement required for newly active tracks |
| 4173 | } |
| 4174 | // cache the combined master volume and stream type volume for fast mixer; this |
| 4175 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4176 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4177 | ++fastTracks; |
| 4178 | } else { |
| 4179 | // was it previously active? |
| 4180 | if (state->mTrackMask & (1 << j)) { |
| 4181 | fastTrack->mBufferProvider = NULL; |
| 4182 | fastTrack->mGeneration++; |
| 4183 | state->mTrackMask &= ~(1 << j); |
| 4184 | didModify = true; |
| 4185 | // If any fast tracks were removed, we must wait for acknowledgement |
| 4186 | // because we're about to decrement the last sp<> on those tracks. |
| 4187 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4188 | } else { |
Glenn Kasten | f7d65ee | 2015-12-02 13:45:01 -0800 | [diff] [blame] | 4189 | LOG_ALWAYS_FATAL("fast track %d should have been active; " |
| 4190 | "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d", |
| 4191 | j, track->mState, state->mTrackMask, recentUnderruns, |
| 4192 | track->sharedBuffer() != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4193 | } |
| 4194 | tracksToRemove->add(track); |
| 4195 | // Avoids a misleading display in dumpsys |
| 4196 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 4197 | } |
| 4198 | continue; |
| 4199 | } |
| 4200 | |
| 4201 | { // local variable scope to avoid goto warning |
| 4202 | |
| 4203 | audio_track_cblk_t* cblk = track->cblk(); |
| 4204 | |
| 4205 | // The first time a track is added we wait |
| 4206 | // for all its buffers to be filled before processing it |
| 4207 | int name = track->name(); |
| 4208 | // make sure that we have enough frames to mix one full buffer. |
| 4209 | // enforce this condition only once to enable draining the buffer in case the client |
| 4210 | // app does not call stop() and relies on underrun to stop: |
| 4211 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 4212 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4213 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4214 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4215 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4216 | |
| 4217 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4218 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4219 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 4220 | // add frames already consumed but not yet released by the resampler |
| 4221 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 4222 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 4223 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4224 | uint32_t minFrames = 1; |
| 4225 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 4226 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4227 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4228 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 4229 | |
| 4230 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 4231 | if (ATRACE_ENABLED()) { |
| 4232 | // I wish we had formatted trace names |
| 4233 | char traceName[16]; |
| 4234 | strcpy(traceName, "nRdy"); |
| 4235 | int name = track->name(); |
| 4236 | if (AudioMixer::TRACK0 <= name && |
| 4237 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 4238 | name -= AudioMixer::TRACK0; |
| 4239 | traceName[4] = (name / 10) + '0'; |
| 4240 | traceName[5] = (name % 10) + '0'; |
| 4241 | } else { |
| 4242 | traceName[4] = '?'; |
| 4243 | traceName[5] = '?'; |
| 4244 | } |
| 4245 | traceName[6] = '\0'; |
| 4246 | ATRACE_INT(traceName, framesReady); |
| 4247 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4248 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4249 | !track->isPaused() && !track->isTerminated()) |
| 4250 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4251 | 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] | 4252 | |
| 4253 | mixedTracks++; |
| 4254 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4255 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 4256 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4257 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4258 | if (track->mainBuffer() != mSinkBuffer && |
| 4259 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4260 | if (mEffectBufferEnabled) { |
| 4261 | mEffectBufferValid = true; // Later can set directly. |
| 4262 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4263 | chain = getEffectChain_l(track->sessionId()); |
| 4264 | // Delegate volume control to effect in track effect chain if needed |
| 4265 | if (chain != 0) { |
| 4266 | tracksWithEffect++; |
| 4267 | } else { |
| 4268 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 4269 | "session %d", |
| 4270 | name, track->sessionId()); |
| 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | |
| 4275 | int param = AudioMixer::VOLUME; |
| 4276 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4277 | // no ramp for the first volume setting |
| 4278 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 4279 | if (track->mState == TrackBase::RESUMING) { |
| 4280 | track->mState = TrackBase::ACTIVE; |
| 4281 | param = AudioMixer::RAMP_VOLUME; |
| 4282 | } |
| 4283 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4284 | // FIXME should not make a decision based on mServer |
| 4285 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4286 | // If the track is stopped before the first frame was mixed, |
| 4287 | // do not apply ramp |
| 4288 | param = AudioMixer::RAMP_VOLUME; |
| 4289 | } |
| 4290 | |
| 4291 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4292 | uint32_t vl, vr; // in U8.24 integer format |
| 4293 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 4294 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4295 | vl = vr = 0; |
| 4296 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4297 | if (track->isPausing()) { |
| 4298 | track->setPaused(); |
| 4299 | } |
| 4300 | } else { |
| 4301 | |
| 4302 | // read original volumes with volume control |
| 4303 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4304 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4305 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4306 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4307 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4308 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4309 | // 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] | 4310 | if (vlf > GAIN_FLOAT_UNITY) { |
| 4311 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 4312 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4313 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4314 | if (vrf > GAIN_FLOAT_UNITY) { |
| 4315 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 4316 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4317 | } |
| 4318 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4319 | vlf *= v; |
| 4320 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4321 | // 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] | 4322 | // then derive vl and vr as U8.24 versions for the effect chain |
| 4323 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 4324 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 4325 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 4326 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4327 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4328 | // send level comes from shared memory and so may be corrupt |
| 4329 | if (sendLevel > MAX_GAIN_INT) { |
| 4330 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 4331 | sendLevel = MAX_GAIN_INT; |
| 4332 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4333 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 4334 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4335 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4336 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4337 | // Delegate volume control to effect in track effect chain if needed |
| 4338 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 4339 | // Do not ramp volume if volume is controlled by effect |
| 4340 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 4341 | // Update remaining floating point volume levels |
| 4342 | vlf = (float)vl / (1 << 24); |
| 4343 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4344 | track->mHasVolumeController = true; |
| 4345 | } else { |
| 4346 | // force no volume ramp when volume controller was just disabled or removed |
| 4347 | // from effect chain to avoid volume spike |
| 4348 | if (track->mHasVolumeController) { |
| 4349 | param = AudioMixer::VOLUME; |
| 4350 | } |
| 4351 | track->mHasVolumeController = false; |
| 4352 | } |
| 4353 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4354 | // XXX: these things DON'T need to be done each time |
| 4355 | mAudioMixer->setBufferProvider(name, track); |
| 4356 | mAudioMixer->enable(name); |
| 4357 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 4358 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 4359 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 4360 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4361 | mAudioMixer->setParameter( |
| 4362 | name, |
| 4363 | AudioMixer::TRACK, |
| 4364 | AudioMixer::FORMAT, (void *)track->format()); |
| 4365 | mAudioMixer->setParameter( |
| 4366 | name, |
| 4367 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4368 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4369 | mAudioMixer->setParameter( |
| 4370 | name, |
| 4371 | AudioMixer::TRACK, |
| 4372 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4373 | // 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] | 4374 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4375 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4376 | if (reqSampleRate == 0) { |
| 4377 | reqSampleRate = mSampleRate; |
| 4378 | } else if (reqSampleRate > maxSampleRate) { |
| 4379 | reqSampleRate = maxSampleRate; |
| 4380 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4381 | mAudioMixer->setParameter( |
| 4382 | name, |
| 4383 | AudioMixer::RESAMPLE, |
| 4384 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4385 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4386 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4387 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4388 | mAudioMixer->setParameter( |
| 4389 | name, |
| 4390 | AudioMixer::TIMESTRETCH, |
| 4391 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4392 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4393 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4394 | /* |
| 4395 | * Select the appropriate output buffer for the track. |
| 4396 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4397 | * Tracks with effects go into their own effects chain buffer |
| 4398 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4399 | * |
| 4400 | * Other tracks can use mMixerBuffer for higher precision |
| 4401 | * channel accumulation. If this buffer is enabled |
| 4402 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4403 | * into it. |
| 4404 | * |
| 4405 | */ |
| 4406 | if (mMixerBufferEnabled |
| 4407 | && (track->mainBuffer() == mSinkBuffer |
| 4408 | || track->mainBuffer() == mMixerBuffer)) { |
| 4409 | mAudioMixer->setParameter( |
| 4410 | name, |
| 4411 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4412 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4413 | mAudioMixer->setParameter( |
| 4414 | name, |
| 4415 | AudioMixer::TRACK, |
| 4416 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4417 | // TODO: override track->mainBuffer()? |
| 4418 | mMixerBufferValid = true; |
| 4419 | } else { |
| 4420 | mAudioMixer->setParameter( |
| 4421 | name, |
| 4422 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4423 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4424 | mAudioMixer->setParameter( |
| 4425 | name, |
| 4426 | AudioMixer::TRACK, |
| 4427 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4428 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4429 | mAudioMixer->setParameter( |
| 4430 | name, |
| 4431 | AudioMixer::TRACK, |
| 4432 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4433 | |
| 4434 | // reset retry count |
| 4435 | track->mRetryCount = kMaxTrackRetries; |
| 4436 | |
| 4437 | // If one track is ready, set the mixer ready if: |
| 4438 | // - the mixer was not ready during previous round OR |
| 4439 | // - no other track is not ready |
| 4440 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4441 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4442 | mixerStatus = MIXER_TRACKS_READY; |
| 4443 | } |
| 4444 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4445 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4446 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4447 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4448 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4449 | } else { |
| 4450 | track->mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4451 | } |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 4452 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4453 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4454 | // previous audio buffer again to effects |
| 4455 | chain = getEffectChain_l(track->sessionId()); |
| 4456 | if (chain != 0) { |
| 4457 | chain->clearInputBuffer(); |
| 4458 | } |
| 4459 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4460 | 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] | 4461 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4462 | track->isStopped() || track->isPaused()) { |
| 4463 | // We have consumed all the buffers of this track. |
| 4464 | // Remove it from the list of active tracks. |
| 4465 | // TODO: use actual buffer filling status instead of latency when available from |
| 4466 | // audio HAL |
| 4467 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 4468 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4469 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4470 | if (track->isStopped()) { |
| 4471 | track->reset(); |
| 4472 | } |
| 4473 | tracksToRemove->add(track); |
| 4474 | } |
| 4475 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4476 | // No buffers for this track. Give it a few chances to |
| 4477 | // fill a buffer, then remove it from active list. |
| 4478 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4479 | 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] | 4480 | tracksToRemove->add(track); |
| 4481 | // indicate to client process that the track was disabled because of underrun; |
| 4482 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 4483 | track->disable(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4484 | // If one track is not ready, mark the mixer also not ready if: |
| 4485 | // - the mixer was ready during previous round OR |
| 4486 | // - no other track is ready |
| 4487 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4488 | mixerStatus != MIXER_TRACKS_READY) { |
| 4489 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4490 | } |
| 4491 | } |
| 4492 | mAudioMixer->disable(name); |
| 4493 | } |
| 4494 | |
| 4495 | } // local variable scope to avoid goto warning |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4496 | |
| 4497 | } |
| 4498 | |
| 4499 | // Push the new FastMixer state if necessary |
| 4500 | bool pauseAudioWatchdog = false; |
| 4501 | if (didModify) { |
| 4502 | state->mFastTracksGen++; |
| 4503 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4504 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4505 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4506 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4507 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4508 | state->mColdGen++; |
| 4509 | mFastMixerFutex = 0; |
| 4510 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4511 | mNormalSink = mOutputSink; |
| 4512 | } |
| 4513 | // If we go into cold idle, need to wait for acknowledgement |
| 4514 | // so that fast mixer stops doing I/O. |
| 4515 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4516 | pauseAudioWatchdog = true; |
| 4517 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4518 | } |
| 4519 | if (sq != NULL) { |
| 4520 | sq->end(didModify); |
| 4521 | sq->push(block); |
| 4522 | } |
| 4523 | #ifdef AUDIO_WATCHDOG |
| 4524 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4525 | mAudioWatchdog->pause(); |
| 4526 | } |
| 4527 | #endif |
| 4528 | |
| 4529 | // Now perform the deferred reset on fast tracks that have stopped |
| 4530 | while (resetMask != 0) { |
| 4531 | size_t i = __builtin_ctz(resetMask); |
| 4532 | ALOG_ASSERT(i < count); |
| 4533 | resetMask &= ~(1 << i); |
| 4534 | sp<Track> t = mActiveTracks[i].promote(); |
| 4535 | if (t == 0) { |
| 4536 | continue; |
| 4537 | } |
| 4538 | Track* track = t.get(); |
| 4539 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4540 | track->reset(); |
| 4541 | } |
| 4542 | |
| 4543 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4544 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4545 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4546 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4547 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4551 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4552 | // passing a non-clean buffer to the effect chain |
| 4553 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4554 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4555 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4556 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4557 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4558 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4559 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4560 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4561 | if (mMixerBufferValid) { |
| 4562 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4563 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4564 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4565 | // after mixing. |
| 4566 | // |
| 4567 | // To enforce this guarantee: |
| 4568 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4569 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4570 | // must imply MIXER_TRACKS_READY. |
| 4571 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4572 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4573 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4574 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | // if any fast tracks, then status is ready |
| 4578 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4579 | if (fastTracks > 0) { |
| 4580 | mixerStatus = MIXER_TRACKS_READY; |
| 4581 | } |
| 4582 | return mixerStatus; |
| 4583 | } |
| 4584 | |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4585 | // trackCountForUid_l() must be called with ThreadBase::mLock held |
| 4586 | uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) |
| 4587 | { |
| 4588 | uint32_t trackCount = 0; |
| 4589 | for (size_t i = 0; i < mTracks.size() ; i++) { |
| 4590 | if (mTracks[i]->uid() == (int)uid) { |
| 4591 | trackCount++; |
| 4592 | } |
| 4593 | } |
| 4594 | return trackCount; |
| 4595 | } |
| 4596 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4597 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4598 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4599 | audio_format_t format, audio_session_t sessionId, uid_t uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4600 | { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4601 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { |
| 4602 | return -1; |
| 4603 | } |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4604 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4608 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4609 | { |
| 4610 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4611 | mAudioMixer->deleteTrackName(name); |
| 4612 | } |
| 4613 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4614 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4615 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4616 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4617 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4618 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4619 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4620 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4621 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4622 | |
Glenn Kasten | c05b8d7 | 2016-03-24 09:48:17 -0700 | [diff] [blame] | 4623 | AutoPark<FastMixer> park(mFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4624 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4625 | AudioParameter param = AudioParameter(keyValuePair); |
| 4626 | int value; |
| 4627 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4628 | reconfig = true; |
| 4629 | } |
| 4630 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4631 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4632 | status = BAD_VALUE; |
| 4633 | } else { |
| 4634 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4635 | reconfig = true; |
| 4636 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4637 | } |
| 4638 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4639 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4640 | status = BAD_VALUE; |
| 4641 | } else { |
| 4642 | // no need to save value, since it's constant |
| 4643 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4644 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4645 | } |
| 4646 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4647 | // do not accept frame count changes if tracks are open as the track buffer |
| 4648 | // size depends on frame count and correct behavior would not be guaranteed |
| 4649 | // if frame count is changed after track creation |
| 4650 | if (!mTracks.isEmpty()) { |
| 4651 | status = INVALID_OPERATION; |
| 4652 | } else { |
| 4653 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4654 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4655 | } |
| 4656 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4657 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4658 | // when changing the audio output device, call addBatteryData to notify |
| 4659 | // the change |
| 4660 | if (mOutDevice != value) { |
| 4661 | uint32_t params = 0; |
| 4662 | // check whether speaker is on |
| 4663 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4664 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4665 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4666 | |
| 4667 | audio_devices_t deviceWithoutSpeaker |
| 4668 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4669 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4670 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4671 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4672 | } |
| 4673 | |
| 4674 | if (params != 0) { |
| 4675 | addBatteryData(params); |
| 4676 | } |
| 4677 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4678 | #endif |
| 4679 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4680 | // forward device change to effects that have requested to be |
| 4681 | // aware of attached audio device. |
| 4682 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4683 | a2dpDeviceChanged = |
| 4684 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4685 | mOutDevice = value; |
| 4686 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4687 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4688 | } |
| 4689 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4690 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4691 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4692 | if (status == NO_ERROR) { |
| 4693 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4694 | keyValuePair.string()); |
| 4695 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4696 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4697 | mStandby = true; |
| 4698 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4699 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4700 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4701 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4702 | if (status == NO_ERROR && reconfig) { |
| 4703 | readOutputParameters_l(); |
| 4704 | delete mAudioMixer; |
| 4705 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4706 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4707 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 4708 | mTracks[i]->mFormat, mTracks[i]->mSessionId, mTracks[i]->uid()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4709 | if (name < 0) { |
| 4710 | break; |
| 4711 | } |
| 4712 | mTracks[i]->mName = name; |
| 4713 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4714 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4715 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4716 | } |
| 4717 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 4718 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | |
| 4722 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4723 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4724 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4725 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4726 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4727 | dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4728 | |
| 4729 | // 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] | 4730 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 4731 | // This is a large object so we place it on the heap. |
| 4732 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 4733 | const FastMixerDumpState *copy = new FastMixerDumpState(mFastMixerDumpState); |
| 4734 | copy->dump(fd); |
| 4735 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4736 | |
| 4737 | #ifdef STATE_QUEUE_DUMP |
| 4738 | // Similar for state queue |
| 4739 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4740 | observerCopy.dump(fd); |
| 4741 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4742 | mutatorCopy.dump(fd); |
| 4743 | #endif |
| 4744 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4745 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4746 | // Write the tee output to a .wav file |
| 4747 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4748 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4749 | |
| 4750 | #ifdef AUDIO_WATCHDOG |
| 4751 | if (mAudioWatchdog != 0) { |
| 4752 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4753 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4754 | wdCopy.dump(fd); |
| 4755 | } |
| 4756 | #endif |
| 4757 | } |
| 4758 | |
| 4759 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4760 | { |
| 4761 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4762 | } |
| 4763 | |
| 4764 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4765 | { |
| 4766 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4767 | } |
| 4768 | |
| 4769 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4770 | { |
| 4771 | PlaybackThread::cacheParameters_l(); |
| 4772 | |
| 4773 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4774 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4775 | // increase threshold again due to low power audio mode. The way this warning |
| 4776 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4777 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4778 | } |
| 4779 | |
| 4780 | // ---------------------------------------------------------------------------- |
| 4781 | |
| 4782 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4783 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 4784 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4785 | // mLeftVolFloat, mRightVolFloat |
| 4786 | { |
| 4787 | } |
| 4788 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4789 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4790 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 4791 | ThreadBase::type_t type, bool systemReady) |
| 4792 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4793 | // mLeftVolFloat, mRightVolFloat |
| 4794 | { |
| 4795 | } |
| 4796 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4797 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4798 | { |
| 4799 | } |
| 4800 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4801 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4802 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4803 | float left, right; |
| 4804 | |
| 4805 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4806 | left = right = 0; |
| 4807 | } else { |
| 4808 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4809 | float v = mMasterVolume * typeVolume; |
| 4810 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4811 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4812 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4813 | if (left > GAIN_FLOAT_UNITY) { |
| 4814 | left = GAIN_FLOAT_UNITY; |
| 4815 | } |
| 4816 | left *= v; |
| 4817 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4818 | if (right > GAIN_FLOAT_UNITY) { |
| 4819 | right = GAIN_FLOAT_UNITY; |
| 4820 | } |
| 4821 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | if (lastTrack) { |
| 4825 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4826 | mLeftVolFloat = left; |
| 4827 | mRightVolFloat = right; |
| 4828 | |
| 4829 | // Convert volumes from float to 8.24 |
| 4830 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4831 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4832 | |
| 4833 | // Delegate volume control to effect in track effect chain if needed |
| 4834 | // only one effect chain can be present on DirectOutputThread, so if |
| 4835 | // there is one, the track is connected to it |
| 4836 | if (!mEffectChains.isEmpty()) { |
| 4837 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4838 | left = (float)vl / (1 << 24); |
| 4839 | right = (float)vr / (1 << 24); |
| 4840 | } |
| 4841 | if (mOutput->stream->set_volume) { |
| 4842 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4843 | } |
| 4844 | } |
| 4845 | } |
| 4846 | } |
| 4847 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4848 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4849 | { |
| 4850 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4851 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4852 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4853 | if (previousTrack != 0 && latestTrack != 0) { |
| 4854 | if (mType == DIRECT) { |
| 4855 | if (previousTrack.get() != latestTrack.get()) { |
| 4856 | mFlushPending = true; |
| 4857 | } |
| 4858 | } else /* mType == OFFLOAD */ { |
| 4859 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4860 | mFlushPending = true; |
| 4861 | } |
| 4862 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4863 | } |
| 4864 | PlaybackThread::onAddNewTrack_l(); |
| 4865 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4866 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4867 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4868 | Vector< sp<Track> > *tracksToRemove |
| 4869 | ) |
| 4870 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4871 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4872 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4873 | bool doHwPause = false; |
| 4874 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4875 | |
| 4876 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4877 | for (size_t i = 0; i < count; i++) { |
| 4878 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4879 | // The track died recently |
| 4880 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4881 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4882 | } |
| 4883 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4884 | if (t->isInvalid()) { |
| 4885 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4886 | tracksToRemove->add(t); |
| 4887 | continue; |
| 4888 | } |
| 4889 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4890 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4891 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4892 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 4893 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4894 | // Only consider last track started for volume and mixer state control. |
| 4895 | // In theory an older track could underrun and restart after the new one starts |
| 4896 | // but as we only care about the transition phase between two tracks on a |
| 4897 | // direct output, it is not a problem to ignore the underrun case. |
| 4898 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4899 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4900 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4901 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4902 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4903 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4904 | doHwPause = true; |
| 4905 | mHwPaused = true; |
| 4906 | } |
| 4907 | tracksToRemove->add(track); |
| 4908 | } else if (track->isFlushPending()) { |
| 4909 | track->flushAck(); |
| 4910 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4911 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4912 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4913 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4914 | track->resumeAck(); |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4915 | if (last) { |
| 4916 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 4917 | if (mHwPaused) { |
| 4918 | doHwResume = true; |
| 4919 | mHwPaused = false; |
| 4920 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4924 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4925 | // for all its buffers to be filled before processing it. |
| 4926 | // Allow draining the buffer in case the client |
| 4927 | // app does not call stop() and relies on underrun to stop: |
| 4928 | // hence the test on (track->mRetryCount > 1). |
| 4929 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4930 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4931 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4932 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4933 | && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4934 | minFrames = mNormalFrameCount; |
| 4935 | } else { |
| 4936 | minFrames = 1; |
| 4937 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4938 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4939 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4940 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4941 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4942 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4943 | |
| 4944 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4945 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 4946 | if (last) { |
| 4947 | // make sure processVolume_l() will apply new volume even if 0 |
| 4948 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 4949 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4950 | if (!mHwSupportsPause) { |
| 4951 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4956 | processVolume_l(track, last); |
| 4957 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4958 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4959 | if (previousTrack != 0) { |
| 4960 | if (track != previousTrack.get()) { |
| 4961 | // Flush any data still being written from last track |
| 4962 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4963 | // Invalidate previous track to force a seek when resuming. |
| 4964 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4965 | } |
| 4966 | } |
| 4967 | mPreviousTrack = track; |
| 4968 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4969 | // reset retry count |
| 4970 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4971 | mActiveTrack = t; |
| 4972 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4973 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4974 | doHwResume = true; |
| 4975 | mHwPaused = false; |
| 4976 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4977 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4978 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4979 | // clear effect chain input buffer if the last active track started underruns |
| 4980 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4981 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4982 | mEffectChains[0]->clearInputBuffer(); |
| 4983 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4984 | if (track->isStopping_1()) { |
| 4985 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4986 | if (last && mHwPaused) { |
| 4987 | doHwResume = true; |
| 4988 | mHwPaused = false; |
| 4989 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4990 | } |
| 4991 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4992 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4993 | // We have consumed all the buffers of this track. |
| 4994 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4995 | size_t audioHALFrames; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 4996 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4997 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4998 | } else { |
| 4999 | audioHALFrames = 0; |
| 5000 | } |
| 5001 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5002 | int64_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5003 | if (mStandby || !last || |
| 5004 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 5005 | if (track->isStopping_2()) { |
| 5006 | track->mState = TrackBase::STOPPED; |
| 5007 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5008 | if (track->isStopped()) { |
| 5009 | track->reset(); |
| 5010 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5011 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5012 | } |
| 5013 | } else { |
| 5014 | // No buffers for this track. Give it a few chances to |
| 5015 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5016 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5017 | if (--(track->mRetryCount) <= 0) { |
| 5018 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 5019 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5020 | // indicate to client process that the track was disabled because of underrun; |
| 5021 | // it will then automatically call start() when data is available |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 5022 | track->disable(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5023 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 5024 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 5025 | "minFrames = %u, mFormat = %#x", |
| 5026 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5027 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5028 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5029 | doHwPause = true; |
| 5030 | mHwPaused = true; |
| 5031 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5037 | // 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] | 5038 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5039 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5040 | if (mTracks[i]->isFlushPending()) { |
| 5041 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5042 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5043 | } |
| 5044 | } |
| 5045 | } |
| 5046 | |
| 5047 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5048 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5049 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5050 | // if resume is received before pause is executed. |
| 5051 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5052 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5053 | mOutput->stream->pause(mOutput->stream); |
| 5054 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5055 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5056 | flushHw_l(); |
| 5057 | } |
| 5058 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 5059 | mOutput->stream->resume(mOutput->stream); |
| 5060 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5061 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5062 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5063 | |
| 5064 | return mixerStatus; |
| 5065 | } |
| 5066 | |
| 5067 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 5068 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5069 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5070 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5071 | // output audio to hardware |
| 5072 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 5073 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5074 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5075 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 5076 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5077 | // no need to pad with 0 for compressed audio |
| 5078 | if (audio_has_proportional_frames(mFormat)) { |
| 5079 | memset(curBuf, 0, frameCount * mFrameSize); |
| 5080 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5081 | break; |
| 5082 | } |
| 5083 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 5084 | frameCount -= buffer.frameCount; |
| 5085 | curBuf += buffer.frameCount * mFrameSize; |
| 5086 | mActiveTrack->releaseBuffer(&buffer); |
| 5087 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5088 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5089 | mSleepTimeUs = 0; |
| 5090 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5091 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 5095 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5096 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 5097 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5098 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5099 | return; |
| 5100 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5101 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5102 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5103 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5104 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5105 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5106 | } |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5107 | } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 5108 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5109 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5110 | } |
| 5111 | } |
| 5112 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5113 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 5114 | { |
| 5115 | { |
| 5116 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5117 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5118 | if (mTracks[i]->isFlushPending()) { |
| 5119 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5120 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5121 | } |
| 5122 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5123 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5124 | flushHw_l(); |
| 5125 | } |
| 5126 | } |
| 5127 | PlaybackThread::threadLoop_exit(); |
| 5128 | } |
| 5129 | |
| 5130 | // must be called with thread mutex locked |
| 5131 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 5132 | { |
| 5133 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5134 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5135 | |
vivek mehta | 9cd7ad1 | 2016-03-17 00:18:29 -0700 | [diff] [blame] | 5136 | if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) { |
| 5137 | return !mStandby; |
| 5138 | } |
| 5139 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5140 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 5141 | // after a timeout and we will enter standby then. |
| 5142 | if (mTracks.size() > 0) { |
| 5143 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5144 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 5145 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5146 | } |
| 5147 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5148 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5149 | } |
| 5150 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5151 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5152 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5153 | audio_format_t format __unused, audio_session_t sessionId __unused, uid_t uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5154 | { |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 5155 | if (trackCountForUid_l(uid) > (PlaybackThread::kMaxTracksPerUid - 1)) { |
| 5156 | return -1; |
| 5157 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5158 | return 0; |
| 5159 | } |
| 5160 | |
| 5161 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 5162 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5163 | { |
| 5164 | } |
| 5165 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5166 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 5167 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 5168 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5169 | { |
| 5170 | bool reconfig = false; |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5171 | bool a2dpDeviceChanged = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5172 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5173 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5174 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5175 | AudioParameter param = AudioParameter(keyValuePair); |
| 5176 | int value; |
| 5177 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 5178 | // forward device change to effects that have requested to be |
| 5179 | // aware of attached audio device. |
| 5180 | if (value != AUDIO_DEVICE_NONE) { |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5181 | a2dpDeviceChanged = |
| 5182 | (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5183 | mOutDevice = value; |
| 5184 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 5185 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 5186 | } |
| 5187 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5188 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5189 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 5190 | // do not accept frame count changes if tracks are open as the track buffer |
| 5191 | // size depends on frame count and correct behavior would not be garantied |
| 5192 | // if frame count is changed after track creation |
| 5193 | if (!mTracks.isEmpty()) { |
| 5194 | status = INVALID_OPERATION; |
| 5195 | } else { |
| 5196 | reconfig = true; |
| 5197 | } |
| 5198 | } |
| 5199 | if (status == NO_ERROR) { |
| 5200 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 5201 | keyValuePair.string()); |
| 5202 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5203 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5204 | mStandby = true; |
| 5205 | mBytesWritten = 0; |
| 5206 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 5207 | keyValuePair.string()); |
| 5208 | } |
| 5209 | if (status == NO_ERROR && reconfig) { |
| 5210 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 5211 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5212 | } |
| 5213 | } |
| 5214 | |
Eric Laurent | 42537be | 2016-01-08 17:16:42 -0800 | [diff] [blame] | 5215 | return reconfig || a2dpDeviceChanged; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 5219 | { |
| 5220 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5221 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5222 | time = PlaybackThread::activeSleepTimeUs(); |
| 5223 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5224 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5225 | } |
| 5226 | return time; |
| 5227 | } |
| 5228 | |
| 5229 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 5230 | { |
| 5231 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5232 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5233 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 5234 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5235 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5236 | } |
| 5237 | return time; |
| 5238 | } |
| 5239 | |
| 5240 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 5241 | { |
| 5242 | uint32_t time; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5243 | if (audio_has_proportional_frames(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5244 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 5245 | } else { |
Eric Laurent | 5171618 | 2016-02-29 18:00:56 -0800 | [diff] [blame] | 5246 | time = kDirectMinSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5247 | } |
| 5248 | return time; |
| 5249 | } |
| 5250 | |
| 5251 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 5252 | { |
| 5253 | PlaybackThread::cacheParameters_l(); |
| 5254 | |
| 5255 | // use shorter standby delay as on normal output to release |
| 5256 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 5257 | // no delay on outputs with HW A/V sync |
| 5258 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5259 | mStandbyDelayNs = 0; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 5260 | } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5261 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5262 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5263 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5264 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5265 | } |
| 5266 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5267 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 5268 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5269 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 5270 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 5271 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5272 | } |
| 5273 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5274 | // ---------------------------------------------------------------------------- |
| 5275 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5276 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5277 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5278 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5279 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5280 | mWriteAckSequence(0), |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5281 | mDrainSequence(0), |
| 5282 | mAsyncError(false) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5283 | { |
| 5284 | } |
| 5285 | |
| 5286 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 5287 | { |
| 5288 | } |
| 5289 | |
| 5290 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 5291 | { |
| 5292 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5293 | } |
| 5294 | |
| 5295 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 5296 | { |
| 5297 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5298 | uint32_t writeAckSequence; |
| 5299 | uint32_t drainSequence; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5300 | bool asyncError; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5301 | |
| 5302 | { |
| 5303 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5304 | while (!((mWriteAckSequence & 1) || |
| 5305 | (mDrainSequence & 1) || |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5306 | mAsyncError || |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 5307 | exitPending())) { |
| 5308 | mWaitWorkCV.wait(mLock); |
| 5309 | } |
| 5310 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5311 | if (exitPending()) { |
| 5312 | break; |
| 5313 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5314 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 5315 | mWriteAckSequence, mDrainSequence); |
| 5316 | writeAckSequence = mWriteAckSequence; |
| 5317 | mWriteAckSequence &= ~1; |
| 5318 | drainSequence = mDrainSequence; |
| 5319 | mDrainSequence &= ~1; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5320 | asyncError = mAsyncError; |
| 5321 | mAsyncError = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5322 | } |
| 5323 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5324 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 5325 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5326 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5327 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5328 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5329 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 5330 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5331 | } |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5332 | if (asyncError) { |
| 5333 | playbackThread->onAsyncError(); |
| 5334 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | return false; |
| 5339 | } |
| 5340 | |
| 5341 | void AudioFlinger::AsyncCallbackThread::exit() |
| 5342 | { |
| 5343 | ALOGV("AsyncCallbackThread::exit"); |
| 5344 | Mutex::Autolock _l(mLock); |
| 5345 | requestExit(); |
| 5346 | mWaitWorkCV.broadcast(); |
| 5347 | } |
| 5348 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5349 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5350 | { |
| 5351 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5352 | // bit 0 is cleared |
| 5353 | mWriteAckSequence = sequence << 1; |
| 5354 | } |
| 5355 | |
| 5356 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 5357 | { |
| 5358 | Mutex::Autolock _l(mLock); |
| 5359 | // ignore unexpected callbacks |
| 5360 | if (mWriteAckSequence & 2) { |
| 5361 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5362 | mWaitWorkCV.signal(); |
| 5363 | } |
| 5364 | } |
| 5365 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5366 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5367 | { |
| 5368 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5369 | // bit 0 is cleared |
| 5370 | mDrainSequence = sequence << 1; |
| 5371 | } |
| 5372 | |
| 5373 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 5374 | { |
| 5375 | Mutex::Autolock _l(mLock); |
| 5376 | // ignore unexpected callbacks |
| 5377 | if (mDrainSequence & 2) { |
| 5378 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5379 | mWaitWorkCV.signal(); |
| 5380 | } |
| 5381 | } |
| 5382 | |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 5383 | void AudioFlinger::AsyncCallbackThread::setAsyncError() |
| 5384 | { |
| 5385 | Mutex::Autolock _l(mLock); |
| 5386 | mAsyncError = true; |
| 5387 | mWaitWorkCV.signal(); |
| 5388 | } |
| 5389 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5390 | |
| 5391 | // ---------------------------------------------------------------------------- |
| 5392 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5393 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5394 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5395 | mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true), |
| 5396 | mOffloadUnderrunPosition(~0LL) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5397 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5398 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5399 | mStandby = true; |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 5400 | mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5401 | } |
| 5402 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5403 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5404 | { |
| 5405 | if (mFlushPending || mHwPaused) { |
| 5406 | // If a flush is pending or track was paused, just discard buffered data |
| 5407 | flushHw_l(); |
| 5408 | } else { |
| 5409 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5410 | threadLoop_drain(); |
| 5411 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5412 | if (mUseAsyncWrite) { |
| 5413 | ALOG_ASSERT(mCallbackThread != 0); |
| 5414 | mCallbackThread->exit(); |
| 5415 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5416 | PlaybackThread::threadLoop_exit(); |
| 5417 | } |
| 5418 | |
| 5419 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5420 | Vector< sp<Track> > *tracksToRemove |
| 5421 | ) |
| 5422 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5423 | size_t count = mActiveTracks.size(); |
| 5424 | |
| 5425 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5426 | bool doHwPause = false; |
| 5427 | bool doHwResume = false; |
| 5428 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 5429 | ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5430 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5431 | // find out which tracks need to be processed |
| 5432 | for (size_t i = 0; i < count; i++) { |
| 5433 | sp<Track> t = mActiveTracks[i].promote(); |
| 5434 | // The track died recently |
| 5435 | if (t == 0) { |
| 5436 | continue; |
| 5437 | } |
| 5438 | Track* const track = t.get(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5439 | #ifdef VERY_VERY_VERBOSE_LOGGING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5440 | audio_track_cblk_t* cblk = track->cblk(); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5441 | #endif |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5442 | // Only consider last track started for volume and mixer state control. |
| 5443 | // In theory an older track could underrun and restart after the new one starts |
| 5444 | // but as we only care about the transition phase between two tracks on a |
| 5445 | // direct output, it is not a problem to ignore the underrun case. |
| 5446 | sp<Track> l = mLatestActiveTrack.promote(); |
| 5447 | bool last = l.get() == track; |
| 5448 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5449 | if (track->isInvalid()) { |
| 5450 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5451 | tracksToRemove->add(track); |
| 5452 | continue; |
| 5453 | } |
| 5454 | |
| 5455 | if (track->mState == TrackBase::IDLE) { |
| 5456 | ALOGW("An idle track shouldn't be in active list"); |
| 5457 | continue; |
| 5458 | } |
| 5459 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5460 | if (track->isPausing()) { |
| 5461 | track->setPaused(); |
| 5462 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5463 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5464 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5465 | mHwPaused = true; |
| 5466 | } |
| 5467 | // If we were part way through writing the mixbuffer to |
| 5468 | // the HAL we must save this until we resume |
| 5469 | // BUG - this will be wrong if a different track is made active, |
| 5470 | // in that case we want to discard the pending data in the |
| 5471 | // mixbuffer and tell the client to present it again when the |
| 5472 | // track is resumed |
| 5473 | mPausedWriteLength = mCurrentWriteLength; |
| 5474 | mPausedBytesRemaining = mBytesRemaining; |
| 5475 | mBytesRemaining = 0; // stop writing |
| 5476 | } |
| 5477 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5478 | } else if (track->isFlushPending()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5479 | if (track->isStopping_1()) { |
| 5480 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5481 | } else { |
| 5482 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5483 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5484 | track->flushAck(); |
| 5485 | if (last) { |
| 5486 | mFlushPending = true; |
| 5487 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5488 | } else if (track->isResumePending()){ |
| 5489 | track->resumeAck(); |
| 5490 | if (last) { |
| 5491 | if (mPausedBytesRemaining) { |
| 5492 | // Need to continue write that was interrupted |
| 5493 | mCurrentWriteLength = mPausedWriteLength; |
| 5494 | mBytesRemaining = mPausedBytesRemaining; |
| 5495 | mPausedBytesRemaining = 0; |
| 5496 | } |
| 5497 | if (mHwPaused) { |
| 5498 | doHwResume = true; |
| 5499 | mHwPaused = false; |
| 5500 | // threadLoop_mix() will handle the case that we need to |
| 5501 | // resume an interrupted write |
| 5502 | } |
| 5503 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5504 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5505 | |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5506 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5507 | |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5508 | // Do not handle new data in this iteration even if track->framesReady() |
| 5509 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5510 | } |
| 5511 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5512 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5513 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5514 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5515 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 3df841a | 2016-07-15 15:15:40 -0700 | [diff] [blame] | 5516 | if (last) { |
| 5517 | // make sure processVolume_l() will apply new volume even if 0 |
| 5518 | mLeftVolFloat = mRightVolFloat = -1.0; |
| 5519 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5523 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5524 | if (previousTrack != 0) { |
| 5525 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5526 | // Flush any data still being written from last track |
| 5527 | mBytesRemaining = 0; |
| 5528 | if (mPausedBytesRemaining) { |
| 5529 | // Last track was paused so we also need to flush saved |
| 5530 | // mixbuffer state and invalidate track so that it will |
| 5531 | // re-submit that unwritten data when it is next resumed |
| 5532 | mPausedBytesRemaining = 0; |
| 5533 | // Invalidate is a bit drastic - would be more efficient |
| 5534 | // to have a flag to tell client that some of the |
| 5535 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5536 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5537 | } |
| 5538 | // flush data already sent to the DSP if changing audio session as audio |
| 5539 | // comes from a different source. Also invalidate previous track to force a |
| 5540 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5541 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5542 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5547 | // reset retry count |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5548 | if (track->isStopping_1()) { |
| 5549 | track->mRetryCount = kMaxTrackStopRetriesOffload; |
| 5550 | } else { |
| 5551 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5552 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5553 | mActiveTrack = t; |
| 5554 | mixerStatus = MIXER_TRACKS_READY; |
| 5555 | } |
| 5556 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5557 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5558 | if (track->isStopping_1()) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5559 | if (--(track->mRetryCount) <= 0) { |
| 5560 | // Hardware buffer can hold a large amount of audio so we must |
| 5561 | // wait for all current track's data to drain before we say |
| 5562 | // that the track is stopped. |
| 5563 | if (mBytesRemaining == 0) { |
| 5564 | // Only start draining when all data in mixbuffer |
| 5565 | // has been written |
| 5566 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5567 | track->mState = TrackBase::STOPPING_2; // so presentation completes after |
| 5568 | // drain do not drain if no data was ever sent to HAL (mStandby == true) |
| 5569 | if (last && !mStandby) { |
| 5570 | // do not modify drain sequence if we are already draining. This happens |
| 5571 | // when resuming from pause after drain. |
| 5572 | if ((mDrainSequence & 1) == 0) { |
| 5573 | mSleepTimeUs = 0; |
| 5574 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 5575 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5576 | mDrainSequence += 2; |
| 5577 | } |
| 5578 | if (mHwPaused) { |
| 5579 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5580 | // a resume so we must ensure hardware is running |
| 5581 | doHwResume = true; |
| 5582 | mHwPaused = false; |
| 5583 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5584 | } |
| 5585 | } |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 5586 | } else if (last) { |
| 5587 | ALOGV("stopping1 underrun retries left %d", track->mRetryCount); |
| 5588 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5589 | } |
| 5590 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5591 | // Drain has completed or we are in standby, signal presentation complete |
| 5592 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5593 | track->mState = TrackBase::STOPPED; |
| 5594 | size_t audioHALFrames = |
| 5595 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 5596 | int64_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5597 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5598 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5599 | track->reset(); |
| 5600 | tracksToRemove->add(track); |
| 5601 | } |
| 5602 | } else { |
| 5603 | // No buffers for this track. Give it a few chances to |
| 5604 | // fill a buffer, then remove it from active list. |
| 5605 | if (--(track->mRetryCount) <= 0) { |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5606 | bool running = false; |
| 5607 | if (mOutput->stream->get_presentation_position != nullptr) { |
| 5608 | uint64_t position = 0; |
| 5609 | struct timespec unused; |
| 5610 | // The running check restarts the retry counter at least once. |
| 5611 | int ret = mOutput->stream->get_presentation_position( |
| 5612 | mOutput->stream, &position, &unused); |
| 5613 | if (ret == NO_ERROR && position != mOffloadUnderrunPosition) { |
| 5614 | running = true; |
| 5615 | mOffloadUnderrunPosition = position; |
| 5616 | } |
| 5617 | ALOGVV("underrun counter, running(%d): %lld vs %lld", running, |
| 5618 | (long long)position, (long long)mOffloadUnderrunPosition); |
| 5619 | } |
| 5620 | if (running) { // still running, give us more time. |
| 5621 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5622 | } else { |
| 5623 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5624 | track->name()); |
| 5625 | tracksToRemove->add(track); |
| 5626 | // indicate to client process that the track was disabled because of underrun; |
| 5627 | // it will then automatically call start() when data is available |
| 5628 | track->disable(); |
| 5629 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5630 | } else if (last){ |
| 5631 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | // compute volume for this track |
| 5636 | processVolume_l(track, last); |
| 5637 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5638 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5639 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5640 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5641 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5642 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5643 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5644 | mOutput->stream->pause(mOutput->stream); |
| 5645 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5646 | if (mFlushPending) { |
| 5647 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5648 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5649 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5650 | mOutput->stream->resume(mOutput->stream); |
| 5651 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5652 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5653 | // remove all the tracks that need to be... |
| 5654 | removeTracks_l(*tracksToRemove); |
| 5655 | |
| 5656 | return mixerStatus; |
| 5657 | } |
| 5658 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5659 | // must be called with thread mutex locked |
| 5660 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5661 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5662 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5663 | mWriteAckSequence, mDrainSequence); |
| 5664 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5665 | return true; |
| 5666 | } |
| 5667 | return false; |
| 5668 | } |
| 5669 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5670 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5671 | { |
| 5672 | Mutex::Autolock _l(mLock); |
| 5673 | return waitingAsyncCallback_l(); |
| 5674 | } |
| 5675 | |
| 5676 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5677 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5678 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5679 | // Flush anything still waiting in the mixbuffer |
| 5680 | mCurrentWriteLength = 0; |
| 5681 | mBytesRemaining = 0; |
| 5682 | mPausedWriteLength = 0; |
| 5683 | mPausedBytesRemaining = 0; |
Eric Laurent | 3eaf66b | 2016-04-01 14:44:17 -0700 | [diff] [blame] | 5684 | // reset bytes written count to reflect that DSP buffers are empty after flush. |
| 5685 | mBytesWritten = 0; |
Andy Hung | f804475 | 2016-07-27 14:58:11 -0700 | [diff] [blame] | 5686 | mOffloadUnderrunPosition = ~0LL; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5687 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5688 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5689 | // discard any pending drain or write ack by incrementing sequence |
| 5690 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5691 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5692 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5693 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5694 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5695 | } |
| 5696 | } |
| 5697 | |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5698 | void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType) |
| 5699 | { |
| 5700 | Mutex::Autolock _l(mLock); |
Eric Laurent | 1308462 | 2016-05-17 10:51:49 -0700 | [diff] [blame] | 5701 | if (PlaybackThread::invalidateTracks_l(streamType)) { |
| 5702 | mFlushPending = true; |
| 5703 | } |
Haynes Mathew George | 05317d2 | 2016-05-03 16:34:26 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5706 | // ---------------------------------------------------------------------------- |
| 5707 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5708 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5709 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5710 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5711 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5712 | mWaitTimeMs(UINT_MAX) |
| 5713 | { |
| 5714 | addOutputTrack(mainThread); |
| 5715 | } |
| 5716 | |
| 5717 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5718 | { |
| 5719 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5720 | mOutputTracks[i]->destroy(); |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5725 | { |
| 5726 | // mix buffers... |
| 5727 | if (outputsReady(outputTracks)) { |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 5728 | mAudioMixer->process(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5729 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5730 | if (mMixerBufferValid) { |
| 5731 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5732 | } else { |
| 5733 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5734 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5735 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5736 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5737 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5738 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5739 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5743 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5744 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5745 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5746 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5747 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5748 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5749 | } |
| 5750 | } else if (mBytesWritten != 0) { |
| 5751 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5752 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5753 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5754 | } else { |
| 5755 | // flush remaining overflow buffers in output tracks |
| 5756 | writeFrames = 0; |
| 5757 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5758 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5762 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5763 | { |
| 5764 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5765 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5766 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5767 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5768 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5772 | { |
| 5773 | // DuplicatingThread implements standby by stopping all tracks |
| 5774 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5775 | outputTracks[i]->stop(); |
| 5776 | } |
| 5777 | } |
| 5778 | |
| 5779 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5780 | { |
| 5781 | outputTracks = mOutputTracks; |
| 5782 | } |
| 5783 | |
| 5784 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5785 | { |
| 5786 | outputTracks.clear(); |
| 5787 | } |
| 5788 | |
| 5789 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5790 | { |
| 5791 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5792 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5793 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5794 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5795 | const size_t frameCount = |
| 5796 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5797 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5798 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5799 | // nearly empty and the other may be dropping data). |
| 5800 | |
| 5801 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5802 | this, |
| 5803 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5804 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5805 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5806 | frameCount, |
| 5807 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5808 | status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY; |
| 5809 | if (status != NO_ERROR) { |
| 5810 | ALOGE("addOutputTrack() initCheck failed %d", status); |
| 5811 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5812 | } |
Eric Laurent | af3ec7c | 2016-08-01 11:25:19 -0700 | [diff] [blame] | 5813 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
| 5814 | mOutputTracks.add(outputTrack); |
| 5815 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
| 5816 | updateWaitTime_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5820 | { |
| 5821 | Mutex::Autolock _l(mLock); |
| 5822 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5823 | if (mOutputTracks[i]->thread() == thread) { |
| 5824 | mOutputTracks[i]->destroy(); |
| 5825 | mOutputTracks.removeAt(i); |
| 5826 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5827 | if (thread->getOutput() == mOutput) { |
| 5828 | mOutput = NULL; |
| 5829 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5830 | return; |
| 5831 | } |
| 5832 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5833 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | // caller must hold mLock |
| 5837 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5838 | { |
| 5839 | mWaitTimeMs = UINT_MAX; |
| 5840 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5841 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5842 | if (strong != 0) { |
| 5843 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5844 | if (waitTimeMs < mWaitTimeMs) { |
| 5845 | mWaitTimeMs = waitTimeMs; |
| 5846 | } |
| 5847 | } |
| 5848 | } |
| 5849 | } |
| 5850 | |
| 5851 | |
| 5852 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5853 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5854 | { |
| 5855 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5856 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5857 | if (thread == 0) { |
| 5858 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5859 | outputTracks[i].get()); |
| 5860 | return false; |
| 5861 | } |
| 5862 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5863 | // see note at standby() declaration |
| 5864 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5865 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5866 | thread.get()); |
| 5867 | return false; |
| 5868 | } |
| 5869 | } |
| 5870 | return true; |
| 5871 | } |
| 5872 | |
| 5873 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5874 | { |
| 5875 | return (mWaitTimeMs * 1000) / 2; |
| 5876 | } |
| 5877 | |
| 5878 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5879 | { |
| 5880 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5881 | updateWaitTime_l(); |
| 5882 | |
| 5883 | MixerThread::cacheParameters_l(); |
| 5884 | } |
| 5885 | |
| 5886 | // ---------------------------------------------------------------------------- |
| 5887 | // Record |
| 5888 | // ---------------------------------------------------------------------------- |
| 5889 | |
| 5890 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5891 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5892 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5893 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5894 | audio_devices_t inDevice, |
| 5895 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5896 | #ifdef TEE_SINK |
| 5897 | , const sp<NBAIO_Sink>& teeSink |
| 5898 | #endif |
| 5899 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5900 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5901 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5902 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5903 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5904 | #ifdef TEE_SINK |
| 5905 | , mTeeSink(teeSink) |
| 5906 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5907 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5908 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5909 | // mFastCapture below |
| 5910 | , mFastCaptureFutex(0) |
| 5911 | // mInputSource |
| 5912 | // mPipeSink |
| 5913 | // mPipeSource |
| 5914 | , mPipeFramesP2(0) |
| 5915 | // mPipeMemory |
| 5916 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5917 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5918 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5919 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5920 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5921 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5922 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5923 | |
| 5924 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5925 | mInputSource = new AudioStreamInSource(input->stream); |
| 5926 | size_t numCounterOffers = 0; |
| 5927 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 5928 | #if !LOG_NDEBUG |
| 5929 | ssize_t index = |
| 5930 | #else |
| 5931 | (void) |
| 5932 | #endif |
| 5933 | mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5934 | ALOG_ASSERT(index == 0); |
| 5935 | |
| 5936 | // initialize fast capture depending on configuration |
| 5937 | bool initFastCapture; |
| 5938 | switch (kUseFastCapture) { |
| 5939 | case FastCapture_Never: |
| 5940 | initFastCapture = false; |
| 5941 | break; |
| 5942 | case FastCapture_Always: |
| 5943 | initFastCapture = true; |
| 5944 | break; |
| 5945 | case FastCapture_Static: |
Glenn Kasten | eb9487e | 2015-07-22 09:15:17 -0700 | [diff] [blame] | 5946 | initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5947 | break; |
| 5948 | // case FastCapture_Dynamic: |
| 5949 | } |
| 5950 | |
| 5951 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5952 | // 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] | 5953 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5954 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5955 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5956 | void *pipeBuffer; |
| 5957 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5958 | sp<IMemory> pipeMemory; |
| 5959 | if ((roHeap == 0) || |
| 5960 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5961 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5962 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5963 | goto failed; |
| 5964 | } |
| 5965 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5966 | memset(pipeBuffer, 0, pipeSize); |
| 5967 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5968 | const NBAIO_Format offers[1] = {format}; |
| 5969 | size_t numCounterOffers = 0; |
| 5970 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5971 | ALOG_ASSERT(index == 0); |
| 5972 | mPipeSink = pipe; |
| 5973 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5974 | numCounterOffers = 0; |
| 5975 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5976 | ALOG_ASSERT(index == 0); |
| 5977 | mPipeSource = pipeReader; |
| 5978 | mPipeFramesP2 = pipeFramesP2; |
| 5979 | mPipeMemory = pipeMemory; |
| 5980 | |
| 5981 | // create fast capture |
| 5982 | mFastCapture = new FastCapture(); |
| 5983 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5984 | #ifdef STATE_QUEUE_DUMP |
| 5985 | // FIXME |
| 5986 | #endif |
| 5987 | FastCaptureState *state = sq->begin(); |
| 5988 | state->mCblk = NULL; |
| 5989 | state->mInputSource = mInputSource.get(); |
| 5990 | state->mInputSourceGen++; |
| 5991 | state->mPipeSink = pipe; |
| 5992 | state->mPipeSinkGen++; |
| 5993 | state->mFrameCount = mFrameCount; |
| 5994 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5995 | // already done in constructor initialization list |
| 5996 | //mFastCaptureFutex = 0; |
| 5997 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5998 | state->mColdGen++; |
| 5999 | state->mDumpState = &mFastCaptureDumpState; |
| 6000 | #ifdef TEE_SINK |
| 6001 | // FIXME |
| 6002 | #endif |
| 6003 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 6004 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 6005 | sq->end(); |
| 6006 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6007 | |
| 6008 | // start the fast capture |
| 6009 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 6010 | pid_t tid = mFastCapture->getTid(); |
Glenn Kasten | 8379b72 | 2016-03-18 14:54:17 -0700 | [diff] [blame] | 6011 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastCapture); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6012 | #ifdef AUDIO_WATCHDOG |
| 6013 | // FIXME |
| 6014 | #endif |
| 6015 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6016 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6017 | } |
| 6018 | failed: ; |
| 6019 | |
| 6020 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6021 | } |
| 6022 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6023 | AudioFlinger::RecordThread::~RecordThread() |
| 6024 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6025 | if (mFastCapture != 0) { |
| 6026 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6027 | FastCaptureState *state = sq->begin(); |
| 6028 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6029 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6030 | if (old == -1) { |
| 6031 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6032 | } |
| 6033 | } |
| 6034 | state->mCommand = FastCaptureState::EXIT; |
| 6035 | sq->end(); |
| 6036 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 6037 | mFastCapture->join(); |
| 6038 | mFastCapture.clear(); |
| 6039 | } |
| 6040 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 6041 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6042 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | void AudioFlinger::RecordThread::onFirstRef() |
| 6046 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 6047 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6048 | } |
| 6049 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6050 | bool AudioFlinger::RecordThread::threadLoop() |
| 6051 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6052 | nsecs_t lastWarning = 0; |
| 6053 | |
| 6054 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6055 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6056 | reacquire_wakelock: |
| 6057 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6058 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6059 | { |
| 6060 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6061 | size_t size = mActiveTracks.size(); |
| 6062 | activeTracksGen = mActiveTracksGen; |
| 6063 | if (size > 0) { |
| 6064 | // FIXME an arbitrary choice |
| 6065 | activeTrack = mActiveTracks[0]; |
| 6066 | acquireWakeLock_l(activeTrack->uid()); |
| 6067 | if (size > 1) { |
| 6068 | SortedVector<int> tmp; |
| 6069 | for (size_t i = 0; i < size; i++) { |
| 6070 | tmp.add(mActiveTracks[i]->uid()); |
| 6071 | } |
| 6072 | updateWakeLockUids_l(tmp); |
| 6073 | } |
| 6074 | } else { |
| 6075 | acquireWakeLock_l(-1); |
| 6076 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6077 | } |
| 6078 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6079 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 6080 | uint32_t sleepUs = 0; |
| 6081 | |
| 6082 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6083 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6084 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 6085 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6086 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 6087 | Vector< sp<RecordTrack> > activeTracks; |
| 6088 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6089 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6090 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6091 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6092 | // reference to a fast track which is about to be removed |
| 6093 | sp<RecordTrack> fastTrackToRemove; |
| 6094 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6095 | { // scope for mLock |
| 6096 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6097 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6098 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6099 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 6100 | // check exitPending here because checkForNewParameters_l() and |
| 6101 | // checkForNewParameters_l() can temporarily release mLock |
| 6102 | if (exitPending()) { |
| 6103 | break; |
| 6104 | } |
| 6105 | |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6106 | // sleep with mutex unlocked |
| 6107 | if (sleepUs > 0) { |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 6108 | ATRACE_BEGIN("sleepC"); |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6109 | mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs)); |
| 6110 | ATRACE_END(); |
| 6111 | sleepUs = 0; |
| 6112 | continue; |
| 6113 | } |
| 6114 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6115 | // if no active track(s), then standby and release wakelock |
| 6116 | size_t size = mActiveTracks.size(); |
| 6117 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6118 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 6119 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6120 | releaseWakeLock_l(); |
| 6121 | ALOGV("RecordThread: loop stopping"); |
| 6122 | // go to sleep |
| 6123 | mWaitWorkCV.wait(mLock); |
| 6124 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6125 | goto reacquire_wakelock; |
| 6126 | } |
| 6127 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6128 | if (mActiveTracksGen != activeTracksGen) { |
| 6129 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6130 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6131 | for (size_t i = 0; i < size; i++) { |
| 6132 | tmp.add(mActiveTracks[i]->uid()); |
| 6133 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6134 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6135 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6136 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6137 | bool doBroadcast = false; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6138 | bool allStopped = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6139 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6140 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6141 | activeTrack = mActiveTracks[i]; |
| 6142 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6143 | if (activeTrack->isFastTrack()) { |
| 6144 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 6145 | fastTrackToRemove = activeTrack; |
| 6146 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6147 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6148 | mActiveTracks.remove(activeTrack); |
| 6149 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6150 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6151 | continue; |
| 6152 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6153 | |
| 6154 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 6155 | switch (activeTrackState) { |
| 6156 | |
| 6157 | case TrackBase::PAUSING: |
| 6158 | mActiveTracks.remove(activeTrack); |
| 6159 | mActiveTracksGen++; |
| 6160 | doBroadcast = true; |
| 6161 | size--; |
| 6162 | continue; |
| 6163 | |
| 6164 | case TrackBase::STARTING_1: |
| 6165 | sleepUs = 10000; |
| 6166 | i++; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6167 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6168 | continue; |
| 6169 | |
| 6170 | case TrackBase::STARTING_2: |
| 6171 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6172 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6173 | activeTrack->mState = TrackBase::ACTIVE; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6174 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6175 | break; |
| 6176 | |
| 6177 | case TrackBase::ACTIVE: |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6178 | allStopped = false; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6179 | break; |
| 6180 | |
| 6181 | case TrackBase::IDLE: |
| 6182 | i++; |
| 6183 | continue; |
| 6184 | |
| 6185 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 6186 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6187 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6188 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6189 | activeTracks.add(activeTrack); |
| 6190 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6191 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6192 | if (activeTrack->isFastTrack()) { |
| 6193 | ALOG_ASSERT(!mFastTrackAvail); |
| 6194 | ALOG_ASSERT(fastTrack == 0); |
| 6195 | fastTrack = activeTrack; |
| 6196 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6197 | } |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6198 | |
| 6199 | if (allStopped) { |
| 6200 | standbyIfNotAlreadyInStandby(); |
| 6201 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6202 | if (doBroadcast) { |
| 6203 | mStartStopCond.broadcast(); |
| 6204 | } |
| 6205 | |
| 6206 | // sleep if there are no active tracks to process |
| 6207 | if (activeTracks.size() == 0) { |
| 6208 | if (sleepUs == 0) { |
| 6209 | sleepUs = kRecordThreadSleepUs; |
| 6210 | } |
| 6211 | continue; |
| 6212 | } |
| 6213 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 6214 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6215 | lockEffectChains_l(effectChains); |
| 6216 | } |
| 6217 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6218 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 6219 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6220 | size_t size = effectChains.size(); |
| 6221 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6222 | // thread mutex is not locked, but effect chain is locked |
| 6223 | effectChains[i]->process_l(); |
| 6224 | } |
| 6225 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6226 | // 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] | 6227 | if (mFastCapture != 0) { |
| 6228 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6229 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6230 | bool didModify = false; |
| 6231 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6232 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 6233 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 6234 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 6235 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 6236 | if (old == -1) { |
| 6237 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 6238 | } |
| 6239 | } |
| 6240 | state->mCommand = FastCaptureState::READ_WRITE; |
| 6241 | #if 0 // FIXME |
| 6242 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 6243 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 6244 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6245 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6246 | didModify = true; |
| 6247 | } |
| 6248 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 6249 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 6250 | if (cblkNew != cblkOld) { |
| 6251 | state->mCblk = cblkNew; |
| 6252 | // block until acked if removing a fast track |
| 6253 | if (cblkOld != NULL) { |
| 6254 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 6255 | } |
| 6256 | didModify = true; |
| 6257 | } |
| 6258 | sq->end(didModify); |
| 6259 | if (didModify) { |
| 6260 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6261 | #if 0 |
| 6262 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6263 | mNormalSource = mPipeSource; |
| 6264 | } |
| 6265 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6266 | } |
| 6267 | } |
| 6268 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 6269 | // now run the fast track destructor with thread mutex unlocked |
| 6270 | fastTrackToRemove.clear(); |
| 6271 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6272 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 6273 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 6274 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 6275 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 6276 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6277 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6278 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6279 | ssize_t framesRead; |
| 6280 | |
| 6281 | // If an NBAIO source is present, use it to read the normal capture's data |
| 6282 | if (mPipeSource != 0) { |
| 6283 | size_t framesToRead = mBufferSize / mFrameSize; |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6284 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6285 | framesToRead); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6286 | if (framesRead == 0) { |
| 6287 | // since pipe is non-blocking, simulate blocking input |
| 6288 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 6289 | } |
| 6290 | // otherwise use the HAL / AudioStreamIn directly |
| 6291 | } else { |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6292 | ATRACE_BEGIN("read"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6293 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6294 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); |
Glenn Kasten | ec6a703 | 2016-03-14 07:40:23 -0700 | [diff] [blame] | 6295 | ATRACE_END(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6296 | if (bytesRead < 0) { |
| 6297 | framesRead = bytesRead; |
| 6298 | } else { |
| 6299 | framesRead = bytesRead / mFrameSize; |
| 6300 | } |
| 6301 | } |
| 6302 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6303 | // Update server timestamp with server stats |
| 6304 | // systemTime() is optional if the hardware supports timestamps. |
| 6305 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead; |
| 6306 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime(); |
| 6307 | |
| 6308 | // Update server timestamp with kernel stats |
Andy Hung | 69ce44d | 2016-07-18 12:14:25 -0700 | [diff] [blame] | 6309 | if (mInput->stream->get_capture_position != nullptr |
| 6310 | && mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) { |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6311 | int64_t position, time; |
| 6312 | int ret = mInput->stream->get_capture_position(mInput->stream, &position, &time); |
| 6313 | if (ret == NO_ERROR) { |
| 6314 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position; |
| 6315 | mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time; |
| 6316 | // Note: In general record buffers should tend to be empty in |
| 6317 | // a properly running pipeline. |
| 6318 | // |
| 6319 | // Also, it is not advantageous to call get_presentation_position during the read |
| 6320 | // as the read obtains a lock, preventing the timestamp call from executing. |
| 6321 | } |
| 6322 | } |
| 6323 | // Use this to track timestamp information |
| 6324 | // ALOGD("%s", mTimestamp.toString().c_str()); |
| 6325 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6326 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6327 | ALOGE("read failed: framesRead=%zd", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6328 | // Force input into standby so that it tries to recover at next read attempt |
| 6329 | inputStandBy(); |
| 6330 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6331 | } |
| 6332 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6333 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6334 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6335 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6336 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6337 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6338 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6339 | } |
| 6340 | // 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] | 6341 | { |
| 6342 | size_t part1 = mRsmpInFramesP2 - rear; |
| 6343 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6344 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6345 | (framesRead - part1) * mFrameSize); |
| 6346 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6347 | } |
| 6348 | rear = mRsmpInRear += framesRead; |
| 6349 | |
| 6350 | size = activeTracks.size(); |
| 6351 | // loop over each active track |
| 6352 | for (size_t i = 0; i < size; i++) { |
| 6353 | activeTrack = activeTracks[i]; |
| 6354 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6355 | // skip fast tracks, as those are handled directly by FastCapture |
| 6356 | if (activeTrack->isFastTrack()) { |
| 6357 | continue; |
| 6358 | } |
| 6359 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6360 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6361 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 6362 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6363 | enum { |
| 6364 | OVERRUN_UNKNOWN, |
| 6365 | OVERRUN_TRUE, |
| 6366 | OVERRUN_FALSE |
| 6367 | } overrun = OVERRUN_UNKNOWN; |
| 6368 | |
| 6369 | // loop over getNextBuffer to handle circular sink |
| 6370 | for (;;) { |
| 6371 | |
| 6372 | activeTrack->mSink.frameCount = ~0; |
| 6373 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 6374 | size_t framesOut = activeTrack->mSink.frameCount; |
| 6375 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 6376 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6377 | // check available frames and handle overrun conditions |
| 6378 | // if the record track isn't draining fast enough. |
| 6379 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6380 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6381 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 6382 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6383 | overrun = OVERRUN_TRUE; |
| 6384 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6385 | if (framesOut == 0 || framesIn == 0) { |
| 6386 | break; |
| 6387 | } |
| 6388 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 6389 | // Don't allow framesOut to be larger than what is possible with resampling |
| 6390 | // from framesIn. |
| 6391 | // This isn't strictly necessary but helps limit buffer resizing in |
| 6392 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 6393 | framesOut = min(framesOut, |
| 6394 | destinationFramesPossible( |
| 6395 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6396 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 6397 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 6398 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6399 | |
| 6400 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 6401 | overrun = OVERRUN_FALSE; |
| 6402 | } |
| 6403 | |
| 6404 | if (activeTrack->mFramesToDrop == 0) { |
| 6405 | if (framesOut > 0) { |
| 6406 | activeTrack->mSink.frameCount = framesOut; |
| 6407 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 6408 | } |
| 6409 | } else { |
| 6410 | // FIXME could do a partial drop of framesOut |
| 6411 | if (activeTrack->mFramesToDrop > 0) { |
| 6412 | activeTrack->mFramesToDrop -= framesOut; |
| 6413 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6414 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6415 | } |
| 6416 | } else { |
| 6417 | activeTrack->mFramesToDrop += framesOut; |
| 6418 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 6419 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 6420 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 6421 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 6422 | activeTrack->sessionId(), |
| 6423 | (activeTrack->mSyncStartEvent != 0) ? |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6424 | activeTrack->mSyncStartEvent->triggerSession() : |
| 6425 | AUDIO_SESSION_NONE); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6426 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6432 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6433 | } |
| 6434 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6435 | |
| 6436 | switch (overrun) { |
| 6437 | case OVERRUN_TRUE: |
| 6438 | // client isn't retrieving buffers fast enough |
| 6439 | if (!activeTrack->setOverflow()) { |
| 6440 | nsecs_t now = systemTime(); |
| 6441 | // FIXME should lastWarning per track? |
| 6442 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 6443 | ALOGW("RecordThread: buffer overflow"); |
| 6444 | lastWarning = now; |
| 6445 | } |
| 6446 | } |
| 6447 | break; |
| 6448 | case OVERRUN_FALSE: |
| 6449 | activeTrack->clearOverflow(); |
| 6450 | break; |
| 6451 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6452 | break; |
| 6453 | } |
| 6454 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6455 | // update frame information and push timestamp out |
| 6456 | activeTrack->updateTrackFrameInfo( |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 6457 | activeTrack->mServerProxy->framesReleased(), |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 6458 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER], |
| 6459 | mSampleRate, mTimestamp); |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 6460 | } |
| 6461 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 6462 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6463 | // enable changes in effect chain |
| 6464 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 6465 | // 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] | 6466 | } |
| 6467 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6468 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6469 | |
| 6470 | { |
| 6471 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 6472 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6473 | sp<RecordTrack> track = mTracks[i]; |
| 6474 | track->invalidate(); |
| 6475 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6476 | mActiveTracks.clear(); |
| 6477 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6478 | mStartStopCond.broadcast(); |
| 6479 | } |
| 6480 | |
| 6481 | releaseWakeLock(); |
| 6482 | |
| 6483 | ALOGV("RecordThread %p exiting", this); |
| 6484 | return false; |
| 6485 | } |
| 6486 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6487 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6488 | { |
| 6489 | if (!mStandby) { |
| 6490 | inputStandBy(); |
| 6491 | mStandby = true; |
| 6492 | } |
| 6493 | } |
| 6494 | |
| 6495 | void AudioFlinger::RecordThread::inputStandBy() |
| 6496 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6497 | // Idle the fast capture if it's currently running |
| 6498 | if (mFastCapture != 0) { |
| 6499 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6500 | FastCaptureState *state = sq->begin(); |
| 6501 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6502 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6503 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6504 | state->mColdGen++; |
| 6505 | mFastCaptureFutex = 0; |
| 6506 | sq->end(); |
| 6507 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6508 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6509 | #if 0 |
| 6510 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6511 | // FIXME |
| 6512 | } |
| 6513 | #endif |
| 6514 | #ifdef AUDIO_WATCHDOG |
| 6515 | // FIXME |
| 6516 | #endif |
| 6517 | } else { |
| 6518 | sq->end(false /*didModify*/); |
| 6519 | } |
| 6520 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6521 | mInput->stream->common.standby(&mInput->stream->common); |
| 6522 | } |
| 6523 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6524 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6525 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6526 | const sp<AudioFlinger::Client>& client, |
| 6527 | uint32_t sampleRate, |
| 6528 | audio_format_t format, |
| 6529 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6530 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6531 | audio_session_t sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6532 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6533 | int uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6534 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6535 | pid_t tid, |
| 6536 | status_t *status) |
| 6537 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6538 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6539 | sp<RecordTrack> track; |
| 6540 | status_t lStatus; |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6541 | audio_input_flags_t inputFlags = mInput->flags; |
| 6542 | |
| 6543 | // special case for FAST flag considered OK if fast capture is present |
| 6544 | if (hasFastCapture()) { |
| 6545 | inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 6546 | } |
| 6547 | |
| 6548 | // Check if requested flags are compatible with output stream flags |
| 6549 | if ((*flags & inputFlags) != *flags) { |
| 6550 | ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and" |
| 6551 | " input flags (%08x)", |
| 6552 | *flags, inputFlags); |
| 6553 | *flags = (audio_input_flags_t)(*flags & inputFlags); |
| 6554 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6555 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6556 | // client expresses a preference for FAST, but we get the final say |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6557 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6558 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6559 | // we formerly checked for a callback handler (non-0 tid), |
| 6560 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6561 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6562 | // frame count is not specified, or is exactly the pipe depth |
| 6563 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6564 | // PCM data |
| 6565 | audio_is_linear_pcm(format) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6566 | // hardware format |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6567 | (format == mFormat) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6568 | // hardware channel mask |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6569 | (channelMask == mChannelMask) && |
Glenn Kasten | 7fd0422 | 2016-02-02 12:38:16 -0800 | [diff] [blame] | 6570 | // hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6571 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6572 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6573 | hasFastCapture() && |
| 6574 | // there are sufficient fast track slots available |
| 6575 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6576 | ) { |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6577 | // check compatibility with audio effects. |
| 6578 | Mutex::Autolock _l(mLock); |
| 6579 | // Do not accept FAST flag if the session has software effects |
| 6580 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 6581 | if (chain != 0) { |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame^] | 6582 | audio_input_flags_t old = *flags; |
| 6583 | chain->checkInputFlagCompatibility(flags); |
| 6584 | if (old != *flags) { |
| 6585 | ALOGV("AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x", |
| 6586 | (int)old, (int)*flags); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6587 | } |
| 6588 | } |
Eric Laurent | 122f7e7 | 2016-06-29 11:53:29 -0700 | [diff] [blame] | 6589 | ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0, |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 6590 | "AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu", |
| 6591 | frameCount, mFrameCount); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6592 | } else { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6593 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu " |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6594 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6595 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6596 | frameCount, mFrameCount, mPipeFramesP2, |
| 6597 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 6598 | hasFastCapture(), tid, mFastTrackAvail); |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6599 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | // compute track buffer size in frames, and suggest the notification frame count |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6604 | if (*flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6605 | // fast track: frame count is exactly the pipe depth |
| 6606 | frameCount = mPipeFramesP2; |
| 6607 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6608 | *notificationFrames = mFrameCount; |
| 6609 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6610 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6611 | // or 20 ms if there is a fast capture |
| 6612 | // TODO This could be a roundupRatio inline, and const |
| 6613 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6614 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6615 | // minimum number of notification periods is at least kMinNotifications, |
| 6616 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6617 | static const size_t kMinNotifications = 3; |
| 6618 | static const uint32_t kMinMs = 30; |
| 6619 | // TODO This could be a roundupRatio inline |
| 6620 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6621 | // TODO This could be a roundupRatio inline |
| 6622 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6623 | maxNotificationFrames; |
| 6624 | const size_t minFrameCount = maxNotificationFrames * |
| 6625 | max(kMinNotifications, minNotificationsByMs); |
| 6626 | frameCount = max(frameCount, minFrameCount); |
| 6627 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6628 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6629 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6630 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6631 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6632 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6633 | lStatus = initCheck(); |
| 6634 | if (lStatus != NO_ERROR) { |
| 6635 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6636 | goto Exit; |
| 6637 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6638 | |
| 6639 | { // scope for mLock |
| 6640 | Mutex::Autolock _l(mLock); |
| 6641 | |
| 6642 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6643 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 6644 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6645 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6646 | lStatus = track->initCheck(); |
| 6647 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6648 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6649 | // 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] | 6650 | goto Exit; |
| 6651 | } |
| 6652 | mTracks.add(track); |
| 6653 | |
| 6654 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6655 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6656 | mAudioFlinger->btNrecIsOff(); |
| 6657 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6658 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6659 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 6660 | if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) { |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6661 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6662 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6663 | // so ask activity manager to do this on our behalf |
| 6664 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 6665 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6666 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6667 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6668 | lStatus = NO_ERROR; |
| 6669 | |
| 6670 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6671 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6672 | return track; |
| 6673 | } |
| 6674 | |
| 6675 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6676 | AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6677 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6678 | { |
| 6679 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6680 | sp<ThreadBase> strongMe = this; |
| 6681 | status_t status = NO_ERROR; |
| 6682 | |
| 6683 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6684 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6685 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6686 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6687 | triggerSession, |
| 6688 | recordTrack->sessionId(), |
| 6689 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6690 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6691 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6692 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6693 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6694 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6695 | } else { |
| 6696 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6697 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6698 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6703 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6704 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6705 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6706 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6707 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6708 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6709 | } else { |
| 6710 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6711 | } |
| 6712 | return status; |
| 6713 | } |
| 6714 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6715 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6716 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6717 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6718 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6719 | mActiveTracks.add(recordTrack); |
| 6720 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6721 | status_t status = NO_ERROR; |
| 6722 | if (recordTrack->isExternalTrack()) { |
| 6723 | mLock.unlock(); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6724 | status = AudioSystem::startInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6725 | mLock.lock(); |
| 6726 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6727 | if (status != NO_ERROR) { |
| 6728 | mActiveTracks.remove(recordTrack); |
| 6729 | mActiveTracksGen++; |
| 6730 | recordTrack->clearSyncStartEvent(); |
| 6731 | ALOGV("RecordThread::start error %d", status); |
| 6732 | return status; |
| 6733 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6734 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6735 | // Catch up with current buffer indices if thread is already running. |
| 6736 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6737 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6738 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6739 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6740 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6741 | // clear any converter state as new data will be discontinuous |
| 6742 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6743 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6744 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6745 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6746 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6747 | ALOGV("Record failed to start"); |
| 6748 | status = BAD_VALUE; |
| 6749 | goto startError; |
| 6750 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6751 | return status; |
| 6752 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6753 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6754 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6755 | if (recordTrack->isExternalTrack()) { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6756 | AudioSystem::stopInput(mId, recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6757 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6758 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6759 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6760 | return status; |
| 6761 | } |
| 6762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6763 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6764 | { |
| 6765 | sp<SyncEvent> strongEvent = event.promote(); |
| 6766 | |
| 6767 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6768 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6769 | if (ptr != 0) { |
| 6770 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6771 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6772 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6773 | } |
| 6774 | } |
| 6775 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6776 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6777 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6778 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6779 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6780 | return false; |
| 6781 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6782 | // 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] | 6783 | recordTrack->mState = TrackBase::PAUSING; |
Eric Laurent | 5c25d56 | 2016-07-13 17:17:45 -0700 | [diff] [blame] | 6784 | // signal thread to stop |
| 6785 | mWaitWorkCV.broadcast(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6786 | // do not wait for mStartStopCond if exiting |
| 6787 | if (exitPending()) { |
| 6788 | return true; |
| 6789 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6790 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6791 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6792 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6793 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6794 | ALOGV("Record stopped OK"); |
| 6795 | return true; |
| 6796 | } |
| 6797 | return false; |
| 6798 | } |
| 6799 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6800 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6801 | { |
| 6802 | return false; |
| 6803 | } |
| 6804 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6805 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6806 | { |
| 6807 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6808 | if (!isValidSyncEvent(event)) { |
| 6809 | return BAD_VALUE; |
| 6810 | } |
| 6811 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 6812 | audio_session_t eventSession = event->triggerSession(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6813 | status_t ret = NAME_NOT_FOUND; |
| 6814 | |
| 6815 | Mutex::Autolock _l(mLock); |
| 6816 | |
| 6817 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6818 | sp<RecordTrack> track = mTracks[i]; |
| 6819 | if (eventSession == track->sessionId()) { |
| 6820 | (void) track->setSyncEvent(event); |
| 6821 | ret = NO_ERROR; |
| 6822 | } |
| 6823 | } |
| 6824 | return ret; |
| 6825 | #else |
| 6826 | return BAD_VALUE; |
| 6827 | #endif |
| 6828 | } |
| 6829 | |
| 6830 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6831 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6832 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6833 | track->terminate(); |
| 6834 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6835 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6836 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6837 | removeTrack_l(track); |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6842 | { |
| 6843 | mTracks.remove(track); |
| 6844 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6845 | if (track->isFastTrack()) { |
| 6846 | ALOG_ASSERT(!mFastTrackAvail); |
| 6847 | mFastTrackAvail = true; |
| 6848 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6849 | } |
| 6850 | |
| 6851 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6852 | { |
| 6853 | dumpInternals(fd, args); |
| 6854 | dumpTracks(fd, args); |
| 6855 | dumpEffectChains(fd, args); |
| 6856 | } |
| 6857 | |
| 6858 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6859 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6860 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6861 | |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6862 | dumpBase(fd, args); |
| 6863 | |
| 6864 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6865 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6866 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6867 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6868 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6869 | |
Glenn Kasten | 2f90c51 | 2015-12-02 11:40:09 -0800 | [diff] [blame] | 6870 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6871 | // while we are dumping it. It may be inconsistent, but it won't mutate! |
| 6872 | // This is a large object so we place it on the heap. |
| 6873 | // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages. |
| 6874 | const FastCaptureDumpState *copy = new FastCaptureDumpState(mFastCaptureDumpState); |
| 6875 | copy->dump(fd); |
| 6876 | delete copy; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6877 | } |
| 6878 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6879 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6880 | { |
| 6881 | const size_t SIZE = 256; |
| 6882 | char buffer[SIZE]; |
| 6883 | String8 result; |
| 6884 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6885 | size_t numtracks = mTracks.size(); |
| 6886 | size_t numactive = mActiveTracks.size(); |
| 6887 | size_t numactiveseen = 0; |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6888 | dprintf(fd, " %zu Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6889 | if (numtracks) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 6890 | dprintf(fd, " of which %zu are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6891 | RecordTrack::appendDumpHeader(result); |
| 6892 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6893 | sp<RecordTrack> track = mTracks[i]; |
| 6894 | if (track != 0) { |
| 6895 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6896 | if (active) { |
| 6897 | numactiveseen++; |
| 6898 | } |
| 6899 | track->dump(buffer, SIZE, active); |
| 6900 | result.append(buffer); |
| 6901 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6902 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6903 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6904 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6905 | } |
| 6906 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6907 | if (numactiveseen != numactive) { |
| 6908 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6909 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6910 | result.append(buffer); |
| 6911 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6912 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6913 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6914 | if (mTracks.indexOf(track) < 0) { |
| 6915 | track->dump(buffer, SIZE, true); |
| 6916 | result.append(buffer); |
| 6917 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6918 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6919 | |
| 6920 | } |
| 6921 | write(fd, result.string(), result.size()); |
| 6922 | } |
| 6923 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6924 | |
| 6925 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6926 | { |
| 6927 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6928 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6929 | mRsmpInFront = recordThread->mRsmpInRear; |
| 6930 | mRsmpInUnrel = 0; |
| 6931 | } |
| 6932 | |
| 6933 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 6934 | size_t *framesAvailable, bool *hasOverrun) |
| 6935 | { |
| 6936 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6937 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6938 | const int32_t rear = recordThread->mRsmpInRear; |
| 6939 | const int32_t front = mRsmpInFront; |
| 6940 | const ssize_t filled = rear - front; |
| 6941 | |
| 6942 | size_t framesIn; |
| 6943 | bool overrun = false; |
| 6944 | if (filled < 0) { |
| 6945 | // should not happen, but treat like a massive overrun and re-sync |
| 6946 | framesIn = 0; |
| 6947 | mRsmpInFront = rear; |
| 6948 | overrun = true; |
| 6949 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 6950 | framesIn = (size_t) filled; |
| 6951 | } else { |
| 6952 | // client is not keeping up with server, but give it latest data |
| 6953 | framesIn = recordThread->mRsmpInFrames; |
| 6954 | mRsmpInFront = /* front = */ rear - framesIn; |
| 6955 | overrun = true; |
| 6956 | } |
| 6957 | if (framesAvailable != NULL) { |
| 6958 | *framesAvailable = framesIn; |
| 6959 | } |
| 6960 | if (hasOverrun != NULL) { |
| 6961 | *hasOverrun = overrun; |
| 6962 | } |
| 6963 | } |
| 6964 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6965 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6966 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 6967 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6968 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6969 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6970 | if (threadBase == 0) { |
| 6971 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6972 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6973 | return NOT_ENOUGH_DATA; |
| 6974 | } |
| 6975 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6976 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6977 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6978 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6979 | // FIXME should not be P2 (don't want to increase latency) |
| 6980 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6981 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6982 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6983 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6984 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6985 | if (part1 > (size_t) filled) { |
| 6986 | part1 = filled; |
| 6987 | } |
| 6988 | size_t ask = buffer->frameCount; |
| 6989 | ALOG_ASSERT(ask > 0); |
| 6990 | if (part1 > ask) { |
| 6991 | part1 = ask; |
| 6992 | } |
| 6993 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6994 | // 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] | 6995 | buffer->raw = NULL; |
| 6996 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6997 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6998 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6999 | } |
| 7000 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7001 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7002 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7003 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7004 | return NO_ERROR; |
| 7005 | } |
| 7006 | |
| 7007 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7008 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 7009 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7010 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7011 | size_t stepCount = buffer->frameCount; |
| 7012 | if (stepCount == 0) { |
| 7013 | return; |
| 7014 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 7015 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 7016 | mRsmpInUnrel -= stepCount; |
| 7017 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7018 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7019 | buffer->frameCount = 0; |
| 7020 | } |
| 7021 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7022 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( |
| 7023 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 7024 | uint32_t srcSampleRate, |
| 7025 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 7026 | uint32_t dstSampleRate) : |
| 7027 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars |
| 7028 | // mSrcFormat |
| 7029 | // mSrcSampleRate |
| 7030 | // mDstChannelMask |
| 7031 | // mDstFormat |
| 7032 | // mDstSampleRate |
| 7033 | // mSrcChannelCount |
| 7034 | // mDstChannelCount |
| 7035 | // mDstFrameSize |
| 7036 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7037 | mResampler(NULL), |
| 7038 | mIsLegacyDownmix(false), |
| 7039 | mIsLegacyUpmix(false), |
| 7040 | mRequiresFloat(false), |
| 7041 | mInputConverterProvider(NULL) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7042 | { |
| 7043 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, |
| 7044 | dstChannelMask, dstFormat, dstSampleRate); |
| 7045 | } |
| 7046 | |
| 7047 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { |
| 7048 | free(mBuf); |
| 7049 | delete mResampler; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7050 | delete mInputConverterProvider; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, |
| 7054 | AudioBufferProvider *provider, size_t frames) |
| 7055 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7056 | if (mInputConverterProvider != NULL) { |
| 7057 | mInputConverterProvider->setBufferProvider(provider); |
| 7058 | provider = mInputConverterProvider; |
| 7059 | } |
| 7060 | |
| 7061 | if (mResampler == NULL) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7062 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 7063 | mSrcSampleRate, mSrcFormat, mDstFormat); |
| 7064 | |
| 7065 | AudioBufferProvider::Buffer buffer; |
| 7066 | for (size_t i = frames; i > 0; ) { |
| 7067 | buffer.frameCount = i; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 7068 | status_t status = provider->getNextBuffer(&buffer); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7069 | if (status != OK || buffer.frameCount == 0) { |
| 7070 | frames -= i; // cannot fill request. |
| 7071 | break; |
| 7072 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7073 | // format convert to destination buffer |
| 7074 | convertNoResampler(dst, buffer.raw, buffer.frameCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7075 | |
| 7076 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; |
| 7077 | i -= buffer.frameCount; |
| 7078 | provider->releaseBuffer(&buffer); |
| 7079 | } |
| 7080 | } else { |
| 7081 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 7082 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); |
| 7083 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7084 | // reallocate buffer if needed |
| 7085 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 7086 | free(mBuf); |
| 7087 | mBufFrames = frames; |
| 7088 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 7089 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7090 | // resampler accumulates, but we only have one source track |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7091 | memset(mBuf, 0, frames * mBufFrameSize); |
| 7092 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); |
| 7093 | // format convert to destination buffer |
| 7094 | convertResampler(dst, mBuf, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7095 | } |
| 7096 | return frames; |
| 7097 | } |
| 7098 | |
| 7099 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( |
| 7100 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 7101 | uint32_t srcSampleRate, |
| 7102 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 7103 | uint32_t dstSampleRate) |
| 7104 | { |
| 7105 | // quick evaluation if there is any change. |
| 7106 | if (mSrcFormat == srcFormat |
| 7107 | && mSrcChannelMask == srcChannelMask |
| 7108 | && mSrcSampleRate == srcSampleRate |
| 7109 | && mDstFormat == dstFormat |
| 7110 | && mDstChannelMask == dstChannelMask |
| 7111 | && mDstSampleRate == dstSampleRate) { |
| 7112 | return NO_ERROR; |
| 7113 | } |
| 7114 | |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 7115 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" |
| 7116 | " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", |
| 7117 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7118 | const bool valid = |
| 7119 | audio_is_input_channel(srcChannelMask) |
| 7120 | && audio_is_input_channel(dstChannelMask) |
| 7121 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) |
| 7122 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) |
| 7123 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) |
| 7124 | ; // no upsampling checks for now |
| 7125 | if (!valid) { |
| 7126 | return BAD_VALUE; |
| 7127 | } |
| 7128 | |
| 7129 | mSrcFormat = srcFormat; |
| 7130 | mSrcChannelMask = srcChannelMask; |
| 7131 | mSrcSampleRate = srcSampleRate; |
| 7132 | mDstFormat = dstFormat; |
| 7133 | mDstChannelMask = dstChannelMask; |
| 7134 | mDstSampleRate = dstSampleRate; |
| 7135 | |
| 7136 | // compute derived parameters |
| 7137 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); |
| 7138 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); |
| 7139 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); |
| 7140 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7141 | // do we need to resample? |
| 7142 | delete mResampler; |
| 7143 | mResampler = NULL; |
| 7144 | if (mSrcSampleRate != mDstSampleRate) { |
| 7145 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, |
| 7146 | mSrcChannelCount, mDstSampleRate); |
| 7147 | mResampler->setSampleRate(mSrcSampleRate); |
| 7148 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
| 7149 | } |
| 7150 | |
| 7151 | // are we running legacy channel conversion modes? |
| 7152 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 7153 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 7154 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; |
| 7155 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO |
| 7156 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 7157 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); |
| 7158 | |
| 7159 | // do we need to process in float? |
| 7160 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; |
| 7161 | |
| 7162 | // do we need a staging buffer to convert for destination (we can still optimize this)? |
| 7163 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity |
| 7164 | if (mResampler != NULL) { |
| 7165 | mBufFrameSize = max(mSrcChannelCount, FCC_2) |
| 7166 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame] | 7167 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7168 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
| 7169 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7170 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); |
| 7171 | } else { |
| 7172 | mBufFrameSize = 0; |
| 7173 | } |
| 7174 | mBufFrames = 0; // force the buffer to be resized. |
| 7175 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7176 | // do we need an input converter buffer provider to give us float? |
| 7177 | delete mInputConverterProvider; |
| 7178 | mInputConverterProvider = NULL; |
| 7179 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 7180 | mInputConverterProvider = new ReformatBufferProvider( |
| 7181 | audio_channel_count_from_in_mask(mSrcChannelMask), |
| 7182 | mSrcFormat, |
| 7183 | AUDIO_FORMAT_PCM_FLOAT, |
| 7184 | 256 /* provider buffer frame count */); |
| 7185 | } |
| 7186 | |
| 7187 | // do we need a remixer to do channel mask conversion |
| 7188 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { |
| 7189 | (void) memcpy_by_index_array_initialization_from_channel_mask( |
| 7190 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7191 | } |
| 7192 | return NO_ERROR; |
| 7193 | } |
| 7194 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7195 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( |
| 7196 | void *dst, const void *src, size_t frames) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7197 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7198 | // 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] | 7199 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 7200 | free(mBuf); |
| 7201 | mBufFrames = frames; |
| 7202 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 7203 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7204 | // do we need to do legacy upmix and downmix? |
| 7205 | if (mIsLegacyUpmix || mIsLegacyDownmix) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7206 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7207 | if (mIsLegacyUpmix) { |
| 7208 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, |
| 7209 | (const float *)src, frames); |
| 7210 | } else /*mIsLegacyDownmix */ { |
| 7211 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, |
| 7212 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7213 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7214 | if (mBuf != NULL) { |
| 7215 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, |
| 7216 | frames * mDstChannelCount); |
| 7217 | } |
| 7218 | return; |
| 7219 | } |
| 7220 | // do we need to do channel mask conversion? |
| 7221 | if (mSrcChannelMask != mDstChannelMask) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7222 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7223 | memcpy_by_index_array(dstBuf, mDstChannelCount, |
| 7224 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); |
| 7225 | if (dstBuf == dst) { |
| 7226 | return; // format is the same |
| 7227 | } |
| 7228 | } |
| 7229 | // convert to destination buffer |
| 7230 | const void *convertBuf = mBuf != NULL ? mBuf : src; |
| 7231 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, |
| 7232 | frames * mDstChannelCount); |
| 7233 | } |
| 7234 | |
| 7235 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( |
| 7236 | void *dst, /*not-a-const*/ void *src, size_t frames) |
| 7237 | { |
| 7238 | // src buffer format is ALWAYS float when entering this routine |
| 7239 | if (mIsLegacyUpmix) { |
| 7240 | ; // mono to stereo already handled by resampler |
| 7241 | } else if (mIsLegacyDownmix |
| 7242 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { |
| 7243 | // the resampler outputs stereo for mono input channel (a feature?) |
| 7244 | // must convert to mono |
| 7245 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 7246 | (const float *)src, frames); |
| 7247 | } else if (mSrcChannelMask != mDstChannelMask) { |
| 7248 | // convert to mono channel again for channel mask conversion (could be skipped |
| 7249 | // with further optimization). |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7250 | if (mSrcChannelCount == 1) { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7251 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 7252 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7253 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7254 | // convert to destination format (in place, OK as float is larger than other types) |
| 7255 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 7256 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 7257 | frames * mSrcChannelCount); |
| 7258 | } |
| 7259 | // channel convert and save to dst |
| 7260 | memcpy_by_index_array(dst, mDstChannelCount, |
| 7261 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); |
| 7262 | return; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7263 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7264 | // convert to destination format and save to dst |
| 7265 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 7266 | frames * mDstChannelCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7267 | } |
| 7268 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7269 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 7270 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7271 | { |
| 7272 | bool reconfig = false; |
| 7273 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7274 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7275 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7276 | audio_format_t reqFormat = mFormat; |
| 7277 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 7278 | // 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] | 7279 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 7280 | |
| 7281 | AudioParameter param = AudioParameter(keyValuePair); |
| 7282 | int value; |
Haynes Mathew George | 9ce67b5 | 2015-09-30 11:40:47 -0700 | [diff] [blame] | 7283 | |
| 7284 | // scope for AutoPark extends to end of method |
| 7285 | AutoPark<FastCapture> park(mFastCapture); |
| 7286 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7287 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 7288 | // channel count change can be requested. Do we mandate the first client defines the |
| 7289 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 7290 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 7291 | samplingRate = value; |
| 7292 | reconfig = true; |
| 7293 | } |
| 7294 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7295 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7296 | status = BAD_VALUE; |
| 7297 | } else { |
| 7298 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7299 | reconfig = true; |
| 7300 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7301 | } |
| 7302 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 7303 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7304 | if (!audio_is_input_channel(mask) || |
| 7305 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7306 | status = BAD_VALUE; |
| 7307 | } else { |
| 7308 | channelMask = mask; |
| 7309 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7310 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7311 | } |
| 7312 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 7313 | // do not accept frame count changes if tracks are open as the track buffer |
| 7314 | // size depends on frame count and correct behavior would not be guaranteed |
| 7315 | // if frame count is changed after track creation |
| 7316 | if (mActiveTracks.size() > 0) { |
| 7317 | status = INVALID_OPERATION; |
| 7318 | } else { |
| 7319 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7320 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7321 | } |
| 7322 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 7323 | // forward device change to effects that have requested to be |
| 7324 | // aware of attached audio device. |
| 7325 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7326 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7327 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7328 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7329 | // store input device and output device but do not forward output device to audio HAL. |
| 7330 | // Note that status is ignored by the caller for output device |
| 7331 | // (see AudioFlinger::setParameters() |
| 7332 | if (audio_is_output_devices(value)) { |
| 7333 | mOutDevice = value; |
| 7334 | status = BAD_VALUE; |
| 7335 | } else { |
| 7336 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7337 | if (value != AUDIO_DEVICE_NONE) { |
| 7338 | mPrevInDevice = value; |
| 7339 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7340 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7341 | // pre processings |
| 7342 | if (mTracks.size() > 0) { |
| 7343 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7344 | mAudioFlinger->btNrecIsOff(); |
| 7345 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7346 | sp<RecordTrack> track = mTracks[i]; |
| 7347 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7348 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7349 | } |
| 7350 | } |
| 7351 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7352 | } |
| 7353 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 7354 | mAudioSource != (audio_source_t)value) { |
| 7355 | // forward device change to effects that have requested to be |
| 7356 | // aware of attached audio device. |
| 7357 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7358 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7359 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7360 | mAudioSource = (audio_source_t)value; |
| 7361 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 7362 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7363 | if (status == NO_ERROR) { |
| 7364 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7365 | keyValuePair.string()); |
| 7366 | if (status == INVALID_OPERATION) { |
| 7367 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7368 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7369 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7370 | } |
| 7371 | if (reconfig) { |
| 7372 | if (status == BAD_VALUE && |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7373 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && |
| 7374 | audio_is_linear_pcm(reqFormat) && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7375 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7376 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7377 | audio_channel_count_from_in_mask( |
Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 7378 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7379 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7380 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7381 | if (status == NO_ERROR) { |
| 7382 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7383 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7384 | } |
| 7385 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7386 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 7387 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7388 | return reconfig; |
| 7389 | } |
| 7390 | |
| 7391 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 7392 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7393 | Mutex::Autolock _l(mLock); |
| 7394 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7395 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7396 | } |
| 7397 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 7398 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 7399 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7400 | free(s); |
| 7401 | return out_s8; |
| 7402 | } |
| 7403 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7404 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7405 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 7406 | |
| 7407 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7408 | |
| 7409 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7410 | case AUDIO_INPUT_OPENED: |
| 7411 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7412 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7413 | desc->mChannelMask = mChannelMask; |
| 7414 | desc->mSamplingRate = mSampleRate; |
| 7415 | desc->mFormat = mFormat; |
| 7416 | desc->mFrameCount = mFrameCount; |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 7417 | desc->mFrameCountHAL = mFrameCount; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7418 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7419 | break; |
| 7420 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 7421 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7422 | default: |
| 7423 | break; |
| 7424 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 7425 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7426 | } |
| 7427 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 7428 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7429 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7430 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 7431 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 7432 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7433 | if (mChannelCount > FCC_8) { |
| 7434 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); |
| 7435 | } |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 7436 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 7437 | mFormat = mHALFormat; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 7438 | if (!audio_is_linear_pcm(mFormat)) { |
| 7439 | ALOGE("HAL format %#x is not linear pcm", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 7440 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 7441 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 7442 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 7443 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7444 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7445 | // 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] | 7446 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7447 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 7448 | // A larger value should allow more old data to be read after a track calls start(), |
| 7449 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 7450 | // |
| 7451 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 7452 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7453 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 7454 | free(mRsmpInBuffer); |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 7455 | mRsmpInBuffer = NULL; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 7456 | |
| 7457 | // TODO optimize audio capture buffer sizes ... |
| 7458 | // Here we calculate the size of the sliding buffer used as a source |
| 7459 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 7460 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 7461 | // be better to have it derived from the pipe depth in the long term. |
| 7462 | // The current value is higher than necessary. However it should not add to latency. |
| 7463 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 7464 | // 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] | 7465 | size_t bufferSize = (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize; |
| 7466 | (void)posix_memalign(&mRsmpInBuffer, 32, bufferSize); |
| 7467 | memset(mRsmpInBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7468 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 7469 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 7470 | // 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] | 7471 | } |
| 7472 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 7473 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7474 | { |
| 7475 | Mutex::Autolock _l(mLock); |
| 7476 | if (initCheck() != NO_ERROR) { |
| 7477 | return 0; |
| 7478 | } |
| 7479 | |
| 7480 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 7481 | } |
| 7482 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7483 | // hasAudioSession_l() must be called with ThreadBase::mLock held |
| 7484 | uint32_t AudioFlinger::RecordThread::hasAudioSession_l(audio_session_t sessionId) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7485 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7486 | uint32_t result = 0; |
| 7487 | if (getEffectChain_l(sessionId) != 0) { |
| 7488 | result = EFFECT_SESSION; |
| 7489 | } |
| 7490 | |
| 7491 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 7492 | if (sessionId == mTracks[i]->sessionId()) { |
| 7493 | result |= TRACK_SESSION; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 7494 | if (mTracks[i]->isFastTrack()) { |
| 7495 | result |= FAST_SESSION; |
| 7496 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7497 | break; |
| 7498 | } |
| 7499 | } |
| 7500 | |
| 7501 | return result; |
| 7502 | } |
| 7503 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7504 | KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7505 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7506 | KeyedVector<audio_session_t, bool> ids; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7507 | Mutex::Autolock _l(mLock); |
| 7508 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 7509 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 7510 | audio_session_t sessionId = track->sessionId(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7511 | if (ids.indexOfKey(sessionId) < 0) { |
| 7512 | ids.add(sessionId, true); |
| 7513 | } |
| 7514 | } |
| 7515 | return ids; |
| 7516 | } |
| 7517 | |
| 7518 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 7519 | { |
| 7520 | Mutex::Autolock _l(mLock); |
| 7521 | AudioStreamIn *input = mInput; |
| 7522 | mInput = NULL; |
| 7523 | return input; |
| 7524 | } |
| 7525 | |
| 7526 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 7527 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 7528 | { |
| 7529 | if (mInput == NULL) { |
| 7530 | return NULL; |
| 7531 | } |
| 7532 | return &mInput->stream->common; |
| 7533 | } |
| 7534 | |
| 7535 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7536 | { |
| 7537 | // only one chain per input thread |
| 7538 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7539 | 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] | 7540 | return INVALID_OPERATION; |
| 7541 | } |
| 7542 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7543 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7544 | chain->setInBuffer(NULL); |
| 7545 | chain->setOutBuffer(NULL); |
| 7546 | |
| 7547 | checkSuspendOnAddEffectChain_l(chain); |
| 7548 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7549 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7550 | // just moved them to a new input stream. |
| 7551 | chain->syncHalEffectsState(); |
| 7552 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7553 | mEffectChains.add(chain); |
| 7554 | |
| 7555 | return NO_ERROR; |
| 7556 | } |
| 7557 | |
| 7558 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7559 | { |
| 7560 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7561 | ALOGW_IF(mEffectChains.size() != 1, |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 7562 | "removeEffectChain_l() %p invalid chain size %zu on thread %p", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7563 | chain.get(), mEffectChains.size(), this); |
| 7564 | if (mEffectChains.size() == 1) { |
| 7565 | mEffectChains.removeAt(0); |
| 7566 | } |
| 7567 | return 0; |
| 7568 | } |
| 7569 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7570 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7571 | audio_patch_handle_t *handle) |
| 7572 | { |
| 7573 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7574 | |
| 7575 | // store new device and send to effects |
| 7576 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7577 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7578 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7579 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7580 | } |
| 7581 | |
| 7582 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7583 | // pre processings |
| 7584 | if (mTracks.size() > 0) { |
| 7585 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7586 | mAudioFlinger->btNrecIsOff(); |
| 7587 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7588 | sp<RecordTrack> track = mTracks[i]; |
| 7589 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7590 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 7591 | } |
| 7592 | } |
| 7593 | |
| 7594 | // store new source and send to effects |
| 7595 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7596 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7597 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7598 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7599 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7600 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7601 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7602 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7603 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7604 | status = hwDevice->create_audio_patch(hwDevice, |
| 7605 | patch->num_sources, |
| 7606 | patch->sources, |
| 7607 | patch->num_sinks, |
| 7608 | patch->sinks, |
| 7609 | handle); |
| 7610 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7611 | char *address; |
| 7612 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7613 | address = audio_device_address_to_parameter( |
| 7614 | patch->sources[0].ext.device.type, |
| 7615 | patch->sources[0].ext.device.address); |
| 7616 | } else { |
| 7617 | address = (char *)calloc(1, 1); |
| 7618 | } |
| 7619 | AudioParameter param = AudioParameter(String8(address)); |
| 7620 | free(address); |
| 7621 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
| 7622 | (int)patch->sources[0].ext.device.type); |
| 7623 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 7624 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7625 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7626 | param.toString().string()); |
| 7627 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7628 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7629 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7630 | if (mInDevice != mPrevInDevice) { |
| 7631 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7632 | mPrevInDevice = mInDevice; |
| 7633 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7634 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7635 | return status; |
| 7636 | } |
| 7637 | |
| 7638 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7639 | { |
| 7640 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7641 | |
| 7642 | mInDevice = AUDIO_DEVICE_NONE; |
| 7643 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7644 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 7645 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7646 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 7647 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7648 | AudioParameter param; |
| 7649 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 7650 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7651 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7652 | } |
| 7653 | return status; |
| 7654 | } |
| 7655 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7656 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7657 | { |
| 7658 | Mutex::Autolock _l(mLock); |
| 7659 | mTracks.add(record); |
| 7660 | } |
| 7661 | |
| 7662 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7663 | { |
| 7664 | Mutex::Autolock _l(mLock); |
| 7665 | destroyTrack_l(record); |
| 7666 | } |
| 7667 | |
| 7668 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7669 | { |
| 7670 | ThreadBase::getAudioPortConfig(config); |
| 7671 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7672 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7673 | config->ext.mix.usecase.source = mAudioSource; |
| 7674 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7675 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7676 | } // namespace android |