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" |
| 59 | #include "FastMixer.h" |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 60 | #include "FastCapture.h" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 61 | #include "ServiceUtilities.h" |
| 62 | #include "SchedulingPolicyService.h" |
| 63 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 64 | #ifdef ADD_BATTERY_DATA |
| 65 | #include <media/IMediaPlayerService.h> |
| 66 | #include <media/IMediaDeathNotifier.h> |
| 67 | #endif |
| 68 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | #ifdef DEBUG_CPU_USAGE |
| 70 | #include <cpustats/CentralTendencyStatistics.h> |
| 71 | #include <cpustats/ThreadCpuUsage.h> |
| 72 | #endif |
| 73 | |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
| 76 | // Note: the following macro is used for extremely verbose logging message. In |
| 77 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 78 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 79 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 80 | // turned on. Do not uncomment the #def below unless you really know what you |
| 81 | // are doing and want to see all of the extremely verbose messages. |
| 82 | //#define VERY_VERY_VERBOSE_LOGGING |
| 83 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 84 | #define ALOGVV ALOGV |
| 85 | #else |
| 86 | #define ALOGVV(a...) do { } while(0) |
| 87 | #endif |
| 88 | |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 89 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 90 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 91 | namespace android { |
| 92 | |
| 93 | // retry counts for buffer fill timeout |
| 94 | // 50 * ~20msecs = 1 second |
| 95 | static const int8_t kMaxTrackRetries = 50; |
| 96 | static const int8_t kMaxTrackStartupRetries = 50; |
| 97 | // allow less retry attempts on direct output thread. |
| 98 | // direct outputs can be a scarce resource in audio hardware and should |
| 99 | // be released as quickly as possible. |
| 100 | static const int8_t kMaxTrackRetriesDirect = 2; |
| 101 | |
| 102 | // don't warn about blocked writes or record buffer overflows more often than this |
| 103 | static const nsecs_t kWarningThrottleNs = seconds(5); |
| 104 | |
| 105 | // RecordThread loop sleep time upon application overrun or audio HAL read error |
| 106 | static const int kRecordThreadSleepUs = 5000; |
| 107 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 108 | // maximum time to wait in sendConfigEvent_l() for a status to be received |
| 109 | static const nsecs_t kConfigEventTimeoutNs = seconds(2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 110 | |
| 111 | // minimum sleep time for the mixer thread loop when tracks are active but in underrun |
| 112 | static const uint32_t kMinThreadSleepTimeUs = 5000; |
| 113 | // maximum divider applied to the active sleep time in the mixer thread loop |
| 114 | static const uint32_t kMaxThreadSleepTimeShift = 2; |
| 115 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 116 | // minimum normal sink buffer size, expressed in milliseconds rather than frames |
| 117 | static const uint32_t kMinNormalSinkBufferSizeMs = 20; |
| 118 | // maximum normal sink buffer size |
| 119 | static const uint32_t kMaxNormalSinkBufferSizeMs = 24; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 120 | |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 121 | // Offloaded output thread standby delay: allows track transition without going to standby |
| 122 | static const nsecs_t kOffloadStandbyDelayNs = seconds(1); |
| 123 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 124 | // Whether to use fast mixer |
| 125 | static const enum { |
| 126 | FastMixer_Never, // never initialize or use: for debugging only |
| 127 | FastMixer_Always, // always initialize and use, even if not needed: for debugging only |
| 128 | // normal mixer multiplier is 1 |
| 129 | FastMixer_Static, // initialize if needed, then use all the time if initialized, |
| 130 | // multiplier is calculated based on min & max normal mixer buffer size |
| 131 | FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load, |
| 132 | // multiplier is calculated based on min & max normal mixer buffer size |
| 133 | // FIXME for FastMixer_Dynamic: |
| 134 | // Supporting this option will require fixing HALs that can't handle large writes. |
| 135 | // For example, one HAL implementation returns an error from a large write, |
| 136 | // and another HAL implementation corrupts memory, possibly in the sample rate converter. |
| 137 | // We could either fix the HAL implementations, or provide a wrapper that breaks |
| 138 | // up large writes into smaller ones, and the wrapper would need to deal with scheduler. |
| 139 | } kUseFastMixer = FastMixer_Static; |
| 140 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 141 | // Whether to use fast capture |
| 142 | static const enum { |
| 143 | FastCapture_Never, // never initialize or use: for debugging only |
| 144 | FastCapture_Always, // always initialize and use, even if not needed: for debugging only |
| 145 | FastCapture_Static, // initialize if needed, then use all the time if initialized |
| 146 | } kUseFastCapture = FastCapture_Static; |
| 147 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 148 | // Priorities for requestPriority |
| 149 | static const int kPriorityAudioApp = 2; |
| 150 | static const int kPriorityFastMixer = 3; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 151 | static const int kPriorityFastCapture = 3; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 152 | |
| 153 | // IAudioFlinger::createTrack() reports back to client the total size of shared memory area |
| 154 | // 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] | 155 | // Currently the client uses N-buffering by default, but doesn't tell us about the value of N. |
| 156 | // So for now we just assume that client is double-buffered for fast tracks. |
| 157 | // FIXME It would be better for client to tell AudioFlinger the value of N, |
| 158 | // so AudioFlinger could allocate the right amount of memory. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 159 | // See the client's minBufCount and mNotificationFramesAct calculations for details. |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 160 | |
| 161 | // This is the default value, if not specified by property. |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 162 | static const int kFastTrackMultiplier = 2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 164 | // The minimum and maximum allowed values |
| 165 | static const int kFastTrackMultiplierMin = 1; |
| 166 | static const int kFastTrackMultiplierMax = 2; |
| 167 | |
| 168 | // The actual value to use, which can be specified per-device via property af.fast_track_multiplier. |
| 169 | static int sFastTrackMultiplier = kFastTrackMultiplier; |
| 170 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 171 | // See Thread::readOnlyHeap(). |
| 172 | // Initially this heap is used to allocate client buffers for "fast" AudioRecord. |
| 173 | // Eventually it will be the single buffer that FastCapture writes into via HAL read(), |
| 174 | // 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] | 175 | static const size_t kRecordThreadReadOnlyHeapSize = 0x2000; |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 176 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 177 | // ---------------------------------------------------------------------------- |
| 178 | |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 179 | static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT; |
| 180 | |
| 181 | static void sFastTrackMultiplierInit() |
| 182 | { |
| 183 | char value[PROPERTY_VALUE_MAX]; |
| 184 | if (property_get("af.fast_track_multiplier", value, NULL) > 0) { |
| 185 | char *endptr; |
| 186 | unsigned long ul = strtoul(value, &endptr, 0); |
| 187 | if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) { |
| 188 | sFastTrackMultiplier = (int) ul; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 195 | #ifdef ADD_BATTERY_DATA |
| 196 | // To collect the amplifier usage |
| 197 | static void addBatteryData(uint32_t params) { |
| 198 | sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService(); |
| 199 | if (service == NULL) { |
| 200 | // it already logged |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | service->addBatteryData(params); |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | |
| 209 | // ---------------------------------------------------------------------------- |
| 210 | // CPU Stats |
| 211 | // ---------------------------------------------------------------------------- |
| 212 | |
| 213 | class CpuStats { |
| 214 | public: |
| 215 | CpuStats(); |
| 216 | void sample(const String8 &title); |
| 217 | #ifdef DEBUG_CPU_USAGE |
| 218 | private: |
| 219 | ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns |
| 220 | CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns |
| 221 | |
| 222 | CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles |
| 223 | |
| 224 | int mCpuNum; // thread's current CPU number |
| 225 | int mCpukHz; // frequency of thread's current CPU in kHz |
| 226 | #endif |
| 227 | }; |
| 228 | |
| 229 | CpuStats::CpuStats() |
| 230 | #ifdef DEBUG_CPU_USAGE |
| 231 | : mCpuNum(-1), mCpukHz(-1) |
| 232 | #endif |
| 233 | { |
| 234 | } |
| 235 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 236 | void CpuStats::sample(const String8 &title |
| 237 | #ifndef DEBUG_CPU_USAGE |
| 238 | __unused |
| 239 | #endif |
| 240 | ) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 241 | #ifdef DEBUG_CPU_USAGE |
| 242 | // get current thread's delta CPU time in wall clock ns |
| 243 | double wcNs; |
| 244 | bool valid = mCpuUsage.sampleAndEnable(wcNs); |
| 245 | |
| 246 | // record sample for wall clock statistics |
| 247 | if (valid) { |
| 248 | mWcStats.sample(wcNs); |
| 249 | } |
| 250 | |
| 251 | // get the current CPU number |
| 252 | int cpuNum = sched_getcpu(); |
| 253 | |
| 254 | // get the current CPU frequency in kHz |
| 255 | int cpukHz = mCpuUsage.getCpukHz(cpuNum); |
| 256 | |
| 257 | // check if either CPU number or frequency changed |
| 258 | if (cpuNum != mCpuNum || cpukHz != mCpukHz) { |
| 259 | mCpuNum = cpuNum; |
| 260 | mCpukHz = cpukHz; |
| 261 | // ignore sample for purposes of cycles |
| 262 | valid = false; |
| 263 | } |
| 264 | |
| 265 | // if no change in CPU number or frequency, then record sample for cycle statistics |
| 266 | if (valid && mCpukHz > 0) { |
| 267 | double cycles = wcNs * cpukHz * 0.000001; |
| 268 | mHzStats.sample(cycles); |
| 269 | } |
| 270 | |
| 271 | unsigned n = mWcStats.n(); |
| 272 | // mCpuUsage.elapsed() is expensive, so don't call it every loop |
| 273 | if ((n & 127) == 1) { |
| 274 | long long elapsed = mCpuUsage.elapsed(); |
| 275 | if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) { |
| 276 | double perLoop = elapsed / (double) n; |
| 277 | double perLoop100 = perLoop * 0.01; |
| 278 | double perLoop1k = perLoop * 0.001; |
| 279 | double mean = mWcStats.mean(); |
| 280 | double stddev = mWcStats.stddev(); |
| 281 | double minimum = mWcStats.minimum(); |
| 282 | double maximum = mWcStats.maximum(); |
| 283 | double meanCycles = mHzStats.mean(); |
| 284 | double stddevCycles = mHzStats.stddev(); |
| 285 | double minCycles = mHzStats.minimum(); |
| 286 | double maxCycles = mHzStats.maximum(); |
| 287 | mCpuUsage.resetElapsed(); |
| 288 | mWcStats.reset(); |
| 289 | mHzStats.reset(); |
| 290 | ALOGD("CPU usage for %s over past %.1f secs\n" |
| 291 | " (%u mixer loops at %.1f mean ms per loop):\n" |
| 292 | " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n" |
| 293 | " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n" |
| 294 | " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f", |
| 295 | title.string(), |
| 296 | elapsed * .000000001, n, perLoop * .000001, |
| 297 | mean * .001, |
| 298 | stddev * .001, |
| 299 | minimum * .001, |
| 300 | maximum * .001, |
| 301 | mean / perLoop100, |
| 302 | stddev / perLoop100, |
| 303 | minimum / perLoop100, |
| 304 | maximum / perLoop100, |
| 305 | meanCycles / perLoop1k, |
| 306 | stddevCycles / perLoop1k, |
| 307 | minCycles / perLoop1k, |
| 308 | maxCycles / perLoop1k); |
| 309 | |
| 310 | } |
| 311 | } |
| 312 | #endif |
| 313 | }; |
| 314 | |
| 315 | // ---------------------------------------------------------------------------- |
| 316 | // ThreadBase |
| 317 | // ---------------------------------------------------------------------------- |
| 318 | |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 319 | // static |
| 320 | const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type) |
| 321 | { |
| 322 | switch (type) { |
| 323 | case MIXER: |
| 324 | return "MIXER"; |
| 325 | case DIRECT: |
| 326 | return "DIRECT"; |
| 327 | case DUPLICATING: |
| 328 | return "DUPLICATING"; |
| 329 | case RECORD: |
| 330 | return "RECORD"; |
| 331 | case OFFLOAD: |
| 332 | return "OFFLOAD"; |
| 333 | default: |
| 334 | return "unknown"; |
| 335 | } |
| 336 | } |
| 337 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 338 | String8 devicesToString(audio_devices_t devices) |
| 339 | { |
| 340 | static const struct mapping { |
| 341 | audio_devices_t mDevices; |
| 342 | const char * mString; |
| 343 | } mappingsOut[] = { |
| 344 | AUDIO_DEVICE_OUT_EARPIECE, "EARPIECE", |
| 345 | AUDIO_DEVICE_OUT_SPEAKER, "SPEAKER", |
| 346 | AUDIO_DEVICE_OUT_WIRED_HEADSET, "WIRED_HEADSET", |
| 347 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE, "WIRED_HEADPHONE", |
| 348 | AUDIO_DEVICE_OUT_TELEPHONY_TX, "TELEPHONY_TX", |
| 349 | AUDIO_DEVICE_NONE, "NONE", // must be last |
| 350 | }, mappingsIn[] = { |
| 351 | AUDIO_DEVICE_IN_BUILTIN_MIC, "BUILTIN_MIC", |
| 352 | AUDIO_DEVICE_IN_WIRED_HEADSET, "WIRED_HEADSET", |
| 353 | AUDIO_DEVICE_IN_VOICE_CALL, "VOICE_CALL", |
| 354 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "REMOTE_SUBMIX", |
| 355 | AUDIO_DEVICE_NONE, "NONE", // must be last |
| 356 | }; |
| 357 | String8 result; |
| 358 | audio_devices_t allDevices = AUDIO_DEVICE_NONE; |
| 359 | const mapping *entry; |
| 360 | if (devices & AUDIO_DEVICE_BIT_IN) { |
| 361 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 362 | entry = mappingsIn; |
| 363 | } else { |
| 364 | entry = mappingsOut; |
| 365 | } |
| 366 | for ( ; entry->mDevices != AUDIO_DEVICE_NONE; entry++) { |
| 367 | allDevices = (audio_devices_t) (allDevices | entry->mDevices); |
| 368 | if (devices & entry->mDevices) { |
| 369 | if (!result.isEmpty()) { |
| 370 | result.append("|"); |
| 371 | } |
| 372 | result.append(entry->mString); |
| 373 | } |
| 374 | } |
| 375 | if (devices & ~allDevices) { |
| 376 | if (!result.isEmpty()) { |
| 377 | result.append("|"); |
| 378 | } |
| 379 | result.appendFormat("0x%X", devices & ~allDevices); |
| 380 | } |
| 381 | if (result.isEmpty()) { |
| 382 | result.append(entry->mString); |
| 383 | } |
| 384 | return result; |
| 385 | } |
| 386 | |
| 387 | String8 inputFlagsToString(audio_input_flags_t flags) |
| 388 | { |
| 389 | static const struct mapping { |
| 390 | audio_input_flags_t mFlag; |
| 391 | const char * mString; |
| 392 | } mappings[] = { |
| 393 | AUDIO_INPUT_FLAG_FAST, "FAST", |
| 394 | AUDIO_INPUT_FLAG_HW_HOTWORD, "HW_HOTWORD", |
| 395 | AUDIO_INPUT_FLAG_NONE, "NONE", // must be last |
| 396 | }; |
| 397 | String8 result; |
| 398 | audio_input_flags_t allFlags = AUDIO_INPUT_FLAG_NONE; |
| 399 | const mapping *entry; |
| 400 | for (entry = mappings; entry->mFlag != AUDIO_INPUT_FLAG_NONE; entry++) { |
| 401 | allFlags = (audio_input_flags_t) (allFlags | entry->mFlag); |
| 402 | if (flags & entry->mFlag) { |
| 403 | if (!result.isEmpty()) { |
| 404 | result.append("|"); |
| 405 | } |
| 406 | result.append(entry->mString); |
| 407 | } |
| 408 | } |
| 409 | if (flags & ~allFlags) { |
| 410 | if (!result.isEmpty()) { |
| 411 | result.append("|"); |
| 412 | } |
| 413 | result.appendFormat("0x%X", flags & ~allFlags); |
| 414 | } |
| 415 | if (result.isEmpty()) { |
| 416 | result.append(entry->mString); |
| 417 | } |
| 418 | return result; |
| 419 | } |
| 420 | |
| 421 | String8 outputFlagsToString(audio_output_flags_t flags) |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 422 | { |
| 423 | static const struct mapping { |
| 424 | audio_output_flags_t mFlag; |
| 425 | const char * mString; |
| 426 | } mappings[] = { |
| 427 | AUDIO_OUTPUT_FLAG_DIRECT, "DIRECT", |
| 428 | AUDIO_OUTPUT_FLAG_PRIMARY, "PRIMARY", |
| 429 | AUDIO_OUTPUT_FLAG_FAST, "FAST", |
| 430 | AUDIO_OUTPUT_FLAG_DEEP_BUFFER, "DEEP_BUFFER", |
Glenn Kasten | dfb0e11 | 2015-02-18 14:33:39 -0800 | [diff] [blame] | 431 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, "COMPRESS_OFFLOAD", |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 432 | AUDIO_OUTPUT_FLAG_NON_BLOCKING, "NON_BLOCKING", |
| 433 | AUDIO_OUTPUT_FLAG_HW_AV_SYNC, "HW_AV_SYNC", |
| 434 | AUDIO_OUTPUT_FLAG_NONE, "NONE", // must be last |
| 435 | }; |
| 436 | String8 result; |
| 437 | audio_output_flags_t allFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 438 | const mapping *entry; |
| 439 | for (entry = mappings; entry->mFlag != AUDIO_OUTPUT_FLAG_NONE; entry++) { |
| 440 | allFlags = (audio_output_flags_t) (allFlags | entry->mFlag); |
| 441 | if (flags & entry->mFlag) { |
| 442 | if (!result.isEmpty()) { |
| 443 | result.append("|"); |
| 444 | } |
| 445 | result.append(entry->mString); |
| 446 | } |
| 447 | } |
| 448 | if (flags & ~allFlags) { |
| 449 | if (!result.isEmpty()) { |
| 450 | result.append("|"); |
| 451 | } |
| 452 | result.appendFormat("0x%X", flags & ~allFlags); |
| 453 | } |
| 454 | if (result.isEmpty()) { |
| 455 | result.append(entry->mString); |
| 456 | } |
| 457 | return result; |
| 458 | } |
| 459 | |
Glenn Kasten | 0f5b562 | 2015-02-18 14:33:30 -0800 | [diff] [blame] | 460 | const char *sourceToString(audio_source_t source) |
| 461 | { |
| 462 | switch (source) { |
| 463 | case AUDIO_SOURCE_DEFAULT: return "default"; |
| 464 | case AUDIO_SOURCE_MIC: return "mic"; |
| 465 | case AUDIO_SOURCE_VOICE_UPLINK: return "voice uplink"; |
| 466 | case AUDIO_SOURCE_VOICE_DOWNLINK: return "voice downlink"; |
| 467 | case AUDIO_SOURCE_VOICE_CALL: return "voice call"; |
| 468 | case AUDIO_SOURCE_CAMCORDER: return "camcorder"; |
| 469 | case AUDIO_SOURCE_VOICE_RECOGNITION: return "voice recognition"; |
| 470 | case AUDIO_SOURCE_VOICE_COMMUNICATION: return "voice communication"; |
| 471 | case AUDIO_SOURCE_REMOTE_SUBMIX: return "remote submix"; |
| 472 | case AUDIO_SOURCE_FM_TUNER: return "FM tuner"; |
| 473 | case AUDIO_SOURCE_HOTWORD: return "hotword"; |
| 474 | default: return "unknown"; |
| 475 | } |
| 476 | } |
| 477 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 478 | AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, |
| 479 | audio_devices_t outDevice, audio_devices_t inDevice, type_t type) |
| 480 | : Thread(false /*canCallJava*/), |
| 481 | mType(type), |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 482 | mAudioFlinger(audioFlinger), |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 483 | // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 484 | // are set by PlaybackThread::readOutputParameters_l() or |
| 485 | // RecordThread::readInputParameters_l() |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 486 | //FIXME: mStandby should be true here. Is this some kind of hack? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 487 | mStandby(false), mOutDevice(outDevice), mInDevice(inDevice), |
| 488 | mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id), |
| 489 | // mName will be set by concrete (non-virtual) subclass |
| 490 | mDeathRecipient(new PMDeathRecipient(this)) |
| 491 | { |
| 492 | } |
| 493 | |
| 494 | AudioFlinger::ThreadBase::~ThreadBase() |
| 495 | { |
Glenn Kasten | c6ae3c8 | 2013-07-17 09:08:51 -0700 | [diff] [blame] | 496 | // 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] | 497 | mConfigEvents.clear(); |
| 498 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | // do not lock the mutex in destructor |
| 500 | releaseWakeLock_l(); |
| 501 | if (mPowerManager != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 502 | sp<IBinder> binder = IInterface::asBinder(mPowerManager); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 503 | binder->unlinkToDeath(mDeathRecipient); |
| 504 | } |
| 505 | } |
| 506 | |
Glenn Kasten | cf04c2c | 2013-08-06 07:41:16 -0700 | [diff] [blame] | 507 | status_t AudioFlinger::ThreadBase::readyToRun() |
| 508 | { |
| 509 | status_t status = initCheck(); |
| 510 | if (status == NO_ERROR) { |
| 511 | ALOGI("AudioFlinger's thread %p ready to run", this); |
| 512 | } else { |
| 513 | ALOGE("No working audio driver found."); |
| 514 | } |
| 515 | return status; |
| 516 | } |
| 517 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 518 | void AudioFlinger::ThreadBase::exit() |
| 519 | { |
| 520 | ALOGV("ThreadBase::exit"); |
| 521 | // do any cleanup required for exit to succeed |
| 522 | preExit(); |
| 523 | { |
| 524 | // This lock prevents the following race in thread (uniprocessor for illustration): |
| 525 | // if (!exitPending()) { |
| 526 | // // context switch from here to exit() |
| 527 | // // exit() calls requestExit(), what exitPending() observes |
| 528 | // // exit() calls signal(), which is dropped since no waiters |
| 529 | // // context switch back from exit() to here |
| 530 | // mWaitWorkCV.wait(...); |
| 531 | // // now thread is hung |
| 532 | // } |
| 533 | AutoMutex lock(mLock); |
| 534 | requestExit(); |
| 535 | mWaitWorkCV.broadcast(); |
| 536 | } |
| 537 | // When Thread::requestExitAndWait is made virtual and this method is renamed to |
| 538 | // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();" |
| 539 | requestExitAndWait(); |
| 540 | } |
| 541 | |
| 542 | status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs) |
| 543 | { |
| 544 | status_t status; |
| 545 | |
| 546 | ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string()); |
| 547 | Mutex::Autolock _l(mLock); |
| 548 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 549 | return sendSetParameterConfigEvent_l(keyValuePairs); |
| 550 | } |
| 551 | |
| 552 | // sendConfigEvent_l() must be called with ThreadBase::mLock held |
| 553 | // Can temporarily release the lock if waiting for a reply from processConfigEvents_l(). |
| 554 | status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event) |
| 555 | { |
| 556 | status_t status = NO_ERROR; |
| 557 | |
| 558 | mConfigEvents.add(event); |
| 559 | ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 560 | mWaitWorkCV.signal(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 561 | mLock.unlock(); |
| 562 | { |
| 563 | Mutex::Autolock _l(event->mLock); |
| 564 | while (event->mWaitStatus) { |
| 565 | if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) { |
| 566 | event->mStatus = TIMED_OUT; |
| 567 | event->mWaitStatus = false; |
| 568 | } |
| 569 | } |
| 570 | status = event->mStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 571 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 572 | mLock.lock(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 573 | return status; |
| 574 | } |
| 575 | |
| 576 | void AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param) |
| 577 | { |
| 578 | Mutex::Autolock _l(mLock); |
| 579 | sendIoConfigEvent_l(event, param); |
| 580 | } |
| 581 | |
| 582 | // sendIoConfigEvent_l() must be called with ThreadBase::mLock held |
| 583 | void AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param) |
| 584 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 585 | sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param); |
| 586 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held |
| 590 | void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio) |
| 591 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 592 | sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio); |
| 593 | sendConfigEvent_l(configEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 596 | // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held |
| 597 | status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 598 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 599 | sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair); |
| 600 | return sendConfigEvent_l(configEvent); |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 603 | status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent( |
| 604 | const struct audio_patch *patch, |
| 605 | audio_patch_handle_t *handle) |
| 606 | { |
| 607 | Mutex::Autolock _l(mLock); |
| 608 | sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle); |
| 609 | status_t status = sendConfigEvent_l(configEvent); |
| 610 | if (status == NO_ERROR) { |
| 611 | CreateAudioPatchConfigEventData *data = |
| 612 | (CreateAudioPatchConfigEventData *)configEvent->mData.get(); |
| 613 | *handle = data->mHandle; |
| 614 | } |
| 615 | return status; |
| 616 | } |
| 617 | |
| 618 | status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent( |
| 619 | const audio_patch_handle_t handle) |
| 620 | { |
| 621 | Mutex::Autolock _l(mLock); |
| 622 | sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle); |
| 623 | return sendConfigEvent_l(configEvent); |
| 624 | } |
| 625 | |
| 626 | |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 627 | // post condition: mConfigEvents.isEmpty() |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 628 | void AudioFlinger::ThreadBase::processConfigEvents_l() |
Glenn Kasten | f777331 | 2013-08-13 16:00:42 -0700 | [diff] [blame] | 629 | { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 630 | bool configChanged = false; |
| 631 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 632 | while (!mConfigEvents.isEmpty()) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 633 | ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size()); |
| 634 | sp<ConfigEvent> event = mConfigEvents[0]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 635 | mConfigEvents.removeAt(0); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 636 | switch (event->mType) { |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 637 | case CFG_EVENT_PRIO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 638 | PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get(); |
| 639 | // FIXME Need to understand why this has to be done asynchronously |
| 640 | int err = requestPriority(data->mPid, data->mTid, data->mPrio, |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 641 | true /*asynchronous*/); |
| 642 | if (err != 0) { |
| 643 | 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] | 644 | data->mPrio, data->mPid, data->mTid, err); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 645 | } |
| 646 | } break; |
| 647 | case CFG_EVENT_IO: { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 648 | IoConfigEventData *data = (IoConfigEventData *)event->mData.get(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 649 | audioConfigChanged(data->mEvent, data->mParam); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 650 | } break; |
| 651 | case CFG_EVENT_SET_PARAMETER: { |
| 652 | SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get(); |
| 653 | if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) { |
| 654 | configChanged = true; |
Glenn Kasten | d5418eb | 2013-08-14 13:11:06 -0700 | [diff] [blame] | 655 | } |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 656 | } break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 657 | case CFG_EVENT_CREATE_AUDIO_PATCH: { |
| 658 | CreateAudioPatchConfigEventData *data = |
| 659 | (CreateAudioPatchConfigEventData *)event->mData.get(); |
| 660 | event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle); |
| 661 | } break; |
| 662 | case CFG_EVENT_RELEASE_AUDIO_PATCH: { |
| 663 | ReleaseAudioPatchConfigEventData *data = |
| 664 | (ReleaseAudioPatchConfigEventData *)event->mData.get(); |
| 665 | event->mStatus = releaseAudioPatch_l(data->mHandle); |
| 666 | } break; |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 667 | default: |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 668 | ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType); |
Glenn Kasten | 3468e8a | 2013-08-13 16:01:22 -0700 | [diff] [blame] | 669 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 670 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 671 | { |
| 672 | Mutex::Autolock _l(event->mLock); |
| 673 | if (event->mWaitStatus) { |
| 674 | event->mWaitStatus = false; |
| 675 | event->mCond.signal(); |
| 676 | } |
| 677 | } |
| 678 | ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this); |
| 679 | } |
| 680 | |
| 681 | if (configChanged) { |
| 682 | cacheParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 683 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 686 | String8 channelMaskToString(audio_channel_mask_t mask, bool output) { |
| 687 | String8 s; |
| 688 | if (output) { |
| 689 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, "); |
| 690 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, "); |
| 691 | if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, "); |
| 692 | if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, "); |
| 693 | if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, "); |
| 694 | if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, "); |
| 695 | if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, "); |
| 696 | if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, "); |
| 697 | if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, "); |
| 698 | if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, "); |
| 699 | if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, "); |
| 700 | if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,"); |
| 701 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, "); |
| 702 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, "); |
| 703 | if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, "); |
| 704 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, "); |
| 705 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " ); |
| 706 | if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " ); |
| 707 | if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, "); |
| 708 | } else { |
| 709 | if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, "); |
| 710 | if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, "); |
| 711 | if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, "); |
| 712 | if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, "); |
| 713 | if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, "); |
| 714 | if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, "); |
| 715 | if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, "); |
| 716 | if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, "); |
| 717 | if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, "); |
| 718 | if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, "); |
| 719 | if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, "); |
| 720 | if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, "); |
| 721 | if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, "); |
| 722 | if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, "); |
| 723 | if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, "); |
| 724 | } |
| 725 | int len = s.length(); |
| 726 | if (s.length() > 2) { |
| 727 | char *str = s.lockBuffer(len); |
| 728 | s.unlockBuffer(len - 2); |
| 729 | } |
| 730 | return s; |
| 731 | } |
| 732 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 733 | void AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 734 | { |
| 735 | const size_t SIZE = 256; |
| 736 | char buffer[SIZE]; |
| 737 | String8 result; |
| 738 | |
| 739 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 740 | if (!locked) { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 741 | dprintf(fd, "thread %p may be deadlocked\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame^] | 744 | dprintf(fd, " Thread name: %s\n", mThreadName); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 745 | dprintf(fd, " I/O handle: %d\n", mId); |
| 746 | dprintf(fd, " TID: %d\n", getTid()); |
| 747 | dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no"); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 748 | dprintf(fd, " Sample rate: %u Hz\n", mSampleRate); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 749 | dprintf(fd, " HAL frame count: %zu\n", mFrameCount); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 750 | dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat)); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 751 | dprintf(fd, " HAL buffer size: %u bytes\n", mBufferSize); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 752 | dprintf(fd, " Channel count: %u\n", mChannelCount); |
| 753 | dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 754 | channelMaskToString(mChannelMask, mType != RECORD).string()); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 755 | dprintf(fd, " Format: 0x%x (%s)\n", mFormat, formatToString(mFormat)); |
| 756 | dprintf(fd, " Frame size: %zu bytes\n", mFrameSize); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 757 | dprintf(fd, " Pending config events:"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 758 | size_t numConfig = mConfigEvents.size(); |
| 759 | if (numConfig) { |
| 760 | for (size_t i = 0; i < numConfig; i++) { |
| 761 | mConfigEvents[i]->dump(buffer, SIZE); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 762 | dprintf(fd, "\n %s", buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 763 | } |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 764 | dprintf(fd, "\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 765 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 766 | dprintf(fd, " none\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 767 | } |
Glenn Kasten | 0b89bc0 | 2015-03-05 16:37:47 -0800 | [diff] [blame^] | 768 | dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, devicesToString(mOutDevice).string()); |
| 769 | dprintf(fd, " Input device: %#x (%s)\n", mInDevice, devicesToString(mInDevice).string()); |
| 770 | dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, sourceToString(mAudioSource)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 771 | |
| 772 | if (locked) { |
| 773 | mLock.unlock(); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | void AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args) |
| 778 | { |
| 779 | const size_t SIZE = 256; |
| 780 | char buffer[SIZE]; |
| 781 | String8 result; |
| 782 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 783 | size_t numEffectChains = mEffectChains.size(); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 784 | snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 785 | write(fd, buffer, strlen(buffer)); |
| 786 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 787 | for (size_t i = 0; i < numEffectChains; ++i) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 788 | sp<EffectChain> chain = mEffectChains[i]; |
| 789 | if (chain != 0) { |
| 790 | chain->dump(fd, args); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 795 | void AudioFlinger::ThreadBase::acquireWakeLock(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 796 | { |
| 797 | Mutex::Autolock _l(mLock); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 798 | acquireWakeLock_l(uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 801 | String16 AudioFlinger::ThreadBase::getWakeLockTag() |
| 802 | { |
| 803 | switch (mType) { |
Glenn Kasten | bcb1486 | 2015-03-05 17:11:21 -0800 | [diff] [blame] | 804 | case MIXER: |
| 805 | return String16("AudioMix"); |
| 806 | case DIRECT: |
| 807 | return String16("AudioDirectOut"); |
| 808 | case DUPLICATING: |
| 809 | return String16("AudioDup"); |
| 810 | case RECORD: |
| 811 | return String16("AudioIn"); |
| 812 | case OFFLOAD: |
| 813 | return String16("AudioOffload"); |
| 814 | default: |
| 815 | ALOG_ASSERT(false); |
| 816 | return String16("AudioUnknown"); |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 820 | void AudioFlinger::ThreadBase::acquireWakeLock_l(int uid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 821 | { |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 822 | getPowerManager_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 823 | if (mPowerManager != 0) { |
| 824 | sp<IBinder> binder = new BBinder(); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 825 | status_t status; |
| 826 | if (uid >= 0) { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 827 | status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 828 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 829 | getWakeLockTag(), |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 830 | String16("media"), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 831 | uid, |
| 832 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 833 | } else { |
Eric Laurent | 547789d | 2013-10-04 11:46:55 -0700 | [diff] [blame] | 834 | status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK, |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 835 | binder, |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 836 | getWakeLockTag(), |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 837 | String16("media"), |
| 838 | true /* FIXME force oneway contrary to .aidl */); |
Marco Nelissen | e14a5d6 | 2013-10-03 08:51:24 -0700 | [diff] [blame] | 839 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 840 | if (status == NO_ERROR) { |
| 841 | mWakeLockToken = binder; |
| 842 | } |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 843 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | void AudioFlinger::ThreadBase::releaseWakeLock() |
| 848 | { |
| 849 | Mutex::Autolock _l(mLock); |
| 850 | releaseWakeLock_l(); |
| 851 | } |
| 852 | |
| 853 | void AudioFlinger::ThreadBase::releaseWakeLock_l() |
| 854 | { |
| 855 | if (mWakeLockToken != 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 856 | ALOGV("releaseWakeLock_l() %s", mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 857 | if (mPowerManager != 0) { |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 858 | mPowerManager->releaseWakeLock(mWakeLockToken, 0, |
| 859 | true /* FIXME force oneway contrary to .aidl */); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 860 | } |
| 861 | mWakeLockToken.clear(); |
| 862 | } |
| 863 | } |
| 864 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 865 | void AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) { |
| 866 | Mutex::Autolock _l(mLock); |
| 867 | updateWakeLockUids_l(uids); |
| 868 | } |
| 869 | |
| 870 | void AudioFlinger::ThreadBase::getPowerManager_l() { |
| 871 | |
| 872 | if (mPowerManager == 0) { |
| 873 | // use checkService() to avoid blocking if power service is not up yet |
| 874 | sp<IBinder> binder = |
| 875 | defaultServiceManager()->checkService(String16("power")); |
| 876 | if (binder == 0) { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 877 | ALOGW("Thread %s cannot connect to the power manager service", mThreadName); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 878 | } else { |
| 879 | mPowerManager = interface_cast<IPowerManager>(binder); |
| 880 | binder->linkToDeath(mDeathRecipient); |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) { |
| 886 | |
| 887 | getPowerManager_l(); |
| 888 | if (mWakeLockToken == NULL) { |
| 889 | ALOGE("no wake lock to update!"); |
| 890 | return; |
| 891 | } |
| 892 | if (mPowerManager != 0) { |
| 893 | sp<IBinder> binder = new BBinder(); |
| 894 | status_t status; |
Glenn Kasten | 3abc2de | 2014-09-05 16:45:52 -0700 | [diff] [blame] | 895 | status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(), |
| 896 | true /* FIXME force oneway contrary to .aidl */); |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 897 | ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 901 | void AudioFlinger::ThreadBase::clearPowerManager() |
| 902 | { |
| 903 | Mutex::Autolock _l(mLock); |
| 904 | releaseWakeLock_l(); |
| 905 | mPowerManager.clear(); |
| 906 | } |
| 907 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 908 | void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 909 | { |
| 910 | sp<ThreadBase> thread = mThread.promote(); |
| 911 | if (thread != 0) { |
| 912 | thread->clearPowerManager(); |
| 913 | } |
| 914 | ALOGW("power manager service died !!!"); |
| 915 | } |
| 916 | |
| 917 | void AudioFlinger::ThreadBase::setEffectSuspended( |
| 918 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 919 | { |
| 920 | Mutex::Autolock _l(mLock); |
| 921 | setEffectSuspended_l(type, suspend, sessionId); |
| 922 | } |
| 923 | |
| 924 | void AudioFlinger::ThreadBase::setEffectSuspended_l( |
| 925 | const effect_uuid_t *type, bool suspend, int sessionId) |
| 926 | { |
| 927 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 928 | if (chain != 0) { |
| 929 | if (type != NULL) { |
| 930 | chain->setEffectSuspended_l(type, suspend); |
| 931 | } else { |
| 932 | chain->setEffectSuspendedAll_l(suspend); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | updateSuspendedSessions_l(type, suspend, sessionId); |
| 937 | } |
| 938 | |
| 939 | void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain) |
| 940 | { |
| 941 | ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId()); |
| 942 | if (index < 0) { |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects = |
| 947 | mSuspendedSessions.valueAt(index); |
| 948 | |
| 949 | for (size_t i = 0; i < sessionEffects.size(); i++) { |
| 950 | sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i); |
| 951 | for (int j = 0; j < desc->mRefCount; j++) { |
| 952 | if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) { |
| 953 | chain->setEffectSuspendedAll_l(true); |
| 954 | } else { |
| 955 | ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x", |
| 956 | desc->mType.timeLow); |
| 957 | chain->setEffectSuspended_l(&desc->mType, true); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type, |
| 964 | bool suspend, |
| 965 | int sessionId) |
| 966 | { |
| 967 | ssize_t index = mSuspendedSessions.indexOfKey(sessionId); |
| 968 | |
| 969 | KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects; |
| 970 | |
| 971 | if (suspend) { |
| 972 | if (index >= 0) { |
| 973 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 974 | } else { |
| 975 | mSuspendedSessions.add(sessionId, sessionEffects); |
| 976 | } |
| 977 | } else { |
| 978 | if (index < 0) { |
| 979 | return; |
| 980 | } |
| 981 | sessionEffects = mSuspendedSessions.valueAt(index); |
| 982 | } |
| 983 | |
| 984 | |
| 985 | int key = EffectChain::kKeyForSuspendAll; |
| 986 | if (type != NULL) { |
| 987 | key = type->timeLow; |
| 988 | } |
| 989 | index = sessionEffects.indexOfKey(key); |
| 990 | |
| 991 | sp<SuspendedSessionDesc> desc; |
| 992 | if (suspend) { |
| 993 | if (index >= 0) { |
| 994 | desc = sessionEffects.valueAt(index); |
| 995 | } else { |
| 996 | desc = new SuspendedSessionDesc(); |
| 997 | if (type != NULL) { |
| 998 | desc->mType = *type; |
| 999 | } |
| 1000 | sessionEffects.add(key, desc); |
| 1001 | ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key); |
| 1002 | } |
| 1003 | desc->mRefCount++; |
| 1004 | } else { |
| 1005 | if (index < 0) { |
| 1006 | return; |
| 1007 | } |
| 1008 | desc = sessionEffects.valueAt(index); |
| 1009 | if (--desc->mRefCount == 0) { |
| 1010 | ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key); |
| 1011 | sessionEffects.removeItemsAt(index); |
| 1012 | if (sessionEffects.isEmpty()) { |
| 1013 | ALOGV("updateSuspendedSessions_l() restore removing session %d", |
| 1014 | sessionId); |
| 1015 | mSuspendedSessions.removeItem(sessionId); |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | if (!sessionEffects.isEmpty()) { |
| 1020 | mSuspendedSessions.replaceValueFor(sessionId, sessionEffects); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 1025 | bool enabled, |
| 1026 | int sessionId) |
| 1027 | { |
| 1028 | Mutex::Autolock _l(mLock); |
| 1029 | checkSuspendOnEffectEnabled_l(effect, enabled, sessionId); |
| 1030 | } |
| 1031 | |
| 1032 | void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, |
| 1033 | bool enabled, |
| 1034 | int sessionId) |
| 1035 | { |
| 1036 | if (mType != RECORD) { |
| 1037 | // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on |
| 1038 | // another session. This gives the priority to well behaved effect control panels |
| 1039 | // and applications not using global effects. |
| 1040 | // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect |
| 1041 | // global effects |
| 1042 | if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) { |
| 1043 | setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1048 | if (chain != 0) { |
| 1049 | chain->checkSuspendOnEffectEnabled(effect, enabled); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held |
| 1054 | sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l( |
| 1055 | const sp<AudioFlinger::Client>& client, |
| 1056 | const sp<IEffectClient>& effectClient, |
| 1057 | int32_t priority, |
| 1058 | int sessionId, |
| 1059 | effect_descriptor_t *desc, |
| 1060 | int *enabled, |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1061 | status_t *status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1062 | { |
| 1063 | sp<EffectModule> effect; |
| 1064 | sp<EffectHandle> handle; |
| 1065 | status_t lStatus; |
| 1066 | sp<EffectChain> chain; |
| 1067 | bool chainCreated = false; |
| 1068 | bool effectCreated = false; |
| 1069 | bool effectRegistered = false; |
| 1070 | |
| 1071 | lStatus = initCheck(); |
| 1072 | if (lStatus != NO_ERROR) { |
| 1073 | ALOGW("createEffect_l() Audio driver not initialized."); |
| 1074 | goto Exit; |
| 1075 | } |
| 1076 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1077 | // Reject any effect on Direct output threads for now, since the format of |
| 1078 | // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo). |
| 1079 | if (mType == DIRECT) { |
| 1080 | 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] | 1081 | desc->name, mThreadName); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1082 | lStatus = BAD_VALUE; |
| 1083 | goto Exit; |
| 1084 | } |
| 1085 | |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1086 | // Reject any effect on mixer or duplicating multichannel sinks. |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1087 | // TODO: fix both format and multichannel issues with effects. |
Andy Hung | 389cfdb | 2014-08-07 17:49:53 -0700 | [diff] [blame] | 1088 | if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) { |
| 1089 | ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads", |
| 1090 | desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING"); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1091 | lStatus = BAD_VALUE; |
| 1092 | goto Exit; |
| 1093 | } |
| 1094 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1095 | // Allow global effects only on offloaded and mixer threads |
| 1096 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 1097 | switch (mType) { |
| 1098 | case MIXER: |
| 1099 | case OFFLOAD: |
| 1100 | break; |
| 1101 | case DIRECT: |
| 1102 | case DUPLICATING: |
| 1103 | case RECORD: |
| 1104 | default: |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1105 | ALOGW("createEffect_l() Cannot add global effect %s on thread %s", |
| 1106 | desc->name, mThreadName); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1107 | lStatus = BAD_VALUE; |
| 1108 | goto Exit; |
| 1109 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1110 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1111 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1112 | // Only Pre processor effects are allowed on input threads and only on input threads |
| 1113 | if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) { |
| 1114 | ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d", |
| 1115 | desc->name, desc->flags, mType); |
| 1116 | lStatus = BAD_VALUE; |
| 1117 | goto Exit; |
| 1118 | } |
| 1119 | |
| 1120 | ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId); |
| 1121 | |
| 1122 | { // scope for mLock |
| 1123 | Mutex::Autolock _l(mLock); |
| 1124 | |
| 1125 | // check for existing effect chain with the requested audio session |
| 1126 | chain = getEffectChain_l(sessionId); |
| 1127 | if (chain == 0) { |
| 1128 | // create a new chain for this session |
| 1129 | ALOGV("createEffect_l() new effect chain for session %d", sessionId); |
| 1130 | chain = new EffectChain(this, sessionId); |
| 1131 | addEffectChain_l(chain); |
| 1132 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1133 | chainCreated = true; |
| 1134 | } else { |
| 1135 | effect = chain->getEffectFromDesc_l(desc); |
| 1136 | } |
| 1137 | |
| 1138 | ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get()); |
| 1139 | |
| 1140 | if (effect == 0) { |
| 1141 | int id = mAudioFlinger->nextUniqueId(); |
| 1142 | // Check CPU and memory usage |
| 1143 | lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id); |
| 1144 | if (lStatus != NO_ERROR) { |
| 1145 | goto Exit; |
| 1146 | } |
| 1147 | effectRegistered = true; |
| 1148 | // create a new effect module if none present in the chain |
| 1149 | effect = new EffectModule(this, chain, desc, id, sessionId); |
| 1150 | lStatus = effect->status(); |
| 1151 | if (lStatus != NO_ERROR) { |
| 1152 | goto Exit; |
| 1153 | } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1154 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1155 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1156 | lStatus = chain->addEffect_l(effect); |
| 1157 | if (lStatus != NO_ERROR) { |
| 1158 | goto Exit; |
| 1159 | } |
| 1160 | effectCreated = true; |
| 1161 | |
| 1162 | effect->setDevice(mOutDevice); |
| 1163 | effect->setDevice(mInDevice); |
| 1164 | effect->setMode(mAudioFlinger->getMode()); |
| 1165 | effect->setAudioSource(mAudioSource); |
| 1166 | } |
| 1167 | // create effect handle and connect it to effect module |
| 1168 | handle = new EffectHandle(effect, client, effectClient, priority); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1169 | lStatus = handle->initCheck(); |
| 1170 | if (lStatus == OK) { |
| 1171 | lStatus = effect->addHandle(handle.get()); |
| 1172 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1173 | if (enabled != NULL) { |
| 1174 | *enabled = (int)effect->isEnabled(); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | Exit: |
| 1179 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 1180 | Mutex::Autolock _l(mLock); |
| 1181 | if (effectCreated) { |
| 1182 | chain->removeEffect_l(effect); |
| 1183 | } |
| 1184 | if (effectRegistered) { |
| 1185 | AudioSystem::unregisterEffect(effect->id()); |
| 1186 | } |
| 1187 | if (chainCreated) { |
| 1188 | removeEffectChain_l(chain); |
| 1189 | } |
| 1190 | handle.clear(); |
| 1191 | } |
| 1192 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1193 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1194 | return handle; |
| 1195 | } |
| 1196 | |
| 1197 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId) |
| 1198 | { |
| 1199 | Mutex::Autolock _l(mLock); |
| 1200 | return getEffect_l(sessionId, effectId); |
| 1201 | } |
| 1202 | |
| 1203 | sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId) |
| 1204 | { |
| 1205 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1206 | return chain != 0 ? chain->getEffectFromId_l(effectId) : 0; |
| 1207 | } |
| 1208 | |
| 1209 | // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and |
| 1210 | // PlaybackThread::mLock held |
| 1211 | status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect) |
| 1212 | { |
| 1213 | // check for existing effect chain with the requested audio session |
| 1214 | int sessionId = effect->sessionId(); |
| 1215 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1216 | bool chainCreated = false; |
| 1217 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1218 | ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(), |
| 1219 | "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x", |
| 1220 | this, effect->desc().name, effect->desc().flags); |
| 1221 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1222 | if (chain == 0) { |
| 1223 | // create a new chain for this session |
| 1224 | ALOGV("addEffect_l() new effect chain for session %d", sessionId); |
| 1225 | chain = new EffectChain(this, sessionId); |
| 1226 | addEffectChain_l(chain); |
| 1227 | chain->setStrategy(getStrategyForSession_l(sessionId)); |
| 1228 | chainCreated = true; |
| 1229 | } |
| 1230 | ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get()); |
| 1231 | |
| 1232 | if (chain->getEffectFromId_l(effect->id()) != 0) { |
| 1233 | ALOGW("addEffect_l() %p effect %s already present in chain %p", |
| 1234 | this, effect->desc().name, chain.get()); |
| 1235 | return BAD_VALUE; |
| 1236 | } |
| 1237 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1238 | effect->setOffloaded(mType == OFFLOAD, mId); |
| 1239 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1240 | status_t status = chain->addEffect_l(effect); |
| 1241 | if (status != NO_ERROR) { |
| 1242 | if (chainCreated) { |
| 1243 | removeEffectChain_l(chain); |
| 1244 | } |
| 1245 | return status; |
| 1246 | } |
| 1247 | |
| 1248 | effect->setDevice(mOutDevice); |
| 1249 | effect->setDevice(mInDevice); |
| 1250 | effect->setMode(mAudioFlinger->getMode()); |
| 1251 | effect->setAudioSource(mAudioSource); |
| 1252 | return NO_ERROR; |
| 1253 | } |
| 1254 | |
| 1255 | void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) { |
| 1256 | |
| 1257 | ALOGV("removeEffect_l() %p effect %p", this, effect.get()); |
| 1258 | effect_descriptor_t desc = effect->desc(); |
| 1259 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1260 | detachAuxEffect_l(effect->id()); |
| 1261 | } |
| 1262 | |
| 1263 | sp<EffectChain> chain = effect->chain().promote(); |
| 1264 | if (chain != 0) { |
| 1265 | // remove effect chain if removing last effect |
| 1266 | if (chain->removeEffect_l(effect) == 0) { |
| 1267 | removeEffectChain_l(chain); |
| 1268 | } |
| 1269 | } else { |
| 1270 | ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get()); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | void AudioFlinger::ThreadBase::lockEffectChains_l( |
| 1275 | Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1276 | { |
| 1277 | effectChains = mEffectChains; |
| 1278 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 1279 | mEffectChains[i]->lock(); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | void AudioFlinger::ThreadBase::unlockEffectChains( |
| 1284 | const Vector< sp<AudioFlinger::EffectChain> >& effectChains) |
| 1285 | { |
| 1286 | for (size_t i = 0; i < effectChains.size(); i++) { |
| 1287 | effectChains[i]->unlock(); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId) |
| 1292 | { |
| 1293 | Mutex::Autolock _l(mLock); |
| 1294 | return getEffectChain_l(sessionId); |
| 1295 | } |
| 1296 | |
| 1297 | sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const |
| 1298 | { |
| 1299 | size_t size = mEffectChains.size(); |
| 1300 | for (size_t i = 0; i < size; i++) { |
| 1301 | if (mEffectChains[i]->sessionId() == sessionId) { |
| 1302 | return mEffectChains[i]; |
| 1303 | } |
| 1304 | } |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | void AudioFlinger::ThreadBase::setMode(audio_mode_t mode) |
| 1309 | { |
| 1310 | Mutex::Autolock _l(mLock); |
| 1311 | size_t size = mEffectChains.size(); |
| 1312 | for (size_t i = 0; i < size; i++) { |
| 1313 | mEffectChains[i]->setMode_l(mode); |
| 1314 | } |
| 1315 | } |
| 1316 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1317 | void AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config) |
| 1318 | { |
| 1319 | config->type = AUDIO_PORT_TYPE_MIX; |
| 1320 | config->ext.mix.handle = mId; |
| 1321 | config->sample_rate = mSampleRate; |
| 1322 | config->format = mFormat; |
| 1323 | config->channel_mask = mChannelMask; |
| 1324 | config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK| |
| 1325 | AUDIO_PORT_CONFIG_FORMAT; |
| 1326 | } |
| 1327 | |
| 1328 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1329 | // ---------------------------------------------------------------------------- |
| 1330 | // Playback |
| 1331 | // ---------------------------------------------------------------------------- |
| 1332 | |
| 1333 | AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, |
| 1334 | AudioStreamOut* output, |
| 1335 | audio_io_handle_t id, |
| 1336 | audio_devices_t device, |
| 1337 | type_t type) |
| 1338 | : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type), |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 1339 | mNormalFrameCount(0), mSinkBuffer(NULL), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1340 | mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1341 | mMixerBuffer(NULL), |
| 1342 | mMixerBufferSize(0), |
| 1343 | mMixerBufferFormat(AUDIO_FORMAT_INVALID), |
| 1344 | mMixerBufferValid(false), |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1345 | mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision), |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1346 | mEffectBuffer(NULL), |
| 1347 | mEffectBufferSize(0), |
| 1348 | mEffectBufferFormat(AUDIO_FORMAT_INVALID), |
| 1349 | mEffectBufferValid(false), |
Glenn Kasten | c1fac19 | 2013-08-06 07:41:36 -0700 | [diff] [blame] | 1350 | mSuspended(0), mBytesWritten(0), |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1351 | mActiveTracksGeneration(0), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1352 | // mStreamTypes[] initialized in constructor body |
| 1353 | mOutput(output), |
| 1354 | mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false), |
| 1355 | mMixerStatus(MIXER_IDLE), |
| 1356 | mMixerStatusIgnoringFastTracks(MIXER_IDLE), |
| 1357 | standbyDelay(AudioFlinger::mStandbyTimeInNsecs), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1358 | mBytesRemaining(0), |
| 1359 | mCurrentWriteLength(0), |
| 1360 | mUseAsyncWrite(false), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1361 | mWriteAckSequence(0), |
| 1362 | mDrainSequence(0), |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1363 | mSignalPending(false), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1364 | mScreenState(AudioFlinger::mScreenState), |
| 1365 | // index 0 is reserved for normal mixer's submix |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1366 | mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1), |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1367 | mHwSupportsPause(false), mHwPaused(false), mFlushPending(false), |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 1368 | // mLatchD, mLatchQ, |
| 1369 | mLatchDValid(false), mLatchQValid(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1370 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1371 | snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id); |
| 1372 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1373 | |
| 1374 | // Assumes constructor is called by AudioFlinger with it's mLock held, but |
| 1375 | // it would be safer to explicitly pass initial masterVolume/masterMute as |
| 1376 | // parameter. |
| 1377 | // |
| 1378 | // If the HAL we are using has support for master volume or master mute, |
| 1379 | // then do not attenuate or mute during mixing (just leave the volume at 1.0 |
| 1380 | // and the mute set to false). |
| 1381 | mMasterVolume = audioFlinger->masterVolume_l(); |
| 1382 | mMasterMute = audioFlinger->masterMute_l(); |
| 1383 | if (mOutput && mOutput->audioHwDev) { |
| 1384 | if (mOutput->audioHwDev->canSetMasterVolume()) { |
| 1385 | mMasterVolume = 1.0; |
| 1386 | } |
| 1387 | |
| 1388 | if (mOutput->audioHwDev->canSetMasterMute()) { |
| 1389 | mMasterMute = false; |
| 1390 | } |
| 1391 | } |
| 1392 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1393 | readOutputParameters_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1394 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1395 | // ++ operator does not compile |
Glenn Kasten | 66e4635 | 2014-01-16 17:44:23 -0800 | [diff] [blame] | 1396 | 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] | 1397 | stream = (audio_stream_type_t) (stream + 1)) { |
| 1398 | mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream); |
| 1399 | mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream); |
| 1400 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | AudioFlinger::PlaybackThread::~PlaybackThread() |
| 1404 | { |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1405 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1406 | free(mSinkBuffer); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1407 | free(mMixerBuffer); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1408 | free(mEffectBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args) |
| 1412 | { |
| 1413 | dumpInternals(fd, args); |
| 1414 | dumpTracks(fd, args); |
| 1415 | dumpEffectChains(fd, args); |
| 1416 | } |
| 1417 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1418 | void AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1419 | { |
| 1420 | const size_t SIZE = 256; |
| 1421 | char buffer[SIZE]; |
| 1422 | String8 result; |
| 1423 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1424 | result.appendFormat(" Stream volumes in dB: "); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1425 | for (int i = 0; i < AUDIO_STREAM_CNT; ++i) { |
| 1426 | const stream_type_t *st = &mStreamTypes[i]; |
| 1427 | if (i > 0) { |
| 1428 | result.appendFormat(", "); |
| 1429 | } |
| 1430 | result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume)); |
| 1431 | if (st->mute) { |
| 1432 | result.append("M"); |
| 1433 | } |
| 1434 | } |
| 1435 | result.append("\n"); |
| 1436 | write(fd, result.string(), result.length()); |
| 1437 | result.clear(); |
| 1438 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1439 | // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way. |
| 1440 | FastTrackUnderruns underruns = getFastTrackUnderruns(0); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1441 | dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1443 | |
| 1444 | size_t numtracks = mTracks.size(); |
| 1445 | size_t numactive = mActiveTracks.size(); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1446 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1447 | size_t numactiveseen = 0; |
| 1448 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1449 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1450 | Track::appendDumpHeader(result); |
| 1451 | for (size_t i = 0; i < numtracks; ++i) { |
| 1452 | sp<Track> track = mTracks[i]; |
| 1453 | if (track != 0) { |
| 1454 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 1455 | if (active) { |
| 1456 | numactiveseen++; |
| 1457 | } |
| 1458 | track->dump(buffer, SIZE, active); |
| 1459 | result.append(buffer); |
| 1460 | } |
| 1461 | } |
| 1462 | } else { |
| 1463 | result.append("\n"); |
| 1464 | } |
| 1465 | if (numactiveseen != numactive) { |
| 1466 | // some tracks in the active list were not in the tracks list |
| 1467 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 1468 | " not in the track list\n"); |
| 1469 | result.append(buffer); |
| 1470 | Track::appendDumpHeader(result); |
| 1471 | for (size_t i = 0; i < numactive; ++i) { |
| 1472 | sp<Track> track = mActiveTracks[i].promote(); |
| 1473 | if (track != 0 && mTracks.indexOf(track) < 0) { |
| 1474 | track->dump(buffer, SIZE, true); |
| 1475 | result.append(buffer); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | write(fd, result.string(), result.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args) |
| 1484 | { |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1485 | dprintf(fd, "\nOutput thread %p type %d (%s):\n", this, type(), threadTypeToString(type())); |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 1486 | dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount); |
| 1487 | dprintf(fd, " Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime)); |
| 1488 | dprintf(fd, " Total writes: %d\n", mNumWrites); |
| 1489 | dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites); |
| 1490 | dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no"); |
| 1491 | dprintf(fd, " Suspend count: %d\n", mSuspended); |
| 1492 | dprintf(fd, " Sink buffer : %p\n", mSinkBuffer); |
| 1493 | dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer); |
| 1494 | dprintf(fd, " Effect buffer: %p\n", mEffectBuffer); |
| 1495 | dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask); |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 1496 | AudioStreamOut *output = mOutput; |
| 1497 | audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE; |
| 1498 | String8 flagsAsString = outputFlagsToString(flags); |
| 1499 | dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n", output, flags, flagsAsString.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1500 | |
| 1501 | dumpBase(fd, args); |
| 1502 | } |
| 1503 | |
| 1504 | // Thread virtuals |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1505 | |
| 1506 | void AudioFlinger::PlaybackThread::onFirstRef() |
| 1507 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 1508 | run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | // ThreadBase virtuals |
| 1512 | void AudioFlinger::PlaybackThread::preExit() |
| 1513 | { |
| 1514 | ALOGV(" preExit()"); |
| 1515 | // FIXME this is using hard-coded strings but in the future, this functionality will be |
| 1516 | // converted to use audio HAL extensions required to support tunneling |
| 1517 | mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1"); |
| 1518 | } |
| 1519 | |
| 1520 | // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held |
| 1521 | sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l( |
| 1522 | const sp<AudioFlinger::Client>& client, |
| 1523 | audio_stream_type_t streamType, |
| 1524 | uint32_t sampleRate, |
| 1525 | audio_format_t format, |
| 1526 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1527 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1528 | const sp<IMemory>& sharedBuffer, |
| 1529 | int sessionId, |
| 1530 | IAudioFlinger::track_flags_t *flags, |
| 1531 | pid_t tid, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1532 | int uid, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1533 | status_t *status) |
| 1534 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1535 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1536 | sp<Track> track; |
| 1537 | status_t lStatus; |
| 1538 | |
| 1539 | bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0; |
| 1540 | |
| 1541 | // client expresses a preference for FAST, but we get the final say |
| 1542 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 1543 | if ( |
| 1544 | // not timed |
| 1545 | (!isTimed) && |
| 1546 | // either of these use cases: |
| 1547 | ( |
| 1548 | // use case 1: shared buffer with any frame count |
| 1549 | ( |
| 1550 | (sharedBuffer != 0) |
| 1551 | ) || |
| 1552 | // use case 2: callback handler and frame count is default or at least as large as HAL |
| 1553 | ( |
| 1554 | (tid != -1) && |
| 1555 | ((frameCount == 0) || |
Glenn Kasten | b5fed68 | 2013-12-03 09:06:43 -0800 | [diff] [blame] | 1556 | (frameCount >= mFrameCount)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1557 | ) |
| 1558 | ) && |
| 1559 | // PCM data |
| 1560 | audio_is_linear_pcm(format) && |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1561 | // identical channel mask to sink, or mono in and stereo sink |
| 1562 | (channelMask == mChannelMask || |
| 1563 | (channelMask == AUDIO_CHANNEL_OUT_MONO && |
| 1564 | mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1565 | // hardware sample rate |
| 1566 | (sampleRate == mSampleRate) && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1567 | // normal mixer has an associated fast mixer |
| 1568 | hasFastMixer() && |
| 1569 | // there are sufficient fast track slots available |
| 1570 | (mFastTrackAvailMask != 0) |
| 1571 | // FIXME test that MixerThread for this fast track has a capable output HAL |
| 1572 | // FIXME add a permission test also? |
| 1573 | ) { |
| 1574 | // if frameCount not specified, then it defaults to fast mixer (HAL) frame count |
| 1575 | if (frameCount == 0) { |
Glenn Kasten | 0349009 | 2014-05-27 12:30:54 -0700 | [diff] [blame] | 1576 | // read the fast track multiplier property the first time it is needed |
| 1577 | int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit); |
| 1578 | if (ok != 0) { |
| 1579 | ALOGE("%s pthread_once failed: %d", __func__, ok); |
| 1580 | } |
| 1581 | frameCount = mFrameCount * sFastTrackMultiplier; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1582 | } |
| 1583 | ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d", |
| 1584 | frameCount, mFrameCount); |
| 1585 | } else { |
| 1586 | ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d " |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1587 | "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x " |
| 1588 | "sampleRate=%u mSampleRate=%u " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1589 | "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x", |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 1590 | isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1591 | audio_is_linear_pcm(format), |
| 1592 | channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask); |
| 1593 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | // For normal PCM streaming tracks, update minimum frame count. |
| 1597 | // For compatibility with AudioTrack calculation, buffer depth is forced |
| 1598 | // to be at least 2 x the normal mixer frame count and cover audio hardware latency. |
| 1599 | // This is probably too conservative, but legacy application code may depend on it. |
| 1600 | // If you change this calculation, also review the start threshold which is related. |
| 1601 | if (!(*flags & IAudioFlinger::TRACK_FAST) |
| 1602 | && audio_is_linear_pcm(format) && sharedBuffer == 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1603 | uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream); |
| 1604 | uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate); |
| 1605 | if (minBufCount < 2) { |
| 1606 | minBufCount = 2; |
| 1607 | } |
Andy Hung | 0e48d25 | 2015-01-26 11:43:15 -0800 | [diff] [blame] | 1608 | size_t minFrameCount = |
| 1609 | minBufCount * sourceFramesNeeded(sampleRate, mNormalFrameCount, mSampleRate); |
| 1610 | if (frameCount < minFrameCount) { // including frameCount == 0 |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1611 | frameCount = minFrameCount; |
| 1612 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1613 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1614 | *pFrameCount = frameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1615 | |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1616 | switch (mType) { |
| 1617 | |
| 1618 | case DIRECT: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1619 | if (audio_is_linear_pcm(format)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1620 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1621 | ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x " |
| 1622 | "for output %p with format %#x", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1623 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1624 | lStatus = BAD_VALUE; |
| 1625 | goto Exit; |
| 1626 | } |
| 1627 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1628 | break; |
| 1629 | |
| 1630 | case OFFLOAD: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1631 | if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1632 | ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \"" |
| 1633 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1634 | sampleRate, format, channelMask, mOutput, mFormat); |
| 1635 | lStatus = BAD_VALUE; |
| 1636 | goto Exit; |
| 1637 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1638 | break; |
| 1639 | |
| 1640 | default: |
Glenn Kasten | 993fa06 | 2014-05-02 11:14:34 -0700 | [diff] [blame] | 1641 | if (!audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1642 | ALOGE("createTrack_l() Bad parameter: format %#x \"" |
| 1643 | "for output %p with format %#x", |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1644 | format, mOutput, mFormat); |
| 1645 | lStatus = BAD_VALUE; |
| 1646 | goto Exit; |
| 1647 | } |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 1648 | if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1649 | ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate); |
| 1650 | lStatus = BAD_VALUE; |
| 1651 | goto Exit; |
| 1652 | } |
Glenn Kasten | c3df838 | 2014-03-13 15:05:25 -0700 | [diff] [blame] | 1653 | break; |
| 1654 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | lStatus = initCheck(); |
| 1658 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1659 | ALOGE("createTrack_l() audio driver not initialized"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1660 | goto Exit; |
| 1661 | } |
| 1662 | |
| 1663 | { // scope for mLock |
| 1664 | Mutex::Autolock _l(mLock); |
| 1665 | |
| 1666 | // all tracks in same audio session must share the same routing strategy otherwise |
| 1667 | // conflicts will happen when tracks are moved from one output to another by audio policy |
| 1668 | // manager |
| 1669 | uint32_t strategy = AudioSystem::getStrategyForStream(streamType); |
| 1670 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 1671 | sp<Track> t = mTracks[i]; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1672 | if (t != 0 && t->isExternalTrack()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1673 | uint32_t actual = AudioSystem::getStrategyForStream(t->streamType()); |
| 1674 | if (sessionId == t->sessionId() && strategy != actual) { |
| 1675 | ALOGE("createTrack_l() mismatched strategy; expected %u but found %u", |
| 1676 | strategy, actual); |
| 1677 | lStatus = BAD_VALUE; |
| 1678 | goto Exit; |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (!isTimed) { |
| 1684 | track = new Track(this, client, streamType, sampleRate, format, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1685 | channelMask, frameCount, NULL, sharedBuffer, |
| 1686 | sessionId, uid, *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1687 | } else { |
| 1688 | track = TimedTrack::create(this, client, streamType, sampleRate, format, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1689 | channelMask, frameCount, sharedBuffer, sessionId, uid); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1690 | } |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 1691 | |
| 1692 | // new Track always returns non-NULL, |
| 1693 | // but TimedTrack::create() is a factory that could fail by returning NULL |
| 1694 | lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY; |
| 1695 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 0cde076 | 2014-01-16 15:06:36 -0800 | [diff] [blame] | 1696 | ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1697 | // 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] | 1698 | goto Exit; |
| 1699 | } |
| 1700 | mTracks.add(track); |
| 1701 | |
| 1702 | sp<EffectChain> chain = getEffectChain_l(sessionId); |
| 1703 | if (chain != 0) { |
| 1704 | ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer()); |
| 1705 | track->setMainBuffer(chain->inBuffer()); |
| 1706 | chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType())); |
| 1707 | chain->incTrackCnt(); |
| 1708 | } |
| 1709 | |
| 1710 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 1711 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1712 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 1713 | // so ask activity manager to do this on our behalf |
| 1714 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | lStatus = NO_ERROR; |
| 1719 | |
| 1720 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1721 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1722 | return track; |
| 1723 | } |
| 1724 | |
| 1725 | uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const |
| 1726 | { |
| 1727 | return latency; |
| 1728 | } |
| 1729 | |
| 1730 | uint32_t AudioFlinger::PlaybackThread::latency() const |
| 1731 | { |
| 1732 | Mutex::Autolock _l(mLock); |
| 1733 | return latency_l(); |
| 1734 | } |
| 1735 | uint32_t AudioFlinger::PlaybackThread::latency_l() const |
| 1736 | { |
| 1737 | if (initCheck() == NO_ERROR) { |
| 1738 | return correctLatency_l(mOutput->stream->get_latency(mOutput->stream)); |
| 1739 | } else { |
| 1740 | return 0; |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | void AudioFlinger::PlaybackThread::setMasterVolume(float value) |
| 1745 | { |
| 1746 | Mutex::Autolock _l(mLock); |
| 1747 | // Don't apply master volume in SW if our HAL can do it for us. |
| 1748 | if (mOutput && mOutput->audioHwDev && |
| 1749 | mOutput->audioHwDev->canSetMasterVolume()) { |
| 1750 | mMasterVolume = 1.0; |
| 1751 | } else { |
| 1752 | mMasterVolume = value; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | void AudioFlinger::PlaybackThread::setMasterMute(bool muted) |
| 1757 | { |
| 1758 | Mutex::Autolock _l(mLock); |
| 1759 | // Don't apply master mute in SW if our HAL can do it for us. |
| 1760 | if (mOutput && mOutput->audioHwDev && |
| 1761 | mOutput->audioHwDev->canSetMasterMute()) { |
| 1762 | mMasterMute = false; |
| 1763 | } else { |
| 1764 | mMasterMute = muted; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value) |
| 1769 | { |
| 1770 | Mutex::Autolock _l(mLock); |
| 1771 | mStreamTypes[stream].volume = value; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1772 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted) |
| 1776 | { |
| 1777 | Mutex::Autolock _l(mLock); |
| 1778 | mStreamTypes[stream].mute = muted; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1779 | broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const |
| 1783 | { |
| 1784 | Mutex::Autolock _l(mLock); |
| 1785 | return mStreamTypes[stream].volume; |
| 1786 | } |
| 1787 | |
| 1788 | // addTrack_l() must be called with ThreadBase::mLock held |
| 1789 | status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track) |
| 1790 | { |
| 1791 | status_t status = ALREADY_EXISTS; |
| 1792 | |
| 1793 | // set retry count for buffer fill |
| 1794 | track->mRetryCount = kMaxTrackStartupRetries; |
| 1795 | if (mActiveTracks.indexOf(track) < 0) { |
| 1796 | // the track is newly added, make sure it fills up all its |
| 1797 | // buffers before playing. This is to ensure the client will |
| 1798 | // effectively get the latency it requested. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1799 | if (track->isExternalTrack()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1800 | TrackBase::track_state state = track->mState; |
| 1801 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1802 | status = AudioSystem::startOutput(mId, track->streamType(), |
| 1803 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1804 | mLock.lock(); |
| 1805 | // abort track was stopped/paused while we released the lock |
| 1806 | if (state != track->mState) { |
| 1807 | if (status == NO_ERROR) { |
| 1808 | mLock.unlock(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1809 | AudioSystem::stopOutput(mId, track->streamType(), |
| 1810 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1811 | mLock.lock(); |
| 1812 | } |
| 1813 | return INVALID_OPERATION; |
| 1814 | } |
| 1815 | // abort if start is rejected by audio policy manager |
| 1816 | if (status != NO_ERROR) { |
| 1817 | return PERMISSION_DENIED; |
| 1818 | } |
| 1819 | #ifdef ADD_BATTERY_DATA |
| 1820 | // to track the speaker usage |
| 1821 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart); |
| 1822 | #endif |
| 1823 | } |
| 1824 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1825 | track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1826 | track->mResetDone = false; |
| 1827 | track->mPresentationCompleteFrames = 0; |
| 1828 | mActiveTracks.add(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1829 | mWakeLockUids.add(track->uid()); |
| 1830 | mActiveTracksGeneration++; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 1831 | mLatestActiveTrack = track; |
Eric Laurent | d0107bc | 2013-06-11 14:38:48 -0700 | [diff] [blame] | 1832 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1833 | if (chain != 0) { |
| 1834 | ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), |
| 1835 | track->sessionId()); |
| 1836 | chain->incActiveTrackCnt(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | status = NO_ERROR; |
| 1840 | } |
| 1841 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 1842 | onAddNewTrack_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1843 | return status; |
| 1844 | } |
| 1845 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1846 | bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1847 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1848 | track->terminate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1849 | // active tracks are removed by threadLoop() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1850 | bool trackActive = (mActiveTracks.indexOf(track) >= 0); |
| 1851 | track->mState = TrackBase::STOPPED; |
| 1852 | if (!trackActive) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1853 | removeTrack_l(track); |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1854 | } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1855 | track->mState = TrackBase::STOPPING_1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1856 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1857 | |
| 1858 | return trackActive; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track) |
| 1862 | { |
| 1863 | track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 1864 | mTracks.remove(track); |
| 1865 | deleteTrackName_l(track->name()); |
| 1866 | // redundant as track is about to be destroyed, for dumpsys only |
| 1867 | track->mName = -1; |
| 1868 | if (track->isFastTrack()) { |
| 1869 | int index = track->mFastIndex; |
| 1870 | ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks); |
| 1871 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index))); |
| 1872 | mFastTrackAvailMask |= 1 << index; |
| 1873 | // redundant as track is about to be destroyed, for dumpsys only |
| 1874 | track->mFastIndex = -1; |
| 1875 | } |
| 1876 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 1877 | if (chain != 0) { |
| 1878 | chain->decTrackCnt(); |
| 1879 | } |
| 1880 | } |
| 1881 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1882 | void AudioFlinger::PlaybackThread::broadcast_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1883 | { |
| 1884 | // Thread could be blocked waiting for async |
| 1885 | // so signal it to handle state changes immediately |
| 1886 | // If threadLoop is currently unlocked a signal of mWaitWorkCV will |
| 1887 | // be lost so we also flag to prevent it blocking on mWaitWorkCV |
| 1888 | mSignalPending = true; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1889 | mWaitWorkCV.broadcast(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 | String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys) |
| 1893 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1894 | Mutex::Autolock _l(mLock); |
| 1895 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1896 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 1899 | char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string()); |
| 1900 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1901 | free(s); |
| 1902 | return out_s8; |
| 1903 | } |
| 1904 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1905 | void AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1906 | AudioSystem::OutputDescriptor desc; |
| 1907 | void *param2 = NULL; |
| 1908 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1909 | ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1910 | param); |
| 1911 | |
| 1912 | switch (event) { |
| 1913 | case AudioSystem::OUTPUT_OPENED: |
| 1914 | case AudioSystem::OUTPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 1915 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1916 | desc.samplingRate = mSampleRate; |
| 1917 | desc.format = mFormat; |
| 1918 | desc.frameCount = mNormalFrameCount; // FIXME see |
| 1919 | // AudioFlinger::frameCount(audio_io_handle_t) |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 1920 | desc.latency = latency_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1921 | param2 = &desc; |
| 1922 | break; |
| 1923 | |
| 1924 | case AudioSystem::STREAM_CONFIG_CHANGED: |
| 1925 | param2 = ¶m; |
| 1926 | case AudioSystem::OUTPUT_CLOSED: |
| 1927 | default: |
| 1928 | break; |
| 1929 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1930 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1931 | } |
| 1932 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1933 | void AudioFlinger::PlaybackThread::writeCallback() |
| 1934 | { |
| 1935 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1936 | mCallbackThread->resetWriteBlocked(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | void AudioFlinger::PlaybackThread::drainCallback() |
| 1940 | { |
| 1941 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1942 | mCallbackThread->resetDraining(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1945 | void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1946 | { |
| 1947 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1948 | // reject out of sequence requests |
| 1949 | if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) { |
| 1950 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1951 | mWaitWorkCV.signal(); |
| 1952 | } |
| 1953 | } |
| 1954 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1955 | void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1956 | { |
| 1957 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1958 | // reject out of sequence requests |
| 1959 | if ((mDrainSequence & 1) && (sequence == mDrainSequence)) { |
| 1960 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1961 | mWaitWorkCV.signal(); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | // static |
| 1966 | int AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event, |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1967 | void *param __unused, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1968 | void *cookie) |
| 1969 | { |
| 1970 | AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie; |
| 1971 | ALOGV("asyncCallback() event %d", event); |
| 1972 | switch (event) { |
| 1973 | case STREAM_CBK_EVENT_WRITE_READY: |
| 1974 | me->writeCallback(); |
| 1975 | break; |
| 1976 | case STREAM_CBK_EVENT_DRAIN_READY: |
| 1977 | me->drainCallback(); |
| 1978 | break; |
| 1979 | default: |
| 1980 | ALOGW("asyncCallback() unknown event %d", event); |
| 1981 | break; |
| 1982 | } |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1986 | void AudioFlinger::PlaybackThread::readOutputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1987 | { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1988 | // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1989 | mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common); |
| 1990 | mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1991 | if (!audio_is_output_channel(mChannelMask)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 1992 | LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1993 | } |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1994 | if ((mType == MIXER || mType == DUPLICATING) |
| 1995 | && !isValidPcmSinkChannelMask(mChannelMask)) { |
| 1996 | LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output", |
| 1997 | mChannelMask); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 1998 | } |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 1999 | mChannelCount = audio_channel_count_from_out_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 2000 | mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common); |
| 2001 | mFormat = mHALFormat; |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2002 | if (!audio_is_valid_format(mFormat)) { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 2003 | LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2004 | } |
Andy Hung | 6146c08 | 2014-03-18 11:56:15 -0700 | [diff] [blame] | 2005 | if ((mType == MIXER || mType == DUPLICATING) |
| 2006 | && !isValidPcmSinkFormat(mFormat)) { |
| 2007 | LOG_FATAL("HAL format %#x not supported for mixed output", |
| 2008 | mFormat); |
Glenn Kasten | 7fc97ba | 2013-07-16 17:18:58 -0700 | [diff] [blame] | 2009 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 2010 | mFrameSize = audio_stream_out_frame_size(mOutput->stream); |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 2011 | mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common); |
| 2012 | mFrameCount = mBufferSize / mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2013 | if (mFrameCount & 15) { |
| 2014 | ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames", |
| 2015 | mFrameCount); |
| 2016 | } |
| 2017 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2018 | if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) && |
| 2019 | (mOutput->stream->set_callback != NULL)) { |
| 2020 | if (mOutput->stream->set_callback(mOutput->stream, |
| 2021 | AudioFlinger::PlaybackThread::asyncCallback, this) == 0) { |
| 2022 | mUseAsyncWrite = true; |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 2023 | mCallbackThread = new AudioFlinger::AsyncCallbackThread(this); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 2027 | mHwSupportsPause = false; |
| 2028 | if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2029 | if (mOutput->stream->pause != NULL) { |
| 2030 | if (mOutput->stream->resume != NULL) { |
| 2031 | mHwSupportsPause = true; |
| 2032 | } else { |
| 2033 | ALOGW("direct output implements pause but not resume"); |
| 2034 | } |
| 2035 | } else if (mOutput->stream->resume != NULL) { |
| 2036 | ALOGW("direct output implements resume but not pause"); |
| 2037 | } |
| 2038 | } |
| 2039 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2040 | if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) { |
| 2041 | // For best precision, we use float instead of the associated output |
| 2042 | // device format (typically PCM 16 bit). |
| 2043 | |
| 2044 | mFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 2045 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 2046 | mBufferSize = mFrameSize * mFrameCount; |
| 2047 | |
| 2048 | // TODO: We currently use the associated output device channel mask and sample rate. |
| 2049 | // (1) Perhaps use the ORed channel mask of all downstream MixerThreads |
| 2050 | // (if a valid mask) to avoid premature downmix. |
| 2051 | // (2) Perhaps use the maximum sample rate of all downstream MixerThreads |
| 2052 | // instead of the output device sample rate to avoid loss of high frequency information. |
| 2053 | // This may need to be updated as MixerThread/OutputTracks are added and not here. |
| 2054 | } |
| 2055 | |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2056 | // 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] | 2057 | double multiplier = 1.0; |
| 2058 | if (mType == MIXER && (kUseFastMixer == FastMixer_Static || |
| 2059 | kUseFastMixer == FastMixer_Dynamic)) { |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2060 | size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000; |
| 2061 | size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2062 | // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer |
| 2063 | minNormalFrameCount = (minNormalFrameCount + 15) & ~15; |
| 2064 | maxNormalFrameCount = maxNormalFrameCount & ~15; |
| 2065 | if (maxNormalFrameCount < minNormalFrameCount) { |
| 2066 | maxNormalFrameCount = minNormalFrameCount; |
| 2067 | } |
| 2068 | multiplier = (double) minNormalFrameCount / (double) mFrameCount; |
| 2069 | if (multiplier <= 1.0) { |
| 2070 | multiplier = 1.0; |
| 2071 | } else if (multiplier <= 2.0) { |
| 2072 | if (2 * mFrameCount <= maxNormalFrameCount) { |
| 2073 | multiplier = 2.0; |
| 2074 | } else { |
| 2075 | multiplier = (double) maxNormalFrameCount / (double) mFrameCount; |
| 2076 | } |
| 2077 | } else { |
| 2078 | // 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] | 2079 | // 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] | 2080 | // track, but we sometimes have to do this to satisfy the maximum frame count |
| 2081 | // constraint) |
| 2082 | // FIXME this rounding up should not be done if no HAL SRC |
| 2083 | uint32_t truncMult = (uint32_t) multiplier; |
| 2084 | if ((truncMult & 1)) { |
| 2085 | if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) { |
| 2086 | ++truncMult; |
| 2087 | } |
| 2088 | } |
| 2089 | multiplier = (double) truncMult; |
| 2090 | } |
| 2091 | } |
| 2092 | mNormalFrameCount = multiplier * mFrameCount; |
| 2093 | // round up to nearest 16 frames to satisfy AudioMixer |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 2094 | if (mType == MIXER || mType == DUPLICATING) { |
| 2095 | mNormalFrameCount = (mNormalFrameCount + 15) & ~15; |
| 2096 | } |
Andy Hung | 09a5007 | 2014-02-27 14:30:47 -0800 | [diff] [blame] | 2097 | 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] | 2098 | mNormalFrameCount); |
| 2099 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2100 | // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames. |
| 2101 | // Originally this was int16_t[] array, need to remove legacy implications. |
| 2102 | free(mSinkBuffer); |
| 2103 | mSinkBuffer = NULL; |
Andy Hung | 5b10a20 | 2014-03-13 13:59:29 -0700 | [diff] [blame] | 2104 | // For sink buffer size, we use the frame size from the downstream sink to avoid problems |
| 2105 | // with non PCM formats for compressed music, e.g. AAC, and Offload threads. |
| 2106 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2107 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2108 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2109 | // We resize the mMixerBuffer according to the requirements of the sink buffer which |
| 2110 | // drives the output. |
| 2111 | free(mMixerBuffer); |
| 2112 | mMixerBuffer = NULL; |
| 2113 | if (mMixerBufferEnabled) { |
| 2114 | mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT. |
| 2115 | mMixerBufferSize = mNormalFrameCount * mChannelCount |
| 2116 | * audio_bytes_per_sample(mMixerBufferFormat); |
| 2117 | (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize); |
| 2118 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2119 | free(mEffectBuffer); |
| 2120 | mEffectBuffer = NULL; |
| 2121 | if (mEffectBufferEnabled) { |
| 2122 | mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only |
| 2123 | mEffectBufferSize = mNormalFrameCount * mChannelCount |
| 2124 | * audio_bytes_per_sample(mEffectBufferFormat); |
| 2125 | (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize); |
| 2126 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 2127 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2128 | // force reconfiguration of effect chains and engines to take new buffer size and audio |
| 2129 | // parameters into account |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 2130 | // 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] | 2131 | // but in this case nothing is done below as no audio sessions have effect yet so it doesn't |
| 2132 | // matter. |
| 2133 | // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains |
| 2134 | Vector< sp<EffectChain> > effectChains = mEffectChains; |
| 2135 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2136 | mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2141 | status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2142 | { |
| 2143 | if (halFrames == NULL || dspFrames == NULL) { |
| 2144 | return BAD_VALUE; |
| 2145 | } |
| 2146 | Mutex::Autolock _l(mLock); |
| 2147 | if (initCheck() != NO_ERROR) { |
| 2148 | return INVALID_OPERATION; |
| 2149 | } |
| 2150 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 2151 | *halFrames = framesWritten; |
| 2152 | |
| 2153 | if (isSuspended()) { |
| 2154 | // return an estimation of rendered frames when the output is suspended |
| 2155 | size_t latencyFrames = (latency_l() * mSampleRate) / 1000; |
| 2156 | *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0; |
| 2157 | return NO_ERROR; |
| 2158 | } else { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 2159 | status_t status; |
| 2160 | uint32_t frames; |
| 2161 | status = mOutput->stream->get_render_position(mOutput->stream, &frames); |
| 2162 | *dspFrames = (size_t)frames; |
| 2163 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const |
| 2168 | { |
| 2169 | Mutex::Autolock _l(mLock); |
| 2170 | uint32_t result = 0; |
| 2171 | if (getEffectChain_l(sessionId) != 0) { |
| 2172 | result = EFFECT_SESSION; |
| 2173 | } |
| 2174 | |
| 2175 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2176 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2177 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2178 | result |= TRACK_SESSION; |
| 2179 | break; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return result; |
| 2184 | } |
| 2185 | |
| 2186 | uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId) |
| 2187 | { |
| 2188 | // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that |
| 2189 | // it is moved to correct output by audio policy manager when A2DP is connected or disconnected |
| 2190 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 2191 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2192 | } |
| 2193 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2194 | sp<Track> track = mTracks[i]; |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2195 | if (sessionId == track->sessionId() && !track->isInvalid()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2196 | return AudioSystem::getStrategyForStream(track->streamType()); |
| 2197 | } |
| 2198 | } |
| 2199 | return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const |
| 2204 | { |
| 2205 | Mutex::Autolock _l(mLock); |
| 2206 | return mOutput; |
| 2207 | } |
| 2208 | |
| 2209 | AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput() |
| 2210 | { |
| 2211 | Mutex::Autolock _l(mLock); |
| 2212 | AudioStreamOut *output = mOutput; |
| 2213 | mOutput = NULL; |
| 2214 | // FIXME FastMixer might also have a raw ptr to mOutputSink; |
| 2215 | // must push a NULL and wait for ack |
| 2216 | mOutputSink.clear(); |
| 2217 | mPipeSink.clear(); |
| 2218 | mNormalSink.clear(); |
| 2219 | return output; |
| 2220 | } |
| 2221 | |
| 2222 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 2223 | audio_stream_t* AudioFlinger::PlaybackThread::stream() const |
| 2224 | { |
| 2225 | if (mOutput == NULL) { |
| 2226 | return NULL; |
| 2227 | } |
| 2228 | return &mOutput->stream->common; |
| 2229 | } |
| 2230 | |
| 2231 | uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const |
| 2232 | { |
| 2233 | return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 2234 | } |
| 2235 | |
| 2236 | status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event) |
| 2237 | { |
| 2238 | if (!isValidSyncEvent(event)) { |
| 2239 | return BAD_VALUE; |
| 2240 | } |
| 2241 | |
| 2242 | Mutex::Autolock _l(mLock); |
| 2243 | |
| 2244 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2245 | sp<Track> track = mTracks[i]; |
| 2246 | if (event->triggerSession() == track->sessionId()) { |
| 2247 | (void) track->setSyncEvent(event); |
| 2248 | return NO_ERROR; |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | return NAME_NOT_FOUND; |
| 2253 | } |
| 2254 | |
| 2255 | bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const |
| 2256 | { |
| 2257 | return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE; |
| 2258 | } |
| 2259 | |
| 2260 | void AudioFlinger::PlaybackThread::threadLoop_removeTracks( |
| 2261 | const Vector< sp<Track> >& tracksToRemove) |
| 2262 | { |
| 2263 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2264 | if (count > 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2265 | for (size_t i = 0 ; i < count ; i++) { |
| 2266 | const sp<Track>& track = tracksToRemove.itemAt(i); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2267 | if (track->isExternalTrack()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2268 | AudioSystem::stopOutput(mId, track->streamType(), |
| 2269 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2270 | #ifdef ADD_BATTERY_DATA |
| 2271 | // to track the speaker usage |
| 2272 | addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop); |
| 2273 | #endif |
| 2274 | if (track->isTerminated()) { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 2275 | AudioSystem::releaseOutput(mId, track->streamType(), |
| 2276 | (audio_session_t)track->sessionId()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2277 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | void AudioFlinger::PlaybackThread::checkSilentMode_l() |
| 2284 | { |
| 2285 | if (!mMasterMute) { |
| 2286 | char value[PROPERTY_VALUE_MAX]; |
| 2287 | if (property_get("ro.audio.silent", value, "0") > 0) { |
| 2288 | char *endptr; |
| 2289 | unsigned long ul = strtoul(value, &endptr, 0); |
| 2290 | if (*endptr == '\0' && ul != 0) { |
| 2291 | ALOGD("Silence is golden"); |
| 2292 | // The setprop command will not allow a property to be changed after |
| 2293 | // the first time it is set, so we don't have to worry about un-muting. |
| 2294 | setMasterMute_l(true); |
| 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2301 | ssize_t AudioFlinger::PlaybackThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2302 | { |
| 2303 | // FIXME rewrite to reduce number of system calls |
| 2304 | mLastWriteTime = systemTime(); |
| 2305 | mInWrite = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2306 | ssize_t bytesWritten; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2307 | const size_t offset = mCurrentWriteLength - mBytesRemaining; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2308 | |
| 2309 | // If an NBAIO sink is present, use it to write the normal mixer's submix |
| 2310 | if (mNormalSink != 0) { |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2311 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2312 | const size_t count = mBytesRemaining / mFrameSize; |
| 2313 | |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2314 | ATRACE_BEGIN("write"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2315 | // update the setpoint when AudioFlinger::mScreenState changes |
| 2316 | uint32_t screenState = AudioFlinger::mScreenState; |
| 2317 | if (screenState != mScreenState) { |
| 2318 | mScreenState = screenState; |
| 2319 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 2320 | if (pipe != NULL) { |
| 2321 | pipe->setAvgFrames((mScreenState & 1) ? |
| 2322 | (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 2323 | } |
| 2324 | } |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2325 | ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count); |
Simon Wilson | 2d59096 | 2012-11-29 15:18:50 -0800 | [diff] [blame] | 2326 | ATRACE_END(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2327 | if (framesWritten > 0) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2328 | bytesWritten = framesWritten * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2329 | } else { |
| 2330 | bytesWritten = framesWritten; |
| 2331 | } |
Glenn Kasten | efaa7ab | 2014-08-20 08:48:54 -0700 | [diff] [blame] | 2332 | mLatchDValid = false; |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2333 | status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp); |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2334 | if (status == NO_ERROR) { |
| 2335 | size_t totalFramesWritten = mNormalSink->framesWritten(); |
| 2336 | if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) { |
| 2337 | mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition; |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2338 | // mLatchD.mFramesReleased is set immediately before D is clocked into Q |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2339 | mLatchDValid = true; |
| 2340 | } |
| 2341 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2342 | // otherwise use the HAL / AudioStreamOut directly |
| 2343 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2344 | // Direct output and offload threads |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2345 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2346 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2347 | ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request"); |
| 2348 | mWriteAckSequence += 2; |
| 2349 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2350 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2351 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2352 | } |
Glenn Kasten | 767094d | 2013-08-23 13:51:43 -0700 | [diff] [blame] | 2353 | // FIXME We should have an implementation of timestamps for direct output threads. |
| 2354 | // They are used e.g for multichannel PCM playback over HDMI. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2355 | bytesWritten = mOutput->stream->write(mOutput->stream, |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 2356 | (char *)mSinkBuffer + offset, mBytesRemaining); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2357 | if (mUseAsyncWrite && |
| 2358 | ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) { |
| 2359 | // 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] | 2360 | mWriteAckSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2361 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2362 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2363 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2364 | } |
| 2365 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2366 | mNumWrites++; |
| 2367 | mInWrite = false; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 2368 | mStandby = false; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2369 | return bytesWritten; |
| 2370 | } |
| 2371 | |
| 2372 | void AudioFlinger::PlaybackThread::threadLoop_drain() |
| 2373 | { |
| 2374 | if (mOutput->stream->drain) { |
| 2375 | ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full"); |
| 2376 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2377 | ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request"); |
| 2378 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2379 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 2380 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2381 | } |
| 2382 | mOutput->stream->drain(mOutput->stream, |
| 2383 | (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY |
| 2384 | : AUDIO_DRAIN_ALL); |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | void AudioFlinger::PlaybackThread::threadLoop_exit() |
| 2389 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2390 | { |
| 2391 | Mutex::Autolock _l(mLock); |
| 2392 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 2393 | sp<Track> track = mTracks[i]; |
| 2394 | track->invalidate(); |
| 2395 | } |
| 2396 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | The derived values that are cached: |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2401 | - mSinkBufferSize from frame count * frame size |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2402 | - activeSleepTime from activeSleepTimeUs() |
| 2403 | - idleSleepTime from idleSleepTimeUs() |
| 2404 | - standbyDelay from mActiveSleepTimeUs (DIRECT only) |
| 2405 | - maxPeriod from frame count and sample rate (MIXER only) |
| 2406 | |
| 2407 | The parameters that affect these derived values are: |
| 2408 | - frame count |
| 2409 | - frame size |
| 2410 | - sample rate |
| 2411 | - device type: A2DP or not |
| 2412 | - device latency |
| 2413 | - format: PCM or not |
| 2414 | - active sleep time |
| 2415 | - idle sleep time |
| 2416 | */ |
| 2417 | |
| 2418 | void AudioFlinger::PlaybackThread::cacheParameters_l() |
| 2419 | { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2420 | mSinkBufferSize = mNormalFrameCount * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2421 | activeSleepTime = activeSleepTimeUs(); |
| 2422 | idleSleepTime = idleSleepTimeUs(); |
| 2423 | } |
| 2424 | |
| 2425 | void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType) |
| 2426 | { |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 2427 | ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d", |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2428 | this, streamType, mTracks.size()); |
| 2429 | Mutex::Autolock _l(mLock); |
| 2430 | |
| 2431 | size_t size = mTracks.size(); |
| 2432 | for (size_t i = 0; i < size; i++) { |
| 2433 | sp<Track> t = mTracks[i]; |
| 2434 | if (t->streamType() == streamType) { |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 2435 | t->invalidate(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 2441 | { |
| 2442 | int session = chain->sessionId(); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2443 | int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2444 | ? mEffectBuffer : mSinkBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2445 | bool ownsBuffer = false; |
| 2446 | |
| 2447 | ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session); |
| 2448 | if (session > 0) { |
| 2449 | // 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] | 2450 | // the sink buffer as input |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2451 | if (mType != DIRECT) { |
| 2452 | size_t numSamples = mNormalFrameCount * mChannelCount; |
| 2453 | buffer = new int16_t[numSamples]; |
| 2454 | memset(buffer, 0, numSamples * sizeof(int16_t)); |
| 2455 | ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session); |
| 2456 | ownsBuffer = true; |
| 2457 | } |
| 2458 | |
| 2459 | // Attach all tracks with same session ID to this chain. |
| 2460 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2461 | sp<Track> track = mTracks[i]; |
| 2462 | if (session == track->sessionId()) { |
| 2463 | ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), |
| 2464 | buffer); |
| 2465 | track->setMainBuffer(buffer); |
| 2466 | chain->incTrackCnt(); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | // indicate all active tracks in the chain |
| 2471 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2472 | sp<Track> track = mActiveTracks[i].promote(); |
| 2473 | if (track == 0) { |
| 2474 | continue; |
| 2475 | } |
| 2476 | if (session == track->sessionId()) { |
| 2477 | ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session); |
| 2478 | chain->incActiveTrackCnt(); |
| 2479 | } |
| 2480 | } |
| 2481 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2482 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2483 | chain->setInBuffer(buffer, ownsBuffer); |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2484 | chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled |
| 2485 | ? mEffectBuffer : mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2486 | // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect |
| 2487 | // chains list in order to be processed last as it contains output stage effects |
| 2488 | // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before |
| 2489 | // session AUDIO_SESSION_OUTPUT_STAGE to be processed |
| 2490 | // after track specific effects and before output stage |
| 2491 | // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and |
| 2492 | // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX |
| 2493 | // Effect chain for other sessions are inserted at beginning of effect |
| 2494 | // chains list to be processed before output mix effects. Relative order between other |
| 2495 | // sessions is not important |
| 2496 | size_t size = mEffectChains.size(); |
| 2497 | size_t i = 0; |
| 2498 | for (i = 0; i < size; i++) { |
| 2499 | if (mEffectChains[i]->sessionId() < session) { |
| 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | mEffectChains.insertAt(chain, i); |
| 2504 | checkSuspendOnAddEffectChain_l(chain); |
| 2505 | |
| 2506 | return NO_ERROR; |
| 2507 | } |
| 2508 | |
| 2509 | size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 2510 | { |
| 2511 | int session = chain->sessionId(); |
| 2512 | |
| 2513 | ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session); |
| 2514 | |
| 2515 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2516 | if (chain == mEffectChains[i]) { |
| 2517 | mEffectChains.removeAt(i); |
| 2518 | // detach all active tracks from the chain |
| 2519 | for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) { |
| 2520 | sp<Track> track = mActiveTracks[i].promote(); |
| 2521 | if (track == 0) { |
| 2522 | continue; |
| 2523 | } |
| 2524 | if (session == track->sessionId()) { |
| 2525 | ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d", |
| 2526 | chain.get(), session); |
| 2527 | chain->decActiveTrackCnt(); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | // detach all tracks with same session ID from this chain |
| 2532 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2533 | sp<Track> track = mTracks[i]; |
| 2534 | if (session == track->sessionId()) { |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 2535 | track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2536 | chain->decTrackCnt(); |
| 2537 | } |
| 2538 | } |
| 2539 | break; |
| 2540 | } |
| 2541 | } |
| 2542 | return mEffectChains.size(); |
| 2543 | } |
| 2544 | |
| 2545 | status_t AudioFlinger::PlaybackThread::attachAuxEffect( |
| 2546 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2547 | { |
| 2548 | Mutex::Autolock _l(mLock); |
| 2549 | return attachAuxEffect_l(track, EffectId); |
| 2550 | } |
| 2551 | |
| 2552 | status_t AudioFlinger::PlaybackThread::attachAuxEffect_l( |
| 2553 | const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId) |
| 2554 | { |
| 2555 | status_t status = NO_ERROR; |
| 2556 | |
| 2557 | if (EffectId == 0) { |
| 2558 | track->setAuxBuffer(0, NULL); |
| 2559 | } else { |
| 2560 | // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX |
| 2561 | sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId); |
| 2562 | if (effect != 0) { |
| 2563 | if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2564 | track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer()); |
| 2565 | } else { |
| 2566 | status = INVALID_OPERATION; |
| 2567 | } |
| 2568 | } else { |
| 2569 | status = BAD_VALUE; |
| 2570 | } |
| 2571 | } |
| 2572 | return status; |
| 2573 | } |
| 2574 | |
| 2575 | void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId) |
| 2576 | { |
| 2577 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 2578 | sp<Track> track = mTracks[i]; |
| 2579 | if (track->auxEffectId() == effectId) { |
| 2580 | attachAuxEffect_l(track, 0); |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | bool AudioFlinger::PlaybackThread::threadLoop() |
| 2586 | { |
| 2587 | Vector< sp<Track> > tracksToRemove; |
| 2588 | |
| 2589 | standbyTime = systemTime(); |
| 2590 | |
| 2591 | // MIXER |
| 2592 | nsecs_t lastWarning = 0; |
| 2593 | |
| 2594 | // DUPLICATING |
| 2595 | // FIXME could this be made local to while loop? |
| 2596 | writeFrames = 0; |
| 2597 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2598 | int lastGeneration = 0; |
| 2599 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2600 | cacheParameters_l(); |
| 2601 | sleepTime = idleSleepTime; |
| 2602 | |
| 2603 | if (mType == MIXER) { |
| 2604 | sleepTimeShift = 0; |
| 2605 | } |
| 2606 | |
| 2607 | CpuStats cpuStats; |
| 2608 | const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid())); |
| 2609 | |
| 2610 | acquireWakeLock(); |
| 2611 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2612 | // mNBLogWriter->log can only be called while thread mutex mLock is held. |
| 2613 | // So if you need to log when mutex is unlocked, set logString to a non-NULL string, |
| 2614 | // and then that string will be logged at the next convenient opportunity. |
| 2615 | const char *logString = NULL; |
| 2616 | |
Eric Laurent | 664539d | 2013-09-23 18:24:31 -0700 | [diff] [blame] | 2617 | checkSilentMode_l(); |
| 2618 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2619 | while (!exitPending()) |
| 2620 | { |
| 2621 | cpuStats.sample(myName); |
| 2622 | |
| 2623 | Vector< sp<EffectChain> > effectChains; |
| 2624 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2625 | { // scope for mLock |
| 2626 | |
| 2627 | Mutex::Autolock _l(mLock); |
| 2628 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2629 | processConfigEvents_l(); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 2630 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 2631 | if (logString != NULL) { |
| 2632 | mNBLogWriter->logTimestamp(); |
| 2633 | mNBLogWriter->log(logString); |
| 2634 | logString = NULL; |
| 2635 | } |
| 2636 | |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 2637 | // Gather the framesReleased counters for all active tracks, |
| 2638 | // and latch them atomically with the timestamp. |
| 2639 | // FIXME We're using raw pointers as indices. A unique track ID would be a better index. |
| 2640 | mLatchD.mFramesReleased.clear(); |
| 2641 | size_t size = mActiveTracks.size(); |
| 2642 | for (size_t i = 0; i < size; i++) { |
| 2643 | sp<Track> t = mActiveTracks[i].promote(); |
| 2644 | if (t != 0) { |
| 2645 | mLatchD.mFramesReleased.add(t.get(), |
| 2646 | t->mAudioTrackServerProxy->framesReleased()); |
| 2647 | } |
| 2648 | } |
Glenn Kasten | bd096fd | 2013-08-23 13:53:56 -0700 | [diff] [blame] | 2649 | if (mLatchDValid) { |
| 2650 | mLatchQ = mLatchD; |
| 2651 | mLatchDValid = false; |
| 2652 | mLatchQValid = true; |
| 2653 | } |
| 2654 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2655 | saveOutputTracks(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2656 | if (mSignalPending) { |
| 2657 | // A signal was raised while we were unlocked |
| 2658 | mSignalPending = false; |
| 2659 | } else if (waitingAsyncCallback_l()) { |
| 2660 | if (exitPending()) { |
| 2661 | break; |
| 2662 | } |
| 2663 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2664 | mWakeLockUids.clear(); |
| 2665 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2666 | ALOGV("wait async completion"); |
| 2667 | mWaitWorkCV.wait(mLock); |
| 2668 | ALOGV("async completion/wake"); |
| 2669 | acquireWakeLock_l(); |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 2670 | standbyTime = systemTime() + standbyDelay; |
| 2671 | sleepTime = 0; |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 2672 | |
| 2673 | continue; |
| 2674 | } |
| 2675 | if ((!mActiveTracks.size() && systemTime() > standbyTime) || |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2676 | isSuspended()) { |
| 2677 | // put audio hardware into standby after short delay |
| 2678 | if (shouldStandby_l()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2679 | |
| 2680 | threadLoop_standby(); |
| 2681 | |
| 2682 | mStandby = true; |
| 2683 | } |
| 2684 | |
| 2685 | if (!mActiveTracks.size() && mConfigEvents.isEmpty()) { |
| 2686 | // we're about to wait, flush the binder command buffer |
| 2687 | IPCThreadState::self()->flushCommands(); |
| 2688 | |
| 2689 | clearOutputTracks(); |
| 2690 | |
| 2691 | if (exitPending()) { |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | releaseWakeLock_l(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2696 | mWakeLockUids.clear(); |
| 2697 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2698 | // wait until we have something to do... |
| 2699 | ALOGV("%s going to sleep", myName.string()); |
| 2700 | mWaitWorkCV.wait(mLock); |
| 2701 | ALOGV("%s waking up", myName.string()); |
| 2702 | acquireWakeLock_l(); |
| 2703 | |
| 2704 | mMixerStatus = MIXER_IDLE; |
| 2705 | mMixerStatusIgnoringFastTracks = MIXER_IDLE; |
| 2706 | mBytesWritten = 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2707 | mBytesRemaining = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2708 | checkSilentMode_l(); |
| 2709 | |
| 2710 | standbyTime = systemTime() + standbyDelay; |
| 2711 | sleepTime = idleSleepTime; |
| 2712 | if (mType == MIXER) { |
| 2713 | sleepTimeShift = 0; |
| 2714 | } |
| 2715 | |
| 2716 | continue; |
| 2717 | } |
| 2718 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2719 | // mMixerStatusIgnoringFastTracks is also updated internally |
| 2720 | mMixerStatus = prepareTracks_l(&tracksToRemove); |
| 2721 | |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2722 | // compare with previously applied list |
| 2723 | if (lastGeneration != mActiveTracksGeneration) { |
| 2724 | // update wakelock |
| 2725 | updateWakeLockUids_l(mWakeLockUids); |
| 2726 | lastGeneration = mActiveTracksGeneration; |
| 2727 | } |
| 2728 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2729 | // prevent any changes in effect chain list and in each effect chain |
| 2730 | // during mixing and effect process as the audio buffers could be deleted |
| 2731 | // or modified if an effect is created or deleted |
| 2732 | lockEffectChains_l(effectChains); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2733 | } // mLock scope ends |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2734 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2735 | if (mBytesRemaining == 0) { |
| 2736 | mCurrentWriteLength = 0; |
| 2737 | if (mMixerStatus == MIXER_TRACKS_READY) { |
| 2738 | // threadLoop_mix() sets mCurrentWriteLength |
| 2739 | threadLoop_mix(); |
| 2740 | } else if ((mMixerStatus != MIXER_DRAIN_TRACK) |
| 2741 | && (mMixerStatus != MIXER_DRAIN_ALL)) { |
| 2742 | // threadLoop_sleepTime sets sleepTime to 0 if data |
| 2743 | // must be written to HAL |
| 2744 | threadLoop_sleepTime(); |
| 2745 | if (sleepTime == 0) { |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2746 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2747 | } |
| 2748 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2749 | // Either threadLoop_mix() or threadLoop_sleepTime() should have set |
| 2750 | // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0. |
| 2751 | // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid) |
| 2752 | // or mSinkBuffer (if there are no effects). |
| 2753 | // |
| 2754 | // This is done pre-effects computation; if effects change to |
| 2755 | // support higher precision, this needs to move. |
| 2756 | // |
| 2757 | // mMixerBufferValid is only set true by MixerThread::prepareTracks_l(). |
| 2758 | // TODO use sleepTime == 0 as an additional condition. |
| 2759 | if (mMixerBufferValid) { |
| 2760 | void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer; |
| 2761 | audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat; |
| 2762 | |
| 2763 | memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat, |
| 2764 | mNormalFrameCount * mChannelCount); |
| 2765 | } |
| 2766 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2767 | mBytesRemaining = mCurrentWriteLength; |
| 2768 | if (isSuspended()) { |
| 2769 | sleepTime = suspendSleepTimeUs(); |
| 2770 | // simulate write to HAL when suspended |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 2771 | mBytesWritten += mSinkBufferSize; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2772 | mBytesRemaining = 0; |
| 2773 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2774 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2775 | // only process effects if we're going to write |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2776 | if (sleepTime == 0 && mType != OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2777 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2778 | effectChains[i]->process_l(); |
| 2779 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2780 | } |
| 2781 | } |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 2782 | // Process effect chains for offloaded thread even if no audio |
| 2783 | // was read from audio track: process only updates effect state |
| 2784 | // and thus does have to be synchronized with audio writes but may have |
| 2785 | // to be called while waiting for async write callback |
| 2786 | if (mType == OFFLOAD) { |
| 2787 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 2788 | effectChains[i]->process_l(); |
| 2789 | } |
| 2790 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 2792 | // Only if the Effects buffer is enabled and there is data in the |
| 2793 | // Effects buffer (buffer valid), we need to |
| 2794 | // copy into the sink buffer. |
| 2795 | // TODO use sleepTime == 0 as an additional condition. |
| 2796 | if (mEffectBufferValid) { |
| 2797 | //ALOGV("writing effect buffer to sink buffer format %#x", mFormat); |
| 2798 | memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat, |
| 2799 | mNormalFrameCount * mChannelCount); |
| 2800 | } |
| 2801 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2802 | // enable changes in effect chain |
| 2803 | unlockEffectChains(effectChains); |
| 2804 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2805 | if (!waitingAsyncCallback()) { |
| 2806 | // sleepTime == 0 means we must write to audio hardware |
| 2807 | if (sleepTime == 0) { |
| 2808 | if (mBytesRemaining) { |
| 2809 | ssize_t ret = threadLoop_write(); |
| 2810 | if (ret < 0) { |
| 2811 | mBytesRemaining = 0; |
| 2812 | } else { |
| 2813 | mBytesWritten += ret; |
| 2814 | mBytesRemaining -= ret; |
| 2815 | } |
| 2816 | } else if ((mMixerStatus == MIXER_DRAIN_TRACK) || |
| 2817 | (mMixerStatus == MIXER_DRAIN_ALL)) { |
| 2818 | threadLoop_drain(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2819 | } |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 2820 | if (mType == MIXER) { |
| 2821 | // write blocked detection |
| 2822 | nsecs_t now = systemTime(); |
| 2823 | nsecs_t delta = now - mLastWriteTime; |
| 2824 | if (!mStandby && delta > maxPeriod) { |
| 2825 | mNumDelayedWrites++; |
| 2826 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 2827 | ATRACE_NAME("underrun"); |
| 2828 | ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p", |
| 2829 | ns2ms(delta), mNumDelayedWrites, this); |
| 2830 | lastWarning = now; |
| 2831 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2832 | } |
| 2833 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2834 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2835 | } else { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2836 | ATRACE_BEGIN("sleep"); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2837 | usleep(sleepTime); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 2838 | ATRACE_END(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2839 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | // Finally let go of removed track(s), without the lock held |
| 2843 | // since we can't guarantee the destructors won't acquire that |
| 2844 | // same lock. This will also mutate and push a new fast mixer state. |
| 2845 | threadLoop_removeTracks(tracksToRemove); |
| 2846 | tracksToRemove.clear(); |
| 2847 | |
| 2848 | // FIXME I don't understand the need for this here; |
| 2849 | // it was in the original code but maybe the |
| 2850 | // assignment in saveOutputTracks() makes this unnecessary? |
| 2851 | clearOutputTracks(); |
| 2852 | |
| 2853 | // Effect chains will be actually deleted here if they were removed from |
| 2854 | // mEffectChains list during mixing or effects processing |
| 2855 | effectChains.clear(); |
| 2856 | |
| 2857 | // FIXME Note that the above .clear() is no longer necessary since effectChains |
| 2858 | // is now local to this block, but will keep it for now (at least until merge done). |
| 2859 | } |
| 2860 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2861 | threadLoop_exit(); |
| 2862 | |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 2863 | if (!mStandby) { |
| 2864 | threadLoop_standby(); |
| 2865 | mStandby = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | releaseWakeLock(); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2869 | mWakeLockUids.clear(); |
| 2870 | mActiveTracksGeneration++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2871 | |
| 2872 | ALOGV("Thread %p type %d exiting", this, mType); |
| 2873 | return false; |
| 2874 | } |
| 2875 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2876 | // removeTracks_l() must be called with ThreadBase::mLock held |
| 2877 | void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove) |
| 2878 | { |
| 2879 | size_t count = tracksToRemove.size(); |
Glenn Kasten | 34fca34 | 2013-08-13 09:48:14 -0700 | [diff] [blame] | 2880 | if (count > 0) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2881 | for (size_t i=0 ; i<count ; i++) { |
| 2882 | const sp<Track>& track = tracksToRemove.itemAt(i); |
| 2883 | mActiveTracks.remove(track); |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2884 | mWakeLockUids.remove(track->uid()); |
| 2885 | mActiveTracksGeneration++; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2886 | ALOGV("removeTracks_l removing track on session %d", track->sessionId()); |
| 2887 | sp<EffectChain> chain = getEffectChain_l(track->sessionId()); |
| 2888 | if (chain != 0) { |
| 2889 | ALOGV("stopping track on chain %p for session Id: %d", chain.get(), |
| 2890 | track->sessionId()); |
| 2891 | chain->decActiveTrackCnt(); |
| 2892 | } |
| 2893 | if (track->isTerminated()) { |
| 2894 | removeTrack_l(track); |
| 2895 | } |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2900 | |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2901 | status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp) |
| 2902 | { |
| 2903 | if (mNormalSink != 0) { |
| 2904 | return mNormalSink->getTimestamp(timestamp); |
| 2905 | } |
Andy Hung | 9a1c889 | 2014-12-03 11:37:42 -0800 | [diff] [blame] | 2906 | if ((mType == OFFLOAD || mType == DIRECT) |
| 2907 | && mOutput != NULL && mOutput->stream->get_presentation_position) { |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 2908 | uint64_t position64; |
| 2909 | int ret = mOutput->stream->get_presentation_position( |
| 2910 | mOutput->stream, &position64, ×tamp.mTime); |
| 2911 | if (ret == 0) { |
| 2912 | timestamp.mPosition = (uint32_t)position64; |
| 2913 | return NO_ERROR; |
| 2914 | } |
| 2915 | } |
| 2916 | return INVALID_OPERATION; |
| 2917 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2918 | |
| 2919 | status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch, |
| 2920 | audio_patch_handle_t *handle) |
| 2921 | { |
| 2922 | status_t status = NO_ERROR; |
| 2923 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2924 | // store new device and send to effects |
| 2925 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| 2926 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 2927 | type |= patch->sinks[i].ext.device.type; |
| 2928 | } |
| 2929 | mOutDevice = type; |
| 2930 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 2931 | mEffectChains[i]->setDevice_l(mOutDevice); |
| 2932 | } |
| 2933 | |
| 2934 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2935 | status = hwDevice->create_audio_patch(hwDevice, |
| 2936 | patch->num_sources, |
| 2937 | patch->sources, |
| 2938 | patch->num_sinks, |
| 2939 | patch->sinks, |
| 2940 | handle); |
| 2941 | } else { |
| 2942 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 2943 | } |
| 2944 | return status; |
| 2945 | } |
| 2946 | |
| 2947 | status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 2948 | { |
| 2949 | status_t status = NO_ERROR; |
| 2950 | if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 2951 | audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice(); |
| 2952 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 2953 | } else { |
| 2954 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 2955 | } |
| 2956 | return status; |
| 2957 | } |
| 2958 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2959 | void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track) |
| 2960 | { |
| 2961 | Mutex::Autolock _l(mLock); |
| 2962 | mTracks.add(track); |
| 2963 | } |
| 2964 | |
| 2965 | void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track) |
| 2966 | { |
| 2967 | Mutex::Autolock _l(mLock); |
| 2968 | destroyTrack_l(track); |
| 2969 | } |
| 2970 | |
| 2971 | void AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config) |
| 2972 | { |
| 2973 | ThreadBase::getAudioPortConfig(config); |
| 2974 | config->role = AUDIO_PORT_ROLE_SOURCE; |
| 2975 | config->ext.mix.hw_module = mOutput->audioHwDev->handle(); |
| 2976 | config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT; |
| 2977 | } |
| 2978 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2979 | // ---------------------------------------------------------------------------- |
| 2980 | |
| 2981 | AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, |
| 2982 | audio_io_handle_t id, audio_devices_t device, type_t type) |
| 2983 | : PlaybackThread(audioFlinger, output, id, device, type), |
| 2984 | // mAudioMixer below |
| 2985 | // mFastMixer below |
| 2986 | mFastMixerFutex(0) |
| 2987 | // mOutputSink below |
| 2988 | // mPipeSink below |
| 2989 | // mNormalSink below |
| 2990 | { |
| 2991 | ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type); |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 2992 | ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, " |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2993 | "mFrameCount=%d, mNormalFrameCount=%d", |
| 2994 | mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount, |
| 2995 | mNormalFrameCount); |
| 2996 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 2997 | |
Andy Hung | fbfc395 | 2015-01-15 13:33:51 -0800 | [diff] [blame] | 2998 | if (type == DUPLICATING) { |
| 2999 | // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks |
| 3000 | // (downstream MixerThreads) in DuplicatingThread::threadLoop_write(). |
| 3001 | // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink. |
| 3002 | return; |
| 3003 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3004 | // create an NBAIO sink for the HAL output stream, and negotiate |
| 3005 | mOutputSink = new AudioStreamOutSink(output->stream); |
| 3006 | size_t numCounterOffers = 0; |
Glenn Kasten | f69f986 | 2014-03-07 08:37:57 -0800 | [diff] [blame] | 3007 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3008 | ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers); |
| 3009 | ALOG_ASSERT(index == 0); |
| 3010 | |
| 3011 | // initialize fast mixer depending on configuration |
| 3012 | bool initFastMixer; |
| 3013 | switch (kUseFastMixer) { |
| 3014 | case FastMixer_Never: |
| 3015 | initFastMixer = false; |
| 3016 | break; |
| 3017 | case FastMixer_Always: |
| 3018 | initFastMixer = true; |
| 3019 | break; |
| 3020 | case FastMixer_Static: |
| 3021 | case FastMixer_Dynamic: |
| 3022 | initFastMixer = mFrameCount < mNormalFrameCount; |
| 3023 | break; |
| 3024 | } |
| 3025 | if (initFastMixer) { |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3026 | audio_format_t fastMixerFormat; |
| 3027 | if (mMixerBufferEnabled && mEffectBufferEnabled) { |
| 3028 | fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 3029 | } else { |
| 3030 | fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3031 | } |
| 3032 | if (mFormat != fastMixerFormat) { |
| 3033 | // change our Sink format to accept our intermediate precision |
| 3034 | mFormat = fastMixerFormat; |
| 3035 | free(mSinkBuffer); |
| 3036 | mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat); |
| 3037 | const size_t sinkBufferSize = mNormalFrameCount * mFrameSize; |
| 3038 | (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize); |
| 3039 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3040 | |
| 3041 | // create a MonoPipe to connect our submix to FastMixer |
| 3042 | NBAIO_Format format = mOutputSink->format(); |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3043 | NBAIO_Format origformat = format; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3044 | // adjust format to match that of the Fast Mixer |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 3045 | ALOGV("format changed from %d to %d", format.mFormat, fastMixerFormat); |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 3046 | format.mFormat = fastMixerFormat; |
| 3047 | format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount; |
| 3048 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3049 | // This pipe depth compensates for scheduling latency of the normal mixer thread. |
| 3050 | // When it wakes up after a maximum latency, it runs a few cycles quickly before |
| 3051 | // finally blocking. Note the pipe implementation rounds up the request to a power of 2. |
| 3052 | MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/); |
| 3053 | const NBAIO_Format offers[1] = {format}; |
| 3054 | size_t numCounterOffers = 0; |
| 3055 | ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 3056 | ALOG_ASSERT(index == 0); |
| 3057 | monoPipe->setAvgFrames((mScreenState & 1) ? |
| 3058 | (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2); |
| 3059 | mPipeSink = monoPipe; |
| 3060 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3061 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3062 | if (mTeeSinkOutputEnabled) { |
| 3063 | // 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] | 3064 | Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat); |
| 3065 | const NBAIO_Format offers2[1] = {origformat}; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3066 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3067 | index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3068 | ALOG_ASSERT(index == 0); |
| 3069 | mTeeSink = teeSink; |
| 3070 | PipeReader *teeSource = new PipeReader(*teeSink); |
| 3071 | numCounterOffers = 0; |
Glenn Kasten | ba0b34c | 2014-09-28 13:06:06 -0700 | [diff] [blame] | 3072 | index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3073 | ALOG_ASSERT(index == 0); |
| 3074 | mTeeSource = teeSource; |
| 3075 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3076 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3077 | |
| 3078 | // create fast mixer and configure it initially with just one fast track for our submix |
| 3079 | mFastMixer = new FastMixer(); |
| 3080 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3081 | #ifdef STATE_QUEUE_DUMP |
| 3082 | sq->setObserverDump(&mStateQueueObserverDump); |
| 3083 | sq->setMutatorDump(&mStateQueueMutatorDump); |
| 3084 | #endif |
| 3085 | FastMixerState *state = sq->begin(); |
| 3086 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3087 | // wrap the source side of the MonoPipe to make it an AudioBufferProvider |
| 3088 | fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe)); |
| 3089 | fastTrack->mVolumeProvider = NULL; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3090 | fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer |
| 3091 | fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3092 | fastTrack->mGeneration++; |
| 3093 | state->mFastTracksGen++; |
| 3094 | state->mTrackMask = 1; |
| 3095 | // fast mixer will use the HAL output sink |
| 3096 | state->mOutputSink = mOutputSink.get(); |
| 3097 | state->mOutputSinkGen++; |
| 3098 | state->mFrameCount = mFrameCount; |
| 3099 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3100 | // already done in constructor initialization list |
| 3101 | //mFastMixerFutex = 0; |
| 3102 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3103 | state->mColdGen++; |
| 3104 | state->mDumpState = &mFastMixerDumpState; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3105 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3106 | state->mTeeSink = mTeeSink.get(); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3107 | #endif |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3108 | mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer"); |
| 3109 | state->mNBLogWriter = mFastMixerNBLogWriter.get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3110 | sq->end(); |
| 3111 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3112 | |
| 3113 | // start the fast mixer |
| 3114 | mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO); |
| 3115 | pid_t tid = mFastMixer->getTid(); |
| 3116 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 3117 | if (err != 0) { |
| 3118 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 3119 | kPriorityFastMixer, getpid_cached, tid, err); |
| 3120 | } |
| 3121 | |
| 3122 | #ifdef AUDIO_WATCHDOG |
| 3123 | // create and start the watchdog |
| 3124 | mAudioWatchdog = new AudioWatchdog(); |
| 3125 | mAudioWatchdog->setDump(&mAudioWatchdogDump); |
| 3126 | mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO); |
| 3127 | tid = mAudioWatchdog->getTid(); |
| 3128 | err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 3129 | if (err != 0) { |
| 3130 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 3131 | kPriorityFastMixer, getpid_cached, tid, err); |
| 3132 | } |
| 3133 | #endif |
| 3134 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | switch (kUseFastMixer) { |
| 3138 | case FastMixer_Never: |
| 3139 | case FastMixer_Dynamic: |
| 3140 | mNormalSink = mOutputSink; |
| 3141 | break; |
| 3142 | case FastMixer_Always: |
| 3143 | mNormalSink = mPipeSink; |
| 3144 | break; |
| 3145 | case FastMixer_Static: |
| 3146 | mNormalSink = initFastMixer ? mPipeSink : mOutputSink; |
| 3147 | break; |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | AudioFlinger::MixerThread::~MixerThread() |
| 3152 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3153 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3154 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3155 | FastMixerState *state = sq->begin(); |
| 3156 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3157 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3158 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3159 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3160 | } |
| 3161 | } |
| 3162 | state->mCommand = FastMixerState::EXIT; |
| 3163 | sq->end(); |
| 3164 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3165 | mFastMixer->join(); |
| 3166 | // Though the fast mixer thread has exited, it's state queue is still valid. |
| 3167 | // We'll use that extract the final state which contains one remaining fast track |
| 3168 | // corresponding to our sub-mix. |
| 3169 | state = sq->begin(); |
| 3170 | ALOG_ASSERT(state->mTrackMask == 1); |
| 3171 | FastTrack *fastTrack = &state->mFastTracks[0]; |
| 3172 | ALOG_ASSERT(fastTrack->mBufferProvider != NULL); |
| 3173 | delete fastTrack->mBufferProvider; |
| 3174 | sq->end(false /*didModify*/); |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3175 | mFastMixer.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3176 | #ifdef AUDIO_WATCHDOG |
| 3177 | if (mAudioWatchdog != 0) { |
| 3178 | mAudioWatchdog->requestExit(); |
| 3179 | mAudioWatchdog->requestExitAndWait(); |
| 3180 | mAudioWatchdog.clear(); |
| 3181 | } |
| 3182 | #endif |
| 3183 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 3184 | mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3185 | delete mAudioMixer; |
| 3186 | } |
| 3187 | |
| 3188 | |
| 3189 | uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const |
| 3190 | { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3191 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3192 | MonoPipe *pipe = (MonoPipe *)mPipeSink.get(); |
| 3193 | latency += (pipe->getAvgFrames() * 1000) / mSampleRate; |
| 3194 | } |
| 3195 | return latency; |
| 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | void AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove) |
| 3200 | { |
| 3201 | PlaybackThread::threadLoop_removeTracks(tracksToRemove); |
| 3202 | } |
| 3203 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3204 | ssize_t AudioFlinger::MixerThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3205 | { |
| 3206 | // FIXME we should only do one push per cycle; confirm this is true |
| 3207 | // Start the fast mixer if it's not already running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3208 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3209 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3210 | FastMixerState *state = sq->begin(); |
| 3211 | if (state->mCommand != FastMixerState::MIX_WRITE && |
| 3212 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) { |
| 3213 | if (state->mCommand == FastMixerState::COLD_IDLE) { |
| 3214 | int32_t old = android_atomic_inc(&mFastMixerFutex); |
| 3215 | if (old == -1) { |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 3216 | (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3217 | } |
| 3218 | #ifdef AUDIO_WATCHDOG |
| 3219 | if (mAudioWatchdog != 0) { |
| 3220 | mAudioWatchdog->resume(); |
| 3221 | } |
| 3222 | #endif |
| 3223 | } |
| 3224 | state->mCommand = FastMixerState::MIX_WRITE; |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3225 | #ifdef FAST_THREAD_STATISTICS |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 3226 | mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 3227 | FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN); |
Glenn Kasten | d797a9d | 2015-03-02 14:19:25 -0800 | [diff] [blame] | 3228 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3229 | sq->end(); |
| 3230 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 3231 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3232 | mNormalSink = mPipeSink; |
| 3233 | } |
| 3234 | } else { |
| 3235 | sq->end(false /*didModify*/); |
| 3236 | } |
| 3237 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3238 | return PlaybackThread::threadLoop_write(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | void AudioFlinger::MixerThread::threadLoop_standby() |
| 3242 | { |
| 3243 | // Idle the fast mixer if it's currently running |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3244 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3245 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3246 | FastMixerState *state = sq->begin(); |
| 3247 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3248 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3249 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3250 | state->mColdGen++; |
| 3251 | mFastMixerFutex = 0; |
| 3252 | sq->end(); |
| 3253 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 3254 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3255 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3256 | mNormalSink = mOutputSink; |
| 3257 | } |
| 3258 | #ifdef AUDIO_WATCHDOG |
| 3259 | if (mAudioWatchdog != 0) { |
| 3260 | mAudioWatchdog->pause(); |
| 3261 | } |
| 3262 | #endif |
| 3263 | } else { |
| 3264 | sq->end(false /*didModify*/); |
| 3265 | } |
| 3266 | } |
| 3267 | PlaybackThread::threadLoop_standby(); |
| 3268 | } |
| 3269 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3270 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l() |
| 3271 | { |
| 3272 | return false; |
| 3273 | } |
| 3274 | |
| 3275 | bool AudioFlinger::PlaybackThread::shouldStandby_l() |
| 3276 | { |
| 3277 | return !mStandby; |
| 3278 | } |
| 3279 | |
| 3280 | bool AudioFlinger::PlaybackThread::waitingAsyncCallback() |
| 3281 | { |
| 3282 | Mutex::Autolock _l(mLock); |
| 3283 | return waitingAsyncCallback_l(); |
| 3284 | } |
| 3285 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3286 | // shared by MIXER and DIRECT, overridden by DUPLICATING |
| 3287 | void AudioFlinger::PlaybackThread::threadLoop_standby() |
| 3288 | { |
| 3289 | ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended); |
| 3290 | mOutput->stream->common.standby(&mOutput->stream->common); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3291 | if (mUseAsyncWrite != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3292 | // discard any pending drain or write ack by incrementing sequence |
| 3293 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 3294 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3295 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 3296 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 3297 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3298 | } |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 3299 | mHwPaused = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3300 | } |
| 3301 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 3302 | void AudioFlinger::PlaybackThread::onAddNewTrack_l() |
| 3303 | { |
| 3304 | ALOGV("signal playback thread"); |
| 3305 | broadcast_l(); |
| 3306 | } |
| 3307 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3308 | void AudioFlinger::MixerThread::threadLoop_mix() |
| 3309 | { |
| 3310 | // obtain the presentation timestamp of the next output buffer |
| 3311 | int64_t pts; |
| 3312 | status_t status = INVALID_OPERATION; |
| 3313 | |
| 3314 | if (mNormalSink != 0) { |
| 3315 | status = mNormalSink->getNextWriteTimestamp(&pts); |
| 3316 | } else { |
| 3317 | status = mOutputSink->getNextWriteTimestamp(&pts); |
| 3318 | } |
| 3319 | |
| 3320 | if (status != NO_ERROR) { |
| 3321 | pts = AudioBufferProvider::kInvalidPTS; |
| 3322 | } |
| 3323 | |
| 3324 | // mix buffers... |
| 3325 | mAudioMixer->process(pts); |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 3326 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3327 | // increase sleep time progressively when application underrun condition clears. |
| 3328 | // Only increase sleep time if the mixer is ready for two consecutive times to avoid |
| 3329 | // that a steady state of alternating ready/not ready conditions keeps the sleep time |
| 3330 | // such that we would underrun the audio HAL. |
| 3331 | if ((sleepTime == 0) && (sleepTimeShift > 0)) { |
| 3332 | sleepTimeShift--; |
| 3333 | } |
| 3334 | sleepTime = 0; |
| 3335 | standbyTime = systemTime() + standbyDelay; |
| 3336 | //TODO: delay standby when effects have a tail |
Glenn Kasten | 4c053ea | 2014-09-28 14:41:07 -0700 | [diff] [blame] | 3337 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | void AudioFlinger::MixerThread::threadLoop_sleepTime() |
| 3341 | { |
| 3342 | // If no tracks are ready, sleep once for the duration of an output |
| 3343 | // buffer size, then write 0s to the output |
| 3344 | if (sleepTime == 0) { |
| 3345 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 3346 | sleepTime = activeSleepTime >> sleepTimeShift; |
| 3347 | if (sleepTime < kMinThreadSleepTimeUs) { |
| 3348 | sleepTime = kMinThreadSleepTimeUs; |
| 3349 | } |
| 3350 | // reduce sleep time in case of consecutive application underruns to avoid |
| 3351 | // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer |
| 3352 | // duration we would end up writing less data than needed by the audio HAL if |
| 3353 | // the condition persists. |
| 3354 | if (sleepTimeShift < kMaxThreadSleepTimeShift) { |
| 3355 | sleepTimeShift++; |
| 3356 | } |
| 3357 | } else { |
| 3358 | sleepTime = idleSleepTime; |
| 3359 | } |
| 3360 | } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3361 | // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared |
| 3362 | // before effects processing or output. |
| 3363 | if (mMixerBufferValid) { |
| 3364 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3365 | } else { |
| 3366 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 3367 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3368 | sleepTime = 0; |
| 3369 | ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED), |
| 3370 | "anticipated start"); |
| 3371 | } |
| 3372 | // TODO add standby time extension fct of effect tail |
| 3373 | } |
| 3374 | |
| 3375 | // prepareTracks_l() must be called with ThreadBase::mLock held |
| 3376 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l( |
| 3377 | Vector< sp<Track> > *tracksToRemove) |
| 3378 | { |
| 3379 | |
| 3380 | mixer_state mixerStatus = MIXER_IDLE; |
| 3381 | // find out which tracks need to be processed |
| 3382 | size_t count = mActiveTracks.size(); |
| 3383 | size_t mixedTracks = 0; |
| 3384 | size_t tracksWithEffect = 0; |
| 3385 | // counts only _active_ fast tracks |
| 3386 | size_t fastTracks = 0; |
| 3387 | uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset |
| 3388 | |
| 3389 | float masterVolume = mMasterVolume; |
| 3390 | bool masterMute = mMasterMute; |
| 3391 | |
| 3392 | if (masterMute) { |
| 3393 | masterVolume = 0; |
| 3394 | } |
| 3395 | // Delegate master volume control to effect in output mix effect chain if needed |
| 3396 | sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3397 | if (chain != 0) { |
| 3398 | uint32_t v = (uint32_t)(masterVolume * (1 << 24)); |
| 3399 | chain->setVolume_l(&v, &v); |
| 3400 | masterVolume = (float)((v + (1 << 23)) >> 24); |
| 3401 | chain.clear(); |
| 3402 | } |
| 3403 | |
| 3404 | // prepare a new state to push |
| 3405 | FastMixerStateQueue *sq = NULL; |
| 3406 | FastMixerState *state = NULL; |
| 3407 | bool didModify = false; |
| 3408 | FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3409 | if (mFastMixer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3410 | sq = mFastMixer->sq(); |
| 3411 | state = sq->begin(); |
| 3412 | } |
| 3413 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3414 | mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found. |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3415 | mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3416 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3417 | for (size_t i=0 ; i<count ; i++) { |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3418 | const sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3419 | if (t == 0) { |
| 3420 | continue; |
| 3421 | } |
| 3422 | |
| 3423 | // this const just means the local variable doesn't change |
| 3424 | Track* const track = t.get(); |
| 3425 | |
| 3426 | // process fast tracks |
| 3427 | if (track->isFastTrack()) { |
| 3428 | |
| 3429 | // It's theoretically possible (though unlikely) for a fast track to be created |
| 3430 | // and then removed within the same normal mix cycle. This is not a problem, as |
| 3431 | // the track never becomes active so it's fast mixer slot is never touched. |
| 3432 | // The converse, of removing an (active) track and then creating a new track |
| 3433 | // at the identical fast mixer slot within the same normal mix cycle, |
| 3434 | // is impossible because the slot isn't marked available until the end of each cycle. |
| 3435 | int j = track->mFastIndex; |
| 3436 | ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks); |
| 3437 | ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j))); |
| 3438 | FastTrack *fastTrack = &state->mFastTracks[j]; |
| 3439 | |
| 3440 | // Determine whether the track is currently in underrun condition, |
| 3441 | // and whether it had a recent underrun. |
| 3442 | FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j]; |
| 3443 | FastTrackUnderruns underruns = ftDump->mUnderruns; |
| 3444 | uint32_t recentFull = (underruns.mBitFields.mFull - |
| 3445 | track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK; |
| 3446 | uint32_t recentPartial = (underruns.mBitFields.mPartial - |
| 3447 | track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK; |
| 3448 | uint32_t recentEmpty = (underruns.mBitFields.mEmpty - |
| 3449 | track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK; |
| 3450 | uint32_t recentUnderruns = recentPartial + recentEmpty; |
| 3451 | track->mObservedUnderruns = underruns; |
| 3452 | // don't count underruns that occur while stopping or pausing |
| 3453 | // or stopped which can occur when flush() is called while active |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3454 | if (!(track->isStopping() || track->isPausing() || track->isStopped()) && |
| 3455 | recentUnderruns > 0) { |
| 3456 | // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun |
| 3457 | track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | // This is similar to the state machine for normal tracks, |
| 3461 | // with a few modifications for fast tracks. |
| 3462 | bool isActive = true; |
| 3463 | switch (track->mState) { |
| 3464 | case TrackBase::STOPPING_1: |
| 3465 | // track stays active in STOPPING_1 state until first underrun |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3466 | if (recentUnderruns > 0 || track->isTerminated()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3467 | track->mState = TrackBase::STOPPING_2; |
| 3468 | } |
| 3469 | break; |
| 3470 | case TrackBase::PAUSING: |
| 3471 | // ramp down is not yet implemented |
| 3472 | track->setPaused(); |
| 3473 | break; |
| 3474 | case TrackBase::RESUMING: |
| 3475 | // ramp up is not yet implemented |
| 3476 | track->mState = TrackBase::ACTIVE; |
| 3477 | break; |
| 3478 | case TrackBase::ACTIVE: |
| 3479 | if (recentFull > 0 || recentPartial > 0) { |
| 3480 | // track has provided at least some frames recently: reset retry count |
| 3481 | track->mRetryCount = kMaxTrackRetries; |
| 3482 | } |
| 3483 | if (recentUnderruns == 0) { |
| 3484 | // no recent underruns: stay active |
| 3485 | break; |
| 3486 | } |
| 3487 | // there has recently been an underrun of some kind |
| 3488 | if (track->sharedBuffer() == 0) { |
| 3489 | // were any of the recent underruns "empty" (no frames available)? |
| 3490 | if (recentEmpty == 0) { |
| 3491 | // no, then ignore the partial underruns as they are allowed indefinitely |
| 3492 | break; |
| 3493 | } |
| 3494 | // there has recently been an "empty" underrun: decrement the retry counter |
| 3495 | if (--(track->mRetryCount) > 0) { |
| 3496 | break; |
| 3497 | } |
| 3498 | // indicate to client process that the track was disabled because of underrun; |
| 3499 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3500 | android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3501 | // remove from active list, but state remains ACTIVE [confusing but true] |
| 3502 | isActive = false; |
| 3503 | break; |
| 3504 | } |
| 3505 | // fall through |
| 3506 | case TrackBase::STOPPING_2: |
| 3507 | case TrackBase::PAUSED: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3508 | case TrackBase::STOPPED: |
| 3509 | case TrackBase::FLUSHED: // flush() while active |
| 3510 | // Check for presentation complete if track is inactive |
| 3511 | // We have consumed all the buffers of this track. |
| 3512 | // This would be incomplete if we auto-paused on underrun |
| 3513 | { |
| 3514 | size_t audioHALFrames = |
| 3515 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 3516 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3517 | if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) { |
| 3518 | // track stays in active list until presentation is complete |
| 3519 | break; |
| 3520 | } |
| 3521 | } |
| 3522 | if (track->isStopping_2()) { |
| 3523 | track->mState = TrackBase::STOPPED; |
| 3524 | } |
| 3525 | if (track->isStopped()) { |
| 3526 | // Can't reset directly, as fast mixer is still polling this track |
| 3527 | // track->reset(); |
| 3528 | // So instead mark this track as needing to be reset after push with ack |
| 3529 | resetMask |= 1 << i; |
| 3530 | } |
| 3531 | isActive = false; |
| 3532 | break; |
| 3533 | case TrackBase::IDLE: |
| 3534 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3535 | LOG_ALWAYS_FATAL("unexpected track state %d", track->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | if (isActive) { |
| 3539 | // was it previously inactive? |
| 3540 | if (!(state->mTrackMask & (1 << j))) { |
| 3541 | ExtendedAudioBufferProvider *eabp = track; |
| 3542 | VolumeProvider *vp = track; |
| 3543 | fastTrack->mBufferProvider = eabp; |
| 3544 | fastTrack->mVolumeProvider = vp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3545 | fastTrack->mChannelMask = track->mChannelMask; |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3546 | fastTrack->mFormat = track->mFormat; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3547 | fastTrack->mGeneration++; |
| 3548 | state->mTrackMask |= 1 << j; |
| 3549 | didModify = true; |
| 3550 | // no acknowledgement required for newly active tracks |
| 3551 | } |
| 3552 | // cache the combined master volume and stream type volume for fast mixer; this |
| 3553 | // lacks any synchronization or barrier so VolumeProvider may read a stale value |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3554 | track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3555 | ++fastTracks; |
| 3556 | } else { |
| 3557 | // was it previously active? |
| 3558 | if (state->mTrackMask & (1 << j)) { |
| 3559 | fastTrack->mBufferProvider = NULL; |
| 3560 | fastTrack->mGeneration++; |
| 3561 | state->mTrackMask &= ~(1 << j); |
| 3562 | didModify = true; |
| 3563 | // If any fast tracks were removed, we must wait for acknowledgement |
| 3564 | // because we're about to decrement the last sp<> on those tracks. |
| 3565 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3566 | } else { |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 3567 | LOG_ALWAYS_FATAL("fast track %d should have been active", j); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3568 | } |
| 3569 | tracksToRemove->add(track); |
| 3570 | // Avoids a misleading display in dumpsys |
| 3571 | track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL; |
| 3572 | } |
| 3573 | continue; |
| 3574 | } |
| 3575 | |
| 3576 | { // local variable scope to avoid goto warning |
| 3577 | |
| 3578 | audio_track_cblk_t* cblk = track->cblk(); |
| 3579 | |
| 3580 | // The first time a track is added we wait |
| 3581 | // for all its buffers to be filled before processing it |
| 3582 | int name = track->name(); |
| 3583 | // make sure that we have enough frames to mix one full buffer. |
| 3584 | // enforce this condition only once to enable draining the buffer in case the client |
| 3585 | // app does not call stop() and relies on underrun to stop: |
| 3586 | // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed |
| 3587 | // during last round |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3588 | size_t desiredFrames; |
Glenn Kasten | 9fdcb0a | 2013-06-26 16:11:36 -0700 | [diff] [blame] | 3589 | uint32_t sr = track->sampleRate(); |
| 3590 | if (sr == mSampleRate) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3591 | desiredFrames = mNormalFrameCount; |
| 3592 | } else { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 3593 | desiredFrames = sourceFramesNeeded(sr, mNormalFrameCount, mSampleRate); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3594 | // add frames already consumed but not yet released by the resampler |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 3595 | // because mAudioTrackServerProxy->framesReady() will include these frames |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3596 | desiredFrames += mAudioMixer->getUnreleasedFrames(track->name()); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3597 | #if 0 |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3598 | // the minimum track buffer size is normally twice the number of frames necessary |
| 3599 | // to fill one buffer and the resampler should not leave more than one buffer worth |
| 3600 | // of unreleased frames after each pass, but just in case... |
| 3601 | ALOG_ASSERT(desiredFrames <= cblk->frameCount_); |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 3602 | #endif |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3603 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3604 | uint32_t minFrames = 1; |
| 3605 | if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() && |
| 3606 | (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3607 | minFrames = desiredFrames; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3608 | } |
Eric Laurent | 13e4c96 | 2013-12-20 17:36:01 -0800 | [diff] [blame] | 3609 | |
| 3610 | size_t framesReady = track->framesReady(); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 3611 | if (ATRACE_ENABLED()) { |
| 3612 | // I wish we had formatted trace names |
| 3613 | char traceName[16]; |
| 3614 | strcpy(traceName, "nRdy"); |
| 3615 | int name = track->name(); |
| 3616 | if (AudioMixer::TRACK0 <= name && |
| 3617 | name < (int) (AudioMixer::TRACK0 + AudioMixer::MAX_NUM_TRACKS)) { |
| 3618 | name -= AudioMixer::TRACK0; |
| 3619 | traceName[4] = (name / 10) + '0'; |
| 3620 | traceName[5] = (name % 10) + '0'; |
| 3621 | } else { |
| 3622 | traceName[4] = '?'; |
| 3623 | traceName[5] = '?'; |
| 3624 | } |
| 3625 | traceName[6] = '\0'; |
| 3626 | ATRACE_INT(traceName, framesReady); |
| 3627 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3628 | if ((framesReady >= minFrames) && track->isReady() && |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3629 | !track->isPaused() && !track->isTerminated()) |
| 3630 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3631 | 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] | 3632 | |
| 3633 | mixedTracks++; |
| 3634 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3635 | // track->mainBuffer() != mSinkBuffer or mMixerBuffer means |
| 3636 | // there is an effect chain connected to the track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3637 | chain.clear(); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3638 | if (track->mainBuffer() != mSinkBuffer && |
| 3639 | track->mainBuffer() != mMixerBuffer) { |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3640 | if (mEffectBufferEnabled) { |
| 3641 | mEffectBufferValid = true; // Later can set directly. |
| 3642 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3643 | chain = getEffectChain_l(track->sessionId()); |
| 3644 | // Delegate volume control to effect in track effect chain if needed |
| 3645 | if (chain != 0) { |
| 3646 | tracksWithEffect++; |
| 3647 | } else { |
| 3648 | ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on " |
| 3649 | "session %d", |
| 3650 | name, track->sessionId()); |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | |
| 3655 | int param = AudioMixer::VOLUME; |
| 3656 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 3657 | // no ramp for the first volume setting |
| 3658 | track->mFillingUpStatus = Track::FS_ACTIVE; |
| 3659 | if (track->mState == TrackBase::RESUMING) { |
| 3660 | track->mState = TrackBase::ACTIVE; |
| 3661 | param = AudioMixer::RAMP_VOLUME; |
| 3662 | } |
| 3663 | mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL); |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3664 | // FIXME should not make a decision based on mServer |
| 3665 | } else if (cblk->mServer != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3666 | // If the track is stopped before the first frame was mixed, |
| 3667 | // do not apply ramp |
| 3668 | param = AudioMixer::RAMP_VOLUME; |
| 3669 | } |
| 3670 | |
| 3671 | // compute volume for this track |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3672 | uint32_t vl, vr; // in U8.24 integer format |
| 3673 | float vlf, vrf, vaf; // in [0.0, 1.0] float format |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 3674 | if (track->isPausing() || mStreamTypes[track->streamType()].mute) { |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3675 | vl = vr = 0; |
| 3676 | vlf = vrf = vaf = 0.; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3677 | if (track->isPausing()) { |
| 3678 | track->setPaused(); |
| 3679 | } |
| 3680 | } else { |
| 3681 | |
| 3682 | // read original volumes with volume control |
| 3683 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 3684 | float v = masterVolume * typeVolume; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3685 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3686 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3687 | vlf = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 3688 | vrf = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3689 | // 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] | 3690 | if (vlf > GAIN_FLOAT_UNITY) { |
| 3691 | ALOGV("Track left volume out of range: %.3g", vlf); |
| 3692 | vlf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3693 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 3694 | if (vrf > GAIN_FLOAT_UNITY) { |
| 3695 | ALOGV("Track right volume out of range: %.3g", vrf); |
| 3696 | vrf = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3697 | } |
| 3698 | // now apply the master volume and stream type volume |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3699 | vlf *= v; |
| 3700 | vrf *= v; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3701 | // 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] | 3702 | // then derive vl and vr as U8.24 versions for the effect chain |
| 3703 | const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT; |
| 3704 | vl = (uint32_t) (scaleto8_24 * vlf); |
| 3705 | vr = (uint32_t) (scaleto8_24 * vrf); |
| 3706 | // vl and vr are now in U8.24 format |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3707 | uint16_t sendLevel = proxy->getSendLevel_U4_12(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3708 | // send level comes from shared memory and so may be corrupt |
| 3709 | if (sendLevel > MAX_GAIN_INT) { |
| 3710 | ALOGV("Track send level out of range: %04X", sendLevel); |
| 3711 | sendLevel = MAX_GAIN_INT; |
| 3712 | } |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3713 | // vaf is represented as [0.0, 1.0] float by rescaling sendLevel |
| 3714 | vaf = v * sendLevel * (1. / MAX_GAIN_INT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3715 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3716 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3717 | // Delegate volume control to effect in track effect chain if needed |
| 3718 | if (chain != 0 && chain->setVolume_l(&vl, &vr)) { |
| 3719 | // Do not ramp volume if volume is controlled by effect |
| 3720 | param = AudioMixer::VOLUME; |
Bryant Liu | b6be7f2 | 2014-06-12 22:02:41 +0800 | [diff] [blame] | 3721 | // Update remaining floating point volume levels |
| 3722 | vlf = (float)vl / (1 << 24); |
| 3723 | vrf = (float)vr / (1 << 24); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3724 | track->mHasVolumeController = true; |
| 3725 | } else { |
| 3726 | // force no volume ramp when volume controller was just disabled or removed |
| 3727 | // from effect chain to avoid volume spike |
| 3728 | if (track->mHasVolumeController) { |
| 3729 | param = AudioMixer::VOLUME; |
| 3730 | } |
| 3731 | track->mHasVolumeController = false; |
| 3732 | } |
| 3733 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3734 | // XXX: these things DON'T need to be done each time |
| 3735 | mAudioMixer->setBufferProvider(name, track); |
| 3736 | mAudioMixer->enable(name); |
| 3737 | |
Andy Hung | 6be4940 | 2014-05-30 10:42:03 -0700 | [diff] [blame] | 3738 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf); |
| 3739 | mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf); |
| 3740 | mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3741 | mAudioMixer->setParameter( |
| 3742 | name, |
| 3743 | AudioMixer::TRACK, |
| 3744 | AudioMixer::FORMAT, (void *)track->format()); |
| 3745 | mAudioMixer->setParameter( |
| 3746 | name, |
| 3747 | AudioMixer::TRACK, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3748 | AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask()); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3749 | mAudioMixer->setParameter( |
| 3750 | name, |
| 3751 | AudioMixer::TRACK, |
| 3752 | AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3753 | // 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] | 3754 | uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3755 | uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 3756 | if (reqSampleRate == 0) { |
| 3757 | reqSampleRate = mSampleRate; |
| 3758 | } else if (reqSampleRate > maxSampleRate) { |
| 3759 | reqSampleRate = maxSampleRate; |
| 3760 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3761 | mAudioMixer->setParameter( |
| 3762 | name, |
| 3763 | AudioMixer::RESAMPLE, |
| 3764 | AudioMixer::SAMPLE_RATE, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 3765 | (void *)(uintptr_t)reqSampleRate); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3766 | /* |
| 3767 | * Select the appropriate output buffer for the track. |
| 3768 | * |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3769 | * Tracks with effects go into their own effects chain buffer |
| 3770 | * and from there into either mEffectBuffer or mSinkBuffer. |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3771 | * |
| 3772 | * Other tracks can use mMixerBuffer for higher precision |
| 3773 | * channel accumulation. If this buffer is enabled |
| 3774 | * (mMixerBufferEnabled true), then selected tracks will accumulate |
| 3775 | * into it. |
| 3776 | * |
| 3777 | */ |
| 3778 | if (mMixerBufferEnabled |
| 3779 | && (track->mainBuffer() == mSinkBuffer |
| 3780 | || track->mainBuffer() == mMixerBuffer)) { |
| 3781 | mAudioMixer->setParameter( |
| 3782 | name, |
| 3783 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3784 | AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3785 | mAudioMixer->setParameter( |
| 3786 | name, |
| 3787 | AudioMixer::TRACK, |
| 3788 | AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer); |
| 3789 | // TODO: override track->mainBuffer()? |
| 3790 | mMixerBufferValid = true; |
| 3791 | } else { |
| 3792 | mAudioMixer->setParameter( |
| 3793 | name, |
| 3794 | AudioMixer::TRACK, |
Andy Hung | 7882070 | 2014-02-28 16:23:02 -0800 | [diff] [blame] | 3795 | AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3796 | mAudioMixer->setParameter( |
| 3797 | name, |
| 3798 | AudioMixer::TRACK, |
| 3799 | AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer()); |
| 3800 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3801 | mAudioMixer->setParameter( |
| 3802 | name, |
| 3803 | AudioMixer::TRACK, |
| 3804 | AudioMixer::AUX_BUFFER, (void *)track->auxBuffer()); |
| 3805 | |
| 3806 | // reset retry count |
| 3807 | track->mRetryCount = kMaxTrackRetries; |
| 3808 | |
| 3809 | // If one track is ready, set the mixer ready if: |
| 3810 | // - the mixer was not ready during previous round OR |
| 3811 | // - no other track is not ready |
| 3812 | if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY || |
| 3813 | mixerStatus != MIXER_TRACKS_ENABLED) { |
| 3814 | mixerStatus = MIXER_TRACKS_READY; |
| 3815 | } |
| 3816 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3817 | if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) { |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 3818 | track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 3819 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3820 | // clear effect chain input buffer if an active track underruns to avoid sending |
| 3821 | // previous audio buffer again to effects |
| 3822 | chain = getEffectChain_l(track->sessionId()); |
| 3823 | if (chain != 0) { |
| 3824 | chain->clearInputBuffer(); |
| 3825 | } |
| 3826 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 3827 | 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] | 3828 | if ((track->sharedBuffer() != 0) || track->isTerminated() || |
| 3829 | track->isStopped() || track->isPaused()) { |
| 3830 | // We have consumed all the buffers of this track. |
| 3831 | // Remove it from the list of active tracks. |
| 3832 | // TODO: use actual buffer filling status instead of latency when available from |
| 3833 | // audio HAL |
| 3834 | size_t audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 3835 | size_t framesWritten = mBytesWritten / mFrameSize; |
| 3836 | if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) { |
| 3837 | if (track->isStopped()) { |
| 3838 | track->reset(); |
| 3839 | } |
| 3840 | tracksToRemove->add(track); |
| 3841 | } |
| 3842 | } else { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3843 | // No buffers for this track. Give it a few chances to |
| 3844 | // fill a buffer, then remove it from active list. |
| 3845 | if (--(track->mRetryCount) <= 0) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 3846 | 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] | 3847 | tracksToRemove->add(track); |
| 3848 | // indicate to client process that the track was disabled because of underrun; |
| 3849 | // it will then automatically call start() when data is available |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 3850 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3851 | // If one track is not ready, mark the mixer also not ready if: |
| 3852 | // - the mixer was ready during previous round OR |
| 3853 | // - no other track is ready |
| 3854 | } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY || |
| 3855 | mixerStatus != MIXER_TRACKS_READY) { |
| 3856 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 3857 | } |
| 3858 | } |
| 3859 | mAudioMixer->disable(name); |
| 3860 | } |
| 3861 | |
| 3862 | } // local variable scope to avoid goto warning |
| 3863 | track_is_ready: ; |
| 3864 | |
| 3865 | } |
| 3866 | |
| 3867 | // Push the new FastMixer state if necessary |
| 3868 | bool pauseAudioWatchdog = false; |
| 3869 | if (didModify) { |
| 3870 | state->mFastTracksGen++; |
| 3871 | // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle |
| 3872 | if (kUseFastMixer == FastMixer_Dynamic && |
| 3873 | state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) { |
| 3874 | state->mCommand = FastMixerState::COLD_IDLE; |
| 3875 | state->mColdFutexAddr = &mFastMixerFutex; |
| 3876 | state->mColdGen++; |
| 3877 | mFastMixerFutex = 0; |
| 3878 | if (kUseFastMixer == FastMixer_Dynamic) { |
| 3879 | mNormalSink = mOutputSink; |
| 3880 | } |
| 3881 | // If we go into cold idle, need to wait for acknowledgement |
| 3882 | // so that fast mixer stops doing I/O. |
| 3883 | block = FastMixerStateQueue::BLOCK_UNTIL_ACKED; |
| 3884 | pauseAudioWatchdog = true; |
| 3885 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3886 | } |
| 3887 | if (sq != NULL) { |
| 3888 | sq->end(didModify); |
| 3889 | sq->push(block); |
| 3890 | } |
| 3891 | #ifdef AUDIO_WATCHDOG |
| 3892 | if (pauseAudioWatchdog && mAudioWatchdog != 0) { |
| 3893 | mAudioWatchdog->pause(); |
| 3894 | } |
| 3895 | #endif |
| 3896 | |
| 3897 | // Now perform the deferred reset on fast tracks that have stopped |
| 3898 | while (resetMask != 0) { |
| 3899 | size_t i = __builtin_ctz(resetMask); |
| 3900 | ALOG_ASSERT(i < count); |
| 3901 | resetMask &= ~(1 << i); |
| 3902 | sp<Track> t = mActiveTracks[i].promote(); |
| 3903 | if (t == 0) { |
| 3904 | continue; |
| 3905 | } |
| 3906 | Track* track = t.get(); |
| 3907 | ALOG_ASSERT(track->isFastTrack() && track->isStopped()); |
| 3908 | track->reset(); |
| 3909 | } |
| 3910 | |
| 3911 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3912 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3913 | |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 3914 | if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) { |
| 3915 | mEffectBufferValid = true; |
Marco Nelissen | ac30214 | 2014-10-20 13:15:38 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | if (mEffectBufferValid) { |
Marco Nelissen | 57088b5 | 2014-10-17 16:39:39 -0700 | [diff] [blame] | 3919 | // as long as there are effects we should clear the effects buffer, to avoid |
| 3920 | // passing a non-clean buffer to the effect chain |
| 3921 | memset(mEffectBuffer, 0, mEffectBufferSize); |
Eric Laurent | 97d547d | 2014-09-02 14:45:53 -0700 | [diff] [blame] | 3922 | } |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3923 | // sink or mix buffer must be cleared if all tracks are connected to an |
| 3924 | // effect chain as in this case the mixer will not write to the sink or mix buffer |
| 3925 | // and track effects will accumulate into it |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 3926 | if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3927 | (mixedTracks == 0 && fastTracks > 0))) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3928 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 3929 | if (mMixerBufferValid) { |
| 3930 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 3931 | // TODO: In testing, mSinkBuffer below need not be cleared because |
| 3932 | // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer |
| 3933 | // after mixing. |
| 3934 | // |
| 3935 | // To enforce this guarantee: |
| 3936 | // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) || |
| 3937 | // (mixedTracks == 0 && fastTracks > 0)) |
| 3938 | // must imply MIXER_TRACKS_READY. |
| 3939 | // Later, we may clear buffers regardless, and skip much of this logic. |
| 3940 | } |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 3941 | // FIXME as a performance optimization, should remember previous zero status |
Andy Hung | 5567aaf | 2014-07-17 14:00:07 -0700 | [diff] [blame] | 3942 | memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | // if any fast tracks, then status is ready |
| 3946 | mMixerStatusIgnoringFastTracks = mixerStatus; |
| 3947 | if (fastTracks > 0) { |
| 3948 | mixerStatus = MIXER_TRACKS_READY; |
| 3949 | } |
| 3950 | return mixerStatus; |
| 3951 | } |
| 3952 | |
| 3953 | // getTrackName_l() must be called with ThreadBase::mLock held |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3954 | int AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask, |
| 3955 | audio_format_t format, int sessionId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3956 | { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 3957 | return mAudioMixer->getTrackName(channelMask, format, sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
| 3961 | void AudioFlinger::MixerThread::deleteTrackName_l(int name) |
| 3962 | { |
| 3963 | ALOGV("remove track (%d) and delete from mixer", name); |
| 3964 | mAudioMixer->deleteTrackName(name); |
| 3965 | } |
| 3966 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3967 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 3968 | bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair, |
| 3969 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3970 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3971 | bool reconfig = false; |
| 3972 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3973 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3974 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3975 | // if !&IDLE, holds the FastMixer state to restore after new parameters processed |
| 3976 | FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE; |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 3977 | if (mFastMixer != 0) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3978 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 3979 | FastMixerState *state = sq->begin(); |
| 3980 | if (!(state->mCommand & FastMixerState::IDLE)) { |
| 3981 | previousCommand = state->mCommand; |
| 3982 | state->mCommand = FastMixerState::HOT_IDLE; |
| 3983 | sq->end(); |
| 3984 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED); |
| 3985 | } else { |
| 3986 | sq->end(false /*didModify*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3987 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3988 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3989 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3990 | AudioParameter param = AudioParameter(keyValuePair); |
| 3991 | int value; |
| 3992 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 3993 | reconfig = true; |
| 3994 | } |
| 3995 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3996 | if (!isValidPcmSinkFormat((audio_format_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 3997 | status = BAD_VALUE; |
| 3998 | } else { |
| 3999 | // no need to save value, since it's constant |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4000 | reconfig = true; |
| 4001 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4002 | } |
| 4003 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 4004 | if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) { |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4005 | status = BAD_VALUE; |
| 4006 | } else { |
| 4007 | // no need to save value, since it's constant |
| 4008 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4009 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4010 | } |
| 4011 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4012 | // do not accept frame count changes if tracks are open as the track buffer |
| 4013 | // size depends on frame count and correct behavior would not be guaranteed |
| 4014 | // if frame count is changed after track creation |
| 4015 | if (!mTracks.isEmpty()) { |
| 4016 | status = INVALID_OPERATION; |
| 4017 | } else { |
| 4018 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4019 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4020 | } |
| 4021 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4022 | #ifdef ADD_BATTERY_DATA |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4023 | // when changing the audio output device, call addBatteryData to notify |
| 4024 | // the change |
| 4025 | if (mOutDevice != value) { |
| 4026 | uint32_t params = 0; |
| 4027 | // check whether speaker is on |
| 4028 | if (value & AUDIO_DEVICE_OUT_SPEAKER) { |
| 4029 | params |= IMediaPlayerService::kBatteryDataSpeakerOn; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4030 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4031 | |
| 4032 | audio_devices_t deviceWithoutSpeaker |
| 4033 | = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4034 | // check if any other device (except speaker) is on |
| 4035 | if (value & deviceWithoutSpeaker ) { |
| 4036 | params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn; |
| 4037 | } |
| 4038 | |
| 4039 | if (params != 0) { |
| 4040 | addBatteryData(params); |
| 4041 | } |
| 4042 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4043 | #endif |
| 4044 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4045 | // forward device change to effects that have requested to be |
| 4046 | // aware of attached audio device. |
| 4047 | if (value != AUDIO_DEVICE_NONE) { |
| 4048 | mOutDevice = value; |
| 4049 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4050 | mEffectChains[i]->setDevice_l(mOutDevice); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4051 | } |
| 4052 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4053 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4054 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4055 | if (status == NO_ERROR) { |
| 4056 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4057 | keyValuePair.string()); |
| 4058 | if (!mStandby && status == INVALID_OPERATION) { |
| 4059 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 4060 | mStandby = true; |
| 4061 | mBytesWritten = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4062 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4063 | keyValuePair.string()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4064 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4065 | if (status == NO_ERROR && reconfig) { |
| 4066 | readOutputParameters_l(); |
| 4067 | delete mAudioMixer; |
| 4068 | mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate); |
| 4069 | for (size_t i = 0; i < mTracks.size() ; i++) { |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4070 | int name = getTrackName_l(mTracks[i]->mChannelMask, |
| 4071 | mTracks[i]->mFormat, mTracks[i]->mSessionId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4072 | if (name < 0) { |
| 4073 | break; |
| 4074 | } |
| 4075 | mTracks[i]->mName = name; |
| 4076 | } |
| 4077 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 4078 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | if (!(previousCommand & FastMixerState::IDLE)) { |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 4082 | ALOG_ASSERT(mFastMixer != 0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4083 | FastMixerStateQueue *sq = mFastMixer->sq(); |
| 4084 | FastMixerState *state = sq->begin(); |
| 4085 | ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE); |
| 4086 | state->mCommand = previousCommand; |
| 4087 | sq->end(); |
| 4088 | sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED); |
| 4089 | } |
| 4090 | |
| 4091 | return reconfig; |
| 4092 | } |
| 4093 | |
| 4094 | |
| 4095 | void AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args) |
| 4096 | { |
| 4097 | const size_t SIZE = 256; |
| 4098 | char buffer[SIZE]; |
| 4099 | String8 result; |
| 4100 | |
| 4101 | PlaybackThread::dumpInternals(fd, args); |
| 4102 | |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 4103 | dprintf(fd, " AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4104 | |
| 4105 | // 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] | 4106 | const FastMixerDumpState copy(mFastMixerDumpState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4107 | copy.dump(fd); |
| 4108 | |
| 4109 | #ifdef STATE_QUEUE_DUMP |
| 4110 | // Similar for state queue |
| 4111 | StateQueueObserverDump observerCopy = mStateQueueObserverDump; |
| 4112 | observerCopy.dump(fd); |
| 4113 | StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump; |
| 4114 | mutatorCopy.dump(fd); |
| 4115 | #endif |
| 4116 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4117 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4118 | // Write the tee output to a .wav file |
| 4119 | dumpTee(fd, mTeeSource, mId); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 4120 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4121 | |
| 4122 | #ifdef AUDIO_WATCHDOG |
| 4123 | if (mAudioWatchdog != 0) { |
| 4124 | // Make a non-atomic copy of audio watchdog dump so it won't change underneath us |
| 4125 | AudioWatchdogDump wdCopy = mAudioWatchdogDump; |
| 4126 | wdCopy.dump(fd); |
| 4127 | } |
| 4128 | #endif |
| 4129 | } |
| 4130 | |
| 4131 | uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const |
| 4132 | { |
| 4133 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4134 | } |
| 4135 | |
| 4136 | uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const |
| 4137 | { |
| 4138 | return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000); |
| 4139 | } |
| 4140 | |
| 4141 | void AudioFlinger::MixerThread::cacheParameters_l() |
| 4142 | { |
| 4143 | PlaybackThread::cacheParameters_l(); |
| 4144 | |
| 4145 | // FIXME: Relaxed timing because of a certain device that can't meet latency |
| 4146 | // Should be reduced to 2x after the vendor fixes the driver issue |
| 4147 | // increase threshold again due to low power audio mode. The way this warning |
| 4148 | // threshold is calculated and its usefulness should be reconsidered anyway. |
| 4149 | maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15; |
| 4150 | } |
| 4151 | |
| 4152 | // ---------------------------------------------------------------------------- |
| 4153 | |
| 4154 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4155 | AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device) |
| 4156 | : PlaybackThread(audioFlinger, output, id, device, DIRECT) |
| 4157 | // mLeftVolFloat, mRightVolFloat |
| 4158 | { |
| 4159 | } |
| 4160 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4161 | AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, |
| 4162 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device, |
| 4163 | ThreadBase::type_t type) |
| 4164 | : PlaybackThread(audioFlinger, output, id, device, type) |
| 4165 | // mLeftVolFloat, mRightVolFloat |
| 4166 | { |
| 4167 | } |
| 4168 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4169 | AudioFlinger::DirectOutputThread::~DirectOutputThread() |
| 4170 | { |
| 4171 | } |
| 4172 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4173 | void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack) |
| 4174 | { |
| 4175 | audio_track_cblk_t* cblk = track->cblk(); |
| 4176 | float left, right; |
| 4177 | |
| 4178 | if (mMasterMute || mStreamTypes[track->streamType()].mute) { |
| 4179 | left = right = 0; |
| 4180 | } else { |
| 4181 | float typeVolume = mStreamTypes[track->streamType()].volume; |
| 4182 | float v = mMasterVolume * typeVolume; |
| 4183 | AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 4184 | gain_minifloat_packed_t vlr = proxy->getVolumeLR(); |
| 4185 | left = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 4186 | if (left > GAIN_FLOAT_UNITY) { |
| 4187 | left = GAIN_FLOAT_UNITY; |
| 4188 | } |
| 4189 | left *= v; |
| 4190 | right = float_from_gain(gain_minifloat_unpack_right(vlr)); |
| 4191 | if (right > GAIN_FLOAT_UNITY) { |
| 4192 | right = GAIN_FLOAT_UNITY; |
| 4193 | } |
| 4194 | right *= v; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | if (lastTrack) { |
| 4198 | if (left != mLeftVolFloat || right != mRightVolFloat) { |
| 4199 | mLeftVolFloat = left; |
| 4200 | mRightVolFloat = right; |
| 4201 | |
| 4202 | // Convert volumes from float to 8.24 |
| 4203 | uint32_t vl = (uint32_t)(left * (1 << 24)); |
| 4204 | uint32_t vr = (uint32_t)(right * (1 << 24)); |
| 4205 | |
| 4206 | // Delegate volume control to effect in track effect chain if needed |
| 4207 | // only one effect chain can be present on DirectOutputThread, so if |
| 4208 | // there is one, the track is connected to it |
| 4209 | if (!mEffectChains.isEmpty()) { |
| 4210 | mEffectChains[0]->setVolume_l(&vl, &vr); |
| 4211 | left = (float)vl / (1 << 24); |
| 4212 | right = (float)vr / (1 << 24); |
| 4213 | } |
| 4214 | if (mOutput->stream->set_volume) { |
| 4215 | mOutput->stream->set_volume(mOutput->stream, left, right); |
| 4216 | } |
| 4217 | } |
| 4218 | } |
| 4219 | } |
| 4220 | |
| 4221 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4222 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l( |
| 4223 | Vector< sp<Track> > *tracksToRemove |
| 4224 | ) |
| 4225 | { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4226 | size_t count = mActiveTracks.size(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4227 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4228 | bool doHwPause = false; |
| 4229 | bool doHwResume = false; |
| 4230 | bool flushPending = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4231 | |
| 4232 | // find out which tracks need to be processed |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4233 | for (size_t i = 0; i < count; i++) { |
| 4234 | sp<Track> t = mActiveTracks[i].promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4235 | // The track died recently |
| 4236 | if (t == 0) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4237 | continue; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | Track* const track = t.get(); |
| 4241 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4242 | // Only consider last track started for volume and mixer state control. |
| 4243 | // In theory an older track could underrun and restart after the new one starts |
| 4244 | // but as we only care about the transition phase between two tracks on a |
| 4245 | // direct output, it is not a problem to ignore the underrun case. |
| 4246 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4247 | bool last = l.get() == track; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4248 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4249 | if (mHwSupportsPause && track->isPausing()) { |
| 4250 | track->setPaused(); |
| 4251 | if (last && !mHwPaused) { |
| 4252 | doHwPause = true; |
| 4253 | mHwPaused = true; |
| 4254 | } |
| 4255 | tracksToRemove->add(track); |
| 4256 | } else if (track->isFlushPending()) { |
| 4257 | track->flushAck(); |
| 4258 | if (last) { |
| 4259 | flushPending = true; |
| 4260 | } |
| 4261 | } else if (mHwSupportsPause && track->isResumePending()){ |
| 4262 | track->resumeAck(); |
| 4263 | if (last) { |
| 4264 | if (mHwPaused) { |
| 4265 | doHwResume = true; |
| 4266 | mHwPaused = false; |
| 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4271 | // The first time a track is added we wait |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4272 | // for all its buffers to be filled before processing it. |
| 4273 | // Allow draining the buffer in case the client |
| 4274 | // app does not call stop() and relies on underrun to stop: |
| 4275 | // hence the test on (track->mRetryCount > 1). |
| 4276 | // If retryCount<=1 then track is about to underrun and be removed. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4277 | uint32_t minFrames; |
Phil Burk | 99adee3 | 2014-12-10 16:46:30 -0800 | [diff] [blame] | 4278 | if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing() |
| 4279 | && (track->mRetryCount > 1)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4280 | minFrames = mNormalFrameCount; |
| 4281 | } else { |
| 4282 | minFrames = 1; |
| 4283 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4284 | |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4285 | if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() && |
| 4286 | !track->isStopping_2() && !track->isStopped()) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4287 | { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4288 | ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4289 | |
| 4290 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4291 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4292 | // make sure processVolume_l() will apply new volume even if 0 |
| 4293 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4294 | if (!mHwSupportsPause) { |
| 4295 | track->resumeAck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | // compute volume for this track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4300 | processVolume_l(track, last); |
| 4301 | if (last) { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4302 | // reset retry count |
| 4303 | track->mRetryCount = kMaxTrackRetriesDirect; |
| 4304 | mActiveTrack = t; |
| 4305 | mixerStatus = MIXER_TRACKS_READY; |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4306 | if (usesHwAvSync() && mHwPaused) { |
| 4307 | doHwResume = true; |
| 4308 | mHwPaused = false; |
| 4309 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4310 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4311 | } else { |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4312 | // clear effect chain input buffer if the last active track started underruns |
| 4313 | // to avoid sending previous audio buffer again to effects |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4314 | if (!mEffectChains.isEmpty() && last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4315 | mEffectChains[0]->clearInputBuffer(); |
| 4316 | } |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4317 | if (track->isStopping_1()) { |
| 4318 | track->mState = TrackBase::STOPPING_2; |
| 4319 | } |
| 4320 | if ((track->sharedBuffer() != 0) || track->isStopped() || |
| 4321 | track->isStopping_2() || track->isPaused()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4322 | // We have consumed all the buffers of this track. |
| 4323 | // Remove it from the list of active tracks. |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4324 | size_t audioHALFrames; |
| 4325 | if (audio_is_linear_pcm(mFormat)) { |
| 4326 | audioHALFrames = (latency_l() * mSampleRate) / 1000; |
| 4327 | } else { |
| 4328 | audioHALFrames = 0; |
| 4329 | } |
| 4330 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4331 | size_t framesWritten = mBytesWritten / mFrameSize; |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4332 | if (mStandby || !last || |
| 4333 | track->presentationComplete(framesWritten, audioHALFrames)) { |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 4334 | if (track->isStopping_2()) { |
| 4335 | track->mState = TrackBase::STOPPED; |
| 4336 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4337 | if (track->isStopped()) { |
| 4338 | track->reset(); |
| 4339 | } |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4340 | tracksToRemove->add(track); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4341 | } |
| 4342 | } else { |
| 4343 | // No buffers for this track. Give it a few chances to |
| 4344 | // fill a buffer, then remove it from active list. |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4345 | // Only consider last track started for mixer state control |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4346 | if (--(track->mRetryCount) <= 0) { |
| 4347 | ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name()); |
Eric Laurent | d595b7c | 2013-04-03 17:27:56 -0700 | [diff] [blame] | 4348 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4349 | // indicate to client process that the track was disabled because of underrun; |
| 4350 | // it will then automatically call start() when data is available |
| 4351 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4352 | } else if (last) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4353 | mixerStatus = MIXER_TRACKS_ENABLED; |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4354 | if (usesHwAvSync() && !mHwPaused && !mStandby) { |
| 4355 | doHwPause = true; |
| 4356 | mHwPaused = true; |
| 4357 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4363 | // if an active track did not command a flush, check for pending flush on stopped tracks |
| 4364 | if (!flushPending) { |
| 4365 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4366 | if (mTracks[i]->isFlushPending()) { |
| 4367 | mTracks[i]->flushAck(); |
| 4368 | flushPending = true; |
| 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4374 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4375 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4376 | // if resume is received before pause is executed. |
| 4377 | if (mHwSupportsPause && !mStandby && |
| 4378 | (doHwPause || (flushPending && !mHwPaused && (count != 0)))) { |
| 4379 | mOutput->stream->pause(mOutput->stream); |
| 4380 | } |
| 4381 | if (flushPending) { |
| 4382 | flushHw_l(); |
| 4383 | } |
| 4384 | if (mHwSupportsPause && !mStandby && doHwResume) { |
| 4385 | mOutput->stream->resume(mOutput->stream); |
| 4386 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4387 | // remove all the tracks that need to be... |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4388 | removeTracks_l(*tracksToRemove); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4389 | |
| 4390 | return mixerStatus; |
| 4391 | } |
| 4392 | |
| 4393 | void AudioFlinger::DirectOutputThread::threadLoop_mix() |
| 4394 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4395 | size_t frameCount = mFrameCount; |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4396 | int8_t *curBuf = (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4397 | // output audio to hardware |
| 4398 | while (frameCount) { |
Glenn Kasten | 34542ac | 2013-06-26 11:29:02 -0700 | [diff] [blame] | 4399 | AudioBufferProvider::Buffer buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4400 | buffer.frameCount = frameCount; |
| 4401 | mActiveTrack->getNextBuffer(&buffer); |
Glenn Kasten | fa319e6 | 2013-07-29 17:17:38 -0700 | [diff] [blame] | 4402 | if (buffer.raw == NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4403 | memset(curBuf, 0, frameCount * mFrameSize); |
| 4404 | break; |
| 4405 | } |
| 4406 | memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize); |
| 4407 | frameCount -= buffer.frameCount; |
| 4408 | curBuf += buffer.frameCount * mFrameSize; |
| 4409 | mActiveTrack->releaseBuffer(&buffer); |
| 4410 | } |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4411 | mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4412 | sleepTime = 0; |
| 4413 | standbyTime = systemTime() + standbyDelay; |
| 4414 | mActiveTrack.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | void AudioFlinger::DirectOutputThread::threadLoop_sleepTime() |
| 4418 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4419 | // do not write to HAL when paused |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4420 | if (mHwPaused || (usesHwAvSync() && mStandby)) { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4421 | sleepTime = idleSleepTime; |
| 4422 | return; |
| 4423 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4424 | if (sleepTime == 0) { |
| 4425 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 4426 | sleepTime = activeSleepTime; |
| 4427 | } else { |
| 4428 | sleepTime = idleSleepTime; |
| 4429 | } |
| 4430 | } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) { |
Andy Hung | 2098f27 | 2014-02-27 14:00:06 -0800 | [diff] [blame] | 4431 | memset(mSinkBuffer, 0, mFrameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4432 | sleepTime = 0; |
| 4433 | } |
| 4434 | } |
| 4435 | |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4436 | void AudioFlinger::DirectOutputThread::threadLoop_exit() |
| 4437 | { |
| 4438 | { |
| 4439 | Mutex::Autolock _l(mLock); |
| 4440 | bool flushPending = false; |
| 4441 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 4442 | if (mTracks[i]->isFlushPending()) { |
| 4443 | mTracks[i]->flushAck(); |
| 4444 | flushPending = true; |
| 4445 | } |
| 4446 | } |
| 4447 | if (flushPending) { |
| 4448 | flushHw_l(); |
| 4449 | } |
| 4450 | } |
| 4451 | PlaybackThread::threadLoop_exit(); |
| 4452 | } |
| 4453 | |
| 4454 | // must be called with thread mutex locked |
| 4455 | bool AudioFlinger::DirectOutputThread::shouldStandby_l() |
| 4456 | { |
| 4457 | bool trackPaused = false; |
| 4458 | |
| 4459 | // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack |
| 4460 | // after a timeout and we will enter standby then. |
| 4461 | if (mTracks.size() > 0) { |
| 4462 | trackPaused = mTracks[mTracks.size() - 1]->isPaused(); |
| 4463 | } |
| 4464 | |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 4465 | return !mStandby && !(trackPaused || (usesHwAvSync() && mHwPaused)); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4466 | } |
| 4467 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4468 | // getTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4469 | int AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused, |
Andy Hung | e8a1ced | 2014-05-09 15:02:21 -0700 | [diff] [blame] | 4470 | audio_format_t format __unused, int sessionId __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4471 | { |
| 4472 | return 0; |
| 4473 | } |
| 4474 | |
| 4475 | // deleteTrackName_l() must be called with ThreadBase::mLock held |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 4476 | void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4477 | { |
| 4478 | } |
| 4479 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4480 | // checkForNewParameter_l() must be called with ThreadBase::mLock held |
| 4481 | bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair, |
| 4482 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4483 | { |
| 4484 | bool reconfig = false; |
| 4485 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4486 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4487 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4488 | AudioParameter param = AudioParameter(keyValuePair); |
| 4489 | int value; |
| 4490 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 4491 | // forward device change to effects that have requested to be |
| 4492 | // aware of attached audio device. |
| 4493 | if (value != AUDIO_DEVICE_NONE) { |
| 4494 | mOutDevice = value; |
| 4495 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 4496 | mEffectChains[i]->setDevice_l(mOutDevice); |
Glenn Kasten | c125f38 | 2014-04-11 18:37:33 -0700 | [diff] [blame] | 4497 | } |
| 4498 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4499 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 4500 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 4501 | // do not accept frame count changes if tracks are open as the track buffer |
| 4502 | // size depends on frame count and correct behavior would not be garantied |
| 4503 | // if frame count is changed after track creation |
| 4504 | if (!mTracks.isEmpty()) { |
| 4505 | status = INVALID_OPERATION; |
| 4506 | } else { |
| 4507 | reconfig = true; |
| 4508 | } |
| 4509 | } |
| 4510 | if (status == NO_ERROR) { |
| 4511 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4512 | keyValuePair.string()); |
| 4513 | if (!mStandby && status == INVALID_OPERATION) { |
| 4514 | mOutput->stream->common.standby(&mOutput->stream->common); |
| 4515 | mStandby = true; |
| 4516 | mBytesWritten = 0; |
| 4517 | status = mOutput->stream->common.set_parameters(&mOutput->stream->common, |
| 4518 | keyValuePair.string()); |
| 4519 | } |
| 4520 | if (status == NO_ERROR && reconfig) { |
| 4521 | readOutputParameters_l(); |
| 4522 | sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED); |
| 4523 | } |
| 4524 | } |
| 4525 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4526 | return reconfig; |
| 4527 | } |
| 4528 | |
| 4529 | uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const |
| 4530 | { |
| 4531 | uint32_t time; |
| 4532 | if (audio_is_linear_pcm(mFormat)) { |
| 4533 | time = PlaybackThread::activeSleepTimeUs(); |
| 4534 | } else { |
| 4535 | time = 10000; |
| 4536 | } |
| 4537 | return time; |
| 4538 | } |
| 4539 | |
| 4540 | uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const |
| 4541 | { |
| 4542 | uint32_t time; |
| 4543 | if (audio_is_linear_pcm(mFormat)) { |
| 4544 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2; |
| 4545 | } else { |
| 4546 | time = 10000; |
| 4547 | } |
| 4548 | return time; |
| 4549 | } |
| 4550 | |
| 4551 | uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const |
| 4552 | { |
| 4553 | uint32_t time; |
| 4554 | if (audio_is_linear_pcm(mFormat)) { |
| 4555 | time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000); |
| 4556 | } else { |
| 4557 | time = 10000; |
| 4558 | } |
| 4559 | return time; |
| 4560 | } |
| 4561 | |
| 4562 | void AudioFlinger::DirectOutputThread::cacheParameters_l() |
| 4563 | { |
| 4564 | PlaybackThread::cacheParameters_l(); |
| 4565 | |
| 4566 | // use shorter standby delay as on normal output to release |
| 4567 | // hardware resources as soon as possible |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4568 | if (audio_is_linear_pcm(mFormat)) { |
| 4569 | standbyDelay = microseconds(activeSleepTime*2); |
| 4570 | } else { |
| 4571 | standbyDelay = kOffloadStandbyDelayNs; |
| 4572 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4573 | } |
| 4574 | |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4575 | void AudioFlinger::DirectOutputThread::flushHw_l() |
| 4576 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4577 | if (mOutput->stream->flush != NULL) { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4578 | mOutput->stream->flush(mOutput->stream); |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 4579 | } |
| 4580 | mHwPaused = false; |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4581 | } |
| 4582 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4583 | // ---------------------------------------------------------------------------- |
| 4584 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4585 | AudioFlinger::AsyncCallbackThread::AsyncCallbackThread( |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4586 | const wp<AudioFlinger::PlaybackThread>& playbackThread) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4587 | : Thread(false /*canCallJava*/), |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4588 | mPlaybackThread(playbackThread), |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4589 | mWriteAckSequence(0), |
| 4590 | mDrainSequence(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4591 | { |
| 4592 | } |
| 4593 | |
| 4594 | AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread() |
| 4595 | { |
| 4596 | } |
| 4597 | |
| 4598 | void AudioFlinger::AsyncCallbackThread::onFirstRef() |
| 4599 | { |
| 4600 | run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 4601 | } |
| 4602 | |
| 4603 | bool AudioFlinger::AsyncCallbackThread::threadLoop() |
| 4604 | { |
| 4605 | while (!exitPending()) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4606 | uint32_t writeAckSequence; |
| 4607 | uint32_t drainSequence; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4608 | |
| 4609 | { |
| 4610 | Mutex::Autolock _l(mLock); |
Haynes Mathew George | 24a325d | 2013-12-03 21:26:02 -0800 | [diff] [blame] | 4611 | while (!((mWriteAckSequence & 1) || |
| 4612 | (mDrainSequence & 1) || |
| 4613 | exitPending())) { |
| 4614 | mWaitWorkCV.wait(mLock); |
| 4615 | } |
| 4616 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4617 | if (exitPending()) { |
| 4618 | break; |
| 4619 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4620 | ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d", |
| 4621 | mWriteAckSequence, mDrainSequence); |
| 4622 | writeAckSequence = mWriteAckSequence; |
| 4623 | mWriteAckSequence &= ~1; |
| 4624 | drainSequence = mDrainSequence; |
| 4625 | mDrainSequence &= ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4626 | } |
| 4627 | { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4628 | sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote(); |
| 4629 | if (playbackThread != 0) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4630 | if (writeAckSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4631 | playbackThread->resetWriteBlocked(writeAckSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4632 | } |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4633 | if (drainSequence & 1) { |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 4634 | playbackThread->resetDraining(drainSequence >> 1); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4635 | } |
| 4636 | } |
| 4637 | } |
| 4638 | } |
| 4639 | return false; |
| 4640 | } |
| 4641 | |
| 4642 | void AudioFlinger::AsyncCallbackThread::exit() |
| 4643 | { |
| 4644 | ALOGV("AsyncCallbackThread::exit"); |
| 4645 | Mutex::Autolock _l(mLock); |
| 4646 | requestExit(); |
| 4647 | mWaitWorkCV.broadcast(); |
| 4648 | } |
| 4649 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4650 | void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4651 | { |
| 4652 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4653 | // bit 0 is cleared |
| 4654 | mWriteAckSequence = sequence << 1; |
| 4655 | } |
| 4656 | |
| 4657 | void AudioFlinger::AsyncCallbackThread::resetWriteBlocked() |
| 4658 | { |
| 4659 | Mutex::Autolock _l(mLock); |
| 4660 | // ignore unexpected callbacks |
| 4661 | if (mWriteAckSequence & 2) { |
| 4662 | mWriteAckSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4663 | mWaitWorkCV.signal(); |
| 4664 | } |
| 4665 | } |
| 4666 | |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4667 | void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4668 | { |
| 4669 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4670 | // bit 0 is cleared |
| 4671 | mDrainSequence = sequence << 1; |
| 4672 | } |
| 4673 | |
| 4674 | void AudioFlinger::AsyncCallbackThread::resetDraining() |
| 4675 | { |
| 4676 | Mutex::Autolock _l(mLock); |
| 4677 | // ignore unexpected callbacks |
| 4678 | if (mDrainSequence & 2) { |
| 4679 | mDrainSequence |= 1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4680 | mWaitWorkCV.signal(); |
| 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | |
| 4685 | // ---------------------------------------------------------------------------- |
| 4686 | AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger, |
| 4687 | AudioStreamOut* output, audio_io_handle_t id, uint32_t device) |
| 4688 | : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD), |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4689 | mPausedBytesRemaining(0) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4690 | { |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4691 | //FIXME: mStandby should be set to true by ThreadBase constructor |
| 4692 | mStandby = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4693 | } |
| 4694 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4695 | void AudioFlinger::OffloadThread::threadLoop_exit() |
| 4696 | { |
| 4697 | if (mFlushPending || mHwPaused) { |
| 4698 | // If a flush is pending or track was paused, just discard buffered data |
| 4699 | flushHw_l(); |
| 4700 | } else { |
| 4701 | mMixerStatus = MIXER_DRAIN_ALL; |
| 4702 | threadLoop_drain(); |
| 4703 | } |
Uday Gupta | 56604aa | 2014-05-13 11:19:17 -0700 | [diff] [blame] | 4704 | if (mUseAsyncWrite) { |
| 4705 | ALOG_ASSERT(mCallbackThread != 0); |
| 4706 | mCallbackThread->exit(); |
| 4707 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4708 | PlaybackThread::threadLoop_exit(); |
| 4709 | } |
| 4710 | |
| 4711 | AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l( |
| 4712 | Vector< sp<Track> > *tracksToRemove |
| 4713 | ) |
| 4714 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4715 | size_t count = mActiveTracks.size(); |
| 4716 | |
| 4717 | mixer_state mixerStatus = MIXER_IDLE; |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4718 | bool doHwPause = false; |
| 4719 | bool doHwResume = false; |
| 4720 | |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 4721 | ALOGV("OffloadThread::prepareTracks_l active tracks %d", count); |
| 4722 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4723 | // find out which tracks need to be processed |
| 4724 | for (size_t i = 0; i < count; i++) { |
| 4725 | sp<Track> t = mActiveTracks[i].promote(); |
| 4726 | // The track died recently |
| 4727 | if (t == 0) { |
| 4728 | continue; |
| 4729 | } |
| 4730 | Track* const track = t.get(); |
| 4731 | audio_track_cblk_t* cblk = track->cblk(); |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4732 | // Only consider last track started for volume and mixer state control. |
| 4733 | // In theory an older track could underrun and restart after the new one starts |
| 4734 | // but as we only care about the transition phase between two tracks on a |
| 4735 | // direct output, it is not a problem to ignore the underrun case. |
| 4736 | sp<Track> l = mLatestActiveTrack.promote(); |
| 4737 | bool last = l.get() == track; |
| 4738 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4739 | if (track->isInvalid()) { |
| 4740 | ALOGW("An invalidated track shouldn't be in active list"); |
| 4741 | tracksToRemove->add(track); |
| 4742 | continue; |
| 4743 | } |
| 4744 | |
| 4745 | if (track->mState == TrackBase::IDLE) { |
| 4746 | ALOGW("An idle track shouldn't be in active list"); |
| 4747 | continue; |
| 4748 | } |
| 4749 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4750 | if (track->isPausing()) { |
| 4751 | track->setPaused(); |
| 4752 | if (last) { |
| 4753 | if (!mHwPaused) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4754 | doHwPause = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4755 | mHwPaused = true; |
| 4756 | } |
| 4757 | // If we were part way through writing the mixbuffer to |
| 4758 | // the HAL we must save this until we resume |
| 4759 | // BUG - this will be wrong if a different track is made active, |
| 4760 | // in that case we want to discard the pending data in the |
| 4761 | // mixbuffer and tell the client to present it again when the |
| 4762 | // track is resumed |
| 4763 | mPausedWriteLength = mCurrentWriteLength; |
| 4764 | mPausedBytesRemaining = mBytesRemaining; |
| 4765 | mBytesRemaining = 0; // stop writing |
| 4766 | } |
| 4767 | tracksToRemove->add(track); |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 4768 | } else if (track->isFlushPending()) { |
| 4769 | track->flushAck(); |
| 4770 | if (last) { |
| 4771 | mFlushPending = true; |
| 4772 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 4773 | } else if (track->isResumePending()){ |
| 4774 | track->resumeAck(); |
| 4775 | if (last) { |
| 4776 | if (mPausedBytesRemaining) { |
| 4777 | // Need to continue write that was interrupted |
| 4778 | mCurrentWriteLength = mPausedWriteLength; |
| 4779 | mBytesRemaining = mPausedBytesRemaining; |
| 4780 | mPausedBytesRemaining = 0; |
| 4781 | } |
| 4782 | if (mHwPaused) { |
| 4783 | doHwResume = true; |
| 4784 | mHwPaused = false; |
| 4785 | // threadLoop_mix() will handle the case that we need to |
| 4786 | // resume an interrupted write |
| 4787 | } |
| 4788 | // enable write to audio HAL |
| 4789 | sleepTime = 0; |
| 4790 | |
| 4791 | // Do not handle new data in this iteration even if track->framesReady() |
| 4792 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4793 | } |
| 4794 | } else if (track->framesReady() && track->isReady() && |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4795 | !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4796 | ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4797 | if (track->mFillingUpStatus == Track::FS_FILLED) { |
| 4798 | track->mFillingUpStatus = Track::FS_ACTIVE; |
Eric Laurent | 1abbdb4 | 2013-09-13 17:00:08 -0700 | [diff] [blame] | 4799 | // make sure processVolume_l() will apply new volume even if 0 |
| 4800 | mLeftVolFloat = mRightVolFloat = -1.0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | if (last) { |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4804 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4805 | if (previousTrack != 0) { |
| 4806 | if (track != previousTrack.get()) { |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4807 | // Flush any data still being written from last track |
| 4808 | mBytesRemaining = 0; |
| 4809 | if (mPausedBytesRemaining) { |
| 4810 | // Last track was paused so we also need to flush saved |
| 4811 | // mixbuffer state and invalidate track so that it will |
| 4812 | // re-submit that unwritten data when it is next resumed |
| 4813 | mPausedBytesRemaining = 0; |
| 4814 | // Invalidate is a bit drastic - would be more efficient |
| 4815 | // to have a flag to tell client that some of the |
| 4816 | // previously written data was lost |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4817 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4818 | } |
| 4819 | // flush data already sent to the DSP if changing audio session as audio |
| 4820 | // comes from a different source. Also invalidate previous track to force a |
| 4821 | // seek when resuming. |
Eric Laurent | d7e5922 | 2013-11-15 12:02:28 -0800 | [diff] [blame] | 4822 | if (previousTrack->sessionId() != track->sessionId()) { |
| 4823 | previousTrack->invalidate(); |
Eric Laurent | 9da3d95 | 2013-11-12 19:25:43 -0800 | [diff] [blame] | 4824 | } |
| 4825 | } |
| 4826 | } |
| 4827 | mPreviousTrack = track; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4828 | // reset retry count |
| 4829 | track->mRetryCount = kMaxTrackRetriesOffload; |
| 4830 | mActiveTrack = t; |
| 4831 | mixerStatus = MIXER_TRACKS_READY; |
| 4832 | } |
| 4833 | } else { |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 4834 | ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4835 | if (track->isStopping_1()) { |
| 4836 | // Hardware buffer can hold a large amount of audio so we must |
| 4837 | // wait for all current track's data to drain before we say |
| 4838 | // that the track is stopped. |
| 4839 | if (mBytesRemaining == 0) { |
| 4840 | // Only start draining when all data in mixbuffer |
| 4841 | // has been written |
| 4842 | ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2"); |
| 4843 | track->mState = TrackBase::STOPPING_2; // so presentation completes after drain |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4844 | // do not drain if no data was ever sent to HAL (mStandby == true) |
| 4845 | if (last && !mStandby) { |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4846 | // do not modify drain sequence if we are already draining. This happens |
| 4847 | // when resuming from pause after drain. |
| 4848 | if ((mDrainSequence & 1) == 0) { |
| 4849 | sleepTime = 0; |
| 4850 | standbyTime = systemTime() + standbyDelay; |
| 4851 | mixerStatus = MIXER_DRAIN_TRACK; |
| 4852 | mDrainSequence += 2; |
| 4853 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4854 | if (mHwPaused) { |
| 4855 | // It is possible to move from PAUSED to STOPPING_1 without |
| 4856 | // a resume so we must ensure hardware is running |
Eric Laurent | 1b9f9b1 | 2013-11-12 19:10:17 -0800 | [diff] [blame] | 4857 | doHwResume = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4858 | mHwPaused = false; |
| 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | } else if (track->isStopping_2()) { |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 4863 | // Drain has completed or we are in standby, signal presentation complete |
| 4864 | if (!(mDrainSequence & 1) || !last || mStandby) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4865 | track->mState = TrackBase::STOPPED; |
| 4866 | size_t audioHALFrames = |
| 4867 | (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000; |
| 4868 | size_t framesWritten = |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 4869 | mBytesWritten / audio_stream_out_frame_size(mOutput->stream); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4870 | track->presentationComplete(framesWritten, audioHALFrames); |
| 4871 | track->reset(); |
| 4872 | tracksToRemove->add(track); |
| 4873 | } |
| 4874 | } else { |
| 4875 | // No buffers for this track. Give it a few chances to |
| 4876 | // fill a buffer, then remove it from active list. |
| 4877 | if (--(track->mRetryCount) <= 0) { |
| 4878 | ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list", |
| 4879 | track->name()); |
| 4880 | tracksToRemove->add(track); |
Eric Laurent | a23f17a | 2013-11-05 18:22:08 -0800 | [diff] [blame] | 4881 | // indicate to client process that the track was disabled because of underrun; |
| 4882 | // it will then automatically call start() when data is available |
| 4883 | android_atomic_or(CBLK_DISABLED, &cblk->mFlags); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4884 | } else if (last){ |
| 4885 | mixerStatus = MIXER_TRACKS_ENABLED; |
| 4886 | } |
| 4887 | } |
| 4888 | } |
| 4889 | // compute volume for this track |
| 4890 | processVolume_l(track, last); |
| 4891 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4892 | |
Eric Laurent | ea0fade | 2013-10-04 16:23:48 -0700 | [diff] [blame] | 4893 | // make sure the pause/flush/resume sequence is executed in the right order. |
| 4894 | // If a flush is pending and a track is active but the HW is not paused, force a HW pause |
| 4895 | // before flush and then resume HW. This can happen in case of pause/flush/resume |
| 4896 | // if resume is received before pause is executed. |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4897 | if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4898 | mOutput->stream->pause(mOutput->stream); |
| 4899 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4900 | if (mFlushPending) { |
| 4901 | flushHw_l(); |
| 4902 | mFlushPending = false; |
| 4903 | } |
Eric Laurent | fd47797 | 2013-10-25 18:10:40 -0700 | [diff] [blame] | 4904 | if (!mStandby && doHwResume) { |
Eric Laurent | 972a173 | 2013-09-04 09:42:59 -0700 | [diff] [blame] | 4905 | mOutput->stream->resume(mOutput->stream); |
| 4906 | } |
Eric Laurent | 6bf9ae2 | 2013-08-30 15:12:37 -0700 | [diff] [blame] | 4907 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4908 | // remove all the tracks that need to be... |
| 4909 | removeTracks_l(*tracksToRemove); |
| 4910 | |
| 4911 | return mixerStatus; |
| 4912 | } |
| 4913 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4914 | // must be called with thread mutex locked |
| 4915 | bool AudioFlinger::OffloadThread::waitingAsyncCallback_l() |
| 4916 | { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4917 | ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d", |
| 4918 | mWriteAckSequence, mDrainSequence); |
| 4919 | if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4920 | return true; |
| 4921 | } |
| 4922 | return false; |
| 4923 | } |
| 4924 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4925 | bool AudioFlinger::OffloadThread::waitingAsyncCallback() |
| 4926 | { |
| 4927 | Mutex::Autolock _l(mLock); |
| 4928 | return waitingAsyncCallback_l(); |
| 4929 | } |
| 4930 | |
| 4931 | void AudioFlinger::OffloadThread::flushHw_l() |
| 4932 | { |
Eric Laurent | e659ef4 | 2014-09-29 13:06:46 -0700 | [diff] [blame] | 4933 | DirectOutputThread::flushHw_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4934 | // Flush anything still waiting in the mixbuffer |
| 4935 | mCurrentWriteLength = 0; |
| 4936 | mBytesRemaining = 0; |
| 4937 | mPausedWriteLength = 0; |
| 4938 | mPausedBytesRemaining = 0; |
Haynes Mathew George | 0f02f26 | 2014-01-11 13:03:57 -0800 | [diff] [blame] | 4939 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4940 | if (mUseAsyncWrite) { |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4941 | // discard any pending drain or write ack by incrementing sequence |
| 4942 | mWriteAckSequence = (mWriteAckSequence + 2) & ~1; |
| 4943 | mDrainSequence = (mDrainSequence + 2) & ~1; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4944 | ALOG_ASSERT(mCallbackThread != 0); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 4945 | mCallbackThread->setWriteBlocked(mWriteAckSequence); |
| 4946 | mCallbackThread->setDraining(mDrainSequence); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4947 | } |
| 4948 | } |
| 4949 | |
Haynes Mathew George | 4c6a433 | 2014-01-15 12:31:39 -0800 | [diff] [blame] | 4950 | void AudioFlinger::OffloadThread::onAddNewTrack_l() |
| 4951 | { |
| 4952 | sp<Track> previousTrack = mPreviousTrack.promote(); |
| 4953 | sp<Track> latestTrack = mLatestActiveTrack.promote(); |
| 4954 | |
| 4955 | if (previousTrack != 0 && latestTrack != 0 && |
| 4956 | (previousTrack->sessionId() != latestTrack->sessionId())) { |
| 4957 | mFlushPending = true; |
| 4958 | } |
| 4959 | PlaybackThread::onAddNewTrack_l(); |
| 4960 | } |
| 4961 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 4962 | // ---------------------------------------------------------------------------- |
| 4963 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4964 | AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, |
| 4965 | AudioFlinger::MixerThread* mainThread, audio_io_handle_t id) |
| 4966 | : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(), |
| 4967 | DUPLICATING), |
| 4968 | mWaitTimeMs(UINT_MAX) |
| 4969 | { |
| 4970 | addOutputTrack(mainThread); |
| 4971 | } |
| 4972 | |
| 4973 | AudioFlinger::DuplicatingThread::~DuplicatingThread() |
| 4974 | { |
| 4975 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 4976 | mOutputTracks[i]->destroy(); |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | void AudioFlinger::DuplicatingThread::threadLoop_mix() |
| 4981 | { |
| 4982 | // mix buffers... |
| 4983 | if (outputsReady(outputTracks)) { |
| 4984 | mAudioMixer->process(AudioBufferProvider::kInvalidPTS); |
| 4985 | } else { |
Eric Laurent | 02b5708 | 2014-11-07 17:28:28 -0800 | [diff] [blame] | 4986 | if (mMixerBufferValid) { |
| 4987 | memset(mMixerBuffer, 0, mMixerBufferSize); |
| 4988 | } else { |
| 4989 | memset(mSinkBuffer, 0, mSinkBufferSize); |
| 4990 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4991 | } |
| 4992 | sleepTime = 0; |
| 4993 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 4994 | mCurrentWriteLength = mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 4995 | standbyTime = systemTime() + standbyDelay; |
| 4996 | } |
| 4997 | |
| 4998 | void AudioFlinger::DuplicatingThread::threadLoop_sleepTime() |
| 4999 | { |
| 5000 | if (sleepTime == 0) { |
| 5001 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5002 | sleepTime = activeSleepTime; |
| 5003 | } else { |
| 5004 | sleepTime = idleSleepTime; |
| 5005 | } |
| 5006 | } else if (mBytesWritten != 0) { |
| 5007 | if (mMixerStatus == MIXER_TRACKS_ENABLED) { |
| 5008 | writeFrames = mNormalFrameCount; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5009 | memset(mSinkBuffer, 0, mSinkBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5010 | } else { |
| 5011 | // flush remaining overflow buffers in output tracks |
| 5012 | writeFrames = 0; |
| 5013 | } |
| 5014 | sleepTime = 0; |
| 5015 | } |
| 5016 | } |
| 5017 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 5018 | ssize_t AudioFlinger::DuplicatingThread::threadLoop_write() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5019 | { |
| 5020 | for (size_t i = 0; i < outputTracks.size(); i++) { |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5021 | outputTracks[i]->write(mSinkBuffer, writeFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5022 | } |
Eric Laurent | 2c3740f | 2013-10-30 16:57:06 -0700 | [diff] [blame] | 5023 | mStandby = false; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 5024 | return (ssize_t)mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | void AudioFlinger::DuplicatingThread::threadLoop_standby() |
| 5028 | { |
| 5029 | // DuplicatingThread implements standby by stopping all tracks |
| 5030 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5031 | outputTracks[i]->stop(); |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | void AudioFlinger::DuplicatingThread::saveOutputTracks() |
| 5036 | { |
| 5037 | outputTracks = mOutputTracks; |
| 5038 | } |
| 5039 | |
| 5040 | void AudioFlinger::DuplicatingThread::clearOutputTracks() |
| 5041 | { |
| 5042 | outputTracks.clear(); |
| 5043 | } |
| 5044 | |
| 5045 | void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread) |
| 5046 | { |
| 5047 | Mutex::Autolock _l(mLock); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5048 | // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass. |
| 5049 | // Adjust for thread->sampleRate() to determine minimum buffer frame count. |
| 5050 | // Then triple buffer because Threads do not run synchronously and may not be clock locked. |
| 5051 | const size_t frameCount = |
| 5052 | 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate()); |
| 5053 | // TODO: Consider asynchronous sample rate conversion to handle clock disparity |
| 5054 | // from different OutputTracks and their associated MixerThreads (e.g. one may |
| 5055 | // nearly empty and the other may be dropping data). |
| 5056 | |
| 5057 | sp<OutputTrack> outputTrack = new OutputTrack(thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5058 | this, |
| 5059 | mSampleRate, |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5060 | mFormat, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5061 | mChannelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5062 | frameCount, |
| 5063 | IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5064 | if (outputTrack->cblk() != NULL) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5065 | thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5066 | mOutputTracks.add(outputTrack); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 5067 | ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5068 | updateWaitTime_l(); |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread) |
| 5073 | { |
| 5074 | Mutex::Autolock _l(mLock); |
| 5075 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5076 | if (mOutputTracks[i]->thread() == thread) { |
| 5077 | mOutputTracks[i]->destroy(); |
| 5078 | mOutputTracks.removeAt(i); |
| 5079 | updateWaitTime_l(); |
| 5080 | return; |
| 5081 | } |
| 5082 | } |
| 5083 | ALOGV("removeOutputTrack(): unkonwn thread: %p", thread); |
| 5084 | } |
| 5085 | |
| 5086 | // caller must hold mLock |
| 5087 | void AudioFlinger::DuplicatingThread::updateWaitTime_l() |
| 5088 | { |
| 5089 | mWaitTimeMs = UINT_MAX; |
| 5090 | for (size_t i = 0; i < mOutputTracks.size(); i++) { |
| 5091 | sp<ThreadBase> strong = mOutputTracks[i]->thread().promote(); |
| 5092 | if (strong != 0) { |
| 5093 | uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate(); |
| 5094 | if (waitTimeMs < mWaitTimeMs) { |
| 5095 | mWaitTimeMs = waitTimeMs; |
| 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | |
| 5102 | bool AudioFlinger::DuplicatingThread::outputsReady( |
| 5103 | const SortedVector< sp<OutputTrack> > &outputTracks) |
| 5104 | { |
| 5105 | for (size_t i = 0; i < outputTracks.size(); i++) { |
| 5106 | sp<ThreadBase> thread = outputTracks[i]->thread().promote(); |
| 5107 | if (thread == 0) { |
| 5108 | ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", |
| 5109 | outputTracks[i].get()); |
| 5110 | return false; |
| 5111 | } |
| 5112 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 5113 | // see note at standby() declaration |
| 5114 | if (playbackThread->standby() && !playbackThread->isSuspended()) { |
| 5115 | ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), |
| 5116 | thread.get()); |
| 5117 | return false; |
| 5118 | } |
| 5119 | } |
| 5120 | return true; |
| 5121 | } |
| 5122 | |
| 5123 | uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const |
| 5124 | { |
| 5125 | return (mWaitTimeMs * 1000) / 2; |
| 5126 | } |
| 5127 | |
| 5128 | void AudioFlinger::DuplicatingThread::cacheParameters_l() |
| 5129 | { |
| 5130 | // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first |
| 5131 | updateWaitTime_l(); |
| 5132 | |
| 5133 | MixerThread::cacheParameters_l(); |
| 5134 | } |
| 5135 | |
| 5136 | // ---------------------------------------------------------------------------- |
| 5137 | // Record |
| 5138 | // ---------------------------------------------------------------------------- |
| 5139 | |
| 5140 | AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, |
| 5141 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5142 | audio_io_handle_t id, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5143 | audio_devices_t outDevice, |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5144 | audio_devices_t inDevice |
| 5145 | #ifdef TEE_SINK |
| 5146 | , const sp<NBAIO_Sink>& teeSink |
| 5147 | #endif |
| 5148 | ) : |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 5149 | ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD), |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5150 | mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL), |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5151 | // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l() |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5152 | mRsmpInRear(0) |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 5153 | #ifdef TEE_SINK |
| 5154 | , mTeeSink(teeSink) |
| 5155 | #endif |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 5156 | , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize, |
| 5157 | "RecordThreadRO", MemoryHeapBase::READ_ONLY)) |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5158 | // mFastCapture below |
| 5159 | , mFastCaptureFutex(0) |
| 5160 | // mInputSource |
| 5161 | // mPipeSink |
| 5162 | // mPipeSource |
| 5163 | , mPipeFramesP2(0) |
| 5164 | // mPipeMemory |
| 5165 | // mFastCaptureNBLogWriter |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5166 | , mFastTrackAvail(false) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5167 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5168 | snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id); |
| 5169 | mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5170 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 5171 | readInputParameters_l(); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5172 | |
| 5173 | // create an NBAIO source for the HAL input stream, and negotiate |
| 5174 | mInputSource = new AudioStreamInSource(input->stream); |
| 5175 | size_t numCounterOffers = 0; |
| 5176 | const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)}; |
| 5177 | ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers); |
| 5178 | ALOG_ASSERT(index == 0); |
| 5179 | |
| 5180 | // initialize fast capture depending on configuration |
| 5181 | bool initFastCapture; |
| 5182 | switch (kUseFastCapture) { |
| 5183 | case FastCapture_Never: |
| 5184 | initFastCapture = false; |
| 5185 | break; |
| 5186 | case FastCapture_Always: |
| 5187 | initFastCapture = true; |
| 5188 | break; |
| 5189 | case FastCapture_Static: |
| 5190 | uint32_t primaryOutputSampleRate; |
| 5191 | { |
| 5192 | AutoMutex _l(audioFlinger->mHardwareLock); |
| 5193 | primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate; |
| 5194 | } |
| 5195 | initFastCapture = |
| 5196 | // either capture sample rate is same as (a reasonable) primary output sample rate |
| 5197 | (((primaryOutputSampleRate == 44100 || primaryOutputSampleRate == 48000) && |
| 5198 | (mSampleRate == primaryOutputSampleRate)) || |
| 5199 | // or primary output sample rate is unknown, and capture sample rate is reasonable |
| 5200 | ((primaryOutputSampleRate == 0) && |
| 5201 | ((mSampleRate == 44100 || mSampleRate == 48000)))) && |
Glenn Kasten | 9f81de3 | 2014-07-27 15:02:23 -0700 | [diff] [blame] | 5202 | // and the buffer size is < 12 ms |
| 5203 | (mFrameCount * 1000) / mSampleRate < 12; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5204 | break; |
| 5205 | // case FastCapture_Dynamic: |
| 5206 | } |
| 5207 | |
| 5208 | if (initFastCapture) { |
| 5209 | // create a Pipe for FastMixer to write to, and for us and fast tracks to read from |
| 5210 | NBAIO_Format format = mInputSource->format(); |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5211 | size_t pipeFramesP2 = roundup(mSampleRate / 25); // double-buffering of 20 ms each |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5212 | size_t pipeSize = pipeFramesP2 * Format_frameSize(format); |
| 5213 | void *pipeBuffer; |
| 5214 | const sp<MemoryDealer> roHeap(readOnlyHeap()); |
| 5215 | sp<IMemory> pipeMemory; |
| 5216 | if ((roHeap == 0) || |
| 5217 | (pipeMemory = roHeap->allocate(pipeSize)) == 0 || |
| 5218 | (pipeBuffer = pipeMemory->pointer()) == NULL) { |
| 5219 | ALOGE("not enough memory for pipe buffer size=%zu", pipeSize); |
| 5220 | goto failed; |
| 5221 | } |
| 5222 | // pipe will be shared directly with fast clients, so clear to avoid leaking old information |
| 5223 | memset(pipeBuffer, 0, pipeSize); |
| 5224 | Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer); |
| 5225 | const NBAIO_Format offers[1] = {format}; |
| 5226 | size_t numCounterOffers = 0; |
| 5227 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 5228 | ALOG_ASSERT(index == 0); |
| 5229 | mPipeSink = pipe; |
| 5230 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 5231 | numCounterOffers = 0; |
| 5232 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 5233 | ALOG_ASSERT(index == 0); |
| 5234 | mPipeSource = pipeReader; |
| 5235 | mPipeFramesP2 = pipeFramesP2; |
| 5236 | mPipeMemory = pipeMemory; |
| 5237 | |
| 5238 | // create fast capture |
| 5239 | mFastCapture = new FastCapture(); |
| 5240 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5241 | #ifdef STATE_QUEUE_DUMP |
| 5242 | // FIXME |
| 5243 | #endif |
| 5244 | FastCaptureState *state = sq->begin(); |
| 5245 | state->mCblk = NULL; |
| 5246 | state->mInputSource = mInputSource.get(); |
| 5247 | state->mInputSourceGen++; |
| 5248 | state->mPipeSink = pipe; |
| 5249 | state->mPipeSinkGen++; |
| 5250 | state->mFrameCount = mFrameCount; |
| 5251 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5252 | // already done in constructor initialization list |
| 5253 | //mFastCaptureFutex = 0; |
| 5254 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5255 | state->mColdGen++; |
| 5256 | state->mDumpState = &mFastCaptureDumpState; |
| 5257 | #ifdef TEE_SINK |
| 5258 | // FIXME |
| 5259 | #endif |
| 5260 | mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture"); |
| 5261 | state->mNBLogWriter = mFastCaptureNBLogWriter.get(); |
| 5262 | sq->end(); |
| 5263 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5264 | |
| 5265 | // start the fast capture |
| 5266 | mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO); |
| 5267 | pid_t tid = mFastCapture->getTid(); |
| 5268 | int err = requestPriority(getpid_cached, tid, kPriorityFastMixer); |
| 5269 | if (err != 0) { |
| 5270 | ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d", |
| 5271 | kPriorityFastCapture, getpid_cached, tid, err); |
| 5272 | } |
| 5273 | |
| 5274 | #ifdef AUDIO_WATCHDOG |
| 5275 | // FIXME |
| 5276 | #endif |
| 5277 | |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 5278 | mFastTrackAvail = true; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5279 | } |
| 5280 | failed: ; |
| 5281 | |
| 5282 | // FIXME mNormalSource |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | |
| 5286 | AudioFlinger::RecordThread::~RecordThread() |
| 5287 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5288 | if (mFastCapture != 0) { |
| 5289 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5290 | FastCaptureState *state = sq->begin(); |
| 5291 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5292 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5293 | if (old == -1) { |
| 5294 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5295 | } |
| 5296 | } |
| 5297 | state->mCommand = FastCaptureState::EXIT; |
| 5298 | sq->end(); |
| 5299 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED); |
| 5300 | mFastCapture->join(); |
| 5301 | mFastCapture.clear(); |
| 5302 | } |
| 5303 | mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 5304 | mAudioFlinger->unregisterWriter(mNBLogWriter); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5305 | delete[] mRsmpInBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | void AudioFlinger::RecordThread::onFirstRef() |
| 5309 | { |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 5310 | run(mThreadName, PRIORITY_URGENT_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5311 | } |
| 5312 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5313 | bool AudioFlinger::RecordThread::threadLoop() |
| 5314 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5315 | nsecs_t lastWarning = 0; |
| 5316 | |
| 5317 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5318 | |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5319 | reacquire_wakelock: |
| 5320 | sp<RecordTrack> activeTrack; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5321 | int activeTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5322 | { |
| 5323 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5324 | size_t size = mActiveTracks.size(); |
| 5325 | activeTracksGen = mActiveTracksGen; |
| 5326 | if (size > 0) { |
| 5327 | // FIXME an arbitrary choice |
| 5328 | activeTrack = mActiveTracks[0]; |
| 5329 | acquireWakeLock_l(activeTrack->uid()); |
| 5330 | if (size > 1) { |
| 5331 | SortedVector<int> tmp; |
| 5332 | for (size_t i = 0; i < size; i++) { |
| 5333 | tmp.add(mActiveTracks[i]->uid()); |
| 5334 | } |
| 5335 | updateWakeLockUids_l(tmp); |
| 5336 | } |
| 5337 | } else { |
| 5338 | acquireWakeLock_l(-1); |
| 5339 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5340 | } |
| 5341 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5342 | // used to request a deferred sleep, to be executed later while mutex is unlocked |
| 5343 | uint32_t sleepUs = 0; |
| 5344 | |
| 5345 | // loop while there is work to do |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5346 | for (;;) { |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5347 | Vector< sp<EffectChain> > effectChains; |
Glenn Kasten | 2cfbf88 | 2013-08-14 13:12:11 -0700 | [diff] [blame] | 5348 | |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5349 | // sleep with mutex unlocked |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5350 | if (sleepUs > 0) { |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5351 | ATRACE_BEGIN("sleep"); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5352 | usleep(sleepUs); |
Glenn Kasten | e775402 | 2014-10-31 12:11:26 -0700 | [diff] [blame] | 5353 | ATRACE_END(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5354 | sleepUs = 0; |
Glenn Kasten | 5edadd4 | 2013-08-14 16:30:49 -0700 | [diff] [blame] | 5355 | } |
| 5356 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5357 | // activeTracks accumulates a copy of a subset of mActiveTracks |
| 5358 | Vector< sp<RecordTrack> > activeTracks; |
| 5359 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5360 | // reference to the (first and only) active fast track |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5361 | sp<RecordTrack> fastTrack; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 5362 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5363 | // reference to a fast track which is about to be removed |
| 5364 | sp<RecordTrack> fastTrackToRemove; |
| 5365 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5366 | { // scope for mLock |
| 5367 | Mutex::Autolock _l(mLock); |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5368 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 5369 | processConfigEvents_l(); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5370 | |
Eric Laurent | 000a419 | 2014-01-29 15:17:32 -0800 | [diff] [blame] | 5371 | // check exitPending here because checkForNewParameters_l() and |
| 5372 | // checkForNewParameters_l() can temporarily release mLock |
| 5373 | if (exitPending()) { |
| 5374 | break; |
| 5375 | } |
| 5376 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5377 | // if no active track(s), then standby and release wakelock |
| 5378 | size_t size = mActiveTracks.size(); |
| 5379 | if (size == 0) { |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5380 | standbyIfNotAlreadyInStandby(); |
Glenn Kasten | 4ef0b46 | 2013-08-14 13:52:27 -0700 | [diff] [blame] | 5381 | // exitPending() can't become true here |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5382 | releaseWakeLock_l(); |
| 5383 | ALOGV("RecordThread: loop stopping"); |
| 5384 | // go to sleep |
| 5385 | mWaitWorkCV.wait(mLock); |
| 5386 | ALOGV("RecordThread: loop starting"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5387 | goto reacquire_wakelock; |
| 5388 | } |
| 5389 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5390 | if (mActiveTracksGen != activeTracksGen) { |
| 5391 | activeTracksGen = mActiveTracksGen; |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5392 | SortedVector<int> tmp; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5393 | for (size_t i = 0; i < size; i++) { |
| 5394 | tmp.add(mActiveTracks[i]->uid()); |
| 5395 | } |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 5396 | updateWakeLockUids_l(tmp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5397 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5398 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5399 | bool doBroadcast = false; |
| 5400 | for (size_t i = 0; i < size; ) { |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5401 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5402 | activeTrack = mActiveTracks[i]; |
| 5403 | if (activeTrack->isTerminated()) { |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5404 | if (activeTrack->isFastTrack()) { |
| 5405 | ALOG_ASSERT(fastTrackToRemove == 0); |
| 5406 | fastTrackToRemove = activeTrack; |
| 5407 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5408 | removeTrack_l(activeTrack); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5409 | mActiveTracks.remove(activeTrack); |
| 5410 | mActiveTracksGen++; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5411 | size--; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5412 | continue; |
| 5413 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5414 | |
| 5415 | TrackBase::track_state activeTrackState = activeTrack->mState; |
| 5416 | switch (activeTrackState) { |
| 5417 | |
| 5418 | case TrackBase::PAUSING: |
| 5419 | mActiveTracks.remove(activeTrack); |
| 5420 | mActiveTracksGen++; |
| 5421 | doBroadcast = true; |
| 5422 | size--; |
| 5423 | continue; |
| 5424 | |
| 5425 | case TrackBase::STARTING_1: |
| 5426 | sleepUs = 10000; |
| 5427 | i++; |
| 5428 | continue; |
| 5429 | |
| 5430 | case TrackBase::STARTING_2: |
| 5431 | doBroadcast = true; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5432 | mStandby = false; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5433 | activeTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5434 | break; |
| 5435 | |
| 5436 | case TrackBase::ACTIVE: |
| 5437 | break; |
| 5438 | |
| 5439 | case TrackBase::IDLE: |
| 5440 | i++; |
| 5441 | continue; |
| 5442 | |
| 5443 | default: |
Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 5444 | LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState); |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5445 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5446 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5447 | activeTracks.add(activeTrack); |
| 5448 | i++; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5449 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5450 | if (activeTrack->isFastTrack()) { |
| 5451 | ALOG_ASSERT(!mFastTrackAvail); |
| 5452 | ALOG_ASSERT(fastTrack == 0); |
| 5453 | fastTrack = activeTrack; |
| 5454 | } |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5455 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5456 | if (doBroadcast) { |
| 5457 | mStartStopCond.broadcast(); |
| 5458 | } |
| 5459 | |
| 5460 | // sleep if there are no active tracks to process |
| 5461 | if (activeTracks.size() == 0) { |
| 5462 | if (sleepUs == 0) { |
| 5463 | sleepUs = kRecordThreadSleepUs; |
| 5464 | } |
| 5465 | continue; |
| 5466 | } |
| 5467 | sleepUs = 0; |
Glenn Kasten | 9e98235 | 2013-08-14 14:39:50 -0700 | [diff] [blame] | 5468 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5469 | lockEffectChains_l(effectChains); |
| 5470 | } |
| 5471 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5472 | // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0 |
Glenn Kasten | 7165268 | 2013-08-14 15:17:55 -0700 | [diff] [blame] | 5473 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5474 | size_t size = effectChains.size(); |
| 5475 | for (size_t i = 0; i < size; i++) { |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5476 | // thread mutex is not locked, but effect chain is locked |
| 5477 | effectChains[i]->process_l(); |
| 5478 | } |
| 5479 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5480 | // 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] | 5481 | if (mFastCapture != 0) { |
| 5482 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5483 | FastCaptureState *state = sq->begin(); |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5484 | bool didModify = false; |
| 5485 | FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5486 | if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME && |
| 5487 | (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) { |
| 5488 | if (state->mCommand == FastCaptureState::COLD_IDLE) { |
| 5489 | int32_t old = android_atomic_inc(&mFastCaptureFutex); |
| 5490 | if (old == -1) { |
| 5491 | (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1); |
| 5492 | } |
| 5493 | } |
| 5494 | state->mCommand = FastCaptureState::READ_WRITE; |
| 5495 | #if 0 // FIXME |
| 5496 | mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ? |
Glenn Kasten | fbdb2ac | 2015-03-02 14:47:19 -0800 | [diff] [blame] | 5497 | FastThreadDumpState::kSamplingNforLowRamDevice : |
| 5498 | FastThreadDumpState::kSamplingN); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5499 | #endif |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5500 | didModify = true; |
| 5501 | } |
| 5502 | audio_track_cblk_t *cblkOld = state->mCblk; |
| 5503 | audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL; |
| 5504 | if (cblkNew != cblkOld) { |
| 5505 | state->mCblk = cblkNew; |
| 5506 | // block until acked if removing a fast track |
| 5507 | if (cblkOld != NULL) { |
| 5508 | block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED; |
| 5509 | } |
| 5510 | didModify = true; |
| 5511 | } |
| 5512 | sq->end(didModify); |
| 5513 | if (didModify) { |
| 5514 | sq->push(block); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5515 | #if 0 |
| 5516 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5517 | mNormalSource = mPipeSource; |
| 5518 | } |
| 5519 | #endif |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
Glenn Kasten | 735f45f | 2014-08-18 15:51:59 -0700 | [diff] [blame] | 5523 | // now run the fast track destructor with thread mutex unlocked |
| 5524 | fastTrackToRemove.clear(); |
| 5525 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5526 | // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one. |
| 5527 | // Only the client(s) that are too slow will overrun. But if even the fastest client is too |
| 5528 | // slow, then this RecordThread will overrun by not calling HAL read often enough. |
| 5529 | // If destination is non-contiguous, first read past the nominal end of buffer, then |
| 5530 | // copy to the right place. Permitted because mRsmpInBuffer was over-allocated. |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5531 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5532 | int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5533 | ssize_t framesRead; |
| 5534 | |
| 5535 | // If an NBAIO source is present, use it to read the normal capture's data |
| 5536 | if (mPipeSource != 0) { |
| 5537 | size_t framesToRead = mBufferSize / mFrameSize; |
| 5538 | framesRead = mPipeSource->read(&mRsmpInBuffer[rear * mChannelCount], |
| 5539 | framesToRead, AudioBufferProvider::kInvalidPTS); |
| 5540 | if (framesRead == 0) { |
| 5541 | // since pipe is non-blocking, simulate blocking input |
| 5542 | sleepUs = (framesToRead * 1000000LL) / mSampleRate; |
| 5543 | } |
| 5544 | // otherwise use the HAL / AudioStreamIn directly |
| 5545 | } else { |
| 5546 | ssize_t bytesRead = mInput->stream->read(mInput->stream, |
| 5547 | &mRsmpInBuffer[rear * mChannelCount], mBufferSize); |
| 5548 | if (bytesRead < 0) { |
| 5549 | framesRead = bytesRead; |
| 5550 | } else { |
| 5551 | framesRead = bytesRead / mFrameSize; |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) { |
| 5556 | ALOGE("read failed: framesRead=%d", framesRead); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5557 | // Force input into standby so that it tries to recover at next read attempt |
| 5558 | inputStandBy(); |
| 5559 | sleepUs = kRecordThreadSleepUs; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5560 | } |
| 5561 | if (framesRead <= 0) { |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5562 | goto unlock; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5563 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5564 | ALOG_ASSERT(framesRead > 0); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5565 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5566 | if (mTeeSink != 0) { |
| 5567 | (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead); |
| 5568 | } |
| 5569 | // 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] | 5570 | { |
| 5571 | size_t part1 = mRsmpInFramesP2 - rear; |
| 5572 | if ((size_t) framesRead > part1) { |
| 5573 | memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount], |
| 5574 | (framesRead - part1) * mFrameSize); |
| 5575 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5576 | } |
| 5577 | rear = mRsmpInRear += framesRead; |
| 5578 | |
| 5579 | size = activeTracks.size(); |
| 5580 | // loop over each active track |
| 5581 | for (size_t i = 0; i < size; i++) { |
| 5582 | activeTrack = activeTracks[i]; |
| 5583 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5584 | // skip fast tracks, as those are handled directly by FastCapture |
| 5585 | if (activeTrack->isFastTrack()) { |
| 5586 | continue; |
| 5587 | } |
| 5588 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5589 | enum { |
| 5590 | OVERRUN_UNKNOWN, |
| 5591 | OVERRUN_TRUE, |
| 5592 | OVERRUN_FALSE |
| 5593 | } overrun = OVERRUN_UNKNOWN; |
| 5594 | |
| 5595 | // loop over getNextBuffer to handle circular sink |
| 5596 | for (;;) { |
| 5597 | |
| 5598 | activeTrack->mSink.frameCount = ~0; |
| 5599 | status_t status = activeTrack->getNextBuffer(&activeTrack->mSink); |
| 5600 | size_t framesOut = activeTrack->mSink.frameCount; |
| 5601 | LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0)); |
| 5602 | |
| 5603 | int32_t front = activeTrack->mRsmpInFront; |
| 5604 | ssize_t filled = rear - front; |
| 5605 | size_t framesIn; |
| 5606 | |
| 5607 | if (filled < 0) { |
| 5608 | // should not happen, but treat like a massive overrun and re-sync |
| 5609 | framesIn = 0; |
| 5610 | activeTrack->mRsmpInFront = rear; |
| 5611 | overrun = OVERRUN_TRUE; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5612 | } else if ((size_t) filled <= mRsmpInFrames) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5613 | framesIn = (size_t) filled; |
| 5614 | } else { |
| 5615 | // client is not keeping up with server, but give it latest data |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5616 | framesIn = mRsmpInFrames; |
| 5617 | activeTrack->mRsmpInFront = front = rear - framesIn; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5618 | overrun = OVERRUN_TRUE; |
| 5619 | } |
| 5620 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5621 | if (framesOut == 0 || framesIn == 0) { |
| 5622 | break; |
| 5623 | } |
| 5624 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5625 | if (activeTrack->mResampler == NULL) { |
| 5626 | // no resampling |
| 5627 | if (framesIn > framesOut) { |
| 5628 | framesIn = framesOut; |
| 5629 | } else { |
| 5630 | framesOut = framesIn; |
| 5631 | } |
| 5632 | int8_t *dst = activeTrack->mSink.i8; |
| 5633 | while (framesIn > 0) { |
| 5634 | front &= mRsmpInFramesP2 - 1; |
| 5635 | size_t part1 = mRsmpInFramesP2 - front; |
| 5636 | if (part1 > framesIn) { |
| 5637 | part1 = framesIn; |
| 5638 | } |
| 5639 | int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize); |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5640 | if (mChannelCount == activeTrack->mChannelCount) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5641 | memcpy(dst, src, part1 * mFrameSize); |
| 5642 | } else if (mChannelCount == 1) { |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5643 | upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, (const int16_t *)src, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5644 | part1); |
| 5645 | } else { |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 5646 | downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, |
| 5647 | (const int16_t *)src, part1); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5648 | } |
| 5649 | dst += part1 * activeTrack->mFrameSize; |
| 5650 | front += part1; |
| 5651 | framesIn -= part1; |
| 5652 | } |
| 5653 | activeTrack->mRsmpInFront += framesOut; |
| 5654 | |
| 5655 | } else { |
| 5656 | // resampling |
| 5657 | // FIXME framesInNeeded should really be part of resampler API, and should |
| 5658 | // depend on the SRC ratio |
| 5659 | // to keep mRsmpInBuffer full so resampler always has sufficient input |
| 5660 | size_t framesInNeeded; |
| 5661 | // FIXME only re-calculate when it changes, and optimize for common ratios |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5662 | // Do not precompute in/out because floating point is not associative |
| 5663 | // e.g. a*b/c != a*(b/c). |
| 5664 | const double in(mSampleRate); |
| 5665 | const double out(activeTrack->mSampleRate); |
| 5666 | framesInNeeded = ceil(framesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5667 | ALOGV("need %u frames in to produce %u out given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5668 | framesInNeeded, framesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5669 | // Although we theoretically have framesIn in circular buffer, some of those are |
| 5670 | // unreleased frames, and thus must be discounted for purpose of budgeting. |
| 5671 | size_t unreleased = activeTrack->mRsmpInUnrel; |
| 5672 | framesIn = framesIn > unreleased ? framesIn - unreleased : 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5673 | if (framesIn < framesInNeeded) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5674 | ALOGV("not enough to resample: have %u frames in but need %u in to " |
| 5675 | "produce %u out given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5676 | framesIn, framesInNeeded, framesOut, in / out); |
| 5677 | size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * out / in) : 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5678 | LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut); |
| 5679 | if (newFramesOut == 0) { |
| 5680 | break; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5681 | } |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5682 | framesInNeeded = ceil(newFramesOut * in / out) + 1; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5683 | ALOGV("now need %u frames in to produce %u out given out/in ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5684 | framesInNeeded, newFramesOut, out / in); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5685 | LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded); |
| 5686 | ALOGV("success 2: have %u frames in and need %u in to produce %u out " |
| 5687 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5688 | framesIn, framesInNeeded, newFramesOut, in / out); |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5689 | framesOut = newFramesOut; |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5690 | } else { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5691 | ALOGV("success 1: have %u in and need %u in to produce %u out " |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5692 | "given in/out ratio of %.4g", |
Andy Hung | 8661aaf | 2014-07-28 14:38:41 -0700 | [diff] [blame] | 5693 | framesIn, framesInNeeded, framesOut, in / out); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5694 | } |
| 5695 | |
| 5696 | // reallocate mRsmpOutBuffer as needed; we will grow but never shrink |
| 5697 | if (activeTrack->mRsmpOutFrameCount < framesOut) { |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5698 | // FIXME why does each track need it's own mRsmpOutBuffer? can't they share? |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5699 | delete[] activeTrack->mRsmpOutBuffer; |
| 5700 | // resampler always outputs stereo |
| 5701 | activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2]; |
| 5702 | activeTrack->mRsmpOutFrameCount = framesOut; |
| 5703 | } |
| 5704 | |
| 5705 | // resampler accumulates, but we only have one source track |
| 5706 | memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t)); |
| 5707 | activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut, |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 5708 | // FIXME how about having activeTrack implement this interface itself? |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5709 | activeTrack->mResamplerBufferProvider |
| 5710 | /*this*/ /* AudioBufferProvider* */); |
| 5711 | // ditherAndClamp() works as long as all buffers returned by |
| 5712 | // activeTrack->getNextBuffer() are 32 bit aligned which should be always true. |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5713 | if (activeTrack->mChannelCount == 1) { |
Andy Hung | 84a0c6e | 2014-04-02 11:24:53 -0700 | [diff] [blame] | 5714 | // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5715 | ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer, |
| 5716 | framesOut); |
| 5717 | // the resampler always outputs stereo samples: |
| 5718 | // do post stereo to mono conversion |
| 5719 | downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16, |
Glenn Kasten | cd70421 | 2014-07-14 17:26:36 -0700 | [diff] [blame] | 5720 | (const int16_t *)activeTrack->mRsmpOutBuffer, framesOut); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5721 | } else { |
| 5722 | ditherAndClamp((int32_t *)activeTrack->mSink.raw, |
| 5723 | activeTrack->mRsmpOutBuffer, framesOut); |
| 5724 | } |
| 5725 | // now done with mRsmpOutBuffer |
| 5726 | |
| 5727 | } |
| 5728 | |
| 5729 | if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) { |
| 5730 | overrun = OVERRUN_FALSE; |
| 5731 | } |
| 5732 | |
| 5733 | if (activeTrack->mFramesToDrop == 0) { |
| 5734 | if (framesOut > 0) { |
| 5735 | activeTrack->mSink.frameCount = framesOut; |
| 5736 | activeTrack->releaseBuffer(&activeTrack->mSink); |
| 5737 | } |
| 5738 | } else { |
| 5739 | // FIXME could do a partial drop of framesOut |
| 5740 | if (activeTrack->mFramesToDrop > 0) { |
| 5741 | activeTrack->mFramesToDrop -= framesOut; |
| 5742 | if (activeTrack->mFramesToDrop <= 0) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5743 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5744 | } |
| 5745 | } else { |
| 5746 | activeTrack->mFramesToDrop += framesOut; |
| 5747 | if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 || |
| 5748 | activeTrack->mSyncStartEvent->isCancelled()) { |
| 5749 | ALOGW("Synced record %s, session %d, trigger session %d", |
| 5750 | (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled", |
| 5751 | activeTrack->sessionId(), |
| 5752 | (activeTrack->mSyncStartEvent != 0) ? |
| 5753 | activeTrack->mSyncStartEvent->triggerSession() : 0); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5754 | activeTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5755 | } |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | if (framesOut == 0) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5760 | break; |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5761 | } |
| 5762 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5763 | |
| 5764 | switch (overrun) { |
| 5765 | case OVERRUN_TRUE: |
| 5766 | // client isn't retrieving buffers fast enough |
| 5767 | if (!activeTrack->setOverflow()) { |
| 5768 | nsecs_t now = systemTime(); |
| 5769 | // FIXME should lastWarning per track? |
| 5770 | if ((now - lastWarning) > kWarningThrottleNs) { |
| 5771 | ALOGW("RecordThread: buffer overflow"); |
| 5772 | lastWarning = now; |
| 5773 | } |
| 5774 | } |
| 5775 | break; |
| 5776 | case OVERRUN_FALSE: |
| 5777 | activeTrack->clearOverflow(); |
| 5778 | break; |
| 5779 | case OVERRUN_UNKNOWN: |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5780 | break; |
| 5781 | } |
| 5782 | |
Glenn Kasten | 1ba19cd | 2013-08-14 14:02:21 -0700 | [diff] [blame] | 5783 | } |
| 5784 | |
Glenn Kasten | 3d61bc1 | 2014-06-16 10:25:20 -0700 | [diff] [blame] | 5785 | unlock: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5786 | // enable changes in effect chain |
| 5787 | unlockEffectChains(effectChains); |
Glenn Kasten | c527a7c | 2013-08-13 15:43:49 -0700 | [diff] [blame] | 5788 | // 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] | 5789 | } |
| 5790 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5791 | standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5792 | |
| 5793 | { |
| 5794 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 5795 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 5796 | sp<RecordTrack> track = mTracks[i]; |
| 5797 | track->invalidate(); |
| 5798 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 5799 | mActiveTracks.clear(); |
| 5800 | mActiveTracksGen++; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5801 | mStartStopCond.broadcast(); |
| 5802 | } |
| 5803 | |
| 5804 | releaseWakeLock(); |
| 5805 | |
| 5806 | ALOGV("RecordThread %p exiting", this); |
| 5807 | return false; |
| 5808 | } |
| 5809 | |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 5810 | void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5811 | { |
| 5812 | if (!mStandby) { |
| 5813 | inputStandBy(); |
| 5814 | mStandby = true; |
| 5815 | } |
| 5816 | } |
| 5817 | |
| 5818 | void AudioFlinger::RecordThread::inputStandBy() |
| 5819 | { |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5820 | // Idle the fast capture if it's currently running |
| 5821 | if (mFastCapture != 0) { |
| 5822 | FastCaptureStateQueue *sq = mFastCapture->sq(); |
| 5823 | FastCaptureState *state = sq->begin(); |
| 5824 | if (!(state->mCommand & FastCaptureState::IDLE)) { |
| 5825 | state->mCommand = FastCaptureState::COLD_IDLE; |
| 5826 | state->mColdFutexAddr = &mFastCaptureFutex; |
| 5827 | state->mColdGen++; |
| 5828 | mFastCaptureFutex = 0; |
| 5829 | sq->end(); |
| 5830 | // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now |
| 5831 | sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED); |
| 5832 | #if 0 |
| 5833 | if (kUseFastCapture == FastCapture_Dynamic) { |
| 5834 | // FIXME |
| 5835 | } |
| 5836 | #endif |
| 5837 | #ifdef AUDIO_WATCHDOG |
| 5838 | // FIXME |
| 5839 | #endif |
| 5840 | } else { |
| 5841 | sq->end(false /*didModify*/); |
| 5842 | } |
| 5843 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5844 | mInput->stream->common.standby(&mInput->stream->common); |
| 5845 | } |
| 5846 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5847 | // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 5848 | sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5849 | const sp<AudioFlinger::Client>& client, |
| 5850 | uint32_t sampleRate, |
| 5851 | audio_format_t format, |
| 5852 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5853 | size_t *pFrameCount, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5854 | int sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 5855 | size_t *notificationFrames, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 5856 | int uid, |
Glenn Kasten | ddb0ccf | 2013-07-31 16:14:50 -0700 | [diff] [blame] | 5857 | IAudioFlinger::track_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5858 | pid_t tid, |
| 5859 | status_t *status) |
| 5860 | { |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5861 | size_t frameCount = *pFrameCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5862 | sp<RecordTrack> track; |
| 5863 | status_t lStatus; |
| 5864 | |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5865 | // client expresses a preference for FAST, but we get the final say |
| 5866 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5867 | if ( |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5868 | // use case: callback handler |
| 5869 | (tid != -1) && |
| 5870 | // frame count is not specified, or is exactly the pipe depth |
| 5871 | ((frameCount == 0) || (frameCount == mPipeFramesP2)) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5872 | // PCM data |
| 5873 | audio_is_linear_pcm(format) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5874 | // native format |
| 5875 | (format == mFormat) && |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5876 | // native channel mask |
| 5877 | (channelMask == mChannelMask) && |
| 5878 | // native hardware sample rate |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5879 | (sampleRate == mSampleRate) && |
Glenn Kasten | 3a6c90a | 2014-03-13 15:07:51 -0700 | [diff] [blame] | 5880 | // record thread has an associated fast capture |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5881 | hasFastCapture() && |
| 5882 | // there are sufficient fast track slots available |
| 5883 | mFastTrackAvail |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5884 | ) { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5885 | ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u", |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5886 | frameCount, mFrameCount); |
| 5887 | } else { |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5888 | ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u " |
| 5889 | "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u " |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 5890 | "hasFastCapture=%d tid=%d mFastTrackAvail=%d", |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5891 | frameCount, mFrameCount, mPipeFramesP2, |
| 5892 | format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate, |
| 5893 | hasFastCapture(), tid, mFastTrackAvail); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5894 | *flags &= ~IAudioFlinger::TRACK_FAST; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | // compute track buffer size in frames, and suggest the notification frame count |
| 5899 | if (*flags & IAudioFlinger::TRACK_FAST) { |
| 5900 | // fast track: frame count is exactly the pipe depth |
| 5901 | frameCount = mPipeFramesP2; |
| 5902 | // ignore requested notificationFrames, and always notify exactly once every HAL buffer |
| 5903 | *notificationFrames = mFrameCount; |
| 5904 | } else { |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 5905 | // not fast track: max notification period is resampled equivalent of one HAL buffer time |
| 5906 | // or 20 ms if there is a fast capture |
| 5907 | // TODO This could be a roundupRatio inline, and const |
| 5908 | size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount) |
| 5909 | * sampleRate + mSampleRate - 1) / mSampleRate; |
| 5910 | // minimum number of notification periods is at least kMinNotifications, |
| 5911 | // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs) |
| 5912 | static const size_t kMinNotifications = 3; |
| 5913 | static const uint32_t kMinMs = 30; |
| 5914 | // TODO This could be a roundupRatio inline |
| 5915 | const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000; |
| 5916 | // TODO This could be a roundupRatio inline |
| 5917 | const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) / |
| 5918 | maxNotificationFrames; |
| 5919 | const size_t minFrameCount = maxNotificationFrames * |
| 5920 | max(kMinNotifications, minNotificationsByMs); |
| 5921 | frameCount = max(frameCount, minFrameCount); |
| 5922 | if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) { |
| 5923 | *notificationFrames = maxNotificationFrames; |
Glenn Kasten | 7410591 | 2014-07-03 12:28:53 -0700 | [diff] [blame] | 5924 | } |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5925 | } |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 5926 | *pFrameCount = frameCount; |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5927 | |
Glenn Kasten | 15e5798 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 5928 | lStatus = initCheck(); |
| 5929 | if (lStatus != NO_ERROR) { |
| 5930 | ALOGE("createRecordTrack_l() audio driver not initialized"); |
| 5931 | goto Exit; |
| 5932 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5933 | |
| 5934 | { // scope for mLock |
| 5935 | Mutex::Autolock _l(mLock); |
| 5936 | |
| 5937 | track = new RecordTrack(this, client, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5938 | format, channelMask, frameCount, NULL, sessionId, uid, |
| 5939 | *flags, TrackBase::TYPE_DEFAULT); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5940 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 5941 | lStatus = track->initCheck(); |
| 5942 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 3529507 | 2013-10-07 09:27:06 -0700 | [diff] [blame] | 5943 | ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 5944 | // 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] | 5945 | goto Exit; |
| 5946 | } |
| 5947 | mTracks.add(track); |
| 5948 | |
| 5949 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 5950 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 5951 | mAudioFlinger->btNrecIsOff(); |
| 5952 | setEffectSuspended_l(FX_IID_AEC, suspend, sessionId); |
| 5953 | setEffectSuspended_l(FX_IID_NS, suspend, sessionId); |
Glenn Kasten | 90e58b1 | 2013-07-31 16:16:02 -0700 | [diff] [blame] | 5954 | |
| 5955 | if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) { |
| 5956 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 5957 | // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful, |
| 5958 | // so ask activity manager to do this on our behalf |
| 5959 | sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp); |
| 5960 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5961 | } |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 5962 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5963 | lStatus = NO_ERROR; |
| 5964 | |
| 5965 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 5966 | *status = lStatus; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5967 | return track; |
| 5968 | } |
| 5969 | |
| 5970 | status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, |
| 5971 | AudioSystem::sync_event_t event, |
| 5972 | int triggerSession) |
| 5973 | { |
| 5974 | ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession); |
| 5975 | sp<ThreadBase> strongMe = this; |
| 5976 | status_t status = NO_ERROR; |
| 5977 | |
| 5978 | if (event == AudioSystem::SYNC_EVENT_NONE) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5979 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5980 | } else if (event != AudioSystem::SYNC_EVENT_SAME) { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5981 | recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5982 | triggerSession, |
| 5983 | recordTrack->sessionId(), |
| 5984 | syncStartEventCallback, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5985 | recordTrack); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5986 | // Sync event can be cancelled by the trigger session if the track is not in a |
| 5987 | // compatible state in which case we start record immediately |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5988 | if (recordTrack->mSyncStartEvent->isCancelled()) { |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 5989 | recordTrack->clearSyncStartEvent(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5990 | } else { |
| 5991 | // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 5992 | recordTrack->mFramesToDrop = - |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 5993 | ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5994 | } |
| 5995 | } |
| 5996 | |
| 5997 | { |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 5998 | // This section is a rendezvous between binder thread executing start() and RecordThread |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 5999 | AutoMutex lock(mLock); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6000 | if (mActiveTracks.indexOf(recordTrack) >= 0) { |
| 6001 | if (recordTrack->mState == TrackBase::PAUSING) { |
| 6002 | ALOGV("active record track PAUSING -> ACTIVE"); |
Glenn Kasten | f10ffec | 2013-11-20 16:40:08 -0800 | [diff] [blame] | 6003 | recordTrack->mState = TrackBase::ACTIVE; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6004 | } else { |
| 6005 | ALOGV("active record track state %d", recordTrack->mState); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6006 | } |
| 6007 | return status; |
| 6008 | } |
| 6009 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6010 | // TODO consider other ways of handling this, such as changing the state to :STARTING and |
| 6011 | // adding the track to mActiveTracks after returning from AudioSystem::startInput(), |
| 6012 | // or using a separate command thread |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6013 | recordTrack->mState = TrackBase::STARTING_1; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6014 | mActiveTracks.add(recordTrack); |
| 6015 | mActiveTracksGen++; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6016 | status_t status = NO_ERROR; |
| 6017 | if (recordTrack->isExternalTrack()) { |
| 6018 | mLock.unlock(); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6019 | status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6020 | mLock.lock(); |
| 6021 | // FIXME should verify that recordTrack is still in mActiveTracks |
| 6022 | if (status != NO_ERROR) { |
| 6023 | mActiveTracks.remove(recordTrack); |
| 6024 | mActiveTracksGen++; |
| 6025 | recordTrack->clearSyncStartEvent(); |
| 6026 | ALOGV("RecordThread::start error %d", status); |
| 6027 | return status; |
| 6028 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6029 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6030 | // Catch up with current buffer indices if thread is already running. |
| 6031 | // This is what makes a new client discard all buffered data. If the track's mRsmpInFront |
| 6032 | // was initialized to some value closer to the thread's mRsmpInFront, then the track could |
| 6033 | // see previously buffered data before it called start(), but with greater risk of overrun. |
| 6034 | |
| 6035 | recordTrack->mRsmpInFront = mRsmpInRear; |
| 6036 | recordTrack->mRsmpInUnrel = 0; |
| 6037 | // FIXME why reset? |
| 6038 | if (recordTrack->mResampler != NULL) { |
| 6039 | recordTrack->mResampler->reset(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6040 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6041 | recordTrack->mState = TrackBase::STARTING_2; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6042 | // signal thread to start |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6043 | mWaitWorkCV.broadcast(); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6044 | if (mActiveTracks.indexOf(recordTrack) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6045 | ALOGV("Record failed to start"); |
| 6046 | status = BAD_VALUE; |
| 6047 | goto startError; |
| 6048 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6049 | return status; |
| 6050 | } |
Glenn Kasten | 7c02724 | 2012-12-26 14:43:16 -0800 | [diff] [blame] | 6051 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6052 | startError: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6053 | if (recordTrack->isExternalTrack()) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 6054 | AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6055 | } |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 6056 | recordTrack->clearSyncStartEvent(); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6057 | // FIXME I wonder why we do not reset the state here? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6058 | return status; |
| 6059 | } |
| 6060 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6061 | void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event) |
| 6062 | { |
| 6063 | sp<SyncEvent> strongEvent = event.promote(); |
| 6064 | |
| 6065 | if (strongEvent != 0) { |
Eric Laurent | 8ea16e4 | 2014-02-20 16:26:11 -0800 | [diff] [blame] | 6066 | sp<RefBase> ptr = strongEvent->cookie().promote(); |
| 6067 | if (ptr != 0) { |
| 6068 | RecordTrack *recordTrack = (RecordTrack *)ptr.get(); |
| 6069 | recordTrack->handleSyncStartEvent(strongEvent); |
| 6070 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6071 | } |
| 6072 | } |
| 6073 | |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6074 | bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6075 | ALOGV("RecordThread::stop"); |
Glenn Kasten | a8356f6 | 2013-07-25 14:37:52 -0700 | [diff] [blame] | 6076 | AutoMutex _l(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6077 | if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6078 | return false; |
| 6079 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6080 | // 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] | 6081 | recordTrack->mState = TrackBase::PAUSING; |
| 6082 | // do not wait for mStartStopCond if exiting |
| 6083 | if (exitPending()) { |
| 6084 | return true; |
| 6085 | } |
Glenn Kasten | 47c2070 | 2013-08-13 15:37:35 -0700 | [diff] [blame] | 6086 | // FIXME incorrect usage of wait: no explicit predicate or loop |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6087 | mStartStopCond.wait(mLock); |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6088 | // if we have been restarted, recordTrack is in mActiveTracks here |
| 6089 | if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6090 | ALOGV("Record stopped OK"); |
| 6091 | return true; |
| 6092 | } |
| 6093 | return false; |
| 6094 | } |
| 6095 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6096 | bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6097 | { |
| 6098 | return false; |
| 6099 | } |
| 6100 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6101 | status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6102 | { |
| 6103 | #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future |
| 6104 | if (!isValidSyncEvent(event)) { |
| 6105 | return BAD_VALUE; |
| 6106 | } |
| 6107 | |
| 6108 | int eventSession = event->triggerSession(); |
| 6109 | status_t ret = NAME_NOT_FOUND; |
| 6110 | |
| 6111 | Mutex::Autolock _l(mLock); |
| 6112 | |
| 6113 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6114 | sp<RecordTrack> track = mTracks[i]; |
| 6115 | if (eventSession == track->sessionId()) { |
| 6116 | (void) track->setSyncEvent(event); |
| 6117 | ret = NO_ERROR; |
| 6118 | } |
| 6119 | } |
| 6120 | return ret; |
| 6121 | #else |
| 6122 | return BAD_VALUE; |
| 6123 | #endif |
| 6124 | } |
| 6125 | |
| 6126 | // destroyTrack_l() must be called with ThreadBase::mLock held |
| 6127 | void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track) |
| 6128 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 6129 | track->terminate(); |
| 6130 | track->mState = TrackBase::STOPPED; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6131 | // active tracks are removed by threadLoop() |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6132 | if (mActiveTracks.indexOf(track) < 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6133 | removeTrack_l(track); |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track) |
| 6138 | { |
| 6139 | mTracks.remove(track); |
| 6140 | // need anything related to effects here? |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6141 | if (track->isFastTrack()) { |
| 6142 | ALOG_ASSERT(!mFastTrackAvail); |
| 6143 | mFastTrackAvail = true; |
| 6144 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | void AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args) |
| 6148 | { |
| 6149 | dumpInternals(fd, args); |
| 6150 | dumpTracks(fd, args); |
| 6151 | dumpEffectChains(fd, args); |
| 6152 | } |
| 6153 | |
| 6154 | void AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args) |
| 6155 | { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6156 | dprintf(fd, "\nInput thread %p:\n", this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6157 | |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6158 | if (mActiveTracks.size() > 0) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6159 | dprintf(fd, " Buffer size: %zu bytes\n", mBufferSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6160 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6161 | dprintf(fd, " No active record clients\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6162 | } |
Glenn Kasten | 6e6704c | 2014-07-03 10:20:00 -0700 | [diff] [blame] | 6163 | dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no"); |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 6164 | dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6165 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6166 | dumpBase(fd, args); |
| 6167 | } |
| 6168 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 6169 | void AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6170 | { |
| 6171 | const size_t SIZE = 256; |
| 6172 | char buffer[SIZE]; |
| 6173 | String8 result; |
| 6174 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6175 | size_t numtracks = mTracks.size(); |
| 6176 | size_t numactive = mActiveTracks.size(); |
| 6177 | size_t numactiveseen = 0; |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6178 | dprintf(fd, " %d Tracks", numtracks); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6179 | if (numtracks) { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6180 | dprintf(fd, " of which %d are active\n", numactive); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6181 | RecordTrack::appendDumpHeader(result); |
| 6182 | for (size_t i = 0; i < numtracks ; ++i) { |
| 6183 | sp<RecordTrack> track = mTracks[i]; |
| 6184 | if (track != 0) { |
| 6185 | bool active = mActiveTracks.indexOf(track) >= 0; |
| 6186 | if (active) { |
| 6187 | numactiveseen++; |
| 6188 | } |
| 6189 | track->dump(buffer, SIZE, active); |
| 6190 | result.append(buffer); |
| 6191 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6192 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6193 | } else { |
Elliott Hughes | 87cebad | 2014-05-22 10:14:43 -0700 | [diff] [blame] | 6194 | dprintf(fd, "\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6195 | } |
| 6196 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6197 | if (numactiveseen != numactive) { |
| 6198 | snprintf(buffer, SIZE, " The following tracks are in the active list but" |
| 6199 | " not in the track list\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6200 | result.append(buffer); |
| 6201 | RecordTrack::appendDumpHeader(result); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6202 | for (size_t i = 0; i < numactive; ++i) { |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6203 | sp<RecordTrack> track = mActiveTracks[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 6204 | if (mTracks.indexOf(track) < 0) { |
| 6205 | track->dump(buffer, SIZE, true); |
| 6206 | result.append(buffer); |
| 6207 | } |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 6208 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6209 | |
| 6210 | } |
| 6211 | write(fd, result.string(), result.size()); |
| 6212 | } |
| 6213 | |
| 6214 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6215 | status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer( |
| 6216 | AudioBufferProvider::Buffer* buffer, int64_t pts __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6217 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6218 | RecordTrack *activeTrack = mRecordTrack; |
| 6219 | sp<ThreadBase> threadBase = activeTrack->mThread.promote(); |
| 6220 | if (threadBase == 0) { |
| 6221 | buffer->frameCount = 0; |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6222 | buffer->raw = NULL; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6223 | return NOT_ENOUGH_DATA; |
| 6224 | } |
| 6225 | RecordThread *recordThread = (RecordThread *) threadBase.get(); |
| 6226 | int32_t rear = recordThread->mRsmpInRear; |
| 6227 | int32_t front = activeTrack->mRsmpInFront; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6228 | ssize_t filled = rear - front; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6229 | // FIXME should not be P2 (don't want to increase latency) |
| 6230 | // FIXME if client not keeping up, discard |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6231 | LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames)); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6232 | // 'filled' may be non-contiguous, so return only the first contiguous chunk |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6233 | front &= recordThread->mRsmpInFramesP2 - 1; |
| 6234 | size_t part1 = recordThread->mRsmpInFramesP2 - front; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6235 | if (part1 > (size_t) filled) { |
| 6236 | part1 = filled; |
| 6237 | } |
| 6238 | size_t ask = buffer->frameCount; |
| 6239 | ALOG_ASSERT(ask > 0); |
| 6240 | if (part1 > ask) { |
| 6241 | part1 = ask; |
| 6242 | } |
| 6243 | if (part1 == 0) { |
| 6244 | // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty |
Glenn Kasten | 607fa3e | 2014-02-21 14:24:58 -0800 | [diff] [blame] | 6245 | LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved"); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6246 | buffer->raw = NULL; |
| 6247 | buffer->frameCount = 0; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6248 | activeTrack->mRsmpInUnrel = 0; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6249 | return NOT_ENOUGH_DATA; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6250 | } |
| 6251 | |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6252 | buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6253 | buffer->frameCount = part1; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6254 | activeTrack->mRsmpInUnrel = part1; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6255 | return NO_ERROR; |
| 6256 | } |
| 6257 | |
| 6258 | // AudioBufferProvider interface |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6259 | void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer( |
| 6260 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6261 | { |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6262 | RecordTrack *activeTrack = mRecordTrack; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6263 | size_t stepCount = buffer->frameCount; |
| 6264 | if (stepCount == 0) { |
| 6265 | return; |
| 6266 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6267 | ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel); |
| 6268 | activeTrack->mRsmpInUnrel -= stepCount; |
| 6269 | activeTrack->mRsmpInFront += stepCount; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6270 | buffer->raw = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6271 | buffer->frameCount = 0; |
| 6272 | } |
| 6273 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6274 | bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair, |
| 6275 | status_t& status) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6276 | { |
| 6277 | bool reconfig = false; |
| 6278 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6279 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6280 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6281 | audio_format_t reqFormat = mFormat; |
| 6282 | uint32_t samplingRate = mSampleRate; |
| 6283 | audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount); |
| 6284 | |
| 6285 | AudioParameter param = AudioParameter(keyValuePair); |
| 6286 | int value; |
| 6287 | // TODO Investigate when this code runs. Check with audio policy when a sample rate and |
| 6288 | // channel count change can be requested. Do we mandate the first client defines the |
| 6289 | // HAL sampling rate and channel count or do we allow changes on the fly? |
| 6290 | if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) { |
| 6291 | samplingRate = value; |
| 6292 | reconfig = true; |
| 6293 | } |
| 6294 | if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) { |
| 6295 | if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) { |
| 6296 | status = BAD_VALUE; |
| 6297 | } else { |
| 6298 | reqFormat = (audio_format_t) value; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6299 | reconfig = true; |
| 6300 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6301 | } |
| 6302 | if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) { |
| 6303 | audio_channel_mask_t mask = (audio_channel_mask_t) value; |
| 6304 | if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) { |
| 6305 | status = BAD_VALUE; |
| 6306 | } else { |
| 6307 | channelMask = mask; |
| 6308 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6309 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6310 | } |
| 6311 | if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) { |
| 6312 | // do not accept frame count changes if tracks are open as the track buffer |
| 6313 | // size depends on frame count and correct behavior would not be guaranteed |
| 6314 | // if frame count is changed after track creation |
| 6315 | if (mActiveTracks.size() > 0) { |
| 6316 | status = INVALID_OPERATION; |
| 6317 | } else { |
| 6318 | reconfig = true; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6319 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6320 | } |
| 6321 | if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) { |
| 6322 | // forward device change to effects that have requested to be |
| 6323 | // aware of attached audio device. |
| 6324 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6325 | mEffectChains[i]->setDevice_l(value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6326 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6327 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6328 | // store input device and output device but do not forward output device to audio HAL. |
| 6329 | // Note that status is ignored by the caller for output device |
| 6330 | // (see AudioFlinger::setParameters() |
| 6331 | if (audio_is_output_devices(value)) { |
| 6332 | mOutDevice = value; |
| 6333 | status = BAD_VALUE; |
| 6334 | } else { |
| 6335 | mInDevice = value; |
| 6336 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 6337 | // pre processings |
| 6338 | if (mTracks.size() > 0) { |
| 6339 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6340 | mAudioFlinger->btNrecIsOff(); |
| 6341 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6342 | sp<RecordTrack> track = mTracks[i]; |
| 6343 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 6344 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6345 | } |
| 6346 | } |
| 6347 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6348 | } |
| 6349 | if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR && |
| 6350 | mAudioSource != (audio_source_t)value) { |
| 6351 | // forward device change to effects that have requested to be |
| 6352 | // aware of attached audio device. |
| 6353 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6354 | mEffectChains[i]->setAudioSource_l((audio_source_t)value); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6355 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6356 | mAudioSource = (audio_source_t)value; |
| 6357 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 6358 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6359 | if (status == NO_ERROR) { |
| 6360 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6361 | keyValuePair.string()); |
| 6362 | if (status == INVALID_OPERATION) { |
| 6363 | inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6364 | status = mInput->stream->common.set_parameters(&mInput->stream->common, |
| 6365 | keyValuePair.string()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6366 | } |
| 6367 | if (reconfig) { |
| 6368 | if (status == BAD_VALUE && |
| 6369 | reqFormat == mInput->stream->common.get_format(&mInput->stream->common) && |
| 6370 | reqFormat == AUDIO_FORMAT_PCM_16_BIT && |
| 6371 | (mInput->stream->common.get_sample_rate(&mInput->stream->common) |
| 6372 | <= (2 * samplingRate)) && |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6373 | audio_channel_count_from_in_mask( |
| 6374 | mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 && |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6375 | (channelMask == AUDIO_CHANNEL_IN_MONO || |
| 6376 | channelMask == AUDIO_CHANNEL_IN_STEREO)) { |
| 6377 | status = NO_ERROR; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6378 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6379 | if (status == NO_ERROR) { |
| 6380 | readInputParameters_l(); |
| 6381 | sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6382 | } |
| 6383 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6384 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 6385 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6386 | return reconfig; |
| 6387 | } |
| 6388 | |
| 6389 | String8 AudioFlinger::RecordThread::getParameters(const String8& keys) |
| 6390 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6391 | Mutex::Autolock _l(mLock); |
| 6392 | if (initCheck() != NO_ERROR) { |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6393 | return String8(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6394 | } |
| 6395 | |
Glenn Kasten | d8ea699 | 2013-07-16 14:17:15 -0700 | [diff] [blame] | 6396 | char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string()); |
| 6397 | const String8 out_s8(s); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6398 | free(s); |
| 6399 | return out_s8; |
| 6400 | } |
| 6401 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6402 | void AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6403 | AudioSystem::OutputDescriptor desc; |
Glenn Kasten | b2737d0 | 2013-08-19 12:03:11 -0700 | [diff] [blame] | 6404 | const void *param2 = NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6405 | |
| 6406 | switch (event) { |
| 6407 | case AudioSystem::INPUT_OPENED: |
| 6408 | case AudioSystem::INPUT_CONFIG_CHANGED: |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 6409 | desc.channelMask = mChannelMask; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6410 | desc.samplingRate = mSampleRate; |
| 6411 | desc.format = mFormat; |
| 6412 | desc.frameCount = mFrameCount; |
| 6413 | desc.latency = 0; |
| 6414 | param2 = &desc; |
| 6415 | break; |
| 6416 | |
| 6417 | case AudioSystem::INPUT_CLOSED: |
| 6418 | default: |
| 6419 | break; |
| 6420 | } |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 6421 | mAudioFlinger->audioConfigChanged(event, mId, param2); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6422 | } |
| 6423 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 6424 | void AudioFlinger::RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6425 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6426 | mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common); |
| 6427 | mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common); |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 6428 | mChannelCount = audio_channel_count_from_in_mask(mChannelMask); |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 6429 | mHALFormat = mInput->stream->common.get_format(&mInput->stream->common); |
| 6430 | mFormat = mHALFormat; |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6431 | if (mFormat != AUDIO_FORMAT_PCM_16_BIT) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 6432 | ALOGE("HAL format %#x not supported; must be AUDIO_FORMAT_PCM_16_BIT", mFormat); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 6433 | } |
Eric Laurent | 665470b | 2014-07-03 16:37:08 -0700 | [diff] [blame] | 6434 | mFrameSize = audio_stream_in_frame_size(mInput->stream); |
Glenn Kasten | 548efc9 | 2012-11-29 08:48:51 -0800 | [diff] [blame] | 6435 | mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common); |
| 6436 | mFrameCount = mBufferSize / mFrameSize; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6437 | // This is the formula for calculating the temporary buffer size. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6438 | // 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] | 6439 | // 1 full output buffer, regardless of the alignment of the available input. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6440 | // The value is somewhat arbitrary, and could probably be even larger. |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6441 | // A larger value should allow more old data to be read after a track calls start(), |
| 6442 | // without increasing latency. |
Glenn Kasten | e842614 | 2014-02-28 16:45:03 -0800 | [diff] [blame] | 6443 | mRsmpInFrames = mFrameCount * 7; |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6444 | mRsmpInFramesP2 = roundup(mRsmpInFrames); |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 6445 | delete[] mRsmpInBuffer; |
Glenn Kasten | 49d00ad | 2014-07-21 11:22:03 -0700 | [diff] [blame] | 6446 | |
| 6447 | // TODO optimize audio capture buffer sizes ... |
| 6448 | // Here we calculate the size of the sliding buffer used as a source |
| 6449 | // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7). |
| 6450 | // For current HAL frame counts, this is usually 2048 = 40 ms. It would |
| 6451 | // be better to have it derived from the pipe depth in the long term. |
| 6452 | // The current value is higher than necessary. However it should not add to latency. |
| 6453 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 6454 | // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer |
| 6455 | mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount]; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6456 | |
Glenn Kasten | 4cc0a6a | 2014-02-17 14:31:46 -0800 | [diff] [blame] | 6457 | // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints. |
| 6458 | // 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] | 6459 | } |
| 6460 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 6461 | uint32_t AudioFlinger::RecordThread::getInputFramesLost() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6462 | { |
| 6463 | Mutex::Autolock _l(mLock); |
| 6464 | if (initCheck() != NO_ERROR) { |
| 6465 | return 0; |
| 6466 | } |
| 6467 | |
| 6468 | return mInput->stream->get_input_frames_lost(mInput->stream); |
| 6469 | } |
| 6470 | |
| 6471 | uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const |
| 6472 | { |
| 6473 | Mutex::Autolock _l(mLock); |
| 6474 | uint32_t result = 0; |
| 6475 | if (getEffectChain_l(sessionId) != 0) { |
| 6476 | result = EFFECT_SESSION; |
| 6477 | } |
| 6478 | |
| 6479 | for (size_t i = 0; i < mTracks.size(); ++i) { |
| 6480 | if (sessionId == mTracks[i]->sessionId()) { |
| 6481 | result |= TRACK_SESSION; |
| 6482 | break; |
| 6483 | } |
| 6484 | } |
| 6485 | |
| 6486 | return result; |
| 6487 | } |
| 6488 | |
| 6489 | KeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const |
| 6490 | { |
| 6491 | KeyedVector<int, bool> ids; |
| 6492 | Mutex::Autolock _l(mLock); |
| 6493 | for (size_t j = 0; j < mTracks.size(); ++j) { |
| 6494 | sp<RecordThread::RecordTrack> track = mTracks[j]; |
| 6495 | int sessionId = track->sessionId(); |
| 6496 | if (ids.indexOfKey(sessionId) < 0) { |
| 6497 | ids.add(sessionId, true); |
| 6498 | } |
| 6499 | } |
| 6500 | return ids; |
| 6501 | } |
| 6502 | |
| 6503 | AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput() |
| 6504 | { |
| 6505 | Mutex::Autolock _l(mLock); |
| 6506 | AudioStreamIn *input = mInput; |
| 6507 | mInput = NULL; |
| 6508 | return input; |
| 6509 | } |
| 6510 | |
| 6511 | // this method must always be called either with ThreadBase mLock held or inside the thread loop |
| 6512 | audio_stream_t* AudioFlinger::RecordThread::stream() const |
| 6513 | { |
| 6514 | if (mInput == NULL) { |
| 6515 | return NULL; |
| 6516 | } |
| 6517 | return &mInput->stream->common; |
| 6518 | } |
| 6519 | |
| 6520 | status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain) |
| 6521 | { |
| 6522 | // only one chain per input thread |
| 6523 | if (mEffectChains.size() != 0) { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 6524 | 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] | 6525 | return INVALID_OPERATION; |
| 6526 | } |
| 6527 | ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 6528 | chain->setThread(this); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6529 | chain->setInBuffer(NULL); |
| 6530 | chain->setOutBuffer(NULL); |
| 6531 | |
| 6532 | checkSuspendOnAddEffectChain_l(chain); |
| 6533 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 6534 | // make sure enabled pre processing effects state is communicated to the HAL as we |
| 6535 | // just moved them to a new input stream. |
| 6536 | chain->syncHalEffectsState(); |
| 6537 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 6538 | mEffectChains.add(chain); |
| 6539 | |
| 6540 | return NO_ERROR; |
| 6541 | } |
| 6542 | |
| 6543 | size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain) |
| 6544 | { |
| 6545 | ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this); |
| 6546 | ALOGW_IF(mEffectChains.size() != 1, |
| 6547 | "removeEffectChain_l() %p invalid chain size %d on thread %p", |
| 6548 | chain.get(), mEffectChains.size(), this); |
| 6549 | if (mEffectChains.size() == 1) { |
| 6550 | mEffectChains.removeAt(0); |
| 6551 | } |
| 6552 | return 0; |
| 6553 | } |
| 6554 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6555 | status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch, |
| 6556 | audio_patch_handle_t *handle) |
| 6557 | { |
| 6558 | status_t status = NO_ERROR; |
| 6559 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6560 | // store new device and send to effects |
| 6561 | mInDevice = patch->sources[0].ext.device.type; |
| 6562 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6563 | mEffectChains[i]->setDevice_l(mInDevice); |
| 6564 | } |
| 6565 | |
| 6566 | // disable AEC and NS if the device is a BT SCO headset supporting those |
| 6567 | // pre processings |
| 6568 | if (mTracks.size() > 0) { |
| 6569 | bool suspend = audio_is_bluetooth_sco_device(mInDevice) && |
| 6570 | mAudioFlinger->btNrecIsOff(); |
| 6571 | for (size_t i = 0; i < mTracks.size(); i++) { |
| 6572 | sp<RecordTrack> track = mTracks[i]; |
| 6573 | setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId()); |
| 6574 | setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId()); |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | // store new source and send to effects |
| 6579 | if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) { |
| 6580 | mAudioSource = patch->sinks[0].ext.mix.usecase.source; |
| 6581 | for (size_t i = 0; i < mEffectChains.size(); i++) { |
| 6582 | mEffectChains[i]->setAudioSource_l(mAudioSource); |
| 6583 | } |
| 6584 | } |
| 6585 | |
| 6586 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6587 | status = hwDevice->create_audio_patch(hwDevice, |
| 6588 | patch->num_sources, |
| 6589 | patch->sources, |
| 6590 | patch->num_sinks, |
| 6591 | patch->sinks, |
| 6592 | handle); |
| 6593 | } else { |
| 6594 | ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL"); |
| 6595 | } |
| 6596 | return status; |
| 6597 | } |
| 6598 | |
| 6599 | status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 6600 | { |
| 6601 | status_t status = NO_ERROR; |
| 6602 | if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 6603 | audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice(); |
| 6604 | status = hwDevice->release_audio_patch(hwDevice, handle); |
| 6605 | } else { |
| 6606 | ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL"); |
| 6607 | } |
| 6608 | return status; |
| 6609 | } |
| 6610 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 6611 | void AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record) |
| 6612 | { |
| 6613 | Mutex::Autolock _l(mLock); |
| 6614 | mTracks.add(record); |
| 6615 | } |
| 6616 | |
| 6617 | void AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record) |
| 6618 | { |
| 6619 | Mutex::Autolock _l(mLock); |
| 6620 | destroyTrack_l(record); |
| 6621 | } |
| 6622 | |
| 6623 | void AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config) |
| 6624 | { |
| 6625 | ThreadBase::getAudioPortConfig(config); |
| 6626 | config->role = AUDIO_PORT_ROLE_SINK; |
| 6627 | config->ext.mix.hw_module = mInput->audioHwDev->handle(); |
| 6628 | config->ext.mix.usecase.source = mAudioSource; |
| 6629 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6630 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 6631 | } // namespace android |