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> |
| 39 | #include <audio_utils/primitives.h> |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 40 | #include <audio_utils/format.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 41 | #include <audio_utils/minifloat.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 42 | |
| 43 | // NBAIO implementations |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 44 | #include <media/nbaio/AudioStreamInSource.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 45 | #include <media/nbaio/AudioStreamOutSink.h> |
| 46 | #include <media/nbaio/MonoPipe.h> |
| 47 | #include <media/nbaio/MonoPipeReader.h> |
| 48 | #include <media/nbaio/Pipe.h> |
| 49 | #include <media/nbaio/PipeReader.h> |
| 50 | #include <media/nbaio/SourceAudioBufferProvider.h> |
| 51 | |
| 52 | #include <powermanager/PowerManager.h> |
| 53 | |
| 54 | #include <common_time/cc_helper.h> |
| 55 | #include <common_time/local_clock.h> |
| 56 | |
| 57 | #include "AudioFlinger.h" |
| 58 | #include "AudioMixer.h" |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 59 | #include "BufferProviders.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 60 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 61 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | #include "ServiceUtilities.h" |
| 63 | #include "SchedulingPolicyService.h" |
| 64 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 65 | #ifdef ADD_BATTERY_DATA |
| 66 | #include <media/IMediaPlayerService.h> |
| 67 | #include <media/IMediaDeathNotifier.h> |
| 68 | #endif |
| 69 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 70 | #ifdef DEBUG_CPU_USAGE |
| 71 | #include <cpustats/CentralTendencyStatistics.h> |
| 72 | #include <cpustats/ThreadCpuUsage.h> |
| 73 | #endif |
| 74 | |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 77 | // Note: the following macro is used for extremely verbose logging message. In |
| 78 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 79 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 80 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 81 | // turned on. Do not uncomment the #def below unless you really know what you |
| 82 | // are doing and want to see all of the extremely verbose messages. |
| 83 | //#define VERY_VERY_VERBOSE_LOGGING |
| 84 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 85 | #define ALOGVV ALOGV |
| 86 | #else |
| 87 | #define ALOGVV(a...) do { } while(0) |
| 88 | #endif |
| 89 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 90 | // TODO: Move these macro/inlines to a header file. |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 91 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 92 | template <typename T> |
| 93 | static inline T min(const T& a, const T& b) |
| 94 | { |
| 95 | return a < b ? a : b; |
| 96 | } |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 97 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 98 | #ifndef ARRAY_SIZE |
| 99 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 100 | #endif |
| 101 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 102 | namespace android { |
| 103 | |
| 104 | // retry counts for buffer fill timeout |
| 105 | // 50 * ~20msecs = 1 second |
| 106 | static const int8_t kMaxTrackRetries = 50; |
| 107 | static const int8_t kMaxTrackStartupRetries = 50; |
| 108 | // allow less retry attempts on direct output thread. |
| 109 | // direct outputs can be a scarce resource in audio hardware and should |
| 110 | // be released as quickly as possible. |
| 111 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 112 | |
| 113 | // don't warn about blocked writes or record buffer overflows more often than this |
| 114 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 115 | |
| 116 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 117 | static const int kRecordThreadSleepUs = 5000; |
| 118 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 119 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 120 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 121 | |
| 122 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 123 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 124 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 125 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 126 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 127 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
| 128 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 129 | // maximum normal sink buffer size |
| 130 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 131 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 132 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 133 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 134 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 135 | // Whether to use fast mixer |
| 136 | static const enum { |
| 137 | FastMixer_Never, // never initialize or use: for debugging only |
| 138 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 139 | // normal mixer multiplier is 1 |
| 140 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 141 | // multiplier is calculated based on min & max normal mixer buffer size |
| 142 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 143 | // multiplier is calculated based on min & max normal mixer buffer size |
| 144 | // FIXME for FastMixer_Dynamic: |
| 145 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 146 | // For example, one HAL implementation returns an error from a large write, |
| 147 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 148 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 149 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 150 | } kUseFastMixer = FastMixer_Static; |
| 151 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 152 | // Whether to use fast capture |
| 153 | static const enum { |
| 154 | FastCapture_Never, // never initialize or use: for debugging only |
| 155 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 156 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 157 | } kUseFastCapture = FastCapture_Static; |
| 158 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 159 | // Priorities for requestPriority |
| 160 | static const int kPriorityAudioApp = 2; |
| 161 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 162 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 | |
| 164 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 165 | // for the track. The client then sub-divides this into smaller buffers for its use. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 166 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 167 | // So for now we just assume that client is double-buffered for fast tracks. |
| 168 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 169 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 170 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 171 | |
| 172 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 173 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 174 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 175 | // The minimum and maximum allowed values |
| 176 | static const int kFastTrackMultiplierMin = 1; |
| 177 | static const int kFastTrackMultiplierMax = 2; |
| 178 | |
| 179 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 180 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 181 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 182 | // See Thread::readOnlyHeap(). |
| 183 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 184 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 185 | // 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] | 186 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 187 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 188 | // ---------------------------------------------------------------------------- |
| 189 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 190 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 191 | |
| 192 | static void sFastTrackMultiplierInit() |
| 193 | { |
| 194 | char value[PROPERTY_VALUE_MAX]; |
| 195 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 196 | char *endptr; |
| 197 | unsigned long ul = strtoul(value, &endptr, 0); |
| 198 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 199 | sFastTrackMultiplier = (int) ul; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // ---------------------------------------------------------------------------- |
| 205 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 206 | #ifdef ADD_BATTERY_DATA |
| 207 | // To collect the amplifier usage |
| 208 | static void addBatteryData(uint32_t params) { |
| 209 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 210 | if (service == NULL) { |
| 211 | // it already logged |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | service->addBatteryData(params); |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | |
| 220 | // ---------------------------------------------------------------------------- |
| 221 | // CPU Stats |
| 222 | // ---------------------------------------------------------------------------- |
| 223 | |
| 224 | class CpuStats { |
| 225 | public: |
| 226 | CpuStats(); |
| 227 | void sample(const String8 &title); |
| 228 | #ifdef DEBUG_CPU_USAGE |
| 229 | private: |
| 230 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 231 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 232 | |
| 233 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 234 | |
| 235 | int mCpuNum; // thread's current CPU number |
| 236 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 237 | #endif |
| 238 | }; |
| 239 | |
| 240 | CpuStats::CpuStats() |
| 241 | #ifdef DEBUG_CPU_USAGE |
| 242 | : mCpuNum(-1), mCpukHz(-1) |
| 243 | #endif |
| 244 | { |
| 245 | } |
| 246 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 247 | void CpuStats::sample(const String8 &title |
| 248 | #ifndef DEBUG_CPU_USAGE |
| 249 | __unused |
| 250 | #endif |
| 251 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 252 | #ifdef DEBUG_CPU_USAGE |
| 253 | // get current thread's delta CPU time in wall clock ns |
| 254 | double wcNs; |
| 255 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 256 | |
| 257 | // record sample for wall clock statistics |
| 258 | if (valid) { |
| 259 | mWcStats.sample(wcNs); |
| 260 | } |
| 261 | |
| 262 | // get the current CPU number |
| 263 | int cpuNum = sched_getcpu(); |
| 264 | |
| 265 | // get the current CPU frequency in kHz |
| 266 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 267 | |
| 268 | // check if either CPU number or frequency changed |
| 269 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 270 | mCpuNum = cpuNum; |
| 271 | mCpukHz = cpukHz; |
| 272 | // ignore sample for purposes of cycles |
| 273 | valid = false; |
| 274 | } |
| 275 | |
| 276 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 277 | if (valid && mCpukHz > 0) { |
| 278 | double cycles = wcNs * cpukHz * 0.000001; |
| 279 | mHzStats.sample(cycles); |
| 280 | } |
| 281 | |
| 282 | unsigned n = mWcStats.n(); |
| 283 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 284 | if ((n & 127) == 1) { |
| 285 | long long elapsed = mCpuUsage.elapsed(); |
| 286 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 287 | double perLoop = elapsed / (double) n; |
| 288 | double perLoop100 = perLoop * 0.01; |
| 289 | double perLoop1k = perLoop * 0.001; |
| 290 | double mean = mWcStats.mean(); |
| 291 | double stddev = mWcStats.stddev(); |
| 292 | double minimum = mWcStats.minimum(); |
| 293 | double maximum = mWcStats.maximum(); |
| 294 | double meanCycles = mHzStats.mean(); |
| 295 | double stddevCycles = mHzStats.stddev(); |
| 296 | double minCycles = mHzStats.minimum(); |
| 297 | double maxCycles = mHzStats.maximum(); |
| 298 | mCpuUsage.resetElapsed(); |
| 299 | mWcStats.reset(); |
| 300 | mHzStats.reset(); |
| 301 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 302 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 303 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 304 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 305 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 306 | title.string(), |
| 307 | elapsed * .000000001, n, perLoop * .000001, |
| 308 | mean * .001, |
| 309 | stddev * .001, |
| 310 | minimum * .001, |
| 311 | maximum * .001, |
| 312 | mean / perLoop100, |
| 313 | stddev / perLoop100, |
| 314 | minimum / perLoop100, |
| 315 | maximum / perLoop100, |
| 316 | meanCycles / perLoop1k, |
| 317 | stddevCycles / perLoop1k, |
| 318 | minCycles / perLoop1k, |
| 319 | maxCycles / perLoop1k); |
| 320 | |
| 321 | } |
| 322 | } |
| 323 | #endif |
| 324 | }; |
| 325 | |
| 326 | // ---------------------------------------------------------------------------- |
| 327 | // ThreadBase |
| 328 | // ---------------------------------------------------------------------------- |
| 329 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 330 | // static |
| 331 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 332 | { |
| 333 | switch (type) { |
| 334 | case MIXER: |
| 335 | return "MIXER"; |
| 336 | case DIRECT: |
| 337 | return "DIRECT"; |
| 338 | case DUPLICATING: |
| 339 | return "DUPLICATING"; |
| 340 | case RECORD: |
| 341 | return "RECORD"; |
| 342 | case OFFLOAD: |
| 343 | return "OFFLOAD"; |
| 344 | default: |
| 345 | return "unknown"; |
| 346 | } |
| 347 | } |
| 348 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 349 | String8 devicesToString(audio_devices_t devices) |
| 350 | { |
| 351 | static const struct mapping { |
| 352 | audio_devices_t mDevices; |
| 353 | const char * mString; |
| 354 | } mappingsOut[] = { |
| 355 | AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE", |
| 356 | AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER", |
| 357 | AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET", |
| 358 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 359 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "BLUETOOTH_SCO", |
| 360 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET", |
| 361 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, "BLUETOOTH_SCO_CARKIT", |
| 362 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, "BLUETOOTH_A2DP", |
| 363 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, "BLUETOOTH_A2DP_HEADPHONES", |
| 364 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, "BLUETOOTH_A2DP_SPEAKER", |
| 365 | AUDIO_DEVICE_OUT_AUX_DIGITAL, "AUX_DIGITAL", |
| 366 | AUDIO_DEVICE_OUT_HDMI, "HDMI", |
| 367 | AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", |
| 368 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", |
| 369 | AUDIO_DEVICE_OUT_USB_ACCESSORY, "USB_ACCESSORY", |
| 370 | AUDIO_DEVICE_OUT_USB_DEVICE, "USB_DEVICE", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 371 | AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 372 | AUDIO_DEVICE_OUT_LINE, "LINE", |
| 373 | AUDIO_DEVICE_OUT_HDMI_ARC, "HDMI_ARC", |
| 374 | AUDIO_DEVICE_OUT_SPDIF, "SPDIF", |
| 375 | AUDIO_DEVICE_OUT_FM, "FM", |
| 376 | AUDIO_DEVICE_OUT_AUX_LINE, "AUX_LINE", |
| 377 | AUDIO_DEVICE_OUT_SPEAKER_SAFE, "SPEAKER_SAFE", |
Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame] | 378 | AUDIO_DEVICE_OUT_IP, "IP", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 379 | AUDIO_DEVICE_NONE, "NONE", // must be last |
| 380 | }, mappingsIn[] = { |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 381 | AUDIO_DEVICE_IN_COMMUNICATION, "COMMUNICATION", |
| 382 | AUDIO_DEVICE_IN_AMBIENT, "AMBIENT", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 383 | AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 384 | AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "BLUETOOTH_SCO_HEADSET", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 385 | AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 386 | AUDIO_DEVICE_IN_AUX_DIGITAL, "AUX_DIGITAL", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 387 | AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 388 | AUDIO_DEVICE_IN_TELEPHONY_RX, "TELEPHONY_RX", |
| 389 | AUDIO_DEVICE_IN_BACK_MIC, "BACK_MIC", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 390 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX", |
Glenn Kasten | 84d61ca | 2015-05-06 18:32:13 -0700 | [diff] [blame] | 391 | AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET, "ANLG_DOCK_HEADSET", |
| 392 | AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET, "DGTL_DOCK_HEADSET", |
| 393 | AUDIO_DEVICE_IN_USB_ACCESSORY, "USB_ACCESSORY", |
| 394 | AUDIO_DEVICE_IN_USB_DEVICE, "USB_DEVICE", |
| 395 | AUDIO_DEVICE_IN_FM_TUNER, "FM_TUNER", |
| 396 | AUDIO_DEVICE_IN_TV_TUNER, "TV_TUNER", |
| 397 | AUDIO_DEVICE_IN_LINE, "LINE", |
| 398 | AUDIO_DEVICE_IN_SPDIF, "SPDIF", |
| 399 | AUDIO_DEVICE_IN_BLUETOOTH_A2DP, "BLUETOOTH_A2DP", |
| 400 | AUDIO_DEVICE_IN_LOOPBACK, "LOOPBACK", |
Eric Laurent | b9d7333 | 2015-06-30 17:09:20 -0700 | [diff] [blame] | 401 | AUDIO_DEVICE_IN_IP, "IP", |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 402 | AUDIO_DEVICE_NONE, "NONE", // must be last |
| 403 | }; |
| 404 | String8 result; |
| 405 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 406 | const mapping *entry; |
| 407 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 408 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 409 | entry = mappingsIn; |
| 410 | } else { |
| 411 | entry = mappingsOut; |
| 412 | } |
| 413 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 414 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 415 | if (devices & entry->mDevices) { |
| 416 | if (!result.isEmpty()) { |
| 417 | result.append("|"); |
| 418 | } |
| 419 | result.append(entry->mString); |
| 420 | } |
| 421 | } |
| 422 | if (devices & ~allDevices) { |
| 423 | if (!result.isEmpty()) { |
| 424 | result.append("|"); |
| 425 | } |
| 426 | result.appendFormat("0x%X", devices & ~allDevices); |
| 427 | } |
| 428 | if (result.isEmpty()) { |
| 429 | result.append(entry->mString); |
| 430 | } |
| 431 | return result; |
| 432 | } |
| 433 | |
| 434 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 435 | { |
| 436 | static const struct mapping { |
| 437 | audio_input_flags_t mFlag; |
| 438 | const char * mString; |
| 439 | } mappings[] = { |
| 440 | AUDIO_INPUT_FLAG_FAST, "FAST", |
| 441 | AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD", |
| 442 | AUDIO_INPUT_FLAG_NONE, "NONE", // must be last |
| 443 | }; |
| 444 | String8 result; |
| 445 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 446 | const mapping *entry; |
| 447 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 448 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 449 | if (flags & entry->mFlag) { |
| 450 | if (!result.isEmpty()) { |
| 451 | result.append("|"); |
| 452 | } |
| 453 | result.append(entry->mString); |
| 454 | } |
| 455 | } |
| 456 | if (flags & ~allFlags) { |
| 457 | if (!result.isEmpty()) { |
| 458 | result.append("|"); |
| 459 | } |
| 460 | result.appendFormat("0x%X", flags & ~allFlags); |
| 461 | } |
| 462 | if (result.isEmpty()) { |
| 463 | result.append(entry->mString); |
| 464 | } |
| 465 | return result; |
| 466 | } |
| 467 | |
| 468 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 469 | { |
| 470 | static const struct mapping { |
| 471 | audio_output_flags_t mFlag; |
| 472 | const char * mString; |
| 473 | } mappings[] = { |
| 474 | AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT", |
| 475 | AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY", |
| 476 | AUDIO_OUTPUT_FLAG_FAST, "FAST", |
| 477 | AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER", |
Glenn Kasten | dfb0e11 | 2015-02-18 14:33:39 -0800 | [diff] [blame] | 478 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 479 | AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING", |
| 480 | AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC", |
| 481 | AUDIO_OUTPUT_FLAG_NONE, "NONE", // must be last |
| 482 | }; |
| 483 | String8 result; |
| 484 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 485 | const mapping *entry; |
| 486 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 487 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 488 | if (flags & entry->mFlag) { |
| 489 | if (!result.isEmpty()) { |
| 490 | result.append("|"); |
| 491 | } |
| 492 | result.append(entry->mString); |
| 493 | } |
| 494 | } |
| 495 | if (flags & ~allFlags) { |
| 496 | if (!result.isEmpty()) { |
| 497 | result.append("|"); |
| 498 | } |
| 499 | result.appendFormat("0x%X", flags & ~allFlags); |
| 500 | } |
| 501 | if (result.isEmpty()) { |
| 502 | result.append(entry->mString); |
| 503 | } |
| 504 | return result; |
| 505 | } |
| 506 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 507 | const char *sourceToString(audio_source_t source) |
| 508 | { |
| 509 | switch (source) { |
| 510 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 511 | case AUDIO_SOURCE_MIC: return "mic"; |
| 512 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 513 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 514 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 515 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 516 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 517 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 518 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
| 519 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 520 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 521 | default: return "unknown"; |
| 522 | } |
| 523 | } |
| 524 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 525 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 526 | 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] | 527 | : Thread(false /*canCallJava*/), |
| 528 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 529 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 530 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 531 | // are set by PlaybackThread::readOutputParameters_l() or |
| 532 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 533 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 534 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 535 | mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE), |
| 536 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 537 | // mName will be set by concrete (non-virtual) subclass |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 538 | mDeathRecipient(new PMDeathRecipient(this)), |
| 539 | mSystemReady(systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 540 | { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 541 | memset(&mPatch, 0, sizeof(struct audio_patch)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | AudioFlinger::ThreadBase::~ThreadBase() |
| 545 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 546 | // 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] | 547 | mConfigEvents.clear(); |
| 548 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 549 | // do not lock the mutex in destructor |
| 550 | releaseWakeLock_l(); |
| 551 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 552 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 553 | binder->unlinkToDeath(mDeathRecipient); |
| 554 | } |
| 555 | } |
| 556 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 557 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 558 | { |
| 559 | status_t status = initCheck(); |
| 560 | if (status == NO_ERROR) { |
| 561 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 562 | } else { |
| 563 | ALOGE("No working audio driver found."); |
| 564 | } |
| 565 | return status; |
| 566 | } |
| 567 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 568 | void AudioFlinger::ThreadBase::exit() |
| 569 | { |
| 570 | ALOGV("ThreadBase::exit"); |
| 571 | // do any cleanup required for exit to succeed |
| 572 | preExit(); |
| 573 | { |
| 574 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 575 | // if (!exitPending()) { |
| 576 | // // context switch from here to exit() |
| 577 | // // exit() calls requestExit(), what exitPending() observes |
| 578 | // // exit() calls signal(), which is dropped since no waiters |
| 579 | // // context switch back from exit() to here |
| 580 | // mWaitWorkCV.wait(...); |
| 581 | // // now thread is hung |
| 582 | // } |
| 583 | AutoMutex lock(mLock); |
| 584 | requestExit(); |
| 585 | mWaitWorkCV.broadcast(); |
| 586 | } |
| 587 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 588 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 589 | requestExitAndWait(); |
| 590 | } |
| 591 | |
| 592 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 593 | { |
| 594 | status_t status; |
| 595 | |
| 596 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 597 | Mutex::Autolock _l(mLock); |
| 598 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 599 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 600 | } |
| 601 | |
| 602 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 603 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 604 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 605 | { |
| 606 | status_t status = NO_ERROR; |
| 607 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 608 | if (event->mRequiresSystemReady && !mSystemReady) { |
| 609 | event->mWaitStatus = false; |
| 610 | mPendingConfigEvents.add(event); |
| 611 | return status; |
| 612 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 613 | mConfigEvents.add(event); |
| 614 | ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 615 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 616 | mLock.unlock(); |
| 617 | { |
| 618 | Mutex::Autolock _l(event->mLock); |
| 619 | while (event->mWaitStatus) { |
| 620 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 621 | event->mStatus = TIMED_OUT; |
| 622 | event->mWaitStatus = false; |
| 623 | } |
| 624 | } |
| 625 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 626 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 627 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 628 | return status; |
| 629 | } |
| 630 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 631 | void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 632 | { |
| 633 | Mutex::Autolock _l(mLock); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 634 | sendIoConfigEvent_l(event, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 638 | 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] | 639 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 640 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 641 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 644 | void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) |
| 645 | { |
| 646 | Mutex::Autolock _l(mLock); |
| 647 | sendPrioConfigEvent_l(pid, tid, prio); |
| 648 | } |
| 649 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 650 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 651 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 652 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 653 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 654 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 657 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 658 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 659 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 660 | sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); |
| 661 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 664 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 665 | const struct audio_patch *patch, |
| 666 | audio_patch_handle_t *handle) |
| 667 | { |
| 668 | Mutex::Autolock _l(mLock); |
| 669 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 670 | status_t status = sendConfigEvent_l(configEvent); |
| 671 | if (status == NO_ERROR) { |
| 672 | CreateAudioPatchConfigEventData *data = |
| 673 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 674 | *handle = data->mHandle; |
| 675 | } |
| 676 | return status; |
| 677 | } |
| 678 | |
| 679 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 680 | const audio_patch_handle_t handle) |
| 681 | { |
| 682 | Mutex::Autolock _l(mLock); |
| 683 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 684 | return sendConfigEvent_l(configEvent); |
| 685 | } |
| 686 | |
| 687 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 688 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 689 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 690 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 691 | bool configChanged = false; |
| 692 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 693 | while (!mConfigEvents.isEmpty()) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 694 | ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); |
| 695 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 696 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 697 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 698 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 699 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 700 | // FIXME Need to understand why this has to be done asynchronously |
| 701 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 702 | true /*asynchronous*/); |
| 703 | if (err != 0) { |
| 704 | 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] | 705 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 706 | } |
| 707 | } break; |
| 708 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 709 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 710 | ioConfigChanged(data->mEvent, data->mPid); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 711 | } break; |
| 712 | case CFG_EVENT_SET_PARAMETER: { |
| 713 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 714 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 715 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 716 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 717 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 718 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 719 | CreateAudioPatchConfigEventData *data = |
| 720 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 721 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 722 | } break; |
| 723 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 724 | ReleaseAudioPatchConfigEventData *data = |
| 725 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 726 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 727 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 728 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 729 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 730 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 731 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 732 | { |
| 733 | Mutex::Autolock _l(event->mLock); |
| 734 | if (event->mWaitStatus) { |
| 735 | event->mWaitStatus = false; |
| 736 | event->mCond.signal(); |
| 737 | } |
| 738 | } |
| 739 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 740 | } |
| 741 | |
| 742 | if (configChanged) { |
| 743 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 744 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 747 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 748 | String8 s; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 749 | const audio_channel_representation_t representation = |
| 750 | audio_channel_mask_get_representation(mask); |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 751 | |
| 752 | switch (representation) { |
| 753 | case AUDIO_CHANNEL_REPRESENTATION_POSITION: { |
| 754 | if (output) { |
| 755 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 756 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 757 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 758 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 759 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 760 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 761 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 762 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 763 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 764 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 765 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 766 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 767 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 768 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 769 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 770 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 771 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 772 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 773 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 774 | } else { |
| 775 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 776 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 777 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 778 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 779 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 780 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 781 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 782 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 783 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 784 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 785 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 786 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 787 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 788 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 789 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 790 | } |
| 791 | const int len = s.length(); |
| 792 | if (len > 2) { |
| 793 | char *str = s.lockBuffer(len); // needed? |
| 794 | s.unlockBuffer(len - 2); // remove trailing ", " |
| 795 | } |
| 796 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 797 | } |
Andy Hung | f98ec8d | 2015-05-19 12:53:24 -0700 | [diff] [blame] | 798 | case AUDIO_CHANNEL_REPRESENTATION_INDEX: |
| 799 | s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask)); |
| 800 | return s; |
| 801 | default: |
| 802 | s.appendFormat("unknown mask, representation:%d bits:%#x", |
| 803 | representation, audio_channel_mask_get_bits(mask)); |
| 804 | return s; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 805 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 808 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 809 | { |
| 810 | const size_t SIZE = 256; |
| 811 | char buffer[SIZE]; |
| 812 | String8 result; |
| 813 | |
| 814 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 815 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 816 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 819 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 820 | dprintf(fd, " I/O handle: %d\n", mId); |
| 821 | dprintf(fd, " TID: %d\n", getTid()); |
| 822 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 823 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 824 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 825 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 826 | dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 827 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 828 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 829 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 830 | dprintf(fd, " Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 831 | dprintf(fd, " Frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 832 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 833 | size_t numConfig = mConfigEvents.size(); |
| 834 | if (numConfig) { |
| 835 | for (size_t i = 0; i < numConfig; i++) { |
| 836 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 837 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 838 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 839 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 840 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 841 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 842 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame] | 843 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 844 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 845 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 846 | |
| 847 | if (locked) { |
| 848 | mLock.unlock(); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 853 | { |
| 854 | const size_t SIZE = 256; |
| 855 | char buffer[SIZE]; |
| 856 | String8 result; |
| 857 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 858 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 859 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | write(fd, buffer, strlen(buffer)); |
| 861 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 862 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 863 | sp<EffectChain> chain = mEffectChains[i]; |
| 864 | if (chain != 0) { |
| 865 | chain->dump(fd, args); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 870 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 871 | { |
| 872 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 873 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 876 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 877 | { |
| 878 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 879 | case MIXER: |
| 880 | return String16("AudioMix"); |
| 881 | case DIRECT: |
| 882 | return String16("AudioDirectOut"); |
| 883 | case DUPLICATING: |
| 884 | return String16("AudioDup"); |
| 885 | case RECORD: |
| 886 | return String16("AudioIn"); |
| 887 | case OFFLOAD: |
| 888 | return String16("AudioOffload"); |
| 889 | default: |
| 890 | ALOG_ASSERT(false); |
| 891 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 895 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 896 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 897 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 898 | if (mPowerManager != 0) { |
| 899 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 900 | status_t status; |
| 901 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 902 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 903 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 904 | getWakeLockTag(), |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 905 | String16("media"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 906 | uid, |
| 907 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 908 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 909 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 910 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 911 | getWakeLockTag(), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 912 | String16("media"), |
| 913 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 914 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 915 | if (status == NO_ERROR) { |
| 916 | mWakeLockToken = binder; |
| 917 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 918 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 923 | { |
| 924 | Mutex::Autolock _l(mLock); |
| 925 | releaseWakeLock_l(); |
| 926 | } |
| 927 | |
| 928 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 929 | { |
| 930 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 931 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 932 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 933 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 934 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 935 | } |
| 936 | mWakeLockToken.clear(); |
| 937 | } |
| 938 | } |
| 939 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 940 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 941 | Mutex::Autolock _l(mLock); |
| 942 | updateWakeLockUids_l(uids); |
| 943 | } |
| 944 | |
| 945 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 946 | if (mSystemReady && mPowerManager == 0) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 947 | // use checkService() to avoid blocking if power service is not up yet |
| 948 | sp<IBinder> binder = |
| 949 | defaultServiceManager()->checkService(String16("power")); |
| 950 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 951 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 952 | } else { |
| 953 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 954 | binder->linkToDeath(mDeathRecipient); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 960 | getPowerManager_l(); |
| 961 | if (mWakeLockToken == NULL) { |
| 962 | ALOGE("no wake lock to update!"); |
| 963 | return; |
| 964 | } |
| 965 | if (mPowerManager != 0) { |
| 966 | sp<IBinder> binder = new BBinder(); |
| 967 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 968 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 969 | true /* FIXME force oneway contrary to .aidl */); |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 970 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 974 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 975 | { |
| 976 | Mutex::Autolock _l(mLock); |
| 977 | releaseWakeLock_l(); |
| 978 | mPowerManager.clear(); |
| 979 | } |
| 980 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 981 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 982 | { |
| 983 | sp<ThreadBase> thread = mThread.promote(); |
| 984 | if (thread != 0) { |
| 985 | thread->clearPowerManager(); |
| 986 | } |
| 987 | ALOGW("power manager service died !!!"); |
| 988 | } |
| 989 | |
| 990 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 991 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 992 | { |
| 993 | Mutex::Autolock _l(mLock); |
| 994 | setEffectSuspended_l(type, suspend, sessionId); |
| 995 | } |
| 996 | |
| 997 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 998 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 999 | { |
| 1000 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1001 | if (chain != 0) { |
| 1002 | if (type != NULL) { |
| 1003 | chain->setEffectSuspended_l(type, suspend); |
| 1004 | } else { |
| 1005 | chain->setEffectSuspendedAll_l(suspend); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 1010 | } |
| 1011 | |
| 1012 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 1013 | { |
| 1014 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 1015 | if (index < 0) { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 1020 | mSuspendedSessions.valueAt(index); |
| 1021 | |
| 1022 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 1023 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 1024 | for (int j = 0; j < desc->mRefCount; j++) { |
| 1025 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 1026 | chain->setEffectSuspendedAll_l(true); |
| 1027 | } else { |
| 1028 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 1029 | desc->mType.timeLow); |
| 1030 | chain->setEffectSuspended_l(&desc->mType, true); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 1037 | bool suspend, |
| 1038 | int sessionId) |
| 1039 | { |
| 1040 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 1041 | |
| 1042 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 1043 | |
| 1044 | if (suspend) { |
| 1045 | if (index >= 0) { |
| 1046 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1047 | } else { |
| 1048 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 1049 | } |
| 1050 | } else { |
| 1051 | if (index < 0) { |
| 1052 | return; |
| 1053 | } |
| 1054 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | int key = EffectChain::kKeyForSuspendAll; |
| 1059 | if (type != NULL) { |
| 1060 | key = type->timeLow; |
| 1061 | } |
| 1062 | index = sessionEffects.indexOfKey(key); |
| 1063 | |
| 1064 | sp<SuspendedSessionDesc> desc; |
| 1065 | if (suspend) { |
| 1066 | if (index >= 0) { |
| 1067 | desc = sessionEffects.valueAt(index); |
| 1068 | } else { |
| 1069 | desc = new SuspendedSessionDesc(); |
| 1070 | if (type != NULL) { |
| 1071 | desc->mType = *type; |
| 1072 | } |
| 1073 | sessionEffects.add(key, desc); |
| 1074 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1075 | } |
| 1076 | desc->mRefCount++; |
| 1077 | } else { |
| 1078 | if (index < 0) { |
| 1079 | return; |
| 1080 | } |
| 1081 | desc = sessionEffects.valueAt(index); |
| 1082 | if (--desc->mRefCount == 0) { |
| 1083 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1084 | sessionEffects.removeItemsAt(index); |
| 1085 | if (sessionEffects.isEmpty()) { |
| 1086 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1087 | sessionId); |
| 1088 | mSuspendedSessions.removeItem(sessionId); |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | if (!sessionEffects.isEmpty()) { |
| 1093 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1098 | bool enabled, |
| 1099 | int sessionId) |
| 1100 | { |
| 1101 | Mutex::Autolock _l(mLock); |
| 1102 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1103 | } |
| 1104 | |
| 1105 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1106 | bool enabled, |
| 1107 | int sessionId) |
| 1108 | { |
| 1109 | if (mType != RECORD) { |
| 1110 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1111 | // another session. This gives the priority to well behaved effect control panels |
| 1112 | // and applications not using global effects. |
| 1113 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1114 | // global effects |
| 1115 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1116 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1121 | if (chain != 0) { |
| 1122 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1127 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1128 | const sp<AudioFlinger::Client>& client, |
| 1129 | const sp<IEffectClient>& effectClient, |
| 1130 | int32_t priority, |
| 1131 | int sessionId, |
| 1132 | effect_descriptor_t *desc, |
| 1133 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1134 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1135 | { |
| 1136 | sp<EffectModule> effect; |
| 1137 | sp<EffectHandle> handle; |
| 1138 | status_t lStatus; |
| 1139 | sp<EffectChain> chain; |
| 1140 | bool chainCreated = false; |
| 1141 | bool effectCreated = false; |
| 1142 | bool effectRegistered = false; |
| 1143 | |
| 1144 | lStatus = initCheck(); |
| 1145 | if (lStatus != NO_ERROR) { |
| 1146 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1147 | goto Exit; |
| 1148 | } |
| 1149 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1150 | // Reject any effect on Direct output threads for now, since the format of |
| 1151 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1152 | if (mType == DIRECT) { |
| 1153 | ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s", |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1154 | desc->name, mThreadName); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1155 | lStatus = BAD_VALUE; |
| 1156 | goto Exit; |
| 1157 | } |
| 1158 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1159 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1160 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1161 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 1162 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 1163 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1164 | lStatus = BAD_VALUE; |
| 1165 | goto Exit; |
| 1166 | } |
| 1167 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1168 | // Allow global effects only on offloaded and mixer threads |
| 1169 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1170 | switch (mType) { |
| 1171 | case MIXER: |
| 1172 | case OFFLOAD: |
| 1173 | break; |
| 1174 | case DIRECT: |
| 1175 | case DUPLICATING: |
| 1176 | case RECORD: |
| 1177 | default: |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1178 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", |
| 1179 | desc->name, mThreadName); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1180 | lStatus = BAD_VALUE; |
| 1181 | goto Exit; |
| 1182 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1183 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1184 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1185 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 1186 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 1187 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 1188 | desc->name, desc->flags, mType); |
| 1189 | lStatus = BAD_VALUE; |
| 1190 | goto Exit; |
| 1191 | } |
| 1192 | |
| 1193 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1194 | |
| 1195 | { // scope for mLock |
| 1196 | Mutex::Autolock _l(mLock); |
| 1197 | |
| 1198 | // check for existing effect chain with the requested audio session |
| 1199 | chain = getEffectChain_l(sessionId); |
| 1200 | if (chain == 0) { |
| 1201 | // create a new chain for this session |
| 1202 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1203 | chain = new EffectChain(this, sessionId); |
| 1204 | addEffectChain_l(chain); |
| 1205 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1206 | chainCreated = true; |
| 1207 | } else { |
| 1208 | effect = chain->getEffectFromDesc_l(desc); |
| 1209 | } |
| 1210 | |
| 1211 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1212 | |
| 1213 | if (effect == 0) { |
| 1214 | int id = mAudioFlinger->nextUniqueId(); |
| 1215 | // Check CPU and memory usage |
| 1216 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1217 | if (lStatus != NO_ERROR) { |
| 1218 | goto Exit; |
| 1219 | } |
| 1220 | effectRegistered = true; |
| 1221 | // create a new effect module if none present in the chain |
| 1222 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1223 | lStatus = effect->status(); |
| 1224 | if (lStatus != NO_ERROR) { |
| 1225 | goto Exit; |
| 1226 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1227 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1228 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1229 | lStatus = chain->addEffect_l(effect); |
| 1230 | if (lStatus != NO_ERROR) { |
| 1231 | goto Exit; |
| 1232 | } |
| 1233 | effectCreated = true; |
| 1234 | |
| 1235 | effect->setDevice(mOutDevice); |
| 1236 | effect->setDevice(mInDevice); |
| 1237 | effect->setMode(mAudioFlinger->getMode()); |
| 1238 | effect->setAudioSource(mAudioSource); |
| 1239 | } |
| 1240 | // create effect handle and connect it to effect module |
| 1241 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1242 | lStatus = handle->initCheck(); |
| 1243 | if (lStatus == OK) { |
| 1244 | lStatus = effect->addHandle(handle.get()); |
| 1245 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1246 | if (enabled != NULL) { |
| 1247 | *enabled = (int)effect->isEnabled(); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | Exit: |
| 1252 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1253 | Mutex::Autolock _l(mLock); |
| 1254 | if (effectCreated) { |
| 1255 | chain->removeEffect_l(effect); |
| 1256 | } |
| 1257 | if (effectRegistered) { |
| 1258 | AudioSystem::unregisterEffect(effect->id()); |
| 1259 | } |
| 1260 | if (chainCreated) { |
| 1261 | removeEffectChain_l(chain); |
| 1262 | } |
| 1263 | handle.clear(); |
| 1264 | } |
| 1265 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1266 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1267 | return handle; |
| 1268 | } |
| 1269 | |
| 1270 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) |
| 1271 | { |
| 1272 | Mutex::Autolock _l(mLock); |
| 1273 | return getEffect_l(sessionId, effectId); |
| 1274 | } |
| 1275 | |
| 1276 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 1277 | { |
| 1278 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1279 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1280 | } |
| 1281 | |
| 1282 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1283 | // PlaybackThread::mLock held |
| 1284 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1285 | { |
| 1286 | // check for existing effect chain with the requested audio session |
| 1287 | int sessionId = effect->sessionId(); |
| 1288 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1289 | bool chainCreated = false; |
| 1290 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1291 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1292 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1293 | this, effect->desc().name, effect->desc().flags); |
| 1294 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1295 | if (chain == 0) { |
| 1296 | // create a new chain for this session |
| 1297 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1298 | chain = new EffectChain(this, sessionId); |
| 1299 | addEffectChain_l(chain); |
| 1300 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1301 | chainCreated = true; |
| 1302 | } |
| 1303 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1304 | |
| 1305 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1306 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1307 | this, effect->desc().name, chain.get()); |
| 1308 | return BAD_VALUE; |
| 1309 | } |
| 1310 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1311 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1312 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1313 | status_t status = chain->addEffect_l(effect); |
| 1314 | if (status != NO_ERROR) { |
| 1315 | if (chainCreated) { |
| 1316 | removeEffectChain_l(chain); |
| 1317 | } |
| 1318 | return status; |
| 1319 | } |
| 1320 | |
| 1321 | effect->setDevice(mOutDevice); |
| 1322 | effect->setDevice(mInDevice); |
| 1323 | effect->setMode(mAudioFlinger->getMode()); |
| 1324 | effect->setAudioSource(mAudioSource); |
| 1325 | return NO_ERROR; |
| 1326 | } |
| 1327 | |
| 1328 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1329 | |
| 1330 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1331 | effect_descriptor_t desc = effect->desc(); |
| 1332 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1333 | detachAuxEffect_l(effect->id()); |
| 1334 | } |
| 1335 | |
| 1336 | sp<EffectChain> chain = effect->chain().promote(); |
| 1337 | if (chain != 0) { |
| 1338 | // remove effect chain if removing last effect |
| 1339 | if (chain->removeEffect_l(effect) == 0) { |
| 1340 | removeEffectChain_l(chain); |
| 1341 | } |
| 1342 | } else { |
| 1343 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1348 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1349 | { |
| 1350 | effectChains = mEffectChains; |
| 1351 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1352 | mEffectChains[i]->lock(); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1357 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1358 | { |
| 1359 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1360 | effectChains[i]->unlock(); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 1365 | { |
| 1366 | Mutex::Autolock _l(mLock); |
| 1367 | return getEffectChain_l(sessionId); |
| 1368 | } |
| 1369 | |
| 1370 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const |
| 1371 | { |
| 1372 | size_t size = mEffectChains.size(); |
| 1373 | for (size_t i = 0; i < size; i++) { |
| 1374 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1375 | return mEffectChains[i]; |
| 1376 | } |
| 1377 | } |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1382 | { |
| 1383 | Mutex::Autolock _l(mLock); |
| 1384 | size_t size = mEffectChains.size(); |
| 1385 | for (size_t i = 0; i < size; i++) { |
| 1386 | mEffectChains[i]->setMode_l(mode); |
| 1387 | } |
| 1388 | } |
| 1389 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1390 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1391 | { |
| 1392 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1393 | config->ext.mix.handle = mId; |
| 1394 | config->sample_rate = mSampleRate; |
| 1395 | config->format = mFormat; |
| 1396 | config->channel_mask = mChannelMask; |
| 1397 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1398 | AUDIO_PORT_CONFIG_FORMAT; |
| 1399 | } |
| 1400 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1401 | void AudioFlinger::ThreadBase::systemReady() |
| 1402 | { |
| 1403 | Mutex::Autolock _l(mLock); |
| 1404 | if (mSystemReady) { |
| 1405 | return; |
| 1406 | } |
| 1407 | mSystemReady = true; |
| 1408 | |
| 1409 | for (size_t i = 0; i < mPendingConfigEvents.size(); i++) { |
| 1410 | sendConfigEvent_l(mPendingConfigEvents.editItemAt(i)); |
| 1411 | } |
| 1412 | mPendingConfigEvents.clear(); |
| 1413 | } |
| 1414 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1415 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1416 | // ---------------------------------------------------------------------------- |
| 1417 | // Playback |
| 1418 | // ---------------------------------------------------------------------------- |
| 1419 | |
| 1420 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1421 | AudioStreamOut* output, |
| 1422 | audio_io_handle_t id, |
| 1423 | audio_devices_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1424 | type_t type, |
| 1425 | bool systemReady) |
| 1426 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1427 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1428 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1429 | mMixerBuffer(NULL), |
| 1430 | mMixerBufferSize(0), |
| 1431 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1432 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1433 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1434 | mEffectBuffer(NULL), |
| 1435 | mEffectBufferSize(0), |
| 1436 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1437 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1438 | mSuspended(0), mBytesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1439 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1440 | // mStreamTypes[] initialized in constructor body |
| 1441 | mOutput(output), |
| 1442 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1443 | mMixerStatus(MIXER_IDLE), |
| 1444 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1445 | mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1446 | mBytesRemaining(0), |
| 1447 | mCurrentWriteLength(0), |
| 1448 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1449 | mWriteAckSequence(0), |
| 1450 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1451 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1452 | mScreenState(AudioFlinger::mScreenState), |
| 1453 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1454 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1455 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1456 | // mLatchD, mLatchQ, |
| 1457 | mLatchDValid(false), mLatchQValid(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1458 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1459 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1460 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | |
| 1462 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1463 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1464 | // parameter. |
| 1465 | // |
| 1466 | // If the HAL we are using has support for master volume or master mute, |
| 1467 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1468 | // and the mute set to false). |
| 1469 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1470 | mMasterMute = audioFlinger->masterMute_l(); |
| 1471 | if (mOutput && mOutput->audioHwDev) { |
| 1472 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1473 | mMasterVolume = 1.0; |
| 1474 | } |
| 1475 | |
| 1476 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1477 | mMasterMute = false; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1481 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1482 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1483 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1484 | 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] | 1485 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1486 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1487 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1488 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1492 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1493 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1494 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1495 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1496 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1500 | { |
| 1501 | dumpInternals(fd, args); |
| 1502 | dumpTracks(fd, args); |
| 1503 | dumpEffectChains(fd, args); |
| 1504 | } |
| 1505 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1506 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1507 | { |
| 1508 | const size_t SIZE = 256; |
| 1509 | char buffer[SIZE]; |
| 1510 | String8 result; |
| 1511 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1512 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1513 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1514 | const stream_type_t *st = &mStreamTypes[i]; |
| 1515 | if (i > 0) { |
| 1516 | result.appendFormat(", "); |
| 1517 | } |
| 1518 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1519 | if (st->mute) { |
| 1520 | result.append("M"); |
| 1521 | } |
| 1522 | } |
| 1523 | result.append("\n"); |
| 1524 | write(fd, result.string(), result.length()); |
| 1525 | result.clear(); |
| 1526 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1527 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1528 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1529 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1530 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1531 | |
| 1532 | size_t numtracks = mTracks.size(); |
| 1533 | size_t numactive = mActiveTracks.size(); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1534 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1535 | size_t numactiveseen = 0; |
| 1536 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1537 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1538 | Track::appendDumpHeader(result); |
| 1539 | for (size_t i = 0; i < numtracks; ++i) { |
| 1540 | sp<Track> track = mTracks[i]; |
| 1541 | if (track != 0) { |
| 1542 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1543 | if (active) { |
| 1544 | numactiveseen++; |
| 1545 | } |
| 1546 | track->dump(buffer, SIZE, active); |
| 1547 | result.append(buffer); |
| 1548 | } |
| 1549 | } |
| 1550 | } else { |
| 1551 | result.append("\n"); |
| 1552 | } |
| 1553 | if (numactiveseen != numactive) { |
| 1554 | // some tracks in the active list were not in the tracks list |
| 1555 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1556 | " not in the track list\n"); |
| 1557 | result.append(buffer); |
| 1558 | Track::appendDumpHeader(result); |
| 1559 | for (size_t i = 0; i < numactive; ++i) { |
| 1560 | sp<Track> track = mActiveTracks[i].promote(); |
| 1561 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1562 | track->dump(buffer, SIZE, true); |
| 1563 | result.append(buffer); |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1572 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1573 | 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] | 1574 | |
| 1575 | dumpBase(fd, args); |
| 1576 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1577 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
| 1578 | dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1579 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1580 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1581 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1582 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1583 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1584 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1585 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1586 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1587 | AudioStreamOut *output = mOutput; |
| 1588 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1589 | String8 flagsAsString = outputFlagsToString(flags); |
| 1590 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1594 | |
| 1595 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1596 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1597 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | // ThreadBase virtuals |
| 1601 | void AudioFlinger::PlaybackThread::preExit() |
| 1602 | { |
| 1603 | ALOGV(" preExit()"); |
| 1604 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1605 | // converted to use audio HAL extensions required to support tunneling |
| 1606 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1607 | } |
| 1608 | |
| 1609 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1610 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1611 | const sp<AudioFlinger::Client>& client, |
| 1612 | audio_stream_type_t streamType, |
| 1613 | uint32_t sampleRate, |
| 1614 | audio_format_t format, |
| 1615 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1616 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1617 | const sp<IMemory>& sharedBuffer, |
| 1618 | int sessionId, |
| 1619 | IAudioFlinger::track_flags_t *flags, |
| 1620 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1621 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1622 | status_t *status) |
| 1623 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1624 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1625 | sp<Track> track; |
| 1626 | status_t lStatus; |
| 1627 | |
| 1628 | bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1629 | |
| 1630 | // client expresses a preference for FAST, but we get the final say |
| 1631 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1632 | if ( |
| 1633 | // not timed |
| 1634 | (!isTimed) && |
| 1635 | // either of these use cases: |
| 1636 | ( |
| 1637 | // use case 1: shared buffer with any frame count |
| 1638 | ( |
| 1639 | (sharedBuffer != 0) |
| 1640 | ) || |
Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1641 | // use case 2: frame count is default or at least as large as HAL |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1642 | ( |
Glenn Kasten | 1dfe2f9 | 2015-03-09 12:03:14 -0700 | [diff] [blame] | 1643 | // we formerly checked for a callback handler (non-0 tid), |
| 1644 | // but that is no longer required for TRANSFER_OBTAIN mode |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1645 | ((frameCount == 0) || |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1646 | (frameCount >= mFrameCount)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1647 | ) |
| 1648 | ) && |
| 1649 | // PCM data |
| 1650 | audio_is_linear_pcm(format) && |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1651 | // TODO: extract as a data library function that checks that a computationally |
| 1652 | // expensive downmixer is not required: isFastOutputChannelConversion() |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1653 | (channelMask == mChannelMask || |
Andy Hung | 1f439e1 | 2015-05-19 12:57:41 -0700 | [diff] [blame] | 1654 | mChannelMask != AUDIO_CHANNEL_OUT_STEREO || |
| 1655 | (channelMask == AUDIO_CHANNEL_OUT_MONO |
| 1656 | /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1657 | // hardware sample rate |
| 1658 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1659 | // normal mixer has an associated fast mixer |
| 1660 | hasFastMixer() && |
| 1661 | // there are sufficient fast track slots available |
| 1662 | (mFastTrackAvailMask != 0) |
| 1663 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1664 | // FIXME add a permission test also? |
| 1665 | ) { |
| 1666 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1667 | if (frameCount == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1668 | // read the fast track multiplier property the first time it is needed |
| 1669 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1670 | if (ok != 0) { |
| 1671 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1672 | } |
| 1673 | frameCount = mFrameCount * sFastTrackMultiplier; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1674 | } |
| 1675 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1676 | frameCount, mFrameCount); |
| 1677 | } else { |
| 1678 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1679 | "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
| 1680 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1681 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1682 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1683 | audio_is_linear_pcm(format), |
| 1684 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1685 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | // For normal PCM streaming tracks, update minimum frame count. |
| 1689 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1690 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1691 | // This is probably too conservative, but legacy application code may depend on it. |
| 1692 | // If you change this calculation, also review the start threshold which is related. |
| 1693 | if (!(*flags & IAudioFlinger::TRACK_FAST) |
| 1694 | && audio_is_linear_pcm(format) && sharedBuffer == 0) { |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1695 | // this must match AudioTrack.cpp calculateMinFrameCount(). |
| 1696 | // TODO: Move to a common library |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1697 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1698 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1699 | if (minBufCount < 2) { |
| 1700 | minBufCount = 2; |
| 1701 | } |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1702 | // For normal mixing tracks, if speed is > 1.0f (normal), AudioTrack |
| 1703 | // or the client should compute and pass in a larger buffer request. |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1704 | size_t minFrameCount = |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 1705 | minBufCount * sourceFramesNeededWithTimestretch( |
| 1706 | sampleRate, mNormalFrameCount, |
| 1707 | mSampleRate, AUDIO_TIMESTRETCH_SPEED_NORMAL /*speed*/); |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1708 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1709 | frameCount = minFrameCount; |
| 1710 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1711 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1712 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1713 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1714 | switch (mType) { |
| 1715 | |
| 1716 | case DIRECT: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1717 | if (audio_is_linear_pcm(format)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1718 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1719 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1720 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1721 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1722 | lStatus = BAD_VALUE; |
| 1723 | goto Exit; |
| 1724 | } |
| 1725 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | |
| 1728 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1729 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1730 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1731 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1732 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1733 | lStatus = BAD_VALUE; |
| 1734 | goto Exit; |
| 1735 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | |
| 1738 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1739 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1740 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1741 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1742 | format, mOutput, mFormat); |
| 1743 | lStatus = BAD_VALUE; |
| 1744 | goto Exit; |
| 1745 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1746 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1747 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1748 | lStatus = BAD_VALUE; |
| 1749 | goto Exit; |
| 1750 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1751 | break; |
| 1752 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | lStatus = initCheck(); |
| 1756 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1757 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1758 | goto Exit; |
| 1759 | } |
| 1760 | |
| 1761 | { // scope for mLock |
| 1762 | Mutex::Autolock _l(mLock); |
| 1763 | |
| 1764 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1765 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1766 | // manager |
| 1767 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1768 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1769 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1770 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1771 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1772 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1773 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1774 | strategy, actual); |
| 1775 | lStatus = BAD_VALUE; |
| 1776 | goto Exit; |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (!isTimed) { |
| 1782 | track = new Track(this, client, streamType, sampleRate, format, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1783 | channelMask, frameCount, NULL, sharedBuffer, |
| 1784 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1785 | } else { |
| 1786 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1787 | channelMask, frameCount, sharedBuffer, sessionId, uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1788 | } |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1789 | |
| 1790 | // new Track always returns non-NULL, |
| 1791 | // but TimedTrack::create() is a factory that could fail by returning NULL |
| 1792 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1793 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1794 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1795 | // 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] | 1796 | goto Exit; |
| 1797 | } |
| 1798 | mTracks.add(track); |
| 1799 | |
| 1800 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1801 | if (chain != 0) { |
| 1802 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1803 | track->setMainBuffer(chain->inBuffer()); |
| 1804 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1805 | chain->incTrackCnt(); |
| 1806 | } |
| 1807 | |
| 1808 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1809 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1810 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1811 | // so ask activity manager to do this on our behalf |
| 1812 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | lStatus = NO_ERROR; |
| 1817 | |
| 1818 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1819 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1820 | return track; |
| 1821 | } |
| 1822 | |
| 1823 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1824 | { |
| 1825 | return latency; |
| 1826 | } |
| 1827 | |
| 1828 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1829 | { |
| 1830 | Mutex::Autolock _l(mLock); |
| 1831 | return latency_l(); |
| 1832 | } |
| 1833 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1834 | { |
| 1835 | if (initCheck() == NO_ERROR) { |
| 1836 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1837 | } else { |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1843 | { |
| 1844 | Mutex::Autolock _l(mLock); |
| 1845 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1846 | if (mOutput && mOutput->audioHwDev && |
| 1847 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1848 | mMasterVolume = 1.0; |
| 1849 | } else { |
| 1850 | mMasterVolume = value; |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1855 | { |
| 1856 | Mutex::Autolock _l(mLock); |
| 1857 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1858 | if (mOutput && mOutput->audioHwDev && |
| 1859 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1860 | mMasterMute = false; |
| 1861 | } else { |
| 1862 | mMasterMute = muted; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 1867 | { |
| 1868 | Mutex::Autolock _l(mLock); |
| 1869 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1870 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 1874 | { |
| 1875 | Mutex::Autolock _l(mLock); |
| 1876 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1877 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 1881 | { |
| 1882 | Mutex::Autolock _l(mLock); |
| 1883 | return mStreamTypes[stream].volume; |
| 1884 | } |
| 1885 | |
| 1886 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1887 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1888 | { |
| 1889 | status_t status = ALREADY_EXISTS; |
| 1890 | |
| 1891 | // set retry count for buffer fill |
| 1892 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1893 | if (mActiveTracks.indexOf(track) < 0) { |
| 1894 | // the track is newly added, make sure it fills up all its |
| 1895 | // buffers before playing. This is to ensure the client will |
| 1896 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1897 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1898 | TrackBase::track_state state = track->mState; |
| 1899 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1900 | status = AudioSystem::startOutput(mId, track->streamType(), |
| 1901 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1902 | mLock.lock(); |
| 1903 | // abort track was stopped/paused while we released the lock |
| 1904 | if (state != track->mState) { |
| 1905 | if (status == NO_ERROR) { |
| 1906 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1907 | AudioSystem::stopOutput(mId, track->streamType(), |
| 1908 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1909 | mLock.lock(); |
| 1910 | } |
| 1911 | return INVALID_OPERATION; |
| 1912 | } |
| 1913 | // abort if start is rejected by audio policy manager |
| 1914 | if (status != NO_ERROR) { |
| 1915 | return PERMISSION_DENIED; |
| 1916 | } |
| 1917 | #ifdef ADD_BATTERY_DATA |
| 1918 | // to track the speaker usage |
| 1919 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 1920 | #endif |
| 1921 | } |
| 1922 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1923 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1924 | track->mResetDone = false; |
| 1925 | track->mPresentationCompleteFrames = 0; |
| 1926 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1927 | mWakeLockUids.add(track->uid()); |
| 1928 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1929 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1930 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1931 | if (chain != 0) { |
| 1932 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 1933 | track->sessionId()); |
| 1934 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | status = NO_ERROR; |
| 1938 | } |
| 1939 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1940 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1941 | return status; |
| 1942 | } |
| 1943 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1944 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1945 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1946 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1947 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1948 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 1949 | track->mState = TrackBase::STOPPED; |
| 1950 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1951 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1952 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1953 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1954 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1955 | |
| 1956 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1960 | { |
| 1961 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 1962 | mTracks.remove(track); |
| 1963 | deleteTrackName_l(track->name()); |
| 1964 | // redundant as track is about to be destroyed, for dumpsys only |
| 1965 | track->mName = -1; |
| 1966 | if (track->isFastTrack()) { |
| 1967 | int index = track->mFastIndex; |
| 1968 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 1969 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1970 | mFastTrackAvailMask |= 1 << index; |
| 1971 | // redundant as track is about to be destroyed, for dumpsys only |
| 1972 | track->mFastIndex = -1; |
| 1973 | } |
| 1974 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1975 | if (chain != 0) { |
| 1976 | chain->decTrackCnt(); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1980 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1981 | { |
| 1982 | // Thread could be blocked waiting for async |
| 1983 | // so signal it to handle state changes immediately |
| 1984 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1985 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1986 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1987 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1990 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1991 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1992 | Mutex::Autolock _l(mLock); |
| 1993 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1994 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1997 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 1998 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1999 | free(s); |
| 2000 | return out_s8; |
| 2001 | } |
| 2002 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2003 | void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2004 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 2005 | ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2006 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2007 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2008 | |
| 2009 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2010 | case AUDIO_OUTPUT_OPENED: |
| 2011 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2012 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2013 | desc->mChannelMask = mChannelMask; |
| 2014 | desc->mSamplingRate = mSampleRate; |
| 2015 | desc->mFormat = mFormat; |
| 2016 | desc->mFrameCount = mNormalFrameCount; // FIXME see |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2017 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2018 | desc->mLatency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2019 | break; |
| 2020 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2021 | case AUDIO_OUTPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2022 | default: |
| 2023 | break; |
| 2024 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2025 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2026 | } |
| 2027 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2028 | void AudioFlinger::PlaybackThread::writeCallback() |
| 2029 | { |
| 2030 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2031 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | void AudioFlinger::PlaybackThread::drainCallback() |
| 2035 | { |
| 2036 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2037 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2040 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2041 | { |
| 2042 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2043 | // reject out of sequence requests |
| 2044 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 2045 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2046 | mWaitWorkCV.signal(); |
| 2047 | } |
| 2048 | } |
| 2049 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2050 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2051 | { |
| 2052 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2053 | // reject out of sequence requests |
| 2054 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 2055 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2056 | mWaitWorkCV.signal(); |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | // static |
| 2061 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 2062 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2063 | void *cookie) |
| 2064 | { |
| 2065 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 2066 | ALOGV("asyncCallback() event %d", event); |
| 2067 | switch (event) { |
| 2068 | case STREAM_CBK_EVENT_WRITE_READY: |
| 2069 | me->writeCallback(); |
| 2070 | break; |
| 2071 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 2072 | me->drainCallback(); |
| 2073 | break; |
| 2074 | default: |
| 2075 | ALOGW("asyncCallback() unknown event %d", event); |
| 2076 | break; |
| 2077 | } |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2081 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2082 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2083 | // 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] | 2084 | mSampleRate = mOutput->getSampleRate(); |
| 2085 | mChannelMask = mOutput->getChannelMask(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2086 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2087 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2088 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2089 | if ((mType == MIXER || mType == DUPLICATING) |
| 2090 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 2091 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 2092 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2093 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 2094 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2095 | |
| 2096 | // Get actual HAL format. |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2097 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 2098 | // Get format from the shim, which will be different than the HAL format |
| 2099 | // if playing compressed audio over HDMI passthrough. |
| 2100 | mFormat = mOutput->getFormat(); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2101 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2102 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2103 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2104 | if ((mType == MIXER || mType == DUPLICATING) |
| 2105 | && !isValidPcmSinkFormat(mFormat)) { |
| 2106 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2107 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2108 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2109 | mFrameSize = mOutput->getFrameSize(); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2110 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2111 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2112 | if (mFrameCount & 15) { |
| 2113 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 2114 | mFrameCount); |
| 2115 | } |
| 2116 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2117 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2118 | (mOutput->stream->set_callback != NULL)) { |
| 2119 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2120 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2121 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2122 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2126 | mHwSupportsPause = false; |
| 2127 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2128 | if (mOutput->stream->pause != NULL) { |
| 2129 | if (mOutput->stream->resume != NULL) { |
| 2130 | mHwSupportsPause = true; |
| 2131 | } else { |
| 2132 | ALOGW("direct output implements pause but not resume"); |
| 2133 | } |
| 2134 | } else if (mOutput->stream->resume != NULL) { |
| 2135 | ALOGW("direct output implements resume but not pause"); |
| 2136 | } |
| 2137 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 2138 | if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) { |
| 2139 | LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume"); |
| 2140 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2141 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2142 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2143 | // For best precision, we use float instead of the associated output |
| 2144 | // device format (typically PCM 16 bit). |
| 2145 | |
| 2146 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2147 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2148 | mBufferSize = mFrameSize * mFrameCount; |
| 2149 | |
| 2150 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2151 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2152 | // (if a valid mask) to avoid premature downmix. |
| 2153 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2154 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2155 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2156 | } |
| 2157 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2158 | // 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] | 2159 | double multiplier = 1.0; |
| 2160 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2161 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2162 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2163 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2164 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2165 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2166 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2167 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2168 | maxNormalFrameCount = minNormalFrameCount; |
| 2169 | } |
| 2170 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2171 | if (multiplier <= 1.0) { |
| 2172 | multiplier = 1.0; |
| 2173 | } else if (multiplier <= 2.0) { |
| 2174 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2175 | multiplier = 2.0; |
| 2176 | } else { |
| 2177 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2178 | } |
| 2179 | } else { |
| 2180 | // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2181 | // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2182 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 2183 | // constraint) |
| 2184 | // FIXME this rounding up should not be done if no HAL SRC |
| 2185 | uint32_t truncMult = (uint32_t) multiplier; |
| 2186 | if ((truncMult & 1)) { |
| 2187 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 2188 | ++truncMult; |
| 2189 | } |
| 2190 | } |
| 2191 | multiplier = (double) truncMult; |
| 2192 | } |
| 2193 | } |
| 2194 | mNormalFrameCount = multiplier * mFrameCount; |
| 2195 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2196 | if (mType == MIXER || mType == DUPLICATING) { |
| 2197 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2198 | } |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2199 | ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2200 | mNormalFrameCount); |
| 2201 | |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2202 | // Check if we want to throttle the processing to no more than 2x normal rate |
| 2203 | mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2204 | mThreadThrottleTimeMs = 0; |
| 2205 | mThreadThrottleEndMs = 0; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2206 | mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate); |
| 2207 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2208 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2209 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2210 | free(mSinkBuffer); |
| 2211 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2212 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2213 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2214 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2215 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2216 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2217 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2218 | // drives the output. |
| 2219 | free(mMixerBuffer); |
| 2220 | mMixerBuffer = NULL; |
| 2221 | if (mMixerBufferEnabled) { |
| 2222 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2223 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2224 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2225 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2226 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2227 | free(mEffectBuffer); |
| 2228 | mEffectBuffer = NULL; |
| 2229 | if (mEffectBufferEnabled) { |
| 2230 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2231 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2232 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2233 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2234 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2235 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2236 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2237 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2238 | // 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] | 2239 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2240 | // matter. |
| 2241 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2242 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2243 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2244 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2249 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2250 | { |
| 2251 | if (halFrames == NULL || dspFrames == NULL) { |
| 2252 | return BAD_VALUE; |
| 2253 | } |
| 2254 | Mutex::Autolock _l(mLock); |
| 2255 | if (initCheck() != NO_ERROR) { |
| 2256 | return INVALID_OPERATION; |
| 2257 | } |
| 2258 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 2259 | *halFrames = framesWritten; |
| 2260 | |
| 2261 | if (isSuspended()) { |
| 2262 | // return an estimation of rendered frames when the output is suspended |
| 2263 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
| 2264 | *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; |
| 2265 | return NO_ERROR; |
| 2266 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2267 | status_t status; |
| 2268 | uint32_t frames; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2269 | status = mOutput->getRenderPosition(&frames); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2270 | *dspFrames = (size_t)frames; |
| 2271 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const |
| 2276 | { |
| 2277 | Mutex::Autolock _l(mLock); |
| 2278 | uint32_t result = 0; |
| 2279 | if (getEffectChain_l(sessionId) != 0) { |
| 2280 | result = EFFECT_SESSION; |
| 2281 | } |
| 2282 | |
| 2283 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2284 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2285 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2286 | result |= TRACK_SESSION; |
| 2287 | break; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | return result; |
| 2292 | } |
| 2293 | |
| 2294 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2295 | { |
| 2296 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2297 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2298 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2299 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2300 | } |
| 2301 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2302 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2303 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2304 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2305 | } |
| 2306 | } |
| 2307 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2308 | } |
| 2309 | |
| 2310 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2311 | AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2312 | { |
| 2313 | Mutex::Autolock _l(mLock); |
| 2314 | return mOutput; |
| 2315 | } |
| 2316 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2317 | AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2318 | { |
| 2319 | Mutex::Autolock _l(mLock); |
| 2320 | AudioStreamOut *output = mOutput; |
| 2321 | mOutput = NULL; |
| 2322 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2323 | // must push a NULL and wait for ack |
| 2324 | mOutputSink.clear(); |
| 2325 | mPipeSink.clear(); |
| 2326 | mNormalSink.clear(); |
| 2327 | return output; |
| 2328 | } |
| 2329 | |
| 2330 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2331 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2332 | { |
| 2333 | if (mOutput == NULL) { |
| 2334 | return NULL; |
| 2335 | } |
| 2336 | return &mOutput->stream->common; |
| 2337 | } |
| 2338 | |
| 2339 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2340 | { |
| 2341 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2342 | } |
| 2343 | |
| 2344 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2345 | { |
| 2346 | if (!isValidSyncEvent(event)) { |
| 2347 | return BAD_VALUE; |
| 2348 | } |
| 2349 | |
| 2350 | Mutex::Autolock _l(mLock); |
| 2351 | |
| 2352 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2353 | sp<Track> track = mTracks[i]; |
| 2354 | if (event->triggerSession() == track->sessionId()) { |
| 2355 | (void) track->setSyncEvent(event); |
| 2356 | return NO_ERROR; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | return NAME_NOT_FOUND; |
| 2361 | } |
| 2362 | |
| 2363 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2364 | { |
| 2365 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2366 | } |
| 2367 | |
| 2368 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2369 | const Vector< sp<Track> >& tracksToRemove) |
| 2370 | { |
| 2371 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2372 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2373 | for (size_t i = 0 ; i < count ; i++) { |
| 2374 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2375 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2376 | AudioSystem::stopOutput(mId, track->streamType(), |
| 2377 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2378 | #ifdef ADD_BATTERY_DATA |
| 2379 | // to track the speaker usage |
| 2380 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2381 | #endif |
| 2382 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2383 | AudioSystem::releaseOutput(mId, track->streamType(), |
| 2384 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2385 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2392 | { |
| 2393 | if (!mMasterMute) { |
| 2394 | char value[PROPERTY_VALUE_MAX]; |
| 2395 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2396 | char *endptr; |
| 2397 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2398 | if (*endptr == '\0' && ul != 0) { |
| 2399 | ALOGD("Silence is golden"); |
| 2400 | // The setprop command will not allow a property to be changed after |
| 2401 | // the first time it is set, so we don't have to worry about un-muting. |
| 2402 | setMasterMute_l(true); |
| 2403 | } |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2409 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2410 | { |
| 2411 | // FIXME rewrite to reduce number of system calls |
| 2412 | mLastWriteTime = systemTime(); |
| 2413 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2414 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2415 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2416 | |
| 2417 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2418 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2419 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2420 | const size_t count = mBytesRemaining / mFrameSize; |
| 2421 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2422 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2423 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2424 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2425 | if (screenState != mScreenState) { |
| 2426 | mScreenState = screenState; |
| 2427 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2428 | if (pipe != NULL) { |
| 2429 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2430 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2431 | } |
| 2432 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2433 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2434 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2435 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2436 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2437 | } else { |
| 2438 | bytesWritten = framesWritten; |
| 2439 | } |
Glenn Kasten | efaa7ab | 2014-08-20 08:48:54 -0700 | [diff] [blame] | 2440 | mLatchDValid = false; |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2441 | status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2442 | if (status == NO_ERROR) { |
| 2443 | size_t totalFramesWritten = mNormalSink->framesWritten(); |
| 2444 | if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { |
| 2445 | mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2446 | // mLatchD.mFramesReleased is set immediately before D is clocked into Q |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2447 | mLatchDValid = true; |
| 2448 | } |
| 2449 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2450 | // otherwise use the HAL / AudioStreamOut directly |
| 2451 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2452 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2453 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2454 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2455 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2456 | mWriteAckSequence += 2; |
| 2457 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2458 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2459 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2460 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2461 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2462 | // They are used e.g for multichannel PCM playback over HDMI. |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2463 | bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2464 | if (mUseAsyncWrite && |
| 2465 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2466 | // 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] | 2467 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2468 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2469 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2470 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2473 | mNumWrites++; |
| 2474 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2475 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2476 | return bytesWritten; |
| 2477 | } |
| 2478 | |
| 2479 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2480 | { |
| 2481 | if (mOutput->stream->drain) { |
| 2482 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2483 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2484 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2485 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2486 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2487 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2488 | } |
| 2489 | mOutput->stream->drain(mOutput->stream, |
| 2490 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2491 | : AUDIO_DRAIN_ALL); |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2496 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2497 | { |
| 2498 | Mutex::Autolock _l(mLock); |
| 2499 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2500 | sp<Track> track = mTracks[i]; |
| 2501 | track->invalidate(); |
| 2502 | } |
| 2503 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | /* |
| 2507 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2508 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2509 | - mActiveSleepTimeUs from activeSleepTimeUs() |
| 2510 | - mIdleSleepTimeUs from idleSleepTimeUs() |
| 2511 | - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2512 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2513 | |
| 2514 | The parameters that affect these derived values are: |
| 2515 | - frame count |
| 2516 | - frame size |
| 2517 | - sample rate |
| 2518 | - device type: A2DP or not |
| 2519 | - device latency |
| 2520 | - format: PCM or not |
| 2521 | - active sleep time |
| 2522 | - idle sleep time |
| 2523 | */ |
| 2524 | |
| 2525 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2526 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2527 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2528 | mActiveSleepTimeUs = activeSleepTimeUs(); |
| 2529 | mIdleSleepTimeUs = idleSleepTimeUs(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2533 | { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2534 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2535 | this, streamType, mTracks.size()); |
| 2536 | Mutex::Autolock _l(mLock); |
| 2537 | |
| 2538 | size_t size = mTracks.size(); |
| 2539 | for (size_t i = 0; i < size; i++) { |
| 2540 | sp<Track> t = mTracks[i]; |
| 2541 | if (t->streamType() == streamType) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2542 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2548 | { |
| 2549 | int session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2550 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2551 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2552 | bool ownsBuffer = false; |
| 2553 | |
| 2554 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 2555 | if (session > 0) { |
| 2556 | // 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] | 2557 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2558 | if (mType != DIRECT) { |
| 2559 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2560 | buffer = new int16_t[numSamples]; |
| 2561 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2562 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2563 | ownsBuffer = true; |
| 2564 | } |
| 2565 | |
| 2566 | // Attach all tracks with same session ID to this chain. |
| 2567 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2568 | sp<Track> track = mTracks[i]; |
| 2569 | if (session == track->sessionId()) { |
| 2570 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2571 | buffer); |
| 2572 | track->setMainBuffer(buffer); |
| 2573 | chain->incTrackCnt(); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | // indicate all active tracks in the chain |
| 2578 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2579 | sp<Track> track = mActiveTracks[i].promote(); |
| 2580 | if (track == 0) { |
| 2581 | continue; |
| 2582 | } |
| 2583 | if (session == track->sessionId()) { |
| 2584 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2585 | chain->incActiveTrackCnt(); |
| 2586 | } |
| 2587 | } |
| 2588 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2589 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2590 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2591 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2592 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2593 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
| 2594 | // chains list in order to be processed last as it contains output stage effects |
| 2595 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2596 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
| 2597 | // after track specific effects and before output stage |
| 2598 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 2599 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
| 2600 | // Effect chain for other sessions are inserted at beginning of effect |
| 2601 | // chains list to be processed before output mix effects. Relative order between other |
| 2602 | // sessions is not important |
| 2603 | size_t size = mEffectChains.size(); |
| 2604 | size_t i = 0; |
| 2605 | for (i = 0; i < size; i++) { |
| 2606 | if (mEffectChains[i]->sessionId() < session) { |
| 2607 | break; |
| 2608 | } |
| 2609 | } |
| 2610 | mEffectChains.insertAt(chain, i); |
| 2611 | checkSuspendOnAddEffectChain_l(chain); |
| 2612 | |
| 2613 | return NO_ERROR; |
| 2614 | } |
| 2615 | |
| 2616 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2617 | { |
| 2618 | int session = chain->sessionId(); |
| 2619 | |
| 2620 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2621 | |
| 2622 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2623 | if (chain == mEffectChains[i]) { |
| 2624 | mEffectChains.removeAt(i); |
| 2625 | // detach all active tracks from the chain |
| 2626 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2627 | sp<Track> track = mActiveTracks[i].promote(); |
| 2628 | if (track == 0) { |
| 2629 | continue; |
| 2630 | } |
| 2631 | if (session == track->sessionId()) { |
| 2632 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2633 | chain.get(), session); |
| 2634 | chain->decActiveTrackCnt(); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | // detach all tracks with same session ID from this chain |
| 2639 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2640 | sp<Track> track = mTracks[i]; |
| 2641 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2642 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2643 | chain->decTrackCnt(); |
| 2644 | } |
| 2645 | } |
| 2646 | break; |
| 2647 | } |
| 2648 | } |
| 2649 | return mEffectChains.size(); |
| 2650 | } |
| 2651 | |
| 2652 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2653 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2654 | { |
| 2655 | Mutex::Autolock _l(mLock); |
| 2656 | return attachAuxEffect_l(track, EffectId); |
| 2657 | } |
| 2658 | |
| 2659 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2660 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2661 | { |
| 2662 | status_t status = NO_ERROR; |
| 2663 | |
| 2664 | if (EffectId == 0) { |
| 2665 | track->setAuxBuffer(0, NULL); |
| 2666 | } else { |
| 2667 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2668 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2669 | if (effect != 0) { |
| 2670 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2671 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2672 | } else { |
| 2673 | status = INVALID_OPERATION; |
| 2674 | } |
| 2675 | } else { |
| 2676 | status = BAD_VALUE; |
| 2677 | } |
| 2678 | } |
| 2679 | return status; |
| 2680 | } |
| 2681 | |
| 2682 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2683 | { |
| 2684 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2685 | sp<Track> track = mTracks[i]; |
| 2686 | if (track->auxEffectId() == effectId) { |
| 2687 | attachAuxEffect_l(track, 0); |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2693 | { |
| 2694 | Vector< sp<Track> > tracksToRemove; |
| 2695 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2696 | mStandbyTimeNs = systemTime(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2697 | |
| 2698 | // MIXER |
| 2699 | nsecs_t lastWarning = 0; |
| 2700 | |
| 2701 | // DUPLICATING |
| 2702 | // FIXME could this be made local to while loop? |
| 2703 | writeFrames = 0; |
| 2704 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2705 | int lastGeneration = 0; |
| 2706 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2707 | cacheParameters_l(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2708 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2709 | |
| 2710 | if (mType == MIXER) { |
| 2711 | sleepTimeShift = 0; |
| 2712 | } |
| 2713 | |
| 2714 | CpuStats cpuStats; |
| 2715 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2716 | |
| 2717 | acquireWakeLock(); |
| 2718 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2719 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2720 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2721 | // and then that string will be logged at the next convenient opportunity. |
| 2722 | const char *logString = NULL; |
| 2723 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2724 | checkSilentMode_l(); |
| 2725 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2726 | while (!exitPending()) |
| 2727 | { |
| 2728 | cpuStats.sample(myName); |
| 2729 | |
| 2730 | Vector< sp<EffectChain> > effectChains; |
| 2731 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2732 | { // scope for mLock |
| 2733 | |
| 2734 | Mutex::Autolock _l(mLock); |
| 2735 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2736 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2737 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2738 | if (logString != NULL) { |
| 2739 | mNBLogWriter->logTimestamp(); |
| 2740 | mNBLogWriter->log(logString); |
| 2741 | logString = NULL; |
| 2742 | } |
| 2743 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2744 | // Gather the framesReleased counters for all active tracks, |
| 2745 | // and latch them atomically with the timestamp. |
| 2746 | // FIXME We're using raw pointers as indices. A unique track ID would be a better index. |
| 2747 | mLatchD.mFramesReleased.clear(); |
| 2748 | size_t size = mActiveTracks.size(); |
| 2749 | for (size_t i = 0; i < size; i++) { |
| 2750 | sp<Track> t = mActiveTracks[i].promote(); |
| 2751 | if (t != 0) { |
| 2752 | mLatchD.mFramesReleased.add(t.get(), |
| 2753 | t->mAudioTrackServerProxy->framesReleased()); |
| 2754 | } |
| 2755 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2756 | if (mLatchDValid) { |
| 2757 | mLatchQ = mLatchD; |
| 2758 | mLatchDValid = false; |
| 2759 | mLatchQValid = true; |
| 2760 | } |
| 2761 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2762 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2763 | if (mSignalPending) { |
| 2764 | // A signal was raised while we were unlocked |
| 2765 | mSignalPending = false; |
| 2766 | } else if (waitingAsyncCallback_l()) { |
| 2767 | if (exitPending()) { |
| 2768 | break; |
| 2769 | } |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2770 | bool released = false; |
| 2771 | // The following works around a bug in the offload driver. Ideally we would release |
| 2772 | // the wake lock every time, but that causes the last offload buffer(s) to be |
| 2773 | // dropped while the device is on battery, so we need to hold a wake lock during |
| 2774 | // the drain phase. |
| 2775 | if (mBytesRemaining && !(mDrainSequence & 1)) { |
| 2776 | releaseWakeLock_l(); |
| 2777 | released = true; |
| 2778 | } |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2779 | mWakeLockUids.clear(); |
| 2780 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2781 | ALOGV("wait async completion"); |
| 2782 | mWaitWorkCV.wait(mLock); |
| 2783 | ALOGV("async completion/wake"); |
Marco Nelissen | 078538c | 2015-05-12 09:17:57 -0700 | [diff] [blame] | 2784 | if (released) { |
| 2785 | acquireWakeLock_l(); |
| 2786 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2787 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2788 | mSleepTimeUs = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2789 | |
| 2790 | continue; |
| 2791 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2792 | if ((!mActiveTracks.size() && systemTime() > mStandbyTimeNs) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2793 | isSuspended()) { |
| 2794 | // put audio hardware into standby after short delay |
| 2795 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2796 | |
| 2797 | threadLoop_standby(); |
| 2798 | |
| 2799 | mStandby = true; |
| 2800 | } |
| 2801 | |
| 2802 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2803 | // we're about to wait, flush the binder command buffer |
| 2804 | IPCThreadState::self()->flushCommands(); |
| 2805 | |
| 2806 | clearOutputTracks(); |
| 2807 | |
| 2808 | if (exitPending()) { |
| 2809 | break; |
| 2810 | } |
| 2811 | |
| 2812 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2813 | mWakeLockUids.clear(); |
| 2814 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2815 | // wait until we have something to do... |
| 2816 | ALOGV("%s going to sleep", myName.string()); |
| 2817 | mWaitWorkCV.wait(mLock); |
| 2818 | ALOGV("%s waking up", myName.string()); |
| 2819 | acquireWakeLock_l(); |
| 2820 | |
| 2821 | mMixerStatus = MIXER_IDLE; |
| 2822 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2823 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2824 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2825 | checkSilentMode_l(); |
| 2826 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2827 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
| 2828 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2829 | if (mType == MIXER) { |
| 2830 | sleepTimeShift = 0; |
| 2831 | } |
| 2832 | |
| 2833 | continue; |
| 2834 | } |
| 2835 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2836 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2837 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2838 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2839 | // compare with previously applied list |
| 2840 | if (lastGeneration != mActiveTracksGeneration) { |
| 2841 | // update wakelock |
| 2842 | updateWakeLockUids_l(mWakeLockUids); |
| 2843 | lastGeneration = mActiveTracksGeneration; |
| 2844 | } |
| 2845 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2846 | // prevent any changes in effect chain list and in each effect chain |
| 2847 | // during mixing and effect process as the audio buffers could be deleted |
| 2848 | // or modified if an effect is created or deleted |
| 2849 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2850 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2851 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2852 | if (mBytesRemaining == 0) { |
| 2853 | mCurrentWriteLength = 0; |
| 2854 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 2855 | // threadLoop_mix() sets mCurrentWriteLength |
| 2856 | threadLoop_mix(); |
| 2857 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 2858 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2859 | // threadLoop_sleepTime sets mSleepTimeUs to 0 if data |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2860 | // must be written to HAL |
| 2861 | threadLoop_sleepTime(); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2862 | if (mSleepTimeUs == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2863 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2864 | } |
| 2865 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2866 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2867 | // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2868 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 2869 | // or mSinkBuffer (if there are no effects). |
| 2870 | // |
| 2871 | // This is done pre-effects computation; if effects change to |
| 2872 | // support higher precision, this needs to move. |
| 2873 | // |
| 2874 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2875 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2876 | if (mMixerBufferValid) { |
| 2877 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 2878 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 2879 | |
| 2880 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 2881 | mNormalFrameCount * mChannelCount); |
| 2882 | } |
| 2883 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2884 | mBytesRemaining = mCurrentWriteLength; |
| 2885 | if (isSuspended()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2886 | mSleepTimeUs = suspendSleepTimeUs(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2887 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2888 | mBytesWritten += mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2889 | mBytesRemaining = 0; |
| 2890 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2891 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2892 | // only process effects if we're going to write |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2893 | if (mSleepTimeUs == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2894 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2895 | effectChains[i]->process_l(); |
| 2896 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2897 | } |
| 2898 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2899 | // Process effect chains for offloaded thread even if no audio |
| 2900 | // was read from audio track: process only updates effect state |
| 2901 | // and thus does have to be synchronized with audio writes but may have |
| 2902 | // to be called while waiting for async write callback |
| 2903 | if (mType == OFFLOAD) { |
| 2904 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2905 | effectChains[i]->process_l(); |
| 2906 | } |
| 2907 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2908 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2909 | // Only if the Effects buffer is enabled and there is data in the |
| 2910 | // Effects buffer (buffer valid), we need to |
| 2911 | // copy into the sink buffer. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2912 | // TODO use mSleepTimeUs == 0 as an additional condition. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2913 | if (mEffectBufferValid) { |
| 2914 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
| 2915 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 2916 | mNormalFrameCount * mChannelCount); |
| 2917 | } |
| 2918 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2919 | // enable changes in effect chain |
| 2920 | unlockEffectChains(effectChains); |
| 2921 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2922 | if (!waitingAsyncCallback()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2923 | // mSleepTimeUs == 0 means we must write to audio hardware |
| 2924 | if (mSleepTimeUs == 0) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2925 | ssize_t ret = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2926 | if (mBytesRemaining) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2927 | ret = threadLoop_write(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2928 | if (ret < 0) { |
| 2929 | mBytesRemaining = 0; |
| 2930 | } else { |
| 2931 | mBytesWritten += ret; |
| 2932 | mBytesRemaining -= ret; |
| 2933 | } |
| 2934 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 2935 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 2936 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2937 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2938 | if (mType == MIXER && !mStandby) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2939 | // write blocked detection |
| 2940 | nsecs_t now = systemTime(); |
| 2941 | nsecs_t delta = now - mLastWriteTime; |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2942 | if (delta > maxPeriod) { |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2943 | mNumDelayedWrites++; |
| 2944 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2945 | ATRACE_NAME("underrun"); |
| 2946 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2947 | ns2ms(delta), mNumDelayedWrites, this); |
| 2948 | lastWarning = now; |
| 2949 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2950 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2951 | |
| 2952 | if (mThreadThrottle |
| 2953 | && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks) |
| 2954 | && ret > 0) { // we wrote something |
| 2955 | // Limit MixerThread data processing to no more than twice the |
| 2956 | // expected processing rate. |
| 2957 | // |
| 2958 | // This helps prevent underruns with NuPlayer and other applications |
| 2959 | // which may set up buffers that are close to the minimum size, or use |
| 2960 | // deep buffers, and rely on a double-buffering sleep strategy to fill. |
| 2961 | // |
| 2962 | // The throttle smooths out sudden large data drains from the device, |
| 2963 | // e.g. when it comes out of standby, which often causes problems with |
| 2964 | // (1) mixer threads without a fast mixer (which has its own warm-up) |
| 2965 | // (2) minimum buffer sized tracks (even if the track is full, |
| 2966 | // the app won't fill fast enough to handle the sudden draw). |
| 2967 | |
| 2968 | const int32_t deltaMs = delta / 1000000; |
| 2969 | const int32_t throttleMs = mHalfBufferMs - deltaMs; |
| 2970 | if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) { |
| 2971 | usleep(throttleMs * 1000); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2972 | // notify of throttle start on verbose log |
| 2973 | ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs, |
| 2974 | "mixer(%p) throttle begin:" |
| 2975 | " ret(%zd) deltaMs(%d) requires sleep %d ms", |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2976 | this, ret, deltaMs, throttleMs); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 2977 | mThreadThrottleTimeMs += throttleMs; |
| 2978 | } else { |
| 2979 | uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs; |
| 2980 | if (diff > 0) { |
| 2981 | // notify of throttle end on debug log |
| 2982 | ALOGD("mixer(%p) throttle end: throttle time(%u)", this, diff); |
| 2983 | mThreadThrottleEndMs = mThreadThrottleTimeMs; |
| 2984 | } |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2987 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2988 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2989 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2990 | ATRACE_BEGIN("sleep"); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 2991 | usleep(mSleepTimeUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2992 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2993 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | // Finally let go of removed track(s), without the lock held |
| 2997 | // since we can't guarantee the destructors won't acquire that |
| 2998 | // same lock. This will also mutate and push a new fast mixer state. |
| 2999 | threadLoop_removeTracks(tracksToRemove); |
| 3000 | tracksToRemove.clear(); |
| 3001 | |
| 3002 | // FIXME I don't understand the need for this here; |
| 3003 | // it was in the original code but maybe the |
| 3004 | // assignment in saveOutputTracks() makes this unnecessary? |
| 3005 | clearOutputTracks(); |
| 3006 | |
| 3007 | // Effect chains will be actually deleted here if they were removed from |
| 3008 | // mEffectChains list during mixing or effects processing |
| 3009 | effectChains.clear(); |
| 3010 | |
| 3011 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 3012 | // is now local to this block, but will keep it for now (at least until merge done). |
| 3013 | } |
| 3014 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3015 | threadLoop_exit(); |
| 3016 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 3017 | if (!mStandby) { |
| 3018 | threadLoop_standby(); |
| 3019 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3023 | mWakeLockUids.clear(); |
| 3024 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3025 | |
| 3026 | ALOGV("Thread %p type %d exiting", this, mType); |
| 3027 | return false; |
| 3028 | } |
| 3029 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3030 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 3031 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 3032 | { |
| 3033 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 3034 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3035 | for (size_t i=0 ; i<count ; i++) { |
| 3036 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 3037 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 3038 | mWakeLockUids.remove(track->uid()); |
| 3039 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3040 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 3041 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 3042 | if (chain != 0) { |
| 3043 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 3044 | track->sessionId()); |
| 3045 | chain->decActiveTrackCnt(); |
| 3046 | } |
| 3047 | if (track->isTerminated()) { |
| 3048 | removeTrack_l(track); |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3054 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3055 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 3056 | { |
| 3057 | if (mNormalSink != 0) { |
| 3058 | return mNormalSink->getTimestamp(timestamp); |
| 3059 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 3060 | if ((mType == OFFLOAD || mType == DIRECT) |
| 3061 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3062 | uint64_t position64; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3063 | int ret = mOutput->getPresentationPosition(&position64, ×tamp.mTime); |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 3064 | if (ret == 0) { |
| 3065 | timestamp.mPosition = (uint32_t)position64; |
| 3066 | return NO_ERROR; |
| 3067 | } |
| 3068 | } |
| 3069 | return INVALID_OPERATION; |
| 3070 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3071 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3072 | status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3073 | audio_patch_handle_t *handle) |
| 3074 | { |
| 3075 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3076 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
| 3077 | if (mFastMixer != 0) { |
| 3078 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3079 | FastMixerState *state = sq->begin(); |
| 3080 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3081 | previousCommand = state->mCommand; |
| 3082 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3083 | sq->end(); |
| 3084 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3085 | } else { |
| 3086 | sq->end(false /*didModify*/); |
| 3087 | } |
| 3088 | } |
| 3089 | status_t status = PlaybackThread::createAudioPatch_l(patch, handle); |
| 3090 | |
| 3091 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3092 | ALOG_ASSERT(mFastMixer != 0); |
| 3093 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3094 | FastMixerState *state = sq->begin(); |
| 3095 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3096 | state->mCommand = previousCommand; |
| 3097 | sq->end(); |
| 3098 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3099 | } |
| 3100 | |
| 3101 | return status; |
| 3102 | } |
| 3103 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3104 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 3105 | audio_patch_handle_t *handle) |
| 3106 | { |
| 3107 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3108 | |
| 3109 | // store new device and send to effects |
| 3110 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 3111 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 3112 | type |= patch->sinks[i].ext.device.type; |
| 3113 | } |
| 3114 | |
| 3115 | #ifdef ADD_BATTERY_DATA |
| 3116 | // when changing the audio output device, call addBatteryData to notify |
| 3117 | // the change |
| 3118 | if (mOutDevice != type) { |
| 3119 | uint32_t params = 0; |
| 3120 | // check whether speaker is on |
| 3121 | if (type & AUDIO_DEVICE_OUT_SPEAKER) { |
| 3122 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3123 | } |
| 3124 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3125 | audio_devices_t deviceWithoutSpeaker |
| 3126 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 3127 | // check if any other device (except speaker) is on |
| 3128 | if (type & deviceWithoutSpeaker) { |
| 3129 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 3130 | } |
| 3131 | |
| 3132 | if (params != 0) { |
| 3133 | addBatteryData(params); |
| 3134 | } |
| 3135 | } |
| 3136 | #endif |
| 3137 | |
| 3138 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 3139 | mEffectChains[i]->setDevice_l(type); |
| 3140 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3141 | |
| 3142 | // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when |
| 3143 | // the thread is created so that the first patch creation triggers an ioConfigChanged callback |
| 3144 | bool configChanged = mPrevOutDevice != type; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3145 | mOutDevice = type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 3146 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3147 | |
| 3148 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3149 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3150 | status = hwDevice->create_audio_patch(hwDevice, |
| 3151 | patch->num_sources, |
| 3152 | patch->sources, |
| 3153 | patch->num_sinks, |
| 3154 | patch->sinks, |
| 3155 | handle); |
| 3156 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3157 | char *address; |
| 3158 | if (strcmp(patch->sinks[0].ext.device.address, "") != 0) { |
| 3159 | //FIXME: we only support address on first sink with HAL version < 3.0 |
| 3160 | address = audio_device_address_to_parameter( |
| 3161 | patch->sinks[0].ext.device.type, |
| 3162 | patch->sinks[0].ext.device.address); |
| 3163 | } else { |
| 3164 | address = (char *)calloc(1, 1); |
| 3165 | } |
| 3166 | AudioParameter param = AudioParameter(String8(address)); |
| 3167 | free(address); |
| 3168 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), (int)type); |
| 3169 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3170 | param.toString().string()); |
| 3171 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3172 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3173 | if (configChanged) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 3174 | mPrevOutDevice = type; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 3175 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
| 3176 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3177 | return status; |
| 3178 | } |
| 3179 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3180 | status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3181 | { |
| 3182 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3183 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
| 3184 | if (mFastMixer != 0) { |
| 3185 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3186 | FastMixerState *state = sq->begin(); |
| 3187 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3188 | previousCommand = state->mCommand; |
| 3189 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3190 | sq->end(); |
| 3191 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3192 | } else { |
| 3193 | sq->end(false /*didModify*/); |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | status_t status = PlaybackThread::releaseAudioPatch_l(handle); |
| 3198 | |
| 3199 | if (!(previousCommand & FastMixerState::IDLE)) { |
| 3200 | ALOG_ASSERT(mFastMixer != 0); |
| 3201 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3202 | FastMixerState *state = sq->begin(); |
| 3203 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 3204 | state->mCommand = previousCommand; |
| 3205 | sq->end(); |
| 3206 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3207 | } |
| 3208 | |
| 3209 | return status; |
| 3210 | } |
| 3211 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3212 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 3213 | { |
| 3214 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3215 | |
| 3216 | mOutDevice = AUDIO_DEVICE_NONE; |
| 3217 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3218 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 3219 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 3220 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 3221 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 3222 | AudioParameter param; |
| 3223 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 3224 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 3225 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3226 | } |
| 3227 | return status; |
| 3228 | } |
| 3229 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3230 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 3231 | { |
| 3232 | Mutex::Autolock _l(mLock); |
| 3233 | mTracks.add(track); |
| 3234 | } |
| 3235 | |
| 3236 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 3237 | { |
| 3238 | Mutex::Autolock _l(mLock); |
| 3239 | destroyTrack_l(track); |
| 3240 | } |
| 3241 | |
| 3242 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 3243 | { |
| 3244 | ThreadBase::getAudioPortConfig(config); |
| 3245 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 3246 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 3247 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 3248 | } |
| 3249 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3250 | // ---------------------------------------------------------------------------- |
| 3251 | |
| 3252 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3253 | audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type) |
| 3254 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3255 | // mAudioMixer below |
| 3256 | // mFastMixer below |
| 3257 | mFastMixerFutex(0) |
| 3258 | // mOutputSink below |
| 3259 | // mPipeSink below |
| 3260 | // mNormalSink below |
| 3261 | { |
| 3262 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 3263 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3264 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 3265 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 3266 | mNormalFrameCount); |
| 3267 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 3268 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 3269 | if (type == DUPLICATING) { |
| 3270 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3271 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3272 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3273 | return; |
| 3274 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3275 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3276 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3277 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3278 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3279 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 3280 | ALOG_ASSERT(index == 0); |
| 3281 | |
| 3282 | // initialize fast mixer depending on configuration |
| 3283 | bool initFastMixer; |
| 3284 | switch (kUseFastMixer) { |
| 3285 | case FastMixer_Never: |
| 3286 | initFastMixer = false; |
| 3287 | break; |
| 3288 | case FastMixer_Always: |
| 3289 | initFastMixer = true; |
| 3290 | break; |
| 3291 | case FastMixer_Static: |
| 3292 | case FastMixer_Dynamic: |
| 3293 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3294 | break; |
| 3295 | } |
| 3296 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3297 | audio_format_t fastMixerFormat; |
| 3298 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3299 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3300 | } else { |
| 3301 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3302 | } |
| 3303 | if (mFormat != fastMixerFormat) { |
| 3304 | // change our Sink format to accept our intermediate precision |
| 3305 | mFormat = fastMixerFormat; |
| 3306 | free(mSinkBuffer); |
| 3307 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3308 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3309 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3310 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3311 | |
| 3312 | // create a MonoPipe to connect our submix to FastMixer |
| 3313 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3314 | NBAIO_Format origformat = format; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3315 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3316 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3317 | format.mFormat = fastMixerFormat; |
| 3318 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3319 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3320 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3321 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3322 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3323 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3324 | const NBAIO_Format offers[1] = {format}; |
| 3325 | size_t numCounterOffers = 0; |
| 3326 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 3327 | ALOG_ASSERT(index == 0); |
| 3328 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3329 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3330 | mPipeSink = monoPipe; |
| 3331 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3332 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3333 | if (mTeeSinkOutputEnabled) { |
| 3334 | // 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] | 3335 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3336 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3337 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3338 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3339 | ALOG_ASSERT(index == 0); |
| 3340 | mTeeSink = teeSink; |
| 3341 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3342 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3343 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3344 | ALOG_ASSERT(index == 0); |
| 3345 | mTeeSource = teeSource; |
| 3346 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3347 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3348 | |
| 3349 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3350 | mFastMixer = new FastMixer(); |
| 3351 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3352 | #ifdef STATE_QUEUE_DUMP |
| 3353 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3354 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3355 | #endif |
| 3356 | FastMixerState *state = sq->begin(); |
| 3357 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3358 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3359 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3360 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3361 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3362 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3363 | fastTrack->mGeneration++; |
| 3364 | state->mFastTracksGen++; |
| 3365 | state->mTrackMask = 1; |
| 3366 | // fast mixer will use the HAL output sink |
| 3367 | state->mOutputSink = mOutputSink.get(); |
| 3368 | state->mOutputSinkGen++; |
| 3369 | state->mFrameCount = mFrameCount; |
| 3370 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3371 | // already done in constructor initialization list |
| 3372 | //mFastMixerFutex = 0; |
| 3373 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3374 | state->mColdGen++; |
| 3375 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3376 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3377 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3378 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3379 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3380 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3381 | sq->end(); |
| 3382 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3383 | |
| 3384 | // start the fast mixer |
| 3385 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3386 | pid_t tid = mFastMixer->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3387 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3388 | |
| 3389 | #ifdef AUDIO_WATCHDOG |
| 3390 | // create and start the watchdog |
| 3391 | mAudioWatchdog = new AudioWatchdog(); |
| 3392 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3393 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3394 | tid = mAudioWatchdog->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 3395 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3396 | #endif |
| 3397 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | switch (kUseFastMixer) { |
| 3401 | case FastMixer_Never: |
| 3402 | case FastMixer_Dynamic: |
| 3403 | mNormalSink = mOutputSink; |
| 3404 | break; |
| 3405 | case FastMixer_Always: |
| 3406 | mNormalSink = mPipeSink; |
| 3407 | break; |
| 3408 | case FastMixer_Static: |
| 3409 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3410 | break; |
| 3411 | } |
| 3412 | } |
| 3413 | |
| 3414 | AudioFlinger::MixerThread::~MixerThread() |
| 3415 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3416 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3417 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3418 | FastMixerState *state = sq->begin(); |
| 3419 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3420 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3421 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3422 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3423 | } |
| 3424 | } |
| 3425 | state->mCommand = FastMixerState::EXIT; |
| 3426 | sq->end(); |
| 3427 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3428 | mFastMixer->join(); |
| 3429 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3430 | // We'll use that extract the final state which contains one remaining fast track |
| 3431 | // corresponding to our sub-mix. |
| 3432 | state = sq->begin(); |
| 3433 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3434 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3435 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3436 | delete fastTrack->mBufferProvider; |
| 3437 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3438 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3439 | #ifdef AUDIO_WATCHDOG |
| 3440 | if (mAudioWatchdog != 0) { |
| 3441 | mAudioWatchdog->requestExit(); |
| 3442 | mAudioWatchdog->requestExitAndWait(); |
| 3443 | mAudioWatchdog.clear(); |
| 3444 | } |
| 3445 | #endif |
| 3446 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3447 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3448 | delete mAudioMixer; |
| 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3453 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3454 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3455 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3456 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3457 | } |
| 3458 | return latency; |
| 3459 | } |
| 3460 | |
| 3461 | |
| 3462 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3463 | { |
| 3464 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3465 | } |
| 3466 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3467 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3468 | { |
| 3469 | // FIXME we should only do one push per cycle; confirm this is true |
| 3470 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3471 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3472 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3473 | FastMixerState *state = sq->begin(); |
| 3474 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3475 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3476 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3477 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3478 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3479 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3480 | } |
| 3481 | #ifdef AUDIO_WATCHDOG |
| 3482 | if (mAudioWatchdog != 0) { |
| 3483 | mAudioWatchdog->resume(); |
| 3484 | } |
| 3485 | #endif |
| 3486 | } |
| 3487 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3488 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3489 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3490 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3491 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3492 | sq->end(); |
| 3493 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3494 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3495 | mNormalSink = mPipeSink; |
| 3496 | } |
| 3497 | } else { |
| 3498 | sq->end(false /*didModify*/); |
| 3499 | } |
| 3500 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3501 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3505 | { |
| 3506 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3507 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3508 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3509 | FastMixerState *state = sq->begin(); |
| 3510 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3511 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3512 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3513 | state->mColdGen++; |
| 3514 | mFastMixerFutex = 0; |
| 3515 | sq->end(); |
| 3516 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3517 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3518 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3519 | mNormalSink = mOutputSink; |
| 3520 | } |
| 3521 | #ifdef AUDIO_WATCHDOG |
| 3522 | if (mAudioWatchdog != 0) { |
| 3523 | mAudioWatchdog->pause(); |
| 3524 | } |
| 3525 | #endif |
| 3526 | } else { |
| 3527 | sq->end(false /*didModify*/); |
| 3528 | } |
| 3529 | } |
| 3530 | PlaybackThread::threadLoop_standby(); |
| 3531 | } |
| 3532 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3533 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3534 | { |
| 3535 | return false; |
| 3536 | } |
| 3537 | |
| 3538 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3539 | { |
| 3540 | return !mStandby; |
| 3541 | } |
| 3542 | |
| 3543 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3544 | { |
| 3545 | Mutex::Autolock _l(mLock); |
| 3546 | return waitingAsyncCallback_l(); |
| 3547 | } |
| 3548 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3549 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3550 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3551 | { |
| 3552 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 3553 | mOutput->standby(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3554 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3555 | // discard any pending drain or write ack by incrementing sequence |
| 3556 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3557 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3558 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3559 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3560 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3561 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3562 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3563 | } |
| 3564 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3565 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3566 | { |
| 3567 | ALOGV("signal playback thread"); |
| 3568 | broadcast_l(); |
| 3569 | } |
| 3570 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3571 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3572 | { |
| 3573 | // obtain the presentation timestamp of the next output buffer |
| 3574 | int64_t pts; |
| 3575 | status_t status = INVALID_OPERATION; |
| 3576 | |
| 3577 | if (mNormalSink != 0) { |
| 3578 | status = mNormalSink->getNextWriteTimestamp(&pts); |
| 3579 | } else { |
| 3580 | status = mOutputSink->getNextWriteTimestamp(&pts); |
| 3581 | } |
| 3582 | |
| 3583 | if (status != NO_ERROR) { |
| 3584 | pts = AudioBufferProvider::kInvalidPTS; |
| 3585 | } |
| 3586 | |
| 3587 | // mix buffers... |
| 3588 | mAudioMixer->process(pts); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3589 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3590 | // increase sleep time progressively when application underrun condition clears. |
| 3591 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3592 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3593 | // such that we would underrun the audio HAL. |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3594 | if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3595 | sleepTimeShift--; |
| 3596 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3597 | mSleepTimeUs = 0; |
| 3598 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3599 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3600 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3604 | { |
| 3605 | // If no tracks are ready, sleep once for the duration of an output |
| 3606 | // buffer size, then write 0s to the output |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3607 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3608 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3609 | mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift; |
| 3610 | if (mSleepTimeUs < kMinThreadSleepTimeUs) { |
| 3611 | mSleepTimeUs = kMinThreadSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3612 | } |
| 3613 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3614 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3615 | // duration we would end up writing less data than needed by the audio HAL if |
| 3616 | // the condition persists. |
| 3617 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3618 | sleepTimeShift++; |
| 3619 | } |
| 3620 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3621 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3622 | } |
| 3623 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3624 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3625 | // before effects processing or output. |
| 3626 | if (mMixerBufferValid) { |
| 3627 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3628 | } else { |
| 3629 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3630 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 3631 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3632 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3633 | "anticipated start"); |
| 3634 | } |
| 3635 | // TODO add standby time extension fct of effect tail |
| 3636 | } |
| 3637 | |
| 3638 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3639 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3640 | Vector< sp<Track> > *tracksToRemove) |
| 3641 | { |
| 3642 | |
| 3643 | mixer_state mixerStatus = MIXER_IDLE; |
| 3644 | // find out which tracks need to be processed |
| 3645 | size_t count = mActiveTracks.size(); |
| 3646 | size_t mixedTracks = 0; |
| 3647 | size_t tracksWithEffect = 0; |
| 3648 | // counts only _active_ fast tracks |
| 3649 | size_t fastTracks = 0; |
| 3650 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3651 | |
| 3652 | float masterVolume = mMasterVolume; |
| 3653 | bool masterMute = mMasterMute; |
| 3654 | |
| 3655 | if (masterMute) { |
| 3656 | masterVolume = 0; |
| 3657 | } |
| 3658 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3659 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3660 | if (chain != 0) { |
| 3661 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3662 | chain->setVolume_l(&v, &v); |
| 3663 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3664 | chain.clear(); |
| 3665 | } |
| 3666 | |
| 3667 | // prepare a new state to push |
| 3668 | FastMixerStateQueue *sq = NULL; |
| 3669 | FastMixerState *state = NULL; |
| 3670 | bool didModify = false; |
| 3671 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3672 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3673 | sq = mFastMixer->sq(); |
| 3674 | state = sq->begin(); |
| 3675 | } |
| 3676 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3677 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3678 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3679 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3680 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3681 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3682 | if (t == 0) { |
| 3683 | continue; |
| 3684 | } |
| 3685 | |
| 3686 | // this const just means the local variable doesn't change |
| 3687 | Track* const track = t.get(); |
| 3688 | |
| 3689 | // process fast tracks |
| 3690 | if (track->isFastTrack()) { |
| 3691 | |
| 3692 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3693 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3694 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3695 | // The converse, of removing an (active) track and then creating a new track |
| 3696 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3697 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3698 | int j = track->mFastIndex; |
| 3699 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3700 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3701 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3702 | |
| 3703 | // Determine whether the track is currently in underrun condition, |
| 3704 | // and whether it had a recent underrun. |
| 3705 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3706 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3707 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3708 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3709 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3710 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3711 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3712 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3713 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3714 | track->mObservedUnderruns = underruns; |
| 3715 | // don't count underruns that occur while stopping or pausing |
| 3716 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3717 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3718 | recentUnderruns > 0) { |
| 3719 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3720 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | // This is similar to the state machine for normal tracks, |
| 3724 | // with a few modifications for fast tracks. |
| 3725 | bool isActive = true; |
| 3726 | switch (track->mState) { |
| 3727 | case TrackBase::STOPPING_1: |
| 3728 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3729 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3730 | track->mState = TrackBase::STOPPING_2; |
| 3731 | } |
| 3732 | break; |
| 3733 | case TrackBase::PAUSING: |
| 3734 | // ramp down is not yet implemented |
| 3735 | track->setPaused(); |
| 3736 | break; |
| 3737 | case TrackBase::RESUMING: |
| 3738 | // ramp up is not yet implemented |
| 3739 | track->mState = TrackBase::ACTIVE; |
| 3740 | break; |
| 3741 | case TrackBase::ACTIVE: |
| 3742 | if (recentFull > 0 || recentPartial > 0) { |
| 3743 | // track has provided at least some frames recently: reset retry count |
| 3744 | track->mRetryCount = kMaxTrackRetries; |
| 3745 | } |
| 3746 | if (recentUnderruns == 0) { |
| 3747 | // no recent underruns: stay active |
| 3748 | break; |
| 3749 | } |
| 3750 | // there has recently been an underrun of some kind |
| 3751 | if (track->sharedBuffer() == 0) { |
| 3752 | // were any of the recent underruns "empty" (no frames available)? |
| 3753 | if (recentEmpty == 0) { |
| 3754 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3755 | break; |
| 3756 | } |
| 3757 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3758 | if (--(track->mRetryCount) > 0) { |
| 3759 | break; |
| 3760 | } |
| 3761 | // indicate to client process that the track was disabled because of underrun; |
| 3762 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3763 | android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3764 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3765 | isActive = false; |
| 3766 | break; |
| 3767 | } |
| 3768 | // fall through |
| 3769 | case TrackBase::STOPPING_2: |
| 3770 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3771 | case TrackBase::STOPPED: |
| 3772 | case TrackBase::FLUSHED: // flush() while active |
| 3773 | // Check for presentation complete if track is inactive |
| 3774 | // We have consumed all the buffers of this track. |
| 3775 | // This would be incomplete if we auto-paused on underrun |
| 3776 | { |
| 3777 | size_t audioHALFrames = |
| 3778 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3779 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3780 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3781 | // track stays in active list until presentation is complete |
| 3782 | break; |
| 3783 | } |
| 3784 | } |
| 3785 | if (track->isStopping_2()) { |
| 3786 | track->mState = TrackBase::STOPPED; |
| 3787 | } |
| 3788 | if (track->isStopped()) { |
| 3789 | // Can't reset directly, as fast mixer is still polling this track |
| 3790 | // track->reset(); |
| 3791 | // So instead mark this track as needing to be reset after push with ack |
| 3792 | resetMask |= 1 << i; |
| 3793 | } |
| 3794 | isActive = false; |
| 3795 | break; |
| 3796 | case TrackBase::IDLE: |
| 3797 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3798 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | if (isActive) { |
| 3802 | // was it previously inactive? |
| 3803 | if (!(state->mTrackMask & (1 << j))) { |
| 3804 | ExtendedAudioBufferProvider *eabp = track; |
| 3805 | VolumeProvider *vp = track; |
| 3806 | fastTrack->mBufferProvider = eabp; |
| 3807 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3808 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3809 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3810 | fastTrack->mGeneration++; |
| 3811 | state->mTrackMask |= 1 << j; |
| 3812 | didModify = true; |
| 3813 | // no acknowledgement required for newly active tracks |
| 3814 | } |
| 3815 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3816 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3817 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3818 | ++fastTracks; |
| 3819 | } else { |
| 3820 | // was it previously active? |
| 3821 | if (state->mTrackMask & (1 << j)) { |
| 3822 | fastTrack->mBufferProvider = NULL; |
| 3823 | fastTrack->mGeneration++; |
| 3824 | state->mTrackMask &= ~(1 << j); |
| 3825 | didModify = true; |
| 3826 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3827 | // because we're about to decrement the last sp<> on those tracks. |
| 3828 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3829 | } else { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3830 | LOG_ALWAYS_FATAL("fast track %d should have been active", j); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3831 | } |
| 3832 | tracksToRemove->add(track); |
| 3833 | // Avoids a misleading display in dumpsys |
| 3834 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 3835 | } |
| 3836 | continue; |
| 3837 | } |
| 3838 | |
| 3839 | { // local variable scope to avoid goto warning |
| 3840 | |
| 3841 | audio_track_cblk_t* cblk = track->cblk(); |
| 3842 | |
| 3843 | // The first time a track is added we wait |
| 3844 | // for all its buffers to be filled before processing it |
| 3845 | int name = track->name(); |
| 3846 | // make sure that we have enough frames to mix one full buffer. |
| 3847 | // enforce this condition only once to enable draining the buffer in case the client |
| 3848 | // app does not call stop() and relies on underrun to stop: |
| 3849 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 3850 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3851 | size_t desiredFrames; |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3852 | const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3853 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3854 | |
| 3855 | desiredFrames = sourceFramesNeededWithTimestretch( |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 3856 | sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 3857 | // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed. |
| 3858 | // add frames already consumed but not yet released by the resampler |
| 3859 | // because mAudioTrackServerProxy->framesReady() will include these frames |
| 3860 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
| 3861 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3862 | uint32_t minFrames = 1; |
| 3863 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 3864 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3865 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3866 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3867 | |
| 3868 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3869 | if (ATRACE_ENABLED()) { |
| 3870 | // I wish we had formatted trace names |
| 3871 | char traceName[16]; |
| 3872 | strcpy(traceName, "nRdy"); |
| 3873 | int name = track->name(); |
| 3874 | if (AudioMixer::TRACK0 <= name && |
| 3875 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 3876 | name -= AudioMixer::TRACK0; |
| 3877 | traceName[4] = (name / 10) + '0'; |
| 3878 | traceName[5] = (name % 10) + '0'; |
| 3879 | } else { |
| 3880 | traceName[4] = '?'; |
| 3881 | traceName[5] = '?'; |
| 3882 | } |
| 3883 | traceName[6] = '\0'; |
| 3884 | ATRACE_INT(traceName, framesReady); |
| 3885 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3886 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3887 | !track->isPaused() && !track->isTerminated()) |
| 3888 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3889 | 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] | 3890 | |
| 3891 | mixedTracks++; |
| 3892 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3893 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 3894 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3895 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3896 | if (track->mainBuffer() != mSinkBuffer && |
| 3897 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3898 | if (mEffectBufferEnabled) { |
| 3899 | mEffectBufferValid = true; // Later can set directly. |
| 3900 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3901 | chain = getEffectChain_l(track->sessionId()); |
| 3902 | // Delegate volume control to effect in track effect chain if needed |
| 3903 | if (chain != 0) { |
| 3904 | tracksWithEffect++; |
| 3905 | } else { |
| 3906 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 3907 | "session %d", |
| 3908 | name, track->sessionId()); |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | |
| 3913 | int param = AudioMixer::VOLUME; |
| 3914 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3915 | // no ramp for the first volume setting |
| 3916 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3917 | if (track->mState == TrackBase::RESUMING) { |
| 3918 | track->mState = TrackBase::ACTIVE; |
| 3919 | param = AudioMixer::RAMP_VOLUME; |
| 3920 | } |
| 3921 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3922 | // FIXME should not make a decision based on mServer |
| 3923 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3924 | // If the track is stopped before the first frame was mixed, |
| 3925 | // do not apply ramp |
| 3926 | param = AudioMixer::RAMP_VOLUME; |
| 3927 | } |
| 3928 | |
| 3929 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3930 | uint32_t vl, vr; // in U8.24 integer format |
| 3931 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3932 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3933 | vl = vr = 0; |
| 3934 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3935 | if (track->isPausing()) { |
| 3936 | track->setPaused(); |
| 3937 | } |
| 3938 | } else { |
| 3939 | |
| 3940 | // read original volumes with volume control |
| 3941 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3942 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3943 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3944 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3945 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3946 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3947 | // 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] | 3948 | if (vlf > GAIN_FLOAT_UNITY) { |
| 3949 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 3950 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3951 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3952 | if (vrf > GAIN_FLOAT_UNITY) { |
| 3953 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 3954 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3955 | } |
| 3956 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3957 | vlf *= v; |
| 3958 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3959 | // 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] | 3960 | // then derive vl and vr as U8.24 versions for the effect chain |
| 3961 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 3962 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 3963 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 3964 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3965 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3966 | // send level comes from shared memory and so may be corrupt |
| 3967 | if (sendLevel > MAX_GAIN_INT) { |
| 3968 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 3969 | sendLevel = MAX_GAIN_INT; |
| 3970 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3971 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 3972 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3973 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3974 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3975 | // Delegate volume control to effect in track effect chain if needed |
| 3976 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 3977 | // Do not ramp volume if volume is controlled by effect |
| 3978 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 3979 | // Update remaining floating point volume levels |
| 3980 | vlf = (float)vl / (1 << 24); |
| 3981 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3982 | track->mHasVolumeController = true; |
| 3983 | } else { |
| 3984 | // force no volume ramp when volume controller was just disabled or removed |
| 3985 | // from effect chain to avoid volume spike |
| 3986 | if (track->mHasVolumeController) { |
| 3987 | param = AudioMixer::VOLUME; |
| 3988 | } |
| 3989 | track->mHasVolumeController = false; |
| 3990 | } |
| 3991 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3992 | // XXX: these things DON'T need to be done each time |
| 3993 | mAudioMixer->setBufferProvider(name, track); |
| 3994 | mAudioMixer->enable(name); |
| 3995 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3996 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 3997 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 3998 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3999 | mAudioMixer->setParameter( |
| 4000 | name, |
| 4001 | AudioMixer::TRACK, |
| 4002 | AudioMixer::FORMAT, (void *)track->format()); |
| 4003 | mAudioMixer->setParameter( |
| 4004 | name, |
| 4005 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4006 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4007 | mAudioMixer->setParameter( |
| 4008 | name, |
| 4009 | AudioMixer::TRACK, |
| 4010 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4011 | // 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] | 4012 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4013 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 4014 | if (reqSampleRate == 0) { |
| 4015 | reqSampleRate = mSampleRate; |
| 4016 | } else if (reqSampleRate > maxSampleRate) { |
| 4017 | reqSampleRate = maxSampleRate; |
| 4018 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4019 | mAudioMixer->setParameter( |
| 4020 | name, |
| 4021 | AudioMixer::RESAMPLE, |
| 4022 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 4023 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4024 | |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4025 | AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate(); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4026 | mAudioMixer->setParameter( |
| 4027 | name, |
| 4028 | AudioMixer::TIMESTRETCH, |
| 4029 | AudioMixer::PLAYBACK_RATE, |
Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 4030 | &playbackRate); |
Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 4031 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4032 | /* |
| 4033 | * Select the appropriate output buffer for the track. |
| 4034 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4035 | * Tracks with effects go into their own effects chain buffer |
| 4036 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4037 | * |
| 4038 | * Other tracks can use mMixerBuffer for higher precision |
| 4039 | * channel accumulation. If this buffer is enabled |
| 4040 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 4041 | * into it. |
| 4042 | * |
| 4043 | */ |
| 4044 | if (mMixerBufferEnabled |
| 4045 | && (track->mainBuffer() == mSinkBuffer |
| 4046 | || track->mainBuffer() == mMixerBuffer)) { |
| 4047 | mAudioMixer->setParameter( |
| 4048 | name, |
| 4049 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4050 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4051 | mAudioMixer->setParameter( |
| 4052 | name, |
| 4053 | AudioMixer::TRACK, |
| 4054 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 4055 | // TODO: override track->mainBuffer()? |
| 4056 | mMixerBufferValid = true; |
| 4057 | } else { |
| 4058 | mAudioMixer->setParameter( |
| 4059 | name, |
| 4060 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 4061 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4062 | mAudioMixer->setParameter( |
| 4063 | name, |
| 4064 | AudioMixer::TRACK, |
| 4065 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 4066 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4067 | mAudioMixer->setParameter( |
| 4068 | name, |
| 4069 | AudioMixer::TRACK, |
| 4070 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 4071 | |
| 4072 | // reset retry count |
| 4073 | track->mRetryCount = kMaxTrackRetries; |
| 4074 | |
| 4075 | // If one track is ready, set the mixer ready if: |
| 4076 | // - the mixer was not ready during previous round OR |
| 4077 | // - no other track is not ready |
| 4078 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 4079 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 4080 | mixerStatus = MIXER_TRACKS_READY; |
| 4081 | } |
| 4082 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4083 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 4084 | ALOGV("track(%p) underrun, framesReady(%zu) < framesDesired(%zd)", |
| 4085 | track, framesReady, desiredFrames); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 4086 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 4087 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4088 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 4089 | // previous audio buffer again to effects |
| 4090 | chain = getEffectChain_l(track->sessionId()); |
| 4091 | if (chain != 0) { |
| 4092 | chain->clearInputBuffer(); |
| 4093 | } |
| 4094 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4095 | 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] | 4096 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 4097 | track->isStopped() || track->isPaused()) { |
| 4098 | // We have consumed all the buffers of this track. |
| 4099 | // Remove it from the list of active tracks. |
| 4100 | // TODO: use actual buffer filling status instead of latency when available from |
| 4101 | // audio HAL |
| 4102 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4103 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 4104 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 4105 | if (track->isStopped()) { |
| 4106 | track->reset(); |
| 4107 | } |
| 4108 | tracksToRemove->add(track); |
| 4109 | } |
| 4110 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4111 | // No buffers for this track. Give it a few chances to |
| 4112 | // fill a buffer, then remove it from active list. |
| 4113 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 4114 | 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] | 4115 | tracksToRemove->add(track); |
| 4116 | // indicate to client process that the track was disabled because of underrun; |
| 4117 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 4118 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4119 | // If one track is not ready, mark the mixer also not ready if: |
| 4120 | // - the mixer was ready during previous round OR |
| 4121 | // - no other track is ready |
| 4122 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 4123 | mixerStatus != MIXER_TRACKS_READY) { |
| 4124 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4125 | } |
| 4126 | } |
| 4127 | mAudioMixer->disable(name); |
| 4128 | } |
| 4129 | |
| 4130 | } // local variable scope to avoid goto warning |
| 4131 | track_is_ready: ; |
| 4132 | |
| 4133 | } |
| 4134 | |
| 4135 | // Push the new FastMixer state if necessary |
| 4136 | bool pauseAudioWatchdog = false; |
| 4137 | if (didModify) { |
| 4138 | state->mFastTracksGen++; |
| 4139 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 4140 | if (kUseFastMixer == FastMixer_Dynamic && |
| 4141 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 4142 | state->mCommand = FastMixerState::COLD_IDLE; |
| 4143 | state->mColdFutexAddr = &mFastMixerFutex; |
| 4144 | state->mColdGen++; |
| 4145 | mFastMixerFutex = 0; |
| 4146 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 4147 | mNormalSink = mOutputSink; |
| 4148 | } |
| 4149 | // If we go into cold idle, need to wait for acknowledgement |
| 4150 | // so that fast mixer stops doing I/O. |
| 4151 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 4152 | pauseAudioWatchdog = true; |
| 4153 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4154 | } |
| 4155 | if (sq != NULL) { |
| 4156 | sq->end(didModify); |
| 4157 | sq->push(block); |
| 4158 | } |
| 4159 | #ifdef AUDIO_WATCHDOG |
| 4160 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 4161 | mAudioWatchdog->pause(); |
| 4162 | } |
| 4163 | #endif |
| 4164 | |
| 4165 | // Now perform the deferred reset on fast tracks that have stopped |
| 4166 | while (resetMask != 0) { |
| 4167 | size_t i = __builtin_ctz(resetMask); |
| 4168 | ALOG_ASSERT(i < count); |
| 4169 | resetMask &= ~(1 << i); |
| 4170 | sp<Track> t = mActiveTracks[i].promote(); |
| 4171 | if (t == 0) { |
| 4172 | continue; |
| 4173 | } |
| 4174 | Track* track = t.get(); |
| 4175 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 4176 | track->reset(); |
| 4177 | } |
| 4178 | |
| 4179 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4180 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4181 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4182 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 4183 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 4187 | // as long as there are effects we should clear the effects buffer, to avoid |
| 4188 | // passing a non-clean buffer to the effect chain |
| 4189 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 4190 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4191 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 4192 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 4193 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4194 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4195 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4196 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 4197 | if (mMixerBufferValid) { |
| 4198 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4199 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 4200 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 4201 | // after mixing. |
| 4202 | // |
| 4203 | // To enforce this guarantee: |
| 4204 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 4205 | // (mixedTracks == 0 && fastTracks > 0)) |
| 4206 | // must imply MIXER_TRACKS_READY. |
| 4207 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 4208 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 4209 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 4210 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | // if any fast tracks, then status is ready |
| 4214 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 4215 | if (fastTracks > 0) { |
| 4216 | mixerStatus = MIXER_TRACKS_READY; |
| 4217 | } |
| 4218 | return mixerStatus; |
| 4219 | } |
| 4220 | |
| 4221 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4222 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
| 4223 | audio_format_t format, int sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4224 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4225 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 4229 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 4230 | { |
| 4231 | ALOGV("remove track (%d) and delete from mixer", name); |
| 4232 | mAudioMixer->deleteTrackName(name); |
| 4233 | } |
| 4234 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4235 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4236 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4237 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4238 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4239 | bool reconfig = false; |
| 4240 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4241 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4242 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4243 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 4244 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4245 | if (mFastMixer != 0) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4246 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4247 | FastMixerState *state = sq->begin(); |
| 4248 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 4249 | previousCommand = state->mCommand; |
| 4250 | state->mCommand = FastMixerState::HOT_IDLE; |
| 4251 | sq->end(); |
| 4252 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 4253 | } else { |
| 4254 | sq->end(false /*didModify*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4255 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4256 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4257 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4258 | AudioParameter param = AudioParameter(keyValuePair); |
| 4259 | int value; |
| 4260 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 4261 | reconfig = true; |
| 4262 | } |
| 4263 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4264 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4265 | status = BAD_VALUE; |
| 4266 | } else { |
| 4267 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4268 | reconfig = true; |
| 4269 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4270 | } |
| 4271 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4272 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4273 | status = BAD_VALUE; |
| 4274 | } else { |
| 4275 | // no need to save value, since it's constant |
| 4276 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4277 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4278 | } |
| 4279 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4280 | // do not accept frame count changes if tracks are open as the track buffer |
| 4281 | // size depends on frame count and correct behavior would not be guaranteed |
| 4282 | // if frame count is changed after track creation |
| 4283 | if (!mTracks.isEmpty()) { |
| 4284 | status = INVALID_OPERATION; |
| 4285 | } else { |
| 4286 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4287 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4288 | } |
| 4289 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4290 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4291 | // when changing the audio output device, call addBatteryData to notify |
| 4292 | // the change |
| 4293 | if (mOutDevice != value) { |
| 4294 | uint32_t params = 0; |
| 4295 | // check whether speaker is on |
| 4296 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4297 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4298 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4299 | |
| 4300 | audio_devices_t deviceWithoutSpeaker |
| 4301 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4302 | // check if any other device (except speaker) is on |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 4303 | if (value & deviceWithoutSpeaker) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4304 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4305 | } |
| 4306 | |
| 4307 | if (params != 0) { |
| 4308 | addBatteryData(params); |
| 4309 | } |
| 4310 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4311 | #endif |
| 4312 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4313 | // forward device change to effects that have requested to be |
| 4314 | // aware of attached audio device. |
| 4315 | if (value != AUDIO_DEVICE_NONE) { |
| 4316 | mOutDevice = value; |
| 4317 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4318 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4319 | } |
| 4320 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4321 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4322 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4323 | if (status == NO_ERROR) { |
| 4324 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4325 | keyValuePair.string()); |
| 4326 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4327 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4328 | mStandby = true; |
| 4329 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4330 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4331 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4332 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4333 | if (status == NO_ERROR && reconfig) { |
| 4334 | readOutputParameters_l(); |
| 4335 | delete mAudioMixer; |
| 4336 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4337 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4338 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 4339 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4340 | if (name < 0) { |
| 4341 | break; |
| 4342 | } |
| 4343 | mTracks[i]->mName = name; |
| 4344 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4345 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4346 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | if (!(previousCommand & FastMixerState::IDLE)) { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4350 | ALOG_ASSERT(mFastMixer != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4351 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4352 | FastMixerState *state = sq->begin(); |
| 4353 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 4354 | state->mCommand = previousCommand; |
| 4355 | sq->end(); |
| 4356 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4357 | } |
| 4358 | |
| 4359 | return reconfig; |
| 4360 | } |
| 4361 | |
| 4362 | |
| 4363 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4364 | { |
| 4365 | const size_t SIZE = 256; |
| 4366 | char buffer[SIZE]; |
| 4367 | String8 result; |
| 4368 | |
| 4369 | PlaybackThread::dumpInternals(fd, args); |
Andy Hung | 40eb1a1 | 2015-06-18 13:42:02 -0700 | [diff] [blame] | 4370 | dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4371 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4372 | |
| 4373 | // Make a non-atomic copy of fast mixer dump state so it won't change underneath us |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 4374 | const FastMixerDumpState copy(mFastMixerDumpState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4375 | copy.dump(fd); |
| 4376 | |
| 4377 | #ifdef STATE_QUEUE_DUMP |
| 4378 | // Similar for state queue |
| 4379 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4380 | observerCopy.dump(fd); |
| 4381 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4382 | mutatorCopy.dump(fd); |
| 4383 | #endif |
| 4384 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4385 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4386 | // Write the tee output to a .wav file |
| 4387 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4388 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4389 | |
| 4390 | #ifdef AUDIO_WATCHDOG |
| 4391 | if (mAudioWatchdog != 0) { |
| 4392 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4393 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4394 | wdCopy.dump(fd); |
| 4395 | } |
| 4396 | #endif |
| 4397 | } |
| 4398 | |
| 4399 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4400 | { |
| 4401 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4402 | } |
| 4403 | |
| 4404 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4405 | { |
| 4406 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4407 | } |
| 4408 | |
| 4409 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4410 | { |
| 4411 | PlaybackThread::cacheParameters_l(); |
| 4412 | |
| 4413 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4414 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4415 | // increase threshold again due to low power audio mode. The way this warning |
| 4416 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4417 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4418 | } |
| 4419 | |
| 4420 | // ---------------------------------------------------------------------------- |
| 4421 | |
| 4422 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4423 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device, bool systemReady) |
| 4424 | : PlaybackThread(audioFlinger, output, id, device, DIRECT, systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4425 | // mLeftVolFloat, mRightVolFloat |
| 4426 | { |
| 4427 | } |
| 4428 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4429 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4430 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4431 | ThreadBase::type_t type, bool systemReady) |
| 4432 | : PlaybackThread(audioFlinger, output, id, device, type, systemReady) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4433 | // mLeftVolFloat, mRightVolFloat |
| 4434 | { |
| 4435 | } |
| 4436 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4437 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4438 | { |
| 4439 | } |
| 4440 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4441 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4442 | { |
| 4443 | audio_track_cblk_t* cblk = track->cblk(); |
| 4444 | float left, right; |
| 4445 | |
| 4446 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4447 | left = right = 0; |
| 4448 | } else { |
| 4449 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4450 | float v = mMasterVolume * typeVolume; |
| 4451 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4452 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4453 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4454 | if (left > GAIN_FLOAT_UNITY) { |
| 4455 | left = GAIN_FLOAT_UNITY; |
| 4456 | } |
| 4457 | left *= v; |
| 4458 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4459 | if (right > GAIN_FLOAT_UNITY) { |
| 4460 | right = GAIN_FLOAT_UNITY; |
| 4461 | } |
| 4462 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | if (lastTrack) { |
| 4466 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4467 | mLeftVolFloat = left; |
| 4468 | mRightVolFloat = right; |
| 4469 | |
| 4470 | // Convert volumes from float to 8.24 |
| 4471 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4472 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4473 | |
| 4474 | // Delegate volume control to effect in track effect chain if needed |
| 4475 | // only one effect chain can be present on DirectOutputThread, so if |
| 4476 | // there is one, the track is connected to it |
| 4477 | if (!mEffectChains.isEmpty()) { |
| 4478 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4479 | left = (float)vl / (1 << 24); |
| 4480 | right = (float)vr / (1 << 24); |
| 4481 | } |
| 4482 | if (mOutput->stream->set_volume) { |
| 4483 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4484 | } |
| 4485 | } |
| 4486 | } |
| 4487 | } |
| 4488 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4489 | void AudioFlinger::DirectOutputThread::onAddNewTrack_l() |
| 4490 | { |
| 4491 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4492 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4493 | |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4494 | if (previousTrack != 0 && latestTrack != 0) { |
| 4495 | if (mType == DIRECT) { |
| 4496 | if (previousTrack.get() != latestTrack.get()) { |
| 4497 | mFlushPending = true; |
| 4498 | } |
| 4499 | } else /* mType == OFFLOAD */ { |
| 4500 | if (previousTrack->sessionId() != latestTrack->sessionId()) { |
| 4501 | mFlushPending = true; |
| 4502 | } |
| 4503 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4504 | } |
| 4505 | PlaybackThread::onAddNewTrack_l(); |
| 4506 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4507 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4508 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4509 | Vector< sp<Track> > *tracksToRemove |
| 4510 | ) |
| 4511 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4512 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4513 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4514 | bool doHwPause = false; |
| 4515 | bool doHwResume = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4516 | |
| 4517 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4518 | for (size_t i = 0; i < count; i++) { |
| 4519 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4520 | // The track died recently |
| 4521 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4522 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4525 | if (t->isInvalid()) { |
| 4526 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4527 | tracksToRemove->add(t); |
| 4528 | continue; |
| 4529 | } |
| 4530 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4531 | Track* const track = t.get(); |
| 4532 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4533 | // Only consider last track started for volume and mixer state control. |
| 4534 | // In theory an older track could underrun and restart after the new one starts |
| 4535 | // but as we only care about the transition phase between two tracks on a |
| 4536 | // direct output, it is not a problem to ignore the underrun case. |
| 4537 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4538 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4539 | |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4540 | if (track->isPausing()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4541 | track->setPaused(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4542 | if (mHwSupportsPause && last && !mHwPaused) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4543 | doHwPause = true; |
| 4544 | mHwPaused = true; |
| 4545 | } |
| 4546 | tracksToRemove->add(track); |
| 4547 | } else if (track->isFlushPending()) { |
| 4548 | track->flushAck(); |
| 4549 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4550 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4551 | } |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4552 | } else if (track->isResumePending()) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4553 | track->resumeAck(); |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 4554 | if (last && mHwPaused) { |
| 4555 | doHwResume = true; |
| 4556 | mHwPaused = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4560 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4561 | // for all its buffers to be filled before processing it. |
| 4562 | // Allow draining the buffer in case the client |
| 4563 | // app does not call stop() and relies on underrun to stop: |
| 4564 | // hence the test on (track->mRetryCount > 1). |
| 4565 | // If retryCount<=1 then track is about to underrun and be removed. |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4566 | // Do not use a high threshold for compressed audio. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4567 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4568 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4569 | && (track->mRetryCount > 1) && audio_is_linear_pcm(mFormat)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4570 | minFrames = mNormalFrameCount; |
| 4571 | } else { |
| 4572 | minFrames = 1; |
| 4573 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4574 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4575 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4576 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4577 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4578 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4579 | |
| 4580 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4581 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4582 | // make sure processVolume_l() will apply new volume even if 0 |
| 4583 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4584 | if (!mHwSupportsPause) { |
| 4585 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | |
| 4589 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4590 | processVolume_l(track, last); |
| 4591 | if (last) { |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4592 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4593 | if (previousTrack != 0) { |
| 4594 | if (track != previousTrack.get()) { |
| 4595 | // Flush any data still being written from last track |
| 4596 | mBytesRemaining = 0; |
Eric Laurent | 0f0631e | 2015-07-06 18:01:25 -0700 | [diff] [blame] | 4597 | // Invalidate previous track to force a seek when resuming. |
| 4598 | previousTrack->invalidate(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | mPreviousTrack = track; |
| 4602 | |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4603 | // reset retry count |
| 4604 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4605 | mActiveTrack = t; |
| 4606 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4607 | if (mHwPaused) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4608 | doHwResume = true; |
| 4609 | mHwPaused = false; |
| 4610 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4611 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4612 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4613 | // clear effect chain input buffer if the last active track started underruns |
| 4614 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4615 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4616 | mEffectChains[0]->clearInputBuffer(); |
| 4617 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4618 | if (track->isStopping_1()) { |
| 4619 | track->mState = TrackBase::STOPPING_2; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4620 | if (last && mHwPaused) { |
| 4621 | doHwResume = true; |
| 4622 | mHwPaused = false; |
| 4623 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4624 | } |
| 4625 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4626 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4627 | // We have consumed all the buffers of this track. |
| 4628 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4629 | size_t audioHALFrames; |
| 4630 | if (audio_is_linear_pcm(mFormat)) { |
| 4631 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4632 | } else { |
| 4633 | audioHALFrames = 0; |
| 4634 | } |
| 4635 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4636 | size_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4637 | if (mStandby || !last || |
| 4638 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4639 | if (track->isStopping_2()) { |
| 4640 | track->mState = TrackBase::STOPPED; |
| 4641 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4642 | if (track->isStopped()) { |
| 4643 | track->reset(); |
| 4644 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4645 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4646 | } |
| 4647 | } else { |
| 4648 | // No buffers for this track. Give it a few chances to |
| 4649 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4650 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4651 | if (--(track->mRetryCount) <= 0) { |
| 4652 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4653 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4654 | // indicate to client process that the track was disabled because of underrun; |
| 4655 | // it will then automatically call start() when data is available |
| 4656 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4657 | } else if (last) { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 4658 | ALOGW("pause because of UNDERRUN, framesReady = %zu," |
| 4659 | "minFrames = %u, mFormat = %#x", |
| 4660 | track->framesReady(), minFrames, mFormat); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4661 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4662 | if (mHwSupportsPause && !mHwPaused && !mStandby) { |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4663 | doHwPause = true; |
| 4664 | mHwPaused = true; |
| 4665 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4666 | } |
| 4667 | } |
| 4668 | } |
| 4669 | } |
| 4670 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4671 | // 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] | 4672 | if (!mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4673 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4674 | if (mTracks[i]->isFlushPending()) { |
| 4675 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4676 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4682 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4683 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4684 | // if resume is received before pause is executed. |
| 4685 | if (mHwSupportsPause && !mStandby && |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4686 | (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4687 | mOutput->stream->pause(mOutput->stream); |
| 4688 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4689 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4690 | flushHw_l(); |
| 4691 | } |
| 4692 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 4693 | mOutput->stream->resume(mOutput->stream); |
| 4694 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4695 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4696 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4697 | |
| 4698 | return mixerStatus; |
| 4699 | } |
| 4700 | |
| 4701 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4702 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4703 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4704 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4705 | // output audio to hardware |
| 4706 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4707 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4708 | buffer.frameCount = frameCount; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4709 | status_t status = mActiveTrack->getNextBuffer(&buffer); |
| 4710 | if (status != NO_ERROR || buffer.raw == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4711 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4712 | break; |
| 4713 | } |
| 4714 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4715 | frameCount -= buffer.frameCount; |
| 4716 | curBuf += buffer.frameCount * mFrameSize; |
| 4717 | mActiveTrack->releaseBuffer(&buffer); |
| 4718 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4719 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4720 | mSleepTimeUs = 0; |
| 4721 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4722 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4726 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4727 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4728 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4729 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4730 | return; |
| 4731 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4732 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4733 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4734 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4735 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4736 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4737 | } |
| 4738 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4739 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4740 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4741 | } |
| 4742 | } |
| 4743 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4744 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 4745 | { |
| 4746 | { |
| 4747 | Mutex::Autolock _l(mLock); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4748 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4749 | if (mTracks[i]->isFlushPending()) { |
| 4750 | mTracks[i]->flushAck(); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4751 | mFlushPending = true; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4752 | } |
| 4753 | } |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4754 | if (mFlushPending) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4755 | flushHw_l(); |
| 4756 | } |
| 4757 | } |
| 4758 | PlaybackThread::threadLoop_exit(); |
| 4759 | } |
| 4760 | |
| 4761 | // must be called with thread mutex locked |
| 4762 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 4763 | { |
| 4764 | bool trackPaused = false; |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4765 | bool trackStopped = false; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4766 | |
| 4767 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4768 | // after a timeout and we will enter standby then. |
| 4769 | if (mTracks.size() > 0) { |
| 4770 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4771 | trackStopped = mTracks[mTracks.size() - 1]->isStopped() || |
| 4772 | mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4773 | } |
| 4774 | |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4775 | return !mStandby && !(trackPaused || (mHwPaused && !trackStopped)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4776 | } |
| 4777 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4778 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4779 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4780 | audio_format_t format __unused, int sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4781 | { |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
| 4785 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4786 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4787 | { |
| 4788 | } |
| 4789 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4790 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4791 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4792 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4793 | { |
| 4794 | bool reconfig = false; |
| 4795 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4796 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4797 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4798 | AudioParameter param = AudioParameter(keyValuePair); |
| 4799 | int value; |
| 4800 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4801 | // forward device change to effects that have requested to be |
| 4802 | // aware of attached audio device. |
| 4803 | if (value != AUDIO_DEVICE_NONE) { |
| 4804 | mOutDevice = value; |
| 4805 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4806 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4807 | } |
| 4808 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4809 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4810 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4811 | // do not accept frame count changes if tracks are open as the track buffer |
| 4812 | // size depends on frame count and correct behavior would not be garantied |
| 4813 | // if frame count is changed after track creation |
| 4814 | if (!mTracks.isEmpty()) { |
| 4815 | status = INVALID_OPERATION; |
| 4816 | } else { |
| 4817 | reconfig = true; |
| 4818 | } |
| 4819 | } |
| 4820 | if (status == NO_ERROR) { |
| 4821 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4822 | keyValuePair.string()); |
| 4823 | if (!mStandby && status == INVALID_OPERATION) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4824 | mOutput->standby(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4825 | mStandby = true; |
| 4826 | mBytesWritten = 0; |
| 4827 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4828 | keyValuePair.string()); |
| 4829 | } |
| 4830 | if (status == NO_ERROR && reconfig) { |
| 4831 | readOutputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 4832 | sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4833 | } |
| 4834 | } |
| 4835 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4836 | return reconfig; |
| 4837 | } |
| 4838 | |
| 4839 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 4840 | { |
| 4841 | uint32_t time; |
| 4842 | if (audio_is_linear_pcm(mFormat)) { |
| 4843 | time = PlaybackThread::activeSleepTimeUs(); |
| 4844 | } else { |
| 4845 | time = 10000; |
| 4846 | } |
| 4847 | return time; |
| 4848 | } |
| 4849 | |
| 4850 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 4851 | { |
| 4852 | uint32_t time; |
| 4853 | if (audio_is_linear_pcm(mFormat)) { |
| 4854 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4855 | } else { |
| 4856 | time = 10000; |
| 4857 | } |
| 4858 | return time; |
| 4859 | } |
| 4860 | |
| 4861 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 4862 | { |
| 4863 | uint32_t time; |
| 4864 | if (audio_is_linear_pcm(mFormat)) { |
| 4865 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 4866 | } else { |
| 4867 | time = 10000; |
| 4868 | } |
| 4869 | return time; |
| 4870 | } |
| 4871 | |
| 4872 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 4873 | { |
| 4874 | PlaybackThread::cacheParameters_l(); |
| 4875 | |
| 4876 | // use shorter standby delay as on normal output to release |
| 4877 | // hardware resources as soon as possible |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 4878 | // no delay on outputs with HW A/V sync |
| 4879 | if (usesHwAvSync()) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4880 | mStandbyDelayNs = 0; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4881 | } else if ((mType == OFFLOAD) && !audio_is_linear_pcm(mFormat)) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4882 | mStandbyDelayNs = kOffloadStandbyDelayNs; |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 4883 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 4884 | mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4885 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4886 | } |
| 4887 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4888 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 4889 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 4890 | mOutput->flush(); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4891 | mHwPaused = false; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 4892 | mFlushPending = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4895 | // ---------------------------------------------------------------------------- |
| 4896 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4897 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4898 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4899 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4900 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4901 | mWriteAckSequence(0), |
| 4902 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4903 | { |
| 4904 | } |
| 4905 | |
| 4906 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 4907 | { |
| 4908 | } |
| 4909 | |
| 4910 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 4911 | { |
| 4912 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4913 | } |
| 4914 | |
| 4915 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 4916 | { |
| 4917 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4918 | uint32_t writeAckSequence; |
| 4919 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4920 | |
| 4921 | { |
| 4922 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4923 | while (!((mWriteAckSequence & 1) || |
| 4924 | (mDrainSequence & 1) || |
| 4925 | exitPending())) { |
| 4926 | mWaitWorkCV.wait(mLock); |
| 4927 | } |
| 4928 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4929 | if (exitPending()) { |
| 4930 | break; |
| 4931 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4932 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 4933 | mWriteAckSequence, mDrainSequence); |
| 4934 | writeAckSequence = mWriteAckSequence; |
| 4935 | mWriteAckSequence &= ~1; |
| 4936 | drainSequence = mDrainSequence; |
| 4937 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4938 | } |
| 4939 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4940 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 4941 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4942 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4943 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4944 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4945 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4946 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | return false; |
| 4952 | } |
| 4953 | |
| 4954 | void AudioFlinger::AsyncCallbackThread::exit() |
| 4955 | { |
| 4956 | ALOGV("AsyncCallbackThread::exit"); |
| 4957 | Mutex::Autolock _l(mLock); |
| 4958 | requestExit(); |
| 4959 | mWaitWorkCV.broadcast(); |
| 4960 | } |
| 4961 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4962 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4963 | { |
| 4964 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4965 | // bit 0 is cleared |
| 4966 | mWriteAckSequence = sequence << 1; |
| 4967 | } |
| 4968 | |
| 4969 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 4970 | { |
| 4971 | Mutex::Autolock _l(mLock); |
| 4972 | // ignore unexpected callbacks |
| 4973 | if (mWriteAckSequence & 2) { |
| 4974 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4975 | mWaitWorkCV.signal(); |
| 4976 | } |
| 4977 | } |
| 4978 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4979 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4980 | { |
| 4981 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4982 | // bit 0 is cleared |
| 4983 | mDrainSequence = sequence << 1; |
| 4984 | } |
| 4985 | |
| 4986 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 4987 | { |
| 4988 | Mutex::Autolock _l(mLock); |
| 4989 | // ignore unexpected callbacks |
| 4990 | if (mDrainSequence & 2) { |
| 4991 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4992 | mWaitWorkCV.signal(); |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | |
| 4997 | // ---------------------------------------------------------------------------- |
| 4998 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 4999 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady) |
| 5000 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady), |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5001 | mPausedBytesRemaining(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5002 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5003 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 5004 | mStandby = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5005 | } |
| 5006 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5007 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 5008 | { |
| 5009 | if (mFlushPending || mHwPaused) { |
| 5010 | // If a flush is pending or track was paused, just discard buffered data |
| 5011 | flushHw_l(); |
| 5012 | } else { |
| 5013 | mMixerStatus = MIXER_DRAIN_ALL; |
| 5014 | threadLoop_drain(); |
| 5015 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 5016 | if (mUseAsyncWrite) { |
| 5017 | ALOG_ASSERT(mCallbackThread != 0); |
| 5018 | mCallbackThread->exit(); |
| 5019 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5020 | PlaybackThread::threadLoop_exit(); |
| 5021 | } |
| 5022 | |
| 5023 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 5024 | Vector< sp<Track> > *tracksToRemove |
| 5025 | ) |
| 5026 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5027 | size_t count = mActiveTracks.size(); |
| 5028 | |
| 5029 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5030 | bool doHwPause = false; |
| 5031 | bool doHwResume = false; |
| 5032 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 5033 | ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); |
| 5034 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5035 | // find out which tracks need to be processed |
| 5036 | for (size_t i = 0; i < count; i++) { |
| 5037 | sp<Track> t = mActiveTracks[i].promote(); |
| 5038 | // The track died recently |
| 5039 | if (t == 0) { |
| 5040 | continue; |
| 5041 | } |
| 5042 | Track* const track = t.get(); |
| 5043 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5044 | // Only consider last track started for volume and mixer state control. |
| 5045 | // In theory an older track could underrun and restart after the new one starts |
| 5046 | // but as we only care about the transition phase between two tracks on a |
| 5047 | // direct output, it is not a problem to ignore the underrun case. |
| 5048 | sp<Track> l = mLatestActiveTrack.promote(); |
| 5049 | bool last = l.get() == track; |
| 5050 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5051 | if (track->isInvalid()) { |
| 5052 | ALOGW("An invalidated track shouldn't be in active list"); |
| 5053 | tracksToRemove->add(track); |
| 5054 | continue; |
| 5055 | } |
| 5056 | |
| 5057 | if (track->mState == TrackBase::IDLE) { |
| 5058 | ALOGW("An idle track shouldn't be in active list"); |
| 5059 | continue; |
| 5060 | } |
| 5061 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5062 | if (track->isPausing()) { |
| 5063 | track->setPaused(); |
| 5064 | if (last) { |
Eric Laurent | 5cff403 | 2015-05-26 13:49:58 -0700 | [diff] [blame] | 5065 | if (mHwSupportsPause && !mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5066 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5067 | mHwPaused = true; |
| 5068 | } |
| 5069 | // If we were part way through writing the mixbuffer to |
| 5070 | // the HAL we must save this until we resume |
| 5071 | // BUG - this will be wrong if a different track is made active, |
| 5072 | // in that case we want to discard the pending data in the |
| 5073 | // mixbuffer and tell the client to present it again when the |
| 5074 | // track is resumed |
| 5075 | mPausedWriteLength = mCurrentWriteLength; |
| 5076 | mPausedBytesRemaining = mBytesRemaining; |
| 5077 | mBytesRemaining = 0; // stop writing |
| 5078 | } |
| 5079 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 5080 | } else if (track->isFlushPending()) { |
| 5081 | track->flushAck(); |
| 5082 | if (last) { |
| 5083 | mFlushPending = true; |
| 5084 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5085 | } else if (track->isResumePending()){ |
| 5086 | track->resumeAck(); |
| 5087 | if (last) { |
| 5088 | if (mPausedBytesRemaining) { |
| 5089 | // Need to continue write that was interrupted |
| 5090 | mCurrentWriteLength = mPausedWriteLength; |
| 5091 | mBytesRemaining = mPausedBytesRemaining; |
| 5092 | mPausedBytesRemaining = 0; |
| 5093 | } |
| 5094 | if (mHwPaused) { |
| 5095 | doHwResume = true; |
| 5096 | mHwPaused = false; |
| 5097 | // threadLoop_mix() will handle the case that we need to |
| 5098 | // resume an interrupted write |
| 5099 | } |
| 5100 | // enable write to audio HAL |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5101 | mSleepTimeUs = 0; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 5102 | |
| 5103 | // Do not handle new data in this iteration even if track->framesReady() |
| 5104 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5105 | } |
| 5106 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5107 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5108 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5109 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 5110 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 5111 | // make sure processVolume_l() will apply new volume even if 0 |
| 5112 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5116 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 5117 | if (previousTrack != 0) { |
| 5118 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5119 | // Flush any data still being written from last track |
| 5120 | mBytesRemaining = 0; |
| 5121 | if (mPausedBytesRemaining) { |
| 5122 | // Last track was paused so we also need to flush saved |
| 5123 | // mixbuffer state and invalidate track so that it will |
| 5124 | // re-submit that unwritten data when it is next resumed |
| 5125 | mPausedBytesRemaining = 0; |
| 5126 | // Invalidate is a bit drastic - would be more efficient |
| 5127 | // to have a flag to tell client that some of the |
| 5128 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5129 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5130 | } |
| 5131 | // flush data already sent to the DSP if changing audio session as audio |
| 5132 | // comes from a different source. Also invalidate previous track to force a |
| 5133 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 5134 | if (previousTrack->sessionId() != track->sessionId()) { |
| 5135 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5140 | // reset retry count |
| 5141 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 5142 | mActiveTrack = t; |
| 5143 | mixerStatus = MIXER_TRACKS_READY; |
| 5144 | } |
| 5145 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 5146 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5147 | if (track->isStopping_1()) { |
| 5148 | // Hardware buffer can hold a large amount of audio so we must |
| 5149 | // wait for all current track's data to drain before we say |
| 5150 | // that the track is stopped. |
| 5151 | if (mBytesRemaining == 0) { |
| 5152 | // Only start draining when all data in mixbuffer |
| 5153 | // has been written |
| 5154 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 5155 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5156 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 5157 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5158 | // do not modify drain sequence if we are already draining. This happens |
| 5159 | // when resuming from pause after drain. |
| 5160 | if ((mDrainSequence & 1) == 0) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5161 | mSleepTimeUs = 0; |
| 5162 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5163 | mixerStatus = MIXER_DRAIN_TRACK; |
| 5164 | mDrainSequence += 2; |
| 5165 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5166 | if (mHwPaused) { |
| 5167 | // It is possible to move from PAUSED to STOPPING_1 without |
| 5168 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 5169 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5170 | mHwPaused = false; |
| 5171 | } |
| 5172 | } |
| 5173 | } |
| 5174 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 5175 | // Drain has completed or we are in standby, signal presentation complete |
| 5176 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5177 | track->mState = TrackBase::STOPPED; |
| 5178 | size_t audioHALFrames = |
| 5179 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 5180 | size_t framesWritten = |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 5181 | mBytesWritten / mOutput->getFrameSize(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5182 | track->presentationComplete(framesWritten, audioHALFrames); |
| 5183 | track->reset(); |
| 5184 | tracksToRemove->add(track); |
| 5185 | } |
| 5186 | } else { |
| 5187 | // No buffers for this track. Give it a few chances to |
| 5188 | // fill a buffer, then remove it from active list. |
| 5189 | if (--(track->mRetryCount) <= 0) { |
| 5190 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 5191 | track->name()); |
| 5192 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 5193 | // indicate to client process that the track was disabled because of underrun; |
| 5194 | // it will then automatically call start() when data is available |
| 5195 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5196 | } else if (last){ |
| 5197 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | // compute volume for this track |
| 5202 | processVolume_l(track, last); |
| 5203 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5204 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 5205 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 5206 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 5207 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 5208 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5209 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5210 | mOutput->stream->pause(mOutput->stream); |
| 5211 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5212 | if (mFlushPending) { |
| 5213 | flushHw_l(); |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5214 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 5215 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 5216 | mOutput->stream->resume(mOutput->stream); |
| 5217 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 5218 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5219 | // remove all the tracks that need to be... |
| 5220 | removeTracks_l(*tracksToRemove); |
| 5221 | |
| 5222 | return mixerStatus; |
| 5223 | } |
| 5224 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5225 | // must be called with thread mutex locked |
| 5226 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 5227 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5228 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 5229 | mWriteAckSequence, mDrainSequence); |
| 5230 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5231 | return true; |
| 5232 | } |
| 5233 | return false; |
| 5234 | } |
| 5235 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5236 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 5237 | { |
| 5238 | Mutex::Autolock _l(mLock); |
| 5239 | return waitingAsyncCallback_l(); |
| 5240 | } |
| 5241 | |
| 5242 | void AudioFlinger::OffloadThread::flushHw_l() |
| 5243 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 5244 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5245 | // Flush anything still waiting in the mixbuffer |
| 5246 | mCurrentWriteLength = 0; |
| 5247 | mBytesRemaining = 0; |
| 5248 | mPausedWriteLength = 0; |
| 5249 | mPausedBytesRemaining = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 5250 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5251 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5252 | // discard any pending drain or write ack by incrementing sequence |
| 5253 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 5254 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5255 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 5256 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 5257 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | // ---------------------------------------------------------------------------- |
| 5262 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5263 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5264 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5265 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5266 | systemReady, DUPLICATING), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5267 | mWaitTimeMs(UINT_MAX) |
| 5268 | { |
| 5269 | addOutputTrack(mainThread); |
| 5270 | } |
| 5271 | |
| 5272 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 5273 | { |
| 5274 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5275 | mOutputTracks[i]->destroy(); |
| 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 5280 | { |
| 5281 | // mix buffers... |
| 5282 | if (outputsReady(outputTracks)) { |
| 5283 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 5284 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 5285 | if (mMixerBufferValid) { |
| 5286 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 5287 | } else { |
| 5288 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 5289 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5290 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5291 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5292 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5293 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5294 | mStandbyTimeNs = systemTime() + mStandbyDelayNs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 5298 | { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5299 | if (mSleepTimeUs == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5300 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5301 | mSleepTimeUs = mActiveSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5302 | } else { |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5303 | mSleepTimeUs = mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5304 | } |
| 5305 | } else if (mBytesWritten != 0) { |
| 5306 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5307 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5308 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5309 | } else { |
| 5310 | // flush remaining overflow buffers in output tracks |
| 5311 | writeFrames = 0; |
| 5312 | } |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 5313 | mSleepTimeUs = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5317 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5318 | { |
| 5319 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5320 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5321 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5322 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5323 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5327 | { |
| 5328 | // DuplicatingThread implements standby by stopping all tracks |
| 5329 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5330 | outputTracks[i]->stop(); |
| 5331 | } |
| 5332 | } |
| 5333 | |
| 5334 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5335 | { |
| 5336 | outputTracks = mOutputTracks; |
| 5337 | } |
| 5338 | |
| 5339 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5340 | { |
| 5341 | outputTracks.clear(); |
| 5342 | } |
| 5343 | |
| 5344 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5345 | { |
| 5346 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5347 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5348 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5349 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5350 | const size_t frameCount = |
| 5351 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5352 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5353 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5354 | // nearly empty and the other may be dropping data). |
| 5355 | |
| 5356 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5357 | this, |
| 5358 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5359 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5360 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5361 | frameCount, |
| 5362 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5363 | if (outputTrack->cblk() != NULL) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5364 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5365 | mOutputTracks.add(outputTrack); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5366 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5367 | updateWaitTime_l(); |
| 5368 | } |
| 5369 | } |
| 5370 | |
| 5371 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5372 | { |
| 5373 | Mutex::Autolock _l(mLock); |
| 5374 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5375 | if (mOutputTracks[i]->thread() == thread) { |
| 5376 | mOutputTracks[i]->destroy(); |
| 5377 | mOutputTracks.removeAt(i); |
| 5378 | updateWaitTime_l(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5379 | if (thread->getOutput() == mOutput) { |
| 5380 | mOutput = NULL; |
| 5381 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5382 | return; |
| 5383 | } |
| 5384 | } |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 5385 | ALOGV("removeOutputTrack(): unknown thread: %p", thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | // caller must hold mLock |
| 5389 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5390 | { |
| 5391 | mWaitTimeMs = UINT_MAX; |
| 5392 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5393 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5394 | if (strong != 0) { |
| 5395 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5396 | if (waitTimeMs < mWaitTimeMs) { |
| 5397 | mWaitTimeMs = waitTimeMs; |
| 5398 | } |
| 5399 | } |
| 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | |
| 5404 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5405 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5406 | { |
| 5407 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5408 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5409 | if (thread == 0) { |
| 5410 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5411 | outputTracks[i].get()); |
| 5412 | return false; |
| 5413 | } |
| 5414 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5415 | // see note at standby() declaration |
| 5416 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5417 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5418 | thread.get()); |
| 5419 | return false; |
| 5420 | } |
| 5421 | } |
| 5422 | return true; |
| 5423 | } |
| 5424 | |
| 5425 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5426 | { |
| 5427 | return (mWaitTimeMs * 1000) / 2; |
| 5428 | } |
| 5429 | |
| 5430 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5431 | { |
| 5432 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5433 | updateWaitTime_l(); |
| 5434 | |
| 5435 | MixerThread::cacheParameters_l(); |
| 5436 | } |
| 5437 | |
| 5438 | // ---------------------------------------------------------------------------- |
| 5439 | // Record |
| 5440 | // ---------------------------------------------------------------------------- |
| 5441 | |
| 5442 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5443 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5444 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5445 | audio_devices_t outDevice, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5446 | audio_devices_t inDevice, |
| 5447 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5448 | #ifdef TEE_SINK |
| 5449 | , const sp<NBAIO_Sink>& teeSink |
| 5450 | #endif |
| 5451 | ) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5452 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5453 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5454 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5455 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5456 | #ifdef TEE_SINK |
| 5457 | , mTeeSink(teeSink) |
| 5458 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5459 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5460 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5461 | // mFastCapture below |
| 5462 | , mFastCaptureFutex(0) |
| 5463 | // mInputSource |
| 5464 | // mPipeSink |
| 5465 | // mPipeSource |
| 5466 | , mPipeFramesP2(0) |
| 5467 | // mPipeMemory |
| 5468 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5469 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5470 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5471 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5472 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5473 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5474 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5475 | |
| 5476 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5477 | mInputSource = new AudioStreamInSource(input->stream); |
| 5478 | size_t numCounterOffers = 0; |
| 5479 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
| 5480 | ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 5481 | ALOG_ASSERT(index == 0); |
| 5482 | |
| 5483 | // initialize fast capture depending on configuration |
| 5484 | bool initFastCapture; |
| 5485 | switch (kUseFastCapture) { |
| 5486 | case FastCapture_Never: |
| 5487 | initFastCapture = false; |
| 5488 | break; |
| 5489 | case FastCapture_Always: |
| 5490 | initFastCapture = true; |
| 5491 | break; |
| 5492 | case FastCapture_Static: |
| 5493 | uint32_t primaryOutputSampleRate; |
| 5494 | { |
| 5495 | AutoMutex _l(audioFlinger->mHardwareLock); |
| 5496 | primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; |
| 5497 | } |
| 5498 | initFastCapture = |
| 5499 | // either capture sample rate is same as (a reasonable) primary output sample rate |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 5500 | ((isMusicRate(primaryOutputSampleRate) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5501 | (mSampleRate == primaryOutputSampleRate)) || |
| 5502 | // or primary output sample rate is unknown, and capture sample rate is reasonable |
| 5503 | ((primaryOutputSampleRate == 0) && |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 5504 | isMusicRate(mSampleRate))) && |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 5505 | // and the buffer size is < 12 ms |
| 5506 | (mFrameCount * 1000) / mSampleRate < 12; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5507 | break; |
| 5508 | // case FastCapture_Dynamic: |
| 5509 | } |
| 5510 | |
| 5511 | if (initFastCapture) { |
Glenn Kasten | d198b85 | 2015-03-16 14:55:53 -0700 | [diff] [blame] | 5512 | // 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] | 5513 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5514 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5515 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5516 | void *pipeBuffer; |
| 5517 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5518 | sp<IMemory> pipeMemory; |
| 5519 | if ((roHeap == 0) || |
| 5520 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5521 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5522 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5523 | goto failed; |
| 5524 | } |
| 5525 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5526 | memset(pipeBuffer, 0, pipeSize); |
| 5527 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5528 | const NBAIO_Format offers[1] = {format}; |
| 5529 | size_t numCounterOffers = 0; |
| 5530 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5531 | ALOG_ASSERT(index == 0); |
| 5532 | mPipeSink = pipe; |
| 5533 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5534 | numCounterOffers = 0; |
| 5535 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5536 | ALOG_ASSERT(index == 0); |
| 5537 | mPipeSource = pipeReader; |
| 5538 | mPipeFramesP2 = pipeFramesP2; |
| 5539 | mPipeMemory = pipeMemory; |
| 5540 | |
| 5541 | // create fast capture |
| 5542 | mFastCapture = new FastCapture(); |
| 5543 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5544 | #ifdef STATE_QUEUE_DUMP |
| 5545 | // FIXME |
| 5546 | #endif |
| 5547 | FastCaptureState *state = sq->begin(); |
| 5548 | state->mCblk = NULL; |
| 5549 | state->mInputSource = mInputSource.get(); |
| 5550 | state->mInputSourceGen++; |
| 5551 | state->mPipeSink = pipe; |
| 5552 | state->mPipeSinkGen++; |
| 5553 | state->mFrameCount = mFrameCount; |
| 5554 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5555 | // already done in constructor initialization list |
| 5556 | //mFastCaptureFutex = 0; |
| 5557 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5558 | state->mColdGen++; |
| 5559 | state->mDumpState = &mFastCaptureDumpState; |
| 5560 | #ifdef TEE_SINK |
| 5561 | // FIXME |
| 5562 | #endif |
| 5563 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 5564 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 5565 | sq->end(); |
| 5566 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5567 | |
| 5568 | // start the fast capture |
| 5569 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5570 | pid_t tid = mFastCapture->getTid(); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 5571 | sendPrioConfigEvent(getpid_cached, tid, kPriorityFastMixer); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5572 | #ifdef AUDIO_WATCHDOG |
| 5573 | // FIXME |
| 5574 | #endif |
| 5575 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5576 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5577 | } |
| 5578 | failed: ; |
| 5579 | |
| 5580 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5581 | } |
| 5582 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5583 | AudioFlinger::RecordThread::~RecordThread() |
| 5584 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5585 | if (mFastCapture != 0) { |
| 5586 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5587 | FastCaptureState *state = sq->begin(); |
| 5588 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5589 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5590 | if (old == -1) { |
| 5591 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5592 | } |
| 5593 | } |
| 5594 | state->mCommand = FastCaptureState::EXIT; |
| 5595 | sq->end(); |
| 5596 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5597 | mFastCapture->join(); |
| 5598 | mFastCapture.clear(); |
| 5599 | } |
| 5600 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5601 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5602 | free(mRsmpInBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | void AudioFlinger::RecordThread::onFirstRef() |
| 5606 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5607 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5608 | } |
| 5609 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5610 | bool AudioFlinger::RecordThread::threadLoop() |
| 5611 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5612 | nsecs_t lastWarning = 0; |
| 5613 | |
| 5614 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5615 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5616 | reacquire_wakelock: |
| 5617 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5618 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5619 | { |
| 5620 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5621 | size_t size = mActiveTracks.size(); |
| 5622 | activeTracksGen = mActiveTracksGen; |
| 5623 | if (size > 0) { |
| 5624 | // FIXME an arbitrary choice |
| 5625 | activeTrack = mActiveTracks[0]; |
| 5626 | acquireWakeLock_l(activeTrack->uid()); |
| 5627 | if (size > 1) { |
| 5628 | SortedVector<int> tmp; |
| 5629 | for (size_t i = 0; i < size; i++) { |
| 5630 | tmp.add(mActiveTracks[i]->uid()); |
| 5631 | } |
| 5632 | updateWakeLockUids_l(tmp); |
| 5633 | } |
| 5634 | } else { |
| 5635 | acquireWakeLock_l(-1); |
| 5636 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5637 | } |
| 5638 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5639 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 5640 | uint32_t sleepUs = 0; |
| 5641 | |
| 5642 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5643 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5644 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5645 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5646 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5647 | if (sleepUs > 0) { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5648 | ATRACE_BEGIN("sleep"); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5649 | usleep(sleepUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5650 | ATRACE_END(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5651 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5652 | } |
| 5653 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5654 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5655 | Vector< sp<RecordTrack> > activeTracks; |
| 5656 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5657 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5658 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5659 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5660 | // reference to a fast track which is about to be removed |
| 5661 | sp<RecordTrack> fastTrackToRemove; |
| 5662 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5663 | { // scope for mLock |
| 5664 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5665 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5666 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5667 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5668 | // check exitPending here because checkForNewParameters_l() and |
| 5669 | // checkForNewParameters_l() can temporarily release mLock |
| 5670 | if (exitPending()) { |
| 5671 | break; |
| 5672 | } |
| 5673 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5674 | // if no active track(s), then standby and release wakelock |
| 5675 | size_t size = mActiveTracks.size(); |
| 5676 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5677 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5678 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5679 | releaseWakeLock_l(); |
| 5680 | ALOGV("RecordThread: loop stopping"); |
| 5681 | // go to sleep |
| 5682 | mWaitWorkCV.wait(mLock); |
| 5683 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5684 | goto reacquire_wakelock; |
| 5685 | } |
| 5686 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5687 | if (mActiveTracksGen != activeTracksGen) { |
| 5688 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5689 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5690 | for (size_t i = 0; i < size; i++) { |
| 5691 | tmp.add(mActiveTracks[i]->uid()); |
| 5692 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5693 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5694 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5695 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5696 | bool doBroadcast = false; |
| 5697 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5698 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5699 | activeTrack = mActiveTracks[i]; |
| 5700 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5701 | if (activeTrack->isFastTrack()) { |
| 5702 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 5703 | fastTrackToRemove = activeTrack; |
| 5704 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5705 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5706 | mActiveTracks.remove(activeTrack); |
| 5707 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5708 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5709 | continue; |
| 5710 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5711 | |
| 5712 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5713 | switch (activeTrackState) { |
| 5714 | |
| 5715 | case TrackBase::PAUSING: |
| 5716 | mActiveTracks.remove(activeTrack); |
| 5717 | mActiveTracksGen++; |
| 5718 | doBroadcast = true; |
| 5719 | size--; |
| 5720 | continue; |
| 5721 | |
| 5722 | case TrackBase::STARTING_1: |
| 5723 | sleepUs = 10000; |
| 5724 | i++; |
| 5725 | continue; |
| 5726 | |
| 5727 | case TrackBase::STARTING_2: |
| 5728 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5729 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5730 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5731 | break; |
| 5732 | |
| 5733 | case TrackBase::ACTIVE: |
| 5734 | break; |
| 5735 | |
| 5736 | case TrackBase::IDLE: |
| 5737 | i++; |
| 5738 | continue; |
| 5739 | |
| 5740 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5741 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5742 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5743 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5744 | activeTracks.add(activeTrack); |
| 5745 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5746 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5747 | if (activeTrack->isFastTrack()) { |
| 5748 | ALOG_ASSERT(!mFastTrackAvail); |
| 5749 | ALOG_ASSERT(fastTrack == 0); |
| 5750 | fastTrack = activeTrack; |
| 5751 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5752 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5753 | if (doBroadcast) { |
| 5754 | mStartStopCond.broadcast(); |
| 5755 | } |
| 5756 | |
| 5757 | // sleep if there are no active tracks to process |
| 5758 | if (activeTracks.size() == 0) { |
| 5759 | if (sleepUs == 0) { |
| 5760 | sleepUs = kRecordThreadSleepUs; |
| 5761 | } |
| 5762 | continue; |
| 5763 | } |
| 5764 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5765 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5766 | lockEffectChains_l(effectChains); |
| 5767 | } |
| 5768 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5769 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5770 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5771 | size_t size = effectChains.size(); |
| 5772 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5773 | // thread mutex is not locked, but effect chain is locked |
| 5774 | effectChains[i]->process_l(); |
| 5775 | } |
| 5776 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5777 | // 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] | 5778 | if (mFastCapture != 0) { |
| 5779 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5780 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5781 | bool didModify = false; |
| 5782 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5783 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5784 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5785 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5786 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5787 | if (old == -1) { |
| 5788 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5789 | } |
| 5790 | } |
| 5791 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5792 | #if 0 // FIXME |
| 5793 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5794 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 5795 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5796 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5797 | didModify = true; |
| 5798 | } |
| 5799 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 5800 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5801 | if (cblkNew != cblkOld) { |
| 5802 | state->mCblk = cblkNew; |
| 5803 | // block until acked if removing a fast track |
| 5804 | if (cblkOld != NULL) { |
| 5805 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 5806 | } |
| 5807 | didModify = true; |
| 5808 | } |
| 5809 | sq->end(didModify); |
| 5810 | if (didModify) { |
| 5811 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5812 | #if 0 |
| 5813 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5814 | mNormalSource = mPipeSource; |
| 5815 | } |
| 5816 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5817 | } |
| 5818 | } |
| 5819 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5820 | // now run the fast track destructor with thread mutex unlocked |
| 5821 | fastTrackToRemove.clear(); |
| 5822 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5823 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 5824 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 5825 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 5826 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 5827 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5828 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5829 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5830 | ssize_t framesRead; |
| 5831 | |
| 5832 | // If an NBAIO source is present, use it to read the normal capture's data |
| 5833 | if (mPipeSource != 0) { |
| 5834 | size_t framesToRead = mBufferSize / mFrameSize; |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5835 | framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize, |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5836 | framesToRead, AudioBufferProvider::kInvalidPTS); |
| 5837 | if (framesRead == 0) { |
| 5838 | // since pipe is non-blocking, simulate blocking input |
| 5839 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 5840 | } |
| 5841 | // otherwise use the HAL / AudioStreamIn directly |
| 5842 | } else { |
| 5843 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5844 | (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5845 | if (bytesRead < 0) { |
| 5846 | framesRead = bytesRead; |
| 5847 | } else { |
| 5848 | framesRead = bytesRead / mFrameSize; |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
| 5853 | ALOGE("read failed: framesRead=%d", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5854 | // Force input into standby so that it tries to recover at next read attempt |
| 5855 | inputStandBy(); |
| 5856 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5857 | } |
| 5858 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5859 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5860 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5861 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5862 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5863 | if (mTeeSink != 0) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5864 | (void) mTeeSink->write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5865 | } |
| 5866 | // 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] | 5867 | { |
| 5868 | size_t part1 = mRsmpInFramesP2 - rear; |
| 5869 | if ((size_t) framesRead > part1) { |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 5870 | memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize, |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5871 | (framesRead - part1) * mFrameSize); |
| 5872 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5873 | } |
| 5874 | rear = mRsmpInRear += framesRead; |
| 5875 | |
| 5876 | size = activeTracks.size(); |
| 5877 | // loop over each active track |
| 5878 | for (size_t i = 0; i < size; i++) { |
| 5879 | activeTrack = activeTracks[i]; |
| 5880 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5881 | // skip fast tracks, as those are handled directly by FastCapture |
| 5882 | if (activeTrack->isFastTrack()) { |
| 5883 | continue; |
| 5884 | } |
| 5885 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5886 | // TODO: This code probably should be moved to RecordTrack. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5887 | // TODO: Update the activeTrack buffer converter in case of reconfigure. |
| 5888 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5889 | enum { |
| 5890 | OVERRUN_UNKNOWN, |
| 5891 | OVERRUN_TRUE, |
| 5892 | OVERRUN_FALSE |
| 5893 | } overrun = OVERRUN_UNKNOWN; |
| 5894 | |
| 5895 | // loop over getNextBuffer to handle circular sink |
| 5896 | for (;;) { |
| 5897 | |
| 5898 | activeTrack->mSink.frameCount = ~0; |
| 5899 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 5900 | size_t framesOut = activeTrack->mSink.frameCount; |
| 5901 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 5902 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5903 | // check available frames and handle overrun conditions |
| 5904 | // if the record track isn't draining fast enough. |
| 5905 | bool hasOverrun; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5906 | size_t framesIn; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 5907 | activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun); |
| 5908 | if (hasOverrun) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5909 | overrun = OVERRUN_TRUE; |
| 5910 | } |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5911 | if (framesOut == 0 || framesIn == 0) { |
| 5912 | break; |
| 5913 | } |
| 5914 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 5915 | // Don't allow framesOut to be larger than what is possible with resampling |
| 5916 | // from framesIn. |
| 5917 | // This isn't strictly necessary but helps limit buffer resizing in |
| 5918 | // RecordBufferConverter. TODO: remove when no longer needed. |
| 5919 | framesOut = min(framesOut, |
| 5920 | destinationFramesPossible( |
| 5921 | framesIn, mSampleRate, activeTrack->mSampleRate)); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 5922 | // process frames from the RecordThread buffer provider to the RecordTrack buffer |
| 5923 | framesOut = activeTrack->mRecordBufferConverter->convert( |
| 5924 | activeTrack->mSink.raw, activeTrack->mResamplerBufferProvider, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5925 | |
| 5926 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 5927 | overrun = OVERRUN_FALSE; |
| 5928 | } |
| 5929 | |
| 5930 | if (activeTrack->mFramesToDrop == 0) { |
| 5931 | if (framesOut > 0) { |
| 5932 | activeTrack->mSink.frameCount = framesOut; |
| 5933 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 5934 | } |
| 5935 | } else { |
| 5936 | // FIXME could do a partial drop of framesOut |
| 5937 | if (activeTrack->mFramesToDrop > 0) { |
| 5938 | activeTrack->mFramesToDrop -= framesOut; |
| 5939 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5940 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5941 | } |
| 5942 | } else { |
| 5943 | activeTrack->mFramesToDrop += framesOut; |
| 5944 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 5945 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 5946 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 5947 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 5948 | activeTrack->sessionId(), |
| 5949 | (activeTrack->mSyncStartEvent != 0) ? |
| 5950 | activeTrack->mSyncStartEvent->triggerSession() : 0); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5951 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5952 | } |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5957 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5958 | } |
| 5959 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5960 | |
| 5961 | switch (overrun) { |
| 5962 | case OVERRUN_TRUE: |
| 5963 | // client isn't retrieving buffers fast enough |
| 5964 | if (!activeTrack->setOverflow()) { |
| 5965 | nsecs_t now = systemTime(); |
| 5966 | // FIXME should lastWarning per track? |
| 5967 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 5968 | ALOGW("RecordThread: buffer overflow"); |
| 5969 | lastWarning = now; |
| 5970 | } |
| 5971 | } |
| 5972 | break; |
| 5973 | case OVERRUN_FALSE: |
| 5974 | activeTrack->clearOverflow(); |
| 5975 | break; |
| 5976 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5977 | break; |
| 5978 | } |
| 5979 | |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5980 | } |
| 5981 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5982 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5983 | // enable changes in effect chain |
| 5984 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5985 | // 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] | 5986 | } |
| 5987 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5988 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5989 | |
| 5990 | { |
| 5991 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 5992 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5993 | sp<RecordTrack> track = mTracks[i]; |
| 5994 | track->invalidate(); |
| 5995 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5996 | mActiveTracks.clear(); |
| 5997 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5998 | mStartStopCond.broadcast(); |
| 5999 | } |
| 6000 | |
| 6001 | releaseWakeLock(); |
| 6002 | |
| 6003 | ALOGV("RecordThread %p exiting", this); |
| 6004 | return false; |
| 6005 | } |
| 6006 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 6007 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6008 | { |
| 6009 | if (!mStandby) { |
| 6010 | inputStandBy(); |
| 6011 | mStandby = true; |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | void AudioFlinger::RecordThread::inputStandBy() |
| 6016 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6017 | // Idle the fast capture if it's currently running |
| 6018 | if (mFastCapture != 0) { |
| 6019 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 6020 | FastCaptureState *state = sq->begin(); |
| 6021 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 6022 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 6023 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 6024 | state->mColdGen++; |
| 6025 | mFastCaptureFutex = 0; |
| 6026 | sq->end(); |
| 6027 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 6028 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 6029 | #if 0 |
| 6030 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 6031 | // FIXME |
| 6032 | } |
| 6033 | #endif |
| 6034 | #ifdef AUDIO_WATCHDOG |
| 6035 | // FIXME |
| 6036 | #endif |
| 6037 | } else { |
| 6038 | sq->end(false /*didModify*/); |
| 6039 | } |
| 6040 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6041 | mInput->stream->common.standby(&mInput->stream->common); |
| 6042 | } |
| 6043 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6044 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6045 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6046 | const sp<AudioFlinger::Client>& client, |
| 6047 | uint32_t sampleRate, |
| 6048 | audio_format_t format, |
| 6049 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6050 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6051 | int sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 6052 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 6053 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 6054 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6055 | pid_t tid, |
| 6056 | status_t *status) |
| 6057 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6058 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6059 | sp<RecordTrack> track; |
| 6060 | status_t lStatus; |
| 6061 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6062 | // client expresses a preference for FAST, but we get the final say |
| 6063 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6064 | if ( |
Glenn Kasten | b7fbf7e | 2015-03-18 12:57:28 -0700 | [diff] [blame] | 6065 | // we formerly checked for a callback handler (non-0 tid), |
| 6066 | // but that is no longer required for TRANSFER_OBTAIN mode |
| 6067 | // |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6068 | // frame count is not specified, or is exactly the pipe depth |
| 6069 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6070 | // PCM data |
| 6071 | audio_is_linear_pcm(format) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6072 | // native format |
| 6073 | (format == mFormat) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6074 | // native channel mask |
| 6075 | (channelMask == mChannelMask) && |
| 6076 | // native hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6077 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 6078 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6079 | hasFastCapture() && |
| 6080 | // there are sufficient fast track slots available |
| 6081 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6082 | ) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6083 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6084 | frameCount, mFrameCount); |
| 6085 | } else { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6086 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " |
| 6087 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6088 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6089 | frameCount, mFrameCount, mPipeFramesP2, |
| 6090 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 6091 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6092 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | |
| 6096 | // compute track buffer size in frames, and suggest the notification frame count |
| 6097 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 6098 | // fast track: frame count is exactly the pipe depth |
| 6099 | frameCount = mPipeFramesP2; |
| 6100 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 6101 | *notificationFrames = mFrameCount; |
| 6102 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6103 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 6104 | // or 20 ms if there is a fast capture |
| 6105 | // TODO This could be a roundupRatio inline, and const |
| 6106 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 6107 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 6108 | // minimum number of notification periods is at least kMinNotifications, |
| 6109 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 6110 | static const size_t kMinNotifications = 3; |
| 6111 | static const uint32_t kMinMs = 30; |
| 6112 | // TODO This could be a roundupRatio inline |
| 6113 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 6114 | // TODO This could be a roundupRatio inline |
| 6115 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 6116 | maxNotificationFrames; |
| 6117 | const size_t minFrameCount = maxNotificationFrames * |
| 6118 | max(kMinNotifications, minNotificationsByMs); |
| 6119 | frameCount = max(frameCount, minFrameCount); |
| 6120 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 6121 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 6122 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6123 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 6124 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6125 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 6126 | lStatus = initCheck(); |
| 6127 | if (lStatus != NO_ERROR) { |
| 6128 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 6129 | goto Exit; |
| 6130 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6131 | |
| 6132 | { // scope for mLock |
| 6133 | Mutex::Autolock _l(mLock); |
| 6134 | |
| 6135 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6136 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 6137 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6138 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 6139 | lStatus = track->initCheck(); |
| 6140 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 6141 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 6142 | // 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] | 6143 | goto Exit; |
| 6144 | } |
| 6145 | mTracks.add(track); |
| 6146 | |
| 6147 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 6148 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6149 | mAudioFlinger->btNrecIsOff(); |
| 6150 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 6151 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 6152 | |
| 6153 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 6154 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 6155 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 6156 | // so ask activity manager to do this on our behalf |
| 6157 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 6158 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6159 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 6160 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6161 | lStatus = NO_ERROR; |
| 6162 | |
| 6163 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 6164 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6165 | return track; |
| 6166 | } |
| 6167 | |
| 6168 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 6169 | AudioSystem::sync_event_t event, |
| 6170 | int triggerSession) |
| 6171 | { |
| 6172 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 6173 | sp<ThreadBase> strongMe = this; |
| 6174 | status_t status = NO_ERROR; |
| 6175 | |
| 6176 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6177 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6178 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6179 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6180 | triggerSession, |
| 6181 | recordTrack->sessionId(), |
| 6182 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6183 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6184 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 6185 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6186 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6187 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6188 | } else { |
| 6189 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6190 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6191 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6196 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6197 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6198 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6199 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6200 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6201 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6202 | } else { |
| 6203 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6204 | } |
| 6205 | return status; |
| 6206 | } |
| 6207 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6208 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6209 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6210 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6211 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6212 | mActiveTracks.add(recordTrack); |
| 6213 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6214 | status_t status = NO_ERROR; |
| 6215 | if (recordTrack->isExternalTrack()) { |
| 6216 | mLock.unlock(); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6217 | status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6218 | mLock.lock(); |
| 6219 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6220 | if (status != NO_ERROR) { |
| 6221 | mActiveTracks.remove(recordTrack); |
| 6222 | mActiveTracksGen++; |
| 6223 | recordTrack->clearSyncStartEvent(); |
| 6224 | ALOGV("RecordThread::start error %d", status); |
| 6225 | return status; |
| 6226 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6227 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6228 | // Catch up with current buffer indices if thread is already running. |
| 6229 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6230 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6231 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6232 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6233 | recordTrack->mResamplerBufferProvider->reset(); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6234 | // clear any converter state as new data will be discontinuous |
| 6235 | recordTrack->mRecordBufferConverter->reset(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6236 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6237 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6238 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6239 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6240 | ALOGV("Record failed to start"); |
| 6241 | status = BAD_VALUE; |
| 6242 | goto startError; |
| 6243 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6244 | return status; |
| 6245 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6246 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6247 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6248 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6249 | AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6250 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6251 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6252 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6253 | return status; |
| 6254 | } |
| 6255 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6256 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6257 | { |
| 6258 | sp<SyncEvent> strongEvent = event.promote(); |
| 6259 | |
| 6260 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6261 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6262 | if (ptr != 0) { |
| 6263 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6264 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6265 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6266 | } |
| 6267 | } |
| 6268 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6269 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6270 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6271 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6272 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6273 | return false; |
| 6274 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6275 | // 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] | 6276 | recordTrack->mState = TrackBase::PAUSING; |
| 6277 | // do not wait for mStartStopCond if exiting |
| 6278 | if (exitPending()) { |
| 6279 | return true; |
| 6280 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6281 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6282 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6283 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6284 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6285 | ALOGV("Record stopped OK"); |
| 6286 | return true; |
| 6287 | } |
| 6288 | return false; |
| 6289 | } |
| 6290 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6291 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6292 | { |
| 6293 | return false; |
| 6294 | } |
| 6295 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6296 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6297 | { |
| 6298 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6299 | if (!isValidSyncEvent(event)) { |
| 6300 | return BAD_VALUE; |
| 6301 | } |
| 6302 | |
| 6303 | int eventSession = event->triggerSession(); |
| 6304 | status_t ret = NAME_NOT_FOUND; |
| 6305 | |
| 6306 | Mutex::Autolock _l(mLock); |
| 6307 | |
| 6308 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6309 | sp<RecordTrack> track = mTracks[i]; |
| 6310 | if (eventSession == track->sessionId()) { |
| 6311 | (void) track->setSyncEvent(event); |
| 6312 | ret = NO_ERROR; |
| 6313 | } |
| 6314 | } |
| 6315 | return ret; |
| 6316 | #else |
| 6317 | return BAD_VALUE; |
| 6318 | #endif |
| 6319 | } |
| 6320 | |
| 6321 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6322 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6323 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6324 | track->terminate(); |
| 6325 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6326 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6327 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6328 | removeTrack_l(track); |
| 6329 | } |
| 6330 | } |
| 6331 | |
| 6332 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6333 | { |
| 6334 | mTracks.remove(track); |
| 6335 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6336 | if (track->isFastTrack()) { |
| 6337 | ALOG_ASSERT(!mFastTrackAvail); |
| 6338 | mFastTrackAvail = true; |
| 6339 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6343 | { |
| 6344 | dumpInternals(fd, args); |
| 6345 | dumpTracks(fd, args); |
| 6346 | dumpEffectChains(fd, args); |
| 6347 | } |
| 6348 | |
| 6349 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6350 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6351 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6352 | |
Glenn Kasten | 44182c2 | 2015-03-05 17:12:23 -0800 | [diff] [blame] | 6353 | dumpBase(fd, args); |
| 6354 | |
| 6355 | if (mActiveTracks.size() == 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6356 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6357 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6358 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6359 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Glenn Kasten | 17c9c99 | 2015-03-02 15:53:01 -0800 | [diff] [blame] | 6360 | |
| 6361 | // Make a non-atomic copy of fast capture dump state so it won't change underneath us |
| 6362 | const FastCaptureDumpState copy(mFastCaptureDumpState); |
| 6363 | copy.dump(fd); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6364 | } |
| 6365 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6366 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6367 | { |
| 6368 | const size_t SIZE = 256; |
| 6369 | char buffer[SIZE]; |
| 6370 | String8 result; |
| 6371 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6372 | size_t numtracks = mTracks.size(); |
| 6373 | size_t numactive = mActiveTracks.size(); |
| 6374 | size_t numactiveseen = 0; |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6375 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6376 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6377 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6378 | RecordTrack::appendDumpHeader(result); |
| 6379 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6380 | sp<RecordTrack> track = mTracks[i]; |
| 6381 | if (track != 0) { |
| 6382 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6383 | if (active) { |
| 6384 | numactiveseen++; |
| 6385 | } |
| 6386 | track->dump(buffer, SIZE, active); |
| 6387 | result.append(buffer); |
| 6388 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6389 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6390 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6391 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6392 | } |
| 6393 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6394 | if (numactiveseen != numactive) { |
| 6395 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6396 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6397 | result.append(buffer); |
| 6398 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6399 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6400 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6401 | if (mTracks.indexOf(track) < 0) { |
| 6402 | track->dump(buffer, SIZE, true); |
| 6403 | result.append(buffer); |
| 6404 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6405 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6406 | |
| 6407 | } |
| 6408 | write(fd, result.string(), result.size()); |
| 6409 | } |
| 6410 | |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6411 | |
| 6412 | void AudioFlinger::RecordThread::ResamplerBufferProvider::reset() |
| 6413 | { |
| 6414 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6415 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6416 | mRsmpInFront = recordThread->mRsmpInRear; |
| 6417 | mRsmpInUnrel = 0; |
| 6418 | } |
| 6419 | |
| 6420 | void AudioFlinger::RecordThread::ResamplerBufferProvider::sync( |
| 6421 | size_t *framesAvailable, bool *hasOverrun) |
| 6422 | { |
| 6423 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
| 6424 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6425 | const int32_t rear = recordThread->mRsmpInRear; |
| 6426 | const int32_t front = mRsmpInFront; |
| 6427 | const ssize_t filled = rear - front; |
| 6428 | |
| 6429 | size_t framesIn; |
| 6430 | bool overrun = false; |
| 6431 | if (filled < 0) { |
| 6432 | // should not happen, but treat like a massive overrun and re-sync |
| 6433 | framesIn = 0; |
| 6434 | mRsmpInFront = rear; |
| 6435 | overrun = true; |
| 6436 | } else if ((size_t) filled <= recordThread->mRsmpInFrames) { |
| 6437 | framesIn = (size_t) filled; |
| 6438 | } else { |
| 6439 | // client is not keeping up with server, but give it latest data |
| 6440 | framesIn = recordThread->mRsmpInFrames; |
| 6441 | mRsmpInFront = /* front = */ rear - framesIn; |
| 6442 | overrun = true; |
| 6443 | } |
| 6444 | if (framesAvailable != NULL) { |
| 6445 | *framesAvailable = framesIn; |
| 6446 | } |
| 6447 | if (hasOverrun != NULL) { |
| 6448 | *hasOverrun = overrun; |
| 6449 | } |
| 6450 | } |
| 6451 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6452 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6453 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
| 6454 | AudioBufferProvider::Buffer* buffer, int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6455 | { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6456 | sp<ThreadBase> threadBase = mRecordTrack->mThread.promote(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6457 | if (threadBase == 0) { |
| 6458 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6459 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6460 | return NOT_ENOUGH_DATA; |
| 6461 | } |
| 6462 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6463 | int32_t rear = recordThread->mRsmpInRear; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6464 | int32_t front = mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6465 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6466 | // FIXME should not be P2 (don't want to increase latency) |
| 6467 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6468 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6469 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6470 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6471 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6472 | if (part1 > (size_t) filled) { |
| 6473 | part1 = filled; |
| 6474 | } |
| 6475 | size_t ask = buffer->frameCount; |
| 6476 | ALOG_ASSERT(ask > 0); |
| 6477 | if (part1 > ask) { |
| 6478 | part1 = ask; |
| 6479 | } |
| 6480 | if (part1 == 0) { |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6481 | // 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] | 6482 | buffer->raw = NULL; |
| 6483 | buffer->frameCount = 0; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6484 | mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6485 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6486 | } |
| 6487 | |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6488 | buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6489 | buffer->frameCount = part1; |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6490 | mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6491 | return NO_ERROR; |
| 6492 | } |
| 6493 | |
| 6494 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6495 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 6496 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6497 | { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6498 | size_t stepCount = buffer->frameCount; |
| 6499 | if (stepCount == 0) { |
| 6500 | return; |
| 6501 | } |
Andy Hung | 73c02e4 | 2015-03-29 01:13:58 -0700 | [diff] [blame] | 6502 | ALOG_ASSERT(stepCount <= mRsmpInUnrel); |
| 6503 | mRsmpInUnrel -= stepCount; |
| 6504 | mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6505 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6506 | buffer->frameCount = 0; |
| 6507 | } |
| 6508 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6509 | AudioFlinger::RecordThread::RecordBufferConverter::RecordBufferConverter( |
| 6510 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6511 | uint32_t srcSampleRate, |
| 6512 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6513 | uint32_t dstSampleRate) : |
| 6514 | mSrcChannelMask(AUDIO_CHANNEL_INVALID), // updateParameters will set following vars |
| 6515 | // mSrcFormat |
| 6516 | // mSrcSampleRate |
| 6517 | // mDstChannelMask |
| 6518 | // mDstFormat |
| 6519 | // mDstSampleRate |
| 6520 | // mSrcChannelCount |
| 6521 | // mDstChannelCount |
| 6522 | // mDstFrameSize |
| 6523 | mBuf(NULL), mBufFrames(0), mBufFrameSize(0), |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6524 | mResampler(NULL), |
| 6525 | mIsLegacyDownmix(false), |
| 6526 | mIsLegacyUpmix(false), |
| 6527 | mRequiresFloat(false), |
| 6528 | mInputConverterProvider(NULL) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6529 | { |
| 6530 | (void)updateParameters(srcChannelMask, srcFormat, srcSampleRate, |
| 6531 | dstChannelMask, dstFormat, dstSampleRate); |
| 6532 | } |
| 6533 | |
| 6534 | AudioFlinger::RecordThread::RecordBufferConverter::~RecordBufferConverter() { |
| 6535 | free(mBuf); |
| 6536 | delete mResampler; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6537 | delete mInputConverterProvider; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | size_t AudioFlinger::RecordThread::RecordBufferConverter::convert(void *dst, |
| 6541 | AudioBufferProvider *provider, size_t frames) |
| 6542 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6543 | if (mInputConverterProvider != NULL) { |
| 6544 | mInputConverterProvider->setBufferProvider(provider); |
| 6545 | provider = mInputConverterProvider; |
| 6546 | } |
| 6547 | |
| 6548 | if (mResampler == NULL) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6549 | ALOGVV("NO RESAMPLING sampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6550 | mSrcSampleRate, mSrcFormat, mDstFormat); |
| 6551 | |
| 6552 | AudioBufferProvider::Buffer buffer; |
| 6553 | for (size_t i = frames; i > 0; ) { |
| 6554 | buffer.frameCount = i; |
| 6555 | status_t status = provider->getNextBuffer(&buffer, 0); |
| 6556 | if (status != OK || buffer.frameCount == 0) { |
| 6557 | frames -= i; // cannot fill request. |
| 6558 | break; |
| 6559 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6560 | // format convert to destination buffer |
| 6561 | convertNoResampler(dst, buffer.raw, buffer.frameCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6562 | |
| 6563 | dst = (int8_t*)dst + buffer.frameCount * mDstFrameSize; |
| 6564 | i -= buffer.frameCount; |
| 6565 | provider->releaseBuffer(&buffer); |
| 6566 | } |
| 6567 | } else { |
| 6568 | ALOGVV("RESAMPLING mSrcSampleRate:%u mDstSampleRate:%u mSrcFormat:%#x mDstFormat:%#x", |
| 6569 | mSrcSampleRate, mDstSampleRate, mSrcFormat, mDstFormat); |
| 6570 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6571 | // reallocate buffer if needed |
| 6572 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6573 | free(mBuf); |
| 6574 | mBufFrames = frames; |
| 6575 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6576 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6577 | // resampler accumulates, but we only have one source track |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6578 | memset(mBuf, 0, frames * mBufFrameSize); |
| 6579 | frames = mResampler->resample((int32_t*)mBuf, frames, provider); |
| 6580 | // format convert to destination buffer |
| 6581 | convertResampler(dst, mBuf, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6582 | } |
| 6583 | return frames; |
| 6584 | } |
| 6585 | |
| 6586 | status_t AudioFlinger::RecordThread::RecordBufferConverter::updateParameters( |
| 6587 | audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, |
| 6588 | uint32_t srcSampleRate, |
| 6589 | audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, |
| 6590 | uint32_t dstSampleRate) |
| 6591 | { |
| 6592 | // quick evaluation if there is any change. |
| 6593 | if (mSrcFormat == srcFormat |
| 6594 | && mSrcChannelMask == srcChannelMask |
| 6595 | && mSrcSampleRate == srcSampleRate |
| 6596 | && mDstFormat == dstFormat |
| 6597 | && mDstChannelMask == dstChannelMask |
| 6598 | && mDstSampleRate == dstSampleRate) { |
| 6599 | return NO_ERROR; |
| 6600 | } |
| 6601 | |
Andy Hung | db4c031 | 2015-05-06 08:46:52 -0700 | [diff] [blame] | 6602 | ALOGV("RecordBufferConverter updateParameters srcMask:%#x dstMask:%#x" |
| 6603 | " srcFormat:%#x dstFormat:%#x srcRate:%u dstRate:%u", |
| 6604 | srcChannelMask, dstChannelMask, srcFormat, dstFormat, srcSampleRate, dstSampleRate); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6605 | const bool valid = |
| 6606 | audio_is_input_channel(srcChannelMask) |
| 6607 | && audio_is_input_channel(dstChannelMask) |
| 6608 | && audio_is_valid_format(srcFormat) && audio_is_linear_pcm(srcFormat) |
| 6609 | && audio_is_valid_format(dstFormat) && audio_is_linear_pcm(dstFormat) |
| 6610 | && (srcSampleRate <= dstSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) |
| 6611 | ; // no upsampling checks for now |
| 6612 | if (!valid) { |
| 6613 | return BAD_VALUE; |
| 6614 | } |
| 6615 | |
| 6616 | mSrcFormat = srcFormat; |
| 6617 | mSrcChannelMask = srcChannelMask; |
| 6618 | mSrcSampleRate = srcSampleRate; |
| 6619 | mDstFormat = dstFormat; |
| 6620 | mDstChannelMask = dstChannelMask; |
| 6621 | mDstSampleRate = dstSampleRate; |
| 6622 | |
| 6623 | // compute derived parameters |
| 6624 | mSrcChannelCount = audio_channel_count_from_in_mask(srcChannelMask); |
| 6625 | mDstChannelCount = audio_channel_count_from_in_mask(dstChannelMask); |
| 6626 | mDstFrameSize = mDstChannelCount * audio_bytes_per_sample(mDstFormat); |
| 6627 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6628 | // do we need to resample? |
| 6629 | delete mResampler; |
| 6630 | mResampler = NULL; |
| 6631 | if (mSrcSampleRate != mDstSampleRate) { |
| 6632 | mResampler = AudioResampler::create(AUDIO_FORMAT_PCM_FLOAT, |
| 6633 | mSrcChannelCount, mDstSampleRate); |
| 6634 | mResampler->setSampleRate(mSrcSampleRate); |
| 6635 | mResampler->setVolume(AudioMixer::UNITY_GAIN_FLOAT, AudioMixer::UNITY_GAIN_FLOAT); |
| 6636 | } |
| 6637 | |
| 6638 | // are we running legacy channel conversion modes? |
| 6639 | mIsLegacyDownmix = (mSrcChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6640 | || mSrcChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 6641 | && mDstChannelMask == AUDIO_CHANNEL_IN_MONO; |
| 6642 | mIsLegacyUpmix = mSrcChannelMask == AUDIO_CHANNEL_IN_MONO |
| 6643 | && (mDstChannelMask == AUDIO_CHANNEL_IN_STEREO |
| 6644 | || mDstChannelMask == AUDIO_CHANNEL_IN_FRONT_BACK); |
| 6645 | |
| 6646 | // do we need to process in float? |
| 6647 | mRequiresFloat = mResampler != NULL || mIsLegacyDownmix || mIsLegacyUpmix; |
| 6648 | |
| 6649 | // do we need a staging buffer to convert for destination (we can still optimize this)? |
| 6650 | // we use mBufFrameSize > 0 to indicate both frame size as well as buffer necessity |
| 6651 | if (mResampler != NULL) { |
| 6652 | mBufFrameSize = max(mSrcChannelCount, FCC_2) |
| 6653 | * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
Andy Hung | a97630b | 2015-07-22 23:27:24 -0700 | [diff] [blame^] | 6654 | } else if (mIsLegacyUpmix || mIsLegacyDownmix) { // legacy modes always float |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6655 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT); |
| 6656 | } else if (mSrcChannelMask != mDstChannelMask && mDstFormat != mSrcFormat) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6657 | mBufFrameSize = mDstChannelCount * audio_bytes_per_sample(mSrcFormat); |
| 6658 | } else { |
| 6659 | mBufFrameSize = 0; |
| 6660 | } |
| 6661 | mBufFrames = 0; // force the buffer to be resized. |
| 6662 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6663 | // do we need an input converter buffer provider to give us float? |
| 6664 | delete mInputConverterProvider; |
| 6665 | mInputConverterProvider = NULL; |
| 6666 | if (mRequiresFloat && mSrcFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6667 | mInputConverterProvider = new ReformatBufferProvider( |
| 6668 | audio_channel_count_from_in_mask(mSrcChannelMask), |
| 6669 | mSrcFormat, |
| 6670 | AUDIO_FORMAT_PCM_FLOAT, |
| 6671 | 256 /* provider buffer frame count */); |
| 6672 | } |
| 6673 | |
| 6674 | // do we need a remixer to do channel mask conversion |
| 6675 | if (!mIsLegacyDownmix && !mIsLegacyUpmix && mSrcChannelMask != mDstChannelMask) { |
| 6676 | (void) memcpy_by_index_array_initialization_from_channel_mask( |
| 6677 | mIdxAry, ARRAY_SIZE(mIdxAry), mDstChannelMask, mSrcChannelMask); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6678 | } |
| 6679 | return NO_ERROR; |
| 6680 | } |
| 6681 | |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6682 | void AudioFlinger::RecordThread::RecordBufferConverter::convertNoResampler( |
| 6683 | void *dst, const void *src, size_t frames) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6684 | { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6685 | // 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] | 6686 | if (mBufFrameSize != 0 && mBufFrames < frames) { |
| 6687 | free(mBuf); |
| 6688 | mBufFrames = frames; |
| 6689 | (void)posix_memalign(&mBuf, 32, mBufFrames * mBufFrameSize); |
| 6690 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6691 | // do we need to do legacy upmix and downmix? |
| 6692 | if (mIsLegacyUpmix || mIsLegacyDownmix) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6693 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6694 | if (mIsLegacyUpmix) { |
| 6695 | upmix_to_stereo_float_from_mono_float((float *)dstBuf, |
| 6696 | (const float *)src, frames); |
| 6697 | } else /*mIsLegacyDownmix */ { |
| 6698 | downmix_to_mono_float_from_stereo_float((float *)dstBuf, |
| 6699 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6700 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6701 | if (mBuf != NULL) { |
| 6702 | memcpy_by_audio_format(dst, mDstFormat, mBuf, AUDIO_FORMAT_PCM_FLOAT, |
| 6703 | frames * mDstChannelCount); |
| 6704 | } |
| 6705 | return; |
| 6706 | } |
| 6707 | // do we need to do channel mask conversion? |
| 6708 | if (mSrcChannelMask != mDstChannelMask) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6709 | void *dstBuf = mBuf != NULL ? mBuf : dst; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6710 | memcpy_by_index_array(dstBuf, mDstChannelCount, |
| 6711 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mSrcFormat), frames); |
| 6712 | if (dstBuf == dst) { |
| 6713 | return; // format is the same |
| 6714 | } |
| 6715 | } |
| 6716 | // convert to destination buffer |
| 6717 | const void *convertBuf = mBuf != NULL ? mBuf : src; |
| 6718 | memcpy_by_audio_format(dst, mDstFormat, convertBuf, mSrcFormat, |
| 6719 | frames * mDstChannelCount); |
| 6720 | } |
| 6721 | |
| 6722 | void AudioFlinger::RecordThread::RecordBufferConverter::convertResampler( |
| 6723 | void *dst, /*not-a-const*/ void *src, size_t frames) |
| 6724 | { |
| 6725 | // src buffer format is ALWAYS float when entering this routine |
| 6726 | if (mIsLegacyUpmix) { |
| 6727 | ; // mono to stereo already handled by resampler |
| 6728 | } else if (mIsLegacyDownmix |
| 6729 | || (mSrcChannelMask == mDstChannelMask && mSrcChannelCount == 1)) { |
| 6730 | // the resampler outputs stereo for mono input channel (a feature?) |
| 6731 | // must convert to mono |
| 6732 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6733 | (const float *)src, frames); |
| 6734 | } else if (mSrcChannelMask != mDstChannelMask) { |
| 6735 | // convert to mono channel again for channel mask conversion (could be skipped |
| 6736 | // with further optimization). |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6737 | if (mSrcChannelCount == 1) { |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6738 | downmix_to_mono_float_from_stereo_float((float *)src, |
| 6739 | (const float *)src, frames); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6740 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6741 | // convert to destination format (in place, OK as float is larger than other types) |
| 6742 | if (mDstFormat != AUDIO_FORMAT_PCM_FLOAT) { |
| 6743 | memcpy_by_audio_format(src, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6744 | frames * mSrcChannelCount); |
| 6745 | } |
| 6746 | // channel convert and save to dst |
| 6747 | memcpy_by_index_array(dst, mDstChannelCount, |
| 6748 | src, mSrcChannelCount, mIdxAry, audio_bytes_per_sample(mDstFormat), frames); |
| 6749 | return; |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6750 | } |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6751 | // convert to destination format and save to dst |
| 6752 | memcpy_by_audio_format(dst, mDstFormat, src, AUDIO_FORMAT_PCM_FLOAT, |
| 6753 | frames * mDstChannelCount); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6754 | } |
| 6755 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6756 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 6757 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6758 | { |
| 6759 | bool reconfig = false; |
| 6760 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6761 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6762 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6763 | audio_format_t reqFormat = mFormat; |
| 6764 | uint32_t samplingRate = mSampleRate; |
Glenn Kasten | e1635ec | 2015-06-08 15:46:49 -0700 | [diff] [blame] | 6765 | // 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] | 6766 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 6767 | |
| 6768 | AudioParameter param = AudioParameter(keyValuePair); |
| 6769 | int value; |
| 6770 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 6771 | // channel count change can be requested. Do we mandate the first client defines the |
| 6772 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 6773 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6774 | samplingRate = value; |
| 6775 | reconfig = true; |
| 6776 | } |
| 6777 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6778 | if (!audio_is_linear_pcm((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6779 | status = BAD_VALUE; |
| 6780 | } else { |
| 6781 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6782 | reconfig = true; |
| 6783 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6784 | } |
| 6785 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 6786 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6787 | if (!audio_is_input_channel(mask) || |
| 6788 | audio_channel_count_from_in_mask(mask) > FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6789 | status = BAD_VALUE; |
| 6790 | } else { |
| 6791 | channelMask = mask; |
| 6792 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6793 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6794 | } |
| 6795 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6796 | // do not accept frame count changes if tracks are open as the track buffer |
| 6797 | // size depends on frame count and correct behavior would not be guaranteed |
| 6798 | // if frame count is changed after track creation |
| 6799 | if (mActiveTracks.size() > 0) { |
| 6800 | status = INVALID_OPERATION; |
| 6801 | } else { |
| 6802 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6803 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6804 | } |
| 6805 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6806 | // forward device change to effects that have requested to be |
| 6807 | // aware of attached audio device. |
| 6808 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6809 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6810 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6811 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6812 | // store input device and output device but do not forward output device to audio HAL. |
| 6813 | // Note that status is ignored by the caller for output device |
| 6814 | // (see AudioFlinger::setParameters() |
| 6815 | if (audio_is_output_devices(value)) { |
| 6816 | mOutDevice = value; |
| 6817 | status = BAD_VALUE; |
| 6818 | } else { |
| 6819 | mInDevice = value; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 6820 | if (value != AUDIO_DEVICE_NONE) { |
| 6821 | mPrevInDevice = value; |
| 6822 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6823 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 6824 | // pre processings |
| 6825 | if (mTracks.size() > 0) { |
| 6826 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6827 | mAudioFlinger->btNrecIsOff(); |
| 6828 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6829 | sp<RecordTrack> track = mTracks[i]; |
| 6830 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 6831 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6832 | } |
| 6833 | } |
| 6834 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6835 | } |
| 6836 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 6837 | mAudioSource != (audio_source_t)value) { |
| 6838 | // forward device change to effects that have requested to be |
| 6839 | // aware of attached audio device. |
| 6840 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6841 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6842 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6843 | mAudioSource = (audio_source_t)value; |
| 6844 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6845 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6846 | if (status == NO_ERROR) { |
| 6847 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6848 | keyValuePair.string()); |
| 6849 | if (status == INVALID_OPERATION) { |
| 6850 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6851 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6852 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6853 | } |
| 6854 | if (reconfig) { |
| 6855 | if (status == BAD_VALUE && |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6856 | audio_is_linear_pcm(mInput->stream->common.get_format(&mInput->stream->common)) && |
| 6857 | audio_is_linear_pcm(reqFormat) && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6858 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6859 | <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6860 | audio_channel_count_from_in_mask( |
Andy Hung | d1abb8f | 2015-05-05 23:42:34 -0700 | [diff] [blame] | 6861 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_8) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6862 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6863 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6864 | if (status == NO_ERROR) { |
| 6865 | readInputParameters_l(); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6866 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6867 | } |
| 6868 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6869 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6870 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6871 | return reconfig; |
| 6872 | } |
| 6873 | |
| 6874 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6875 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6876 | Mutex::Autolock _l(mLock); |
| 6877 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6878 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6879 | } |
| 6880 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6881 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 6882 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6883 | free(s); |
| 6884 | return out_s8; |
| 6885 | } |
| 6886 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 6887 | void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6888 | sp<AudioIoDescriptor> desc = new AudioIoDescriptor(); |
| 6889 | |
| 6890 | desc->mIoHandle = mId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6891 | |
| 6892 | switch (event) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6893 | case AUDIO_INPUT_OPENED: |
| 6894 | case AUDIO_INPUT_CONFIG_CHANGED: |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 6895 | desc->mPatch = mPatch; |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6896 | desc->mChannelMask = mChannelMask; |
| 6897 | desc->mSamplingRate = mSampleRate; |
| 6898 | desc->mFormat = mFormat; |
| 6899 | desc->mFrameCount = mFrameCount; |
| 6900 | desc->mLatency = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6901 | break; |
| 6902 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 6903 | case AUDIO_INPUT_CLOSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6904 | default: |
| 6905 | break; |
| 6906 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 6907 | mAudioFlinger->ioConfigChanged(event, desc, pid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6908 | } |
| 6909 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6910 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6911 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6912 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 6913 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6914 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6915 | if (mChannelCount > FCC_8) { |
| 6916 | ALOGE("HAL channel count %d > %d", mChannelCount, FCC_8); |
| 6917 | } |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6918 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 6919 | mFormat = mHALFormat; |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 6920 | if (!audio_is_linear_pcm(mFormat)) { |
| 6921 | ALOGE("HAL format %#x is not linear pcm", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6922 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6923 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6924 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 6925 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6926 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6927 | // 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] | 6928 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6929 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6930 | // A larger value should allow more old data to be read after a track calls start(), |
| 6931 | // without increasing latency. |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 6932 | // |
| 6933 | // Note this is independent of the maximum downsampling ratio permitted for capture. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6934 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6935 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6936 | free(mRsmpInBuffer); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6937 | |
| 6938 | // TODO optimize audio capture buffer sizes ... |
| 6939 | // Here we calculate the size of the sliding buffer used as a source |
| 6940 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 6941 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 6942 | // be better to have it derived from the pipe depth in the long term. |
| 6943 | // The current value is higher than necessary. However it should not add to latency. |
| 6944 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6945 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
Andy Hung | 5744661 | 2015-04-19 23:56:46 -0700 | [diff] [blame] | 6946 | (void)posix_memalign(&mRsmpInBuffer, 32, (mRsmpInFramesP2 + mFrameCount - 1) * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6947 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6948 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 6949 | // 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] | 6950 | } |
| 6951 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6952 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6953 | { |
| 6954 | Mutex::Autolock _l(mLock); |
| 6955 | if (initCheck() != NO_ERROR) { |
| 6956 | return 0; |
| 6957 | } |
| 6958 | |
| 6959 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 6960 | } |
| 6961 | |
| 6962 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const |
| 6963 | { |
| 6964 | Mutex::Autolock _l(mLock); |
| 6965 | uint32_t result = 0; |
| 6966 | if (getEffectChain_l(sessionId) != 0) { |
| 6967 | result = EFFECT_SESSION; |
| 6968 | } |
| 6969 | |
| 6970 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6971 | if (sessionId == mTracks[i]->sessionId()) { |
| 6972 | result |= TRACK_SESSION; |
| 6973 | break; |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | return result; |
| 6978 | } |
| 6979 | |
| 6980 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const |
| 6981 | { |
| 6982 | KeyedVector<int, bool> ids; |
| 6983 | Mutex::Autolock _l(mLock); |
| 6984 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 6985 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
| 6986 | int sessionId = track->sessionId(); |
| 6987 | if (ids.indexOfKey(sessionId) < 0) { |
| 6988 | ids.add(sessionId, true); |
| 6989 | } |
| 6990 | } |
| 6991 | return ids; |
| 6992 | } |
| 6993 | |
| 6994 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6995 | { |
| 6996 | Mutex::Autolock _l(mLock); |
| 6997 | AudioStreamIn *input = mInput; |
| 6998 | mInput = NULL; |
| 6999 | return input; |
| 7000 | } |
| 7001 | |
| 7002 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 7003 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 7004 | { |
| 7005 | if (mInput == NULL) { |
| 7006 | return NULL; |
| 7007 | } |
| 7008 | return &mInput->stream->common; |
| 7009 | } |
| 7010 | |
| 7011 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 7012 | { |
| 7013 | // only one chain per input thread |
| 7014 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7015 | 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] | 7016 | return INVALID_OPERATION; |
| 7017 | } |
| 7018 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 7019 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7020 | chain->setInBuffer(NULL); |
| 7021 | chain->setOutBuffer(NULL); |
| 7022 | |
| 7023 | checkSuspendOnAddEffectChain_l(chain); |
| 7024 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 7025 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 7026 | // just moved them to a new input stream. |
| 7027 | chain->syncHalEffectsState(); |
| 7028 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 7029 | mEffectChains.add(chain); |
| 7030 | |
| 7031 | return NO_ERROR; |
| 7032 | } |
| 7033 | |
| 7034 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 7035 | { |
| 7036 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 7037 | ALOGW_IF(mEffectChains.size() != 1, |
| 7038 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 7039 | chain.get(), mEffectChains.size(), this); |
| 7040 | if (mEffectChains.size() == 1) { |
| 7041 | mEffectChains.removeAt(0); |
| 7042 | } |
| 7043 | return 0; |
| 7044 | } |
| 7045 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7046 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 7047 | audio_patch_handle_t *handle) |
| 7048 | { |
| 7049 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7050 | |
| 7051 | // store new device and send to effects |
| 7052 | mInDevice = patch->sources[0].ext.device.type; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7053 | mPatch = *patch; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7054 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 7055 | mEffectChains[i]->setDevice_l(mInDevice); |
| 7056 | } |
| 7057 | |
| 7058 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 7059 | // pre processings |
| 7060 | if (mTracks.size() > 0) { |
| 7061 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 7062 | mAudioFlinger->btNrecIsOff(); |
| 7063 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 7064 | sp<RecordTrack> track = mTracks[i]; |
| 7065 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 7066 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | // store new source and send to effects |
| 7071 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 7072 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7073 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7074 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7075 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7076 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7077 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7078 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7079 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7080 | status = hwDevice->create_audio_patch(hwDevice, |
| 7081 | patch->num_sources, |
| 7082 | patch->sources, |
| 7083 | patch->num_sinks, |
| 7084 | patch->sinks, |
| 7085 | handle); |
| 7086 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7087 | char *address; |
| 7088 | if (strcmp(patch->sources[0].ext.device.address, "") != 0) { |
| 7089 | address = audio_device_address_to_parameter( |
| 7090 | patch->sources[0].ext.device.type, |
| 7091 | patch->sources[0].ext.device.address); |
| 7092 | } else { |
| 7093 | address = (char *)calloc(1, 1); |
| 7094 | } |
| 7095 | AudioParameter param = AudioParameter(String8(address)); |
| 7096 | free(address); |
| 7097 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), |
| 7098 | (int)patch->sources[0].ext.device.type); |
| 7099 | param.addInt(String8(AUDIO_PARAMETER_STREAM_INPUT_SOURCE), |
| 7100 | (int)patch->sinks[0].ext.mix.usecase.source); |
| 7101 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7102 | param.toString().string()); |
| 7103 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7104 | } |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7105 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 7106 | if (mInDevice != mPrevInDevice) { |
| 7107 | sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED); |
| 7108 | mPrevInDevice = mInDevice; |
| 7109 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 7110 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7111 | return status; |
| 7112 | } |
| 7113 | |
| 7114 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 7115 | { |
| 7116 | status_t status = NO_ERROR; |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7117 | |
| 7118 | mInDevice = AUDIO_DEVICE_NONE; |
| 7119 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7120 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 7121 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 7122 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 7123 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 7124 | AudioParameter param; |
| 7125 | param.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), 0); |
| 7126 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 7127 | param.toString().string()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7128 | } |
| 7129 | return status; |
| 7130 | } |
| 7131 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 7132 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 7133 | { |
| 7134 | Mutex::Autolock _l(mLock); |
| 7135 | mTracks.add(record); |
| 7136 | } |
| 7137 | |
| 7138 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 7139 | { |
| 7140 | Mutex::Autolock _l(mLock); |
| 7141 | destroyTrack_l(record); |
| 7142 | } |
| 7143 | |
| 7144 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 7145 | { |
| 7146 | ThreadBase::getAudioPortConfig(config); |
| 7147 | config->role = AUDIO_PORT_ROLE_SINK; |
| 7148 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 7149 | config->ext.mix.usecase.source = mAudioSource; |
| 7150 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7151 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 7152 | } // namespace android |